mshyperv.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <[email protected]>
  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 <linux/cpumask.h>
  22. #include <linux/nmi.h>
  23. #include <asm/ptrace.h>
  24. #include <asm/hyperv-tlfs.h>
  25. struct ms_hyperv_info {
  26. u32 features;
  27. u32 priv_high;
  28. u32 misc_features;
  29. u32 hints;
  30. u32 nested_features;
  31. u32 max_vp_index;
  32. u32 max_lp_index;
  33. u32 isolation_config_a;
  34. union {
  35. u32 isolation_config_b;
  36. struct {
  37. u32 cvm_type : 4;
  38. u32 reserved1 : 1;
  39. u32 shared_gpa_boundary_active : 1;
  40. u32 shared_gpa_boundary_bits : 6;
  41. u32 reserved2 : 20;
  42. };
  43. };
  44. u64 shared_gpa_boundary;
  45. };
  46. extern struct ms_hyperv_info ms_hyperv;
  47. extern void * __percpu *hyperv_pcpu_input_arg;
  48. extern void * __percpu *hyperv_pcpu_output_arg;
  49. extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
  50. extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
  51. extern bool hv_isolation_type_snp(void);
  52. /* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */
  53. static inline int hv_result(u64 status)
  54. {
  55. return status & HV_HYPERCALL_RESULT_MASK;
  56. }
  57. static inline bool hv_result_success(u64 status)
  58. {
  59. return hv_result(status) == HV_STATUS_SUCCESS;
  60. }
  61. static inline unsigned int hv_repcomp(u64 status)
  62. {
  63. /* Bits [43:32] of status have 'Reps completed' data. */
  64. return (status & HV_HYPERCALL_REP_COMP_MASK) >>
  65. HV_HYPERCALL_REP_COMP_OFFSET;
  66. }
  67. /*
  68. * Rep hypercalls. Callers of this functions are supposed to ensure that
  69. * rep_count and varhead_size comply with Hyper-V hypercall definition.
  70. */
  71. static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
  72. void *input, void *output)
  73. {
  74. u64 control = code;
  75. u64 status;
  76. u16 rep_comp;
  77. control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
  78. control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
  79. do {
  80. status = hv_do_hypercall(control, input, output);
  81. if (!hv_result_success(status))
  82. return status;
  83. rep_comp = hv_repcomp(status);
  84. control &= ~HV_HYPERCALL_REP_START_MASK;
  85. control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
  86. touch_nmi_watchdog();
  87. } while (rep_comp < rep_count);
  88. return status;
  89. }
  90. /* Generate the guest OS identifier as described in the Hyper-V TLFS */
  91. static inline u64 hv_generate_guest_id(u64 kernel_version)
  92. {
  93. u64 guest_id;
  94. guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
  95. guest_id |= (kernel_version << 16);
  96. return guest_id;
  97. }
  98. /* Free the message slot and signal end-of-message if required */
  99. static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
  100. {
  101. /*
  102. * On crash we're reading some other CPU's message page and we need
  103. * to be careful: this other CPU may already had cleared the header
  104. * and the host may already had delivered some other message there.
  105. * In case we blindly write msg->header.message_type we're going
  106. * to lose it. We can still lose a message of the same type but
  107. * we count on the fact that there can only be one
  108. * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
  109. * on crash.
  110. */
  111. if (cmpxchg(&msg->header.message_type, old_msg_type,
  112. HVMSG_NONE) != old_msg_type)
  113. return;
  114. /*
  115. * The cmxchg() above does an implicit memory barrier to
  116. * ensure the write to MessageType (ie set to
  117. * HVMSG_NONE) happens before we read the
  118. * MessagePending and EOMing. Otherwise, the EOMing
  119. * will not deliver any more messages since there is
  120. * no empty slot
  121. */
  122. if (msg->header.message_flags.msg_pending) {
  123. /*
  124. * This will cause message queue rescan to
  125. * possibly deliver another msg from the
  126. * hypervisor
  127. */
  128. hv_set_register(HV_REGISTER_EOM, 0);
  129. }
  130. }
  131. void hv_setup_vmbus_handler(void (*handler)(void));
  132. void hv_remove_vmbus_handler(void);
  133. void hv_setup_stimer0_handler(void (*handler)(void));
  134. void hv_remove_stimer0_handler(void);
  135. void hv_setup_kexec_handler(void (*handler)(void));
  136. void hv_remove_kexec_handler(void);
  137. void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
  138. void hv_remove_crash_handler(void);
  139. extern int vmbus_interrupt;
  140. extern int vmbus_irq;
  141. extern bool hv_root_partition;
  142. #if IS_ENABLED(CONFIG_HYPERV)
  143. /*
  144. * Hypervisor's notion of virtual processor ID is different from
  145. * Linux' notion of CPU ID. This information can only be retrieved
  146. * in the context of the calling CPU. Setup a map for easy access
  147. * to this information.
  148. */
  149. extern u32 *hv_vp_index;
  150. extern u32 hv_max_vp_index;
  151. extern u64 (*hv_read_reference_counter)(void);
  152. /* Sentinel value for an uninitialized entry in hv_vp_index array */
  153. #define VP_INVAL U32_MAX
  154. int __init hv_common_init(void);
  155. void __init hv_common_free(void);
  156. int hv_common_cpu_init(unsigned int cpu);
  157. int hv_common_cpu_die(unsigned int cpu);
  158. void *hv_alloc_hyperv_page(void);
  159. void *hv_alloc_hyperv_zeroed_page(void);
  160. void hv_free_hyperv_page(unsigned long addr);
  161. /**
  162. * hv_cpu_number_to_vp_number() - Map CPU to VP.
  163. * @cpu_number: CPU number in Linux terms
  164. *
  165. * This function returns the mapping between the Linux processor
  166. * number and the hypervisor's virtual processor number, useful
  167. * in making hypercalls and such that talk about specific
  168. * processors.
  169. *
  170. * Return: Virtual processor number in Hyper-V terms
  171. */
  172. static inline int hv_cpu_number_to_vp_number(int cpu_number)
  173. {
  174. return hv_vp_index[cpu_number];
  175. }
  176. static inline int __cpumask_to_vpset(struct hv_vpset *vpset,
  177. const struct cpumask *cpus,
  178. bool exclude_self)
  179. {
  180. int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
  181. int this_cpu = smp_processor_id();
  182. /* valid_bank_mask can represent up to 64 banks */
  183. if (hv_max_vp_index / 64 >= 64)
  184. return 0;
  185. /*
  186. * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
  187. * structs are not cleared between calls, we risk flushing unneeded
  188. * vCPUs otherwise.
  189. */
  190. for (vcpu_bank = 0; vcpu_bank <= hv_max_vp_index / 64; vcpu_bank++)
  191. vpset->bank_contents[vcpu_bank] = 0;
  192. /*
  193. * Some banks may end up being empty but this is acceptable.
  194. */
  195. for_each_cpu(cpu, cpus) {
  196. if (exclude_self && cpu == this_cpu)
  197. continue;
  198. vcpu = hv_cpu_number_to_vp_number(cpu);
  199. if (vcpu == VP_INVAL)
  200. return -1;
  201. vcpu_bank = vcpu / 64;
  202. vcpu_offset = vcpu % 64;
  203. __set_bit(vcpu_offset, (unsigned long *)
  204. &vpset->bank_contents[vcpu_bank]);
  205. if (vcpu_bank >= nr_bank)
  206. nr_bank = vcpu_bank + 1;
  207. }
  208. vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
  209. return nr_bank;
  210. }
  211. static inline int cpumask_to_vpset(struct hv_vpset *vpset,
  212. const struct cpumask *cpus)
  213. {
  214. return __cpumask_to_vpset(vpset, cpus, false);
  215. }
  216. static inline int cpumask_to_vpset_noself(struct hv_vpset *vpset,
  217. const struct cpumask *cpus)
  218. {
  219. WARN_ON_ONCE(preemptible());
  220. return __cpumask_to_vpset(vpset, cpus, true);
  221. }
  222. void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
  223. bool hv_is_hyperv_initialized(void);
  224. bool hv_is_hibernation_supported(void);
  225. enum hv_isolation_type hv_get_isolation_type(void);
  226. bool hv_is_isolation_supported(void);
  227. bool hv_isolation_type_snp(void);
  228. u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
  229. void hyperv_cleanup(void);
  230. bool hv_query_ext_cap(u64 cap_query);
  231. void hv_setup_dma_ops(struct device *dev, bool coherent);
  232. void *hv_map_memory(void *addr, unsigned long size);
  233. void hv_unmap_memory(void *addr);
  234. #else /* CONFIG_HYPERV */
  235. static inline bool hv_is_hyperv_initialized(void) { return false; }
  236. static inline bool hv_is_hibernation_supported(void) { return false; }
  237. static inline void hyperv_cleanup(void) {}
  238. static inline bool hv_is_isolation_supported(void) { return false; }
  239. static inline enum hv_isolation_type hv_get_isolation_type(void)
  240. {
  241. return HV_ISOLATION_TYPE_NONE;
  242. }
  243. #endif /* CONFIG_HYPERV */
  244. #endif