1 // SPDX-License-Identifier: GPL-2.0-only
3 * X86 specific Hyper-V initialization code.
5 * Copyright (C) 2016, Microsoft, Inc.
7 * Author : K. Y. Srinivasan <kys@microsoft.com>
10 #define pr_fmt(fmt) "Hyper-V: " fmt
12 #include <linux/efi.h>
13 #include <linux/types.h>
14 #include <linux/bitfield.h>
20 #include <asm/hypervisor.h>
21 #include <asm/hyperv-tlfs.h>
22 #include <asm/mshyperv.h>
23 #include <asm/idtentry.h>
24 #include <asm/set_memory.h>
25 #include <linux/kexec.h>
26 #include <linux/version.h>
27 #include <linux/vmalloc.h>
29 #include <linux/hyperv.h>
30 #include <linux/slab.h>
31 #include <linux/kernel.h>
32 #include <linux/cpuhotplug.h>
33 #include <linux/syscore_ops.h>
34 #include <clocksource/hyperv_timer.h>
35 #include <linux/highmem.h>
37 int hyperv_init_cpuhp;
38 u64 hv_current_partition_id = ~0ull;
39 EXPORT_SYMBOL_GPL(hv_current_partition_id);
41 void *hv_hypercall_pg;
42 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
44 union hv_ghcb * __percpu *hv_ghcb_pg;
46 /* Storage to save the hypercall page temporarily for hibernation */
47 static void *hv_hypercall_pg_saved;
49 struct hv_vp_assist_page **hv_vp_assist_page;
50 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
52 static int hyperv_init_ghcb(void)
58 if (!ms_hyperv.paravisor_present || !hv_isolation_type_snp())
65 * GHCB page is allocated by paravisor. The address
66 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
67 * memory boundary and map it here.
69 rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
71 /* Mask out vTOM bit. ioremap_cache() maps decrypted */
72 ghcb_gpa &= ~ms_hyperv.shared_gpa_boundary;
73 ghcb_va = (void *)ioremap_cache(ghcb_gpa, HV_HYP_PAGE_SIZE);
77 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
83 static int hv_cpu_init(unsigned int cpu)
85 union hv_vp_assist_msr_contents msr = { 0 };
86 struct hv_vp_assist_page **hvp;
89 ret = hv_common_cpu_init(cpu);
93 if (!hv_vp_assist_page)
96 hvp = &hv_vp_assist_page[cpu];
97 if (hv_root_partition) {
99 * For root partition we get the hypervisor provided VP assist
100 * page, instead of allocating a new page.
102 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
103 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
104 PAGE_SIZE, MEMREMAP_WB);
107 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
108 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
109 * out to make sure we always write the EOI MSR in
110 * hv_apic_eoi_write() *after* the EOI optimization is disabled
111 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
112 * case of CPU offlining and the VM will hang.
115 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
118 * Hyper-V should never specify a VM that is a Confidential
119 * VM and also running in the root partition. Root partition
120 * is blocked to run in Confidential VM. So only decrypt assist
121 * page in non-root partition here.
123 if (*hvp && !ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
124 WARN_ON_ONCE(set_memory_decrypted((unsigned long)(*hvp), 1));
125 memset(*hvp, 0, PAGE_SIZE);
130 msr.pfn = vmalloc_to_pfn(*hvp);
133 if (!WARN_ON(!(*hvp))) {
135 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
138 return hyperv_init_ghcb();
141 static void (*hv_reenlightenment_cb)(void);
143 static void hv_reenlightenment_notify(struct work_struct *dummy)
145 struct hv_tsc_emulation_status emu_status;
147 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
149 /* Don't issue the callback if TSC accesses are not emulated */
150 if (hv_reenlightenment_cb && emu_status.inprogress)
151 hv_reenlightenment_cb();
153 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
155 void hyperv_stop_tsc_emulation(void)
158 struct hv_tsc_emulation_status emu_status;
160 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
161 emu_status.inprogress = 0;
162 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
164 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
165 tsc_khz = div64_u64(freq, 1000);
167 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
169 static inline bool hv_reenlightenment_available(void)
172 * Check for required features and privileges to make TSC frequency
173 * change notifications work.
175 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
176 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
177 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
180 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
183 inc_irq_stat(irq_hv_reenlightenment_count);
184 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
187 void set_hv_tscchange_cb(void (*cb)(void))
189 struct hv_reenlightenment_control re_ctrl = {
190 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
193 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
195 if (!hv_reenlightenment_available()) {
196 pr_warn("reenlightenment support is unavailable\n");
203 hv_reenlightenment_cb = cb;
205 /* Make sure callback is registered before we write to MSRs */
208 re_ctrl.target_vp = hv_vp_index[get_cpu()];
210 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
211 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
215 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
217 void clear_hv_tscchange_cb(void)
219 struct hv_reenlightenment_control re_ctrl;
221 if (!hv_reenlightenment_available())
224 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
226 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
228 hv_reenlightenment_cb = NULL;
230 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
232 static int hv_cpu_die(unsigned int cpu)
234 struct hv_reenlightenment_control re_ctrl;
235 unsigned int new_cpu;
239 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
245 hv_common_cpu_die(cpu);
247 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
248 union hv_vp_assist_msr_contents msr = { 0 };
249 if (hv_root_partition) {
251 * For root partition the VP assist page is mapped to
252 * hypervisor provided page, and thus we unmap the
253 * page here and nullify it, so that in future we have
254 * correct page address mapped in hv_cpu_init.
256 memunmap(hv_vp_assist_page[cpu]);
257 hv_vp_assist_page[cpu] = NULL;
258 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
261 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
264 if (hv_reenlightenment_cb == NULL)
267 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
268 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
270 * Reassign reenlightenment notifications to some other online
271 * CPU or just disable the feature if there are no online CPUs
272 * left (happens on hibernation).
274 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
276 if (new_cpu < nr_cpu_ids)
277 re_ctrl.target_vp = hv_vp_index[new_cpu];
281 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
287 static int __init hv_pci_init(void)
289 int gen2vm = efi_enabled(EFI_BOOT);
292 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
293 * The purpose is to suppress the harmless warning:
294 * "PCI: Fatal: No config space access function found"
299 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
303 static int hv_suspend(void)
305 union hv_x64_msr_hypercall_contents hypercall_msr;
308 if (hv_root_partition)
312 * Reset the hypercall page as it is going to be invalidated
313 * across hibernation. Setting hv_hypercall_pg to NULL ensures
314 * that any subsequent hypercall operation fails safely instead of
315 * crashing due to an access of an invalid page. The hypercall page
316 * pointer is restored on resume.
318 hv_hypercall_pg_saved = hv_hypercall_pg;
319 hv_hypercall_pg = NULL;
321 /* Disable the hypercall page in the hypervisor */
322 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
323 hypercall_msr.enable = 0;
324 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
330 static void hv_resume(void)
332 union hv_x64_msr_hypercall_contents hypercall_msr;
335 ret = hv_cpu_init(0);
338 /* Re-enable the hypercall page */
339 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
340 hypercall_msr.enable = 1;
341 hypercall_msr.guest_physical_address =
342 vmalloc_to_pfn(hv_hypercall_pg_saved);
343 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
345 hv_hypercall_pg = hv_hypercall_pg_saved;
346 hv_hypercall_pg_saved = NULL;
349 * Reenlightenment notifications are disabled by hv_cpu_die(0),
350 * reenable them here if hv_reenlightenment_cb was previously set.
352 if (hv_reenlightenment_cb)
353 set_hv_tscchange_cb(hv_reenlightenment_cb);
356 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
357 static struct syscore_ops hv_syscore_ops = {
358 .suspend = hv_suspend,
362 static void (* __initdata old_setup_percpu_clockev)(void);
364 static void __init hv_stimer_setup_percpu_clockev(void)
367 * Ignore any errors in setting up stimer clockevents
368 * as we can run with the LAPIC timer as a fallback.
370 (void)hv_stimer_alloc(false);
373 * Still register the LAPIC timer, because the direct-mode STIMER is
374 * not supported by old versions of Hyper-V. This also allows users
375 * to switch to LAPIC timer via /sys, if they want to.
377 if (old_setup_percpu_clockev)
378 old_setup_percpu_clockev();
381 static void __init hv_get_partition_id(void)
383 struct hv_get_partition_id *output_page;
387 local_irq_save(flags);
388 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
389 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
390 if (!hv_result_success(status)) {
391 /* No point in proceeding if this failed */
392 pr_err("Failed to get partition ID: %lld\n", status);
395 hv_current_partition_id = output_page->partition_id;
396 local_irq_restore(flags);
399 #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)
400 static u8 __init get_vtl(void)
402 u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS;
403 struct hv_get_vp_registers_input *input;
404 struct hv_get_vp_registers_output *output;
408 local_irq_save(flags);
409 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
410 output = (struct hv_get_vp_registers_output *)input;
412 memset(input, 0, struct_size(input, element, 1));
413 input->header.partitionid = HV_PARTITION_ID_SELF;
414 input->header.vpindex = HV_VP_INDEX_SELF;
415 input->header.inputvtl = 0;
416 input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS;
418 ret = hv_do_hypercall(control, input, output);
419 if (hv_result_success(ret)) {
420 ret = output->as64.low & HV_X64_VTL_MASK;
422 pr_err("Failed to get VTL(error: %lld) exiting...\n", ret);
426 local_irq_restore(flags);
430 static inline u8 get_vtl(void) { return 0; }
434 * This function is to be invoked early in the boot sequence after the
435 * hypervisor has been detected.
437 * 1. Setup the hypercall page.
438 * 2. Register Hyper-V specific clocksource.
439 * 3. Setup Hyper-V specific APIC entry points.
441 void __init hyperv_init(void)
444 union hv_x64_msr_hypercall_contents hypercall_msr;
447 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
450 if (hv_common_init())
454 * The VP assist page is useless to a TDX guest: the only use we
455 * would have for it is lazy EOI, which can not be used with TDX.
457 if (hv_isolation_type_tdx())
458 hv_vp_assist_page = NULL;
460 hv_vp_assist_page = kcalloc(num_possible_cpus(),
461 sizeof(*hv_vp_assist_page),
463 if (!hv_vp_assist_page) {
464 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
466 if (!hv_isolation_type_tdx())
470 if (ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
471 /* Negotiate GHCB Version. */
472 if (!hv_ghcb_negotiate_protocol())
473 hv_ghcb_terminate(SEV_TERM_SET_GEN,
474 GHCB_SEV_ES_PROT_UNSUPPORTED);
476 hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
478 goto free_vp_assist_page;
481 cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online",
482 hv_cpu_init, hv_cpu_die);
487 * Setup the hypercall page and enable hypercalls.
488 * 1. Register the guest ID
489 * 2. Enable the hypercall and register the hypercall page
491 * A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg:
492 * when the hypercall input is a page, such a VM must pass a decrypted
493 * page to Hyper-V, e.g. hv_post_message() uses the per-CPU page
494 * hyperv_pcpu_input_arg, which is decrypted if no paravisor is present.
496 * A TDX VM with the paravisor uses hv_hypercall_pg for most hypercalls,
497 * which are handled by the paravisor and the VM must use an encrypted
498 * input page: in such a VM, the hyperv_pcpu_input_arg is encrypted and
499 * used in the hypercalls, e.g. see hv_mark_gpa_visibility() and
500 * hv_arch_irq_unmask(). Such a VM uses TDX GHCI for two hypercalls:
501 * 1. HVCALL_SIGNAL_EVENT: see vmbus_set_event() and _hv_do_fast_hypercall8().
502 * 2. HVCALL_POST_MESSAGE: the input page must be a decrypted page, i.e.
503 * hv_post_message() in such a VM can't use the encrypted hyperv_pcpu_input_arg;
504 * instead, hv_post_message() uses the post_msg_page, which is decrypted
505 * in such a VM and is only used in such a VM.
507 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
508 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
510 /* With the paravisor, the VM must also write the ID via GHCB/GHCI */
511 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
513 /* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg */
514 if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
515 goto skip_hypercall_pg_init;
517 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
518 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
519 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
520 __builtin_return_address(0));
521 if (hv_hypercall_pg == NULL)
522 goto clean_guest_os_id;
524 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
525 hypercall_msr.enable = 1;
527 if (hv_root_partition) {
532 * For the root partition, the hypervisor will set up its
533 * hypercall page. The hypervisor guarantees it will not show
534 * up in the root's address space. The root can't change the
535 * location of the hypercall page.
537 * Order is important here. We must enable the hypercall page
538 * so it is populated with code, then copy the code to an
541 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
543 pg = vmalloc_to_page(hv_hypercall_pg);
544 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
547 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
550 hv_remap_tsc_clocksource();
552 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
553 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
556 skip_hypercall_pg_init:
558 * Some versions of Hyper-V that provide IBT in guest VMs have a bug
559 * in that there's no ENDBR64 instruction at the entry to the
560 * hypercall page. Because hypercalls are invoked via an indirect call
561 * to the hypercall page, all hypercall attempts fail when IBT is
562 * enabled, and Linux panics. For such buggy versions, disable IBT.
564 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
565 * page, so if future Linux kernel versions enable IBT for 32-bit
566 * builds, additional hypercall page hackery will be required here
567 * to provide an ENDBR32.
569 #ifdef CONFIG_X86_KERNEL_IBT
570 if (cpu_feature_enabled(X86_FEATURE_IBT) &&
571 *(u32 *)hv_hypercall_pg != gen_endbr()) {
572 setup_clear_cpu_cap(X86_FEATURE_IBT);
573 pr_warn("Disabling IBT because of Hyper-V bug\n");
578 * hyperv_init() is called before LAPIC is initialized: see
579 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
580 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
581 * depends on LAPIC, so hv_stimer_alloc() should be called from
582 * x86_init.timers.setup_percpu_clockev.
584 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
585 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
589 x86_init.pci.arch_init = hv_pci_init;
591 register_syscore_ops(&hv_syscore_ops);
593 hyperv_init_cpuhp = cpuhp;
595 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
596 hv_get_partition_id();
598 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
600 #ifdef CONFIG_PCI_MSI
602 * If we're running as root, we want to create our own PCI MSI domain.
603 * We can't set this in hv_pci_init because that would be too late.
605 if (hv_root_partition)
606 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
609 /* Query the VMs extended capability once, so that it can be cached. */
613 ms_hyperv.vtl = get_vtl();
615 if (ms_hyperv.vtl > 0) /* non default VTL */
621 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
622 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
623 cpuhp_remove_state(cpuhp);
625 free_percpu(hv_ghcb_pg);
627 kfree(hv_vp_assist_page);
628 hv_vp_assist_page = NULL;
634 * This routine is called before kexec/kdump, it does the required cleanup.
636 void hyperv_cleanup(void)
638 union hv_x64_msr_hypercall_contents hypercall_msr;
639 union hv_reference_tsc_msr tsc_msr;
641 /* Reset our OS id */
642 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
643 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
646 * Reset hypercall page reference before reset the page,
647 * let hypercall operations fail safely rather than
648 * panic the kernel for using invalid hypercall page
650 hv_hypercall_pg = NULL;
652 /* Reset the hypercall page */
653 hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
654 hypercall_msr.enable = 0;
655 hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
657 /* Reset the TSC page */
658 tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
660 hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
663 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
665 static bool panic_reported;
668 if (in_die && !panic_on_oops)
672 * We prefer to report panic on 'die' chain as we have proper
673 * registers to report, but if we miss it (e.g. on BUG()) we need
674 * to report it on 'panic'.
678 panic_reported = true;
680 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
682 wrmsrl(HV_X64_MSR_CRASH_P0, err);
683 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
684 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
685 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
686 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
689 * Let Hyper-V know there is crash data available
691 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
693 EXPORT_SYMBOL_GPL(hyperv_report_panic);
695 bool hv_is_hyperv_initialized(void)
697 union hv_x64_msr_hypercall_contents hypercall_msr;
700 * Ensure that we're really on Hyper-V, and not a KVM or Xen
701 * emulation of Hyper-V
703 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
706 /* A TDX VM with no paravisor uses TDX GHCI call rather than hv_hypercall_pg */
707 if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
710 * Verify that earlier initialization succeeded by checking
711 * that the hypercall page is setup
713 hypercall_msr.as_uint64 = 0;
714 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
716 return hypercall_msr.enable;
718 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);