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/cpuhotplug.h>
23 #include <clocksource/hyperv_timer.h>
25 void *hv_hypercall_pg;
26 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
29 EXPORT_SYMBOL_GPL(hv_vp_index);
31 struct hv_vp_assist_page **hv_vp_assist_page;
32 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
34 void __percpu **hyperv_pcpu_input_arg;
35 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
38 EXPORT_SYMBOL_GPL(hv_max_vp_index);
40 static int hv_cpu_init(unsigned int cpu)
43 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
47 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
48 pg = alloc_page(GFP_KERNEL);
51 *input_arg = page_address(pg);
53 hv_get_vp_index(msr_vp_index);
55 hv_vp_index[smp_processor_id()] = msr_vp_index;
57 if (msr_vp_index > hv_max_vp_index)
58 hv_max_vp_index = msr_vp_index;
60 if (!hv_vp_assist_page)
64 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
65 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
66 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
67 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
68 * not be stopped in the case of CPU offlining and the VM will hang.
71 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
78 val = vmalloc_to_pfn(*hvp);
79 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
80 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
82 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
88 static void (*hv_reenlightenment_cb)(void);
90 static void hv_reenlightenment_notify(struct work_struct *dummy)
92 struct hv_tsc_emulation_status emu_status;
94 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
96 /* Don't issue the callback if TSC accesses are not emulated */
97 if (hv_reenlightenment_cb && emu_status.inprogress)
98 hv_reenlightenment_cb();
100 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
102 void hyperv_stop_tsc_emulation(void)
105 struct hv_tsc_emulation_status emu_status;
107 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
108 emu_status.inprogress = 0;
109 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
111 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
112 tsc_khz = div64_u64(freq, 1000);
114 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
116 static inline bool hv_reenlightenment_available(void)
119 * Check for required features and priviliges to make TSC frequency
120 * change notifications work.
122 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
123 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
124 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
127 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
131 inc_irq_stat(irq_hv_reenlightenment_count);
133 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
138 void set_hv_tscchange_cb(void (*cb)(void))
140 struct hv_reenlightenment_control re_ctrl = {
141 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
143 .target_vp = hv_vp_index[smp_processor_id()]
145 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
147 if (!hv_reenlightenment_available()) {
148 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
152 hv_reenlightenment_cb = cb;
154 /* Make sure callback is registered before we write to MSRs */
157 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
158 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
160 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
162 void clear_hv_tscchange_cb(void)
164 struct hv_reenlightenment_control re_ctrl;
166 if (!hv_reenlightenment_available())
169 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
171 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
173 hv_reenlightenment_cb = NULL;
175 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
177 static int hv_cpu_die(unsigned int cpu)
179 struct hv_reenlightenment_control re_ctrl;
180 unsigned int new_cpu;
183 void *input_pg = NULL;
185 local_irq_save(flags);
186 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
187 input_pg = *input_arg;
189 local_irq_restore(flags);
190 free_page((unsigned long)input_pg);
192 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
193 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
195 if (hv_reenlightenment_cb == NULL)
198 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
199 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
200 /* Reassign to some other online CPU */
201 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
203 re_ctrl.target_vp = hv_vp_index[new_cpu];
204 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
210 static int __init hv_pci_init(void)
212 int gen2vm = efi_enabled(EFI_BOOT);
215 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
216 * The purpose is to suppress the harmless warning:
217 * "PCI: Fatal: No config space access function found"
222 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
227 * This function is to be invoked early in the boot sequence after the
228 * hypervisor has been detected.
230 * 1. Setup the hypercall page.
231 * 2. Register Hyper-V specific clocksource.
232 * 3. Setup Hyper-V specific APIC entry points.
234 void __init hyperv_init(void)
236 u64 guest_id, required_msrs;
237 union hv_x64_msr_hypercall_contents hypercall_msr;
240 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
243 /* Absolutely required MSRs */
244 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
245 HV_X64_MSR_VP_INDEX_AVAILABLE;
247 if ((ms_hyperv.features & required_msrs) != required_msrs)
251 * Allocate the per-CPU state for the hypercall input arg.
252 * If this allocation fails, we will not be able to setup
253 * (per-CPU) hypercall input page and thus this failure is
256 hyperv_pcpu_input_arg = alloc_percpu(void *);
258 BUG_ON(hyperv_pcpu_input_arg == NULL);
260 /* Allocate percpu VP index */
261 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
266 for (i = 0; i < num_possible_cpus(); i++)
267 hv_vp_index[i] = VP_INVAL;
269 hv_vp_assist_page = kcalloc(num_possible_cpus(),
270 sizeof(*hv_vp_assist_page), GFP_KERNEL);
271 if (!hv_vp_assist_page) {
272 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
276 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
277 hv_cpu_init, hv_cpu_die);
279 goto free_vp_assist_page;
282 * Setup the hypercall page and enable hypercalls.
283 * 1. Register the guest ID
284 * 2. Enable the hypercall and register the hypercall page
286 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
287 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
289 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
290 if (hv_hypercall_pg == NULL) {
291 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
292 goto remove_cpuhp_state;
295 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
296 hypercall_msr.enable = 1;
297 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
298 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
302 x86_init.pci.arch_init = hv_pci_init;
304 /* Register Hyper-V specific clocksource */
305 hv_init_clocksource();
309 cpuhp_remove_state(cpuhp);
311 kfree(hv_vp_assist_page);
312 hv_vp_assist_page = NULL;
319 * This routine is called before kexec/kdump, it does the required cleanup.
321 void hyperv_cleanup(void)
323 union hv_x64_msr_hypercall_contents hypercall_msr;
325 /* Reset our OS id */
326 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
329 * Reset hypercall page reference before reset the page,
330 * let hypercall operations fail safely rather than
331 * panic the kernel for using invalid hypercall page
333 hv_hypercall_pg = NULL;
335 /* Reset the hypercall page */
336 hypercall_msr.as_uint64 = 0;
337 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
339 /* Reset the TSC page */
340 hypercall_msr.as_uint64 = 0;
341 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
343 EXPORT_SYMBOL_GPL(hyperv_cleanup);
345 void hyperv_report_panic(struct pt_regs *regs, long err)
347 static bool panic_reported;
351 * We prefer to report panic on 'die' chain as we have proper
352 * registers to report, but if we miss it (e.g. on BUG()) we need
353 * to report it on 'panic'.
357 panic_reported = true;
359 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
361 wrmsrl(HV_X64_MSR_CRASH_P0, err);
362 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
363 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
364 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
365 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
368 * Let Hyper-V know there is crash data available
370 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
372 EXPORT_SYMBOL_GPL(hyperv_report_panic);
375 * hyperv_report_panic_msg - report panic message to Hyper-V
376 * @pa: physical address of the panic page containing the message
377 * @size: size of the message in the page
379 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
382 * P3 to contain the physical address of the panic page & P4 to
383 * contain the size of the panic data in that page. Rest of the
384 * registers are no-op when the NOTIFY_MSG flag is set.
386 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
387 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
388 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
389 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
390 wrmsrl(HV_X64_MSR_CRASH_P4, size);
393 * Let Hyper-V know there is crash data available along with
396 wrmsrl(HV_X64_MSR_CRASH_CTL,
397 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
399 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
401 bool hv_is_hyperv_initialized(void)
403 union hv_x64_msr_hypercall_contents hypercall_msr;
406 * Ensure that we're really on Hyper-V, and not a KVM or Xen
407 * emulation of Hyper-V
409 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
413 * Verify that earlier initialization succeeded by checking
414 * that the hypercall page is setup
416 hypercall_msr.as_uint64 = 0;
417 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
419 return hypercall_msr.enable;
421 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);