grant_table.h revision 181624
1248484Sneel/******************************************************************************
2248484Sneel * grant_table.h
3248484Sneel *
4248484Sneel * Interface for granting foreign access to page frames, and receiving
5248484Sneel * page-ownership transfers.
6248484Sneel *
7248484Sneel * Permission is hereby granted, free of charge, to any person obtaining a copy
8248484Sneel * of this software and associated documentation files (the "Software"), to
9248484Sneel * deal in the Software without restriction, including without limitation the
10248484Sneel * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11248484Sneel * sell copies of the Software, and to permit persons to whom the Software is
12248484Sneel * furnished to do so, subject to the following conditions:
13248484Sneel *
14248484Sneel * The above copyright notice and this permission notice shall be included in
15248484Sneel * all copies or substantial portions of the Software.
16248484Sneel *
17248484Sneel * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18248484Sneel * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19248484Sneel * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20248484Sneel * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21248484Sneel * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22248484Sneel * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23248484Sneel * DEALINGS IN THE SOFTWARE.
24248484Sneel *
25248484Sneel * Copyright (c) 2004, K A Fraser
26248484Sneel */
27248484Sneel
28248484Sneel#ifndef __XEN_PUBLIC_GRANT_TABLE_H__
29248484Sneel#define __XEN_PUBLIC_GRANT_TABLE_H__
30248484Sneel
31248484Sneel
32248484Sneel/***********************************
33248484Sneel * GRANT TABLE REPRESENTATION
34256176Sneel */
35248484Sneel
36248484Sneel/* Some rough guidelines on accessing and updating grant-table entries
37248484Sneel * in a concurrency-safe manner. For more information, Linux contains a
38248484Sneel * reference implementation for guest OSes (arch/xen/kernel/grant_table.c).
39248484Sneel *
40248484Sneel * NB. WMB is a no-op on current-generation x86 processors. However, a
41248484Sneel *     compiler barrier will still be required.
42256657Sneel *
43248484Sneel * Introducing a valid entry into the grant table:
44248484Sneel *  1. Write ent->domid.
45248484Sneel *  2. Write ent->frame:
46248484Sneel *      GTF_permit_access:   Frame to which access is permitted.
47256657Sneel *      GTF_accept_transfer: Pseudo-phys frame slot being filled by new
48248840Sneel *                           frame, or zero if none.
49248484Sneel *  3. Write memory barrier (WMB).
50248484Sneel *  4. Write ent->flags, inc. valid type.
51256176Sneel *
52248484Sneel * Invalidating an unused GTF_permit_access entry:
53248484Sneel *  1. flags = ent->flags.
54248484Sneel *  2. Observe that !(flags & (GTF_reading|GTF_writing)).
55248484Sneel *  3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
56248484Sneel *  NB. No need for WMB as reuse of entry is control-dependent on success of
57248484Sneel *      step 3, and all architectures guarantee ordering of ctrl-dep writes.
58248484Sneel *
59248484Sneel * Invalidating an in-use GTF_permit_access entry:
60248484Sneel *  This cannot be done directly. Request assistance from the domain controller
61248484Sneel *  which can set a timeout on the use of a grant entry and take necessary
62248484Sneel *  action. (NB. This is not yet implemented!).
63248484Sneel *
64248484Sneel * Invalidating an unused GTF_accept_transfer entry:
65248484Sneel *  1. flags = ent->flags.
66248484Sneel *  2. Observe that !(flags & GTF_transfer_committed). [*]
67248484Sneel *  3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
68248484Sneel *  NB. No need for WMB as reuse of entry is control-dependent on success of
69248484Sneel *      step 3, and all architectures guarantee ordering of ctrl-dep writes.
70248484Sneel *  [*] If GTF_transfer_committed is set then the grant entry is 'committed'.
71248484Sneel *      The guest must /not/ modify the grant entry until the address of the
72248484Sneel *      transferred frame is written. It is safe for the guest to spin waiting
73248484Sneel *      for this to occur (detect by observing GTF_transfer_completed in
74248484Sneel *      ent->flags).
75248484Sneel *
76248840Sneel * Invalidating a committed GTF_accept_transfer entry:
77256657Sneel *  1. Wait for (ent->flags & GTF_transfer_completed).
78248484Sneel *
79256657Sneel * Changing a GTF_permit_access from writable to read-only:
80248484Sneel *  Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.
81248484Sneel *
82248484Sneel * Changing a GTF_permit_access from read-only to writable:
83248484Sneel *  Use SMP-safe bit-setting instruction.
84248484Sneel */
85248484Sneel
86248484Sneel/*
87248484Sneel * A grant table comprises a packed array of grant entries in one or more
88248484Sneel * page frames shared between Xen and a guest.
89248484Sneel * [XEN]: This field is written by Xen and read by the sharing guest.
90256657Sneel * [GST]: This field is written by the guest and read by Xen.
91256657Sneel */
92256657Sneelstruct grant_entry {
93248840Sneel    /* GTF_xxx: various type and flag information.  [XEN,GST] */
94248840Sneel    uint16_t flags;
95248484Sneel    /* The domain being granted foreign privileges. [GST] */
96248484Sneel    domid_t  domid;
97248484Sneel    /*
98248484Sneel     * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
99248484Sneel     * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
100248484Sneel     */
101248484Sneel    uint32_t frame;
102248484Sneel};
103248484Sneeltypedef struct grant_entry grant_entry_t;
104248484Sneel
105248484Sneel/*
106248484Sneel * Type of grant entry.
107248484Sneel *  GTF_invalid: This grant entry grants no privileges.
108248484Sneel *  GTF_permit_access: Allow @domid to map/access @frame.
109248484Sneel *  GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
110248484Sneel *                       to this guest. Xen writes the page number to @frame.
111248484Sneel */
112248484Sneel#define GTF_invalid         (0U<<0)
113248484Sneel#define GTF_permit_access   (1U<<0)
114248484Sneel#define GTF_accept_transfer (2U<<0)
115248484Sneel#define GTF_type_mask       (3U<<0)
116248484Sneel
117248484Sneel/*
118248484Sneel * Subflags for GTF_permit_access.
119248484Sneel *  GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
120248484Sneel *  GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
121248484Sneel *  GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
122248484Sneel */
123248484Sneel#define _GTF_readonly       (2)
124248484Sneel#define GTF_readonly        (1U<<_GTF_readonly)
125248484Sneel#define _GTF_reading        (3)
126248484Sneel#define GTF_reading         (1U<<_GTF_reading)
127248484Sneel#define _GTF_writing        (4)
128248484Sneel#define GTF_writing         (1U<<_GTF_writing)
129248484Sneel
130248484Sneel/*
131248484Sneel * Subflags for GTF_accept_transfer:
132248484Sneel *  GTF_transfer_committed: Xen sets this flag to indicate that it is committed
133248484Sneel *      to transferring ownership of a page frame. When a guest sees this flag
134248484Sneel *      it must /not/ modify the grant entry until GTF_transfer_completed is
135248484Sneel *      set by Xen.
136248484Sneel *  GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
137248484Sneel *      after reading GTF_transfer_committed. Xen will always write the frame
138248484Sneel *      address, followed by ORing this flag, in a timely manner.
139248484Sneel */
140248484Sneel#define _GTF_transfer_committed (2)
141248484Sneel#define GTF_transfer_committed  (1U<<_GTF_transfer_committed)
142248484Sneel#define _GTF_transfer_completed (3)
143248484Sneel#define GTF_transfer_completed  (1U<<_GTF_transfer_completed)
144248484Sneel
145248484Sneel
146248484Sneel/***********************************
147248484Sneel * GRANT TABLE QUERIES AND USES
148248484Sneel */
149248484Sneel
150248484Sneel/*
151248484Sneel * Reference to a grant entry in a specified domain's grant table.
152248484Sneel */
153248484Sneeltypedef uint32_t grant_ref_t;
154248484Sneel
155248484Sneel/*
156248484Sneel * Handle to track a mapping created via a grant reference.
157248484Sneel */
158248484Sneeltypedef uint32_t grant_handle_t;
159248484Sneel
160248484Sneel/*
161248484Sneel * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
162248484Sneel * by devices and/or host CPUs. If successful, <handle> is a tracking number
163248484Sneel * that must be presented later to destroy the mapping(s). On error, <handle>
164248484Sneel * is a negative status code.
165248484Sneel * NOTES:
166248484Sneel *  1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
167248484Sneel *     via which I/O devices may access the granted frame.
168248484Sneel *  2. If GNTMAP_host_map is specified then a mapping will be added at
169248484Sneel *     either a host virtual address in the current address space, or at
170248484Sneel *     a PTE at the specified machine address.  The type of mapping to
171256657Sneel *     perform is selected through the GNTMAP_contains_pte flag, and the
172248484Sneel *     address is specified in <host_addr>.
173248484Sneel *  3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
174248484Sneel *     host mapping is destroyed by other means then it is *NOT* guaranteed
175248484Sneel *     to be accounted to the correct grant reference!
176248840Sneel */
177248840Sneel#define GNTTABOP_map_grant_ref        0
178248484Sneelstruct gnttab_map_grant_ref {
179248484Sneel    /* IN parameters. */
180248484Sneel    uint64_t host_addr;
181248484Sneel    uint32_t flags;               /* GNTMAP_* */
182248484Sneel    grant_ref_t ref;
183248484Sneel    domid_t  dom;
184248484Sneel    /* OUT parameters. */
185248484Sneel    int16_t  status;              /* GNTST_* */
186248484Sneel    grant_handle_t handle;
187248484Sneel    uint64_t dev_bus_addr;
188248484Sneel};
189248484Sneeltypedef struct gnttab_map_grant_ref gnttab_map_grant_ref_t;
190DEFINE_XEN_GUEST_HANDLE(gnttab_map_grant_ref_t);
191
192/*
193 * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
194 * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
195 * field is ignored. If non-zero, they must refer to a device/host mapping
196 * that is tracked by <handle>
197 * NOTES:
198 *  1. The call may fail in an undefined manner if either mapping is not
199 *     tracked by <handle>.
200 *  3. After executing a batch of unmaps, it is guaranteed that no stale
201 *     mappings will remain in the device or host TLBs.
202 */
203#define GNTTABOP_unmap_grant_ref      1
204struct gnttab_unmap_grant_ref {
205    /* IN parameters. */
206    uint64_t host_addr;
207    uint64_t dev_bus_addr;
208    grant_handle_t handle;
209    /* OUT parameters. */
210    int16_t  status;              /* GNTST_* */
211};
212typedef struct gnttab_unmap_grant_ref gnttab_unmap_grant_ref_t;
213DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_grant_ref_t);
214
215/*
216 * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
217 * <nr_frames> pages. The frame addresses are written to the <frame_list>.
218 * Only <nr_frames> addresses are written, even if the table is larger.
219 * NOTES:
220 *  1. <dom> may be specified as DOMID_SELF.
221 *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
222 *  3. Xen may not support more than a single grant-table page per domain.
223 */
224#define GNTTABOP_setup_table          2
225struct gnttab_setup_table {
226    /* IN parameters. */
227    domid_t  dom;
228    uint32_t nr_frames;
229    /* OUT parameters. */
230    int16_t  status;              /* GNTST_* */
231    XEN_GUEST_HANDLE(ulong) frame_list;
232};
233typedef struct gnttab_setup_table gnttab_setup_table_t;
234DEFINE_XEN_GUEST_HANDLE(gnttab_setup_table_t);
235
236/*
237 * GNTTABOP_dump_table: Dump the contents of the grant table to the
238 * xen console. Debugging use only.
239 */
240#define GNTTABOP_dump_table           3
241struct gnttab_dump_table {
242    /* IN parameters. */
243    domid_t dom;
244    /* OUT parameters. */
245    int16_t status;               /* GNTST_* */
246};
247typedef struct gnttab_dump_table gnttab_dump_table_t;
248DEFINE_XEN_GUEST_HANDLE(gnttab_dump_table_t);
249
250/*
251 * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
252 * foreign domain has previously registered its interest in the transfer via
253 * <domid, ref>.
254 *
255 * Note that, even if the transfer fails, the specified page no longer belongs
256 * to the calling domain *unless* the error is GNTST_bad_page.
257 */
258#define GNTTABOP_transfer                4
259struct gnttab_transfer {
260    /* IN parameters. */
261    xen_pfn_t     mfn;
262    domid_t       domid;
263    grant_ref_t   ref;
264    /* OUT parameters. */
265    int16_t       status;
266};
267typedef struct gnttab_transfer gnttab_transfer_t;
268DEFINE_XEN_GUEST_HANDLE(gnttab_transfer_t);
269
270
271/*
272 * GNTTABOP_copy: Hypervisor based copy
273 * source and destinations can be eithers MFNs or, for foreign domains,
274 * grant references. the foreign domain has to grant read/write access
275 * in its grant table.
276 *
277 * The flags specify what type source and destinations are (either MFN
278 * or grant reference).
279 *
280 * Note that this can also be used to copy data between two domains
281 * via a third party if the source and destination domains had previously
282 * grant appropriate access to their pages to the third party.
283 *
284 * source_offset specifies an offset in the source frame, dest_offset
285 * the offset in the target frame and  len specifies the number of
286 * bytes to be copied.
287 */
288
289#define _GNTCOPY_source_gref      (0)
290#define GNTCOPY_source_gref       (1<<_GNTCOPY_source_gref)
291#define _GNTCOPY_dest_gref        (1)
292#define GNTCOPY_dest_gref         (1<<_GNTCOPY_dest_gref)
293
294#define GNTTABOP_copy                 5
295typedef struct gnttab_copy {
296    /* IN parameters. */
297    struct {
298        union {
299            grant_ref_t ref;
300            xen_pfn_t   gmfn;
301        } u;
302        domid_t  domid;
303        uint16_t offset;
304    } source, dest;
305    uint16_t      len;
306    uint16_t      flags;          /* GNTCOPY_* */
307    /* OUT parameters. */
308    int16_t       status;
309} gnttab_copy_t;
310DEFINE_XEN_GUEST_HANDLE(gnttab_copy_t);
311
312/*
313 * GNTTABOP_query_size: Query the current and maximum sizes of the shared
314 * grant table.
315 * NOTES:
316 *  1. <dom> may be specified as DOMID_SELF.
317 *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
318 */
319#define GNTTABOP_query_size           6
320struct gnttab_query_size {
321    /* IN parameters. */
322    domid_t  dom;
323    /* OUT parameters. */
324    uint32_t nr_frames;
325    uint32_t max_nr_frames;
326    int16_t  status;              /* GNTST_* */
327};
328typedef struct gnttab_query_size gnttab_query_size_t;
329DEFINE_XEN_GUEST_HANDLE(gnttab_query_size_t);
330
331
332/*
333 * Bitfield values for update_pin_status.flags.
334 */
335 /* Map the grant entry for access by I/O devices. */
336#define _GNTMAP_device_map      (0)
337#define GNTMAP_device_map       (1<<_GNTMAP_device_map)
338 /* Map the grant entry for access by host CPUs. */
339#define _GNTMAP_host_map        (1)
340#define GNTMAP_host_map         (1<<_GNTMAP_host_map)
341 /* Accesses to the granted frame will be restricted to read-only access. */
342#define _GNTMAP_readonly        (2)
343#define GNTMAP_readonly         (1<<_GNTMAP_readonly)
344 /*
345  * GNTMAP_host_map subflag:
346  *  0 => The host mapping is usable only by the guest OS.
347  *  1 => The host mapping is usable by guest OS + current application.
348  */
349#define _GNTMAP_application_map (3)
350#define GNTMAP_application_map  (1<<_GNTMAP_application_map)
351
352 /*
353  * GNTMAP_contains_pte subflag:
354  *  0 => This map request contains a host virtual address.
355  *  1 => This map request contains the machine addess of the PTE to update.
356  */
357#define _GNTMAP_contains_pte    (4)
358#define GNTMAP_contains_pte     (1<<_GNTMAP_contains_pte)
359
360/*
361 * Values for error status returns. All errors are -ve.
362 */
363#define GNTST_okay             (0)  /* Normal return.                        */
364#define GNTST_general_error    (-1) /* General undefined error.              */
365#define GNTST_bad_domain       (-2) /* Unrecognsed domain id.                */
366#define GNTST_bad_gntref       (-3) /* Unrecognised or inappropriate gntref. */
367#define GNTST_bad_handle       (-4) /* Unrecognised or inappropriate handle. */
368#define GNTST_bad_virt_addr    (-5) /* Inappropriate virtual address to map. */
369#define GNTST_bad_dev_addr     (-6) /* Inappropriate device address to unmap.*/
370#define GNTST_no_device_space  (-7) /* Out of space in I/O MMU.              */
371#define GNTST_permission_denied (-8) /* Not enough privilege for operation.  */
372#define GNTST_bad_page         (-9) /* Specified page was invalid for op.    */
373#define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary.   */
374#define GNTST_address_too_big (-11) /* transfer page address too large.      */
375
376#define GNTTABOP_error_msgs {                   \
377    "okay",                                     \
378    "undefined error",                          \
379    "unrecognised domain id",                   \
380    "invalid grant reference",                  \
381    "invalid mapping handle",                   \
382    "invalid virtual address",                  \
383    "invalid device address",                   \
384    "no spare translation slot in the I/O MMU", \
385    "permission denied",                        \
386    "bad page",                                 \
387    "copy arguments cross page boundary",       \
388    "page address size too large"               \
389}
390
391#endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
392
393/*
394 * Local variables:
395 * mode: C
396 * c-set-style: "BSD"
397 * c-basic-offset: 4
398 * tab-width: 4
399 * indent-tabs-mode: nil
400 * End:
401 */
402