rpc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2015-2016, 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/slab.h>
  18. #include <linux/tee_drv.h>
  19. #include "optee_private.h"
  20. #include "optee_smc.h"
  21. struct wq_entry {
  22. struct list_head link;
  23. struct completion c;
  24. u32 key;
  25. };
  26. void optee_wait_queue_init(struct optee_wait_queue *priv)
  27. {
  28. mutex_init(&priv->mu);
  29. INIT_LIST_HEAD(&priv->db);
  30. }
  31. void optee_wait_queue_exit(struct optee_wait_queue *priv)
  32. {
  33. mutex_destroy(&priv->mu);
  34. }
  35. static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)
  36. {
  37. struct timespec64 ts;
  38. if (arg->num_params != 1)
  39. goto bad;
  40. if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
  41. OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT)
  42. goto bad;
  43. getnstimeofday64(&ts);
  44. arg->params[0].u.value.a = ts.tv_sec;
  45. arg->params[0].u.value.b = ts.tv_nsec;
  46. arg->ret = TEEC_SUCCESS;
  47. return;
  48. bad:
  49. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  50. }
  51. static struct wq_entry *wq_entry_get(struct optee_wait_queue *wq, u32 key)
  52. {
  53. struct wq_entry *w;
  54. mutex_lock(&wq->mu);
  55. list_for_each_entry(w, &wq->db, link)
  56. if (w->key == key)
  57. goto out;
  58. w = kmalloc(sizeof(*w), GFP_KERNEL);
  59. if (w) {
  60. init_completion(&w->c);
  61. w->key = key;
  62. list_add_tail(&w->link, &wq->db);
  63. }
  64. out:
  65. mutex_unlock(&wq->mu);
  66. return w;
  67. }
  68. static void wq_sleep(struct optee_wait_queue *wq, u32 key)
  69. {
  70. struct wq_entry *w = wq_entry_get(wq, key);
  71. if (w) {
  72. wait_for_completion(&w->c);
  73. mutex_lock(&wq->mu);
  74. list_del(&w->link);
  75. mutex_unlock(&wq->mu);
  76. kfree(w);
  77. }
  78. }
  79. static void wq_wakeup(struct optee_wait_queue *wq, u32 key)
  80. {
  81. struct wq_entry *w = wq_entry_get(wq, key);
  82. if (w)
  83. complete(&w->c);
  84. }
  85. static void handle_rpc_func_cmd_wq(struct optee *optee,
  86. struct optee_msg_arg *arg)
  87. {
  88. if (arg->num_params != 1)
  89. goto bad;
  90. if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
  91. OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
  92. goto bad;
  93. switch (arg->params[0].u.value.a) {
  94. case OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP:
  95. wq_sleep(&optee->wait_queue, arg->params[0].u.value.b);
  96. break;
  97. case OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP:
  98. wq_wakeup(&optee->wait_queue, arg->params[0].u.value.b);
  99. break;
  100. default:
  101. goto bad;
  102. }
  103. arg->ret = TEEC_SUCCESS;
  104. return;
  105. bad:
  106. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  107. }
  108. static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
  109. {
  110. u32 msec_to_wait;
  111. if (arg->num_params != 1)
  112. goto bad;
  113. if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
  114. OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
  115. goto bad;
  116. msec_to_wait = arg->params[0].u.value.a;
  117. /* set task's state to interruptible sleep */
  118. set_current_state(TASK_INTERRUPTIBLE);
  119. /* take a nap */
  120. msleep(msec_to_wait);
  121. arg->ret = TEEC_SUCCESS;
  122. return;
  123. bad:
  124. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  125. }
  126. static void handle_rpc_supp_cmd(struct tee_context *ctx,
  127. struct optee_msg_arg *arg)
  128. {
  129. struct tee_param *params;
  130. arg->ret_origin = TEEC_ORIGIN_COMMS;
  131. params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
  132. GFP_KERNEL);
  133. if (!params) {
  134. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  135. return;
  136. }
  137. if (optee_from_msg_param(params, arg->num_params, arg->params)) {
  138. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  139. goto out;
  140. }
  141. arg->ret = optee_supp_thrd_req(ctx, arg->cmd, arg->num_params, params);
  142. if (optee_to_msg_param(arg->params, arg->num_params, params))
  143. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  144. out:
  145. kfree(params);
  146. }
  147. static struct tee_shm *cmd_alloc_suppl(struct tee_context *ctx, size_t sz)
  148. {
  149. u32 ret;
  150. struct tee_param param;
  151. struct optee *optee = tee_get_drvdata(ctx->teedev);
  152. struct tee_shm *shm;
  153. param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
  154. param.u.value.a = OPTEE_MSG_RPC_SHM_TYPE_APPL;
  155. param.u.value.b = sz;
  156. param.u.value.c = 0;
  157. ret = optee_supp_thrd_req(ctx, OPTEE_MSG_RPC_CMD_SHM_ALLOC, 1, &param);
  158. if (ret)
  159. return ERR_PTR(-ENOMEM);
  160. mutex_lock(&optee->supp.ctx_mutex);
  161. /* Increases count as secure world doesn't have a reference */
  162. shm = tee_shm_get_from_id(optee->supp.ctx, param.u.value.c);
  163. mutex_unlock(&optee->supp.ctx_mutex);
  164. return shm;
  165. }
  166. static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
  167. struct optee_msg_arg *arg)
  168. {
  169. phys_addr_t pa;
  170. struct tee_shm *shm;
  171. size_t sz;
  172. size_t n;
  173. arg->ret_origin = TEEC_ORIGIN_COMMS;
  174. if (!arg->num_params ||
  175. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  176. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  177. return;
  178. }
  179. for (n = 1; n < arg->num_params; n++) {
  180. if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE) {
  181. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  182. return;
  183. }
  184. }
  185. sz = arg->params[0].u.value.b;
  186. switch (arg->params[0].u.value.a) {
  187. case OPTEE_MSG_RPC_SHM_TYPE_APPL:
  188. shm = cmd_alloc_suppl(ctx, sz);
  189. break;
  190. case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
  191. shm = tee_shm_alloc(ctx, sz, TEE_SHM_MAPPED);
  192. break;
  193. default:
  194. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  195. return;
  196. }
  197. if (IS_ERR(shm)) {
  198. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  199. return;
  200. }
  201. if (tee_shm_get_pa(shm, 0, &pa)) {
  202. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  203. goto bad;
  204. }
  205. arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
  206. arg->params[0].u.tmem.buf_ptr = pa;
  207. arg->params[0].u.tmem.size = sz;
  208. arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
  209. arg->ret = TEEC_SUCCESS;
  210. return;
  211. bad:
  212. tee_shm_free(shm);
  213. }
  214. static void cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm)
  215. {
  216. struct tee_param param;
  217. param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
  218. param.u.value.a = OPTEE_MSG_RPC_SHM_TYPE_APPL;
  219. param.u.value.b = tee_shm_get_id(shm);
  220. param.u.value.c = 0;
  221. /*
  222. * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
  223. * world has released its reference.
  224. *
  225. * It's better to do this before sending the request to supplicant
  226. * as we'd like to let the process doing the initial allocation to
  227. * do release the last reference too in order to avoid stacking
  228. * many pending fput() on the client process. This could otherwise
  229. * happen if secure world does many allocate and free in a single
  230. * invoke.
  231. */
  232. tee_shm_put(shm);
  233. optee_supp_thrd_req(ctx, OPTEE_MSG_RPC_CMD_SHM_FREE, 1, &param);
  234. }
  235. static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
  236. struct optee_msg_arg *arg)
  237. {
  238. struct tee_shm *shm;
  239. arg->ret_origin = TEEC_ORIGIN_COMMS;
  240. if (arg->num_params != 1 ||
  241. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  242. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  243. return;
  244. }
  245. shm = (struct tee_shm *)(unsigned long)arg->params[0].u.value.b;
  246. switch (arg->params[0].u.value.a) {
  247. case OPTEE_MSG_RPC_SHM_TYPE_APPL:
  248. cmd_free_suppl(ctx, shm);
  249. break;
  250. case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
  251. tee_shm_free(shm);
  252. break;
  253. default:
  254. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  255. }
  256. arg->ret = TEEC_SUCCESS;
  257. }
  258. static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
  259. struct tee_shm *shm)
  260. {
  261. struct optee_msg_arg *arg;
  262. arg = tee_shm_get_va(shm, 0);
  263. if (IS_ERR(arg)) {
  264. pr_err("%s: tee_shm_get_va %p failed\n", __func__, shm);
  265. return;
  266. }
  267. switch (arg->cmd) {
  268. case OPTEE_MSG_RPC_CMD_GET_TIME:
  269. handle_rpc_func_cmd_get_time(arg);
  270. break;
  271. case OPTEE_MSG_RPC_CMD_WAIT_QUEUE:
  272. handle_rpc_func_cmd_wq(optee, arg);
  273. break;
  274. case OPTEE_MSG_RPC_CMD_SUSPEND:
  275. handle_rpc_func_cmd_wait(arg);
  276. break;
  277. case OPTEE_MSG_RPC_CMD_SHM_ALLOC:
  278. handle_rpc_func_cmd_shm_alloc(ctx, arg);
  279. break;
  280. case OPTEE_MSG_RPC_CMD_SHM_FREE:
  281. handle_rpc_func_cmd_shm_free(ctx, arg);
  282. break;
  283. default:
  284. handle_rpc_supp_cmd(ctx, arg);
  285. }
  286. }
  287. /**
  288. * optee_handle_rpc() - handle RPC from secure world
  289. * @ctx: context doing the RPC
  290. * @param: value of registers for the RPC
  291. *
  292. * Result of RPC is written back into @param.
  293. */
  294. void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param)
  295. {
  296. struct tee_device *teedev = ctx->teedev;
  297. struct optee *optee = tee_get_drvdata(teedev);
  298. struct tee_shm *shm;
  299. phys_addr_t pa;
  300. switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
  301. case OPTEE_SMC_RPC_FUNC_ALLOC:
  302. shm = tee_shm_alloc(ctx, param->a1, TEE_SHM_MAPPED);
  303. if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
  304. reg_pair_from_64(&param->a1, &param->a2, pa);
  305. reg_pair_from_64(&param->a4, &param->a5,
  306. (unsigned long)shm);
  307. } else {
  308. param->a1 = 0;
  309. param->a2 = 0;
  310. param->a4 = 0;
  311. param->a5 = 0;
  312. }
  313. break;
  314. case OPTEE_SMC_RPC_FUNC_FREE:
  315. shm = reg_pair_to_ptr(param->a1, param->a2);
  316. tee_shm_free(shm);
  317. break;
  318. case OPTEE_SMC_RPC_FUNC_IRQ:
  319. /*
  320. * An IRQ was raised while secure world was executing,
  321. * since all IRQs are handled in Linux a dummy RPC is
  322. * performed to let Linux take the IRQ through the normal
  323. * vector.
  324. */
  325. break;
  326. case OPTEE_SMC_RPC_FUNC_CMD:
  327. shm = reg_pair_to_ptr(param->a1, param->a2);
  328. handle_rpc_func_cmd(ctx, optee, shm);
  329. break;
  330. default:
  331. pr_warn("Unknown RPC func 0x%x\n",
  332. (u32)OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0));
  333. break;
  334. }
  335. param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
  336. }