PageRenderTime 474ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/x86/hyperv/hv_init.c

https://gitlab.com/kush/linux
C | 507 lines | 303 code | 99 blank | 105 comment | 35 complexity | 83f285d7526d59cee97b8769a356970b MD5 | raw file
  1. /*
  2. * X86 specific Hyper-V initialization code.
  3. *
  4. * Copyright (C) 2016, Microsoft, Inc.
  5. *
  6. * Author : K. Y. Srinivasan <kys@microsoft.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  15. * NON INFRINGEMENT. See the GNU General Public License for more
  16. * details.
  17. *
  18. */
  19. #include <linux/efi.h>
  20. #include <linux/types.h>
  21. #include <asm/apic.h>
  22. #include <asm/desc.h>
  23. #include <asm/hypervisor.h>
  24. #include <asm/hyperv-tlfs.h>
  25. #include <asm/mshyperv.h>
  26. #include <linux/version.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/mm.h>
  29. #include <linux/clockchips.h>
  30. #include <linux/hyperv.h>
  31. #include <linux/slab.h>
  32. #include <linux/cpuhotplug.h>
  33. #ifdef CONFIG_HYPERV_TSCPAGE
  34. static struct ms_hyperv_tsc_page *tsc_pg;
  35. struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
  36. {
  37. return tsc_pg;
  38. }
  39. EXPORT_SYMBOL_GPL(hv_get_tsc_page);
  40. static u64 read_hv_clock_tsc(struct clocksource *arg)
  41. {
  42. u64 current_tick = hv_read_tsc_page(tsc_pg);
  43. if (current_tick == U64_MAX)
  44. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  45. return current_tick;
  46. }
  47. static struct clocksource hyperv_cs_tsc = {
  48. .name = "hyperv_clocksource_tsc_page",
  49. .rating = 400,
  50. .read = read_hv_clock_tsc,
  51. .mask = CLOCKSOURCE_MASK(64),
  52. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  53. };
  54. #endif
  55. static u64 read_hv_clock_msr(struct clocksource *arg)
  56. {
  57. u64 current_tick;
  58. /*
  59. * Read the partition counter to get the current tick count. This count
  60. * is set to 0 when the partition is created and is incremented in
  61. * 100 nanosecond units.
  62. */
  63. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  64. return current_tick;
  65. }
  66. static struct clocksource hyperv_cs_msr = {
  67. .name = "hyperv_clocksource_msr",
  68. .rating = 400,
  69. .read = read_hv_clock_msr,
  70. .mask = CLOCKSOURCE_MASK(64),
  71. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  72. };
  73. void *hv_hypercall_pg;
  74. EXPORT_SYMBOL_GPL(hv_hypercall_pg);
  75. struct clocksource *hyperv_cs;
  76. EXPORT_SYMBOL_GPL(hyperv_cs);
  77. u32 *hv_vp_index;
  78. EXPORT_SYMBOL_GPL(hv_vp_index);
  79. struct hv_vp_assist_page **hv_vp_assist_page;
  80. EXPORT_SYMBOL_GPL(hv_vp_assist_page);
  81. void __percpu **hyperv_pcpu_input_arg;
  82. EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
  83. u32 hv_max_vp_index;
  84. EXPORT_SYMBOL_GPL(hv_max_vp_index);
  85. static int hv_cpu_init(unsigned int cpu)
  86. {
  87. u64 msr_vp_index;
  88. struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
  89. void **input_arg;
  90. struct page *pg;
  91. input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  92. pg = alloc_page(GFP_KERNEL);
  93. if (unlikely(!pg))
  94. return -ENOMEM;
  95. *input_arg = page_address(pg);
  96. hv_get_vp_index(msr_vp_index);
  97. hv_vp_index[smp_processor_id()] = msr_vp_index;
  98. if (msr_vp_index > hv_max_vp_index)
  99. hv_max_vp_index = msr_vp_index;
  100. if (!hv_vp_assist_page)
  101. return 0;
  102. if (!*hvp)
  103. *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  104. if (*hvp) {
  105. u64 val;
  106. val = vmalloc_to_pfn(*hvp);
  107. val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
  108. HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
  109. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
  110. }
  111. return 0;
  112. }
  113. static void (*hv_reenlightenment_cb)(void);
  114. static void hv_reenlightenment_notify(struct work_struct *dummy)
  115. {
  116. struct hv_tsc_emulation_status emu_status;
  117. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  118. /* Don't issue the callback if TSC accesses are not emulated */
  119. if (hv_reenlightenment_cb && emu_status.inprogress)
  120. hv_reenlightenment_cb();
  121. }
  122. static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
  123. void hyperv_stop_tsc_emulation(void)
  124. {
  125. u64 freq;
  126. struct hv_tsc_emulation_status emu_status;
  127. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  128. emu_status.inprogress = 0;
  129. wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  130. rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
  131. tsc_khz = div64_u64(freq, 1000);
  132. }
  133. EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
  134. static inline bool hv_reenlightenment_available(void)
  135. {
  136. /*
  137. * Check for required features and priviliges to make TSC frequency
  138. * change notifications work.
  139. */
  140. return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
  141. ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
  142. ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
  143. }
  144. __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
  145. {
  146. entering_ack_irq();
  147. inc_irq_stat(irq_hv_reenlightenment_count);
  148. schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
  149. exiting_irq();
  150. }
  151. void set_hv_tscchange_cb(void (*cb)(void))
  152. {
  153. struct hv_reenlightenment_control re_ctrl = {
  154. .vector = HYPERV_REENLIGHTENMENT_VECTOR,
  155. .enabled = 1,
  156. .target_vp = hv_vp_index[smp_processor_id()]
  157. };
  158. struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
  159. if (!hv_reenlightenment_available()) {
  160. pr_warn("Hyper-V: reenlightenment support is unavailable\n");
  161. return;
  162. }
  163. hv_reenlightenment_cb = cb;
  164. /* Make sure callback is registered before we write to MSRs */
  165. wmb();
  166. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  167. wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
  168. }
  169. EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
  170. void clear_hv_tscchange_cb(void)
  171. {
  172. struct hv_reenlightenment_control re_ctrl;
  173. if (!hv_reenlightenment_available())
  174. return;
  175. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  176. re_ctrl.enabled = 0;
  177. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  178. hv_reenlightenment_cb = NULL;
  179. }
  180. EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
  181. static int hv_cpu_die(unsigned int cpu)
  182. {
  183. struct hv_reenlightenment_control re_ctrl;
  184. unsigned int new_cpu;
  185. unsigned long flags;
  186. void **input_arg;
  187. void *input_pg = NULL;
  188. local_irq_save(flags);
  189. input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  190. input_pg = *input_arg;
  191. *input_arg = NULL;
  192. local_irq_restore(flags);
  193. free_page((unsigned long)input_pg);
  194. if (hv_vp_assist_page && hv_vp_assist_page[cpu])
  195. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
  196. if (hv_reenlightenment_cb == NULL)
  197. return 0;
  198. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  199. if (re_ctrl.target_vp == hv_vp_index[cpu]) {
  200. /* Reassign to some other online CPU */
  201. new_cpu = cpumask_any_but(cpu_online_mask, cpu);
  202. re_ctrl.target_vp = hv_vp_index[new_cpu];
  203. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  204. }
  205. return 0;
  206. }
  207. static int __init hv_pci_init(void)
  208. {
  209. int gen2vm = efi_enabled(EFI_BOOT);
  210. /*
  211. * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
  212. * The purpose is to suppress the harmless warning:
  213. * "PCI: Fatal: No config space access function found"
  214. */
  215. if (gen2vm)
  216. return 0;
  217. /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
  218. return 1;
  219. }
  220. /*
  221. * This function is to be invoked early in the boot sequence after the
  222. * hypervisor has been detected.
  223. *
  224. * 1. Setup the hypercall page.
  225. * 2. Register Hyper-V specific clocksource.
  226. * 3. Setup Hyper-V specific APIC entry points.
  227. */
  228. void __init hyperv_init(void)
  229. {
  230. u64 guest_id, required_msrs;
  231. union hv_x64_msr_hypercall_contents hypercall_msr;
  232. int cpuhp, i;
  233. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  234. return;
  235. /* Absolutely required MSRs */
  236. required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
  237. HV_X64_MSR_VP_INDEX_AVAILABLE;
  238. if ((ms_hyperv.features & required_msrs) != required_msrs)
  239. return;
  240. /*
  241. * Allocate the per-CPU state for the hypercall input arg.
  242. * If this allocation fails, we will not be able to setup
  243. * (per-CPU) hypercall input page and thus this failure is
  244. * fatal on Hyper-V.
  245. */
  246. hyperv_pcpu_input_arg = alloc_percpu(void *);
  247. BUG_ON(hyperv_pcpu_input_arg == NULL);
  248. /* Allocate percpu VP index */
  249. hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
  250. GFP_KERNEL);
  251. if (!hv_vp_index)
  252. return;
  253. for (i = 0; i < num_possible_cpus(); i++)
  254. hv_vp_index[i] = VP_INVAL;
  255. hv_vp_assist_page = kcalloc(num_possible_cpus(),
  256. sizeof(*hv_vp_assist_page), GFP_KERNEL);
  257. if (!hv_vp_assist_page) {
  258. ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
  259. goto free_vp_index;
  260. }
  261. cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
  262. hv_cpu_init, hv_cpu_die);
  263. if (cpuhp < 0)
  264. goto free_vp_assist_page;
  265. /*
  266. * Setup the hypercall page and enable hypercalls.
  267. * 1. Register the guest ID
  268. * 2. Enable the hypercall and register the hypercall page
  269. */
  270. guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  271. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  272. hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
  273. if (hv_hypercall_pg == NULL) {
  274. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  275. goto remove_cpuhp_state;
  276. }
  277. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  278. hypercall_msr.enable = 1;
  279. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
  280. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  281. hv_apic_init();
  282. x86_init.pci.arch_init = hv_pci_init;
  283. /*
  284. * Register Hyper-V specific clocksource.
  285. */
  286. #ifdef CONFIG_HYPERV_TSCPAGE
  287. if (ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) {
  288. union hv_x64_msr_hypercall_contents tsc_msr;
  289. tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  290. if (!tsc_pg)
  291. goto register_msr_cs;
  292. hyperv_cs = &hyperv_cs_tsc;
  293. rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  294. tsc_msr.enable = 1;
  295. tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
  296. wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  297. hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
  298. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  299. return;
  300. }
  301. register_msr_cs:
  302. #endif
  303. /*
  304. * For 32 bit guests just use the MSR based mechanism for reading
  305. * the partition counter.
  306. */
  307. hyperv_cs = &hyperv_cs_msr;
  308. if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
  309. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  310. return;
  311. remove_cpuhp_state:
  312. cpuhp_remove_state(cpuhp);
  313. free_vp_assist_page:
  314. kfree(hv_vp_assist_page);
  315. hv_vp_assist_page = NULL;
  316. free_vp_index:
  317. kfree(hv_vp_index);
  318. hv_vp_index = NULL;
  319. }
  320. /*
  321. * This routine is called before kexec/kdump, it does the required cleanup.
  322. */
  323. void hyperv_cleanup(void)
  324. {
  325. union hv_x64_msr_hypercall_contents hypercall_msr;
  326. /* Reset our OS id */
  327. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  328. /*
  329. * Reset hypercall page reference before reset the page,
  330. * let hypercall operations fail safely rather than
  331. * panic the kernel for using invalid hypercall page
  332. */
  333. hv_hypercall_pg = NULL;
  334. /* Reset the hypercall page */
  335. hypercall_msr.as_uint64 = 0;
  336. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  337. /* Reset the TSC page */
  338. hypercall_msr.as_uint64 = 0;
  339. wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
  340. }
  341. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  342. void hyperv_report_panic(struct pt_regs *regs, long err)
  343. {
  344. static bool panic_reported;
  345. u64 guest_id;
  346. /*
  347. * We prefer to report panic on 'die' chain as we have proper
  348. * registers to report, but if we miss it (e.g. on BUG()) we need
  349. * to report it on 'panic'.
  350. */
  351. if (panic_reported)
  352. return;
  353. panic_reported = true;
  354. rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  355. wrmsrl(HV_X64_MSR_CRASH_P0, err);
  356. wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
  357. wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
  358. wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
  359. wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
  360. /*
  361. * Let Hyper-V know there is crash data available
  362. */
  363. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  364. }
  365. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  366. /**
  367. * hyperv_report_panic_msg - report panic message to Hyper-V
  368. * @pa: physical address of the panic page containing the message
  369. * @size: size of the message in the page
  370. */
  371. void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
  372. {
  373. /*
  374. * P3 to contain the physical address of the panic page & P4 to
  375. * contain the size of the panic data in that page. Rest of the
  376. * registers are no-op when the NOTIFY_MSG flag is set.
  377. */
  378. wrmsrl(HV_X64_MSR_CRASH_P0, 0);
  379. wrmsrl(HV_X64_MSR_CRASH_P1, 0);
  380. wrmsrl(HV_X64_MSR_CRASH_P2, 0);
  381. wrmsrl(HV_X64_MSR_CRASH_P3, pa);
  382. wrmsrl(HV_X64_MSR_CRASH_P4, size);
  383. /*
  384. * Let Hyper-V know there is crash data available along with
  385. * the panic message.
  386. */
  387. wrmsrl(HV_X64_MSR_CRASH_CTL,
  388. (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
  389. }
  390. EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
  391. bool hv_is_hyperv_initialized(void)
  392. {
  393. union hv_x64_msr_hypercall_contents hypercall_msr;
  394. /*
  395. * Ensure that we're really on Hyper-V, and not a KVM or Xen
  396. * emulation of Hyper-V
  397. */
  398. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  399. return false;
  400. /*
  401. * Verify that earlier initialization succeeded by checking
  402. * that the hypercall page is setup
  403. */
  404. hypercall_msr.as_uint64 = 0;
  405. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  406. return hypercall_msr.enable;
  407. }
  408. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);