/* SPDX-License-Identifier: MIT */1/******************************************************************************2* grant_table.h3*4* Interface for granting foreign access to page frames, and receiving5* page-ownership transfers.6*7* Copyright (c) 2004, K A Fraser8*/910#ifndef __XEN_PUBLIC_GRANT_TABLE_H__11#define __XEN_PUBLIC_GRANT_TABLE_H__1213#include <xen/interface/xen.h>1415/***********************************16* GRANT TABLE REPRESENTATION17*/1819/* Some rough guidelines on accessing and updating grant-table entries20* in a concurrency-safe manner. For more information, Linux contains a21* reference implementation for guest OSes (drivers/xen/grant_table.c, see22* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=drivers/xen/grant-table.c;hb=HEAD23*24* NB. WMB is a no-op on current-generation x86 processors. However, a25* 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 new32* 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 of41* 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 controller45* which can set a timeout on the use of a grant entry and take necessary46* 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 of53* 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 the56* transferred frame is written. It is safe for the guest to spin waiting57* for this to occur (detect by observing GTF_transfer_completed in58* 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*/6970/*71* Reference to a grant entry in a specified domain's grant table.72*/73typedef uint32_t grant_ref_t;7475/*76* A grant table comprises a packed array of grant entries in one or more77* 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*/8182/*83* Version 1 of the grant table entry structure is maintained largely for84* backwards compatibility. New guests are recommended to support using85* version 2 to overcome version 1 limitations, but to default to version 1.86*/87struct grant_entry_v1 {88/* GTF_xxx: various type and flag information. [XEN,GST] */89uint16_t flags;90/* The domain being granted foreign privileges. [GST] */91domid_t domid;92/*93* GTF_permit_access: GFN that @domid is allowed to map and access. [GST]94* GTF_accept_transfer: GFN that @domid is allowed to transfer into. [GST]95* GTF_transfer_completed: MFN whose ownership transferred by @domid96* (non-translated guests only). [XEN]97*/98uint32_t frame;99};100101/* The first few grant table entries will be preserved across grant table102* version changes and may be pre-populated at domain creation by tools.103*/104#define GNTTAB_NR_RESERVED_ENTRIES 8105#define GNTTAB_RESERVED_CONSOLE 0106#define GNTTAB_RESERVED_XENSTORE 1107108/*109* Type of grant entry.110* GTF_invalid: This grant entry grants no privileges.111* GTF_permit_access: Allow @domid to map/access @frame.112* GTF_accept_transfer: Allow @domid to transfer ownership of one page frame113* to this guest. Xen writes the page number to @frame.114* GTF_transitive: Allow @domid to transitively access a subrange of115* @trans_grant in @trans_domid. No mappings are allowed.116*/117#define GTF_invalid (0U<<0)118#define GTF_permit_access (1U<<0)119#define GTF_accept_transfer (2U<<0)120#define GTF_transitive (3U<<0)121#define GTF_type_mask (3U<<0)122123/*124* Subflags for GTF_permit_access and GTF_transitive.125* GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]126* GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]127* GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]128* Further subflags for GTF_permit_access only.129* GTF_PAT, GTF_PWT, GTF_PCD: (x86) cache attribute flags to be used for130* mappings of the grant [GST]131* GTF_sub_page: Grant access to only a subrange of the page. @domid132* will only be allowed to copy from the grant, and not133* map it. [GST]134*/135#define _GTF_readonly (2)136#define GTF_readonly (1U<<_GTF_readonly)137#define _GTF_reading (3)138#define GTF_reading (1U<<_GTF_reading)139#define _GTF_writing (4)140#define GTF_writing (1U<<_GTF_writing)141#define _GTF_PWT (5)142#define GTF_PWT (1U<<_GTF_PWT)143#define _GTF_PCD (6)144#define GTF_PCD (1U<<_GTF_PCD)145#define _GTF_PAT (7)146#define GTF_PAT (1U<<_GTF_PAT)147#define _GTF_sub_page (8)148#define GTF_sub_page (1U<<_GTF_sub_page)149150/*151* Subflags for GTF_accept_transfer:152* GTF_transfer_committed: Xen sets this flag to indicate that it is committed153* to transferring ownership of a page frame. When a guest sees this flag154* it must /not/ modify the grant entry until GTF_transfer_completed is155* set by Xen.156* GTF_transfer_completed: It is safe for the guest to spin-wait on this flag157* after reading GTF_transfer_committed. Xen will always write the frame158* address, followed by ORing this flag, in a timely manner.159*/160#define _GTF_transfer_committed (2)161#define GTF_transfer_committed (1U<<_GTF_transfer_committed)162#define _GTF_transfer_completed (3)163#define GTF_transfer_completed (1U<<_GTF_transfer_completed)164165/*166* Version 2 grant table entries. These fulfil the same role as167* version 1 entries, but can represent more complicated operations.168* Any given domain will have either a version 1 or a version 2 table,169* and every entry in the table will be the same version.170*171* The interface by which domains use grant references does not depend172* on the grant table version in use by the other domain.173*/174175/*176* Version 1 and version 2 grant entries share a common prefix. The177* fields of the prefix are documented as part of struct178* grant_entry_v1.179*/180struct grant_entry_header {181uint16_t flags;182domid_t domid;183};184185/*186* Version 2 of the grant entry structure.187*/188union grant_entry_v2 {189struct grant_entry_header hdr;190191/*192* This member is used for V1-style full page grants, where either:193*194* -- hdr.type is GTF_accept_transfer, or195* -- hdr.type is GTF_permit_access and GTF_sub_page is not set.196*197* In that case, the frame field has the same semantics as the198* field of the same name in the V1 entry structure.199*/200struct {201struct grant_entry_header hdr;202uint32_t pad0;203uint64_t frame;204} full_page;205206/*207* If the grant type is GTF_grant_access and GTF_sub_page is set,208* @domid is allowed to access bytes [@page_off,@page_off+@length)209* in frame @frame.210*/211struct {212struct grant_entry_header hdr;213uint16_t page_off;214uint16_t length;215uint64_t frame;216} sub_page;217218/*219* If the grant is GTF_transitive, @domid is allowed to use the220* grant @gref in domain @trans_domid, as if it was the local221* domain. Obviously, the transitive access must be compatible222* with the original grant.223*224* The current version of Xen does not allow transitive grants225* to be mapped.226*/227struct {228struct grant_entry_header hdr;229domid_t trans_domid;230uint16_t pad0;231grant_ref_t gref;232} transitive;233234uint32_t __spacer[4]; /* Pad to a power of two */235};236237typedef uint16_t grant_status_t;238239/***********************************240* GRANT TABLE QUERIES AND USES241*/242243#define GNTTABOP_map_grant_ref 0244#define GNTTABOP_unmap_grant_ref 1245#define GNTTABOP_setup_table 2246#define GNTTABOP_dump_table 3247#define GNTTABOP_transfer 4248#define GNTTABOP_copy 5249#define GNTTABOP_query_size 6250#define GNTTABOP_unmap_and_replace 7251#define GNTTABOP_set_version 8252#define GNTTABOP_get_status_frames 9253#define GNTTABOP_get_version 10254#define GNTTABOP_swap_grant_ref 11255#define GNTTABOP_cache_flush 12256/* ` } */257258/*259* Handle to track a mapping created via a grant reference.260*/261typedef uint32_t grant_handle_t;262263/*264* GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access265* by devices and/or host CPUs. If successful, <handle> is a tracking number266* that must be presented later to destroy the mapping(s). On error, <status>267* is a negative status code.268* NOTES:269* 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address270* via which I/O devices may access the granted frame.271* 2. If GNTMAP_host_map is specified then a mapping will be added at272* either a host virtual address in the current address space, or at273* a PTE at the specified machine address. The type of mapping to274* perform is selected through the GNTMAP_contains_pte flag, and the275* address is specified in <host_addr>.276* 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a277* host mapping is destroyed by other means then it is *NOT* guaranteed278* to be accounted to the correct grant reference!279*/280struct gnttab_map_grant_ref {281/* IN parameters. */282uint64_t host_addr;283uint32_t flags; /* GNTMAP_* */284grant_ref_t ref;285domid_t dom;286/* OUT parameters. */287int16_t status; /* GNTST_* */288grant_handle_t handle;289uint64_t dev_bus_addr;290};291DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref);292293/*294* GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings295* tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that296* field is ignored. If non-zero, they must refer to a device/host mapping297* that is tracked by <handle>298* NOTES:299* 1. The call may fail in an undefined manner if either mapping is not300* tracked by <handle>.301* 3. After executing a batch of unmaps, it is guaranteed that no stale302* mappings will remain in the device or host TLBs.303*/304struct gnttab_unmap_grant_ref {305/* IN parameters. */306uint64_t host_addr;307uint64_t dev_bus_addr;308grant_handle_t handle;309/* OUT parameters. */310int16_t status; /* GNTST_* */311};312DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref);313314/*315* GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least316* <nr_frames> pages. The frame addresses are written to the <frame_list>.317* Only <nr_frames> addresses are written, even if the table is larger.318* NOTES:319* 1. <dom> may be specified as DOMID_SELF.320* 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.321* 3. Xen may not support more than a single grant-table page per domain.322*/323struct gnttab_setup_table {324/* IN parameters. */325domid_t dom;326uint32_t nr_frames;327/* OUT parameters. */328int16_t status; /* GNTST_* */329GUEST_HANDLE(xen_pfn_t) frame_list;330};331DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table);332333/*334* GNTTABOP_dump_table: Dump the contents of the grant table to the335* xen console. Debugging use only.336*/337struct gnttab_dump_table {338/* IN parameters. */339domid_t dom;340/* OUT parameters. */341int16_t status; /* GNTST_* */342};343DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table);344345/*346* GNTTABOP_transfer: Transfer <frame> to a foreign domain. The foreign domain347* has previously registered its interest in the transfer via <domid, ref>.348*349* Note that, even if the transfer fails, the specified page no longer belongs350* to the calling domain *unless* the error is GNTST_bad_page.351*352* Note further that only PV guests can use this operation.353*/354struct gnttab_transfer {355/* IN parameters. */356xen_pfn_t mfn;357domid_t domid;358grant_ref_t ref;359/* OUT parameters. */360int16_t status;361};362DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer);363364/*365* GNTTABOP_copy: Hypervisor based copy366* source and destinations can be eithers MFNs or, for foreign domains,367* grant references. the foreign domain has to grant read/write access368* in its grant table.369*370* The flags specify what type source and destinations are (either MFN371* or grant reference).372*373* Note that this can also be used to copy data between two domains374* via a third party if the source and destination domains had previously375* grant appropriate access to their pages to the third party.376*377* source_offset specifies an offset in the source frame, dest_offset378* the offset in the target frame and len specifies the number of379* bytes to be copied.380*/381382#define _GNTCOPY_source_gref (0)383#define GNTCOPY_source_gref (1<<_GNTCOPY_source_gref)384#define _GNTCOPY_dest_gref (1)385#define GNTCOPY_dest_gref (1<<_GNTCOPY_dest_gref)386387struct gnttab_copy {388/* IN parameters. */389struct gnttab_copy_ptr {390union {391grant_ref_t ref;392xen_pfn_t gmfn;393} u;394domid_t domid;395uint16_t offset;396} source, dest;397uint16_t len;398uint16_t flags; /* GNTCOPY_* */399/* OUT parameters. */400int16_t status;401};402DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy);403404/*405* GNTTABOP_query_size: Query the current and maximum sizes of the shared406* grant table.407* NOTES:408* 1. <dom> may be specified as DOMID_SELF.409* 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.410*/411struct gnttab_query_size {412/* IN parameters. */413domid_t dom;414/* OUT parameters. */415uint32_t nr_frames;416uint32_t max_nr_frames;417int16_t status; /* GNTST_* */418};419DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size);420421/*422* GNTTABOP_unmap_and_replace: Destroy one or more grant-reference mappings423* tracked by <handle> but atomically replace the page table entry with one424* pointing to the machine address under <new_addr>. <new_addr> will be425* redirected to the null entry.426* NOTES:427* 1. The call may fail in an undefined manner if either mapping is not428* tracked by <handle>.429* 2. After executing a batch of unmaps, it is guaranteed that no stale430* mappings will remain in the device or host TLBs.431*/432struct gnttab_unmap_and_replace {433/* IN parameters. */434uint64_t host_addr;435uint64_t new_addr;436grant_handle_t handle;437/* OUT parameters. */438int16_t status; /* GNTST_* */439};440DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_and_replace);441442/*443* GNTTABOP_set_version: Request a particular version of the grant444* table shared table structure. This operation may be used to toggle445* between different versions, but must be performed while no grants446* are active. The only defined versions are 1 and 2.447*/448struct gnttab_set_version {449/* IN/OUT parameters */450uint32_t version;451};452DEFINE_GUEST_HANDLE_STRUCT(gnttab_set_version);453454/*455* GNTTABOP_get_status_frames: Get the list of frames used to store grant456* status for <dom>. In grant format version 2, the status is separated457* from the other shared grant fields to allow more efficient synchronization458* using barriers instead of atomic cmpexch operations.459* <nr_frames> specify the size of vector <frame_list>.460* The frame addresses are returned in the <frame_list>.461* Only <nr_frames> addresses are returned, even if the table is larger.462* NOTES:463* 1. <dom> may be specified as DOMID_SELF.464* 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.465*/466struct gnttab_get_status_frames {467/* IN parameters. */468uint32_t nr_frames;469domid_t dom;470/* OUT parameters. */471int16_t status; /* GNTST_* */472GUEST_HANDLE(uint64_t) frame_list;473};474DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_status_frames);475476/*477* GNTTABOP_get_version: Get the grant table version which is in478* effect for domain <dom>.479*/480struct gnttab_get_version {481/* IN parameters */482domid_t dom;483uint16_t pad;484/* OUT parameters */485uint32_t version;486};487DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);488489/*490* GNTTABOP_swap_grant_ref: Swap the contents of two grant entries.491*/492struct gnttab_swap_grant_ref {493/* IN parameters */494grant_ref_t ref_a;495grant_ref_t ref_b;496/* OUT parameters */497int16_t status; /* GNTST_* */498};499DEFINE_GUEST_HANDLE_STRUCT(gnttab_swap_grant_ref);500501/*502* Issue one or more cache maintenance operations on a portion of a503* page granted to the calling domain by a foreign domain.504*/505struct gnttab_cache_flush {506union {507uint64_t dev_bus_addr;508grant_ref_t ref;509} a;510uint16_t offset; /* offset from start of grant */511uint16_t length; /* size within the grant */512#define GNTTAB_CACHE_CLEAN (1u<<0)513#define GNTTAB_CACHE_INVAL (1u<<1)514#define GNTTAB_CACHE_SOURCE_GREF (1u<<31)515uint32_t op;516};517DEFINE_GUEST_HANDLE_STRUCT(gnttab_cache_flush);518519/*520* Bitfield values for gnttab_map_grant_ref.flags.521*/522/* Map the grant entry for access by I/O devices. */523#define _GNTMAP_device_map (0)524#define GNTMAP_device_map (1<<_GNTMAP_device_map)525/* Map the grant entry for access by host CPUs. */526#define _GNTMAP_host_map (1)527#define GNTMAP_host_map (1<<_GNTMAP_host_map)528/* Accesses to the granted frame will be restricted to read-only access. */529#define _GNTMAP_readonly (2)530#define GNTMAP_readonly (1<<_GNTMAP_readonly)531/*532* GNTMAP_host_map subflag:533* 0 => The host mapping is usable only by the guest OS.534* 1 => The host mapping is usable by guest OS + current application.535*/536#define _GNTMAP_application_map (3)537#define GNTMAP_application_map (1<<_GNTMAP_application_map)538539/*540* GNTMAP_contains_pte subflag:541* 0 => This map request contains a host virtual address.542* 1 => This map request contains the machine addess of the PTE to update.543*/544#define _GNTMAP_contains_pte (4)545#define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte)546547/*548* Bits to be placed in guest kernel available PTE bits (architecture549* dependent; only supported when XENFEAT_gnttab_map_avail_bits is set).550*/551#define _GNTMAP_guest_avail0 (16)552#define GNTMAP_guest_avail_mask ((uint32_t)~0 << _GNTMAP_guest_avail0)553554/*555* Values for error status returns. All errors are -ve.556*/557#define GNTST_okay (0) /* Normal return. */558#define GNTST_general_error (-1) /* General undefined error. */559#define GNTST_bad_domain (-2) /* Unrecognsed domain id. */560#define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */561#define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */562#define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */563#define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/564#define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */565#define GNTST_permission_denied (-8) /* Not enough privilege for operation. */566#define GNTST_bad_page (-9) /* Specified page was invalid for op. */567#define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary. */568#define GNTST_address_too_big (-11) /* transfer page address too large. */569#define GNTST_eagain (-12) /* Operation not done; try again. */570#define GNTST_no_space (-13) /* Out of space (handles etc). */571572#define GNTTABOP_error_msgs { \573"okay", \574"undefined error", \575"unrecognised domain id", \576"invalid grant reference", \577"invalid mapping handle", \578"invalid virtual address", \579"invalid device address", \580"no spare translation slot in the I/O MMU", \581"permission denied", \582"bad page", \583"copy arguments cross page boundary", \584"page address size too large", \585"operation not done; try again", \586"out of space", \587}588589#endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */590591592