grant_table.h revision 183340
1115733Smarkm/****************************************************************************** 2115733Smarkm * grant_table.h 3115733Smarkm * 4115733Smarkm * Interface for granting foreign access to page frames, and receiving 5115733Smarkm * page-ownership transfers. 6115733Smarkm * 7115733Smarkm * Permission is hereby granted, free of charge, to any person obtaining a copy 8115733Smarkm * of this software and associated documentation files (the "Software"), to 9115733Smarkm * deal in the Software without restriction, including without limitation the 10115733Smarkm * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11115733Smarkm * sell copies of the Software, and to permit persons to whom the Software is 12115733Smarkm * furnished to do so, subject to the following conditions: 13115733Smarkm * 14115733Smarkm * The above copyright notice and this permission notice shall be included in 15115733Smarkm * all copies or substantial portions of the Software. 16115733Smarkm * 17115733Smarkm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18115733Smarkm * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19115733Smarkm * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20115733Smarkm * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21115733Smarkm * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22115733Smarkm * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23115733Smarkm * DEALINGS IN THE SOFTWARE. 24115733Smarkm * 2551462Smarkm * Copyright (c) 2004, K A Fraser 2651462Smarkm */ 2783551Sdillon 2883551Sdillon#ifndef __XEN_PUBLIC_GRANT_TABLE_H__ 2951462Smarkm#define __XEN_PUBLIC_GRANT_TABLE_H__ 30115733Smarkm 31115733Smarkm 32115733Smarkm/*********************************** 33115733Smarkm * GRANT TABLE REPRESENTATION 3451462Smarkm */ 3551462Smarkm 36115733Smarkm/* Some rough guidelines on accessing and updating grant-table entries 37115733Smarkm * in a concurrency-safe manner. For more information, Linux contains a 3851462Smarkm * reference implementation for guest OSes (arch/xen/kernel/grant_table.c). 3951462Smarkm * 4051462Smarkm * NB. WMB is a no-op on current-generation x86 processors. However, a 4151462Smarkm * compiler barrier will still be required. 4251462Smarkm * 4351462Smarkm * Introducing a valid entry into the grant table: 4451462Smarkm * 1. Write ent->domid. 4591754Smarkm * 2. Write ent->frame: 4651462Smarkm * GTF_permit_access: Frame to which access is permitted. 4751462Smarkm * GTF_accept_transfer: Pseudo-phys frame slot being filled by new 4851462Smarkm * frame, or zero if none. 4991795Smarkm * 3. Write memory barrier (WMB). 5091795Smarkm * 4. Write ent->flags, inc. valid type. 5191754Smarkm * 5291754Smarkm * Invalidating an unused GTF_permit_access entry: 5391754Smarkm * 1. flags = ent->flags. 54115733Smarkm * 2. Observe that !(flags & (GTF_reading|GTF_writing)). 5551462Smarkm * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0). 5651462Smarkm * NB. No need for WMB as reuse of entry is control-dependent on success of 5751462Smarkm * step 3, and all architectures guarantee ordering of ctrl-dep writes. 5851462Smarkm * 5951462Smarkm * Invalidating an in-use GTF_permit_access entry: 6091754Smarkm * This cannot be done directly. Request assistance from the domain controller 6151462Smarkm * which can set a timeout on the use of a grant entry and take necessary 6251462Smarkm * action. (NB. This is not yet implemented!). 6351462Smarkm * 6491754Smarkm * Invalidating an unused GTF_accept_transfer entry: 6551462Smarkm * 1. flags = ent->flags. 6651462Smarkm * 2. Observe that !(flags & GTF_transfer_committed). [*] 6751462Smarkm * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0). 6851462Smarkm * NB. No need for WMB as reuse of entry is control-dependent on success of 6951462Smarkm * step 3, and all architectures guarantee ordering of ctrl-dep writes. 7051462Smarkm * [*] If GTF_transfer_committed is set then the grant entry is 'committed'. 7151462Smarkm * The guest must /not/ modify the grant entry until the address of the 7251462Smarkm * transferred frame is written. It is safe for the guest to spin waiting 7391754Smarkm * for this to occur (detect by observing GTF_transfer_completed in 7451462Smarkm * ent->flags). 7551462Smarkm * 7691754Smarkm * Invalidating a committed GTF_accept_transfer entry: 7751462Smarkm * 1. Wait for (ent->flags & GTF_transfer_completed). 7851462Smarkm * 7991754Smarkm * Changing a GTF_permit_access from writable to read-only: 8051462Smarkm * Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing. 8151462Smarkm * 8251462Smarkm * Changing a GTF_permit_access from read-only to writable: 8391754Smarkm * Use SMP-safe bit-setting instruction. 8491754Smarkm */ 8591754Smarkm 8691754Smarkm/* 8791795Smarkm * A grant table comprises a packed array of grant entries in one or more 8891754Smarkm * page frames shared between Xen and a guest. 8991795Smarkm * [XEN]: This field is written by Xen and read by the sharing guest. 9051462Smarkm * [GST]: This field is written by the guest and read by Xen. 9151462Smarkm */ 9291754Smarkmstruct grant_entry { 9351462Smarkm /* GTF_xxx: various type and flag information. [XEN,GST] */ 9451462Smarkm uint16_t flags; 9591754Smarkm /* The domain being granted foreign privileges. [GST] */ 9691754Smarkm domid_t domid; 9791754Smarkm /* 9851462Smarkm * GTF_permit_access: Frame that @domid is allowed to map and access. [GST] 9991754Smarkm * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN] 10051462Smarkm */ 10151462Smarkm uint32_t frame; 10291754Smarkm}; 10391754Smarkmtypedef struct grant_entry grant_entry_t; 10491754Smarkm 10551462Smarkm/* 10691754Smarkm * Type of grant entry. 10751462Smarkm * GTF_invalid: This grant entry grants no privileges. 10851462Smarkm * GTF_permit_access: Allow @domid to map/access @frame. 10951462Smarkm * GTF_accept_transfer: Allow @domid to transfer ownership of one page frame 11051462Smarkm * to this guest. Xen writes the page number to @frame. 11151462Smarkm */ 11251462Smarkm#define GTF_invalid (0U<<0) 11391754Smarkm#define GTF_permit_access (1U<<0) 11451462Smarkm#define GTF_accept_transfer (2U<<0) 11551462Smarkm#define GTF_type_mask (3U<<0) 11691754Smarkm 11751462Smarkm/* 11891754Smarkm * Subflags for GTF_permit_access. 11951462Smarkm * GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST] 12051462Smarkm * GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN] 12191754Smarkm * GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN] 12251462Smarkm * GTF_PAT, GTF_PWT, GTF_PCD: (x86) cache attribute flags for the grant [GST] 12351462Smarkm */ 12491754Smarkm#define _GTF_readonly (2) 12551462Smarkm#define GTF_readonly (1U<<_GTF_readonly) 12651462Smarkm#define _GTF_reading (3) 12791754Smarkm#define GTF_reading (1U<<_GTF_reading) 12851462Smarkm#define _GTF_writing (4) 12991754Smarkm#define GTF_writing (1U<<_GTF_writing) 13091754Smarkm#define _GTF_PWT (5) 13151462Smarkm#define GTF_PWT (1U<<_GTF_PWT) 13251462Smarkm#define _GTF_PCD (6) 13351462Smarkm#define GTF_PCD (1U<<_GTF_PCD) 13451462Smarkm#define _GTF_PAT (7) 13551462Smarkm#define GTF_PAT (1U<<_GTF_PAT) 13691754Smarkm 13751462Smarkm/* 13891754Smarkm * Subflags for GTF_accept_transfer: 13951462Smarkm * GTF_transfer_committed: Xen sets this flag to indicate that it is committed 14091754Smarkm * to transferring ownership of a page frame. When a guest sees this flag 14151462Smarkm * it must /not/ modify the grant entry until GTF_transfer_completed is 14291754Smarkm * set by Xen. 14351462Smarkm * GTF_transfer_completed: It is safe for the guest to spin-wait on this flag 14491754Smarkm * after reading GTF_transfer_committed. Xen will always write the frame 145115733Smarkm * address, followed by ORing this flag, in a timely manner. 14691754Smarkm */ 14751462Smarkm#define _GTF_transfer_committed (2) 14851462Smarkm#define GTF_transfer_committed (1U<<_GTF_transfer_committed) 14951462Smarkm#define _GTF_transfer_completed (3) 15091754Smarkm#define GTF_transfer_completed (1U<<_GTF_transfer_completed) 15151462Smarkm 152115733Smarkm 15351462Smarkm/*********************************** 154 * GRANT TABLE QUERIES AND USES 155 */ 156 157/* 158 * Reference to a grant entry in a specified domain's grant table. 159 */ 160typedef uint32_t grant_ref_t; 161 162/* 163 * Handle to track a mapping created via a grant reference. 164 */ 165typedef uint32_t grant_handle_t; 166 167/* 168 * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access 169 * by devices and/or host CPUs. If successful, <handle> is a tracking number 170 * that must be presented later to destroy the mapping(s). On error, <handle> 171 * is a negative status code. 172 * NOTES: 173 * 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address 174 * via which I/O devices may access the granted frame. 175 * 2. If GNTMAP_host_map is specified then a mapping will be added at 176 * either a host virtual address in the current address space, or at 177 * a PTE at the specified machine address. The type of mapping to 178 * perform is selected through the GNTMAP_contains_pte flag, and the 179 * address is specified in <host_addr>. 180 * 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a 181 * host mapping is destroyed by other means then it is *NOT* guaranteed 182 * to be accounted to the correct grant reference! 183 */ 184#define GNTTABOP_map_grant_ref 0 185struct gnttab_map_grant_ref { 186 /* IN parameters. */ 187 uint64_t host_addr; 188 uint32_t flags; /* GNTMAP_* */ 189 grant_ref_t ref; 190 domid_t dom; 191 /* OUT parameters. */ 192 int16_t status; /* GNTST_* */ 193 grant_handle_t handle; 194 uint64_t dev_bus_addr; 195}; 196typedef struct gnttab_map_grant_ref gnttab_map_grant_ref_t; 197DEFINE_XEN_GUEST_HANDLE(gnttab_map_grant_ref_t); 198 199/* 200 * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings 201 * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that 202 * field is ignored. If non-zero, they must refer to a device/host mapping 203 * that is tracked by <handle> 204 * NOTES: 205 * 1. The call may fail in an undefined manner if either mapping is not 206 * tracked by <handle>. 207 * 3. After executing a batch of unmaps, it is guaranteed that no stale 208 * mappings will remain in the device or host TLBs. 209 */ 210#define GNTTABOP_unmap_grant_ref 1 211struct gnttab_unmap_grant_ref { 212 /* IN parameters. */ 213 uint64_t host_addr; 214 uint64_t dev_bus_addr; 215 grant_handle_t handle; 216 /* OUT parameters. */ 217 int16_t status; /* GNTST_* */ 218}; 219typedef struct gnttab_unmap_grant_ref gnttab_unmap_grant_ref_t; 220DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_grant_ref_t); 221 222/* 223 * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least 224 * <nr_frames> pages. The frame addresses are written to the <frame_list>. 225 * Only <nr_frames> addresses are written, even if the table is larger. 226 * NOTES: 227 * 1. <dom> may be specified as DOMID_SELF. 228 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF. 229 * 3. Xen may not support more than a single grant-table page per domain. 230 */ 231#define GNTTABOP_setup_table 2 232struct gnttab_setup_table { 233 /* IN parameters. */ 234 domid_t dom; 235 uint32_t nr_frames; 236 /* OUT parameters. */ 237 int16_t status; /* GNTST_* */ 238#ifdef __LP64__ 239 XEN_GUEST_HANDLE(uint64_t) frame_list; 240#else 241 XEN_GUEST_HANDLE(uint32_t) frame_list; 242#endif 243}; 244typedef struct gnttab_setup_table gnttab_setup_table_t; 245DEFINE_XEN_GUEST_HANDLE(gnttab_setup_table_t); 246 247/* 248 * GNTTABOP_dump_table: Dump the contents of the grant table to the 249 * xen console. Debugging use only. 250 */ 251#define GNTTABOP_dump_table 3 252struct gnttab_dump_table { 253 /* IN parameters. */ 254 domid_t dom; 255 /* OUT parameters. */ 256 int16_t status; /* GNTST_* */ 257}; 258typedef struct gnttab_dump_table gnttab_dump_table_t; 259DEFINE_XEN_GUEST_HANDLE(gnttab_dump_table_t); 260 261/* 262 * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The 263 * foreign domain has previously registered its interest in the transfer via 264 * <domid, ref>. 265 * 266 * Note that, even if the transfer fails, the specified page no longer belongs 267 * to the calling domain *unless* the error is GNTST_bad_page. 268 */ 269#define GNTTABOP_transfer 4 270struct gnttab_transfer { 271 /* IN parameters. */ 272 xen_pfn_t mfn; 273 domid_t domid; 274 grant_ref_t ref; 275 /* OUT parameters. */ 276 int16_t status; 277}; 278typedef struct gnttab_transfer gnttab_transfer_t; 279DEFINE_XEN_GUEST_HANDLE(gnttab_transfer_t); 280 281 282/* 283 * GNTTABOP_copy: Hypervisor based copy 284 * source and destinations can be eithers MFNs or, for foreign domains, 285 * grant references. the foreign domain has to grant read/write access 286 * in its grant table. 287 * 288 * The flags specify what type source and destinations are (either MFN 289 * or grant reference). 290 * 291 * Note that this can also be used to copy data between two domains 292 * via a third party if the source and destination domains had previously 293 * grant appropriate access to their pages to the third party. 294 * 295 * source_offset specifies an offset in the source frame, dest_offset 296 * the offset in the target frame and len specifies the number of 297 * bytes to be copied. 298 */ 299 300#define _GNTCOPY_source_gref (0) 301#define GNTCOPY_source_gref (1<<_GNTCOPY_source_gref) 302#define _GNTCOPY_dest_gref (1) 303#define GNTCOPY_dest_gref (1<<_GNTCOPY_dest_gref) 304 305#define GNTTABOP_copy 5 306typedef struct gnttab_copy { 307 /* IN parameters. */ 308 struct { 309 union { 310 grant_ref_t ref; 311 xen_pfn_t gmfn; 312 } u; 313 domid_t domid; 314 uint16_t offset; 315 } source, dest; 316 uint16_t len; 317 uint16_t flags; /* GNTCOPY_* */ 318 /* OUT parameters. */ 319 int16_t status; 320} gnttab_copy_t; 321DEFINE_XEN_GUEST_HANDLE(gnttab_copy_t); 322 323/* 324 * GNTTABOP_query_size: Query the current and maximum sizes of the shared 325 * grant table. 326 * NOTES: 327 * 1. <dom> may be specified as DOMID_SELF. 328 * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF. 329 */ 330#define GNTTABOP_query_size 6 331struct gnttab_query_size { 332 /* IN parameters. */ 333 domid_t dom; 334 /* OUT parameters. */ 335 uint32_t nr_frames; 336 uint32_t max_nr_frames; 337 int16_t status; /* GNTST_* */ 338}; 339typedef struct gnttab_query_size gnttab_query_size_t; 340DEFINE_XEN_GUEST_HANDLE(gnttab_query_size_t); 341 342/* 343 * GNTTABOP_unmap_and_replace: Destroy one or more grant-reference mappings 344 * tracked by <handle> but atomically replace the page table entry with one 345 * pointing to the machine address under <new_addr>. <new_addr> will be 346 * redirected to the null entry. 347 * NOTES: 348 * 1. The call may fail in an undefined manner if either mapping is not 349 * tracked by <handle>. 350 * 2. After executing a batch of unmaps, it is guaranteed that no stale 351 * mappings will remain in the device or host TLBs. 352 */ 353#define GNTTABOP_unmap_and_replace 7 354struct gnttab_unmap_and_replace { 355 /* IN parameters. */ 356 uint64_t host_addr; 357 uint64_t new_addr; 358 grant_handle_t handle; 359 /* OUT parameters. */ 360 int16_t status; /* GNTST_* */ 361}; 362typedef struct gnttab_unmap_and_replace gnttab_unmap_and_replace_t; 363DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_and_replace_t); 364 365 366/* 367 * Bitfield values for update_pin_status.flags. 368 */ 369 /* Map the grant entry for access by I/O devices. */ 370#define _GNTMAP_device_map (0) 371#define GNTMAP_device_map (1<<_GNTMAP_device_map) 372 /* Map the grant entry for access by host CPUs. */ 373#define _GNTMAP_host_map (1) 374#define GNTMAP_host_map (1<<_GNTMAP_host_map) 375 /* Accesses to the granted frame will be restricted to read-only access. */ 376#define _GNTMAP_readonly (2) 377#define GNTMAP_readonly (1<<_GNTMAP_readonly) 378 /* 379 * GNTMAP_host_map subflag: 380 * 0 => The host mapping is usable only by the guest OS. 381 * 1 => The host mapping is usable by guest OS + current application. 382 */ 383#define _GNTMAP_application_map (3) 384#define GNTMAP_application_map (1<<_GNTMAP_application_map) 385 386 /* 387 * GNTMAP_contains_pte subflag: 388 * 0 => This map request contains a host virtual address. 389 * 1 => This map request contains the machine addess of the PTE to update. 390 */ 391#define _GNTMAP_contains_pte (4) 392#define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte) 393 394/* 395 * Values for error status returns. All errors are -ve. 396 */ 397#define GNTST_okay (0) /* Normal return. */ 398#define GNTST_general_error (-1) /* General undefined error. */ 399#define GNTST_bad_domain (-2) /* Unrecognsed domain id. */ 400#define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */ 401#define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */ 402#define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */ 403#define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/ 404#define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */ 405#define GNTST_permission_denied (-8) /* Not enough privilege for operation. */ 406#define GNTST_bad_page (-9) /* Specified page was invalid for op. */ 407#define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary. */ 408#define GNTST_address_too_big (-11) /* transfer page address too large. */ 409 410#define GNTTABOP_error_msgs { \ 411 "okay", \ 412 "undefined error", \ 413 "unrecognised domain id", \ 414 "invalid grant reference", \ 415 "invalid mapping handle", \ 416 "invalid virtual address", \ 417 "invalid device address", \ 418 "no spare translation slot in the I/O MMU", \ 419 "permission denied", \ 420 "bad page", \ 421 "copy arguments cross page boundary", \ 422 "page address size too large" \ 423} 424 425#endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */ 426 427/* 428 * Local variables: 429 * mode: C 430 * c-set-style: "BSD" 431 * c-basic-offset: 4 432 * tab-width: 4 433 * indent-tabs-mode: nil 434 * End: 435 */ 436