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