hv_init.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 <[email protected]>
  8. */
  9. #include <linux/efi.h>
  10. #include <linux/types.h>
  11. #include <linux/bitfield.h>
  12. #include <linux/io.h>
  13. #include <asm/apic.h>
  14. #include <asm/desc.h>
  15. #include <asm/sev.h>
  16. #include <asm/ibt.h>
  17. #include <asm/hypervisor.h>
  18. #include <asm/hyperv-tlfs.h>
  19. #include <asm/mshyperv.h>
  20. #include <asm/idtentry.h>
  21. #include <linux/kexec.h>
  22. #include <linux/version.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/mm.h>
  25. #include <linux/hyperv.h>
  26. #include <linux/slab.h>
  27. #include <linux/kernel.h>
  28. #include <linux/cpuhotplug.h>
  29. #include <linux/syscore_ops.h>
  30. #include <clocksource/hyperv_timer.h>
  31. #include <linux/highmem.h>
  32. #include <linux/swiotlb.h>
  33. int hyperv_init_cpuhp;
  34. u64 hv_current_partition_id = ~0ull;
  35. EXPORT_SYMBOL_GPL(hv_current_partition_id);
  36. void *hv_hypercall_pg;
  37. EXPORT_SYMBOL_GPL(hv_hypercall_pg);
  38. union hv_ghcb * __percpu *hv_ghcb_pg;
  39. /* Storage to save the hypercall page temporarily for hibernation */
  40. static void *hv_hypercall_pg_saved;
  41. struct hv_vp_assist_page **hv_vp_assist_page;
  42. EXPORT_SYMBOL_GPL(hv_vp_assist_page);
  43. static int hyperv_init_ghcb(void)
  44. {
  45. u64 ghcb_gpa;
  46. void *ghcb_va;
  47. void **ghcb_base;
  48. if (!hv_isolation_type_snp())
  49. return 0;
  50. if (!hv_ghcb_pg)
  51. return -EINVAL;
  52. /*
  53. * GHCB page is allocated by paravisor. The address
  54. * returned by MSR_AMD64_SEV_ES_GHCB is above shared
  55. * memory boundary and map it here.
  56. */
  57. rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
  58. ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB);
  59. if (!ghcb_va)
  60. return -ENOMEM;
  61. ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
  62. *ghcb_base = ghcb_va;
  63. return 0;
  64. }
  65. static int hv_cpu_init(unsigned int cpu)
  66. {
  67. union hv_vp_assist_msr_contents msr = { 0 };
  68. struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
  69. int ret;
  70. ret = hv_common_cpu_init(cpu);
  71. if (ret)
  72. return ret;
  73. if (!hv_vp_assist_page)
  74. return 0;
  75. if (hv_root_partition) {
  76. /*
  77. * For root partition we get the hypervisor provided VP assist
  78. * page, instead of allocating a new page.
  79. */
  80. rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
  81. *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
  82. PAGE_SIZE, MEMREMAP_WB);
  83. } else {
  84. /*
  85. * The VP assist page is an "overlay" page (see Hyper-V TLFS's
  86. * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
  87. * out to make sure we always write the EOI MSR in
  88. * hv_apic_eoi_write() *after* the EOI optimization is disabled
  89. * in hv_cpu_die(), otherwise a CPU may not be stopped in the
  90. * case of CPU offlining and the VM will hang.
  91. */
  92. if (!*hvp)
  93. *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
  94. if (*hvp)
  95. msr.pfn = vmalloc_to_pfn(*hvp);
  96. }
  97. if (!WARN_ON(!(*hvp))) {
  98. msr.enable = 1;
  99. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
  100. }
  101. return hyperv_init_ghcb();
  102. }
  103. static void (*hv_reenlightenment_cb)(void);
  104. static void hv_reenlightenment_notify(struct work_struct *dummy)
  105. {
  106. struct hv_tsc_emulation_status emu_status;
  107. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  108. /* Don't issue the callback if TSC accesses are not emulated */
  109. if (hv_reenlightenment_cb && emu_status.inprogress)
  110. hv_reenlightenment_cb();
  111. }
  112. static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
  113. void hyperv_stop_tsc_emulation(void)
  114. {
  115. u64 freq;
  116. struct hv_tsc_emulation_status emu_status;
  117. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  118. emu_status.inprogress = 0;
  119. wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  120. rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
  121. tsc_khz = div64_u64(freq, 1000);
  122. }
  123. EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
  124. static inline bool hv_reenlightenment_available(void)
  125. {
  126. /*
  127. * Check for required features and privileges to make TSC frequency
  128. * change notifications work.
  129. */
  130. return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
  131. ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
  132. ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
  133. }
  134. DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
  135. {
  136. ack_APIC_irq();
  137. inc_irq_stat(irq_hv_reenlightenment_count);
  138. schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
  139. }
  140. void set_hv_tscchange_cb(void (*cb)(void))
  141. {
  142. struct hv_reenlightenment_control re_ctrl = {
  143. .vector = HYPERV_REENLIGHTENMENT_VECTOR,
  144. .enabled = 1,
  145. };
  146. struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
  147. if (!hv_reenlightenment_available()) {
  148. pr_warn("Hyper-V: reenlightenment support is unavailable\n");
  149. return;
  150. }
  151. if (!hv_vp_index)
  152. return;
  153. hv_reenlightenment_cb = cb;
  154. /* Make sure callback is registered before we write to MSRs */
  155. wmb();
  156. re_ctrl.target_vp = hv_vp_index[get_cpu()];
  157. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  158. wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
  159. put_cpu();
  160. }
  161. EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
  162. void clear_hv_tscchange_cb(void)
  163. {
  164. struct hv_reenlightenment_control re_ctrl;
  165. if (!hv_reenlightenment_available())
  166. return;
  167. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  168. re_ctrl.enabled = 0;
  169. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  170. hv_reenlightenment_cb = NULL;
  171. }
  172. EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
  173. static int hv_cpu_die(unsigned int cpu)
  174. {
  175. struct hv_reenlightenment_control re_ctrl;
  176. unsigned int new_cpu;
  177. void **ghcb_va;
  178. if (hv_ghcb_pg) {
  179. ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
  180. if (*ghcb_va)
  181. memunmap(*ghcb_va);
  182. *ghcb_va = NULL;
  183. }
  184. hv_common_cpu_die(cpu);
  185. if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
  186. union hv_vp_assist_msr_contents msr = { 0 };
  187. if (hv_root_partition) {
  188. /*
  189. * For root partition the VP assist page is mapped to
  190. * hypervisor provided page, and thus we unmap the
  191. * page here and nullify it, so that in future we have
  192. * correct page address mapped in hv_cpu_init.
  193. */
  194. memunmap(hv_vp_assist_page[cpu]);
  195. hv_vp_assist_page[cpu] = NULL;
  196. rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
  197. msr.enable = 0;
  198. }
  199. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
  200. }
  201. if (hv_reenlightenment_cb == NULL)
  202. return 0;
  203. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  204. if (re_ctrl.target_vp == hv_vp_index[cpu]) {
  205. /*
  206. * Reassign reenlightenment notifications to some other online
  207. * CPU or just disable the feature if there are no online CPUs
  208. * left (happens on hibernation).
  209. */
  210. new_cpu = cpumask_any_but(cpu_online_mask, cpu);
  211. if (new_cpu < nr_cpu_ids)
  212. re_ctrl.target_vp = hv_vp_index[new_cpu];
  213. else
  214. re_ctrl.enabled = 0;
  215. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  216. }
  217. return 0;
  218. }
  219. static int __init hv_pci_init(void)
  220. {
  221. int gen2vm = efi_enabled(EFI_BOOT);
  222. /*
  223. * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
  224. * The purpose is to suppress the harmless warning:
  225. * "PCI: Fatal: No config space access function found"
  226. */
  227. if (gen2vm)
  228. return 0;
  229. /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
  230. return 1;
  231. }
  232. static int hv_suspend(void)
  233. {
  234. union hv_x64_msr_hypercall_contents hypercall_msr;
  235. int ret;
  236. if (hv_root_partition)
  237. return -EPERM;
  238. /*
  239. * Reset the hypercall page as it is going to be invalidated
  240. * across hibernation. Setting hv_hypercall_pg to NULL ensures
  241. * that any subsequent hypercall operation fails safely instead of
  242. * crashing due to an access of an invalid page. The hypercall page
  243. * pointer is restored on resume.
  244. */
  245. hv_hypercall_pg_saved = hv_hypercall_pg;
  246. hv_hypercall_pg = NULL;
  247. /* Disable the hypercall page in the hypervisor */
  248. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  249. hypercall_msr.enable = 0;
  250. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  251. ret = hv_cpu_die(0);
  252. return ret;
  253. }
  254. static void hv_resume(void)
  255. {
  256. union hv_x64_msr_hypercall_contents hypercall_msr;
  257. int ret;
  258. ret = hv_cpu_init(0);
  259. WARN_ON(ret);
  260. /* Re-enable the hypercall page */
  261. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  262. hypercall_msr.enable = 1;
  263. hypercall_msr.guest_physical_address =
  264. vmalloc_to_pfn(hv_hypercall_pg_saved);
  265. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  266. hv_hypercall_pg = hv_hypercall_pg_saved;
  267. hv_hypercall_pg_saved = NULL;
  268. /*
  269. * Reenlightenment notifications are disabled by hv_cpu_die(0),
  270. * reenable them here if hv_reenlightenment_cb was previously set.
  271. */
  272. if (hv_reenlightenment_cb)
  273. set_hv_tscchange_cb(hv_reenlightenment_cb);
  274. }
  275. /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
  276. static struct syscore_ops hv_syscore_ops = {
  277. .suspend = hv_suspend,
  278. .resume = hv_resume,
  279. };
  280. static void (* __initdata old_setup_percpu_clockev)(void);
  281. static void __init hv_stimer_setup_percpu_clockev(void)
  282. {
  283. /*
  284. * Ignore any errors in setting up stimer clockevents
  285. * as we can run with the LAPIC timer as a fallback.
  286. */
  287. (void)hv_stimer_alloc(false);
  288. /*
  289. * Still register the LAPIC timer, because the direct-mode STIMER is
  290. * not supported by old versions of Hyper-V. This also allows users
  291. * to switch to LAPIC timer via /sys, if they want to.
  292. */
  293. if (old_setup_percpu_clockev)
  294. old_setup_percpu_clockev();
  295. }
  296. static void __init hv_get_partition_id(void)
  297. {
  298. struct hv_get_partition_id *output_page;
  299. u64 status;
  300. unsigned long flags;
  301. local_irq_save(flags);
  302. output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
  303. status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
  304. if (!hv_result_success(status)) {
  305. /* No point in proceeding if this failed */
  306. pr_err("Failed to get partition ID: %lld\n", status);
  307. BUG();
  308. }
  309. hv_current_partition_id = output_page->partition_id;
  310. local_irq_restore(flags);
  311. }
  312. /*
  313. * This function is to be invoked early in the boot sequence after the
  314. * hypervisor has been detected.
  315. *
  316. * 1. Setup the hypercall page.
  317. * 2. Register Hyper-V specific clocksource.
  318. * 3. Setup Hyper-V specific APIC entry points.
  319. */
  320. void __init hyperv_init(void)
  321. {
  322. u64 guest_id;
  323. union hv_x64_msr_hypercall_contents hypercall_msr;
  324. int cpuhp;
  325. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  326. return;
  327. if (hv_common_init())
  328. return;
  329. hv_vp_assist_page = kcalloc(num_possible_cpus(),
  330. sizeof(*hv_vp_assist_page), GFP_KERNEL);
  331. if (!hv_vp_assist_page) {
  332. ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
  333. goto common_free;
  334. }
  335. if (hv_isolation_type_snp()) {
  336. /* Negotiate GHCB Version. */
  337. if (!hv_ghcb_negotiate_protocol())
  338. hv_ghcb_terminate(SEV_TERM_SET_GEN,
  339. GHCB_SEV_ES_PROT_UNSUPPORTED);
  340. hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
  341. if (!hv_ghcb_pg)
  342. goto free_vp_assist_page;
  343. }
  344. cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
  345. hv_cpu_init, hv_cpu_die);
  346. if (cpuhp < 0)
  347. goto free_ghcb_page;
  348. /*
  349. * Setup the hypercall page and enable hypercalls.
  350. * 1. Register the guest ID
  351. * 2. Enable the hypercall and register the hypercall page
  352. */
  353. guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
  354. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  355. /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
  356. hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
  357. hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
  358. VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
  359. VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
  360. __builtin_return_address(0));
  361. if (hv_hypercall_pg == NULL)
  362. goto clean_guest_os_id;
  363. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  364. hypercall_msr.enable = 1;
  365. if (hv_root_partition) {
  366. struct page *pg;
  367. void *src;
  368. /*
  369. * For the root partition, the hypervisor will set up its
  370. * hypercall page. The hypervisor guarantees it will not show
  371. * up in the root's address space. The root can't change the
  372. * location of the hypercall page.
  373. *
  374. * Order is important here. We must enable the hypercall page
  375. * so it is populated with code, then copy the code to an
  376. * executable page.
  377. */
  378. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  379. pg = vmalloc_to_page(hv_hypercall_pg);
  380. src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
  381. MEMREMAP_WB);
  382. BUG_ON(!src);
  383. memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
  384. memunmap(src);
  385. } else {
  386. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
  387. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  388. }
  389. /*
  390. * Some versions of Hyper-V that provide IBT in guest VMs have a bug
  391. * in that there's no ENDBR64 instruction at the entry to the
  392. * hypercall page. Because hypercalls are invoked via an indirect call
  393. * to the hypercall page, all hypercall attempts fail when IBT is
  394. * enabled, and Linux panics. For such buggy versions, disable IBT.
  395. *
  396. * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
  397. * page, so if future Linux kernel versions enable IBT for 32-bit
  398. * builds, additional hypercall page hackery will be required here
  399. * to provide an ENDBR32.
  400. */
  401. #ifdef CONFIG_X86_KERNEL_IBT
  402. if (cpu_feature_enabled(X86_FEATURE_IBT) &&
  403. *(u32 *)hv_hypercall_pg != gen_endbr()) {
  404. setup_clear_cpu_cap(X86_FEATURE_IBT);
  405. pr_warn("Hyper-V: Disabling IBT because of Hyper-V bug\n");
  406. }
  407. #endif
  408. /*
  409. * hyperv_init() is called before LAPIC is initialized: see
  410. * apic_intr_mode_init() -> x86_platform.apic_post_init() and
  411. * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
  412. * depends on LAPIC, so hv_stimer_alloc() should be called from
  413. * x86_init.timers.setup_percpu_clockev.
  414. */
  415. old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
  416. x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
  417. hv_apic_init();
  418. x86_init.pci.arch_init = hv_pci_init;
  419. register_syscore_ops(&hv_syscore_ops);
  420. hyperv_init_cpuhp = cpuhp;
  421. if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
  422. hv_get_partition_id();
  423. BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
  424. #ifdef CONFIG_PCI_MSI
  425. /*
  426. * If we're running as root, we want to create our own PCI MSI domain.
  427. * We can't set this in hv_pci_init because that would be too late.
  428. */
  429. if (hv_root_partition)
  430. x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
  431. #endif
  432. /* Query the VMs extended capability once, so that it can be cached. */
  433. hv_query_ext_cap(0);
  434. #ifdef CONFIG_SWIOTLB
  435. /*
  436. * Swiotlb bounce buffer needs to be mapped in extra address
  437. * space. Map function doesn't work in the early place and so
  438. * call swiotlb_update_mem_attributes() here.
  439. */
  440. if (hv_is_isolation_supported())
  441. swiotlb_update_mem_attributes();
  442. #endif
  443. return;
  444. clean_guest_os_id:
  445. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  446. hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
  447. cpuhp_remove_state(cpuhp);
  448. free_ghcb_page:
  449. free_percpu(hv_ghcb_pg);
  450. free_vp_assist_page:
  451. kfree(hv_vp_assist_page);
  452. hv_vp_assist_page = NULL;
  453. common_free:
  454. hv_common_free();
  455. }
  456. /*
  457. * This routine is called before kexec/kdump, it does the required cleanup.
  458. */
  459. void hyperv_cleanup(void)
  460. {
  461. union hv_x64_msr_hypercall_contents hypercall_msr;
  462. union hv_reference_tsc_msr tsc_msr;
  463. /* Reset our OS id */
  464. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  465. hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
  466. /*
  467. * Reset hypercall page reference before reset the page,
  468. * let hypercall operations fail safely rather than
  469. * panic the kernel for using invalid hypercall page
  470. */
  471. hv_hypercall_pg = NULL;
  472. /* Reset the hypercall page */
  473. hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
  474. hypercall_msr.enable = 0;
  475. hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  476. /* Reset the TSC page */
  477. tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
  478. tsc_msr.enable = 0;
  479. hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  480. }
  481. void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
  482. {
  483. static bool panic_reported;
  484. u64 guest_id;
  485. if (in_die && !panic_on_oops)
  486. return;
  487. /*
  488. * We prefer to report panic on 'die' chain as we have proper
  489. * registers to report, but if we miss it (e.g. on BUG()) we need
  490. * to report it on 'panic'.
  491. */
  492. if (panic_reported)
  493. return;
  494. panic_reported = true;
  495. rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  496. wrmsrl(HV_X64_MSR_CRASH_P0, err);
  497. wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
  498. wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
  499. wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
  500. wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
  501. /*
  502. * Let Hyper-V know there is crash data available
  503. */
  504. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  505. }
  506. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  507. bool hv_is_hyperv_initialized(void)
  508. {
  509. union hv_x64_msr_hypercall_contents hypercall_msr;
  510. /*
  511. * Ensure that we're really on Hyper-V, and not a KVM or Xen
  512. * emulation of Hyper-V
  513. */
  514. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  515. return false;
  516. /*
  517. * Verify that earlier initialization succeeded by checking
  518. * that the hypercall page is setup
  519. */
  520. hypercall_msr.as_uint64 = 0;
  521. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  522. return hypercall_msr.enable;
  523. }
  524. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);