Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2621484rdg; Mon, 16 Oct 2023 09:37:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFB70Z4cxE4N1uGu5huEHHRcqsb3PdjcO1XnC1pSg/wma+k9v+2GaHSgeLUydJUBNJ8y/8E X-Received: by 2002:a17:902:cec8:b0:1ca:7af1:8a85 with SMTP id d8-20020a170902cec800b001ca7af18a85mr3722123plg.57.1697474238564; Mon, 16 Oct 2023 09:37:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697474238; cv=none; d=google.com; s=arc-20160816; b=piQPxcIHB340cS97z842m87n7T/9mV6rXsvfKaQ/PFbAlm26xhhmFDRo+/L4kYJprt zKwZRj0jzhC2fYxcckHnFjEgpXe78/+Emgcc/fJ0brHxhrqv6/V3ONZUbKRe8lqbaG5M +44eymae/JUKF7lW4BAncuHd4XUVhfwZb84Am0eQGW2YCIPgjW4OSVRDc3TfMrHoJjII nyylRBjpfki5747++JaX7xMIwEObGq2Q2kIO4HrhZ1fR8b5CGahLjnaOL+j7jfi8cIKx Vq8ycmB0gEeZH72Fu0dYnd+gFb5IfrVaqRhBdjVfWUQOQuljZMf2zJBiR50ZIIqGbhsD Qw5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jYHCf+k3V810GbirONt7sqtn5yYSG+4EixL/YXwQP3o=; fh=lRdU2Q/1zx5DcPdZuWBjshA5VT5Oc9cEhB1tCFiV0Nw=; b=y1CWbjKCiGhtcrsBT09kXoNvOYvjo8OV1y3TMjksZWwgnSuCWccXgsXllmals3uyE6 SWrCZZ5AF8+A7gBh+DXPufskbQKVCWALBBfJCWPSHUJpCORddf91T5eSUqc7xB1gyyVu s7gP3By8SI6YMK7Y5QoTbHyl4IA7e6uwWWx6iH61ewQfGScflmHLf4eityv0v7AR4mbz RHy4AHK3d+zLO8yk7U8kyGfI2fNxlT7HV3ccuz6Umbqj87FwL7glHzJ6qaQetH6VqNch Qp1/qfE/9PYAmvX0RibnocKo/NkT82fAm/Zlnni/kuSNJuL/HDe/ee3oSgReXpMNhYyO PWwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Qa4QmndU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id s7-20020a170902ea0700b001ca3cd7ead6si5470732plg.452.2023.10.16.09.37.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:37:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Qa4QmndU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 08EB38089E6B; Mon, 16 Oct 2023 09:37:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234265AbjJPQgA (ORCPT + 99 others); Mon, 16 Oct 2023 12:36:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234146AbjJPQff (ORCPT ); Mon, 16 Oct 2023 12:35:35 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 748CC4C19; Mon, 16 Oct 2023 09:21:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473265; x=1729009265; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZkDrmhHprfLg9OVvz2l5tMQiZhG3zk7CJS4j8C0RO8E=; b=Qa4QmndUi3+J5+/BjfVoF11yuMnIGj7W0ni8xGdmCwvgj1ojWIVUyaj3 adYxInVD+47diFl/yBnmSlRKieziyFJBFBbDukFSfFmtWbHqk+GsxK8Hf a1VDsR9f+G0QIZHGsXJNgE1f8e/WEODaslQ29LuGx/kYvwqkVUag0w8z9 4EHqFMRlREWL77BiFXO/plAWIj/Bmf0li6nNo9dEyWOBWSsrlfaAyYRIV trJRpQHmNjY76if3mU5HrtRSS4k6iMt6VEZ1poenT635ynm6D09jXXR1a L39lKeS6KYmxdET7Er+FKPTHEWWKfGdiuHQZ+1/1XYZuFo3jXpiyNL1Id w==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364922055" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364922055" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:16:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="846448310" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="846448310" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:16:05 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v16 093/116] KVM: TDX: Handle TDX PV port io hypercall Date: Mon, 16 Oct 2023 09:14:45 -0700 Message-Id: <1c3db2c798e25f6a144314785600fd07a4b0bf4e.1697471314.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:37:01 -0700 (PDT) From: Isaku Yamahata Wire up TDX PV port IO hypercall to the KVM backend function. Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini --- arch/x86/kvm/vmx/tdx.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 2f8bb0e9394e..fd6907d5ba9d 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1140,6 +1140,61 @@ static int tdx_emulate_hlt(struct kvm_vcpu *vcpu) return kvm_emulate_halt_noskip(vcpu); } +static int tdx_complete_pio_in(struct kvm_vcpu *vcpu) +{ + struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; + unsigned long val = 0; + int ret; + + WARN_ON_ONCE(vcpu->arch.pio.count != 1); + + ret = ctxt->ops->pio_in_emulated(ctxt, vcpu->arch.pio.size, + vcpu->arch.pio.port, &val, 1); + WARN_ON_ONCE(!ret); + + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); + tdvmcall_set_return_val(vcpu, val); + + return 1; +} + +static int tdx_emulate_io(struct kvm_vcpu *vcpu) +{ + struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; + unsigned long val = 0; + unsigned int port; + int size, ret; + bool write; + + ++vcpu->stat.io_exits; + + size = tdvmcall_a0_read(vcpu); + write = tdvmcall_a1_read(vcpu); + port = tdvmcall_a2_read(vcpu); + + if (size != 1 && size != 2 && size != 4) { + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_INVALID_OPERAND); + return 1; + } + + if (write) { + val = tdvmcall_a3_read(vcpu); + ret = ctxt->ops->pio_out_emulated(ctxt, size, port, &val, 1); + + /* No need for a complete_userspace_io callback. */ + vcpu->arch.pio.count = 0; + } else { + ret = ctxt->ops->pio_in_emulated(ctxt, size, port, &val, 1); + if (!ret) + vcpu->arch.complete_userspace_io = tdx_complete_pio_in; + else + tdvmcall_set_return_val(vcpu, val); + } + if (ret) + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); + return ret; +} + static int handle_tdvmcall(struct kvm_vcpu *vcpu) { if (tdvmcall_exit_type(vcpu)) @@ -1150,6 +1205,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu) return tdx_emulate_cpuid(vcpu); case EXIT_REASON_HLT: return tdx_emulate_hlt(vcpu); + case EXIT_REASON_IO_INSTRUCTION: + return tdx_emulate_io(vcpu); default: break; } -- 2.25.1