rpc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /* Go to interruptible sleep */
  118. msleep_interruptible(msec_to_wait);
  119. arg->ret = TEEC_SUCCESS;
  120. return;
  121. bad:
  122. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  123. }
  124. static void handle_rpc_supp_cmd(struct tee_context *ctx,
  125. struct optee_msg_arg *arg)
  126. {
  127. struct tee_param *params;
  128. arg->ret_origin = TEEC_ORIGIN_COMMS;
  129. params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
  130. GFP_KERNEL);
  131. if (!params) {
  132. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  133. return;
  134. }
  135. if (optee_from_msg_param(params, arg->num_params, arg->params)) {
  136. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  137. goto out;
  138. }
  139. arg->ret = optee_supp_thrd_req(ctx, arg->cmd, arg->num_params, params);
  140. if (optee_to_msg_param(arg->params, arg->num_params, params))
  141. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  142. out:
  143. kfree(params);
  144. }
  145. static struct tee_shm *cmd_alloc_suppl(struct tee_context *ctx, size_t sz)
  146. {
  147. u32 ret;
  148. struct tee_param param;
  149. struct optee *optee = tee_get_drvdata(ctx->teedev);
  150. struct tee_shm *shm;
  151. param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
  152. param.u.value.a = OPTEE_MSG_RPC_SHM_TYPE_APPL;
  153. param.u.value.b = sz;
  154. param.u.value.c = 0;
  155. ret = optee_supp_thrd_req(ctx, OPTEE_MSG_RPC_CMD_SHM_ALLOC, 1, &param);
  156. if (ret)
  157. return ERR_PTR(-ENOMEM);
  158. mutex_lock(&optee->supp.mutex);
  159. /* Increases count as secure world doesn't have a reference */
  160. shm = tee_shm_get_from_id(optee->supp.ctx, param.u.value.c);
  161. mutex_unlock(&optee->supp.mutex);
  162. return shm;
  163. }
  164. static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
  165. struct optee_msg_arg *arg,
  166. struct optee_call_ctx *call_ctx)
  167. {
  168. phys_addr_t pa;
  169. struct tee_shm *shm;
  170. size_t sz;
  171. size_t n;
  172. arg->ret_origin = TEEC_ORIGIN_COMMS;
  173. if (!arg->num_params ||
  174. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  175. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  176. return;
  177. }
  178. for (n = 1; n < arg->num_params; n++) {
  179. if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE) {
  180. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  181. return;
  182. }
  183. }
  184. sz = arg->params[0].u.value.b;
  185. switch (arg->params[0].u.value.a) {
  186. case OPTEE_MSG_RPC_SHM_TYPE_APPL:
  187. shm = cmd_alloc_suppl(ctx, sz);
  188. break;
  189. case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
  190. shm = tee_shm_alloc(ctx, sz, TEE_SHM_MAPPED);
  191. break;
  192. default:
  193. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  194. return;
  195. }
  196. if (IS_ERR(shm)) {
  197. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  198. return;
  199. }
  200. if (tee_shm_get_pa(shm, 0, &pa)) {
  201. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  202. goto bad;
  203. }
  204. sz = tee_shm_get_size(shm);
  205. if (tee_shm_is_registered(shm)) {
  206. struct page **pages;
  207. u64 *pages_list;
  208. size_t page_num;
  209. pages = tee_shm_get_pages(shm, &page_num);
  210. if (!pages || !page_num) {
  211. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  212. goto bad;
  213. }
  214. pages_list = optee_allocate_pages_list(page_num);
  215. if (!pages_list) {
  216. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  217. goto bad;
  218. }
  219. call_ctx->pages_list = pages_list;
  220. call_ctx->num_entries = page_num;
  221. arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
  222. OPTEE_MSG_ATTR_NONCONTIG;
  223. /*
  224. * In the least bits of u.tmem.buf_ptr we store buffer offset
  225. * from 4k page, as described in OP-TEE ABI.
  226. */
  227. arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
  228. (tee_shm_get_page_offset(shm) &
  229. (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
  230. arg->params[0].u.tmem.size = tee_shm_get_size(shm);
  231. arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
  232. optee_fill_pages_list(pages_list, pages, page_num,
  233. tee_shm_get_page_offset(shm));
  234. } else {
  235. arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
  236. arg->params[0].u.tmem.buf_ptr = pa;
  237. arg->params[0].u.tmem.size = sz;
  238. arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
  239. }
  240. arg->ret = TEEC_SUCCESS;
  241. return;
  242. bad:
  243. tee_shm_free(shm);
  244. }
  245. static void cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm)
  246. {
  247. struct tee_param param;
  248. param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
  249. param.u.value.a = OPTEE_MSG_RPC_SHM_TYPE_APPL;
  250. param.u.value.b = tee_shm_get_id(shm);
  251. param.u.value.c = 0;
  252. /*
  253. * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
  254. * world has released its reference.
  255. *
  256. * It's better to do this before sending the request to supplicant
  257. * as we'd like to let the process doing the initial allocation to
  258. * do release the last reference too in order to avoid stacking
  259. * many pending fput() on the client process. This could otherwise
  260. * happen if secure world does many allocate and free in a single
  261. * invoke.
  262. */
  263. tee_shm_put(shm);
  264. optee_supp_thrd_req(ctx, OPTEE_MSG_RPC_CMD_SHM_FREE, 1, &param);
  265. }
  266. static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
  267. struct optee_msg_arg *arg)
  268. {
  269. struct tee_shm *shm;
  270. arg->ret_origin = TEEC_ORIGIN_COMMS;
  271. if (arg->num_params != 1 ||
  272. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  273. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  274. return;
  275. }
  276. shm = (struct tee_shm *)(unsigned long)arg->params[0].u.value.b;
  277. switch (arg->params[0].u.value.a) {
  278. case OPTEE_MSG_RPC_SHM_TYPE_APPL:
  279. cmd_free_suppl(ctx, shm);
  280. break;
  281. case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
  282. tee_shm_free(shm);
  283. break;
  284. default:
  285. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  286. }
  287. arg->ret = TEEC_SUCCESS;
  288. }
  289. static void free_pages_list(struct optee_call_ctx *call_ctx)
  290. {
  291. if (call_ctx->pages_list) {
  292. optee_free_pages_list(call_ctx->pages_list,
  293. call_ctx->num_entries);
  294. call_ctx->pages_list = NULL;
  295. call_ctx->num_entries = 0;
  296. }
  297. }
  298. void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx)
  299. {
  300. free_pages_list(call_ctx);
  301. }
  302. static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
  303. struct tee_shm *shm,
  304. struct optee_call_ctx *call_ctx)
  305. {
  306. struct optee_msg_arg *arg;
  307. arg = tee_shm_get_va(shm, 0);
  308. if (IS_ERR(arg)) {
  309. pr_err("%s: tee_shm_get_va %p failed\n", __func__, shm);
  310. return;
  311. }
  312. switch (arg->cmd) {
  313. case OPTEE_MSG_RPC_CMD_GET_TIME:
  314. handle_rpc_func_cmd_get_time(arg);
  315. break;
  316. case OPTEE_MSG_RPC_CMD_WAIT_QUEUE:
  317. handle_rpc_func_cmd_wq(optee, arg);
  318. break;
  319. case OPTEE_MSG_RPC_CMD_SUSPEND:
  320. handle_rpc_func_cmd_wait(arg);
  321. break;
  322. case OPTEE_MSG_RPC_CMD_SHM_ALLOC:
  323. free_pages_list(call_ctx);
  324. handle_rpc_func_cmd_shm_alloc(ctx, arg, call_ctx);
  325. break;
  326. case OPTEE_MSG_RPC_CMD_SHM_FREE:
  327. handle_rpc_func_cmd_shm_free(ctx, arg);
  328. break;
  329. default:
  330. handle_rpc_supp_cmd(ctx, arg);
  331. }
  332. }
  333. /**
  334. * optee_handle_rpc() - handle RPC from secure world
  335. * @ctx: context doing the RPC
  336. * @param: value of registers for the RPC
  337. * @call_ctx: call context. Preserved during one OP-TEE invocation
  338. *
  339. * Result of RPC is written back into @param.
  340. */
  341. void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
  342. struct optee_call_ctx *call_ctx)
  343. {
  344. struct tee_device *teedev = ctx->teedev;
  345. struct optee *optee = tee_get_drvdata(teedev);
  346. struct tee_shm *shm;
  347. phys_addr_t pa;
  348. switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
  349. case OPTEE_SMC_RPC_FUNC_ALLOC:
  350. shm = tee_shm_alloc(ctx, param->a1, TEE_SHM_MAPPED);
  351. if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
  352. reg_pair_from_64(&param->a1, &param->a2, pa);
  353. reg_pair_from_64(&param->a4, &param->a5,
  354. (unsigned long)shm);
  355. } else {
  356. param->a1 = 0;
  357. param->a2 = 0;
  358. param->a4 = 0;
  359. param->a5 = 0;
  360. }
  361. break;
  362. case OPTEE_SMC_RPC_FUNC_FREE:
  363. shm = reg_pair_to_ptr(param->a1, param->a2);
  364. tee_shm_free(shm);
  365. break;
  366. case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
  367. /*
  368. * A foreign interrupt was raised while secure world was
  369. * executing, since they are handled in Linux a dummy RPC is
  370. * performed to let Linux take the interrupt through the normal
  371. * vector.
  372. */
  373. break;
  374. case OPTEE_SMC_RPC_FUNC_CMD:
  375. shm = reg_pair_to_ptr(param->a1, param->a2);
  376. handle_rpc_func_cmd(ctx, optee, shm, call_ctx);
  377. break;
  378. default:
  379. pr_warn("Unknown RPC func 0x%x\n",
  380. (u32)OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0));
  381. break;
  382. }
  383. param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
  384. }