RE: [PATCH 5/6] x86/hyperv: Support hypercalls for TDX guests

From: Michael Kelley (LINUX)
Date: Wed Nov 23 2022 - 09:45:32 EST


From: Dexuan Cui <decui@xxxxxxxxxxxxx> Sent: Monday, November 21, 2022 11:52 AM
>
> A TDX guest uses the GHCI call rather than hv_hypercall_pg.
>
> In hv_do_hypercall(), Hyper-V requires that the input/output addresses
> must have the vTOM bit set. With current Hyper-V, the bit for TDX is
> bit 47, which is saved into ms_hyperv.shared_gpa_boundary() in
> ms_hyperv_init_platform().
>
> arch/x86/include/asm/mshyperv.h: hv_do_hypercall() needs
> "struct ms_hyperv_info", which is defined in
> include/asm-generic/mshyperv.h, which can't be included in
> arch/x86/include/asm/mshyperv.h because include/asm-generic/mshyperv.h
> has vmbus_signal_eom() -> hv_set_register(), which is defined in
> arch/x86/include/asm/mshyperv.h.
>
> Break this circular dependency by introducing a new header file
> for "struct ms_hyperv_info".
>
> Signed-off-by: Dexuan Cui <decui@xxxxxxxxxxxxx>
> ---
> MAINTAINERS | 1 +
> arch/x86/hyperv/hv_init.c | 8 ++++++++
> arch/x86/include/asm/mshyperv.h | 24 ++++++++++++++++++++++-
> arch/x86/kernel/cpu/mshyperv.c | 2 ++
> include/asm-generic/ms_hyperv_info.h | 29 ++++++++++++++++++++++++++++
> include/asm-generic/mshyperv.h | 24 +----------------------
> 6 files changed, 64 insertions(+), 24 deletions(-)
> create mode 100644 include/asm-generic/ms_hyperv_info.h
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 256f03904987..455ecaf188fe 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -9537,6 +9537,7 @@ F: drivers/scsi/storvsc_drv.c
> F: drivers/uio/uio_hv_generic.c
> F: drivers/video/fbdev/hyperv_fb.c
> F: include/asm-generic/hyperv-tlfs.h
> +F: include/asm-generic/ms_hyperv_info.h
> F: include/asm-generic/mshyperv.h
> F: include/clocksource/hyperv_timer.h
> F: include/linux/hyperv.h
> diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
> index 89954490af93..05682c4e327f 100644
> --- a/arch/x86/hyperv/hv_init.c
> +++ b/arch/x86/hyperv/hv_init.c
> @@ -432,6 +432,10 @@ void __init hyperv_init(void)
> /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
> hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
>
> + /* A TDX guest uses the GHCI call rather than hv_hypercall_pg. */
> + if (hv_isolation_type_tdx())
> + goto skip_hypercall_pg_init;
> +
> hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
> VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
> VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
> @@ -471,6 +475,7 @@ void __init hyperv_init(void)
> wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
> }
>
> +skip_hypercall_pg_init:
> /*
> * hyperv_init() is called before LAPIC is initialized: see
> * apic_intr_mode_init() -> x86_platform.apic_post_init() and
> @@ -606,6 +611,9 @@ bool hv_is_hyperv_initialized(void)
> if (x86_hyper_type != X86_HYPER_MS_HYPERV)
> return false;
>
> + /* A TDX guest uses the GHCI call rather than hv_hypercall_pg. */
> + if (hv_isolation_type_tdx())
> + return true;
> /*
> * Verify that earlier initialization succeeded by checking
> * that the hypercall page is setup
> diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
> index 9d593ab2be26..650b4fae2fd8 100644
> --- a/arch/x86/include/asm/mshyperv.h
> +++ b/arch/x86/include/asm/mshyperv.h
> @@ -9,7 +9,7 @@
> #include <asm/hyperv-tlfs.h>
> #include <asm/nospec-branch.h>
> #include <asm/paravirt.h>
> -#include <asm/mshyperv.h>
> +#include <asm-generic/ms_hyperv_info.h>
>
> union hv_ghcb;
>
> @@ -48,6 +48,18 @@ static inline u64 hv_do_hypercall(u64 control, void *input, void
> *output)
> u64 hv_status;
>
> #ifdef CONFIG_X86_64
> +#if CONFIG_INTEL_TDX_GUEST
> + if (hv_isolation_type_tdx()) {
> + if (input_address)
> + input_address += ms_hyperv.shared_gpa_boundary;
> +
> + if (output_address)
> + output_address += ms_hyperv.shared_gpa_boundary;
> +
> + return __tdx_ms_hv_hypercall(control, output_address,
> + input_address);
> + }
> +#endif

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

> if (!hv_hypercall_pg)
> return U64_MAX;
>
> @@ -85,6 +97,11 @@ 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 CONFIG_INTEL_TDX_GUEST
> + if (hv_isolation_type_tdx())
> + return __tdx_ms_hv_hypercall(control, 0, input1);
> +#endif
> +
> {
> __asm__ __volatile__(CALL_NOSPEC
> : "=a" (hv_status), ASM_CALL_CONSTRAINT,
> @@ -116,6 +133,11 @@ 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 CONFIG_INTEL_TDX_GUEST
> + if (hv_isolation_type_tdx())
> + return __tdx_ms_hv_hypercall(control, input2, input1);
> +#endif
> +
> {
> __asm__ __volatile__("mov %4, %%r8\n"
> CALL_NOSPEC
> diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
> index 9ad0b0abf0e0..dddccdbc5526 100644
> --- a/arch/x86/kernel/cpu/mshyperv.c
> +++ b/arch/x86/kernel/cpu/mshyperv.c
> @@ -349,6 +349,8 @@ static void __init ms_hyperv_init_platform(void)
>
> case HV_ISOLATION_TYPE_TDX:
> static_branch_enable(&isolation_type_tdx);
> +
> + ms_hyperv.shared_gpa_boundary = cc_mkdec(0);
> break;
>
> default:
> diff --git a/include/asm-generic/ms_hyperv_info.h b/include/asm-
> generic/ms_hyperv_info.h
> new file mode 100644
> index 000000000000..734583dfea99
> --- /dev/null
> +++ b/include/asm-generic/ms_hyperv_info.h
> @@ -0,0 +1,29 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +
> +#ifndef _ASM_GENERIC_MS_HYPERV_INFO_H
> +#define _ASM_GENERIC_MS_HYPERV_INFO_H
> +
> +struct ms_hyperv_info {
> + u32 features;
> + u32 priv_high;
> + u32 misc_features;
> + u32 hints;
> + u32 nested_features;
> + u32 max_vp_index;
> + u32 max_lp_index;
> + u32 isolation_config_a;
> + union {
> + u32 isolation_config_b;
> + struct {
> + u32 cvm_type : 4;
> + u32 reserved1 : 1;
> + u32 shared_gpa_boundary_active : 1;
> + u32 shared_gpa_boundary_bits : 6;
> + u32 reserved2 : 20;
> + };
> + };
> + u64 shared_gpa_boundary;
> +};
> +extern struct ms_hyperv_info ms_hyperv;
> +
> +#endif
> diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h
> index bfb9eb9d7215..2ae3e4e4256b 100644
> --- a/include/asm-generic/mshyperv.h
> +++ b/include/asm-generic/mshyperv.h
> @@ -25,29 +25,7 @@
> #include <linux/nmi.h>
> #include <asm/ptrace.h>
> #include <asm/hyperv-tlfs.h>
> -
> -struct ms_hyperv_info {
> - u32 features;
> - u32 priv_high;
> - u32 misc_features;
> - u32 hints;
> - u32 nested_features;
> - u32 max_vp_index;
> - u32 max_lp_index;
> - u32 isolation_config_a;
> - union {
> - u32 isolation_config_b;
> - struct {
> - u32 cvm_type : 4;
> - u32 reserved1 : 1;
> - u32 shared_gpa_boundary_active : 1;
> - u32 shared_gpa_boundary_bits : 6;
> - u32 reserved2 : 20;
> - };
> - };
> - u64 shared_gpa_boundary;
> -};
> -extern struct ms_hyperv_info ms_hyperv;
> +#include <asm-generic/ms_hyperv_info.h>
>
> extern void * __percpu *hyperv_pcpu_input_arg;
> extern void * __percpu *hyperv_pcpu_output_arg;
> --
> 2.25.1