mshyperv.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Linux-specific definitions for managing interactions with Microsoft's
  4. * Hyper-V hypervisor. The definitions in this file are architecture
  5. * independent. See arch/<arch>/include/asm/mshyperv.h for definitions
  6. * that are specific to architecture <arch>.
  7. *
  8. * Definitions that are specified in the Hyper-V Top Level Functional
  9. * Spec (TLFS) should not go in this file, but should instead go in
  10. * hyperv-tlfs.h.
  11. *
  12. * Copyright (C) 2019, Microsoft, Inc.
  13. *
  14. * Author : Michael Kelley <mikelley@microsoft.com>
  15. */
  16. #ifndef _ASM_GENERIC_MSHYPERV_H
  17. #define _ASM_GENERIC_MSHYPERV_H
  18. #include <linux/types.h>
  19. #include <linux/atomic.h>
  20. #include <linux/bitops.h>
  21. #include <acpi/acpi_numa.h>
  22. #include <linux/cpumask.h>
  23. #include <linux/nmi.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/hyperv-tlfs.h>
  26. #define VTPM_BASE_ADDRESS 0xfed40000
  27. struct ms_hyperv_info {
  28. u32 features;
  29. u32 priv_high;
  30. u32 misc_features;
  31. u32 hints;
  32. u32 nested_features;
  33. u32 max_vp_index;
  34. u32 max_lp_index;
  35. u8 vtl;
  36. union {
  37. u32 isolation_config_a;
  38. struct {
  39. u32 paravisor_present : 1;
  40. u32 reserved_a1 : 31;
  41. };
  42. };
  43. union {
  44. u32 isolation_config_b;
  45. struct {
  46. u32 cvm_type : 4;
  47. u32 reserved_b1 : 1;
  48. u32 shared_gpa_boundary_active : 1;
  49. u32 shared_gpa_boundary_bits : 6;
  50. u32 reserved_b2 : 20;
  51. };
  52. };
  53. u64 shared_gpa_boundary;
  54. };
  55. extern struct ms_hyperv_info ms_hyperv;
  56. extern bool hv_nested;
  57. extern void * __percpu *hyperv_pcpu_input_arg;
  58. extern void * __percpu *hyperv_pcpu_output_arg;
  59. extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
  60. extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
  61. bool hv_isolation_type_snp(void);
  62. bool hv_isolation_type_tdx(void);
  63. static inline struct hv_proximity_domain_info hv_numa_node_to_pxm_info(int node)
  64. {
  65. struct hv_proximity_domain_info pxm_info = {};
  66. if (node != NUMA_NO_NODE) {
  67. pxm_info.domain_id = node_to_pxm(node);
  68. pxm_info.flags.proximity_info_valid = 1;
  69. pxm_info.flags.proximity_preferred = 1;
  70. }
  71. return pxm_info;
  72. }
  73. /* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */
  74. static inline int hv_result(u64 status)
  75. {
  76. return status & HV_HYPERCALL_RESULT_MASK;
  77. }
  78. static inline bool hv_result_success(u64 status)
  79. {
  80. return hv_result(status) == HV_STATUS_SUCCESS;
  81. }
  82. static inline unsigned int hv_repcomp(u64 status)
  83. {
  84. /* Bits [43:32] of status have 'Reps completed' data. */
  85. return (status & HV_HYPERCALL_REP_COMP_MASK) >>
  86. HV_HYPERCALL_REP_COMP_OFFSET;
  87. }
  88. /*
  89. * Rep hypercalls. Callers of this functions are supposed to ensure that
  90. * rep_count and varhead_size comply with Hyper-V hypercall definition.
  91. */
  92. static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
  93. void *input, void *output)
  94. {
  95. u64 control = code;
  96. u64 status;
  97. u16 rep_comp;
  98. control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
  99. control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
  100. do {
  101. status = hv_do_hypercall(control, input, output);
  102. if (!hv_result_success(status))
  103. return status;
  104. rep_comp = hv_repcomp(status);
  105. control &= ~HV_HYPERCALL_REP_START_MASK;
  106. control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
  107. touch_nmi_watchdog();
  108. } while (rep_comp < rep_count);
  109. return status;
  110. }
  111. /* Generate the guest OS identifier as described in the Hyper-V TLFS */
  112. static inline u64 hv_generate_guest_id(u64 kernel_version)
  113. {
  114. u64 guest_id;
  115. guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
  116. guest_id |= (kernel_version << 16);
  117. return guest_id;
  118. }
  119. /* Free the message slot and signal end-of-message if required */
  120. static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
  121. {
  122. /*
  123. * On crash we're reading some other CPU's message page and we need
  124. * to be careful: this other CPU may already had cleared the header
  125. * and the host may already had delivered some other message there.
  126. * In case we blindly write msg->header.message_type we're going
  127. * to lose it. We can still lose a message of the same type but
  128. * we count on the fact that there can only be one
  129. * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
  130. * on crash.
  131. */
  132. if (cmpxchg(&msg->header.message_type, old_msg_type,
  133. HVMSG_NONE) != old_msg_type)
  134. return;
  135. /*
  136. * The cmxchg() above does an implicit memory barrier to
  137. * ensure the write to MessageType (ie set to
  138. * HVMSG_NONE) happens before we read the
  139. * MessagePending and EOMing. Otherwise, the EOMing
  140. * will not deliver any more messages since there is
  141. * no empty slot
  142. */
  143. if (msg->header.message_flags.msg_pending) {
  144. /*
  145. * This will cause message queue rescan to
  146. * possibly deliver another msg from the
  147. * hypervisor
  148. */
  149. hv_set_msr(HV_MSR_EOM, 0);
  150. }
  151. }
  152. int hv_get_hypervisor_version(union hv_hypervisor_version_info *info);
  153. void hv_setup_vmbus_handler(void (*handler)(void));
  154. void hv_remove_vmbus_handler(void);
  155. void hv_setup_stimer0_handler(void (*handler)(void));
  156. void hv_remove_stimer0_handler(void);
  157. void hv_setup_kexec_handler(void (*handler)(void));
  158. void hv_remove_kexec_handler(void);
  159. void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
  160. void hv_remove_crash_handler(void);
  161. extern int vmbus_interrupt;
  162. extern int vmbus_irq;
  163. extern bool hv_root_partition;
  164. #if IS_ENABLED(CONFIG_HYPERV)
  165. /*
  166. * Hypervisor's notion of virtual processor ID is different from
  167. * Linux' notion of CPU ID. This information can only be retrieved
  168. * in the context of the calling CPU. Setup a map for easy access
  169. * to this information.
  170. */
  171. extern u32 *hv_vp_index;
  172. extern u32 hv_max_vp_index;
  173. extern u64 (*hv_read_reference_counter)(void);
  174. /* Sentinel value for an uninitialized entry in hv_vp_index array */
  175. #define VP_INVAL U32_MAX
  176. int __init hv_common_init(void);
  177. void __init hv_common_free(void);
  178. void __init ms_hyperv_late_init(void);
  179. int hv_common_cpu_init(unsigned int cpu);
  180. int hv_common_cpu_die(unsigned int cpu);
  181. void *hv_alloc_hyperv_page(void);
  182. void *hv_alloc_hyperv_zeroed_page(void);
  183. void hv_free_hyperv_page(void *addr);
  184. /**
  185. * hv_cpu_number_to_vp_number() - Map CPU to VP.
  186. * @cpu_number: CPU number in Linux terms
  187. *
  188. * This function returns the mapping between the Linux processor
  189. * number and the hypervisor's virtual processor number, useful
  190. * in making hypercalls and such that talk about specific
  191. * processors.
  192. *
  193. * Return: Virtual processor number in Hyper-V terms
  194. */
  195. static inline int hv_cpu_number_to_vp_number(int cpu_number)
  196. {
  197. return hv_vp_index[cpu_number];
  198. }
  199. static inline int __cpumask_to_vpset(struct hv_vpset *vpset,
  200. const struct cpumask *cpus,
  201. bool (*func)(int cpu))
  202. {
  203. int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
  204. int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK;
  205. /* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */
  206. if (max_vcpu_bank >= HV_MAX_SPARSE_VCPU_BANKS)
  207. return 0;
  208. /*
  209. * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
  210. * structs are not cleared between calls, we risk flushing unneeded
  211. * vCPUs otherwise.
  212. */
  213. for (vcpu_bank = 0; vcpu_bank <= max_vcpu_bank; vcpu_bank++)
  214. vpset->bank_contents[vcpu_bank] = 0;
  215. /*
  216. * Some banks may end up being empty but this is acceptable.
  217. */
  218. for_each_cpu(cpu, cpus) {
  219. if (func && func(cpu))
  220. continue;
  221. vcpu = hv_cpu_number_to_vp_number(cpu);
  222. if (vcpu == VP_INVAL)
  223. return -1;
  224. vcpu_bank = vcpu / HV_VCPUS_PER_SPARSE_BANK;
  225. vcpu_offset = vcpu % HV_VCPUS_PER_SPARSE_BANK;
  226. __set_bit(vcpu_offset, (unsigned long *)
  227. &vpset->bank_contents[vcpu_bank]);
  228. if (vcpu_bank >= nr_bank)
  229. nr_bank = vcpu_bank + 1;
  230. }
  231. vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
  232. return nr_bank;
  233. }
  234. /*
  235. * Convert a Linux cpumask into a Hyper-V VPset. In the _skip variant,
  236. * 'func' is called for each CPU present in cpumask. If 'func' returns
  237. * true, that CPU is skipped -- i.e., that CPU from cpumask is *not*
  238. * added to the Hyper-V VPset. If 'func' is NULL, no CPUs are
  239. * skipped.
  240. */
  241. static inline int cpumask_to_vpset(struct hv_vpset *vpset,
  242. const struct cpumask *cpus)
  243. {
  244. return __cpumask_to_vpset(vpset, cpus, NULL);
  245. }
  246. static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
  247. const struct cpumask *cpus,
  248. bool (*func)(int cpu))
  249. {
  250. return __cpumask_to_vpset(vpset, cpus, func);
  251. }
  252. void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
  253. bool hv_is_hyperv_initialized(void);
  254. bool hv_is_hibernation_supported(void);
  255. enum hv_isolation_type hv_get_isolation_type(void);
  256. bool hv_is_isolation_supported(void);
  257. bool hv_isolation_type_snp(void);
  258. u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
  259. u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
  260. void hyperv_cleanup(void);
  261. bool hv_query_ext_cap(u64 cap_query);
  262. void hv_setup_dma_ops(struct device *dev, bool coherent);
  263. #else /* CONFIG_HYPERV */
  264. static inline bool hv_is_hyperv_initialized(void) { return false; }
  265. static inline bool hv_is_hibernation_supported(void) { return false; }
  266. static inline void hyperv_cleanup(void) {}
  267. static inline void ms_hyperv_late_init(void) {}
  268. static inline bool hv_is_isolation_supported(void) { return false; }
  269. static inline enum hv_isolation_type hv_get_isolation_type(void)
  270. {
  271. return HV_ISOLATION_TYPE_NONE;
  272. }
  273. #endif /* CONFIG_HYPERV */
  274. #endif