call.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2015, Linaro Limited
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/arm-smccc.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/tee_drv.h>
  20. #include <linux/types.h>
  21. #include <linux/uaccess.h>
  22. #include "optee_private.h"
  23. #include "optee_smc.h"
  24. struct optee_call_waiter {
  25. struct list_head list_node;
  26. struct completion c;
  27. };
  28. static void optee_cq_wait_init(struct optee_call_queue *cq,
  29. struct optee_call_waiter *w)
  30. {
  31. /*
  32. * We're preparing to make a call to secure world. In case we can't
  33. * allocate a thread in secure world we'll end up waiting in
  34. * optee_cq_wait_for_completion().
  35. *
  36. * Normally if there's no contention in secure world the call will
  37. * complete and we can cleanup directly with optee_cq_wait_final().
  38. */
  39. mutex_lock(&cq->mutex);
  40. /*
  41. * We add ourselves to the queue, but we don't wait. This
  42. * guarantees that we don't lose a completion if secure world
  43. * returns busy and another thread just exited and try to complete
  44. * someone.
  45. */
  46. init_completion(&w->c);
  47. list_add_tail(&w->list_node, &cq->waiters);
  48. mutex_unlock(&cq->mutex);
  49. }
  50. static void optee_cq_wait_for_completion(struct optee_call_queue *cq,
  51. struct optee_call_waiter *w)
  52. {
  53. wait_for_completion(&w->c);
  54. mutex_lock(&cq->mutex);
  55. /* Move to end of list to get out of the way for other waiters */
  56. list_del(&w->list_node);
  57. reinit_completion(&w->c);
  58. list_add_tail(&w->list_node, &cq->waiters);
  59. mutex_unlock(&cq->mutex);
  60. }
  61. static void optee_cq_complete_one(struct optee_call_queue *cq)
  62. {
  63. struct optee_call_waiter *w;
  64. list_for_each_entry(w, &cq->waiters, list_node) {
  65. if (!completion_done(&w->c)) {
  66. complete(&w->c);
  67. break;
  68. }
  69. }
  70. }
  71. static void optee_cq_wait_final(struct optee_call_queue *cq,
  72. struct optee_call_waiter *w)
  73. {
  74. /*
  75. * We're done with the call to secure world. The thread in secure
  76. * world that was used for this call is now available for some
  77. * other task to use.
  78. */
  79. mutex_lock(&cq->mutex);
  80. /* Get out of the list */
  81. list_del(&w->list_node);
  82. /* Wake up one eventual waiting task */
  83. optee_cq_complete_one(cq);
  84. /*
  85. * If we're completed we've got a completion from another task that
  86. * was just done with its call to secure world. Since yet another
  87. * thread now is available in secure world wake up another eventual
  88. * waiting task.
  89. */
  90. if (completion_done(&w->c))
  91. optee_cq_complete_one(cq);
  92. mutex_unlock(&cq->mutex);
  93. }
  94. /* Requires the filpstate mutex to be held */
  95. static struct optee_session *find_session(struct optee_context_data *ctxdata,
  96. u32 session_id)
  97. {
  98. struct optee_session *sess;
  99. list_for_each_entry(sess, &ctxdata->sess_list, list_node)
  100. if (sess->session_id == session_id)
  101. return sess;
  102. return NULL;
  103. }
  104. /**
  105. * optee_do_call_with_arg() - Do an SMC to OP-TEE in secure world
  106. * @ctx: calling context
  107. * @parg: physical address of message to pass to secure world
  108. *
  109. * Does and SMC to OP-TEE in secure world and handles eventual resulting
  110. * Remote Procedure Calls (RPC) from OP-TEE.
  111. *
  112. * Returns return code from secure world, 0 is OK
  113. */
  114. u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg)
  115. {
  116. struct optee *optee = tee_get_drvdata(ctx->teedev);
  117. struct optee_call_waiter w;
  118. struct optee_rpc_param param = { };
  119. u32 ret;
  120. param.a0 = OPTEE_SMC_CALL_WITH_ARG;
  121. reg_pair_from_64(&param.a1, &param.a2, parg);
  122. /* Initialize waiter */
  123. optee_cq_wait_init(&optee->call_queue, &w);
  124. while (true) {
  125. struct arm_smccc_res res;
  126. optee->invoke_fn(param.a0, param.a1, param.a2, param.a3,
  127. param.a4, param.a5, param.a6, param.a7,
  128. &res);
  129. if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) {
  130. /*
  131. * Out of threads in secure world, wait for a thread
  132. * become available.
  133. */
  134. optee_cq_wait_for_completion(&optee->call_queue, &w);
  135. } else if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
  136. param.a0 = res.a0;
  137. param.a1 = res.a1;
  138. param.a2 = res.a2;
  139. param.a3 = res.a3;
  140. optee_handle_rpc(ctx, &param);
  141. } else {
  142. ret = res.a0;
  143. break;
  144. }
  145. }
  146. /*
  147. * We're done with our thread in secure world, if there's any
  148. * thread waiters wake up one.
  149. */
  150. optee_cq_wait_final(&optee->call_queue, &w);
  151. return ret;
  152. }
  153. static struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params,
  154. struct optee_msg_arg **msg_arg,
  155. phys_addr_t *msg_parg)
  156. {
  157. int rc;
  158. struct tee_shm *shm;
  159. struct optee_msg_arg *ma;
  160. shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params),
  161. TEE_SHM_MAPPED);
  162. if (IS_ERR(shm))
  163. return shm;
  164. ma = tee_shm_get_va(shm, 0);
  165. if (IS_ERR(ma)) {
  166. rc = PTR_ERR(ma);
  167. goto out;
  168. }
  169. rc = tee_shm_get_pa(shm, 0, msg_parg);
  170. if (rc)
  171. goto out;
  172. memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
  173. ma->num_params = num_params;
  174. *msg_arg = ma;
  175. out:
  176. if (rc) {
  177. tee_shm_free(shm);
  178. return ERR_PTR(rc);
  179. }
  180. return shm;
  181. }
  182. int optee_open_session(struct tee_context *ctx,
  183. struct tee_ioctl_open_session_arg *arg,
  184. struct tee_param *param)
  185. {
  186. struct optee_context_data *ctxdata = ctx->data;
  187. int rc;
  188. struct tee_shm *shm;
  189. struct optee_msg_arg *msg_arg;
  190. phys_addr_t msg_parg;
  191. struct optee_session *sess = NULL;
  192. /* +2 for the meta parameters added below */
  193. shm = get_msg_arg(ctx, arg->num_params + 2, &msg_arg, &msg_parg);
  194. if (IS_ERR(shm))
  195. return PTR_ERR(shm);
  196. msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
  197. msg_arg->cancel_id = arg->cancel_id;
  198. /*
  199. * Initialize and add the meta parameters needed when opening a
  200. * session.
  201. */
  202. msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
  203. OPTEE_MSG_ATTR_META;
  204. msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
  205. OPTEE_MSG_ATTR_META;
  206. memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
  207. memcpy(&msg_arg->params[1].u.value, arg->uuid, sizeof(arg->clnt_uuid));
  208. msg_arg->params[1].u.value.c = arg->clnt_login;
  209. rc = optee_to_msg_param(msg_arg->params + 2, arg->num_params, param);
  210. if (rc)
  211. goto out;
  212. sess = kzalloc(sizeof(*sess), GFP_KERNEL);
  213. if (!sess) {
  214. rc = -ENOMEM;
  215. goto out;
  216. }
  217. if (optee_do_call_with_arg(ctx, msg_parg)) {
  218. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  219. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  220. }
  221. if (msg_arg->ret == TEEC_SUCCESS) {
  222. /* A new session has been created, add it to the list. */
  223. sess->session_id = msg_arg->session;
  224. mutex_lock(&ctxdata->mutex);
  225. list_add(&sess->list_node, &ctxdata->sess_list);
  226. mutex_unlock(&ctxdata->mutex);
  227. } else {
  228. kfree(sess);
  229. }
  230. if (optee_from_msg_param(param, arg->num_params, msg_arg->params + 2)) {
  231. arg->ret = TEEC_ERROR_COMMUNICATION;
  232. arg->ret_origin = TEEC_ORIGIN_COMMS;
  233. /* Close session again to avoid leakage */
  234. optee_close_session(ctx, msg_arg->session);
  235. } else {
  236. arg->session = msg_arg->session;
  237. arg->ret = msg_arg->ret;
  238. arg->ret_origin = msg_arg->ret_origin;
  239. }
  240. out:
  241. tee_shm_free(shm);
  242. return rc;
  243. }
  244. int optee_close_session(struct tee_context *ctx, u32 session)
  245. {
  246. struct optee_context_data *ctxdata = ctx->data;
  247. struct tee_shm *shm;
  248. struct optee_msg_arg *msg_arg;
  249. phys_addr_t msg_parg;
  250. struct optee_session *sess;
  251. /* Check that the session is valid and remove it from the list */
  252. mutex_lock(&ctxdata->mutex);
  253. sess = find_session(ctxdata, session);
  254. if (sess)
  255. list_del(&sess->list_node);
  256. mutex_unlock(&ctxdata->mutex);
  257. if (!sess)
  258. return -EINVAL;
  259. kfree(sess);
  260. shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
  261. if (IS_ERR(shm))
  262. return PTR_ERR(shm);
  263. msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
  264. msg_arg->session = session;
  265. optee_do_call_with_arg(ctx, msg_parg);
  266. tee_shm_free(shm);
  267. return 0;
  268. }
  269. int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
  270. struct tee_param *param)
  271. {
  272. struct optee_context_data *ctxdata = ctx->data;
  273. struct tee_shm *shm;
  274. struct optee_msg_arg *msg_arg;
  275. phys_addr_t msg_parg;
  276. struct optee_session *sess;
  277. int rc;
  278. /* Check that the session is valid */
  279. mutex_lock(&ctxdata->mutex);
  280. sess = find_session(ctxdata, arg->session);
  281. mutex_unlock(&ctxdata->mutex);
  282. if (!sess)
  283. return -EINVAL;
  284. shm = get_msg_arg(ctx, arg->num_params, &msg_arg, &msg_parg);
  285. if (IS_ERR(shm))
  286. return PTR_ERR(shm);
  287. msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
  288. msg_arg->func = arg->func;
  289. msg_arg->session = arg->session;
  290. msg_arg->cancel_id = arg->cancel_id;
  291. rc = optee_to_msg_param(msg_arg->params, arg->num_params, param);
  292. if (rc)
  293. goto out;
  294. if (optee_do_call_with_arg(ctx, msg_parg)) {
  295. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  296. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  297. }
  298. if (optee_from_msg_param(param, arg->num_params, msg_arg->params)) {
  299. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  300. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  301. }
  302. arg->ret = msg_arg->ret;
  303. arg->ret_origin = msg_arg->ret_origin;
  304. out:
  305. tee_shm_free(shm);
  306. return rc;
  307. }
  308. int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
  309. {
  310. struct optee_context_data *ctxdata = ctx->data;
  311. struct tee_shm *shm;
  312. struct optee_msg_arg *msg_arg;
  313. phys_addr_t msg_parg;
  314. struct optee_session *sess;
  315. /* Check that the session is valid */
  316. mutex_lock(&ctxdata->mutex);
  317. sess = find_session(ctxdata, session);
  318. mutex_unlock(&ctxdata->mutex);
  319. if (!sess)
  320. return -EINVAL;
  321. shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
  322. if (IS_ERR(shm))
  323. return PTR_ERR(shm);
  324. msg_arg->cmd = OPTEE_MSG_CMD_CANCEL;
  325. msg_arg->session = session;
  326. msg_arg->cancel_id = cancel_id;
  327. optee_do_call_with_arg(ctx, msg_parg);
  328. tee_shm_free(shm);
  329. return 0;
  330. }
  331. /**
  332. * optee_enable_shm_cache() - Enables caching of some shared memory allocation
  333. * in OP-TEE
  334. * @optee: main service struct
  335. */
  336. void optee_enable_shm_cache(struct optee *optee)
  337. {
  338. struct optee_call_waiter w;
  339. /* We need to retry until secure world isn't busy. */
  340. optee_cq_wait_init(&optee->call_queue, &w);
  341. while (true) {
  342. struct arm_smccc_res res;
  343. optee->invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
  344. 0, &res);
  345. if (res.a0 == OPTEE_SMC_RETURN_OK)
  346. break;
  347. optee_cq_wait_for_completion(&optee->call_queue, &w);
  348. }
  349. optee_cq_wait_final(&optee->call_queue, &w);
  350. }
  351. /**
  352. * optee_disable_shm_cache() - Disables caching of some shared memory allocation
  353. * in OP-TEE
  354. * @optee: main service struct
  355. */
  356. void optee_disable_shm_cache(struct optee *optee)
  357. {
  358. struct optee_call_waiter w;
  359. /* We need to retry until secure world isn't busy. */
  360. optee_cq_wait_init(&optee->call_queue, &w);
  361. while (true) {
  362. union {
  363. struct arm_smccc_res smccc;
  364. struct optee_smc_disable_shm_cache_result result;
  365. } res;
  366. optee->invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
  367. 0, &res.smccc);
  368. if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL)
  369. break; /* All shm's freed */
  370. if (res.result.status == OPTEE_SMC_RETURN_OK) {
  371. struct tee_shm *shm;
  372. shm = reg_pair_to_ptr(res.result.shm_upper32,
  373. res.result.shm_lower32);
  374. tee_shm_free(shm);
  375. } else {
  376. optee_cq_wait_for_completion(&optee->call_queue, &w);
  377. }
  378. }
  379. optee_cq_wait_final(&optee->call_queue, &w);
  380. }