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