vmw_vmci_defs.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * VMware VMCI Driver
  4. *
  5. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  6. */
  7. #ifndef _VMW_VMCI_DEF_H_
  8. #define _VMW_VMCI_DEF_H_
  9. #include <linux/atomic.h>
  10. #include <linux/bits.h>
  11. /* Register offsets. */
  12. #define VMCI_STATUS_ADDR 0x00
  13. #define VMCI_CONTROL_ADDR 0x04
  14. #define VMCI_ICR_ADDR 0x08
  15. #define VMCI_IMR_ADDR 0x0c
  16. #define VMCI_DATA_OUT_ADDR 0x10
  17. #define VMCI_DATA_IN_ADDR 0x14
  18. #define VMCI_CAPS_ADDR 0x18
  19. #define VMCI_RESULT_LOW_ADDR 0x1c
  20. #define VMCI_RESULT_HIGH_ADDR 0x20
  21. #define VMCI_DATA_OUT_LOW_ADDR 0x24
  22. #define VMCI_DATA_OUT_HIGH_ADDR 0x28
  23. #define VMCI_DATA_IN_LOW_ADDR 0x2c
  24. #define VMCI_DATA_IN_HIGH_ADDR 0x30
  25. #define VMCI_GUEST_PAGE_SHIFT 0x34
  26. /* Max number of devices. */
  27. #define VMCI_MAX_DEVICES 1
  28. /* Status register bits. */
  29. #define VMCI_STATUS_INT_ON BIT(0)
  30. /* Control register bits. */
  31. #define VMCI_CONTROL_RESET BIT(0)
  32. #define VMCI_CONTROL_INT_ENABLE BIT(1)
  33. #define VMCI_CONTROL_INT_DISABLE BIT(2)
  34. /* Capabilities register bits. */
  35. #define VMCI_CAPS_HYPERCALL BIT(0)
  36. #define VMCI_CAPS_GUESTCALL BIT(1)
  37. #define VMCI_CAPS_DATAGRAM BIT(2)
  38. #define VMCI_CAPS_NOTIFICATIONS BIT(3)
  39. #define VMCI_CAPS_PPN64 BIT(4)
  40. #define VMCI_CAPS_DMA_DATAGRAM BIT(5)
  41. /* Interrupt Cause register bits. */
  42. #define VMCI_ICR_DATAGRAM BIT(0)
  43. #define VMCI_ICR_NOTIFICATION BIT(1)
  44. #define VMCI_ICR_DMA_DATAGRAM BIT(2)
  45. /* Interrupt Mask register bits. */
  46. #define VMCI_IMR_DATAGRAM BIT(0)
  47. #define VMCI_IMR_NOTIFICATION BIT(1)
  48. #define VMCI_IMR_DMA_DATAGRAM BIT(2)
  49. /*
  50. * Maximum MSI/MSI-X interrupt vectors in the device.
  51. * If VMCI_CAPS_DMA_DATAGRAM is supported by the device,
  52. * VMCI_MAX_INTRS_DMA_DATAGRAM vectors are available,
  53. * otherwise only VMCI_MAX_INTRS_NOTIFICATION.
  54. */
  55. #define VMCI_MAX_INTRS_NOTIFICATION 2
  56. #define VMCI_MAX_INTRS_DMA_DATAGRAM 3
  57. #define VMCI_MAX_INTRS VMCI_MAX_INTRS_DMA_DATAGRAM
  58. /*
  59. * Supported interrupt vectors. There is one for each ICR value above,
  60. * but here they indicate the position in the vector array/message ID.
  61. */
  62. enum {
  63. VMCI_INTR_DATAGRAM = 0,
  64. VMCI_INTR_NOTIFICATION = 1,
  65. VMCI_INTR_DMA_DATAGRAM = 2,
  66. };
  67. /*
  68. * A single VMCI device has an upper limit of 128MB on the amount of
  69. * memory that can be used for queue pairs. Since each queue pair
  70. * consists of at least two pages, the memory limit also dictates the
  71. * number of queue pairs a guest can create.
  72. */
  73. #define VMCI_MAX_GUEST_QP_MEMORY ((size_t)(128 * 1024 * 1024))
  74. #define VMCI_MAX_GUEST_QP_COUNT (VMCI_MAX_GUEST_QP_MEMORY / PAGE_SIZE / 2)
  75. /*
  76. * There can be at most PAGE_SIZE doorbells since there is one doorbell
  77. * per byte in the doorbell bitmap page.
  78. */
  79. #define VMCI_MAX_GUEST_DOORBELL_COUNT PAGE_SIZE
  80. /*
  81. * Queues with pre-mapped data pages must be small, so that we don't pin
  82. * too much kernel memory (especially on vmkernel). We limit a queuepair to
  83. * 32 KB, or 16 KB per queue for symmetrical pairs.
  84. */
  85. #define VMCI_MAX_PINNED_QP_MEMORY ((size_t)(32 * 1024))
  86. /*
  87. * The version of the VMCI device that supports MMIO access to registers
  88. * requests 256KB for BAR1 whereas the version of VMCI that supports
  89. * MSI/MSI-X only requests 8KB. The layout of the larger 256KB region is:
  90. * - the first 128KB are used for MSI/MSI-X.
  91. * - the following 64KB are used for MMIO register access.
  92. * - the remaining 64KB are unused.
  93. */
  94. #define VMCI_WITH_MMIO_ACCESS_BAR_SIZE ((size_t)(256 * 1024))
  95. #define VMCI_MMIO_ACCESS_OFFSET ((size_t)(128 * 1024))
  96. #define VMCI_MMIO_ACCESS_SIZE ((size_t)(64 * 1024))
  97. /*
  98. * For VMCI devices supporting the VMCI_CAPS_DMA_DATAGRAM capability, the
  99. * sending and receiving of datagrams can be performed using DMA to/from
  100. * a driver allocated buffer.
  101. * Sending and receiving will be handled as follows:
  102. * - when sending datagrams, the driver initializes the buffer where the
  103. * data part will refer to the outgoing VMCI datagram, sets the busy flag
  104. * to 1 and writes the address of the buffer to VMCI_DATA_OUT_HIGH_ADDR
  105. * and VMCI_DATA_OUT_LOW_ADDR. Writing to VMCI_DATA_OUT_LOW_ADDR triggers
  106. * the device processing of the buffer. When the device has processed the
  107. * buffer, it will write the result value to the buffer and then clear the
  108. * busy flag.
  109. * - when receiving datagrams, the driver initializes the buffer where the
  110. * data part will describe the receive buffer, clears the busy flag and
  111. * writes the address of the buffer to VMCI_DATA_IN_HIGH_ADDR and
  112. * VMCI_DATA_IN_LOW_ADDR. Writing to VMCI_DATA_IN_LOW_ADDR triggers the
  113. * device processing of the buffer. The device will copy as many available
  114. * datagrams into the buffer as possible, and then sets the busy flag.
  115. * When the busy flag is set, the driver will process the datagrams in the
  116. * buffer.
  117. */
  118. struct vmci_data_in_out_header {
  119. uint32_t busy;
  120. uint32_t opcode;
  121. uint32_t size;
  122. uint32_t rsvd;
  123. uint64_t result;
  124. };
  125. struct vmci_sg_elem {
  126. uint64_t addr;
  127. uint64_t size;
  128. };
  129. /*
  130. * We have a fixed set of resource IDs available in the VMX.
  131. * This allows us to have a very simple implementation since we statically
  132. * know how many will create datagram handles. If a new caller arrives and
  133. * we have run out of slots we can manually increment the maximum size of
  134. * available resource IDs.
  135. *
  136. * VMCI reserved hypervisor datagram resource IDs.
  137. */
  138. enum {
  139. VMCI_RESOURCES_QUERY = 0,
  140. VMCI_GET_CONTEXT_ID = 1,
  141. VMCI_SET_NOTIFY_BITMAP = 2,
  142. VMCI_DOORBELL_LINK = 3,
  143. VMCI_DOORBELL_UNLINK = 4,
  144. VMCI_DOORBELL_NOTIFY = 5,
  145. /*
  146. * VMCI_DATAGRAM_REQUEST_MAP and VMCI_DATAGRAM_REMOVE_MAP are
  147. * obsoleted by the removal of VM to VM communication.
  148. */
  149. VMCI_DATAGRAM_REQUEST_MAP = 6,
  150. VMCI_DATAGRAM_REMOVE_MAP = 7,
  151. VMCI_EVENT_SUBSCRIBE = 8,
  152. VMCI_EVENT_UNSUBSCRIBE = 9,
  153. VMCI_QUEUEPAIR_ALLOC = 10,
  154. VMCI_QUEUEPAIR_DETACH = 11,
  155. /*
  156. * VMCI_VSOCK_VMX_LOOKUP was assigned to 12 for Fusion 3.0/3.1,
  157. * WS 7.0/7.1 and ESX 4.1
  158. */
  159. VMCI_HGFS_TRANSPORT = 13,
  160. VMCI_UNITY_PBRPC_REGISTER = 14,
  161. VMCI_RPC_PRIVILEGED = 15,
  162. VMCI_RPC_UNPRIVILEGED = 16,
  163. VMCI_RESOURCE_MAX = 17,
  164. };
  165. /*
  166. * struct vmci_handle - Ownership information structure
  167. * @context: The VMX context ID.
  168. * @resource: The resource ID (used for locating in resource hash).
  169. *
  170. * The vmci_handle structure is used to track resources used within
  171. * vmw_vmci.
  172. */
  173. struct vmci_handle {
  174. u32 context;
  175. u32 resource;
  176. };
  177. #define vmci_make_handle(_cid, _rid) \
  178. (struct vmci_handle){ .context = _cid, .resource = _rid }
  179. static inline bool vmci_handle_is_equal(struct vmci_handle h1,
  180. struct vmci_handle h2)
  181. {
  182. return h1.context == h2.context && h1.resource == h2.resource;
  183. }
  184. #define VMCI_INVALID_ID ~0
  185. static const struct vmci_handle VMCI_INVALID_HANDLE = {
  186. .context = VMCI_INVALID_ID,
  187. .resource = VMCI_INVALID_ID
  188. };
  189. static inline bool vmci_handle_is_invalid(struct vmci_handle h)
  190. {
  191. return vmci_handle_is_equal(h, VMCI_INVALID_HANDLE);
  192. }
  193. /*
  194. * The below defines can be used to send anonymous requests.
  195. * This also indicates that no response is expected.
  196. */
  197. #define VMCI_ANON_SRC_CONTEXT_ID VMCI_INVALID_ID
  198. #define VMCI_ANON_SRC_RESOURCE_ID VMCI_INVALID_ID
  199. static const struct vmci_handle __maybe_unused VMCI_ANON_SRC_HANDLE = {
  200. .context = VMCI_ANON_SRC_CONTEXT_ID,
  201. .resource = VMCI_ANON_SRC_RESOURCE_ID
  202. };
  203. /* The lowest 16 context ids are reserved for internal use. */
  204. #define VMCI_RESERVED_CID_LIMIT ((u32) 16)
  205. /*
  206. * Hypervisor context id, used for calling into hypervisor
  207. * supplied services from the VM.
  208. */
  209. #define VMCI_HYPERVISOR_CONTEXT_ID 0
  210. /*
  211. * Well-known context id, a logical context that contains a set of
  212. * well-known services. This context ID is now obsolete.
  213. */
  214. #define VMCI_WELL_KNOWN_CONTEXT_ID 1
  215. /*
  216. * Context ID used by host endpoints.
  217. */
  218. #define VMCI_HOST_CONTEXT_ID 2
  219. #define VMCI_CONTEXT_IS_VM(_cid) (VMCI_INVALID_ID != (_cid) && \
  220. (_cid) > VMCI_HOST_CONTEXT_ID)
  221. /*
  222. * The VMCI_CONTEXT_RESOURCE_ID is used together with vmci_make_handle to make
  223. * handles that refer to a specific context.
  224. */
  225. #define VMCI_CONTEXT_RESOURCE_ID 0
  226. /*
  227. * VMCI error codes.
  228. */
  229. enum {
  230. VMCI_SUCCESS_QUEUEPAIR_ATTACH = 5,
  231. VMCI_SUCCESS_QUEUEPAIR_CREATE = 4,
  232. VMCI_SUCCESS_LAST_DETACH = 3,
  233. VMCI_SUCCESS_ACCESS_GRANTED = 2,
  234. VMCI_SUCCESS_ENTRY_DEAD = 1,
  235. VMCI_SUCCESS = 0,
  236. VMCI_ERROR_INVALID_RESOURCE = (-1),
  237. VMCI_ERROR_INVALID_ARGS = (-2),
  238. VMCI_ERROR_NO_MEM = (-3),
  239. VMCI_ERROR_DATAGRAM_FAILED = (-4),
  240. VMCI_ERROR_MORE_DATA = (-5),
  241. VMCI_ERROR_NO_MORE_DATAGRAMS = (-6),
  242. VMCI_ERROR_NO_ACCESS = (-7),
  243. VMCI_ERROR_NO_HANDLE = (-8),
  244. VMCI_ERROR_DUPLICATE_ENTRY = (-9),
  245. VMCI_ERROR_DST_UNREACHABLE = (-10),
  246. VMCI_ERROR_PAYLOAD_TOO_LARGE = (-11),
  247. VMCI_ERROR_INVALID_PRIV = (-12),
  248. VMCI_ERROR_GENERIC = (-13),
  249. VMCI_ERROR_PAGE_ALREADY_SHARED = (-14),
  250. VMCI_ERROR_CANNOT_SHARE_PAGE = (-15),
  251. VMCI_ERROR_CANNOT_UNSHARE_PAGE = (-16),
  252. VMCI_ERROR_NO_PROCESS = (-17),
  253. VMCI_ERROR_NO_DATAGRAM = (-18),
  254. VMCI_ERROR_NO_RESOURCES = (-19),
  255. VMCI_ERROR_UNAVAILABLE = (-20),
  256. VMCI_ERROR_NOT_FOUND = (-21),
  257. VMCI_ERROR_ALREADY_EXISTS = (-22),
  258. VMCI_ERROR_NOT_PAGE_ALIGNED = (-23),
  259. VMCI_ERROR_INVALID_SIZE = (-24),
  260. VMCI_ERROR_REGION_ALREADY_SHARED = (-25),
  261. VMCI_ERROR_TIMEOUT = (-26),
  262. VMCI_ERROR_DATAGRAM_INCOMPLETE = (-27),
  263. VMCI_ERROR_INCORRECT_IRQL = (-28),
  264. VMCI_ERROR_EVENT_UNKNOWN = (-29),
  265. VMCI_ERROR_OBSOLETE = (-30),
  266. VMCI_ERROR_QUEUEPAIR_MISMATCH = (-31),
  267. VMCI_ERROR_QUEUEPAIR_NOTSET = (-32),
  268. VMCI_ERROR_QUEUEPAIR_NOTOWNER = (-33),
  269. VMCI_ERROR_QUEUEPAIR_NOTATTACHED = (-34),
  270. VMCI_ERROR_QUEUEPAIR_NOSPACE = (-35),
  271. VMCI_ERROR_QUEUEPAIR_NODATA = (-36),
  272. VMCI_ERROR_BUSMEM_INVALIDATION = (-37),
  273. VMCI_ERROR_MODULE_NOT_LOADED = (-38),
  274. VMCI_ERROR_DEVICE_NOT_FOUND = (-39),
  275. VMCI_ERROR_QUEUEPAIR_NOT_READY = (-40),
  276. VMCI_ERROR_WOULD_BLOCK = (-41),
  277. /* VMCI clients should return error code within this range */
  278. VMCI_ERROR_CLIENT_MIN = (-500),
  279. VMCI_ERROR_CLIENT_MAX = (-550),
  280. /* Internal error codes. */
  281. VMCI_SHAREDMEM_ERROR_BAD_CONTEXT = (-1000),
  282. };
  283. /* VMCI reserved events. */
  284. enum {
  285. /* Only applicable to guest endpoints */
  286. VMCI_EVENT_CTX_ID_UPDATE = 0,
  287. /* Applicable to guest and host */
  288. VMCI_EVENT_CTX_REMOVED = 1,
  289. /* Only applicable to guest endpoints */
  290. VMCI_EVENT_QP_RESUMED = 2,
  291. /* Applicable to guest and host */
  292. VMCI_EVENT_QP_PEER_ATTACH = 3,
  293. /* Applicable to guest and host */
  294. VMCI_EVENT_QP_PEER_DETACH = 4,
  295. /*
  296. * Applicable to VMX and vmk. On vmk,
  297. * this event has the Context payload type.
  298. */
  299. VMCI_EVENT_MEM_ACCESS_ON = 5,
  300. /*
  301. * Applicable to VMX and vmk. Same as
  302. * above for the payload type.
  303. */
  304. VMCI_EVENT_MEM_ACCESS_OFF = 6,
  305. VMCI_EVENT_MAX = 7,
  306. };
  307. /*
  308. * Of the above events, a few are reserved for use in the VMX, and
  309. * other endpoints (guest and host kernel) should not use them. For
  310. * the rest of the events, we allow both host and guest endpoints to
  311. * subscribe to them, to maintain the same API for host and guest
  312. * endpoints.
  313. */
  314. #define VMCI_EVENT_VALID_VMX(_event) ((_event) == VMCI_EVENT_MEM_ACCESS_ON || \
  315. (_event) == VMCI_EVENT_MEM_ACCESS_OFF)
  316. #define VMCI_EVENT_VALID(_event) ((_event) < VMCI_EVENT_MAX && \
  317. !VMCI_EVENT_VALID_VMX(_event))
  318. /* Reserved guest datagram resource ids. */
  319. #define VMCI_EVENT_HANDLER 0
  320. /*
  321. * VMCI coarse-grained privileges (per context or host
  322. * process/endpoint. An entity with the restricted flag is only
  323. * allowed to interact with the hypervisor and trusted entities.
  324. */
  325. enum {
  326. VMCI_NO_PRIVILEGE_FLAGS = 0,
  327. VMCI_PRIVILEGE_FLAG_RESTRICTED = 1,
  328. VMCI_PRIVILEGE_FLAG_TRUSTED = 2,
  329. VMCI_PRIVILEGE_ALL_FLAGS = (VMCI_PRIVILEGE_FLAG_RESTRICTED |
  330. VMCI_PRIVILEGE_FLAG_TRUSTED),
  331. VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS = VMCI_NO_PRIVILEGE_FLAGS,
  332. VMCI_LEAST_PRIVILEGE_FLAGS = VMCI_PRIVILEGE_FLAG_RESTRICTED,
  333. VMCI_MAX_PRIVILEGE_FLAGS = VMCI_PRIVILEGE_FLAG_TRUSTED,
  334. };
  335. /* 0 through VMCI_RESERVED_RESOURCE_ID_MAX are reserved. */
  336. #define VMCI_RESERVED_RESOURCE_ID_MAX 1023
  337. /*
  338. * Driver version.
  339. *
  340. * Increment major version when you make an incompatible change.
  341. * Compatibility goes both ways (old driver with new executable
  342. * as well as new driver with old executable).
  343. */
  344. /* Never change VMCI_VERSION_SHIFT_WIDTH */
  345. #define VMCI_VERSION_SHIFT_WIDTH 16
  346. #define VMCI_MAKE_VERSION(_major, _minor) \
  347. ((_major) << VMCI_VERSION_SHIFT_WIDTH | (u16) (_minor))
  348. #define VMCI_VERSION_MAJOR(v) ((u32) (v) >> VMCI_VERSION_SHIFT_WIDTH)
  349. #define VMCI_VERSION_MINOR(v) ((u16) (v))
  350. /*
  351. * VMCI_VERSION is always the current version. Subsequently listed
  352. * versions are ways of detecting previous versions of the connecting
  353. * application (i.e., VMX).
  354. *
  355. * VMCI_VERSION_NOVMVM: This version removed support for VM to VM
  356. * communication.
  357. *
  358. * VMCI_VERSION_NOTIFY: This version introduced doorbell notification
  359. * support.
  360. *
  361. * VMCI_VERSION_HOSTQP: This version introduced host end point support
  362. * for hosted products.
  363. *
  364. * VMCI_VERSION_PREHOSTQP: This is the version prior to the adoption of
  365. * support for host end-points.
  366. *
  367. * VMCI_VERSION_PREVERS2: This fictional version number is intended to
  368. * represent the version of a VMX which doesn't call into the driver
  369. * with ioctl VERSION2 and thus doesn't establish its version with the
  370. * driver.
  371. */
  372. #define VMCI_VERSION VMCI_VERSION_NOVMVM
  373. #define VMCI_VERSION_NOVMVM VMCI_MAKE_VERSION(11, 0)
  374. #define VMCI_VERSION_NOTIFY VMCI_MAKE_VERSION(10, 0)
  375. #define VMCI_VERSION_HOSTQP VMCI_MAKE_VERSION(9, 0)
  376. #define VMCI_VERSION_PREHOSTQP VMCI_MAKE_VERSION(8, 0)
  377. #define VMCI_VERSION_PREVERS2 VMCI_MAKE_VERSION(1, 0)
  378. #define VMCI_SOCKETS_MAKE_VERSION(_p) \
  379. ((((_p)[0] & 0xFF) << 24) | (((_p)[1] & 0xFF) << 16) | ((_p)[2]))
  380. /*
  381. * The VMCI IOCTLs. We use identity code 7, as noted in ioctl-number.h, and
  382. * we start at sequence 9f. This gives us the same values that our shipping
  383. * products use, starting at 1951, provided we leave out the direction and
  384. * structure size. Note that VMMon occupies the block following us, starting
  385. * at 2001.
  386. */
  387. #define IOCTL_VMCI_VERSION _IO(7, 0x9f) /* 1951 */
  388. #define IOCTL_VMCI_INIT_CONTEXT _IO(7, 0xa0)
  389. #define IOCTL_VMCI_QUEUEPAIR_SETVA _IO(7, 0xa4)
  390. #define IOCTL_VMCI_NOTIFY_RESOURCE _IO(7, 0xa5)
  391. #define IOCTL_VMCI_NOTIFICATIONS_RECEIVE _IO(7, 0xa6)
  392. #define IOCTL_VMCI_VERSION2 _IO(7, 0xa7)
  393. #define IOCTL_VMCI_QUEUEPAIR_ALLOC _IO(7, 0xa8)
  394. #define IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE _IO(7, 0xa9)
  395. #define IOCTL_VMCI_QUEUEPAIR_DETACH _IO(7, 0xaa)
  396. #define IOCTL_VMCI_DATAGRAM_SEND _IO(7, 0xab)
  397. #define IOCTL_VMCI_DATAGRAM_RECEIVE _IO(7, 0xac)
  398. #define IOCTL_VMCI_CTX_ADD_NOTIFICATION _IO(7, 0xaf)
  399. #define IOCTL_VMCI_CTX_REMOVE_NOTIFICATION _IO(7, 0xb0)
  400. #define IOCTL_VMCI_CTX_GET_CPT_STATE _IO(7, 0xb1)
  401. #define IOCTL_VMCI_CTX_SET_CPT_STATE _IO(7, 0xb2)
  402. #define IOCTL_VMCI_GET_CONTEXT_ID _IO(7, 0xb3)
  403. #define IOCTL_VMCI_SOCKETS_VERSION _IO(7, 0xb4)
  404. #define IOCTL_VMCI_SOCKETS_GET_AF_VALUE _IO(7, 0xb8)
  405. #define IOCTL_VMCI_SOCKETS_GET_LOCAL_CID _IO(7, 0xb9)
  406. #define IOCTL_VMCI_SET_NOTIFY _IO(7, 0xcb) /* 1995 */
  407. /*IOCTL_VMMON_START _IO(7, 0xd1)*/ /* 2001 */
  408. /*
  409. * struct vmci_queue_header - VMCI Queue Header information.
  410. *
  411. * A Queue cannot stand by itself as designed. Each Queue's header
  412. * contains a pointer into itself (the producer_tail) and into its peer
  413. * (consumer_head). The reason for the separation is one of
  414. * accessibility: Each end-point can modify two things: where the next
  415. * location to enqueue is within its produce_q (producer_tail); and
  416. * where the next dequeue location is in its consume_q (consumer_head).
  417. *
  418. * An end-point cannot modify the pointers of its peer (guest to
  419. * guest; NOTE that in the host both queue headers are mapped r/w).
  420. * But, each end-point needs read access to both Queue header
  421. * structures in order to determine how much space is used (or left)
  422. * in the Queue. This is because for an end-point to know how full
  423. * its produce_q is, it needs to use the consumer_head that points into
  424. * the produce_q but -that- consumer_head is in the Queue header for
  425. * that end-points consume_q.
  426. *
  427. * Thoroughly confused? Sorry.
  428. *
  429. * producer_tail: the point to enqueue new entrants. When you approach
  430. * a line in a store, for example, you walk up to the tail.
  431. *
  432. * consumer_head: the point in the queue from which the next element is
  433. * dequeued. In other words, who is next in line is he who is at the
  434. * head of the line.
  435. *
  436. * Also, producer_tail points to an empty byte in the Queue, whereas
  437. * consumer_head points to a valid byte of data (unless producer_tail ==
  438. * consumer_head in which case consumer_head does not point to a valid
  439. * byte of data).
  440. *
  441. * For a queue of buffer 'size' bytes, the tail and head pointers will be in
  442. * the range [0, size-1].
  443. *
  444. * If produce_q_header->producer_tail == consume_q_header->consumer_head
  445. * then the produce_q is empty.
  446. */
  447. struct vmci_queue_header {
  448. /* All fields are 64bit and aligned. */
  449. struct vmci_handle handle; /* Identifier. */
  450. u64 producer_tail; /* Offset in this queue. */
  451. u64 consumer_head; /* Offset in peer queue. */
  452. };
  453. /*
  454. * struct vmci_datagram - Base struct for vmci datagrams.
  455. * @dst: A vmci_handle that tracks the destination of the datagram.
  456. * @src: A vmci_handle that tracks the source of the datagram.
  457. * @payload_size: The size of the payload.
  458. *
  459. * vmci_datagram structs are used when sending vmci datagrams. They include
  460. * the necessary source and destination information to properly route
  461. * the information along with the size of the package.
  462. */
  463. struct vmci_datagram {
  464. struct vmci_handle dst;
  465. struct vmci_handle src;
  466. u64 payload_size;
  467. };
  468. /*
  469. * Second flag is for creating a well-known handle instead of a per context
  470. * handle. Next flag is for deferring datagram delivery, so that the
  471. * datagram callback is invoked in a delayed context (not interrupt context).
  472. */
  473. #define VMCI_FLAG_DG_NONE 0
  474. #define VMCI_FLAG_WELLKNOWN_DG_HND BIT(0)
  475. #define VMCI_FLAG_ANYCID_DG_HND BIT(1)
  476. #define VMCI_FLAG_DG_DELAYED_CB BIT(2)
  477. /*
  478. * Maximum supported size of a VMCI datagram for routable datagrams.
  479. * Datagrams going to the hypervisor are allowed to be larger.
  480. */
  481. #define VMCI_MAX_DG_SIZE (17 * 4096)
  482. #define VMCI_MAX_DG_PAYLOAD_SIZE (VMCI_MAX_DG_SIZE - \
  483. sizeof(struct vmci_datagram))
  484. #define VMCI_DG_PAYLOAD(_dg) (void *)((char *)(_dg) + \
  485. sizeof(struct vmci_datagram))
  486. #define VMCI_DG_HEADERSIZE sizeof(struct vmci_datagram)
  487. #define VMCI_DG_SIZE(_dg) (VMCI_DG_HEADERSIZE + (size_t)(_dg)->payload_size)
  488. #define VMCI_DG_SIZE_ALIGNED(_dg) ((VMCI_DG_SIZE(_dg) + 7) & (~((size_t) 0x7)))
  489. #define VMCI_MAX_DATAGRAM_QUEUE_SIZE (VMCI_MAX_DG_SIZE * 2)
  490. struct vmci_event_payload_qp {
  491. struct vmci_handle handle; /* queue_pair handle. */
  492. u32 peer_id; /* Context id of attaching/detaching VM. */
  493. u32 _pad;
  494. };
  495. /* Flags for VMCI queue_pair API. */
  496. enum {
  497. /* Fail alloc if QP not created by peer. */
  498. VMCI_QPFLAG_ATTACH_ONLY = 1 << 0,
  499. /* Only allow attaches from local context. */
  500. VMCI_QPFLAG_LOCAL = 1 << 1,
  501. /* Host won't block when guest is quiesced. */
  502. VMCI_QPFLAG_NONBLOCK = 1 << 2,
  503. /* Pin data pages in ESX. Used with NONBLOCK */
  504. VMCI_QPFLAG_PINNED = 1 << 3,
  505. /* Update the following flag when adding new flags. */
  506. VMCI_QP_ALL_FLAGS = (VMCI_QPFLAG_ATTACH_ONLY | VMCI_QPFLAG_LOCAL |
  507. VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED),
  508. /* Convenience flags */
  509. VMCI_QP_ASYMM = (VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED),
  510. VMCI_QP_ASYMM_PEER = (VMCI_QPFLAG_ATTACH_ONLY | VMCI_QP_ASYMM),
  511. };
  512. /*
  513. * We allow at least 1024 more event datagrams from the hypervisor past the
  514. * normally allowed datagrams pending for a given context. We define this
  515. * limit on event datagrams from the hypervisor to guard against DoS attack
  516. * from a malicious VM which could repeatedly attach to and detach from a queue
  517. * pair, causing events to be queued at the destination VM. However, the rate
  518. * at which such events can be generated is small since it requires a VM exit
  519. * and handling of queue pair attach/detach call at the hypervisor. Event
  520. * datagrams may be queued up at the destination VM if it has interrupts
  521. * disabled or if it is not draining events for some other reason. 1024
  522. * datagrams is a grossly conservative estimate of the time for which
  523. * interrupts may be disabled in the destination VM, but at the same time does
  524. * not exacerbate the memory pressure problem on the host by much (size of each
  525. * event datagram is small).
  526. */
  527. #define VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE \
  528. (VMCI_MAX_DATAGRAM_QUEUE_SIZE + \
  529. 1024 * (sizeof(struct vmci_datagram) + \
  530. sizeof(struct vmci_event_data_max)))
  531. /*
  532. * Struct used for querying, via VMCI_RESOURCES_QUERY, the availability of
  533. * hypervisor resources. Struct size is 16 bytes. All fields in struct are
  534. * aligned to their natural alignment.
  535. */
  536. struct vmci_resource_query_hdr {
  537. struct vmci_datagram hdr;
  538. u32 num_resources;
  539. u32 _padding;
  540. };
  541. /*
  542. * Convenience struct for negotiating vectors. Must match layout of
  543. * VMCIResourceQueryHdr minus the struct vmci_datagram header.
  544. */
  545. struct vmci_resource_query_msg {
  546. u32 num_resources;
  547. u32 _padding;
  548. u32 resources[1];
  549. };
  550. /*
  551. * The maximum number of resources that can be queried using
  552. * VMCI_RESOURCE_QUERY is 31, as the result is encoded in the lower 31
  553. * bits of a positive return value. Negative values are reserved for
  554. * errors.
  555. */
  556. #define VMCI_RESOURCE_QUERY_MAX_NUM 31
  557. /* Maximum size for the VMCI_RESOURCE_QUERY request. */
  558. #define VMCI_RESOURCE_QUERY_MAX_SIZE \
  559. (sizeof(struct vmci_resource_query_hdr) + \
  560. sizeof(u32) * VMCI_RESOURCE_QUERY_MAX_NUM)
  561. /*
  562. * Struct used for setting the notification bitmap. All fields in
  563. * struct are aligned to their natural alignment.
  564. */
  565. struct vmci_notify_bm_set_msg {
  566. struct vmci_datagram hdr;
  567. union {
  568. u32 bitmap_ppn32;
  569. u64 bitmap_ppn64;
  570. };
  571. };
  572. /*
  573. * Struct used for linking a doorbell handle with an index in the
  574. * notify bitmap. All fields in struct are aligned to their natural
  575. * alignment.
  576. */
  577. struct vmci_doorbell_link_msg {
  578. struct vmci_datagram hdr;
  579. struct vmci_handle handle;
  580. u64 notify_idx;
  581. };
  582. /*
  583. * Struct used for unlinking a doorbell handle from an index in the
  584. * notify bitmap. All fields in struct are aligned to their natural
  585. * alignment.
  586. */
  587. struct vmci_doorbell_unlink_msg {
  588. struct vmci_datagram hdr;
  589. struct vmci_handle handle;
  590. };
  591. /*
  592. * Struct used for generating a notification on a doorbell handle. All
  593. * fields in struct are aligned to their natural alignment.
  594. */
  595. struct vmci_doorbell_notify_msg {
  596. struct vmci_datagram hdr;
  597. struct vmci_handle handle;
  598. };
  599. /*
  600. * This struct is used to contain data for events. Size of this struct is a
  601. * multiple of 8 bytes, and all fields are aligned to their natural alignment.
  602. */
  603. struct vmci_event_data {
  604. u32 event; /* 4 bytes. */
  605. u32 _pad;
  606. /* Event payload is put here. */
  607. };
  608. /*
  609. * Define the different VMCI_EVENT payload data types here. All structs must
  610. * be a multiple of 8 bytes, and fields must be aligned to their natural
  611. * alignment.
  612. */
  613. struct vmci_event_payld_ctx {
  614. u32 context_id; /* 4 bytes. */
  615. u32 _pad;
  616. };
  617. struct vmci_event_payld_qp {
  618. struct vmci_handle handle; /* queue_pair handle. */
  619. u32 peer_id; /* Context id of attaching/detaching VM. */
  620. u32 _pad;
  621. };
  622. /*
  623. * We define the following struct to get the size of the maximum event
  624. * data the hypervisor may send to the guest. If adding a new event
  625. * payload type above, add it to the following struct too (inside the
  626. * union).
  627. */
  628. struct vmci_event_data_max {
  629. struct vmci_event_data event_data;
  630. union {
  631. struct vmci_event_payld_ctx context_payload;
  632. struct vmci_event_payld_qp qp_payload;
  633. } ev_data_payload;
  634. };
  635. /*
  636. * Struct used for VMCI_EVENT_SUBSCRIBE/UNSUBSCRIBE and
  637. * VMCI_EVENT_HANDLER messages. Struct size is 32 bytes. All fields
  638. * in struct are aligned to their natural alignment.
  639. */
  640. struct vmci_event_msg {
  641. struct vmci_datagram hdr;
  642. /* Has event type and payload. */
  643. struct vmci_event_data event_data;
  644. /* Payload gets put here. */
  645. };
  646. /* Event with context payload. */
  647. struct vmci_event_ctx {
  648. struct vmci_event_msg msg;
  649. struct vmci_event_payld_ctx payload;
  650. };
  651. /* Event with QP payload. */
  652. struct vmci_event_qp {
  653. struct vmci_event_msg msg;
  654. struct vmci_event_payld_qp payload;
  655. };
  656. /*
  657. * Structs used for queue_pair alloc and detach messages. We align fields of
  658. * these structs to 64bit boundaries.
  659. */
  660. struct vmci_qp_alloc_msg {
  661. struct vmci_datagram hdr;
  662. struct vmci_handle handle;
  663. u32 peer;
  664. u32 flags;
  665. u64 produce_size;
  666. u64 consume_size;
  667. u64 num_ppns;
  668. /* List of PPNs placed here. */
  669. };
  670. struct vmci_qp_detach_msg {
  671. struct vmci_datagram hdr;
  672. struct vmci_handle handle;
  673. };
  674. /* VMCI Doorbell API. */
  675. #define VMCI_FLAG_DELAYED_CB BIT(0)
  676. typedef void (*vmci_callback) (void *client_data);
  677. /*
  678. * struct vmci_qp - A vmw_vmci queue pair handle.
  679. *
  680. * This structure is used as a handle to a queue pair created by
  681. * VMCI. It is intentionally left opaque to clients.
  682. */
  683. struct vmci_qp;
  684. /* Callback needed for correctly waiting on events. */
  685. typedef int (*vmci_datagram_recv_cb) (void *client_data,
  686. struct vmci_datagram *msg);
  687. /* VMCI Event API. */
  688. typedef void (*vmci_event_cb) (u32 sub_id, const struct vmci_event_data *ed,
  689. void *client_data);
  690. /*
  691. * We use the following inline function to access the payload data
  692. * associated with an event data.
  693. */
  694. static inline const void *
  695. vmci_event_data_const_payload(const struct vmci_event_data *ev_data)
  696. {
  697. return (const char *)ev_data + sizeof(*ev_data);
  698. }
  699. static inline void *vmci_event_data_payload(struct vmci_event_data *ev_data)
  700. {
  701. return (void *)vmci_event_data_const_payload(ev_data);
  702. }
  703. /*
  704. * Helper to read a value from a head or tail pointer. For X86_32, the
  705. * pointer is treated as a 32bit value, since the pointer value
  706. * never exceeds a 32bit value in this case. Also, doing an
  707. * atomic64_read on X86_32 uniprocessor systems may be implemented
  708. * as a non locked cmpxchg8b, that may end up overwriting updates done
  709. * by the VMCI device to the memory location. On 32bit SMP, the lock
  710. * prefix will be used, so correctness isn't an issue, but using a
  711. * 64bit operation still adds unnecessary overhead.
  712. */
  713. static inline u64 vmci_q_read_pointer(u64 *var)
  714. {
  715. return READ_ONCE(*(unsigned long *)var);
  716. }
  717. /*
  718. * Helper to set the value of a head or tail pointer. For X86_32, the
  719. * pointer is treated as a 32bit value, since the pointer value
  720. * never exceeds a 32bit value in this case. On 32bit SMP, using a
  721. * locked cmpxchg8b adds unnecessary overhead.
  722. */
  723. static inline void vmci_q_set_pointer(u64 *var, u64 new_val)
  724. {
  725. /* XXX buggered on big-endian */
  726. WRITE_ONCE(*(unsigned long *)var, (unsigned long)new_val);
  727. }
  728. /*
  729. * Helper to add a given offset to a head or tail pointer. Wraps the
  730. * value of the pointer around the max size of the queue.
  731. */
  732. static inline void vmci_qp_add_pointer(u64 *var, size_t add, u64 size)
  733. {
  734. u64 new_val = vmci_q_read_pointer(var);
  735. if (new_val >= size - add)
  736. new_val -= size;
  737. new_val += add;
  738. vmci_q_set_pointer(var, new_val);
  739. }
  740. /*
  741. * Helper routine to get the Producer Tail from the supplied queue.
  742. */
  743. static inline u64
  744. vmci_q_header_producer_tail(const struct vmci_queue_header *q_header)
  745. {
  746. struct vmci_queue_header *qh = (struct vmci_queue_header *)q_header;
  747. return vmci_q_read_pointer(&qh->producer_tail);
  748. }
  749. /*
  750. * Helper routine to get the Consumer Head from the supplied queue.
  751. */
  752. static inline u64
  753. vmci_q_header_consumer_head(const struct vmci_queue_header *q_header)
  754. {
  755. struct vmci_queue_header *qh = (struct vmci_queue_header *)q_header;
  756. return vmci_q_read_pointer(&qh->consumer_head);
  757. }
  758. /*
  759. * Helper routine to increment the Producer Tail. Fundamentally,
  760. * vmci_qp_add_pointer() is used to manipulate the tail itself.
  761. */
  762. static inline void
  763. vmci_q_header_add_producer_tail(struct vmci_queue_header *q_header,
  764. size_t add,
  765. u64 queue_size)
  766. {
  767. vmci_qp_add_pointer(&q_header->producer_tail, add, queue_size);
  768. }
  769. /*
  770. * Helper routine to increment the Consumer Head. Fundamentally,
  771. * vmci_qp_add_pointer() is used to manipulate the head itself.
  772. */
  773. static inline void
  774. vmci_q_header_add_consumer_head(struct vmci_queue_header *q_header,
  775. size_t add,
  776. u64 queue_size)
  777. {
  778. vmci_qp_add_pointer(&q_header->consumer_head, add, queue_size);
  779. }
  780. /*
  781. * Helper routine for getting the head and the tail pointer for a queue.
  782. * Both the VMCIQueues are needed to get both the pointers for one queue.
  783. */
  784. static inline void
  785. vmci_q_header_get_pointers(const struct vmci_queue_header *produce_q_header,
  786. const struct vmci_queue_header *consume_q_header,
  787. u64 *producer_tail,
  788. u64 *consumer_head)
  789. {
  790. if (producer_tail)
  791. *producer_tail = vmci_q_header_producer_tail(produce_q_header);
  792. if (consumer_head)
  793. *consumer_head = vmci_q_header_consumer_head(consume_q_header);
  794. }
  795. static inline void vmci_q_header_init(struct vmci_queue_header *q_header,
  796. const struct vmci_handle handle)
  797. {
  798. q_header->handle = handle;
  799. q_header->producer_tail = 0;
  800. q_header->consumer_head = 0;
  801. }
  802. /*
  803. * Finds available free space in a produce queue to enqueue more
  804. * data or reports an error if queue pair corruption is detected.
  805. */
  806. static s64
  807. vmci_q_header_free_space(const struct vmci_queue_header *produce_q_header,
  808. const struct vmci_queue_header *consume_q_header,
  809. const u64 produce_q_size)
  810. {
  811. u64 tail;
  812. u64 head;
  813. u64 free_space;
  814. tail = vmci_q_header_producer_tail(produce_q_header);
  815. head = vmci_q_header_consumer_head(consume_q_header);
  816. if (tail >= produce_q_size || head >= produce_q_size)
  817. return VMCI_ERROR_INVALID_SIZE;
  818. /*
  819. * Deduct 1 to avoid tail becoming equal to head which causes
  820. * ambiguity. If head and tail are equal it means that the
  821. * queue is empty.
  822. */
  823. if (tail >= head)
  824. free_space = produce_q_size - (tail - head) - 1;
  825. else
  826. free_space = head - tail - 1;
  827. return free_space;
  828. }
  829. /*
  830. * vmci_q_header_free_space() does all the heavy lifting of
  831. * determing the number of free bytes in a Queue. This routine,
  832. * then subtracts that size from the full size of the Queue so
  833. * the caller knows how many bytes are ready to be dequeued.
  834. * Results:
  835. * On success, available data size in bytes (up to MAX_INT64).
  836. * On failure, appropriate error code.
  837. */
  838. static inline s64
  839. vmci_q_header_buf_ready(const struct vmci_queue_header *consume_q_header,
  840. const struct vmci_queue_header *produce_q_header,
  841. const u64 consume_q_size)
  842. {
  843. s64 free_space;
  844. free_space = vmci_q_header_free_space(consume_q_header,
  845. produce_q_header, consume_q_size);
  846. if (free_space < VMCI_SUCCESS)
  847. return free_space;
  848. return consume_q_size - free_space - 1;
  849. }
  850. #endif /* _VMW_VMCI_DEF_H_ */