From patchwork Mon Feb 26 08:26:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 206445 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1950643dyb; Mon, 26 Feb 2024 01:06:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUZwZMGABT46nwNxa9UYfVF1njjLXwRDpnGH3XrqZtzoQP50mvXp94jLWtdrsAEIlQHfYJkfYfiAVpQRMRjrVUBhwXwqw== X-Google-Smtp-Source: AGHT+IGkQMtrjTJgndZ6mCgiI4Laa7wnzfFavI5FFqG/z5OrM8mFT7GzQPjwSAi3t82JuCPNxrMS X-Received: by 2002:a0c:e44d:0:b0:68f:cbfa:6876 with SMTP id d13-20020a0ce44d000000b0068fcbfa6876mr6673905qvm.38.1708938389756; Mon, 26 Feb 2024 01:06:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708938389; cv=pass; d=google.com; s=arc-20160816; b=dGdBxTDZoNNmCWIqeKKxbYtbOXM15ETtCT/YJiL5FTj8onmMRIDfPG8XRLGxZ1lM/Z l+g5ove/LsbJ9uG5csrFrHssOy1eSr03F09DM26K5hA4ZANcs0E2GZNvAXn4GkUPP+Pb mc1PKbHc+zOBvK7K2UivIeRhROgteojuegpoz1tTdjZ68pDxJ/gPcDEpnJHCepgDZqvv 1LtjDOXeAQFFD+OpdPOd7A+7PBrz/sof1dwG48YYg7sIVDTC4AXTs1PLICX6mGgXbEpw BbrWzzYjOJf1LofMDDejNE5QO8/z9J/c9WFzdq7Zvc6pPeyJ/3f7+YKBzQzO1W4CJNwR obqg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=qNs0EyGECi2l26RZJHyWk07jKNgeRqyvIxVSSPSJtqw=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=ZDUlsbkhjIEdCnTz31R1JYdBW8jaJXG7xLV+w2rhN20oGAhac0bClOZCiHiDhb+zmj pc3ETFt8hM+LcxoeT1IJMgCy2ZrRKhOJCn0OP2wZ76dDckP3jAqcLCDR47/tXt34TRSs H07wjQcFRdFmI33l4y+USWlYiVYsvvnOl5KV6uPHyOGc+gqGkarTYtX/Zb1ffmEQxAJ9 fETnpRjjxN52in9AgK8ZzssUNHuzIOsiiJsPY9Tjh6PhPBYtv9IE/VeFlcrLsZ12Zk7x wdG/M9uU0d4LgEwuIAn/lOq2zwicZebbfFnNuajo1czEtLcmEs/p7TYoENp5NdsIDrVS 8M/Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="amdvzmG/"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80873-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80873-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id kc6-20020a056214410600b0068f7565e263si4619303qvb.553.2024.02.26.01.06.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 01:06:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80873-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="amdvzmG/"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80873-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80873-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3B86D1C23AD8 for ; Mon, 26 Feb 2024 09:05:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B167412C7E2; Mon, 26 Feb 2024 08:29:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="amdvzmG/" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDB27129A61; Mon, 26 Feb 2024 08:29:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936148; cv=none; b=YEx3xDhYEkSWPEq1jPA1TU0W4lF3Uu0FFC3u+l9z7B1qEYMtfvZUmK/lfb13J8Sks9xwmgaFBGWUTTsJQ2cCR5hHz1cPvq4uZ93k93TYMGbmhsldauV5G4UdpA0gdh+Q00J1h9th1C9eLmuhD/BwbB5fmfOElQ0fKNt9D9hDgKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936148; c=relaxed/simple; bh=eVhOiOfXw4o2qt4bdllBY+jHKu0pt33vUmwn4aS9VO8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ll8jJKTcNoadgX+s/UzIbyYDxXpzmX1l5JwTn7omRrknyoDeytyFyxjngJBiUXG2m/IYi0gLfsv+HErXdO7cyKXq90mkWxM7lGXm7tXJN4n1axMqPBt54oBANy+EIM23exc2npnCaIyrYDQfsZGtKQMbHuiO5OqyWS7T3zxVPa0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=amdvzmG/; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708936147; x=1740472147; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eVhOiOfXw4o2qt4bdllBY+jHKu0pt33vUmwn4aS9VO8=; b=amdvzmG/JT+1hjNqSrMOKgsBxZvJiTn+0tGCpt3c//0ZDNFoFoNCpyl1 6xJfpxYoknbMOa+gMYnv+vtCIaQ9DVWvU74CJuw561zJ1N8JGlN33sqVw ytVx50LbTX6emSSWcWCDvv3qE5Id3m+zQYArkx+o3+3cFCUczC7fyyhUB E+2JqMcMjBaK7QLy3yoyoSWREMrStQuO/0Phxhf1HaQ54QJVl1lzitr8D 1KqyhoL5VA/G+tJriBtxPCR5BCjAcy3WORe/XGQqvauxbj1l/Hkc66O8o Jq/DaVOwGnyqXAUNCuvaafI9UPXVTca3z0nJQEqKC1oynJBKkUL5M0ZmD w==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="20751334" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="20751334" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:29:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="6735068" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:29:04 -0800 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 , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v19 109/130] KVM: TDX: Handle TDX PV port io hypercall Date: Mon, 26 Feb 2024 00:26:51 -0800 Message-Id: <4f4aaf292008608a8717e9553c3315ee02f66b20.1708933498.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791951780955642706 X-GMAIL-MSGID: 1791951780955642706 From: Isaku Yamahata Wire up TDX PV port IO hypercall to the KVM backend function. Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini --- v18: - Fix out case to set R10 and R11 correctly when user space handled port out. --- arch/x86/kvm/vmx/tdx.c | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index a2caf2ae838c..55fc6cc6c816 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1152,6 +1152,71 @@ static int tdx_emulate_hlt(struct kvm_vcpu *vcpu) return kvm_emulate_halt_noskip(vcpu); } +static int tdx_complete_pio_out(struct kvm_vcpu *vcpu) +{ + tdvmcall_set_return_code(vcpu, TDVMCALL_SUCCESS); + tdvmcall_set_return_val(vcpu, 0); + return 1; +} + +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, TDVMCALL_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, TDVMCALL_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) + tdvmcall_set_return_val(vcpu, val); + else { + if (write) + vcpu->arch.complete_userspace_io = tdx_complete_pio_out; + else + vcpu->arch.complete_userspace_io = tdx_complete_pio_in; + } + + return ret; +} + static int handle_tdvmcall(struct kvm_vcpu *vcpu) { if (tdvmcall_exit_type(vcpu)) @@ -1162,6 +1227,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; }