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 void *hv_alloc_hyperv_page(void)
42 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
44 return (void *)__get_free_page(GFP_KERNEL);
46 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
48 void hv_free_hyperv_page(unsigned long addr)
52 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
54 static int hv_cpu_init(unsigned int cpu)
57 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
61 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
62 pg = alloc_page(GFP_KERNEL);
65 *input_arg = page_address(pg);
67 hv_get_vp_index(msr_vp_index);
69 hv_vp_index[smp_processor_id()] = msr_vp_index;
71 if (msr_vp_index > hv_max_vp_index)
72 hv_max_vp_index = msr_vp_index;
74 if (!hv_vp_assist_page)
78 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
79 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
80 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
81 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
82 * not be stopped in the case of CPU offlining and the VM will hang.
85 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
92 val = vmalloc_to_pfn(*hvp);
93 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
94 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
96 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
102 static void (*hv_reenlightenment_cb)(void);
104 static void hv_reenlightenment_notify(struct work_struct *dummy)
106 struct hv_tsc_emulation_status emu_status;
108 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
110 /* Don't issue the callback if TSC accesses are not emulated */
111 if (hv_reenlightenment_cb && emu_status.inprogress)
112 hv_reenlightenment_cb();
114 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
116 void hyperv_stop_tsc_emulation(void)
119 struct hv_tsc_emulation_status emu_status;
121 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
122 emu_status.inprogress = 0;
123 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
125 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
126 tsc_khz = div64_u64(freq, 1000);
128 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
130 static inline bool hv_reenlightenment_available(void)
133 * Check for required features and priviliges to make TSC frequency
134 * change notifications work.
136 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
137 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
138 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
141 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
145 inc_irq_stat(irq_hv_reenlightenment_count);
147 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
152 void set_hv_tscchange_cb(void (*cb)(void))
154 struct hv_reenlightenment_control re_ctrl = {
155 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
157 .target_vp = hv_vp_index[smp_processor_id()]
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");
166 hv_reenlightenment_cb = cb;
168 /* Make sure callback is registered before we write to MSRs */
171 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
172 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
174 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
176 void clear_hv_tscchange_cb(void)
178 struct hv_reenlightenment_control re_ctrl;
180 if (!hv_reenlightenment_available())
183 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
185 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
187 hv_reenlightenment_cb = NULL;
189 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
191 static int hv_cpu_die(unsigned int cpu)
193 struct hv_reenlightenment_control re_ctrl;
194 unsigned int new_cpu;
197 void *input_pg = NULL;
199 local_irq_save(flags);
200 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
201 input_pg = *input_arg;
203 local_irq_restore(flags);
204 free_page((unsigned long)input_pg);
206 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
207 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
209 if (hv_reenlightenment_cb == NULL)
212 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
213 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
214 /* Reassign to some other online CPU */
215 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
217 re_ctrl.target_vp = hv_vp_index[new_cpu];
218 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
224 static int __init hv_pci_init(void)
226 int gen2vm = efi_enabled(EFI_BOOT);
229 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
230 * The purpose is to suppress the harmless warning:
231 * "PCI: Fatal: No config space access function found"
236 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
241 * This function is to be invoked early in the boot sequence after the
242 * hypervisor has been detected.
244 * 1. Setup the hypercall page.
245 * 2. Register Hyper-V specific clocksource.
246 * 3. Setup Hyper-V specific APIC entry points.
248 void __init hyperv_init(void)
250 u64 guest_id, required_msrs;
251 union hv_x64_msr_hypercall_contents hypercall_msr;
254 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
257 /* Absolutely required MSRs */
258 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
259 HV_X64_MSR_VP_INDEX_AVAILABLE;
261 if ((ms_hyperv.features & required_msrs) != required_msrs)
265 * Allocate the per-CPU state for the hypercall input arg.
266 * If this allocation fails, we will not be able to setup
267 * (per-CPU) hypercall input page and thus this failure is
270 hyperv_pcpu_input_arg = alloc_percpu(void *);
272 BUG_ON(hyperv_pcpu_input_arg == NULL);
274 /* Allocate percpu VP index */
275 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
280 for (i = 0; i < num_possible_cpus(); i++)
281 hv_vp_index[i] = VP_INVAL;
283 hv_vp_assist_page = kcalloc(num_possible_cpus(),
284 sizeof(*hv_vp_assist_page), GFP_KERNEL);
285 if (!hv_vp_assist_page) {
286 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
290 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
291 hv_cpu_init, hv_cpu_die);
293 goto free_vp_assist_page;
296 * Setup the hypercall page and enable hypercalls.
297 * 1. Register the guest ID
298 * 2. Enable the hypercall and register the hypercall page
300 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
301 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
303 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
304 if (hv_hypercall_pg == NULL) {
305 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
306 goto remove_cpuhp_state;
309 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
310 hypercall_msr.enable = 1;
311 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
312 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
316 x86_init.pci.arch_init = hv_pci_init;
321 cpuhp_remove_state(cpuhp);
323 kfree(hv_vp_assist_page);
324 hv_vp_assist_page = NULL;
331 * This routine is called before kexec/kdump, it does the required cleanup.
333 void hyperv_cleanup(void)
335 union hv_x64_msr_hypercall_contents hypercall_msr;
337 /* Reset our OS id */
338 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
341 * Reset hypercall page reference before reset the page,
342 * let hypercall operations fail safely rather than
343 * panic the kernel for using invalid hypercall page
345 hv_hypercall_pg = NULL;
347 /* Reset the hypercall page */
348 hypercall_msr.as_uint64 = 0;
349 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
351 /* Reset the TSC page */
352 hypercall_msr.as_uint64 = 0;
353 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
355 EXPORT_SYMBOL_GPL(hyperv_cleanup);
357 void hyperv_report_panic(struct pt_regs *regs, long err)
359 static bool panic_reported;
363 * We prefer to report panic on 'die' chain as we have proper
364 * registers to report, but if we miss it (e.g. on BUG()) we need
365 * to report it on 'panic'.
369 panic_reported = true;
371 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
373 wrmsrl(HV_X64_MSR_CRASH_P0, err);
374 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
375 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
376 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
377 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
380 * Let Hyper-V know there is crash data available
382 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
384 EXPORT_SYMBOL_GPL(hyperv_report_panic);
387 * hyperv_report_panic_msg - report panic message to Hyper-V
388 * @pa: physical address of the panic page containing the message
389 * @size: size of the message in the page
391 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
394 * P3 to contain the physical address of the panic page & P4 to
395 * contain the size of the panic data in that page. Rest of the
396 * registers are no-op when the NOTIFY_MSG flag is set.
398 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
399 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
400 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
401 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
402 wrmsrl(HV_X64_MSR_CRASH_P4, size);
405 * Let Hyper-V know there is crash data available along with
408 wrmsrl(HV_X64_MSR_CRASH_CTL,
409 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
411 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
413 bool hv_is_hyperv_initialized(void)
415 union hv_x64_msr_hypercall_contents hypercall_msr;
418 * Ensure that we're really on Hyper-V, and not a KVM or Xen
419 * emulation of Hyper-V
421 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
425 * Verify that earlier initialization succeeded by checking
426 * that the hypercall page is setup
428 hypercall_msr.as_uint64 = 0;
429 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
431 return hypercall_msr.enable;
433 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);