xref: /openbmc/linux/arch/x86/hyperv/hv_init.c (revision 8387ce06)
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/sev.h>
17 #include <asm/hypervisor.h>
18 #include <asm/hyperv-tlfs.h>
19 #include <asm/mshyperv.h>
20 #include <asm/idtentry.h>
21 #include <linux/kexec.h>
22 #include <linux/version.h>
23 #include <linux/vmalloc.h>
24 #include <linux/mm.h>
25 #include <linux/hyperv.h>
26 #include <linux/slab.h>
27 #include <linux/kernel.h>
28 #include <linux/cpuhotplug.h>
29 #include <linux/syscore_ops.h>
30 #include <clocksource/hyperv_timer.h>
31 #include <linux/highmem.h>
32 
33 int hyperv_init_cpuhp;
34 u64 hv_current_partition_id = ~0ull;
35 EXPORT_SYMBOL_GPL(hv_current_partition_id);
36 
37 void *hv_hypercall_pg;
38 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
39 
40 union hv_ghcb * __percpu *hv_ghcb_pg;
41 
42 /* Storage to save the hypercall page temporarily for hibernation */
43 static void *hv_hypercall_pg_saved;
44 
45 struct hv_vp_assist_page **hv_vp_assist_page;
46 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
47 
48 static int hyperv_init_ghcb(void)
49 {
50 	u64 ghcb_gpa;
51 	void *ghcb_va;
52 	void **ghcb_base;
53 
54 	if (!hv_isolation_type_snp())
55 		return 0;
56 
57 	if (!hv_ghcb_pg)
58 		return -EINVAL;
59 
60 	/*
61 	 * GHCB page is allocated by paravisor. The address
62 	 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
63 	 * memory boundary and map it here.
64 	 */
65 	rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
66 
67 	/* Mask out vTOM bit. ioremap_cache() maps decrypted */
68 	ghcb_gpa &= ~ms_hyperv.shared_gpa_boundary;
69 	ghcb_va = (void *)ioremap_cache(ghcb_gpa, HV_HYP_PAGE_SIZE);
70 	if (!ghcb_va)
71 		return -ENOMEM;
72 
73 	ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
74 	*ghcb_base = ghcb_va;
75 
76 	return 0;
77 }
78 
79 static int hv_cpu_init(unsigned int cpu)
80 {
81 	union hv_vp_assist_msr_contents msr = { 0 };
82 	struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
83 	int ret;
84 
85 	ret = hv_common_cpu_init(cpu);
86 	if (ret)
87 		return ret;
88 
89 	if (!hv_vp_assist_page)
90 		return 0;
91 
92 	if (hv_root_partition) {
93 		/*
94 		 * For root partition we get the hypervisor provided VP assist
95 		 * page, instead of allocating a new page.
96 		 */
97 		rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
98 		*hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
99 				PAGE_SIZE, MEMREMAP_WB);
100 	} else {
101 		/*
102 		 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
103 		 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
104 		 * out to make sure we always write the EOI MSR in
105 		 * hv_apic_eoi_write() *after* the EOI optimization is disabled
106 		 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
107 		 * case of CPU offlining and the VM will hang.
108 		 */
109 		if (!*hvp)
110 			*hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
111 		if (*hvp)
112 			msr.pfn = vmalloc_to_pfn(*hvp);
113 
114 	}
115 	if (!WARN_ON(!(*hvp))) {
116 		msr.enable = 1;
117 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
118 	}
119 
120 	return hyperv_init_ghcb();
121 }
122 
123 static void (*hv_reenlightenment_cb)(void);
124 
125 static void hv_reenlightenment_notify(struct work_struct *dummy)
126 {
127 	struct hv_tsc_emulation_status emu_status;
128 
129 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
130 
131 	/* Don't issue the callback if TSC accesses are not emulated */
132 	if (hv_reenlightenment_cb && emu_status.inprogress)
133 		hv_reenlightenment_cb();
134 }
135 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
136 
137 void hyperv_stop_tsc_emulation(void)
138 {
139 	u64 freq;
140 	struct hv_tsc_emulation_status emu_status;
141 
142 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
143 	emu_status.inprogress = 0;
144 	wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
145 
146 	rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
147 	tsc_khz = div64_u64(freq, 1000);
148 }
149 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
150 
151 static inline bool hv_reenlightenment_available(void)
152 {
153 	/*
154 	 * Check for required features and privileges to make TSC frequency
155 	 * change notifications work.
156 	 */
157 	return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
158 		ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
159 		ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
160 }
161 
162 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
163 {
164 	ack_APIC_irq();
165 	inc_irq_stat(irq_hv_reenlightenment_count);
166 	schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
167 }
168 
169 void set_hv_tscchange_cb(void (*cb)(void))
170 {
171 	struct hv_reenlightenment_control re_ctrl = {
172 		.vector = HYPERV_REENLIGHTENMENT_VECTOR,
173 		.enabled = 1,
174 	};
175 	struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
176 
177 	if (!hv_reenlightenment_available()) {
178 		pr_warn("Hyper-V: reenlightenment support is unavailable\n");
179 		return;
180 	}
181 
182 	if (!hv_vp_index)
183 		return;
184 
185 	hv_reenlightenment_cb = cb;
186 
187 	/* Make sure callback is registered before we write to MSRs */
188 	wmb();
189 
190 	re_ctrl.target_vp = hv_vp_index[get_cpu()];
191 
192 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
193 	wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
194 
195 	put_cpu();
196 }
197 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
198 
199 void clear_hv_tscchange_cb(void)
200 {
201 	struct hv_reenlightenment_control re_ctrl;
202 
203 	if (!hv_reenlightenment_available())
204 		return;
205 
206 	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
207 	re_ctrl.enabled = 0;
208 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
209 
210 	hv_reenlightenment_cb = NULL;
211 }
212 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
213 
214 static int hv_cpu_die(unsigned int cpu)
215 {
216 	struct hv_reenlightenment_control re_ctrl;
217 	unsigned int new_cpu;
218 	void **ghcb_va;
219 
220 	if (hv_ghcb_pg) {
221 		ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
222 		if (*ghcb_va)
223 			iounmap(*ghcb_va);
224 		*ghcb_va = NULL;
225 	}
226 
227 	hv_common_cpu_die(cpu);
228 
229 	if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
230 		union hv_vp_assist_msr_contents msr = { 0 };
231 		if (hv_root_partition) {
232 			/*
233 			 * For root partition the VP assist page is mapped to
234 			 * hypervisor provided page, and thus we unmap the
235 			 * page here and nullify it, so that in future we have
236 			 * correct page address mapped in hv_cpu_init.
237 			 */
238 			memunmap(hv_vp_assist_page[cpu]);
239 			hv_vp_assist_page[cpu] = NULL;
240 			rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
241 			msr.enable = 0;
242 		}
243 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
244 	}
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 	 * across 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(false);
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 (!hv_result_success(status)) {
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 static u8 __init get_vtl(void)
382 {
383 	u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS;
384 	struct hv_get_vp_registers_input *input;
385 	struct hv_get_vp_registers_output *output;
386 	unsigned long flags;
387 	u64 ret;
388 
389 	local_irq_save(flags);
390 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
391 	output = (struct hv_get_vp_registers_output *)input;
392 
393 	memset(input, 0, struct_size(input, element, 1));
394 	input->header.partitionid = HV_PARTITION_ID_SELF;
395 	input->header.vpindex = HV_VP_INDEX_SELF;
396 	input->header.inputvtl = 0;
397 	input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS;
398 
399 	ret = hv_do_hypercall(control, input, output);
400 	if (hv_result_success(ret)) {
401 		ret = output->as64.low & HV_X64_VTL_MASK;
402 	} else {
403 		pr_err("Failed to get VTL(%lld) and set VTL to zero by default.\n", ret);
404 		ret = 0;
405 	}
406 
407 	local_irq_restore(flags);
408 	return ret;
409 }
410 
411 /*
412  * This function is to be invoked early in the boot sequence after the
413  * hypervisor has been detected.
414  *
415  * 1. Setup the hypercall page.
416  * 2. Register Hyper-V specific clocksource.
417  * 3. Setup Hyper-V specific APIC entry points.
418  */
419 void __init hyperv_init(void)
420 {
421 	u64 guest_id;
422 	union hv_x64_msr_hypercall_contents hypercall_msr;
423 	int cpuhp;
424 
425 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
426 		return;
427 
428 	if (hv_common_init())
429 		return;
430 
431 	hv_vp_assist_page = kcalloc(num_possible_cpus(),
432 				    sizeof(*hv_vp_assist_page), GFP_KERNEL);
433 	if (!hv_vp_assist_page) {
434 		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
435 		goto common_free;
436 	}
437 
438 	if (hv_isolation_type_snp()) {
439 		/* Negotiate GHCB Version. */
440 		if (!hv_ghcb_negotiate_protocol())
441 			hv_ghcb_terminate(SEV_TERM_SET_GEN,
442 					  GHCB_SEV_ES_PROT_UNSUPPORTED);
443 
444 		hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
445 		if (!hv_ghcb_pg)
446 			goto free_vp_assist_page;
447 	}
448 
449 	cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online",
450 				  hv_cpu_init, hv_cpu_die);
451 	if (cpuhp < 0)
452 		goto free_ghcb_page;
453 
454 	/*
455 	 * Setup the hypercall page and enable hypercalls.
456 	 * 1. Register the guest ID
457 	 * 2. Enable the hypercall and register the hypercall page
458 	 */
459 	guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
460 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
461 
462 	/* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
463 	hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
464 
465 	hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
466 			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
467 			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
468 			__builtin_return_address(0));
469 	if (hv_hypercall_pg == NULL)
470 		goto clean_guest_os_id;
471 
472 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
473 	hypercall_msr.enable = 1;
474 
475 	if (hv_root_partition) {
476 		struct page *pg;
477 		void *src;
478 
479 		/*
480 		 * For the root partition, the hypervisor will set up its
481 		 * hypercall page. The hypervisor guarantees it will not show
482 		 * up in the root's address space. The root can't change the
483 		 * location of the hypercall page.
484 		 *
485 		 * Order is important here. We must enable the hypercall page
486 		 * so it is populated with code, then copy the code to an
487 		 * executable page.
488 		 */
489 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
490 
491 		pg = vmalloc_to_page(hv_hypercall_pg);
492 		src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
493 				MEMREMAP_WB);
494 		BUG_ON(!src);
495 		memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
496 		memunmap(src);
497 
498 		hv_remap_tsc_clocksource();
499 	} else {
500 		hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
501 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
502 	}
503 
504 	/*
505 	 * hyperv_init() is called before LAPIC is initialized: see
506 	 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
507 	 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
508 	 * depends on LAPIC, so hv_stimer_alloc() should be called from
509 	 * x86_init.timers.setup_percpu_clockev.
510 	 */
511 	old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
512 	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
513 
514 	hv_apic_init();
515 
516 	x86_init.pci.arch_init = hv_pci_init;
517 
518 	register_syscore_ops(&hv_syscore_ops);
519 
520 	hyperv_init_cpuhp = cpuhp;
521 
522 	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
523 		hv_get_partition_id();
524 
525 	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
526 
527 #ifdef CONFIG_PCI_MSI
528 	/*
529 	 * If we're running as root, we want to create our own PCI MSI domain.
530 	 * We can't set this in hv_pci_init because that would be too late.
531 	 */
532 	if (hv_root_partition)
533 		x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
534 #endif
535 
536 	/* Query the VMs extended capability once, so that it can be cached. */
537 	hv_query_ext_cap(0);
538 
539 	/* Find the VTL */
540 	if (hv_isolation_type_en_snp())
541 		ms_hyperv.vtl = get_vtl();
542 
543 	return;
544 
545 clean_guest_os_id:
546 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
547 	hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
548 	cpuhp_remove_state(cpuhp);
549 free_ghcb_page:
550 	free_percpu(hv_ghcb_pg);
551 free_vp_assist_page:
552 	kfree(hv_vp_assist_page);
553 	hv_vp_assist_page = NULL;
554 common_free:
555 	hv_common_free();
556 }
557 
558 /*
559  * This routine is called before kexec/kdump, it does the required cleanup.
560  */
561 void hyperv_cleanup(void)
562 {
563 	union hv_x64_msr_hypercall_contents hypercall_msr;
564 	union hv_reference_tsc_msr tsc_msr;
565 
566 	/* Reset our OS id */
567 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
568 	hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
569 
570 	/*
571 	 * Reset hypercall page reference before reset the page,
572 	 * let hypercall operations fail safely rather than
573 	 * panic the kernel for using invalid hypercall page
574 	 */
575 	hv_hypercall_pg = NULL;
576 
577 	/* Reset the hypercall page */
578 	hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
579 	hypercall_msr.enable = 0;
580 	hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
581 
582 	/* Reset the TSC page */
583 	tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
584 	tsc_msr.enable = 0;
585 	hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
586 }
587 
588 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
589 {
590 	static bool panic_reported;
591 	u64 guest_id;
592 
593 	if (in_die && !panic_on_oops)
594 		return;
595 
596 	/*
597 	 * We prefer to report panic on 'die' chain as we have proper
598 	 * registers to report, but if we miss it (e.g. on BUG()) we need
599 	 * to report it on 'panic'.
600 	 */
601 	if (panic_reported)
602 		return;
603 	panic_reported = true;
604 
605 	rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
606 
607 	wrmsrl(HV_X64_MSR_CRASH_P0, err);
608 	wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
609 	wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
610 	wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
611 	wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
612 
613 	/*
614 	 * Let Hyper-V know there is crash data available
615 	 */
616 	wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
617 }
618 EXPORT_SYMBOL_GPL(hyperv_report_panic);
619 
620 bool hv_is_hyperv_initialized(void)
621 {
622 	union hv_x64_msr_hypercall_contents hypercall_msr;
623 
624 	/*
625 	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
626 	 * emulation of Hyper-V
627 	 */
628 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
629 		return false;
630 
631 	/*
632 	 * Verify that earlier initialization succeeded by checking
633 	 * that the hypercall page is setup
634 	 */
635 	hypercall_msr.as_uint64 = 0;
636 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
637 
638 	return hypercall_msr.enable;
639 }
640 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
641