1 // SPDX-License-Identifier: GPL-2.0-only
3 * X86 specific Hyper-V initialization code.
5 * Copyright (C) 2016, Microsoft, Inc.
10 #include <linux/efi.h>
11 #include <linux/types.h>
12 #include <linux/bitfield.h>
18 #include <asm/hypervisor.h>
19 #include <asm/hyperv-tlfs.h>
20 #include <asm/mshyperv.h>
21 #include <asm/idtentry.h>
22 #include <asm/set_memory.h>
23 #include <linux/kexec.h>
24 #include <linux/version.h>
25 #include <linux/vmalloc.h>
27 #include <linux/hyperv.h>
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
30 #include <linux/cpuhotplug.h>
31 #include <linux/syscore_ops.h>
32 #include <clocksource/hyperv_timer.h>
33 #include <linux/highmem.h>
35 int hyperv_init_cpuhp;
36 u64 hv_current_partition_id = ~0ull;
37 EXPORT_SYMBOL_GPL(hv_current_partition_id);
39 void *hv_hypercall_pg;
40 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
42 union hv_ghcb * __percpu *hv_ghcb_pg;
44 /* Storage to save the hypercall page temporarily for hibernation */
45 static void *hv_hypercall_pg_saved;
47 struct hv_vp_assist_page **hv_vp_assist_page;
48 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
50 static int hyperv_init_ghcb(void)
56 if (!ms_hyperv.paravisor_present || !hv_isolation_type_snp())
63 * GHCB page is allocated by paravisor. The address
64 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
65 * memory boundary and map it here.
67 rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
69 /* Mask out vTOM bit. ioremap_cache() maps decrypted */
70 ghcb_gpa &= ~ms_hyperv.shared_gpa_boundary;
71 ghcb_va = (void *)ioremap_cache(ghcb_gpa, HV_HYP_PAGE_SIZE);
75 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
81 static int hv_cpu_init(unsigned int cpu)
83 union hv_vp_assist_msr_contents msr = { 0 };
84 struct hv_vp_assist_page **hvp;
87 ret = hv_common_cpu_init(cpu);
91 if (!hv_vp_assist_page)
94 hvp = &hv_vp_assist_page[cpu];
95 if (hv_root_partition) {
97 * For root partition we get the hypervisor provided VP assist
98 * page, instead of allocating a new page.
100 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
101 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
102 PAGE_SIZE, MEMREMAP_WB);
105 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
106 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
107 * out to make sure we always write the EOI MSR in
108 * hv_apic_eoi_write() *after* the EOI optimization is disabled
109 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
110 * case of CPU offlining and the VM will hang.
113 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
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.
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);
128 msr.pfn = vmalloc_to_pfn(*hvp);
131 if (!WARN_ON(!(*hvp))) {
133 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
136 return hyperv_init_ghcb();
139 static void (*hv_reenlightenment_cb)(void);
141 static void hv_reenlightenment_notify(struct work_struct *dummy)
143 struct hv_tsc_emulation_status emu_status;
145 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
147 /* Don't issue the callback if TSC accesses are not emulated */
148 if (hv_reenlightenment_cb && emu_status.inprogress)
149 hv_reenlightenment_cb();
151 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
153 void hyperv_stop_tsc_emulation(void)
156 struct hv_tsc_emulation_status emu_status;
158 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
159 emu_status.inprogress = 0;
160 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
162 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
163 tsc_khz = div64_u64(freq, 1000);
165 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
167 static inline bool hv_reenlightenment_available(void)
170 * Check for required features and privileges to make TSC frequency
171 * change notifications work.
173 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
174 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
175 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
178 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
181 inc_irq_stat(irq_hv_reenlightenment_count);
182 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
185 void set_hv_tscchange_cb(void (*cb)(void))
187 struct hv_reenlightenment_control re_ctrl = {
188 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
191 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
193 if (!hv_reenlightenment_available()) {
194 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
201 hv_reenlightenment_cb = cb;
203 /* Make sure callback is registered before we write to MSRs */
206 re_ctrl.target_vp = hv_vp_index[get_cpu()];
208 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
209 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
213 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
215 void clear_hv_tscchange_cb(void)
217 struct hv_reenlightenment_control re_ctrl;
219 if (!hv_reenlightenment_available())
222 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
224 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
226 hv_reenlightenment_cb = NULL;
228 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
230 static int hv_cpu_die(unsigned int cpu)
232 struct hv_reenlightenment_control re_ctrl;
233 unsigned int new_cpu;
237 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
243 hv_common_cpu_die(cpu);
245 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
246 union hv_vp_assist_msr_contents msr = { 0 };
247 if (hv_root_partition) {
249 * For root partition the VP assist page is mapped to
250 * hypervisor provided page, and thus we unmap the
251 * page here and nullify it, so that in future we have
252 * correct page address mapped in hv_cpu_init.
254 memunmap(hv_vp_assist_page[cpu]);
255 hv_vp_assist_page[cpu] = NULL;
256 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
259 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
262 if (hv_reenlightenment_cb == NULL)
265 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
266 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
268 * Reassign reenlightenment notifications to some other online
269 * CPU or just disable the feature if there are no online CPUs
270 * left (happens on hibernation).
272 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
274 if (new_cpu < nr_cpu_ids)
275 re_ctrl.target_vp = hv_vp_index[new_cpu];
279 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
285 static int __init hv_pci_init(void)
287 int gen2vm = efi_enabled(EFI_BOOT);
290 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
291 * The purpose is to suppress the harmless warning:
292 * "PCI: Fatal: No config space access function found"
297 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
301 static int hv_suspend(void)
303 union hv_x64_msr_hypercall_contents hypercall_msr;
306 if (hv_root_partition)
310 * Reset the hypercall page as it is going to be invalidated
311 * across hibernation. Setting hv_hypercall_pg to NULL ensures
312 * that any subsequent hypercall operation fails safely instead of
313 * crashing due to an access of an invalid page. The hypercall page
314 * pointer is restored on resume.
316 hv_hypercall_pg_saved = hv_hypercall_pg;
317 hv_hypercall_pg = NULL;
319 /* Disable the hypercall page in the hypervisor */
320 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
321 hypercall_msr.enable = 0;
322 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
328 static void hv_resume(void)
330 union hv_x64_msr_hypercall_contents hypercall_msr;
333 ret = hv_cpu_init(0);
336 /* Re-enable the hypercall page */
337 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
338 hypercall_msr.enable = 1;
339 hypercall_msr.guest_physical_address =
340 vmalloc_to_pfn(hv_hypercall_pg_saved);
341 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
343 hv_hypercall_pg = hv_hypercall_pg_saved;
344 hv_hypercall_pg_saved = NULL;
347 * Reenlightenment notifications are disabled by hv_cpu_die(0),
348 * reenable them here if hv_reenlightenment_cb was previously set.
350 if (hv_reenlightenment_cb)
351 set_hv_tscchange_cb(hv_reenlightenment_cb);
354 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
355 static struct syscore_ops hv_syscore_ops = {
356 .suspend = hv_suspend,
360 static void (* __initdata old_setup_percpu_clockev)(void);
362 static void __init hv_stimer_setup_percpu_clockev(void)
365 * Ignore any errors in setting up stimer clockevents
366 * as we can run with the LAPIC timer as a fallback.
368 (void)hv_stimer_alloc(false);
371 * Still register the LAPIC timer, because the direct-mode STIMER is
372 * not supported by old versions of Hyper-V. This also allows users
373 * to switch to LAPIC timer via /sys, if they want to.
375 if (old_setup_percpu_clockev)
376 old_setup_percpu_clockev();
379 static void __init hv_get_partition_id(void)
381 struct hv_get_partition_id *output_page;
385 local_irq_save(flags);
386 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
387 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
388 if (!hv_result_success(status)) {
389 /* No point in proceeding if this failed */
390 pr_err("Failed to get partition ID: %lld\n", status);
393 hv_current_partition_id = output_page->partition_id;
394 local_irq_restore(flags);
397 static u8 __init get_vtl(void)
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;
405 local_irq_save(flags);
406 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
407 output = (struct hv_get_vp_registers_output *)input;
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;
415 ret = hv_do_hypercall(control, input, output);
416 if (hv_result_success(ret)) {
417 ret = output->as64.low & HV_X64_VTL_MASK;
419 pr_err("Failed to get VTL(%lld) and set VTL to zero by default.\n", ret);
423 local_irq_restore(flags);
428 * This function is to be invoked early in the boot sequence after the
429 * hypervisor has been detected.
431 * 1. Setup the hypercall page.
432 * 2. Register Hyper-V specific clocksource.
433 * 3. Setup Hyper-V specific APIC entry points.
435 void __init hyperv_init(void)
438 union hv_x64_msr_hypercall_contents hypercall_msr;
441 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
444 if (hv_common_init())
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.
451 if (hv_isolation_type_tdx())
452 hv_vp_assist_page = NULL;
454 hv_vp_assist_page = kcalloc(num_possible_cpus(),
455 sizeof(*hv_vp_assist_page),
457 if (!hv_vp_assist_page) {
458 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
460 if (!hv_isolation_type_tdx())
464 if (ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
465 /* Negotiate GHCB Version. */
466 if (!hv_ghcb_negotiate_protocol())
467 hv_ghcb_terminate(SEV_TERM_SET_GEN,
468 GHCB_SEV_ES_PROT_UNSUPPORTED);
470 hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
472 goto free_vp_assist_page;
475 cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online",
476 hv_cpu_init, hv_cpu_die);
481 * Setup the hypercall page and enable hypercalls.
482 * 1. Register the guest ID
483 * 2. Enable the hypercall and register the hypercall page
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.
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.
501 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
502 wrmsrl(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);
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;
511 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
512 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
513 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
514 __builtin_return_address(0));
515 if (hv_hypercall_pg == NULL)
516 goto clean_guest_os_id;
518 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
519 hypercall_msr.enable = 1;
521 if (hv_root_partition) {
526 * For the root partition, the hypervisor will set up its
527 * hypercall page. The hypervisor guarantees it will not show
528 * up in the root's address space. The root can't change the
529 * location of the hypercall page.
531 * Order is important here. We must enable the hypercall page
532 * so it is populated with code, then copy the code to an
535 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
537 pg = vmalloc_to_page(hv_hypercall_pg);
538 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
541 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
544 hv_remap_tsc_clocksource();
546 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
547 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
550 skip_hypercall_pg_init:
552 * Some versions of Hyper-V that provide IBT in guest VMs have a bug
553 * in that there's no ENDBR64 instruction at the entry to the
554 * hypercall page. Because hypercalls are invoked via an indirect call
555 * to the hypercall page, all hypercall attempts fail when IBT is
556 * enabled, and Linux panics. For such buggy versions, disable IBT.
558 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
559 * page, so if future Linux kernel versions enable IBT for 32-bit
560 * builds, additional hypercall page hackery will be required here
561 * to provide an ENDBR32.
563 #ifdef CONFIG_X86_KERNEL_IBT
564 if (cpu_feature_enabled(X86_FEATURE_IBT) &&
565 *(u32 *)hv_hypercall_pg != gen_endbr()) {
566 setup_clear_cpu_cap(X86_FEATURE_IBT);
567 pr_warn("Hyper-V: Disabling IBT because of Hyper-V bug\n");
572 * hyperv_init() is called before LAPIC is initialized: see
573 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
574 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
575 * depends on LAPIC, so hv_stimer_alloc() should be called from
576 * x86_init.timers.setup_percpu_clockev.
578 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
579 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
583 x86_init.pci.arch_init = hv_pci_init;
585 register_syscore_ops(&hv_syscore_ops);
587 hyperv_init_cpuhp = cpuhp;
589 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
590 hv_get_partition_id();
592 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
594 #ifdef CONFIG_PCI_MSI
596 * If we're running as root, we want to create our own PCI MSI domain.
597 * We can't set this in hv_pci_init because that would be too late.
599 if (hv_root_partition)
600 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
603 /* Query the VMs extended capability once, so that it can be cached. */
607 if (!ms_hyperv.paravisor_present && hv_isolation_type_snp())
608 ms_hyperv.vtl = get_vtl();
613 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
614 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
615 cpuhp_remove_state(cpuhp);
617 free_percpu(hv_ghcb_pg);
619 kfree(hv_vp_assist_page);
620 hv_vp_assist_page = NULL;
626 * This routine is called before kexec/kdump, it does the required cleanup.
628 void hyperv_cleanup(void)
630 union hv_x64_msr_hypercall_contents hypercall_msr;
631 union hv_reference_tsc_msr tsc_msr;
633 /* Reset our OS id */
634 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
635 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
638 * Reset hypercall page reference before reset the page,
639 * let hypercall operations fail safely rather than
640 * panic the kernel for using invalid hypercall page
642 hv_hypercall_pg = NULL;
644 /* Reset the hypercall page */
645 hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
646 hypercall_msr.enable = 0;
647 hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
649 /* Reset the TSC page */
650 tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
652 hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
655 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
657 static bool panic_reported;
660 if (in_die && !panic_on_oops)
664 * We prefer to report panic on 'die' chain as we have proper
665 * registers to report, but if we miss it (e.g. on BUG()) we need
666 * to report it on 'panic'.
670 panic_reported = true;
672 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
674 wrmsrl(HV_X64_MSR_CRASH_P0, err);
675 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
676 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
677 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
678 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
681 * Let Hyper-V know there is crash data available
683 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
685 EXPORT_SYMBOL_GPL(hyperv_report_panic);
687 bool hv_is_hyperv_initialized(void)
689 union hv_x64_msr_hypercall_contents hypercall_msr;
692 * Ensure that we're really on Hyper-V, and not a KVM or Xen
693 * emulation of Hyper-V
695 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
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)
702 * Verify that earlier initialization succeeded by checking
703 * that the hypercall page is setup
705 hypercall_msr.as_uint64 = 0;
706 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
708 return hypercall_msr.enable;
710 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);