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 struct grant_entry_v1
{
104 /* GTF_xxx: various type and flag information. [XEN,GST] */
106 /* The domain being granted foreign privileges. [GST] */
109 * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
110 * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
116 * Type of grant entry.
117 * GTF_invalid: This grant entry grants no privileges.
118 * GTF_permit_access: Allow @domid to map/access @frame.
119 * GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
120 * to this guest. Xen writes the page number to @frame.
121 * GTF_transitive: Allow @domid to transitively access a subrange of
122 * @trans_grant in @trans_domid. No mappings are allowed.
124 #define GTF_invalid (0U<<0)
125 #define GTF_permit_access (1U<<0)
126 #define GTF_accept_transfer (2U<<0)
127 #define GTF_transitive (3U<<0)
128 #define GTF_type_mask (3U<<0)
131 * Subflags for GTF_permit_access.
132 * GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
133 * GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
134 * GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
135 * GTF_sub_page: Grant access to only a subrange of the page. @domid
136 * will only be allowed to copy from the grant, and not
139 #define _GTF_readonly (2)
140 #define GTF_readonly (1U<<_GTF_readonly)
141 #define _GTF_reading (3)
142 #define GTF_reading (1U<<_GTF_reading)
143 #define _GTF_writing (4)
144 #define GTF_writing (1U<<_GTF_writing)
145 #define _GTF_sub_page (8)
146 #define GTF_sub_page (1U<<_GTF_sub_page)
149 * Subflags for GTF_accept_transfer:
150 * GTF_transfer_committed: Xen sets this flag to indicate that it is committed
151 * to transferring ownership of a page frame. When a guest sees this flag
152 * it must /not/ modify the grant entry until GTF_transfer_completed is
154 * GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
155 * after reading GTF_transfer_committed. Xen will always write the frame
156 * address, followed by ORing this flag, in a timely manner.
158 #define _GTF_transfer_committed (2)
159 #define GTF_transfer_committed (1U<<_GTF_transfer_committed)
160 #define _GTF_transfer_completed (3)
161 #define GTF_transfer_completed (1U<<_GTF_transfer_completed)
164 * Version 2 grant table entries. These fulfil the same role as
165 * version 1 entries, but can represent more complicated operations.
166 * Any given domain will have either a version 1 or a version 2 table,
167 * and every entry in the table will be the same version.
169 * The interface by which domains use grant references does not depend
170 * on the grant table version in use by the other domain.
174 * Version 1 and version 2 grant entries share a common prefix. The
175 * fields of the prefix are documented as part of struct
178 struct grant_entry_header
{
184 * Version 2 of the grant entry structure, here is a union because three
185 * different types are suppotted: full_page, sub_page and transitive.
187 union grant_entry_v2
{
188 struct grant_entry_header hdr
;
191 * This member is used for V1-style full page grants, where either:
193 * -- hdr.type is GTF_accept_transfer, or
194 * -- hdr.type is GTF_permit_access and GTF_sub_page is not set.
196 * In that case, the frame field has the same semantics as the
197 * field of the same name in the V1 entry structure.
200 struct grant_entry_header hdr
;
206 * If the grant type is GTF_grant_access and GTF_sub_page is set,
207 * @domid is allowed to access bytes [@page_off,@page_off+@length)
211 struct grant_entry_header hdr
;
218 * If the grant is GTF_transitive, @domid is allowed to use the
219 * grant @gref in domain @trans_domid, as if it was the local
220 * domain. Obviously, the transitive access must be compatible
221 * with the original grant.
224 struct grant_entry_header hdr
;
230 uint32_t __spacer
[4]; /* Pad to a power of two */
233 typedef uint16_t grant_status_t
;
235 /***********************************
236 * GRANT TABLE QUERIES AND USES
240 * Handle to track a mapping created via a grant reference.
242 typedef uint32_t grant_handle_t
;
245 * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
246 * by devices and/or host CPUs. If successful, <handle> is a tracking number
247 * that must be presented later to destroy the mapping(s). On error, <handle>
248 * is a negative status code.
250 * 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
251 * via which I/O devices may access the granted frame.
252 * 2. If GNTMAP_host_map is specified then a mapping will be added at
253 * either a host virtual address in the current address space, or at
254 * a PTE at the specified machine address. The type of mapping to
255 * perform is selected through the GNTMAP_contains_pte flag, and the
256 * address is specified in <host_addr>.
257 * 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
258 * host mapping is destroyed by other means then it is *NOT* guaranteed
259 * to be accounted to the correct grant reference!
261 #define GNTTABOP_map_grant_ref 0
262 struct gnttab_map_grant_ref
{
265 uint32_t flags
; /* GNTMAP_* */
268 /* OUT parameters. */
269 int16_t status
; /* GNTST_* */
270 grant_handle_t handle
;
271 uint64_t dev_bus_addr
;
273 DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref
);
276 * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
277 * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
278 * field is ignored. If non-zero, they must refer to a device/host mapping
279 * that is tracked by <handle>
281 * 1. The call may fail in an undefined manner if either mapping is not
282 * tracked by <handle>.
283 * 3. After executing a batch of unmaps, it is guaranteed that no stale
284 * mappings will remain in the device or host TLBs.
286 #define GNTTABOP_unmap_grant_ref 1
287 struct gnttab_unmap_grant_ref
{
290 uint64_t dev_bus_addr
;
291 grant_handle_t handle
;
292 /* OUT parameters. */
293 int16_t status
; /* GNTST_* */
295 DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref
);
298 * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
299 * <nr_frames> pages. The frame addresses are written to the <frame_list>.
300 * Only <nr_frames> addresses are written, even if the table is larger.
302 * 1. <dom> may be specified as DOMID_SELF.
303 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
304 * 3. Xen may not support more than a single grant-table page per domain.
306 #define GNTTABOP_setup_table 2
307 struct gnttab_setup_table
{
311 /* OUT parameters. */
312 int16_t status
; /* GNTST_* */
313 GUEST_HANDLE(xen_pfn_t
) frame_list
;
315 DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table
);
318 * GNTTABOP_dump_table: Dump the contents of the grant table to the
319 * xen console. Debugging use only.
321 #define GNTTABOP_dump_table 3
322 struct gnttab_dump_table
{
325 /* OUT parameters. */
326 int16_t status
; /* GNTST_* */
328 DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table
);
331 * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
332 * foreign domain has previously registered its interest in the transfer via
335 * Note that, even if the transfer fails, the specified page no longer belongs
336 * to the calling domain *unless* the error is GNTST_bad_page.
338 #define GNTTABOP_transfer 4
339 struct gnttab_transfer
{
344 /* OUT parameters. */
347 DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer
);
350 * GNTTABOP_copy: Hypervisor based copy
351 * source and destinations can be eithers MFNs or, for foreign domains,
352 * grant references. the foreign domain has to grant read/write access
353 * in its grant table.
355 * The flags specify what type source and destinations are (either MFN
356 * or grant reference).
358 * Note that this can also be used to copy data between two domains
359 * via a third party if the source and destination domains had previously
360 * grant appropriate access to their pages to the third party.
362 * source_offset specifies an offset in the source frame, dest_offset
363 * the offset in the target frame and len specifies the number of
364 * bytes to be copied.
367 #define _GNTCOPY_source_gref (0)
368 #define GNTCOPY_source_gref (1<<_GNTCOPY_source_gref)
369 #define _GNTCOPY_dest_gref (1)
370 #define GNTCOPY_dest_gref (1<<_GNTCOPY_dest_gref)
372 #define GNTTABOP_copy 5
384 uint16_t flags
; /* GNTCOPY_* */
385 /* OUT parameters. */
388 DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy
);
391 * GNTTABOP_query_size: Query the current and maximum sizes of the shared
394 * 1. <dom> may be specified as DOMID_SELF.
395 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
397 #define GNTTABOP_query_size 6
398 struct gnttab_query_size
{
401 /* OUT parameters. */
403 uint32_t max_nr_frames
;
404 int16_t status
; /* GNTST_* */
406 DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size
);
409 * GNTTABOP_unmap_and_replace: Destroy one or more grant-reference mappings
410 * tracked by <handle> but atomically replace the page table entry with one
411 * pointing to the machine address under <new_addr>. <new_addr> will be
412 * redirected to the null entry.
414 * 1. The call may fail in an undefined manner if either mapping is not
415 * tracked by <handle>.
416 * 2. After executing a batch of unmaps, it is guaranteed that no stale
417 * mappings will remain in the device or host TLBs.
419 #define GNTTABOP_unmap_and_replace 7
420 struct gnttab_unmap_and_replace
{
424 grant_handle_t handle
;
425 /* OUT parameters. */
426 int16_t status
; /* GNTST_* */
428 DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_and_replace
);
431 * GNTTABOP_set_version: Request a particular version of the grant
432 * table shared table structure. This operation can only be performed
433 * once in any given domain. It must be performed before any grants
434 * are activated; otherwise, the domain will be stuck with version 1.
435 * The only defined versions are 1 and 2.
437 #define GNTTABOP_set_version 8
438 struct gnttab_set_version
{
442 DEFINE_GUEST_HANDLE_STRUCT(gnttab_set_version
);
445 * GNTTABOP_get_status_frames: Get the list of frames used to store grant
446 * status for <dom>. In grant format version 2, the status is separated
447 * from the other shared grant fields to allow more efficient synchronization
448 * using barriers instead of atomic cmpexch operations.
449 * <nr_frames> specify the size of vector <frame_list>.
450 * The frame addresses are returned in the <frame_list>.
451 * Only <nr_frames> addresses are returned, even if the table is larger.
453 * 1. <dom> may be specified as DOMID_SELF.
454 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
456 #define GNTTABOP_get_status_frames 9
457 struct gnttab_get_status_frames
{
461 /* OUT parameters. */
462 int16_t status
; /* GNTST_* */
463 GUEST_HANDLE(uint64_t) frame_list
;
465 DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_status_frames
);
468 * GNTTABOP_get_version: Get the grant table version which is in
469 * effect for domain <dom>.
471 #define GNTTABOP_get_version 10
472 struct gnttab_get_version
{
479 DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version
);
482 * Issue one or more cache maintenance operations on a portion of a
483 * page granted to the calling domain by a foreign domain.
485 #define GNTTABOP_cache_flush 12
486 struct gnttab_cache_flush
{
488 uint64_t dev_bus_addr
;
491 uint16_t offset
; /* offset from start of grant */
492 uint16_t length
; /* size within the grant */
493 #define GNTTAB_CACHE_CLEAN (1<<0)
494 #define GNTTAB_CACHE_INVAL (1<<1)
495 #define GNTTAB_CACHE_SOURCE_GREF (1<<31)
498 DEFINE_GUEST_HANDLE_STRUCT(gnttab_cache_flush
);
501 * Bitfield values for update_pin_status.flags.
503 /* Map the grant entry for access by I/O devices. */
504 #define _GNTMAP_device_map (0)
505 #define GNTMAP_device_map (1<<_GNTMAP_device_map)
506 /* Map the grant entry for access by host CPUs. */
507 #define _GNTMAP_host_map (1)
508 #define GNTMAP_host_map (1<<_GNTMAP_host_map)
509 /* Accesses to the granted frame will be restricted to read-only access. */
510 #define _GNTMAP_readonly (2)
511 #define GNTMAP_readonly (1<<_GNTMAP_readonly)
513 * GNTMAP_host_map subflag:
514 * 0 => The host mapping is usable only by the guest OS.
515 * 1 => The host mapping is usable by guest OS + current application.
517 #define _GNTMAP_application_map (3)
518 #define GNTMAP_application_map (1<<_GNTMAP_application_map)
521 * GNTMAP_contains_pte subflag:
522 * 0 => This map request contains a host virtual address.
523 * 1 => This map request contains the machine addess of the PTE to update.
525 #define _GNTMAP_contains_pte (4)
526 #define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte)
529 * Bits to be placed in guest kernel available PTE bits (architecture
530 * dependent; only supported when XENFEAT_gnttab_map_avail_bits is set).
532 #define _GNTMAP_guest_avail0 (16)
533 #define GNTMAP_guest_avail_mask ((uint32_t)~0 << _GNTMAP_guest_avail0)
536 * Values for error status returns. All errors are -ve.
538 #define GNTST_okay (0) /* Normal return. */
539 #define GNTST_general_error (-1) /* General undefined error. */
540 #define GNTST_bad_domain (-2) /* Unrecognsed domain id. */
541 #define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */
542 #define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */
543 #define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */
544 #define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/
545 #define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */
546 #define GNTST_permission_denied (-8) /* Not enough privilege for operation. */
547 #define GNTST_bad_page (-9) /* Specified page was invalid for op. */
548 #define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary. */
549 #define GNTST_address_too_big (-11) /* transfer page address too large. */
550 #define GNTST_eagain (-12) /* Operation not done; try again. */
552 #define GNTTABOP_error_msgs { \
555 "unrecognised domain id", \
556 "invalid grant reference", \
557 "invalid mapping handle", \
558 "invalid virtual address", \
559 "invalid device address", \
560 "no spare translation slot in the I/O MMU", \
561 "permission denied", \
563 "copy arguments cross page boundary", \
564 "page address size too large", \
565 "operation not done; try again" \
568 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */