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>
18 #include <asm/hypervisor.h>
19 #include <asm/hyperv-tlfs.h>
20 #include <asm/mshyperv.h>
21 #include <asm/idtentry.h>
22 #include <linux/kexec.h>
23 #include <linux/version.h>
24 #include <linux/vmalloc.h>
26 #include <linux/hyperv.h>
27 #include <linux/slab.h>
28 #include <linux/kernel.h>
29 #include <linux/cpuhotplug.h>
30 #include <linux/syscore_ops.h>
31 #include <clocksource/hyperv_timer.h>
32 #include <linux/highmem.h>
34 int hyperv_init_cpuhp;
35 u64 hv_current_partition_id = ~0ull;
36 EXPORT_SYMBOL_GPL(hv_current_partition_id);
38 void *hv_hypercall_pg;
39 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
41 union hv_ghcb * __percpu *hv_ghcb_pg;
43 /* Storage to save the hypercall page temporarily for hibernation */
44 static void *hv_hypercall_pg_saved;
46 struct hv_vp_assist_page **hv_vp_assist_page;
47 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
49 static int hyperv_init_ghcb(void)
55 if (!hv_isolation_type_snp())
62 * GHCB page is allocated by paravisor. The address
63 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
64 * memory boundary and map it here.
66 rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
68 /* Mask out vTOM bit. ioremap_cache() maps decrypted */
69 ghcb_gpa &= ~ms_hyperv.shared_gpa_boundary;
70 ghcb_va = (void *)ioremap_cache(ghcb_gpa, HV_HYP_PAGE_SIZE);
74 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
80 static int hv_cpu_init(unsigned int cpu)
82 union hv_vp_assist_msr_contents msr = { 0 };
83 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
86 ret = hv_common_cpu_init(cpu);
90 if (!hv_vp_assist_page)
93 if (hv_root_partition) {
95 * For root partition we get the hypervisor provided VP assist
96 * page, instead of allocating a new page.
98 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
99 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
100 PAGE_SIZE, MEMREMAP_WB);
103 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
104 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
105 * out to make sure we always write the EOI MSR in
106 * hv_apic_eoi_write() *after* the EOI optimization is disabled
107 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
108 * case of CPU offlining and the VM will hang.
111 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
113 msr.pfn = vmalloc_to_pfn(*hvp);
116 if (!WARN_ON(!(*hvp))) {
118 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
121 return hyperv_init_ghcb();
124 static void (*hv_reenlightenment_cb)(void);
126 static void hv_reenlightenment_notify(struct work_struct *dummy)
128 struct hv_tsc_emulation_status emu_status;
130 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
132 /* Don't issue the callback if TSC accesses are not emulated */
133 if (hv_reenlightenment_cb && emu_status.inprogress)
134 hv_reenlightenment_cb();
136 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
138 void hyperv_stop_tsc_emulation(void)
141 struct hv_tsc_emulation_status emu_status;
143 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
144 emu_status.inprogress = 0;
145 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
147 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
148 tsc_khz = div64_u64(freq, 1000);
150 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
152 static inline bool hv_reenlightenment_available(void)
155 * Check for required features and privileges to make TSC frequency
156 * change notifications work.
158 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
159 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
160 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
163 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
166 inc_irq_stat(irq_hv_reenlightenment_count);
167 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
170 void set_hv_tscchange_cb(void (*cb)(void))
172 struct hv_reenlightenment_control re_ctrl = {
173 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
176 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
178 if (!hv_reenlightenment_available()) {
179 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
186 hv_reenlightenment_cb = cb;
188 /* Make sure callback is registered before we write to MSRs */
191 re_ctrl.target_vp = hv_vp_index[get_cpu()];
193 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
194 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
198 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
200 void clear_hv_tscchange_cb(void)
202 struct hv_reenlightenment_control re_ctrl;
204 if (!hv_reenlightenment_available())
207 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
209 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
211 hv_reenlightenment_cb = NULL;
213 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
215 static int hv_cpu_die(unsigned int cpu)
217 struct hv_reenlightenment_control re_ctrl;
218 unsigned int new_cpu;
222 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
228 hv_common_cpu_die(cpu);
230 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
231 union hv_vp_assist_msr_contents msr = { 0 };
232 if (hv_root_partition) {
234 * For root partition the VP assist page is mapped to
235 * hypervisor provided page, and thus we unmap the
236 * page here and nullify it, so that in future we have
237 * correct page address mapped in hv_cpu_init.
239 memunmap(hv_vp_assist_page[cpu]);
240 hv_vp_assist_page[cpu] = NULL;
241 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
244 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
247 if (hv_reenlightenment_cb == NULL)
250 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
251 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
253 * Reassign reenlightenment notifications to some other online
254 * CPU or just disable the feature if there are no online CPUs
255 * left (happens on hibernation).
257 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
259 if (new_cpu < nr_cpu_ids)
260 re_ctrl.target_vp = hv_vp_index[new_cpu];
264 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
270 static int __init hv_pci_init(void)
272 int gen2vm = efi_enabled(EFI_BOOT);
275 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
276 * The purpose is to suppress the harmless warning:
277 * "PCI: Fatal: No config space access function found"
282 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
286 static int hv_suspend(void)
288 union hv_x64_msr_hypercall_contents hypercall_msr;
291 if (hv_root_partition)
295 * Reset the hypercall page as it is going to be invalidated
296 * across hibernation. Setting hv_hypercall_pg to NULL ensures
297 * that any subsequent hypercall operation fails safely instead of
298 * crashing due to an access of an invalid page. The hypercall page
299 * pointer is restored on resume.
301 hv_hypercall_pg_saved = hv_hypercall_pg;
302 hv_hypercall_pg = NULL;
304 /* Disable the hypercall page in the hypervisor */
305 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
306 hypercall_msr.enable = 0;
307 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
313 static void hv_resume(void)
315 union hv_x64_msr_hypercall_contents hypercall_msr;
318 ret = hv_cpu_init(0);
321 /* Re-enable the hypercall page */
322 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
323 hypercall_msr.enable = 1;
324 hypercall_msr.guest_physical_address =
325 vmalloc_to_pfn(hv_hypercall_pg_saved);
326 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
328 hv_hypercall_pg = hv_hypercall_pg_saved;
329 hv_hypercall_pg_saved = NULL;
332 * Reenlightenment notifications are disabled by hv_cpu_die(0),
333 * reenable them here if hv_reenlightenment_cb was previously set.
335 if (hv_reenlightenment_cb)
336 set_hv_tscchange_cb(hv_reenlightenment_cb);
339 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
340 static struct syscore_ops hv_syscore_ops = {
341 .suspend = hv_suspend,
345 static void (* __initdata old_setup_percpu_clockev)(void);
347 static void __init hv_stimer_setup_percpu_clockev(void)
350 * Ignore any errors in setting up stimer clockevents
351 * as we can run with the LAPIC timer as a fallback.
353 (void)hv_stimer_alloc(false);
356 * Still register the LAPIC timer, because the direct-mode STIMER is
357 * not supported by old versions of Hyper-V. This also allows users
358 * to switch to LAPIC timer via /sys, if they want to.
360 if (old_setup_percpu_clockev)
361 old_setup_percpu_clockev();
364 static void __init hv_get_partition_id(void)
366 struct hv_get_partition_id *output_page;
370 local_irq_save(flags);
371 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
372 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
373 if (!hv_result_success(status)) {
374 /* No point in proceeding if this failed */
375 pr_err("Failed to get partition ID: %lld\n", status);
378 hv_current_partition_id = output_page->partition_id;
379 local_irq_restore(flags);
383 * This function is to be invoked early in the boot sequence after the
384 * hypervisor has been detected.
386 * 1. Setup the hypercall page.
387 * 2. Register Hyper-V specific clocksource.
388 * 3. Setup Hyper-V specific APIC entry points.
390 void __init hyperv_init(void)
393 union hv_x64_msr_hypercall_contents hypercall_msr;
396 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
399 if (hv_common_init())
402 hv_vp_assist_page = kcalloc(num_possible_cpus(),
403 sizeof(*hv_vp_assist_page), GFP_KERNEL);
404 if (!hv_vp_assist_page) {
405 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
409 if (hv_isolation_type_snp()) {
410 /* Negotiate GHCB Version. */
411 if (!hv_ghcb_negotiate_protocol())
412 hv_ghcb_terminate(SEV_TERM_SET_GEN,
413 GHCB_SEV_ES_PROT_UNSUPPORTED);
415 hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
417 goto free_vp_assist_page;
420 cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online",
421 hv_cpu_init, hv_cpu_die);
426 * Setup the hypercall page and enable hypercalls.
427 * 1. Register the guest ID
428 * 2. Enable the hypercall and register the hypercall page
430 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
431 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
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);
436 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
437 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
438 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
439 __builtin_return_address(0));
440 if (hv_hypercall_pg == NULL)
441 goto clean_guest_os_id;
443 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
444 hypercall_msr.enable = 1;
446 if (hv_root_partition) {
451 * For the root partition, the hypervisor will set up its
452 * hypercall page. The hypervisor guarantees it will not show
453 * up in the root's address space. The root can't change the
454 * location of the hypercall page.
456 * Order is important here. We must enable the hypercall page
457 * so it is populated with code, then copy the code to an
460 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
462 pg = vmalloc_to_page(hv_hypercall_pg);
463 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
466 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
469 hv_remap_tsc_clocksource();
471 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
472 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
476 * Some versions of Hyper-V that provide IBT in guest VMs have a bug
477 * in that there's no ENDBR64 instruction at the entry to the
478 * hypercall page. Because hypercalls are invoked via an indirect call
479 * to the hypercall page, all hypercall attempts fail when IBT is
480 * enabled, and Linux panics. For such buggy versions, disable IBT.
482 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
483 * page, so if future Linux kernel versions enable IBT for 32-bit
484 * builds, additional hypercall page hackery will be required here
485 * to provide an ENDBR32.
487 #ifdef CONFIG_X86_KERNEL_IBT
488 if (cpu_feature_enabled(X86_FEATURE_IBT) &&
489 *(u32 *)hv_hypercall_pg != gen_endbr()) {
490 setup_clear_cpu_cap(X86_FEATURE_IBT);
491 pr_warn("Hyper-V: Disabling IBT because of Hyper-V bug\n");
496 * hyperv_init() is called before LAPIC is initialized: see
497 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
498 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
499 * depends on LAPIC, so hv_stimer_alloc() should be called from
500 * x86_init.timers.setup_percpu_clockev.
502 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
503 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
507 x86_init.pci.arch_init = hv_pci_init;
509 register_syscore_ops(&hv_syscore_ops);
511 hyperv_init_cpuhp = cpuhp;
513 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
514 hv_get_partition_id();
516 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
518 #ifdef CONFIG_PCI_MSI
520 * If we're running as root, we want to create our own PCI MSI domain.
521 * We can't set this in hv_pci_init because that would be too late.
523 if (hv_root_partition)
524 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
527 /* Query the VMs extended capability once, so that it can be cached. */
533 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
534 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
535 cpuhp_remove_state(cpuhp);
537 free_percpu(hv_ghcb_pg);
539 kfree(hv_vp_assist_page);
540 hv_vp_assist_page = NULL;
546 * This routine is called before kexec/kdump, it does the required cleanup.
548 void hyperv_cleanup(void)
550 union hv_x64_msr_hypercall_contents hypercall_msr;
551 union hv_reference_tsc_msr tsc_msr;
553 /* Reset our OS id */
554 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
555 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
558 * Reset hypercall page reference before reset the page,
559 * let hypercall operations fail safely rather than
560 * panic the kernel for using invalid hypercall page
562 hv_hypercall_pg = NULL;
564 /* Reset the hypercall page */
565 hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
566 hypercall_msr.enable = 0;
567 hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
569 /* Reset the TSC page */
570 tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
572 hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
575 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
577 static bool panic_reported;
580 if (in_die && !panic_on_oops)
584 * We prefer to report panic on 'die' chain as we have proper
585 * registers to report, but if we miss it (e.g. on BUG()) we need
586 * to report it on 'panic'.
590 panic_reported = true;
592 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
594 wrmsrl(HV_X64_MSR_CRASH_P0, err);
595 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
596 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
597 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
598 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
601 * Let Hyper-V know there is crash data available
603 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
605 EXPORT_SYMBOL_GPL(hyperv_report_panic);
607 bool hv_is_hyperv_initialized(void)
609 union hv_x64_msr_hypercall_contents hypercall_msr;
612 * Ensure that we're really on Hyper-V, and not a KVM or Xen
613 * emulation of Hyper-V
615 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
619 * Verify that earlier initialization succeeded by checking
620 * that the hypercall page is setup
622 hypercall_msr.as_uint64 = 0;
623 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
625 return hypercall_msr.enable;
627 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);