From patchwork Tue Dec 10 00:49:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Binbin Wu X-Patchwork-Id: 13900577 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 6CB3A1CEAC0; Tue, 10 Dec 2024 00:48:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733791700; cv=none; b=IOPGw6svb/OWf7nO7nUPRZxVTVfpt/bvZ+qpGbbJNo+heN44mBpUq9QMJnazXZXQX7iBPh2vP/V6fwdLuPw3oryJ0rGnTg9BrlZywkbUN1vzlQE+jVgKfa00XxXYJW3bM/LVK7YBaXOk1uQELkevKTorT5Bd7qOHduFmHSXJG5k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733791700; c=relaxed/simple; bh=gBLPzmj8cVe3Ps1IqTTp5ny47gy8z17oWB2zry3ynkY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mRF20KX+FE2sDgYYQwQeQtt6tnjAOe1UT+HC34Vem7rUO5angNJyONM1Grrhs6gQ/bBsraS4YCJ9XufDhQhsMK+CCwkAFFAyT3Kc6RGH5nOscJbEAZUeyidtpo5vJVx+NDZ4CaUgCkMfh88KLPR3cQUl+amAsndaEKM4GiTfoJg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Z30iF2uE; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Z30iF2uE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1733791698; x=1765327698; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gBLPzmj8cVe3Ps1IqTTp5ny47gy8z17oWB2zry3ynkY=; b=Z30iF2uErzbo94PF7S4RXEgW6wcz/SXrbWDOTB3TUW7g4f99PtEMVmXH fQcduoTeaRrnpK3U5cfMgrytTwhNQbN2zUM3xtAebZHWtxDOi9SoH+xmW 17xWe1njF/JSgmq3YBaOTUfmJWfwf73eG++EQAU1D/CJ7t7VHmkZrvEZ5 PASeG4UX94w5z3PrQ4Ou/yqBngl95tQ9a9zHvu4jaDLqEhqs+dWVicP03 GBNQKUAw6y5PRCIAxDo9ihGW9eUm5x9l5LQpMsjSPG+143OxtfEVWoaBd ejRMBltFoqSJp4GMHWom5vB1VeGb0X/jU6swGMD29qaFQaC1GQp/1e3YY w==; X-CSE-ConnectionGUID: XuUGAJHKSCS+pZP2dTswgQ== X-CSE-MsgGUID: Poj29lamS8CAPUTWNxpYcA== X-IronPort-AV: E=McAfee;i="6700,10204,11281"; a="44793714" X-IronPort-AV: E=Sophos;i="6.12,220,1728975600"; d="scan'208";a="44793714" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Dec 2024 16:48:18 -0800 X-CSE-ConnectionGUID: pQULy6oqR7mRScT//9vZSw== X-CSE-MsgGUID: L4vpe74tQLqkV83G5LtrxA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,220,1728975600"; d="scan'208";a="96033046" Received: from litbin-desktop.sh.intel.com ([10.239.156.93]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Dec 2024 16:48:14 -0800 From: Binbin Wu To: pbonzini@redhat.com, seanjc@google.com, kvm@vger.kernel.org Cc: rick.p.edgecombe@intel.com, kai.huang@intel.com, adrian.hunter@intel.com, reinette.chatre@intel.com, xiaoyao.li@intel.com, tony.lindgren@linux.intel.com, isaku.yamahata@intel.com, yan.y.zhao@intel.com, chao.gao@intel.com, linux-kernel@vger.kernel.org, binbin.wu@linux.intel.com Subject: [PATCH 08/18] KVM: TDX: Handle TDX PV rdmsr/wrmsr hypercall Date: Tue, 10 Dec 2024 08:49:34 +0800 Message-ID: <20241210004946.3718496-9-binbin.wu@linux.intel.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241210004946.3718496-1-binbin.wu@linux.intel.com> References: <20241210004946.3718496-1-binbin.wu@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Isaku Yamahata Wire up TDX PV rdmsr/wrmsr hypercall to the KVM backend function. Signed-off-by: Isaku Yamahata Signed-off-by: Binbin Wu Reviewed-by: Paolo Bonzini --- TDX "the rest" breakout: - Use TDVMCALL_STATUS prefix for TDX call status codes (Binbin) --- arch/x86/kvm/vmx/tdx.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 770e3b847cd6..d5343e2ba509 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1493,6 +1493,41 @@ static int tdx_emulate_mmio(struct kvm_vcpu *vcpu) return 1; } +static int tdx_emulate_rdmsr(struct kvm_vcpu *vcpu) +{ + u32 index = tdvmcall_a0_read(vcpu); + u64 data; + + if (!kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ) || + kvm_get_msr(vcpu, index, &data)) { + trace_kvm_msr_read_ex(index); + tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_INVALID_OPERAND); + return 1; + } + trace_kvm_msr_read(index, data); + + tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_SUCCESS); + tdvmcall_set_return_val(vcpu, data); + return 1; +} + +static int tdx_emulate_wrmsr(struct kvm_vcpu *vcpu) +{ + u32 index = tdvmcall_a0_read(vcpu); + u64 data = tdvmcall_a1_read(vcpu); + + if (!kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE) || + kvm_set_msr(vcpu, index, data)) { + trace_kvm_msr_write_ex(index, data); + tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_INVALID_OPERAND); + return 1; + } + + trace_kvm_msr_write(index, data); + tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_SUCCESS); + return 1; +} + static int handle_tdvmcall(struct kvm_vcpu *vcpu) { if (tdvmcall_exit_type(vcpu)) @@ -1511,6 +1546,10 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu) return tdx_emulate_io(vcpu); case EXIT_REASON_EPT_VIOLATION: return tdx_emulate_mmio(vcpu); + case EXIT_REASON_MSR_READ: + return tdx_emulate_rdmsr(vcpu); + case EXIT_REASON_MSR_WRITE: + return tdx_emulate_wrmsr(vcpu); default: break; }