lkml.org 
[lkml]   [2021]   [Oct]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH v10 11/11] x86/tdx: Handle CPUID via #VE
Date
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>

When running virtualized, the CPUID instruction is handled differently
based on the leaf being accessed. The behavior depends only on the
leaf and applies equally to both kernel/ring-0 and userspace/ring-3
execution of CPUID. Historically, there are two basic classes:

* Leaves handled transparently to the guest
* Leaves handled by the VMM

In a typical guest without TDX, "handled by the VMM" leaves cause a
VMEXIT. TDX replaces these VMEXITs with a #VE exception in the guest.
The guest typically handles the #VE by making a hypercall to the VMM.

The TDX module specification [1], section titled "CPUID Virtualization"
talks about a few more classes of CPUID handling. But, for the purposes
of this patch, the "handled transparently" CPUID leaves are all lumped
together because the guest handling is the same.

[1] - https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1.0-public-spec-v0.931.pdf

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reviewed-by: Andi Kleen <ak@linux.intel.com>
Reviewed-by: Tony Luck <tony.luck@intel.com>
Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
---

Changes since v9:
* None

Changes since v8:
* Removed zero initialization of struct tdx_hypercall_output in
tdx_handle_cpuid().
* Changed return type of tdx_handle_cpuid() to bool.

Changes since v7:
* None

Changes since v6:
* Fixed commit log as per Dave Hansen comments.
* Added extra comments for _tdx_hypercall() usage.

Changes since v5:
* Fixed commit log as per review comments.
* Removed WARN_ON() in tdx_handle_cpuid().
* Renamed "tdg" prefix with "tdx".

Changes since v4:
* None

Changes since v3:
* None

arch/x86/kernel/tdx.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)

diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c
index 7c6c84015302..f8f242215dbc 100644
--- a/arch/x86/kernel/tdx.c
+++ b/arch/x86/kernel/tdx.c
@@ -143,6 +143,31 @@ static bool tdx_write_msr_safe(unsigned int msr, unsigned int low,
return ret ? false : true;
}

+static bool tdx_handle_cpuid(struct pt_regs *regs)
+{
+ struct tdx_hypercall_output out;
+
+ /*
+ * Emulate CPUID instruction via hypercall. More info about
+ * ABI can be found in TDX Guest-Host-Communication Interface
+ * (GHCI), section titled "VP.VMCALL<Instruction.CPUID>".
+ */
+ if (_tdx_hypercall(EXIT_REASON_CPUID, regs->ax, regs->cx, 0, 0, &out))
+ return false;
+
+ /*
+ * As per TDX GHCI CPUID ABI, r12-r15 registers contains contents of
+ * EAX, EBX, ECX, EDX registers after CPUID instruction execution.
+ * So copy the register contents back to pt_regs.
+ */
+ regs->ax = out.r12;
+ regs->bx = out.r13;
+ regs->cx = out.r14;
+ regs->dx = out.r15;
+
+ return true;
+}
+
bool tdx_get_ve_info(struct ve_info *ve)
{
struct tdx_module_output out;
@@ -191,6 +216,9 @@ bool tdx_handle_virtualization_exception(struct pt_regs *regs,
case EXIT_REASON_MSR_WRITE:
ret = tdx_write_msr_safe(regs->cx, regs->ax, regs->dx);
break;
+ case EXIT_REASON_CPUID:
+ ret = tdx_handle_cpuid(regs);
+ break;
default:
pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
return false;
--
2.25.1
\
 
 \ /
  Last update: 2021-10-09 07:38    [W:0.268 / U:1.320 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site