1 /* SPDX-License-Identifier: MIT */
2 /**
3  * @file
4  * @section AUTHORS
5  *
6  * Copyright (C) 2010  Rafal Wojtczuk  <rafal@invisiblethingslab.com>
7  *
8  *  Authors:
9  *       Rafal Wojtczuk  <rafal@invisiblethingslab.com>
10  *       Daniel De Graaf <dgdegra@tycho.nsa.gov>
11  *
12  * @section LICENSE
13  *
14  * @section DESCRIPTION
15  *
16  *  Originally borrowed from the Qubes OS Project, http://www.qubes-os.org,
17  *  this code has been substantially rewritten to use the gntdev and gntalloc
18  *  devices instead of raw MFNs and map_foreign_range.
19  *
20  *  This is a library for inter-domain communication.  A standard Xen ring
21  *  buffer is used, with a datagram-based interface built on top.  The grant
22  *  reference and event channels are shared in XenStore under a user-specified
23  *  path.
24  *
25  *  The ring.h macros define an asymmetric interface to a shared data structure
26  *  that assumes all rings reside in a single contiguous memory space. This is
27  *  not suitable for vchan because the interface to the ring is symmetric except
28  *  for the setup. Unlike the producer-consumer rings defined in ring.h, the
29  *  size of the rings used in vchan are determined at execution time instead of
30  *  compile time, so the macros in ring.h cannot be used to access the rings.
31  */
32 
33 #include <stdint.h>
34 #include <sys/types.h>
35 
36 struct ring_shared {
37 	uint32_t cons, prod;
38 };
39 
40 #define VCHAN_NOTIFY_WRITE 0x1
41 #define VCHAN_NOTIFY_READ 0x2
42 
43 /**
44  * vchan_interface: primary shared data structure
45  */
46 struct vchan_interface {
47 	/**
48 	 * Standard consumer/producer interface, one pair per buffer
49 	 * left is client write, server read
50 	 * right is client read, server write
51 	 */
52 	struct ring_shared left, right;
53 	/**
54 	 * size of the rings, which determines their location
55 	 * 10   - at offset 1024 in ring's page
56 	 * 11   - at offset 2048 in ring's page
57 	 * 12+  - uses 2^(N-12) grants to describe the multi-page ring
58 	 * These should remain constant once the page is shared.
59 	 * Only one of the two orders can be 10 (or 11).
60 	 */
61 	uint16_t left_order, right_order;
62 	/**
63 	 * Shutdown detection:
64 	 *  0: client (or server) has exited
65 	 *  1: client (or server) is connected
66 	 *  2: client has not yet connected
67 	 */
68 	uint8_t cli_live, srv_live;
69 	/**
70 	 * Notification bits:
71 	 *  VCHAN_NOTIFY_WRITE: send notify when data is written
72 	 *  VCHAN_NOTIFY_READ: send notify when data is read (consumed)
73 	 * cli_notify is used for the client to inform the server of its action
74 	 */
75 	uint8_t cli_notify, srv_notify;
76 	/**
77 	 * Grant list: ordering is left, right. Must not extend into actual ring
78 	 * or grow beyond the end of the initial shared page.
79 	 * These should remain constant once the page is shared, to allow
80 	 * for possible remapping by a client that restarts.
81 	 */
82 	uint32_t grants[0];
83 };
84 
85