xref: /openbmc/linux/arch/x86/hyperv/hv_init.c (revision 479965a2)
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 static u8 __init get_vtl(void)
398 {
399 	u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS;
400 	struct hv_get_vp_registers_input *input;
401 	struct hv_get_vp_registers_output *output;
402 	unsigned long flags;
403 	u64 ret;
404 
405 	local_irq_save(flags);
406 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
407 	output = (struct hv_get_vp_registers_output *)input;
408 
409 	memset(input, 0, struct_size(input, element, 1));
410 	input->header.partitionid = HV_PARTITION_ID_SELF;
411 	input->header.vpindex = HV_VP_INDEX_SELF;
412 	input->header.inputvtl = 0;
413 	input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS;
414 
415 	ret = hv_do_hypercall(control, input, output);
416 	if (hv_result_success(ret)) {
417 		ret = output->as64.low & HV_X64_VTL_MASK;
418 	} else {
419 		pr_err("Failed to get VTL(%lld) and set VTL to zero by default.\n", ret);
420 		ret = 0;
421 	}
422 
423 	local_irq_restore(flags);
424 	return ret;
425 }
426 
427 /*
428  * This function is to be invoked early in the boot sequence after the
429  * hypervisor has been detected.
430  *
431  * 1. Setup the hypercall page.
432  * 2. Register Hyper-V specific clocksource.
433  * 3. Setup Hyper-V specific APIC entry points.
434  */
435 void __init hyperv_init(void)
436 {
437 	u64 guest_id;
438 	union hv_x64_msr_hypercall_contents hypercall_msr;
439 	int cpuhp;
440 
441 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
442 		return;
443 
444 	if (hv_common_init())
445 		return;
446 
447 	/*
448 	 * The VP assist page is useless to a TDX guest: the only use we
449 	 * would have for it is lazy EOI, which can not be used with TDX.
450 	 */
451 	if (hv_isolation_type_tdx())
452 		hv_vp_assist_page = NULL;
453 	else
454 		hv_vp_assist_page = kcalloc(num_possible_cpus(),
455 					    sizeof(*hv_vp_assist_page),
456 					    GFP_KERNEL);
457 	if (!hv_vp_assist_page) {
458 		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
459 
460 		if (!hv_isolation_type_tdx())
461 			goto common_free;
462 	}
463 
464 	if (ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
465 		/* Negotiate GHCB Version. */
466 		if (!hv_ghcb_negotiate_protocol())
467 			hv_ghcb_terminate(SEV_TERM_SET_GEN,
468 					  GHCB_SEV_ES_PROT_UNSUPPORTED);
469 
470 		hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
471 		if (!hv_ghcb_pg)
472 			goto free_vp_assist_page;
473 	}
474 
475 	cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online",
476 				  hv_cpu_init, hv_cpu_die);
477 	if (cpuhp < 0)
478 		goto free_ghcb_page;
479 
480 	/*
481 	 * Setup the hypercall page and enable hypercalls.
482 	 * 1. Register the guest ID
483 	 * 2. Enable the hypercall and register the hypercall page
484 	 *
485 	 * A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg:
486 	 * when the hypercall input is a page, such a VM must pass a decrypted
487 	 * page to Hyper-V, e.g. hv_post_message() uses the per-CPU page
488 	 * hyperv_pcpu_input_arg, which is decrypted if no paravisor is present.
489 	 *
490 	 * A TDX VM with the paravisor uses hv_hypercall_pg for most hypercalls,
491 	 * which are handled by the paravisor and the VM must use an encrypted
492 	 * input page: in such a VM, the hyperv_pcpu_input_arg is encrypted and
493 	 * used in the hypercalls, e.g. see hv_mark_gpa_visibility() and
494 	 * hv_arch_irq_unmask(). Such a VM uses TDX GHCI for two hypercalls:
495 	 * 1. HVCALL_SIGNAL_EVENT: see vmbus_set_event() and _hv_do_fast_hypercall8().
496 	 * 2. HVCALL_POST_MESSAGE: the input page must be a decrypted page, i.e.
497 	 * hv_post_message() in such a VM can't use the encrypted hyperv_pcpu_input_arg;
498 	 * instead, hv_post_message() uses the post_msg_page, which is decrypted
499 	 * in such a VM and is only used in such a VM.
500 	 */
501 	guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
502 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
503 
504 	/* With the paravisor, the VM must also write the ID via GHCB/GHCI */
505 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
506 
507 	/* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg */
508 	if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
509 		goto skip_hypercall_pg_init;
510 
511 	hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
512 			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
513 			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
514 			__builtin_return_address(0));
515 	if (hv_hypercall_pg == NULL)
516 		goto clean_guest_os_id;
517 
518 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
519 	hypercall_msr.enable = 1;
520 
521 	if (hv_root_partition) {
522 		struct page *pg;
523 		void *src;
524 
525 		/*
526 		 * For the root partition, the hypervisor will set up its
527 		 * hypercall page. The hypervisor guarantees it will not show
528 		 * up in the root's address space. The root can't change the
529 		 * location of the hypercall page.
530 		 *
531 		 * Order is important here. We must enable the hypercall page
532 		 * so it is populated with code, then copy the code to an
533 		 * executable page.
534 		 */
535 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
536 
537 		pg = vmalloc_to_page(hv_hypercall_pg);
538 		src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
539 				MEMREMAP_WB);
540 		BUG_ON(!src);
541 		memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
542 		memunmap(src);
543 
544 		hv_remap_tsc_clocksource();
545 	} else {
546 		hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
547 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
548 	}
549 
550 skip_hypercall_pg_init:
551 	/*
552 	 * Some versions of Hyper-V that provide IBT in guest VMs have a bug
553 	 * in that there's no ENDBR64 instruction at the entry to the
554 	 * hypercall page. Because hypercalls are invoked via an indirect call
555 	 * to the hypercall page, all hypercall attempts fail when IBT is
556 	 * enabled, and Linux panics. For such buggy versions, disable IBT.
557 	 *
558 	 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
559 	 * page, so if future Linux kernel versions enable IBT for 32-bit
560 	 * builds, additional hypercall page hackery will be required here
561 	 * to provide an ENDBR32.
562 	 */
563 #ifdef CONFIG_X86_KERNEL_IBT
564 	if (cpu_feature_enabled(X86_FEATURE_IBT) &&
565 	    *(u32 *)hv_hypercall_pg != gen_endbr()) {
566 		setup_clear_cpu_cap(X86_FEATURE_IBT);
567 		pr_warn("Hyper-V: Disabling IBT because of Hyper-V bug\n");
568 	}
569 #endif
570 
571 	/*
572 	 * hyperv_init() is called before LAPIC is initialized: see
573 	 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
574 	 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
575 	 * depends on LAPIC, so hv_stimer_alloc() should be called from
576 	 * x86_init.timers.setup_percpu_clockev.
577 	 */
578 	old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
579 	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
580 
581 	hv_apic_init();
582 
583 	x86_init.pci.arch_init = hv_pci_init;
584 
585 	register_syscore_ops(&hv_syscore_ops);
586 
587 	hyperv_init_cpuhp = cpuhp;
588 
589 	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
590 		hv_get_partition_id();
591 
592 	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
593 
594 #ifdef CONFIG_PCI_MSI
595 	/*
596 	 * If we're running as root, we want to create our own PCI MSI domain.
597 	 * We can't set this in hv_pci_init because that would be too late.
598 	 */
599 	if (hv_root_partition)
600 		x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
601 #endif
602 
603 	/* Query the VMs extended capability once, so that it can be cached. */
604 	hv_query_ext_cap(0);
605 
606 	/* Find the VTL */
607 	if (!ms_hyperv.paravisor_present && hv_isolation_type_snp())
608 		ms_hyperv.vtl = get_vtl();
609 
610 	return;
611 
612 clean_guest_os_id:
613 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
614 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
615 	cpuhp_remove_state(cpuhp);
616 free_ghcb_page:
617 	free_percpu(hv_ghcb_pg);
618 free_vp_assist_page:
619 	kfree(hv_vp_assist_page);
620 	hv_vp_assist_page = NULL;
621 common_free:
622 	hv_common_free();
623 }
624 
625 /*
626  * This routine is called before kexec/kdump, it does the required cleanup.
627  */
628 void hyperv_cleanup(void)
629 {
630 	union hv_x64_msr_hypercall_contents hypercall_msr;
631 	union hv_reference_tsc_msr tsc_msr;
632 
633 	/* Reset our OS id */
634 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
635 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
636 
637 	/*
638 	 * Reset hypercall page reference before reset the page,
639 	 * let hypercall operations fail safely rather than
640 	 * panic the kernel for using invalid hypercall page
641 	 */
642 	hv_hypercall_pg = NULL;
643 
644 	/* Reset the hypercall page */
645 	hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
646 	hypercall_msr.enable = 0;
647 	hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
648 
649 	/* Reset the TSC page */
650 	tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
651 	tsc_msr.enable = 0;
652 	hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
653 }
654 
655 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
656 {
657 	static bool panic_reported;
658 	u64 guest_id;
659 
660 	if (in_die && !panic_on_oops)
661 		return;
662 
663 	/*
664 	 * We prefer to report panic on 'die' chain as we have proper
665 	 * registers to report, but if we miss it (e.g. on BUG()) we need
666 	 * to report it on 'panic'.
667 	 */
668 	if (panic_reported)
669 		return;
670 	panic_reported = true;
671 
672 	rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
673 
674 	wrmsrl(HV_X64_MSR_CRASH_P0, err);
675 	wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
676 	wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
677 	wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
678 	wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
679 
680 	/*
681 	 * Let Hyper-V know there is crash data available
682 	 */
683 	wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
684 }
685 EXPORT_SYMBOL_GPL(hyperv_report_panic);
686 
687 bool hv_is_hyperv_initialized(void)
688 {
689 	union hv_x64_msr_hypercall_contents hypercall_msr;
690 
691 	/*
692 	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
693 	 * emulation of Hyper-V
694 	 */
695 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
696 		return false;
697 
698 	/* A TDX VM with no paravisor uses TDX GHCI call rather than hv_hypercall_pg */
699 	if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
700 		return true;
701 	/*
702 	 * Verify that earlier initialization succeeded by checking
703 	 * that the hypercall page is setup
704 	 */
705 	hypercall_msr.as_uint64 = 0;
706 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
707 
708 	return hypercall_msr.enable;
709 }
710 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
711