PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/include/xen/interface/grant_table.h

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