mem.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Header files for KREE memory related functions.
  3. */
  4. #ifndef __KREE_MEM_H__
  5. #define __KREE_MEM_H__
  6. #if defined(CONFIG_MTK_IN_HOUSE_TEE_SUPPORT) || defined(CONFIG_TRUSTY)
  7. #include "tz_cross/trustzone.h"
  8. /* / KREE session handle type. */
  9. typedef uint32_t KREE_SESSION_HANDLE;
  10. #define KREE_SESSION_HANDLE_NULL ((KREE_SESSION_HANDLE)0)
  11. #define KREE_SESSION_HANDLE_FAIL ((KREE_SESSION_HANDLE)-1)
  12. /**
  13. * Memory handle define
  14. *
  15. * Handle is used to communicate with normal world:
  16. * 1. Memory information can not expose to normal world. (Major, important!)
  17. * 2. Too much information, and thet can be grouped by handle.
  18. *
  19. * All kinds of memory use the same handle define.
  20. * According to their different purpose, they are redefined to specific name.
  21. * Just for easy programming.
  22. */
  23. /* Shared memory handle define */
  24. typedef uint32_t KREE_SHAREDMEM_HANDLE;
  25. /* Secure memory handle define */
  26. typedef uint32_t KREE_SECUREMEM_HANDLE;
  27. /* Secure chunk memory handle define */
  28. typedef uint32_t KREE_SECURECM_HANDLE;
  29. /* Release Secure chunk memory handle define */
  30. typedef uint32_t KREE_RELEASECM_HANDLE;
  31. /**
  32. * Shared memory parameter
  33. *
  34. * It defines the types for shared memory.
  35. *
  36. * @param buffer A pointer to shared memory buffer
  37. * @param size shared memory size in bytes
  38. */
  39. typedef struct {
  40. void *buffer;
  41. uint32_t size;
  42. } KREE_SHAREDMEM_PARAM;
  43. /* map_p: 0 = no remap, 1 = remap */
  44. TZ_RESULT kree_register_sharedmem(KREE_SESSION_HANDLE session,
  45. KREE_SHAREDMEM_HANDLE *mem_handle, void *start,
  46. uint32_t size, void *map_p);
  47. TZ_RESULT kree_unregister_sharedmem(KREE_SESSION_HANDLE session,
  48. KREE_SHAREDMEM_HANDLE mem_handle);
  49. /**
  50. * Shared memory
  51. *
  52. * A shared memory is normal memory, which can be seen by Normal world and
  53. * Secure world.
  54. * It is used to create the comminicattion between two worlds.
  55. * Currently, zero-copy data transfer is supportted, for simple and efficient
  56. * design.
  57. *
  58. * The shared memory lifetime:
  59. * 1. CA (Client Application at REE) prepares memory
  60. * 2. CA registers it to TEE scope.
  61. * 3. A handle is returned. CA can use it to communicate with TEE.
  62. * 4. If shared memory is not used, CA unregisters it.
  63. * 5. CA frees memory.
  64. *
  65. * Because it is zero-copy shared memory, the memory characteritics is
  66. * inherited. If the shared memory will be used for HW, CA must allocate
  67. * physical continuous memory.
  68. *
  69. * Note: Because shared memory can be seen by both Normal and Secure world.
  70. * It is a possible weakpoint to bed attacked or leak secure data.
  71. *
  72. * Note: ONLY support memory allocated by kmalloc!!!
  73. */
  74. /**
  75. * Register shared memory
  76. *
  77. * @param session The session handle.
  78. * @param shm_handle [out] A pointer to shared memory handle.
  79. * @param param A pointer to shared memory parameters.
  80. * @return return code.
  81. */
  82. TZ_RESULT KREE_RegisterSharedmem(KREE_SESSION_HANDLE session,
  83. KREE_SHAREDMEM_HANDLE *shm_handle, KREE_SHAREDMEM_PARAM *param);
  84. /**
  85. * Unregister shared memory
  86. *
  87. * @param session The session handle.
  88. * @param shm_handle The shared memory handle.
  89. * @return return code.
  90. */
  91. TZ_RESULT KREE_UnregisterSharedmem(KREE_SESSION_HANDLE session,
  92. KREE_SHAREDMEM_HANDLE shm_handle);
  93. /**
  94. * Secure memory
  95. *
  96. * A secure memory can be seen only in Secure world.
  97. * Secure memory, here, is defined as external memory (ex: DRAM) protected
  98. * by trustzone.
  99. * It can protect from software attack very well, but can not protect from
  100. * physical attack, like memory probe.
  101. * CA (Client Application at REE) can ask TEE for a secure buffer, then
  102. * control it:
  103. * to reference, or to free...etc.
  104. *
  105. * Secure memory spec.:
  106. * 1. Protected by trustzone (NS = 0).
  107. * 2. External memory (ex: external DRAM).
  108. * 3. With cache.
  109. */
  110. /**
  111. * Secure memory allocation
  112. *
  113. * Allocate one memory.
  114. * If memory is allocated successfully, a handle will be provided.
  115. *
  116. * Memory lifetime:
  117. * 1. Allocate memory, and get the handle.
  118. * 2. If other process wants to use the same memory, reference it.
  119. * 3. If they stop to use it, unreference it.
  120. * 4. Free it (by unreference), if it is not used.
  121. *
  122. * Simple rules:
  123. * 1. start by allocate, end by unreference (for free).
  124. * 2. start by reference, end by unreference.
  125. *
  126. * @param session The session handle.
  127. * @param mem_handle [out] A pointer to secure memory handle.
  128. * @param alignment Memory alignment in bytes.
  129. * @param size The size of the buffer to be allocated in bytes.
  130. * @return return code.
  131. */
  132. TZ_RESULT KREE_AllocSecuremem(KREE_SESSION_HANDLE session,
  133. KREE_SECUREMEM_HANDLE *mem_handle, uint32_t alignment, uint32_t size);
  134. /**
  135. * Secure memory reference
  136. *
  137. * Reference memory.
  138. * Referen count will be increased by 1 after reference.
  139. *
  140. * Reference lifetime:
  141. * 1. Reference the memory before using it, if the memory is allocated by
  142. * other process.
  143. * 2. Unreference it if it is not used.
  144. *
  145. * @param session The session handle.
  146. * @param mem_handle The secure memory handle.
  147. * @param return return code.
  148. */
  149. TZ_RESULT KREE_ReferenceSecuremem(KREE_SESSION_HANDLE session,
  150. KREE_SECUREMEM_HANDLE mem_handle);
  151. /**
  152. * Secure memory unreference
  153. *
  154. * Unreference memory.
  155. * Reference count will be decreased by 1 after unreference.
  156. * Once reference count is zero, memory will be freed.
  157. *
  158. * @param session The session handle.
  159. * @param mem_handle The secure memory handle.
  160. * @param return return code.
  161. */
  162. TZ_RESULT KREE_UnreferenceSecuremem(KREE_SESSION_HANDLE session,
  163. KREE_SECUREMEM_HANDLE mem_handle);
  164. /**
  165. * Secure chunk memory
  166. *
  167. * A secure chunk memory can be seen only in Secure world.
  168. * It is a kind of secure memory but with difference characteristic:
  169. * 1. It is designed and optimized for chunk memory usage.
  170. * 2. For future work, it can be released as normal memory for more flexible
  171. * memory usage.
  172. *
  173. * Secure chunk memory spec.:
  174. * 1. Protected by trustzone (NS = 0).
  175. * 2. External memory (ex: external DRAM).
  176. * 3. With cache.
  177. * 4. For future, it can be released to normal world.
  178. */
  179. /**
  180. * Secure chunk memory allocation
  181. *
  182. * Allocate one memory.
  183. * If memory is allocated successfully, a handle will be provided.
  184. *
  185. * Memory lifetime:
  186. * 1. Allocate memory, and get the handle.
  187. * 2. If other process wants to use the same memory, reference it.
  188. * 3. If they stop to use it, unreference it.
  189. * 4. Free it (by unreference), if it is not used.
  190. *
  191. * Simple rules:
  192. * 1. start by allocate, end by unreference (for free).
  193. * 2. start by reference, end by unreference.
  194. *
  195. * @param session The session handle.
  196. * @param cm_handle [out] A pointer to secure chunk memory handle.
  197. * @param alignment Memory alignment in bytes.
  198. * @param size The size of the buffer to be allocated in bytes.
  199. * @return return code.
  200. */
  201. TZ_RESULT KREE_AllocSecurechunkmem(KREE_SESSION_HANDLE session,
  202. KREE_SECURECM_HANDLE *cm_handle, uint32_t alignment, uint32_t size);
  203. /**
  204. * Secure chunk memory reference
  205. *
  206. * Reference memory.
  207. * Referen count will be increased by 1 after reference.
  208. *
  209. * Reference lifetime:
  210. * 1. Reference the memory before using it, if the memory is allocated by
  211. * other process.
  212. * 2. Unreference it if it is not used.
  213. *
  214. * @param session The session handle.
  215. * @param cm_handle The secure chunk memory handle.
  216. * @param return return code.
  217. */
  218. TZ_RESULT KREE_ReferenceSecurechunkmem(KREE_SESSION_HANDLE session,
  219. KREE_SECURECM_HANDLE cm_handle);
  220. /**
  221. * Secure chunk memory unreference
  222. *
  223. * Unreference memory.
  224. * Reference count will be decreased by 1 after unreference.
  225. * Once reference count is zero, memory will be freed.
  226. *
  227. * @param session The session handle.
  228. * @param cm_handle The secure chunk memory handle.
  229. * @param return return code.
  230. */
  231. TZ_RESULT KREE_UnreferenceSecurechunkmem(KREE_SESSION_HANDLE session,
  232. KREE_SECURECM_HANDLE cm_handle);
  233. /**
  234. * Released secure chunk memory Read
  235. *
  236. * Read release secure chunk memory for normal world usage.
  237. *
  238. * @param session The session handle.
  239. * @param offset offset in bytes.
  240. * @param size size in bytes.
  241. * @param buffer The pointer to read buffer.
  242. * @param return return code.
  243. */
  244. TZ_RESULT KREE_ReadSecurechunkmem(KREE_SESSION_HANDLE session,
  245. uint32_t offset, uint32_t size, void *buffer);
  246. /**
  247. * Released secure chunk memory Write
  248. *
  249. * Write release secure chunk memory for normal world usage.
  250. *
  251. * @param session The session handle.
  252. * @param offset offset in bytes.
  253. * @param size size in bytes.
  254. * @param buffer The pointer to write buffer.
  255. * @param return return code.
  256. */
  257. TZ_RESULT KREE_WriteSecurechunkmem(KREE_SESSION_HANDLE session,
  258. uint32_t offset, uint32_t size, void *buffer);
  259. /**
  260. * Released secure chunk memory get size
  261. *
  262. * Get released secure chunk memory for normal world usage size.
  263. *
  264. * @param session The session handle.
  265. * @param size [out] The pointer to size in bytes.
  266. * @param return return code.
  267. */
  268. TZ_RESULT KREE_GetSecurechunkReleaseSize(KREE_SESSION_HANDLE session,
  269. uint32_t *size);
  270. /**
  271. * Start chunk memory allocation service in TEE
  272. *
  273. * Pass the reserve the buffer for secure chunk memory usage
  274. *
  275. * @param session The session handle.
  276. * @param start_pa The physical address of chunk memory buffer.
  277. * @param size The size in bytes of chunk memory buffer.
  278. * @param return return code.
  279. */
  280. TZ_RESULT KREE_StartSecurechunkmemSvc(KREE_SESSION_HANDLE session,
  281. unsigned long start_pa, uint32_t size);
  282. /**
  283. * Stop chunk memory allocation service in TEE
  284. *
  285. * reclaim the secure chunk memory used in TEE
  286. *
  287. * @param session The session handle.
  288. * @param cm_pa The physical address of chunk memory buffer.
  289. * @param size The size in bytes of chunk memory buffer.
  290. * @param return return code.
  291. */
  292. TZ_RESULT KREE_StopSecurechunkmemSvc(KREE_SESSION_HANDLE session,
  293. unsigned long *cm_pa, uint32_t *size);
  294. /**
  295. * Query chunk memory physical location / size in TEE
  296. *
  297. * Query the secure chunk memory information in TEE
  298. *
  299. * @param session The session handle.
  300. * @param cm_pa The physical address of chunk memory buffer.
  301. * @param size The size in bytes of chunk memory buffer.
  302. * @param return return code.
  303. */
  304. TZ_RESULT KREE_QuerySecurechunkmem(KREE_SESSION_HANDLE session,
  305. unsigned long *cm_pa, uint32_t *size);
  306. #endif /* CONFIG_MTK_IN_HOUSE_TEE_SUPPORT || CONFIG_TRUSTY*/
  307. #endif /* __KREE_MEM_H__ */