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