call.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, Linaro Limited
  4. */
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/errno.h>
  8. #include <linux/mm.h>
  9. #include <linux/slab.h>
  10. #include <linux/tee_drv.h>
  11. #include <linux/types.h>
  12. #include "optee_private.h"
  13. #define MAX_ARG_PARAM_COUNT 6
  14. /*
  15. * How much memory we allocate for each entry. This doesn't have to be a
  16. * single page, but it makes sense to keep at least keep it as multiples of
  17. * the page size.
  18. */
  19. #define SHM_ENTRY_SIZE PAGE_SIZE
  20. /*
  21. * We need to have a compile time constant to be able to determine the
  22. * maximum needed size of the bit field.
  23. */
  24. #define MIN_ARG_SIZE OPTEE_MSG_GET_ARG_SIZE(MAX_ARG_PARAM_COUNT)
  25. #define MAX_ARG_COUNT_PER_ENTRY (SHM_ENTRY_SIZE / MIN_ARG_SIZE)
  26. /*
  27. * Shared memory for argument structs are cached here. The number of
  28. * arguments structs that can fit is determined at runtime depending on the
  29. * needed RPC parameter count reported by secure world
  30. * (optee->rpc_param_count).
  31. */
  32. struct optee_shm_arg_entry {
  33. struct list_head list_node;
  34. struct tee_shm *shm;
  35. DECLARE_BITMAP(map, MAX_ARG_COUNT_PER_ENTRY);
  36. };
  37. void optee_cq_wait_init(struct optee_call_queue *cq,
  38. struct optee_call_waiter *w)
  39. {
  40. /*
  41. * We're preparing to make a call to secure world. In case we can't
  42. * allocate a thread in secure world we'll end up waiting in
  43. * optee_cq_wait_for_completion().
  44. *
  45. * Normally if there's no contention in secure world the call will
  46. * complete and we can cleanup directly with optee_cq_wait_final().
  47. */
  48. mutex_lock(&cq->mutex);
  49. /*
  50. * We add ourselves to the queue, but we don't wait. This
  51. * guarantees that we don't lose a completion if secure world
  52. * returns busy and another thread just exited and try to complete
  53. * someone.
  54. */
  55. init_completion(&w->c);
  56. list_add_tail(&w->list_node, &cq->waiters);
  57. mutex_unlock(&cq->mutex);
  58. }
  59. void optee_cq_wait_for_completion(struct optee_call_queue *cq,
  60. struct optee_call_waiter *w)
  61. {
  62. wait_for_completion(&w->c);
  63. mutex_lock(&cq->mutex);
  64. /* Move to end of list to get out of the way for other waiters */
  65. list_del(&w->list_node);
  66. reinit_completion(&w->c);
  67. list_add_tail(&w->list_node, &cq->waiters);
  68. mutex_unlock(&cq->mutex);
  69. }
  70. static void optee_cq_complete_one(struct optee_call_queue *cq)
  71. {
  72. struct optee_call_waiter *w;
  73. list_for_each_entry(w, &cq->waiters, list_node) {
  74. if (!completion_done(&w->c)) {
  75. complete(&w->c);
  76. break;
  77. }
  78. }
  79. }
  80. void optee_cq_wait_final(struct optee_call_queue *cq,
  81. struct optee_call_waiter *w)
  82. {
  83. /*
  84. * We're done with the call to secure world. The thread in secure
  85. * world that was used for this call is now available for some
  86. * other task to use.
  87. */
  88. mutex_lock(&cq->mutex);
  89. /* Get out of the list */
  90. list_del(&w->list_node);
  91. /* Wake up one eventual waiting task */
  92. optee_cq_complete_one(cq);
  93. /*
  94. * If we're completed we've got a completion from another task that
  95. * was just done with its call to secure world. Since yet another
  96. * thread now is available in secure world wake up another eventual
  97. * waiting task.
  98. */
  99. if (completion_done(&w->c))
  100. optee_cq_complete_one(cq);
  101. mutex_unlock(&cq->mutex);
  102. }
  103. /* Requires the filpstate mutex to be held */
  104. static struct optee_session *find_session(struct optee_context_data *ctxdata,
  105. u32 session_id)
  106. {
  107. struct optee_session *sess;
  108. list_for_each_entry(sess, &ctxdata->sess_list, list_node)
  109. if (sess->session_id == session_id)
  110. return sess;
  111. return NULL;
  112. }
  113. void optee_shm_arg_cache_init(struct optee *optee, u32 flags)
  114. {
  115. INIT_LIST_HEAD(&optee->shm_arg_cache.shm_args);
  116. mutex_init(&optee->shm_arg_cache.mutex);
  117. optee->shm_arg_cache.flags = flags;
  118. }
  119. void optee_shm_arg_cache_uninit(struct optee *optee)
  120. {
  121. struct list_head *head = &optee->shm_arg_cache.shm_args;
  122. struct optee_shm_arg_entry *entry;
  123. mutex_destroy(&optee->shm_arg_cache.mutex);
  124. while (!list_empty(head)) {
  125. entry = list_first_entry(head, struct optee_shm_arg_entry,
  126. list_node);
  127. list_del(&entry->list_node);
  128. if (find_first_bit(entry->map, MAX_ARG_COUNT_PER_ENTRY) !=
  129. MAX_ARG_COUNT_PER_ENTRY) {
  130. pr_err("Freeing non-free entry\n");
  131. }
  132. tee_shm_free(entry->shm);
  133. kfree(entry);
  134. }
  135. }
  136. size_t optee_msg_arg_size(size_t rpc_param_count)
  137. {
  138. size_t sz = OPTEE_MSG_GET_ARG_SIZE(MAX_ARG_PARAM_COUNT);
  139. if (rpc_param_count)
  140. sz += OPTEE_MSG_GET_ARG_SIZE(rpc_param_count);
  141. return sz;
  142. }
  143. /**
  144. * optee_get_msg_arg() - Provide shared memory for argument struct
  145. * @ctx: Caller TEE context
  146. * @num_params: Number of parameter to store
  147. * @entry_ret: Entry pointer, needed when freeing the buffer
  148. * @shm_ret: Shared memory buffer
  149. * @offs_ret: Offset of argument strut in shared memory buffer
  150. *
  151. * @returns a pointer to the argument struct in memory, else an ERR_PTR
  152. */
  153. struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx,
  154. size_t num_params,
  155. struct optee_shm_arg_entry **entry_ret,
  156. struct tee_shm **shm_ret,
  157. u_int *offs_ret)
  158. {
  159. struct optee *optee = tee_get_drvdata(ctx->teedev);
  160. size_t sz = optee_msg_arg_size(optee->rpc_param_count);
  161. struct optee_shm_arg_entry *entry;
  162. struct optee_msg_arg *ma;
  163. size_t args_per_entry;
  164. u_long bit;
  165. u_int offs;
  166. void *res;
  167. if (num_params > MAX_ARG_PARAM_COUNT)
  168. return ERR_PTR(-EINVAL);
  169. if (optee->shm_arg_cache.flags & OPTEE_SHM_ARG_SHARED)
  170. args_per_entry = SHM_ENTRY_SIZE / sz;
  171. else
  172. args_per_entry = 1;
  173. mutex_lock(&optee->shm_arg_cache.mutex);
  174. list_for_each_entry(entry, &optee->shm_arg_cache.shm_args, list_node) {
  175. bit = find_first_zero_bit(entry->map, MAX_ARG_COUNT_PER_ENTRY);
  176. if (bit < args_per_entry)
  177. goto have_entry;
  178. }
  179. /*
  180. * No entry was found, let's allocate a new.
  181. */
  182. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  183. if (!entry) {
  184. res = ERR_PTR(-ENOMEM);
  185. goto out;
  186. }
  187. if (optee->shm_arg_cache.flags & OPTEE_SHM_ARG_ALLOC_PRIV)
  188. res = tee_shm_alloc_priv_buf(ctx, SHM_ENTRY_SIZE);
  189. else
  190. res = tee_shm_alloc_kernel_buf(ctx, SHM_ENTRY_SIZE);
  191. if (IS_ERR(res)) {
  192. kfree(entry);
  193. goto out;
  194. }
  195. entry->shm = res;
  196. list_add(&entry->list_node, &optee->shm_arg_cache.shm_args);
  197. bit = 0;
  198. have_entry:
  199. offs = bit * sz;
  200. res = tee_shm_get_va(entry->shm, offs);
  201. if (IS_ERR(res))
  202. goto out;
  203. ma = res;
  204. set_bit(bit, entry->map);
  205. memset(ma, 0, sz);
  206. ma->num_params = num_params;
  207. *entry_ret = entry;
  208. *shm_ret = entry->shm;
  209. *offs_ret = offs;
  210. out:
  211. mutex_unlock(&optee->shm_arg_cache.mutex);
  212. return res;
  213. }
  214. /**
  215. * optee_free_msg_arg() - Free previsouly obtained shared memory
  216. * @ctx: Caller TEE context
  217. * @entry: Pointer returned when the shared memory was obtained
  218. * @offs: Offset of shared memory buffer to free
  219. *
  220. * This function frees the shared memory obtained with optee_get_msg_arg().
  221. */
  222. void optee_free_msg_arg(struct tee_context *ctx,
  223. struct optee_shm_arg_entry *entry, u_int offs)
  224. {
  225. struct optee *optee = tee_get_drvdata(ctx->teedev);
  226. size_t sz = optee_msg_arg_size(optee->rpc_param_count);
  227. u_long bit;
  228. if (offs > SHM_ENTRY_SIZE || offs % sz) {
  229. pr_err("Invalid offs %u\n", offs);
  230. return;
  231. }
  232. bit = offs / sz;
  233. mutex_lock(&optee->shm_arg_cache.mutex);
  234. if (!test_bit(bit, entry->map))
  235. pr_err("Bit pos %lu is already free\n", bit);
  236. clear_bit(bit, entry->map);
  237. mutex_unlock(&optee->shm_arg_cache.mutex);
  238. }
  239. int optee_open_session(struct tee_context *ctx,
  240. struct tee_ioctl_open_session_arg *arg,
  241. struct tee_param *param)
  242. {
  243. struct optee *optee = tee_get_drvdata(ctx->teedev);
  244. struct optee_context_data *ctxdata = ctx->data;
  245. struct optee_shm_arg_entry *entry;
  246. struct tee_shm *shm;
  247. struct optee_msg_arg *msg_arg;
  248. struct optee_session *sess = NULL;
  249. uuid_t client_uuid;
  250. u_int offs;
  251. int rc;
  252. /* +2 for the meta parameters added below */
  253. msg_arg = optee_get_msg_arg(ctx, arg->num_params + 2,
  254. &entry, &shm, &offs);
  255. if (IS_ERR(msg_arg))
  256. return PTR_ERR(msg_arg);
  257. msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
  258. msg_arg->cancel_id = arg->cancel_id;
  259. /*
  260. * Initialize and add the meta parameters needed when opening a
  261. * session.
  262. */
  263. msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
  264. OPTEE_MSG_ATTR_META;
  265. msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
  266. OPTEE_MSG_ATTR_META;
  267. memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
  268. msg_arg->params[1].u.value.c = arg->clnt_login;
  269. rc = tee_session_calc_client_uuid(&client_uuid, arg->clnt_login,
  270. arg->clnt_uuid);
  271. if (rc)
  272. goto out;
  273. export_uuid(msg_arg->params[1].u.octets, &client_uuid);
  274. rc = optee->ops->to_msg_param(optee, msg_arg->params + 2,
  275. arg->num_params, param);
  276. if (rc)
  277. goto out;
  278. sess = kzalloc(sizeof(*sess), GFP_KERNEL);
  279. if (!sess) {
  280. rc = -ENOMEM;
  281. goto out;
  282. }
  283. if (optee->ops->do_call_with_arg(ctx, shm, offs)) {
  284. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  285. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  286. }
  287. if (msg_arg->ret == TEEC_SUCCESS) {
  288. /* A new session has been created, add it to the list. */
  289. sess->session_id = msg_arg->session;
  290. mutex_lock(&ctxdata->mutex);
  291. list_add(&sess->list_node, &ctxdata->sess_list);
  292. mutex_unlock(&ctxdata->mutex);
  293. } else {
  294. kfree(sess);
  295. }
  296. if (optee->ops->from_msg_param(optee, param, arg->num_params,
  297. msg_arg->params + 2)) {
  298. arg->ret = TEEC_ERROR_COMMUNICATION;
  299. arg->ret_origin = TEEC_ORIGIN_COMMS;
  300. /* Close session again to avoid leakage */
  301. optee_close_session(ctx, msg_arg->session);
  302. } else {
  303. arg->session = msg_arg->session;
  304. arg->ret = msg_arg->ret;
  305. arg->ret_origin = msg_arg->ret_origin;
  306. }
  307. out:
  308. optee_free_msg_arg(ctx, entry, offs);
  309. return rc;
  310. }
  311. int optee_close_session_helper(struct tee_context *ctx, u32 session)
  312. {
  313. struct optee *optee = tee_get_drvdata(ctx->teedev);
  314. struct optee_shm_arg_entry *entry;
  315. struct optee_msg_arg *msg_arg;
  316. struct tee_shm *shm;
  317. u_int offs;
  318. msg_arg = optee_get_msg_arg(ctx, 0, &entry, &shm, &offs);
  319. if (IS_ERR(msg_arg))
  320. return PTR_ERR(msg_arg);
  321. msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
  322. msg_arg->session = session;
  323. optee->ops->do_call_with_arg(ctx, shm, offs);
  324. optee_free_msg_arg(ctx, entry, offs);
  325. return 0;
  326. }
  327. int optee_close_session(struct tee_context *ctx, u32 session)
  328. {
  329. struct optee_context_data *ctxdata = ctx->data;
  330. struct optee_session *sess;
  331. /* Check that the session is valid and remove it from the list */
  332. mutex_lock(&ctxdata->mutex);
  333. sess = find_session(ctxdata, session);
  334. if (sess)
  335. list_del(&sess->list_node);
  336. mutex_unlock(&ctxdata->mutex);
  337. if (!sess)
  338. return -EINVAL;
  339. kfree(sess);
  340. return optee_close_session_helper(ctx, session);
  341. }
  342. int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
  343. struct tee_param *param)
  344. {
  345. struct optee *optee = tee_get_drvdata(ctx->teedev);
  346. struct optee_context_data *ctxdata = ctx->data;
  347. struct optee_shm_arg_entry *entry;
  348. struct optee_msg_arg *msg_arg;
  349. struct optee_session *sess;
  350. struct tee_shm *shm;
  351. u_int offs;
  352. int rc;
  353. /* Check that the session is valid */
  354. mutex_lock(&ctxdata->mutex);
  355. sess = find_session(ctxdata, arg->session);
  356. mutex_unlock(&ctxdata->mutex);
  357. if (!sess)
  358. return -EINVAL;
  359. msg_arg = optee_get_msg_arg(ctx, arg->num_params,
  360. &entry, &shm, &offs);
  361. if (IS_ERR(msg_arg))
  362. return PTR_ERR(msg_arg);
  363. msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
  364. msg_arg->func = arg->func;
  365. msg_arg->session = arg->session;
  366. msg_arg->cancel_id = arg->cancel_id;
  367. rc = optee->ops->to_msg_param(optee, msg_arg->params, arg->num_params,
  368. param);
  369. if (rc)
  370. goto out;
  371. if (optee->ops->do_call_with_arg(ctx, shm, offs)) {
  372. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  373. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  374. }
  375. if (optee->ops->from_msg_param(optee, param, arg->num_params,
  376. msg_arg->params)) {
  377. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  378. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  379. }
  380. arg->ret = msg_arg->ret;
  381. arg->ret_origin = msg_arg->ret_origin;
  382. out:
  383. optee_free_msg_arg(ctx, entry, offs);
  384. return rc;
  385. }
  386. int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
  387. {
  388. struct optee *optee = tee_get_drvdata(ctx->teedev);
  389. struct optee_context_data *ctxdata = ctx->data;
  390. struct optee_shm_arg_entry *entry;
  391. struct optee_msg_arg *msg_arg;
  392. struct optee_session *sess;
  393. struct tee_shm *shm;
  394. u_int offs;
  395. /* Check that the session is valid */
  396. mutex_lock(&ctxdata->mutex);
  397. sess = find_session(ctxdata, session);
  398. mutex_unlock(&ctxdata->mutex);
  399. if (!sess)
  400. return -EINVAL;
  401. msg_arg = optee_get_msg_arg(ctx, 0, &entry, &shm, &offs);
  402. if (IS_ERR(msg_arg))
  403. return PTR_ERR(msg_arg);
  404. msg_arg->cmd = OPTEE_MSG_CMD_CANCEL;
  405. msg_arg->session = session;
  406. msg_arg->cancel_id = cancel_id;
  407. optee->ops->do_call_with_arg(ctx, shm, offs);
  408. optee_free_msg_arg(ctx, entry, offs);
  409. return 0;
  410. }
  411. static bool is_normal_memory(pgprot_t p)
  412. {
  413. #if defined(CONFIG_ARM)
  414. return (((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEALLOC) ||
  415. ((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEBACK));
  416. #elif defined(CONFIG_ARM64)
  417. return (pgprot_val(p) & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL);
  418. #else
  419. #error "Unuspported architecture"
  420. #endif
  421. }
  422. static int __check_mem_type(struct mm_struct *mm, unsigned long start,
  423. unsigned long end)
  424. {
  425. struct vm_area_struct *vma;
  426. VMA_ITERATOR(vmi, mm, start);
  427. for_each_vma_range(vmi, vma, end) {
  428. if (!is_normal_memory(vma->vm_page_prot))
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. int optee_check_mem_type(unsigned long start, size_t num_pages)
  434. {
  435. struct mm_struct *mm = current->mm;
  436. int rc;
  437. /*
  438. * Allow kernel address to register with OP-TEE as kernel
  439. * pages are configured as normal memory only.
  440. */
  441. if (virt_addr_valid((void *)start) || is_vmalloc_addr((void *)start))
  442. return 0;
  443. mmap_read_lock(mm);
  444. rc = __check_mem_type(mm, start, start + num_pages * PAGE_SIZE);
  445. mmap_read_unlock(mm);
  446. return rc;
  447. }