hv_init.c 6.5 KB

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