hv_init.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #ifdef CONFIG_X86_64
  28. static struct ms_hyperv_tsc_page *tsc_pg;
  29. static u64 read_hv_clock_tsc(struct clocksource *arg)
  30. {
  31. u64 current_tick;
  32. if (tsc_pg->tsc_sequence != 0) {
  33. /*
  34. * Use the tsc page to compute the value.
  35. */
  36. while (1) {
  37. u64 tmp;
  38. u32 sequence = tsc_pg->tsc_sequence;
  39. u64 cur_tsc;
  40. u64 scale = tsc_pg->tsc_scale;
  41. s64 offset = tsc_pg->tsc_offset;
  42. rdtscll(cur_tsc);
  43. /* current_tick = ((cur_tsc *scale) >> 64) + offset */
  44. asm("mulq %3"
  45. : "=d" (current_tick), "=a" (tmp)
  46. : "a" (cur_tsc), "r" (scale));
  47. current_tick += offset;
  48. if (tsc_pg->tsc_sequence == sequence)
  49. return current_tick;
  50. if (tsc_pg->tsc_sequence != 0)
  51. continue;
  52. /*
  53. * Fallback using MSR method.
  54. */
  55. break;
  56. }
  57. }
  58. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  59. return current_tick;
  60. }
  61. static struct clocksource hyperv_cs_tsc = {
  62. .name = "hyperv_clocksource_tsc_page",
  63. .rating = 400,
  64. .read = read_hv_clock_tsc,
  65. .mask = CLOCKSOURCE_MASK(64),
  66. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  67. };
  68. #endif
  69. static u64 read_hv_clock_msr(struct clocksource *arg)
  70. {
  71. u64 current_tick;
  72. /*
  73. * Read the partition counter to get the current tick count. This count
  74. * is set to 0 when the partition is created and is incremented in
  75. * 100 nanosecond units.
  76. */
  77. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  78. return current_tick;
  79. }
  80. static struct clocksource hyperv_cs_msr = {
  81. .name = "hyperv_clocksource_msr",
  82. .rating = 400,
  83. .read = read_hv_clock_msr,
  84. .mask = CLOCKSOURCE_MASK(64),
  85. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  86. };
  87. static void *hypercall_pg;
  88. struct clocksource *hyperv_cs;
  89. EXPORT_SYMBOL_GPL(hyperv_cs);
  90. /*
  91. * This function is to be invoked early in the boot sequence after the
  92. * hypervisor has been detected.
  93. *
  94. * 1. Setup the hypercall page.
  95. * 2. Register Hyper-V specific clocksource.
  96. */
  97. void hyperv_init(void)
  98. {
  99. u64 guest_id;
  100. union hv_x64_msr_hypercall_contents hypercall_msr;
  101. if (x86_hyper != &x86_hyper_ms_hyperv)
  102. return;
  103. /*
  104. * Setup the hypercall page and enable hypercalls.
  105. * 1. Register the guest ID
  106. * 2. Enable the hypercall and register the hypercall page
  107. */
  108. guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  109. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  110. hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
  111. if (hypercall_pg == NULL) {
  112. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  113. return;
  114. }
  115. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  116. hypercall_msr.enable = 1;
  117. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hypercall_pg);
  118. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  119. /*
  120. * Register Hyper-V specific clocksource.
  121. */
  122. #ifdef CONFIG_X86_64
  123. if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
  124. union hv_x64_msr_hypercall_contents tsc_msr;
  125. tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  126. if (!tsc_pg)
  127. goto register_msr_cs;
  128. hyperv_cs = &hyperv_cs_tsc;
  129. rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  130. tsc_msr.enable = 1;
  131. tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
  132. wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  133. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  134. return;
  135. }
  136. register_msr_cs:
  137. #endif
  138. /*
  139. * For 32 bit guests just use the MSR based mechanism for reading
  140. * the partition counter.
  141. */
  142. hyperv_cs = &hyperv_cs_msr;
  143. if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
  144. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  145. }
  146. /*
  147. * This routine is called before kexec/kdump, it does the required cleanup.
  148. */
  149. void hyperv_cleanup(void)
  150. {
  151. union hv_x64_msr_hypercall_contents hypercall_msr;
  152. /* Reset our OS id */
  153. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  154. /* Reset the hypercall page */
  155. hypercall_msr.as_uint64 = 0;
  156. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  157. /* Reset the TSC page */
  158. hypercall_msr.as_uint64 = 0;
  159. wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
  160. }
  161. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  162. /*
  163. * hv_do_hypercall- Invoke the specified hypercall
  164. */
  165. u64 hv_do_hypercall(u64 control, void *input, void *output)
  166. {
  167. u64 input_address = (input) ? virt_to_phys(input) : 0;
  168. u64 output_address = (output) ? virt_to_phys(output) : 0;
  169. #ifdef CONFIG_X86_64
  170. u64 hv_status = 0;
  171. if (!hypercall_pg)
  172. return (u64)ULLONG_MAX;
  173. __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
  174. __asm__ __volatile__("call *%3" : "=a" (hv_status) :
  175. "c" (control), "d" (input_address),
  176. "m" (hypercall_pg));
  177. return hv_status;
  178. #else
  179. u32 control_hi = control >> 32;
  180. u32 control_lo = control & 0xFFFFFFFF;
  181. u32 hv_status_hi = 1;
  182. u32 hv_status_lo = 1;
  183. u32 input_address_hi = input_address >> 32;
  184. u32 input_address_lo = input_address & 0xFFFFFFFF;
  185. u32 output_address_hi = output_address >> 32;
  186. u32 output_address_lo = output_address & 0xFFFFFFFF;
  187. if (!hypercall_pg)
  188. return (u64)ULLONG_MAX;
  189. __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
  190. "=a"(hv_status_lo) : "d" (control_hi),
  191. "a" (control_lo), "b" (input_address_hi),
  192. "c" (input_address_lo), "D"(output_address_hi),
  193. "S"(output_address_lo), "m" (hypercall_pg));
  194. return hv_status_lo | ((u64)hv_status_hi << 32);
  195. #endif /* !x86_64 */
  196. }
  197. EXPORT_SYMBOL_GPL(hv_do_hypercall);
  198. void hyperv_report_panic(struct pt_regs *regs)
  199. {
  200. static bool panic_reported;
  201. /*
  202. * We prefer to report panic on 'die' chain as we have proper
  203. * registers to report, but if we miss it (e.g. on BUG()) we need
  204. * to report it on 'panic'.
  205. */
  206. if (panic_reported)
  207. return;
  208. panic_reported = true;
  209. wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
  210. wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
  211. wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
  212. wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
  213. wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
  214. /*
  215. * Let Hyper-V know there is crash data available
  216. */
  217. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  218. }
  219. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  220. bool hv_is_hypercall_page_setup(void)
  221. {
  222. union hv_x64_msr_hypercall_contents hypercall_msr;
  223. /* Check if the hypercall page is setup */
  224. hypercall_msr.as_uint64 = 0;
  225. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  226. if (!hypercall_msr.enable)
  227. return false;
  228. return true;
  229. }
  230. EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);