hv_init.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/hypervisor.h>
  21. #include <asm/hyperv.h>
  22. #include <asm/mshyperv.h>
  23. #include <linux/version.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mm.h>
  26. #include <linux/clockchips.h>
  27. #include <linux/hyperv.h>
  28. #include <linux/slab.h>
  29. #include <linux/cpuhotplug.h>
  30. #ifdef CONFIG_HYPERV_TSCPAGE
  31. static struct ms_hyperv_tsc_page *tsc_pg;
  32. struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
  33. {
  34. return tsc_pg;
  35. }
  36. static u64 read_hv_clock_tsc(struct clocksource *arg)
  37. {
  38. u64 current_tick = hv_read_tsc_page(tsc_pg);
  39. if (current_tick == U64_MAX)
  40. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  41. return current_tick;
  42. }
  43. static struct clocksource hyperv_cs_tsc = {
  44. .name = "hyperv_clocksource_tsc_page",
  45. .rating = 400,
  46. .read = read_hv_clock_tsc,
  47. .mask = CLOCKSOURCE_MASK(64),
  48. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  49. };
  50. #endif
  51. static u64 read_hv_clock_msr(struct clocksource *arg)
  52. {
  53. u64 current_tick;
  54. /*
  55. * Read the partition counter to get the current tick count. This count
  56. * is set to 0 when the partition is created and is incremented in
  57. * 100 nanosecond units.
  58. */
  59. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  60. return current_tick;
  61. }
  62. static struct clocksource hyperv_cs_msr = {
  63. .name = "hyperv_clocksource_msr",
  64. .rating = 400,
  65. .read = read_hv_clock_msr,
  66. .mask = CLOCKSOURCE_MASK(64),
  67. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  68. };
  69. void *hv_hypercall_pg;
  70. EXPORT_SYMBOL_GPL(hv_hypercall_pg);
  71. struct clocksource *hyperv_cs;
  72. EXPORT_SYMBOL_GPL(hyperv_cs);
  73. u32 *hv_vp_index;
  74. EXPORT_SYMBOL_GPL(hv_vp_index);
  75. u32 hv_max_vp_index;
  76. static int hv_cpu_init(unsigned int cpu)
  77. {
  78. u64 msr_vp_index;
  79. hv_get_vp_index(msr_vp_index);
  80. hv_vp_index[smp_processor_id()] = msr_vp_index;
  81. if (msr_vp_index > hv_max_vp_index)
  82. hv_max_vp_index = msr_vp_index;
  83. return 0;
  84. }
  85. /*
  86. * This function is to be invoked early in the boot sequence after the
  87. * hypervisor has been detected.
  88. *
  89. * 1. Setup the hypercall page.
  90. * 2. Register Hyper-V specific clocksource.
  91. */
  92. void hyperv_init(void)
  93. {
  94. u64 guest_id;
  95. union hv_x64_msr_hypercall_contents hypercall_msr;
  96. if (x86_hyper != &x86_hyper_ms_hyperv)
  97. return;
  98. /* Allocate percpu VP index */
  99. hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
  100. GFP_KERNEL);
  101. if (!hv_vp_index)
  102. return;
  103. if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
  104. hv_cpu_init, NULL) < 0)
  105. goto free_vp_index;
  106. /*
  107. * Setup the hypercall page and enable hypercalls.
  108. * 1. Register the guest ID
  109. * 2. Enable the hypercall and register the hypercall page
  110. */
  111. guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  112. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  113. hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
  114. if (hv_hypercall_pg == NULL) {
  115. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  116. goto free_vp_index;
  117. }
  118. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  119. hypercall_msr.enable = 1;
  120. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
  121. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  122. hyper_alloc_mmu();
  123. /*
  124. * Register Hyper-V specific clocksource.
  125. */
  126. #ifdef CONFIG_HYPERV_TSCPAGE
  127. if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
  128. union hv_x64_msr_hypercall_contents tsc_msr;
  129. tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  130. if (!tsc_pg)
  131. goto register_msr_cs;
  132. hyperv_cs = &hyperv_cs_tsc;
  133. rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  134. tsc_msr.enable = 1;
  135. tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
  136. wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  137. hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
  138. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  139. return;
  140. }
  141. register_msr_cs:
  142. #endif
  143. /*
  144. * For 32 bit guests just use the MSR based mechanism for reading
  145. * the partition counter.
  146. */
  147. hyperv_cs = &hyperv_cs_msr;
  148. if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
  149. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  150. return;
  151. free_vp_index:
  152. kfree(hv_vp_index);
  153. hv_vp_index = NULL;
  154. }
  155. /*
  156. * This routine is called before kexec/kdump, it does the required cleanup.
  157. */
  158. void hyperv_cleanup(void)
  159. {
  160. union hv_x64_msr_hypercall_contents hypercall_msr;
  161. /* Reset our OS id */
  162. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  163. /* Reset the hypercall page */
  164. hypercall_msr.as_uint64 = 0;
  165. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  166. /* Reset the TSC page */
  167. hypercall_msr.as_uint64 = 0;
  168. wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
  169. }
  170. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  171. void hyperv_report_panic(struct pt_regs *regs)
  172. {
  173. static bool panic_reported;
  174. /*
  175. * We prefer to report panic on 'die' chain as we have proper
  176. * registers to report, but if we miss it (e.g. on BUG()) we need
  177. * to report it on 'panic'.
  178. */
  179. if (panic_reported)
  180. return;
  181. panic_reported = true;
  182. wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
  183. wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
  184. wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
  185. wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
  186. wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
  187. /*
  188. * Let Hyper-V know there is crash data available
  189. */
  190. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  191. }
  192. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  193. bool hv_is_hypercall_page_setup(void)
  194. {
  195. union hv_x64_msr_hypercall_contents hypercall_msr;
  196. /* Check if the hypercall page is setup */
  197. hypercall_msr.as_uint64 = 0;
  198. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  199. if (!hypercall_msr.enable)
  200. return false;
  201. return true;
  202. }
  203. EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);