supp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, Linaro Limited
  4. */
  5. #include <linux/device.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include "optee_private.h"
  9. struct optee_supp_req {
  10. struct list_head link;
  11. bool in_queue;
  12. u32 func;
  13. u32 ret;
  14. size_t num_params;
  15. struct tee_param *param;
  16. struct completion c;
  17. };
  18. void optee_supp_init(struct optee_supp *supp)
  19. {
  20. memset(supp, 0, sizeof(*supp));
  21. mutex_init(&supp->mutex);
  22. init_completion(&supp->reqs_c);
  23. idr_init(&supp->idr);
  24. INIT_LIST_HEAD(&supp->reqs);
  25. supp->req_id = -1;
  26. }
  27. void optee_supp_uninit(struct optee_supp *supp)
  28. {
  29. mutex_destroy(&supp->mutex);
  30. idr_destroy(&supp->idr);
  31. }
  32. void optee_supp_release(struct optee_supp *supp)
  33. {
  34. int id;
  35. struct optee_supp_req *req;
  36. struct optee_supp_req *req_tmp;
  37. mutex_lock(&supp->mutex);
  38. /* Abort all request retrieved by supplicant */
  39. idr_for_each_entry(&supp->idr, req, id) {
  40. idr_remove(&supp->idr, id);
  41. req->ret = TEEC_ERROR_COMMUNICATION;
  42. complete(&req->c);
  43. }
  44. /* Abort all queued requests */
  45. list_for_each_entry_safe(req, req_tmp, &supp->reqs, link) {
  46. list_del(&req->link);
  47. req->in_queue = false;
  48. req->ret = TEEC_ERROR_COMMUNICATION;
  49. complete(&req->c);
  50. }
  51. supp->ctx = NULL;
  52. supp->req_id = -1;
  53. mutex_unlock(&supp->mutex);
  54. }
  55. /**
  56. * optee_supp_thrd_req() - request service from supplicant
  57. * @ctx: context doing the request
  58. * @func: function requested
  59. * @num_params: number of elements in @param array
  60. * @param: parameters for function
  61. *
  62. * Returns result of operation to be passed to secure world
  63. */
  64. u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
  65. struct tee_param *param)
  66. {
  67. struct optee *optee = tee_get_drvdata(ctx->teedev);
  68. struct optee_supp *supp = &optee->supp;
  69. struct optee_supp_req *req;
  70. bool interruptable;
  71. u32 ret;
  72. /*
  73. * Return in case there is no supplicant available and
  74. * non-blocking request.
  75. */
  76. if (!supp->ctx && ctx->supp_nowait)
  77. return TEEC_ERROR_COMMUNICATION;
  78. req = kzalloc(sizeof(*req), GFP_KERNEL);
  79. if (!req)
  80. return TEEC_ERROR_OUT_OF_MEMORY;
  81. init_completion(&req->c);
  82. req->func = func;
  83. req->num_params = num_params;
  84. req->param = param;
  85. /* Insert the request in the request list */
  86. mutex_lock(&supp->mutex);
  87. list_add_tail(&req->link, &supp->reqs);
  88. req->in_queue = true;
  89. mutex_unlock(&supp->mutex);
  90. /* Tell an eventual waiter there's a new request */
  91. complete(&supp->reqs_c);
  92. /*
  93. * Wait for supplicant to process and return result, once we've
  94. * returned from wait_for_completion(&req->c) successfully we have
  95. * exclusive access again.
  96. */
  97. while (wait_for_completion_interruptible(&req->c)) {
  98. mutex_lock(&supp->mutex);
  99. interruptable = !supp->ctx;
  100. if (interruptable) {
  101. /*
  102. * There's no supplicant available and since the
  103. * supp->mutex currently is held none can
  104. * become available until the mutex released
  105. * again.
  106. *
  107. * Interrupting an RPC to supplicant is only
  108. * allowed as a way of slightly improving the user
  109. * experience in case the supplicant hasn't been
  110. * started yet. During normal operation the supplicant
  111. * will serve all requests in a timely manner and
  112. * interrupting then wouldn't make sense.
  113. */
  114. if (req->in_queue) {
  115. list_del(&req->link);
  116. req->in_queue = false;
  117. }
  118. }
  119. mutex_unlock(&supp->mutex);
  120. if (interruptable) {
  121. req->ret = TEEC_ERROR_COMMUNICATION;
  122. break;
  123. }
  124. }
  125. ret = req->ret;
  126. kfree(req);
  127. return ret;
  128. }
  129. static struct optee_supp_req *supp_pop_entry(struct optee_supp *supp,
  130. int num_params, int *id)
  131. {
  132. struct optee_supp_req *req;
  133. if (supp->req_id != -1) {
  134. /*
  135. * Supplicant should not mix synchronous and asnynchronous
  136. * requests.
  137. */
  138. return ERR_PTR(-EINVAL);
  139. }
  140. if (list_empty(&supp->reqs))
  141. return NULL;
  142. req = list_first_entry(&supp->reqs, struct optee_supp_req, link);
  143. if (num_params < req->num_params) {
  144. /* Not enough room for parameters */
  145. return ERR_PTR(-EINVAL);
  146. }
  147. *id = idr_alloc(&supp->idr, req, 1, 0, GFP_KERNEL);
  148. if (*id < 0)
  149. return ERR_PTR(-ENOMEM);
  150. list_del(&req->link);
  151. req->in_queue = false;
  152. return req;
  153. }
  154. static int supp_check_recv_params(size_t num_params, struct tee_param *params,
  155. size_t *num_meta)
  156. {
  157. size_t n;
  158. if (!num_params)
  159. return -EINVAL;
  160. /*
  161. * If there's memrefs we need to decrease those as they where
  162. * increased earlier and we'll even refuse to accept any below.
  163. */
  164. for (n = 0; n < num_params; n++)
  165. if (tee_param_is_memref(params + n) && params[n].u.memref.shm)
  166. tee_shm_put(params[n].u.memref.shm);
  167. /*
  168. * We only expect parameters as TEE_IOCTL_PARAM_ATTR_TYPE_NONE with
  169. * or without the TEE_IOCTL_PARAM_ATTR_META bit set.
  170. */
  171. for (n = 0; n < num_params; n++)
  172. if (params[n].attr &&
  173. params[n].attr != TEE_IOCTL_PARAM_ATTR_META)
  174. return -EINVAL;
  175. /* At most we'll need one meta parameter so no need to check for more */
  176. if (params->attr == TEE_IOCTL_PARAM_ATTR_META)
  177. *num_meta = 1;
  178. else
  179. *num_meta = 0;
  180. return 0;
  181. }
  182. /**
  183. * optee_supp_recv() - receive request for supplicant
  184. * @ctx: context receiving the request
  185. * @func: requested function in supplicant
  186. * @num_params: number of elements allocated in @param, updated with number
  187. * used elements
  188. * @param: space for parameters for @func
  189. *
  190. * Returns 0 on success or <0 on failure
  191. */
  192. int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
  193. struct tee_param *param)
  194. {
  195. struct tee_device *teedev = ctx->teedev;
  196. struct optee *optee = tee_get_drvdata(teedev);
  197. struct optee_supp *supp = &optee->supp;
  198. struct optee_supp_req *req = NULL;
  199. int id;
  200. size_t num_meta;
  201. int rc;
  202. rc = supp_check_recv_params(*num_params, param, &num_meta);
  203. if (rc)
  204. return rc;
  205. while (true) {
  206. mutex_lock(&supp->mutex);
  207. req = supp_pop_entry(supp, *num_params - num_meta, &id);
  208. mutex_unlock(&supp->mutex);
  209. if (req) {
  210. if (IS_ERR(req))
  211. return PTR_ERR(req);
  212. break;
  213. }
  214. /*
  215. * If we didn't get a request we'll block in
  216. * wait_for_completion() to avoid needless spinning.
  217. *
  218. * This is where supplicant will be hanging most of
  219. * the time, let's make this interruptable so we
  220. * can easily restart supplicant if needed.
  221. */
  222. if (wait_for_completion_interruptible(&supp->reqs_c))
  223. return -ERESTARTSYS;
  224. }
  225. if (num_meta) {
  226. /*
  227. * tee-supplicant support meta parameters -> requsts can be
  228. * processed asynchronously.
  229. */
  230. param->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT |
  231. TEE_IOCTL_PARAM_ATTR_META;
  232. param->u.value.a = id;
  233. param->u.value.b = 0;
  234. param->u.value.c = 0;
  235. } else {
  236. mutex_lock(&supp->mutex);
  237. supp->req_id = id;
  238. mutex_unlock(&supp->mutex);
  239. }
  240. *func = req->func;
  241. *num_params = req->num_params + num_meta;
  242. memcpy(param + num_meta, req->param,
  243. sizeof(struct tee_param) * req->num_params);
  244. return 0;
  245. }
  246. static struct optee_supp_req *supp_pop_req(struct optee_supp *supp,
  247. size_t num_params,
  248. struct tee_param *param,
  249. size_t *num_meta)
  250. {
  251. struct optee_supp_req *req;
  252. int id;
  253. size_t nm;
  254. const u32 attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT |
  255. TEE_IOCTL_PARAM_ATTR_META;
  256. if (!num_params)
  257. return ERR_PTR(-EINVAL);
  258. if (supp->req_id == -1) {
  259. if (param->attr != attr)
  260. return ERR_PTR(-EINVAL);
  261. id = param->u.value.a;
  262. nm = 1;
  263. } else {
  264. id = supp->req_id;
  265. nm = 0;
  266. }
  267. req = idr_find(&supp->idr, id);
  268. if (!req)
  269. return ERR_PTR(-ENOENT);
  270. if ((num_params - nm) != req->num_params)
  271. return ERR_PTR(-EINVAL);
  272. idr_remove(&supp->idr, id);
  273. supp->req_id = -1;
  274. *num_meta = nm;
  275. return req;
  276. }
  277. /**
  278. * optee_supp_send() - send result of request from supplicant
  279. * @ctx: context sending result
  280. * @ret: return value of request
  281. * @num_params: number of parameters returned
  282. * @param: returned parameters
  283. *
  284. * Returns 0 on success or <0 on failure.
  285. */
  286. int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
  287. struct tee_param *param)
  288. {
  289. struct tee_device *teedev = ctx->teedev;
  290. struct optee *optee = tee_get_drvdata(teedev);
  291. struct optee_supp *supp = &optee->supp;
  292. struct optee_supp_req *req;
  293. size_t n;
  294. size_t num_meta;
  295. mutex_lock(&supp->mutex);
  296. req = supp_pop_req(supp, num_params, param, &num_meta);
  297. mutex_unlock(&supp->mutex);
  298. if (IS_ERR(req)) {
  299. /* Something is wrong, let supplicant restart. */
  300. return PTR_ERR(req);
  301. }
  302. /* Update out and in/out parameters */
  303. for (n = 0; n < req->num_params; n++) {
  304. struct tee_param *p = req->param + n;
  305. switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
  306. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  307. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  308. p->u.value.a = param[n + num_meta].u.value.a;
  309. p->u.value.b = param[n + num_meta].u.value.b;
  310. p->u.value.c = param[n + num_meta].u.value.c;
  311. break;
  312. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  313. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  314. p->u.memref.size = param[n + num_meta].u.memref.size;
  315. break;
  316. default:
  317. break;
  318. }
  319. }
  320. req->ret = ret;
  321. /* Let the requesting thread continue */
  322. complete(&req->c);
  323. return 0;
  324. }