blob: 39e571796e324fac9782cf75fe27563bf6be0f78 [file] [log] [blame]
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -07001/******************************************************************************
2 * grant_table.h
3 *
4 * Interface for granting foreign access to page frames, and receiving
5 * page-ownership transfers.
6 *
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -07007 * 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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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.
24 *
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -070025 * Copyright (c) 2004, K A Fraser
26 */
27
28#ifndef __XEN_PUBLIC_GRANT_TABLE_H__
29#define __XEN_PUBLIC_GRANT_TABLE_H__
30
Stefano Stabellini183d03c2010-05-17 17:08:21 +010031#include <xen/interface/xen.h>
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -070032
33/***********************************
34 * GRANT TABLE REPRESENTATION
35 */
36
37/* Some rough guidelines on accessing and updating grant-table entries
38 * in a concurrency-safe manner. For more information, Linux contains a
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -070039 * reference implementation for guest OSes (arch/xen/kernel/grant_table.c).
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -070040 *
41 * NB. WMB is a no-op on current-generation x86 processors. However, a
42 * compiler barrier will still be required.
43 *
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.
52 *
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.
59 *
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!).
64 *
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
75 * ent->flags).
76 *
77 * Invalidating a committed GTF_accept_transfer entry:
78 * 1. Wait for (ent->flags & GTF_transfer_completed).
79 *
80 * Changing a GTF_permit_access from writable to read-only:
81 * Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.
82 *
83 * Changing a GTF_permit_access from read-only to writable:
84 * Use SMP-safe bit-setting instruction.
85 */
86
87/*
88 * A grant table comprises a packed array of grant entries in one or more
89 * page frames shared between Xen and a guest.
90 * [XEN]: This field is written by Xen and read by the sharing guest.
91 * [GST]: This field is written by the guest and read by Xen.
92 */
93struct grant_entry {
94 /* GTF_xxx: various type and flag information. [XEN,GST] */
95 uint16_t flags;
96 /* The domain being granted foreign privileges. [GST] */
97 domid_t domid;
98 /*
99 * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
100 * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
101 */
102 uint32_t frame;
103};
104
105/*
106 * Type of grant entry.
107 * GTF_invalid: This grant entry grants no privileges.
108 * GTF_permit_access: Allow @domid to map/access @frame.
109 * GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
110 * to this guest. Xen writes the page number to @frame.
111 */
112#define GTF_invalid (0U<<0)
113#define GTF_permit_access (1U<<0)
114#define GTF_accept_transfer (2U<<0)
115#define GTF_type_mask (3U<<0)
116
117/*
118 * Subflags for GTF_permit_access.
119 * GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
120 * GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
121 * GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
122 */
123#define _GTF_readonly (2)
124#define GTF_readonly (1U<<_GTF_readonly)
125#define _GTF_reading (3)
126#define GTF_reading (1U<<_GTF_reading)
127#define _GTF_writing (4)
128#define GTF_writing (1U<<_GTF_writing)
129
130/*
131 * Subflags for GTF_accept_transfer:
132 * GTF_transfer_committed: Xen sets this flag to indicate that it is committed
133 * to transferring ownership of a page frame. When a guest sees this flag
134 * it must /not/ modify the grant entry until GTF_transfer_completed is
135 * set by Xen.
136 * GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
137 * after reading GTF_transfer_committed. Xen will always write the frame
138 * address, followed by ORing this flag, in a timely manner.
139 */
140#define _GTF_transfer_committed (2)
141#define GTF_transfer_committed (1U<<_GTF_transfer_committed)
142#define _GTF_transfer_completed (3)
143#define GTF_transfer_completed (1U<<_GTF_transfer_completed)
144
145
146/***********************************
147 * GRANT TABLE QUERIES AND USES
148 */
149
150/*
151 * Reference to a grant entry in a specified domain's grant table.
152 */
153typedef uint32_t grant_ref_t;
154
155/*
156 * Handle to track a mapping created via a grant reference.
157 */
158typedef uint32_t grant_handle_t;
159
160/*
161 * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
162 * by devices and/or host CPUs. If successful, <handle> is a tracking number
163 * that must be presented later to destroy the mapping(s). On error, <handle>
164 * is a negative status code.
165 * NOTES:
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700166 * 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700167 * via which I/O devices may access the granted frame.
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700168 * 2. If GNTMAP_host_map is specified then a mapping will be added at
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700169 * either a host virtual address in the current address space, or at
170 * a PTE at the specified machine address. The type of mapping to
171 * perform is selected through the GNTMAP_contains_pte flag, and the
172 * address is specified in <host_addr>.
173 * 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
174 * host mapping is destroyed by other means then it is *NOT* guaranteed
175 * to be accounted to the correct grant reference!
176 */
177#define GNTTABOP_map_grant_ref 0
178struct gnttab_map_grant_ref {
179 /* IN parameters. */
180 uint64_t host_addr;
181 uint32_t flags; /* GNTMAP_* */
182 grant_ref_t ref;
183 domid_t dom;
184 /* OUT parameters. */
185 int16_t status; /* GNTST_* */
186 grant_handle_t handle;
187 uint64_t dev_bus_addr;
188};
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700189DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref);
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700190
191/*
192 * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
193 * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
194 * field is ignored. If non-zero, they must refer to a device/host mapping
195 * that is tracked by <handle>
196 * NOTES:
197 * 1. The call may fail in an undefined manner if either mapping is not
198 * tracked by <handle>.
199 * 3. After executing a batch of unmaps, it is guaranteed that no stale
200 * mappings will remain in the device or host TLBs.
201 */
202#define GNTTABOP_unmap_grant_ref 1
203struct gnttab_unmap_grant_ref {
204 /* IN parameters. */
205 uint64_t host_addr;
206 uint64_t dev_bus_addr;
207 grant_handle_t handle;
208 /* OUT parameters. */
209 int16_t status; /* GNTST_* */
210};
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700211DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref);
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700212
213/*
214 * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
215 * <nr_frames> pages. The frame addresses are written to the <frame_list>.
216 * Only <nr_frames> addresses are written, even if the table is larger.
217 * NOTES:
218 * 1. <dom> may be specified as DOMID_SELF.
219 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
220 * 3. Xen may not support more than a single grant-table page per domain.
221 */
222#define GNTTABOP_setup_table 2
223struct gnttab_setup_table {
224 /* IN parameters. */
225 domid_t dom;
226 uint32_t nr_frames;
227 /* OUT parameters. */
228 int16_t status; /* GNTST_* */
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700229 GUEST_HANDLE(ulong) frame_list;
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700230};
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700231DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table);
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700232
233/*
234 * GNTTABOP_dump_table: Dump the contents of the grant table to the
235 * xen console. Debugging use only.
236 */
237#define GNTTABOP_dump_table 3
238struct gnttab_dump_table {
239 /* IN parameters. */
240 domid_t dom;
241 /* OUT parameters. */
242 int16_t status; /* GNTST_* */
243};
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700244DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table);
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700245
246/*
247 * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
248 * foreign domain has previously registered its interest in the transfer via
249 * <domid, ref>.
250 *
251 * Note that, even if the transfer fails, the specified page no longer belongs
252 * to the calling domain *unless* the error is GNTST_bad_page.
253 */
254#define GNTTABOP_transfer 4
255struct gnttab_transfer {
256 /* IN parameters. */
257 unsigned long mfn;
258 domid_t domid;
259 grant_ref_t ref;
260 /* OUT parameters. */
261 int16_t status;
262};
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700263DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700264
265/*
266 * GNTTABOP_copy: Hypervisor based copy
267 * source and destinations can be eithers MFNs or, for foreign domains,
268 * grant references. the foreign domain has to grant read/write access
269 * in its grant table.
270 *
271 * The flags specify what type source and destinations are (either MFN
272 * or grant reference).
273 *
274 * Note that this can also be used to copy data between two domains
275 * via a third party if the source and destination domains had previously
276 * grant appropriate access to their pages to the third party.
277 *
278 * source_offset specifies an offset in the source frame, dest_offset
279 * the offset in the target frame and len specifies the number of
280 * bytes to be copied.
281 */
282
283#define _GNTCOPY_source_gref (0)
284#define GNTCOPY_source_gref (1<<_GNTCOPY_source_gref)
285#define _GNTCOPY_dest_gref (1)
286#define GNTCOPY_dest_gref (1<<_GNTCOPY_dest_gref)
287
288#define GNTTABOP_copy 5
289struct gnttab_copy {
290 /* IN parameters. */
291 struct {
292 union {
293 grant_ref_t ref;
294 unsigned long gmfn;
295 } u;
296 domid_t domid;
297 uint16_t offset;
298 } source, dest;
299 uint16_t len;
300 uint16_t flags; /* GNTCOPY_* */
301 /* OUT parameters. */
302 int16_t status;
303};
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700304DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700305
306/*
307 * GNTTABOP_query_size: Query the current and maximum sizes of the shared
308 * grant table.
309 * NOTES:
310 * 1. <dom> may be specified as DOMID_SELF.
311 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
312 */
313#define GNTTABOP_query_size 6
314struct gnttab_query_size {
315 /* IN parameters. */
316 domid_t dom;
317 /* OUT parameters. */
318 uint32_t nr_frames;
319 uint32_t max_nr_frames;
320 int16_t status; /* GNTST_* */
321};
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700322DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size);
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700323
324/*
325 * Bitfield values for update_pin_status.flags.
326 */
327 /* Map the grant entry for access by I/O devices. */
328#define _GNTMAP_device_map (0)
329#define GNTMAP_device_map (1<<_GNTMAP_device_map)
330 /* Map the grant entry for access by host CPUs. */
331#define _GNTMAP_host_map (1)
332#define GNTMAP_host_map (1<<_GNTMAP_host_map)
333 /* Accesses to the granted frame will be restricted to read-only access. */
334#define _GNTMAP_readonly (2)
335#define GNTMAP_readonly (1<<_GNTMAP_readonly)
336 /*
337 * GNTMAP_host_map subflag:
338 * 0 => The host mapping is usable only by the guest OS.
339 * 1 => The host mapping is usable by guest OS + current application.
340 */
341#define _GNTMAP_application_map (3)
342#define GNTMAP_application_map (1<<_GNTMAP_application_map)
343
344 /*
345 * GNTMAP_contains_pte subflag:
346 * 0 => This map request contains a host virtual address.
347 * 1 => This map request contains the machine addess of the PTE to update.
348 */
349#define _GNTMAP_contains_pte (4)
350#define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte)
351
352/*
353 * Values for error status returns. All errors are -ve.
354 */
355#define GNTST_okay (0) /* Normal return. */
356#define GNTST_general_error (-1) /* General undefined error. */
357#define GNTST_bad_domain (-2) /* Unrecognsed domain id. */
358#define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */
359#define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */
360#define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */
361#define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/
362#define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */
363#define GNTST_permission_denied (-8) /* Not enough privilege for operation. */
364#define GNTST_bad_page (-9) /* Specified page was invalid for op. */
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700365#define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary */
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700366
367#define GNTTABOP_error_msgs { \
368 "okay", \
369 "undefined error", \
370 "unrecognised domain id", \
371 "invalid grant reference", \
372 "invalid mapping handle", \
373 "invalid virtual address", \
374 "invalid device address", \
375 "no spare translation slot in the I/O MMU", \
376 "permission denied", \
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700377 "bad page", \
378 "copy arguments cross page boundary" \
Jeremy Fitzhardingea42089d2007-07-17 18:37:04 -0700379}
380
381#endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */