hv_init.c 20 KB

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