xref: /linux/arch/x86/hyperv/hv_init.c (revision 25489a4f556414445d342951615178368ee45cde)
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 #define pr_fmt(fmt)  "Hyper-V: " fmt
11 
12 #include <linux/efi.h>
13 #include <linux/types.h>
14 #include <linux/bitfield.h>
15 #include <linux/io.h>
16 #include <asm/apic.h>
17 #include <asm/desc.h>
18 #include <asm/e820/api.h>
19 #include <asm/sev.h>
20 #include <asm/ibt.h>
21 #include <asm/hypervisor.h>
22 #include <hyperv/hvhdk.h>
23 #include <asm/mshyperv.h>
24 #include <asm/msr.h>
25 #include <asm/idtentry.h>
26 #include <asm/set_memory.h>
27 #include <linux/kexec.h>
28 #include <linux/version.h>
29 #include <linux/vmalloc.h>
30 #include <linux/mm.h>
31 #include <linux/slab.h>
32 #include <linux/kernel.h>
33 #include <linux/cpuhotplug.h>
34 #include <linux/syscore_ops.h>
35 #include <clocksource/hyperv_timer.h>
36 #include <linux/highmem.h>
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 (!ms_hyperv.paravisor_present || !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 	rdmsrq(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 		rdmsrq(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 && !ms_hyperv.paravisor_present && hv_isolation_type_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 		wrmsrq(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 	rdmsrq(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 	rdmsrq(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
158 	emu_status.inprogress = 0;
159 	wrmsrq(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
160 
161 	rdmsrq(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 	apic_eoi();
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("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 	wrmsrq(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
208 	wrmsrq(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 	rdmsrq(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
222 	re_ctrl.enabled = 0;
223 	wrmsrq(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 			rdmsrq(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
256 			msr.enable = 0;
257 		}
258 		wrmsrq(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
259 	}
260 
261 	if (hv_reenlightenment_cb == NULL)
262 		return 0;
263 
264 	rdmsrq(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 		wrmsrq(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 	bool gen2vm = efi_enabled(EFI_BOOT);
287 
288 	/*
289 	 * A Generation-2 VM doesn't support legacy PCI/PCIe, so both
290 	 * raw_pci_ops and raw_pci_ext_ops are NULL, and pci_subsys_init() ->
291 	 * pcibios_init() doesn't call pcibios_resource_survey() ->
292 	 * e820__reserve_resources_late(); as a result, any emulated persistent
293 	 * memory of E820_TYPE_PRAM (12) via the kernel parameter
294 	 * memmap=nn[KMG]!ss is not added into iomem_resource and hence can't be
295 	 * detected by register_e820_pmem(). Fix this by directly calling
296 	 * e820__reserve_resources_late() here: e820__reserve_resources_late()
297 	 * depends on e820__reserve_resources(), which has been called earlier
298 	 * from setup_arch(). Note: e820__reserve_resources_late() also adds
299 	 * any memory of E820_TYPE_PMEM (7) into iomem_resource, and
300 	 * acpi_nfit_register_region() -> acpi_nfit_insert_resource() ->
301 	 * region_intersects() returns REGION_INTERSECTS, so the memory of
302 	 * E820_TYPE_PMEM won't get added twice.
303 	 *
304 	 * We return 0 here so that pci_arch_init() won't print the warning:
305 	 * "PCI: Fatal: No config space access function found"
306 	 */
307 	if (gen2vm) {
308 		e820__reserve_resources_late();
309 		return 0;
310 	}
311 
312 	/* For Generation-1 VM, we'll proceed in pci_arch_init().  */
313 	return 1;
314 }
315 
316 static int hv_suspend(void)
317 {
318 	union hv_x64_msr_hypercall_contents hypercall_msr;
319 	int ret;
320 
321 	if (hv_root_partition())
322 		return -EPERM;
323 
324 	/*
325 	 * Reset the hypercall page as it is going to be invalidated
326 	 * across hibernation. Setting hv_hypercall_pg to NULL ensures
327 	 * that any subsequent hypercall operation fails safely instead of
328 	 * crashing due to an access of an invalid page. The hypercall page
329 	 * pointer is restored on resume.
330 	 */
331 	hv_hypercall_pg_saved = hv_hypercall_pg;
332 	hv_hypercall_pg = NULL;
333 
334 	/* Disable the hypercall page in the hypervisor */
335 	rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
336 	hypercall_msr.enable = 0;
337 	wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
338 
339 	ret = hv_cpu_die(0);
340 	return ret;
341 }
342 
343 static void hv_resume(void)
344 {
345 	union hv_x64_msr_hypercall_contents hypercall_msr;
346 	int ret;
347 
348 	ret = hv_cpu_init(0);
349 	WARN_ON(ret);
350 
351 	/* Re-enable the hypercall page */
352 	rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
353 	hypercall_msr.enable = 1;
354 	hypercall_msr.guest_physical_address =
355 		vmalloc_to_pfn(hv_hypercall_pg_saved);
356 	wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
357 
358 	hv_hypercall_pg = hv_hypercall_pg_saved;
359 	hv_hypercall_pg_saved = NULL;
360 
361 	/*
362 	 * Reenlightenment notifications are disabled by hv_cpu_die(0),
363 	 * reenable them here if hv_reenlightenment_cb was previously set.
364 	 */
365 	if (hv_reenlightenment_cb)
366 		set_hv_tscchange_cb(hv_reenlightenment_cb);
367 }
368 
369 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
370 static struct syscore_ops hv_syscore_ops = {
371 	.suspend	= hv_suspend,
372 	.resume		= hv_resume,
373 };
374 
375 static void (* __initdata old_setup_percpu_clockev)(void);
376 
377 static void __init hv_stimer_setup_percpu_clockev(void)
378 {
379 	/*
380 	 * Ignore any errors in setting up stimer clockevents
381 	 * as we can run with the LAPIC timer as a fallback.
382 	 */
383 	(void)hv_stimer_alloc(false);
384 
385 	/*
386 	 * Still register the LAPIC timer, because the direct-mode STIMER is
387 	 * not supported by old versions of Hyper-V. This also allows users
388 	 * to switch to LAPIC timer via /sys, if they want to.
389 	 */
390 	if (old_setup_percpu_clockev)
391 		old_setup_percpu_clockev();
392 }
393 
394 /*
395  * This function is to be invoked early in the boot sequence after the
396  * hypervisor has been detected.
397  *
398  * 1. Setup the hypercall page.
399  * 2. Register Hyper-V specific clocksource.
400  * 3. Setup Hyper-V specific APIC entry points.
401  */
402 void __init hyperv_init(void)
403 {
404 	u64 guest_id;
405 	union hv_x64_msr_hypercall_contents hypercall_msr;
406 	int cpuhp;
407 
408 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
409 		return;
410 
411 	if (hv_common_init())
412 		return;
413 
414 	/*
415 	 * The VP assist page is useless to a TDX guest: the only use we
416 	 * would have for it is lazy EOI, which can not be used with TDX.
417 	 */
418 	if (hv_isolation_type_tdx())
419 		hv_vp_assist_page = NULL;
420 	else
421 		hv_vp_assist_page = kcalloc(nr_cpu_ids,
422 					    sizeof(*hv_vp_assist_page),
423 					    GFP_KERNEL);
424 	if (!hv_vp_assist_page) {
425 		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
426 
427 		if (!hv_isolation_type_tdx())
428 			goto common_free;
429 	}
430 
431 	if (ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
432 		/* Negotiate GHCB Version. */
433 		if (!hv_ghcb_negotiate_protocol())
434 			hv_ghcb_terminate(SEV_TERM_SET_GEN,
435 					  GHCB_SEV_ES_PROT_UNSUPPORTED);
436 
437 		hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
438 		if (!hv_ghcb_pg)
439 			goto free_vp_assist_page;
440 	}
441 
442 	cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online",
443 				  hv_cpu_init, hv_cpu_die);
444 	if (cpuhp < 0)
445 		goto free_ghcb_page;
446 
447 	/*
448 	 * Setup the hypercall page and enable hypercalls.
449 	 * 1. Register the guest ID
450 	 * 2. Enable the hypercall and register the hypercall page
451 	 *
452 	 * A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg:
453 	 * when the hypercall input is a page, such a VM must pass a decrypted
454 	 * page to Hyper-V, e.g. hv_post_message() uses the per-CPU page
455 	 * hyperv_pcpu_input_arg, which is decrypted if no paravisor is present.
456 	 *
457 	 * A TDX VM with the paravisor uses hv_hypercall_pg for most hypercalls,
458 	 * which are handled by the paravisor and the VM must use an encrypted
459 	 * input page: in such a VM, the hyperv_pcpu_input_arg is encrypted and
460 	 * used in the hypercalls, e.g. see hv_mark_gpa_visibility() and
461 	 * hv_arch_irq_unmask(). Such a VM uses TDX GHCI for two hypercalls:
462 	 * 1. HVCALL_SIGNAL_EVENT: see vmbus_set_event() and _hv_do_fast_hypercall8().
463 	 * 2. HVCALL_POST_MESSAGE: the input page must be a decrypted page, i.e.
464 	 * hv_post_message() in such a VM can't use the encrypted hyperv_pcpu_input_arg;
465 	 * instead, hv_post_message() uses the post_msg_page, which is decrypted
466 	 * in such a VM and is only used in such a VM.
467 	 */
468 	guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
469 	wrmsrq(HV_X64_MSR_GUEST_OS_ID, guest_id);
470 
471 	/* With the paravisor, the VM must also write the ID via GHCB/GHCI */
472 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
473 
474 	/* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg */
475 	if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
476 		goto skip_hypercall_pg_init;
477 
478 	hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
479 			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
480 			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
481 			__builtin_return_address(0));
482 	if (hv_hypercall_pg == NULL)
483 		goto clean_guest_os_id;
484 
485 	rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
486 	hypercall_msr.enable = 1;
487 
488 	if (hv_root_partition()) {
489 		struct page *pg;
490 		void *src;
491 
492 		/*
493 		 * For the root partition, the hypervisor will set up its
494 		 * hypercall page. The hypervisor guarantees it will not show
495 		 * up in the root's address space. The root can't change the
496 		 * location of the hypercall page.
497 		 *
498 		 * Order is important here. We must enable the hypercall page
499 		 * so it is populated with code, then copy the code to an
500 		 * executable page.
501 		 */
502 		wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
503 
504 		pg = vmalloc_to_page(hv_hypercall_pg);
505 		src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
506 				MEMREMAP_WB);
507 		BUG_ON(!src);
508 		memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
509 		memunmap(src);
510 
511 		hv_remap_tsc_clocksource();
512 	} else {
513 		hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
514 		wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
515 	}
516 
517 skip_hypercall_pg_init:
518 	/*
519 	 * Some versions of Hyper-V that provide IBT in guest VMs have a bug
520 	 * in that there's no ENDBR64 instruction at the entry to the
521 	 * hypercall page. Because hypercalls are invoked via an indirect call
522 	 * to the hypercall page, all hypercall attempts fail when IBT is
523 	 * enabled, and Linux panics. For such buggy versions, disable IBT.
524 	 *
525 	 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
526 	 * page, so if future Linux kernel versions enable IBT for 32-bit
527 	 * builds, additional hypercall page hackery will be required here
528 	 * to provide an ENDBR32.
529 	 */
530 #ifdef CONFIG_X86_KERNEL_IBT
531 	if (cpu_feature_enabled(X86_FEATURE_IBT) &&
532 	    *(u32 *)hv_hypercall_pg != gen_endbr()) {
533 		setup_clear_cpu_cap(X86_FEATURE_IBT);
534 		pr_warn("Disabling IBT because of Hyper-V bug\n");
535 	}
536 #endif
537 
538 	/*
539 	 * hyperv_init() is called before LAPIC is initialized: see
540 	 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
541 	 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
542 	 * depends on LAPIC, so hv_stimer_alloc() should be called from
543 	 * x86_init.timers.setup_percpu_clockev.
544 	 */
545 	old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
546 	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
547 
548 	hv_apic_init();
549 
550 	x86_init.pci.arch_init = hv_pci_init;
551 
552 	register_syscore_ops(&hv_syscore_ops);
553 
554 	if (ms_hyperv.priv_high & HV_ACCESS_PARTITION_ID)
555 		hv_get_partition_id();
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 	ms_hyperv.vtl = get_vtl();
571 
572 	if (ms_hyperv.vtl > 0) /* non default VTL */
573 		hv_vtl_early_init();
574 
575 	return;
576 
577 clean_guest_os_id:
578 	wrmsrq(HV_X64_MSR_GUEST_OS_ID, 0);
579 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
580 	cpuhp_remove_state(CPUHP_AP_HYPERV_ONLINE);
581 free_ghcb_page:
582 	free_percpu(hv_ghcb_pg);
583 free_vp_assist_page:
584 	kfree(hv_vp_assist_page);
585 	hv_vp_assist_page = NULL;
586 common_free:
587 	hv_common_free();
588 }
589 
590 /*
591  * This routine is called before kexec/kdump, it does the required cleanup.
592  */
593 void hyperv_cleanup(void)
594 {
595 	union hv_x64_msr_hypercall_contents hypercall_msr;
596 	union hv_reference_tsc_msr tsc_msr;
597 
598 	/* Reset our OS id */
599 	wrmsrq(HV_X64_MSR_GUEST_OS_ID, 0);
600 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
601 
602 	/*
603 	 * Reset hypercall page reference before reset the page,
604 	 * let hypercall operations fail safely rather than
605 	 * panic the kernel for using invalid hypercall page
606 	 */
607 	hv_hypercall_pg = NULL;
608 
609 	/* Reset the hypercall page */
610 	hypercall_msr.as_uint64 = hv_get_msr(HV_X64_MSR_HYPERCALL);
611 	hypercall_msr.enable = 0;
612 	hv_set_msr(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
613 
614 	/* Reset the TSC page */
615 	tsc_msr.as_uint64 = hv_get_msr(HV_X64_MSR_REFERENCE_TSC);
616 	tsc_msr.enable = 0;
617 	hv_set_msr(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
618 }
619 
620 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
621 {
622 	static bool panic_reported;
623 	u64 guest_id;
624 
625 	if (in_die && !panic_on_oops)
626 		return;
627 
628 	/*
629 	 * We prefer to report panic on 'die' chain as we have proper
630 	 * registers to report, but if we miss it (e.g. on BUG()) we need
631 	 * to report it on 'panic'.
632 	 */
633 	if (panic_reported)
634 		return;
635 	panic_reported = true;
636 
637 	rdmsrq(HV_X64_MSR_GUEST_OS_ID, guest_id);
638 
639 	wrmsrq(HV_X64_MSR_CRASH_P0, err);
640 	wrmsrq(HV_X64_MSR_CRASH_P1, guest_id);
641 	wrmsrq(HV_X64_MSR_CRASH_P2, regs->ip);
642 	wrmsrq(HV_X64_MSR_CRASH_P3, regs->ax);
643 	wrmsrq(HV_X64_MSR_CRASH_P4, regs->sp);
644 
645 	/*
646 	 * Let Hyper-V know there is crash data available
647 	 */
648 	wrmsrq(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
649 }
650 EXPORT_SYMBOL_GPL(hyperv_report_panic);
651 
652 bool hv_is_hyperv_initialized(void)
653 {
654 	union hv_x64_msr_hypercall_contents hypercall_msr;
655 
656 	/*
657 	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
658 	 * emulation of Hyper-V
659 	 */
660 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
661 		return false;
662 
663 	/* A TDX VM with no paravisor uses TDX GHCI call rather than hv_hypercall_pg */
664 	if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
665 		return true;
666 	/*
667 	 * Verify that earlier initialization succeeded by checking
668 	 * that the hypercall page is setup
669 	 */
670 	hypercall_msr.as_uint64 = 0;
671 	rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
672 
673 	return hypercall_msr.enable;
674 }
675 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
676 
677 int hv_apicid_to_vp_index(u32 apic_id)
678 {
679 	u64 control;
680 	u64 status;
681 	unsigned long irq_flags;
682 	struct hv_get_vp_from_apic_id_in *input;
683 	u32 *output, ret;
684 
685 	local_irq_save(irq_flags);
686 
687 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
688 	memset(input, 0, sizeof(*input));
689 	input->partition_id = HV_PARTITION_ID_SELF;
690 	input->apic_ids[0] = apic_id;
691 
692 	output = *this_cpu_ptr(hyperv_pcpu_output_arg);
693 
694 	control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_INDEX_FROM_APIC_ID;
695 	status = hv_do_hypercall(control, input, output);
696 	ret = output[0];
697 
698 	local_irq_restore(irq_flags);
699 
700 	if (!hv_result_success(status)) {
701 		pr_err("failed to get vp index from apic id %d, status %#llx\n",
702 		       apic_id, status);
703 		return -EINVAL;
704 	}
705 
706 	return ret;
707 }
708 EXPORT_SYMBOL_GPL(hv_apicid_to_vp_index);
709