Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp269321rwb; Tue, 25 Jul 2023 15:46:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlHq3h5KCci8F55pOGA/0oYHmyU1V8yUL7gp3dsZtPSTsSoevg8GMPz9+0pJRCbwmitDSUfX X-Received: by 2002:a05:6358:429e:b0:131:b4c:b876 with SMTP id s30-20020a056358429e00b001310b4cb876mr145342rwc.1.1690325188356; Tue, 25 Jul 2023 15:46:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690325188; cv=none; d=google.com; s=arc-20160816; b=RtwHWRkdpUMSo7jV+NOE+2DBStOz0U/FCA9mQXfDAr6niL/8aSX49kSt0t+O434Jxd NuIcUzd8maS5LeuQGKvT5Y4iOEH9P6U38I/aNuemyXeTAvoKogft/SQpMzPbYSXGY9x1 GCIuYXt2/VY9xls42itn2jLpe2jnkR4Z9dX4uiRJOhUxDi7jdzxxWsnHHwLxtTmZGk87 axDLkui6S05X+7G5an3CW7n0BOP8QIkQfWNi5jGtZ7Zhiyk/RTqrJZDvlWJj2rm92AuX XXJ30rbPZ4SAalGIovl3iSJe76UktgUrKMQqu2W3vaKn18oLs4ZwtnCjXqoADFRSdEvZ d31g== 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=WRWg52eRqthcr+2rGv3B+mut/REEiUkLsC6/S6wqYBI=; fh=juxwNcA6iKwLISFgUCNwdoIYC0NKLUseq3xZdq25RR4=; b=taHgXSnuoaeaXsnl1rTSvKc2oqYxMvSoeHVGw6kCG+tNZObJGXVVYXN2Cp84G/mJNK FQLi0L23zPL0a5wxCw2rMIy03Z8KZaCII1QEgyZSS4mbFe2SjC3Plif4GAmiVY367fiY 4gqkbAB0KX30jkuafAjrkZToutlYg4BekH9eo0T3xNmBlGVArmsOk6BPU/Ney+4l3fNP YTN32uTS0ev1vfScGApfSGWqEs3nHou6SSM3MGrbYlQZOfz4ep9AJQUolr096cW2THQJ rB8ZINDFA5UMBDFZLAJ+j4+z+Wjir8kkXKEhUYPEYCuvGGEDYtLLhGDhk9Nlg9ERBEf4 OozA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HBegaGZ3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j184-20020a6380c1000000b0055397816fc1si11779686pgd.758.2023.07.25.15.46.16; Tue, 25 Jul 2023 15:46:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HBegaGZ3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233029AbjGYWZJ (ORCPT + 99 others); Tue, 25 Jul 2023 18:25:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232910AbjGYWXJ (ORCPT ); Tue, 25 Jul 2023 18:23:09 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68945264; Tue, 25 Jul 2023 15:18:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323504; x=1721859504; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8V2SRp2uSRLLY3IXZcPybdE4foVsDrQlY95Wj3aquPE=; b=HBegaGZ3J3VOv0VfitOpKR1yDRgBRJlggP+cBVw5/mBkIsR+aSximBk1 M5UyGIvt7VdSiyCbQKs92w5d7jxMFdtzcJDRw7k46328soRrJoLY87xJS E+GQXZa/i7FPCGTpYMzUI9c4dNIf7lBp9ASr47k7H2xUvSwaeKpCgS53a wgpThEQ81rPJj0nCko0N9Sd42+29o9a7iji+/n7qDu9LSPESaCKiXtSOQ tVfgDlYzJVn2Is6jA0eKe+X7rVGM9oihtEi7I7vuCnWaRiDSAO0SlBizk 478MeDC/4hygJr+yoey0g6KqVNsKyW4jODei5GdbrKIq8wuFp0JEvfk0U g==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="367882709" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="367882709" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:16:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="840001903" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="840001903" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:16:03 -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 v15 094/115] KVM: TDX: Handle TDX PV port io hypercall Date: Tue, 25 Jul 2023 15:14:45 -0700 Message-Id: <1e1f52c90dede77d0feadb19b1dc16bb66d85a35.1690322424.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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 d3ea368db5c6..965c5fecea6c 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1045,6 +1045,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)) @@ -1055,6 +1110,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