tee_core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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) "%s: " fmt, __func__
  15. #include <linux/cdev.h>
  16. #include <linux/device.h>
  17. #include <linux/fs.h>
  18. #include <linux/idr.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/tee_drv.h>
  22. #include <linux/uaccess.h>
  23. #include "tee_private.h"
  24. #define TEE_NUM_DEVICES 32
  25. #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
  26. /*
  27. * Unprivileged devices in the lower half range and privileged devices in
  28. * the upper half range.
  29. */
  30. static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
  31. static DEFINE_SPINLOCK(driver_lock);
  32. static struct class *tee_class;
  33. static dev_t tee_devt;
  34. static int tee_open(struct inode *inode, struct file *filp)
  35. {
  36. int rc;
  37. struct tee_device *teedev;
  38. struct tee_context *ctx;
  39. teedev = container_of(inode->i_cdev, struct tee_device, cdev);
  40. if (!tee_device_get(teedev))
  41. return -EINVAL;
  42. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  43. if (!ctx) {
  44. rc = -ENOMEM;
  45. goto err;
  46. }
  47. kref_init(&ctx->refcount);
  48. ctx->teedev = teedev;
  49. INIT_LIST_HEAD(&ctx->list_shm);
  50. filp->private_data = ctx;
  51. rc = teedev->desc->ops->open(ctx);
  52. if (rc)
  53. goto err;
  54. return 0;
  55. err:
  56. kfree(ctx);
  57. tee_device_put(teedev);
  58. return rc;
  59. }
  60. void teedev_ctx_get(struct tee_context *ctx)
  61. {
  62. if (ctx->releasing)
  63. return;
  64. kref_get(&ctx->refcount);
  65. }
  66. static void teedev_ctx_release(struct kref *ref)
  67. {
  68. struct tee_context *ctx = container_of(ref, struct tee_context,
  69. refcount);
  70. ctx->releasing = true;
  71. ctx->teedev->desc->ops->release(ctx);
  72. kfree(ctx);
  73. }
  74. void teedev_ctx_put(struct tee_context *ctx)
  75. {
  76. if (ctx->releasing)
  77. return;
  78. kref_put(&ctx->refcount, teedev_ctx_release);
  79. }
  80. static void teedev_close_context(struct tee_context *ctx)
  81. {
  82. tee_device_put(ctx->teedev);
  83. teedev_ctx_put(ctx);
  84. }
  85. static int tee_release(struct inode *inode, struct file *filp)
  86. {
  87. teedev_close_context(filp->private_data);
  88. return 0;
  89. }
  90. static int tee_ioctl_version(struct tee_context *ctx,
  91. struct tee_ioctl_version_data __user *uvers)
  92. {
  93. struct tee_ioctl_version_data vers;
  94. ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
  95. if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
  96. vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
  97. if (copy_to_user(uvers, &vers, sizeof(vers)))
  98. return -EFAULT;
  99. return 0;
  100. }
  101. static int tee_ioctl_shm_alloc(struct tee_context *ctx,
  102. struct tee_ioctl_shm_alloc_data __user *udata)
  103. {
  104. long ret;
  105. struct tee_ioctl_shm_alloc_data data;
  106. struct tee_shm *shm;
  107. if (copy_from_user(&data, udata, sizeof(data)))
  108. return -EFAULT;
  109. /* Currently no input flags are supported */
  110. if (data.flags)
  111. return -EINVAL;
  112. shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
  113. if (IS_ERR(shm))
  114. return PTR_ERR(shm);
  115. data.id = shm->id;
  116. data.flags = shm->flags;
  117. data.size = shm->size;
  118. if (copy_to_user(udata, &data, sizeof(data)))
  119. ret = -EFAULT;
  120. else
  121. ret = tee_shm_get_fd(shm);
  122. /*
  123. * When user space closes the file descriptor the shared memory
  124. * should be freed or if tee_shm_get_fd() failed then it will
  125. * be freed immediately.
  126. */
  127. tee_shm_put(shm);
  128. return ret;
  129. }
  130. static int
  131. tee_ioctl_shm_register(struct tee_context *ctx,
  132. struct tee_ioctl_shm_register_data __user *udata)
  133. {
  134. long ret;
  135. struct tee_ioctl_shm_register_data data;
  136. struct tee_shm *shm;
  137. if (copy_from_user(&data, udata, sizeof(data)))
  138. return -EFAULT;
  139. /* Currently no input flags are supported */
  140. if (data.flags)
  141. return -EINVAL;
  142. shm = tee_shm_register(ctx, data.addr, data.length,
  143. TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED);
  144. if (IS_ERR(shm))
  145. return PTR_ERR(shm);
  146. data.id = shm->id;
  147. data.flags = shm->flags;
  148. data.length = shm->size;
  149. if (copy_to_user(udata, &data, sizeof(data)))
  150. ret = -EFAULT;
  151. else
  152. ret = tee_shm_get_fd(shm);
  153. /*
  154. * When user space closes the file descriptor the shared memory
  155. * should be freed or if tee_shm_get_fd() failed then it will
  156. * be freed immediately.
  157. */
  158. tee_shm_put(shm);
  159. return ret;
  160. }
  161. static int params_from_user(struct tee_context *ctx, struct tee_param *params,
  162. size_t num_params,
  163. struct tee_ioctl_param __user *uparams)
  164. {
  165. size_t n;
  166. for (n = 0; n < num_params; n++) {
  167. struct tee_shm *shm;
  168. struct tee_ioctl_param ip;
  169. if (copy_from_user(&ip, uparams + n, sizeof(ip)))
  170. return -EFAULT;
  171. /* All unused attribute bits has to be zero */
  172. if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
  173. return -EINVAL;
  174. params[n].attr = ip.attr;
  175. switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
  176. case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
  177. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  178. break;
  179. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
  180. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  181. params[n].u.value.a = ip.a;
  182. params[n].u.value.b = ip.b;
  183. params[n].u.value.c = ip.c;
  184. break;
  185. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
  186. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  187. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  188. /*
  189. * If we fail to get a pointer to a shared memory
  190. * object (and increase the ref count) from an
  191. * identifier we return an error. All pointers that
  192. * has been added in params have an increased ref
  193. * count. It's the callers responibility to do
  194. * tee_shm_put() on all resolved pointers.
  195. */
  196. shm = tee_shm_get_from_id(ctx, ip.c);
  197. if (IS_ERR(shm))
  198. return PTR_ERR(shm);
  199. params[n].u.memref.shm_offs = ip.a;
  200. params[n].u.memref.size = ip.b;
  201. params[n].u.memref.shm = shm;
  202. break;
  203. default:
  204. /* Unknown attribute */
  205. return -EINVAL;
  206. }
  207. }
  208. return 0;
  209. }
  210. static int params_to_user(struct tee_ioctl_param __user *uparams,
  211. size_t num_params, struct tee_param *params)
  212. {
  213. size_t n;
  214. for (n = 0; n < num_params; n++) {
  215. struct tee_ioctl_param __user *up = uparams + n;
  216. struct tee_param *p = params + n;
  217. switch (p->attr) {
  218. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  219. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  220. if (put_user(p->u.value.a, &up->a) ||
  221. put_user(p->u.value.b, &up->b) ||
  222. put_user(p->u.value.c, &up->c))
  223. return -EFAULT;
  224. break;
  225. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  226. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  227. if (put_user((u64)p->u.memref.size, &up->b))
  228. return -EFAULT;
  229. default:
  230. break;
  231. }
  232. }
  233. return 0;
  234. }
  235. static int tee_ioctl_open_session(struct tee_context *ctx,
  236. struct tee_ioctl_buf_data __user *ubuf)
  237. {
  238. int rc;
  239. size_t n;
  240. struct tee_ioctl_buf_data buf;
  241. struct tee_ioctl_open_session_arg __user *uarg;
  242. struct tee_ioctl_open_session_arg arg;
  243. struct tee_ioctl_param __user *uparams = NULL;
  244. struct tee_param *params = NULL;
  245. bool have_session = false;
  246. if (!ctx->teedev->desc->ops->open_session)
  247. return -EINVAL;
  248. if (copy_from_user(&buf, ubuf, sizeof(buf)))
  249. return -EFAULT;
  250. if (buf.buf_len > TEE_MAX_ARG_SIZE ||
  251. buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
  252. return -EINVAL;
  253. uarg = u64_to_user_ptr(buf.buf_ptr);
  254. if (copy_from_user(&arg, uarg, sizeof(arg)))
  255. return -EFAULT;
  256. if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
  257. return -EINVAL;
  258. if (arg.num_params) {
  259. params = kcalloc(arg.num_params, sizeof(struct tee_param),
  260. GFP_KERNEL);
  261. if (!params)
  262. return -ENOMEM;
  263. uparams = uarg->params;
  264. rc = params_from_user(ctx, params, arg.num_params, uparams);
  265. if (rc)
  266. goto out;
  267. }
  268. rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
  269. if (rc)
  270. goto out;
  271. have_session = true;
  272. if (put_user(arg.session, &uarg->session) ||
  273. put_user(arg.ret, &uarg->ret) ||
  274. put_user(arg.ret_origin, &uarg->ret_origin)) {
  275. rc = -EFAULT;
  276. goto out;
  277. }
  278. rc = params_to_user(uparams, arg.num_params, params);
  279. out:
  280. /*
  281. * If we've succeeded to open the session but failed to communicate
  282. * it back to user space, close the session again to avoid leakage.
  283. */
  284. if (rc && have_session && ctx->teedev->desc->ops->close_session)
  285. ctx->teedev->desc->ops->close_session(ctx, arg.session);
  286. if (params) {
  287. /* Decrease ref count for all valid shared memory pointers */
  288. for (n = 0; n < arg.num_params; n++)
  289. if (tee_param_is_memref(params + n) &&
  290. params[n].u.memref.shm)
  291. tee_shm_put(params[n].u.memref.shm);
  292. kfree(params);
  293. }
  294. return rc;
  295. }
  296. static int tee_ioctl_invoke(struct tee_context *ctx,
  297. struct tee_ioctl_buf_data __user *ubuf)
  298. {
  299. int rc;
  300. size_t n;
  301. struct tee_ioctl_buf_data buf;
  302. struct tee_ioctl_invoke_arg __user *uarg;
  303. struct tee_ioctl_invoke_arg arg;
  304. struct tee_ioctl_param __user *uparams = NULL;
  305. struct tee_param *params = NULL;
  306. if (!ctx->teedev->desc->ops->invoke_func)
  307. return -EINVAL;
  308. if (copy_from_user(&buf, ubuf, sizeof(buf)))
  309. return -EFAULT;
  310. if (buf.buf_len > TEE_MAX_ARG_SIZE ||
  311. buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
  312. return -EINVAL;
  313. uarg = u64_to_user_ptr(buf.buf_ptr);
  314. if (copy_from_user(&arg, uarg, sizeof(arg)))
  315. return -EFAULT;
  316. if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
  317. return -EINVAL;
  318. if (arg.num_params) {
  319. params = kcalloc(arg.num_params, sizeof(struct tee_param),
  320. GFP_KERNEL);
  321. if (!params)
  322. return -ENOMEM;
  323. uparams = uarg->params;
  324. rc = params_from_user(ctx, params, arg.num_params, uparams);
  325. if (rc)
  326. goto out;
  327. }
  328. rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
  329. if (rc)
  330. goto out;
  331. if (put_user(arg.ret, &uarg->ret) ||
  332. put_user(arg.ret_origin, &uarg->ret_origin)) {
  333. rc = -EFAULT;
  334. goto out;
  335. }
  336. rc = params_to_user(uparams, arg.num_params, params);
  337. out:
  338. if (params) {
  339. /* Decrease ref count for all valid shared memory pointers */
  340. for (n = 0; n < arg.num_params; n++)
  341. if (tee_param_is_memref(params + n) &&
  342. params[n].u.memref.shm)
  343. tee_shm_put(params[n].u.memref.shm);
  344. kfree(params);
  345. }
  346. return rc;
  347. }
  348. static int tee_ioctl_cancel(struct tee_context *ctx,
  349. struct tee_ioctl_cancel_arg __user *uarg)
  350. {
  351. struct tee_ioctl_cancel_arg arg;
  352. if (!ctx->teedev->desc->ops->cancel_req)
  353. return -EINVAL;
  354. if (copy_from_user(&arg, uarg, sizeof(arg)))
  355. return -EFAULT;
  356. return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
  357. arg.session);
  358. }
  359. static int
  360. tee_ioctl_close_session(struct tee_context *ctx,
  361. struct tee_ioctl_close_session_arg __user *uarg)
  362. {
  363. struct tee_ioctl_close_session_arg arg;
  364. if (!ctx->teedev->desc->ops->close_session)
  365. return -EINVAL;
  366. if (copy_from_user(&arg, uarg, sizeof(arg)))
  367. return -EFAULT;
  368. return ctx->teedev->desc->ops->close_session(ctx, arg.session);
  369. }
  370. static int params_to_supp(struct tee_context *ctx,
  371. struct tee_ioctl_param __user *uparams,
  372. size_t num_params, struct tee_param *params)
  373. {
  374. size_t n;
  375. for (n = 0; n < num_params; n++) {
  376. struct tee_ioctl_param ip;
  377. struct tee_param *p = params + n;
  378. ip.attr = p->attr;
  379. switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
  380. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
  381. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  382. ip.a = p->u.value.a;
  383. ip.b = p->u.value.b;
  384. ip.c = p->u.value.c;
  385. break;
  386. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
  387. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  388. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  389. ip.b = p->u.memref.size;
  390. if (!p->u.memref.shm) {
  391. ip.a = 0;
  392. ip.c = (u64)-1; /* invalid shm id */
  393. break;
  394. }
  395. ip.a = p->u.memref.shm_offs;
  396. ip.c = p->u.memref.shm->id;
  397. break;
  398. default:
  399. ip.a = 0;
  400. ip.b = 0;
  401. ip.c = 0;
  402. break;
  403. }
  404. if (copy_to_user(uparams + n, &ip, sizeof(ip)))
  405. return -EFAULT;
  406. }
  407. return 0;
  408. }
  409. static int tee_ioctl_supp_recv(struct tee_context *ctx,
  410. struct tee_ioctl_buf_data __user *ubuf)
  411. {
  412. int rc;
  413. struct tee_ioctl_buf_data buf;
  414. struct tee_iocl_supp_recv_arg __user *uarg;
  415. struct tee_param *params;
  416. u32 num_params;
  417. u32 func;
  418. if (!ctx->teedev->desc->ops->supp_recv)
  419. return -EINVAL;
  420. if (copy_from_user(&buf, ubuf, sizeof(buf)))
  421. return -EFAULT;
  422. if (buf.buf_len > TEE_MAX_ARG_SIZE ||
  423. buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
  424. return -EINVAL;
  425. uarg = u64_to_user_ptr(buf.buf_ptr);
  426. if (get_user(num_params, &uarg->num_params))
  427. return -EFAULT;
  428. if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
  429. return -EINVAL;
  430. params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
  431. if (!params)
  432. return -ENOMEM;
  433. rc = params_from_user(ctx, params, num_params, uarg->params);
  434. if (rc)
  435. goto out;
  436. rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
  437. if (rc)
  438. goto out;
  439. if (put_user(func, &uarg->func) ||
  440. put_user(num_params, &uarg->num_params)) {
  441. rc = -EFAULT;
  442. goto out;
  443. }
  444. rc = params_to_supp(ctx, uarg->params, num_params, params);
  445. out:
  446. kfree(params);
  447. return rc;
  448. }
  449. static int params_from_supp(struct tee_param *params, size_t num_params,
  450. struct tee_ioctl_param __user *uparams)
  451. {
  452. size_t n;
  453. for (n = 0; n < num_params; n++) {
  454. struct tee_param *p = params + n;
  455. struct tee_ioctl_param ip;
  456. if (copy_from_user(&ip, uparams + n, sizeof(ip)))
  457. return -EFAULT;
  458. /* All unused attribute bits has to be zero */
  459. if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
  460. return -EINVAL;
  461. p->attr = ip.attr;
  462. switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
  463. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  464. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  465. /* Only out and in/out values can be updated */
  466. p->u.value.a = ip.a;
  467. p->u.value.b = ip.b;
  468. p->u.value.c = ip.c;
  469. break;
  470. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  471. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  472. /*
  473. * Only the size of the memref can be updated.
  474. * Since we don't have access to the original
  475. * parameters here, only store the supplied size.
  476. * The driver will copy the updated size into the
  477. * original parameters.
  478. */
  479. p->u.memref.shm = NULL;
  480. p->u.memref.shm_offs = 0;
  481. p->u.memref.size = ip.b;
  482. break;
  483. default:
  484. memset(&p->u, 0, sizeof(p->u));
  485. break;
  486. }
  487. }
  488. return 0;
  489. }
  490. static int tee_ioctl_supp_send(struct tee_context *ctx,
  491. struct tee_ioctl_buf_data __user *ubuf)
  492. {
  493. long rc;
  494. struct tee_ioctl_buf_data buf;
  495. struct tee_iocl_supp_send_arg __user *uarg;
  496. struct tee_param *params;
  497. u32 num_params;
  498. u32 ret;
  499. /* Not valid for this driver */
  500. if (!ctx->teedev->desc->ops->supp_send)
  501. return -EINVAL;
  502. if (copy_from_user(&buf, ubuf, sizeof(buf)))
  503. return -EFAULT;
  504. if (buf.buf_len > TEE_MAX_ARG_SIZE ||
  505. buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
  506. return -EINVAL;
  507. uarg = u64_to_user_ptr(buf.buf_ptr);
  508. if (get_user(ret, &uarg->ret) ||
  509. get_user(num_params, &uarg->num_params))
  510. return -EFAULT;
  511. if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
  512. return -EINVAL;
  513. params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
  514. if (!params)
  515. return -ENOMEM;
  516. rc = params_from_supp(params, num_params, uarg->params);
  517. if (rc)
  518. goto out;
  519. rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
  520. out:
  521. kfree(params);
  522. return rc;
  523. }
  524. static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  525. {
  526. struct tee_context *ctx = filp->private_data;
  527. void __user *uarg = (void __user *)arg;
  528. switch (cmd) {
  529. case TEE_IOC_VERSION:
  530. return tee_ioctl_version(ctx, uarg);
  531. case TEE_IOC_SHM_ALLOC:
  532. return tee_ioctl_shm_alloc(ctx, uarg);
  533. case TEE_IOC_SHM_REGISTER:
  534. return tee_ioctl_shm_register(ctx, uarg);
  535. case TEE_IOC_OPEN_SESSION:
  536. return tee_ioctl_open_session(ctx, uarg);
  537. case TEE_IOC_INVOKE:
  538. return tee_ioctl_invoke(ctx, uarg);
  539. case TEE_IOC_CANCEL:
  540. return tee_ioctl_cancel(ctx, uarg);
  541. case TEE_IOC_CLOSE_SESSION:
  542. return tee_ioctl_close_session(ctx, uarg);
  543. case TEE_IOC_SUPPL_RECV:
  544. return tee_ioctl_supp_recv(ctx, uarg);
  545. case TEE_IOC_SUPPL_SEND:
  546. return tee_ioctl_supp_send(ctx, uarg);
  547. default:
  548. return -EINVAL;
  549. }
  550. }
  551. static const struct file_operations tee_fops = {
  552. .owner = THIS_MODULE,
  553. .open = tee_open,
  554. .release = tee_release,
  555. .unlocked_ioctl = tee_ioctl,
  556. .compat_ioctl = tee_ioctl,
  557. };
  558. static void tee_release_device(struct device *dev)
  559. {
  560. struct tee_device *teedev = container_of(dev, struct tee_device, dev);
  561. spin_lock(&driver_lock);
  562. clear_bit(teedev->id, dev_mask);
  563. spin_unlock(&driver_lock);
  564. mutex_destroy(&teedev->mutex);
  565. idr_destroy(&teedev->idr);
  566. kfree(teedev);
  567. }
  568. /**
  569. * tee_device_alloc() - Allocate a new struct tee_device instance
  570. * @teedesc: Descriptor for this driver
  571. * @dev: Parent device for this device
  572. * @pool: Shared memory pool, NULL if not used
  573. * @driver_data: Private driver data for this device
  574. *
  575. * Allocates a new struct tee_device instance. The device is
  576. * removed by tee_device_unregister().
  577. *
  578. * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
  579. */
  580. struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
  581. struct device *dev,
  582. struct tee_shm_pool *pool,
  583. void *driver_data)
  584. {
  585. struct tee_device *teedev;
  586. void *ret;
  587. int rc;
  588. int offs = 0;
  589. if (!teedesc || !teedesc->name || !teedesc->ops ||
  590. !teedesc->ops->get_version || !teedesc->ops->open ||
  591. !teedesc->ops->release || !pool)
  592. return ERR_PTR(-EINVAL);
  593. teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
  594. if (!teedev) {
  595. ret = ERR_PTR(-ENOMEM);
  596. goto err;
  597. }
  598. if (teedesc->flags & TEE_DESC_PRIVILEGED)
  599. offs = TEE_NUM_DEVICES / 2;
  600. spin_lock(&driver_lock);
  601. teedev->id = find_next_zero_bit(dev_mask, TEE_NUM_DEVICES, offs);
  602. if (teedev->id < TEE_NUM_DEVICES)
  603. set_bit(teedev->id, dev_mask);
  604. spin_unlock(&driver_lock);
  605. if (teedev->id >= TEE_NUM_DEVICES) {
  606. ret = ERR_PTR(-ENOMEM);
  607. goto err;
  608. }
  609. snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
  610. teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
  611. teedev->id - offs);
  612. teedev->dev.class = tee_class;
  613. teedev->dev.release = tee_release_device;
  614. teedev->dev.parent = dev;
  615. teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
  616. rc = dev_set_name(&teedev->dev, "%s", teedev->name);
  617. if (rc) {
  618. ret = ERR_PTR(rc);
  619. goto err_devt;
  620. }
  621. cdev_init(&teedev->cdev, &tee_fops);
  622. teedev->cdev.owner = teedesc->owner;
  623. teedev->cdev.kobj.parent = &teedev->dev.kobj;
  624. dev_set_drvdata(&teedev->dev, driver_data);
  625. device_initialize(&teedev->dev);
  626. /* 1 as tee_device_unregister() does one final tee_device_put() */
  627. teedev->num_users = 1;
  628. init_completion(&teedev->c_no_users);
  629. mutex_init(&teedev->mutex);
  630. idr_init(&teedev->idr);
  631. teedev->desc = teedesc;
  632. teedev->pool = pool;
  633. return teedev;
  634. err_devt:
  635. unregister_chrdev_region(teedev->dev.devt, 1);
  636. err:
  637. pr_err("could not register %s driver\n",
  638. teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
  639. if (teedev && teedev->id < TEE_NUM_DEVICES) {
  640. spin_lock(&driver_lock);
  641. clear_bit(teedev->id, dev_mask);
  642. spin_unlock(&driver_lock);
  643. }
  644. kfree(teedev);
  645. return ret;
  646. }
  647. EXPORT_SYMBOL_GPL(tee_device_alloc);
  648. static ssize_t implementation_id_show(struct device *dev,
  649. struct device_attribute *attr, char *buf)
  650. {
  651. struct tee_device *teedev = container_of(dev, struct tee_device, dev);
  652. struct tee_ioctl_version_data vers;
  653. teedev->desc->ops->get_version(teedev, &vers);
  654. return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
  655. }
  656. static DEVICE_ATTR_RO(implementation_id);
  657. static struct attribute *tee_dev_attrs[] = {
  658. &dev_attr_implementation_id.attr,
  659. NULL
  660. };
  661. static const struct attribute_group tee_dev_group = {
  662. .attrs = tee_dev_attrs,
  663. };
  664. /**
  665. * tee_device_register() - Registers a TEE device
  666. * @teedev: Device to register
  667. *
  668. * tee_device_unregister() need to be called to remove the @teedev if
  669. * this function fails.
  670. *
  671. * @returns < 0 on failure
  672. */
  673. int tee_device_register(struct tee_device *teedev)
  674. {
  675. int rc;
  676. if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
  677. dev_err(&teedev->dev, "attempt to register twice\n");
  678. return -EINVAL;
  679. }
  680. rc = cdev_add(&teedev->cdev, teedev->dev.devt, 1);
  681. if (rc) {
  682. dev_err(&teedev->dev,
  683. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  684. teedev->name, MAJOR(teedev->dev.devt),
  685. MINOR(teedev->dev.devt), rc);
  686. return rc;
  687. }
  688. rc = device_add(&teedev->dev);
  689. if (rc) {
  690. dev_err(&teedev->dev,
  691. "unable to device_add() %s, major %d, minor %d, err=%d\n",
  692. teedev->name, MAJOR(teedev->dev.devt),
  693. MINOR(teedev->dev.devt), rc);
  694. goto err_device_add;
  695. }
  696. rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group);
  697. if (rc) {
  698. dev_err(&teedev->dev,
  699. "failed to create sysfs attributes, err=%d\n", rc);
  700. goto err_sysfs_create_group;
  701. }
  702. teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
  703. return 0;
  704. err_sysfs_create_group:
  705. device_del(&teedev->dev);
  706. err_device_add:
  707. cdev_del(&teedev->cdev);
  708. return rc;
  709. }
  710. EXPORT_SYMBOL_GPL(tee_device_register);
  711. void tee_device_put(struct tee_device *teedev)
  712. {
  713. mutex_lock(&teedev->mutex);
  714. /* Shouldn't put in this state */
  715. if (!WARN_ON(!teedev->desc)) {
  716. teedev->num_users--;
  717. if (!teedev->num_users) {
  718. teedev->desc = NULL;
  719. complete(&teedev->c_no_users);
  720. }
  721. }
  722. mutex_unlock(&teedev->mutex);
  723. }
  724. bool tee_device_get(struct tee_device *teedev)
  725. {
  726. mutex_lock(&teedev->mutex);
  727. if (!teedev->desc) {
  728. mutex_unlock(&teedev->mutex);
  729. return false;
  730. }
  731. teedev->num_users++;
  732. mutex_unlock(&teedev->mutex);
  733. return true;
  734. }
  735. /**
  736. * tee_device_unregister() - Removes a TEE device
  737. * @teedev: Device to unregister
  738. *
  739. * This function should be called to remove the @teedev even if
  740. * tee_device_register() hasn't been called yet. Does nothing if
  741. * @teedev is NULL.
  742. */
  743. void tee_device_unregister(struct tee_device *teedev)
  744. {
  745. if (!teedev)
  746. return;
  747. if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
  748. sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group);
  749. cdev_del(&teedev->cdev);
  750. device_del(&teedev->dev);
  751. }
  752. tee_device_put(teedev);
  753. wait_for_completion(&teedev->c_no_users);
  754. /*
  755. * No need to take a mutex any longer now since teedev->desc was
  756. * set to NULL before teedev->c_no_users was completed.
  757. */
  758. teedev->pool = NULL;
  759. put_device(&teedev->dev);
  760. }
  761. EXPORT_SYMBOL_GPL(tee_device_unregister);
  762. /**
  763. * tee_get_drvdata() - Return driver_data pointer
  764. * @teedev: Device containing the driver_data pointer
  765. * @returns the driver_data pointer supplied to tee_register().
  766. */
  767. void *tee_get_drvdata(struct tee_device *teedev)
  768. {
  769. return dev_get_drvdata(&teedev->dev);
  770. }
  771. EXPORT_SYMBOL_GPL(tee_get_drvdata);
  772. static int __init tee_init(void)
  773. {
  774. int rc;
  775. tee_class = class_create(THIS_MODULE, "tee");
  776. if (IS_ERR(tee_class)) {
  777. pr_err("couldn't create class\n");
  778. return PTR_ERR(tee_class);
  779. }
  780. rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
  781. if (rc) {
  782. pr_err("failed to allocate char dev region\n");
  783. class_destroy(tee_class);
  784. tee_class = NULL;
  785. }
  786. return rc;
  787. }
  788. static void __exit tee_exit(void)
  789. {
  790. class_destroy(tee_class);
  791. tee_class = NULL;
  792. unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
  793. }
  794. subsys_initcall(tee_init);
  795. module_exit(tee_exit);
  796. MODULE_AUTHOR("Linaro");
  797. MODULE_DESCRIPTION("TEE Driver");
  798. MODULE_VERSION("1.0");
  799. MODULE_LICENSE("GPL v2");