Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp170637rdb; Mon, 22 Jan 2024 16:15:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IFhOBs4rnVCNBCwaa9Pi48xk0yuxA9sFyuVgRy38JcrcdjQ0+/8N9kON2geIhMvUp+DOPmO X-Received: by 2002:a17:907:8e8c:b0:a30:9fb2:22d0 with SMTP id tx12-20020a1709078e8c00b00a309fb222d0mr398309ejc.74.1705968919954; Mon, 22 Jan 2024 16:15:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705968919; cv=pass; d=google.com; s=arc-20160816; b=d4+KHVGNLRSfEm7bP0gtZ+rmVXQNtqVdSreXhGPPJMzDfcG2Ye0MzETpcyYR2nvAJd mpTzJq4DN4D8C5ks8uyjNHI3BhJNHUI3XcVlWTVOKoLpGFaidd6c81IQqy74Rlrtcgpn RA6RlX4NIwKJoEhwenKLUkFBHGu0lp3U/LjEwRFyXQFo9q2YMJguQP4cGTHnqlMxV2Kr rDg+7LpOZTE1Iur4NbL7s9BQdvQxgdDNV7tk9jzoaPvibrcJOeJq46dgwkdpHXKe/vSV AfX8RbTRMneUEZKaUkuakSO5U+4b1TQ4e/IbjWlTBwCXKnswlVzTCEy3WTIDxxwcU23U 2KWg== 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=lej+ezp87+xjXvTAtwON3QKC8X6RbJlfjLnd4eir2lA=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=uX66nMwEHpShQxbJF2RVmJwhNEdHTf5qzVHCophwvCSXSYVSsScnXd+XdA/phx/Y1G 6OyX7PsLvYvS//AdsrDZzC9AOJRx6FJf2M2y7U4LzIRYHfMfylI4bmeRLTI+i69zFn+U la+7LgA6Nr4OnAKgHIGrvIOCwVMxgIuKwNKljmdX4jkW1T11/QGNAOKltmuMULg/NRJ+ MIaodfxdTaU0PMx05XZplCr+094NzvZjUIqWRfpqVdRVrSgLQTNpWe0c7zzk1OXez1zs 4HFRznHxREW9ZUFa6ptAxA7c+sWIfgTZhtmznUROR+4zBjK++ts7cF5ccenPKxaHd2lu 6jSw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QWvZdbVG; 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-34407-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34407-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q4-20020a1709066b0400b00a29efb615c5si11394828ejr.613.2024.01.22.16.15.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 16:15:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34407-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QWvZdbVG; 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-34407-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34407-linux.lists.archive=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 am.mirrors.kernel.org (Postfix) with ESMTPS id B54401F2401D for ; Tue, 23 Jan 2024 00:15:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0959C77632; Mon, 22 Jan 2024 23:56:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QWvZdbVG" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) (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 093BA6DD1C; Mon, 22 Jan 2024 23:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.120 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967764; cv=none; b=QQFX33nNkp/Iyvaid3eE8AnUFZeI4yYtNiMgaI38NtticTyCu4yUBYoxRPuHB6GnPq3B4nZrIZucIR4bhkS8fxBMYNlU/5SPIgbLyHoa4DY6OLF/P6DRM/Ue3SzK9EYQynViRr4w0F+PY9btT1dh/afq41LYpP+oJdcLKU8hlLI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967764; c=relaxed/simple; bh=7mL7xnz9vwulESCO11rlmOjEDuZMNOvUZdFeDQ4Cjk0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sM2CtkCHwWlsnN42p8lUEDHq0iSiY7q2A3lh18FxMOmRcl5nfWgiHJjiUTg+eJMjcbUA90U7Lv25B8RoBvS9x4GYV6ctntuRhEG2GfD7wOmZzBUV0Tf5VtnqoAxAVVUc3Yyk5aYsFasAHCLvoLBnzbKeN2gXJeiU07bB+hD0tjQ= 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=QWvZdbVG; arc=none smtp.client-ip=192.55.52.120 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=1705967762; x=1737503762; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7mL7xnz9vwulESCO11rlmOjEDuZMNOvUZdFeDQ4Cjk0=; b=QWvZdbVGFYb6mNtnHPWR/EbXqxhj44tXAPuC6Gm0I3DVZB9YybfiwuRq KfsuEUfQpe0qmmwvh7GxF5uAidRe00/X9eh3ks547WsX+cL1b/LcNAVfu K4WZa/eu1efYayiqXPh4ib04bkQ6CbrVmUa7LonPlFFaRkFIiojILWww7 xJvKK+1h856nhodlBbMXBz43l3vUxb2bLaIVusqzOuTiTMCse6hSeQ+nT Z2n7RqA7XPVXGRfpkVkq2CxpsaV9XF3twhOaPhmSQkAYRdqz7XNf95VqN 7BHl9XlRpJO7YrcQOM2d3ERmtXU+JpS5lQPKVgKIAsgnm+JIRQsj0xRIy g==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="400217863" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="400217863" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="27817980" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:51 -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 v18 098/121] KVM: TDX: Handle TDX PV port io hypercall Date: Mon, 22 Jan 2024 15:54:14 -0800 Message-Id: <06a142259ea1d80ae54161e18e8d4983d4d11210.1705965635.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 Content-Transfer-Encoding: 8bit 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 4628c7eb3002..71c444cfbc9e 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1202,6 +1202,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, TDG_VP_VMCALL_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, 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) + 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)) @@ -1212,6 +1277,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