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/acpi.h>
11 #include <linux/efi.h>
12 #include <linux/types.h>
15 #include <asm/hypervisor.h>
16 #include <asm/hyperv-tlfs.h>
17 #include <asm/mshyperv.h>
18 #include <asm/idtentry.h>
19 #include <linux/kexec.h>
20 #include <linux/version.h>
21 #include <linux/vmalloc.h>
23 #include <linux/hyperv.h>
24 #include <linux/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/cpuhotplug.h>
27 #include <linux/syscore_ops.h>
28 #include <clocksource/hyperv_timer.h>
30 int hyperv_init_cpuhp;
32 void *hv_hypercall_pg;
33 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
35 /* Storage to save the hypercall page temporarily for hibernation */
36 static void *hv_hypercall_pg_saved;
39 EXPORT_SYMBOL_GPL(hv_vp_index);
41 struct hv_vp_assist_page **hv_vp_assist_page;
42 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
44 void __percpu **hyperv_pcpu_input_arg;
45 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
48 EXPORT_SYMBOL_GPL(hv_max_vp_index);
50 void *hv_alloc_hyperv_page(void)
52 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
54 return (void *)__get_free_page(GFP_KERNEL);
56 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
58 void *hv_alloc_hyperv_zeroed_page(void)
60 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
62 return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
64 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page);
66 void hv_free_hyperv_page(unsigned long addr)
70 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
72 static int hv_cpu_init(unsigned int cpu)
75 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
79 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
80 /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
81 pg = alloc_page(irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
84 *input_arg = page_address(pg);
86 hv_get_vp_index(msr_vp_index);
88 hv_vp_index[smp_processor_id()] = msr_vp_index;
90 if (msr_vp_index > hv_max_vp_index)
91 hv_max_vp_index = msr_vp_index;
93 if (!hv_vp_assist_page)
97 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
98 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
99 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
100 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
101 * not be stopped in the case of CPU offlining and the VM will hang.
104 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
110 val = vmalloc_to_pfn(*hvp);
111 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
112 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
114 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
120 static void (*hv_reenlightenment_cb)(void);
122 static void hv_reenlightenment_notify(struct work_struct *dummy)
124 struct hv_tsc_emulation_status emu_status;
126 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
128 /* Don't issue the callback if TSC accesses are not emulated */
129 if (hv_reenlightenment_cb && emu_status.inprogress)
130 hv_reenlightenment_cb();
132 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
134 void hyperv_stop_tsc_emulation(void)
137 struct hv_tsc_emulation_status emu_status;
139 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
140 emu_status.inprogress = 0;
141 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
143 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
144 tsc_khz = div64_u64(freq, 1000);
146 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
148 static inline bool hv_reenlightenment_available(void)
151 * Check for required features and priviliges to make TSC frequency
152 * change notifications work.
154 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
155 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
156 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
159 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
162 inc_irq_stat(irq_hv_reenlightenment_count);
163 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
166 void set_hv_tscchange_cb(void (*cb)(void))
168 struct hv_reenlightenment_control re_ctrl = {
169 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
171 .target_vp = hv_vp_index[smp_processor_id()]
173 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
175 if (!hv_reenlightenment_available()) {
176 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
180 hv_reenlightenment_cb = cb;
182 /* Make sure callback is registered before we write to MSRs */
185 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
186 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
188 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
190 void clear_hv_tscchange_cb(void)
192 struct hv_reenlightenment_control re_ctrl;
194 if (!hv_reenlightenment_available())
197 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
199 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
201 hv_reenlightenment_cb = NULL;
203 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
205 static int hv_cpu_die(unsigned int cpu)
207 struct hv_reenlightenment_control re_ctrl;
208 unsigned int new_cpu;
211 void *input_pg = NULL;
213 local_irq_save(flags);
214 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
215 input_pg = *input_arg;
217 local_irq_restore(flags);
218 free_page((unsigned long)input_pg);
220 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
221 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
223 if (hv_reenlightenment_cb == NULL)
226 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
227 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
229 * Reassign reenlightenment notifications to some other online
230 * CPU or just disable the feature if there are no online CPUs
231 * left (happens on hibernation).
233 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
235 if (new_cpu < nr_cpu_ids)
236 re_ctrl.target_vp = hv_vp_index[new_cpu];
240 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
246 static int __init hv_pci_init(void)
248 int gen2vm = efi_enabled(EFI_BOOT);
251 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
252 * The purpose is to suppress the harmless warning:
253 * "PCI: Fatal: No config space access function found"
258 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
262 static int hv_suspend(void)
264 union hv_x64_msr_hypercall_contents hypercall_msr;
268 * Reset the hypercall page as it is going to be invalidated
269 * accross hibernation. Setting hv_hypercall_pg to NULL ensures
270 * that any subsequent hypercall operation fails safely instead of
271 * crashing due to an access of an invalid page. The hypercall page
272 * pointer is restored on resume.
274 hv_hypercall_pg_saved = hv_hypercall_pg;
275 hv_hypercall_pg = NULL;
277 /* Disable the hypercall page in the hypervisor */
278 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
279 hypercall_msr.enable = 0;
280 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
286 static void hv_resume(void)
288 union hv_x64_msr_hypercall_contents hypercall_msr;
291 ret = hv_cpu_init(0);
294 /* Re-enable the hypercall page */
295 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
296 hypercall_msr.enable = 1;
297 hypercall_msr.guest_physical_address =
298 vmalloc_to_pfn(hv_hypercall_pg_saved);
299 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
301 hv_hypercall_pg = hv_hypercall_pg_saved;
302 hv_hypercall_pg_saved = NULL;
305 * Reenlightenment notifications are disabled by hv_cpu_die(0),
306 * reenable them here if hv_reenlightenment_cb was previously set.
308 if (hv_reenlightenment_cb)
309 set_hv_tscchange_cb(hv_reenlightenment_cb);
312 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
313 static struct syscore_ops hv_syscore_ops = {
314 .suspend = hv_suspend,
318 static void (* __initdata old_setup_percpu_clockev)(void);
320 static void __init hv_stimer_setup_percpu_clockev(void)
323 * Ignore any errors in setting up stimer clockevents
324 * as we can run with the LAPIC timer as a fallback.
326 (void)hv_stimer_alloc();
329 * Still register the LAPIC timer, because the direct-mode STIMER is
330 * not supported by old versions of Hyper-V. This also allows users
331 * to switch to LAPIC timer via /sys, if they want to.
333 if (old_setup_percpu_clockev)
334 old_setup_percpu_clockev();
338 * This function is to be invoked early in the boot sequence after the
339 * hypervisor has been detected.
341 * 1. Setup the hypercall page.
342 * 2. Register Hyper-V specific clocksource.
343 * 3. Setup Hyper-V specific APIC entry points.
345 void __init hyperv_init(void)
347 u64 guest_id, required_msrs;
348 union hv_x64_msr_hypercall_contents hypercall_msr;
351 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
354 /* Absolutely required MSRs */
355 required_msrs = HV_MSR_HYPERCALL_AVAILABLE |
356 HV_MSR_VP_INDEX_AVAILABLE;
358 if ((ms_hyperv.features & required_msrs) != required_msrs)
362 * Allocate the per-CPU state for the hypercall input arg.
363 * If this allocation fails, we will not be able to setup
364 * (per-CPU) hypercall input page and thus this failure is
367 hyperv_pcpu_input_arg = alloc_percpu(void *);
369 BUG_ON(hyperv_pcpu_input_arg == NULL);
371 /* Allocate percpu VP index */
372 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
377 for (i = 0; i < num_possible_cpus(); i++)
378 hv_vp_index[i] = VP_INVAL;
380 hv_vp_assist_page = kcalloc(num_possible_cpus(),
381 sizeof(*hv_vp_assist_page), GFP_KERNEL);
382 if (!hv_vp_assist_page) {
383 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
387 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
388 hv_cpu_init, hv_cpu_die);
390 goto free_vp_assist_page;
393 * Setup the hypercall page and enable hypercalls.
394 * 1. Register the guest ID
395 * 2. Enable the hypercall and register the hypercall page
397 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
398 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
400 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
401 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
402 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
403 __builtin_return_address(0));
404 if (hv_hypercall_pg == NULL) {
405 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
406 goto remove_cpuhp_state;
409 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
410 hypercall_msr.enable = 1;
411 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
412 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
415 * hyperv_init() is called before LAPIC is initialized: see
416 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
417 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
418 * depends on LAPIC, so hv_stimer_alloc() should be called from
419 * x86_init.timers.setup_percpu_clockev.
421 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
422 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
426 x86_init.pci.arch_init = hv_pci_init;
428 register_syscore_ops(&hv_syscore_ops);
430 hyperv_init_cpuhp = cpuhp;
434 cpuhp_remove_state(cpuhp);
436 kfree(hv_vp_assist_page);
437 hv_vp_assist_page = NULL;
444 * This routine is called before kexec/kdump, it does the required cleanup.
446 void hyperv_cleanup(void)
448 union hv_x64_msr_hypercall_contents hypercall_msr;
450 unregister_syscore_ops(&hv_syscore_ops);
452 /* Reset our OS id */
453 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
456 * Reset hypercall page reference before reset the page,
457 * let hypercall operations fail safely rather than
458 * panic the kernel for using invalid hypercall page
460 hv_hypercall_pg = NULL;
462 /* Reset the hypercall page */
463 hypercall_msr.as_uint64 = 0;
464 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
466 /* Reset the TSC page */
467 hypercall_msr.as_uint64 = 0;
468 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
470 EXPORT_SYMBOL_GPL(hyperv_cleanup);
472 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
474 static bool panic_reported;
477 if (in_die && !panic_on_oops)
481 * We prefer to report panic on 'die' chain as we have proper
482 * registers to report, but if we miss it (e.g. on BUG()) we need
483 * to report it on 'panic'.
487 panic_reported = true;
489 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
491 wrmsrl(HV_X64_MSR_CRASH_P0, err);
492 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
493 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
494 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
495 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
498 * Let Hyper-V know there is crash data available
500 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
502 EXPORT_SYMBOL_GPL(hyperv_report_panic);
505 * hyperv_report_panic_msg - report panic message to Hyper-V
506 * @pa: physical address of the panic page containing the message
507 * @size: size of the message in the page
509 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
512 * P3 to contain the physical address of the panic page & P4 to
513 * contain the size of the panic data in that page. Rest of the
514 * registers are no-op when the NOTIFY_MSG flag is set.
516 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
517 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
518 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
519 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
520 wrmsrl(HV_X64_MSR_CRASH_P4, size);
523 * Let Hyper-V know there is crash data available along with
526 wrmsrl(HV_X64_MSR_CRASH_CTL,
527 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
529 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
531 bool hv_is_hyperv_initialized(void)
533 union hv_x64_msr_hypercall_contents hypercall_msr;
536 * Ensure that we're really on Hyper-V, and not a KVM or Xen
537 * emulation of Hyper-V
539 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
543 * Verify that earlier initialization succeeded by checking
544 * that the hypercall page is setup
546 hypercall_msr.as_uint64 = 0;
547 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
549 return hypercall_msr.enable;
551 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
553 bool hv_is_hibernation_supported(void)
555 return acpi_sleep_state_supported(ACPI_STATE_S4);
557 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);