Skip to content

Commit d6e0228

Browse files
dcuiliuw
authored andcommitted
x86/hyperv: Support hypercalls for fully enlightened TDX guests
A fully enlightened TDX guest on Hyper-V (i.e. without the paravisor) only uses the GHCI call rather than hv_hypercall_pg. Do not initialize hypercall_pg for such a guest. In hv_common_cpu_init(), the hyperv_pcpu_input_arg page needs to be decrypted in such a guest. Reviewed-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Reviewed-by: Tianyu Lan <tiala@microsoft.com> Signed-off-by: Dexuan Cui <decui@microsoft.com> Signed-off-by: Wei Liu <wei.liu@kernel.org> Link: https://lore.kernel.org/r/20230824080712.30327-3-decui@microsoft.com
1 parent 08e9d12 commit d6e0228

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

arch/x86/hyperv/hv_init.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ void __init hyperv_init(void)
476476
/* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
477477
hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
478478

479+
/* A TDX guest uses the GHCI call rather than hv_hypercall_pg. */
480+
if (hv_isolation_type_tdx())
481+
goto skip_hypercall_pg_init;
482+
479483
hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
480484
VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
481485
VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
@@ -515,6 +519,7 @@ void __init hyperv_init(void)
515519
wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
516520
}
517521

522+
skip_hypercall_pg_init:
518523
/*
519524
* hyperv_init() is called before LAPIC is initialized: see
520525
* apic_intr_mode_init() -> x86_platform.apic_post_init() and
@@ -642,6 +647,9 @@ bool hv_is_hyperv_initialized(void)
642647
if (x86_hyper_type != X86_HYPER_MS_HYPERV)
643648
return false;
644649

650+
/* A TDX guest uses the GHCI call rather than hv_hypercall_pg. */
651+
if (hv_isolation_type_tdx())
652+
return true;
645653
/*
646654
* Verify that earlier initialization succeeded by checking
647655
* that the hypercall page is setup

arch/x86/hyperv/ivm.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,20 @@ bool hv_isolation_type_tdx(void)
571571
{
572572
return static_branch_unlikely(&isolation_type_tdx);
573573
}
574+
575+
#ifdef CONFIG_INTEL_TDX_GUEST
576+
577+
u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2)
578+
{
579+
struct tdx_hypercall_args args = { };
580+
581+
args.r10 = control;
582+
args.rdx = param1;
583+
args.r8 = param2;
584+
585+
(void)__tdx_hypercall_ret(&args);
586+
587+
return args.r11;
588+
}
589+
590+
#endif

arch/x86/include/asm/mshyperv.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern union hv_ghcb * __percpu *hv_ghcb_pg;
5151

5252
extern bool hv_isolation_type_en_snp(void);
5353
bool hv_isolation_type_tdx(void);
54+
u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
5455

5556
/*
5657
* DEFAULT INIT GPAT and SEGMENT LIMIT value in struct VMSA
@@ -63,13 +64,20 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
6364
int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
6465
int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
6566

67+
/*
68+
* If the hypercall involves no input or output parameters, the hypervisor
69+
* ignores the corresponding GPA pointer.
70+
*/
6671
static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
6772
{
6873
u64 input_address = input ? virt_to_phys(input) : 0;
6974
u64 output_address = output ? virt_to_phys(output) : 0;
7075
u64 hv_status;
7176

7277
#ifdef CONFIG_X86_64
78+
if (hv_isolation_type_tdx())
79+
return hv_tdx_hypercall(control, input_address, output_address);
80+
7381
if (hv_isolation_type_en_snp()) {
7482
__asm__ __volatile__("mov %4, %%r8\n"
7583
"vmmcall"
@@ -123,6 +131,9 @@ static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
123131
u64 hv_status;
124132

125133
#ifdef CONFIG_X86_64
134+
if (hv_isolation_type_tdx())
135+
return hv_tdx_hypercall(control, input1, 0);
136+
126137
if (hv_isolation_type_en_snp()) {
127138
__asm__ __volatile__(
128139
"vmmcall"
@@ -174,6 +185,9 @@ static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
174185
u64 hv_status;
175186

176187
#ifdef CONFIG_X86_64
188+
if (hv_isolation_type_tdx())
189+
return hv_tdx_hypercall(control, input1, input2);
190+
177191
if (hv_isolation_type_en_snp()) {
178192
__asm__ __volatile__("mov %4, %%r8\n"
179193
"vmmcall"

drivers/hv/hv_common.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,10 @@ int hv_common_cpu_init(unsigned int cpu)
381381
*outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE;
382382
}
383383

384-
if (hv_isolation_type_en_snp()) {
384+
if (hv_isolation_type_en_snp() || hv_isolation_type_tdx()) {
385385
ret = set_memory_decrypted((unsigned long)*inputarg, pgcount);
386386
if (ret) {
387-
kfree(*inputarg);
387+
/* It may be unsafe to free *inputarg */
388388
*inputarg = NULL;
389389
return ret;
390390
}
@@ -567,3 +567,9 @@ u64 __weak hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_s
567567
return HV_STATUS_INVALID_PARAMETER;
568568
}
569569
EXPORT_SYMBOL_GPL(hv_ghcb_hypercall);
570+
571+
u64 __weak hv_tdx_hypercall(u64 control, u64 param1, u64 param2)
572+
{
573+
return HV_STATUS_INVALID_PARAMETER;
574+
}
575+
EXPORT_SYMBOL_GPL(hv_tdx_hypercall);

include/asm-generic/mshyperv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ enum hv_isolation_type hv_get_isolation_type(void);
283283
bool hv_is_isolation_supported(void);
284284
bool hv_isolation_type_snp(void);
285285
u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
286+
u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
286287
void hyperv_cleanup(void);
287288
bool hv_query_ext_cap(u64 cap_query);
288289
void hv_setup_dma_ops(struct device *dev, bool coherent);

0 commit comments

Comments
 (0)