call.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, 2023 Linaro Limited
  4. */
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/errno.h>
  8. #include <linux/mm.h>
  9. #include <linux/slab.h>
  10. #include <linux/tee_core.h>
  11. #include <linux/types.h>
  12. #include "optee_private.h"
  13. #define MAX_ARG_PARAM_COUNT 6
  14. /*
  15. * How much memory we allocate for each entry. This doesn't have to be a
  16. * single page, but it makes sense to keep at least keep it as multiples of
  17. * the page size.
  18. */
  19. #define SHM_ENTRY_SIZE PAGE_SIZE
  20. /*
  21. * We need to have a compile time constant to be able to determine the
  22. * maximum needed size of the bit field.
  23. */
  24. #define MIN_ARG_SIZE OPTEE_MSG_GET_ARG_SIZE(MAX_ARG_PARAM_COUNT)
  25. #define MAX_ARG_COUNT_PER_ENTRY (SHM_ENTRY_SIZE / MIN_ARG_SIZE)
  26. /*
  27. * Shared memory for argument structs are cached here. The number of
  28. * arguments structs that can fit is determined at runtime depending on the
  29. * needed RPC parameter count reported by secure world
  30. * (optee->rpc_param_count).
  31. */
  32. struct optee_shm_arg_entry {
  33. struct list_head list_node;
  34. struct tee_shm *shm;
  35. DECLARE_BITMAP(map, MAX_ARG_COUNT_PER_ENTRY);
  36. };
  37. void optee_cq_init(struct optee_call_queue *cq, int thread_count)
  38. {
  39. mutex_init(&cq->mutex);
  40. INIT_LIST_HEAD(&cq->waiters);
  41. /*
  42. * If cq->total_thread_count is 0 then we're not trying to keep
  43. * track of how many free threads we have, instead we're relying on
  44. * the secure world to tell us when we're out of thread and have to
  45. * wait for another thread to become available.
  46. */
  47. cq->total_thread_count = thread_count;
  48. cq->free_thread_count = thread_count;
  49. }
  50. void optee_cq_wait_init(struct optee_call_queue *cq,
  51. struct optee_call_waiter *w, bool sys_thread)
  52. {
  53. unsigned int free_thread_threshold;
  54. bool need_wait = false;
  55. memset(w, 0, sizeof(*w));
  56. /*
  57. * We're preparing to make a call to secure world. In case we can't
  58. * allocate a thread in secure world we'll end up waiting in
  59. * optee_cq_wait_for_completion().
  60. *
  61. * Normally if there's no contention in secure world the call will
  62. * complete and we can cleanup directly with optee_cq_wait_final().
  63. */
  64. mutex_lock(&cq->mutex);
  65. /*
  66. * We add ourselves to the queue, but we don't wait. This
  67. * guarantees that we don't lose a completion if secure world
  68. * returns busy and another thread just exited and try to complete
  69. * someone.
  70. */
  71. init_completion(&w->c);
  72. list_add_tail(&w->list_node, &cq->waiters);
  73. w->sys_thread = sys_thread;
  74. if (cq->total_thread_count) {
  75. if (sys_thread || !cq->sys_thread_req_count)
  76. free_thread_threshold = 0;
  77. else
  78. free_thread_threshold = 1;
  79. if (cq->free_thread_count > free_thread_threshold)
  80. cq->free_thread_count--;
  81. else
  82. need_wait = true;
  83. }
  84. mutex_unlock(&cq->mutex);
  85. while (need_wait) {
  86. optee_cq_wait_for_completion(cq, w);
  87. mutex_lock(&cq->mutex);
  88. if (sys_thread || !cq->sys_thread_req_count)
  89. free_thread_threshold = 0;
  90. else
  91. free_thread_threshold = 1;
  92. if (cq->free_thread_count > free_thread_threshold) {
  93. cq->free_thread_count--;
  94. need_wait = false;
  95. }
  96. mutex_unlock(&cq->mutex);
  97. }
  98. }
  99. void optee_cq_wait_for_completion(struct optee_call_queue *cq,
  100. struct optee_call_waiter *w)
  101. {
  102. wait_for_completion(&w->c);
  103. mutex_lock(&cq->mutex);
  104. /* Move to end of list to get out of the way for other waiters */
  105. list_del(&w->list_node);
  106. reinit_completion(&w->c);
  107. list_add_tail(&w->list_node, &cq->waiters);
  108. mutex_unlock(&cq->mutex);
  109. }
  110. static void optee_cq_complete_one(struct optee_call_queue *cq)
  111. {
  112. struct optee_call_waiter *w;
  113. /* Wake a waiting system session if any, prior to a normal session */
  114. list_for_each_entry(w, &cq->waiters, list_node) {
  115. if (w->sys_thread && !completion_done(&w->c)) {
  116. complete(&w->c);
  117. return;
  118. }
  119. }
  120. list_for_each_entry(w, &cq->waiters, list_node) {
  121. if (!completion_done(&w->c)) {
  122. complete(&w->c);
  123. break;
  124. }
  125. }
  126. }
  127. void optee_cq_wait_final(struct optee_call_queue *cq,
  128. struct optee_call_waiter *w)
  129. {
  130. /*
  131. * We're done with the call to secure world. The thread in secure
  132. * world that was used for this call is now available for some
  133. * other task to use.
  134. */
  135. mutex_lock(&cq->mutex);
  136. /* Get out of the list */
  137. list_del(&w->list_node);
  138. cq->free_thread_count++;
  139. /* Wake up one eventual waiting task */
  140. optee_cq_complete_one(cq);
  141. /*
  142. * If we're completed we've got a completion from another task that
  143. * was just done with its call to secure world. Since yet another
  144. * thread now is available in secure world wake up another eventual
  145. * waiting task.
  146. */
  147. if (completion_done(&w->c))
  148. optee_cq_complete_one(cq);
  149. mutex_unlock(&cq->mutex);
  150. }
  151. /* Count registered system sessions to reserved a system thread or not */
  152. static bool optee_cq_incr_sys_thread_count(struct optee_call_queue *cq)
  153. {
  154. if (cq->total_thread_count <= 1)
  155. return false;
  156. mutex_lock(&cq->mutex);
  157. cq->sys_thread_req_count++;
  158. mutex_unlock(&cq->mutex);
  159. return true;
  160. }
  161. static void optee_cq_decr_sys_thread_count(struct optee_call_queue *cq)
  162. {
  163. mutex_lock(&cq->mutex);
  164. cq->sys_thread_req_count--;
  165. /* If there's someone waiting, let it resume */
  166. optee_cq_complete_one(cq);
  167. mutex_unlock(&cq->mutex);
  168. }
  169. /* Requires the filpstate mutex to be held */
  170. static struct optee_session *find_session(struct optee_context_data *ctxdata,
  171. u32 session_id)
  172. {
  173. struct optee_session *sess;
  174. list_for_each_entry(sess, &ctxdata->sess_list, list_node)
  175. if (sess->session_id == session_id)
  176. return sess;
  177. return NULL;
  178. }
  179. void optee_shm_arg_cache_init(struct optee *optee, u32 flags)
  180. {
  181. INIT_LIST_HEAD(&optee->shm_arg_cache.shm_args);
  182. mutex_init(&optee->shm_arg_cache.mutex);
  183. optee->shm_arg_cache.flags = flags;
  184. }
  185. void optee_shm_arg_cache_uninit(struct optee *optee)
  186. {
  187. struct list_head *head = &optee->shm_arg_cache.shm_args;
  188. struct optee_shm_arg_entry *entry;
  189. mutex_destroy(&optee->shm_arg_cache.mutex);
  190. while (!list_empty(head)) {
  191. entry = list_first_entry(head, struct optee_shm_arg_entry,
  192. list_node);
  193. list_del(&entry->list_node);
  194. if (find_first_bit(entry->map, MAX_ARG_COUNT_PER_ENTRY) !=
  195. MAX_ARG_COUNT_PER_ENTRY) {
  196. pr_err("Freeing non-free entry\n");
  197. }
  198. tee_shm_free(entry->shm);
  199. kfree(entry);
  200. }
  201. }
  202. size_t optee_msg_arg_size(size_t rpc_param_count)
  203. {
  204. size_t sz = OPTEE_MSG_GET_ARG_SIZE(MAX_ARG_PARAM_COUNT);
  205. if (rpc_param_count)
  206. sz += OPTEE_MSG_GET_ARG_SIZE(rpc_param_count);
  207. return sz;
  208. }
  209. /**
  210. * optee_get_msg_arg() - Provide shared memory for argument struct
  211. * @ctx: Caller TEE context
  212. * @num_params: Number of parameter to store
  213. * @entry_ret: Entry pointer, needed when freeing the buffer
  214. * @shm_ret: Shared memory buffer
  215. * @offs_ret: Offset of argument strut in shared memory buffer
  216. *
  217. * @returns a pointer to the argument struct in memory, else an ERR_PTR
  218. */
  219. struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx,
  220. size_t num_params,
  221. struct optee_shm_arg_entry **entry_ret,
  222. struct tee_shm **shm_ret,
  223. u_int *offs_ret)
  224. {
  225. struct optee *optee = tee_get_drvdata(ctx->teedev);
  226. size_t sz = optee_msg_arg_size(optee->rpc_param_count);
  227. struct optee_shm_arg_entry *entry;
  228. struct optee_msg_arg *ma;
  229. size_t args_per_entry;
  230. u_long bit;
  231. u_int offs;
  232. void *res;
  233. if (num_params > MAX_ARG_PARAM_COUNT)
  234. return ERR_PTR(-EINVAL);
  235. if (optee->shm_arg_cache.flags & OPTEE_SHM_ARG_SHARED)
  236. args_per_entry = SHM_ENTRY_SIZE / sz;
  237. else
  238. args_per_entry = 1;
  239. mutex_lock(&optee->shm_arg_cache.mutex);
  240. list_for_each_entry(entry, &optee->shm_arg_cache.shm_args, list_node) {
  241. bit = find_first_zero_bit(entry->map, MAX_ARG_COUNT_PER_ENTRY);
  242. if (bit < args_per_entry)
  243. goto have_entry;
  244. }
  245. /*
  246. * No entry was found, let's allocate a new.
  247. */
  248. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  249. if (!entry) {
  250. res = ERR_PTR(-ENOMEM);
  251. goto out;
  252. }
  253. if (optee->shm_arg_cache.flags & OPTEE_SHM_ARG_ALLOC_PRIV)
  254. res = tee_shm_alloc_priv_buf(ctx, SHM_ENTRY_SIZE);
  255. else
  256. res = tee_shm_alloc_kernel_buf(ctx, SHM_ENTRY_SIZE);
  257. if (IS_ERR(res)) {
  258. kfree(entry);
  259. goto out;
  260. }
  261. entry->shm = res;
  262. list_add(&entry->list_node, &optee->shm_arg_cache.shm_args);
  263. bit = 0;
  264. have_entry:
  265. offs = bit * sz;
  266. res = tee_shm_get_va(entry->shm, offs);
  267. if (IS_ERR(res))
  268. goto out;
  269. ma = res;
  270. set_bit(bit, entry->map);
  271. memset(ma, 0, sz);
  272. ma->num_params = num_params;
  273. *entry_ret = entry;
  274. *shm_ret = entry->shm;
  275. *offs_ret = offs;
  276. out:
  277. mutex_unlock(&optee->shm_arg_cache.mutex);
  278. return res;
  279. }
  280. /**
  281. * optee_free_msg_arg() - Free previsouly obtained shared memory
  282. * @ctx: Caller TEE context
  283. * @entry: Pointer returned when the shared memory was obtained
  284. * @offs: Offset of shared memory buffer to free
  285. *
  286. * This function frees the shared memory obtained with optee_get_msg_arg().
  287. */
  288. void optee_free_msg_arg(struct tee_context *ctx,
  289. struct optee_shm_arg_entry *entry, u_int offs)
  290. {
  291. struct optee *optee = tee_get_drvdata(ctx->teedev);
  292. size_t sz = optee_msg_arg_size(optee->rpc_param_count);
  293. u_long bit;
  294. if (offs > SHM_ENTRY_SIZE || offs % sz) {
  295. pr_err("Invalid offs %u\n", offs);
  296. return;
  297. }
  298. bit = offs / sz;
  299. mutex_lock(&optee->shm_arg_cache.mutex);
  300. if (!test_bit(bit, entry->map))
  301. pr_err("Bit pos %lu is already free\n", bit);
  302. clear_bit(bit, entry->map);
  303. mutex_unlock(&optee->shm_arg_cache.mutex);
  304. }
  305. int optee_open_session(struct tee_context *ctx,
  306. struct tee_ioctl_open_session_arg *arg,
  307. struct tee_param *param)
  308. {
  309. struct optee *optee = tee_get_drvdata(ctx->teedev);
  310. struct optee_context_data *ctxdata = ctx->data;
  311. struct optee_shm_arg_entry *entry;
  312. struct tee_shm *shm;
  313. struct optee_msg_arg *msg_arg;
  314. struct optee_session *sess = NULL;
  315. uuid_t client_uuid;
  316. u_int offs;
  317. int rc;
  318. /* +2 for the meta parameters added below */
  319. msg_arg = optee_get_msg_arg(ctx, arg->num_params + 2,
  320. &entry, &shm, &offs);
  321. if (IS_ERR(msg_arg))
  322. return PTR_ERR(msg_arg);
  323. msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
  324. msg_arg->cancel_id = arg->cancel_id;
  325. /*
  326. * Initialize and add the meta parameters needed when opening a
  327. * session.
  328. */
  329. msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
  330. OPTEE_MSG_ATTR_META;
  331. msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
  332. OPTEE_MSG_ATTR_META;
  333. memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
  334. msg_arg->params[1].u.value.c = arg->clnt_login;
  335. rc = tee_session_calc_client_uuid(&client_uuid, arg->clnt_login,
  336. arg->clnt_uuid);
  337. if (rc)
  338. goto out;
  339. export_uuid(msg_arg->params[1].u.octets, &client_uuid);
  340. rc = optee->ops->to_msg_param(optee, msg_arg->params + 2,
  341. arg->num_params, param);
  342. if (rc)
  343. goto out;
  344. sess = kzalloc(sizeof(*sess), GFP_KERNEL);
  345. if (!sess) {
  346. rc = -ENOMEM;
  347. goto out;
  348. }
  349. if (optee->ops->do_call_with_arg(ctx, shm, offs,
  350. sess->use_sys_thread)) {
  351. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  352. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  353. }
  354. if (msg_arg->ret == TEEC_SUCCESS) {
  355. /* A new session has been created, add it to the list. */
  356. sess->session_id = msg_arg->session;
  357. mutex_lock(&ctxdata->mutex);
  358. list_add(&sess->list_node, &ctxdata->sess_list);
  359. mutex_unlock(&ctxdata->mutex);
  360. } else {
  361. kfree(sess);
  362. }
  363. if (optee->ops->from_msg_param(optee, param, arg->num_params,
  364. msg_arg->params + 2)) {
  365. arg->ret = TEEC_ERROR_COMMUNICATION;
  366. arg->ret_origin = TEEC_ORIGIN_COMMS;
  367. /* Close session again to avoid leakage */
  368. optee_close_session(ctx, msg_arg->session);
  369. } else {
  370. arg->session = msg_arg->session;
  371. arg->ret = msg_arg->ret;
  372. arg->ret_origin = msg_arg->ret_origin;
  373. }
  374. out:
  375. optee_free_msg_arg(ctx, entry, offs);
  376. return rc;
  377. }
  378. int optee_system_session(struct tee_context *ctx, u32 session)
  379. {
  380. struct optee *optee = tee_get_drvdata(ctx->teedev);
  381. struct optee_context_data *ctxdata = ctx->data;
  382. struct optee_session *sess;
  383. int rc = -EINVAL;
  384. mutex_lock(&ctxdata->mutex);
  385. sess = find_session(ctxdata, session);
  386. if (sess && (sess->use_sys_thread ||
  387. optee_cq_incr_sys_thread_count(&optee->call_queue))) {
  388. sess->use_sys_thread = true;
  389. rc = 0;
  390. }
  391. mutex_unlock(&ctxdata->mutex);
  392. return rc;
  393. }
  394. int optee_close_session_helper(struct tee_context *ctx, u32 session,
  395. bool system_thread)
  396. {
  397. struct optee *optee = tee_get_drvdata(ctx->teedev);
  398. struct optee_shm_arg_entry *entry;
  399. struct optee_msg_arg *msg_arg;
  400. struct tee_shm *shm;
  401. u_int offs;
  402. msg_arg = optee_get_msg_arg(ctx, 0, &entry, &shm, &offs);
  403. if (IS_ERR(msg_arg))
  404. return PTR_ERR(msg_arg);
  405. msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
  406. msg_arg->session = session;
  407. optee->ops->do_call_with_arg(ctx, shm, offs, system_thread);
  408. optee_free_msg_arg(ctx, entry, offs);
  409. if (system_thread)
  410. optee_cq_decr_sys_thread_count(&optee->call_queue);
  411. return 0;
  412. }
  413. int optee_close_session(struct tee_context *ctx, u32 session)
  414. {
  415. struct optee_context_data *ctxdata = ctx->data;
  416. struct optee_session *sess;
  417. bool system_thread;
  418. /* Check that the session is valid and remove it from the list */
  419. mutex_lock(&ctxdata->mutex);
  420. sess = find_session(ctxdata, session);
  421. if (sess)
  422. list_del(&sess->list_node);
  423. mutex_unlock(&ctxdata->mutex);
  424. if (!sess)
  425. return -EINVAL;
  426. system_thread = sess->use_sys_thread;
  427. kfree(sess);
  428. return optee_close_session_helper(ctx, session, system_thread);
  429. }
  430. int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
  431. struct tee_param *param)
  432. {
  433. struct optee *optee = tee_get_drvdata(ctx->teedev);
  434. struct optee_context_data *ctxdata = ctx->data;
  435. struct optee_shm_arg_entry *entry;
  436. struct optee_msg_arg *msg_arg;
  437. struct optee_session *sess;
  438. struct tee_shm *shm;
  439. bool system_thread;
  440. u_int offs;
  441. int rc;
  442. /* Check that the session is valid */
  443. mutex_lock(&ctxdata->mutex);
  444. sess = find_session(ctxdata, arg->session);
  445. if (sess)
  446. system_thread = sess->use_sys_thread;
  447. mutex_unlock(&ctxdata->mutex);
  448. if (!sess)
  449. return -EINVAL;
  450. msg_arg = optee_get_msg_arg(ctx, arg->num_params,
  451. &entry, &shm, &offs);
  452. if (IS_ERR(msg_arg))
  453. return PTR_ERR(msg_arg);
  454. msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
  455. msg_arg->func = arg->func;
  456. msg_arg->session = arg->session;
  457. msg_arg->cancel_id = arg->cancel_id;
  458. rc = optee->ops->to_msg_param(optee, msg_arg->params, arg->num_params,
  459. param);
  460. if (rc)
  461. goto out;
  462. if (optee->ops->do_call_with_arg(ctx, shm, offs, system_thread)) {
  463. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  464. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  465. }
  466. if (optee->ops->from_msg_param(optee, param, arg->num_params,
  467. msg_arg->params)) {
  468. msg_arg->ret = TEEC_ERROR_COMMUNICATION;
  469. msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
  470. }
  471. arg->ret = msg_arg->ret;
  472. arg->ret_origin = msg_arg->ret_origin;
  473. out:
  474. optee_free_msg_arg(ctx, entry, offs);
  475. return rc;
  476. }
  477. int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
  478. {
  479. struct optee *optee = tee_get_drvdata(ctx->teedev);
  480. struct optee_context_data *ctxdata = ctx->data;
  481. struct optee_shm_arg_entry *entry;
  482. struct optee_msg_arg *msg_arg;
  483. struct optee_session *sess;
  484. bool system_thread;
  485. struct tee_shm *shm;
  486. u_int offs;
  487. /* Check that the session is valid */
  488. mutex_lock(&ctxdata->mutex);
  489. sess = find_session(ctxdata, session);
  490. if (sess)
  491. system_thread = sess->use_sys_thread;
  492. mutex_unlock(&ctxdata->mutex);
  493. if (!sess)
  494. return -EINVAL;
  495. msg_arg = optee_get_msg_arg(ctx, 0, &entry, &shm, &offs);
  496. if (IS_ERR(msg_arg))
  497. return PTR_ERR(msg_arg);
  498. msg_arg->cmd = OPTEE_MSG_CMD_CANCEL;
  499. msg_arg->session = session;
  500. msg_arg->cancel_id = cancel_id;
  501. optee->ops->do_call_with_arg(ctx, shm, offs, system_thread);
  502. optee_free_msg_arg(ctx, entry, offs);
  503. return 0;
  504. }
  505. static bool is_normal_memory(pgprot_t p)
  506. {
  507. #if defined(CONFIG_ARM)
  508. return (((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEALLOC) ||
  509. ((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEBACK));
  510. #elif defined(CONFIG_ARM64)
  511. return (pgprot_val(p) & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL);
  512. #else
  513. #error "Unsupported architecture"
  514. #endif
  515. }
  516. static int __check_mem_type(struct mm_struct *mm, unsigned long start,
  517. unsigned long end)
  518. {
  519. struct vm_area_struct *vma;
  520. VMA_ITERATOR(vmi, mm, start);
  521. for_each_vma_range(vmi, vma, end) {
  522. if (!is_normal_memory(vma->vm_page_prot))
  523. return -EINVAL;
  524. }
  525. return 0;
  526. }
  527. int optee_check_mem_type(unsigned long start, size_t num_pages)
  528. {
  529. struct mm_struct *mm = current->mm;
  530. int rc;
  531. /*
  532. * Allow kernel address to register with OP-TEE as kernel
  533. * pages are configured as normal memory only.
  534. */
  535. if (virt_addr_valid((void *)start) || is_vmalloc_addr((void *)start))
  536. return 0;
  537. mmap_read_lock(mm);
  538. rc = __check_mem_type(mm, start, start + num_pages * PAGE_SIZE);
  539. mmap_read_unlock(mm);
  540. return rc;
  541. }
  542. static int simple_call_with_arg(struct tee_context *ctx, u32 cmd)
  543. {
  544. struct optee *optee = tee_get_drvdata(ctx->teedev);
  545. struct optee_shm_arg_entry *entry;
  546. struct optee_msg_arg *msg_arg;
  547. struct tee_shm *shm;
  548. u_int offs;
  549. msg_arg = optee_get_msg_arg(ctx, 0, &entry, &shm, &offs);
  550. if (IS_ERR(msg_arg))
  551. return PTR_ERR(msg_arg);
  552. msg_arg->cmd = cmd;
  553. optee->ops->do_call_with_arg(ctx, shm, offs, false);
  554. optee_free_msg_arg(ctx, entry, offs);
  555. return 0;
  556. }
  557. int optee_do_bottom_half(struct tee_context *ctx)
  558. {
  559. return simple_call_with_arg(ctx, OPTEE_MSG_CMD_DO_BOTTOM_HALF);
  560. }
  561. int optee_stop_async_notif(struct tee_context *ctx)
  562. {
  563. return simple_call_with_arg(ctx, OPTEE_MSG_CMD_STOP_ASYNC_NOTIF);
  564. }