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/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;
42 struct hv_vp_assist_page **hv_vp_assist_page;
43 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
45 static int hv_cpu_init(unsigned int cpu)
47 union hv_vp_assist_msr_contents msr = { 0 };
48 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
51 ret = hv_common_cpu_init(cpu);
55 if (!hv_vp_assist_page)
59 if (hv_root_partition) {
61 * For root partition we get the hypervisor provided VP assist
62 * page, instead of allocating a new page.
64 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
65 *hvp = memremap(msr.pfn <<
66 HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
67 PAGE_SIZE, MEMREMAP_WB);
70 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
71 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
72 * out to make sure we always write the EOI MSR in
73 * hv_apic_eoi_write() *after* the EOI optimization is disabled
74 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
75 * case of CPU offlining and the VM will hang.
77 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
79 msr.pfn = vmalloc_to_pfn(*hvp);
84 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
91 static void (*hv_reenlightenment_cb)(void);
93 static void hv_reenlightenment_notify(struct work_struct *dummy)
95 struct hv_tsc_emulation_status emu_status;
97 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
99 /* Don't issue the callback if TSC accesses are not emulated */
100 if (hv_reenlightenment_cb && emu_status.inprogress)
101 hv_reenlightenment_cb();
103 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
105 void hyperv_stop_tsc_emulation(void)
108 struct hv_tsc_emulation_status emu_status;
110 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
111 emu_status.inprogress = 0;
112 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
114 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
115 tsc_khz = div64_u64(freq, 1000);
117 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
119 static inline bool hv_reenlightenment_available(void)
122 * Check for required features and privileges to make TSC frequency
123 * change notifications work.
125 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
126 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
127 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
130 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
133 inc_irq_stat(irq_hv_reenlightenment_count);
134 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
137 void set_hv_tscchange_cb(void (*cb)(void))
139 struct hv_reenlightenment_control re_ctrl = {
140 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
143 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
145 if (!hv_reenlightenment_available()) {
146 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
153 hv_reenlightenment_cb = cb;
155 /* Make sure callback is registered before we write to MSRs */
158 re_ctrl.target_vp = hv_vp_index[get_cpu()];
160 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
161 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
165 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
167 void clear_hv_tscchange_cb(void)
169 struct hv_reenlightenment_control re_ctrl;
171 if (!hv_reenlightenment_available())
174 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
176 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
178 hv_reenlightenment_cb = NULL;
180 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
182 static int hv_cpu_die(unsigned int cpu)
184 struct hv_reenlightenment_control re_ctrl;
185 unsigned int new_cpu;
187 hv_common_cpu_die(cpu);
189 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
190 union hv_vp_assist_msr_contents msr = { 0 };
191 if (hv_root_partition) {
193 * For root partition the VP assist page is mapped to
194 * hypervisor provided page, and thus we unmap the
195 * page here and nullify it, so that in future we have
196 * correct page address mapped in hv_cpu_init.
198 memunmap(hv_vp_assist_page[cpu]);
199 hv_vp_assist_page[cpu] = NULL;
200 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
203 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
206 if (hv_reenlightenment_cb == NULL)
209 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
210 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
212 * Reassign reenlightenment notifications to some other online
213 * CPU or just disable the feature if there are no online CPUs
214 * left (happens on hibernation).
216 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
218 if (new_cpu < nr_cpu_ids)
219 re_ctrl.target_vp = hv_vp_index[new_cpu];
223 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
229 static int __init hv_pci_init(void)
231 int gen2vm = efi_enabled(EFI_BOOT);
234 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
235 * The purpose is to suppress the harmless warning:
236 * "PCI: Fatal: No config space access function found"
241 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
245 static int hv_suspend(void)
247 union hv_x64_msr_hypercall_contents hypercall_msr;
250 if (hv_root_partition)
254 * Reset the hypercall page as it is going to be invalidated
255 * across hibernation. Setting hv_hypercall_pg to NULL ensures
256 * that any subsequent hypercall operation fails safely instead of
257 * crashing due to an access of an invalid page. The hypercall page
258 * pointer is restored on resume.
260 hv_hypercall_pg_saved = hv_hypercall_pg;
261 hv_hypercall_pg = NULL;
263 /* Disable the hypercall page in the hypervisor */
264 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
265 hypercall_msr.enable = 0;
266 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
272 static void hv_resume(void)
274 union hv_x64_msr_hypercall_contents hypercall_msr;
277 ret = hv_cpu_init(0);
280 /* Re-enable the hypercall page */
281 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
282 hypercall_msr.enable = 1;
283 hypercall_msr.guest_physical_address =
284 vmalloc_to_pfn(hv_hypercall_pg_saved);
285 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
287 hv_hypercall_pg = hv_hypercall_pg_saved;
288 hv_hypercall_pg_saved = NULL;
291 * Reenlightenment notifications are disabled by hv_cpu_die(0),
292 * reenable them here if hv_reenlightenment_cb was previously set.
294 if (hv_reenlightenment_cb)
295 set_hv_tscchange_cb(hv_reenlightenment_cb);
298 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
299 static struct syscore_ops hv_syscore_ops = {
300 .suspend = hv_suspend,
304 static void (* __initdata old_setup_percpu_clockev)(void);
306 static void __init hv_stimer_setup_percpu_clockev(void)
309 * Ignore any errors in setting up stimer clockevents
310 * as we can run with the LAPIC timer as a fallback.
312 (void)hv_stimer_alloc(false);
315 * Still register the LAPIC timer, because the direct-mode STIMER is
316 * not supported by old versions of Hyper-V. This also allows users
317 * to switch to LAPIC timer via /sys, if they want to.
319 if (old_setup_percpu_clockev)
320 old_setup_percpu_clockev();
323 static void __init hv_get_partition_id(void)
325 struct hv_get_partition_id *output_page;
329 local_irq_save(flags);
330 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
331 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
332 if (!hv_result_success(status)) {
333 /* No point in proceeding if this failed */
334 pr_err("Failed to get partition ID: %lld\n", status);
337 hv_current_partition_id = output_page->partition_id;
338 local_irq_restore(flags);
342 * This function is to be invoked early in the boot sequence after the
343 * hypervisor has been detected.
345 * 1. Setup the hypercall page.
346 * 2. Register Hyper-V specific clocksource.
347 * 3. Setup Hyper-V specific APIC entry points.
349 void __init hyperv_init(void)
352 union hv_x64_msr_hypercall_contents hypercall_msr;
355 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
358 if (hv_common_init())
361 hv_vp_assist_page = kcalloc(num_possible_cpus(),
362 sizeof(*hv_vp_assist_page), GFP_KERNEL);
363 if (!hv_vp_assist_page) {
364 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
368 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
369 hv_cpu_init, hv_cpu_die);
371 goto free_vp_assist_page;
374 * Setup the hypercall page and enable hypercalls.
375 * 1. Register the guest ID
376 * 2. Enable the hypercall and register the hypercall page
378 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
379 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
381 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
382 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
383 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
384 __builtin_return_address(0));
385 if (hv_hypercall_pg == NULL) {
386 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
387 goto remove_cpuhp_state;
390 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
391 hypercall_msr.enable = 1;
393 if (hv_root_partition) {
398 * For the root partition, the hypervisor will set up its
399 * hypercall page. The hypervisor guarantees it will not show
400 * up in the root's address space. The root can't change the
401 * location of the hypercall page.
403 * Order is important here. We must enable the hypercall page
404 * so it is populated with code, then copy the code to an
407 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
409 pg = vmalloc_to_page(hv_hypercall_pg);
411 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
413 BUG_ON(!(src && dst));
414 memcpy(dst, src, HV_HYP_PAGE_SIZE);
418 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
419 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
423 * hyperv_init() is called before LAPIC is initialized: see
424 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
425 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
426 * depends on LAPIC, so hv_stimer_alloc() should be called from
427 * x86_init.timers.setup_percpu_clockev.
429 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
430 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
434 x86_init.pci.arch_init = hv_pci_init;
436 register_syscore_ops(&hv_syscore_ops);
438 hyperv_init_cpuhp = cpuhp;
440 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
441 hv_get_partition_id();
443 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
445 #ifdef CONFIG_PCI_MSI
447 * If we're running as root, we want to create our own PCI MSI domain.
448 * We can't set this in hv_pci_init because that would be too late.
450 if (hv_root_partition)
451 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
454 /* Query the VMs extended capability once, so that it can be cached. */
459 cpuhp_remove_state(cpuhp);
461 kfree(hv_vp_assist_page);
462 hv_vp_assist_page = NULL;
468 * This routine is called before kexec/kdump, it does the required cleanup.
470 void hyperv_cleanup(void)
472 union hv_x64_msr_hypercall_contents hypercall_msr;
474 unregister_syscore_ops(&hv_syscore_ops);
476 /* Reset our OS id */
477 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
480 * Reset hypercall page reference before reset the page,
481 * let hypercall operations fail safely rather than
482 * panic the kernel for using invalid hypercall page
484 hv_hypercall_pg = NULL;
486 /* Reset the hypercall page */
487 hypercall_msr.as_uint64 = 0;
488 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
490 /* Reset the TSC page */
491 hypercall_msr.as_uint64 = 0;
492 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
495 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
497 static bool panic_reported;
500 if (in_die && !panic_on_oops)
504 * We prefer to report panic on 'die' chain as we have proper
505 * registers to report, but if we miss it (e.g. on BUG()) we need
506 * to report it on 'panic'.
510 panic_reported = true;
512 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
514 wrmsrl(HV_X64_MSR_CRASH_P0, err);
515 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
516 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
517 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
518 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
521 * Let Hyper-V know there is crash data available
523 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
525 EXPORT_SYMBOL_GPL(hyperv_report_panic);
527 bool hv_is_hyperv_initialized(void)
529 union hv_x64_msr_hypercall_contents hypercall_msr;
532 * Ensure that we're really on Hyper-V, and not a KVM or Xen
533 * emulation of Hyper-V
535 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
539 * Verify that earlier initialization succeeded by checking
540 * that the hypercall page is setup
542 hypercall_msr.as_uint64 = 0;
543 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
545 return hypercall_msr.enable;
547 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
549 enum hv_isolation_type hv_get_isolation_type(void)
551 if (!(ms_hyperv.priv_high & HV_ISOLATION))
552 return HV_ISOLATION_TYPE_NONE;
553 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
555 EXPORT_SYMBOL_GPL(hv_get_isolation_type);
557 bool hv_is_isolation_supported(void)
559 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;