1 /******************************************************************************
4 * Interface for granting foreign access to page frames, and receiving
5 * page-ownership transfers.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Copyright (c) 2004, K A Fraser
28 #ifndef __XEN_PUBLIC_GRANT_TABLE_H__
29 #define __XEN_PUBLIC_GRANT_TABLE_H__
31 #include <xen/interface/xen.h>
33 /***********************************
34 * GRANT TABLE REPRESENTATION
37 /* Some rough guidelines on accessing and updating grant-table entries
38 * in a concurrency-safe manner. For more information, Linux contains a
39 * reference implementation for guest OSes (arch/xen/kernel/grant_table.c).
41 * NB. WMB is a no-op on current-generation x86 processors. However, a
42 * compiler barrier will still be required.
44 * Introducing a valid entry into the grant table:
45 * 1. Write ent->domid.
46 * 2. Write ent->frame:
47 * GTF_permit_access: Frame to which access is permitted.
48 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
49 * frame, or zero if none.
50 * 3. Write memory barrier (WMB).
51 * 4. Write ent->flags, inc. valid type.
53 * Invalidating an unused GTF_permit_access entry:
54 * 1. flags = ent->flags.
55 * 2. Observe that !(flags & (GTF_reading|GTF_writing)).
56 * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
57 * NB. No need for WMB as reuse of entry is control-dependent on success of
58 * step 3, and all architectures guarantee ordering of ctrl-dep writes.
60 * Invalidating an in-use GTF_permit_access entry:
61 * This cannot be done directly. Request assistance from the domain controller
62 * which can set a timeout on the use of a grant entry and take necessary
63 * action. (NB. This is not yet implemented!).
65 * Invalidating an unused GTF_accept_transfer entry:
66 * 1. flags = ent->flags.
67 * 2. Observe that !(flags & GTF_transfer_committed). [*]
68 * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
69 * NB. No need for WMB as reuse of entry is control-dependent on success of
70 * step 3, and all architectures guarantee ordering of ctrl-dep writes.
71 * [*] If GTF_transfer_committed is set then the grant entry is 'committed'.
72 * The guest must /not/ modify the grant entry until the address of the
73 * transferred frame is written. It is safe for the guest to spin waiting
74 * for this to occur (detect by observing GTF_transfer_completed in
77 * Invalidating a committed GTF_accept_transfer entry:
78 * 1. Wait for (ent->flags & GTF_transfer_completed).
80 * Changing a GTF_permit_access from writable to read-only:
81 * Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.
83 * Changing a GTF_permit_access from read-only to writable:
84 * Use SMP-safe bit-setting instruction.
88 * Reference to a grant entry in a specified domain's grant table.
90 typedef uint32_t grant_ref_t;
93 * A grant table comprises a packed array of grant entries in one or more
94 * page frames shared between Xen and a guest.
95 * [XEN]: This field is written by Xen and read by the sharing guest.
96 * [GST]: This field is written by the guest and read by Xen.
100 * Version 1 of the grant table entry structure is maintained purely
101 * for backwards compatibility. New guests should use version 2.
103 #if defined(CONFIG_PARAVIRT_XEN)
104 #define grant_entry grant_entry_v1
105 #elif __XEN_INTERFACE_VERSION__ < 0x0003020a
106 #define grant_entry_v1 grant_entry
107 #define grant_entry_v1_t grant_entry_t
109 struct grant_entry_v1 {
110 /* GTF_xxx: various type and flag information. [XEN,GST] */
112 /* The domain being granted foreign privileges. [GST] */
115 * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
116 * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
120 typedef struct grant_entry_v1 grant_entry_v1_t;
122 /* The first few grant table entries will be preserved across grant table
123 * version changes and may be pre-populated at domain creation by tools.
125 #define GNTTAB_NR_RESERVED_ENTRIES 8
126 #define GNTTAB_RESERVED_CONSOLE 0
127 #define GNTTAB_RESERVED_XENSTORE 1
130 * Type of grant entry.
131 * GTF_invalid: This grant entry grants no privileges.
132 * GTF_permit_access: Allow @domid to map/access @frame.
133 * GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
134 * to this guest. Xen writes the page number to @frame.
135 * GTF_transitive: Allow @domid to transitively access a subrange of
136 * @trans_grant in @trans_domid. No mappings are allowed.
138 #define GTF_invalid (0U<<0)
139 #define GTF_permit_access (1U<<0)
140 #define GTF_accept_transfer (2U<<0)
141 #define GTF_transitive (3U<<0)
142 #define GTF_type_mask (3U<<0)
145 * Subflags for GTF_permit_access.
146 * GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
147 * GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
148 * GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
149 * GTF_PAT, GTF_PWT, GTF_PCD: (x86) cache attribute flags for the grant [GST]
150 * GTF_sub_page: Grant access to only a subrange of the page. @domid
151 * will only be allowed to copy from the grant, and not
154 #define _GTF_readonly (2)
155 #define GTF_readonly (1U<<_GTF_readonly)
156 #define _GTF_reading (3)
157 #define GTF_reading (1U<<_GTF_reading)
158 #define _GTF_writing (4)
159 #define GTF_writing (1U<<_GTF_writing)
161 #define GTF_PWT (1U<<_GTF_PWT)
163 #define GTF_PCD (1U<<_GTF_PCD)
165 #define GTF_PAT (1U<<_GTF_PAT)
166 #define _GTF_sub_page (8)
167 #define GTF_sub_page (1U<<_GTF_sub_page)
170 * Subflags for GTF_accept_transfer:
171 * GTF_transfer_committed: Xen sets this flag to indicate that it is committed
172 * to transferring ownership of a page frame. When a guest sees this flag
173 * it must /not/ modify the grant entry until GTF_transfer_completed is
175 * GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
176 * after reading GTF_transfer_committed. Xen will always write the frame
177 * address, followed by ORing this flag, in a timely manner.
179 #define _GTF_transfer_committed (2)
180 #define GTF_transfer_committed (1U<<_GTF_transfer_committed)
181 #define _GTF_transfer_completed (3)
182 #define GTF_transfer_completed (1U<<_GTF_transfer_completed)
185 * Version 2 grant table entries. These fulfil the same role as
186 * version 1 entries, but can represent more complicated operations.
187 * Any given domain will have either a version 1 or a version 2 table,
188 * and every entry in the table will be the same version.
190 * The interface by which domains use grant references does not depend
191 * on the grant table version in use by the other domain.
193 #if defined(CONFIG_PARAVIRT_XEN) || __XEN_INTERFACE_VERSION__ >= 0x0003020a
195 * Version 1 and version 2 grant entries share a common prefix. The
196 * fields of the prefix are documented as part of struct
199 struct grant_entry_header {
203 typedef struct grant_entry_header grant_entry_header_t;
206 * Version 2 of the grant entry structure, here is a union because three
207 * different types are supported: full_page, sub_page and transitive.
209 union grant_entry_v2 {
210 struct grant_entry_header hdr;
213 * This member is used for V1-style full page grants, where either:
215 * -- hdr.type is GTF_accept_transfer, or
216 * -- hdr.type is GTF_permit_access and GTF_sub_page is not set.
218 * In that case, the frame field has the same semantics as the
219 * field of the same name in the V1 entry structure.
222 struct grant_entry_header hdr;
228 * If the grant type is GTF_grant_access and GTF_sub_page is set,
229 * @domid is allowed to access bytes [@page_off,@page_off+@length)
233 struct grant_entry_header hdr;
240 * If the grant is GTF_transitive, @domid is allowed to use the
241 * grant @gref in domain @trans_domid, as if it was the local
242 * domain. Obviously, the transitive access must be compatible
243 * with the original grant.
245 * The current version of Xen does not allow transitive grants
249 struct grant_entry_header hdr;
255 uint32_t __spacer[4]; /* Pad to a power of two */
257 typedef union grant_entry_v2 grant_entry_v2_t;
259 typedef uint16_t grant_status_t;
261 #endif /* __XEN_INTERFACE_VERSION__ */
263 /***********************************
264 * GRANT TABLE QUERIES AND USES
268 * Handle to track a mapping created via a grant reference.
270 typedef uint32_t grant_handle_t;
273 * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
274 * by devices and/or host CPUs. If successful, <handle> is a tracking number
275 * that must be presented later to destroy the mapping(s). On error, <handle>
276 * is a negative status code.
278 * 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
279 * via which I/O devices may access the granted frame.
280 * 2. If GNTMAP_host_map is specified then a mapping will be added at
281 * either a host virtual address in the current address space, or at
282 * a PTE at the specified machine address. The type of mapping to
283 * perform is selected through the GNTMAP_contains_pte flag, and the
284 * address is specified in <host_addr>.
285 * 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
286 * host mapping is destroyed by other means then it is *NOT* guaranteed
287 * to be accounted to the correct grant reference!
289 #define GNTTABOP_map_grant_ref 0
290 struct gnttab_map_grant_ref {
293 uint32_t flags; /* GNTMAP_* */
296 /* OUT parameters. */
297 int16_t status; /* GNTST_* */
298 grant_handle_t handle;
299 uint64_t dev_bus_addr;
301 DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref);
302 typedef struct gnttab_map_grant_ref gnttab_map_grant_ref_t;
303 DEFINE_XEN_GUEST_HANDLE(gnttab_map_grant_ref_t);
306 * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
307 * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
308 * field is ignored. If non-zero, they must refer to a device/host mapping
309 * that is tracked by <handle>
311 * 1. The call may fail in an undefined manner if either mapping is not
312 * tracked by <handle>.
313 * 3. After executing a batch of unmaps, it is guaranteed that no stale
314 * mappings will remain in the device or host TLBs.
316 #define GNTTABOP_unmap_grant_ref 1
317 struct gnttab_unmap_grant_ref {
320 uint64_t dev_bus_addr;
321 grant_handle_t handle;
322 /* OUT parameters. */
323 int16_t status; /* GNTST_* */
325 DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref);
326 typedef struct gnttab_unmap_grant_ref gnttab_unmap_grant_ref_t;
327 DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_grant_ref_t);
330 * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
331 * <nr_frames> pages. The frame addresses are written to the <frame_list>.
332 * Only <nr_frames> addresses are written, even if the table is larger.
334 * 1. <dom> may be specified as DOMID_SELF.
335 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
336 * 3. Xen may not support more than a single grant-table page per domain.
338 #define GNTTABOP_setup_table 2
339 struct gnttab_setup_table {
343 /* OUT parameters. */
344 int16_t status; /* GNTST_* */
345 XEN_GUEST_HANDLE(ulong) frame_list;
347 DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table);
348 typedef struct gnttab_setup_table gnttab_setup_table_t;
349 DEFINE_XEN_GUEST_HANDLE(gnttab_setup_table_t);
352 * GNTTABOP_dump_table: Dump the contents of the grant table to the
353 * xen console. Debugging use only.
355 #define GNTTABOP_dump_table 3
356 struct gnttab_dump_table {
359 /* OUT parameters. */
360 int16_t status; /* GNTST_* */
362 DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table);
363 typedef struct gnttab_dump_table gnttab_dump_table_t;
364 DEFINE_XEN_GUEST_HANDLE(gnttab_dump_table_t);
367 * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
368 * foreign domain has previously registered its interest in the transfer via
371 * Note that, even if the transfer fails, the specified page no longer belongs
372 * to the calling domain *unless* the error is GNTST_bad_page.
374 #define GNTTABOP_transfer 4
375 struct gnttab_transfer {
380 /* OUT parameters. */
383 DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer);
384 typedef struct gnttab_transfer gnttab_transfer_t;
385 DEFINE_XEN_GUEST_HANDLE(gnttab_transfer_t);
389 * GNTTABOP_copy: Hypervisor based copy
390 * source and destinations can be eithers MFNs or, for foreign domains,
391 * grant references. the foreign domain has to grant read/write access
392 * in its grant table.
394 * The flags specify what type source and destinations are (either MFN
395 * or grant reference).
397 * Note that this can also be used to copy data between two domains
398 * via a third party if the source and destination domains had previously
399 * grant appropriate access to their pages to the third party.
401 * source_offset specifies an offset in the source frame, dest_offset
402 * the offset in the target frame and len specifies the number of
403 * bytes to be copied.
406 #define _GNTCOPY_source_gref (0)
407 #define GNTCOPY_source_gref (1<<_GNTCOPY_source_gref)
408 #define _GNTCOPY_dest_gref (1)
409 #define GNTCOPY_dest_gref (1<<_GNTCOPY_dest_gref)
410 #define _GNTCOPY_can_fail (2)
411 #define GNTCOPY_can_fail (1<<_GNTCOPY_can_fail)
413 #define GNTTABOP_copy 5
414 typedef struct gnttab_copy {
425 uint16_t flags; /* GNTCOPY_* */
426 /* OUT parameters. */
429 DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy);
430 DEFINE_XEN_GUEST_HANDLE(gnttab_copy_t);
433 * GNTTABOP_query_size: Query the current and maximum sizes of the shared
436 * 1. <dom> may be specified as DOMID_SELF.
437 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
439 #define GNTTABOP_query_size 6
440 struct gnttab_query_size {
443 /* OUT parameters. */
445 uint32_t max_nr_frames;
446 int16_t status; /* GNTST_* */
448 DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size);
449 typedef struct gnttab_query_size gnttab_query_size_t;
450 DEFINE_XEN_GUEST_HANDLE(gnttab_query_size_t);
453 * GNTTABOP_unmap_and_replace: Destroy one or more grant-reference mappings
454 * tracked by <handle> but atomically replace the page table entry with one
455 * pointing to the machine address under <new_addr>. <new_addr> will be
456 * redirected to the null entry.
458 * 1. The call may fail in an undefined manner if either mapping is not
459 * tracked by <handle>.
460 * 2. After executing a batch of unmaps, it is guaranteed that no stale
461 * mappings will remain in the device or host TLBs.
463 #define GNTTABOP_unmap_and_replace 7
464 struct gnttab_unmap_and_replace {
468 grant_handle_t handle;
469 /* OUT parameters. */
470 int16_t status; /* GNTST_* */
472 DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_and_replace);
473 typedef struct gnttab_unmap_and_replace gnttab_unmap_and_replace_t;
474 DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_and_replace_t);
476 #if defined(CONFIG_PARAVIRT_XEN) || __XEN_INTERFACE_VERSION__ >= 0x0003020a
478 * GNTTABOP_set_version: Request a particular version of the grant
479 * table shared table structure. This operation can only be performed
480 * once in any given domain. It must be performed before any grants
481 * are activated; otherwise, the domain will be stuck with version 1.
482 * The only defined versions are 1 and 2.
484 #define GNTTABOP_set_version 8
485 struct gnttab_set_version {
486 /* IN/OUT parameters */
489 DEFINE_GUEST_HANDLE_STRUCT(gnttab_set_version);
490 typedef struct gnttab_set_version gnttab_set_version_t;
491 DEFINE_XEN_GUEST_HANDLE(gnttab_set_version_t);
495 * GNTTABOP_get_status_frames: Get the list of frames used to store grant
496 * status for <dom>. In grant format version 2, the status is separated
497 * from the other shared grant fields to allow more efficient synchronization
498 * using barriers instead of atomic cmpexch operations.
499 * <nr_frames> specify the size of vector <frame_list>.
500 * The frame addresses are returned in the <frame_list>.
501 * Only <nr_frames> addresses are returned, even if the table is larger.
503 * 1. <dom> may be specified as DOMID_SELF.
504 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
506 #define GNTTABOP_get_status_frames 9
507 struct gnttab_get_status_frames {
511 /* OUT parameters. */
512 int16_t status; /* GNTST_* */
513 XEN_GUEST_HANDLE(uint64_t) frame_list;
515 DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_status_frames);
516 typedef struct gnttab_get_status_frames gnttab_get_status_frames_t;
517 DEFINE_XEN_GUEST_HANDLE(gnttab_get_status_frames_t);
520 * GNTTABOP_get_version: Get the grant table version which is in
521 * effect for domain <dom>.
523 #define GNTTABOP_get_version 10
524 struct gnttab_get_version {
531 DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
532 typedef struct gnttab_get_version gnttab_get_version_t;
533 DEFINE_XEN_GUEST_HANDLE(gnttab_get_version_t);
536 * GNTTABOP_swap_grant_ref: Swap the contents of two grant entries.
538 #define GNTTABOP_swap_grant_ref 11
539 struct gnttab_swap_grant_ref {
544 int16_t status; /* GNTST_* */
546 typedef struct gnttab_swap_grant_ref gnttab_swap_grant_ref_t;
547 DEFINE_XEN_GUEST_HANDLE(gnttab_swap_grant_ref_t);
549 #endif /* __XEN_INTERFACE_VERSION__ */
552 * Bitfield values for gnttab_map_grant_ref.flags.
554 /* Map the grant entry for access by I/O devices. */
555 #define _GNTMAP_device_map (0)
556 #define GNTMAP_device_map (1<<_GNTMAP_device_map)
557 /* Map the grant entry for access by host CPUs. */
558 #define _GNTMAP_host_map (1)
559 #define GNTMAP_host_map (1<<_GNTMAP_host_map)
560 /* Accesses to the granted frame will be restricted to read-only access. */
561 #define _GNTMAP_readonly (2)
562 #define GNTMAP_readonly (1<<_GNTMAP_readonly)
564 * GNTMAP_host_map subflag:
565 * 0 => The host mapping is usable only by the guest OS.
566 * 1 => The host mapping is usable by guest OS + current application.
568 #define _GNTMAP_application_map (3)
569 #define GNTMAP_application_map (1<<_GNTMAP_application_map)
572 * GNTMAP_contains_pte subflag:
573 * 0 => This map request contains a host virtual address.
574 * 1 => This map request contains the machine addess of the PTE to update.
576 #define _GNTMAP_contains_pte (4)
577 #define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte)
579 #define _GNTMAP_can_fail (5)
580 #define GNTMAP_can_fail (1<<_GNTMAP_can_fail)
583 * Bits to be placed in guest kernel available PTE bits (architecture
584 * dependent; only supported when XENFEAT_gnttab_map_avail_bits is set).
586 #define _GNTMAP_guest_avail0 (16)
587 #define GNTMAP_guest_avail_mask ((uint32_t)~0 << _GNTMAP_guest_avail0)
590 * Values for error status returns. All errors are -ve.
592 #define GNTST_okay (0) /* Normal return. */
593 #define GNTST_general_error (-1) /* General undefined error. */
594 #define GNTST_bad_domain (-2) /* Unrecognsed domain id. */
595 #define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */
596 #define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */
597 #define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */
598 #define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/
599 #define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */
600 #define GNTST_permission_denied (-8) /* Not enough privilege for operation. */
601 #define GNTST_bad_page (-9) /* Specified page was invalid for op. */
602 #define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary. */
603 #define GNTST_address_too_big (-11) /* transfer page address too large. */
604 #define GNTST_eagain (-12) /* Operation not done; try again. */
606 #define GNTTABOP_error_msgs { \
609 "unrecognised domain id", \
610 "invalid grant reference", \
611 "invalid mapping handle", \
612 "invalid virtual address", \
613 "invalid device address", \
614 "no spare translation slot in the I/O MMU", \
615 "permission denied", \
617 "copy arguments cross page boundary", \
618 "page address size too large", \
619 "operation not done; try again" \
622 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */