vmw_vmci_defs.h 28 KB

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