hv_init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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/types.h>
  20. #include <asm/apic.h>
  21. #include <asm/desc.h>
  22. #include <asm/hypervisor.h>
  23. #include <asm/hyperv-tlfs.h>
  24. #include <asm/mshyperv.h>
  25. #include <linux/version.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/mm.h>
  28. #include <linux/clockchips.h>
  29. #include <linux/hyperv.h>
  30. #include <linux/slab.h>
  31. #include <linux/cpuhotplug.h>
  32. #ifdef CONFIG_HYPERV_TSCPAGE
  33. static struct ms_hyperv_tsc_page *tsc_pg;
  34. struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
  35. {
  36. return tsc_pg;
  37. }
  38. EXPORT_SYMBOL_GPL(hv_get_tsc_page);
  39. static u64 read_hv_clock_tsc(struct clocksource *arg)
  40. {
  41. u64 current_tick = hv_read_tsc_page(tsc_pg);
  42. if (current_tick == U64_MAX)
  43. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  44. return current_tick;
  45. }
  46. static struct clocksource hyperv_cs_tsc = {
  47. .name = "hyperv_clocksource_tsc_page",
  48. .rating = 400,
  49. .read = read_hv_clock_tsc,
  50. .mask = CLOCKSOURCE_MASK(64),
  51. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  52. };
  53. #endif
  54. static u64 read_hv_clock_msr(struct clocksource *arg)
  55. {
  56. u64 current_tick;
  57. /*
  58. * Read the partition counter to get the current tick count. This count
  59. * is set to 0 when the partition is created and is incremented in
  60. * 100 nanosecond units.
  61. */
  62. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  63. return current_tick;
  64. }
  65. static struct clocksource hyperv_cs_msr = {
  66. .name = "hyperv_clocksource_msr",
  67. .rating = 400,
  68. .read = read_hv_clock_msr,
  69. .mask = CLOCKSOURCE_MASK(64),
  70. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  71. };
  72. void *hv_hypercall_pg;
  73. EXPORT_SYMBOL_GPL(hv_hypercall_pg);
  74. struct clocksource *hyperv_cs;
  75. EXPORT_SYMBOL_GPL(hyperv_cs);
  76. u32 *hv_vp_index;
  77. EXPORT_SYMBOL_GPL(hv_vp_index);
  78. struct hv_vp_assist_page **hv_vp_assist_page;
  79. EXPORT_SYMBOL_GPL(hv_vp_assist_page);
  80. void __percpu **hyperv_pcpu_input_arg;
  81. EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
  82. u32 hv_max_vp_index;
  83. static int hv_cpu_init(unsigned int cpu)
  84. {
  85. u64 msr_vp_index;
  86. struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
  87. void **input_arg;
  88. input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  89. *input_arg = page_address(alloc_page(GFP_KERNEL));
  90. hv_get_vp_index(msr_vp_index);
  91. hv_vp_index[smp_processor_id()] = msr_vp_index;
  92. if (msr_vp_index > hv_max_vp_index)
  93. hv_max_vp_index = msr_vp_index;
  94. if (!hv_vp_assist_page)
  95. return 0;
  96. if (!*hvp)
  97. *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  98. if (*hvp) {
  99. u64 val;
  100. val = vmalloc_to_pfn(*hvp);
  101. val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
  102. HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
  103. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
  104. }
  105. return 0;
  106. }
  107. static void (*hv_reenlightenment_cb)(void);
  108. static void hv_reenlightenment_notify(struct work_struct *dummy)
  109. {
  110. struct hv_tsc_emulation_status emu_status;
  111. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  112. /* Don't issue the callback if TSC accesses are not emulated */
  113. if (hv_reenlightenment_cb && emu_status.inprogress)
  114. hv_reenlightenment_cb();
  115. }
  116. static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
  117. void hyperv_stop_tsc_emulation(void)
  118. {
  119. u64 freq;
  120. struct hv_tsc_emulation_status emu_status;
  121. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  122. emu_status.inprogress = 0;
  123. wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  124. rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
  125. tsc_khz = div64_u64(freq, 1000);
  126. }
  127. EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
  128. static inline bool hv_reenlightenment_available(void)
  129. {
  130. /*
  131. * Check for required features and priviliges to make TSC frequency
  132. * change notifications work.
  133. */
  134. return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
  135. ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
  136. ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
  137. }
  138. __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
  139. {
  140. entering_ack_irq();
  141. inc_irq_stat(irq_hv_reenlightenment_count);
  142. schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
  143. exiting_irq();
  144. }
  145. void set_hv_tscchange_cb(void (*cb)(void))
  146. {
  147. struct hv_reenlightenment_control re_ctrl = {
  148. .vector = HYPERV_REENLIGHTENMENT_VECTOR,
  149. .enabled = 1,
  150. .target_vp = hv_vp_index[smp_processor_id()]
  151. };
  152. struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
  153. if (!hv_reenlightenment_available()) {
  154. pr_warn("Hyper-V: reenlightenment support is unavailable\n");
  155. return;
  156. }
  157. hv_reenlightenment_cb = cb;
  158. /* Make sure callback is registered before we write to MSRs */
  159. wmb();
  160. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  161. wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
  162. }
  163. EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
  164. void clear_hv_tscchange_cb(void)
  165. {
  166. struct hv_reenlightenment_control re_ctrl;
  167. if (!hv_reenlightenment_available())
  168. return;
  169. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  170. re_ctrl.enabled = 0;
  171. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  172. hv_reenlightenment_cb = NULL;
  173. }
  174. EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
  175. static int hv_cpu_die(unsigned int cpu)
  176. {
  177. struct hv_reenlightenment_control re_ctrl;
  178. unsigned int new_cpu;
  179. unsigned long flags;
  180. void **input_arg;
  181. void *input_pg = NULL;
  182. local_irq_save(flags);
  183. input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  184. input_pg = *input_arg;
  185. *input_arg = NULL;
  186. local_irq_restore(flags);
  187. free_page((unsigned long)input_pg);
  188. if (hv_vp_assist_page && hv_vp_assist_page[cpu])
  189. wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
  190. if (hv_reenlightenment_cb == NULL)
  191. return 0;
  192. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  193. if (re_ctrl.target_vp == hv_vp_index[cpu]) {
  194. /* Reassign to some other online CPU */
  195. new_cpu = cpumask_any_but(cpu_online_mask, cpu);
  196. re_ctrl.target_vp = hv_vp_index[new_cpu];
  197. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  198. }
  199. return 0;
  200. }
  201. /*
  202. * This function is to be invoked early in the boot sequence after the
  203. * hypervisor has been detected.
  204. *
  205. * 1. Setup the hypercall page.
  206. * 2. Register Hyper-V specific clocksource.
  207. * 3. Setup Hyper-V specific APIC entry points.
  208. */
  209. void __init hyperv_init(void)
  210. {
  211. u64 guest_id, required_msrs;
  212. union hv_x64_msr_hypercall_contents hypercall_msr;
  213. int cpuhp;
  214. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  215. return;
  216. /* Absolutely required MSRs */
  217. required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
  218. HV_X64_MSR_VP_INDEX_AVAILABLE;
  219. if ((ms_hyperv.features & required_msrs) != required_msrs)
  220. return;
  221. /*
  222. * Allocate the per-CPU state for the hypercall input arg.
  223. * If this allocation fails, we will not be able to setup
  224. * (per-CPU) hypercall input page and thus this failure is
  225. * fatal on Hyper-V.
  226. */
  227. hyperv_pcpu_input_arg = alloc_percpu(void *);
  228. BUG_ON(hyperv_pcpu_input_arg == NULL);
  229. /* Allocate percpu VP index */
  230. hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
  231. GFP_KERNEL);
  232. if (!hv_vp_index)
  233. return;
  234. hv_vp_assist_page = kcalloc(num_possible_cpus(),
  235. sizeof(*hv_vp_assist_page), GFP_KERNEL);
  236. if (!hv_vp_assist_page) {
  237. ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
  238. goto free_vp_index;
  239. }
  240. cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
  241. hv_cpu_init, hv_cpu_die);
  242. if (cpuhp < 0)
  243. goto free_vp_assist_page;
  244. /*
  245. * Setup the hypercall page and enable hypercalls.
  246. * 1. Register the guest ID
  247. * 2. Enable the hypercall and register the hypercall page
  248. */
  249. guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  250. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  251. hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
  252. if (hv_hypercall_pg == NULL) {
  253. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  254. goto remove_cpuhp_state;
  255. }
  256. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  257. hypercall_msr.enable = 1;
  258. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
  259. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  260. hv_apic_init();
  261. /*
  262. * Register Hyper-V specific clocksource.
  263. */
  264. #ifdef CONFIG_HYPERV_TSCPAGE
  265. if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
  266. union hv_x64_msr_hypercall_contents tsc_msr;
  267. tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  268. if (!tsc_pg)
  269. goto register_msr_cs;
  270. hyperv_cs = &hyperv_cs_tsc;
  271. rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  272. tsc_msr.enable = 1;
  273. tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
  274. wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  275. hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
  276. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  277. return;
  278. }
  279. register_msr_cs:
  280. #endif
  281. /*
  282. * For 32 bit guests just use the MSR based mechanism for reading
  283. * the partition counter.
  284. */
  285. hyperv_cs = &hyperv_cs_msr;
  286. if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
  287. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  288. return;
  289. remove_cpuhp_state:
  290. cpuhp_remove_state(cpuhp);
  291. free_vp_assist_page:
  292. kfree(hv_vp_assist_page);
  293. hv_vp_assist_page = NULL;
  294. free_vp_index:
  295. kfree(hv_vp_index);
  296. hv_vp_index = NULL;
  297. }
  298. /*
  299. * This routine is called before kexec/kdump, it does the required cleanup.
  300. */
  301. void hyperv_cleanup(void)
  302. {
  303. union hv_x64_msr_hypercall_contents hypercall_msr;
  304. /* Reset our OS id */
  305. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  306. /* Reset the hypercall page */
  307. hypercall_msr.as_uint64 = 0;
  308. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  309. /* Reset the TSC page */
  310. hypercall_msr.as_uint64 = 0;
  311. wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
  312. }
  313. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  314. void hyperv_report_panic(struct pt_regs *regs, long err)
  315. {
  316. static bool panic_reported;
  317. u64 guest_id;
  318. /*
  319. * We prefer to report panic on 'die' chain as we have proper
  320. * registers to report, but if we miss it (e.g. on BUG()) we need
  321. * to report it on 'panic'.
  322. */
  323. if (panic_reported)
  324. return;
  325. panic_reported = true;
  326. rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  327. wrmsrl(HV_X64_MSR_CRASH_P0, err);
  328. wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
  329. wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
  330. wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
  331. wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
  332. /*
  333. * Let Hyper-V know there is crash data available
  334. */
  335. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  336. }
  337. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  338. bool hv_is_hyperv_initialized(void)
  339. {
  340. union hv_x64_msr_hypercall_contents hypercall_msr;
  341. /*
  342. * Ensure that we're really on Hyper-V, and not a KVM or Xen
  343. * emulation of Hyper-V
  344. */
  345. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  346. return false;
  347. /*
  348. * Verify that earlier initialization succeeded by checking
  349. * that the hypercall page is setup
  350. */
  351. hypercall_msr.as_uint64 = 0;
  352. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  353. return hypercall_msr.enable;
  354. }
  355. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);