]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Include/IndustryStandard/Xen/grant_table.h
OvmfPkg/IndustryStandard/Xen: replace MIT license text with SPDX ID
[mirror_edk2.git] / OvmfPkg / Include / IndustryStandard / Xen / grant_table.h
1 /******************************************************************************
2 * grant_table.h
3 *
4 * Interface for granting foreign access to page frames, and receiving
5 * page-ownership transfers.
6 *
7 * SPDX-License-Identifier: MIT
8 *
9 * Copyright (c) 2004, K A Fraser
10 */
11
12 #ifndef __XEN_PUBLIC_GRANT_TABLE_H__
13 #define __XEN_PUBLIC_GRANT_TABLE_H__
14
15 #include "xen.h"
16
17 /*
18 * `incontents 150 gnttab Grant Tables
19 *
20 * Xen's grant tables provide a generic mechanism to memory sharing
21 * between domains. This shared memory interface underpins the split
22 * device drivers for block and network IO.
23 *
24 * Each domain has its own grant table. This is a data structure that
25 * is shared with Xen; it allows the domain to tell Xen what kind of
26 * permissions other domains have on its pages. Entries in the grant
27 * table are identified by grant references. A grant reference is an
28 * integer, which indexes into the grant table. It acts as a
29 * capability which the grantee can use to perform operations on the
30 * granter's memory.
31 *
32 * This capability-based system allows shared-memory communications
33 * between unprivileged domains. A grant reference also encapsulates
34 * the details of a shared page, removing the need for a domain to
35 * know the real machine address of a page it is sharing. This makes
36 * it possible to share memory correctly with domains running in
37 * fully virtualised memory.
38 */
39
40 /***********************************
41 * GRANT TABLE REPRESENTATION
42 */
43
44 /* Some rough guidelines on accessing and updating grant-table entries
45 * in a concurrency-safe manner. For more information, Linux contains a
46 * reference implementation for guest OSes (drivers/xen/grant_table.c, see
47 * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=drivers/xen/grant-table.c;hb=HEAD
48 *
49 * NB. WMB is a no-op on current-generation x86 processors. However, a
50 * compiler barrier will still be required.
51 *
52 * Introducing a valid entry into the grant table:
53 * 1. Write ent->domid.
54 * 2. Write ent->frame:
55 * GTF_permit_access: Frame to which access is permitted.
56 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
57 * frame, or zero if none.
58 * 3. Write memory barrier (WMB).
59 * 4. Write ent->flags, inc. valid type.
60 *
61 * Invalidating an unused GTF_permit_access entry:
62 * 1. flags = ent->flags.
63 * 2. Observe that !(flags & (GTF_reading|GTF_writing)).
64 * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
65 * NB. No need for WMB as reuse of entry is control-dependent on success of
66 * step 3, and all architectures guarantee ordering of ctrl-dep writes.
67 *
68 * Invalidating an in-use GTF_permit_access entry:
69 * This cannot be done directly. Request assistance from the domain controller
70 * which can set a timeout on the use of a grant entry and take necessary
71 * action. (NB. This is not yet implemented!).
72 *
73 * Invalidating an unused GTF_accept_transfer entry:
74 * 1. flags = ent->flags.
75 * 2. Observe that !(flags & GTF_transfer_committed). [*]
76 * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
77 * NB. No need for WMB as reuse of entry is control-dependent on success of
78 * step 3, and all architectures guarantee ordering of ctrl-dep writes.
79 * [*] If GTF_transfer_committed is set then the grant entry is 'committed'.
80 * The guest must /not/ modify the grant entry until the address of the
81 * transferred frame is written. It is safe for the guest to spin waiting
82 * for this to occur (detect by observing GTF_transfer_completed in
83 * ent->flags).
84 *
85 * Invalidating a committed GTF_accept_transfer entry:
86 * 1. Wait for (ent->flags & GTF_transfer_completed).
87 *
88 * Changing a GTF_permit_access from writable to read-only:
89 * Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.
90 *
91 * Changing a GTF_permit_access from read-only to writable:
92 * Use SMP-safe bit-setting instruction.
93 */
94
95 /*
96 * Reference to a grant entry in a specified domain's grant table.
97 */
98 typedef UINT32 grant_ref_t;
99
100 /*
101 * A grant table comprises a packed array of grant entries in one or more
102 * page frames shared between Xen and a guest.
103 * [XEN]: This field is written by Xen and read by the sharing guest.
104 * [GST]: This field is written by the guest and read by Xen.
105 */
106
107 /*
108 * Version 1 of the grant table entry structure is maintained purely
109 * for backwards compatibility. New guests should use version 2.
110 */
111 #if __XEN_INTERFACE_VERSION__ < 0x0003020a
112 #define grant_entry_v1 grant_entry
113 #define grant_entry_v1_t grant_entry_t
114 #endif
115 struct grant_entry_v1 {
116 /* GTF_xxx: various type and flag information. [XEN,GST] */
117 UINT16 flags;
118 /* The domain being granted foreign privileges. [GST] */
119 domid_t domid;
120 /*
121 * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
122 * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
123 */
124 UINT32 frame;
125 };
126 typedef struct grant_entry_v1 grant_entry_v1_t;
127
128 /* The first few grant table entries will be preserved across grant table
129 * version changes and may be pre-populated at domain creation by tools.
130 */
131 #define GNTTAB_NR_RESERVED_ENTRIES 8
132 #define GNTTAB_RESERVED_CONSOLE 0
133 #define GNTTAB_RESERVED_XENSTORE 1
134
135 /*
136 * Type of grant entry.
137 * GTF_invalid: This grant entry grants no privileges.
138 * GTF_permit_access: Allow @domid to map/access @frame.
139 * GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
140 * to this guest. Xen writes the page number to @frame.
141 * GTF_transitive: Allow @domid to transitively access a subrange of
142 * @trans_grant in @trans_domid. No mappings are allowed.
143 */
144 #define GTF_invalid (0U<<0)
145 #define GTF_permit_access (1U<<0)
146 #define GTF_accept_transfer (2U<<0)
147 #define GTF_transitive (3U<<0)
148 #define GTF_type_mask (3U<<0)
149
150 /*
151 * Subflags for GTF_permit_access.
152 * GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
153 * GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
154 * GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
155 * GTF_PAT, GTF_PWT, GTF_PCD: (x86) cache attribute flags for the grant [GST]
156 * GTF_sub_page: Grant access to only a subrange of the page. @domid
157 * will only be allowed to copy from the grant, and not
158 * map it. [GST]
159 */
160 #define _GTF_readonly (2)
161 #define GTF_readonly (1U<<_GTF_readonly)
162 #define _GTF_reading (3)
163 #define GTF_reading (1U<<_GTF_reading)
164 #define _GTF_writing (4)
165 #define GTF_writing (1U<<_GTF_writing)
166 #define _GTF_PWT (5)
167 #define GTF_PWT (1U<<_GTF_PWT)
168 #define _GTF_PCD (6)
169 #define GTF_PCD (1U<<_GTF_PCD)
170 #define _GTF_PAT (7)
171 #define GTF_PAT (1U<<_GTF_PAT)
172 #define _GTF_sub_page (8)
173 #define GTF_sub_page (1U<<_GTF_sub_page)
174
175 /*
176 * Subflags for GTF_accept_transfer:
177 * GTF_transfer_committed: Xen sets this flag to indicate that it is committed
178 * to transferring ownership of a page frame. When a guest sees this flag
179 * it must /not/ modify the grant entry until GTF_transfer_completed is
180 * set by Xen.
181 * GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
182 * after reading GTF_transfer_committed. Xen will always write the frame
183 * address, followed by ORing this flag, in a timely manner.
184 */
185 #define _GTF_transfer_committed (2)
186 #define GTF_transfer_committed (1U<<_GTF_transfer_committed)
187 #define _GTF_transfer_completed (3)
188 #define GTF_transfer_completed (1U<<_GTF_transfer_completed)
189
190 /*
191 * Version 2 grant table entries. These fulfil the same role as
192 * version 1 entries, but can represent more complicated operations.
193 * Any given domain will have either a version 1 or a version 2 table,
194 * and every entry in the table will be the same version.
195 *
196 * The interface by which domains use grant references does not depend
197 * on the grant table version in use by the other domain.
198 */
199 #if __XEN_INTERFACE_VERSION__ >= 0x0003020a
200 /*
201 * Version 1 and version 2 grant entries share a common prefix. The
202 * fields of the prefix are documented as part of struct
203 * grant_entry_v1.
204 */
205 struct grant_entry_header {
206 UINT16 flags;
207 domid_t domid;
208 };
209 typedef struct grant_entry_header grant_entry_header_t;
210
211 /*
212 * Version 2 of the grant entry structure.
213 */
214 union grant_entry_v2 {
215 grant_entry_header_t hdr;
216
217 /*
218 * This member is used for V1-style full page grants, where either:
219 *
220 * -- hdr.type is GTF_accept_transfer, or
221 * -- hdr.type is GTF_permit_access and GTF_sub_page is not set.
222 *
223 * In that case, the frame field has the same semantics as the
224 * field of the same name in the V1 entry structure.
225 */
226 struct {
227 grant_entry_header_t hdr;
228 UINT32 pad0;
229 UINT64 frame;
230 } full_page;
231
232 /*
233 * If the grant type is GTF_grant_access and GTF_sub_page is set,
234 * @domid is allowed to access bytes [@page_off,@page_off+@length)
235 * in frame @frame.
236 */
237 struct {
238 grant_entry_header_t hdr;
239 UINT16 page_off;
240 UINT16 length;
241 UINT64 frame;
242 } sub_page;
243
244 /*
245 * If the grant is GTF_transitive, @domid is allowed to use the
246 * grant @gref in domain @trans_domid, as if it was the local
247 * domain. Obviously, the transitive access must be compatible
248 * with the original grant.
249 *
250 * The current version of Xen does not allow transitive grants
251 * to be mapped.
252 */
253 struct {
254 grant_entry_header_t hdr;
255 domid_t trans_domid;
256 UINT16 pad0;
257 grant_ref_t gref;
258 } transitive;
259
260 UINT32 __spacer[4]; /* Pad to a power of two */
261 };
262 typedef union grant_entry_v2 grant_entry_v2_t;
263
264 typedef UINT16 grant_status_t;
265
266 #endif /* __XEN_INTERFACE_VERSION__ */
267
268 /***********************************
269 * GRANT TABLE QUERIES AND USES
270 */
271
272 /* ` enum neg_errnoval
273 * ` HYPERVISOR_grant_table_op(enum grant_table_op cmd,
274 * ` VOID *args,
275 * ` UINT32 count)
276 * `
277 *
278 * @args points to an array of a per-command data structure. The array
279 * has @count members
280 */
281
282 /* ` enum grant_table_op { // GNTTABOP_* => struct gnttab_* */
283 #define GNTTABOP_map_grant_ref 0
284 #define GNTTABOP_unmap_grant_ref 1
285 /* ` } */
286
287 /*
288 * Handle to track a mapping created via a grant reference.
289 */
290 typedef UINT32 grant_handle_t;
291
292 /*
293 * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
294 * by devices and/or host CPUs. If successful, <handle> is a tracking number
295 * that must be presented later to destroy the mapping(s). On error, <handle>
296 * is a negative status code.
297 * NOTES:
298 * 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
299 * via which I/O devices may access the granted frame.
300 * 2. If GNTMAP_host_map is specified then a mapping will be added at
301 * either a host virtual address in the current address space, or at
302 * a PTE at the specified machine address. The type of mapping to
303 * perform is selected through the GNTMAP_contains_pte flag, and the
304 * address is specified in <host_addr>.
305 * 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
306 * host mapping is destroyed by other means then it is *NOT* guaranteed
307 * to be accounted to the correct grant reference!
308 */
309 struct gnttab_map_grant_ref {
310 /* IN parameters. */
311 UINT64 host_addr;
312 UINT32 flags; /* GNTMAP_* */
313 grant_ref_t ref;
314 domid_t dom;
315 /* OUT parameters. */
316 INT16 status; /* => enum grant_status */
317 grant_handle_t handle;
318 UINT64 dev_bus_addr;
319 };
320 typedef struct gnttab_map_grant_ref gnttab_map_grant_ref_t;
321 DEFINE_XEN_GUEST_HANDLE(gnttab_map_grant_ref_t);
322
323 /*
324 * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
325 * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
326 * field is ignored. If non-zero, they must refer to a device/host mapping
327 * that is tracked by <handle>
328 * NOTES:
329 * 1. The call may fail in an undefined manner if either mapping is not
330 * tracked by <handle>.
331 * 3. After executing a batch of unmaps, it is guaranteed that no stale
332 * mappings will remain in the device or host TLBs.
333 */
334 struct gnttab_unmap_grant_ref {
335 /* IN parameters. */
336 UINT64 host_addr;
337 UINT64 dev_bus_addr;
338 grant_handle_t handle;
339 /* OUT parameters. */
340 INT16 status; /* => enum grant_status */
341 };
342 typedef struct gnttab_unmap_grant_ref gnttab_unmap_grant_ref_t;
343 DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_grant_ref_t);
344
345 /*
346 * Bitfield values for gnttab_map_grant_ref.flags.
347 */
348 /* Map the grant entry for access by I/O devices. */
349 #define _GNTMAP_device_map (0)
350 #define GNTMAP_device_map (1<<_GNTMAP_device_map)
351 /* Map the grant entry for access by host CPUs. */
352 #define _GNTMAP_host_map (1)
353 #define GNTMAP_host_map (1<<_GNTMAP_host_map)
354 /* Accesses to the granted frame will be restricted to read-only access. */
355 #define _GNTMAP_readonly (2)
356 #define GNTMAP_readonly (1<<_GNTMAP_readonly)
357 /*
358 * GNTMAP_host_map subflag:
359 * 0 => The host mapping is usable only by the guest OS.
360 * 1 => The host mapping is usable by guest OS + current application.
361 */
362 #define _GNTMAP_application_map (3)
363 #define GNTMAP_application_map (1<<_GNTMAP_application_map)
364
365 /*
366 * GNTMAP_contains_pte subflag:
367 * 0 => This map request contains a host virtual address.
368 * 1 => This map request contains the machine addess of the PTE to update.
369 */
370 #define _GNTMAP_contains_pte (4)
371 #define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte)
372
373 #define _GNTMAP_can_fail (5)
374 #define GNTMAP_can_fail (1<<_GNTMAP_can_fail)
375
376 /*
377 * Bits to be placed in guest kernel available PTE bits (architecture
378 * dependent; only supported when XENFEAT_gnttab_map_avail_bits is set).
379 */
380 #define _GNTMAP_guest_avail0 (16)
381 #define GNTMAP_guest_avail_mask ((UINT32)~0 << _GNTMAP_guest_avail0)
382
383 /*
384 * Values for error status returns. All errors are -ve.
385 */
386 /* ` enum grant_status { */
387 #define GNTST_okay (0) /* Normal return. */
388 #define GNTST_general_error (-1) /* General undefined error. */
389 #define GNTST_bad_domain (-2) /* Unrecognsed domain id. */
390 #define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */
391 #define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */
392 #define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */
393 #define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/
394 #define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */
395 #define GNTST_permission_denied (-8) /* Not enough privilege for operation. */
396 #define GNTST_bad_page (-9) /* Specified page was invalid for op. */
397 #define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary. */
398 #define GNTST_address_too_big (-11) /* transfer page address too large. */
399 #define GNTST_eagain (-12) /* Operation not done; try again. */
400 /* ` } */
401
402 #define GNTTABOP_error_msgs { \
403 "okay", \
404 "undefined error", \
405 "unrecognised domain id", \
406 "invalid grant reference", \
407 "invalid mapping handle", \
408 "invalid virtual address", \
409 "invalid device address", \
410 "no spare translation slot in the I/O MMU", \
411 "permission denied", \
412 "bad page", \
413 "copy arguments cross page boundary", \
414 "page address size too large", \
415 "operation not done; try again" \
416 }
417
418 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
419
420 /*
421 * Local variables:
422 * mode: C
423 * c-file-style: "BSD"
424 * c-basic-offset: 4
425 * tab-width: 4
426 * indent-tabs-mode: nil
427 * End:
428 */