vmw_vmci_defs.h 27 KB

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