This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / include / xen / interface / 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  * Copyright (c) 2004, K A Fraser
8  */
9
10 #ifndef __XEN_PUBLIC_GRANT_TABLE_H__
11 #define __XEN_PUBLIC_GRANT_TABLE_H__
12
13
14 /***********************************
15  * GRANT TABLE REPRESENTATION
16  */
17
18 /* Some rough guidelines on accessing and updating grant-table entries
19  * in a concurrency-safe manner. For more information, Linux contains a
20  * reference implementation for guest OSes (arch/xen/kernel/grant_table.c).
21  * 
22  * NB. WMB is a no-op on current-generation x86 processors. However, a
23  *     compiler barrier will still be required.
24  * 
25  * Introducing a valid entry into the grant table:
26  *  1. Write ent->domid.
27  *  2. Write ent->frame:
28  *      GTF_permit_access:   Frame to which access is permitted.
29  *      GTF_accept_transfer: Pseudo-phys frame slot being filled by new
30  *                           frame, or zero if none.
31  *  3. Write memory barrier (WMB).
32  *  4. Write ent->flags, inc. valid type.
33  * 
34  * Invalidating an unused GTF_permit_access entry:
35  *  1. flags = ent->flags.
36  *  2. Observe that !(flags & (GTF_reading|GTF_writing)).
37  *  3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
38  *  NB. No need for WMB as reuse of entry is control-dependent on success of
39  *      step 3, and all architectures guarantee ordering of ctrl-dep writes.
40  *
41  * Invalidating an in-use GTF_permit_access entry:
42  *  This cannot be done directly. Request assistance from the domain controller
43  *  which can set a timeout on the use of a grant entry and take necessary
44  *  action. (NB. This is not yet implemented!).
45  * 
46  * Invalidating an unused GTF_accept_transfer entry:
47  *  1. flags = ent->flags.
48  *  2. Observe that !(flags & GTF_transfer_committed). [*]
49  *  3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
50  *  NB. No need for WMB as reuse of entry is control-dependent on success of
51  *      step 3, and all architectures guarantee ordering of ctrl-dep writes.
52  *  [*] If GTF_transfer_committed is set then the grant entry is 'committed'.
53  *      The guest must /not/ modify the grant entry until the address of the
54  *      transferred frame is written. It is safe for the guest to spin waiting
55  *      for this to occur (detect by observing GTF_transfer_completed in
56  *      ent->flags).
57  *
58  * Invalidating a committed GTF_accept_transfer entry:
59  *  1. Wait for (ent->flags & GTF_transfer_completed).
60  *
61  * Changing a GTF_permit_access from writable to read-only:
62  *  Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.
63  * 
64  * Changing a GTF_permit_access from read-only to writable:
65  *  Use SMP-safe bit-setting instruction.
66  */
67
68 /*
69  * A grant table comprises a packed array of grant entries in one or more
70  * page frames shared between Xen and a guest.
71  * [XEN]: This field is written by Xen and read by the sharing guest.
72  * [GST]: This field is written by the guest and read by Xen.
73  */
74 struct grant_entry {
75     /* GTF_xxx: various type and flag information.  [XEN,GST] */
76     uint16_t flags;
77     /* The domain being granted foreign privileges. [GST] */
78     domid_t  domid;
79     /*
80      * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
81      * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
82      */
83     uint32_t frame;
84 };
85 typedef struct grant_entry grant_entry_t;
86
87 /*
88  * Type of grant entry.
89  *  GTF_invalid: This grant entry grants no privileges.
90  *  GTF_permit_access: Allow @domid to map/access @frame.
91  *  GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
92  *                       to this guest. Xen writes the page number to @frame.
93  */
94 #define GTF_invalid         (0U<<0)
95 #define GTF_permit_access   (1U<<0)
96 #define GTF_accept_transfer (2U<<0)
97 #define GTF_type_mask       (3U<<0)
98
99 /*
100  * Subflags for GTF_permit_access.
101  *  GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
102  *  GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
103  *  GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
104  */
105 #define _GTF_readonly       (2)
106 #define GTF_readonly        (1U<<_GTF_readonly)
107 #define _GTF_reading        (3)
108 #define GTF_reading         (1U<<_GTF_reading)
109 #define _GTF_writing        (4)
110 #define GTF_writing         (1U<<_GTF_writing)
111
112 /*
113  * Subflags for GTF_accept_transfer:
114  *  GTF_transfer_committed: Xen sets this flag to indicate that it is committed
115  *      to transferring ownership of a page frame. When a guest sees this flag
116  *      it must /not/ modify the grant entry until GTF_transfer_completed is
117  *      set by Xen.
118  *  GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
119  *      after reading GTF_transfer_committed. Xen will always write the frame
120  *      address, followed by ORing this flag, in a timely manner.
121  */
122 #define _GTF_transfer_committed (2)
123 #define GTF_transfer_committed  (1U<<_GTF_transfer_committed)
124 #define _GTF_transfer_completed (3)
125 #define GTF_transfer_completed  (1U<<_GTF_transfer_completed)
126
127
128 /***********************************
129  * GRANT TABLE QUERIES AND USES
130  */
131
132 /*
133  * Reference to a grant entry in a specified domain's grant table.
134  */
135 typedef uint32_t grant_ref_t;
136
137 /*
138  * Handle to track a mapping created via a grant reference.
139  */
140 typedef uint32_t grant_handle_t;
141
142 /*
143  * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
144  * by devices and/or host CPUs. If successful, <handle> is a tracking number
145  * that must be presented later to destroy the mapping(s). On error, <handle>
146  * is a negative status code.
147  * NOTES:
148  *  1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
149  *     via which I/O devices may access the granted frame.
150  *  2. If GNTMAP_host_map is specified then a mapping will be added at
151  *     either a host virtual address in the current address space, or at
152  *     a PTE at the specified machine address.  The type of mapping to
153  *     perform is selected through the GNTMAP_contains_pte flag, and the 
154  *     address is specified in <host_addr>.
155  *  3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
156  *     host mapping is destroyed by other means then it is *NOT* guaranteed
157  *     to be accounted to the correct grant reference!
158  */
159 #define GNTTABOP_map_grant_ref        0
160 struct gnttab_map_grant_ref {
161     /* IN parameters. */
162     uint64_t host_addr;
163     uint32_t flags;               /* GNTMAP_* */
164     grant_ref_t ref;
165     domid_t  dom;
166     /* OUT parameters. */
167     int16_t  status;              /* GNTST_* */
168     grant_handle_t handle;
169     uint64_t dev_bus_addr;
170 };
171 typedef struct gnttab_map_grant_ref gnttab_map_grant_ref_t;
172 DEFINE_XEN_GUEST_HANDLE(gnttab_map_grant_ref_t);
173
174 /*
175  * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
176  * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
177  * field is ignored. If non-zero, they must refer to a device/host mapping
178  * that is tracked by <handle>
179  * NOTES:
180  *  1. The call may fail in an undefined manner if either mapping is not
181  *     tracked by <handle>.
182  *  3. After executing a batch of unmaps, it is guaranteed that no stale
183  *     mappings will remain in the device or host TLBs.
184  */
185 #define GNTTABOP_unmap_grant_ref      1
186 struct gnttab_unmap_grant_ref {
187     /* IN parameters. */
188     uint64_t host_addr;
189     uint64_t dev_bus_addr;
190     grant_handle_t handle;
191     /* OUT parameters. */
192     int16_t  status;              /* GNTST_* */
193 };
194 typedef struct gnttab_unmap_grant_ref gnttab_unmap_grant_ref_t;
195 DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_grant_ref_t);
196
197 /*
198  * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
199  * <nr_frames> pages. The frame addresses are written to the <frame_list>.
200  * Only <nr_frames> addresses are written, even if the table is larger.
201  * NOTES:
202  *  1. <dom> may be specified as DOMID_SELF.
203  *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
204  *  3. Xen may not support more than a single grant-table page per domain.
205  */
206 #define GNTTABOP_setup_table          2
207 struct gnttab_setup_table {
208     /* IN parameters. */
209     domid_t  dom;
210     uint32_t nr_frames;
211     /* OUT parameters. */
212     int16_t  status;              /* GNTST_* */
213     XEN_GUEST_HANDLE(ulong) frame_list;
214 };
215 typedef struct gnttab_setup_table gnttab_setup_table_t;
216 DEFINE_XEN_GUEST_HANDLE(gnttab_setup_table_t);
217
218 /*
219  * GNTTABOP_dump_table: Dump the contents of the grant table to the
220  * xen console. Debugging use only.
221  */
222 #define GNTTABOP_dump_table           3
223 struct gnttab_dump_table {
224     /* IN parameters. */
225     domid_t dom;
226     /* OUT parameters. */
227     int16_t status;               /* GNTST_* */
228 };
229 typedef struct gnttab_dump_table gnttab_dump_table_t;
230 DEFINE_XEN_GUEST_HANDLE(gnttab_dump_table_t);
231
232 /*
233  * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
234  * foreign domain has previously registered its interest in the transfer via
235  * <domid, ref>.
236  * 
237  * Note that, even if the transfer fails, the specified page no longer belongs
238  * to the calling domain *unless* the error is GNTST_bad_page.
239  */
240 #define GNTTABOP_transfer                4
241 struct gnttab_transfer {
242     /* IN parameters. */
243     xen_pfn_t     mfn;
244     domid_t       domid;
245     grant_ref_t   ref;
246     /* OUT parameters. */
247     int16_t       status;
248 };
249 typedef struct gnttab_transfer gnttab_transfer_t;
250 DEFINE_XEN_GUEST_HANDLE(gnttab_transfer_t);
251
252
253 /*
254  * GNTTABOP_copy: Hypervisor based copy
255  * source and destinations can be eithers MFNs or, for foreign domains,
256  * grant references. the foreign domain has to grant read/write access
257  * in its grant table.
258  *
259  * The flags specify what type source and destinations are (either MFN
260  * or grant reference).
261  *
262  * Note that this can also be used to copy data between two domains
263  * via a third party if the source and destination domains had previously
264  * grant appropriate access to their pages to the third party.
265  *
266  * source_offset specifies an offset in the source frame, dest_offset
267  * the offset in the target frame and  len specifies the number of
268  * bytes to be copied.
269  */
270
271 #define _GNTCOPY_source_gref      (0)
272 #define GNTCOPY_source_gref       (1<<_GNTCOPY_source_gref)
273 #define _GNTCOPY_dest_gref        (1)
274 #define GNTCOPY_dest_gref         (1<<_GNTCOPY_dest_gref)
275
276 #define GNTTABOP_copy                 5
277 typedef struct gnttab_copy {
278     /* IN parameters. */
279     struct {
280         union {
281             grant_ref_t ref;
282             xen_pfn_t   gmfn;
283         } u;
284         domid_t  domid;
285         uint16_t offset;
286     } source, dest;
287     uint16_t      len;
288     uint16_t      flags;          /* GNTCOPY_* */
289     /* OUT parameters. */
290     int16_t       status;
291 } gnttab_copy_t;
292 DEFINE_XEN_GUEST_HANDLE(gnttab_copy_t);
293
294
295 /*
296  * Bitfield values for update_pin_status.flags.
297  */
298  /* Map the grant entry for access by I/O devices. */
299 #define _GNTMAP_device_map      (0)
300 #define GNTMAP_device_map       (1<<_GNTMAP_device_map)
301  /* Map the grant entry for access by host CPUs. */
302 #define _GNTMAP_host_map        (1)
303 #define GNTMAP_host_map         (1<<_GNTMAP_host_map)
304  /* Accesses to the granted frame will be restricted to read-only access. */
305 #define _GNTMAP_readonly        (2)
306 #define GNTMAP_readonly         (1<<_GNTMAP_readonly)
307  /*
308   * GNTMAP_host_map subflag:
309   *  0 => The host mapping is usable only by the guest OS.
310   *  1 => The host mapping is usable by guest OS + current application.
311   */
312 #define _GNTMAP_application_map (3)
313 #define GNTMAP_application_map  (1<<_GNTMAP_application_map)
314
315  /*
316   * GNTMAP_contains_pte subflag:
317   *  0 => This map request contains a host virtual address.
318   *  1 => This map request contains the machine addess of the PTE to update.
319   */
320 #define _GNTMAP_contains_pte    (4)
321 #define GNTMAP_contains_pte     (1<<_GNTMAP_contains_pte)
322
323 /*
324  * Values for error status returns. All errors are -ve.
325  */
326 #define GNTST_okay             (0)  /* Normal return.                        */
327 #define GNTST_general_error    (-1) /* General undefined error.              */
328 #define GNTST_bad_domain       (-2) /* Unrecognsed domain id.                */
329 #define GNTST_bad_gntref       (-3) /* Unrecognised or inappropriate gntref. */
330 #define GNTST_bad_handle       (-4) /* Unrecognised or inappropriate handle. */
331 #define GNTST_bad_virt_addr    (-5) /* Inappropriate virtual address to map. */
332 #define GNTST_bad_dev_addr     (-6) /* Inappropriate device address to unmap.*/
333 #define GNTST_no_device_space  (-7) /* Out of space in I/O MMU.              */
334 #define GNTST_permission_denied (-8) /* Not enough privilege for operation.  */
335 #define GNTST_bad_page         (-9) /* Specified page was invalid for op.    */
336 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary */
337
338 #define GNTTABOP_error_msgs {                   \
339     "okay",                                     \
340     "undefined error",                          \
341     "unrecognised domain id",                   \
342     "invalid grant reference",                  \
343     "invalid mapping handle",                   \
344     "invalid virtual address",                  \
345     "invalid device address",                   \
346     "no spare translation slot in the I/O MMU", \
347     "permission denied",                        \
348     "bad page",                                 \
349     "copy arguments cross page boundary"        \
350 }
351
352 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
353
354 /*
355  * Local variables:
356  * mode: C
357  * c-set-style: "BSD"
358  * c-basic-offset: 4
359  * tab-width: 4
360  * indent-tabs-mode: nil
361  * End:
362  */