1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * X86 specific Hyper-V initialization code.
4 *
5 * Copyright (C) 2016, Microsoft, Inc.
6 *
7 * Author : K. Y. Srinivasan <kys@microsoft.com>
8 */
9
10 #include <linux/efi.h>
11 #include <linux/types.h>
12 #include <asm/apic.h>
13 #include <asm/desc.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>
19 #include <linux/mm.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>
25
26 void *hv_hypercall_pg;
27 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
28
29 u32 *hv_vp_index;
30 EXPORT_SYMBOL_GPL(hv_vp_index);
31
32 struct hv_vp_assist_page **hv_vp_assist_page;
33 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
34
35 void __percpu **hyperv_pcpu_input_arg;
36 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
37
38 u32 hv_max_vp_index;
39 EXPORT_SYMBOL_GPL(hv_max_vp_index);
40
hv_alloc_hyperv_page(void)41 void *hv_alloc_hyperv_page(void)
42 {
43 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
44
45 return (void *)__get_free_page(GFP_KERNEL);
46 }
47 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
48
hv_free_hyperv_page(unsigned long addr)49 void hv_free_hyperv_page(unsigned long addr)
50 {
51 free_page(addr);
52 }
53 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
54
hv_cpu_init(unsigned int cpu)55 static int hv_cpu_init(unsigned int cpu)
56 {
57 u64 msr_vp_index;
58 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
59 void **input_arg;
60 struct page *pg;
61
62 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
63 pg = alloc_page(GFP_KERNEL);
64 if (unlikely(!pg))
65 return -ENOMEM;
66 *input_arg = page_address(pg);
67
68 hv_get_vp_index(msr_vp_index);
69
70 hv_vp_index[smp_processor_id()] = msr_vp_index;
71
72 if (msr_vp_index > hv_max_vp_index)
73 hv_max_vp_index = msr_vp_index;
74
75 if (!hv_vp_assist_page)
76 return 0;
77
78 /*
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.
84 */
85 if (!*hvp) {
86 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
87 PAGE_KERNEL);
88 }
89
90 if (*hvp) {
91 u64 val;
92
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;
96
97 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
98 }
99
100 return 0;
101 }
102
103 static void (*hv_reenlightenment_cb)(void);
104
hv_reenlightenment_notify(struct work_struct * dummy)105 static void hv_reenlightenment_notify(struct work_struct *dummy)
106 {
107 struct hv_tsc_emulation_status emu_status;
108
109 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
110
111 /* Don't issue the callback if TSC accesses are not emulated */
112 if (hv_reenlightenment_cb && emu_status.inprogress)
113 hv_reenlightenment_cb();
114 }
115 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
116
hyperv_stop_tsc_emulation(void)117 void hyperv_stop_tsc_emulation(void)
118 {
119 u64 freq;
120 struct hv_tsc_emulation_status emu_status;
121
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);
125
126 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
127 tsc_khz = div64_u64(freq, 1000);
128 }
129 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
130
hv_reenlightenment_available(void)131 static inline bool hv_reenlightenment_available(void)
132 {
133 /*
134 * Check for required features and priviliges to make TSC frequency
135 * change notifications work.
136 */
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;
140 }
141
hyperv_reenlightenment_intr(struct pt_regs * regs)142 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
143 {
144 entering_ack_irq();
145
146 inc_irq_stat(irq_hv_reenlightenment_count);
147
148 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
149
150 exiting_irq();
151 }
152
set_hv_tscchange_cb(void (* cb)(void))153 void set_hv_tscchange_cb(void (*cb)(void))
154 {
155 struct hv_reenlightenment_control re_ctrl = {
156 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
157 .enabled = 1,
158 };
159 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
160
161 if (!hv_reenlightenment_available()) {
162 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
163 return;
164 }
165
166 if (!hv_vp_index)
167 return;
168
169 hv_reenlightenment_cb = cb;
170
171 /* Make sure callback is registered before we write to MSRs */
172 wmb();
173
174 re_ctrl.target_vp = hv_vp_index[get_cpu()];
175
176 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
177 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
178
179 put_cpu();
180 }
181 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
182
clear_hv_tscchange_cb(void)183 void clear_hv_tscchange_cb(void)
184 {
185 struct hv_reenlightenment_control re_ctrl;
186
187 if (!hv_reenlightenment_available())
188 return;
189
190 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
191 re_ctrl.enabled = 0;
192 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
193
194 hv_reenlightenment_cb = NULL;
195 }
196 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
197
hv_cpu_die(unsigned int cpu)198 static int hv_cpu_die(unsigned int cpu)
199 {
200 struct hv_reenlightenment_control re_ctrl;
201 unsigned int new_cpu;
202 unsigned long flags;
203 void **input_arg;
204 void *input_pg = NULL;
205
206 local_irq_save(flags);
207 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
208 input_pg = *input_arg;
209 *input_arg = NULL;
210 local_irq_restore(flags);
211 free_page((unsigned long)input_pg);
212
213 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
214 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
215
216 if (hv_reenlightenment_cb == NULL)
217 return 0;
218
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);
223
224 re_ctrl.target_vp = hv_vp_index[new_cpu];
225 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
226 }
227
228 return 0;
229 }
230
hv_pci_init(void)231 static int __init hv_pci_init(void)
232 {
233 int gen2vm = efi_enabled(EFI_BOOT);
234
235 /*
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"
239 */
240 if (gen2vm)
241 return 0;
242
243 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
244 return 1;
245 }
246
247 /*
248 * This function is to be invoked early in the boot sequence after the
249 * hypervisor has been detected.
250 *
251 * 1. Setup the hypercall page.
252 * 2. Register Hyper-V specific clocksource.
253 * 3. Setup Hyper-V specific APIC entry points.
254 */
hyperv_init(void)255 void __init hyperv_init(void)
256 {
257 u64 guest_id, required_msrs;
258 union hv_x64_msr_hypercall_contents hypercall_msr;
259 int cpuhp, i;
260
261 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
262 return;
263
264 /* Absolutely required MSRs */
265 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
266 HV_X64_MSR_VP_INDEX_AVAILABLE;
267
268 if ((ms_hyperv.features & required_msrs) != required_msrs)
269 return;
270
271 /*
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
275 * fatal on Hyper-V.
276 */
277 hyperv_pcpu_input_arg = alloc_percpu(void *);
278
279 BUG_ON(hyperv_pcpu_input_arg == NULL);
280
281 /* Allocate percpu VP index */
282 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
283 GFP_KERNEL);
284 if (!hv_vp_index)
285 return;
286
287 for (i = 0; i < num_possible_cpus(); i++)
288 hv_vp_index[i] = VP_INVAL;
289
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;
294 goto free_vp_index;
295 }
296
297 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
298 hv_cpu_init, hv_cpu_die);
299 if (cpuhp < 0)
300 goto free_vp_assist_page;
301
302 /*
303 * Setup the hypercall page and enable hypercalls.
304 * 1. Register the guest ID
305 * 2. Enable the hypercall and register the hypercall page
306 */
307 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
308 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
309
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;
314 }
315
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);
320
321 hv_apic_init();
322
323 x86_init.pci.arch_init = hv_pci_init;
324
325 return;
326
327 remove_cpuhp_state:
328 cpuhp_remove_state(cpuhp);
329 free_vp_assist_page:
330 kfree(hv_vp_assist_page);
331 hv_vp_assist_page = NULL;
332 free_vp_index:
333 kfree(hv_vp_index);
334 hv_vp_index = NULL;
335 }
336
337 /*
338 * This routine is called before kexec/kdump, it does the required cleanup.
339 */
hyperv_cleanup(void)340 void hyperv_cleanup(void)
341 {
342 union hv_x64_msr_hypercall_contents hypercall_msr;
343
344 /* Reset our OS id */
345 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
346
347 /*
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
351 */
352 hv_hypercall_pg = NULL;
353
354 /* Reset the hypercall page */
355 hypercall_msr.as_uint64 = 0;
356 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
357
358 /* Reset the TSC page */
359 hypercall_msr.as_uint64 = 0;
360 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
361 }
362 EXPORT_SYMBOL_GPL(hyperv_cleanup);
363
hyperv_report_panic(struct pt_regs * regs,long err,bool in_die)364 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
365 {
366 static bool panic_reported;
367 u64 guest_id;
368
369 if (in_die && !panic_on_oops)
370 return;
371
372 /*
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'.
376 */
377 if (panic_reported)
378 return;
379 panic_reported = true;
380
381 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
382
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);
388
389 /*
390 * Let Hyper-V know there is crash data available
391 */
392 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
393 }
394 EXPORT_SYMBOL_GPL(hyperv_report_panic);
395
396 /**
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
400 */
hyperv_report_panic_msg(phys_addr_t pa,size_t size)401 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
402 {
403 /*
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.
407 */
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);
413
414 /*
415 * Let Hyper-V know there is crash data available along with
416 * the panic message.
417 */
418 wrmsrl(HV_X64_MSR_CRASH_CTL,
419 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
420 }
421 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
422
hv_is_hyperv_initialized(void)423 bool hv_is_hyperv_initialized(void)
424 {
425 union hv_x64_msr_hypercall_contents hypercall_msr;
426
427 /*
428 * Ensure that we're really on Hyper-V, and not a KVM or Xen
429 * emulation of Hyper-V
430 */
431 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
432 return false;
433
434 /*
435 * Verify that earlier initialization succeeded by checking
436 * that the hypercall page is setup
437 */
438 hypercall_msr.as_uint64 = 0;
439 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
440
441 return hypercall_msr.enable;
442 }
443 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
444