tee.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (c) 2015-2016, Linaro Limited
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef __TEE_H
  28. #define __TEE_H
  29. #include <linux/ioctl.h>
  30. #include <linux/types.h>
  31. /*
  32. * This file describes the API provided by a TEE driver to user space.
  33. *
  34. * Each TEE driver defines a TEE specific protocol which is used for the
  35. * data passed back and forth using TEE_IOC_CMD.
  36. */
  37. /* Helpers to make the ioctl defines */
  38. #define TEE_IOC_MAGIC 0xa4
  39. #define TEE_IOC_BASE 0
  40. #define TEE_MAX_ARG_SIZE 1024
  41. #define TEE_GEN_CAP_GP (1 << 0)/* GlobalPlatform compliant TEE */
  42. #define TEE_GEN_CAP_PRIVILEGED (1 << 1)/* Privileged device (for supplicant) */
  43. #define TEE_GEN_CAP_REG_MEM (1 << 2)/* Supports registering shared memory */
  44. #define TEE_GEN_CAP_MEMREF_NULL (1 << 3)/* NULL MemRef support */
  45. #define TEE_MEMREF_NULL (__u64)(-1) /* NULL MemRef Buffer */
  46. /*
  47. * TEE Implementation ID
  48. */
  49. #define TEE_IMPL_ID_OPTEE 1
  50. #define TEE_IMPL_ID_AMDTEE 2
  51. /*
  52. * OP-TEE specific capabilities
  53. */
  54. #define TEE_OPTEE_CAP_TZ (1 << 0)
  55. /**
  56. * struct tee_ioctl_version_data - TEE version
  57. * @impl_id: [out] TEE implementation id
  58. * @impl_caps: [out] Implementation specific capabilities
  59. * @gen_caps: [out] Generic capabilities, defined by TEE_GEN_CAPS_* above
  60. *
  61. * Identifies the TEE implementation, @impl_id is one of TEE_IMPL_ID_* above.
  62. * @impl_caps is implementation specific, for example TEE_OPTEE_CAP_*
  63. * is valid when @impl_id == TEE_IMPL_ID_OPTEE.
  64. */
  65. struct tee_ioctl_version_data {
  66. __u32 impl_id;
  67. __u32 impl_caps;
  68. __u32 gen_caps;
  69. };
  70. /**
  71. * TEE_IOC_VERSION - query version of TEE
  72. *
  73. * Takes a tee_ioctl_version_data struct and returns with the TEE version
  74. * data filled in.
  75. */
  76. #define TEE_IOC_VERSION _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 0, \
  77. struct tee_ioctl_version_data)
  78. /**
  79. * struct tee_ioctl_shm_alloc_data - Shared memory allocate argument
  80. * @size: [in/out] Size of shared memory to allocate
  81. * @flags: [in/out] Flags to/from allocation.
  82. * @id: [out] Identifier of the shared memory
  83. *
  84. * The flags field should currently be zero as input. Updated by the call
  85. * with actual flags as defined by TEE_IOCTL_SHM_* above.
  86. * This structure is used as argument for TEE_IOC_SHM_ALLOC below.
  87. */
  88. struct tee_ioctl_shm_alloc_data {
  89. __u64 size;
  90. __u32 flags;
  91. __s32 id;
  92. };
  93. /**
  94. * TEE_IOC_SHM_ALLOC - allocate shared memory
  95. *
  96. * Allocates shared memory between the user space process and secure OS.
  97. *
  98. * Returns a file descriptor on success or < 0 on failure
  99. *
  100. * The returned file descriptor is used to map the shared memory into user
  101. * space. The shared memory is freed when the descriptor is closed and the
  102. * memory is unmapped.
  103. */
  104. #define TEE_IOC_SHM_ALLOC _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 1, \
  105. struct tee_ioctl_shm_alloc_data)
  106. /**
  107. * struct tee_ioctl_buf_data - Variable sized buffer
  108. * @buf_ptr: [in] A pointer to a buffer
  109. * @buf_len: [in] Length of the buffer above
  110. *
  111. * Used as argument for TEE_IOC_OPEN_SESSION, TEE_IOC_INVOKE,
  112. * TEE_IOC_SUPPL_RECV, and TEE_IOC_SUPPL_SEND below.
  113. */
  114. struct tee_ioctl_buf_data {
  115. __u64 buf_ptr;
  116. __u64 buf_len;
  117. };
  118. /*
  119. * Attributes for struct tee_ioctl_param, selects field in the union
  120. */
  121. #define TEE_IOCTL_PARAM_ATTR_TYPE_NONE 0 /* parameter not used */
  122. /*
  123. * These defines value parameters (struct tee_ioctl_param_value)
  124. */
  125. #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT 1
  126. #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT 2
  127. #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT 3 /* input and output */
  128. /*
  129. * These defines shared memory reference parameters (struct
  130. * tee_ioctl_param_memref)
  131. */
  132. #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT 5
  133. #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT 6
  134. #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT 7 /* input and output */
  135. /*
  136. * Mask for the type part of the attribute, leaves room for more types
  137. */
  138. #define TEE_IOCTL_PARAM_ATTR_TYPE_MASK 0xff
  139. /* Meta parameter carrying extra information about the message. */
  140. #define TEE_IOCTL_PARAM_ATTR_META 0x100
  141. /* Mask of all known attr bits */
  142. #define TEE_IOCTL_PARAM_ATTR_MASK \
  143. (TEE_IOCTL_PARAM_ATTR_TYPE_MASK | TEE_IOCTL_PARAM_ATTR_META)
  144. /*
  145. * Matches TEEC_LOGIN_* in GP TEE Client API
  146. * Are only defined for GP compliant TEEs
  147. */
  148. #define TEE_IOCTL_LOGIN_PUBLIC 0
  149. #define TEE_IOCTL_LOGIN_USER 1
  150. #define TEE_IOCTL_LOGIN_GROUP 2
  151. #define TEE_IOCTL_LOGIN_APPLICATION 4
  152. #define TEE_IOCTL_LOGIN_USER_APPLICATION 5
  153. #define TEE_IOCTL_LOGIN_GROUP_APPLICATION 6
  154. /*
  155. * Disallow user-space to use GP implementation specific login
  156. * method range (0x80000000 - 0xBFFFFFFF). This range is rather
  157. * being reserved for REE kernel clients or TEE implementation.
  158. */
  159. #define TEE_IOCTL_LOGIN_REE_KERNEL_MIN 0x80000000
  160. #define TEE_IOCTL_LOGIN_REE_KERNEL_MAX 0xBFFFFFFF
  161. /* Private login method for REE kernel clients */
  162. #define TEE_IOCTL_LOGIN_REE_KERNEL 0x80000000
  163. /**
  164. * struct tee_ioctl_param - parameter
  165. * @attr: attributes
  166. * @a: if a memref, offset into the shared memory object, else a value parameter
  167. * @b: if a memref, size of the buffer, else a value parameter
  168. * @c: if a memref, shared memory identifier, else a value parameter
  169. *
  170. * @attr & TEE_PARAM_ATTR_TYPE_MASK indicates if memref or value is used in
  171. * the union. TEE_PARAM_ATTR_TYPE_VALUE_* indicates value and
  172. * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref. TEE_PARAM_ATTR_TYPE_NONE
  173. * indicates that none of the members are used.
  174. *
  175. * Shared memory is allocated with TEE_IOC_SHM_ALLOC which returns an
  176. * identifier representing the shared memory object. A memref can reference
  177. * a part of a shared memory by specifying an offset (@a) and size (@b) of
  178. * the object. To supply the entire shared memory object set the offset
  179. * (@a) to 0 and size (@b) to the previously returned size of the object.
  180. *
  181. * A client may need to present a NULL pointer in the argument
  182. * passed to a trusted application in the TEE.
  183. * This is also a requirement in GlobalPlatform Client API v1.0c
  184. * (section 3.2.5 memory references), which can be found at
  185. * http://www.globalplatform.org/specificationsdevice.asp
  186. *
  187. * If a NULL pointer is passed to a TA in the TEE, the (@c)
  188. * IOCTL parameters value must be set to TEE_MEMREF_NULL indicating a NULL
  189. * memory reference.
  190. */
  191. struct tee_ioctl_param {
  192. __u64 attr;
  193. __u64 a;
  194. __u64 b;
  195. __u64 c;
  196. };
  197. #define TEE_IOCTL_UUID_LEN 16
  198. /**
  199. * struct tee_ioctl_open_session_arg - Open session argument
  200. * @uuid: [in] UUID of the Trusted Application
  201. * @clnt_uuid: [in] UUID of client
  202. * @clnt_login: [in] Login class of client, TEE_IOCTL_LOGIN_* above
  203. * @cancel_id: [in] Cancellation id, a unique value to identify this request
  204. * @session: [out] Session id
  205. * @ret: [out] return value
  206. * @ret_origin [out] origin of the return value
  207. * @num_params [in] number of parameters following this struct
  208. */
  209. struct tee_ioctl_open_session_arg {
  210. __u8 uuid[TEE_IOCTL_UUID_LEN];
  211. __u8 clnt_uuid[TEE_IOCTL_UUID_LEN];
  212. __u32 clnt_login;
  213. __u32 cancel_id;
  214. __u32 session;
  215. __u32 ret;
  216. __u32 ret_origin;
  217. __u32 num_params;
  218. /* num_params tells the actual number of element in params */
  219. struct tee_ioctl_param params[];
  220. };
  221. /**
  222. * TEE_IOC_OPEN_SESSION - opens a session to a Trusted Application
  223. *
  224. * Takes a struct tee_ioctl_buf_data which contains a struct
  225. * tee_ioctl_open_session_arg followed by any array of struct
  226. * tee_ioctl_param
  227. */
  228. #define TEE_IOC_OPEN_SESSION _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 2, \
  229. struct tee_ioctl_buf_data)
  230. /**
  231. * struct tee_ioctl_invoke_func_arg - Invokes a function in a Trusted
  232. * Application
  233. * @func: [in] Trusted Application function, specific to the TA
  234. * @session: [in] Session id
  235. * @cancel_id: [in] Cancellation id, a unique value to identify this request
  236. * @ret: [out] return value
  237. * @ret_origin [out] origin of the return value
  238. * @num_params [in] number of parameters following this struct
  239. */
  240. struct tee_ioctl_invoke_arg {
  241. __u32 func;
  242. __u32 session;
  243. __u32 cancel_id;
  244. __u32 ret;
  245. __u32 ret_origin;
  246. __u32 num_params;
  247. /* num_params tells the actual number of element in params */
  248. struct tee_ioctl_param params[];
  249. };
  250. /**
  251. * TEE_IOC_INVOKE - Invokes a function in a Trusted Application
  252. *
  253. * Takes a struct tee_ioctl_buf_data which contains a struct
  254. * tee_invoke_func_arg followed by any array of struct tee_param
  255. */
  256. #define TEE_IOC_INVOKE _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 3, \
  257. struct tee_ioctl_buf_data)
  258. /**
  259. * struct tee_ioctl_cancel_arg - Cancels an open session or invoke ioctl
  260. * @cancel_id: [in] Cancellation id, a unique value to identify this request
  261. * @session: [in] Session id, if the session is opened, else set to 0
  262. */
  263. struct tee_ioctl_cancel_arg {
  264. __u32 cancel_id;
  265. __u32 session;
  266. };
  267. /**
  268. * TEE_IOC_CANCEL - Cancels an open session or invoke
  269. */
  270. #define TEE_IOC_CANCEL _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 4, \
  271. struct tee_ioctl_cancel_arg)
  272. /**
  273. * struct tee_ioctl_close_session_arg - Closes an open session
  274. * @session: [in] Session id
  275. */
  276. struct tee_ioctl_close_session_arg {
  277. __u32 session;
  278. };
  279. /**
  280. * TEE_IOC_CLOSE_SESSION - Closes a session
  281. */
  282. #define TEE_IOC_CLOSE_SESSION _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 5, \
  283. struct tee_ioctl_close_session_arg)
  284. /**
  285. * struct tee_iocl_supp_recv_arg - Receive a request for a supplicant function
  286. * @func: [in] supplicant function
  287. * @num_params [in/out] number of parameters following this struct
  288. *
  289. * @num_params is the number of params that tee-supplicant has room to
  290. * receive when input, @num_params is the number of actual params
  291. * tee-supplicant receives when output.
  292. */
  293. struct tee_iocl_supp_recv_arg {
  294. __u32 func;
  295. __u32 num_params;
  296. /* num_params tells the actual number of element in params */
  297. struct tee_ioctl_param params[];
  298. };
  299. /**
  300. * TEE_IOC_SUPPL_RECV - Receive a request for a supplicant function
  301. *
  302. * Takes a struct tee_ioctl_buf_data which contains a struct
  303. * tee_iocl_supp_recv_arg followed by any array of struct tee_param
  304. */
  305. #define TEE_IOC_SUPPL_RECV _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 6, \
  306. struct tee_ioctl_buf_data)
  307. /**
  308. * struct tee_iocl_supp_send_arg - Send a response to a received request
  309. * @ret: [out] return value
  310. * @num_params [in] number of parameters following this struct
  311. */
  312. struct tee_iocl_supp_send_arg {
  313. __u32 ret;
  314. __u32 num_params;
  315. /* num_params tells the actual number of element in params */
  316. struct tee_ioctl_param params[];
  317. };
  318. /**
  319. * TEE_IOC_SUPPL_SEND - Send a response to a received request
  320. *
  321. * Takes a struct tee_ioctl_buf_data which contains a struct
  322. * tee_iocl_supp_send_arg followed by any array of struct tee_param
  323. */
  324. #define TEE_IOC_SUPPL_SEND _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 7, \
  325. struct tee_ioctl_buf_data)
  326. /**
  327. * struct tee_ioctl_shm_register_data - Shared memory register argument
  328. * @addr: [in] Start address of shared memory to register
  329. * @length: [in/out] Length of shared memory to register
  330. * @flags: [in/out] Flags to/from registration.
  331. * @id: [out] Identifier of the shared memory
  332. *
  333. * The flags field should currently be zero as input. Updated by the call
  334. * with actual flags as defined by TEE_IOCTL_SHM_* above.
  335. * This structure is used as argument for TEE_IOC_SHM_REGISTER below.
  336. */
  337. struct tee_ioctl_shm_register_data {
  338. __u64 addr;
  339. __u64 length;
  340. __u32 flags;
  341. __s32 id;
  342. };
  343. /**
  344. * TEE_IOC_SHM_REGISTER - Register shared memory argument
  345. *
  346. * Registers shared memory between the user space process and secure OS.
  347. *
  348. * Returns a file descriptor on success or < 0 on failure
  349. *
  350. * The shared memory is unregisterred when the descriptor is closed.
  351. */
  352. #define TEE_IOC_SHM_REGISTER _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 9, \
  353. struct tee_ioctl_shm_register_data)
  354. /*
  355. * Five syscalls are used when communicating with the TEE driver.
  356. * open(): opens the device associated with the driver
  357. * ioctl(): as described above operating on the file descriptor from open()
  358. * close(): two cases
  359. * - closes the device file descriptor
  360. * - closes a file descriptor connected to allocated shared memory
  361. * mmap(): maps shared memory into user space using information from struct
  362. * tee_ioctl_shm_alloc_data
  363. * munmap(): unmaps previously shared memory
  364. */
  365. #endif /*__TEE_H*/