hv_init.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /*
  89. * This function is to be invoked early in the boot sequence after the
  90. * hypervisor has been detected.
  91. *
  92. * 1. Setup the hypercall page.
  93. * 2. Register Hyper-V specific clocksource.
  94. */
  95. void hyperv_init(void)
  96. {
  97. u64 guest_id;
  98. union hv_x64_msr_hypercall_contents hypercall_msr;
  99. if (x86_hyper != &x86_hyper_ms_hyperv)
  100. return;
  101. /*
  102. * Setup the hypercall page and enable hypercalls.
  103. * 1. Register the guest ID
  104. * 2. Enable the hypercall and register the hypercall page
  105. */
  106. guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  107. wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
  108. hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
  109. if (hypercall_pg == NULL) {
  110. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  111. return;
  112. }
  113. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  114. hypercall_msr.enable = 1;
  115. hypercall_msr.guest_physical_address = vmalloc_to_pfn(hypercall_pg);
  116. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  117. /*
  118. * Register Hyper-V specific clocksource.
  119. */
  120. #ifdef CONFIG_X86_64
  121. if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
  122. union hv_x64_msr_hypercall_contents tsc_msr;
  123. tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
  124. if (!tsc_pg) {
  125. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  126. return;
  127. }
  128. rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  129. tsc_msr.enable = 1;
  130. tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
  131. wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  132. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  133. return;
  134. }
  135. #endif
  136. /*
  137. * For 32 bit guests just use the MSR based mechanism for reading
  138. * the partition counter.
  139. */
  140. if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
  141. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  142. }
  143. /*
  144. * This routine is called before kexec/kdump, it does the required cleanup.
  145. */
  146. void hyperv_cleanup(void)
  147. {
  148. union hv_x64_msr_hypercall_contents hypercall_msr;
  149. /* Reset our OS id */
  150. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  151. /* Reset the hypercall page */
  152. hypercall_msr.as_uint64 = 0;
  153. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  154. }
  155. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  156. /*
  157. * hv_do_hypercall- Invoke the specified hypercall
  158. */
  159. u64 hv_do_hypercall(u64 control, void *input, void *output)
  160. {
  161. u64 input_address = (input) ? virt_to_phys(input) : 0;
  162. u64 output_address = (output) ? virt_to_phys(output) : 0;
  163. #ifdef CONFIG_X86_64
  164. u64 hv_status = 0;
  165. if (!hypercall_pg)
  166. return (u64)ULLONG_MAX;
  167. __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
  168. __asm__ __volatile__("call *%3" : "=a" (hv_status) :
  169. "c" (control), "d" (input_address),
  170. "m" (hypercall_pg));
  171. return hv_status;
  172. #else
  173. u32 control_hi = control >> 32;
  174. u32 control_lo = control & 0xFFFFFFFF;
  175. u32 hv_status_hi = 1;
  176. u32 hv_status_lo = 1;
  177. u32 input_address_hi = input_address >> 32;
  178. u32 input_address_lo = input_address & 0xFFFFFFFF;
  179. u32 output_address_hi = output_address >> 32;
  180. u32 output_address_lo = output_address & 0xFFFFFFFF;
  181. if (!hypercall_pg)
  182. return (u64)ULLONG_MAX;
  183. __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
  184. "=a"(hv_status_lo) : "d" (control_hi),
  185. "a" (control_lo), "b" (input_address_hi),
  186. "c" (input_address_lo), "D"(output_address_hi),
  187. "S"(output_address_lo), "m" (hypercall_pg));
  188. return hv_status_lo | ((u64)hv_status_hi << 32);
  189. #endif /* !x86_64 */
  190. }
  191. EXPORT_SYMBOL_GPL(hv_do_hypercall);
  192. void hyperv_report_panic(struct pt_regs *regs)
  193. {
  194. static bool panic_reported;
  195. /*
  196. * We prefer to report panic on 'die' chain as we have proper
  197. * registers to report, but if we miss it (e.g. on BUG()) we need
  198. * to report it on 'panic'.
  199. */
  200. if (panic_reported)
  201. return;
  202. panic_reported = true;
  203. wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
  204. wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
  205. wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
  206. wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
  207. wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
  208. /*
  209. * Let Hyper-V know there is crash data available
  210. */
  211. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  212. }
  213. EXPORT_SYMBOL_GPL(hyperv_report_panic);
  214. bool hv_is_hypercall_page_setup(void)
  215. {
  216. union hv_x64_msr_hypercall_contents hypercall_msr;
  217. /* Check if the hypercall page is setup */
  218. hypercall_msr.as_uint64 = 0;
  219. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  220. if (!hypercall_msr.enable)
  221. return false;
  222. return true;
  223. }
  224. EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);