hv_init.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.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. u32 hv_max_vp_index;
  79. static int hv_cpu_init(unsigned int cpu)
  80. {
  81. u64 msr_vp_index;
  82. hv_get_vp_index(msr_vp_index);
  83. hv_vp_index[smp_processor_id()] = msr_vp_index;
  84. if (msr_vp_index > hv_max_vp_index)
  85. hv_max_vp_index = msr_vp_index;
  86. return 0;
  87. }
  88. static void (*hv_reenlightenment_cb)(void);
  89. static void hv_reenlightenment_notify(struct work_struct *dummy)
  90. {
  91. struct hv_tsc_emulation_status emu_status;
  92. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  93. /* Don't issue the callback if TSC accesses are not emulated */
  94. if (hv_reenlightenment_cb && emu_status.inprogress)
  95. hv_reenlightenment_cb();
  96. }
  97. static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
  98. void hyperv_stop_tsc_emulation(void)
  99. {
  100. u64 freq;
  101. struct hv_tsc_emulation_status emu_status;
  102. rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  103. emu_status.inprogress = 0;
  104. wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
  105. rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
  106. tsc_khz = div64_u64(freq, 1000);
  107. }
  108. EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
  109. static inline bool hv_reenlightenment_available(void)
  110. {
  111. /*
  112. * Check for required features and priviliges to make TSC frequency
  113. * change notifications work.
  114. */
  115. return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
  116. ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
  117. ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
  118. }
  119. __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
  120. {
  121. entering_ack_irq();
  122. inc_irq_stat(irq_hv_reenlightenment_count);
  123. schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
  124. exiting_irq();
  125. }
  126. void set_hv_tscchange_cb(void (*cb)(void))
  127. {
  128. struct hv_reenlightenment_control re_ctrl = {
  129. .vector = HYPERV_REENLIGHTENMENT_VECTOR,
  130. .enabled = 1,
  131. .target_vp = hv_vp_index[smp_processor_id()]
  132. };
  133. struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
  134. if (!hv_reenlightenment_available()) {
  135. pr_warn("Hyper-V: reenlightenment support is unavailable\n");
  136. return;
  137. }
  138. hv_reenlightenment_cb = cb;
  139. /* Make sure callback is registered before we write to MSRs */
  140. wmb();
  141. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  142. wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
  143. }
  144. EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
  145. void clear_hv_tscchange_cb(void)
  146. {
  147. struct hv_reenlightenment_control re_ctrl;
  148. if (!hv_reenlightenment_available())
  149. return;
  150. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  151. re_ctrl.enabled = 0;
  152. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
  153. hv_reenlightenment_cb = NULL;
  154. }
  155. EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
  156. static int hv_cpu_die(unsigned int cpu)
  157. {
  158. struct hv_reenlightenment_control re_ctrl;
  159. unsigned int new_cpu;
  160. if (hv_reenlightenment_cb == NULL)
  161. return 0;
  162. rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  163. if (re_ctrl.target_vp == hv_vp_index[cpu]) {
  164. /* Reassign to some other online CPU */
  165. new_cpu = cpumask_any_but(cpu_online_mask, cpu);
  166. re_ctrl.target_vp = hv_vp_index[new_cpu];
  167. wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
  168. }
  169. return 0;
  170. }
  171. /*
  172. * This function is to be invoked early in the boot sequence after the
  173. * hypervisor has been detected.
  174. *
  175. * 1. Setup the hypercall page.
  176. * 2. Register Hyper-V specific clocksource.
  177. */
  178. void hyperv_init(void)
  179. {
  180. u64 guest_id, required_msrs;
  181. union hv_x64_msr_hypercall_contents hypercall_msr;
  182. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  183. return;
  184. /* Absolutely required MSRs */
  185. required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
  186. HV_X64_MSR_VP_INDEX_AVAILABLE;
  187. if ((ms_hyperv.features & required_msrs) != required_msrs)
  188. return;
  189. /* Allocate percpu VP index */
  190. hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
  191. GFP_KERNEL);
  192. if (!hv_vp_index)
  193. return;
  194. if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
  195. hv_cpu_init, hv_cpu_die) < 0)
  196. goto free_vp_index;
  197. /*
  198. * Setup the hypercall page and enable hypercalls.
  199. * 1. Register the guest ID
  200. * 2. Enable the hypercall and register the hypercall page
  201. */
  202. guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  203. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  204. hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
  205. if (hv_hypercall_pg == NULL) {
  206. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  207. goto free_vp_index;
  208. }
  209. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  210. hypercall_msr.enable = 1;
  211. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
  212. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  213. hyper_alloc_mmu();
  214. /*
  215. * Register Hyper-V specific clocksource.
  216. */
  217. #ifdef CONFIG_HYPERV_TSCPAGE
  218. if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
  219. union hv_x64_msr_hypercall_contents tsc_msr;
  220. tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  221. if (!tsc_pg)
  222. goto register_msr_cs;
  223. hyperv_cs = &hyperv_cs_tsc;
  224. rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  225. tsc_msr.enable = 1;
  226. tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
  227. wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  228. hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
  229. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  230. return;
  231. }
  232. register_msr_cs:
  233. #endif
  234. /*
  235. * For 32 bit guests just use the MSR based mechanism for reading
  236. * the partition counter.
  237. */
  238. hyperv_cs = &hyperv_cs_msr;
  239. if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
  240. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  241. return;
  242. free_vp_index:
  243. kfree(hv_vp_index);
  244. hv_vp_index = NULL;
  245. }
  246. /*
  247. * This routine is called before kexec/kdump, it does the required cleanup.
  248. */
  249. void hyperv_cleanup(void)
  250. {
  251. union hv_x64_msr_hypercall_contents hypercall_msr;
  252. /* Reset our OS id */
  253. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  254. /* Reset the hypercall page */
  255. hypercall_msr.as_uint64 = 0;
  256. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  257. /* Reset the TSC page */
  258. hypercall_msr.as_uint64 = 0;
  259. wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
  260. }
  261. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  262. void hyperv_report_panic(struct pt_regs *regs, long err)
  263. {
  264. static bool panic_reported;
  265. u64 guest_id;
  266. /*
  267. * We prefer to report panic on 'die' chain as we have proper
  268. * registers to report, but if we miss it (e.g. on BUG()) we need
  269. * to report it on 'panic'.
  270. */
  271. if (panic_reported)
  272. return;
  273. panic_reported = true;
  274. rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  275. wrmsrl(HV_X64_MSR_CRASH_P0, err);
  276. wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
  277. wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
  278. wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
  279. wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
  280. /*
  281. * Let Hyper-V know there is crash data available
  282. */
  283. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  284. }
  285. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  286. bool hv_is_hyperv_initialized(void)
  287. {
  288. union hv_x64_msr_hypercall_contents hypercall_msr;
  289. /*
  290. * Ensure that we're really on Hyper-V, and not a KVM or Xen
  291. * emulation of Hyper-V
  292. */
  293. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  294. return false;
  295. /*
  296. * Verify that earlier initialization succeeded by checking
  297. * that the hypercall page is setup
  298. */
  299. hypercall_msr.as_uint64 = 0;
  300. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  301. return hypercall_msr.enable;
  302. }
  303. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);