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