Skip to content

Commit ae87f60

Browse files
kirylhansendc
authored andcommitted
x86/tdx: Add MSR support for TDX guests
Use hypercall to emulate MSR read/write for the TDX platform. There are two viable approaches for doing MSRs in a TD guest: 1. Execute the RDMSR/WRMSR instructions like most VMs and bare metal do. Some will succeed, others will cause a #VE. All of those that cause a #VE will be handled with a TDCALL. 2. Use paravirt infrastructure. The paravirt hook has to keep a list of which MSRs would cause a #VE and use a TDCALL. All other MSRs execute RDMSR/WRMSR instructions directly. The second option can be ruled out because the list of MSRs was challenging to maintain. That leaves option #1 as the only viable solution for the minimal TDX support. Kernel relies on the exception fixup machinery to handle MSR access errors. #VE handler uses the same exception fixup code as #GP. It covers MSR accesses along with other types of fixups. For performance-critical MSR writes (like TSC_DEADLINE), future patches will replace the WRMSR/#VE sequence with the direct TDCALL. RDMSR and WRMSR specification details can be found in Guest-Host-Communication Interface (GHCI) for Intel Trust Domain Extensions (Intel TDX) specification, sec titled "TDG.VP. VMCALL<Instruction.RDMSR>" and "TDG.VP.VMCALL<Instruction.WRMSR>". Co-developed-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com> Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Andi Kleen <ak@linux.intel.com> Reviewed-by: Tony Luck <tony.luck@intel.com> Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lkml.kernel.org/r/20220405232939.73860-10-kirill.shutemov@linux.intel.com
1 parent bfe6ed0 commit ae87f60

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

arch/x86/coco/tdx/tdx.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,44 @@ void __cpuidle tdx_safe_halt(void)
142142
WARN_ONCE(1, "HLT instruction emulation failed\n");
143143
}
144144

145+
static bool read_msr(struct pt_regs *regs)
146+
{
147+
struct tdx_hypercall_args args = {
148+
.r10 = TDX_HYPERCALL_STANDARD,
149+
.r11 = hcall_func(EXIT_REASON_MSR_READ),
150+
.r12 = regs->cx,
151+
};
152+
153+
/*
154+
* Emulate the MSR read via hypercall. More info about ABI
155+
* can be found in TDX Guest-Host-Communication Interface
156+
* (GHCI), section titled "TDG.VP.VMCALL<Instruction.RDMSR>".
157+
*/
158+
if (__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT))
159+
return false;
160+
161+
regs->ax = lower_32_bits(args.r11);
162+
regs->dx = upper_32_bits(args.r11);
163+
return true;
164+
}
165+
166+
static bool write_msr(struct pt_regs *regs)
167+
{
168+
struct tdx_hypercall_args args = {
169+
.r10 = TDX_HYPERCALL_STANDARD,
170+
.r11 = hcall_func(EXIT_REASON_MSR_WRITE),
171+
.r12 = regs->cx,
172+
.r13 = (u64)regs->dx << 32 | regs->ax,
173+
};
174+
175+
/*
176+
* Emulate the MSR write via hypercall. More info about ABI
177+
* can be found in TDX Guest-Host-Communication Interface
178+
* (GHCI) section titled "TDG.VP.VMCALL<Instruction.WRMSR>".
179+
*/
180+
return !__tdx_hypercall(&args, 0);
181+
}
182+
145183
void tdx_get_ve_info(struct ve_info *ve)
146184
{
147185
struct tdx_module_output out;
@@ -178,6 +216,10 @@ static bool virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve)
178216
switch (ve->exit_reason) {
179217
case EXIT_REASON_HLT:
180218
return handle_halt();
219+
case EXIT_REASON_MSR_READ:
220+
return read_msr(regs);
221+
case EXIT_REASON_MSR_WRITE:
222+
return write_msr(regs);
181223
default:
182224
pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
183225
return false;

0 commit comments

Comments
 (0)