Skip to content

Commit 0b90c56

Browse files
committed
Merge tag 'hyperv-next-signed-20230902' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux
Pull hyperv updates from Wei Liu: - Support for SEV-SNP guests on Hyper-V (Tianyu Lan) - Support for TDX guests on Hyper-V (Dexuan Cui) - Use SBRM API in Hyper-V balloon driver (Mitchell Levy) - Avoid dereferencing ACPI root object handle in VMBus driver (Maciej Szmigiero) - A few misecllaneous fixes (Jiapeng Chong, Nathan Chancellor, Saurabh Sengar) * tag 'hyperv-next-signed-20230902' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux: (24 commits) x86/hyperv: Remove duplicate include x86/hyperv: Move the code in ivm.c around to avoid unnecessary ifdef's x86/hyperv: Remove hv_isolation_type_en_snp x86/hyperv: Use TDX GHCI to access some MSRs in a TDX VM with the paravisor Drivers: hv: vmbus: Bring the post_msg_page back for TDX VMs with the paravisor x86/hyperv: Introduce a global variable hyperv_paravisor_present Drivers: hv: vmbus: Support >64 VPs for a fully enlightened TDX/SNP VM x86/hyperv: Fix serial console interrupts for fully enlightened TDX guests Drivers: hv: vmbus: Support fully enlightened TDX guests x86/hyperv: Support hypercalls for fully enlightened TDX guests x86/hyperv: Add hv_isolation_type_tdx() to detect TDX guests x86/hyperv: Fix undefined reference to isolation_type_en_snp without CONFIG_HYPERV x86/hyperv: Add missing 'inline' to hv_snp_boot_ap() stub hv: hyperv.h: Replace one-element array with flexible-array member Drivers: hv: vmbus: Don't dereference ACPI root object handle x86/hyperv: Add hyperv-specific handling for VMMCALL under SEV-ES x86/hyperv: Add smp support for SEV-SNP guest clocksource: hyper-v: Mark hyperv tsc page unencrypted in sev-snp enlightened guest x86/hyperv: Use vmmcall to implement Hyper-V hypercall in sev-snp enlightened guest drivers: hv: Mark percpu hvcall input arg page unencrypted in SEV-SNP enlightened guest ...
2 parents e4f1b82 + 284930a commit 0b90c56

File tree

16 files changed

+759
-113
lines changed

16 files changed

+759
-113
lines changed

arch/x86/hyperv/hv_apic.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ static bool __send_ipi_mask(const struct cpumask *mask, int vector,
175175
(exclude_self && weight == 1 && cpumask_test_cpu(this_cpu, mask)))
176176
return true;
177177

178-
if (!hv_hypercall_pg)
179-
return false;
178+
/* A fully enlightened TDX VM uses GHCI rather than hv_hypercall_pg. */
179+
if (!hv_hypercall_pg) {
180+
if (ms_hyperv.paravisor_present || !hv_isolation_type_tdx())
181+
return false;
182+
}
180183

181184
if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
182185
return false;
@@ -229,9 +232,15 @@ static bool __send_ipi_one(int cpu, int vector)
229232

230233
trace_hyperv_send_ipi_one(cpu, vector);
231234

232-
if (!hv_hypercall_pg || (vp == VP_INVAL))
235+
if (vp == VP_INVAL)
233236
return false;
234237

238+
/* A fully enlightened TDX VM uses GHCI rather than hv_hypercall_pg. */
239+
if (!hv_hypercall_pg) {
240+
if (ms_hyperv.paravisor_present || !hv_isolation_type_tdx())
241+
return false;
242+
}
243+
235244
if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
236245
return false;
237246

arch/x86/hyperv/hv_init.c

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <asm/hyperv-tlfs.h>
2020
#include <asm/mshyperv.h>
2121
#include <asm/idtentry.h>
22+
#include <asm/set_memory.h>
2223
#include <linux/kexec.h>
2324
#include <linux/version.h>
2425
#include <linux/vmalloc.h>
@@ -52,7 +53,7 @@ static int hyperv_init_ghcb(void)
5253
void *ghcb_va;
5354
void **ghcb_base;
5455

