supp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/uaccess.h>
  17. #include "optee_private.h"
  18. struct optee_supp_req {
  19. struct list_head link;
  20. bool busy;
  21. u32 func;
  22. u32 ret;
  23. size_t num_params;
  24. struct tee_param *param;
  25. struct completion c;
  26. };
  27. void optee_supp_init(struct optee_supp *supp)
  28. {
  29. memset(supp, 0, sizeof(*supp));
  30. mutex_init(&supp->mutex);
  31. init_completion(&supp->reqs_c);
  32. idr_init(&supp->idr);
  33. INIT_LIST_HEAD(&supp->reqs);
  34. supp->req_id = -1;
  35. }
  36. void optee_supp_uninit(struct optee_supp *supp)
  37. {
  38. mutex_destroy(&supp->mutex);
  39. idr_destroy(&supp->idr);
  40. }
  41. void optee_supp_release(struct optee_supp *supp)
  42. {
  43. int id;
  44. struct optee_supp_req *req;
  45. struct optee_supp_req *req_tmp;
  46. mutex_lock(&supp->mutex);
  47. /* Abort all request retrieved by supplicant */
  48. idr_for_each_entry(&supp->idr, req, id) {
  49. req->busy = false;
  50. idr_remove(&supp->idr, id);
  51. req->ret = TEEC_ERROR_COMMUNICATION;
  52. complete(&req->c);
  53. }
  54. /* Abort all queued requests */
  55. list_for_each_entry_safe(req, req_tmp, &supp->reqs, link) {
  56. list_del(&req->link);
  57. req->ret = TEEC_ERROR_COMMUNICATION;
  58. complete(&req->c);
  59. }
  60. supp->ctx = NULL;
  61. supp->req_id = -1;
  62. mutex_unlock(&supp->mutex);
  63. }
  64. /**
  65. * optee_supp_thrd_req() - request service from supplicant
  66. * @ctx: context doing the request
  67. * @func: function requested
  68. * @num_params: number of elements in @param array
  69. * @param: parameters for function
  70. *
  71. * Returns result of operation to be passed to secure world
  72. */
  73. u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
  74. struct tee_param *param)
  75. {
  76. struct optee *optee = tee_get_drvdata(ctx->teedev);
  77. struct optee_supp *supp = &optee->supp;
  78. struct optee_supp_req *req = kzalloc(sizeof(*req), GFP_KERNEL);
  79. bool interruptable;
  80. u32 ret;
  81. if (!req)
  82. return TEEC_ERROR_OUT_OF_MEMORY;
  83. init_completion(&req->c);
  84. req->func = func;
  85. req->num_params = num_params;
  86. req->param = param;
  87. /* Insert the request in the request list */
  88. mutex_lock(&supp->mutex);
  89. list_add_tail(&req->link, &supp->reqs);
  90. mutex_unlock(&supp->mutex);
  91. /* Tell an eventual waiter there's a new request */
  92. complete(&supp->reqs_c);
  93. /*
  94. * Wait for supplicant to process and return result, once we've
  95. * returned from wait_for_completion(&req->c) successfully we have
  96. * exclusive access again.
  97. */
  98. while (wait_for_completion_interruptible(&req->c)) {
  99. mutex_lock(&supp->mutex);
  100. interruptable = !supp->ctx;
  101. if (interruptable) {
  102. /*
  103. * There's no supplicant available and since the
  104. * supp->mutex currently is held none can
  105. * become available until the mutex released
  106. * again.
  107. *
  108. * Interrupting an RPC to supplicant is only
  109. * allowed as a way of slightly improving the user
  110. * experience in case the supplicant hasn't been
  111. * started yet. During normal operation the supplicant
  112. * will serve all requests in a timely manner and
  113. * interrupting then wouldn't make sense.
  114. */
  115. interruptable = !req->busy;
  116. if (!req->busy)
  117. list_del(&req->link);
  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->busy = true;
  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. req->busy = false;
  273. idr_remove(&supp->idr, id);
  274. supp->req_id = -1;
  275. *num_meta = nm;
  276. return req;
  277. }
  278. /**
  279. * optee_supp_send() - send result of request from supplicant
  280. * @ctx: context sending result
  281. * @ret: return value of request
  282. * @num_params: number of parameters returned
  283. * @param: returned parameters
  284. *
  285. * Returns 0 on success or <0 on failure.
  286. */
  287. int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
  288. struct tee_param *param)
  289. {
  290. struct tee_device *teedev = ctx->teedev;
  291. struct optee *optee = tee_get_drvdata(teedev);
  292. struct optee_supp *supp = &optee->supp;
  293. struct optee_supp_req *req;
  294. size_t n;
  295. size_t num_meta;
  296. mutex_lock(&supp->mutex);
  297. req = supp_pop_req(supp, num_params, param, &num_meta);
  298. mutex_unlock(&supp->mutex);
  299. if (IS_ERR(req)) {
  300. /* Something is wrong, let supplicant restart. */
  301. return PTR_ERR(req);
  302. }
  303. /* Update out and in/out parameters */
  304. for (n = 0; n < req->num_params; n++) {
  305. struct tee_param *p = req->param + n;
  306. switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
  307. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  308. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  309. p->u.value.a = param[n + num_meta].u.value.a;
  310. p->u.value.b = param[n + num_meta].u.value.b;
  311. p->u.value.c = param[n + num_meta].u.value.c;
  312. break;
  313. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  314. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  315. p->u.memref.size = param[n + num_meta].u.memref.size;
  316. break;
  317. default:
  318. break;
  319. }
  320. }
  321. req->ret = ret;
  322. /* Let the requesting thread continue */
  323. complete(&req->c);
  324. return 0;
  325. }