hyperv-tlfs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This file contains definitions from Hyper-V Hypervisor Top-Level Functional
  4. * Specification (TLFS):
  5. * https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
  6. */
  7. #ifndef _ASM_GENERIC_HYPERV_TLFS_H
  8. #define _ASM_GENERIC_HYPERV_TLFS_H
  9. #include <linux/types.h>
  10. #include <linux/bits.h>
  11. #include <linux/time64.h>
  12. /*
  13. * While not explicitly listed in the TLFS, Hyper-V always runs with a page size
  14. * of 4096. These definitions are used when communicating with Hyper-V using
  15. * guest physical pages and guest physical page addresses, since the guest page
  16. * size may not be 4096 on all architectures.
  17. */
  18. #define HV_HYP_PAGE_SHIFT 12
  19. #define HV_HYP_PAGE_SIZE BIT(HV_HYP_PAGE_SHIFT)
  20. #define HV_HYP_PAGE_MASK (~(HV_HYP_PAGE_SIZE - 1))
  21. /*
  22. * Hyper-V provides two categories of flags relevant to guest VMs. The
  23. * "Features" category indicates specific functionality that is available
  24. * to guests on this particular instance of Hyper-V. The "Features"
  25. * are presented in four groups, each of which is 32 bits. The group A
  26. * and B definitions are common across architectures and are listed here.
  27. * However, not all flags are relevant on all architectures.
  28. *
  29. * Groups C and D vary across architectures and are listed in the
  30. * architecture specific portion of hyperv-tlfs.h. Some of these flags exist
  31. * on multiple architectures, but the bit positions are different so they
  32. * cannot appear in the generic portion of hyperv-tlfs.h.
  33. *
  34. * The "Enlightenments" category provides recommendations on whether to use
  35. * specific enlightenments that are available. The Enlighenments are a single
  36. * group of 32 bits, but they vary across architectures and are listed in
  37. * the architecture specific portion of hyperv-tlfs.h.
  38. */
  39. /*
  40. * Group A Features.
  41. */
  42. /* VP Runtime register available */
  43. #define HV_MSR_VP_RUNTIME_AVAILABLE BIT(0)
  44. /* Partition Reference Counter available*/
  45. #define HV_MSR_TIME_REF_COUNT_AVAILABLE BIT(1)
  46. /* Basic SynIC register available */
  47. #define HV_MSR_SYNIC_AVAILABLE BIT(2)
  48. /* Synthetic Timer registers available */
  49. #define HV_MSR_SYNTIMER_AVAILABLE BIT(3)
  50. /* Virtual APIC assist and VP assist page registers available */
  51. #define HV_MSR_APIC_ACCESS_AVAILABLE BIT(4)
  52. /* Hypercall and Guest OS ID registers available*/
  53. #define HV_MSR_HYPERCALL_AVAILABLE BIT(5)
  54. /* Access virtual processor index register available*/
  55. #define HV_MSR_VP_INDEX_AVAILABLE BIT(6)
  56. /* Virtual system reset register available*/
  57. #define HV_MSR_RESET_AVAILABLE BIT(7)
  58. /* Access statistics page registers available */
  59. #define HV_MSR_STAT_PAGES_AVAILABLE BIT(8)
  60. /* Partition reference TSC register is available */
  61. #define HV_MSR_REFERENCE_TSC_AVAILABLE BIT(9)
  62. /* Partition Guest IDLE register is available */
  63. #define HV_MSR_GUEST_IDLE_AVAILABLE BIT(10)
  64. /* Partition local APIC and TSC frequency registers available */
  65. #define HV_ACCESS_FREQUENCY_MSRS BIT(11)
  66. /* AccessReenlightenmentControls privilege */
  67. #define HV_ACCESS_REENLIGHTENMENT BIT(13)
  68. /* AccessTscInvariantControls privilege */
  69. #define HV_ACCESS_TSC_INVARIANT BIT(15)
  70. /*
  71. * Group B features.
  72. */
  73. #define HV_CREATE_PARTITIONS BIT(0)
  74. #define HV_ACCESS_PARTITION_ID BIT(1)
  75. #define HV_ACCESS_MEMORY_POOL BIT(2)
  76. #define HV_ADJUST_MESSAGE_BUFFERS BIT(3)
  77. #define HV_POST_MESSAGES BIT(4)
  78. #define HV_SIGNAL_EVENTS BIT(5)
  79. #define HV_CREATE_PORT BIT(6)
  80. #define HV_CONNECT_PORT BIT(7)
  81. #define HV_ACCESS_STATS BIT(8)
  82. #define HV_DEBUGGING BIT(11)
  83. #define HV_CPU_POWER_MANAGEMENT BIT(12)
  84. /*
  85. * TSC page layout.
  86. */
  87. struct ms_hyperv_tsc_page {
  88. volatile u32 tsc_sequence;
  89. u32 reserved1;
  90. volatile u64 tsc_scale;
  91. volatile s64 tsc_offset;
  92. } __packed;
  93. /*
  94. * The guest OS needs to register the guest ID with the hypervisor.
  95. * The guest ID is a 64 bit entity and the structure of this ID is
  96. * specified in the Hyper-V specification:
  97. *
  98. * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
  99. *
  100. * While the current guideline does not specify how Linux guest ID(s)
  101. * need to be generated, our plan is to publish the guidelines for
  102. * Linux and other guest operating systems that currently are hosted
  103. * on Hyper-V. The implementation here conforms to this yet
  104. * unpublished guidelines.
  105. *
  106. *
  107. * Bit(s)
  108. * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
  109. * 62:56 - Os Type; Linux is 0x100
  110. * 55:48 - Distro specific identification
  111. * 47:16 - Linux kernel version number
  112. * 15:0 - Distro specific identification
  113. *
  114. *
  115. */
  116. #define HV_LINUX_VENDOR_ID 0x8100
  117. /*
  118. * Crash notification flags.
  119. */
  120. #define HV_CRASH_CTL_CRASH_NOTIFY_MSG BIT_ULL(62)
  121. #define HV_CRASH_CTL_CRASH_NOTIFY BIT_ULL(63)
  122. /* Declare the various hypercall operations. */
  123. #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE 0x0002
  124. #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST 0x0003
  125. #define HVCALL_NOTIFY_LONG_SPIN_WAIT 0x0008
  126. #define HVCALL_SEND_IPI 0x000b
  127. #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX 0x0013
  128. #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX 0x0014
  129. #define HVCALL_SEND_IPI_EX 0x0015
  130. #define HVCALL_GET_VP_REGISTERS 0x0050
  131. #define HVCALL_SET_VP_REGISTERS 0x0051
  132. #define HVCALL_POST_MESSAGE 0x005c
  133. #define HVCALL_SIGNAL_EVENT 0x005d
  134. #define HVCALL_POST_DEBUG_DATA 0x0069
  135. #define HVCALL_RETRIEVE_DEBUG_DATA 0x006a
  136. #define HVCALL_RESET_DEBUG_SESSION 0x006b
  137. #define HVCALL_RETARGET_INTERRUPT 0x007e
  138. #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
  139. #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
  140. #define HV_FLUSH_ALL_PROCESSORS BIT(0)
  141. #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES BIT(1)
  142. #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY BIT(2)
  143. #define HV_FLUSH_USE_EXTENDED_RANGE_FORMAT BIT(3)
  144. enum HV_GENERIC_SET_FORMAT {
  145. HV_GENERIC_SET_SPARSE_4K,
  146. HV_GENERIC_SET_ALL,
  147. };
  148. #define HV_PARTITION_ID_SELF ((u64)-1)
  149. #define HV_VP_INDEX_SELF ((u32)-2)
  150. #define HV_HYPERCALL_RESULT_MASK GENMASK_ULL(15, 0)
  151. #define HV_HYPERCALL_FAST_BIT BIT(16)
  152. #define HV_HYPERCALL_VARHEAD_OFFSET 17
  153. #define HV_HYPERCALL_REP_COMP_OFFSET 32
  154. #define HV_HYPERCALL_REP_COMP_1 BIT_ULL(32)
  155. #define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32)
  156. #define HV_HYPERCALL_REP_START_OFFSET 48
  157. #define HV_HYPERCALL_REP_START_MASK GENMASK_ULL(59, 48)
  158. /* hypercall status code */
  159. #define HV_STATUS_SUCCESS 0
  160. #define HV_STATUS_INVALID_HYPERCALL_CODE 2
  161. #define HV_STATUS_INVALID_HYPERCALL_INPUT 3
  162. #define HV_STATUS_INVALID_ALIGNMENT 4
  163. #define HV_STATUS_INVALID_PARAMETER 5
  164. #define HV_STATUS_OPERATION_DENIED 8
  165. #define HV_STATUS_INSUFFICIENT_MEMORY 11
  166. #define HV_STATUS_INVALID_PORT_ID 17
  167. #define HV_STATUS_INVALID_CONNECTION_ID 18
  168. #define HV_STATUS_INSUFFICIENT_BUFFERS 19
  169. /*
  170. * The Hyper-V TimeRefCount register and the TSC
  171. * page provide a guest VM clock with 100ns tick rate
  172. */
  173. #define HV_CLOCK_HZ (NSEC_PER_SEC/100)
  174. /* Define the number of synthetic interrupt sources. */
  175. #define HV_SYNIC_SINT_COUNT (16)
  176. /* Define the expected SynIC version. */
  177. #define HV_SYNIC_VERSION_1 (0x1)
  178. /* Valid SynIC vectors are 16-255. */
  179. #define HV_SYNIC_FIRST_VALID_VECTOR (16)
  180. #define HV_SYNIC_CONTROL_ENABLE (1ULL << 0)
  181. #define HV_SYNIC_SIMP_ENABLE (1ULL << 0)
  182. #define HV_SYNIC_SIEFP_ENABLE (1ULL << 0)
  183. #define HV_SYNIC_SINT_MASKED (1ULL << 16)
  184. #define HV_SYNIC_SINT_AUTO_EOI (1ULL << 17)
  185. #define HV_SYNIC_SINT_VECTOR_MASK (0xFF)
  186. #define HV_SYNIC_STIMER_COUNT (4)
  187. /* Define synthetic interrupt controller message constants. */
  188. #define HV_MESSAGE_SIZE (256)
  189. #define HV_MESSAGE_PAYLOAD_BYTE_COUNT (240)
  190. #define HV_MESSAGE_PAYLOAD_QWORD_COUNT (30)
  191. /* Define synthetic interrupt controller message flags. */
  192. union hv_message_flags {
  193. __u8 asu8;
  194. struct {
  195. __u8 msg_pending:1;
  196. __u8 reserved:7;
  197. } __packed;
  198. };
  199. /* Define port identifier type. */
  200. union hv_port_id {
  201. __u32 asu32;
  202. struct {
  203. __u32 id:24;
  204. __u32 reserved:8;
  205. } __packed u;
  206. };
  207. /* Define synthetic interrupt controller message header. */
  208. struct hv_message_header {
  209. __u32 message_type;
  210. __u8 payload_size;
  211. union hv_message_flags message_flags;
  212. __u8 reserved[2];
  213. union {
  214. __u64 sender;
  215. union hv_port_id port;
  216. };
  217. } __packed;
  218. /* Define synthetic interrupt controller message format. */
  219. struct hv_message {
  220. struct hv_message_header header;
  221. union {
  222. __u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
  223. } u;
  224. } __packed;
  225. /* Define the synthetic interrupt message page layout. */
  226. struct hv_message_page {
  227. struct hv_message sint_message[HV_SYNIC_SINT_COUNT];
  228. } __packed;
  229. /* Define timer message payload structure. */
  230. struct hv_timer_message_payload {
  231. __u32 timer_index;
  232. __u32 reserved;
  233. __u64 expiration_time; /* When the timer expired */
  234. __u64 delivery_time; /* When the message was delivered */
  235. } __packed;
  236. /* Define synthetic interrupt controller flag constants. */
  237. #define HV_EVENT_FLAGS_COUNT (256 * 8)
  238. #define HV_EVENT_FLAGS_LONG_COUNT (256 / sizeof(unsigned long))
  239. /*
  240. * Synthetic timer configuration.
  241. */
  242. union hv_stimer_config {
  243. u64 as_uint64;
  244. struct {
  245. u64 enable:1;
  246. u64 periodic:1;
  247. u64 lazy:1;
  248. u64 auto_enable:1;
  249. u64 apic_vector:8;
  250. u64 direct_mode:1;
  251. u64 reserved_z0:3;
  252. u64 sintx:4;
  253. u64 reserved_z1:44;
  254. } __packed;
  255. };
  256. /* Define the synthetic interrupt controller event flags format. */
  257. union hv_synic_event_flags {
  258. unsigned long flags[HV_EVENT_FLAGS_LONG_COUNT];
  259. };
  260. /* Define SynIC control register. */
  261. union hv_synic_scontrol {
  262. u64 as_uint64;
  263. struct {
  264. u64 enable:1;
  265. u64 reserved:63;
  266. } __packed;
  267. };
  268. /* Define synthetic interrupt source. */
  269. union hv_synic_sint {
  270. u64 as_uint64;
  271. struct {
  272. u64 vector:8;
  273. u64 reserved1:8;
  274. u64 masked:1;
  275. u64 auto_eoi:1;
  276. u64 polling:1;
  277. u64 reserved2:45;
  278. } __packed;
  279. };
  280. /* Define the format of the SIMP register */
  281. union hv_synic_simp {
  282. u64 as_uint64;
  283. struct {
  284. u64 simp_enabled:1;
  285. u64 preserved:11;
  286. u64 base_simp_gpa:52;
  287. } __packed;
  288. };
  289. /* Define the format of the SIEFP register */
  290. union hv_synic_siefp {
  291. u64 as_uint64;
  292. struct {
  293. u64 siefp_enabled:1;
  294. u64 preserved:11;
  295. u64 base_siefp_gpa:52;
  296. } __packed;
  297. };
  298. struct hv_vpset {
  299. u64 format;
  300. u64 valid_bank_mask;
  301. u64 bank_contents[];
  302. } __packed;
  303. /* HvCallSendSyntheticClusterIpi hypercall */
  304. struct hv_send_ipi {
  305. u32 vector;
  306. u32 reserved;
  307. u64 cpu_mask;
  308. } __packed;
  309. /* HvCallSendSyntheticClusterIpiEx hypercall */
  310. struct hv_send_ipi_ex {
  311. u32 vector;
  312. u32 reserved;
  313. struct hv_vpset vp_set;
  314. } __packed;
  315. /* HvFlushGuestPhysicalAddressSpace hypercalls */
  316. struct hv_guest_mapping_flush {
  317. u64 address_space;
  318. u64 flags;
  319. } __packed;
  320. /*
  321. * HV_MAX_FLUSH_PAGES = "additional_pages" + 1. It's limited
  322. * by the bitwidth of "additional_pages" in union hv_gpa_page_range.
  323. */
  324. #define HV_MAX_FLUSH_PAGES (2048)
  325. /* HvFlushGuestPhysicalAddressList hypercall */
  326. union hv_gpa_page_range {
  327. u64 address_space;
  328. struct {
  329. u64 additional_pages:11;
  330. u64 largepage:1;
  331. u64 basepfn:52;
  332. } page;
  333. };
  334. /*
  335. * All input flush parameters should be in single page. The max flush
  336. * count is equal with how many entries of union hv_gpa_page_range can
  337. * be populated into the input parameter page.
  338. */
  339. #define HV_MAX_FLUSH_REP_COUNT ((HV_HYP_PAGE_SIZE - 2 * sizeof(u64)) / \
  340. sizeof(union hv_gpa_page_range))
  341. struct hv_guest_mapping_flush_list {
  342. u64 address_space;
  343. u64 flags;
  344. union hv_gpa_page_range gpa_list[HV_MAX_FLUSH_REP_COUNT];
  345. };
  346. /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
  347. struct hv_tlb_flush {
  348. u64 address_space;
  349. u64 flags;
  350. u64 processor_mask;
  351. u64 gva_list[];
  352. } __packed;
  353. /* HvFlushVirtualAddressSpaceEx, HvFlushVirtualAddressListEx hypercalls */
  354. struct hv_tlb_flush_ex {
  355. u64 address_space;
  356. u64 flags;
  357. struct hv_vpset hv_vp_set;
  358. u64 gva_list[];
  359. } __packed;
  360. /* HvRetargetDeviceInterrupt hypercall */
  361. union hv_msi_entry {
  362. u64 as_uint64;
  363. struct {
  364. u32 address;
  365. u32 data;
  366. } __packed;
  367. };
  368. struct hv_interrupt_entry {
  369. u32 source; /* 1 for MSI(-X) */
  370. u32 reserved1;
  371. union hv_msi_entry msi_entry;
  372. } __packed;
  373. /*
  374. * flags for hv_device_interrupt_target.flags
  375. */
  376. #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
  377. #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
  378. struct hv_device_interrupt_target {
  379. u32 vector;
  380. u32 flags;
  381. union {
  382. u64 vp_mask;
  383. struct hv_vpset vp_set;
  384. };
  385. } __packed;
  386. struct hv_retarget_device_interrupt {
  387. u64 partition_id; /* use "self" */
  388. u64 device_id;
  389. struct hv_interrupt_entry int_entry;
  390. u64 reserved2;
  391. struct hv_device_interrupt_target int_target;
  392. } __packed __aligned(8);
  393. /* HvGetVpRegisters hypercall input with variable size reg name list*/
  394. struct hv_get_vp_registers_input {
  395. struct {
  396. u64 partitionid;
  397. u32 vpindex;
  398. u8 inputvtl;
  399. u8 padding[3];
  400. } header;
  401. struct input {
  402. u32 name0;
  403. u32 name1;
  404. } element[];
  405. } __packed;
  406. /* HvGetVpRegisters returns an array of these output elements */
  407. struct hv_get_vp_registers_output {
  408. union {
  409. struct {
  410. u32 a;
  411. u32 b;
  412. u32 c;
  413. u32 d;
  414. } as32 __packed;
  415. struct {
  416. u64 low;
  417. u64 high;
  418. } as64 __packed;
  419. };
  420. };
  421. /* HvSetVpRegisters hypercall with variable size reg name/value list*/
  422. struct hv_set_vp_registers_input {
  423. struct {
  424. u64 partitionid;
  425. u32 vpindex;
  426. u8 inputvtl;
  427. u8 padding[3];
  428. } header;
  429. struct {
  430. u32 name;
  431. u32 padding1;
  432. u64 padding2;
  433. u64 valuelow;
  434. u64 valuehigh;
  435. } element[];
  436. } __packed;
  437. #endif