tee.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. =============
  2. TEE subsystem
  3. =============
  4. This document describes the TEE subsystem in Linux.
  5. A TEE (Trusted Execution Environment) is a trusted OS running in some
  6. secure environment, for example, TrustZone on ARM CPUs, or a separate
  7. secure co-processor etc. A TEE driver handles the details needed to
  8. communicate with the TEE.
  9. This subsystem deals with:
  10. - Registration of TEE drivers
  11. - Managing shared memory between Linux and the TEE
  12. - Providing a generic API to the TEE
  13. The TEE interface
  14. =================
  15. include/uapi/linux/tee.h defines the generic interface to a TEE.
  16. User space (the client) connects to the driver by opening /dev/tee[0-9]* or
  17. /dev/teepriv[0-9]*.
  18. - TEE_IOC_SHM_ALLOC allocates shared memory and returns a file descriptor
  19. which user space can mmap. When user space doesn't need the file
  20. descriptor any more, it should be closed. When shared memory isn't needed
  21. any longer it should be unmapped with munmap() to allow the reuse of
  22. memory.
  23. - TEE_IOC_VERSION lets user space know which TEE this driver handles and
  24. its capabilities.
  25. - TEE_IOC_OPEN_SESSION opens a new session to a Trusted Application.
  26. - TEE_IOC_INVOKE invokes a function in a Trusted Application.
  27. - TEE_IOC_CANCEL may cancel an ongoing TEE_IOC_OPEN_SESSION or TEE_IOC_INVOKE.
  28. - TEE_IOC_CLOSE_SESSION closes a session to a Trusted Application.
  29. There are two classes of clients, normal clients and supplicants. The latter is
  30. a helper process for the TEE to access resources in Linux, for example file
  31. system access. A normal client opens /dev/tee[0-9]* and a supplicant opens
  32. /dev/teepriv[0-9].
  33. Much of the communication between clients and the TEE is opaque to the
  34. driver. The main job for the driver is to receive requests from the
  35. clients, forward them to the TEE and send back the results. In the case of
  36. supplicants the communication goes in the other direction, the TEE sends
  37. requests to the supplicant which then sends back the result.
  38. The TEE kernel interface
  39. ========================
  40. Kernel provides a TEE bus infrastructure where a Trusted Application is
  41. represented as a device identified via Universally Unique Identifier (UUID) and
  42. client drivers register a table of supported device UUIDs.
  43. TEE bus infrastructure registers following APIs:
  44. match():
  45. iterates over the client driver UUID table to find a corresponding
  46. match for device UUID. If a match is found, then this particular device is
  47. probed via corresponding probe API registered by the client driver. This
  48. process happens whenever a device or a client driver is registered with TEE
  49. bus.
  50. uevent():
  51. notifies user-space (udev) whenever a new device is registered on
  52. TEE bus for auto-loading of modularized client drivers.
  53. TEE bus device enumeration is specific to underlying TEE implementation, so it
  54. is left open for TEE drivers to provide corresponding implementation.
  55. Then TEE client driver can talk to a matched Trusted Application using APIs
  56. listed in include/linux/tee_drv.h.
  57. TEE client driver example
  58. -------------------------
  59. Suppose a TEE client driver needs to communicate with a Trusted Application
  60. having UUID: ``ac6a4085-0e82-4c33-bf98-8eb8e118b6c2``, so driver registration
  61. snippet would look like::
  62. static const struct tee_client_device_id client_id_table[] = {
  63. {UUID_INIT(0xac6a4085, 0x0e82, 0x4c33,
  64. 0xbf, 0x98, 0x8e, 0xb8, 0xe1, 0x18, 0xb6, 0xc2)},
  65. {}
  66. };
  67. MODULE_DEVICE_TABLE(tee, client_id_table);
  68. static struct tee_client_driver client_driver = {
  69. .id_table = client_id_table,
  70. .driver = {
  71. .name = DRIVER_NAME,
  72. .bus = &tee_bus_type,
  73. .probe = client_probe,
  74. .remove = client_remove,
  75. },
  76. };
  77. static int __init client_init(void)
  78. {
  79. return driver_register(&client_driver.driver);
  80. }
  81. static void __exit client_exit(void)
  82. {
  83. driver_unregister(&client_driver.driver);
  84. }
  85. module_init(client_init);
  86. module_exit(client_exit);
  87. OP-TEE driver
  88. =============
  89. The OP-TEE driver handles OP-TEE [1] based TEEs. Currently it is only the ARM
  90. TrustZone based OP-TEE solution that is supported.
  91. Lowest level of communication with OP-TEE builds on ARM SMC Calling
  92. Convention (SMCCC) [2], which is the foundation for OP-TEE's SMC interface
  93. [3] used internally by the driver. Stacked on top of that is OP-TEE Message
  94. Protocol [4].
  95. OP-TEE SMC interface provides the basic functions required by SMCCC and some
  96. additional functions specific for OP-TEE. The most interesting functions are:
  97. - OPTEE_SMC_FUNCID_CALLS_UID (part of SMCCC) returns the version information
  98. which is then returned by TEE_IOC_VERSION
  99. - OPTEE_SMC_CALL_GET_OS_UUID returns the particular OP-TEE implementation, used
  100. to tell, for instance, a TrustZone OP-TEE apart from an OP-TEE running on a
  101. separate secure co-processor.
  102. - OPTEE_SMC_CALL_WITH_ARG drives the OP-TEE message protocol
  103. - OPTEE_SMC_GET_SHM_CONFIG lets the driver and OP-TEE agree on which memory
  104. range to used for shared memory between Linux and OP-TEE.
  105. The GlobalPlatform TEE Client API [5] is implemented on top of the generic
  106. TEE API.
  107. Picture of the relationship between the different components in the
  108. OP-TEE architecture::
  109. User space Kernel Secure world
  110. ~~~~~~~~~~ ~~~~~~ ~~~~~~~~~~~~
  111. +--------+ +-------------+
  112. | Client | | Trusted |
  113. +--------+ | Application |
  114. /\ +-------------+
  115. || +----------+ /\
  116. || |tee- | ||
  117. || |supplicant| \/
  118. || +----------+ +-------------+
  119. \/ /\ | TEE Internal|
  120. +-------+ || | API |
  121. + TEE | || +--------+--------+ +-------------+
  122. | Client| || | TEE | OP-TEE | | OP-TEE |
  123. | API | \/ | subsys | driver | | Trusted OS |
  124. +-------+----------------+----+-------+----+-----------+-------------+
  125. | Generic TEE API | | OP-TEE MSG |
  126. | IOCTL (TEE_IOC_*) | | SMCCC (OPTEE_SMC_CALL_*) |
  127. +-----------------------------+ +------------------------------+
  128. RPC (Remote Procedure Call) are requests from secure world to kernel driver
  129. or tee-supplicant. An RPC is identified by a special range of SMCCC return
  130. values from OPTEE_SMC_CALL_WITH_ARG. RPC messages which are intended for the
  131. kernel are handled by the kernel driver. Other RPC messages will be forwarded to
  132. tee-supplicant without further involvement of the driver, except switching
  133. shared memory buffer representation.
  134. OP-TEE device enumeration
  135. -------------------------
  136. OP-TEE provides a pseudo Trusted Application: drivers/tee/optee/device.c in
  137. order to support device enumeration. In other words, OP-TEE driver invokes this
  138. application to retrieve a list of Trusted Applications which can be registered
  139. as devices on the TEE bus.
  140. AMD-TEE driver
  141. ==============
  142. The AMD-TEE driver handles the communication with AMD's TEE environment. The
  143. TEE environment is provided by AMD Secure Processor.
  144. The AMD Secure Processor (formerly called Platform Security Processor or PSP)
  145. is a dedicated processor that features ARM TrustZone technology, along with a
  146. software-based Trusted Execution Environment (TEE) designed to enable
  147. third-party Trusted Applications. This feature is currently enabled only for
  148. APUs.
  149. The following picture shows a high level overview of AMD-TEE::
  150. |
  151. x86 |
  152. |
  153. User space (Kernel space) | AMD Secure Processor (PSP)
  154. ~~~~~~~~~~ ~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~
  155. |
  156. +--------+ | +-------------+
  157. | Client | | | Trusted |
  158. +--------+ | | Application |
  159. /\ | +-------------+
  160. || | /\
  161. || | ||
  162. || | \/
  163. || | +----------+
  164. || | | TEE |
  165. || | | Internal |
  166. \/ | | API |
  167. +---------+ +-----------+---------+ +----------+
  168. | TEE | | TEE | AMD-TEE | | AMD-TEE |
  169. | Client | | subsystem | driver | | Trusted |
  170. | API | | | | | OS |
  171. +---------+-----------+----+------+---------+---------+----------+
  172. | Generic TEE API | | ASP | Mailbox |
  173. | IOCTL (TEE_IOC_*) | | driver | Register Protocol |
  174. +--------------------------+ +---------+--------------------+
  175. At the lowest level (in x86), the AMD Secure Processor (ASP) driver uses the
  176. CPU to PSP mailbox regsister to submit commands to the PSP. The format of the
  177. command buffer is opaque to the ASP driver. It's role is to submit commands to
  178. the secure processor and return results to AMD-TEE driver. The interface
  179. between AMD-TEE driver and AMD Secure Processor driver can be found in [6].
  180. The AMD-TEE driver packages the command buffer payload for processing in TEE.
  181. The command buffer format for the different TEE commands can be found in [7].
  182. The TEE commands supported by AMD-TEE Trusted OS are:
  183. * TEE_CMD_ID_LOAD_TA - loads a Trusted Application (TA) binary into
  184. TEE environment.
  185. * TEE_CMD_ID_UNLOAD_TA - unloads TA binary from TEE environment.
  186. * TEE_CMD_ID_OPEN_SESSION - opens a session with a loaded TA.
  187. * TEE_CMD_ID_CLOSE_SESSION - closes session with loaded TA
  188. * TEE_CMD_ID_INVOKE_CMD - invokes a command with loaded TA
  189. * TEE_CMD_ID_MAP_SHARED_MEM - maps shared memory
  190. * TEE_CMD_ID_UNMAP_SHARED_MEM - unmaps shared memory
  191. AMD-TEE Trusted OS is the firmware running on AMD Secure Processor.
  192. The AMD-TEE driver registers itself with TEE subsystem and implements the
  193. following driver function callbacks:
  194. * get_version - returns the driver implementation id and capability.
  195. * open - sets up the driver context data structure.
  196. * release - frees up driver resources.
  197. * open_session - loads the TA binary and opens session with loaded TA.
  198. * close_session - closes session with loaded TA and unloads it.
  199. * invoke_func - invokes a command with loaded TA.
  200. cancel_req driver callback is not supported by AMD-TEE.
  201. The GlobalPlatform TEE Client API [5] can be used by the user space (client) to
  202. talk to AMD's TEE. AMD's TEE provides a secure environment for loading, opening
  203. a session, invoking commands and clossing session with TA.
  204. References
  205. ==========
  206. [1] https://github.com/OP-TEE/optee_os
  207. [2] http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html
  208. [3] drivers/tee/optee/optee_smc.h
  209. [4] drivers/tee/optee/optee_msg.h
  210. [5] http://www.globalplatform.org/specificationsdevice.asp look for
  211. "TEE Client API Specification v1.0" and click download.
  212. [6] include/linux/psp-tee.h
  213. [7] drivers/tee/amdtee/amdtee_if.h