hv_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. static int hv_cpu_init(unsigned int cpu)
  85. {
  86. u64 msr_vp_index;
  87. struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
  88. void **input_arg;
  89. struct page *pg;
  90. input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  91. pg = alloc_page(GFP_KERNEL);
  92. if (unlikely(!pg))
  93. return -ENOMEM;
  94. *input_arg = page_address(pg);
  95. hv_get_vp_index(msr_vp_index);
  96. hv_vp_index[smp_processor_id()] = msr_vp_index;
  97. if (msr_vp_index > hv_max_vp_index)
  98. hv_max_vp_index = msr_vp_index;
  99. if (!hv_vp_assist_page)
  100. return 0;
  101. if (!*hvp)
  102. *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  103. if (*hvp) {
  104. u64 val;
  105. val = vmalloc_to_pfn(*hvp);
  106. val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
  107. HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
  108. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
  109. }
  110. return 0;
  111. }
  112. static void (*hv_reenlightenment_cb)(void);
  113. static void hv_reenlightenment_notify(struct work_struct *dummy)
  114. {
  115. struct hv_tsc_emulation_status emu_status;
  116. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  117. /* Don't issue the callback if TSC accesses are not emulated */
  118. if (hv_reenlightenment_cb && emu_status.inprogress)
  119. hv_reenlightenment_cb();
  120. }
  121. static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
  122. void hyperv_stop_tsc_emulation(void)
  123. {
  124. u64 freq;
  125. struct hv_tsc_emulation_status emu_status;
  126. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  127. emu_status.inprogress = 0;
  128. wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  129. rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
  130. tsc_khz = div64_u64(freq, 1000);
  131. }
  132. EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
  133. static inline bool hv_reenlightenment_available(void)
  134. {
  135. /*
  136. * Check for required features and priviliges to make TSC frequency
  137. * change notifications work.
  138. */
  139. return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
  140. ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
  141. ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
  142. }
  143. __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
  144. {
  145. entering_ack_irq();
  146. inc_irq_stat(irq_hv_reenlightenment_count);
  147. schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
  148. exiting_irq();
  149. }
  150. void set_hv_tscchange_cb(void (*cb)(void))
  151. {
  152. struct hv_reenlightenment_control re_ctrl = {
  153. .vector = HYPERV_REENLIGHTENMENT_VECTOR,
  154. .enabled = 1,
  155. .target_vp = hv_vp_index[smp_processor_id()]
  156. };
  157. struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
  158. if (!hv_reenlightenment_available()) {
  159. pr_warn("Hyper-V: reenlightenment support is unavailable\n");
  160. return;
  161. }
  162. hv_reenlightenment_cb = cb;
  163. /* Make sure callback is registered before we write to MSRs */
  164. wmb();
  165. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  166. wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
  167. }
  168. EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
  169. void clear_hv_tscchange_cb(void)
  170. {
  171. struct hv_reenlightenment_control re_ctrl;
  172. if (!hv_reenlightenment_available())
  173. return;
  174. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  175. re_ctrl.enabled = 0;
  176. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  177. hv_reenlightenment_cb = NULL;
  178. }
  179. EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
  180. static int hv_cpu_die(unsigned int cpu)
  181. {
  182. struct hv_reenlightenment_control re_ctrl;
  183. unsigned int new_cpu;
  184. unsigned long flags;
  185. void **input_arg;
  186. void *input_pg = NULL;
  187. local_irq_save(flags);
  188. input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  189. input_pg = *input_arg;
  190. *input_arg = NULL;
  191. local_irq_restore(flags);
  192. free_page((unsigned long)input_pg);
  193. if (hv_vp_assist_page && hv_vp_assist_page[cpu])
  194. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
  195. if (hv_reenlightenment_cb == NULL)
  196. return 0;
  197. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  198. if (re_ctrl.target_vp == hv_vp_index[cpu]) {
  199. /* Reassign to some other online CPU */
  200. new_cpu = cpumask_any_but(cpu_online_mask, cpu);
  201. re_ctrl.target_vp = hv_vp_index[new_cpu];
  202. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  203. }
  204. return 0;
  205. }
  206. static int __init hv_pci_init(void)
  207. {
  208. int gen2vm = efi_enabled(EFI_BOOT);
  209. /*
  210. * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
  211. * The purpose is to suppress the harmless warning:
  212. * "PCI: Fatal: No config space access function found"
  213. */
  214. if (gen2vm)
  215. return 0;
  216. /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
  217. return 1;
  218. }
  219. /*
  220. * This function is to be invoked early in the boot sequence after the
  221. * hypervisor has been detected.
  222. *
  223. * 1. Setup the hypercall page.
  224. * 2. Register Hyper-V specific clocksource.
  225. * 3. Setup Hyper-V specific APIC entry points.
  226. */
  227. void __init hyperv_init(void)
  228. {
  229. u64 guest_id, required_msrs;
  230. union hv_x64_msr_hypercall_contents hypercall_msr;
  231. int cpuhp, i;
  232. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  233. return;
  234. /* Absolutely required MSRs */
  235. required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
  236. HV_X64_MSR_VP_INDEX_AVAILABLE;
  237. if ((ms_hyperv.features & required_msrs) != required_msrs)
  238. return;
  239. /*
  240. * Allocate the per-CPU state for the hypercall input arg.
  241. * If this allocation fails, we will not be able to setup
  242. * (per-CPU) hypercall input page and thus this failure is
  243. * fatal on Hyper-V.
  244. */
  245. hyperv_pcpu_input_arg = alloc_percpu(void *);
  246. BUG_ON(hyperv_pcpu_input_arg == NULL);
  247. /* Allocate percpu VP index */
  248. hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
  249. GFP_KERNEL);
  250. if (!hv_vp_index)
  251. return;
  252. for (i = 0; i < num_possible_cpus(); i++)
  253. hv_vp_index[i] = VP_INVAL;
  254. hv_vp_assist_page = kcalloc(num_possible_cpus(),
  255. sizeof(*hv_vp_assist_page), GFP_KERNEL);
  256. if (!hv_vp_assist_page) {
  257. ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
  258. goto free_vp_index;
  259. }
  260. cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
  261. hv_cpu_init, hv_cpu_die);
  262. if (cpuhp < 0)
  263. goto free_vp_assist_page;
  264. /*
  265. * Setup the hypercall page and enable hypercalls.
  266. * 1. Register the guest ID
  267. * 2. Enable the hypercall and register the hypercall page
  268. */
  269. guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  270. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  271. hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
  272. if (hv_hypercall_pg == NULL) {
  273. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  274. goto remove_cpuhp_state;
  275. }
  276. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  277. hypercall_msr.enable = 1;
  278. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
  279. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  280. hv_apic_init();
  281. x86_init.pci.arch_init = hv_pci_init;
  282. /*
  283. * Register Hyper-V specific clocksource.
  284. */
  285. #ifdef CONFIG_HYPERV_TSCPAGE
  286. if (ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) {
  287. union hv_x64_msr_hypercall_contents tsc_msr;
  288. tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  289. if (!tsc_pg)
  290. goto register_msr_cs;
  291. hyperv_cs = &hyperv_cs_tsc;
  292. rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  293. tsc_msr.enable = 1;
  294. tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
  295. wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  296. hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
  297. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  298. return;
  299. }
  300. register_msr_cs:
  301. #endif
  302. /*
  303. * For 32 bit guests just use the MSR based mechanism for reading
  304. * the partition counter.
  305. */
  306. hyperv_cs = &hyperv_cs_msr;
  307. if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
  308. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  309. return;
  310. remove_cpuhp_state:
  311. cpuhp_remove_state(cpuhp);
  312. free_vp_assist_page:
  313. kfree(hv_vp_assist_page);
  314. hv_vp_assist_page = NULL;
  315. free_vp_index:
  316. kfree(hv_vp_index);
  317. hv_vp_index = NULL;
  318. }
  319. /*
  320. * This routine is called before kexec/kdump, it does the required cleanup.
  321. */
  322. void hyperv_cleanup(void)
  323. {
  324. union hv_x64_msr_hypercall_contents hypercall_msr;
  325. /* Reset our OS id */
  326. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  327. /*
  328. * Reset hypercall page reference before reset the page,
  329. * let hypercall operations fail safely rather than
  330. * panic the kernel for using invalid hypercall page
  331. */
  332. hv_hypercall_pg = NULL;
  333. /* Reset the hypercall page */
  334. hypercall_msr.as_uint64 = 0;
  335. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  336. /* Reset the TSC page */
  337. hypercall_msr.as_uint64 = 0;
  338. wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
  339. }
  340. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  341. void hyperv_report_panic(struct pt_regs *regs, long err)
  342. {
  343. static bool panic_reported;
  344. u64 guest_id;
  345. /*
  346. * We prefer to report panic on 'die' chain as we have proper
  347. * registers to report, but if we miss it (e.g. on BUG()) we need
  348. * to report it on 'panic'.
  349. */
  350. if (panic_reported)
  351. return;
  352. panic_reported = true;
  353. rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  354. wrmsrl(HV_X64_MSR_CRASH_P0, err);
  355. wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
  356. wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
  357. wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
  358. wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
  359. /*
  360. * Let Hyper-V know there is crash data available
  361. */
  362. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  363. }
  364. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  365. /**
  366. * hyperv_report_panic_msg - report panic message to Hyper-V
  367. * @pa: physical address of the panic page containing the message
  368. * @size: size of the message in the page
  369. */
  370. void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
  371. {
  372. /*
  373. * P3 to contain the physical address of the panic page & P4 to
  374. * contain the size of the panic data in that page. Rest of the
  375. * registers are no-op when the NOTIFY_MSG flag is set.
  376. */
  377. wrmsrl(HV_X64_MSR_CRASH_P0, 0);
  378. wrmsrl(HV_X64_MSR_CRASH_P1, 0);
  379. wrmsrl(HV_X64_MSR_CRASH_P2, 0);
  380. wrmsrl(HV_X64_MSR_CRASH_P3, pa);
  381. wrmsrl(HV_X64_MSR_CRASH_P4, size);
  382. /*
  383. * Let Hyper-V know there is crash data available along with
  384. * the panic message.
  385. */
  386. wrmsrl(HV_X64_MSR_CRASH_CTL,
  387. (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
  388. }
  389. EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
  390. bool hv_is_hyperv_initialized(void)
  391. {
  392. union hv_x64_msr_hypercall_contents hypercall_msr;
  393. /*
  394. * Ensure that we're really on Hyper-V, and not a KVM or Xen
  395. * emulation of Hyper-V
  396. */
  397. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  398. return false;
  399. /*
  400. * Verify that earlier initialization succeeded by checking
  401. * that the hypercall page is setup
  402. */
  403. hypercall_msr.as_uint64 = 0;
  404. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  405. return hypercall_msr.enable;
  406. }
  407. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);