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