Path: blob/master/include/xen/interface/grant_table.h
10817 views
/******************************************************************************1* grant_table.h2*3* Interface for granting foreign access to page frames, and receiving4* page-ownership transfers.5*6* Permission is hereby granted, free of charge, to any person obtaining a copy7* of this software and associated documentation files (the "Software"), to8* deal in the Software without restriction, including without limitation the9* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or10* sell copies of the Software, and to permit persons to whom the Software is11* furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included in14* all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING21* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER22* DEALINGS IN THE SOFTWARE.23*24* Copyright (c) 2004, K A Fraser25*/2627#ifndef __XEN_PUBLIC_GRANT_TABLE_H__28#define __XEN_PUBLIC_GRANT_TABLE_H__2930#include <xen/interface/xen.h>3132/***********************************33* GRANT TABLE REPRESENTATION34*/3536/* Some rough guidelines on accessing and updating grant-table entries37* in a concurrency-safe manner. For more information, Linux contains a38* reference implementation for guest OSes (arch/xen/kernel/grant_table.c).39*40* NB. WMB is a no-op on current-generation x86 processors. However, a41* compiler barrier will still be required.42*43* Introducing a valid entry into the grant table:44* 1. Write ent->domid.45* 2. Write ent->frame:46* GTF_permit_access: Frame to which access is permitted.47* GTF_accept_transfer: Pseudo-phys frame slot being filled by new48* frame, or zero if none.49* 3. Write memory barrier (WMB).50* 4. Write ent->flags, inc. valid type.51*52* Invalidating an unused GTF_permit_access entry:53* 1. flags = ent->flags.54* 2. Observe that !(flags & (GTF_reading|GTF_writing)).55* 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).56* NB. No need for WMB as reuse of entry is control-dependent on success of57* step 3, and all architectures guarantee ordering of ctrl-dep writes.58*59* Invalidating an in-use GTF_permit_access entry:60* This cannot be done directly. Request assistance from the domain controller61* which can set a timeout on the use of a grant entry and take necessary62* action. (NB. This is not yet implemented!).63*64* Invalidating an unused GTF_accept_transfer entry:65* 1. flags = ent->flags.66* 2. Observe that !(flags & GTF_transfer_committed). [*]67* 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).68* NB. No need for WMB as reuse of entry is control-dependent on success of69* step 3, and all architectures guarantee ordering of ctrl-dep writes.70* [*] If GTF_transfer_committed is set then the grant entry is 'committed'.71* The guest must /not/ modify the grant entry until the address of the72* transferred frame is written. It is safe for the guest to spin waiting73* for this to occur (detect by observing GTF_transfer_completed in74* ent->flags).75*76* Invalidating a committed GTF_accept_transfer entry:77* 1. Wait for (ent->flags & GTF_transfer_completed).78*79* Changing a GTF_permit_access from writable to read-only:80* Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.81*82* Changing a GTF_permit_access from read-only to writable:83* Use SMP-safe bit-setting instruction.84*/8586/*87* A grant table comprises a packed array of grant entries in one or more88* page frames shared between Xen and a guest.89* [XEN]: This field is written by Xen and read by the sharing guest.90* [GST]: This field is written by the guest and read by Xen.91*/92struct grant_entry {93/* GTF_xxx: various type and flag information. [XEN,GST] */94uint16_t flags;95/* The domain being granted foreign privileges. [GST] */96domid_t domid;97/*98* GTF_permit_access: Frame that @domid is allowed to map and access. [GST]99* GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]100*/101uint32_t frame;102};103104/*105* Type of grant entry.106* GTF_invalid: This grant entry grants no privileges.107* GTF_permit_access: Allow @domid to map/access @frame.108* GTF_accept_transfer: Allow @domid to transfer ownership of one page frame109* to this guest. Xen writes the page number to @frame.110*/111#define GTF_invalid (0U<<0)112#define GTF_permit_access (1U<<0)113#define GTF_accept_transfer (2U<<0)114#define GTF_type_mask (3U<<0)115116/*117* Subflags for GTF_permit_access.118* GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]119* GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]120* GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]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)128129/*130* Subflags for GTF_accept_transfer:131* GTF_transfer_committed: Xen sets this flag to indicate that it is committed132* to transferring ownership of a page frame. When a guest sees this flag133* it must /not/ modify the grant entry until GTF_transfer_completed is134* set by Xen.135* GTF_transfer_completed: It is safe for the guest to spin-wait on this flag136* after reading GTF_transfer_committed. Xen will always write the frame137* address, followed by ORing this flag, in a timely manner.138*/139#define _GTF_transfer_committed (2)140#define GTF_transfer_committed (1U<<_GTF_transfer_committed)141#define _GTF_transfer_completed (3)142#define GTF_transfer_completed (1U<<_GTF_transfer_completed)143144145/***********************************146* GRANT TABLE QUERIES AND USES147*/148149/*150* Reference to a grant entry in a specified domain's grant table.151*/152typedef uint32_t grant_ref_t;153154/*155* Handle to track a mapping created via a grant reference.156*/157typedef uint32_t grant_handle_t;158159/*160* GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access161* by devices and/or host CPUs. If successful, <handle> is a tracking number162* that must be presented later to destroy the mapping(s). On error, <handle>163* is a negative status code.164* NOTES:165* 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address166* via which I/O devices may access the granted frame.167* 2. If GNTMAP_host_map is specified then a mapping will be added at168* either a host virtual address in the current address space, or at169* a PTE at the specified machine address. The type of mapping to170* perform is selected through the GNTMAP_contains_pte flag, and the171* address is specified in <host_addr>.172* 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a173* host mapping is destroyed by other means then it is *NOT* guaranteed174* to be accounted to the correct grant reference!175*/176#define GNTTABOP_map_grant_ref 0177struct gnttab_map_grant_ref {178/* IN parameters. */179uint64_t host_addr;180uint32_t flags; /* GNTMAP_* */181grant_ref_t ref;182domid_t dom;183/* OUT parameters. */184int16_t status; /* GNTST_* */185grant_handle_t handle;186uint64_t dev_bus_addr;187};188DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref);189190/*191* GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings192* tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that193* field is ignored. If non-zero, they must refer to a device/host mapping194* that is tracked by <handle>195* NOTES:196* 1. The call may fail in an undefined manner if either mapping is not197* tracked by <handle>.198* 3. After executing a batch of unmaps, it is guaranteed that no stale199* mappings will remain in the device or host TLBs.200*/201#define GNTTABOP_unmap_grant_ref 1202struct gnttab_unmap_grant_ref {203/* IN parameters. */204uint64_t host_addr;205uint64_t dev_bus_addr;206grant_handle_t handle;207/* OUT parameters. */208int16_t status; /* GNTST_* */209};210DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref);211212/*213* GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least214* <nr_frames> pages. The frame addresses are written to the <frame_list>.215* Only <nr_frames> addresses are written, even if the table is larger.216* NOTES:217* 1. <dom> may be specified as DOMID_SELF.218* 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.219* 3. Xen may not support more than a single grant-table page per domain.220*/221#define GNTTABOP_setup_table 2222struct gnttab_setup_table {223/* IN parameters. */224domid_t dom;225uint32_t nr_frames;226/* OUT parameters. */227int16_t status; /* GNTST_* */228GUEST_HANDLE(ulong) frame_list;229};230DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table);231232/*233* GNTTABOP_dump_table: Dump the contents of the grant table to the234* xen console. Debugging use only.235*/236#define GNTTABOP_dump_table 3237struct gnttab_dump_table {238/* IN parameters. */239domid_t dom;240/* OUT parameters. */241int16_t status; /* GNTST_* */242};243DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table);244245/*246* GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The247* foreign domain has previously registered its interest in the transfer via248* <domid, ref>.249*250* Note that, even if the transfer fails, the specified page no longer belongs251* to the calling domain *unless* the error is GNTST_bad_page.252*/253#define GNTTABOP_transfer 4254struct gnttab_transfer {255/* IN parameters. */256unsigned long mfn;257domid_t domid;258grant_ref_t ref;259/* OUT parameters. */260int16_t status;261};262DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer);263264/*265* GNTTABOP_copy: Hypervisor based copy266* source and destinations can be eithers MFNs or, for foreign domains,267* grant references. the foreign domain has to grant read/write access268* in its grant table.269*270* The flags specify what type source and destinations are (either MFN271* or grant reference).272*273* Note that this can also be used to copy data between two domains274* via a third party if the source and destination domains had previously275* grant appropriate access to their pages to the third party.276*277* source_offset specifies an offset in the source frame, dest_offset278* the offset in the target frame and len specifies the number of279* bytes to be copied.280*/281282#define _GNTCOPY_source_gref (0)283#define GNTCOPY_source_gref (1<<_GNTCOPY_source_gref)284#define _GNTCOPY_dest_gref (1)285#define GNTCOPY_dest_gref (1<<_GNTCOPY_dest_gref)286287#define GNTTABOP_copy 5288struct gnttab_copy {289/* IN parameters. */290struct {291union {292grant_ref_t ref;293unsigned long gmfn;294} u;295domid_t domid;296uint16_t offset;297} source, dest;298uint16_t len;299uint16_t flags; /* GNTCOPY_* */300/* OUT parameters. */301int16_t status;302};303DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy);304305/*306* GNTTABOP_query_size: Query the current and maximum sizes of the shared307* grant table.308* NOTES:309* 1. <dom> may be specified as DOMID_SELF.310* 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.311*/312#define GNTTABOP_query_size 6313struct gnttab_query_size {314/* IN parameters. */315domid_t dom;316/* OUT parameters. */317uint32_t nr_frames;318uint32_t max_nr_frames;319int16_t status; /* GNTST_* */320};321DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size);322323/*324* Bitfield values for update_pin_status.flags.325*/326/* Map the grant entry for access by I/O devices. */327#define _GNTMAP_device_map (0)328#define GNTMAP_device_map (1<<_GNTMAP_device_map)329/* Map the grant entry for access by host CPUs. */330#define _GNTMAP_host_map (1)331#define GNTMAP_host_map (1<<_GNTMAP_host_map)332/* Accesses to the granted frame will be restricted to read-only access. */333#define _GNTMAP_readonly (2)334#define GNTMAP_readonly (1<<_GNTMAP_readonly)335/*336* GNTMAP_host_map subflag:337* 0 => The host mapping is usable only by the guest OS.338* 1 => The host mapping is usable by guest OS + current application.339*/340#define _GNTMAP_application_map (3)341#define GNTMAP_application_map (1<<_GNTMAP_application_map)342343/*344* GNTMAP_contains_pte subflag:345* 0 => This map request contains a host virtual address.346* 1 => This map request contains the machine addess of the PTE to update.347*/348#define _GNTMAP_contains_pte (4)349#define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte)350351/*352* Values for error status returns. All errors are -ve.353*/354#define GNTST_okay (0) /* Normal return. */355#define GNTST_general_error (-1) /* General undefined error. */356#define GNTST_bad_domain (-2) /* Unrecognsed domain id. */357#define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */358#define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */359#define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */360#define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/361#define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */362#define GNTST_permission_denied (-8) /* Not enough privilege for operation. */363#define GNTST_bad_page (-9) /* Specified page was invalid for op. */364#define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary */365366#define GNTTABOP_error_msgs { \367"okay", \368"undefined error", \369"unrecognised domain id", \370"invalid grant reference", \371"invalid mapping handle", \372"invalid virtual address", \373"invalid device address", \374"no spare translation slot in the I/O MMU", \375"permission denied", \376"bad page", \377"copy arguments cross page boundary" \378}379380#endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */381382383