dynmem-proto.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #ifndef HW_HYPERV_DYNMEM_PROTO_H
  2. #define HW_HYPERV_DYNMEM_PROTO_H
  3. /*
  4. * Hyper-V Dynamic Memory Protocol definitions
  5. *
  6. * Copyright (C) 2020-2023 Oracle and/or its affiliates.
  7. *
  8. * Based on drivers/hv/hv_balloon.c from Linux kernel:
  9. * Copyright (c) 2012, Microsoft Corporation.
  10. *
  11. * Author: K. Y. Srinivasan <kys@microsoft.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2.
  14. * See the COPYING file in the top-level directory.
  15. */
  16. /*
  17. * Protocol versions. The low word is the minor version, the high word the major
  18. * version.
  19. *
  20. * History:
  21. * Initial version 1.0
  22. * Changed to 0.1 on 2009/03/25
  23. * Changes to 0.2 on 2009/05/14
  24. * Changes to 0.3 on 2009/12/03
  25. * Changed to 1.0 on 2011/04/05
  26. * Changed to 2.0 on 2019/12/10
  27. */
  28. #define DYNMEM_MAKE_VERSION(Major, Minor) ((uint32_t)(((Major) << 16) | (Minor)))
  29. #define DYNMEM_MAJOR_VERSION(Version) ((uint32_t)(Version) >> 16)
  30. #define DYNMEM_MINOR_VERSION(Version) ((uint32_t)(Version) & 0xff)
  31. enum {
  32. DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
  33. DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
  34. DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
  35. DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
  36. DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
  37. DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
  38. DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
  39. };
  40. /*
  41. * Message Types
  42. */
  43. enum dm_message_type {
  44. /*
  45. * Version 0.3
  46. */
  47. DM_ERROR = 0,
  48. DM_VERSION_REQUEST = 1,
  49. DM_VERSION_RESPONSE = 2,
  50. DM_CAPABILITIES_REPORT = 3,
  51. DM_CAPABILITIES_RESPONSE = 4,
  52. DM_STATUS_REPORT = 5,
  53. DM_BALLOON_REQUEST = 6,
  54. DM_BALLOON_RESPONSE = 7,
  55. DM_UNBALLOON_REQUEST = 8,
  56. DM_UNBALLOON_RESPONSE = 9,
  57. DM_MEM_HOT_ADD_REQUEST = 10,
  58. DM_MEM_HOT_ADD_RESPONSE = 11,
  59. DM_VERSION_03_MAX = 11,
  60. /*
  61. * Version 1.0.
  62. */
  63. DM_INFO_MESSAGE = 12,
  64. DM_VERSION_1_MAX = 12,
  65. /*
  66. * Version 2.0
  67. */
  68. DM_MEM_HOT_REMOVE_REQUEST = 13,
  69. DM_MEM_HOT_REMOVE_RESPONSE = 14
  70. };
  71. /*
  72. * Structures defining the dynamic memory management
  73. * protocol.
  74. */
  75. union dm_version {
  76. struct {
  77. uint16_t minor_version;
  78. uint16_t major_version;
  79. };
  80. uint32_t version;
  81. } QEMU_PACKED;
  82. union dm_caps {
  83. struct {
  84. uint64_t balloon:1;
  85. uint64_t hot_add:1;
  86. /*
  87. * To support guests that may have alignment
  88. * limitations on hot-add, the guest can specify
  89. * its alignment requirements; a value of n
  90. * represents an alignment of 2^n in mega bytes.
  91. */
  92. uint64_t hot_add_alignment:4;
  93. uint64_t hot_remove:1;
  94. uint64_t reservedz:57;
  95. } cap_bits;
  96. uint64_t caps;
  97. } QEMU_PACKED;
  98. union dm_mem_page_range {
  99. struct {
  100. /*
  101. * The PFN number of the first page in the range.
  102. * 40 bits is the architectural limit of a PFN
  103. * number for AMD64.
  104. */
  105. uint64_t start_page:40;
  106. /*
  107. * The number of pages in the range.
  108. */
  109. uint64_t page_cnt:24;
  110. } finfo;
  111. uint64_t page_range;
  112. } QEMU_PACKED;
  113. /*
  114. * The header for all dynamic memory messages:
  115. *
  116. * type: Type of the message.
  117. * size: Size of the message in bytes; including the header.
  118. * trans_id: The guest is responsible for manufacturing this ID.
  119. */
  120. struct dm_header {
  121. uint16_t type;
  122. uint16_t size;
  123. uint32_t trans_id;
  124. } QEMU_PACKED;
  125. /*
  126. * A generic message format for dynamic memory.
  127. * Specific message formats are defined later in the file.
  128. */
  129. struct dm_message {
  130. struct dm_header hdr;
  131. uint8_t data[]; /* enclosed message */
  132. } QEMU_PACKED;
  133. /*
  134. * Specific message types supporting the dynamic memory protocol.
  135. */
  136. /*
  137. * Version negotiation message. Sent from the guest to the host.
  138. * The guest is free to try different versions until the host
  139. * accepts the version.
  140. *
  141. * dm_version: The protocol version requested.
  142. * is_last_attempt: If TRUE, this is the last version guest will request.
  143. * reservedz: Reserved field, set to zero.
  144. */
  145. struct dm_version_request {
  146. struct dm_header hdr;
  147. union dm_version version;
  148. uint32_t is_last_attempt:1;
  149. uint32_t reservedz:31;
  150. } QEMU_PACKED;
  151. /*
  152. * Version response message; Host to Guest and indicates
  153. * if the host has accepted the version sent by the guest.
  154. *
  155. * is_accepted: If TRUE, host has accepted the version and the guest
  156. * should proceed to the next stage of the protocol. FALSE indicates that
  157. * guest should re-try with a different version.
  158. *
  159. * reservedz: Reserved field, set to zero.
  160. */
  161. struct dm_version_response {
  162. struct dm_header hdr;
  163. uint64_t is_accepted:1;
  164. uint64_t reservedz:63;
  165. } QEMU_PACKED;
  166. /*
  167. * Message reporting capabilities. This is sent from the guest to the
  168. * host.
  169. */
  170. struct dm_capabilities {
  171. struct dm_header hdr;
  172. union dm_caps caps;
  173. uint64_t min_page_cnt;
  174. uint64_t max_page_number;
  175. } QEMU_PACKED;
  176. /*
  177. * Response to the capabilities message. This is sent from the host to the
  178. * guest. This message notifies if the host has accepted the guest's
  179. * capabilities. If the host has not accepted, the guest must shutdown
  180. * the service.
  181. *
  182. * is_accepted: Indicates if the host has accepted guest's capabilities.
  183. * reservedz: Must be 0.
  184. */
  185. struct dm_capabilities_resp_msg {
  186. struct dm_header hdr;
  187. uint64_t is_accepted:1;
  188. uint64_t hot_remove:1;
  189. uint64_t suppress_pressure_reports:1;
  190. uint64_t reservedz:61;
  191. } QEMU_PACKED;
  192. /*
  193. * This message is used to report memory pressure from the guest.
  194. * This message is not part of any transaction and there is no
  195. * response to this message.
  196. *
  197. * num_avail: Available memory in pages.
  198. * num_committed: Committed memory in pages.
  199. * page_file_size: The accumulated size of all page files
  200. * in the system in pages.
  201. * zero_free: The number of zero and free pages.
  202. * page_file_writes: The writes to the page file in pages.
  203. * io_diff: An indicator of file cache efficiency or page file activity,
  204. * calculated as File Cache Page Fault Count - Page Read Count.
  205. * This value is in pages.
  206. *
  207. * Some of these metrics are Windows specific and fortunately
  208. * the algorithm on the host side that computes the guest memory
  209. * pressure only uses num_committed value.
  210. */
  211. struct dm_status {
  212. struct dm_header hdr;
  213. uint64_t num_avail;
  214. uint64_t num_committed;
  215. uint64_t page_file_size;
  216. uint64_t zero_free;
  217. uint32_t page_file_writes;
  218. uint32_t io_diff;
  219. } QEMU_PACKED;
  220. /*
  221. * Message to ask the guest to allocate memory - balloon up message.
  222. * This message is sent from the host to the guest. The guest may not be
  223. * able to allocate as much memory as requested.
  224. *
  225. * num_pages: number of pages to allocate.
  226. */
  227. struct dm_balloon {
  228. struct dm_header hdr;
  229. uint32_t num_pages;
  230. uint32_t reservedz;
  231. } QEMU_PACKED;
  232. /*
  233. * Balloon response message; this message is sent from the guest
  234. * to the host in response to the balloon message.
  235. *
  236. * reservedz: Reserved; must be set to zero.
  237. * more_pages: If FALSE, this is the last message of the transaction.
  238. * if TRUE there will be at least one more message from the guest.
  239. *
  240. * range_count: The number of ranges in the range array.
  241. *
  242. * range_array: An array of page ranges returned to the host.
  243. *
  244. */
  245. struct dm_balloon_response {
  246. struct dm_header hdr;
  247. uint32_t reservedz;
  248. uint32_t more_pages:1;
  249. uint32_t range_count:31;
  250. union dm_mem_page_range range_array[];
  251. } QEMU_PACKED;
  252. /*
  253. * Un-balloon message; this message is sent from the host
  254. * to the guest to give guest more memory.
  255. *
  256. * more_pages: If FALSE, this is the last message of the transaction.
  257. * if TRUE there will be at least one more message from the guest.
  258. *
  259. * reservedz: Reserved; must be set to zero.
  260. *
  261. * range_count: The number of ranges in the range array.
  262. *
  263. * range_array: An array of page ranges returned to the host.
  264. *
  265. */
  266. struct dm_unballoon_request {
  267. struct dm_header hdr;
  268. uint32_t more_pages:1;
  269. uint32_t reservedz:31;
  270. uint32_t range_count;
  271. union dm_mem_page_range range_array[];
  272. } QEMU_PACKED;
  273. /*
  274. * Un-balloon response message; this message is sent from the guest
  275. * to the host in response to an unballoon request.
  276. *
  277. */
  278. struct dm_unballoon_response {
  279. struct dm_header hdr;
  280. } QEMU_PACKED;
  281. /*
  282. * Hot add request message. Message sent from the host to the guest.
  283. *
  284. * range: Memory range to hot add.
  285. * region: Explicit hot add memory region for guest to use. Optional.
  286. *
  287. */
  288. struct dm_hot_add {
  289. struct dm_header hdr;
  290. union dm_mem_page_range range;
  291. } QEMU_PACKED;
  292. struct dm_hot_add_with_region {
  293. struct dm_header hdr;
  294. union dm_mem_page_range range;
  295. union dm_mem_page_range region;
  296. } QEMU_PACKED;
  297. /*
  298. * Hot add response message.
  299. * This message is sent by the guest to report the status of a hot add request.
  300. * If page_count is less than the requested page count, then the host should
  301. * assume all further hot add requests will fail, since this indicates that
  302. * the guest has hit an upper physical memory barrier.
  303. *
  304. * Hot adds may also fail due to low resources; in this case, the guest must
  305. * not complete this message until the hot add can succeed, and the host must
  306. * not send a new hot add request until the response is sent.
  307. * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
  308. * times it fails the request.
  309. *
  310. *
  311. * page_count: number of pages that were successfully hot added.
  312. *
  313. * result: result of the operation 1: success, 0: failure.
  314. *
  315. */
  316. struct dm_hot_add_response {
  317. struct dm_header hdr;
  318. uint32_t page_count;
  319. uint32_t result;
  320. } QEMU_PACKED;
  321. struct dm_hot_remove {
  322. struct dm_header hdr;
  323. uint32_t virtual_node;
  324. uint32_t page_count;
  325. uint32_t qos_flags;
  326. uint32_t reservedZ;
  327. } QEMU_PACKED;
  328. struct dm_hot_remove_response {
  329. struct dm_header hdr;
  330. uint32_t result;
  331. uint32_t range_count;
  332. uint64_t more_pages:1;
  333. uint64_t reservedz:63;
  334. union dm_mem_page_range range_array[];
  335. } QEMU_PACKED;
  336. #define DM_REMOVE_QOS_LARGE (1 << 0)
  337. #define DM_REMOVE_QOS_LOCAL (1 << 1)
  338. #define DM_REMOVE_QOS_MASK (0x3)
  339. /*
  340. * Types of information sent from host to the guest.
  341. */
  342. enum dm_info_type {
  343. INFO_TYPE_MAX_PAGE_CNT = 0,
  344. MAX_INFO_TYPE
  345. };
  346. /*
  347. * Header for the information message.
  348. */
  349. struct dm_info_header {
  350. enum dm_info_type type;
  351. uint32_t data_size;
  352. uint8_t data[];
  353. } QEMU_PACKED;
  354. /*
  355. * This message is sent from the host to the guest to pass
  356. * some relevant information (win8 addition).
  357. *
  358. * reserved: no used.
  359. * info_size: size of the information blob.
  360. * info: information blob.
  361. */
  362. struct dm_info_msg {
  363. struct dm_header hdr;
  364. uint32_t reserved;
  365. uint32_t info_size;
  366. uint8_t info[];
  367. };
  368. #endif