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>
14 #include <asm/hypervisor.h>
15 #include <asm/hyperv-tlfs.h>
16 #include <asm/mshyperv.h>
17 #include <linux/version.h>
18 #include <linux/vmalloc.h>
20 #include <linux/hyperv.h>
21 #include <linux/slab.h>
22 #include <linux/kernel.h>
23 #include <linux/cpuhotplug.h>
24 #include <clocksource/hyperv_timer.h>
26 void *hv_hypercall_pg;
27 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
30 EXPORT_SYMBOL_GPL(hv_vp_index);
32 struct hv_vp_assist_page **hv_vp_assist_page;
33 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
35 void __percpu **hyperv_pcpu_input_arg;
36 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
39 EXPORT_SYMBOL_GPL(hv_max_vp_index);
41 void *hv_alloc_hyperv_page(void)
43 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
45 return (void *)__get_free_page(GFP_KERNEL);
47 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
49 void hv_free_hyperv_page(unsigned long addr)
53 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
55 static int hv_cpu_init(unsigned int cpu)
58 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
62 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
63 pg = alloc_page(GFP_KERNEL);
66 *input_arg = page_address(pg);
68 hv_get_vp_index(msr_vp_index);
70 hv_vp_index[smp_processor_id()] = msr_vp_index;
72 if (msr_vp_index > hv_max_vp_index)
73 hv_max_vp_index = msr_vp_index;
75 if (!hv_vp_assist_page)
79 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
80 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
81 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
82 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
83 * not be stopped in the case of CPU offlining and the VM will hang.
86 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
93 val = vmalloc_to_pfn(*hvp);
94 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
95 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
97 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
103 static void (*hv_reenlightenment_cb)(void);
105 static void hv_reenlightenment_notify(struct work_struct *dummy)
107 struct hv_tsc_emulation_status emu_status;
109 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
111 /* Don't issue the callback if TSC accesses are not emulated */
112 if (hv_reenlightenment_cb && emu_status.inprogress)
113 hv_reenlightenment_cb();
115 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
117 void hyperv_stop_tsc_emulation(void)
120 struct hv_tsc_emulation_status emu_status;
122 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
123 emu_status.inprogress = 0;
124 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
126 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
127 tsc_khz = div64_u64(freq, 1000);
129 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
131 static inline bool hv_reenlightenment_available(void)
134 * Check for required features and priviliges to make TSC frequency
135 * change notifications work.
137 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
138 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
139 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
142 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
146 inc_irq_stat(irq_hv_reenlightenment_count);
148 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
153 void set_hv_tscchange_cb(void (*cb)(void))
155 struct hv_reenlightenment_control re_ctrl = {
156 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
159 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
161 if (!hv_reenlightenment_available()) {
162 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
169 hv_reenlightenment_cb = cb;
171 /* Make sure callback is registered before we write to MSRs */
174 re_ctrl.target_vp = hv_vp_index[get_cpu()];
176 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
177 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
181 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
183 void clear_hv_tscchange_cb(void)
185 struct hv_reenlightenment_control re_ctrl;
187 if (!hv_reenlightenment_available())
190 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
192 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
194 hv_reenlightenment_cb = NULL;
196 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
198 static int hv_cpu_die(unsigned int cpu)
200 struct hv_reenlightenment_control re_ctrl;
201 unsigned int new_cpu;
204 void *input_pg = NULL;
206 local_irq_save(flags);
207 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
208 input_pg = *input_arg;
210 local_irq_restore(flags);
211 free_page((unsigned long)input_pg);
213 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
214 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
216 if (hv_reenlightenment_cb == NULL)
219 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
220 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
221 /* Reassign to some other online CPU */
222 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
224 re_ctrl.target_vp = hv_vp_index[new_cpu];
225 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
231 static int __init hv_pci_init(void)
233 int gen2vm = efi_enabled(EFI_BOOT);
236 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
237 * The purpose is to suppress the harmless warning:
238 * "PCI: Fatal: No config space access function found"
243 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
248 * This function is to be invoked early in the boot sequence after the
249 * hypervisor has been detected.
251 * 1. Setup the hypercall page.
252 * 2. Register Hyper-V specific clocksource.
253 * 3. Setup Hyper-V specific APIC entry points.
255 void __init hyperv_init(void)
257 u64 guest_id, required_msrs;
258 union hv_x64_msr_hypercall_contents hypercall_msr;
261 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
264 /* Absolutely required MSRs */
265 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
266 HV_X64_MSR_VP_INDEX_AVAILABLE;
268 if ((ms_hyperv.features & required_msrs) != required_msrs)
272 * Allocate the per-CPU state for the hypercall input arg.
273 * If this allocation fails, we will not be able to setup
274 * (per-CPU) hypercall input page and thus this failure is
277 hyperv_pcpu_input_arg = alloc_percpu(void *);
279 BUG_ON(hyperv_pcpu_input_arg == NULL);
281 /* Allocate percpu VP index */
282 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
287 for (i = 0; i < num_possible_cpus(); i++)
288 hv_vp_index[i] = VP_INVAL;
290 hv_vp_assist_page = kcalloc(num_possible_cpus(),
291 sizeof(*hv_vp_assist_page), GFP_KERNEL);
292 if (!hv_vp_assist_page) {
293 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
297 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
298 hv_cpu_init, hv_cpu_die);
300 goto free_vp_assist_page;
303 * Setup the hypercall page and enable hypercalls.
304 * 1. Register the guest ID
305 * 2. Enable the hypercall and register the hypercall page
307 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
308 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
310 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
311 if (hv_hypercall_pg == NULL) {
312 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
313 goto remove_cpuhp_state;
316 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
317 hypercall_msr.enable = 1;
318 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
319 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
323 x86_init.pci.arch_init = hv_pci_init;
328 cpuhp_remove_state(cpuhp);
330 kfree(hv_vp_assist_page);
331 hv_vp_assist_page = NULL;
338 * This routine is called before kexec/kdump, it does the required cleanup.
340 void hyperv_cleanup(void)
342 union hv_x64_msr_hypercall_contents hypercall_msr;
344 /* Reset our OS id */
345 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
348 * Reset hypercall page reference before reset the page,
349 * let hypercall operations fail safely rather than
350 * panic the kernel for using invalid hypercall page
352 hv_hypercall_pg = NULL;
354 /* Reset the hypercall page */
355 hypercall_msr.as_uint64 = 0;
356 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
358 /* Reset the TSC page */
359 hypercall_msr.as_uint64 = 0;
360 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
362 EXPORT_SYMBOL_GPL(hyperv_cleanup);
364 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
366 static bool panic_reported;
369 if (in_die && !panic_on_oops)
373 * We prefer to report panic on 'die' chain as we have proper
374 * registers to report, but if we miss it (e.g. on BUG()) we need
375 * to report it on 'panic'.
379 panic_reported = true;
381 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
383 wrmsrl(HV_X64_MSR_CRASH_P0, err);
384 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
385 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
386 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
387 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
390 * Let Hyper-V know there is crash data available
392 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
394 EXPORT_SYMBOL_GPL(hyperv_report_panic);
397 * hyperv_report_panic_msg - report panic message to Hyper-V
398 * @pa: physical address of the panic page containing the message
399 * @size: size of the message in the page
401 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
404 * P3 to contain the physical address of the panic page & P4 to
405 * contain the size of the panic data in that page. Rest of the
406 * registers are no-op when the NOTIFY_MSG flag is set.
408 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
409 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
410 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
411 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
412 wrmsrl(HV_X64_MSR_CRASH_P4, size);
415 * Let Hyper-V know there is crash data available along with
418 wrmsrl(HV_X64_MSR_CRASH_CTL,
419 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
421 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
423 bool hv_is_hyperv_initialized(void)
425 union hv_x64_msr_hypercall_contents hypercall_msr;
428 * Ensure that we're really on Hyper-V, and not a KVM or Xen
429 * emulation of Hyper-V
431 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
435 * Verify that earlier initialization succeeded by checking
436 * that the hypercall page is setup
438 hypercall_msr.as_uint64 = 0;
439 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
441 return hypercall_msr.enable;
443 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);