grant_table.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /******************************************************************************
  2. * grant_table.h
  3. *
  4. * Interface for granting foreign access to page frames, and receiving
  5. * page-ownership transfers.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to
  9. * deal in the Software without restriction, including without limitation the
  10. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. * Copyright (c) 2004, K A Fraser
  26. */
  27. #ifndef __XEN_PUBLIC_GRANT_TABLE_H__
  28. #define __XEN_PUBLIC_GRANT_TABLE_H__
  29. #include <xen/interface/xen.h>
  30. /***********************************
  31. * GRANT TABLE REPRESENTATION
  32. */
  33. /* Some rough guidelines on accessing and updating grant-table entries
  34. * in a concurrency-safe manner. For more information, Linux contains a
  35. * reference implementation for guest OSes (arch/xen/kernel/grant_table.c).
  36. *
  37. * NB. WMB is a no-op on current-generation x86 processors. However, a
  38. * compiler barrier will still be required.
  39. *
  40. * Introducing a valid entry into the grant table:
  41. * 1. Write ent->domid.
  42. * 2. Write ent->frame:
  43. * GTF_permit_access: Frame to which access is permitted.
  44. * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
  45. * frame, or zero if none.
  46. * 3. Write memory barrier (WMB).
  47. * 4. Write ent->flags, inc. valid type.
  48. *
  49. * Invalidating an unused GTF_permit_access entry:
  50. * 1. flags = ent->flags.
  51. * 2. Observe that !(flags & (GTF_reading|GTF_writing)).
  52. * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
  53. * NB. No need for WMB as reuse of entry is control-dependent on success of
  54. * step 3, and all architectures guarantee ordering of ctrl-dep writes.
  55. *
  56. * Invalidating an in-use GTF_permit_access entry:
  57. * This cannot be done directly. Request assistance from the domain controller
  58. * which can set a timeout on the use of a grant entry and take necessary
  59. * action. (NB. This is not yet implemented!).
  60. *
  61. * Invalidating an unused GTF_accept_transfer entry:
  62. * 1. flags = ent->flags.
  63. * 2. Observe that !(flags & GTF_transfer_committed). [*]
  64. * 3. Check result of SMP-safe CMPXCHG(&ent->flags, flags, 0).
  65. * NB. No need for WMB as reuse of entry is control-dependent on success of
  66. * step 3, and all architectures guarantee ordering of ctrl-dep writes.
  67. * [*] If GTF_transfer_committed is set then the grant entry is 'committed'.
  68. * The guest must /not/ modify the grant entry until the address of the
  69. * transferred frame is written. It is safe for the guest to spin waiting
  70. * for this to occur (detect by observing GTF_transfer_completed in
  71. * ent->flags).
  72. *
  73. * Invalidating a committed GTF_accept_transfer entry:
  74. * 1. Wait for (ent->flags & GTF_transfer_completed).
  75. *
  76. * Changing a GTF_permit_access from writable to read-only:
  77. * Use SMP-safe CMPXCHG to set GTF_readonly, while checking !GTF_writing.
  78. *
  79. * Changing a GTF_permit_access from read-only to writable:
  80. * Use SMP-safe bit-setting instruction.
  81. */
  82. /*
  83. * A grant table comprises a packed array of grant entries in one or more
  84. * page frames shared between Xen and a guest.
  85. * [XEN]: This field is written by Xen and read by the sharing guest.
  86. * [GST]: This field is written by the guest and read by Xen.
  87. */
  88. struct grant_entry {
  89. /* GTF_xxx: various type and flag information. [XEN,GST] */
  90. uint16_t flags;
  91. /* The domain being granted foreign privileges. [GST] */
  92. domid_t domid;
  93. /*
  94. * GTF_permit_access: Frame that @domid is allowed to map and access. [GST]
  95. * GTF_accept_transfer: Frame whose ownership transferred by @domid. [XEN]
  96. */
  97. uint32_t frame;
  98. };
  99. /*
  100. * Type of grant entry.
  101. * GTF_invalid: This grant entry grants no privileges.
  102. * GTF_permit_access: Allow @domid to map/access @frame.
  103. * GTF_accept_transfer: Allow @domid to transfer ownership of one page frame
  104. * to this guest. Xen writes the page number to @frame.
  105. */
  106. #define GTF_invalid (0U<<0)
  107. #define GTF_permit_access (1U<<0)
  108. #define GTF_accept_transfer (2U<<0)
  109. #define GTF_type_mask (3U<<0)
  110. /*
  111. * Subflags for GTF_permit_access.
  112. * GTF_readonly: Restrict @domid to read-only mappings and accesses. [GST]
  113. * GTF_reading: Grant entry is currently mapped for reading by @domid. [XEN]
  114. * GTF_writing: Grant entry is currently mapped for writing by @domid. [XEN]
  115. */
  116. #define _GTF_readonly (2)
  117. #define GTF_readonly (1U<<_GTF_readonly)
  118. #define _GTF_reading (3)
  119. #define GTF_reading (1U<<_GTF_reading)
  120. #define _GTF_writing (4)
  121. #define GTF_writing (1U<<_GTF_writing)
  122. /*
  123. * Subflags for GTF_accept_transfer:
  124. * GTF_transfer_committed: Xen sets this flag to indicate that it is committed
  125. * to transferring ownership of a page frame. When a guest sees this flag
  126. * it must /not/ modify the grant entry until GTF_transfer_completed is
  127. * set by Xen.
  128. * GTF_transfer_completed: It is safe for the guest to spin-wait on this flag
  129. * after reading GTF_transfer_committed. Xen will always write the frame
  130. * address, followed by ORing this flag, in a timely manner.
  131. */
  132. #define _GTF_transfer_committed (2)
  133. #define GTF_transfer_committed (1U<<_GTF_transfer_committed)
  134. #define _GTF_transfer_completed (3)
  135. #define GTF_transfer_completed (1U<<_GTF_transfer_completed)
  136. /***********************************
  137. * GRANT TABLE QUERIES AND USES
  138. */
  139. /*
  140. * Reference to a grant entry in a specified domain's grant table.
  141. */
  142. typedef uint32_t grant_ref_t;
  143. /*
  144. * Handle to track a mapping created via a grant reference.
  145. */
  146. typedef uint32_t grant_handle_t;
  147. /*
  148. * GNTTABOP_map_grant_ref: Map the grant entry (<dom>,<ref>) for access
  149. * by devices and/or host CPUs. If successful, <handle> is a tracking number
  150. * that must be presented later to destroy the mapping(s). On error, <handle>
  151. * is a negative status code.
  152. * NOTES:
  153. * 1. If GNTMAP_device_map is specified then <dev_bus_addr> is the address
  154. * via which I/O devices may access the granted frame.
  155. * 2. If GNTMAP_host_map is specified then a mapping will be added at
  156. * either a host virtual address in the current address space, or at
  157. * a PTE at the specified machine address. The type of mapping to
  158. * perform is selected through the GNTMAP_contains_pte flag, and the
  159. * address is specified in <host_addr>.
  160. * 3. Mappings should only be destroyed via GNTTABOP_unmap_grant_ref. If a
  161. * host mapping is destroyed by other means then it is *NOT* guaranteed
  162. * to be accounted to the correct grant reference!
  163. */
  164. #define GNTTABOP_map_grant_ref 0
  165. struct gnttab_map_grant_ref {
  166. /* IN parameters. */
  167. uint64_t host_addr;
  168. uint32_t flags; /* GNTMAP_* */
  169. grant_ref_t ref;
  170. domid_t dom;
  171. /* OUT parameters. */
  172. int16_t status; /* GNTST_* */
  173. grant_handle_t handle;
  174. uint64_t dev_bus_addr;
  175. };
  176. DEFINE_GUEST_HANDLE_STRUCT(gnttab_map_grant_ref);
  177. /*
  178. * GNTTABOP_unmap_grant_ref: Destroy one or more grant-reference mappings
  179. * tracked by <handle>. If <host_addr> or <dev_bus_addr> is zero, that
  180. * field is ignored. If non-zero, they must refer to a device/host mapping
  181. * that is tracked by <handle>
  182. * NOTES:
  183. * 1. The call may fail in an undefined manner if either mapping is not
  184. * tracked by <handle>.
  185. * 3. After executing a batch of unmaps, it is guaranteed that no stale
  186. * mappings will remain in the device or host TLBs.
  187. */
  188. #define GNTTABOP_unmap_grant_ref 1
  189. struct gnttab_unmap_grant_ref {
  190. /* IN parameters. */
  191. uint64_t host_addr;
  192. uint64_t dev_bus_addr;
  193. grant_handle_t handle;
  194. /* OUT parameters. */
  195. int16_t status; /* GNTST_* */
  196. };
  197. DEFINE_GUEST_HANDLE_STRUCT(gnttab_unmap_grant_ref);
  198. /*
  199. * GNTTABOP_setup_table: Set up a grant table for <dom> comprising at least
  200. * <nr_frames> pages. The frame addresses are written to the <frame_list>.
  201. * Only <nr_frames> addresses are written, even if the table is larger.
  202. * NOTES:
  203. * 1. <dom> may be specified as DOMID_SELF.
  204. * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
  205. * 3. Xen may not support more than a single grant-table page per domain.
  206. */
  207. #define GNTTABOP_setup_table 2
  208. struct gnttab_setup_table {
  209. /* IN parameters. */
  210. domid_t dom;
  211. uint32_t nr_frames;
  212. /* OUT parameters. */
  213. int16_t status; /* GNTST_* */
  214. GUEST_HANDLE(ulong) frame_list;
  215. };
  216. DEFINE_GUEST_HANDLE_STRUCT(gnttab_setup_table);
  217. /*
  218. * GNTTABOP_dump_table: Dump the contents of the grant table to the
  219. * xen console. Debugging use only.
  220. */
  221. #define GNTTABOP_dump_table 3
  222. struct gnttab_dump_table {
  223. /* IN parameters. */
  224. domid_t dom;
  225. /* OUT parameters. */
  226. int16_t status; /* GNTST_* */
  227. };
  228. DEFINE_GUEST_HANDLE_STRUCT(gnttab_dump_table);
  229. /*
  230. * GNTTABOP_transfer_grant_ref: Transfer <frame> to a foreign domain. The
  231. * foreign domain has previously registered its interest in the transfer via
  232. * <domid, ref>.
  233. *
  234. * Note that, even if the transfer fails, the specified page no longer belongs
  235. * to the calling domain *unless* the error is GNTST_bad_page.
  236. */
  237. #define GNTTABOP_transfer 4
  238. struct gnttab_transfer {
  239. /* IN parameters. */
  240. unsigned long mfn;
  241. domid_t domid;
  242. grant_ref_t ref;
  243. /* OUT parameters. */
  244. int16_t status;
  245. };
  246. DEFINE_GUEST_HANDLE_STRUCT(gnttab_transfer);
  247. /*
  248. * GNTTABOP_copy: Hypervisor based copy
  249. * source and destinations can be eithers MFNs or, for foreign domains,
  250. * grant references. the foreign domain has to grant read/write access
  251. * in its grant table.
  252. *
  253. * The flags specify what type source and destinations are (either MFN
  254. * or grant reference).
  255. *
  256. * Note that this can also be used to copy data between two domains
  257. * via a third party if the source and destination domains had previously
  258. * grant appropriate access to their pages to the third party.
  259. *
  260. * source_offset specifies an offset in the source frame, dest_offset
  261. * the offset in the target frame and len specifies the number of
  262. * bytes to be copied.
  263. */
  264. #define _GNTCOPY_source_gref (0)
  265. #define GNTCOPY_source_gref (1<<_GNTCOPY_source_gref)
  266. #define _GNTCOPY_dest_gref (1)
  267. #define GNTCOPY_dest_gref (1<<_GNTCOPY_dest_gref)
  268. #define GNTTABOP_copy 5
  269. struct gnttab_copy {
  270. /* IN parameters. */
  271. struct {
  272. union {
  273. grant_ref_t ref;
  274. unsigned long gmfn;
  275. } u;
  276. domid_t domid;
  277. uint16_t offset;
  278. } source, dest;
  279. uint16_t len;
  280. uint16_t flags; /* GNTCOPY_* */
  281. /* OUT parameters. */
  282. int16_t status;
  283. };
  284. DEFINE_GUEST_HANDLE_STRUCT(gnttab_copy);
  285. /*
  286. * GNTTABOP_query_size: Query the current and maximum sizes of the shared
  287. * grant table.
  288. * NOTES:
  289. * 1. <dom> may be specified as DOMID_SELF.
  290. * 2. Only a sufficiently-privileged domain may specify <dom> != DOMID_SELF.
  291. */
  292. #define GNTTABOP_query_size 6
  293. struct gnttab_query_size {
  294. /* IN parameters. */
  295. domid_t dom;
  296. /* OUT parameters. */
  297. uint32_t nr_frames;
  298. uint32_t max_nr_frames;
  299. int16_t status; /* GNTST_* */
  300. };
  301. DEFINE_GUEST_HANDLE_STRUCT(gnttab_query_size);
  302. /*
  303. * Bitfield values for update_pin_status.flags.
  304. */
  305. /* Map the grant entry for access by I/O devices. */
  306. #define _GNTMAP_device_map (0)
  307. #define GNTMAP_device_map (1<<_GNTMAP_device_map)
  308. /* Map the grant entry for access by host CPUs. */
  309. #define _GNTMAP_host_map (1)
  310. #define GNTMAP_host_map (1<<_GNTMAP_host_map)
  311. /* Accesses to the granted frame will be restricted to read-only access. */
  312. #define _GNTMAP_readonly (2)
  313. #define GNTMAP_readonly (1<<_GNTMAP_readonly)
  314. /*
  315. * GNTMAP_host_map subflag:
  316. * 0 => The host mapping is usable only by the guest OS.
  317. * 1 => The host mapping is usable by guest OS + current application.
  318. */
  319. #define _GNTMAP_application_map (3)
  320. #define GNTMAP_application_map (1<<_GNTMAP_application_map)
  321. /*
  322. * GNTMAP_contains_pte subflag:
  323. * 0 => This map request contains a host virtual address.
  324. * 1 => This map request contains the machine addess of the PTE to update.
  325. */
  326. #define _GNTMAP_contains_pte (4)
  327. #define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte)
  328. /*
  329. * Values for error status returns. All errors are -ve.
  330. */
  331. #define GNTST_okay (0) /* Normal return. */
  332. #define GNTST_general_error (-1) /* General undefined error. */
  333. #define GNTST_bad_domain (-2) /* Unrecognsed domain id. */
  334. #define GNTST_bad_gntref (-3) /* Unrecognised or inappropriate gntref. */
  335. #define GNTST_bad_handle (-4) /* Unrecognised or inappropriate handle. */
  336. #define GNTST_bad_virt_addr (-5) /* Inappropriate virtual address to map. */
  337. #define GNTST_bad_dev_addr (-6) /* Inappropriate device address to unmap.*/
  338. #define GNTST_no_device_space (-7) /* Out of space in I/O MMU. */
  339. #define GNTST_permission_denied (-8) /* Not enough privilege for operation. */
  340. #define GNTST_bad_page (-9) /* Specified page was invalid for op. */
  341. #define GNTST_bad_copy_arg (-10) /* copy arguments cross page boundary */
  342. #define GNTTABOP_error_msgs { \
  343. "okay", \
  344. "undefined error", \
  345. "unrecognised domain id", \
  346. "invalid grant reference", \
  347. "invalid mapping handle", \
  348. "invalid virtual address", \
  349. "invalid device address", \
  350. "no spare translation slot in the I/O MMU", \
  351. "permission denied", \
  352. "bad page", \
  353. "copy arguments cross page boundary" \
  354. }
  355. #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */