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/version.h>
20 #include <linux/vmalloc.h>
22 #include <linux/hyperv.h>
23 #include <linux/slab.h>
24 #include <linux/kernel.h>
25 #include <linux/cpuhotplug.h>
26 #include <linux/syscore_ops.h>
27 #include <clocksource/hyperv_timer.h>
29 void *hv_hypercall_pg
;
30 EXPORT_SYMBOL_GPL(hv_hypercall_pg
);
32 /* Storage to save the hypercall page temporarily for hibernation */
33 static void *hv_hypercall_pg_saved
;
36 EXPORT_SYMBOL_GPL(hv_vp_index
);
38 struct hv_vp_assist_page
**hv_vp_assist_page
;
39 EXPORT_SYMBOL_GPL(hv_vp_assist_page
);
41 void __percpu
**hyperv_pcpu_input_arg
;
42 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg
);
45 EXPORT_SYMBOL_GPL(hv_max_vp_index
);
47 void *hv_alloc_hyperv_page(void)
49 BUILD_BUG_ON(PAGE_SIZE
!= HV_HYP_PAGE_SIZE
);
51 return (void *)__get_free_page(GFP_KERNEL
);
53 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page
);
55 void *hv_alloc_hyperv_zeroed_page(void)
57 BUILD_BUG_ON(PAGE_SIZE
!= HV_HYP_PAGE_SIZE
);
59 return (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
61 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page
);
63 void hv_free_hyperv_page(unsigned long addr
)
67 EXPORT_SYMBOL_GPL(hv_free_hyperv_page
);
69 static int hv_cpu_init(unsigned int cpu
)
72 struct hv_vp_assist_page
**hvp
= &hv_vp_assist_page
[smp_processor_id()];
76 input_arg
= (void **)this_cpu_ptr(hyperv_pcpu_input_arg
);
77 /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
78 pg
= alloc_page(irqs_disabled() ? GFP_ATOMIC
: GFP_KERNEL
);
81 *input_arg
= page_address(pg
);
83 hv_get_vp_index(msr_vp_index
);
85 hv_vp_index
[smp_processor_id()] = msr_vp_index
;
87 if (msr_vp_index
> hv_max_vp_index
)
88 hv_max_vp_index
= msr_vp_index
;
90 if (!hv_vp_assist_page
)
94 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
95 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
96 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
97 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
98 * not be stopped in the case of CPU offlining and the VM will hang.
101 *hvp
= __vmalloc(PAGE_SIZE
, GFP_KERNEL
| __GFP_ZERO
);
107 val
= vmalloc_to_pfn(*hvp
);
108 val
= (val
<< HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT
) |
109 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE
;
111 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE
, val
);
117 static void (*hv_reenlightenment_cb
)(void);
119 static void hv_reenlightenment_notify(struct work_struct
*dummy
)
121 struct hv_tsc_emulation_status emu_status
;
123 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS
, *(u64
*)&emu_status
);
125 /* Don't issue the callback if TSC accesses are not emulated */
126 if (hv_reenlightenment_cb
&& emu_status
.inprogress
)
127 hv_reenlightenment_cb();
129 static DECLARE_DELAYED_WORK(hv_reenlightenment_work
, hv_reenlightenment_notify
);
131 void hyperv_stop_tsc_emulation(void)
134 struct hv_tsc_emulation_status emu_status
;
136 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS
, *(u64
*)&emu_status
);
137 emu_status
.inprogress
= 0;
138 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS
, *(u64
*)&emu_status
);
140 rdmsrl(HV_X64_MSR_TSC_FREQUENCY
, freq
);
141 tsc_khz
= div64_u64(freq
, 1000);
143 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation
);
145 static inline bool hv_reenlightenment_available(void)
148 * Check for required features and priviliges to make TSC frequency
149 * change notifications work.
151 return ms_hyperv
.features
& HV_ACCESS_FREQUENCY_MSRS
&&
152 ms_hyperv
.misc_features
& HV_FEATURE_FREQUENCY_MSRS_AVAILABLE
&&
153 ms_hyperv
.features
& HV_ACCESS_REENLIGHTENMENT
;
156 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment
)
159 inc_irq_stat(irq_hv_reenlightenment_count
);
160 schedule_delayed_work(&hv_reenlightenment_work
, HZ
/10);
163 void set_hv_tscchange_cb(void (*cb
)(void))
165 struct hv_reenlightenment_control re_ctrl
= {
166 .vector
= HYPERV_REENLIGHTENMENT_VECTOR
,
168 .target_vp
= hv_vp_index
[smp_processor_id()]
170 struct hv_tsc_emulation_control emu_ctrl
= {.enabled
= 1};
172 if (!hv_reenlightenment_available()) {
173 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
177 hv_reenlightenment_cb
= cb
;
179 /* Make sure callback is registered before we write to MSRs */
182 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *((u64
*)&re_ctrl
));
183 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL
, *((u64
*)&emu_ctrl
));
185 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb
);
187 void clear_hv_tscchange_cb(void)
189 struct hv_reenlightenment_control re_ctrl
;
191 if (!hv_reenlightenment_available())
194 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *(u64
*)&re_ctrl
);
196 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *(u64
*)&re_ctrl
);
198 hv_reenlightenment_cb
= NULL
;
200 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb
);
202 static int hv_cpu_die(unsigned int cpu
)
204 struct hv_reenlightenment_control re_ctrl
;
205 unsigned int new_cpu
;
208 void *input_pg
= NULL
;
210 local_irq_save(flags
);
211 input_arg
= (void **)this_cpu_ptr(hyperv_pcpu_input_arg
);
212 input_pg
= *input_arg
;
214 local_irq_restore(flags
);
215 free_page((unsigned long)input_pg
);
217 if (hv_vp_assist_page
&& hv_vp_assist_page
[cpu
])
218 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE
, 0);
220 if (hv_reenlightenment_cb
== NULL
)
223 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *((u64
*)&re_ctrl
));
224 if (re_ctrl
.target_vp
== hv_vp_index
[cpu
]) {
226 * Reassign reenlightenment notifications to some other online
227 * CPU or just disable the feature if there are no online CPUs
228 * left (happens on hibernation).
230 new_cpu
= cpumask_any_but(cpu_online_mask
, cpu
);
232 if (new_cpu
< nr_cpu_ids
)
233 re_ctrl
.target_vp
= hv_vp_index
[new_cpu
];
237 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *((u64
*)&re_ctrl
));
243 static int __init
hv_pci_init(void)
245 int gen2vm
= efi_enabled(EFI_BOOT
);
248 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
249 * The purpose is to suppress the harmless warning:
250 * "PCI: Fatal: No config space access function found"
255 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
259 static int hv_suspend(void)
261 union hv_x64_msr_hypercall_contents hypercall_msr
;
265 * Reset the hypercall page as it is going to be invalidated
266 * accross hibernation. Setting hv_hypercall_pg to NULL ensures
267 * that any subsequent hypercall operation fails safely instead of
268 * crashing due to an access of an invalid page. The hypercall page
269 * pointer is restored on resume.
271 hv_hypercall_pg_saved
= hv_hypercall_pg
;
272 hv_hypercall_pg
= NULL
;
274 /* Disable the hypercall page in the hypervisor */
275 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
276 hypercall_msr
.enable
= 0;
277 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
283 static void hv_resume(void)
285 union hv_x64_msr_hypercall_contents hypercall_msr
;
288 ret
= hv_cpu_init(0);
291 /* Re-enable the hypercall page */
292 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
293 hypercall_msr
.enable
= 1;
294 hypercall_msr
.guest_physical_address
=
295 vmalloc_to_pfn(hv_hypercall_pg_saved
);
296 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
298 hv_hypercall_pg
= hv_hypercall_pg_saved
;
299 hv_hypercall_pg_saved
= NULL
;
302 * Reenlightenment notifications are disabled by hv_cpu_die(0),
303 * reenable them here if hv_reenlightenment_cb was previously set.
305 if (hv_reenlightenment_cb
)
306 set_hv_tscchange_cb(hv_reenlightenment_cb
);
309 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
310 static struct syscore_ops hv_syscore_ops
= {
311 .suspend
= hv_suspend
,
316 * This function is to be invoked early in the boot sequence after the
317 * hypervisor has been detected.
319 * 1. Setup the hypercall page.
320 * 2. Register Hyper-V specific clocksource.
321 * 3. Setup Hyper-V specific APIC entry points.
323 void __init
hyperv_init(void)
325 u64 guest_id
, required_msrs
;
326 union hv_x64_msr_hypercall_contents hypercall_msr
;
329 if (x86_hyper_type
!= X86_HYPER_MS_HYPERV
)
332 /* Absolutely required MSRs */
333 required_msrs
= HV_MSR_HYPERCALL_AVAILABLE
|
334 HV_MSR_VP_INDEX_AVAILABLE
;
336 if ((ms_hyperv
.features
& required_msrs
) != required_msrs
)
340 * Allocate the per-CPU state for the hypercall input arg.
341 * If this allocation fails, we will not be able to setup
342 * (per-CPU) hypercall input page and thus this failure is
345 hyperv_pcpu_input_arg
= alloc_percpu(void *);
347 BUG_ON(hyperv_pcpu_input_arg
== NULL
);
349 /* Allocate percpu VP index */
350 hv_vp_index
= kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index
),
355 for (i
= 0; i
< num_possible_cpus(); i
++)
356 hv_vp_index
[i
] = VP_INVAL
;
358 hv_vp_assist_page
= kcalloc(num_possible_cpus(),
359 sizeof(*hv_vp_assist_page
), GFP_KERNEL
);
360 if (!hv_vp_assist_page
) {
361 ms_hyperv
.hints
&= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED
;
365 cpuhp
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "x86/hyperv_init:online",
366 hv_cpu_init
, hv_cpu_die
);
368 goto free_vp_assist_page
;
371 * Setup the hypercall page and enable hypercalls.
372 * 1. Register the guest ID
373 * 2. Enable the hypercall and register the hypercall page
375 guest_id
= generate_guest_id(0, LINUX_VERSION_CODE
, 0);
376 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, guest_id
);
378 hv_hypercall_pg
= __vmalloc_node_range(PAGE_SIZE
, 1, VMALLOC_START
,
379 VMALLOC_END
, GFP_KERNEL
, PAGE_KERNEL_ROX
,
380 VM_FLUSH_RESET_PERMS
, NUMA_NO_NODE
,
381 __builtin_return_address(0));
382 if (hv_hypercall_pg
== NULL
) {
383 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, 0);
384 goto remove_cpuhp_state
;
387 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
388 hypercall_msr
.enable
= 1;
389 hypercall_msr
.guest_physical_address
= vmalloc_to_pfn(hv_hypercall_pg
);
390 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
393 * Ignore any errors in setting up stimer clockevents
394 * as we can run with the LAPIC timer as a fallback.
396 (void)hv_stimer_alloc();
400 x86_init
.pci
.arch_init
= hv_pci_init
;
402 register_syscore_ops(&hv_syscore_ops
);
407 cpuhp_remove_state(cpuhp
);
409 kfree(hv_vp_assist_page
);
410 hv_vp_assist_page
= NULL
;
417 * This routine is called before kexec/kdump, it does the required cleanup.
419 void hyperv_cleanup(void)
421 union hv_x64_msr_hypercall_contents hypercall_msr
;
423 unregister_syscore_ops(&hv_syscore_ops
);
425 /* Reset our OS id */
426 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, 0);
429 * Reset hypercall page reference before reset the page,
430 * let hypercall operations fail safely rather than
431 * panic the kernel for using invalid hypercall page
433 hv_hypercall_pg
= NULL
;
435 /* Reset the hypercall page */
436 hypercall_msr
.as_uint64
= 0;
437 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
439 /* Reset the TSC page */
440 hypercall_msr
.as_uint64
= 0;
441 wrmsrl(HV_X64_MSR_REFERENCE_TSC
, hypercall_msr
.as_uint64
);
443 EXPORT_SYMBOL_GPL(hyperv_cleanup
);
445 void hyperv_report_panic(struct pt_regs
*regs
, long err
, bool in_die
)
447 static bool panic_reported
;
450 if (in_die
&& !panic_on_oops
)
454 * We prefer to report panic on 'die' chain as we have proper
455 * registers to report, but if we miss it (e.g. on BUG()) we need
456 * to report it on 'panic'.
460 panic_reported
= true;
462 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, guest_id
);
464 wrmsrl(HV_X64_MSR_CRASH_P0
, err
);
465 wrmsrl(HV_X64_MSR_CRASH_P1
, guest_id
);
466 wrmsrl(HV_X64_MSR_CRASH_P2
, regs
->ip
);
467 wrmsrl(HV_X64_MSR_CRASH_P3
, regs
->ax
);
468 wrmsrl(HV_X64_MSR_CRASH_P4
, regs
->sp
);
471 * Let Hyper-V know there is crash data available
473 wrmsrl(HV_X64_MSR_CRASH_CTL
, HV_CRASH_CTL_CRASH_NOTIFY
);
475 EXPORT_SYMBOL_GPL(hyperv_report_panic
);
478 * hyperv_report_panic_msg - report panic message to Hyper-V
479 * @pa: physical address of the panic page containing the message
480 * @size: size of the message in the page
482 void hyperv_report_panic_msg(phys_addr_t pa
, size_t size
)
485 * P3 to contain the physical address of the panic page & P4 to
486 * contain the size of the panic data in that page. Rest of the
487 * registers are no-op when the NOTIFY_MSG flag is set.
489 wrmsrl(HV_X64_MSR_CRASH_P0
, 0);
490 wrmsrl(HV_X64_MSR_CRASH_P1
, 0);
491 wrmsrl(HV_X64_MSR_CRASH_P2
, 0);
492 wrmsrl(HV_X64_MSR_CRASH_P3
, pa
);
493 wrmsrl(HV_X64_MSR_CRASH_P4
, size
);
496 * Let Hyper-V know there is crash data available along with
499 wrmsrl(HV_X64_MSR_CRASH_CTL
,
500 (HV_CRASH_CTL_CRASH_NOTIFY
| HV_CRASH_CTL_CRASH_NOTIFY_MSG
));
502 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg
);
504 bool hv_is_hyperv_initialized(void)
506 union hv_x64_msr_hypercall_contents hypercall_msr
;
509 * Ensure that we're really on Hyper-V, and not a KVM or Xen
510 * emulation of Hyper-V
512 if (x86_hyper_type
!= X86_HYPER_MS_HYPERV
)
516 * Verify that earlier initialization succeeded by checking
517 * that the hypercall page is setup
519 hypercall_msr
.as_uint64
= 0;
520 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
522 return hypercall_msr
.enable
;
524 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized
);
526 bool hv_is_hibernation_supported(void)
528 return acpi_sleep_state_supported(ACPI_STATE_S4
);
530 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported
);