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 static int hv_cpu_init(unsigned int cpu)
60 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
64 /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
65 pg = alloc_pages(irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL, hv_root_partition ? 1 : 0);
69 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
70 *input_arg = page_address(pg);
71 if (hv_root_partition) {
74 output_arg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
75 *output_arg = page_address(pg + 1);
78 msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);
80 hv_vp_index[smp_processor_id()] = msr_vp_index;
82 if (msr_vp_index > hv_max_vp_index)
83 hv_max_vp_index = msr_vp_index;
85 if (!hv_vp_assist_page)
89 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
90 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
91 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
92 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
93 * not be stopped in the case of CPU offlining and the VM will hang.
96 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
102 val = vmalloc_to_pfn(*hvp);
103 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
104 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
106 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
112 static void (*hv_reenlightenment_cb)(void);
114 static void hv_reenlightenment_notify(struct work_struct *dummy)
116 struct hv_tsc_emulation_status emu_status;
118 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
120 /* Don't issue the callback if TSC accesses are not emulated */
121 if (hv_reenlightenment_cb && emu_status.inprogress)
122 hv_reenlightenment_cb();
124 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
126 void hyperv_stop_tsc_emulation(void)
129 struct hv_tsc_emulation_status emu_status;
131 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
132 emu_status.inprogress = 0;
133 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
135 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
136 tsc_khz = div64_u64(freq, 1000);
138 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
140 static inline bool hv_reenlightenment_available(void)
143 * Check for required features and privileges to make TSC frequency
144 * change notifications work.
146 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
147 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
148 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
151 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
154 inc_irq_stat(irq_hv_reenlightenment_count);
155 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
158 void set_hv_tscchange_cb(void (*cb)(void))
160 struct hv_reenlightenment_control re_ctrl = {
161 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
163 .target_vp = hv_vp_index[smp_processor_id()]
165 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
167 if (!hv_reenlightenment_available()) {
168 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
172 hv_reenlightenment_cb = cb;
174 /* Make sure callback is registered before we write to MSRs */
177 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
178 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
180 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
182 void clear_hv_tscchange_cb(void)
184 struct hv_reenlightenment_control re_ctrl;
186 if (!hv_reenlightenment_available())
189 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
191 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
193 hv_reenlightenment_cb = NULL;
195 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
197 static int hv_cpu_die(unsigned int cpu)
199 struct hv_reenlightenment_control re_ctrl;
200 unsigned int new_cpu;
205 local_irq_save(flags);
206 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
210 if (hv_root_partition) {
213 output_arg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
217 local_irq_restore(flags);
219 free_pages((unsigned long)pg, hv_root_partition ? 1 : 0);
221 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
222 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
224 if (hv_reenlightenment_cb == NULL)
227 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
228 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
230 * Reassign reenlightenment notifications to some other online
231 * CPU or just disable the feature if there are no online CPUs
232 * left (happens on hibernation).
234 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
236 if (new_cpu < nr_cpu_ids)
237 re_ctrl.target_vp = hv_vp_index[new_cpu];
241 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
247 static int __init hv_pci_init(void)
249 int gen2vm = efi_enabled(EFI_BOOT);
252 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
253 * The purpose is to suppress the harmless warning:
254 * "PCI: Fatal: No config space access function found"
259 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
263 static int hv_suspend(void)
265 union hv_x64_msr_hypercall_contents hypercall_msr;
268 if (hv_root_partition)
272 * Reset the hypercall page as it is going to be invalidated
273 * across hibernation. Setting hv_hypercall_pg to NULL ensures
274 * that any subsequent hypercall operation fails safely instead of
275 * crashing due to an access of an invalid page. The hypercall page
276 * pointer is restored on resume.
278 hv_hypercall_pg_saved = hv_hypercall_pg;
279 hv_hypercall_pg = NULL;
281 /* Disable the hypercall page in the hypervisor */
282 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
283 hypercall_msr.enable = 0;
284 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
290 static void hv_resume(void)
292 union hv_x64_msr_hypercall_contents hypercall_msr;
295 ret = hv_cpu_init(0);
298 /* Re-enable the hypercall page */
299 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
300 hypercall_msr.enable = 1;
301 hypercall_msr.guest_physical_address =
302 vmalloc_to_pfn(hv_hypercall_pg_saved);
303 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
305 hv_hypercall_pg = hv_hypercall_pg_saved;
306 hv_hypercall_pg_saved = NULL;
309 * Reenlightenment notifications are disabled by hv_cpu_die(0),
310 * reenable them here if hv_reenlightenment_cb was previously set.
312 if (hv_reenlightenment_cb)
313 set_hv_tscchange_cb(hv_reenlightenment_cb);
316 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
317 static struct syscore_ops hv_syscore_ops = {
318 .suspend = hv_suspend,
322 static void (* __initdata old_setup_percpu_clockev)(void);
324 static void __init hv_stimer_setup_percpu_clockev(void)
327 * Ignore any errors in setting up stimer clockevents
328 * as we can run with the LAPIC timer as a fallback.
330 (void)hv_stimer_alloc(false);
333 * Still register the LAPIC timer, because the direct-mode STIMER is
334 * not supported by old versions of Hyper-V. This also allows users
335 * to switch to LAPIC timer via /sys, if they want to.
337 if (old_setup_percpu_clockev)
338 old_setup_percpu_clockev();
341 static void __init hv_get_partition_id(void)
343 struct hv_get_partition_id *output_page;
347 local_irq_save(flags);
348 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
349 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
350 if (!hv_result_success(status)) {
351 /* No point in proceeding if this failed */
352 pr_err("Failed to get partition ID: %lld\n", status);
355 hv_current_partition_id = output_page->partition_id;
356 local_irq_restore(flags);
360 * This function is to be invoked early in the boot sequence after the
361 * hypervisor has been detected.
363 * 1. Setup the hypercall page.
364 * 2. Register Hyper-V specific clocksource.
365 * 3. Setup Hyper-V specific APIC entry points.
367 void __init hyperv_init(void)
369 u64 guest_id, required_msrs;
370 union hv_x64_msr_hypercall_contents hypercall_msr;
373 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
376 /* Absolutely required MSRs */
377 required_msrs = HV_MSR_HYPERCALL_AVAILABLE |
378 HV_MSR_VP_INDEX_AVAILABLE;
380 if ((ms_hyperv.features & required_msrs) != required_msrs)
384 * Allocate the per-CPU state for the hypercall input arg.
385 * If this allocation fails, we will not be able to setup
386 * (per-CPU) hypercall input page and thus this failure is
389 hyperv_pcpu_input_arg = alloc_percpu(void *);
391 BUG_ON(hyperv_pcpu_input_arg == NULL);
393 /* Allocate the per-CPU state for output arg for root */
394 if (hv_root_partition) {
395 hyperv_pcpu_output_arg = alloc_percpu(void *);
396 BUG_ON(hyperv_pcpu_output_arg == NULL);
399 /* Allocate percpu VP index */
400 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
405 for (i = 0; i < num_possible_cpus(); i++)
406 hv_vp_index[i] = VP_INVAL;
408 hv_vp_assist_page = kcalloc(num_possible_cpus(),
409 sizeof(*hv_vp_assist_page), GFP_KERNEL);
410 if (!hv_vp_assist_page) {
411 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
415 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
416 hv_cpu_init, hv_cpu_die);
418 goto free_vp_assist_page;
421 * Setup the hypercall page and enable hypercalls.
422 * 1. Register the guest ID
423 * 2. Enable the hypercall and register the hypercall page
425 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
426 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
428 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
429 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
430 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
431 __builtin_return_address(0));
432 if (hv_hypercall_pg == NULL) {
433 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
434 goto remove_cpuhp_state;
437 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
438 hypercall_msr.enable = 1;
440 if (hv_root_partition) {
445 * For the root partition, the hypervisor will set up its
446 * hypercall page. The hypervisor guarantees it will not show
447 * up in the root's address space. The root can't change the
448 * location of the hypercall page.
450 * Order is important here. We must enable the hypercall page
451 * so it is populated with code, then copy the code to an
454 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
456 pg = vmalloc_to_page(hv_hypercall_pg);
458 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
460 BUG_ON(!(src && dst));
461 memcpy(dst, src, HV_HYP_PAGE_SIZE);
465 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
466 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
470 * hyperv_init() is called before LAPIC is initialized: see
471 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
472 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
473 * depends on LAPIC, so hv_stimer_alloc() should be called from
474 * x86_init.timers.setup_percpu_clockev.
476 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
477 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
481 x86_init.pci.arch_init = hv_pci_init;
483 register_syscore_ops(&hv_syscore_ops);
485 hyperv_init_cpuhp = cpuhp;
487 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
488 hv_get_partition_id();
490 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
492 #ifdef CONFIG_PCI_MSI
494 * If we're running as root, we want to create our own PCI MSI domain.
495 * We can't set this in hv_pci_init because that would be too late.
497 if (hv_root_partition)
498 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
501 /* Query the VMs extended capability once, so that it can be cached. */
506 cpuhp_remove_state(cpuhp);
508 kfree(hv_vp_assist_page);
509 hv_vp_assist_page = NULL;
516 * This routine is called before kexec/kdump, it does the required cleanup.
518 void hyperv_cleanup(void)
520 union hv_x64_msr_hypercall_contents hypercall_msr;
522 unregister_syscore_ops(&hv_syscore_ops);
524 /* Reset our OS id */
525 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
528 * Reset hypercall page reference before reset the page,
529 * let hypercall operations fail safely rather than
530 * panic the kernel for using invalid hypercall page
532 hv_hypercall_pg = NULL;
534 /* Reset the hypercall page */
535 hypercall_msr.as_uint64 = 0;
536 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
538 /* Reset the TSC page */
539 hypercall_msr.as_uint64 = 0;
540 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
542 EXPORT_SYMBOL_GPL(hyperv_cleanup);
544 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
546 static bool panic_reported;
549 if (in_die && !panic_on_oops)
553 * We prefer to report panic on 'die' chain as we have proper
554 * registers to report, but if we miss it (e.g. on BUG()) we need
555 * to report it on 'panic'.
559 panic_reported = true;
561 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
563 wrmsrl(HV_X64_MSR_CRASH_P0, err);
564 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
565 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
566 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
567 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
570 * Let Hyper-V know there is crash data available
572 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
574 EXPORT_SYMBOL_GPL(hyperv_report_panic);
576 bool hv_is_hyperv_initialized(void)
578 union hv_x64_msr_hypercall_contents hypercall_msr;
581 * Ensure that we're really on Hyper-V, and not a KVM or Xen
582 * emulation of Hyper-V
584 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
588 * Verify that earlier initialization succeeded by checking
589 * that the hypercall page is setup
591 hypercall_msr.as_uint64 = 0;
592 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
594 return hypercall_msr.enable;
596 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
598 bool hv_is_hibernation_supported(void)
600 return !hv_root_partition && acpi_sleep_state_supported(ACPI_STATE_S4);
602 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
604 enum hv_isolation_type hv_get_isolation_type(void)
606 if (!(ms_hyperv.priv_high & HV_ISOLATION))
607 return HV_ISOLATION_TYPE_NONE;
608 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
610 EXPORT_SYMBOL_GPL(hv_get_isolation_type);
612 bool hv_is_isolation_supported(void)
614 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
616 EXPORT_SYMBOL_GPL(hv_is_isolation_supported);
618 /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */
619 bool hv_query_ext_cap(u64 cap_query)
622 * The address of the 'hv_extended_cap' variable will be used as an
623 * output parameter to the hypercall below and so it should be
624 * compatible with 'virt_to_phys'. Which means, it's address should be
625 * directly mapped. Use 'static' to keep it compatible; stack variables
626 * can be virtually mapped, making them imcompatible with
628 * Hypercall input/output addresses should also be 8-byte aligned.
630 static u64 hv_extended_cap __aligned(8);
631 static bool hv_extended_cap_queried;
635 * Querying extended capabilities is an extended hypercall. Check if the
636 * partition supports extended hypercall, first.
638 if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS))
641 /* Extended capabilities do not change at runtime. */
642 if (hv_extended_cap_queried)
643 return hv_extended_cap & cap_query;
645 status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL,
649 * The query extended capabilities hypercall should not fail under
650 * any normal circumstances. Avoid repeatedly making the hypercall, on
653 hv_extended_cap_queried = true;
654 status &= HV_HYPERCALL_RESULT_MASK;
655 if (status != HV_STATUS_SUCCESS) {
656 pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n",
661 return hv_extended_cap & cap_query;
663 EXPORT_SYMBOL_GPL(hv_query_ext_cap);