mshyperv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #ifndef _ASM_X86_MSHYPER_H
  2. #define _ASM_X86_MSHYPER_H
  3. #include <linux/types.h>
  4. #include <linux/atomic.h>
  5. #include <linux/nmi.h>
  6. #include <asm/io.h>
  7. #include <asm/hyperv.h>
  8. /*
  9. * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
  10. * is set by CPUID(HVCPUID_VERSION_FEATURES).
  11. */
  12. enum hv_cpuid_function {
  13. HVCPUID_VERSION_FEATURES = 0x00000001,
  14. HVCPUID_VENDOR_MAXFUNCTION = 0x40000000,
  15. HVCPUID_INTERFACE = 0x40000001,
  16. /*
  17. * The remaining functions depend on the value of
  18. * HVCPUID_INTERFACE
  19. */
  20. HVCPUID_VERSION = 0x40000002,
  21. HVCPUID_FEATURES = 0x40000003,
  22. HVCPUID_ENLIGHTENMENT_INFO = 0x40000004,
  23. HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005,
  24. };
  25. struct ms_hyperv_info {
  26. u32 features;
  27. u32 misc_features;
  28. u32 hints;
  29. u32 max_vp_index;
  30. u32 max_lp_index;
  31. };
  32. extern struct ms_hyperv_info ms_hyperv;
  33. /*
  34. * Declare the MSR used to setup pages used to communicate with the hypervisor.
  35. */
  36. union hv_x64_msr_hypercall_contents {
  37. u64 as_uint64;
  38. struct {
  39. u64 enable:1;
  40. u64 reserved:11;
  41. u64 guest_physical_address:52;
  42. };
  43. };
  44. /*
  45. * TSC page layout.
  46. */
  47. struct ms_hyperv_tsc_page {
  48. volatile u32 tsc_sequence;
  49. u32 reserved1;
  50. volatile u64 tsc_scale;
  51. volatile s64 tsc_offset;
  52. u64 reserved2[509];
  53. };
  54. /*
  55. * The guest OS needs to register the guest ID with the hypervisor.
  56. * The guest ID is a 64 bit entity and the structure of this ID is
  57. * specified in the Hyper-V specification:
  58. *
  59. * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
  60. *
  61. * While the current guideline does not specify how Linux guest ID(s)
  62. * need to be generated, our plan is to publish the guidelines for
  63. * Linux and other guest operating systems that currently are hosted
  64. * on Hyper-V. The implementation here conforms to this yet
  65. * unpublished guidelines.
  66. *
  67. *
  68. * Bit(s)
  69. * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
  70. * 62:56 - Os Type; Linux is 0x100
  71. * 55:48 - Distro specific identification
  72. * 47:16 - Linux kernel version number
  73. * 15:0 - Distro specific identification
  74. *
  75. *
  76. */
  77. #define HV_LINUX_VENDOR_ID 0x8100
  78. /*
  79. * Generate the guest ID based on the guideline described above.
  80. */
  81. static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
  82. __u64 d_info2)
  83. {
  84. __u64 guest_id = 0;
  85. guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
  86. guest_id |= (d_info1 << 48);
  87. guest_id |= (kernel_version << 16);
  88. guest_id |= d_info2;
  89. return guest_id;
  90. }
  91. /* Free the message slot and signal end-of-message if required */
  92. static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
  93. {
  94. /*
  95. * On crash we're reading some other CPU's message page and we need
  96. * to be careful: this other CPU may already had cleared the header
  97. * and the host may already had delivered some other message there.
  98. * In case we blindly write msg->header.message_type we're going
  99. * to lose it. We can still lose a message of the same type but
  100. * we count on the fact that there can only be one
  101. * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
  102. * on crash.
  103. */
  104. if (cmpxchg(&msg->header.message_type, old_msg_type,
  105. HVMSG_NONE) != old_msg_type)
  106. return;
  107. /*
  108. * Make sure the write to MessageType (ie set to
  109. * HVMSG_NONE) happens before we read the
  110. * MessagePending and EOMing. Otherwise, the EOMing
  111. * will not deliver any more messages since there is
  112. * no empty slot
  113. */
  114. mb();
  115. if (msg->header.message_flags.msg_pending) {
  116. /*
  117. * This will cause message queue rescan to
  118. * possibly deliver another msg from the
  119. * hypervisor
  120. */
  121. wrmsrl(HV_X64_MSR_EOM, 0);
  122. }
  123. }
  124. #define hv_init_timer(timer, tick) wrmsrl(timer, tick)
  125. #define hv_init_timer_config(config, val) wrmsrl(config, val)
  126. #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
  127. #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
  128. #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
  129. #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
  130. #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val)
  131. #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val)
  132. #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
  133. #define hv_get_synint_state(int_num, val) rdmsrl(int_num, val)
  134. #define hv_set_synint_state(int_num, val) wrmsrl(int_num, val)
  135. void hyperv_callback_vector(void);
  136. #ifdef CONFIG_TRACING
  137. #define trace_hyperv_callback_vector hyperv_callback_vector
  138. #endif
  139. void hyperv_vector_handler(struct pt_regs *regs);
  140. void hv_setup_vmbus_irq(void (*handler)(void));
  141. void hv_remove_vmbus_irq(void);
  142. void hv_setup_kexec_handler(void (*handler)(void));
  143. void hv_remove_kexec_handler(void);
  144. void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
  145. void hv_remove_crash_handler(void);
  146. #if IS_ENABLED(CONFIG_HYPERV)
  147. extern struct clocksource *hyperv_cs;
  148. extern void *hv_hypercall_pg;
  149. static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
  150. {
  151. u64 input_address = input ? virt_to_phys(input) : 0;
  152. u64 output_address = output ? virt_to_phys(output) : 0;
  153. u64 hv_status;
  154. #ifdef CONFIG_X86_64
  155. if (!hv_hypercall_pg)
  156. return U64_MAX;
  157. __asm__ __volatile__("mov %4, %%r8\n"
  158. "call *%5"
  159. : "=a" (hv_status), ASM_CALL_CONSTRAINT,
  160. "+c" (control), "+d" (input_address)
  161. : "r" (output_address), "m" (hv_hypercall_pg)
  162. : "cc", "memory", "r8", "r9", "r10", "r11");
  163. #else
  164. u32 input_address_hi = upper_32_bits(input_address);
  165. u32 input_address_lo = lower_32_bits(input_address);
  166. u32 output_address_hi = upper_32_bits(output_address);
  167. u32 output_address_lo = lower_32_bits(output_address);
  168. if (!hv_hypercall_pg)
  169. return U64_MAX;
  170. __asm__ __volatile__("call *%7"
  171. : "=A" (hv_status),
  172. "+c" (input_address_lo), ASM_CALL_CONSTRAINT
  173. : "A" (control),
  174. "b" (input_address_hi),
  175. "D"(output_address_hi), "S"(output_address_lo),
  176. "m" (hv_hypercall_pg)
  177. : "cc", "memory");
  178. #endif /* !x86_64 */
  179. return hv_status;
  180. }
  181. #define HV_HYPERCALL_RESULT_MASK GENMASK_ULL(15, 0)
  182. #define HV_HYPERCALL_FAST_BIT BIT(16)
  183. #define HV_HYPERCALL_VARHEAD_OFFSET 17
  184. #define HV_HYPERCALL_REP_COMP_OFFSET 32
  185. #define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32)
  186. #define HV_HYPERCALL_REP_START_OFFSET 48
  187. #define HV_HYPERCALL_REP_START_MASK GENMASK_ULL(59, 48)
  188. /* Fast hypercall with 8 bytes of input and no output */
  189. static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
  190. {
  191. u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
  192. #ifdef CONFIG_X86_64
  193. {
  194. __asm__ __volatile__("call *%4"
  195. : "=a" (hv_status), ASM_CALL_CONSTRAINT,
  196. "+c" (control), "+d" (input1)
  197. : "m" (hv_hypercall_pg)
  198. : "cc", "r8", "r9", "r10", "r11");
  199. }
  200. #else
  201. {
  202. u32 input1_hi = upper_32_bits(input1);
  203. u32 input1_lo = lower_32_bits(input1);
  204. __asm__ __volatile__ ("call *%5"
  205. : "=A"(hv_status),
  206. "+c"(input1_lo),
  207. ASM_CALL_CONSTRAINT
  208. : "A" (control),
  209. "b" (input1_hi),
  210. "m" (hv_hypercall_pg)
  211. : "cc", "edi", "esi");
  212. }
  213. #endif
  214. return hv_status;
  215. }
  216. /*
  217. * Rep hypercalls. Callers of this functions are supposed to ensure that
  218. * rep_count and varhead_size comply with Hyper-V hypercall definition.
  219. */
  220. static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
  221. void *input, void *output)
  222. {
  223. u64 control = code;
  224. u64 status;
  225. u16 rep_comp;
  226. control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
  227. control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
  228. do {
  229. status = hv_do_hypercall(control, input, output);
  230. if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
  231. return status;
  232. /* Bits 32-43 of status have 'Reps completed' data. */
  233. rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
  234. HV_HYPERCALL_REP_COMP_OFFSET;
  235. control &= ~HV_HYPERCALL_REP_START_MASK;
  236. control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
  237. touch_nmi_watchdog();
  238. } while (rep_comp < rep_count);
  239. return status;
  240. }
  241. /*
  242. * Hypervisor's notion of virtual processor ID is different from
  243. * Linux' notion of CPU ID. This information can only be retrieved
  244. * in the context of the calling CPU. Setup a map for easy access
  245. * to this information.
  246. */
  247. extern u32 *hv_vp_index;
  248. extern u32 hv_max_vp_index;
  249. /**
  250. * hv_cpu_number_to_vp_number() - Map CPU to VP.
  251. * @cpu_number: CPU number in Linux terms
  252. *
  253. * This function returns the mapping between the Linux processor
  254. * number and the hypervisor's virtual processor number, useful
  255. * in making hypercalls and such that talk about specific
  256. * processors.
  257. *
  258. * Return: Virtual processor number in Hyper-V terms
  259. */
  260. static inline int hv_cpu_number_to_vp_number(int cpu_number)
  261. {
  262. return hv_vp_index[cpu_number];
  263. }
  264. void hyperv_init(void);
  265. void hyperv_setup_mmu_ops(void);
  266. void hyper_alloc_mmu(void);
  267. void hyperv_report_panic(struct pt_regs *regs);
  268. bool hv_is_hypercall_page_setup(void);
  269. void hyperv_cleanup(void);
  270. #else /* CONFIG_HYPERV */
  271. static inline void hyperv_init(void) {}
  272. static inline bool hv_is_hypercall_page_setup(void) { return false; }
  273. static inline void hyperv_cleanup(void) {}
  274. static inline void hyperv_setup_mmu_ops(void) {}
  275. #endif /* CONFIG_HYPERV */
  276. #ifdef CONFIG_HYPERV_TSCPAGE
  277. struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
  278. static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
  279. {
  280. u64 scale, offset, cur_tsc;
  281. u32 sequence;
  282. /*
  283. * The protocol for reading Hyper-V TSC page is specified in Hypervisor
  284. * Top-Level Functional Specification ver. 3.0 and above. To get the
  285. * reference time we must do the following:
  286. * - READ ReferenceTscSequence
  287. * A special '0' value indicates the time source is unreliable and we
  288. * need to use something else. The currently published specification
  289. * versions (up to 4.0b) contain a mistake and wrongly claim '-1'
  290. * instead of '0' as the special value, see commit c35b82ef0294.
  291. * - ReferenceTime =
  292. * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
  293. * - READ ReferenceTscSequence again. In case its value has changed
  294. * since our first reading we need to discard ReferenceTime and repeat
  295. * the whole sequence as the hypervisor was updating the page in
  296. * between.
  297. */
  298. do {
  299. sequence = READ_ONCE(tsc_pg->tsc_sequence);
  300. if (!sequence)
  301. return U64_MAX;
  302. /*
  303. * Make sure we read sequence before we read other values from
  304. * TSC page.
  305. */
  306. smp_rmb();
  307. scale = READ_ONCE(tsc_pg->tsc_scale);
  308. offset = READ_ONCE(tsc_pg->tsc_offset);
  309. cur_tsc = rdtsc_ordered();
  310. /*
  311. * Make sure we read sequence after we read all other values
  312. * from TSC page.
  313. */
  314. smp_rmb();
  315. } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
  316. return mul_u64_u64_shr(cur_tsc, scale, 64) + offset;
  317. }
  318. #else
  319. static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
  320. {
  321. return NULL;
  322. }
  323. #endif
  324. #endif