smcinvoke_kernel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/file.h>
  7. #include <linux/fs.h>
  8. #include <linux/fdtable.h>
  9. #include <linux/anon_inodes.h>
  10. #include <linux/kref.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/firmware.h>
  14. #include <linux/elf.h>
  15. #include "smcinvoke.h"
  16. #include "smcinvoke_object.h"
  17. #include "IClientEnv.h"
  18. #if !IS_ENABLED(CONFIG_QSEECOM)
  19. #include "IQSEEComCompat.h"
  20. #include "IQSEEComCompatAppLoader.h"
  21. #include "linux/qseecom.h"
  22. #if IS_ENABLED(CONFIG_QSEECOM_PROXY)
  23. #include <linux/qseecom_kernel.h>
  24. #else
  25. #include "misc/qseecom_kernel.h"
  26. #endif
  27. #endif
  28. const uint32_t CQSEEComCompatAppLoader_UID = 122;
  29. struct qseecom_compat_context {
  30. void *dev; /* in/out */
  31. unsigned char *sbuf; /* in/out */
  32. uint32_t sbuf_len; /* in/out */
  33. struct qtee_shm shm;
  34. uint8_t app_arch;
  35. struct Object client_env;
  36. struct Object app_loader;
  37. struct Object app_controller;
  38. };
  39. struct tzobject_context {
  40. int fd;
  41. struct kref refs;
  42. };
  43. static int invoke_over_smcinvoke(void *cxt,
  44. uint32_t op,
  45. union ObjectArg *args,
  46. uint32_t counts);
  47. static struct Object tzobject_new(int fd)
  48. {
  49. struct tzobject_context *me =
  50. kzalloc(sizeof(struct tzobject_context), GFP_KERNEL);
  51. if (!me)
  52. return Object_NULL;
  53. kref_init(&me->refs);
  54. me->fd = fd;
  55. pr_debug("%s: me->fd = %d, me->refs = %u\n", __func__,
  56. me->fd, kref_read(&me->refs));
  57. return (struct Object) { invoke_over_smcinvoke, me };
  58. }
  59. static void tzobject_delete(struct kref *refs)
  60. {
  61. struct tzobject_context *me = container_of(refs,
  62. struct tzobject_context, refs);
  63. pr_info("%s: me->fd = %d, me->refs = %d, files = %p\n",
  64. __func__, me->fd, kref_read(&me->refs), current->files);
  65. /*
  66. * after _close_fd(), ref_cnt will be 0,
  67. * but smcinvoke_release() was still not called,
  68. * so we first call smcinvoke_release_from_kernel_client() to
  69. * free filp and ask TZ to release object, then call _close_fd()
  70. */
  71. smcinvoke_release_from_kernel_client(me->fd);
  72. close_fd(me->fd);
  73. kfree(me);
  74. }
  75. int getObjectFromHandle(int handle, struct Object *obj)
  76. {
  77. int ret = 0;
  78. if (handle == SMCINVOKE_USERSPACE_OBJ_NULL) {
  79. /* NULL object*/
  80. Object_ASSIGN_NULL(*obj);
  81. } else if (handle > SMCINVOKE_USERSPACE_OBJ_NULL) {
  82. *obj = tzobject_new(handle);
  83. if (Object_isNull(*obj))
  84. ret = OBJECT_ERROR_BADOBJ;
  85. } else {
  86. pr_err("CBobj not supported for handle %d\n", handle);
  87. ret = OBJECT_ERROR_BADOBJ;
  88. }
  89. return ret;
  90. }
  91. int getHandleFromObject(struct Object obj, int *handle)
  92. {
  93. int ret = 0;
  94. if (Object_isNull(obj)) {
  95. /* set NULL Object's fd to be -1 */
  96. *handle = SMCINVOKE_USERSPACE_OBJ_NULL;
  97. return ret;
  98. }
  99. if (obj.invoke == invoke_over_smcinvoke) {
  100. struct tzobject_context *ctx = (struct tzobject_context *)(obj.context);
  101. if (ctx != NULL) {
  102. *handle = ctx->fd;
  103. } else {
  104. pr_err("Failed to get tzobject_context obj handle, ret = %d\n", ret);
  105. ret = OBJECT_ERROR_BADOBJ;
  106. }
  107. } else {
  108. pr_err("CBobj not supported\n");
  109. ret = OBJECT_ERROR_BADOBJ;
  110. }
  111. return ret;
  112. }
  113. static int marshalIn(struct smcinvoke_cmd_req *req,
  114. union smcinvoke_arg *argptr,
  115. uint32_t op, union ObjectArg *args,
  116. uint32_t counts)
  117. {
  118. size_t i = 0;
  119. req->op = op;
  120. req->counts = counts;
  121. req->argsize = sizeof(union smcinvoke_arg);
  122. req->args = (uintptr_t)argptr;
  123. FOR_ARGS(i, counts, buffers) {
  124. argptr[i].b.addr = (uintptr_t) args[i].b.ptr;
  125. argptr[i].b.size = args[i].b.size;
  126. }
  127. FOR_ARGS(i, counts, OI) {
  128. int handle = -1, ret;
  129. ret = getHandleFromObject(args[i].o, &handle);
  130. if (ret) {
  131. pr_err("invalid OI[%zu]\n", i);
  132. return OBJECT_ERROR_BADOBJ;
  133. }
  134. argptr[i].o.fd = handle;
  135. }
  136. FOR_ARGS(i, counts, OO) {
  137. argptr[i].o.fd = SMCINVOKE_USERSPACE_OBJ_NULL;
  138. }
  139. return OBJECT_OK;
  140. }
  141. static int marshalOut(struct smcinvoke_cmd_req *req,
  142. union smcinvoke_arg *argptr,
  143. union ObjectArg *args, uint32_t counts,
  144. struct tzobject_context *me)
  145. {
  146. int ret = req->result;
  147. bool failed = false;
  148. size_t i = 0;
  149. argptr = (union smcinvoke_arg *)(uintptr_t)(req->args);
  150. FOR_ARGS(i, counts, BO) {
  151. args[i].b.size = argptr[i].b.size;
  152. }
  153. FOR_ARGS(i, counts, OO) {
  154. ret = getObjectFromHandle(argptr[i].o.fd, &(args[i].o));
  155. if (ret) {
  156. pr_err("Failed to get OO[%zu] from handle = %d\n",
  157. i, (int)argptr[i].o.fd);
  158. failed = true;
  159. break;
  160. }
  161. pr_debug("Succeed to create OO for args[%zu].o, fd = %d\n",
  162. i, (int)argptr[i].o.fd);
  163. }
  164. if (failed) {
  165. FOR_ARGS(i, counts, OO) {
  166. Object_ASSIGN_NULL(args[i].o);
  167. }
  168. /* Only overwrite ret value if invoke result is 0 */
  169. if (ret == 0)
  170. ret = OBJECT_ERROR_BADOBJ;
  171. }
  172. return ret;
  173. }
  174. static int invoke_over_smcinvoke(void *cxt,
  175. uint32_t op,
  176. union ObjectArg *args,
  177. uint32_t counts)
  178. {
  179. int ret = OBJECT_OK;
  180. struct smcinvoke_cmd_req req = {0, 0, 0, 0, 0};
  181. size_t i = 0;
  182. struct tzobject_context *me = NULL;
  183. uint32_t method;
  184. union smcinvoke_arg *argptr = NULL;
  185. FOR_ARGS(i, counts, OO) {
  186. args[i].o = Object_NULL;
  187. }
  188. me = (struct tzobject_context *)cxt;
  189. method = ObjectOp_methodID(op);
  190. pr_debug("%s: cxt = %p, fd = %d, op = %u, cnt = %x, refs = %u\n",
  191. __func__, me, me->fd, op, counts, kref_read(&me->refs));
  192. if (ObjectOp_isLocal(op)) {
  193. switch (method) {
  194. case Object_OP_retain:
  195. kref_get(&me->refs);
  196. return OBJECT_OK;
  197. case Object_OP_release:
  198. kref_put(&me->refs, tzobject_delete);
  199. return OBJECT_OK;
  200. }
  201. return OBJECT_ERROR_REMOTE;
  202. }
  203. argptr = kcalloc(OBJECT_COUNTS_TOTAL(counts),
  204. sizeof(union smcinvoke_arg), GFP_KERNEL);
  205. if (argptr == NULL)
  206. return OBJECT_ERROR_KMEM;
  207. ret = marshalIn(&req, argptr, op, args, counts);
  208. if (ret)
  209. goto exit;
  210. ret = process_invoke_request_from_kernel_client(me->fd, &req);
  211. if (ret) {
  212. pr_err("INVOKE failed with ret = %d, result = %d\n"
  213. "obj.context = %p, fd = %d, op = %d, counts = 0x%x\n",
  214. ret, req.result, me, me->fd, op, counts);
  215. FOR_ARGS(i, counts, OO) {
  216. struct smcinvoke_obj obj = argptr[i].o;
  217. if (obj.fd >= 0) {
  218. pr_err("Close OO[%zu].fd = %d\n", i, obj.fd);
  219. close_fd(obj.fd);
  220. }
  221. }
  222. if (ret == -EBUSY) {
  223. ret = OBJECT_ERROR_BUSY;
  224. }
  225. else if (ret == -ENOMEM){
  226. ret = OBJECT_ERROR_KMEM;
  227. } else {
  228. ret = OBJECT_ERROR_UNAVAIL;
  229. }
  230. goto exit;
  231. }
  232. if (!req.result)
  233. ret = marshalOut(&req, argptr, args, counts, me);
  234. exit:
  235. kfree(argptr);
  236. return ret | req.result;
  237. }
  238. static int get_root_obj(struct Object *rootObj)
  239. {
  240. int ret = 0;
  241. int root_fd = -1;
  242. ret = get_root_fd(&root_fd);
  243. if (ret) {
  244. pr_err("Failed to get root fd, ret = %d\n", ret);
  245. return ret;
  246. }
  247. *rootObj = tzobject_new(root_fd);
  248. if (Object_isNull(*rootObj)) {
  249. close_fd(root_fd);
  250. ret = -ENOMEM;
  251. }
  252. return ret;
  253. }
  254. /*
  255. * Get a client environment using a NULL credentials Object
  256. */
  257. int32_t get_client_env_object(struct Object *clientEnvObj)
  258. {
  259. int32_t ret = OBJECT_ERROR;
  260. struct Object rootObj = Object_NULL;
  261. /* get rootObj */
  262. ret = get_root_obj(&rootObj);
  263. if (ret) {
  264. pr_err("Failed to create rootobj\n");
  265. return ret;
  266. }
  267. /* get client env */
  268. ret = IClientEnv_registerWithCredentials(rootObj,
  269. Object_NULL, clientEnvObj);
  270. if (ret)
  271. pr_err("Failed to get ClientEnvObject, ret = %d\n", ret);
  272. Object_release(rootObj);
  273. return ret;
  274. }
  275. EXPORT_SYMBOL(get_client_env_object);
  276. #if !IS_ENABLED(CONFIG_QSEECOM)
  277. static int load_app(struct qseecom_compat_context *cxt, const char *app_name)
  278. {
  279. size_t fw_size = 0;
  280. u8 *imgbuf_va = NULL;
  281. int ret = 0;
  282. char dist_name[MAX_APP_NAME_SIZE] = {0};
  283. size_t dist_name_len = 0;
  284. struct qtee_shm shm = {0};
  285. if (strnlen(app_name, MAX_APP_NAME_SIZE) == MAX_APP_NAME_SIZE) {
  286. pr_err("The app_name (%s) with length %zu is not valid\n",
  287. app_name, strnlen(app_name, MAX_APP_NAME_SIZE));
  288. return -EINVAL;
  289. }
  290. ret = IQSEEComCompatAppLoader_lookupTA(cxt->app_loader,
  291. app_name, strlen(app_name), &cxt->app_controller);
  292. if (!ret) {
  293. pr_info("app %s exists\n", app_name);
  294. return ret;
  295. }
  296. imgbuf_va = firmware_request_from_smcinvoke(app_name, &fw_size, &shm);
  297. if (imgbuf_va == NULL) {
  298. pr_err("Failed on firmware_request_from_smcinvoke\n");
  299. return -EINVAL;
  300. }
  301. ret = IQSEEComCompatAppLoader_loadFromBuffer(
  302. cxt->app_loader, imgbuf_va, fw_size,
  303. app_name, strlen(app_name),
  304. dist_name, MAX_APP_NAME_SIZE, &dist_name_len,
  305. &cxt->app_controller);
  306. if (ret) {
  307. pr_err("loadFromBuffer failed for app %s, ret = %d\n",
  308. app_name, ret);
  309. goto exit_release_shm;
  310. }
  311. cxt->app_arch = *(uint8_t *)(imgbuf_va + EI_CLASS);
  312. pr_info("%s %d, loaded app %s, dist_name %s, dist_name_len %zu\n",
  313. __func__, __LINE__, app_name, dist_name, dist_name_len);
  314. exit_release_shm:
  315. qtee_shmbridge_free_shm(&shm);
  316. return ret;
  317. }
  318. static int __qseecom_start_app(struct qseecom_handle **handle,
  319. char *app_name, uint32_t size)
  320. {
  321. int ret = 0;
  322. struct qseecom_compat_context *cxt = NULL;
  323. pr_warn("%s, start app %s, size %zu\n",
  324. __func__, app_name, size);
  325. if (app_name == NULL || handle == NULL) {
  326. pr_err("app_name is null or invalid handle\n");
  327. return -EINVAL;
  328. }
  329. /* allocate qseecom_compat_context */
  330. cxt = kzalloc(sizeof(struct qseecom_compat_context), GFP_KERNEL);
  331. if (!cxt)
  332. return -ENOMEM;
  333. /* get client env */
  334. ret = get_client_env_object(&cxt->client_env);
  335. if (ret) {
  336. pr_err("failed to get clientEnv when loading app %s, ret %d\n",
  337. app_name, ret);
  338. ret = -EINVAL;
  339. goto exit_free_cxt;
  340. }
  341. /* get apploader with CQSEEComCompatAppLoader_UID */
  342. ret = IClientEnv_open(cxt->client_env, CQSEEComCompatAppLoader_UID,
  343. &cxt->app_loader);
  344. if (ret) {
  345. pr_err("failed to get apploader when loading app %s, ret %d\n",
  346. app_name, ret);
  347. ret = -EINVAL;
  348. goto exit_release_clientenv;
  349. }
  350. /* load app*/
  351. ret = load_app(cxt, app_name);
  352. if (ret) {
  353. pr_err("failed to load app %s, ret = %d\n",
  354. app_name, ret);
  355. ret = -EINVAL;
  356. goto exit_release_apploader;
  357. }
  358. /* Get the physical address of the req/resp buffer */
  359. ret = qtee_shmbridge_allocate_shm(size, &cxt->shm);
  360. if (ret) {
  361. pr_err("qtee_shmbridge_allocate_shm failed, ret :%d\n", ret);
  362. ret = -EINVAL;
  363. goto exit_release_appcontroller;
  364. }
  365. cxt->sbuf = cxt->shm.vaddr;
  366. cxt->sbuf_len = size;
  367. *handle = (struct qseecom_handle *)cxt;
  368. return ret;
  369. exit_release_appcontroller:
  370. Object_release(cxt->app_controller);
  371. exit_release_apploader:
  372. Object_release(cxt->app_loader);
  373. exit_release_clientenv:
  374. Object_release(cxt->client_env);
  375. exit_free_cxt:
  376. kfree(cxt);
  377. return ret;
  378. }
  379. static int __qseecom_shutdown_app(struct qseecom_handle **handle)
  380. {
  381. struct qseecom_compat_context *cxt =
  382. (struct qseecom_compat_context *)(*handle);
  383. if ((handle == NULL) || (*handle == NULL)) {
  384. pr_err("Handle is NULL\n");
  385. return -EINVAL;
  386. }
  387. qtee_shmbridge_free_shm(&cxt->shm);
  388. Object_release(cxt->app_controller);
  389. Object_release(cxt->app_loader);
  390. Object_release(cxt->client_env);
  391. kfree(cxt);
  392. *handle = NULL;
  393. return 0;
  394. }
  395. static int __qseecom_send_command(struct qseecom_handle *handle, void *send_buf,
  396. uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len)
  397. {
  398. struct qseecom_compat_context *cxt =
  399. (struct qseecom_compat_context *)handle;
  400. size_t out_len = 0;
  401. pr_debug("%s, sbuf_len %u, rbuf_len %u\n",
  402. __func__, sbuf_len, rbuf_len);
  403. if (!handle || !send_buf || !resp_buf || !sbuf_len || !rbuf_len) {
  404. pr_err("One of params is invalid. %s, handle %x, send_buf %x,resp_buf %x,sbuf_len %u, rbuf_len %u\n",
  405. __func__, handle, send_buf, resp_buf, sbuf_len, rbuf_len);
  406. return -EINVAL;
  407. }
  408. return IQSEEComCompat_sendRequest(cxt->app_controller,
  409. send_buf, sbuf_len,
  410. resp_buf, rbuf_len,
  411. send_buf, sbuf_len, &out_len,
  412. resp_buf, rbuf_len, &out_len,
  413. NULL, 0, /* embedded offset array */
  414. (cxt->app_arch == ELFCLASS64),
  415. Object_NULL, Object_NULL,
  416. Object_NULL, Object_NULL);
  417. }
  418. #if IS_ENABLED(CONFIG_QSEECOM_PROXY)
  419. const static struct qseecom_drv_ops qseecom_driver_ops = {
  420. .qseecom_send_command = __qseecom_send_command,
  421. .qseecom_start_app = __qseecom_start_app,
  422. .qseecom_shutdown_app = __qseecom_shutdown_app,
  423. };
  424. int get_qseecom_kernel_fun_ops(void)
  425. {
  426. return provide_qseecom_kernel_fun_ops(&qseecom_driver_ops);
  427. }
  428. #else
  429. int qseecom_start_app(struct qseecom_handle **handle,
  430. char *app_name, uint32_t size)
  431. {
  432. return __qseecom_start_app(handle, app_name, size);
  433. }
  434. EXPORT_SYMBOL(qseecom_start_app);
  435. int qseecom_shutdown_app(struct qseecom_handle **handle)
  436. {
  437. return __qseecom_shutdown_app(handle);
  438. }
  439. EXPORT_SYMBOL(qseecom_shutdown_app);
  440. int qseecom_send_command(struct qseecom_handle *handle, void *send_buf,
  441. uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len)
  442. {
  443. return __qseecom_send_command(handle, send_buf, sbuf_len,
  444. resp_buf, rbuf_len);
  445. }
  446. EXPORT_SYMBOL(qseecom_send_command);
  447. #endif
  448. #endif