1 /* SPDX-License-Identifier: MIT
2  *
3  * grant_table.h
4  *
5  * Interface for granting foreign access to page frames, and receiving
6  * page-ownership transfers.
7  *
8  * Copyright (c) 2004, K A Fraser
9  */
10 
11 #ifndef __XEN_PUBLIC_GRANT_TABLE_H__
12 #define __XEN_PUBLIC_GRANT_TABLE_H__
13 
14 #include <xen/interface/xen.h>
15 
16 /***********************************
17  * GRANT TABLE REPRESENTATION
18  */
19 
20 /* Some rough guidelines on accessing and updating grant-table entries
21  * in a concurrency-safe manner. For more information, Linux contains a
22  * reference implementation for guest OSes (arch/xen/kernel/grant_table.c).
23  *
24  * NB. WMB is a no-op on current-generation x86 processors. However, a
25  *     compiler barrier will still be required.
26  *
27  * Introducing a valid entry into the grant table:
28  *  1. Write ent->domid.
29  *  2. Write ent->frame:
30  *      GTF_permit_access:   Frame to which access is permitted.
31  *      GTF_accept_transfer: Pseudo-phys frame slot being filled by new
32  *                           frame, or zero if none.
33  *  3. Write memory barrier (WMB).
34  *  4. Write ent->flags, inc. valid type.
35  *
36  * Invalidating an unused GTF_permit_access entry:
37  *  1. flags = ent->flags.
38  *  2. Observe that !(flags & (GTF_reading|GTF_writing)).
39  *  3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
40  *  NB. No need for WMB as reuse of entry is control-dependent on success of
41  *      step 3, and all architectures guarantee ordering of ctrl-dep writes.
42  *
43  * Invalidating an in-use GTF_permit_access entry:
44  *  This cannot be done directly. Request assistance from the domain controller
45  *  which can set a timeout on the use of a grant entry and take necessary
46  *  action. (NB. This is not yet implemented!).
47  *
48  * Invalidating an unused GTF_accept_transfer entry:
49  *  1. flags = ent->flags.
50  *  2. Observe that !(flags & GTF_transfer_committed). [*]
51  *  3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
52  *  NB. No need for WMB as reuse of entry is control-dependent on success of
53  *      step 3, and all architectures guarantee ordering of ctrl-dep writes.
54  *  [*] If GTF_transfer_committed is set then the grant entry is 'committed'.
55  *      The guest must /not/ modify the grant entry until the address of the
56  *      transferred frame is written. It is safe for the guest to spin waiting
57  *      for this to occur (detect by observing GTF_transfer_completed in
58  *      ent->flags).
59  *
60  * Invalidating a committed GTF_accept_transfer entry:
61  *  1. Wait for (ent->flags & GTF_transfer_completed).
62  *
63  * Changing a GTF_permit_access from writable to read-only:
64  *  Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.
65  *
66  * Changing a GTF_permit_access from read-only to writable:
67  *  Use SMP-safe bit-setting instruction.
68  */
69 
70 /*
71  * Reference to a grant entry in a specified domain's grant table.
72  */
73 typedef u32 grant_ref_t;
74 
75 /*
76  * A grant table comprises a packed array of grant entries in one or more
77  * page frames shared between Xen and a guest.
78  * [XEN]: This field is written by Xen and read by the sharing guest.
79  * [GST]: This field is written by the guest and read by Xen.
80  */
81 
82 /*
83  * Version 1 of the grant table entry structure is maintained purely
84  * for backwards compatibility.  New guests should use version 2.
85  */
86 struct grant_entry_v1 {
87 	/* GTF_xxx: various type and flag information.  [XEN,GST] */
88 	u16 flags;
89 	/* The domain being granted foreign privileges. [GST] */
90 	domid_t  domid;
91 	/*
92 	 * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
93 	 * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
94 	 */
95 	u32 frame;
96 };
97 
98 /*
99  * Type of grant entry.
100  *  GTF_invalid: This grant entry grants no privileges.
101  *  GTF_permit_access: Allow @domid to map/access @frame.
102  *  GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
103  *                       to this guest. Xen writes the page number to @frame.
104  *  GTF_transitive: Allow @domid to transitively access a subrange of
105  *                  @trans_grant in @trans_domid.  No mappings are allowed.
106  */
107 #define GTF_invalid         (0U << 0)
108 #define GTF_permit_access   (1U << 0)
109 #define GTF_accept_transfer (2U << 0)
110 #define GTF_transitive      (3U << 0)
111 #define GTF_type_mask       (3U << 0)
112 
113 /*
114  * Subflags for GTF_permit_access.
115  *  GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
116  *  GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
117  *  GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
118  *  GTF_sub_page: Grant access to only a subrange of the page.  @domid
119  *                will only be allowed to copy from the grant, and not
120  *                map it. [GST]
121  */
122 #define _GTF_readonly       (2)
123 #define GTF_readonly        (1U << _GTF_readonly)
124 #define _GTF_reading        (3)
125 #define GTF_reading         (1U << _GTF_reading)
126 #define _GTF_writing        (4)
127 #define GTF_writing         (1U << _GTF_writing)
128 #define _GTF_sub_page       (8)
129 #define GTF_sub_page        (1U << _GTF_sub_page)
130 
131 /*
132  * Subflags for GTF_accept_transfer:
133  *  GTF_transfer_committed: Xen sets this flag to indicate that it is committed
134  *      to transferring ownership of a page frame. When a guest sees this flag
135  *      it must /not/ modify the grant entry until GTF_transfer_completed is
136  *      set by Xen.
137  *  GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
138  *      after reading GTF_transfer_committed. Xen will always write the frame
139  *      address, followed by ORing this flag, in a timely manner.
140  */
141 #define _GTF_transfer_committed (2)
142 #define GTF_transfer_committed  (1U << _GTF_transfer_committed)
143 #define _GTF_transfer_completed (3)
144 #define GTF_transfer_completed  (1U << _GTF_transfer_completed)
145 
146 /*
147  * Version 2 grant table entries.  These fulfil the same role as
148  * version 1 entries, but can represent more complicated operations.
149  * Any given domain will have either a version 1 or a version 2 table,
150  * and every entry in the table will be the same version.
151  *
152  * The interface by which domains use grant references does not depend
153  * on the grant table version in use by the other domain.
154  */
155 
156 /*
157  * Version 1 and version 2 grant entries share a common prefix.  The
158  * fields of the prefix are documented as part of struct
159  * grant_entry_v1.
160  */
161 struct grant_entry_header {
162 	u16 flags;
163 	domid_t  domid;
164 };
165 
166 /*
167  * Version 2 of the grant entry structure, here is a union because three
168  * different types are suppotted: full_page, sub_page and transitive.
169  */
170 union grant_entry_v2 {
171 	struct grant_entry_header hdr;
172 
173 	/*
174 	 * This member is used for V1-style full page grants, where either:
175 	 *
176 	 * -- hdr.type is GTF_accept_transfer, or
177 	 * -- hdr.type is GTF_permit_access and GTF_sub_page is not set.
178 	 *
179 	 * In that case, the frame field has the same semantics as the
180 	 * field of the same name in the V1 entry structure.
181 	 */
182 	struct {
183 	struct grant_entry_header hdr;
184 	u32 pad0;
185 	u64 frame;
186 	} full_page;
187 
188 	/*
189 	 * If the grant type is GTF_grant_access and GTF_sub_page is set,
190 	 * @domid is allowed to access bytes [@page_off,@page_off+@length)
191 	 * in frame @frame.
192 	 */
193 	struct {
194 	struct grant_entry_header hdr;
195 	u16 page_off;
196 	u16 length;
197 	u64 frame;
198 	} sub_page;
199 
200 	/*
201 	 * If the grant is GTF_transitive, @domid is allowed to use the
202 	 * grant @gref in domain @trans_domid, as if it was the local
203 	 * domain.  Obviously, the transitive access must be compatible
204 	 * with the original grant.
205 	 */
206 	struct {
207 	struct grant_entry_header hdr;
208 	domid_t trans_domid;
209 	u16 pad0;
210 	grant_ref_t gref;
211 	} transitive;
212 
213 	u32 __spacer[4]; /* Pad to a power of two */
214 };
215 
216 typedef u16 grant_status_t;
217 
218 /***********************************
219  * GRANT TABLE QUERIES AND USES
220  */
221 
222 /*
223  * Handle to track a mapping created via a grant reference.
224  */
225 typedef u32 grant_handle_t;
226 
227 /*
228  * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
229  * by devices and/or host CPUs. If successful, <handle> is a tracking number
230  * that must be presented later to destroy the mapping(s). On error, <handle>
231  * is a negative status code.
232  * NOTES:
233  *  1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
234  *     via which I/O devices may access the granted frame.
235  *  2. If GNTMAP_host_map is specified then a mapping will be added at
236  *     either a host virtual address in the current address space, or at
237  *     a PTE at the specified machine address.  The type of mapping to
238  *     perform is selected through the GNTMAP_contains_pte flag, and the
239  *     address is specified in <host_addr>.
240  *  3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
241  *     host mapping is destroyed by other means then it is *NOT* guaranteed
242  *     to be accounted to the correct grant reference!
243  */
244 #define GNTTABOP_map_grant_ref        0
245 struct gnttab_map_grant_ref {
246 	/* IN parameters. */
247 	u64 host_addr;
248 	u32 flags;               /* GNTMAP_* */
249 	grant_ref_t ref;
250 	domid_t  dom;
251 	/* OUT parameters. */
252 	s16  status;              /* GNTST_* */
253 	grant_handle_t handle;
254 	u64 dev_bus_addr;
255 };
256 
257 DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref);
258 
259 /*
260  * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
261  * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
262  * field is ignored. If non-zero, they must refer to a device/host mapping
263  * that is tracked by <handle>
264  * NOTES:
265  *  1. The call may fail in an undefined manner if either mapping is not
266  *     tracked by <handle>.
267  *  3. After executing a batch of unmaps, it is guaranteed that no stale
268  *     mappings will remain in the device or host TLBs.
269  */
270 #define GNTTABOP_unmap_grant_ref      1
271 struct gnttab_unmap_grant_ref {
272 	/* IN parameters. */
273 	u64 host_addr;
274 	u64 dev_bus_addr;
275 	grant_handle_t handle;
276 	/* OUT parameters. */
277 	s16  status;              /* GNTST_* */
278 };
279 
280 DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref);
281 
282 /*
283  * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
284  * <nr_frames> pages. The frame addresses are written to the <frame_list>.
285  * Only <nr_frames> addresses are written, even if the table is larger.
286  * NOTES:
287  *  1. <dom> may be specified as DOMID_SELF.
288  *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
289  *  3. Xen may not support more than a single grant-table page per domain.
290  */
291 #define GNTTABOP_setup_table          2
292 struct gnttab_setup_table {
293 	/* IN parameters. */
294 	domid_t  dom;
295 	u32 nr_frames;
296 	/* OUT parameters. */
297 	s16  status;              /* GNTST_* */
298 
299 	GUEST_HANDLE(xen_pfn_t)frame_list;
300 };
301 
302 DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table);
303 
304 /*
305  * GNTTABOP_dump_table: Dump the contents of the grant table to the
306  * xen console. Debugging use only.
307  */
308 #define GNTTABOP_dump_table           3
309 struct gnttab_dump_table {
310 	/* IN parameters. */
311 	domid_t dom;
312 	/* OUT parameters. */
313 	s16 status;               /* GNTST_* */
314 };
315 
316 DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table);
317 
318 /*
319  * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
320  * foreign domain has previously registered its interest in the transfer via
321  * <domid, ref>.
322  *
323  * Note that, even if the transfer fails, the specified page no longer belongs
324  * to the calling domain *unless* the error is GNTST_bad_page.
325  */
326 #define GNTTABOP_transfer                4
327 struct gnttab_transfer {
328 	/* IN parameters. */
329 	xen_pfn_t mfn;
330 	domid_t       domid;
331 	grant_ref_t   ref;
332 	/* OUT parameters. */
333 	s16       status;
334 };
335 
336 DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer);
337 
338 /*
339  * GNTTABOP_copy: Hypervisor based copy
340  * source and destinations can be eithers MFNs or, for foreign domains,
341  * grant references. the foreign domain has to grant read/write access
342  * in its grant table.
343  *
344  * The flags specify what type source and destinations are (either MFN
345  * or grant reference).
346  *
347  * Note that this can also be used to copy data between two domains
348  * via a third party if the source and destination domains had previously
349  * grant appropriate access to their pages to the third party.
350  *
351  * source_offset specifies an offset in the source frame, dest_offset
352  * the offset in the target frame and  len specifies the number of
353  * bytes to be copied.
354  */
355 
356 #define _GNTCOPY_source_gref      (0)
357 #define GNTCOPY_source_gref       (1 << _GNTCOPY_source_gref)
358 #define _GNTCOPY_dest_gref        (1)
359 #define GNTCOPY_dest_gref         (1 << _GNTCOPY_dest_gref)
360 
361 #define GNTTABOP_copy                 5
362 struct gnttab_copy {
363 	/* IN parameters. */
364 	struct {
365 		union {
366 			grant_ref_t ref;
367 			xen_pfn_t   gmfn;
368 		} u;
369 		domid_t  domid;
370 		u16 offset;
371 	} source, dest;
372 	u16      len;
373 	u16      flags;          /* GNTCOPY_* */
374 	/* OUT parameters. */
375 	s16       status;
376 };
377 
378 DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy);
379 
380 /*
381  * GNTTABOP_query_size: Query the current and maximum sizes of the shared
382  * grant table.
383  * NOTES:
384  *  1. <dom> may be specified as DOMID_SELF.
385  *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
386  */
387 #define GNTTABOP_query_size           6
388 struct gnttab_query_size {
389 	/* IN parameters. */
390 	domid_t  dom;
391 	/* OUT parameters. */
392 	u32 nr_frames;
393 	u32 max_nr_frames;
394 	s16  status;              /* GNTST_* */
395 };
396 
397 DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size);
398 
399 /*
400  * GNTTABOP_unmap_and_replace: Destroy one or more grant-reference mappings
401  * tracked by <handle> but atomically replace the page table entry with one
402  * pointing to the machine address under <new_addr>.  <new_addr> will be
403  * redirected to the null entry.
404  * NOTES:
405  *  1. The call may fail in an undefined manner if either mapping is not
406  *     tracked by <handle>.
407  *  2. After executing a batch of unmaps, it is guaranteed that no stale
408  *     mappings will remain in the device or host TLBs.
409  */
410 #define GNTTABOP_unmap_and_replace    7
411 struct gnttab_unmap_and_replace {
412 	/* IN parameters. */
413 	u64 host_addr;
414 	u64 new_addr;
415 	grant_handle_t handle;
416 	/* OUT parameters. */
417 	s16  status;              /* GNTST_* */
418 };
419 
420 DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_and_replace);
421 
422 /*
423  * GNTTABOP_set_version: Request a particular version of the grant
424  * table shared table structure.  This operation can only be performed
425  * once in any given domain.  It must be performed before any grants
426  * are activated; otherwise, the domain will be stuck with version 1.
427  * The only defined versions are 1 and 2.
428  */
429 #define GNTTABOP_set_version          8
430 struct gnttab_set_version {
431 	/* IN parameters */
432 	u32 version;
433 };
434 
435 DEFINE_GUEST_HANDLE_STRUCT(gnttab_set_version);
436 
437 /*
438  * GNTTABOP_get_status_frames: Get the list of frames used to store grant
439  * status for <dom>. In grant format version 2, the status is separated
440  * from the other shared grant fields to allow more efficient synchronization
441  * using barriers instead of atomic cmpexch operations.
442  * <nr_frames> specify the size of vector <frame_list>.
443  * The frame addresses are returned in the <frame_list>.
444  * Only <nr_frames> addresses are returned, even if the table is larger.
445  * NOTES:
446  *  1. <dom> may be specified as DOMID_SELF.
447  *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
448  */
449 #define GNTTABOP_get_status_frames     9
450 struct gnttab_get_status_frames {
451 	/* IN parameters. */
452 	u32 nr_frames;
453 	domid_t  dom;
454 	/* OUT parameters. */
455 	s16  status;              /* GNTST_* */
456 
457 	GUEST_HANDLE(u64)frame_list;
458 };
459 
460 DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_status_frames);
461 
462 /*
463  * GNTTABOP_get_version: Get the grant table version which is in
464  * effect for domain <dom>.
465  */
466 #define GNTTABOP_get_version          10
467 struct gnttab_get_version {
468 	/* IN parameters */
469 	domid_t dom;
470 	u16 pad;
471 	/* OUT parameters */
472 	u32 version;
473 };
474 
475 DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
476 
477 /*
478  * Issue one or more cache maintenance operations on a portion of a
479  * page granted to the calling domain by a foreign domain.
480  */
481 #define GNTTABOP_cache_flush          12
482 struct gnttab_cache_flush {
483 	union {
484 		u64 dev_bus_addr;
485 		grant_ref_t ref;
486 	} a;
487 	u16 offset;   /* offset from start of grant */
488 	u16 length;   /* size within the grant */
489 #define GNTTAB_CACHE_CLEAN          (1 << 0)
490 #define GNTTAB_CACHE_INVAL          (1 << 1)
491 #define GNTTAB_CACHE_SOURCE_GREF    (1 << 31)
492 	u32 op;
493 };
494 
495 DEFINE_GUEST_HANDLE_STRUCT(gnttab_cache_flush);
496 
497 /*
498  * Bitfield values for update_pin_status.flags.
499  */
500  /* Map the grant entry for access by I/O devices. */
501 #define _GNTMAP_device_map      (0)
502 #define GNTMAP_device_map       (1 << _GNTMAP_device_map)
503 /* Map the grant entry for access by host CPUs. */
504 #define _GNTMAP_host_map        (1)
505 #define GNTMAP_host_map         (1 << _GNTMAP_host_map)
506 /* Accesses to the granted frame will be restricted to read-only access. */
507 #define _GNTMAP_readonly        (2)
508 #define GNTMAP_readonly         (1 << _GNTMAP_readonly)
509 /*
510  * GNTMAP_host_map subflag:
511  *  0 => The host mapping is usable only by the guest OS.
512  *  1 => The host mapping is usable by guest OS + current application.
513  */
514 #define _GNTMAP_application_map (3)
515 #define GNTMAP_application_map  (1 << _GNTMAP_application_map)
516 
517 /*
518  * GNTMAP_contains_pte subflag:
519  *  0 => This map request contains a host virtual address.
520  *  1 => This map request contains the machine addess of the PTE to update.
521  */
522 #define _GNTMAP_contains_pte    (4)
523 #define GNTMAP_contains_pte     (1 << _GNTMAP_contains_pte)
524 
525 /*
526  * Bits to be placed in guest kernel available PTE bits (architecture
527  * dependent; only supported when XENFEAT_gnttab_map_avail_bits is set).
528  */
529 #define _GNTMAP_guest_avail0    (16)
530 #define GNTMAP_guest_avail_mask ((u32)~0 << _GNTMAP_guest_avail0)
531 
532 /*
533  * Values for error status returns. All errors are -ve.
534  */
535 #define GNTST_okay             (0)  /* Normal return.                        */
536 #define GNTST_general_error    (-1) /* General undefined error.              */
537 #define GNTST_bad_domain       (-2) /* Unrecognsed domain id.                */
538 #define GNTST_bad_gntref       (-3) /* Unrecognised or inappropriate gntref. */
539 #define GNTST_bad_handle       (-4) /* Unrecognised or inappropriate handle. */
540 #define GNTST_bad_virt_addr    (-5) /* Inappropriate virtual address to map. */
541 #define GNTST_bad_dev_addr     (-6) /* Inappropriate device address to unmap.*/
542 #define GNTST_no_device_space  (-7) /* Out of space in I/O MMU.              */
543 #define GNTST_permission_denied (-8) /* Not enough privilege for operation.  */
544 #define GNTST_bad_page         (-9) /* Specified page was invalid for op.    */
545 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary.   */
546 #define GNTST_address_too_big (-11) /* transfer page address too large.      */
547 #define GNTST_eagain          (-12) /* Operation not done; try again.        */
548 
549 #define GNTTABOP_error_msgs {                   \
550 	"okay",                                     \
551 	"undefined error",                          \
552 	"unrecognised domain id",                   \
553 	"invalid grant reference",                  \
554 	"invalid mapping handle",                   \
555 	"invalid virtual address",                  \
556 	"invalid device address",                   \
557 	"no spare translation slot in the I/O MMU", \
558 	"permission denied",                        \
559 	"bad page",                                 \
560 	"copy arguments cross page boundary",       \
561 	"page address size too large",              \
562 	"operation not done; try again"             \
563 }
564 
565 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
566