Messages in this thread Patch in this message | | | From | "Kirill A. Shutemov" <> | Subject | [PATCHv3 18/32] x86/tdx: Add port I/O emulation | Date | Fri, 18 Feb 2022 19:17:04 +0300 |
| |
From: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
TDX hypervisors cannot emulate instructions directly. This includes port I/O which is normally emulated in the hypervisor. All port I/O instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
Use a hypercall to emulate port I/O. Extend the tdx_handle_virt_exception() and add support to handle the #VE due to port I/O instructions.
String I/O operations are not supported in TDX. Unroll them by declaring CC_ATTR_GUEST_UNROLL_STRING_IO confidential computing attribute.
Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com> Reviewed-by: Andi Kleen <ak@linux.intel.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> --- arch/x86/coco/tdx.c | 54 +++++++++++++++++++++++++++++++++++ arch/x86/kernel/cc_platform.c | 7 ++++- 2 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/arch/x86/coco/tdx.c b/arch/x86/coco/tdx.c index 74ab7c5a767d..e3693d0494a7 100644 --- a/arch/x86/coco/tdx.c +++ b/arch/x86/coco/tdx.c @@ -19,6 +19,12 @@ #define EPT_READ 0 #define EPT_WRITE 1 +/* See Exit Qualification for I/O Instructions in VMX documentation */ +#define VE_IS_IO_IN(e) ((e) & BIT(3)) +#define VE_GET_IO_SIZE(e) (((e) & GENMASK(2, 0)) + 1) +#define VE_GET_PORT_NUM(e) ((e) >> 16) +#define VE_IS_IO_STRING(e) ((e) & BIT(4)) + static struct { unsigned int gpa_width; unsigned long attributes; @@ -292,6 +298,52 @@ static bool handle_mmio(struct pt_regs *regs, struct ve_info *ve) return true; } +/* + * Emulate I/O using hypercall. + * + * Assumes the IO instruction was using ax, which is enforced + * by the standard io.h macros. + * + * Return True on success or False on failure. + */ +static bool handle_io(struct pt_regs *regs, u32 exit_qual) +{ + struct tdx_hypercall_args args = { + .r10 = TDX_HYPERCALL_STANDARD, + .r11 = EXIT_REASON_IO_INSTRUCTION, + }; + int size, port; + u64 mask; + bool in, ret; + + if (VE_IS_IO_STRING(exit_qual)) + return false; + + in = VE_IS_IO_IN(exit_qual); + size = VE_GET_IO_SIZE(exit_qual); + port = VE_GET_PORT_NUM(exit_qual); + mask = GENMASK(BITS_PER_BYTE * size, 0); + + args.r12 = size; + args.r13 = !in; + args.r14 = port; + args.r15 = in ? 0 : regs->ax; + + /* + * Emulate the I/O read/write via hypercall. More info about + * ABI can be found in TDX Guest-Host-Communication Interface + * (GHCI) section titled "TDG.VP.VMCALL<Instruction.IO>". + */ + ret = !__tdx_hypercall(&args, in ? TDX_HCALL_HAS_OUTPUT : 0); + if (!ret || !in) + return ret; + + regs->ax &= ~mask; + regs->ax |= ret ? args.r11 & mask : UINT_MAX; + + return ret; +} + void tdx_get_ve_info(struct ve_info *ve) { struct tdx_module_output out; @@ -347,6 +399,8 @@ static bool virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve) return handle_cpuid(regs); case EXIT_REASON_EPT_VIOLATION: return handle_mmio(regs, ve); + case EXIT_REASON_IO_INSTRUCTION: + return handle_io(regs, ve->exit_qual); default: pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); return false; diff --git a/arch/x86/kernel/cc_platform.c b/arch/x86/kernel/cc_platform.c index dba713c444ab..55128de5e27c 100644 --- a/arch/x86/kernel/cc_platform.c +++ b/arch/x86/kernel/cc_platform.c @@ -18,7 +18,12 @@ static enum cc_vendor cc_vendor; static bool intel_cc_platform_has(enum cc_attr attr) { - return false; + switch (attr) { + case CC_ATTR_GUEST_UNROLL_STRING_IO: + return true; + default: + return false; + } } /* -- 2.34.1
| |