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 <linux/bitfield.h>
13 #include <linux/io.h>
14 #include <asm/apic.h>
15 #include <asm/desc.h>
16 #include <asm/hypervisor.h>
17 #include <asm/hyperv-tlfs.h>
18 #include <asm/mshyperv.h>
19 #include <asm/idtentry.h>
20 #include <linux/kexec.h>
21 #include <linux/version.h>
22 #include <linux/vmalloc.h>
23 #include <linux/mm.h>
24 #include <linux/hyperv.h>
25 #include <linux/slab.h>
26 #include <linux/kernel.h>
27 #include <linux/cpuhotplug.h>
28 #include <linux/syscore_ops.h>
29 #include <clocksource/hyperv_timer.h>
30 #include <linux/highmem.h>
31
32 int hyperv_init_cpuhp;
33 u64 hv_current_partition_id = ~0ull;
34 EXPORT_SYMBOL_GPL(hv_current_partition_id);
35
36 void *hv_hypercall_pg;
37 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
38
39 /* Storage to save the hypercall page temporarily for hibernation */
40 static void *hv_hypercall_pg_saved;
41
42 struct hv_vp_assist_page **hv_vp_assist_page;
43 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
44
hv_cpu_init(unsigned int cpu)45 static int hv_cpu_init(unsigned int cpu)
46 {
47 union hv_vp_assist_msr_contents msr = { 0 };
48 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
49 int ret;
50
51 ret = hv_common_cpu_init(cpu);
52 if (ret)
53 return ret;
54
55 if (!hv_vp_assist_page)
56 return 0;
57
58 if (hv_root_partition) {
59 /*
60 * For root partition we get the hypervisor provided VP assist
61 * page, instead of allocating a new page.
62 */
63 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
64 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
65 PAGE_SIZE, MEMREMAP_WB);
66 } else {
67 /*
68 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
69 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
70 * out to make sure we always write the EOI MSR in
71 * hv_apic_eoi_write() *after* the EOI optimization is disabled
72 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
73 * case of CPU offlining and the VM will hang.
74 */
75 if (!*hvp)
76 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
77 if (*hvp)
78 msr.pfn = vmalloc_to_pfn(*hvp);
79
80 }
81 if (!WARN_ON(!(*hvp))) {
82 msr.enable = 1;
83 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
84 }
85
86 return 0;
87 }
88
89 static void (*hv_reenlightenment_cb)(void);
90
hv_reenlightenment_notify(struct work_struct * dummy)91 static void hv_reenlightenment_notify(struct work_struct *dummy)
92 {
93 struct hv_tsc_emulation_status emu_status;
94
95 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
96
97 /* Don't issue the callback if TSC accesses are not emulated */
98 if (hv_reenlightenment_cb && emu_status.inprogress)
99 hv_reenlightenment_cb();
100 }
101 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
102
hyperv_stop_tsc_emulation(void)103 void hyperv_stop_tsc_emulation(void)
104 {
105 u64 freq;
106 struct hv_tsc_emulation_status emu_status;
107
108 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
109 emu_status.inprogress = 0;
110 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
111
112 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
113 tsc_khz = div64_u64(freq, 1000);
114 }
115 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
116
hv_reenlightenment_available(void)117 static inline bool hv_reenlightenment_available(void)
118 {
119 /*
120 * Check for required features and privileges to make TSC frequency
121 * change notifications work.
122 */
123 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
124 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
125 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
126 }
127
DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)128 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
129 {
130 ack_APIC_irq();
131 inc_irq_stat(irq_hv_reenlightenment_count);
132 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
133 }
134
set_hv_tscchange_cb(void (* cb)(void))135 void set_hv_tscchange_cb(void (*cb)(void))
136 {
137 struct hv_reenlightenment_control re_ctrl = {
138 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
139 .enabled = 1,
140 };
141 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
142
143 if (!hv_reenlightenment_available()) {
144 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
145 return;
146 }
147
148 if (!hv_vp_index)
149 return;
150
151 hv_reenlightenment_cb = cb;
152
153 /* Make sure callback is registered before we write to MSRs */
154 wmb();
155
156 re_ctrl.target_vp = hv_vp_index[get_cpu()];
157
158 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
159 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
160
161 put_cpu();
162 }
163 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
164
clear_hv_tscchange_cb(void)165 void clear_hv_tscchange_cb(void)
166 {
167 struct hv_reenlightenment_control re_ctrl;
168
169 if (!hv_reenlightenment_available())
170 return;
171
172 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
173 re_ctrl.enabled = 0;
174 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
175
176 hv_reenlightenment_cb = NULL;
177 }
178 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
179
hv_cpu_die(unsigned int cpu)180 static int hv_cpu_die(unsigned int cpu)
181 {
182 struct hv_reenlightenment_control re_ctrl;
183 unsigned int new_cpu;
184
185 hv_common_cpu_die(cpu);
186
187 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
188 union hv_vp_assist_msr_contents msr = { 0 };
189 if (hv_root_partition) {
190 /*
191 * For root partition the VP assist page is mapped to
192 * hypervisor provided page, and thus we unmap the
193 * page here and nullify it, so that in future we have
194 * correct page address mapped in hv_cpu_init.
195 */
196 memunmap(hv_vp_assist_page[cpu]);
197 hv_vp_assist_page[cpu] = NULL;
198 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
199 msr.enable = 0;
200 }
201 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
202 }
203
204 if (hv_reenlightenment_cb == NULL)
205 return 0;
206
207 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
208 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
209 /*
210 * Reassign reenlightenment notifications to some other online
211 * CPU or just disable the feature if there are no online CPUs
212 * left (happens on hibernation).
213 */
214 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
215
216 if (new_cpu < nr_cpu_ids)
217 re_ctrl.target_vp = hv_vp_index[new_cpu];
218 else
219 re_ctrl.enabled = 0;
220
221 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
222 }
223
224 return 0;
225 }
226
hv_pci_init(void)227 static int __init hv_pci_init(void)
228 {
229 int gen2vm = efi_enabled(EFI_BOOT);
230
231 /*
232 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
233 * The purpose is to suppress the harmless warning:
234 * "PCI: Fatal: No config space access function found"
235 */
236 if (gen2vm)
237 return 0;
238
239 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
240 return 1;
241 }
242
hv_suspend(void)243 static int hv_suspend(void)
244 {
245 union hv_x64_msr_hypercall_contents hypercall_msr;
246 int ret;
247
248 if (hv_root_partition)
249 return -EPERM;
250
251 /*
252 * Reset the hypercall page as it is going to be invalidated
253 * across hibernation. Setting hv_hypercall_pg to NULL ensures
254 * that any subsequent hypercall operation fails safely instead of
255 * crashing due to an access of an invalid page. The hypercall page
256 * pointer is restored on resume.
257 */
258 hv_hypercall_pg_saved = hv_hypercall_pg;
259 hv_hypercall_pg = NULL;
260
261 /* Disable the hypercall page in the hypervisor */
262 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
263 hypercall_msr.enable = 0;
264 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
265
266 ret = hv_cpu_die(0);
267 return ret;
268 }
269
hv_resume(void)270 static void hv_resume(void)
271 {
272 union hv_x64_msr_hypercall_contents hypercall_msr;
273 int ret;
274
275 ret = hv_cpu_init(0);
276 WARN_ON(ret);
277
278 /* Re-enable the hypercall page */
279 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
280 hypercall_msr.enable = 1;
281 hypercall_msr.guest_physical_address =
282 vmalloc_to_pfn(hv_hypercall_pg_saved);
283 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
284
285 hv_hypercall_pg = hv_hypercall_pg_saved;
286 hv_hypercall_pg_saved = NULL;
287
288 /*
289 * Reenlightenment notifications are disabled by hv_cpu_die(0),
290 * reenable them here if hv_reenlightenment_cb was previously set.
291 */
292 if (hv_reenlightenment_cb)
293 set_hv_tscchange_cb(hv_reenlightenment_cb);
294 }
295
296 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
297 static struct syscore_ops hv_syscore_ops = {
298 .suspend = hv_suspend,
299 .resume = hv_resume,
300 };
301
302 static void (* __initdata old_setup_percpu_clockev)(void);
303
hv_stimer_setup_percpu_clockev(void)304 static void __init hv_stimer_setup_percpu_clockev(void)
305 {
306 /*
307 * Ignore any errors in setting up stimer clockevents
308 * as we can run with the LAPIC timer as a fallback.
309 */
310 (void)hv_stimer_alloc(false);
311
312 /*
313 * Still register the LAPIC timer, because the direct-mode STIMER is
314 * not supported by old versions of Hyper-V. This also allows users
315 * to switch to LAPIC timer via /sys, if they want to.
316 */
317 if (old_setup_percpu_clockev)
318 old_setup_percpu_clockev();
319 }
320
hv_get_partition_id(void)321 static void __init hv_get_partition_id(void)
322 {
323 struct hv_get_partition_id *output_page;
324 u64 status;
325 unsigned long flags;
326
327 local_irq_save(flags);
328 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
329 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
330 if (!hv_result_success(status)) {
331 /* No point in proceeding if this failed */
332 pr_err("Failed to get partition ID: %lld\n", status);
333 BUG();
334 }
335 hv_current_partition_id = output_page->partition_id;
336 local_irq_restore(flags);
337 }
338
339 /*
340 * This function is to be invoked early in the boot sequence after the
341 * hypervisor has been detected.
342 *
343 * 1. Setup the hypercall page.
344 * 2. Register Hyper-V specific clocksource.
345 * 3. Setup Hyper-V specific APIC entry points.
346 */
hyperv_init(void)347 void __init hyperv_init(void)
348 {
349 u64 guest_id;
350 union hv_x64_msr_hypercall_contents hypercall_msr;
351 int cpuhp;
352
353 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
354 return;
355
356 if (hv_common_init())
357 return;
358
359 hv_vp_assist_page = kcalloc(num_possible_cpus(),
360 sizeof(*hv_vp_assist_page), GFP_KERNEL);
361 if (!hv_vp_assist_page) {
362 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
363 goto common_free;
364 }
365
366 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
367 hv_cpu_init, hv_cpu_die);
368 if (cpuhp < 0)
369 goto free_vp_assist_page;
370
371 /*
372 * Setup the hypercall page and enable hypercalls.
373 * 1. Register the guest ID
374 * 2. Enable the hypercall and register the hypercall page
375 */
376 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
377 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
378
379 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
380 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
381 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
382 __builtin_return_address(0));
383 if (hv_hypercall_pg == NULL) {
384 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
385 goto remove_cpuhp_state;
386 }
387
388 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
389 hypercall_msr.enable = 1;
390
391 if (hv_root_partition) {
392 struct page *pg;
393 void *src, *dst;
394
395 /*
396 * For the root partition, the hypervisor will set up its
397 * hypercall page. The hypervisor guarantees it will not show
398 * up in the root's address space. The root can't change the
399 * location of the hypercall page.
400 *
401 * Order is important here. We must enable the hypercall page
402 * so it is populated with code, then copy the code to an
403 * executable page.
404 */
405 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
406
407 pg = vmalloc_to_page(hv_hypercall_pg);
408 dst = kmap(pg);
409 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
410 MEMREMAP_WB);
411 BUG_ON(!(src && dst));
412 memcpy(dst, src, HV_HYP_PAGE_SIZE);
413 memunmap(src);
414 kunmap(pg);
415 } else {
416 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
417 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
418 }
419
420 /*
421 * hyperv_init() is called before LAPIC is initialized: see
422 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
423 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
424 * depends on LAPIC, so hv_stimer_alloc() should be called from
425 * x86_init.timers.setup_percpu_clockev.
426 */
427 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
428 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
429
430 hv_apic_init();
431
432 x86_init.pci.arch_init = hv_pci_init;
433
434 register_syscore_ops(&hv_syscore_ops);
435
436 hyperv_init_cpuhp = cpuhp;
437
438 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
439 hv_get_partition_id();
440
441 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
442
443 #ifdef CONFIG_PCI_MSI
444 /*
445 * If we're running as root, we want to create our own PCI MSI domain.
446 * We can't set this in hv_pci_init because that would be too late.
447 */
448 if (hv_root_partition)
449 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
450 #endif
451
452 /* Query the VMs extended capability once, so that it can be cached. */
453 hv_query_ext_cap(0);
454 return;
455
456 remove_cpuhp_state:
457 cpuhp_remove_state(cpuhp);
458 free_vp_assist_page:
459 kfree(hv_vp_assist_page);
460 hv_vp_assist_page = NULL;
461 common_free:
462 hv_common_free();
463 }
464
465 /*
466 * This routine is called before kexec/kdump, it does the required cleanup.
467 */
hyperv_cleanup(void)468 void hyperv_cleanup(void)
469 {
470 union hv_x64_msr_hypercall_contents hypercall_msr;
471
472 /* Reset our OS id */
473 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
474
475 /*
476 * Reset hypercall page reference before reset the page,
477 * let hypercall operations fail safely rather than
478 * panic the kernel for using invalid hypercall page
479 */
480 hv_hypercall_pg = NULL;
481
482 /* Reset the hypercall page */
483 hypercall_msr.as_uint64 = 0;
484 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
485
486 /* Reset the TSC page */
487 hypercall_msr.as_uint64 = 0;
488 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
489 }
490
hyperv_report_panic(struct pt_regs * regs,long err,bool in_die)491 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
492 {
493 static bool panic_reported;
494 u64 guest_id;
495
496 if (in_die && !panic_on_oops)
497 return;
498
499 /*
500 * We prefer to report panic on 'die' chain as we have proper
501 * registers to report, but if we miss it (e.g. on BUG()) we need
502 * to report it on 'panic'.
503 */
504 if (panic_reported)
505 return;
506 panic_reported = true;
507
508 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
509
510 wrmsrl(HV_X64_MSR_CRASH_P0, err);
511 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
512 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
513 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
514 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
515
516 /*
517 * Let Hyper-V know there is crash data available
518 */
519 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
520 }
521 EXPORT_SYMBOL_GPL(hyperv_report_panic);
522
hv_is_hyperv_initialized(void)523 bool hv_is_hyperv_initialized(void)
524 {
525 union hv_x64_msr_hypercall_contents hypercall_msr;
526
527 /*
528 * Ensure that we're really on Hyper-V, and not a KVM or Xen
529 * emulation of Hyper-V
530 */
531 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
532 return false;
533
534 /*
535 * Verify that earlier initialization succeeded by checking
536 * that the hypercall page is setup
537 */
538 hypercall_msr.as_uint64 = 0;
539 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
540
541 return hypercall_msr.enable;
542 }
543 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
544
hv_get_isolation_type(void)545 enum hv_isolation_type hv_get_isolation_type(void)
546 {
547 if (!(ms_hyperv.priv_high & HV_ISOLATION))
548 return HV_ISOLATION_TYPE_NONE;
549 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
550 }
551 EXPORT_SYMBOL_GPL(hv_get_isolation_type);
552
hv_is_isolation_supported(void)553 bool hv_is_isolation_supported(void)
554 {
555 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
556 }
557