linux/include/xen/interface/grant_table.h
<<
>>
Prefs
   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 */
  73typedef uint32_t 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 */
  86struct grant_entry_v1 {
  87    /* GTF_xxx: various type and flag information.  [XEN,GST] */
  88    uint16_t 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    uint32_t 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 */
 161struct grant_entry_header {
 162    uint16_t 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 */
 170union 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        uint32_t pad0;
 185        uint64_t 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        uint16_t page_off;
 196        uint16_t length;
 197        uint64_t 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        uint16_t pad0;
 210        grant_ref_t gref;
 211    } transitive;
 212
 213    uint32_t __spacer[4]; /* Pad to a power of two */
 214};
 215
 216typedef uint16_t 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 */
 225typedef uint32_t 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
 245struct gnttab_map_grant_ref {
 246    /* IN parameters. */
 247    uint64_t host_addr;
 248    uint32_t flags;               /* GNTMAP_* */
 249    grant_ref_t ref;
 250    domid_t  dom;
 251    /* OUT parameters. */
 252    int16_t  status;              /* GNTST_* */
 253    grant_handle_t handle;
 254    uint64_t dev_bus_addr;
 255};
 256DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref);
 257
 258/*
 259 * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
 260 * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
 261 * field is ignored. If non-zero, they must refer to a device/host mapping
 262 * that is tracked by <handle>
 263 * NOTES:
 264 *  1. The call may fail in an undefined manner if either mapping is not
 265 *     tracked by <handle>.
 266 *  3. After executing a batch of unmaps, it is guaranteed that no stale
 267 *     mappings will remain in the device or host TLBs.
 268 */
 269#define GNTTABOP_unmap_grant_ref      1
 270struct gnttab_unmap_grant_ref {
 271    /* IN parameters. */
 272    uint64_t host_addr;
 273    uint64_t dev_bus_addr;
 274    grant_handle_t handle;
 275    /* OUT parameters. */
 276    int16_t  status;              /* GNTST_* */
 277};
 278DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref);
 279
 280/*
 281 * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
 282 * <nr_frames> pages. The frame addresses are written to the <frame_list>.
 283 * Only <nr_frames> addresses are written, even if the table is larger.
 284 * NOTES:
 285 *  1. <dom> may be specified as DOMID_SELF.
 286 *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
 287 *  3. Xen may not support more than a single grant-table page per domain.
 288 */
 289#define GNTTABOP_setup_table          2
 290struct gnttab_setup_table {
 291    /* IN parameters. */
 292    domid_t  dom;
 293    uint32_t nr_frames;
 294    /* OUT parameters. */
 295    int16_t  status;              /* GNTST_* */
 296    GUEST_HANDLE(xen_pfn_t) frame_list;
 297};
 298DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table);
 299
 300/*
 301 * GNTTABOP_dump_table: Dump the contents of the grant table to the
 302 * xen console. Debugging use only.
 303 */
 304#define GNTTABOP_dump_table           3
 305struct gnttab_dump_table {
 306    /* IN parameters. */
 307    domid_t dom;
 308    /* OUT parameters. */
 309    int16_t status;               /* GNTST_* */
 310};
 311DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table);
 312
 313/*
 314 * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
 315 * foreign domain has previously registered its interest in the transfer via
 316 * <domid, ref>.
 317 *
 318 * Note that, even if the transfer fails, the specified page no longer belongs
 319 * to the calling domain *unless* the error is GNTST_bad_page.
 320 */
 321#define GNTTABOP_transfer                4
 322struct gnttab_transfer {
 323    /* IN parameters. */
 324    xen_pfn_t mfn;
 325    domid_t       domid;
 326    grant_ref_t   ref;
 327    /* OUT parameters. */
 328    int16_t       status;
 329};
 330DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer);
 331
 332/*
 333 * GNTTABOP_copy: Hypervisor based copy
 334 * source and destinations can be eithers MFNs or, for foreign domains,
 335 * grant references. the foreign domain has to grant read/write access
 336 * in its grant table.
 337 *
 338 * The flags specify what type source and destinations are (either MFN
 339 * or grant reference).
 340 *
 341 * Note that this can also be used to copy data between two domains
 342 * via a third party if the source and destination domains had previously
 343 * grant appropriate access to their pages to the third party.
 344 *
 345 * source_offset specifies an offset in the source frame, dest_offset
 346 * the offset in the target frame and  len specifies the number of
 347 * bytes to be copied.
 348 */
 349
 350#define _GNTCOPY_source_gref      (0)
 351#define GNTCOPY_source_gref       (1<<_GNTCOPY_source_gref)
 352#define _GNTCOPY_dest_gref        (1)
 353#define GNTCOPY_dest_gref         (1<<_GNTCOPY_dest_gref)
 354
 355#define GNTTABOP_copy                 5
 356struct gnttab_copy {
 357        /* IN parameters. */
 358        struct {
 359                union {
 360                        grant_ref_t ref;
 361                        xen_pfn_t   gmfn;
 362                } u;
 363                domid_t  domid;
 364                uint16_t offset;
 365        } source, dest;
 366        uint16_t      len;
 367        uint16_t      flags;          /* GNTCOPY_* */
 368        /* OUT parameters. */
 369        int16_t       status;
 370};
 371DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy);
 372
 373/*
 374 * GNTTABOP_query_size: Query the current and maximum sizes of the shared
 375 * grant table.
 376 * NOTES:
 377 *  1. <dom> may be specified as DOMID_SELF.
 378 *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
 379 */
 380#define GNTTABOP_query_size           6
 381struct gnttab_query_size {
 382    /* IN parameters. */
 383    domid_t  dom;
 384    /* OUT parameters. */
 385    uint32_t nr_frames;
 386    uint32_t max_nr_frames;
 387    int16_t  status;              /* GNTST_* */
 388};
 389DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size);
 390
 391/*
 392 * GNTTABOP_unmap_and_replace: Destroy one or more grant-reference mappings
 393 * tracked by <handle> but atomically replace the page table entry with one
 394 * pointing to the machine address under <new_addr>.  <new_addr> will be
 395 * redirected to the null entry.
 396 * NOTES:
 397 *  1. The call may fail in an undefined manner if either mapping is not
 398 *     tracked by <handle>.
 399 *  2. After executing a batch of unmaps, it is guaranteed that no stale
 400 *     mappings will remain in the device or host TLBs.
 401 */
 402#define GNTTABOP_unmap_and_replace    7
 403struct gnttab_unmap_and_replace {
 404    /* IN parameters. */
 405    uint64_t host_addr;
 406    uint64_t new_addr;
 407    grant_handle_t handle;
 408    /* OUT parameters. */
 409    int16_t  status;              /* GNTST_* */
 410};
 411DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_and_replace);
 412
 413/*
 414 * GNTTABOP_set_version: Request a particular version of the grant
 415 * table shared table structure.  This operation can only be performed
 416 * once in any given domain.  It must be performed before any grants
 417 * are activated; otherwise, the domain will be stuck with version 1.
 418 * The only defined versions are 1 and 2.
 419 */
 420#define GNTTABOP_set_version          8
 421struct gnttab_set_version {
 422    /* IN parameters */
 423    uint32_t version;
 424};
 425DEFINE_GUEST_HANDLE_STRUCT(gnttab_set_version);
 426
 427/*
 428 * GNTTABOP_get_status_frames: Get the list of frames used to store grant
 429 * status for <dom>. In grant format version 2, the status is separated
 430 * from the other shared grant fields to allow more efficient synchronization
 431 * using barriers instead of atomic cmpexch operations.
 432 * <nr_frames> specify the size of vector <frame_list>.
 433 * The frame addresses are returned in the <frame_list>.
 434 * Only <nr_frames> addresses are returned, even if the table is larger.
 435 * NOTES:
 436 *  1. <dom> may be specified as DOMID_SELF.
 437 *  2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
 438 */
 439#define GNTTABOP_get_status_frames     9
 440struct gnttab_get_status_frames {
 441    /* IN parameters. */
 442    uint32_t nr_frames;
 443    domid_t  dom;
 444    /* OUT parameters. */
 445    int16_t  status;              /* GNTST_* */
 446    GUEST_HANDLE(uint64_t) frame_list;
 447};
 448DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_status_frames);
 449
 450/*
 451 * GNTTABOP_get_version: Get the grant table version which is in
 452 * effect for domain <dom>.
 453 */
 454#define GNTTABOP_get_version          10
 455struct gnttab_get_version {
 456    /* IN parameters */
 457    domid_t dom;
 458    uint16_t pad;
 459    /* OUT parameters */
 460    uint32_t version;
 461};
 462DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
 463
 464/*
 465 * Issue one or more cache maintenance operations on a portion of a
 466 * page granted to the calling domain by a foreign domain.
 467 */
 468#define GNTTABOP_cache_flush          12
 469struct gnttab_cache_flush {
 470    union {
 471        uint64_t dev_bus_addr;
 472        grant_ref_t ref;
 473    } a;
 474    uint16_t offset;   /* offset from start of grant */
 475    uint16_t length;   /* size within the grant */
 476#define GNTTAB_CACHE_CLEAN          (1<<0)
 477#define GNTTAB_CACHE_INVAL          (1<<1)
 478#define GNTTAB_CACHE_SOURCE_GREF    (1<<31)
 479    uint32_t op;
 480};
 481DEFINE_GUEST_HANDLE_STRUCT(gnttab_cache_flush);
 482
 483/*
 484 * Bitfield values for update_pin_status.flags.
 485 */
 486 /* Map the grant entry for access by I/O devices. */
 487#define _GNTMAP_device_map      (0)
 488#define GNTMAP_device_map       (1<<_GNTMAP_device_map)
 489 /* Map the grant entry for access by host CPUs. */
 490#define _GNTMAP_host_map        (1)
 491#define GNTMAP_host_map         (1<<_GNTMAP_host_map)
 492 /* Accesses to the granted frame will be restricted to read-only access. */
 493#define _GNTMAP_readonly        (2)
 494#define GNTMAP_readonly         (1<<_GNTMAP_readonly)
 495 /*
 496  * GNTMAP_host_map subflag:
 497  *  0 => The host mapping is usable only by the guest OS.
 498  *  1 => The host mapping is usable by guest OS + current application.
 499  */
 500#define _GNTMAP_application_map (3)
 501#define GNTMAP_application_map  (1<<_GNTMAP_application_map)
 502
 503 /*
 504  * GNTMAP_contains_pte subflag:
 505  *  0 => This map request contains a host virtual address.
 506  *  1 => This map request contains the machine addess of the PTE to update.
 507  */
 508#define _GNTMAP_contains_pte    (4)
 509#define GNTMAP_contains_pte     (1<<_GNTMAP_contains_pte)
 510
 511/*
 512 * Bits to be placed in guest kernel available PTE bits (architecture
 513 * dependent; only supported when XENFEAT_gnttab_map_avail_bits is set).
 514 */
 515#define _GNTMAP_guest_avail0    (16)
 516#define GNTMAP_guest_avail_mask ((uint32_t)~0 << _GNTMAP_guest_avail0)
 517
 518/*
 519 * Values for error status returns. All errors are -ve.
 520 */
 521#define GNTST_okay             (0)  /* Normal return.                        */
 522#define GNTST_general_error    (-1) /* General undefined error.              */
 523#define GNTST_bad_domain       (-2) /* Unrecognsed domain id.                */
 524#define GNTST_bad_gntref       (-3) /* Unrecognised or inappropriate gntref. */
 525#define GNTST_bad_handle       (-4) /* Unrecognised or inappropriate handle. */
 526#define GNTST_bad_virt_addr    (-5) /* Inappropriate virtual address to map. */
 527#define GNTST_bad_dev_addr     (-6) /* Inappropriate device address to unmap.*/
 528#define GNTST_no_device_space  (-7) /* Out of space in I/O MMU.              */
 529#define GNTST_permission_denied (-8) /* Not enough privilege for operation.  */
 530#define GNTST_bad_page         (-9) /* Specified page was invalid for op.    */
 531#define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary.   */
 532#define GNTST_address_too_big (-11) /* transfer page address too large.      */
 533#define GNTST_eagain          (-12) /* Operation not done; try again.        */
 534
 535#define GNTTABOP_error_msgs {                   \
 536    "okay",                                     \
 537    "undefined error",                          \
 538    "unrecognised domain id",                   \
 539    "invalid grant reference",                  \
 540    "invalid mapping handle",                   \
 541    "invalid virtual address",                  \
 542    "invalid device address",                   \
 543    "no spare translation slot in the I/O MMU", \
 544    "permission denied",                        \
 545    "bad page",                                 \
 546    "copy arguments cross page boundary",       \
 547    "page address size too large",              \
 548    "operation not done; try again"             \
 549}
 550
 551#endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
 552