Re: [RFC PATCH v2 03/17] KVM: selftest: Adding TDX life cycle test.
From: Isaku Yamahata
Date: Wed Aug 31 2022 - 20:46:40 EST
On Tue, Aug 30, 2022 at 10:19:46PM +0000,
Sagi Shahar <sagis@xxxxxxxxxx> wrote:
> diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
> index 61b997dfc420..d5de52657112 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/tdx.h
> +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
> @@ -51,6 +51,12 @@
> #define _PAGE_RW (1UL<<1) /* writeable */
> #define _PAGE_PS (1UL<<7) /* page size bit*/
>
> +#define TDX_INSTRUCTION_IO 30
> +
> +#define TDX_SUCCESS_PORT 0x30
> +#define TDX_IO_READ 0
> +#define TDX_IO_WRITE 1
> +
> #define GDT_ENTRY(flags, base, limit) \
> ((((base) & 0xff000000ULL) << (56-24)) | \
> (((flags) & 0x0000f0ffULL) << 40) | \
> @@ -83,4 +89,147 @@ void prepare_source_image(struct kvm_vm *vm, void *guest_code,
> size_t guest_code_size,
> uint64_t guest_code_signature);
>
> +/*
> + * Generic TDCALL function that can be used to communicate with TDX module or
> + * VMM.
> + * Input operands: rax, rbx, rcx, rdx, r8-r15, rbp, rsi, rdi
> + * Output operands: rax, r8-r15, rbx, rdx, rdi, rsi
> + * rcx is actually a bitmap to tell TDX module which register values will be
> + * exposed to the VMM.
> + * XMM0-XMM15 registers can be used as input operands but the current
> + * implementation does not support it yet.
> + */
> +static inline void tdcall(struct kvm_regs *regs)
> +{
> + asm volatile (
> + "mov %13, %%rax;\n\t"
> + "mov %14, %%rbx;\n\t"
> + "mov %15, %%rcx;\n\t"
> + "mov %16, %%rdx;\n\t"
> + "mov %17, %%r8;\n\t"
> + "mov %18, %%r9;\n\t"
> + "mov %19, %%r10;\n\t"
> + "mov %20, %%r11;\n\t"
> + "mov %21, %%r12;\n\t"
> + "mov %22, %%r13;\n\t"
> + "mov %23, %%r14;\n\t"
> + "mov %24, %%r15;\n\t"
> + "mov %25, %%rbp;\n\t"
> + "mov %26, %%rsi;\n\t"
> + "mov %27, %%rdi;\n\t"
> + ".byte 0x66, 0x0F, 0x01, 0xCC;\n\t"
> + "mov %%rax, %0;\n\t"
> + "mov %%rbx, %1;\n\t"
> + "mov %%rdx, %2;\n\t"
> + "mov %%r8, %3;\n\t"
> + "mov %%r9, %4;\n\t"
> + "mov %%r10, %5;\n\t"
> + "mov %%r11, %6;\n\t"
> + "mov %%r12, %7;\n\t"
> + "mov %%r13, %8;\n\t"
> + "mov %%r14, %9;\n\t"
> + "mov %%r15, %10;\n\t"
> + "mov %%rsi, %11;\n\t"
> + "mov %%rdi, %12;\n\t"
> + : "=m" (regs->rax), "=m" (regs->rbx), "=m" (regs->rdx),
> + "=m" (regs->r8), "=m" (regs->r9), "=m" (regs->r10),
> + "=m" (regs->r11), "=m" (regs->r12), "=m" (regs->r13),
> + "=m" (regs->r14), "=m" (regs->r15), "=m" (regs->rsi),
> + "=m" (regs->rdi)
> + : "m" (regs->rax), "m" (regs->rbx), "m" (regs->rcx),
> + "m" (regs->rdx), "m" (regs->r8), "m" (regs->r9),
> + "m" (regs->r10), "m" (regs->r11), "m" (regs->r12),
> + "m" (regs->r13), "m" (regs->r14), "m" (regs->r15),
> + "m" (regs->rbp), "m" (regs->rsi), "m" (regs->rdi)
> + : "rax", "rbx", "rcx", "rdx", "r8", "r9", "r10", "r11",
> + "r12", "r13", "r14", "r15", "rbp", "rsi", "rdi");
> +}
Sometimes compiler (my gcc is (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0) doesn't like
clobering the frame pointer as follows. (I edited the caller site for other test.)
x86_64/tdx_vm_tests.c:343:1: error: bp cannot be used in ‘asm’ here
I ended up the following workaround. I didn't use pushq/popq pair because
I didn't want to play with offset in the stack of the caller.
diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
index aa6961c6f304..8ddf3b64f003 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/tdx.h
+++ b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
@@ -122,7 +122,11 @@ void prepare_source_image(struct kvm_vm *vm, void *guest_code,
*/
static inline void tdcall(struct kvm_regs *regs)
{
+ unsigned long saved_rbp = 0;
+
asm volatile (
+ /* gcc complains that frame pointer %rbp can't be clobbered. */
+ "movq %%rbp, %28;\n\t"
"mov %13, %%rax;\n\t"
"mov %14, %%rbx;\n\t"
"mov %15, %%rcx;\n\t"
@@ -152,6 +156,8 @@ static inline void tdcall(struct kvm_regs *regs)
"mov %%r15, %10;\n\t"
"mov %%rsi, %11;\n\t"
"mov %%rdi, %12;\n\t"
+ "movq %28, %%rbp\n\t"
+ "movq $0, %28\n\t"
: "=m" (regs->rax), "=m" (regs->rbx), "=m" (regs->rdx),
"=m" (regs->r8), "=m" (regs->r9), "=m" (regs->r10),
"=m" (regs->r11), "=m" (regs->r12), "=m" (regs->r13),
@@ -161,9 +167,10 @@ static inline void tdcall(struct kvm_regs *regs)
"m" (regs->rdx), "m" (regs->r8), "m" (regs->r9),
"m" (regs->r10), "m" (regs->r11), "m" (regs->r12),
"m" (regs->r13), "m" (regs->r14), "m" (regs->r15),
- "m" (regs->rbp), "m" (regs->rsi), "m" (regs->rdi)
+ "m" (regs->rbp), "m" (regs->rsi), "m" (regs->rdi),
+ "m" (saved_rbp)
: "rax", "rbx", "rcx", "rdx", "r8", "r9", "r10", "r11",
- "r12", "r13", "r14", "r15", "rbp", "rsi", "rdi");
+ "r12", "r13", "r14", "r15", "rsi", "rdi");
}
--
Isaku Yamahata <isaku.yamahata@xxxxxxxxx>