smcinvoke.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "smcinvoke: %s: " fmt, __func__
  7. #include <linux/module.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/anon_inodes.h>
  15. #include <linux/hashtable.h>
  16. #include <linux/cdev.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/dma-buf.h>
  19. #include <linux/delay.h>
  20. #include <linux/kref.h>
  21. #include <linux/signal.h>
  22. #include <linux/msm_ion.h>
  23. #include <linux/mem-buf.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/firmware.h>
  26. #include <linux/qcom_scm.h>
  27. #include <asm/cacheflush.h>
  28. #include <soc/qcom/qseecomi.h>
  29. #include <linux/qtee_shmbridge.h>
  30. #include "misc/qseecom_kernel.h"
  31. #include "smcinvoke.h"
  32. #include "smcinvoke_object.h"
  33. #define CREATE_TRACE_POINTS
  34. #include "trace_smcinvoke.h"
  35. #define SMCINVOKE_DEV "smcinvoke"
  36. #define SMCINVOKE_TZ_ROOT_OBJ 1
  37. #define SMCINVOKE_TZ_OBJ_NULL 0
  38. #define SMCINVOKE_TZ_MIN_BUF_SIZE 4096
  39. #define SMCINVOKE_ARGS_ALIGN_SIZE (sizeof(uint64_t))
  40. #define SMCINVOKE_NEXT_AVAILABLE_TXN 0
  41. #define SMCINVOKE_REQ_PLACED 1
  42. #define SMCINVOKE_REQ_PROCESSING 2
  43. #define SMCINVOKE_REQ_PROCESSED 3
  44. #define SMCINVOKE_INCREMENT 1
  45. #define SMCINVOKE_DECREMENT 0
  46. #define SMCINVOKE_OBJ_TYPE_TZ_OBJ 0
  47. #define SMCINVOKE_OBJ_TYPE_SERVER 1
  48. #define SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL 2
  49. #define SMCINVOKE_MEM_MAP_OBJ 0
  50. #define SMCINVOKE_MEM_RGN_OBJ 1
  51. #define SMCINVOKE_MEM_PERM_RW 6
  52. #define SMCINVOKE_SCM_EBUSY_WAIT_MS 30
  53. #define SMCINVOKE_SCM_EBUSY_MAX_RETRY 67
  54. /* TZ defined values - Start */
  55. #define SMCINVOKE_INVOKE_PARAM_ID 0x224
  56. #define SMCINVOKE_CB_RSP_PARAM_ID 0x22
  57. #define SMCINVOKE_INVOKE_CMD_LEGACY 0x32000600
  58. #define SMCINVOKE_INVOKE_CMD 0x32000602
  59. #define SMCINVOKE_CB_RSP_CMD 0x32000601
  60. #define SMCINVOKE_RESULT_INBOUND_REQ_NEEDED 3
  61. /* TZ defined values - End */
  62. /*
  63. * This is the state when server FD has been closed but
  64. * TZ still has refs of CBOBjs served by this server
  65. */
  66. #define SMCINVOKE_SERVER_STATE_DEFUNCT 1
  67. #define CBOBJ_MAX_RETRIES 5
  68. #define FOR_ARGS(ndxvar, counts, section) \
  69. for (ndxvar = OBJECT_COUNTS_INDEX_##section(counts); \
  70. ndxvar < (OBJECT_COUNTS_INDEX_##section(counts) \
  71. + OBJECT_COUNTS_NUM_##section(counts)); \
  72. ++ndxvar)
  73. #define TZCB_BUF_OFFSET(tzcb_req) (sizeof(tzcb_req->result) + \
  74. sizeof(struct smcinvoke_msg_hdr) + \
  75. sizeof(union smcinvoke_tz_args) * \
  76. OBJECT_COUNTS_TOTAL(tzcb_req->hdr.counts))
  77. /*
  78. * +ve uhandle : either remote obj or mem obj, decided by f_ops
  79. * -ve uhandle : either Obj NULL or CBObj
  80. * - -1: OBJ NULL
  81. * - < -1: CBObj
  82. */
  83. #define UHANDLE_IS_FD(h) ((h) >= 0)
  84. #define UHANDLE_IS_NULL(h) ((h) == SMCINVOKE_USERSPACE_OBJ_NULL)
  85. #define UHANDLE_IS_CB_OBJ(h) (h < SMCINVOKE_USERSPACE_OBJ_NULL)
  86. #define UHANDLE_NULL (SMCINVOKE_USERSPACE_OBJ_NULL)
  87. /*
  88. * MAKE => create handle for other domain i.e. TZ or userspace
  89. * GET => retrieve obj from incoming handle
  90. */
  91. #define UHANDLE_GET_CB_OBJ(h) (-2-(h))
  92. #define UHANDLE_MAKE_CB_OBJ(o) (-2-(o))
  93. #define UHANDLE_GET_FD(h) (h)
  94. /*
  95. * +ve tzhandle : remote object i.e. owned by TZ
  96. * -ve tzhandle : local object i.e. owned by linux
  97. * --------------------------------------------------
  98. *| 1 (1 bit) | Obj Id (15 bits) | srvr id (16 bits) |
  99. * ---------------------------------------------------
  100. * Server ids are defined below for various local objects
  101. * server id 0 : Kernel Obj
  102. * server id 1 : Memory region Obj
  103. * server id 2 : Memory map Obj
  104. * server id 3-15: Reserverd
  105. * server id 16 & up: Callback Objs
  106. */
  107. #define KRNL_SRVR_ID 0
  108. #define MEM_RGN_SRVR_ID 1
  109. #define MEM_MAP_SRVR_ID 2
  110. #define CBOBJ_SERVER_ID_START 0x10
  111. #define CBOBJ_SERVER_ID_END ((1<<16) - 1)
  112. /* local obj id is represented by 15 bits */
  113. #define MAX_LOCAL_OBJ_ID ((1<<15) - 1)
  114. /* CBOBJs will be served by server id 0x10 onwards */
  115. #define TZHANDLE_GET_SERVER(h) ((uint16_t)((h) & 0xFFFF))
  116. #define TZHANDLE_GET_OBJID(h) (((h) >> 16) & 0x7FFF)
  117. #define TZHANDLE_MAKE_LOCAL(s, o) (((0x8000 | (o)) << 16) | s)
  118. #define TZHANDLE_IS_NULL(h) ((h) == SMCINVOKE_TZ_OBJ_NULL)
  119. #define TZHANDLE_IS_LOCAL(h) ((h) & 0x80000000)
  120. #define TZHANDLE_IS_REMOTE(h) (!TZHANDLE_IS_NULL(h) && !TZHANDLE_IS_LOCAL(h))
  121. #define TZHANDLE_IS_KERNEL_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  122. TZHANDLE_GET_SERVER(h) == KRNL_SRVR_ID)
  123. #define TZHANDLE_IS_MEM_RGN_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  124. TZHANDLE_GET_SERVER(h) == MEM_RGN_SRVR_ID)
  125. #define TZHANDLE_IS_MEM_MAP_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  126. TZHANDLE_GET_SERVER(h) == MEM_MAP_SRVR_ID)
  127. #define TZHANDLE_IS_MEM_OBJ(h) (TZHANDLE_IS_MEM_RGN_OBJ(h) || \
  128. TZHANDLE_IS_MEM_MAP_OBJ(h))
  129. #define TZHANDLE_IS_CB_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  130. TZHANDLE_GET_SERVER(h) >= CBOBJ_SERVER_ID_START)
  131. #define FILE_IS_REMOTE_OBJ(f) ((f)->f_op && (f)->f_op == &g_smcinvoke_fops)
  132. static DEFINE_MUTEX(g_smcinvoke_lock);
  133. #define NO_LOCK 0
  134. #define TAKE_LOCK 1
  135. #define MUTEX_LOCK(x) { if (x) mutex_lock(&g_smcinvoke_lock); }
  136. #define MUTEX_UNLOCK(x) { if (x) mutex_unlock(&g_smcinvoke_lock); }
  137. static DEFINE_HASHTABLE(g_cb_servers, 8);
  138. static LIST_HEAD(g_mem_objs);
  139. static uint16_t g_last_cb_server_id = CBOBJ_SERVER_ID_START;
  140. static uint16_t g_last_mem_rgn_id, g_last_mem_map_obj_id;
  141. static size_t g_max_cb_buf_size = SMCINVOKE_TZ_MIN_BUF_SIZE;
  142. static unsigned int cb_reqs_inflight;
  143. static bool legacy_smc_call;
  144. static int invoke_cmd;
  145. static long smcinvoke_ioctl(struct file *, unsigned int, unsigned long);
  146. static int smcinvoke_open(struct inode *, struct file *);
  147. static int smcinvoke_release(struct inode *, struct file *);
  148. static int release_cb_server(uint16_t);
  149. static const struct file_operations g_smcinvoke_fops = {
  150. .owner = THIS_MODULE,
  151. .unlocked_ioctl = smcinvoke_ioctl,
  152. .compat_ioctl = smcinvoke_ioctl,
  153. .open = smcinvoke_open,
  154. .release = smcinvoke_release,
  155. };
  156. static dev_t smcinvoke_device_no;
  157. static struct cdev smcinvoke_cdev;
  158. static struct class *driver_class;
  159. static struct device *class_dev;
  160. static struct platform_device *smcinvoke_pdev;
  161. struct smcinvoke_buf_hdr {
  162. uint32_t offset;
  163. uint32_t size;
  164. };
  165. union smcinvoke_tz_args {
  166. struct smcinvoke_buf_hdr b;
  167. int32_t handle;
  168. };
  169. struct smcinvoke_msg_hdr {
  170. uint32_t tzhandle;
  171. uint32_t op;
  172. uint32_t counts;
  173. };
  174. /* Inbound reqs from TZ */
  175. struct smcinvoke_tzcb_req {
  176. int32_t result;
  177. struct smcinvoke_msg_hdr hdr;
  178. union smcinvoke_tz_args args[0];
  179. };
  180. struct smcinvoke_file_data {
  181. uint32_t context_type;
  182. union {
  183. uint32_t tzhandle;
  184. uint16_t server_id;
  185. };
  186. };
  187. struct smcinvoke_piggyback_msg {
  188. uint32_t version;
  189. uint32_t op;
  190. uint32_t counts;
  191. int32_t objs[0];
  192. };
  193. /* Data structure to hold request coming from TZ */
  194. struct smcinvoke_cb_txn {
  195. uint32_t txn_id;
  196. int32_t state;
  197. struct smcinvoke_tzcb_req *cb_req;
  198. size_t cb_req_bytes;
  199. struct file **filp_to_release;
  200. struct hlist_node hash;
  201. struct kref ref_cnt;
  202. };
  203. struct smcinvoke_server_info {
  204. uint16_t server_id;
  205. uint16_t state;
  206. uint32_t txn_id;
  207. struct kref ref_cnt;
  208. wait_queue_head_t req_wait_q;
  209. wait_queue_head_t rsp_wait_q;
  210. size_t cb_buf_size;
  211. DECLARE_HASHTABLE(reqs_table, 4);
  212. DECLARE_HASHTABLE(responses_table, 4);
  213. struct hlist_node hash;
  214. struct list_head pending_cbobjs;
  215. };
  216. struct smcinvoke_cbobj {
  217. uint16_t cbobj_id;
  218. struct kref ref_cnt;
  219. struct smcinvoke_server_info *server;
  220. struct list_head list;
  221. };
  222. /*
  223. * We require couple of objects, one for mem region & another
  224. * for mapped mem_obj once mem region has been mapped. It is
  225. * possible that TZ can release either independent of other.
  226. */
  227. struct smcinvoke_mem_obj {
  228. /* these ids are objid part of tzhandle */
  229. uint16_t mem_region_id;
  230. uint16_t mem_map_obj_id;
  231. struct dma_buf *dma_buf;
  232. struct dma_buf_attachment *buf_attach;
  233. struct sg_table *sgt;
  234. struct kref mem_regn_ref_cnt;
  235. struct kref mem_map_obj_ref_cnt;
  236. uint64_t p_addr;
  237. size_t p_addr_len;
  238. struct list_head list;
  239. bool bridge_created_by_others;
  240. uint64_t shmbridge_handle;
  241. };
  242. static void destroy_cb_server(struct kref *kref)
  243. {
  244. struct smcinvoke_server_info *server = container_of(kref,
  245. struct smcinvoke_server_info, ref_cnt);
  246. if (server) {
  247. hash_del(&server->hash);
  248. kfree(server);
  249. }
  250. }
  251. /*
  252. * A separate find func is reqd mainly for couple of cases:
  253. * next_cb_server_id_locked which checks if server id had been utilized or not.
  254. * - It would be overhead if we do ref_cnt for this case
  255. * smcinvoke_release: which is called when server is closed from userspace.
  256. * - During server creation we init ref count, now put it back
  257. */
  258. static struct smcinvoke_server_info *find_cb_server_locked(uint16_t server_id)
  259. {
  260. struct smcinvoke_server_info *data = NULL;
  261. hash_for_each_possible(g_cb_servers, data, hash, server_id) {
  262. if (data->server_id == server_id)
  263. return data;
  264. }
  265. return NULL;
  266. }
  267. static struct smcinvoke_server_info *get_cb_server_locked(uint16_t server_id)
  268. {
  269. struct smcinvoke_server_info *server = find_cb_server_locked(server_id);
  270. if (server)
  271. kref_get(&server->ref_cnt);
  272. return server;
  273. }
  274. static uint16_t next_cb_server_id_locked(void)
  275. {
  276. if (g_last_cb_server_id == CBOBJ_SERVER_ID_END)
  277. g_last_cb_server_id = CBOBJ_SERVER_ID_START;
  278. while (find_cb_server_locked(++g_last_cb_server_id))
  279. ;
  280. return g_last_cb_server_id;
  281. }
  282. static inline void release_filp(struct file **filp_to_release, size_t arr_len)
  283. {
  284. size_t i = 0;
  285. for (i = 0; i < arr_len; i++) {
  286. if (filp_to_release[i]) {
  287. fput(filp_to_release[i]);
  288. filp_to_release[i] = NULL;
  289. }
  290. }
  291. }
  292. static struct smcinvoke_mem_obj *find_mem_obj_locked(uint16_t mem_obj_id,
  293. bool is_mem_rgn_obj)
  294. {
  295. struct smcinvoke_mem_obj *mem_obj = NULL;
  296. if (list_empty(&g_mem_objs))
  297. return NULL;
  298. list_for_each_entry(mem_obj, &g_mem_objs, list) {
  299. if ((is_mem_rgn_obj &&
  300. (mem_obj->mem_region_id == mem_obj_id)) ||
  301. (!is_mem_rgn_obj &&
  302. (mem_obj->mem_map_obj_id == mem_obj_id)))
  303. return mem_obj;
  304. }
  305. return NULL;
  306. }
  307. static uint32_t next_mem_region_obj_id_locked(void)
  308. {
  309. if (g_last_mem_rgn_id == MAX_LOCAL_OBJ_ID)
  310. g_last_mem_rgn_id = 0;
  311. while (find_mem_obj_locked(++g_last_mem_rgn_id, SMCINVOKE_MEM_RGN_OBJ))
  312. ;
  313. return g_last_mem_rgn_id;
  314. }
  315. static uint32_t next_mem_map_obj_id_locked(void)
  316. {
  317. if (g_last_mem_map_obj_id == MAX_LOCAL_OBJ_ID)
  318. g_last_mem_map_obj_id = 0;
  319. while (find_mem_obj_locked(++g_last_mem_map_obj_id,
  320. SMCINVOKE_MEM_MAP_OBJ))
  321. ;
  322. return g_last_mem_map_obj_id;
  323. }
  324. static inline void free_mem_obj_locked(struct smcinvoke_mem_obj *mem_obj)
  325. {
  326. int ret = 0;
  327. bool is_bridge_created_by_others = mem_obj->bridge_created_by_others;
  328. struct dma_buf *dmabuf_to_free = mem_obj->dma_buf;
  329. uint64_t shmbridge_handle = mem_obj->shmbridge_handle;
  330. list_del(&mem_obj->list);
  331. kfree(mem_obj);
  332. mem_obj = NULL;
  333. mutex_unlock(&g_smcinvoke_lock);
  334. if (!is_bridge_created_by_others)
  335. ret = qtee_shmbridge_deregister(shmbridge_handle);
  336. if (ret)
  337. pr_err("Error:%d delete bridge failed leaking memory 0x%x\n",
  338. ret, dmabuf_to_free);
  339. else
  340. dma_buf_put(dmabuf_to_free);
  341. }
  342. static void del_mem_regn_obj_locked(struct kref *kref)
  343. {
  344. struct smcinvoke_mem_obj *mem_obj = container_of(kref,
  345. struct smcinvoke_mem_obj, mem_regn_ref_cnt);
  346. /*
  347. * mem_regn obj and mem_map obj are held into mem_obj structure which
  348. * can't be released until both kinds of objs have been released.
  349. * So check whether mem_map iobj has ref 0 and only then release mem_obj
  350. */
  351. if (kref_read(&mem_obj->mem_map_obj_ref_cnt) == 0)
  352. free_mem_obj_locked(mem_obj);
  353. }
  354. static void del_mem_map_obj_locked(struct kref *kref)
  355. {
  356. struct smcinvoke_mem_obj *mem_obj = container_of(kref,
  357. struct smcinvoke_mem_obj, mem_map_obj_ref_cnt);
  358. mem_obj->p_addr_len = 0;
  359. mem_obj->p_addr = 0;
  360. if (mem_obj->sgt)
  361. dma_buf_unmap_attachment(mem_obj->buf_attach,
  362. mem_obj->sgt, DMA_BIDIRECTIONAL);
  363. if (mem_obj->buf_attach)
  364. dma_buf_detach(mem_obj->dma_buf, mem_obj->buf_attach);
  365. /*
  366. * mem_regn obj and mem_map obj are held into mem_obj structure which
  367. * can't be released until both kinds of objs have been released.
  368. * So check if mem_regn obj has ref 0 and only then release mem_obj
  369. */
  370. if (kref_read(&mem_obj->mem_regn_ref_cnt) == 0)
  371. free_mem_obj_locked(mem_obj);
  372. }
  373. static int release_mem_obj_locked(int32_t tzhandle)
  374. {
  375. int is_mem_regn_obj = TZHANDLE_IS_MEM_RGN_OBJ(tzhandle);
  376. struct smcinvoke_mem_obj *mem_obj = find_mem_obj_locked(
  377. TZHANDLE_GET_OBJID(tzhandle), is_mem_regn_obj);
  378. if (!mem_obj) {
  379. pr_err("memory object not found\n");
  380. return OBJECT_ERROR_BADOBJ;
  381. }
  382. if (is_mem_regn_obj)
  383. kref_put(&mem_obj->mem_regn_ref_cnt, del_mem_regn_obj_locked);
  384. else
  385. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  386. return OBJECT_OK;
  387. }
  388. static void free_pending_cbobj_locked(struct kref *kref)
  389. {
  390. struct smcinvoke_server_info *server = NULL;
  391. struct smcinvoke_cbobj *obj = container_of(kref,
  392. struct smcinvoke_cbobj, ref_cnt);
  393. list_del(&obj->list);
  394. server = obj->server;
  395. kfree(obj);
  396. if (server)
  397. kref_put(&server->ref_cnt, destroy_cb_server);
  398. }
  399. static int get_pending_cbobj_locked(uint16_t srvr_id, int16_t obj_id)
  400. {
  401. int ret = 0;
  402. bool release_server = true;
  403. struct list_head *head = NULL;
  404. struct smcinvoke_cbobj *cbobj = NULL;
  405. struct smcinvoke_cbobj *obj = NULL;
  406. struct smcinvoke_server_info *server = get_cb_server_locked(srvr_id);
  407. if (!server) {
  408. pr_err("%s, server id : %u not found\n", __func__, srvr_id);
  409. return OBJECT_ERROR_BADOBJ;
  410. }
  411. head = &server->pending_cbobjs;
  412. list_for_each_entry(cbobj, head, list)
  413. if (cbobj->cbobj_id == obj_id) {
  414. kref_get(&cbobj->ref_cnt);
  415. goto out;
  416. }
  417. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  418. if (!obj) {
  419. ret = OBJECT_ERROR_KMEM;
  420. goto out;
  421. }
  422. obj->cbobj_id = obj_id;
  423. kref_init(&obj->ref_cnt);
  424. obj->server = server;
  425. /*
  426. * we are holding server ref in cbobj; we will
  427. * release server ref when cbobj is destroyed
  428. */
  429. release_server = false;
  430. list_add_tail(&obj->list, head);
  431. out:
  432. if (release_server)
  433. kref_put(&server->ref_cnt, destroy_cb_server);
  434. return ret;
  435. }
  436. static int put_pending_cbobj_locked(uint16_t srvr_id, int16_t obj_id)
  437. {
  438. int ret = -EINVAL;
  439. struct smcinvoke_server_info *srvr_info =
  440. get_cb_server_locked(srvr_id);
  441. struct list_head *head = NULL;
  442. struct smcinvoke_cbobj *cbobj = NULL;
  443. if (!srvr_info) {
  444. pr_err("%s, server id : %u not found\n", __func__, srvr_id);
  445. return ret;
  446. }
  447. trace_put_pending_cbobj_locked(srvr_id, obj_id);
  448. head = &srvr_info->pending_cbobjs;
  449. list_for_each_entry(cbobj, head, list)
  450. if (cbobj->cbobj_id == obj_id) {
  451. kref_put(&cbobj->ref_cnt, free_pending_cbobj_locked);
  452. ret = 0;
  453. break;
  454. }
  455. kref_put(&srvr_info->ref_cnt, destroy_cb_server);
  456. return ret;
  457. }
  458. static int release_tzhandle_locked(int32_t tzhandle)
  459. {
  460. if (TZHANDLE_IS_MEM_OBJ(tzhandle))
  461. return release_mem_obj_locked(tzhandle);
  462. else if (TZHANDLE_IS_CB_OBJ(tzhandle))
  463. return put_pending_cbobj_locked(TZHANDLE_GET_SERVER(tzhandle),
  464. TZHANDLE_GET_OBJID(tzhandle));
  465. return OBJECT_ERROR;
  466. }
  467. static void release_tzhandles(const int32_t *tzhandles, size_t len)
  468. {
  469. size_t i;
  470. mutex_lock(&g_smcinvoke_lock);
  471. for (i = 0; i < len; i++)
  472. release_tzhandle_locked(tzhandles[i]);
  473. mutex_unlock(&g_smcinvoke_lock);
  474. }
  475. static void delete_cb_txn(struct kref *kref)
  476. {
  477. struct smcinvoke_cb_txn *cb_txn = container_of(kref,
  478. struct smcinvoke_cb_txn, ref_cnt);
  479. if (OBJECT_OP_METHODID(cb_txn->cb_req->hdr.op) == OBJECT_OP_RELEASE)
  480. release_tzhandle_locked(cb_txn->cb_req->hdr.tzhandle);
  481. kfree(cb_txn->cb_req);
  482. hash_del(&cb_txn->hash);
  483. kfree(cb_txn);
  484. }
  485. static struct smcinvoke_cb_txn *find_cbtxn_locked(
  486. struct smcinvoke_server_info *server,
  487. uint32_t txn_id, int32_t state)
  488. {
  489. int i = 0;
  490. struct smcinvoke_cb_txn *cb_txn = NULL;
  491. /*
  492. * Since HASH_BITS() does not work on pointers, we can't select hash
  493. * table using state and loop over it.
  494. */
  495. if (state == SMCINVOKE_REQ_PLACED) {
  496. /* pick up 1st req */
  497. hash_for_each(server->reqs_table, i, cb_txn, hash) {
  498. kref_get(&cb_txn->ref_cnt);
  499. hash_del(&cb_txn->hash);
  500. return cb_txn;
  501. }
  502. } else if (state == SMCINVOKE_REQ_PROCESSING) {
  503. hash_for_each_possible(
  504. server->responses_table, cb_txn, hash, txn_id) {
  505. if (cb_txn->txn_id == txn_id) {
  506. kref_get(&cb_txn->ref_cnt);
  507. hash_del(&cb_txn->hash);
  508. return cb_txn;
  509. }
  510. }
  511. }
  512. return NULL;
  513. }
  514. /*
  515. * size_add saturates at SIZE_MAX. If integer overflow is detected,
  516. * this function would return SIZE_MAX otherwise normal a+b is returned.
  517. */
  518. static inline size_t size_add(size_t a, size_t b)
  519. {
  520. return (b > (SIZE_MAX - a)) ? SIZE_MAX : a + b;
  521. }
  522. /*
  523. * pad_size is used along with size_align to define a buffer overflow
  524. * protected version of ALIGN
  525. */
  526. static inline size_t pad_size(size_t a, size_t b)
  527. {
  528. return (~a + 1) % b;
  529. }
  530. /*
  531. * size_align saturates at SIZE_MAX. If integer overflow is detected, this
  532. * function would return SIZE_MAX otherwise next aligned size is returned.
  533. */
  534. static inline size_t size_align(size_t a, size_t b)
  535. {
  536. return size_add(a, pad_size(a, b));
  537. }
  538. static uint16_t get_server_id(int cb_server_fd)
  539. {
  540. uint16_t server_id = 0;
  541. struct smcinvoke_file_data *svr_cxt = NULL;
  542. struct file *tmp_filp = fget(cb_server_fd);
  543. if (!tmp_filp)
  544. return server_id;
  545. svr_cxt = tmp_filp->private_data;
  546. if (svr_cxt && svr_cxt->context_type == SMCINVOKE_OBJ_TYPE_SERVER)
  547. server_id = svr_cxt->server_id;
  548. if (tmp_filp)
  549. fput(tmp_filp);
  550. return server_id;
  551. }
  552. static bool is_dma_fd(int32_t uhandle, struct dma_buf **dma_buf)
  553. {
  554. *dma_buf = dma_buf_get(uhandle);
  555. return IS_ERR_OR_NULL(*dma_buf) ? false : true;
  556. }
  557. static bool is_remote_obj(int32_t uhandle, struct smcinvoke_file_data **tzobj,
  558. struct file **filp)
  559. {
  560. bool ret = false;
  561. struct file *tmp_filp = fget(uhandle);
  562. if (!tmp_filp)
  563. return ret;
  564. if (FILE_IS_REMOTE_OBJ(tmp_filp)) {
  565. *tzobj = tmp_filp->private_data;
  566. if ((*tzobj)->context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  567. *filp = tmp_filp;
  568. tmp_filp = NULL;
  569. ret = true;
  570. }
  571. }
  572. if (tmp_filp)
  573. fput(tmp_filp);
  574. return ret;
  575. }
  576. static int create_mem_obj(struct dma_buf *dma_buf, int32_t *mem_obj)
  577. {
  578. struct smcinvoke_mem_obj *t_mem_obj =
  579. kzalloc(sizeof(*t_mem_obj), GFP_KERNEL);
  580. if (!t_mem_obj) {
  581. dma_buf_put(dma_buf);
  582. return -ENOMEM;
  583. }
  584. kref_init(&t_mem_obj->mem_regn_ref_cnt);
  585. t_mem_obj->dma_buf = dma_buf;
  586. mutex_lock(&g_smcinvoke_lock);
  587. t_mem_obj->mem_region_id = next_mem_region_obj_id_locked();
  588. list_add_tail(&t_mem_obj->list, &g_mem_objs);
  589. mutex_unlock(&g_smcinvoke_lock);
  590. *mem_obj = TZHANDLE_MAKE_LOCAL(MEM_RGN_SRVR_ID,
  591. t_mem_obj->mem_region_id);
  592. return 0;
  593. }
  594. /*
  595. * This function retrieves file pointer corresponding to FD provided. It stores
  596. * retrieved file pointer until IOCTL call is concluded. Once call is completed,
  597. * all stored file pointers are released. file pointers are stored to prevent
  598. * other threads from releasing that FD while IOCTL is in progress.
  599. */
  600. static int get_tzhandle_from_uhandle(int32_t uhandle, int32_t server_fd,
  601. struct file **filp, uint32_t *tzhandle)
  602. {
  603. int ret = -EBADF;
  604. uint16_t server_id = 0;
  605. if (UHANDLE_IS_NULL(uhandle)) {
  606. *tzhandle = SMCINVOKE_TZ_OBJ_NULL;
  607. ret = 0;
  608. } else if (UHANDLE_IS_CB_OBJ(uhandle)) {
  609. server_id = get_server_id(server_fd);
  610. if (server_id < CBOBJ_SERVER_ID_START)
  611. goto out;
  612. mutex_lock(&g_smcinvoke_lock);
  613. ret = get_pending_cbobj_locked(server_id,
  614. UHANDLE_GET_CB_OBJ(uhandle));
  615. mutex_unlock(&g_smcinvoke_lock);
  616. if (ret)
  617. goto out;
  618. *tzhandle = TZHANDLE_MAKE_LOCAL(server_id,
  619. UHANDLE_GET_CB_OBJ(uhandle));
  620. ret = 0;
  621. } else if (UHANDLE_IS_FD(uhandle)) {
  622. struct dma_buf *dma_buf = NULL;
  623. struct smcinvoke_file_data *tzobj = NULL;
  624. if (is_dma_fd(UHANDLE_GET_FD(uhandle), &dma_buf)) {
  625. ret = create_mem_obj(dma_buf, tzhandle);
  626. } else if (is_remote_obj(UHANDLE_GET_FD(uhandle),
  627. &tzobj, filp)) {
  628. *tzhandle = tzobj->tzhandle;
  629. ret = 0;
  630. }
  631. }
  632. out:
  633. return ret;
  634. }
  635. static int get_fd_for_obj(uint32_t obj_type, uint32_t obj, int32_t *fd)
  636. {
  637. int unused_fd = -1, ret = -EINVAL;
  638. struct file *f = NULL;
  639. struct smcinvoke_file_data *cxt = NULL;
  640. cxt = kzalloc(sizeof(*cxt), GFP_KERNEL);
  641. if (!cxt) {
  642. ret = -ENOMEM;
  643. goto out;
  644. }
  645. if (obj_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ ||
  646. obj_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  647. cxt->context_type = obj_type;
  648. cxt->tzhandle = obj;
  649. } else if (obj_type == SMCINVOKE_OBJ_TYPE_SERVER) {
  650. cxt->context_type = SMCINVOKE_OBJ_TYPE_SERVER;
  651. cxt->server_id = obj;
  652. } else {
  653. goto out;
  654. }
  655. unused_fd = get_unused_fd_flags(O_RDWR);
  656. if (unused_fd < 0)
  657. goto out;
  658. if (fd == NULL)
  659. goto out;
  660. f = anon_inode_getfile(SMCINVOKE_DEV, &g_smcinvoke_fops, cxt, O_RDWR);
  661. if (IS_ERR(f))
  662. goto out;
  663. *fd = unused_fd;
  664. fd_install(*fd, f);
  665. return 0;
  666. out:
  667. if (unused_fd >= 0)
  668. put_unused_fd(unused_fd);
  669. kfree(cxt);
  670. return ret;
  671. }
  672. static int get_uhandle_from_tzhandle(int32_t tzhandle, int32_t srvr_id,
  673. int32_t *uhandle, bool lock, uint32_t context_type)
  674. {
  675. int ret = -1;
  676. if (TZHANDLE_IS_NULL(tzhandle)) {
  677. *uhandle = UHANDLE_NULL;
  678. ret = 0;
  679. } else if (TZHANDLE_IS_CB_OBJ(tzhandle)) {
  680. if (srvr_id != TZHANDLE_GET_SERVER(tzhandle))
  681. goto out;
  682. *uhandle = UHANDLE_MAKE_CB_OBJ(TZHANDLE_GET_OBJID(tzhandle));
  683. MUTEX_LOCK(lock)
  684. ret = get_pending_cbobj_locked(TZHANDLE_GET_SERVER(tzhandle),
  685. TZHANDLE_GET_OBJID(tzhandle));
  686. MUTEX_UNLOCK(lock)
  687. } else if (TZHANDLE_IS_MEM_RGN_OBJ(tzhandle)) {
  688. struct smcinvoke_mem_obj *mem_obj = NULL;
  689. MUTEX_LOCK(lock)
  690. mem_obj = find_mem_obj_locked(TZHANDLE_GET_OBJID(tzhandle),
  691. SMCINVOKE_MEM_RGN_OBJ);
  692. if (mem_obj != NULL) {
  693. int fd;
  694. fd = dma_buf_fd(mem_obj->dma_buf, O_CLOEXEC);
  695. if (fd < 0)
  696. goto exit_lock;
  697. *uhandle = fd;
  698. ret = 0;
  699. }
  700. exit_lock:
  701. MUTEX_UNLOCK(lock)
  702. } else if (TZHANDLE_IS_REMOTE(tzhandle)) {
  703. /* if execution comes here => tzhandle is an unsigned int */
  704. ret = get_fd_for_obj(context_type,
  705. (uint32_t)tzhandle, uhandle);
  706. }
  707. out:
  708. return ret;
  709. }
  710. static int smcinvoke_create_bridge(struct smcinvoke_mem_obj *mem_obj)
  711. {
  712. int ret = 0;
  713. int tz_perm = PERM_READ|PERM_WRITE;
  714. uint32_t *vmid_list;
  715. uint32_t *perms_list;
  716. uint32_t nelems = 0;
  717. struct dma_buf *dmabuf = mem_obj->dma_buf;
  718. phys_addr_t phys = mem_obj->p_addr;
  719. size_t size = mem_obj->p_addr_len;
  720. if (!qtee_shmbridge_is_enabled())
  721. return 0;
  722. ret = mem_buf_dma_buf_copy_vmperm(dmabuf, (int **)&vmid_list,
  723. (int **)&perms_list, (int *)&nelems);
  724. if (ret) {
  725. pr_err("mem_buf_dma_buf_copy_vmperm failure, err=%d\n", ret);
  726. return ret;
  727. }
  728. if (mem_buf_dma_buf_exclusive_owner(dmabuf))
  729. perms_list[0] = PERM_READ | PERM_WRITE;
  730. ret = qtee_shmbridge_register(phys, size, vmid_list, perms_list, nelems,
  731. tz_perm, &mem_obj->shmbridge_handle);
  732. if (ret && ret != -EEXIST) {
  733. pr_err("creation of shm bridge for mem_region_id %d failed ret %d\n",
  734. mem_obj->mem_region_id, ret);
  735. goto exit;
  736. }
  737. if (ret == -EEXIST) {
  738. mem_obj->bridge_created_by_others = true;
  739. ret = 0;
  740. }
  741. trace_smcinvoke_create_bridge(mem_obj->shmbridge_handle, mem_obj->mem_region_id);
  742. exit:
  743. kfree(perms_list);
  744. kfree(vmid_list);
  745. return ret;
  746. }
  747. static int32_t smcinvoke_release_mem_obj_locked(void *buf, size_t buf_len)
  748. {
  749. struct smcinvoke_tzcb_req *msg = buf;
  750. if (msg->hdr.counts != OBJECT_COUNTS_PACK(0, 0, 0, 0)) {
  751. pr_err("Invalid object count in %s\n", __func__);
  752. return OBJECT_ERROR_INVALID;
  753. }
  754. trace_release_mem_obj_locked(msg->hdr.tzhandle, buf_len);
  755. return release_tzhandle_locked(msg->hdr.tzhandle);
  756. }
  757. static int32_t smcinvoke_map_mem_region(void *buf, size_t buf_len)
  758. {
  759. int ret = OBJECT_OK;
  760. struct smcinvoke_tzcb_req *msg = buf;
  761. struct {
  762. uint64_t p_addr;
  763. uint64_t len;
  764. uint32_t perms;
  765. } *ob = NULL;
  766. int32_t *oo = NULL;
  767. struct smcinvoke_mem_obj *mem_obj = NULL;
  768. struct dma_buf_attachment *buf_attach = NULL;
  769. struct sg_table *sgt = NULL;
  770. if (msg->hdr.counts != OBJECT_COUNTS_PACK(0, 1, 1, 1) ||
  771. (buf_len - msg->args[0].b.offset < msg->args[0].b.size)) {
  772. pr_err("Invalid counts received for mapping mem obj\n");
  773. return OBJECT_ERROR_INVALID;
  774. }
  775. /* args[0] = BO, args[1] = OI, args[2] = OO */
  776. ob = buf + msg->args[0].b.offset;
  777. oo = &msg->args[2].handle;
  778. mutex_lock(&g_smcinvoke_lock);
  779. mem_obj = find_mem_obj_locked(TZHANDLE_GET_OBJID(msg->args[1].handle),
  780. SMCINVOKE_MEM_RGN_OBJ);
  781. if (!mem_obj) {
  782. mutex_unlock(&g_smcinvoke_lock);
  783. pr_err("Memory object not found\n");
  784. return OBJECT_ERROR_BADOBJ;
  785. }
  786. if (!mem_obj->p_addr) {
  787. kref_init(&mem_obj->mem_map_obj_ref_cnt);
  788. buf_attach = dma_buf_attach(mem_obj->dma_buf,
  789. &smcinvoke_pdev->dev);
  790. if (IS_ERR(buf_attach)) {
  791. ret = OBJECT_ERROR_KMEM;
  792. pr_err("dma buf attach failed, ret: %d\n", ret);
  793. goto out;
  794. }
  795. mem_obj->buf_attach = buf_attach;
  796. sgt = dma_buf_map_attachment(buf_attach, DMA_BIDIRECTIONAL);
  797. if (IS_ERR(sgt)) {
  798. pr_err("mapping dma buffers failed, ret: %d\n",
  799. PTR_ERR(sgt));
  800. ret = OBJECT_ERROR_KMEM;
  801. goto out;
  802. }
  803. mem_obj->sgt = sgt;
  804. /* contiguous only => nents=1 */
  805. if (sgt->nents != 1) {
  806. ret = OBJECT_ERROR_INVALID;
  807. pr_err("sg enries are not contigous, ret: %d\n", ret);
  808. goto out;
  809. }
  810. mem_obj->p_addr = sg_dma_address(sgt->sgl);
  811. mem_obj->p_addr_len = sgt->sgl->length;
  812. if (!mem_obj->p_addr) {
  813. ret = OBJECT_ERROR_INVALID;
  814. pr_err("invalid physical address, ret: %d\n", ret);
  815. goto out;
  816. }
  817. /* Increase reference count as we are feeding the memobj to
  818. * smcinvoke and unlock the mutex. No need to hold the mutex in
  819. * case of shmbridge creation.
  820. */
  821. kref_get(&mem_obj->mem_map_obj_ref_cnt);
  822. mutex_unlock(&g_smcinvoke_lock);
  823. ret = smcinvoke_create_bridge(mem_obj);
  824. /* Take lock again and decrease the reference count which we
  825. * increased for shmbridge but before proceeding further we
  826. * have to check again if the memobj is still valid or not
  827. * after decreasing the reference.
  828. */
  829. mutex_lock(&g_smcinvoke_lock);
  830. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  831. if (ret) {
  832. ret = OBJECT_ERROR_INVALID;
  833. goto out;
  834. }
  835. if (!find_mem_obj_locked(TZHANDLE_GET_OBJID(msg->args[1].handle),
  836. SMCINVOKE_MEM_RGN_OBJ)) {
  837. mutex_unlock(&g_smcinvoke_lock);
  838. pr_err("Memory object not found\n");
  839. return OBJECT_ERROR_BADOBJ;
  840. }
  841. mem_obj->mem_map_obj_id = next_mem_map_obj_id_locked();
  842. } else {
  843. kref_get(&mem_obj->mem_map_obj_ref_cnt);
  844. }
  845. ob->p_addr = mem_obj->p_addr;
  846. ob->len = mem_obj->p_addr_len;
  847. ob->perms = SMCINVOKE_MEM_PERM_RW;
  848. *oo = TZHANDLE_MAKE_LOCAL(MEM_MAP_SRVR_ID, mem_obj->mem_map_obj_id);
  849. out:
  850. if (ret != OBJECT_OK)
  851. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  852. mutex_unlock(&g_smcinvoke_lock);
  853. return ret;
  854. }
  855. static void process_kernel_obj(void *buf, size_t buf_len)
  856. {
  857. struct smcinvoke_tzcb_req *cb_req = buf;
  858. switch (cb_req->hdr.op) {
  859. case OBJECT_OP_MAP_REGION:
  860. cb_req->result = smcinvoke_map_mem_region(buf, buf_len);
  861. break;
  862. case OBJECT_OP_YIELD:
  863. cb_req->result = OBJECT_OK;
  864. break;
  865. default:
  866. pr_err(" invalid operation for tz kernel object\n");
  867. cb_req->result = OBJECT_ERROR_INVALID;
  868. break;
  869. }
  870. }
  871. static void process_mem_obj(void *buf, size_t buf_len)
  872. {
  873. struct smcinvoke_tzcb_req *cb_req = buf;
  874. mutex_lock(&g_smcinvoke_lock);
  875. cb_req->result = (cb_req->hdr.op == OBJECT_OP_RELEASE) ?
  876. smcinvoke_release_mem_obj_locked(buf, buf_len) :
  877. OBJECT_ERROR_INVALID;
  878. mutex_unlock(&g_smcinvoke_lock);
  879. }
  880. static int invoke_cmd_handler(int cmd, phys_addr_t in_paddr, size_t in_buf_len,
  881. uint8_t *out_buf, phys_addr_t out_paddr,
  882. size_t out_buf_len, int32_t *result, u64 *response_type,
  883. unsigned int *data, struct qtee_shm *in_shm,
  884. struct qtee_shm *out_shm)
  885. {
  886. int ret = 0;
  887. switch (cmd) {
  888. case SMCINVOKE_INVOKE_CMD_LEGACY:
  889. qtee_shmbridge_flush_shm_buf(in_shm);
  890. qtee_shmbridge_flush_shm_buf(out_shm);
  891. ret = qcom_scm_invoke_smc_legacy(in_paddr, in_buf_len, out_paddr, out_buf_len,
  892. result, response_type, data);
  893. qtee_shmbridge_inv_shm_buf(in_shm);
  894. qtee_shmbridge_inv_shm_buf(out_shm);
  895. break;
  896. case SMCINVOKE_INVOKE_CMD:
  897. ret = qcom_scm_invoke_smc(in_paddr, in_buf_len, out_paddr, out_buf_len,
  898. result, response_type, data);
  899. break;
  900. case SMCINVOKE_CB_RSP_CMD:
  901. ret = qcom_scm_invoke_callback_response(virt_to_phys(out_buf), out_buf_len,
  902. result, response_type, data);
  903. break;
  904. default:
  905. ret = -EINVAL;
  906. break;
  907. }
  908. trace_invoke_cmd_handler(cmd, *response_type, *result, ret);
  909. return ret;
  910. }
  911. /*
  912. * Buf should be aligned to struct smcinvoke_tzcb_req
  913. */
  914. static void process_tzcb_req(void *buf, size_t buf_len, struct file **arr_filp)
  915. {
  916. /* ret is going to TZ. Provide values from OBJECT_ERROR_<> */
  917. int ret = OBJECT_ERROR_DEFUNCT;
  918. int cbobj_retries = 0;
  919. long timeout_jiff;
  920. struct smcinvoke_cb_txn *cb_txn = NULL;
  921. struct smcinvoke_tzcb_req *cb_req = NULL, *tmp_cb_req = NULL;
  922. struct smcinvoke_server_info *srvr_info = NULL;
  923. if (buf_len < sizeof(struct smcinvoke_tzcb_req)) {
  924. pr_err("smaller buffer length : %u\n", buf_len);
  925. return;
  926. }
  927. cb_req = buf;
  928. /* check whether it is to be served by kernel or userspace */
  929. if (TZHANDLE_IS_KERNEL_OBJ(cb_req->hdr.tzhandle)) {
  930. return process_kernel_obj(buf, buf_len);
  931. } else if (TZHANDLE_IS_MEM_OBJ(cb_req->hdr.tzhandle)) {
  932. return process_mem_obj(buf, buf_len);
  933. } else if (!TZHANDLE_IS_CB_OBJ(cb_req->hdr.tzhandle)) {
  934. pr_err("Request object is not a callback object\n");
  935. cb_req->result = OBJECT_ERROR_INVALID;
  936. return;
  937. }
  938. /*
  939. * We need a copy of req that could be sent to server. Otherwise, if
  940. * someone kills invoke caller, buf would go away and server would be
  941. * working on already freed buffer, causing a device crash.
  942. */
  943. tmp_cb_req = kmemdup(buf, buf_len, GFP_KERNEL);
  944. if (!tmp_cb_req) {
  945. /* we need to return error to caller so fill up result */
  946. cb_req->result = OBJECT_ERROR_KMEM;
  947. pr_err("failed to create copy of request, set result: %d\n",
  948. cb_req->result);
  949. return;
  950. }
  951. cb_txn = kzalloc(sizeof(*cb_txn), GFP_KERNEL);
  952. if (!cb_txn) {
  953. cb_req->result = OBJECT_ERROR_KMEM;
  954. pr_err("failed to allocate memory for request, result: %d\n",
  955. cb_req->result);
  956. kfree(tmp_cb_req);
  957. return;
  958. }
  959. /* no need for memcpy as we did kmemdup() above */
  960. cb_req = tmp_cb_req;
  961. trace_process_tzcb_req_handle(cb_req->hdr.tzhandle, cb_req->hdr.op, cb_req->hdr.counts);
  962. cb_txn->state = SMCINVOKE_REQ_PLACED;
  963. cb_txn->cb_req = cb_req;
  964. cb_txn->cb_req_bytes = buf_len;
  965. cb_txn->filp_to_release = arr_filp;
  966. kref_init(&cb_txn->ref_cnt);
  967. mutex_lock(&g_smcinvoke_lock);
  968. ++cb_reqs_inflight;
  969. srvr_info = get_cb_server_locked(
  970. TZHANDLE_GET_SERVER(cb_req->hdr.tzhandle));
  971. if (!srvr_info || srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT) {
  972. /* ret equals Object_ERROR_DEFUNCT, at this point go to out */
  973. if (!srvr_info)
  974. pr_err("server is invalid\n");
  975. else {
  976. pr_err("server is defunct, state= %d tzhandle = %d\n",
  977. srvr_info->state, cb_req->hdr.tzhandle);
  978. }
  979. mutex_unlock(&g_smcinvoke_lock);
  980. goto out;
  981. }
  982. cb_txn->txn_id = ++srvr_info->txn_id;
  983. hash_add(srvr_info->reqs_table, &cb_txn->hash, cb_txn->txn_id);
  984. mutex_unlock(&g_smcinvoke_lock);
  985. trace_process_tzcb_req_wait(cb_req->hdr.tzhandle, cbobj_retries, cb_txn->txn_id,
  986. current->pid, current->tgid, srvr_info->state, srvr_info->server_id,
  987. cb_reqs_inflight);
  988. /*
  989. * we need not worry that server_info will be deleted because as long
  990. * as this CBObj is served by this server, srvr_info will be valid.
  991. */
  992. wake_up_interruptible_all(&srvr_info->req_wait_q);
  993. /* timeout before 1s otherwise tzbusy would come */
  994. timeout_jiff = msecs_to_jiffies(1000);
  995. while (cbobj_retries < CBOBJ_MAX_RETRIES) {
  996. ret = wait_event_interruptible_timeout(srvr_info->rsp_wait_q,
  997. (cb_txn->state == SMCINVOKE_REQ_PROCESSED) ||
  998. (srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT),
  999. timeout_jiff);
  1000. if (ret == 0) {
  1001. pr_err("CBobj timed out cb-tzhandle:%d, retry:%d, op:%d counts :%d\n",
  1002. cb_req->hdr.tzhandle, cbobj_retries,
  1003. cb_req->hdr.op, cb_req->hdr.counts);
  1004. pr_err("CBobj %d timedout pid %x,tid %x, srvr state=%d, srvr id:%u\n",
  1005. cb_req->hdr.tzhandle, current->pid,
  1006. current->tgid, srvr_info->state,
  1007. srvr_info->server_id);
  1008. } else {
  1009. break;
  1010. }
  1011. cbobj_retries++;
  1012. }
  1013. out:
  1014. /*
  1015. * we could be here because of either:
  1016. * a. Req is PROCESSED
  1017. * b. Server was killed
  1018. * c. Invoke thread is killed
  1019. * sometime invoke thread and server are part of same process.
  1020. */
  1021. mutex_lock(&g_smcinvoke_lock);
  1022. hash_del(&cb_txn->hash);
  1023. if (ret == 0) {
  1024. pr_err("CBObj timed out! No more retries\n");
  1025. cb_req->result = Object_ERROR_TIMEOUT;
  1026. } else if (ret == -ERESTARTSYS) {
  1027. pr_err("wait event interruped, ret: %d\n", ret);
  1028. cb_req->result = OBJECT_ERROR_ABORT;
  1029. } else {
  1030. if (cb_txn->state == SMCINVOKE_REQ_PROCESSED) {
  1031. /*
  1032. * it is possible that server was killed immediately
  1033. * after CB Req was processed but who cares now!
  1034. */
  1035. } else if (!srvr_info ||
  1036. srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT) {
  1037. cb_req->result = OBJECT_ERROR_DEFUNCT;
  1038. pr_err("server invalid, res: %d\n", cb_req->result);
  1039. } else {
  1040. pr_err("%s: unexpected event happened, ret:%d\n", __func__, ret);
  1041. cb_req->result = OBJECT_ERROR_ABORT;
  1042. }
  1043. }
  1044. --cb_reqs_inflight;
  1045. trace_process_tzcb_req_result(cb_req->result, cb_req->hdr.tzhandle, cb_req->hdr.op,
  1046. cb_req->hdr.counts, cb_reqs_inflight);
  1047. memcpy(buf, cb_req, buf_len);
  1048. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1049. if (srvr_info)
  1050. kref_put(&srvr_info->ref_cnt, destroy_cb_server);
  1051. mutex_unlock(&g_smcinvoke_lock);
  1052. }
  1053. static int marshal_out_invoke_req(const uint8_t *buf, uint32_t buf_size,
  1054. struct smcinvoke_cmd_req *req,
  1055. union smcinvoke_arg *args_buf,
  1056. uint32_t context_type)
  1057. {
  1058. int ret = -EINVAL, i = 0;
  1059. int32_t temp_fd = UHANDLE_NULL;
  1060. union smcinvoke_tz_args *tz_args = NULL;
  1061. size_t offset = sizeof(struct smcinvoke_msg_hdr) +
  1062. OBJECT_COUNTS_TOTAL(req->counts) *
  1063. sizeof(union smcinvoke_tz_args);
  1064. if (offset > buf_size)
  1065. goto out;
  1066. tz_args = (union smcinvoke_tz_args *)
  1067. (buf + sizeof(struct smcinvoke_msg_hdr));
  1068. tz_args += OBJECT_COUNTS_NUM_BI(req->counts);
  1069. if (args_buf == NULL)
  1070. return 0;
  1071. FOR_ARGS(i, req->counts, BO) {
  1072. args_buf[i].b.size = tz_args->b.size;
  1073. if ((buf_size - tz_args->b.offset < tz_args->b.size) ||
  1074. tz_args->b.offset > buf_size) {
  1075. pr_err("%s: buffer overflow detected\n", __func__);
  1076. goto out;
  1077. }
  1078. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  1079. if (copy_to_user((void __user *)
  1080. (uintptr_t)(args_buf[i].b.addr),
  1081. (uint8_t *)(buf) + tz_args->b.offset,
  1082. tz_args->b.size)) {
  1083. pr_err("Error %d copying ctxt to user\n", ret);
  1084. goto out;
  1085. }
  1086. } else {
  1087. memcpy((uint8_t *)(args_buf[i].b.addr),
  1088. (uint8_t *)(buf) + tz_args->b.offset,
  1089. tz_args->b.size);
  1090. }
  1091. tz_args++;
  1092. }
  1093. tz_args += OBJECT_COUNTS_NUM_OI(req->counts);
  1094. FOR_ARGS(i, req->counts, OO) {
  1095. /*
  1096. * create a new FD and assign to output object's context.
  1097. * We are passing cb_server_fd from output param in case OO
  1098. * is a CBObj. For CBObj, we have to ensure that it is sent
  1099. * to server who serves it and that info comes from USpace.
  1100. */
  1101. temp_fd = UHANDLE_NULL;
  1102. ret = get_uhandle_from_tzhandle(tz_args->handle,
  1103. TZHANDLE_GET_SERVER(tz_args->handle),
  1104. &temp_fd, NO_LOCK, context_type);
  1105. args_buf[i].o.fd = temp_fd;
  1106. if (ret)
  1107. goto out;
  1108. trace_marshal_out_invoke_req(i, tz_args->handle,
  1109. TZHANDLE_GET_SERVER(tz_args->handle), temp_fd);
  1110. tz_args++;
  1111. }
  1112. ret = 0;
  1113. out:
  1114. return ret;
  1115. }
  1116. static bool is_inbound_req(int val)
  1117. {
  1118. return (val == SMCINVOKE_RESULT_INBOUND_REQ_NEEDED ||
  1119. val == QSEOS_RESULT_INCOMPLETE ||
  1120. val == QSEOS_RESULT_BLOCKED_ON_LISTENER);
  1121. }
  1122. static int prepare_send_scm_msg(const uint8_t *in_buf, phys_addr_t in_paddr,
  1123. size_t in_buf_len,
  1124. uint8_t *out_buf, phys_addr_t out_paddr,
  1125. size_t out_buf_len,
  1126. struct smcinvoke_cmd_req *req,
  1127. union smcinvoke_arg *args_buf,
  1128. bool *tz_acked, uint32_t context_type,
  1129. struct qtee_shm *in_shm, struct qtee_shm *out_shm)
  1130. {
  1131. int ret = 0, cmd, retry_count = 0;
  1132. u64 response_type;
  1133. unsigned int data;
  1134. struct file *arr_filp[OBJECT_COUNTS_MAX_OO] = {NULL};
  1135. *tz_acked = false;
  1136. /* buf size should be page aligned */
  1137. if ((in_buf_len % PAGE_SIZE) != 0 || (out_buf_len % PAGE_SIZE) != 0)
  1138. return -EINVAL;
  1139. cmd = invoke_cmd;
  1140. /*
  1141. * purpose of lock here is to ensure that any CB obj that may be going
  1142. * to user as OO is not released by piggyback message on another invoke
  1143. * request. We should not move this lock to process_invoke_req() because
  1144. * that will either cause deadlock or prevent any other invoke request
  1145. * to come in. We release this lock when either
  1146. * a) TZ requires HLOS action to complete ongoing invoke operation
  1147. * b) Final response to invoke has been marshalled out
  1148. */
  1149. while (1) {
  1150. mutex_lock(&g_smcinvoke_lock);
  1151. do {
  1152. ret = invoke_cmd_handler(cmd, in_paddr, in_buf_len, out_buf,
  1153. out_paddr, out_buf_len, &req->result,
  1154. &response_type, &data, in_shm, out_shm);
  1155. if (ret == -EBUSY) {
  1156. pr_err("Secure side is busy,will retry after 30 ms\n");
  1157. mutex_unlock(&g_smcinvoke_lock);
  1158. msleep(SMCINVOKE_SCM_EBUSY_WAIT_MS);
  1159. mutex_lock(&g_smcinvoke_lock);
  1160. }
  1161. } while ((ret == -EBUSY) &&
  1162. (retry_count++ < SMCINVOKE_SCM_EBUSY_MAX_RETRY));
  1163. if (!ret && !is_inbound_req(response_type)) {
  1164. /* dont marshal if Obj returns an error */
  1165. if (!req->result) {
  1166. if (args_buf != NULL)
  1167. ret = marshal_out_invoke_req(in_buf,
  1168. in_buf_len, req, args_buf,
  1169. context_type);
  1170. }
  1171. *tz_acked = true;
  1172. }
  1173. mutex_unlock(&g_smcinvoke_lock);
  1174. if (cmd == SMCINVOKE_CB_RSP_CMD)
  1175. release_filp(arr_filp, OBJECT_COUNTS_MAX_OO);
  1176. if (ret || !is_inbound_req(response_type))
  1177. break;
  1178. /* process listener request */
  1179. if (response_type == QSEOS_RESULT_INCOMPLETE ||
  1180. response_type == QSEOS_RESULT_BLOCKED_ON_LISTENER) {
  1181. ret = qseecom_process_listener_from_smcinvoke(
  1182. &req->result, &response_type, &data);
  1183. trace_prepare_send_scm_msg(response_type, req->result);
  1184. if (!req->result &&
  1185. response_type != SMCINVOKE_RESULT_INBOUND_REQ_NEEDED) {
  1186. ret = marshal_out_invoke_req(in_buf,
  1187. in_buf_len, req, args_buf,
  1188. context_type);
  1189. }
  1190. *tz_acked = true;
  1191. }
  1192. /*
  1193. * qseecom does not understand smcinvoke's callback object &&
  1194. * erringly sets ret value as -EINVAL :( We need to handle it.
  1195. */
  1196. if (response_type != SMCINVOKE_RESULT_INBOUND_REQ_NEEDED)
  1197. break;
  1198. if (response_type == SMCINVOKE_RESULT_INBOUND_REQ_NEEDED) {
  1199. trace_status(__func__, "looks like inbnd req reqd");
  1200. process_tzcb_req(out_buf, out_buf_len, arr_filp);
  1201. cmd = SMCINVOKE_CB_RSP_CMD;
  1202. }
  1203. }
  1204. return ret;
  1205. }
  1206. /*
  1207. * SMC expects arguments in following format
  1208. * ---------------------------------------------------------------------------
  1209. * | cxt | op | counts | ptr|size |ptr|size...|ORef|ORef|...| rest of payload |
  1210. * ---------------------------------------------------------------------------
  1211. * cxt: target, op: operation, counts: total arguments
  1212. * offset: offset is from beginning of buffer i.e. cxt
  1213. * size: size is 8 bytes aligned value
  1214. */
  1215. static size_t compute_in_msg_size(const struct smcinvoke_cmd_req *req,
  1216. const union smcinvoke_arg *args_buf)
  1217. {
  1218. uint32_t i = 0;
  1219. size_t total_size = sizeof(struct smcinvoke_msg_hdr) +
  1220. OBJECT_COUNTS_TOTAL(req->counts) *
  1221. sizeof(union smcinvoke_tz_args);
  1222. /* Computed total_size should be 8 bytes aligned from start of buf */
  1223. total_size = ALIGN(total_size, SMCINVOKE_ARGS_ALIGN_SIZE);
  1224. /* each buffer has to be 8 bytes aligned */
  1225. while (i < OBJECT_COUNTS_NUM_buffers(req->counts))
  1226. total_size = size_add(total_size,
  1227. size_align(args_buf[i++].b.size,
  1228. SMCINVOKE_ARGS_ALIGN_SIZE));
  1229. return PAGE_ALIGN(total_size);
  1230. }
  1231. static int marshal_in_invoke_req(const struct smcinvoke_cmd_req *req,
  1232. const union smcinvoke_arg *args_buf, uint32_t tzhandle,
  1233. uint8_t *buf, size_t buf_size, struct file **arr_filp,
  1234. int32_t *tzhandles_to_release, uint32_t context_type)
  1235. {
  1236. int ret = -EINVAL, i = 0, j = 0, k = 0;
  1237. const struct smcinvoke_msg_hdr msg_hdr = {
  1238. tzhandle, req->op, req->counts};
  1239. uint32_t offset = sizeof(struct smcinvoke_msg_hdr) +
  1240. sizeof(union smcinvoke_tz_args) *
  1241. OBJECT_COUNTS_TOTAL(req->counts);
  1242. union smcinvoke_tz_args *tz_args = NULL;
  1243. if (buf_size < offset)
  1244. goto out;
  1245. *(struct smcinvoke_msg_hdr *)buf = msg_hdr;
  1246. tz_args = (union smcinvoke_tz_args *)(buf +
  1247. sizeof(struct smcinvoke_msg_hdr));
  1248. if (args_buf == NULL)
  1249. return 0;
  1250. FOR_ARGS(i, req->counts, BI) {
  1251. offset = size_align(offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  1252. if ((offset > buf_size) ||
  1253. (args_buf[i].b.size > (buf_size - offset)))
  1254. goto out;
  1255. tz_args[i].b.offset = offset;
  1256. tz_args[i].b.size = args_buf[i].b.size;
  1257. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1258. if (copy_from_user(buf + offset,
  1259. (void __user *)(uintptr_t)(args_buf[i].b.addr),
  1260. args_buf[i].b.size))
  1261. goto out;
  1262. } else {
  1263. memcpy(buf + offset, (void *)(args_buf[i].b.addr),
  1264. args_buf[i].b.size);
  1265. }
  1266. offset += args_buf[i].b.size;
  1267. }
  1268. FOR_ARGS(i, req->counts, BO) {
  1269. offset = size_align(offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  1270. if ((offset > buf_size) ||
  1271. (args_buf[i].b.size > (buf_size - offset)))
  1272. goto out;
  1273. tz_args[i].b.offset = offset;
  1274. tz_args[i].b.size = args_buf[i].b.size;
  1275. offset += args_buf[i].b.size;
  1276. }
  1277. FOR_ARGS(i, req->counts, OI) {
  1278. ret = get_tzhandle_from_uhandle(args_buf[i].o.fd,
  1279. args_buf[i].o.cb_server_fd, &arr_filp[j++],
  1280. &(tz_args[i].handle));
  1281. if (ret)
  1282. goto out;
  1283. trace_marshal_in_invoke_req(i, args_buf[i].o.fd,
  1284. args_buf[i].o.cb_server_fd, tz_args[i].handle);
  1285. tzhandles_to_release[k++] = tz_args[i].handle;
  1286. }
  1287. ret = 0;
  1288. out:
  1289. return ret;
  1290. }
  1291. static int marshal_in_tzcb_req(const struct smcinvoke_cb_txn *cb_txn,
  1292. struct smcinvoke_accept *user_req, int srvr_id)
  1293. {
  1294. int ret = 0, i = 0;
  1295. int32_t temp_fd = UHANDLE_NULL;
  1296. union smcinvoke_arg tmp_arg;
  1297. struct smcinvoke_tzcb_req *tzcb_req = cb_txn->cb_req;
  1298. union smcinvoke_tz_args *tz_args = tzcb_req->args;
  1299. size_t tzcb_req_len = cb_txn->cb_req_bytes;
  1300. size_t tz_buf_offset = TZCB_BUF_OFFSET(tzcb_req);
  1301. size_t user_req_buf_offset = sizeof(union smcinvoke_arg) *
  1302. OBJECT_COUNTS_TOTAL(tzcb_req->hdr.counts);
  1303. if (tz_buf_offset > tzcb_req_len) {
  1304. ret = -EINVAL;
  1305. goto out;
  1306. }
  1307. user_req->txn_id = cb_txn->txn_id;
  1308. if (get_uhandle_from_tzhandle(tzcb_req->hdr.tzhandle, srvr_id,
  1309. &user_req->cbobj_id, TAKE_LOCK,
  1310. SMCINVOKE_OBJ_TYPE_TZ_OBJ)) {
  1311. ret = -EINVAL;
  1312. goto out;
  1313. }
  1314. user_req->op = tzcb_req->hdr.op;
  1315. user_req->counts = tzcb_req->hdr.counts;
  1316. user_req->argsize = sizeof(union smcinvoke_arg);
  1317. trace_marshal_in_tzcb_req_handle(tzcb_req->hdr.tzhandle, srvr_id,
  1318. user_req->cbobj_id, user_req->op, user_req->counts);
  1319. FOR_ARGS(i, tzcb_req->hdr.counts, BI) {
  1320. user_req_buf_offset = size_align(user_req_buf_offset,
  1321. SMCINVOKE_ARGS_ALIGN_SIZE);
  1322. tmp_arg.b.size = tz_args[i].b.size;
  1323. if ((tz_args[i].b.offset > tzcb_req_len) ||
  1324. (tz_args[i].b.size > tzcb_req_len - tz_args[i].b.offset) ||
  1325. (user_req_buf_offset > user_req->buf_len) ||
  1326. (tmp_arg.b.size >
  1327. user_req->buf_len - user_req_buf_offset)) {
  1328. ret = -EINVAL;
  1329. pr_err("%s: buffer overflow detected\n", __func__);
  1330. goto out;
  1331. }
  1332. tmp_arg.b.addr = user_req->buf_addr + user_req_buf_offset;
  1333. if (copy_to_user(u64_to_user_ptr
  1334. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1335. &tmp_arg, sizeof(tmp_arg)) ||
  1336. copy_to_user(u64_to_user_ptr(tmp_arg.b.addr),
  1337. (uint8_t *)(tzcb_req) + tz_args[i].b.offset,
  1338. tz_args[i].b.size)) {
  1339. ret = -EFAULT;
  1340. goto out;
  1341. }
  1342. user_req_buf_offset += tmp_arg.b.size;
  1343. }
  1344. FOR_ARGS(i, tzcb_req->hdr.counts, BO) {
  1345. user_req_buf_offset = size_align(user_req_buf_offset,
  1346. SMCINVOKE_ARGS_ALIGN_SIZE);
  1347. tmp_arg.b.size = tz_args[i].b.size;
  1348. if ((user_req_buf_offset > user_req->buf_len) ||
  1349. (tmp_arg.b.size >
  1350. user_req->buf_len - user_req_buf_offset)) {
  1351. ret = -EINVAL;
  1352. pr_err("%s: buffer overflow detected\n", __func__);
  1353. goto out;
  1354. }
  1355. tmp_arg.b.addr = user_req->buf_addr + user_req_buf_offset;
  1356. if (copy_to_user(u64_to_user_ptr
  1357. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1358. &tmp_arg, sizeof(tmp_arg))) {
  1359. ret = -EFAULT;
  1360. goto out;
  1361. }
  1362. user_req_buf_offset += tmp_arg.b.size;
  1363. }
  1364. FOR_ARGS(i, tzcb_req->hdr.counts, OI) {
  1365. /*
  1366. * create a new FD and assign to output object's
  1367. * context
  1368. */
  1369. temp_fd = UHANDLE_NULL;
  1370. ret = get_uhandle_from_tzhandle(tz_args[i].handle, srvr_id,
  1371. &temp_fd, TAKE_LOCK, SMCINVOKE_OBJ_TYPE_TZ_OBJ);
  1372. tmp_arg.o.fd = temp_fd;
  1373. if (ret) {
  1374. ret = -EINVAL;
  1375. goto out;
  1376. }
  1377. if (copy_to_user(u64_to_user_ptr
  1378. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1379. &tmp_arg, sizeof(tmp_arg))) {
  1380. ret = -EFAULT;
  1381. goto out;
  1382. }
  1383. trace_marshal_in_tzcb_req_fd(i, tz_args[i].handle, srvr_id, temp_fd);
  1384. }
  1385. out:
  1386. return ret;
  1387. }
  1388. static int marshal_out_tzcb_req(const struct smcinvoke_accept *user_req,
  1389. struct smcinvoke_cb_txn *cb_txn,
  1390. struct file **arr_filp)
  1391. {
  1392. int ret = -EINVAL, i = 0;
  1393. int32_t tzhandles_to_release[OBJECT_COUNTS_MAX_OO] = {0};
  1394. struct smcinvoke_tzcb_req *tzcb_req = cb_txn->cb_req;
  1395. union smcinvoke_tz_args *tz_args = tzcb_req->args;
  1396. release_tzhandles(&cb_txn->cb_req->hdr.tzhandle, 1);
  1397. tzcb_req->result = user_req->result;
  1398. FOR_ARGS(i, tzcb_req->hdr.counts, BO) {
  1399. union smcinvoke_arg tmp_arg;
  1400. if (copy_from_user((uint8_t *)&tmp_arg, u64_to_user_ptr(
  1401. user_req->buf_addr + i * sizeof(union smcinvoke_arg)),
  1402. sizeof(union smcinvoke_arg))) {
  1403. ret = -EFAULT;
  1404. goto out;
  1405. }
  1406. if (tmp_arg.b.size > tz_args[i].b.size)
  1407. goto out;
  1408. if (copy_from_user((uint8_t *)(tzcb_req) + tz_args[i].b.offset,
  1409. u64_to_user_ptr(tmp_arg.b.addr),
  1410. tmp_arg.b.size)) {
  1411. ret = -EFAULT;
  1412. goto out;
  1413. }
  1414. }
  1415. FOR_ARGS(i, tzcb_req->hdr.counts, OO) {
  1416. union smcinvoke_arg tmp_arg;
  1417. if (copy_from_user((uint8_t *)&tmp_arg, u64_to_user_ptr(
  1418. user_req->buf_addr + i * sizeof(union smcinvoke_arg)),
  1419. sizeof(union smcinvoke_arg))) {
  1420. ret = -EFAULT;
  1421. goto out;
  1422. }
  1423. ret = get_tzhandle_from_uhandle(tmp_arg.o.fd,
  1424. tmp_arg.o.cb_server_fd, &arr_filp[i],
  1425. &(tz_args[i].handle));
  1426. if (ret)
  1427. goto out;
  1428. tzhandles_to_release[i] = tz_args[i].handle;
  1429. trace_marshal_out_tzcb_req(i, tmp_arg.o.fd,
  1430. tmp_arg.o.cb_server_fd, tz_args[i].handle);
  1431. }
  1432. FOR_ARGS(i, tzcb_req->hdr.counts, OI) {
  1433. if (TZHANDLE_IS_CB_OBJ(tz_args[i].handle))
  1434. release_tzhandles(&tz_args[i].handle, 1);
  1435. }
  1436. ret = 0;
  1437. out:
  1438. if (ret)
  1439. release_tzhandles(tzhandles_to_release, OBJECT_COUNTS_MAX_OO);
  1440. return ret;
  1441. }
  1442. static void process_piggyback_data(void *buf, size_t buf_size)
  1443. {
  1444. int i;
  1445. struct smcinvoke_tzcb_req req = {0};
  1446. struct smcinvoke_piggyback_msg *msg = buf;
  1447. int32_t *objs = msg->objs;
  1448. for (i = 0; i < msg->counts; i++) {
  1449. req.hdr.op = msg->op;
  1450. req.hdr.counts = 0; /* release op does not require any args */
  1451. req.hdr.tzhandle = objs[i];
  1452. process_tzcb_req(&req, sizeof(struct smcinvoke_tzcb_req), NULL);
  1453. /* cbobjs_in_flight will be adjusted during CB processing */
  1454. }
  1455. }
  1456. static long process_ack_local_obj(struct file *filp, unsigned int cmd,
  1457. unsigned long arg)
  1458. {
  1459. int ret = -1;
  1460. int32_t local_obj = SMCINVOKE_USERSPACE_OBJ_NULL;
  1461. struct smcinvoke_file_data *filp_data = filp->private_data;
  1462. if (_IOC_SIZE(cmd) != sizeof(int32_t))
  1463. return -EINVAL;
  1464. ret = copy_from_user(&local_obj, (void __user *)(uintptr_t)arg,
  1465. sizeof(int32_t));
  1466. if (ret)
  1467. return -EFAULT;
  1468. mutex_lock(&g_smcinvoke_lock);
  1469. if (UHANDLE_IS_CB_OBJ(local_obj))
  1470. ret = put_pending_cbobj_locked(filp_data->server_id,
  1471. UHANDLE_GET_CB_OBJ(local_obj));
  1472. mutex_unlock(&g_smcinvoke_lock);
  1473. return ret;
  1474. }
  1475. static long process_server_req(struct file *filp, unsigned int cmd,
  1476. unsigned long arg)
  1477. {
  1478. int ret = -1;
  1479. int32_t server_fd = -1;
  1480. struct smcinvoke_server server_req = {0};
  1481. struct smcinvoke_server_info *server_info = NULL;
  1482. if (_IOC_SIZE(cmd) != sizeof(server_req)) {
  1483. pr_err("invalid command size received for server request\n");
  1484. return -EINVAL;
  1485. }
  1486. ret = copy_from_user(&server_req, (void __user *)(uintptr_t)arg,
  1487. sizeof(server_req));
  1488. if (ret) {
  1489. pr_err("copying server request from user failed\n");
  1490. return -EFAULT;
  1491. }
  1492. server_info = kzalloc(sizeof(*server_info), GFP_KERNEL);
  1493. if (!server_info)
  1494. return -ENOMEM;
  1495. kref_init(&server_info->ref_cnt);
  1496. init_waitqueue_head(&server_info->req_wait_q);
  1497. init_waitqueue_head(&server_info->rsp_wait_q);
  1498. server_info->cb_buf_size = server_req.cb_buf_size;
  1499. hash_init(server_info->reqs_table);
  1500. hash_init(server_info->responses_table);
  1501. INIT_LIST_HEAD(&server_info->pending_cbobjs);
  1502. mutex_lock(&g_smcinvoke_lock);
  1503. server_info->server_id = next_cb_server_id_locked();
  1504. hash_add(g_cb_servers, &server_info->hash,
  1505. server_info->server_id);
  1506. if (g_max_cb_buf_size < server_req.cb_buf_size)
  1507. g_max_cb_buf_size = server_req.cb_buf_size;
  1508. mutex_unlock(&g_smcinvoke_lock);
  1509. ret = get_fd_for_obj(SMCINVOKE_OBJ_TYPE_SERVER,
  1510. server_info->server_id, &server_fd);
  1511. if (ret)
  1512. release_cb_server(server_info->server_id);
  1513. return server_fd;
  1514. }
  1515. static long process_accept_req(struct file *filp, unsigned int cmd,
  1516. unsigned long arg)
  1517. {
  1518. int ret = -1;
  1519. struct smcinvoke_file_data *server_obj = filp->private_data;
  1520. struct smcinvoke_accept user_args = {0};
  1521. struct smcinvoke_cb_txn *cb_txn = NULL;
  1522. struct smcinvoke_server_info *server_info = NULL;
  1523. if (_IOC_SIZE(cmd) != sizeof(struct smcinvoke_accept)) {
  1524. pr_err("command size invalid for accept request\n");
  1525. return -EINVAL;
  1526. }
  1527. if (copy_from_user(&user_args, (void __user *)arg,
  1528. sizeof(struct smcinvoke_accept))) {
  1529. pr_err("copying accept request from user failed\n");
  1530. return -EFAULT;
  1531. }
  1532. if (user_args.argsize != sizeof(union smcinvoke_arg)) {
  1533. pr_err("arguments size is invalid for accept thread\n");
  1534. return -EINVAL;
  1535. }
  1536. /* ACCEPT is available only on server obj */
  1537. if (server_obj->context_type != SMCINVOKE_OBJ_TYPE_SERVER) {
  1538. pr_err("invalid object type received for accept req\n");
  1539. return -EPERM;
  1540. }
  1541. mutex_lock(&g_smcinvoke_lock);
  1542. server_info = get_cb_server_locked(server_obj->server_id);
  1543. if (!server_info) {
  1544. pr_err("No matching server with server id : %u found\n",
  1545. server_obj->server_id);
  1546. mutex_unlock(&g_smcinvoke_lock);
  1547. return -EINVAL;
  1548. }
  1549. if (server_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT)
  1550. server_info->state = 0;
  1551. mutex_unlock(&g_smcinvoke_lock);
  1552. /* First check if it has response otherwise wait for req */
  1553. if (user_args.has_resp) {
  1554. trace_process_accept_req_has_response(current->pid, current->tgid);
  1555. mutex_lock(&g_smcinvoke_lock);
  1556. cb_txn = find_cbtxn_locked(server_info, user_args.txn_id,
  1557. SMCINVOKE_REQ_PROCESSING);
  1558. mutex_unlock(&g_smcinvoke_lock);
  1559. /*
  1560. * cb_txn can be null if userspace provides wrong txn id OR
  1561. * invoke thread died while server was processing cb req.
  1562. * if invoke thread dies, it would remove req from Q. So
  1563. * no matching cb_txn would be on Q and hence NULL cb_txn.
  1564. * In this case, we want this thread to come back and start
  1565. * waiting for new cb requests, hence return EAGAIN here
  1566. */
  1567. if (!cb_txn) {
  1568. pr_err("%s txn %d either invalid or removed from Q\n",
  1569. __func__, user_args.txn_id);
  1570. ret = -EAGAIN;
  1571. goto out;
  1572. }
  1573. ret = marshal_out_tzcb_req(&user_args, cb_txn,
  1574. cb_txn->filp_to_release);
  1575. /*
  1576. * if client did not set error and we get error locally,
  1577. * we return local error to TA
  1578. */
  1579. if (ret && cb_txn->cb_req->result == 0)
  1580. cb_txn->cb_req->result = OBJECT_ERROR_UNAVAIL;
  1581. cb_txn->state = SMCINVOKE_REQ_PROCESSED;
  1582. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1583. wake_up(&server_info->rsp_wait_q);
  1584. /*
  1585. * if marshal_out fails, we should let userspace release
  1586. * any ref/obj it created for CB processing
  1587. */
  1588. if (ret && OBJECT_COUNTS_NUM_OO(user_args.counts))
  1589. goto out;
  1590. }
  1591. /*
  1592. * Once response has been delivered, thread will wait for another
  1593. * callback req to process.
  1594. */
  1595. do {
  1596. ret = wait_event_interruptible(server_info->req_wait_q,
  1597. !hash_empty(server_info->reqs_table));
  1598. if (ret) {
  1599. trace_process_accept_req_ret(current->pid, current->tgid, ret);
  1600. /*
  1601. * Ideally, we should destroy server if accept threads
  1602. * are returning due to client being killed or device
  1603. * going down (Shutdown/Reboot) but that would make
  1604. * server_info invalid. Other accept/invoke threads are
  1605. * using server_info and would crash. So dont do that.
  1606. */
  1607. mutex_lock(&g_smcinvoke_lock);
  1608. server_info->state = SMCINVOKE_SERVER_STATE_DEFUNCT;
  1609. mutex_unlock(&g_smcinvoke_lock);
  1610. wake_up_interruptible(&server_info->rsp_wait_q);
  1611. goto out;
  1612. }
  1613. mutex_lock(&g_smcinvoke_lock);
  1614. cb_txn = find_cbtxn_locked(server_info,
  1615. SMCINVOKE_NEXT_AVAILABLE_TXN,
  1616. SMCINVOKE_REQ_PLACED);
  1617. mutex_unlock(&g_smcinvoke_lock);
  1618. if (cb_txn) {
  1619. cb_txn->state = SMCINVOKE_REQ_PROCESSING;
  1620. ret = marshal_in_tzcb_req(cb_txn, &user_args,
  1621. server_obj->server_id);
  1622. if (ret) {
  1623. pr_err("failed to marshal in the callback request\n");
  1624. cb_txn->cb_req->result = OBJECT_ERROR_UNAVAIL;
  1625. cb_txn->state = SMCINVOKE_REQ_PROCESSED;
  1626. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1627. wake_up_interruptible(&server_info->rsp_wait_q);
  1628. continue;
  1629. }
  1630. mutex_lock(&g_smcinvoke_lock);
  1631. hash_add(server_info->responses_table, &cb_txn->hash,
  1632. cb_txn->txn_id);
  1633. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1634. mutex_unlock(&g_smcinvoke_lock);
  1635. trace_process_accept_req_placed(current->pid, current->tgid);
  1636. ret = copy_to_user((void __user *)arg, &user_args,
  1637. sizeof(struct smcinvoke_accept));
  1638. }
  1639. } while (!cb_txn);
  1640. out:
  1641. if (server_info)
  1642. kref_put(&server_info->ref_cnt, destroy_cb_server);
  1643. if (ret && ret != -ERESTARTSYS)
  1644. pr_err("accept thread returning with ret: %d\n", ret);
  1645. return ret;
  1646. }
  1647. static long process_invoke_req(struct file *filp, unsigned int cmd,
  1648. unsigned long arg)
  1649. {
  1650. int ret = -1, nr_args = 0;
  1651. struct smcinvoke_cmd_req req = {0};
  1652. void *in_msg = NULL, *out_msg = NULL;
  1653. size_t inmsg_size = 0, outmsg_size = SMCINVOKE_TZ_MIN_BUF_SIZE;
  1654. union smcinvoke_arg *args_buf = NULL;
  1655. struct smcinvoke_file_data *tzobj = filp->private_data;
  1656. struct qtee_shm in_shm = {0}, out_shm = {0};
  1657. /*
  1658. * Hold reference to remote object until invoke op is not
  1659. * completed. Release once invoke is done.
  1660. */
  1661. struct file *filp_to_release[OBJECT_COUNTS_MAX_OO] = {NULL};
  1662. /*
  1663. * If anything goes wrong, release alloted tzhandles for
  1664. * local objs which could be either CBObj or MemObj.
  1665. */
  1666. int32_t tzhandles_to_release[OBJECT_COUNTS_MAX_OO] = {0};
  1667. bool tz_acked = false;
  1668. uint32_t context_type = tzobj->context_type;
  1669. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ &&
  1670. _IOC_SIZE(cmd) != sizeof(req)) {
  1671. pr_err("command size for invoke req is invalid\n");
  1672. return -EINVAL;
  1673. }
  1674. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ &&
  1675. context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1676. pr_err("invalid context_type %d\n", context_type);
  1677. return -EPERM;
  1678. }
  1679. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1680. ret = copy_from_user(&req, (void __user *)arg, sizeof(req));
  1681. if (ret) {
  1682. pr_err("copying invoke req failed\n");
  1683. return -EFAULT;
  1684. }
  1685. } else {
  1686. req = *(struct smcinvoke_cmd_req *)arg;
  1687. }
  1688. if (req.argsize != sizeof(union smcinvoke_arg)) {
  1689. pr_err("arguments size for invoke req is invalid\n");
  1690. return -EINVAL;
  1691. }
  1692. nr_args = OBJECT_COUNTS_NUM_buffers(req.counts) +
  1693. OBJECT_COUNTS_NUM_objects(req.counts);
  1694. if (nr_args) {
  1695. args_buf = kcalloc(nr_args, req.argsize, GFP_KERNEL);
  1696. if (!args_buf)
  1697. return -ENOMEM;
  1698. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  1699. ret = copy_from_user(args_buf,
  1700. u64_to_user_ptr(req.args),
  1701. nr_args * req.argsize);
  1702. if (ret) {
  1703. ret = -EFAULT;
  1704. goto out;
  1705. }
  1706. } else {
  1707. memcpy(args_buf, (void *)(req.args),
  1708. nr_args * req.argsize);
  1709. }
  1710. }
  1711. inmsg_size = compute_in_msg_size(&req, args_buf);
  1712. ret = qtee_shmbridge_allocate_shm(inmsg_size, &in_shm);
  1713. if (ret) {
  1714. ret = -ENOMEM;
  1715. pr_err("shmbridge alloc failed for in msg in invoke req\n");
  1716. goto out;
  1717. }
  1718. in_msg = in_shm.vaddr;
  1719. mutex_lock(&g_smcinvoke_lock);
  1720. outmsg_size = PAGE_ALIGN(g_max_cb_buf_size);
  1721. mutex_unlock(&g_smcinvoke_lock);
  1722. ret = qtee_shmbridge_allocate_shm(outmsg_size, &out_shm);
  1723. if (ret) {
  1724. ret = -ENOMEM;
  1725. pr_err("shmbridge alloc failed for out msg in invoke req\n");
  1726. goto out;
  1727. }
  1728. out_msg = out_shm.vaddr;
  1729. trace_process_invoke_req_tzhandle(tzobj->tzhandle, req.op, req.counts);
  1730. ret = marshal_in_invoke_req(&req, args_buf, tzobj->tzhandle, in_msg,
  1731. inmsg_size, filp_to_release, tzhandles_to_release,
  1732. context_type);
  1733. if (ret) {
  1734. pr_err("failed to marshal in invoke req, ret :%d\n", ret);
  1735. goto out;
  1736. }
  1737. ret = prepare_send_scm_msg(in_msg, in_shm.paddr, inmsg_size,
  1738. out_msg, out_shm.paddr, outmsg_size,
  1739. &req, args_buf, &tz_acked, context_type,
  1740. &in_shm, &out_shm);
  1741. /*
  1742. * If scm_call is success, TZ owns responsibility to release
  1743. * refs for local objs.
  1744. */
  1745. if (!tz_acked) {
  1746. trace_status(__func__, "scm call successful");
  1747. goto out;
  1748. }
  1749. memset(tzhandles_to_release, 0, sizeof(tzhandles_to_release));
  1750. /*
  1751. * if invoke op results in an err, no need to marshal_out and
  1752. * copy args buf to user space
  1753. */
  1754. if (!req.result) {
  1755. /*
  1756. * Dont check ret of marshal_out because there might be a
  1757. * FD for OO which userspace must release even if an error
  1758. * occurs. Releasing FD from user space is much simpler than
  1759. * doing here. ORing of ret is reqd not to miss past error
  1760. */
  1761. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ)
  1762. ret |= copy_to_user(u64_to_user_ptr(req.args),
  1763. args_buf, nr_args * req.argsize);
  1764. else
  1765. memcpy((void *)(req.args), args_buf,
  1766. nr_args * req.argsize);
  1767. }
  1768. /* copy result of invoke op */
  1769. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  1770. ret |= copy_to_user((void __user *)arg, &req, sizeof(req));
  1771. if (ret)
  1772. goto out;
  1773. } else {
  1774. memcpy((void *)arg, (void *)&req, sizeof(req));
  1775. }
  1776. /* Outbuf could be carrying local objs to be released. */
  1777. process_piggyback_data(out_msg, outmsg_size);
  1778. out:
  1779. trace_process_invoke_req_result(ret, req.result, tzobj->tzhandle,
  1780. req.op, req.counts);
  1781. release_filp(filp_to_release, OBJECT_COUNTS_MAX_OO);
  1782. if (ret)
  1783. release_tzhandles(tzhandles_to_release, OBJECT_COUNTS_MAX_OO);
  1784. qtee_shmbridge_free_shm(&in_shm);
  1785. qtee_shmbridge_free_shm(&out_shm);
  1786. kfree(args_buf);
  1787. if (ret)
  1788. pr_err("invoke thread returning with ret = %d\n", ret);
  1789. return ret;
  1790. }
  1791. static long process_log_info(struct file *filp, unsigned int cmd,
  1792. unsigned long arg)
  1793. {
  1794. int ret = 0;
  1795. char buf[SMCINVOKE_LOG_BUF_SIZE];
  1796. struct smcinvoke_file_data *tzobj = filp->private_data;
  1797. ret = copy_from_user(buf, (void __user *)arg, SMCINVOKE_LOG_BUF_SIZE);
  1798. if (ret) {
  1799. pr_err("logging HLOS info copy failed\n");
  1800. return -EFAULT;
  1801. }
  1802. buf[SMCINVOKE_LOG_BUF_SIZE - 1] = '\0';
  1803. trace_process_log_info(buf, tzobj->context_type, tzobj->tzhandle);
  1804. return ret;
  1805. }
  1806. static long smcinvoke_ioctl(struct file *filp, unsigned int cmd,
  1807. unsigned long arg)
  1808. {
  1809. long ret = 0;
  1810. switch (cmd) {
  1811. case SMCINVOKE_IOCTL_INVOKE_REQ:
  1812. ret = process_invoke_req(filp, cmd, arg);
  1813. break;
  1814. case SMCINVOKE_IOCTL_ACCEPT_REQ:
  1815. ret = process_accept_req(filp, cmd, arg);
  1816. break;
  1817. case SMCINVOKE_IOCTL_SERVER_REQ:
  1818. ret = process_server_req(filp, cmd, arg);
  1819. break;
  1820. case SMCINVOKE_IOCTL_ACK_LOCAL_OBJ:
  1821. ret = process_ack_local_obj(filp, cmd, arg);
  1822. break;
  1823. case SMCINVOKE_IOCTL_LOG:
  1824. ret = process_log_info(filp, cmd, arg);
  1825. break;
  1826. default:
  1827. ret = -ENOIOCTLCMD;
  1828. break;
  1829. }
  1830. trace_smcinvoke_ioctl(cmd, ret);
  1831. return ret;
  1832. }
  1833. int get_root_fd(int *root_fd)
  1834. {
  1835. if (!root_fd)
  1836. return -EINVAL;
  1837. else
  1838. return get_fd_for_obj(SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL,
  1839. SMCINVOKE_TZ_ROOT_OBJ, root_fd);
  1840. }
  1841. int process_invoke_request_from_kernel_client(int fd,
  1842. struct smcinvoke_cmd_req *req)
  1843. {
  1844. struct file *filp = NULL;
  1845. int ret = 0;
  1846. if (!req) {
  1847. pr_err("NULL req\n");
  1848. return -EINVAL;
  1849. }
  1850. filp = fget(fd);
  1851. if (!filp) {
  1852. pr_err("Invalid fd %d\n", fd);
  1853. return -EINVAL;
  1854. }
  1855. ret = process_invoke_req(filp, 0, (uintptr_t)req);
  1856. fput(filp);
  1857. trace_process_invoke_request_from_kernel_client(fd, filp, file_count(filp));
  1858. return ret;
  1859. }
  1860. char *firmware_request_from_smcinvoke(const char *appname, size_t *fw_size, struct qtee_shm *shm)
  1861. {
  1862. int rc = 0;
  1863. const struct firmware *fw_entry = NULL, *fw_entry00 = NULL, *fw_entrylast = NULL;
  1864. char fw_name[MAX_APP_NAME_SIZE] = "\0";
  1865. int num_images = 0, phi = 0;
  1866. unsigned char app_arch = 0;
  1867. u8 *img_data_ptr = NULL;
  1868. size_t bufferOffset = 0, phdr_table_offset = 0;
  1869. size_t *offset = NULL;
  1870. Elf32_Phdr phdr32;
  1871. Elf64_Phdr phdr64;
  1872. struct elf32_hdr *ehdr = NULL;
  1873. struct elf64_hdr *ehdr64 = NULL;
  1874. /* load b00*/
  1875. snprintf(fw_name, sizeof(fw_name), "%s.b00", appname);
  1876. rc = firmware_request_nowarn(&fw_entry00, fw_name, class_dev);
  1877. if (rc) {
  1878. pr_err("Load %s failed, ret:%d\n", fw_name, rc);
  1879. return NULL;
  1880. }
  1881. app_arch = *(unsigned char *)(fw_entry00->data + EI_CLASS);
  1882. /*Get the offsets for split images header*/
  1883. if (app_arch == ELFCLASS32) {
  1884. ehdr = (struct elf32_hdr *)fw_entry00->data;
  1885. num_images = ehdr->e_phnum;
  1886. offset = kcalloc(num_images, sizeof(size_t), GFP_KERNEL);
  1887. if (offset == NULL)
  1888. goto release_fw_entry00;
  1889. phdr_table_offset = (size_t) ehdr->e_phoff;
  1890. for (phi = 1; phi < num_images; ++phi) {
  1891. bufferOffset = phdr_table_offset + phi * sizeof(Elf32_Phdr);
  1892. phdr32 = *(Elf32_Phdr *)(fw_entry00->data + bufferOffset);
  1893. offset[phi] = (size_t)phdr32.p_offset;
  1894. }
  1895. } else if (app_arch == ELFCLASS64) {
  1896. ehdr64 = (struct elf64_hdr *)fw_entry00->data;
  1897. num_images = ehdr64->e_phnum;
  1898. offset = kcalloc(num_images, sizeof(size_t), GFP_KERNEL);
  1899. if (offset == NULL)
  1900. goto release_fw_entry00;
  1901. phdr_table_offset = (size_t) ehdr64->e_phoff;
  1902. for (phi = 1; phi < num_images; ++phi) {
  1903. bufferOffset = phdr_table_offset + phi * sizeof(Elf64_Phdr);
  1904. phdr64 = *(Elf64_Phdr *)(fw_entry00->data + bufferOffset);
  1905. offset[phi] = (size_t)phdr64.p_offset;
  1906. }
  1907. } else {
  1908. pr_err("QSEE %s app, arch %u is not supported\n", appname, app_arch);
  1909. goto release_fw_entry00;
  1910. }
  1911. /*Find the size of last split bin image*/
  1912. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, num_images-1);
  1913. rc = firmware_request_nowarn(&fw_entrylast, fw_name, class_dev);
  1914. if (rc) {
  1915. pr_err("Failed to locate blob %s\n", fw_name);
  1916. goto release_fw_entry00;
  1917. }
  1918. /*Total size of image will be the offset of last image + the size of last split image*/
  1919. *fw_size = fw_entrylast->size + offset[num_images-1];
  1920. /*Allocate memory for the buffer that will hold the split image*/
  1921. rc = qtee_shmbridge_allocate_shm((*fw_size), shm);
  1922. if (rc) {
  1923. pr_err("smbridge alloc failed for size: %zu\n", *fw_size);
  1924. goto release_fw_entrylast;
  1925. }
  1926. img_data_ptr = shm->vaddr;
  1927. /*
  1928. * Copy contents of split bins to the buffer
  1929. */
  1930. memcpy(img_data_ptr, fw_entry00->data, fw_entry00->size);
  1931. for (phi = 1; phi < num_images-1; phi++) {
  1932. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, phi);
  1933. rc = firmware_request_nowarn(&fw_entry, fw_name, class_dev);
  1934. if (rc) {
  1935. pr_err("Failed to locate blob %s\n", fw_name);
  1936. qtee_shmbridge_free_shm(shm);
  1937. img_data_ptr = NULL;
  1938. goto release_fw_entrylast;
  1939. }
  1940. memcpy(img_data_ptr + offset[phi], fw_entry->data, fw_entry->size);
  1941. release_firmware(fw_entry);
  1942. fw_entry = NULL;
  1943. }
  1944. memcpy(img_data_ptr + offset[phi], fw_entrylast->data, fw_entrylast->size);
  1945. release_fw_entrylast:
  1946. release_firmware(fw_entrylast);
  1947. release_fw_entry00:
  1948. release_firmware(fw_entry00);
  1949. kfree(offset);
  1950. return img_data_ptr;
  1951. }
  1952. EXPORT_SYMBOL(firmware_request_from_smcinvoke);
  1953. static int smcinvoke_open(struct inode *nodp, struct file *filp)
  1954. {
  1955. struct smcinvoke_file_data *tzcxt = NULL;
  1956. tzcxt = kzalloc(sizeof(*tzcxt), GFP_KERNEL);
  1957. if (!tzcxt)
  1958. return -ENOMEM;
  1959. tzcxt->tzhandle = SMCINVOKE_TZ_ROOT_OBJ;
  1960. tzcxt->context_type = SMCINVOKE_OBJ_TYPE_TZ_OBJ;
  1961. filp->private_data = tzcxt;
  1962. return 0;
  1963. }
  1964. static int release_cb_server(uint16_t server_id)
  1965. {
  1966. struct smcinvoke_server_info *server = NULL;
  1967. mutex_lock(&g_smcinvoke_lock);
  1968. server = find_cb_server_locked(server_id);
  1969. if (server)
  1970. kref_put(&server->ref_cnt, destroy_cb_server);
  1971. mutex_unlock(&g_smcinvoke_lock);
  1972. return 0;
  1973. }
  1974. int smcinvoke_release_filp(struct file *filp)
  1975. {
  1976. int ret = 0;
  1977. bool release_handles;
  1978. uint8_t *in_buf = NULL;
  1979. uint8_t *out_buf = NULL;
  1980. struct smcinvoke_msg_hdr hdr = {0};
  1981. struct smcinvoke_file_data *file_data = filp->private_data;
  1982. struct smcinvoke_cmd_req req = {0};
  1983. uint32_t tzhandle = 0;
  1984. struct qtee_shm in_shm = {0}, out_shm = {0};
  1985. trace_smcinvoke_release_filp(current->files, filp,
  1986. file_count(filp), file_data->context_type);
  1987. if (file_data->context_type == SMCINVOKE_OBJ_TYPE_SERVER) {
  1988. ret = release_cb_server(file_data->server_id);
  1989. goto out;
  1990. }
  1991. tzhandle = file_data->tzhandle;
  1992. /* Root object is special in sense it is indestructible */
  1993. if (!tzhandle || tzhandle == SMCINVOKE_TZ_ROOT_OBJ)
  1994. goto out;
  1995. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &in_shm);
  1996. if (ret) {
  1997. ret = -ENOMEM;
  1998. pr_err("shmbridge alloc failed for in msg in release\n");
  1999. goto out;
  2000. }
  2001. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &out_shm);
  2002. if (ret) {
  2003. ret = -ENOMEM;
  2004. pr_err("shmbridge alloc failed for out msg in release\n");
  2005. goto out;
  2006. }
  2007. in_buf = in_shm.vaddr;
  2008. out_buf = out_shm.vaddr;
  2009. hdr.tzhandle = tzhandle;
  2010. hdr.op = OBJECT_OP_RELEASE;
  2011. hdr.counts = 0;
  2012. *(struct smcinvoke_msg_hdr *)in_buf = hdr;
  2013. ret = prepare_send_scm_msg(in_buf, in_shm.paddr,
  2014. SMCINVOKE_TZ_MIN_BUF_SIZE, out_buf, out_shm.paddr,
  2015. SMCINVOKE_TZ_MIN_BUF_SIZE, &req, NULL, &release_handles,
  2016. file_data->context_type, &in_shm, &out_shm);
  2017. process_piggyback_data(out_buf, SMCINVOKE_TZ_MIN_BUF_SIZE);
  2018. out:
  2019. kfree(filp->private_data);
  2020. filp->private_data = NULL;
  2021. qtee_shmbridge_free_shm(&in_shm);
  2022. qtee_shmbridge_free_shm(&out_shm);
  2023. return ret;
  2024. }
  2025. int smcinvoke_release_from_kernel_client(int fd)
  2026. {
  2027. struct file *filp = NULL;
  2028. /* use fget() to get filp, but this will increase file ref_cnt to 1,
  2029. * then decrease file ref_cnt to 0 with fput().
  2030. */
  2031. filp = fget(fd);
  2032. if (!filp) {
  2033. pr_err("invalid fd %d to release\n", fd);
  2034. return -EINVAL;
  2035. }
  2036. trace_smcinvoke_release_from_kernel_client(current->files, filp,
  2037. file_count(filp));
  2038. /* free filp, notify TZ to release object */
  2039. smcinvoke_release_filp(filp);
  2040. fput(filp);
  2041. return 0;
  2042. }
  2043. static int smcinvoke_release(struct inode *nodp, struct file *filp)
  2044. {
  2045. trace_smcinvoke_release(current->files, filp, file_count(filp),
  2046. filp->private_data);
  2047. if (filp->private_data)
  2048. return smcinvoke_release_filp(filp);
  2049. else
  2050. return 0;
  2051. }
  2052. static int smcinvoke_probe(struct platform_device *pdev)
  2053. {
  2054. unsigned int baseminor = 0;
  2055. unsigned int count = 1;
  2056. int rc = 0;
  2057. rc = alloc_chrdev_region(&smcinvoke_device_no, baseminor, count,
  2058. SMCINVOKE_DEV);
  2059. if (rc < 0) {
  2060. pr_err("chrdev_region failed %d for %s\n", rc, SMCINVOKE_DEV);
  2061. return rc;
  2062. }
  2063. driver_class = class_create(THIS_MODULE, SMCINVOKE_DEV);
  2064. if (IS_ERR(driver_class)) {
  2065. rc = -ENOMEM;
  2066. pr_err("class_create failed %d\n", rc);
  2067. goto exit_unreg_chrdev_region;
  2068. }
  2069. class_dev = device_create(driver_class, NULL, smcinvoke_device_no,
  2070. NULL, SMCINVOKE_DEV);
  2071. if (!class_dev) {
  2072. pr_err("class_device_create failed %d\n", rc);
  2073. rc = -ENOMEM;
  2074. goto exit_destroy_class;
  2075. }
  2076. cdev_init(&smcinvoke_cdev, &g_smcinvoke_fops);
  2077. smcinvoke_cdev.owner = THIS_MODULE;
  2078. rc = cdev_add(&smcinvoke_cdev, MKDEV(MAJOR(smcinvoke_device_no), 0),
  2079. count);
  2080. if (rc < 0) {
  2081. pr_err("cdev_add failed %d for %s\n", rc, SMCINVOKE_DEV);
  2082. goto exit_destroy_device;
  2083. }
  2084. smcinvoke_pdev = pdev;
  2085. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2086. if (rc) {
  2087. pr_err("dma_set_mask_and_coherent failed %d\n", rc);
  2088. goto exit_destroy_device;
  2089. }
  2090. legacy_smc_call = of_property_read_bool((&pdev->dev)->of_node,
  2091. "qcom,support-legacy_smc");
  2092. invoke_cmd = legacy_smc_call ? SMCINVOKE_INVOKE_CMD_LEGACY : SMCINVOKE_INVOKE_CMD;
  2093. return 0;
  2094. exit_destroy_device:
  2095. device_destroy(driver_class, smcinvoke_device_no);
  2096. exit_destroy_class:
  2097. class_destroy(driver_class);
  2098. exit_unreg_chrdev_region:
  2099. unregister_chrdev_region(smcinvoke_device_no, count);
  2100. return rc;
  2101. }
  2102. static int smcinvoke_remove(struct platform_device *pdev)
  2103. {
  2104. int count = 1;
  2105. cdev_del(&smcinvoke_cdev);
  2106. device_destroy(driver_class, smcinvoke_device_no);
  2107. class_destroy(driver_class);
  2108. unregister_chrdev_region(smcinvoke_device_no, count);
  2109. return 0;
  2110. }
  2111. static int __maybe_unused smcinvoke_suspend(struct platform_device *pdev,
  2112. pm_message_t state)
  2113. {
  2114. int ret = 0;
  2115. mutex_lock(&g_smcinvoke_lock);
  2116. if (cb_reqs_inflight) {
  2117. pr_err("Failed to suspend smcinvoke driver\n");
  2118. ret = -EIO;
  2119. }
  2120. mutex_unlock(&g_smcinvoke_lock);
  2121. return ret;
  2122. }
  2123. static int __maybe_unused smcinvoke_resume(struct platform_device *pdev)
  2124. {
  2125. return 0;
  2126. }
  2127. static const struct of_device_id smcinvoke_match[] = {
  2128. {
  2129. .compatible = "qcom,smcinvoke",
  2130. },
  2131. {},
  2132. };
  2133. static struct platform_driver smcinvoke_plat_driver = {
  2134. .probe = smcinvoke_probe,
  2135. .remove = smcinvoke_remove,
  2136. .suspend = smcinvoke_suspend,
  2137. .resume = smcinvoke_resume,
  2138. .driver = {
  2139. .name = "smcinvoke",
  2140. .of_match_table = smcinvoke_match,
  2141. },
  2142. };
  2143. static int smcinvoke_init(void)
  2144. {
  2145. return platform_driver_register(&smcinvoke_plat_driver);
  2146. }
  2147. static void smcinvoke_exit(void)
  2148. {
  2149. platform_driver_unregister(&smcinvoke_plat_driver);
  2150. }
  2151. module_init(smcinvoke_init);
  2152. module_exit(smcinvoke_exit);
  2153. MODULE_LICENSE("GPL v2");
  2154. MODULE_DESCRIPTION("SMC Invoke driver");