1 // SPDX-License-Identifier: GPL-2.0-only
3 * X86 specific Hyper-V initialization code.
5 * Copyright (C) 2016, Microsoft, Inc.
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>
31 #include <linux/swiotlb.h>
33 int hyperv_init_cpuhp;
34 u64 hv_current_partition_id = ~0ull;
35 EXPORT_SYMBOL_GPL(hv_current_partition_id);
37 void *hv_hypercall_pg;
38 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
40 union hv_ghcb * __percpu *hv_ghcb_pg;
42 /* Storage to save the hypercall page temporarily for hibernation */
43 static void *hv_hypercall_pg_saved;
45 struct hv_vp_assist_page **hv_vp_assist_page;
46 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
48 static int hyperv_init_ghcb(void)
54 if (!hv_isolation_type_snp())
61 * GHCB page is allocated by paravisor. The address
62 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
63 * memory boundary and map it here.
65 rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
66 ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB);
70 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
76 static int hv_cpu_init(unsigned int cpu)
78 union hv_vp_assist_msr_contents msr = { 0 };
79 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
82 ret = hv_common_cpu_init(cpu);
86 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 <<
97 HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
98 PAGE_SIZE, MEMREMAP_WB);
101 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
102 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
103 * out to make sure we always write the EOI MSR in
104 * hv_apic_eoi_write() *after* the EOI optimization is disabled
105 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
106 * 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);
115 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
119 return hyperv_init_ghcb();
122 static void (*hv_reenlightenment_cb)(void);
124 static void hv_reenlightenment_notify(struct work_struct *dummy)
126 struct hv_tsc_emulation_status emu_status;
128 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
130 /* Don't issue the callback if TSC accesses are not emulated */
131 if (hv_reenlightenment_cb && emu_status.inprogress)
132 hv_reenlightenment_cb();
134 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
136 void hyperv_stop_tsc_emulation(void)
139 struct hv_tsc_emulation_status emu_status;
141 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
142 emu_status.inprogress = 0;
143 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
145 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
146 tsc_khz = div64_u64(freq, 1000);
148 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
150 static inline bool hv_reenlightenment_available(void)
153 * Check for required features and privileges to make TSC frequency
154 * change notifications work.
156 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
157 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
158 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
161 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
164 inc_irq_stat(irq_hv_reenlightenment_count);
165 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
168 void set_hv_tscchange_cb(void (*cb)(void))
170 struct hv_reenlightenment_control re_ctrl = {
171 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
174 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
176 if (!hv_reenlightenment_available()) {
177 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
184 hv_reenlightenment_cb = cb;
186 /* Make sure callback is registered before we write to MSRs */
189 re_ctrl.target_vp = hv_vp_index[get_cpu()];
191 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
192 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
196 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
198 void clear_hv_tscchange_cb(void)
200 struct hv_reenlightenment_control re_ctrl;
202 if (!hv_reenlightenment_available())
205 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
207 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
209 hv_reenlightenment_cb = NULL;
211 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
213 static int hv_cpu_die(unsigned int cpu)
215 struct hv_reenlightenment_control re_ctrl;
216 unsigned int new_cpu;
220 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
226 hv_common_cpu_die(cpu);
228 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
229 union hv_vp_assist_msr_contents msr = { 0 };
230 if (hv_root_partition) {
232 * For root partition the VP assist page is mapped to
233 * hypervisor provided page, and thus we unmap the
234 * page here and nullify it, so that in future we have
235 * correct page address mapped in hv_cpu_init.
237 memunmap(hv_vp_assist_page[cpu]);
238 hv_vp_assist_page[cpu] = NULL;
239 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
242 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
245 if (hv_reenlightenment_cb == NULL)
248 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
249 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
251 * Reassign reenlightenment notifications to some other online
252 * CPU or just disable the feature if there are no online CPUs
253 * left (happens on hibernation).
255 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
257 if (new_cpu < nr_cpu_ids)
258 re_ctrl.target_vp = hv_vp_index[new_cpu];
262 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
268 static int __init hv_pci_init(void)
270 int gen2vm = efi_enabled(EFI_BOOT);
273 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
274 * The purpose is to suppress the harmless warning:
275 * "PCI: Fatal: No config space access function found"
280 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
284 static int hv_suspend(void)
286 union hv_x64_msr_hypercall_contents hypercall_msr;
289 if (hv_root_partition)
293 * Reset the hypercall page as it is going to be invalidated
294 * across hibernation. Setting hv_hypercall_pg to NULL ensures
295 * that any subsequent hypercall operation fails safely instead of
296 * crashing due to an access of an invalid page. The hypercall page
297 * pointer is restored on resume.
299 hv_hypercall_pg_saved = hv_hypercall_pg;
300 hv_hypercall_pg = NULL;
302 /* Disable the hypercall page in the hypervisor */
303 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
304 hypercall_msr.enable = 0;
305 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
311 static void hv_resume(void)
313 union hv_x64_msr_hypercall_contents hypercall_msr;
316 ret = hv_cpu_init(0);
319 /* Re-enable the hypercall page */
320 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
321 hypercall_msr.enable = 1;
322 hypercall_msr.guest_physical_address =
323 vmalloc_to_pfn(hv_hypercall_pg_saved);
324 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
326 hv_hypercall_pg = hv_hypercall_pg_saved;
327 hv_hypercall_pg_saved = NULL;
330 * Reenlightenment notifications are disabled by hv_cpu_die(0),
331 * reenable them here if hv_reenlightenment_cb was previously set.
333 if (hv_reenlightenment_cb)
334 set_hv_tscchange_cb(hv_reenlightenment_cb);
337 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
338 static struct syscore_ops hv_syscore_ops = {
339 .suspend = hv_suspend,
343 static void (* __initdata old_setup_percpu_clockev)(void);
345 static void __init hv_stimer_setup_percpu_clockev(void)
348 * Ignore any errors in setting up stimer clockevents
349 * as we can run with the LAPIC timer as a fallback.
351 (void)hv_stimer_alloc(false);
354 * Still register the LAPIC timer, because the direct-mode STIMER is
355 * not supported by old versions of Hyper-V. This also allows users
356 * to switch to LAPIC timer via /sys, if they want to.
358 if (old_setup_percpu_clockev)
359 old_setup_percpu_clockev();
362 static void __init hv_get_partition_id(void)
364 struct hv_get_partition_id *output_page;
368 local_irq_save(flags);
369 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
370 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
371 if (!hv_result_success(status)) {
372 /* No point in proceeding if this failed */
373 pr_err("Failed to get partition ID: %lld\n", status);
376 hv_current_partition_id = output_page->partition_id;
377 local_irq_restore(flags);
381 * This function is to be invoked early in the boot sequence after the
382 * hypervisor has been detected.
384 * 1. Setup the hypercall page.
385 * 2. Register Hyper-V specific clocksource.
386 * 3. Setup Hyper-V specific APIC entry points.
388 void __init hyperv_init(void)
391 union hv_x64_msr_hypercall_contents hypercall_msr;
394 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
397 if (hv_common_init())
400 hv_vp_assist_page = kcalloc(num_possible_cpus(),
401 sizeof(*hv_vp_assist_page), GFP_KERNEL);
402 if (!hv_vp_assist_page) {
403 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
407 if (hv_isolation_type_snp()) {
408 hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
410 goto free_vp_assist_page;
413 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
414 hv_cpu_init, hv_cpu_die);
419 * Setup the hypercall page and enable hypercalls.
420 * 1. Register the guest ID
421 * 2. Enable the hypercall and register the hypercall page
423 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
424 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
426 /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
427 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
429 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
430 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
431 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
432 __builtin_return_address(0));
433 if (hv_hypercall_pg == NULL)
434 goto clean_guest_os_id;
436 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
437 hypercall_msr.enable = 1;
439 if (hv_root_partition) {
444 * For the root partition, the hypervisor will set up its
445 * hypercall page. The hypervisor guarantees it will not show
446 * up in the root's address space. The root can't change the
447 * location of the hypercall page.
449 * Order is important here. We must enable the hypercall page
450 * so it is populated with code, then copy the code to an
453 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
455 pg = vmalloc_to_page(hv_hypercall_pg);
457 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
459 BUG_ON(!(src && dst));
460 memcpy(dst, src, HV_HYP_PAGE_SIZE);
464 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
465 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
469 * hyperv_init() is called before LAPIC is initialized: see
470 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
471 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
472 * depends on LAPIC, so hv_stimer_alloc() should be called from
473 * x86_init.timers.setup_percpu_clockev.
475 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
476 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
480 x86_init.pci.arch_init = hv_pci_init;
482 register_syscore_ops(&hv_syscore_ops);
484 hyperv_init_cpuhp = cpuhp;
486 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
487 hv_get_partition_id();
489 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
491 #ifdef CONFIG_PCI_MSI
493 * If we're running as root, we want to create our own PCI MSI domain.
494 * We can't set this in hv_pci_init because that would be too late.
496 if (hv_root_partition)
497 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
500 /* Query the VMs extended capability once, so that it can be cached. */
503 #ifdef CONFIG_SWIOTLB
505 * Swiotlb bounce buffer needs to be mapped in extra address
506 * space. Map function doesn't work in the early place and so
507 * call swiotlb_update_mem_attributes() here.
509 if (hv_is_isolation_supported())
510 swiotlb_update_mem_attributes();
516 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
517 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
518 cpuhp_remove_state(cpuhp);
520 free_percpu(hv_ghcb_pg);
522 kfree(hv_vp_assist_page);
523 hv_vp_assist_page = NULL;
529 * This routine is called before kexec/kdump, it does the required cleanup.
531 void hyperv_cleanup(void)
533 union hv_x64_msr_hypercall_contents hypercall_msr;
535 unregister_syscore_ops(&hv_syscore_ops);
537 /* Reset our OS id */
538 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
539 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
542 * Reset hypercall page reference before reset the page,
543 * let hypercall operations fail safely rather than
544 * panic the kernel for using invalid hypercall page
546 hv_hypercall_pg = NULL;
548 /* Reset the hypercall page */
549 hypercall_msr.as_uint64 = 0;
550 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
552 /* Reset the TSC page */
553 hypercall_msr.as_uint64 = 0;
554 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
557 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
559 static bool panic_reported;
562 if (in_die && !panic_on_oops)
566 * We prefer to report panic on 'die' chain as we have proper
567 * registers to report, but if we miss it (e.g. on BUG()) we need
568 * to report it on 'panic'.
572 panic_reported = true;
574 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
576 wrmsrl(HV_X64_MSR_CRASH_P0, err);
577 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
578 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
579 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
580 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
583 * Let Hyper-V know there is crash data available
585 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
587 EXPORT_SYMBOL_GPL(hyperv_report_panic);
589 bool hv_is_hyperv_initialized(void)
591 union hv_x64_msr_hypercall_contents hypercall_msr;
594 * Ensure that we're really on Hyper-V, and not a KVM or Xen
595 * emulation of Hyper-V
597 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
601 * Verify that earlier initialization succeeded by checking
602 * that the hypercall page is setup
604 hypercall_msr.as_uint64 = 0;
605 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
607 return hypercall_msr.enable;
609 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);