iPXE
ring.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: MIT */
2/******************************************************************************
3 * ring.h
4 *
5 * Shared producer-consumer ring macros.
6 *
7 * Tim Deegan and Andrew Warfield November 2004.
8 */
9
10#ifndef __XEN_PUBLIC_IO_RING_H__
11#define __XEN_PUBLIC_IO_RING_H__
12
14FILE_SECBOOT ( PERMITTED );
15
16/*
17 * When #include'ing this header, you need to provide the following
18 * declaration upfront:
19 * - standard integers types (uint8_t, uint16_t, etc)
20 * They are provided by stdint.h of the standard headers.
21 *
22 * In addition, if you intend to use the FLEX macros, you also need to
23 * provide the following, before invoking the FLEX macros:
24 * - size_t
25 * - memcpy
26 * - grant_ref_t
27 * These declarations are provided by string.h of the standard headers,
28 * and grant_table.h from the Xen public headers.
29 */
30
31#include "../xen.h"
32#include "../xen-compat.h"
33
34/* Some PV I/O interfaces need a compatibility variant. */
35#if __XEN_INTERFACE_VERSION__ < 0x00041300
36#define XENPV_FLEX_ARRAY_DIM 1 /* variable size */
37#else
38#define XENPV_FLEX_ARRAY_DIM XEN_FLEX_ARRAY_DIM
39#endif
40
41#if __XEN_INTERFACE_VERSION__ < 0x00030208
42#define xen_mb() mb()
43#define xen_rmb() rmb()
44#define xen_wmb() wmb()
45#endif
46
47typedef unsigned int RING_IDX;
48
49/* Round a 32-bit unsigned constant down to the nearest power of two. */
50#define __RD2(x) (((x) & 0x00000002U) ? 0x2 : ((x) & 0x1))
51#define __RD4(x) (((x) & 0x0000000cU) ? __RD2((x) >> 2) << 2 : __RD2(x))
52#define __RD8(x) (((x) & 0x000000f0U) ? __RD4((x) >> 4) << 4 : __RD4(x))
53#define __RD16(x) (((x) & 0x0000ff00U) ? __RD8((x) >> 8) << 8 : __RD8(x))
54#define __RD32(x) (((x) & 0xffff0000U) ? __RD16((x) >> 16) << 16 : __RD16(x))
55
56/*
57 * Calculate size of a shared ring, given the total available space for the
58 * ring and indexes (_sz), and the name tag of the request/response structure.
59 * A ring contains as many entries as will fit, rounded down to the nearest
60 * power of two (so we can mask with (size-1) to loop around).
61 */
62#define __CONST_RING_SIZE(_s, _sz) \
63 (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
64 sizeof(((struct _s##_sring *)0)->ring[0])))
65/*
66 * The same for passing in an actual pointer instead of a name tag.
67 */
68#define __RING_SIZE(_s, _sz) \
69 (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
70
71/*
72 * Macros to make the correct C datatypes for a new kind of ring.
73 *
74 * To make a new ring datatype, you need to have two message structures,
75 * let's say request_t, and response_t already defined.
76 *
77 * In a header where you want the ring datatype declared, you then do:
78 *
79 * DEFINE_RING_TYPES(mytag, request_t, response_t);
80 *
81 * These expand out to give you a set of types, as you can see below.
82 * The most important of these are:
83 *
84 * mytag_sring_t - The shared ring.
85 * mytag_front_ring_t - The 'front' half of the ring.
86 * mytag_back_ring_t - The 'back' half of the ring.
87 *
88 * To initialize a ring in your code you need to know the location and size
89 * of the shared memory area (PAGE_SIZE, for instance). To initialise
90 * the front half:
91 *
92 * mytag_front_ring_t ring;
93 * XEN_FRONT_RING_INIT(&ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
94 *
95 * Initializing the back follows similarly (note that only the front
96 * initializes the shared ring):
97 *
98 * mytag_back_ring_t back_ring;
99 * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
100 */
101
102#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
103 \
104/* Shared ring entry */ \
105union __name##_sring_entry { \
106 __req_t req; \
107 __rsp_t rsp; \
108}; \
109 \
110/* Shared ring page */ \
111struct __name##_sring { \
112 RING_IDX req_prod, req_event; \
113 RING_IDX rsp_prod, rsp_event; \
114 union { \
115 struct { \
116 uint8_t smartpoll_active; \
117 } netif; \
118 struct { \
119 uint8_t msg; \
120 } tapif_user; \
121 uint8_t pvt_pad[4]; \
122 } pvt; \
123 uint8_t __pad[44]; \
124 union __name##_sring_entry ring[XENPV_FLEX_ARRAY_DIM]; \
125}; \
126 \
127/* "Front" end's private variables */ \
128struct __name##_front_ring { \
129 RING_IDX req_prod_pvt; \
130 RING_IDX rsp_cons; \
131 unsigned int nr_ents; \
132 struct __name##_sring *sring; \
133}; \
134 \
135/* "Back" end's private variables */ \
136struct __name##_back_ring { \
137 RING_IDX rsp_prod_pvt; \
138 RING_IDX req_cons; \
139 unsigned int nr_ents; \
140 struct __name##_sring *sring; \
141}; \
142 \
143/* Syntactic sugar */ \
144typedef struct __name##_sring __name##_sring_t; \
145typedef struct __name##_front_ring __name##_front_ring_t; \
146typedef struct __name##_back_ring __name##_back_ring_t
147
148/*
149 * Macros for manipulating rings.
150 *
151 * FRONT_RING_whatever works on the "front end" of a ring: here
152 * requests are pushed on to the ring and responses taken off it.
153 *
154 * BACK_RING_whatever works on the "back end" of a ring: here
155 * requests are taken off the ring and responses put on.
156 *
157 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
158 * This is OK in 1-for-1 request-response situations where the
159 * requestor (front end) never has more than RING_SIZE()-1
160 * outstanding requests.
161 */
162
163/* Initialising empty rings */
164#define SHARED_RING_INIT(_s) do { \
165 (_s)->req_prod = (_s)->rsp_prod = 0; \
166 (_s)->req_event = (_s)->rsp_event = 1; \
167 (void)memset((_s)->pvt.pvt_pad, 0, sizeof((_s)->pvt.pvt_pad)); \
168 (void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \
169} while(0)
170
171#define FRONT_RING_ATTACH(_r, _s, _i, __size) do { \
172 (_r)->req_prod_pvt = (_i); \
173 (_r)->rsp_cons = (_i); \
174 (_r)->nr_ents = __RING_SIZE(_s, __size); \
175 (_r)->sring = (_s); \
176} while (0)
177
178#define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)
179
180#define XEN_FRONT_RING_INIT(r, s, size) do { \
181 SHARED_RING_INIT(s); \
182 FRONT_RING_INIT(r, s, size); \
183} while (0)
184
185#define BACK_RING_ATTACH(_r, _s, _i, __size) do { \
186 (_r)->rsp_prod_pvt = (_i); \
187 (_r)->req_cons = (_i); \
188 (_r)->nr_ents = __RING_SIZE(_s, __size); \
189 (_r)->sring = (_s); \
190} while (0)
191
192#define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)
193
194/* How big is this ring? */
195#define RING_SIZE(_r) \
196 ((_r)->nr_ents)
197
198/* Number of free requests (for use on front side only). */
199#define RING_FREE_REQUESTS(_r) \
200 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
201
202/* Test if there is an empty slot available on the front ring.
203 * (This is only meaningful from the front. )
204 */
205#define RING_FULL(_r) \
206 (RING_FREE_REQUESTS(_r) == 0)
207
208/* Test if there are outstanding messages to be processed on a ring. */
209#define XEN_RING_NR_UNCONSUMED_RESPONSES(_r) \
210 ((_r)->sring->rsp_prod - (_r)->rsp_cons)
211
212#ifdef __GNUC__
213#define XEN_RING_NR_UNCONSUMED_REQUESTS(_r) ({ \
214 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
215 unsigned int rsp = RING_SIZE(_r) - \
216 ((_r)->req_cons - (_r)->rsp_prod_pvt); \
217 req < rsp ? req : rsp; \
218})
219#else
220/* Same as above, but without the nice GCC ({ ... }) syntax. */
221#define XEN_RING_NR_UNCONSUMED_REQUESTS(_r) \
222 ((((_r)->sring->req_prod - (_r)->req_cons) < \
223 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
224 ((_r)->sring->req_prod - (_r)->req_cons) : \
225 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
226#endif
227
228#ifdef XEN_RING_HAS_UNCONSUMED_IS_BOOL
229/*
230 * These variants should only be used in case no caller is abusing them for
231 * obtaining the number of unconsumed responses/requests.
232 */
233#define RING_HAS_UNCONSUMED_RESPONSES(_r) \
234 (!!XEN_RING_NR_UNCONSUMED_RESPONSES(_r))
235#define RING_HAS_UNCONSUMED_REQUESTS(_r) \
236 (!!XEN_RING_NR_UNCONSUMED_REQUESTS(_r))
237#else
238#define RING_HAS_UNCONSUMED_RESPONSES(_r) XEN_RING_NR_UNCONSUMED_RESPONSES(_r)
239#define RING_HAS_UNCONSUMED_REQUESTS(_r) XEN_RING_NR_UNCONSUMED_REQUESTS(_r)
240#endif
241
242/* Direct access to individual ring elements, by index. */
243#define RING_GET_REQUEST(_r, _idx) \
244 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
245
246#define RING_GET_RESPONSE(_r, _idx) \
247 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
248
249/*
250 * Get a local copy of a request/response.
251 *
252 * Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is
253 * done on a local copy that cannot be modified by the other end.
254 *
255 * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
256 * to be ineffective where dest is a struct which consists of only bitfields.
257 */
258#define RING_COPY_(type, r, idx, dest) do { \
259 /* Use volatile to force the copy into dest. */ \
260 *(dest) = *(volatile __typeof__(dest))RING_GET_##type(r, idx); \
261} while (0)
262
263#define RING_COPY_REQUEST(r, idx, req) RING_COPY_(REQUEST, r, idx, req)
264#define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)
265
266/* Loop termination condition: Would the specified index overflow the ring? */
267#define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
268 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
269
270/* Ill-behaved frontend determination: Can there be this many requests? */
271#define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \
272 (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
273
274/* Ill-behaved backend determination: Can there be this many responses? */
275#define RING_RESPONSE_PROD_OVERFLOW(_r, _prod) \
276 (((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))
277
278#define RING_PUSH_REQUESTS(_r) do { \
279 xen_wmb(); /* back sees requests /before/ updated producer index */ \
280 (_r)->sring->req_prod = (_r)->req_prod_pvt; \
281} while (0)
282
283#define RING_PUSH_RESPONSES(_r) do { \
284 xen_wmb(); /* front sees resps /before/ updated producer index */ \
285 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
286} while (0)
287
288/*
289 * Notification hold-off (req_event and rsp_event):
290 *
291 * When queueing requests or responses on a shared ring, it may not always be
292 * necessary to notify the remote end. For example, if requests are in flight
293 * in a backend, the front may be able to queue further requests without
294 * notifying the back (if the back checks for new requests when it queues
295 * responses).
296 *
297 * When enqueuing requests or responses:
298 *
299 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
300 * is a boolean return value. True indicates that the receiver requires an
301 * asynchronous notification.
302 *
303 * After dequeuing requests or responses (before sleeping the connection):
304 *
305 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
306 * The second argument is a boolean return value. True indicates that there
307 * are pending messages on the ring (i.e., the connection should not be put
308 * to sleep).
309 *
310 * These macros will set the req_event/rsp_event field to trigger a
311 * notification on the very next message that is enqueued. If you want to
312 * create batches of work (i.e., only receive a notification after several
313 * messages have been enqueued) then you will need to create a customised
314 * version of the FINAL_CHECK macro in your own code, which sets the event
315 * field appropriately.
316 */
317
318#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
319 RING_IDX __old = (_r)->sring->req_prod; \
320 RING_IDX __new = (_r)->req_prod_pvt; \
321 xen_wmb(); /* back sees requests /before/ updated producer index */ \
322 (_r)->sring->req_prod = __new; \
323 xen_mb(); /* back sees new requests /before/ we check req_event */ \
324 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
325 (RING_IDX)(__new - __old)); \
326} while (0)
327
328#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
329 RING_IDX __old = (_r)->sring->rsp_prod; \
330 RING_IDX __new = (_r)->rsp_prod_pvt; \
331 xen_wmb(); /* front sees resps /before/ updated producer index */ \
332 (_r)->sring->rsp_prod = __new; \
333 xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
334 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
335 (RING_IDX)(__new - __old)); \
336} while (0)
337
338#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
339 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
340 if (_work_to_do) break; \
341 (_r)->sring->req_event = (_r)->req_cons + 1; \
342 xen_mb(); \
343 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
344} while (0)
345
346#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
347 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
348 if (_work_to_do) break; \
349 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
350 xen_mb(); \
351 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
352} while (0)
353
354
355/*
356 * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
357 * functions to check if there is data on the ring, and to read and
358 * write to them.
359 *
360 * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
361 * does not define the indexes page. As different protocols can have
362 * extensions to the basic format, this macro allow them to define their
363 * own struct.
364 *
365 * XEN_FLEX_RING_SIZE
366 * Convenience macro to calculate the size of one of the two rings
367 * from the overall order.
368 *
369 * $NAME_mask
370 * Function to apply the size mask to an index, to reduce the index
371 * within the range [0-size].
372 *
373 * $NAME_read_packet
374 * Function to read data from the ring. The amount of data to read is
375 * specified by the "size" argument.
376 *
377 * $NAME_write_packet
378 * Function to write data to the ring. The amount of data to write is
379 * specified by the "size" argument.
380 *
381 * $NAME_get_ring_ptr
382 * Convenience function that returns a pointer to read/write to the
383 * ring at the right location.
384 *
385 * $NAME_data_intf
386 * Indexes page, shared between frontend and backend. It also
387 * contains the array of grant refs.
388 *
389 * $NAME_queued
390 * Function to calculate how many bytes are currently on the ring,
391 * ready to be read. It can also be used to calculate how much free
392 * space is currently on the ring (XEN_FLEX_RING_SIZE() -
393 * $NAME_queued()).
394 */
395
396#ifndef XEN_PAGE_SHIFT
397/* The PAGE_SIZE for ring protocols and hypercall interfaces is always
398 * 4K, regardless of the architecture, and page granularity chosen by
399 * operating systems.
400 */
401#define XEN_PAGE_SHIFT 12
402#endif
403#define XEN_FLEX_RING_SIZE(order) \
404 (1UL << ((order) + XEN_PAGE_SHIFT - 1))
405
406#define DEFINE_XEN_FLEX_RING(name) \
407static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size) \
408{ \
409 return idx & (ring_size - 1); \
410} \
411 \
412static inline unsigned char *name##_get_ring_ptr(unsigned char *buf, \
413 RING_IDX idx, \
414 RING_IDX ring_size) \
415{ \
416 return buf + name##_mask(idx, ring_size); \
417} \
418 \
419static inline void name##_read_packet(void *opaque, \
420 const unsigned char *buf, \
421 size_t size, \
422 RING_IDX masked_prod, \
423 RING_IDX *masked_cons, \
424 RING_IDX ring_size) \
425{ \
426 if (*masked_cons < masked_prod || \
427 size <= ring_size - *masked_cons) { \
428 memcpy(opaque, buf + *masked_cons, size); \
429 } else { \
430 memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons); \
431 memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf, \
432 size - (ring_size - *masked_cons)); \
433 } \
434 *masked_cons = name##_mask(*masked_cons + size, ring_size); \
435} \
436 \
437static inline void name##_write_packet(unsigned char *buf, \
438 const void *opaque, \
439 size_t size, \
440 RING_IDX *masked_prod, \
441 RING_IDX masked_cons, \
442 RING_IDX ring_size) \
443{ \
444 if (*masked_prod < masked_cons || \
445 size <= ring_size - *masked_prod) { \
446 memcpy(buf + *masked_prod, opaque, size); \
447 } else { \
448 memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod); \
449 memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod), \
450 size - (ring_size - *masked_prod)); \
451 } \
452 *masked_prod = name##_mask(*masked_prod + size, ring_size); \
453} \
454 \
455static inline RING_IDX name##_queued(RING_IDX prod, \
456 RING_IDX cons, \
457 RING_IDX ring_size) \
458{ \
459 RING_IDX size; \
460 \
461 if (prod == cons) \
462 return 0; \
463 \
464 prod = name##_mask(prod, ring_size); \
465 cons = name##_mask(cons, ring_size); \
466 \
467 if (prod == cons) \
468 return ring_size; \
469 \
470 if (prod > cons) \
471 size = prod - cons; \
472 else \
473 size = ring_size - (cons - prod); \
474 return size; \
475} \
476 \
477struct name##_data { \
478 unsigned char *in; /* half of the allocation */ \
479 unsigned char *out; /* half of the allocation */ \
480}
481
482#define DEFINE_XEN_FLEX_RING_AND_INTF(name) \
483struct name##_data_intf { \
484 RING_IDX in_cons, in_prod; \
485 \
486 uint8_t pad1[56]; \
487 \
488 RING_IDX out_cons, out_prod; \
489 \
490 uint8_t pad2[56]; \
491 \
492 RING_IDX ring_order; \
493 grant_ref_t ref[XEN_FLEX_ARRAY_DIM]; \
494}; \
495DEFINE_XEN_FLEX_RING(name)
496
497#endif /* __XEN_PUBLIC_IO_RING_H__ */
498
499/*
500 * Local variables:
501 * mode: C
502 * c-file-style: "BSD"
503 * c-basic-offset: 4
504 * tab-width: 4
505 * indent-tabs-mode: nil
506 * End:
507 */
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
unsigned int RING_IDX
Definition ring.h:47