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