RE: [PATCH 5/6] x86/hyperv: Support hypercalls for TDX guests
From: Dexuan Cui
Date: Sun Nov 27 2022 - 19:58:44 EST
> From: Michael Kelley (LINUX) <mikelley@xxxxxxxxxxxxx>
> Sent: Wednesday, November 23, 2022 6:45 AM
> To: Dexuan Cui <decui@xxxxxxxxxxxxx>; ak@xxxxxxxxxxxxxxx; arnd@xxxxxxxx;
>
> Two thoughts:
>
> 1) The #ifdef CONFIG_INTEL_TDX_GUEST could probably be removed
> entirely
> with a tweak. hv_isolation_type_tdx() already doesn't need the #ifdef as
> there's
> already a stub that returns 'false'. Then you just need a way to handle
> __tdx_ms_hv_hypercall(), or whatever it becomes based on the other
> discussion.
> As long as you can provide a stub that does nothing, the #ifdef won't be
> needed.
>
> 2) Assuming that we end up with some kind of Hyper-V specific version of
> __tdx_hypercall(), and hopefully as a "C" function, could you move the
> handling
> of ms_hyperv.shared_gpa_boundary into that function? Then you won't
> need
> to break out a separate include file for struct ms_hyperv. The Hyper-V TDX
> hypercall function must handle both normal and "fast" hypercalls, and the
> shared_gpa_boundary adjustment is needed only for normal hypercalls,
> but you can check the "fast" bit in the control word to decide.
>
> I haven't coded these ideas, so maybe there are snags I haven't thought of.
> But I'm really hoping we can avoid having to create a separate include
> file for struct ms_hyperv.
>
> Michael
Thanks for the great suggestions! Now the code looks like this:
(the full list of v2 patches are still WIP:
https://github.com/dcui/tdx/commits/decui/hyperv-next/2022-1121/v6.1-rc5/v2)
diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c
index 13ccb52eecd7..00e5c84e380b 100644
--- a/arch/x86/hyperv/ivm.c
+++ b/arch/x86/hyperv/ivm.c
@@ -276,6 +276,27 @@ bool hv_isolation_type_tdx(void)
{
return static_branch_unlikely(&isolation_type_tdx);
}
+
+u64 hv_tdx_hypercall(u64 control, u64 input_addr, u64 output_addr)
+{
+ struct tdx_hypercall_args args = { };
+
+ if (!(control & HV_HYPERCALL_FAST_BIT)) {
+ if (input_addr)
+ input_addr += ms_hyperv.shared_gpa_boundary;
+
+ if (output_addr)
+ output_addr += ms_hyperv.shared_gpa_boundary;
+ }
+
+ args.r10 = control;
+ args.rdx = input_addr;
+ args.r8 = output_addr;
+
+ (void)__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT);
+
+ return args.r11;
+}
#endif
/*
diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
index 8a2cafec4675..1be7bcf0d7d1 100644
--- a/arch/x86/include/asm/mshyperv.h
+++ b/arch/x86/include/asm/mshyperv.h
@@ -39,6 +39,8 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
+u64 hv_tdx_hypercall(u64 control, u64 input_addr, u64 output_addr);
+
static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
{
u64 input_address = input ? virt_to_phys(input) : 0;
@@ -46,6 +48,9 @@ static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
u64 hv_status;
#ifdef CONFIG_X86_64
+ if (hv_isolation_type_tdx())
+ return hv_tdx_hypercall(control, input_address, output_address);
+
if (!hv_hypercall_pg)
return U64_MAX;
@@ -83,6 +88,9 @@ static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
#ifdef CONFIG_X86_64
+ if (hv_isolation_type_tdx())
+ return hv_tdx_hypercall(control, input1, 0);
+
{
__asm__ __volatile__(CALL_NOSPEC
: "=a" (hv_status), ASM_CALL_CONSTRAINT,
@@ -114,6 +122,9 @@ static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
#ifdef CONFIG_X86_64
+ if (hv_isolation_type_tdx())
+ return hv_tdx_hypercall(control, input1, input2);
+
{
__asm__ __volatile__("mov %4, %%r8\n"
CALL_NOSPEC