55-
if (!hv_isolation_type_snp())
56+
if (!ms_hyperv.paravisor_present || !hv_isolation_type_snp())
5657
return 0;
5758

5859
if (!hv_ghcb_pg)
@@ -80,7 +81,7 @@ static int hyperv_init_ghcb(void)
8081
static int hv_cpu_init(unsigned int cpu)
8182
{
8283
union hv_vp_assist_msr_contents msr = { 0 };
83-
struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
84+
struct hv_vp_assist_page **hvp;
8485
int ret;
8586

8687
ret = hv_common_cpu_init(cpu);
@@ -90,6 +91,7 @@ static int hv_cpu_init(unsigned int cpu)
9091
if (!hv_vp_assist_page)
9192
return 0;
9293

94+
hvp = &hv_vp_assist_page[cpu];
9395
if (hv_root_partition) {
9496
/*
9597
* For root partition we get the hypervisor provided VP assist
@@ -107,8 +109,21 @@ static int hv_cpu_init(unsigned int cpu)
107109
* in hv_cpu_die(), otherwise a CPU may not be stopped in the
108110
* case of CPU offlining and the VM will hang.
109111
*/
110-
if (!*hvp)
112+
if (!*hvp) {
111113
*hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
114+
115+
/*
116+
* Hyper-V should never specify a VM that is a Confidential
117+
* VM and also running in the root partition. Root partition
118+
* is blocked to run in Confidential VM. So only decrypt assist
119+
* page in non-root partition here.
120+
*/
121+
if (*hvp && !ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
122+
WARN_ON_ONCE(set_memory_decrypted((unsigned long)(*hvp), 1));
123+
memset(*hvp, 0, PAGE_SIZE);
124+
}
125+
}
126+
112127
if (*hvp)
113128
msr.pfn = vmalloc_to_pfn(*hvp);
114129

@@ -379,6 +394,36 @@ static void __init hv_get_partition_id(void)
379394
local_irq_restore(flags);
380395
}
381396

397+
static u8 __init get_vtl(void)
398+
{
399+
u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS;
400+
struct hv_get_vp_registers_input *input;
401+
struct hv_get_vp_registers_output *output;
402+
unsigned long flags;
403+
u64 ret;
404+
405+
local_irq_save(flags);
406+
input = *this_cpu_ptr(hyperv_pcpu_input_arg);
407+
output = (struct hv_get_vp_registers_output *)input;
408+
409+
memset(input, 0, struct_size(input, element, 1));
410+
input->header.partitionid = HV_PARTITION_ID_SELF;
411+
input->header.vpindex = HV_VP_INDEX_SELF;
412+
input->header.inputvtl = 0;
413+
input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS;
414+
415+
ret = hv_do_hypercall(control, input, output);
416+
if (hv_result_success(ret)) {
417+
ret = output->as64.low & HV_X64_VTL_MASK;
418+
} else {
419+
pr_err("Failed to get VTL(%lld) and set VTL to zero by default.\n", ret);
420+
ret = 0;
421+
}
422+
423+
local_irq_restore(flags);
424+
return ret;
425+
}
426+
382427
/*
383428
* This function is to be invoked early in the boot sequence after the
384429
* hypervisor has been detected.
@@ -399,14 +444,24 @@ void __init hyperv_init(void)
399444
if (hv_common_init())
400445
return;
401446

402-
hv_vp_assist_page = kcalloc(num_possible_cpus(),
403-
sizeof(*hv_vp_assist_page), GFP_KERNEL);
447+
/*
448+
* The VP assist page is useless to a TDX guest: the only use we
449+
* would have for it is lazy EOI, which can not be used with TDX.
450+
*/
451+
if (hv_isolation_type_tdx())
452+
hv_vp_assist_page = NULL;
453+
else
454+
hv_vp_assist_page = kcalloc(num_possible_cpus(),
455+
sizeof(*hv_vp_assist_page),
456+
GFP_KERNEL);
404457
if (!hv_vp_assist_page) {
405458
ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
406-
goto common_free;
459+
460+
if (!hv_isolation_type_tdx())
461+
goto common_free;
407462
}
408463

409-
if (hv_isolation_type_snp()) {
464+
if (ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
410465
/* Negotiate GHCB Version. */
411466
if (!hv_ghcb_negotiate_protocol())
412467
hv_ghcb_terminate(SEV_TERM_SET_GEN,
@@ -426,12 +481,32 @@ void __init hyperv_init(void)
426481
* Setup the hypercall page and enable hypercalls.
427482
* 1. Register the guest ID
428483
* 2. Enable the hypercall and register the hypercall page
484+
*
485+
* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg:
486+
* when the hypercall input is a page, such a VM must pass a decrypted
487+
* page to Hyper-V, e.g. hv_post_message() uses the per-CPU page
488+
* hyperv_pcpu_input_arg, which is decrypted if no paravisor is present.
489+
*
490+
* A TDX VM with the paravisor uses hv_hypercall_pg for most hypercalls,
491+
* which are handled by the paravisor and the VM must use an encrypted
492+
* input page: in such a VM, the hyperv_pcpu_input_arg is encrypted and
493+
* used in the hypercalls, e.g. see hv_mark_gpa_visibility() and
494+
* hv_arch_irq_unmask(). Such a VM uses TDX GHCI for two hypercalls:
495+
* 1. HVCALL_SIGNAL_EVENT: see vmbus_set_event() and _hv_do_fast_hypercall8().
496+
* 2. HVCALL_POST_MESSAGE: the input page must be a decrypted page, i.e.
497+
* hv_post_message() in such a VM can't use the encrypted hyperv_pcpu_input_arg;
498+
* instead, hv_post_message() uses the post_msg_page, which is decrypted
499+
* in such a VM and is only used in such a VM.
429500
*/
430501
guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
431502
wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
432503

433-
/* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
434-
hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
504+
/* With the paravisor, the VM must also write the ID via GHCB/GHCI */
505+
hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
506+
507+
/* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg */
508+
if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
509+
goto skip_hypercall_pg_init;
435510

436511
hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
437512
VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
@@ -472,6 +547,7 @@ void __init hyperv_init(void)
472547
wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
473548
}
474549

550+
skip_hypercall_pg_init:
475551
/*
476552
* Some versions of Hyper-V that provide IBT in guest VMs have a bug
477553
* in that there's no ENDBR64 instruction at the entry to the
@@ -527,11 +603,15 @@ void __init hyperv_init(void)
527603
/* Query the VMs extended capability once, so that it can be cached. */
528604
hv_query_ext_cap(0);
529605

606+
/* Find the VTL */
607+
if (!ms_hyperv.paravisor_present && hv_isolation_type_snp())
608+
ms_hyperv.vtl = get_vtl();
609+
530610
return;
531611

532612
clean_guest_os_id:
533613
wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
534-
hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
614+
hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
535615
cpuhp_remove_state(cpuhp);
536616
free_ghcb_page:
537617
free_percpu(hv_ghcb_pg);
@@ -552,7 +632,7 @@ void hyperv_cleanup(void)
552632

553633
/* Reset our OS id */
554634
wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
555-
hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
635+
hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
556636

557637
/*
558638
* Reset hypercall page reference before reset the page,
@@ -615,6 +695,9 @@ bool hv_is_hyperv_initialized(void)
615695
if (x86_hyper_type != X86_HYPER_MS_HYPERV)
616696
return false;
617697

698+
/* A TDX VM with no paravisor uses TDX GHCI call rather than hv_hypercall_pg */
699+
if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
700+
return true;
618701
/*
619702
* Verify that earlier initialization succeeded by checking
620703
* that the hypercall page is setup

0 commit comments

Comments
 (0)