2022-01-24 19:26:37

by Kirill A. Shutemov

[permalink] [raw]
Subject: [PATCHv2 13/29] x86/tdx: Add port I/O emulation

From: Kuppuswamy Sathyanarayanan <[email protected]>

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 <[email protected]>
Reviewed-by: Andi Kleen <[email protected]>
Reviewed-by: Dan Williams <[email protected]>
Signed-off-by: Kirill A. Shutemov <[email protected]>
---
arch/x86/kernel/cc_platform.c | 3 +++
arch/x86/kernel/tdx.c | 48 +++++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)

diff --git a/arch/x86/kernel/cc_platform.c b/arch/x86/kernel/cc_platform.c
index c72b3919bca9..8da246ab4339 100644
--- a/arch/x86/kernel/cc_platform.c
+++ b/arch/x86/kernel/cc_platform.c
@@ -17,6 +17,9 @@

static bool intel_cc_platform_has(enum cc_attr attr)
{
+ if (attr == CC_ATTR_GUEST_UNROLL_STRING_IO)
+ return true;
+
return false;
}

diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c
index 8e630eeb765d..e73af22a4c11 100644
--- a/arch/x86/kernel/tdx.c
+++ b/arch/x86/kernel/tdx.c
@@ -13,6 +13,12 @@
/* TDX module Call Leaf IDs */
#define TDX_GET_VEINFO 3

+/* See Exit Qualification for I/O Instructions in VMX documentation */
+#define VE_IS_IO_IN(exit_qual) (((exit_qual) & 8) ? 1 : 0)
+#define VE_GET_IO_SIZE(exit_qual) (((exit_qual) & 7) + 1)
+#define VE_GET_PORT_NUM(exit_qual) ((exit_qual) >> 16)
+#define VE_IS_IO_STRING(exit_qual) ((exit_qual) & 16 ? 1 : 0)
+
static bool tdx_guest_detected __ro_after_init;

/*
@@ -257,6 +263,45 @@ static int tdx_handle_mmio(struct pt_regs *regs, struct ve_info *ve)
return insn.length;
}

+/*
+ * 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 tdx_handle_io(struct pt_regs *regs, u32 exit_qual)
+{
+ struct tdx_hypercall_output out;
+ int size, port, ret;
+ u64 mask;
+ bool in;
+
+ 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);
+
+ /*
+ * Emulate the I/O read/write via hypercall. More info about
+ * ABI can be found in TDX Guest-Host-Communication Interface
+ * (GHCI) sec titled "TDG.VP.VMCALL<Instruction.IO>".
+ */
+ ret = _tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, !in, port,
+ in ? 0 : regs->ax, &out);
+ if (!in)
+ return !ret;
+
+ regs->ax &= ~mask;
+ regs->ax |= ret ? UINT_MAX : out.r11 & mask;
+
+ return !ret;
+}
+
bool tdx_get_ve_info(struct ve_info *ve)
{
struct tdx_module_output out;
@@ -333,6 +378,9 @@ static bool tdx_virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve)
if (!ret)
pr_warn_once("MMIO failed\n");
break;
+ case EXIT_REASON_IO_INSTRUCTION:
+ ret = tdx_handle_io(regs, ve->exit_qual);
+ break;
default:
pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
break;
--
2.34.1


2022-02-02 08:14:39

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [PATCHv2 13/29] x86/tdx: Add port I/O emulation

On Mon, Jan 24 2022 at 18:01, Kirill A. Shutemov wrote:
> static bool intel_cc_platform_has(enum cc_attr attr)
> {
> + if (attr == CC_ATTR_GUEST_UNROLL_STRING_IO)
> + return true;
> +

switch (attr) perhaps as there are more coming, right?

> return false;
> }

> +/*
> + * 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 tdx_handle_io(struct pt_regs *regs, u32 exit_qual)
> +{
> + struct tdx_hypercall_output out;
> + int size, port, ret;
> + u64 mask;
> + bool in;
> +
> + 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);
> +
> + /*
> + * Emulate the I/O read/write via hypercall. More info about
> + * ABI can be found in TDX Guest-Host-Communication Interface
> + * (GHCI) sec titled "TDG.VP.VMCALL<Instruction.IO>".
> + */
> + ret = _tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, !in, port,
> + in ? 0 : regs->ax, &out);
> + if (!in)
> + return !ret;
> +
> + regs->ax &= ~mask;
> + regs->ax |= ret ? UINT_MAX : out.r11 & mask;
> +
> + return !ret;
> +}
> +
> bool tdx_get_ve_info(struct ve_info *ve)
> {
> struct tdx_module_output out;
> @@ -333,6 +378,9 @@ static bool tdx_virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve)
> if (!ret)
> pr_warn_once("MMIO failed\n");
> break;
> + case EXIT_REASON_IO_INSTRUCTION:
> + ret = tdx_handle_io(regs, ve->exit_qual);

return ...

Other than that LGTM.

Thanks,

tglx

2022-02-03 19:02:41

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCHv2 13/29] x86/tdx: Add port I/O emulation

On Mon, Jan 24, 2022 at 06:01:59PM +0300, Kirill A. Shutemov wrote:
> diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c
> index 8e630eeb765d..e73af22a4c11 100644
> --- a/arch/x86/kernel/tdx.c
> +++ b/arch/x86/kernel/tdx.c
> @@ -13,6 +13,12 @@
> /* TDX module Call Leaf IDs */
> #define TDX_GET_VEINFO 3
>
> +/* See Exit Qualification for I/O Instructions in VMX documentation */
> +#define VE_IS_IO_IN(exit_qual) (((exit_qual) & 8) ? 1 : 0)
> +#define VE_GET_IO_SIZE(exit_qual) (((exit_qual) & 7) + 1)
> +#define VE_GET_PORT_NUM(exit_qual) ((exit_qual) >> 16)
> +#define VE_IS_IO_STRING(exit_qual) ((exit_qual) & 16 ? 1 : 0)

Use BIT() and masks here. For example:

#define VE_IS_IO_STRING(e) ((e) & BIT(4))

You don't need the ternary ?: either as you're using them all in a
boolean context.

> +static bool tdx_handle_io(struct pt_regs *regs, u32 exit_qual)
> +{
> + struct tdx_hypercall_output out;
> + int size, port, ret;
> + u64 mask;
> + bool in;
> +
> + 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);
> +
> + /*
> + * Emulate the I/O read/write via hypercall. More info about
> + * ABI can be found in TDX Guest-Host-Communication Interface
> + * (GHCI) sec titled "TDG.VP.VMCALL<Instruction.IO>".

"section"

> + */
> + ret = _tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, !in, port,
> + in ? 0 : regs->ax, &out);
> + if (!in)
> + return !ret;
> +
> + regs->ax &= ~mask;
> + regs->ax |= ret ? UINT_MAX : out.r11 & mask;
> +
> + return !ret;
> +}

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette