1 // SPDX-License-Identifier: GPL-2.0-only
3 * X86 specific Hyper-V initialization code.
5 * Copyright (C) 2016, Microsoft, Inc.
10 #include <linux/acpi.h>
11 #include <linux/efi.h>
12 #include <linux/types.h>
13 #include <linux/bitfield.h>
16 #include <asm/hypervisor.h>
17 #include <asm/hyperv-tlfs.h>
18 #include <asm/mshyperv.h>
19 #include <asm/idtentry.h>
20 #include <linux/kexec.h>
21 #include <linux/version.h>
22 #include <linux/vmalloc.h>
24 #include <linux/hyperv.h>
25 #include <linux/slab.h>
26 #include <linux/kernel.h>
27 #include <linux/cpuhotplug.h>
28 #include <linux/syscore_ops.h>
29 #include <clocksource/hyperv_timer.h>
30 #include <linux/highmem.h>
32 int hyperv_init_cpuhp;
33 u64 hv_current_partition_id = ~0ull;
34 EXPORT_SYMBOL_GPL(hv_current_partition_id);
36 void *hv_hypercall_pg;
37 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
39 /* Storage to save the hypercall page temporarily for hibernation */
40 static void *hv_hypercall_pg_saved;
43 EXPORT_SYMBOL_GPL(hv_vp_index);
45 struct hv_vp_assist_page **hv_vp_assist_page;
46 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
48 void __percpu **hyperv_pcpu_input_arg;
49 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
51 void __percpu **hyperv_pcpu_output_arg;
52 EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
55 EXPORT_SYMBOL_GPL(hv_max_vp_index);
57 void *hv_alloc_hyperv_page(void)
59 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
61 return (void *)__get_free_page(GFP_KERNEL);
63 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
65 void *hv_alloc_hyperv_zeroed_page(void)
67 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
69 return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
71 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page);
73 void hv_free_hyperv_page(unsigned long addr)
77 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
79 static int hv_cpu_init(unsigned int cpu)
82 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
86 /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
87 pg = alloc_pages(irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL, hv_root_partition ? 1 : 0);
91 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
92 *input_arg = page_address(pg);
93 if (hv_root_partition) {
96 output_arg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
97 *output_arg = page_address(pg + 1);
100 hv_get_vp_index(msr_vp_index);
102 hv_vp_index[smp_processor_id()] = msr_vp_index;
104 if (msr_vp_index > hv_max_vp_index)
105 hv_max_vp_index = msr_vp_index;
107 if (!hv_vp_assist_page)
111 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
112 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
113 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
114 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
115 * not be stopped in the case of CPU offlining and the VM will hang.
118 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
124 val = vmalloc_to_pfn(*hvp);
125 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
126 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
128 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
134 static void (*hv_reenlightenment_cb)(void);
136 static void hv_reenlightenment_notify(struct work_struct *dummy)
138 struct hv_tsc_emulation_status emu_status;
140 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
142 /* Don't issue the callback if TSC accesses are not emulated */
143 if (hv_reenlightenment_cb && emu_status.inprogress)
144 hv_reenlightenment_cb();
146 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
148 void hyperv_stop_tsc_emulation(void)
151 struct hv_tsc_emulation_status emu_status;
153 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
154 emu_status.inprogress = 0;
155 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
157 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
158 tsc_khz = div64_u64(freq, 1000);
160 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
162 static inline bool hv_reenlightenment_available(void)
165 * Check for required features and priviliges to make TSC frequency
166 * change notifications work.
168 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
169 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
170 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
173 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
176 inc_irq_stat(irq_hv_reenlightenment_count);
177 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
180 void set_hv_tscchange_cb(void (*cb)(void))
182 struct hv_reenlightenment_control re_ctrl = {
183 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
185 .target_vp = hv_vp_index[smp_processor_id()]
187 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
189 if (!hv_reenlightenment_available()) {
190 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
194 hv_reenlightenment_cb = cb;
196 /* Make sure callback is registered before we write to MSRs */
199 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
200 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
202 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
204 void clear_hv_tscchange_cb(void)
206 struct hv_reenlightenment_control re_ctrl;
208 if (!hv_reenlightenment_available())
211 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
213 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
215 hv_reenlightenment_cb = NULL;
217 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
219 static int hv_cpu_die(unsigned int cpu)
221 struct hv_reenlightenment_control re_ctrl;
222 unsigned int new_cpu;
227 local_irq_save(flags);
228 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
232 if (hv_root_partition) {
235 output_arg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
239 local_irq_restore(flags);
241 free_pages((unsigned long)pg, hv_root_partition ? 1 : 0);
243 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
244 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
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 int gen2vm = efi_enabled(EFI_BOOT);
274 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
275 * The purpose is to suppress the harmless warning:
276 * "PCI: Fatal: No config space access function found"
281 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
285 static int hv_suspend(void)
287 union hv_x64_msr_hypercall_contents hypercall_msr;
290 if (hv_root_partition)
294 * Reset the hypercall page as it is going to be invalidated
295 * accross hibernation. Setting hv_hypercall_pg to NULL ensures
296 * that any subsequent hypercall operation fails safely instead of
297 * crashing due to an access of an invalid page. The hypercall page
298 * pointer is restored on resume.
300 hv_hypercall_pg_saved = hv_hypercall_pg;
301 hv_hypercall_pg = NULL;
303 /* Disable the hypercall page in the hypervisor */
304 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
305 hypercall_msr.enable = 0;
306 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
312 static void hv_resume(void)
314 union hv_x64_msr_hypercall_contents hypercall_msr;
317 ret = hv_cpu_init(0);
320 /* Re-enable the hypercall page */
321 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
322 hypercall_msr.enable = 1;
323 hypercall_msr.guest_physical_address =
324 vmalloc_to_pfn(hv_hypercall_pg_saved);
325 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
327 hv_hypercall_pg = hv_hypercall_pg_saved;
328 hv_hypercall_pg_saved = NULL;
331 * Reenlightenment notifications are disabled by hv_cpu_die(0),
332 * reenable them here if hv_reenlightenment_cb was previously set.
334 if (hv_reenlightenment_cb)
335 set_hv_tscchange_cb(hv_reenlightenment_cb);
338 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
339 static struct syscore_ops hv_syscore_ops = {
340 .suspend = hv_suspend,
344 static void (* __initdata old_setup_percpu_clockev)(void);
346 static void __init hv_stimer_setup_percpu_clockev(void)
349 * Ignore any errors in setting up stimer clockevents
350 * as we can run with the LAPIC timer as a fallback.
352 (void)hv_stimer_alloc();
355 * Still register the LAPIC timer, because the direct-mode STIMER is
356 * not supported by old versions of Hyper-V. This also allows users
357 * to switch to LAPIC timer via /sys, if they want to.
359 if (old_setup_percpu_clockev)
360 old_setup_percpu_clockev();
363 static void __init hv_get_partition_id(void)
365 struct hv_get_partition_id *output_page;
369 local_irq_save(flags);
370 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
371 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
372 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) {
373 /* No point in proceeding if this failed */
374 pr_err("Failed to get partition ID: %lld\n", status);
377 hv_current_partition_id = output_page->partition_id;
378 local_irq_restore(flags);
382 * This function is to be invoked early in the boot sequence after the
383 * hypervisor has been detected.
385 * 1. Setup the hypercall page.
386 * 2. Register Hyper-V specific clocksource.
387 * 3. Setup Hyper-V specific APIC entry points.
389 void __init hyperv_init(void)
391 u64 guest_id, required_msrs;
392 union hv_x64_msr_hypercall_contents hypercall_msr;
395 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
398 /* Absolutely required MSRs */
399 required_msrs = HV_MSR_HYPERCALL_AVAILABLE |
400 HV_MSR_VP_INDEX_AVAILABLE;
402 if ((ms_hyperv.features & required_msrs) != required_msrs)
406 * Allocate the per-CPU state for the hypercall input arg.
407 * If this allocation fails, we will not be able to setup
408 * (per-CPU) hypercall input page and thus this failure is
411 hyperv_pcpu_input_arg = alloc_percpu(void *);
413 BUG_ON(hyperv_pcpu_input_arg == NULL);
415 /* Allocate the per-CPU state for output arg for root */
416 if (hv_root_partition) {
417 hyperv_pcpu_output_arg = alloc_percpu(void *);
418 BUG_ON(hyperv_pcpu_output_arg == NULL);
421 /* Allocate percpu VP index */
422 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
427 for (i = 0; i < num_possible_cpus(); i++)
428 hv_vp_index[i] = VP_INVAL;
430 hv_vp_assist_page = kcalloc(num_possible_cpus(),
431 sizeof(*hv_vp_assist_page), GFP_KERNEL);
432 if (!hv_vp_assist_page) {
433 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
437 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
438 hv_cpu_init, hv_cpu_die);
440 goto free_vp_assist_page;
443 * Setup the hypercall page and enable hypercalls.
444 * 1. Register the guest ID
445 * 2. Enable the hypercall and register the hypercall page
447 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
448 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
450 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
451 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
452 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
453 __builtin_return_address(0));
454 if (hv_hypercall_pg == NULL) {
455 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
456 goto remove_cpuhp_state;
459 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
460 hypercall_msr.enable = 1;
462 if (hv_root_partition) {
467 * For the root partition, the hypervisor will set up its
468 * hypercall page. The hypervisor guarantees it will not show
469 * up in the root's address space. The root can't change the
470 * location of the hypercall page.
472 * Order is important here. We must enable the hypercall page
473 * so it is populated with code, then copy the code to an
476 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
478 pg = vmalloc_to_page(hv_hypercall_pg);
480 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
482 BUG_ON(!(src && dst));
483 memcpy(dst, src, HV_HYP_PAGE_SIZE);
487 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
488 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
492 * hyperv_init() is called before LAPIC is initialized: see
493 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
494 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
495 * depends on LAPIC, so hv_stimer_alloc() should be called from
496 * x86_init.timers.setup_percpu_clockev.
498 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
499 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
503 x86_init.pci.arch_init = hv_pci_init;
505 register_syscore_ops(&hv_syscore_ops);
507 hyperv_init_cpuhp = cpuhp;
509 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
510 hv_get_partition_id();
512 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
514 #ifdef CONFIG_PCI_MSI
516 * If we're running as root, we want to create our own PCI MSI domain.
517 * We can't set this in hv_pci_init because that would be too late.
519 if (hv_root_partition)
520 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
526 cpuhp_remove_state(cpuhp);
528 kfree(hv_vp_assist_page);
529 hv_vp_assist_page = NULL;
536 * This routine is called before kexec/kdump, it does the required cleanup.
538 void hyperv_cleanup(void)
540 union hv_x64_msr_hypercall_contents hypercall_msr;
542 unregister_syscore_ops(&hv_syscore_ops);
544 /* Reset our OS id */
545 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
548 * Reset hypercall page reference before reset the page,
549 * let hypercall operations fail safely rather than
550 * panic the kernel for using invalid hypercall page
552 hv_hypercall_pg = NULL;
554 /* Reset the hypercall page */
555 hypercall_msr.as_uint64 = 0;
556 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
558 /* Reset the TSC page */
559 hypercall_msr.as_uint64 = 0;
560 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
562 EXPORT_SYMBOL_GPL(hyperv_cleanup);
564 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
566 static bool panic_reported;
569 if (in_die && !panic_on_oops)
573 * We prefer to report panic on 'die' chain as we have proper
574 * registers to report, but if we miss it (e.g. on BUG()) we need
575 * to report it on 'panic'.
579 panic_reported = true;
581 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
583 wrmsrl(HV_X64_MSR_CRASH_P0, err);
584 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
585 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
586 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
587 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
590 * Let Hyper-V know there is crash data available
592 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
594 EXPORT_SYMBOL_GPL(hyperv_report_panic);
597 * hyperv_report_panic_msg - report panic message to Hyper-V
598 * @pa: physical address of the panic page containing the message
599 * @size: size of the message in the page
601 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
604 * P3 to contain the physical address of the panic page & P4 to
605 * contain the size of the panic data in that page. Rest of the
606 * registers are no-op when the NOTIFY_MSG flag is set.
608 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
609 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
610 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
611 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
612 wrmsrl(HV_X64_MSR_CRASH_P4, size);
615 * Let Hyper-V know there is crash data available along with
618 wrmsrl(HV_X64_MSR_CRASH_CTL,
619 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
621 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
623 bool hv_is_hyperv_initialized(void)
625 union hv_x64_msr_hypercall_contents hypercall_msr;
628 * Ensure that we're really on Hyper-V, and not a KVM or Xen
629 * emulation of Hyper-V
631 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
635 * Verify that earlier initialization succeeded by checking
636 * that the hypercall page is setup
638 hypercall_msr.as_uint64 = 0;
639 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
641 return hypercall_msr.enable;
643 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
645 bool hv_is_hibernation_supported(void)
647 return !hv_root_partition && acpi_sleep_state_supported(ACPI_STATE_S4);
649 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
651 enum hv_isolation_type hv_get_isolation_type(void)
653 if (!(ms_hyperv.features_b & HV_ISOLATION))
654 return HV_ISOLATION_TYPE_NONE;
655 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
657 EXPORT_SYMBOL_GPL(hv_get_isolation_type);
659 bool hv_is_isolation_supported(void)
661 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
663 EXPORT_SYMBOL_GPL(hv_is_isolation_supported);