hv_common.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Architecture neutral utility routines for interacting with
  4. * Hyper-V. This file is specifically for code that must be
  5. * built-in to the kernel image when CONFIG_HYPERV is set
  6. * (vs. being in a module) because it is called from architecture
  7. * specific code under arch/.
  8. *
  9. * Copyright (C) 2021, Microsoft, Inc.
  10. *
  11. * Author : Michael Kelley <[email protected]>
  12. */
  13. #include <linux/types.h>
  14. #include <linux/acpi.h>
  15. #include <linux/export.h>
  16. #include <linux/bitfield.h>
  17. #include <linux/cpumask.h>
  18. #include <linux/panic_notifier.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/slab.h>
  21. #include <linux/dma-map-ops.h>
  22. #include <asm/hyperv-tlfs.h>
  23. #include <asm/mshyperv.h>
  24. /*
  25. * hv_root_partition and ms_hyperv are defined here with other Hyper-V
  26. * specific globals so they are shared across all architectures and are
  27. * built only when CONFIG_HYPERV is defined. But on x86,
  28. * ms_hyperv_init_platform() is built even when CONFIG_HYPERV is not
  29. * defined, and it uses these two variables. So mark them as __weak
  30. * here, allowing for an overriding definition in the module containing
  31. * ms_hyperv_init_platform().
  32. */
  33. bool __weak hv_root_partition;
  34. EXPORT_SYMBOL_GPL(hv_root_partition);
  35. struct ms_hyperv_info __weak ms_hyperv;
  36. EXPORT_SYMBOL_GPL(ms_hyperv);
  37. u32 *hv_vp_index;
  38. EXPORT_SYMBOL_GPL(hv_vp_index);
  39. u32 hv_max_vp_index;
  40. EXPORT_SYMBOL_GPL(hv_max_vp_index);
  41. void * __percpu *hyperv_pcpu_input_arg;
  42. EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
  43. void * __percpu *hyperv_pcpu_output_arg;
  44. EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
  45. /*
  46. * Hyper-V specific initialization and shutdown code that is
  47. * common across all architectures. Called from architecture
  48. * specific initialization functions.
  49. */
  50. void __init hv_common_free(void)
  51. {
  52. kfree(hv_vp_index);
  53. hv_vp_index = NULL;
  54. free_percpu(hyperv_pcpu_output_arg);
  55. hyperv_pcpu_output_arg = NULL;
  56. free_percpu(hyperv_pcpu_input_arg);
  57. hyperv_pcpu_input_arg = NULL;
  58. }
  59. int __init hv_common_init(void)
  60. {
  61. int i;
  62. /*
  63. * Hyper-V expects to get crash register data or kmsg when
  64. * crash enlightment is available and system crashes. Set
  65. * crash_kexec_post_notifiers to be true to make sure that
  66. * calling crash enlightment interface before running kdump
  67. * kernel.
  68. */
  69. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  70. crash_kexec_post_notifiers = true;
  71. pr_info("Hyper-V: enabling crash_kexec_post_notifiers\n");
  72. }
  73. /*
  74. * Allocate the per-CPU state for the hypercall input arg.
  75. * If this allocation fails, we will not be able to setup
  76. * (per-CPU) hypercall input page and thus this failure is
  77. * fatal on Hyper-V.
  78. */
  79. hyperv_pcpu_input_arg = alloc_percpu(void *);
  80. BUG_ON(!hyperv_pcpu_input_arg);
  81. /* Allocate the per-CPU state for output arg for root */
  82. if (hv_root_partition) {
  83. hyperv_pcpu_output_arg = alloc_percpu(void *);
  84. BUG_ON(!hyperv_pcpu_output_arg);
  85. }
  86. hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
  87. GFP_KERNEL);
  88. if (!hv_vp_index) {
  89. hv_common_free();
  90. return -ENOMEM;
  91. }
  92. for (i = 0; i < num_possible_cpus(); i++)
  93. hv_vp_index[i] = VP_INVAL;
  94. return 0;
  95. }
  96. /*
  97. * Hyper-V specific initialization and die code for
  98. * individual CPUs that is common across all architectures.
  99. * Called by the CPU hotplug mechanism.
  100. */
  101. int hv_common_cpu_init(unsigned int cpu)
  102. {
  103. void **inputarg, **outputarg;
  104. u64 msr_vp_index;
  105. gfp_t flags;
  106. int pgcount = hv_root_partition ? 2 : 1;
  107. /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
  108. flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL;
  109. inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  110. *inputarg = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
  111. if (!(*inputarg))
  112. return -ENOMEM;
  113. if (hv_root_partition) {
  114. outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
  115. *outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE;
  116. }
  117. msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);
  118. hv_vp_index[cpu] = msr_vp_index;
  119. if (msr_vp_index > hv_max_vp_index)
  120. hv_max_vp_index = msr_vp_index;
  121. return 0;
  122. }
  123. int hv_common_cpu_die(unsigned int cpu)
  124. {
  125. unsigned long flags;
  126. void **inputarg, **outputarg;
  127. void *mem;
  128. local_irq_save(flags);
  129. inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
  130. mem = *inputarg;
  131. *inputarg = NULL;
  132. if (hv_root_partition) {
  133. outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
  134. *outputarg = NULL;
  135. }
  136. local_irq_restore(flags);
  137. kfree(mem);
  138. return 0;
  139. }
  140. /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */
  141. bool hv_query_ext_cap(u64 cap_query)
  142. {
  143. /*
  144. * The address of the 'hv_extended_cap' variable will be used as an
  145. * output parameter to the hypercall below and so it should be
  146. * compatible with 'virt_to_phys'. Which means, it's address should be
  147. * directly mapped. Use 'static' to keep it compatible; stack variables
  148. * can be virtually mapped, making them incompatible with
  149. * 'virt_to_phys'.
  150. * Hypercall input/output addresses should also be 8-byte aligned.
  151. */
  152. static u64 hv_extended_cap __aligned(8);
  153. static bool hv_extended_cap_queried;
  154. u64 status;
  155. /*
  156. * Querying extended capabilities is an extended hypercall. Check if the
  157. * partition supports extended hypercall, first.
  158. */
  159. if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS))
  160. return false;
  161. /* Extended capabilities do not change at runtime. */
  162. if (hv_extended_cap_queried)
  163. return hv_extended_cap & cap_query;
  164. status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL,
  165. &hv_extended_cap);
  166. /*
  167. * The query extended capabilities hypercall should not fail under
  168. * any normal circumstances. Avoid repeatedly making the hypercall, on
  169. * error.
  170. */
  171. hv_extended_cap_queried = true;
  172. if (!hv_result_success(status)) {
  173. pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n",
  174. status);
  175. return false;
  176. }
  177. return hv_extended_cap & cap_query;
  178. }
  179. EXPORT_SYMBOL_GPL(hv_query_ext_cap);
  180. void hv_setup_dma_ops(struct device *dev, bool coherent)
  181. {
  182. /*
  183. * Hyper-V does not offer a vIOMMU in the guest
  184. * VM, so pass 0/NULL for the IOMMU settings
  185. */
  186. arch_setup_dma_ops(dev, 0, 0, NULL, coherent);
  187. }
  188. EXPORT_SYMBOL_GPL(hv_setup_dma_ops);
  189. bool hv_is_hibernation_supported(void)
  190. {
  191. return !hv_root_partition && acpi_sleep_state_supported(ACPI_STATE_S4);
  192. }
  193. EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
  194. /*
  195. * Default function to read the Hyper-V reference counter, independent
  196. * of whether Hyper-V enlightened clocks/timers are being used. But on
  197. * architectures where it is used, Hyper-V enlightenment code in
  198. * hyperv_timer.c may override this function.
  199. */
  200. static u64 __hv_read_ref_counter(void)
  201. {
  202. return hv_get_register(HV_REGISTER_TIME_REF_COUNT);
  203. }
  204. u64 (*hv_read_reference_counter)(void) = __hv_read_ref_counter;
  205. EXPORT_SYMBOL_GPL(hv_read_reference_counter);
  206. /* These __weak functions provide default "no-op" behavior and
  207. * may be overridden by architecture specific versions. Architectures
  208. * for which the default "no-op" behavior is sufficient can leave
  209. * them unimplemented and not be cluttered with a bunch of stub
  210. * functions in arch-specific code.
  211. */
  212. bool __weak hv_is_isolation_supported(void)
  213. {
  214. return false;
  215. }
  216. EXPORT_SYMBOL_GPL(hv_is_isolation_supported);
  217. bool __weak hv_isolation_type_snp(void)
  218. {
  219. return false;
  220. }
  221. EXPORT_SYMBOL_GPL(hv_isolation_type_snp);
  222. void __weak hv_setup_vmbus_handler(void (*handler)(void))
  223. {
  224. }
  225. EXPORT_SYMBOL_GPL(hv_setup_vmbus_handler);
  226. void __weak hv_remove_vmbus_handler(void)
  227. {
  228. }
  229. EXPORT_SYMBOL_GPL(hv_remove_vmbus_handler);
  230. void __weak hv_setup_kexec_handler(void (*handler)(void))
  231. {
  232. }
  233. EXPORT_SYMBOL_GPL(hv_setup_kexec_handler);
  234. void __weak hv_remove_kexec_handler(void)
  235. {
  236. }
  237. EXPORT_SYMBOL_GPL(hv_remove_kexec_handler);
  238. void __weak hv_setup_crash_handler(void (*handler)(struct pt_regs *regs))
  239. {
  240. }
  241. EXPORT_SYMBOL_GPL(hv_setup_crash_handler);
  242. void __weak hv_remove_crash_handler(void)
  243. {
  244. }
  245. EXPORT_SYMBOL_GPL(hv_remove_crash_handler);
  246. void __weak hyperv_cleanup(void)
  247. {
  248. }
  249. EXPORT_SYMBOL_GPL(hyperv_cleanup);
  250. u64 __weak hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
  251. {
  252. return HV_STATUS_INVALID_PARAMETER;
  253. }
  254. EXPORT_SYMBOL_GPL(hv_ghcb_hypercall);
  255. void __weak *hv_map_memory(void *addr, unsigned long size)
  256. {
  257. return NULL;
  258. }
  259. EXPORT_SYMBOL_GPL(hv_map_memory);
  260. void __weak hv_unmap_memory(void *addr)
  261. {
  262. }
  263. EXPORT_SYMBOL_GPL(hv_unmap_memory);