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