PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/x86/hyperv/hv_init.c

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