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>
17 #include <asm/hypervisor.h>
18 #include <asm/hyperv-tlfs.h>
19 #include <asm/mshyperv.h>
20 #include <asm/idtentry.h>
21 #include <linux/kexec.h>
22 #include <linux/version.h>
23 #include <linux/vmalloc.h>
25 #include <linux/hyperv.h>
26 #include <linux/slab.h>
27 #include <linux/kernel.h>
28 #include <linux/cpuhotplug.h>
29 #include <linux/syscore_ops.h>
30 #include <clocksource/hyperv_timer.h>
31 #include <linux/highmem.h>
32 #include <linux/swiotlb.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);
67 ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB);
71 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
77 static int hv_cpu_init(unsigned int cpu)
79 union hv_vp_assist_msr_contents msr = { 0 };
80 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
83 ret = hv_common_cpu_init(cpu);
87 if (!hv_vp_assist_page)
90 if (hv_root_partition) {
92 * For root partition we get the hypervisor provided VP assist
93 * page, instead of allocating a new page.
95 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
96 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
97 PAGE_SIZE, MEMREMAP_WB);
100 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
101 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
102 * out to make sure we always write the EOI MSR in
103 * hv_apic_eoi_write() *after* the EOI optimization is disabled
104 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
105 * case of CPU offlining and the VM will hang.
108 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
110 msr.pfn = vmalloc_to_pfn(*hvp);
113 if (!WARN_ON(!(*hvp))) {
115 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
118 return hyperv_init_ghcb();
121 static void (*hv_reenlightenment_cb)(void);
123 static void hv_reenlightenment_notify(struct work_struct *dummy)
125 struct hv_tsc_emulation_status emu_status;
127 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
129 /* Don't issue the callback if TSC accesses are not emulated */
130 if (hv_reenlightenment_cb && emu_status.inprogress)
131 hv_reenlightenment_cb();
133 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
135 void hyperv_stop_tsc_emulation(void)
138 struct hv_tsc_emulation_status emu_status;
140 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
141 emu_status.inprogress = 0;
142 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
144 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
145 tsc_khz = div64_u64(freq, 1000);
147 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
149 static inline bool hv_reenlightenment_available(void)
152 * Check for required features and privileges to make TSC frequency
153 * change notifications work.
155 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
156 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
157 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
160 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
163 inc_irq_stat(irq_hv_reenlightenment_count);
164 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
167 void set_hv_tscchange_cb(void (*cb)(void))
169 struct hv_reenlightenment_control re_ctrl = {
170 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
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");
183 hv_reenlightenment_cb = cb;
185 /* Make sure callback is registered before we write to MSRs */
188 re_ctrl.target_vp = hv_vp_index[get_cpu()];
190 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
191 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
195 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
197 void clear_hv_tscchange_cb(void)
199 struct hv_reenlightenment_control re_ctrl;
201 if (!hv_reenlightenment_available())
204 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
206 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
208 hv_reenlightenment_cb = NULL;
210 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
212 static int hv_cpu_die(unsigned int cpu)
214 struct hv_reenlightenment_control re_ctrl;
215 unsigned int new_cpu;
219 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
225 hv_common_cpu_die(cpu);
227 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
228 union hv_vp_assist_msr_contents msr = { 0 };
229 if (hv_root_partition) {
231 * For root partition the VP assist page is mapped to
232 * hypervisor provided page, and thus we unmap the
233 * page here and nullify it, so that in future we have
234 * correct page address mapped in hv_cpu_init.
236 memunmap(hv_vp_assist_page[cpu]);
237 hv_vp_assist_page[cpu] = NULL;
238 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
241 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
244 if (hv_reenlightenment_cb == NULL)
247 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
248 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
250 * Reassign reenlightenment notifications to some other online
251 * CPU or just disable the feature if there are no online CPUs
252 * left (happens on hibernation).
254 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
256 if (new_cpu < nr_cpu_ids)
257 re_ctrl.target_vp = hv_vp_index[new_cpu];
261 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
267 static int __init hv_pci_init(void)
269 int gen2vm = efi_enabled(EFI_BOOT);
272 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
273 * The purpose is to suppress the harmless warning:
274 * "PCI: Fatal: No config space access function found"
279 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
283 static int hv_suspend(void)
285 union hv_x64_msr_hypercall_contents hypercall_msr;
288 if (hv_root_partition)
292 * Reset the hypercall page as it is going to be invalidated
293 * across hibernation. Setting hv_hypercall_pg to NULL ensures
294 * that any subsequent hypercall operation fails safely instead of
295 * crashing due to an access of an invalid page. The hypercall page
296 * pointer is restored on resume.
298 hv_hypercall_pg_saved = hv_hypercall_pg;
299 hv_hypercall_pg = NULL;
301 /* Disable the hypercall page in the hypervisor */
302 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
303 hypercall_msr.enable = 0;
304 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
310 static void hv_resume(void)
312 union hv_x64_msr_hypercall_contents hypercall_msr;
315 ret = hv_cpu_init(0);
318 /* Re-enable the hypercall page */
319 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
320 hypercall_msr.enable = 1;
321 hypercall_msr.guest_physical_address =
322 vmalloc_to_pfn(hv_hypercall_pg_saved);
323 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
325 hv_hypercall_pg = hv_hypercall_pg_saved;
326 hv_hypercall_pg_saved = NULL;
329 * Reenlightenment notifications are disabled by hv_cpu_die(0),
330 * reenable them here if hv_reenlightenment_cb was previously set.
332 if (hv_reenlightenment_cb)
333 set_hv_tscchange_cb(hv_reenlightenment_cb);
336 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
337 static struct syscore_ops hv_syscore_ops = {
338 .suspend = hv_suspend,
342 static void (* __initdata old_setup_percpu_clockev)(void);
344 static void __init hv_stimer_setup_percpu_clockev(void)
347 * Ignore any errors in setting up stimer clockevents
348 * as we can run with the LAPIC timer as a fallback.
350 (void)hv_stimer_alloc(false);
353 * Still register the LAPIC timer, because the direct-mode STIMER is
354 * not supported by old versions of Hyper-V. This also allows users
355 * to switch to LAPIC timer via /sys, if they want to.
357 if (old_setup_percpu_clockev)
358 old_setup_percpu_clockev();
361 static void __init hv_get_partition_id(void)
363 struct hv_get_partition_id *output_page;
367 local_irq_save(flags);
368 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
369 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
370 if (!hv_result_success(status)) {
371 /* No point in proceeding if this failed */
372 pr_err("Failed to get partition ID: %lld\n", status);
375 hv_current_partition_id = output_page->partition_id;
376 local_irq_restore(flags);
380 * This function is to be invoked early in the boot sequence after the
381 * hypervisor has been detected.
383 * 1. Setup the hypercall page.
384 * 2. Register Hyper-V specific clocksource.
385 * 3. Setup Hyper-V specific APIC entry points.
387 void __init hyperv_init(void)
390 union hv_x64_msr_hypercall_contents hypercall_msr;
393 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
396 if (hv_common_init())
399 hv_vp_assist_page = kcalloc(num_possible_cpus(),
400 sizeof(*hv_vp_assist_page), GFP_KERNEL);
401 if (!hv_vp_assist_page) {
402 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
406 if (hv_isolation_type_snp()) {
407 /* Negotiate GHCB Version. */
408 if (!hv_ghcb_negotiate_protocol())
409 hv_ghcb_terminate(SEV_TERM_SET_GEN,
410 GHCB_SEV_ES_PROT_UNSUPPORTED);
412 hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
414 goto free_vp_assist_page;
417 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
418 hv_cpu_init, hv_cpu_die);
423 * Setup the hypercall page and enable hypercalls.
424 * 1. Register the guest ID
425 * 2. Enable the hypercall and register the hypercall page
427 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
428 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
430 /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
431 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
433 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
434 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
435 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
436 __builtin_return_address(0));
437 if (hv_hypercall_pg == NULL)
438 goto clean_guest_os_id;
440 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
441 hypercall_msr.enable = 1;
443 if (hv_root_partition) {
448 * For the root partition, the hypervisor will set up its
449 * hypercall page. The hypervisor guarantees it will not show
450 * up in the root's address space. The root can't change the
451 * location of the hypercall page.
453 * Order is important here. We must enable the hypercall page
454 * so it is populated with code, then copy the code to an
457 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
459 pg = vmalloc_to_page(hv_hypercall_pg);
460 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
463 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
466 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
467 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
471 * hyperv_init() is called before LAPIC is initialized: see
472 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
473 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
474 * depends on LAPIC, so hv_stimer_alloc() should be called from
475 * x86_init.timers.setup_percpu_clockev.
477 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
478 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
482 x86_init.pci.arch_init = hv_pci_init;
484 register_syscore_ops(&hv_syscore_ops);
486 hyperv_init_cpuhp = cpuhp;
488 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
489 hv_get_partition_id();
491 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
493 #ifdef CONFIG_PCI_MSI
495 * If we're running as root, we want to create our own PCI MSI domain.
496 * We can't set this in hv_pci_init because that would be too late.
498 if (hv_root_partition)
499 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
502 /* Query the VMs extended capability once, so that it can be cached. */
505 #ifdef CONFIG_SWIOTLB
507 * Swiotlb bounce buffer needs to be mapped in extra address
508 * space. Map function doesn't work in the early place and so
509 * call swiotlb_update_mem_attributes() here.
511 if (hv_is_isolation_supported())
512 swiotlb_update_mem_attributes();
518 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
519 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
520 cpuhp_remove_state(cpuhp);
522 free_percpu(hv_ghcb_pg);
524 kfree(hv_vp_assist_page);
525 hv_vp_assist_page = NULL;
531 * This routine is called before kexec/kdump, it does the required cleanup.
533 void hyperv_cleanup(void)
535 union hv_x64_msr_hypercall_contents hypercall_msr;
536 union hv_reference_tsc_msr tsc_msr;
538 unregister_syscore_ops(&hv_syscore_ops);
540 /* Reset our OS id */
541 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
542 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
545 * Reset hypercall page reference before reset the page,
546 * let hypercall operations fail safely rather than
547 * panic the kernel for using invalid hypercall page
549 hv_hypercall_pg = NULL;
551 /* Reset the hypercall page */
552 hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
553 hypercall_msr.enable = 0;
554 hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
556 /* Reset the TSC page */
557 tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
559 hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
562 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
564 static bool panic_reported;
567 if (in_die && !panic_on_oops)
571 * We prefer to report panic on 'die' chain as we have proper
572 * registers to report, but if we miss it (e.g. on BUG()) we need
573 * to report it on 'panic'.
577 panic_reported = true;
579 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
581 wrmsrl(HV_X64_MSR_CRASH_P0, err);
582 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
583 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
584 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
585 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
588 * Let Hyper-V know there is crash data available
590 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
592 EXPORT_SYMBOL_GPL(hyperv_report_panic);
594 bool hv_is_hyperv_initialized(void)
596 union hv_x64_msr_hypercall_contents hypercall_msr;
599 * Ensure that we're really on Hyper-V, and not a KVM or Xen
600 * emulation of Hyper-V
602 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
606 * Verify that earlier initialization succeeded by checking
607 * that the hypercall page is setup
609 hypercall_msr.as_uint64 = 0;
610 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
612 return hypercall_msr.enable;
614 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);