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 #include <linux/efi.h>
11 #include <linux/types.h>
12 #include <linux/bitfield.h>
16 #include <asm/e820/api.h>
19 #include <asm/hypervisor.h>
20 #include <asm/hyperv-tlfs.h>
21 #include <asm/mshyperv.h>
22 #include <asm/idtentry.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>
34 #include <linux/swiotlb.h>
36 int hyperv_init_cpuhp;
37 u64 hv_current_partition_id = ~0ull;
38 EXPORT_SYMBOL_GPL(hv_current_partition_id);
40 void *hv_hypercall_pg;
41 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
43 union hv_ghcb * __percpu *hv_ghcb_pg;
45 /* Storage to save the hypercall page temporarily for hibernation */
46 static void *hv_hypercall_pg_saved;
48 struct hv_vp_assist_page **hv_vp_assist_page;
49 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
51 static int hyperv_init_ghcb(void)
57 if (!hv_isolation_type_snp())
64 * GHCB page is allocated by paravisor. The address
65 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
66 * memory boundary and map it here.
68 rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
69 ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB);
73 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
79 static int hv_cpu_init(unsigned int cpu)
81 union hv_vp_assist_msr_contents msr = { 0 };
82 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
85 ret = hv_common_cpu_init(cpu);
89 if (!hv_vp_assist_page)
92 if (hv_root_partition) {
94 * For root partition we get the hypervisor provided VP assist
95 * page, instead of allocating a new page.
97 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
98 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
99 PAGE_SIZE, MEMREMAP_WB);
102 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
103 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
104 * out to make sure we always write the EOI MSR in
105 * hv_apic_eoi_write() *after* the EOI optimization is disabled
106 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
107 * case of CPU offlining and the VM will hang.
110 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
112 msr.pfn = vmalloc_to_pfn(*hvp);
115 if (!WARN_ON(!(*hvp))) {
117 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
120 return hyperv_init_ghcb();
123 static void (*hv_reenlightenment_cb)(void);
125 static void hv_reenlightenment_notify(struct work_struct *dummy)
127 struct hv_tsc_emulation_status emu_status;
129 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
131 /* Don't issue the callback if TSC accesses are not emulated */
132 if (hv_reenlightenment_cb && emu_status.inprogress)
133 hv_reenlightenment_cb();
135 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
137 void hyperv_stop_tsc_emulation(void)
140 struct hv_tsc_emulation_status emu_status;
142 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
143 emu_status.inprogress = 0;
144 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
146 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
147 tsc_khz = div64_u64(freq, 1000);
149 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
151 static inline bool hv_reenlightenment_available(void)
154 * Check for required features and privileges to make TSC frequency
155 * change notifications work.
157 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
158 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
159 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
162 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
165 inc_irq_stat(irq_hv_reenlightenment_count);
166 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
169 void set_hv_tscchange_cb(void (*cb)(void))
171 struct hv_reenlightenment_control re_ctrl = {
172 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
175 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
177 if (!hv_reenlightenment_available()) {
178 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
185 hv_reenlightenment_cb = cb;
187 /* Make sure callback is registered before we write to MSRs */
190 re_ctrl.target_vp = hv_vp_index[get_cpu()];
192 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
193 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
197 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
199 void clear_hv_tscchange_cb(void)
201 struct hv_reenlightenment_control re_ctrl;
203 if (!hv_reenlightenment_available())
206 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
208 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
210 hv_reenlightenment_cb = NULL;
212 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
214 static int hv_cpu_die(unsigned int cpu)
216 struct hv_reenlightenment_control re_ctrl;
217 unsigned int new_cpu;
221 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
227 hv_common_cpu_die(cpu);
229 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
230 union hv_vp_assist_msr_contents msr = { 0 };
231 if (hv_root_partition) {
233 * For root partition the VP assist page is mapped to
234 * hypervisor provided page, and thus we unmap the
235 * page here and nullify it, so that in future we have
236 * correct page address mapped in hv_cpu_init.
238 memunmap(hv_vp_assist_page[cpu]);
239 hv_vp_assist_page[cpu] = NULL;
240 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
243 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
246 if (hv_reenlightenment_cb == NULL)
249 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
250 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
252 * Reassign reenlightenment notifications to some other online
253 * CPU or just disable the feature if there are no online CPUs
254 * left (happens on hibernation).
256 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
258 if (new_cpu < nr_cpu_ids)
259 re_ctrl.target_vp = hv_vp_index[new_cpu];
263 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
269 static int __init hv_pci_init(void)
271 bool gen2vm = efi_enabled(EFI_BOOT);
274 * A Generation-2 VM doesn't support legacy PCI/PCIe, so both
275 * raw_pci_ops and raw_pci_ext_ops are NULL, and pci_subsys_init() ->
276 * pcibios_init() doesn't call pcibios_resource_survey() ->
277 * e820__reserve_resources_late(); as a result, any emulated persistent
278 * memory of E820_TYPE_PRAM (12) via the kernel parameter
279 * memmap=nn[KMG]!ss is not added into iomem_resource and hence can't be
280 * detected by register_e820_pmem(). Fix this by directly calling
281 * e820__reserve_resources_late() here: e820__reserve_resources_late()
282 * depends on e820__reserve_resources(), which has been called earlier
283 * from setup_arch(). Note: e820__reserve_resources_late() also adds
284 * any memory of E820_TYPE_PMEM (7) into iomem_resource, and
285 * acpi_nfit_register_region() -> acpi_nfit_insert_resource() ->
286 * region_intersects() returns REGION_INTERSECTS, so the memory of
287 * E820_TYPE_PMEM won't get added twice.
289 * We return 0 here so that pci_arch_init() won't print the warning:
290 * "PCI: Fatal: No config space access function found"
293 e820__reserve_resources_late();
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);
398 * This function is to be invoked early in the boot sequence after the
399 * hypervisor has been detected.
401 * 1. Setup the hypercall page.
402 * 2. Register Hyper-V specific clocksource.
403 * 3. Setup Hyper-V specific APIC entry points.
405 void __init hyperv_init(void)
408 union hv_x64_msr_hypercall_contents hypercall_msr;
411 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
414 if (hv_common_init())
417 hv_vp_assist_page = kcalloc(num_possible_cpus(),
418 sizeof(*hv_vp_assist_page), GFP_KERNEL);
419 if (!hv_vp_assist_page) {
420 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
424 if (hv_isolation_type_snp()) {
425 /* Negotiate GHCB Version. */
426 if (!hv_ghcb_negotiate_protocol())
427 hv_ghcb_terminate(SEV_TERM_SET_GEN,
428 GHCB_SEV_ES_PROT_UNSUPPORTED);
430 hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
432 goto free_vp_assist_page;
435 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
436 hv_cpu_init, hv_cpu_die);
441 * Setup the hypercall page and enable hypercalls.
442 * 1. Register the guest ID
443 * 2. Enable the hypercall and register the hypercall page
445 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
446 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
448 /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
449 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
451 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
452 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
453 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
454 __builtin_return_address(0));
455 if (hv_hypercall_pg == NULL)
456 goto clean_guest_os_id;
458 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
459 hypercall_msr.enable = 1;
461 if (hv_root_partition) {
466 * For the root partition, the hypervisor will set up its
467 * hypercall page. The hypervisor guarantees it will not show
468 * up in the root's address space. The root can't change the
469 * location of the hypercall page.
471 * Order is important here. We must enable the hypercall page
472 * so it is populated with code, then copy the code to an
475 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
477 pg = vmalloc_to_page(hv_hypercall_pg);
478 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
481 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
484 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
485 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
489 * Some versions of Hyper-V that provide IBT in guest VMs have a bug
490 * in that there's no ENDBR64 instruction at the entry to the
491 * hypercall page. Because hypercalls are invoked via an indirect call
492 * to the hypercall page, all hypercall attempts fail when IBT is
493 * enabled, and Linux panics. For such buggy versions, disable IBT.
495 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
496 * page, so if future Linux kernel versions enable IBT for 32-bit
497 * builds, additional hypercall page hackery will be required here
498 * to provide an ENDBR32.
500 #ifdef CONFIG_X86_KERNEL_IBT
501 if (cpu_feature_enabled(X86_FEATURE_IBT) &&
502 *(u32 *)hv_hypercall_pg != gen_endbr()) {
503 setup_clear_cpu_cap(X86_FEATURE_IBT);
504 pr_warn("Hyper-V: Disabling IBT because of Hyper-V bug\n");
509 * hyperv_init() is called before LAPIC is initialized: see
510 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
511 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
512 * depends on LAPIC, so hv_stimer_alloc() should be called from
513 * x86_init.timers.setup_percpu_clockev.
515 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
516 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
520 x86_init.pci.arch_init = hv_pci_init;
522 register_syscore_ops(&hv_syscore_ops);
524 hyperv_init_cpuhp = cpuhp;
526 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
527 hv_get_partition_id();
529 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
531 #ifdef CONFIG_PCI_MSI
533 * If we're running as root, we want to create our own PCI MSI domain.
534 * We can't set this in hv_pci_init because that would be too late.
536 if (hv_root_partition)
537 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
540 /* Query the VMs extended capability once, so that it can be cached. */
543 #ifdef CONFIG_SWIOTLB
545 * Swiotlb bounce buffer needs to be mapped in extra address
546 * space. Map function doesn't work in the early place and so
547 * call swiotlb_update_mem_attributes() here.
549 if (hv_is_isolation_supported())
550 swiotlb_update_mem_attributes();
556 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
557 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
558 cpuhp_remove_state(cpuhp);
560 free_percpu(hv_ghcb_pg);
562 kfree(hv_vp_assist_page);
563 hv_vp_assist_page = NULL;
569 * This routine is called before kexec/kdump, it does the required cleanup.
571 void hyperv_cleanup(void)
573 union hv_x64_msr_hypercall_contents hypercall_msr;
574 union hv_reference_tsc_msr tsc_msr;
576 /* Reset our OS id */
577 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
578 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
581 * Reset hypercall page reference before reset the page,
582 * let hypercall operations fail safely rather than
583 * panic the kernel for using invalid hypercall page
585 hv_hypercall_pg = NULL;
587 /* Reset the hypercall page */
588 hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
589 hypercall_msr.enable = 0;
590 hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
592 /* Reset the TSC page */
593 tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
595 hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
598 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
600 static bool panic_reported;
603 if (in_die && !panic_on_oops)
607 * We prefer to report panic on 'die' chain as we have proper
608 * registers to report, but if we miss it (e.g. on BUG()) we need
609 * to report it on 'panic'.
613 panic_reported = true;
615 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
617 wrmsrl(HV_X64_MSR_CRASH_P0, err);
618 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
619 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
620 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
621 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
624 * Let Hyper-V know there is crash data available
626 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
628 EXPORT_SYMBOL_GPL(hyperv_report_panic);
630 bool hv_is_hyperv_initialized(void)
632 union hv_x64_msr_hypercall_contents hypercall_msr;
635 * Ensure that we're really on Hyper-V, and not a KVM or Xen
636 * emulation of Hyper-V
638 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
642 * Verify that earlier initialization succeeded by checking
643 * that the hypercall page is setup
645 hypercall_msr.as_uint64 = 0;
646 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
648 return hypercall_msr.enable;
650 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);