smcinvoke.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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 is_smcinvoke_created_shmbridge;
  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 = mem_obj->is_smcinvoke_created_shmbridge;
  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)
  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. mutex_lock(&g_smcinvoke_lock);
  342. }
  343. static void del_mem_regn_obj_locked(struct kref *kref)
  344. {
  345. struct smcinvoke_mem_obj *mem_obj = container_of(kref,
  346. struct smcinvoke_mem_obj, mem_regn_ref_cnt);
  347. /*
  348. * mem_regn obj and mem_map obj are held into mem_obj structure which
  349. * can't be released until both kinds of objs have been released.
  350. * So check whether mem_map iobj has ref 0 and only then release mem_obj
  351. */
  352. if (kref_read(&mem_obj->mem_map_obj_ref_cnt) == 0)
  353. free_mem_obj_locked(mem_obj);
  354. }
  355. static void del_mem_map_obj_locked(struct kref *kref)
  356. {
  357. struct smcinvoke_mem_obj *mem_obj = container_of(kref,
  358. struct smcinvoke_mem_obj, mem_map_obj_ref_cnt);
  359. mem_obj->p_addr_len = 0;
  360. mem_obj->p_addr = 0;
  361. if (mem_obj->sgt)
  362. dma_buf_unmap_attachment(mem_obj->buf_attach,
  363. mem_obj->sgt, DMA_BIDIRECTIONAL);
  364. if (mem_obj->buf_attach)
  365. dma_buf_detach(mem_obj->dma_buf, mem_obj->buf_attach);
  366. /*
  367. * mem_regn obj and mem_map obj are held into mem_obj structure which
  368. * can't be released until both kinds of objs have been released.
  369. * So check if mem_regn obj has ref 0 and only then release mem_obj
  370. */
  371. if (kref_read(&mem_obj->mem_regn_ref_cnt) == 0)
  372. free_mem_obj_locked(mem_obj);
  373. }
  374. static int release_mem_obj_locked(int32_t tzhandle)
  375. {
  376. int is_mem_regn_obj = TZHANDLE_IS_MEM_RGN_OBJ(tzhandle);
  377. struct smcinvoke_mem_obj *mem_obj = find_mem_obj_locked(
  378. TZHANDLE_GET_OBJID(tzhandle), is_mem_regn_obj);
  379. if (!mem_obj) {
  380. pr_err("memory object not found\n");
  381. return OBJECT_ERROR_BADOBJ;
  382. }
  383. if (is_mem_regn_obj)
  384. kref_put(&mem_obj->mem_regn_ref_cnt, del_mem_regn_obj_locked);
  385. else
  386. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  387. return OBJECT_OK;
  388. }
  389. static void free_pending_cbobj_locked(struct kref *kref)
  390. {
  391. struct smcinvoke_server_info *server = NULL;
  392. struct smcinvoke_cbobj *obj = container_of(kref,
  393. struct smcinvoke_cbobj, ref_cnt);
  394. list_del(&obj->list);
  395. server = obj->server;
  396. kfree(obj);
  397. if (server)
  398. kref_put(&server->ref_cnt, destroy_cb_server);
  399. }
  400. static int get_pending_cbobj_locked(uint16_t srvr_id, int16_t obj_id)
  401. {
  402. int ret = 0;
  403. bool release_server = true;
  404. struct list_head *head = NULL;
  405. struct smcinvoke_cbobj *cbobj = NULL;
  406. struct smcinvoke_cbobj *obj = NULL;
  407. struct smcinvoke_server_info *server = get_cb_server_locked(srvr_id);
  408. if (!server) {
  409. pr_err("%s, server id : %u not found\n", __func__, srvr_id);
  410. return OBJECT_ERROR_BADOBJ;
  411. }
  412. head = &server->pending_cbobjs;
  413. list_for_each_entry(cbobj, head, list)
  414. if (cbobj->cbobj_id == obj_id) {
  415. kref_get(&cbobj->ref_cnt);
  416. goto out;
  417. }
  418. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  419. if (!obj) {
  420. ret = OBJECT_ERROR_KMEM;
  421. goto out;
  422. }
  423. obj->cbobj_id = obj_id;
  424. kref_init(&obj->ref_cnt);
  425. obj->server = server;
  426. /*
  427. * we are holding server ref in cbobj; we will
  428. * release server ref when cbobj is destroyed
  429. */
  430. release_server = false;
  431. list_add_tail(&obj->list, head);
  432. out:
  433. if (release_server)
  434. kref_put(&server->ref_cnt, destroy_cb_server);
  435. return ret;
  436. }
  437. static int put_pending_cbobj_locked(uint16_t srvr_id, int16_t obj_id)
  438. {
  439. int ret = -EINVAL;
  440. struct smcinvoke_server_info *srvr_info =
  441. get_cb_server_locked(srvr_id);
  442. struct list_head *head = NULL;
  443. struct smcinvoke_cbobj *cbobj = NULL;
  444. if (!srvr_info) {
  445. pr_err("%s, server id : %u not found\n", __func__, srvr_id);
  446. return ret;
  447. }
  448. trace_put_pending_cbobj_locked(srvr_id, obj_id);
  449. head = &srvr_info->pending_cbobjs;
  450. list_for_each_entry(cbobj, head, list)
  451. if (cbobj->cbobj_id == obj_id) {
  452. kref_put(&cbobj->ref_cnt, free_pending_cbobj_locked);
  453. ret = 0;
  454. break;
  455. }
  456. kref_put(&srvr_info->ref_cnt, destroy_cb_server);
  457. return ret;
  458. }
  459. static int release_tzhandle_locked(int32_t tzhandle)
  460. {
  461. if (TZHANDLE_IS_MEM_OBJ(tzhandle))
  462. return release_mem_obj_locked(tzhandle);
  463. else if (TZHANDLE_IS_CB_OBJ(tzhandle))
  464. return put_pending_cbobj_locked(TZHANDLE_GET_SERVER(tzhandle),
  465. TZHANDLE_GET_OBJID(tzhandle));
  466. return OBJECT_ERROR;
  467. }
  468. static void release_tzhandles(const int32_t *tzhandles, size_t len)
  469. {
  470. size_t i;
  471. mutex_lock(&g_smcinvoke_lock);
  472. for (i = 0; i < len; i++)
  473. release_tzhandle_locked(tzhandles[i]);
  474. mutex_unlock(&g_smcinvoke_lock);
  475. }
  476. static void delete_cb_txn(struct kref *kref)
  477. {
  478. struct smcinvoke_cb_txn *cb_txn = container_of(kref,
  479. struct smcinvoke_cb_txn, ref_cnt);
  480. if (OBJECT_OP_METHODID(cb_txn->cb_req->hdr.op) == OBJECT_OP_RELEASE)
  481. release_tzhandle_locked(cb_txn->cb_req->hdr.tzhandle);
  482. kfree(cb_txn->cb_req);
  483. hash_del(&cb_txn->hash);
  484. kfree(cb_txn);
  485. }
  486. static struct smcinvoke_cb_txn *find_cbtxn_locked(
  487. struct smcinvoke_server_info *server,
  488. uint32_t txn_id, int32_t state)
  489. {
  490. int i = 0;
  491. struct smcinvoke_cb_txn *cb_txn = NULL;
  492. /*
  493. * Since HASH_BITS() does not work on pointers, we can't select hash
  494. * table using state and loop over it.
  495. */
  496. if (state == SMCINVOKE_REQ_PLACED) {
  497. /* pick up 1st req */
  498. hash_for_each(server->reqs_table, i, cb_txn, hash) {
  499. kref_get(&cb_txn->ref_cnt);
  500. hash_del(&cb_txn->hash);
  501. return cb_txn;
  502. }
  503. } else if (state == SMCINVOKE_REQ_PROCESSING) {
  504. hash_for_each_possible(
  505. server->responses_table, cb_txn, hash, txn_id) {
  506. if (cb_txn->txn_id == txn_id) {
  507. kref_get(&cb_txn->ref_cnt);
  508. hash_del(&cb_txn->hash);
  509. return cb_txn;
  510. }
  511. }
  512. }
  513. return NULL;
  514. }
  515. /*
  516. * size_add saturates at SIZE_MAX. If integer overflow is detected,
  517. * this function would return SIZE_MAX otherwise normal a+b is returned.
  518. */
  519. static inline size_t size_add(size_t a, size_t b)
  520. {
  521. return (b > (SIZE_MAX - a)) ? SIZE_MAX : a + b;
  522. }
  523. /*
  524. * pad_size is used along with size_align to define a buffer overflow
  525. * protected version of ALIGN
  526. */
  527. static inline size_t pad_size(size_t a, size_t b)
  528. {
  529. return (~a + 1) % b;
  530. }
  531. /*
  532. * size_align saturates at SIZE_MAX. If integer overflow is detected, this
  533. * function would return SIZE_MAX otherwise next aligned size is returned.
  534. */
  535. static inline size_t size_align(size_t a, size_t b)
  536. {
  537. return size_add(a, pad_size(a, b));
  538. }
  539. static uint16_t get_server_id(int cb_server_fd)
  540. {
  541. uint16_t server_id = 0;
  542. struct smcinvoke_file_data *svr_cxt = NULL;
  543. struct file *tmp_filp = fget(cb_server_fd);
  544. if (!tmp_filp || !FILE_IS_REMOTE_OBJ(tmp_filp))
  545. return server_id;
  546. svr_cxt = tmp_filp->private_data;
  547. if (svr_cxt && svr_cxt->context_type == SMCINVOKE_OBJ_TYPE_SERVER)
  548. server_id = svr_cxt->server_id;
  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 == 0) {
  733. /* In case of ret=0/success handle has to be freed in memobj release */
  734. mem_obj->is_smcinvoke_created_shmbridge = true;
  735. } else if (ret == -EEXIST) {
  736. ret = 0;
  737. goto exit;
  738. } else {
  739. pr_err("creation of shm bridge for mem_region_id %d failed ret %d\n",
  740. mem_obj->mem_region_id, ret);
  741. goto exit;
  742. }
  743. trace_smcinvoke_create_bridge(mem_obj->shmbridge_handle, mem_obj->mem_region_id);
  744. exit:
  745. kfree(perms_list);
  746. kfree(vmid_list);
  747. return ret;
  748. }
  749. static int32_t smcinvoke_release_mem_obj_locked(void *buf, size_t buf_len)
  750. {
  751. struct smcinvoke_tzcb_req *msg = buf;
  752. if (msg->hdr.counts != OBJECT_COUNTS_PACK(0, 0, 0, 0)) {
  753. pr_err("Invalid object count in %s\n", __func__);
  754. return OBJECT_ERROR_INVALID;
  755. }
  756. trace_release_mem_obj_locked(msg->hdr.tzhandle, buf_len);
  757. return release_tzhandle_locked(msg->hdr.tzhandle);
  758. }
  759. static int32_t smcinvoke_map_mem_region(void *buf, size_t buf_len)
  760. {
  761. int ret = OBJECT_OK;
  762. struct smcinvoke_tzcb_req *msg = buf;
  763. struct {
  764. uint64_t p_addr;
  765. uint64_t len;
  766. uint32_t perms;
  767. } *ob = NULL;
  768. int32_t *oo = NULL;
  769. struct smcinvoke_mem_obj *mem_obj = NULL;
  770. struct dma_buf_attachment *buf_attach = NULL;
  771. struct sg_table *sgt = NULL;
  772. if (msg->hdr.counts != OBJECT_COUNTS_PACK(0, 1, 1, 1) ||
  773. (buf_len - msg->args[0].b.offset < msg->args[0].b.size)) {
  774. pr_err("Invalid counts received for mapping mem obj\n");
  775. return OBJECT_ERROR_INVALID;
  776. }
  777. /* args[0] = BO, args[1] = OI, args[2] = OO */
  778. ob = buf + msg->args[0].b.offset;
  779. oo = &msg->args[2].handle;
  780. mutex_lock(&g_smcinvoke_lock);
  781. mem_obj = find_mem_obj_locked(TZHANDLE_GET_OBJID(msg->args[1].handle),
  782. SMCINVOKE_MEM_RGN_OBJ);
  783. if (!mem_obj) {
  784. mutex_unlock(&g_smcinvoke_lock);
  785. pr_err("Memory object not found\n");
  786. return OBJECT_ERROR_BADOBJ;
  787. }
  788. if (!mem_obj->p_addr) {
  789. kref_init(&mem_obj->mem_map_obj_ref_cnt);
  790. buf_attach = dma_buf_attach(mem_obj->dma_buf,
  791. &smcinvoke_pdev->dev);
  792. if (IS_ERR(buf_attach)) {
  793. ret = OBJECT_ERROR_KMEM;
  794. pr_err("dma buf attach failed, ret: %d\n", ret);
  795. goto out;
  796. }
  797. mem_obj->buf_attach = buf_attach;
  798. sgt = dma_buf_map_attachment(buf_attach, DMA_BIDIRECTIONAL);
  799. if (IS_ERR(sgt)) {
  800. pr_err("mapping dma buffers failed, ret: %d\n",
  801. PTR_ERR(sgt));
  802. ret = OBJECT_ERROR_KMEM;
  803. goto out;
  804. }
  805. mem_obj->sgt = sgt;
  806. /* contiguous only => nents=1 */
  807. if (sgt->nents != 1) {
  808. ret = OBJECT_ERROR_INVALID;
  809. pr_err("sg enries are not contigous, ret: %d\n", ret);
  810. goto out;
  811. }
  812. mem_obj->p_addr = sg_dma_address(sgt->sgl);
  813. mem_obj->p_addr_len = sgt->sgl->length;
  814. if (!mem_obj->p_addr) {
  815. ret = OBJECT_ERROR_INVALID;
  816. pr_err("invalid physical address, ret: %d\n", ret);
  817. goto out;
  818. }
  819. /* Increase reference count as we are feeding the memobj to
  820. * smcinvoke and unlock the mutex. No need to hold the mutex in
  821. * case of shmbridge creation.
  822. */
  823. kref_get(&mem_obj->mem_map_obj_ref_cnt);
  824. mutex_unlock(&g_smcinvoke_lock);
  825. ret = smcinvoke_create_bridge(mem_obj);
  826. /* Take lock again and decrease the reference count which we
  827. * increased for shmbridge but before proceeding further we
  828. * have to check again if the memobj is still valid or not
  829. * after decreasing the reference.
  830. */
  831. mutex_lock(&g_smcinvoke_lock);
  832. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  833. if (ret) {
  834. ret = OBJECT_ERROR_INVALID;
  835. goto out;
  836. }
  837. if (!find_mem_obj_locked(TZHANDLE_GET_OBJID(msg->args[1].handle),
  838. SMCINVOKE_MEM_RGN_OBJ)) {
  839. mutex_unlock(&g_smcinvoke_lock);
  840. pr_err("Memory object not found\n");
  841. return OBJECT_ERROR_BADOBJ;
  842. }
  843. mem_obj->mem_map_obj_id = next_mem_map_obj_id_locked();
  844. } else {
  845. kref_get(&mem_obj->mem_map_obj_ref_cnt);
  846. }
  847. ob->p_addr = mem_obj->p_addr;
  848. ob->len = mem_obj->p_addr_len;
  849. ob->perms = SMCINVOKE_MEM_PERM_RW;
  850. *oo = TZHANDLE_MAKE_LOCAL(MEM_MAP_SRVR_ID, mem_obj->mem_map_obj_id);
  851. out:
  852. if (ret != OBJECT_OK)
  853. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  854. mutex_unlock(&g_smcinvoke_lock);
  855. return ret;
  856. }
  857. static int32_t smcinvoke_sleep(void *buf, size_t buf_len)
  858. {
  859. struct smcinvoke_tzcb_req *msg = buf;
  860. uint32_t sleepTimeMs_val = 0;
  861. if (msg->hdr.counts != OBJECT_COUNTS_PACK(1, 0, 0, 0) ||
  862. (buf_len - msg->args[0].b.offset < msg->args[0].b.size)) {
  863. pr_err("Invalid counts received for sleeping in hlos\n");
  864. return OBJECT_ERROR_INVALID;
  865. }
  866. /* Time in miliseconds is expected from tz */
  867. sleepTimeMs_val = *((uint32_t *)(buf + msg->args[0].b.offset));
  868. msleep(sleepTimeMs_val);
  869. return OBJECT_OK;
  870. }
  871. static void process_kernel_obj(void *buf, size_t buf_len)
  872. {
  873. struct smcinvoke_tzcb_req *cb_req = buf;
  874. switch (cb_req->hdr.op) {
  875. case OBJECT_OP_MAP_REGION:
  876. cb_req->result = smcinvoke_map_mem_region(buf, buf_len);
  877. break;
  878. case OBJECT_OP_YIELD:
  879. cb_req->result = OBJECT_OK;
  880. break;
  881. case OBJECT_OP_SLEEP:
  882. cb_req->result = smcinvoke_sleep(buf, buf_len);
  883. break;
  884. default:
  885. pr_err(" invalid operation for tz kernel object\n");
  886. cb_req->result = OBJECT_ERROR_INVALID;
  887. break;
  888. }
  889. }
  890. static void process_mem_obj(void *buf, size_t buf_len)
  891. {
  892. struct smcinvoke_tzcb_req *cb_req = buf;
  893. mutex_lock(&g_smcinvoke_lock);
  894. cb_req->result = (cb_req->hdr.op == OBJECT_OP_RELEASE) ?
  895. smcinvoke_release_mem_obj_locked(buf, buf_len) :
  896. OBJECT_ERROR_INVALID;
  897. mutex_unlock(&g_smcinvoke_lock);
  898. }
  899. static int invoke_cmd_handler(int cmd, phys_addr_t in_paddr, size_t in_buf_len,
  900. uint8_t *out_buf, phys_addr_t out_paddr,
  901. size_t out_buf_len, int32_t *result, u64 *response_type,
  902. unsigned int *data, struct qtee_shm *in_shm,
  903. struct qtee_shm *out_shm)
  904. {
  905. int ret = 0;
  906. switch (cmd) {
  907. case SMCINVOKE_INVOKE_CMD_LEGACY:
  908. qtee_shmbridge_flush_shm_buf(in_shm);
  909. qtee_shmbridge_flush_shm_buf(out_shm);
  910. ret = qcom_scm_invoke_smc_legacy(in_paddr, in_buf_len, out_paddr, out_buf_len,
  911. result, response_type, data);
  912. qtee_shmbridge_inv_shm_buf(in_shm);
  913. qtee_shmbridge_inv_shm_buf(out_shm);
  914. break;
  915. case SMCINVOKE_INVOKE_CMD:
  916. ret = qcom_scm_invoke_smc(in_paddr, in_buf_len, out_paddr, out_buf_len,
  917. result, response_type, data);
  918. break;
  919. case SMCINVOKE_CB_RSP_CMD:
  920. ret = qcom_scm_invoke_callback_response(virt_to_phys(out_buf), out_buf_len,
  921. result, response_type, data);
  922. break;
  923. default:
  924. ret = -EINVAL;
  925. break;
  926. }
  927. trace_invoke_cmd_handler(cmd, *response_type, *result, ret);
  928. return ret;
  929. }
  930. /*
  931. * Buf should be aligned to struct smcinvoke_tzcb_req
  932. */
  933. static void process_tzcb_req(void *buf, size_t buf_len, struct file **arr_filp)
  934. {
  935. /* ret is going to TZ. Provide values from OBJECT_ERROR_<> */
  936. int ret = OBJECT_ERROR_DEFUNCT;
  937. int cbobj_retries = 0;
  938. long timeout_jiff;
  939. struct smcinvoke_cb_txn *cb_txn = NULL;
  940. struct smcinvoke_tzcb_req *cb_req = NULL, *tmp_cb_req = NULL;
  941. struct smcinvoke_server_info *srvr_info = NULL;
  942. if (buf_len < sizeof(struct smcinvoke_tzcb_req)) {
  943. pr_err("smaller buffer length : %u\n", buf_len);
  944. return;
  945. }
  946. cb_req = buf;
  947. /* check whether it is to be served by kernel or userspace */
  948. if (TZHANDLE_IS_KERNEL_OBJ(cb_req->hdr.tzhandle)) {
  949. return process_kernel_obj(buf, buf_len);
  950. } else if (TZHANDLE_IS_MEM_OBJ(cb_req->hdr.tzhandle)) {
  951. return process_mem_obj(buf, buf_len);
  952. } else if (!TZHANDLE_IS_CB_OBJ(cb_req->hdr.tzhandle)) {
  953. pr_err("Request object is not a callback object\n");
  954. cb_req->result = OBJECT_ERROR_INVALID;
  955. return;
  956. }
  957. /*
  958. * We need a copy of req that could be sent to server. Otherwise, if
  959. * someone kills invoke caller, buf would go away and server would be
  960. * working on already freed buffer, causing a device crash.
  961. */
  962. tmp_cb_req = kmemdup(buf, buf_len, GFP_KERNEL);
  963. if (!tmp_cb_req) {
  964. /* we need to return error to caller so fill up result */
  965. cb_req->result = OBJECT_ERROR_KMEM;
  966. pr_err("failed to create copy of request, set result: %d\n",
  967. cb_req->result);
  968. return;
  969. }
  970. cb_txn = kzalloc(sizeof(*cb_txn), GFP_KERNEL);
  971. if (!cb_txn) {
  972. cb_req->result = OBJECT_ERROR_KMEM;
  973. pr_err("failed to allocate memory for request, result: %d\n",
  974. cb_req->result);
  975. kfree(tmp_cb_req);
  976. return;
  977. }
  978. /* no need for memcpy as we did kmemdup() above */
  979. cb_req = tmp_cb_req;
  980. trace_process_tzcb_req_handle(cb_req->hdr.tzhandle, cb_req->hdr.op, cb_req->hdr.counts);
  981. cb_txn->state = SMCINVOKE_REQ_PLACED;
  982. cb_txn->cb_req = cb_req;
  983. cb_txn->cb_req_bytes = buf_len;
  984. cb_txn->filp_to_release = arr_filp;
  985. kref_init(&cb_txn->ref_cnt);
  986. mutex_lock(&g_smcinvoke_lock);
  987. ++cb_reqs_inflight;
  988. srvr_info = get_cb_server_locked(
  989. TZHANDLE_GET_SERVER(cb_req->hdr.tzhandle));
  990. if (!srvr_info || srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT) {
  991. /* ret equals Object_ERROR_DEFUNCT, at this point go to out */
  992. if (!srvr_info)
  993. pr_err("server is invalid\n");
  994. else {
  995. pr_err("server is defunct, state= %d tzhandle = %d\n",
  996. srvr_info->state, cb_req->hdr.tzhandle);
  997. }
  998. mutex_unlock(&g_smcinvoke_lock);
  999. goto out;
  1000. }
  1001. cb_txn->txn_id = ++srvr_info->txn_id;
  1002. hash_add(srvr_info->reqs_table, &cb_txn->hash, cb_txn->txn_id);
  1003. mutex_unlock(&g_smcinvoke_lock);
  1004. trace_process_tzcb_req_wait(cb_req->hdr.tzhandle, cbobj_retries, cb_txn->txn_id,
  1005. current->pid, current->tgid, srvr_info->state, srvr_info->server_id,
  1006. cb_reqs_inflight);
  1007. /*
  1008. * we need not worry that server_info will be deleted because as long
  1009. * as this CBObj is served by this server, srvr_info will be valid.
  1010. */
  1011. wake_up_interruptible_all(&srvr_info->req_wait_q);
  1012. /* timeout before 1s otherwise tzbusy would come */
  1013. timeout_jiff = msecs_to_jiffies(1000);
  1014. while (cbobj_retries < CBOBJ_MAX_RETRIES) {
  1015. ret = wait_event_interruptible_timeout(srvr_info->rsp_wait_q,
  1016. (cb_txn->state == SMCINVOKE_REQ_PROCESSED) ||
  1017. (srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT),
  1018. timeout_jiff);
  1019. if (ret == 0) {
  1020. pr_err("CBobj timed out cb-tzhandle:%d, retry:%d, op:%d counts :%d\n",
  1021. cb_req->hdr.tzhandle, cbobj_retries,
  1022. cb_req->hdr.op, cb_req->hdr.counts);
  1023. pr_err("CBobj %d timedout pid %x,tid %x, srvr state=%d, srvr id:%u\n",
  1024. cb_req->hdr.tzhandle, current->pid,
  1025. current->tgid, srvr_info->state,
  1026. srvr_info->server_id);
  1027. } else {
  1028. break;
  1029. }
  1030. cbobj_retries++;
  1031. }
  1032. out:
  1033. /*
  1034. * we could be here because of either:
  1035. * a. Req is PROCESSED
  1036. * b. Server was killed
  1037. * c. Invoke thread is killed
  1038. * sometime invoke thread and server are part of same process.
  1039. */
  1040. mutex_lock(&g_smcinvoke_lock);
  1041. hash_del(&cb_txn->hash);
  1042. if (ret == 0) {
  1043. pr_err("CBObj timed out! No more retries\n");
  1044. cb_req->result = Object_ERROR_TIMEOUT;
  1045. } else if (ret == -ERESTARTSYS) {
  1046. pr_err("wait event interruped, ret: %d\n", ret);
  1047. cb_req->result = OBJECT_ERROR_ABORT;
  1048. } else {
  1049. if (cb_txn->state == SMCINVOKE_REQ_PROCESSED) {
  1050. /*
  1051. * it is possible that server was killed immediately
  1052. * after CB Req was processed but who cares now!
  1053. */
  1054. } else if (!srvr_info ||
  1055. srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT) {
  1056. cb_req->result = OBJECT_ERROR_DEFUNCT;
  1057. pr_err("server invalid, res: %d\n", cb_req->result);
  1058. } else {
  1059. pr_err("%s: unexpected event happened, ret:%d\n", __func__, ret);
  1060. cb_req->result = OBJECT_ERROR_ABORT;
  1061. }
  1062. }
  1063. --cb_reqs_inflight;
  1064. trace_process_tzcb_req_result(cb_req->result, cb_req->hdr.tzhandle, cb_req->hdr.op,
  1065. cb_req->hdr.counts, cb_reqs_inflight);
  1066. memcpy(buf, cb_req, buf_len);
  1067. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1068. if (srvr_info)
  1069. kref_put(&srvr_info->ref_cnt, destroy_cb_server);
  1070. mutex_unlock(&g_smcinvoke_lock);
  1071. }
  1072. static int marshal_out_invoke_req(const uint8_t *buf, uint32_t buf_size,
  1073. struct smcinvoke_cmd_req *req,
  1074. union smcinvoke_arg *args_buf,
  1075. uint32_t context_type)
  1076. {
  1077. int ret = -EINVAL, i = 0;
  1078. int32_t temp_fd = UHANDLE_NULL;
  1079. union smcinvoke_tz_args *tz_args = NULL;
  1080. size_t offset = sizeof(struct smcinvoke_msg_hdr) +
  1081. OBJECT_COUNTS_TOTAL(req->counts) *
  1082. sizeof(union smcinvoke_tz_args);
  1083. if (offset > buf_size)
  1084. goto out;
  1085. tz_args = (union smcinvoke_tz_args *)
  1086. (buf + sizeof(struct smcinvoke_msg_hdr));
  1087. tz_args += OBJECT_COUNTS_NUM_BI(req->counts);
  1088. if (args_buf == NULL)
  1089. return 0;
  1090. FOR_ARGS(i, req->counts, BO) {
  1091. args_buf[i].b.size = tz_args->b.size;
  1092. if ((buf_size - tz_args->b.offset < tz_args->b.size) ||
  1093. tz_args->b.offset > buf_size) {
  1094. pr_err("%s: buffer overflow detected\n", __func__);
  1095. goto out;
  1096. }
  1097. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  1098. if (copy_to_user((void __user *)
  1099. (uintptr_t)(args_buf[i].b.addr),
  1100. (uint8_t *)(buf) + tz_args->b.offset,
  1101. tz_args->b.size)) {
  1102. pr_err("Error %d copying ctxt to user\n", ret);
  1103. goto out;
  1104. }
  1105. } else {
  1106. memcpy((uint8_t *)(args_buf[i].b.addr),
  1107. (uint8_t *)(buf) + tz_args->b.offset,
  1108. tz_args->b.size);
  1109. }
  1110. tz_args++;
  1111. }
  1112. tz_args += OBJECT_COUNTS_NUM_OI(req->counts);
  1113. FOR_ARGS(i, req->counts, OO) {
  1114. /*
  1115. * create a new FD and assign to output object's context.
  1116. * We are passing cb_server_fd from output param in case OO
  1117. * is a CBObj. For CBObj, we have to ensure that it is sent
  1118. * to server who serves it and that info comes from USpace.
  1119. */
  1120. temp_fd = UHANDLE_NULL;
  1121. ret = get_uhandle_from_tzhandle(tz_args->handle,
  1122. TZHANDLE_GET_SERVER(tz_args->handle),
  1123. &temp_fd, NO_LOCK, context_type);
  1124. args_buf[i].o.fd = temp_fd;
  1125. if (ret)
  1126. goto out;
  1127. trace_marshal_out_invoke_req(i, tz_args->handle,
  1128. TZHANDLE_GET_SERVER(tz_args->handle), temp_fd);
  1129. tz_args++;
  1130. }
  1131. ret = 0;
  1132. out:
  1133. return ret;
  1134. }
  1135. static bool is_inbound_req(int val)
  1136. {
  1137. return (val == SMCINVOKE_RESULT_INBOUND_REQ_NEEDED ||
  1138. val == QSEOS_RESULT_INCOMPLETE ||
  1139. val == QSEOS_RESULT_BLOCKED_ON_LISTENER);
  1140. }
  1141. static int prepare_send_scm_msg(const uint8_t *in_buf, phys_addr_t in_paddr,
  1142. size_t in_buf_len,
  1143. uint8_t *out_buf, phys_addr_t out_paddr,
  1144. size_t out_buf_len,
  1145. struct smcinvoke_cmd_req *req,
  1146. union smcinvoke_arg *args_buf,
  1147. bool *tz_acked, uint32_t context_type,
  1148. struct qtee_shm *in_shm, struct qtee_shm *out_shm)
  1149. {
  1150. int ret = 0, cmd, retry_count = 0;
  1151. u64 response_type;
  1152. unsigned int data;
  1153. struct file *arr_filp[OBJECT_COUNTS_MAX_OO] = {NULL};
  1154. *tz_acked = false;
  1155. /* buf size should be page aligned */
  1156. if ((in_buf_len % PAGE_SIZE) != 0 || (out_buf_len % PAGE_SIZE) != 0)
  1157. return -EINVAL;
  1158. cmd = invoke_cmd;
  1159. /*
  1160. * purpose of lock here is to ensure that any CB obj that may be going
  1161. * to user as OO is not released by piggyback message on another invoke
  1162. * request. We should not move this lock to process_invoke_req() because
  1163. * that will either cause deadlock or prevent any other invoke request
  1164. * to come in. We release this lock when either
  1165. * a) TZ requires HLOS action to complete ongoing invoke operation
  1166. * b) Final response to invoke has been marshalled out
  1167. */
  1168. while (1) {
  1169. mutex_lock(&g_smcinvoke_lock);
  1170. do {
  1171. ret = invoke_cmd_handler(cmd, in_paddr, in_buf_len, out_buf,
  1172. out_paddr, out_buf_len, &req->result,
  1173. &response_type, &data, in_shm, out_shm);
  1174. if (ret == -EBUSY) {
  1175. pr_err("Secure side is busy,will retry after 30 ms\n");
  1176. mutex_unlock(&g_smcinvoke_lock);
  1177. msleep(SMCINVOKE_SCM_EBUSY_WAIT_MS);
  1178. mutex_lock(&g_smcinvoke_lock);
  1179. }
  1180. } while ((ret == -EBUSY) &&
  1181. (retry_count++ < SMCINVOKE_SCM_EBUSY_MAX_RETRY));
  1182. if (!ret && !is_inbound_req(response_type)) {
  1183. /* dont marshal if Obj returns an error */
  1184. if (!req->result) {
  1185. if (args_buf != NULL)
  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. mutex_unlock(&g_smcinvoke_lock);
  1193. if (cmd == SMCINVOKE_CB_RSP_CMD)
  1194. release_filp(arr_filp, OBJECT_COUNTS_MAX_OO);
  1195. if (ret || !is_inbound_req(response_type))
  1196. break;
  1197. /* process listener request */
  1198. if (response_type == QSEOS_RESULT_INCOMPLETE ||
  1199. response_type == QSEOS_RESULT_BLOCKED_ON_LISTENER) {
  1200. ret = qseecom_process_listener_from_smcinvoke(
  1201. &req->result, &response_type, &data);
  1202. trace_prepare_send_scm_msg(response_type, req->result);
  1203. if (!req->result &&
  1204. response_type != SMCINVOKE_RESULT_INBOUND_REQ_NEEDED) {
  1205. ret = marshal_out_invoke_req(in_buf,
  1206. in_buf_len, req, args_buf,
  1207. context_type);
  1208. }
  1209. *tz_acked = true;
  1210. }
  1211. /*
  1212. * qseecom does not understand smcinvoke's callback object &&
  1213. * erringly sets ret value as -EINVAL :( We need to handle it.
  1214. */
  1215. if (response_type != SMCINVOKE_RESULT_INBOUND_REQ_NEEDED)
  1216. break;
  1217. if (response_type == SMCINVOKE_RESULT_INBOUND_REQ_NEEDED) {
  1218. trace_status(__func__, "looks like inbnd req reqd");
  1219. process_tzcb_req(out_buf, out_buf_len, arr_filp);
  1220. cmd = SMCINVOKE_CB_RSP_CMD;
  1221. }
  1222. }
  1223. return ret;
  1224. }
  1225. /*
  1226. * SMC expects arguments in following format
  1227. * ---------------------------------------------------------------------------
  1228. * | cxt | op | counts | ptr|size |ptr|size...|ORef|ORef|...| rest of payload |
  1229. * ---------------------------------------------------------------------------
  1230. * cxt: target, op: operation, counts: total arguments
  1231. * offset: offset is from beginning of buffer i.e. cxt
  1232. * size: size is 8 bytes aligned value
  1233. */
  1234. static size_t compute_in_msg_size(const struct smcinvoke_cmd_req *req,
  1235. const union smcinvoke_arg *args_buf)
  1236. {
  1237. uint32_t i = 0;
  1238. size_t total_size = sizeof(struct smcinvoke_msg_hdr) +
  1239. OBJECT_COUNTS_TOTAL(req->counts) *
  1240. sizeof(union smcinvoke_tz_args);
  1241. /* Computed total_size should be 8 bytes aligned from start of buf */
  1242. total_size = ALIGN(total_size, SMCINVOKE_ARGS_ALIGN_SIZE);
  1243. /* each buffer has to be 8 bytes aligned */
  1244. while (i < OBJECT_COUNTS_NUM_buffers(req->counts))
  1245. total_size = size_add(total_size,
  1246. size_align(args_buf[i++].b.size,
  1247. SMCINVOKE_ARGS_ALIGN_SIZE));
  1248. return PAGE_ALIGN(total_size);
  1249. }
  1250. static int marshal_in_invoke_req(const struct smcinvoke_cmd_req *req,
  1251. const union smcinvoke_arg *args_buf, uint32_t tzhandle,
  1252. uint8_t *buf, size_t buf_size, struct file **arr_filp,
  1253. int32_t *tzhandles_to_release, uint32_t context_type)
  1254. {
  1255. int ret = -EINVAL, i = 0, j = 0, k = 0;
  1256. const struct smcinvoke_msg_hdr msg_hdr = {
  1257. tzhandle, req->op, req->counts};
  1258. uint32_t offset = sizeof(struct smcinvoke_msg_hdr) +
  1259. sizeof(union smcinvoke_tz_args) *
  1260. OBJECT_COUNTS_TOTAL(req->counts);
  1261. union smcinvoke_tz_args *tz_args = NULL;
  1262. if (buf_size < offset)
  1263. goto out;
  1264. *(struct smcinvoke_msg_hdr *)buf = msg_hdr;
  1265. tz_args = (union smcinvoke_tz_args *)(buf +
  1266. sizeof(struct smcinvoke_msg_hdr));
  1267. if (args_buf == NULL)
  1268. return 0;
  1269. FOR_ARGS(i, req->counts, BI) {
  1270. offset = size_align(offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  1271. if ((offset > buf_size) ||
  1272. (args_buf[i].b.size > (buf_size - offset)))
  1273. goto out;
  1274. tz_args[i].b.offset = offset;
  1275. tz_args[i].b.size = args_buf[i].b.size;
  1276. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1277. if (copy_from_user(buf + offset,
  1278. (void __user *)(uintptr_t)(args_buf[i].b.addr),
  1279. args_buf[i].b.size))
  1280. goto out;
  1281. } else {
  1282. memcpy(buf + offset, (void *)(args_buf[i].b.addr),
  1283. args_buf[i].b.size);
  1284. }
  1285. offset += args_buf[i].b.size;
  1286. }
  1287. FOR_ARGS(i, req->counts, BO) {
  1288. offset = size_align(offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  1289. if ((offset > buf_size) ||
  1290. (args_buf[i].b.size > (buf_size - offset)))
  1291. goto out;
  1292. tz_args[i].b.offset = offset;
  1293. tz_args[i].b.size = args_buf[i].b.size;
  1294. offset += args_buf[i].b.size;
  1295. }
  1296. FOR_ARGS(i, req->counts, OI) {
  1297. ret = get_tzhandle_from_uhandle(args_buf[i].o.fd,
  1298. args_buf[i].o.cb_server_fd, &arr_filp[j++],
  1299. &(tz_args[i].handle));
  1300. if (ret)
  1301. goto out;
  1302. trace_marshal_in_invoke_req(i, args_buf[i].o.fd,
  1303. args_buf[i].o.cb_server_fd, tz_args[i].handle);
  1304. tzhandles_to_release[k++] = tz_args[i].handle;
  1305. }
  1306. ret = 0;
  1307. out:
  1308. return ret;
  1309. }
  1310. static int marshal_in_tzcb_req(const struct smcinvoke_cb_txn *cb_txn,
  1311. struct smcinvoke_accept *user_req, int srvr_id)
  1312. {
  1313. int ret = 0, i = 0;
  1314. int32_t temp_fd = UHANDLE_NULL;
  1315. union smcinvoke_arg tmp_arg;
  1316. struct smcinvoke_tzcb_req *tzcb_req = cb_txn->cb_req;
  1317. union smcinvoke_tz_args *tz_args = tzcb_req->args;
  1318. size_t tzcb_req_len = cb_txn->cb_req_bytes;
  1319. size_t tz_buf_offset = TZCB_BUF_OFFSET(tzcb_req);
  1320. size_t user_req_buf_offset = sizeof(union smcinvoke_arg) *
  1321. OBJECT_COUNTS_TOTAL(tzcb_req->hdr.counts);
  1322. if (tz_buf_offset > tzcb_req_len) {
  1323. ret = -EINVAL;
  1324. goto out;
  1325. }
  1326. user_req->txn_id = cb_txn->txn_id;
  1327. if (get_uhandle_from_tzhandle(tzcb_req->hdr.tzhandle, srvr_id,
  1328. &user_req->cbobj_id, TAKE_LOCK,
  1329. SMCINVOKE_OBJ_TYPE_TZ_OBJ)) {
  1330. ret = -EINVAL;
  1331. goto out;
  1332. }
  1333. user_req->op = tzcb_req->hdr.op;
  1334. user_req->counts = tzcb_req->hdr.counts;
  1335. user_req->argsize = sizeof(union smcinvoke_arg);
  1336. trace_marshal_in_tzcb_req_handle(tzcb_req->hdr.tzhandle, srvr_id,
  1337. user_req->cbobj_id, user_req->op, user_req->counts);
  1338. FOR_ARGS(i, tzcb_req->hdr.counts, BI) {
  1339. user_req_buf_offset = size_align(user_req_buf_offset,
  1340. SMCINVOKE_ARGS_ALIGN_SIZE);
  1341. tmp_arg.b.size = tz_args[i].b.size;
  1342. if ((tz_args[i].b.offset > tzcb_req_len) ||
  1343. (tz_args[i].b.size > tzcb_req_len - tz_args[i].b.offset) ||
  1344. (user_req_buf_offset > user_req->buf_len) ||
  1345. (tmp_arg.b.size >
  1346. user_req->buf_len - user_req_buf_offset)) {
  1347. ret = -EINVAL;
  1348. pr_err("%s: buffer overflow detected\n", __func__);
  1349. goto out;
  1350. }
  1351. tmp_arg.b.addr = user_req->buf_addr + user_req_buf_offset;
  1352. if (copy_to_user(u64_to_user_ptr
  1353. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1354. &tmp_arg, sizeof(tmp_arg)) ||
  1355. copy_to_user(u64_to_user_ptr(tmp_arg.b.addr),
  1356. (uint8_t *)(tzcb_req) + tz_args[i].b.offset,
  1357. tz_args[i].b.size)) {
  1358. ret = -EFAULT;
  1359. goto out;
  1360. }
  1361. user_req_buf_offset += tmp_arg.b.size;
  1362. }
  1363. FOR_ARGS(i, tzcb_req->hdr.counts, BO) {
  1364. user_req_buf_offset = size_align(user_req_buf_offset,
  1365. SMCINVOKE_ARGS_ALIGN_SIZE);
  1366. tmp_arg.b.size = tz_args[i].b.size;
  1367. if ((user_req_buf_offset > user_req->buf_len) ||
  1368. (tmp_arg.b.size >
  1369. user_req->buf_len - user_req_buf_offset)) {
  1370. ret = -EINVAL;
  1371. pr_err("%s: buffer overflow detected\n", __func__);
  1372. goto out;
  1373. }
  1374. tmp_arg.b.addr = user_req->buf_addr + user_req_buf_offset;
  1375. if (copy_to_user(u64_to_user_ptr
  1376. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1377. &tmp_arg, sizeof(tmp_arg))) {
  1378. ret = -EFAULT;
  1379. goto out;
  1380. }
  1381. user_req_buf_offset += tmp_arg.b.size;
  1382. }
  1383. FOR_ARGS(i, tzcb_req->hdr.counts, OI) {
  1384. /*
  1385. * create a new FD and assign to output object's
  1386. * context
  1387. */
  1388. temp_fd = UHANDLE_NULL;
  1389. ret = get_uhandle_from_tzhandle(tz_args[i].handle, srvr_id,
  1390. &temp_fd, TAKE_LOCK, SMCINVOKE_OBJ_TYPE_TZ_OBJ);
  1391. tmp_arg.o.fd = temp_fd;
  1392. if (ret) {
  1393. ret = -EINVAL;
  1394. goto out;
  1395. }
  1396. if (copy_to_user(u64_to_user_ptr
  1397. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1398. &tmp_arg, sizeof(tmp_arg))) {
  1399. ret = -EFAULT;
  1400. goto out;
  1401. }
  1402. trace_marshal_in_tzcb_req_fd(i, tz_args[i].handle, srvr_id, temp_fd);
  1403. }
  1404. out:
  1405. return ret;
  1406. }
  1407. static int marshal_out_tzcb_req(const struct smcinvoke_accept *user_req,
  1408. struct smcinvoke_cb_txn *cb_txn,
  1409. struct file **arr_filp)
  1410. {
  1411. int ret = -EINVAL, i = 0;
  1412. int32_t tzhandles_to_release[OBJECT_COUNTS_MAX_OO] = {0};
  1413. struct smcinvoke_tzcb_req *tzcb_req = cb_txn->cb_req;
  1414. union smcinvoke_tz_args *tz_args = tzcb_req->args;
  1415. release_tzhandles(&cb_txn->cb_req->hdr.tzhandle, 1);
  1416. tzcb_req->result = user_req->result;
  1417. FOR_ARGS(i, tzcb_req->hdr.counts, BO) {
  1418. union smcinvoke_arg tmp_arg;
  1419. if (copy_from_user((uint8_t *)&tmp_arg, u64_to_user_ptr(
  1420. user_req->buf_addr + i * sizeof(union smcinvoke_arg)),
  1421. sizeof(union smcinvoke_arg))) {
  1422. ret = -EFAULT;
  1423. goto out;
  1424. }
  1425. if (tmp_arg.b.size > tz_args[i].b.size)
  1426. goto out;
  1427. if (copy_from_user((uint8_t *)(tzcb_req) + tz_args[i].b.offset,
  1428. u64_to_user_ptr(tmp_arg.b.addr),
  1429. tmp_arg.b.size)) {
  1430. ret = -EFAULT;
  1431. goto out;
  1432. }
  1433. }
  1434. FOR_ARGS(i, tzcb_req->hdr.counts, OO) {
  1435. union smcinvoke_arg tmp_arg;
  1436. if (copy_from_user((uint8_t *)&tmp_arg, u64_to_user_ptr(
  1437. user_req->buf_addr + i * sizeof(union smcinvoke_arg)),
  1438. sizeof(union smcinvoke_arg))) {
  1439. ret = -EFAULT;
  1440. goto out;
  1441. }
  1442. ret = get_tzhandle_from_uhandle(tmp_arg.o.fd,
  1443. tmp_arg.o.cb_server_fd, &arr_filp[i],
  1444. &(tz_args[i].handle));
  1445. if (ret)
  1446. goto out;
  1447. tzhandles_to_release[i] = tz_args[i].handle;
  1448. trace_marshal_out_tzcb_req(i, tmp_arg.o.fd,
  1449. tmp_arg.o.cb_server_fd, tz_args[i].handle);
  1450. }
  1451. FOR_ARGS(i, tzcb_req->hdr.counts, OI) {
  1452. if (TZHANDLE_IS_CB_OBJ(tz_args[i].handle))
  1453. release_tzhandles(&tz_args[i].handle, 1);
  1454. }
  1455. ret = 0;
  1456. out:
  1457. if (ret)
  1458. release_tzhandles(tzhandles_to_release, OBJECT_COUNTS_MAX_OO);
  1459. return ret;
  1460. }
  1461. static void process_piggyback_data(void *buf, size_t buf_size)
  1462. {
  1463. int i;
  1464. struct smcinvoke_tzcb_req req = {0};
  1465. struct smcinvoke_piggyback_msg *msg = buf;
  1466. int32_t *objs = msg->objs;
  1467. for (i = 0; i < msg->counts; i++) {
  1468. req.hdr.op = msg->op;
  1469. req.hdr.counts = 0; /* release op does not require any args */
  1470. req.hdr.tzhandle = objs[i];
  1471. process_tzcb_req(&req, sizeof(struct smcinvoke_tzcb_req), NULL);
  1472. /* cbobjs_in_flight will be adjusted during CB processing */
  1473. }
  1474. }
  1475. static long process_ack_local_obj(struct file *filp, unsigned int cmd,
  1476. unsigned long arg)
  1477. {
  1478. int ret = -1;
  1479. int32_t local_obj = SMCINVOKE_USERSPACE_OBJ_NULL;
  1480. struct smcinvoke_file_data *filp_data = filp->private_data;
  1481. if (_IOC_SIZE(cmd) != sizeof(int32_t))
  1482. return -EINVAL;
  1483. ret = copy_from_user(&local_obj, (void __user *)(uintptr_t)arg,
  1484. sizeof(int32_t));
  1485. if (ret)
  1486. return -EFAULT;
  1487. mutex_lock(&g_smcinvoke_lock);
  1488. if (UHANDLE_IS_CB_OBJ(local_obj))
  1489. ret = put_pending_cbobj_locked(filp_data->server_id,
  1490. UHANDLE_GET_CB_OBJ(local_obj));
  1491. mutex_unlock(&g_smcinvoke_lock);
  1492. return ret;
  1493. }
  1494. static long process_server_req(struct file *filp, unsigned int cmd,
  1495. unsigned long arg)
  1496. {
  1497. int ret = -1;
  1498. int32_t server_fd = -1;
  1499. struct smcinvoke_server server_req = {0};
  1500. struct smcinvoke_server_info *server_info = NULL;
  1501. if (_IOC_SIZE(cmd) != sizeof(server_req)) {
  1502. pr_err("invalid command size received for server request\n");
  1503. return -EINVAL;
  1504. }
  1505. ret = copy_from_user(&server_req, (void __user *)(uintptr_t)arg,
  1506. sizeof(server_req));
  1507. if (ret) {
  1508. pr_err("copying server request from user failed\n");
  1509. return -EFAULT;
  1510. }
  1511. server_info = kzalloc(sizeof(*server_info), GFP_KERNEL);
  1512. if (!server_info)
  1513. return -ENOMEM;
  1514. kref_init(&server_info->ref_cnt);
  1515. init_waitqueue_head(&server_info->req_wait_q);
  1516. init_waitqueue_head(&server_info->rsp_wait_q);
  1517. server_info->cb_buf_size = server_req.cb_buf_size;
  1518. hash_init(server_info->reqs_table);
  1519. hash_init(server_info->responses_table);
  1520. INIT_LIST_HEAD(&server_info->pending_cbobjs);
  1521. mutex_lock(&g_smcinvoke_lock);
  1522. server_info->server_id = next_cb_server_id_locked();
  1523. hash_add(g_cb_servers, &server_info->hash,
  1524. server_info->server_id);
  1525. if (g_max_cb_buf_size < server_req.cb_buf_size)
  1526. g_max_cb_buf_size = server_req.cb_buf_size;
  1527. mutex_unlock(&g_smcinvoke_lock);
  1528. ret = get_fd_for_obj(SMCINVOKE_OBJ_TYPE_SERVER,
  1529. server_info->server_id, &server_fd);
  1530. if (ret)
  1531. release_cb_server(server_info->server_id);
  1532. return server_fd;
  1533. }
  1534. static long process_accept_req(struct file *filp, unsigned int cmd,
  1535. unsigned long arg)
  1536. {
  1537. int ret = -1;
  1538. struct smcinvoke_file_data *server_obj = filp->private_data;
  1539. struct smcinvoke_accept user_args = {0};
  1540. struct smcinvoke_cb_txn *cb_txn = NULL;
  1541. struct smcinvoke_server_info *server_info = NULL;
  1542. if (_IOC_SIZE(cmd) != sizeof(struct smcinvoke_accept)) {
  1543. pr_err("command size invalid for accept request\n");
  1544. return -EINVAL;
  1545. }
  1546. if (copy_from_user(&user_args, (void __user *)arg,
  1547. sizeof(struct smcinvoke_accept))) {
  1548. pr_err("copying accept request from user failed\n");
  1549. return -EFAULT;
  1550. }
  1551. if (user_args.argsize != sizeof(union smcinvoke_arg)) {
  1552. pr_err("arguments size is invalid for accept thread\n");
  1553. return -EINVAL;
  1554. }
  1555. /* ACCEPT is available only on server obj */
  1556. if (server_obj->context_type != SMCINVOKE_OBJ_TYPE_SERVER) {
  1557. pr_err("invalid object type received for accept req\n");
  1558. return -EPERM;
  1559. }
  1560. mutex_lock(&g_smcinvoke_lock);
  1561. server_info = get_cb_server_locked(server_obj->server_id);
  1562. if (!server_info) {
  1563. pr_err("No matching server with server id : %u found\n",
  1564. server_obj->server_id);
  1565. mutex_unlock(&g_smcinvoke_lock);
  1566. return -EINVAL;
  1567. }
  1568. if (server_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT)
  1569. server_info->state = 0;
  1570. mutex_unlock(&g_smcinvoke_lock);
  1571. /* First check if it has response otherwise wait for req */
  1572. if (user_args.has_resp) {
  1573. trace_process_accept_req_has_response(current->pid, current->tgid);
  1574. mutex_lock(&g_smcinvoke_lock);
  1575. cb_txn = find_cbtxn_locked(server_info, user_args.txn_id,
  1576. SMCINVOKE_REQ_PROCESSING);
  1577. mutex_unlock(&g_smcinvoke_lock);
  1578. /*
  1579. * cb_txn can be null if userspace provides wrong txn id OR
  1580. * invoke thread died while server was processing cb req.
  1581. * if invoke thread dies, it would remove req from Q. So
  1582. * no matching cb_txn would be on Q and hence NULL cb_txn.
  1583. * In this case, we want this thread to come back and start
  1584. * waiting for new cb requests, hence return EAGAIN here
  1585. */
  1586. if (!cb_txn) {
  1587. pr_err("%s txn %d either invalid or removed from Q\n",
  1588. __func__, user_args.txn_id);
  1589. ret = -EAGAIN;
  1590. goto out;
  1591. }
  1592. ret = marshal_out_tzcb_req(&user_args, cb_txn,
  1593. cb_txn->filp_to_release);
  1594. /*
  1595. * if client did not set error and we get error locally,
  1596. * we return local error to TA
  1597. */
  1598. if (ret && cb_txn->cb_req->result == 0)
  1599. cb_txn->cb_req->result = OBJECT_ERROR_UNAVAIL;
  1600. cb_txn->state = SMCINVOKE_REQ_PROCESSED;
  1601. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1602. wake_up(&server_info->rsp_wait_q);
  1603. /*
  1604. * if marshal_out fails, we should let userspace release
  1605. * any ref/obj it created for CB processing
  1606. */
  1607. if (ret && OBJECT_COUNTS_NUM_OO(user_args.counts))
  1608. goto out;
  1609. }
  1610. /*
  1611. * Once response has been delivered, thread will wait for another
  1612. * callback req to process.
  1613. */
  1614. do {
  1615. ret = wait_event_interruptible(server_info->req_wait_q,
  1616. !hash_empty(server_info->reqs_table));
  1617. if (ret) {
  1618. trace_process_accept_req_ret(current->pid, current->tgid, ret);
  1619. /*
  1620. * Ideally, we should destroy server if accept threads
  1621. * are returning due to client being killed or device
  1622. * going down (Shutdown/Reboot) but that would make
  1623. * server_info invalid. Other accept/invoke threads are
  1624. * using server_info and would crash. So dont do that.
  1625. */
  1626. mutex_lock(&g_smcinvoke_lock);
  1627. server_info->state = SMCINVOKE_SERVER_STATE_DEFUNCT;
  1628. mutex_unlock(&g_smcinvoke_lock);
  1629. wake_up_interruptible(&server_info->rsp_wait_q);
  1630. goto out;
  1631. }
  1632. mutex_lock(&g_smcinvoke_lock);
  1633. cb_txn = find_cbtxn_locked(server_info,
  1634. SMCINVOKE_NEXT_AVAILABLE_TXN,
  1635. SMCINVOKE_REQ_PLACED);
  1636. mutex_unlock(&g_smcinvoke_lock);
  1637. if (cb_txn) {
  1638. cb_txn->state = SMCINVOKE_REQ_PROCESSING;
  1639. ret = marshal_in_tzcb_req(cb_txn, &user_args,
  1640. server_obj->server_id);
  1641. if (ret) {
  1642. pr_err("failed to marshal in the callback request\n");
  1643. cb_txn->cb_req->result = OBJECT_ERROR_UNAVAIL;
  1644. cb_txn->state = SMCINVOKE_REQ_PROCESSED;
  1645. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1646. wake_up_interruptible(&server_info->rsp_wait_q);
  1647. continue;
  1648. }
  1649. mutex_lock(&g_smcinvoke_lock);
  1650. hash_add(server_info->responses_table, &cb_txn->hash,
  1651. cb_txn->txn_id);
  1652. kref_put(&cb_txn->ref_cnt, delete_cb_txn);
  1653. mutex_unlock(&g_smcinvoke_lock);
  1654. trace_process_accept_req_placed(current->pid, current->tgid);
  1655. ret = copy_to_user((void __user *)arg, &user_args,
  1656. sizeof(struct smcinvoke_accept));
  1657. }
  1658. } while (!cb_txn);
  1659. out:
  1660. if (server_info)
  1661. kref_put(&server_info->ref_cnt, destroy_cb_server);
  1662. if (ret && ret != -ERESTARTSYS)
  1663. pr_err("accept thread returning with ret: %d\n", ret);
  1664. return ret;
  1665. }
  1666. static long process_invoke_req(struct file *filp, unsigned int cmd,
  1667. unsigned long arg)
  1668. {
  1669. int ret = -1, nr_args = 0;
  1670. struct smcinvoke_cmd_req req = {0};
  1671. void *in_msg = NULL, *out_msg = NULL;
  1672. size_t inmsg_size = 0, outmsg_size = SMCINVOKE_TZ_MIN_BUF_SIZE;
  1673. union smcinvoke_arg *args_buf = NULL;
  1674. struct smcinvoke_file_data *tzobj = filp->private_data;
  1675. struct qtee_shm in_shm = {0}, out_shm = {0};
  1676. /*
  1677. * Hold reference to remote object until invoke op is not
  1678. * completed. Release once invoke is done.
  1679. */
  1680. struct file *filp_to_release[OBJECT_COUNTS_MAX_OO] = {NULL};
  1681. /*
  1682. * If anything goes wrong, release alloted tzhandles for
  1683. * local objs which could be either CBObj or MemObj.
  1684. */
  1685. int32_t tzhandles_to_release[OBJECT_COUNTS_MAX_OO] = {0};
  1686. bool tz_acked = false;
  1687. uint32_t context_type = tzobj->context_type;
  1688. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ &&
  1689. _IOC_SIZE(cmd) != sizeof(req)) {
  1690. pr_err("command size for invoke req is invalid\n");
  1691. return -EINVAL;
  1692. }
  1693. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ &&
  1694. context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1695. pr_err("invalid context_type %d\n", context_type);
  1696. return -EPERM;
  1697. }
  1698. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1699. ret = copy_from_user(&req, (void __user *)arg, sizeof(req));
  1700. if (ret) {
  1701. pr_err("copying invoke req failed\n");
  1702. return -EFAULT;
  1703. }
  1704. } else {
  1705. req = *(struct smcinvoke_cmd_req *)arg;
  1706. }
  1707. if (req.argsize != sizeof(union smcinvoke_arg)) {
  1708. pr_err("arguments size for invoke req is invalid\n");
  1709. return -EINVAL;
  1710. }
  1711. nr_args = OBJECT_COUNTS_NUM_buffers(req.counts) +
  1712. OBJECT_COUNTS_NUM_objects(req.counts);
  1713. if (nr_args) {
  1714. args_buf = kcalloc(nr_args, req.argsize, GFP_KERNEL);
  1715. if (!args_buf)
  1716. return -ENOMEM;
  1717. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  1718. ret = copy_from_user(args_buf,
  1719. u64_to_user_ptr(req.args),
  1720. nr_args * req.argsize);
  1721. if (ret) {
  1722. ret = -EFAULT;
  1723. goto out;
  1724. }
  1725. } else {
  1726. memcpy(args_buf, (void *)(req.args),
  1727. nr_args * req.argsize);
  1728. }
  1729. }
  1730. inmsg_size = compute_in_msg_size(&req, args_buf);
  1731. ret = qtee_shmbridge_allocate_shm(inmsg_size, &in_shm);
  1732. if (ret) {
  1733. ret = -ENOMEM;
  1734. pr_err("shmbridge alloc failed for in msg in invoke req\n");
  1735. goto out;
  1736. }
  1737. in_msg = in_shm.vaddr;
  1738. mutex_lock(&g_smcinvoke_lock);
  1739. outmsg_size = PAGE_ALIGN(g_max_cb_buf_size);
  1740. mutex_unlock(&g_smcinvoke_lock);
  1741. ret = qtee_shmbridge_allocate_shm(outmsg_size, &out_shm);
  1742. if (ret) {
  1743. ret = -ENOMEM;
  1744. pr_err("shmbridge alloc failed for out msg in invoke req\n");
  1745. goto out;
  1746. }
  1747. out_msg = out_shm.vaddr;
  1748. trace_process_invoke_req_tzhandle(tzobj->tzhandle, req.op, req.counts);
  1749. ret = marshal_in_invoke_req(&req, args_buf, tzobj->tzhandle, in_msg,
  1750. inmsg_size, filp_to_release, tzhandles_to_release,
  1751. context_type);
  1752. if (ret) {
  1753. pr_err("failed to marshal in invoke req, ret :%d\n", ret);
  1754. goto out;
  1755. }
  1756. ret = prepare_send_scm_msg(in_msg, in_shm.paddr, inmsg_size,
  1757. out_msg, out_shm.paddr, outmsg_size,
  1758. &req, args_buf, &tz_acked, context_type,
  1759. &in_shm, &out_shm);
  1760. /*
  1761. * If scm_call is success, TZ owns responsibility to release
  1762. * refs for local objs.
  1763. */
  1764. if (!tz_acked) {
  1765. trace_status(__func__, "scm call successful");
  1766. goto out;
  1767. }
  1768. memset(tzhandles_to_release, 0, sizeof(tzhandles_to_release));
  1769. /*
  1770. * if invoke op results in an err, no need to marshal_out and
  1771. * copy args buf to user space
  1772. */
  1773. if (!req.result) {
  1774. /*
  1775. * Dont check ret of marshal_out because there might be a
  1776. * FD for OO which userspace must release even if an error
  1777. * occurs. Releasing FD from user space is much simpler than
  1778. * doing here. ORing of ret is reqd not to miss past error
  1779. */
  1780. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ)
  1781. ret |= copy_to_user(u64_to_user_ptr(req.args),
  1782. args_buf, nr_args * req.argsize);
  1783. else
  1784. memcpy((void *)(req.args), args_buf,
  1785. nr_args * req.argsize);
  1786. }
  1787. /* copy result of invoke op */
  1788. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  1789. ret |= copy_to_user((void __user *)arg, &req, sizeof(req));
  1790. if (ret)
  1791. goto out;
  1792. } else {
  1793. memcpy((void *)arg, (void *)&req, sizeof(req));
  1794. }
  1795. /* Outbuf could be carrying local objs to be released. */
  1796. process_piggyback_data(out_msg, outmsg_size);
  1797. out:
  1798. trace_process_invoke_req_result(ret, req.result, tzobj->tzhandle,
  1799. req.op, req.counts);
  1800. release_filp(filp_to_release, OBJECT_COUNTS_MAX_OO);
  1801. if (ret)
  1802. release_tzhandles(tzhandles_to_release, OBJECT_COUNTS_MAX_OO);
  1803. qtee_shmbridge_free_shm(&in_shm);
  1804. qtee_shmbridge_free_shm(&out_shm);
  1805. kfree(args_buf);
  1806. if (ret)
  1807. pr_err("invoke thread returning with ret = %d\n", ret);
  1808. return ret;
  1809. }
  1810. static long process_log_info(struct file *filp, unsigned int cmd,
  1811. unsigned long arg)
  1812. {
  1813. int ret = 0;
  1814. char buf[SMCINVOKE_LOG_BUF_SIZE];
  1815. struct smcinvoke_file_data *tzobj = filp->private_data;
  1816. ret = copy_from_user(buf, (void __user *)arg, SMCINVOKE_LOG_BUF_SIZE);
  1817. if (ret) {
  1818. pr_err("logging HLOS info copy failed\n");
  1819. return -EFAULT;
  1820. }
  1821. buf[SMCINVOKE_LOG_BUF_SIZE - 1] = '\0';
  1822. trace_process_log_info(buf, tzobj->context_type, tzobj->tzhandle);
  1823. return ret;
  1824. }
  1825. static long smcinvoke_ioctl(struct file *filp, unsigned int cmd,
  1826. unsigned long arg)
  1827. {
  1828. long ret = 0;
  1829. switch (cmd) {
  1830. case SMCINVOKE_IOCTL_INVOKE_REQ:
  1831. ret = process_invoke_req(filp, cmd, arg);
  1832. break;
  1833. case SMCINVOKE_IOCTL_ACCEPT_REQ:
  1834. ret = process_accept_req(filp, cmd, arg);
  1835. break;
  1836. case SMCINVOKE_IOCTL_SERVER_REQ:
  1837. ret = process_server_req(filp, cmd, arg);
  1838. break;
  1839. case SMCINVOKE_IOCTL_ACK_LOCAL_OBJ:
  1840. ret = process_ack_local_obj(filp, cmd, arg);
  1841. break;
  1842. case SMCINVOKE_IOCTL_LOG:
  1843. ret = process_log_info(filp, cmd, arg);
  1844. break;
  1845. default:
  1846. ret = -ENOIOCTLCMD;
  1847. break;
  1848. }
  1849. trace_smcinvoke_ioctl(cmd, ret);
  1850. return ret;
  1851. }
  1852. int get_root_fd(int *root_fd)
  1853. {
  1854. if (!root_fd)
  1855. return -EINVAL;
  1856. else
  1857. return get_fd_for_obj(SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL,
  1858. SMCINVOKE_TZ_ROOT_OBJ, root_fd);
  1859. }
  1860. int process_invoke_request_from_kernel_client(int fd,
  1861. struct smcinvoke_cmd_req *req)
  1862. {
  1863. struct file *filp = NULL;
  1864. int ret = 0;
  1865. if (!req) {
  1866. pr_err("NULL req\n");
  1867. return -EINVAL;
  1868. }
  1869. filp = fget(fd);
  1870. if (!filp) {
  1871. pr_err("Invalid fd %d\n", fd);
  1872. return -EINVAL;
  1873. }
  1874. ret = process_invoke_req(filp, 0, (uintptr_t)req);
  1875. fput(filp);
  1876. trace_process_invoke_request_from_kernel_client(fd, filp, file_count(filp));
  1877. return ret;
  1878. }
  1879. char *firmware_request_from_smcinvoke(const char *appname, size_t *fw_size, struct qtee_shm *shm)
  1880. {
  1881. int rc = 0;
  1882. const struct firmware *fw_entry = NULL, *fw_entry00 = NULL, *fw_entrylast = NULL;
  1883. char fw_name[MAX_APP_NAME_SIZE] = "\0";
  1884. int num_images = 0, phi = 0;
  1885. unsigned char app_arch = 0;
  1886. u8 *img_data_ptr = NULL;
  1887. size_t bufferOffset = 0, phdr_table_offset = 0;
  1888. size_t *offset = NULL;
  1889. Elf32_Phdr phdr32;
  1890. Elf64_Phdr phdr64;
  1891. struct elf32_hdr *ehdr = NULL;
  1892. struct elf64_hdr *ehdr64 = NULL;
  1893. /* load b00*/
  1894. snprintf(fw_name, sizeof(fw_name), "%s.b00", appname);
  1895. rc = firmware_request_nowarn(&fw_entry00, fw_name, class_dev);
  1896. if (rc) {
  1897. pr_err("Load %s failed, ret:%d\n", fw_name, rc);
  1898. return NULL;
  1899. }
  1900. app_arch = *(unsigned char *)(fw_entry00->data + EI_CLASS);
  1901. /*Get the offsets for split images header*/
  1902. if (app_arch == ELFCLASS32) {
  1903. ehdr = (struct elf32_hdr *)fw_entry00->data;
  1904. num_images = ehdr->e_phnum;
  1905. offset = kcalloc(num_images, sizeof(size_t), GFP_KERNEL);
  1906. if (offset == NULL)
  1907. goto release_fw_entry00;
  1908. phdr_table_offset = (size_t) ehdr->e_phoff;
  1909. for (phi = 1; phi < num_images; ++phi) {
  1910. bufferOffset = phdr_table_offset + phi * sizeof(Elf32_Phdr);
  1911. phdr32 = *(Elf32_Phdr *)(fw_entry00->data + bufferOffset);
  1912. offset[phi] = (size_t)phdr32.p_offset;
  1913. }
  1914. } else if (app_arch == ELFCLASS64) {
  1915. ehdr64 = (struct elf64_hdr *)fw_entry00->data;
  1916. num_images = ehdr64->e_phnum;
  1917. offset = kcalloc(num_images, sizeof(size_t), GFP_KERNEL);
  1918. if (offset == NULL)
  1919. goto release_fw_entry00;
  1920. phdr_table_offset = (size_t) ehdr64->e_phoff;
  1921. for (phi = 1; phi < num_images; ++phi) {
  1922. bufferOffset = phdr_table_offset + phi * sizeof(Elf64_Phdr);
  1923. phdr64 = *(Elf64_Phdr *)(fw_entry00->data + bufferOffset);
  1924. offset[phi] = (size_t)phdr64.p_offset;
  1925. }
  1926. } else {
  1927. pr_err("QSEE %s app, arch %u is not supported\n", appname, app_arch);
  1928. goto release_fw_entry00;
  1929. }
  1930. /*Find the size of last split bin image*/
  1931. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, num_images-1);
  1932. rc = firmware_request_nowarn(&fw_entrylast, fw_name, class_dev);
  1933. if (rc) {
  1934. pr_err("Failed to locate blob %s\n", fw_name);
  1935. goto release_fw_entry00;
  1936. }
  1937. /*Total size of image will be the offset of last image + the size of last split image*/
  1938. *fw_size = fw_entrylast->size + offset[num_images-1];
  1939. /*Allocate memory for the buffer that will hold the split image*/
  1940. rc = qtee_shmbridge_allocate_shm((*fw_size), shm);
  1941. if (rc) {
  1942. pr_err("smbridge alloc failed for size: %zu\n", *fw_size);
  1943. goto release_fw_entrylast;
  1944. }
  1945. img_data_ptr = shm->vaddr;
  1946. /*
  1947. * Copy contents of split bins to the buffer
  1948. */
  1949. memcpy(img_data_ptr, fw_entry00->data, fw_entry00->size);
  1950. for (phi = 1; phi < num_images-1; phi++) {
  1951. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, phi);
  1952. rc = firmware_request_nowarn(&fw_entry, fw_name, class_dev);
  1953. if (rc) {
  1954. pr_err("Failed to locate blob %s\n", fw_name);
  1955. qtee_shmbridge_free_shm(shm);
  1956. img_data_ptr = NULL;
  1957. goto release_fw_entrylast;
  1958. }
  1959. memcpy(img_data_ptr + offset[phi], fw_entry->data, fw_entry->size);
  1960. release_firmware(fw_entry);
  1961. fw_entry = NULL;
  1962. }
  1963. memcpy(img_data_ptr + offset[phi], fw_entrylast->data, fw_entrylast->size);
  1964. release_fw_entrylast:
  1965. release_firmware(fw_entrylast);
  1966. release_fw_entry00:
  1967. release_firmware(fw_entry00);
  1968. kfree(offset);
  1969. return img_data_ptr;
  1970. }
  1971. EXPORT_SYMBOL(firmware_request_from_smcinvoke);
  1972. static int smcinvoke_open(struct inode *nodp, struct file *filp)
  1973. {
  1974. struct smcinvoke_file_data *tzcxt = NULL;
  1975. tzcxt = kzalloc(sizeof(*tzcxt), GFP_KERNEL);
  1976. if (!tzcxt)
  1977. return -ENOMEM;
  1978. tzcxt->tzhandle = SMCINVOKE_TZ_ROOT_OBJ;
  1979. tzcxt->context_type = SMCINVOKE_OBJ_TYPE_TZ_OBJ;
  1980. filp->private_data = tzcxt;
  1981. return 0;
  1982. }
  1983. static int release_cb_server(uint16_t server_id)
  1984. {
  1985. struct smcinvoke_server_info *server = NULL;
  1986. mutex_lock(&g_smcinvoke_lock);
  1987. server = find_cb_server_locked(server_id);
  1988. if (server)
  1989. kref_put(&server->ref_cnt, destroy_cb_server);
  1990. mutex_unlock(&g_smcinvoke_lock);
  1991. return 0;
  1992. }
  1993. int smcinvoke_release_filp(struct file *filp)
  1994. {
  1995. int ret = 0;
  1996. bool release_handles;
  1997. uint8_t *in_buf = NULL;
  1998. uint8_t *out_buf = NULL;
  1999. struct smcinvoke_msg_hdr hdr = {0};
  2000. struct smcinvoke_file_data *file_data = filp->private_data;
  2001. struct smcinvoke_cmd_req req = {0};
  2002. uint32_t tzhandle = 0;
  2003. struct qtee_shm in_shm = {0}, out_shm = {0};
  2004. trace_smcinvoke_release_filp(current->files, filp,
  2005. file_count(filp), file_data->context_type);
  2006. if (file_data->context_type == SMCINVOKE_OBJ_TYPE_SERVER) {
  2007. ret = release_cb_server(file_data->server_id);
  2008. goto out;
  2009. }
  2010. tzhandle = file_data->tzhandle;
  2011. /* Root object is special in sense it is indestructible */
  2012. if (!tzhandle || tzhandle == SMCINVOKE_TZ_ROOT_OBJ)
  2013. goto out;
  2014. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &in_shm);
  2015. if (ret) {
  2016. ret = -ENOMEM;
  2017. pr_err("shmbridge alloc failed for in msg in release\n");
  2018. goto out;
  2019. }
  2020. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &out_shm);
  2021. if (ret) {
  2022. ret = -ENOMEM;
  2023. pr_err("shmbridge alloc failed for out msg in release\n");
  2024. goto out;
  2025. }
  2026. in_buf = in_shm.vaddr;
  2027. out_buf = out_shm.vaddr;
  2028. hdr.tzhandle = tzhandle;
  2029. hdr.op = OBJECT_OP_RELEASE;
  2030. hdr.counts = 0;
  2031. *(struct smcinvoke_msg_hdr *)in_buf = hdr;
  2032. ret = prepare_send_scm_msg(in_buf, in_shm.paddr,
  2033. SMCINVOKE_TZ_MIN_BUF_SIZE, out_buf, out_shm.paddr,
  2034. SMCINVOKE_TZ_MIN_BUF_SIZE, &req, NULL, &release_handles,
  2035. file_data->context_type, &in_shm, &out_shm);
  2036. process_piggyback_data(out_buf, SMCINVOKE_TZ_MIN_BUF_SIZE);
  2037. out:
  2038. kfree(filp->private_data);
  2039. filp->private_data = NULL;
  2040. qtee_shmbridge_free_shm(&in_shm);
  2041. qtee_shmbridge_free_shm(&out_shm);
  2042. return ret;
  2043. }
  2044. int smcinvoke_release_from_kernel_client(int fd)
  2045. {
  2046. struct file *filp = NULL;
  2047. /* use fget() to get filp, but this will increase file ref_cnt to 1,
  2048. * then decrease file ref_cnt to 0 with fput().
  2049. */
  2050. filp = fget(fd);
  2051. if (!filp) {
  2052. pr_err("invalid fd %d to release\n", fd);
  2053. return -EINVAL;
  2054. }
  2055. trace_smcinvoke_release_from_kernel_client(current->files, filp,
  2056. file_count(filp));
  2057. /* free filp, notify TZ to release object */
  2058. smcinvoke_release_filp(filp);
  2059. fput(filp);
  2060. return 0;
  2061. }
  2062. static int smcinvoke_release(struct inode *nodp, struct file *filp)
  2063. {
  2064. trace_smcinvoke_release(current->files, filp, file_count(filp),
  2065. filp->private_data);
  2066. if (filp->private_data)
  2067. return smcinvoke_release_filp(filp);
  2068. else
  2069. return 0;
  2070. }
  2071. static int smcinvoke_probe(struct platform_device *pdev)
  2072. {
  2073. unsigned int baseminor = 0;
  2074. unsigned int count = 1;
  2075. int rc = 0;
  2076. rc = alloc_chrdev_region(&smcinvoke_device_no, baseminor, count,
  2077. SMCINVOKE_DEV);
  2078. if (rc < 0) {
  2079. pr_err("chrdev_region failed %d for %s\n", rc, SMCINVOKE_DEV);
  2080. return rc;
  2081. }
  2082. driver_class = class_create(THIS_MODULE, SMCINVOKE_DEV);
  2083. if (IS_ERR(driver_class)) {
  2084. rc = -ENOMEM;
  2085. pr_err("class_create failed %d\n", rc);
  2086. goto exit_unreg_chrdev_region;
  2087. }
  2088. class_dev = device_create(driver_class, NULL, smcinvoke_device_no,
  2089. NULL, SMCINVOKE_DEV);
  2090. if (!class_dev) {
  2091. pr_err("class_device_create failed %d\n", rc);
  2092. rc = -ENOMEM;
  2093. goto exit_destroy_class;
  2094. }
  2095. cdev_init(&smcinvoke_cdev, &g_smcinvoke_fops);
  2096. smcinvoke_cdev.owner = THIS_MODULE;
  2097. rc = cdev_add(&smcinvoke_cdev, MKDEV(MAJOR(smcinvoke_device_no), 0),
  2098. count);
  2099. if (rc < 0) {
  2100. pr_err("cdev_add failed %d for %s\n", rc, SMCINVOKE_DEV);
  2101. goto exit_destroy_device;
  2102. }
  2103. smcinvoke_pdev = pdev;
  2104. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2105. if (rc) {
  2106. pr_err("dma_set_mask_and_coherent failed %d\n", rc);
  2107. goto exit_destroy_device;
  2108. }
  2109. legacy_smc_call = of_property_read_bool((&pdev->dev)->of_node,
  2110. "qcom,support-legacy_smc");
  2111. invoke_cmd = legacy_smc_call ? SMCINVOKE_INVOKE_CMD_LEGACY : SMCINVOKE_INVOKE_CMD;
  2112. return 0;
  2113. exit_destroy_device:
  2114. device_destroy(driver_class, smcinvoke_device_no);
  2115. exit_destroy_class:
  2116. class_destroy(driver_class);
  2117. exit_unreg_chrdev_region:
  2118. unregister_chrdev_region(smcinvoke_device_no, count);
  2119. return rc;
  2120. }
  2121. static int smcinvoke_remove(struct platform_device *pdev)
  2122. {
  2123. int count = 1;
  2124. cdev_del(&smcinvoke_cdev);
  2125. device_destroy(driver_class, smcinvoke_device_no);
  2126. class_destroy(driver_class);
  2127. unregister_chrdev_region(smcinvoke_device_no, count);
  2128. return 0;
  2129. }
  2130. static int __maybe_unused smcinvoke_suspend(struct platform_device *pdev,
  2131. pm_message_t state)
  2132. {
  2133. int ret = 0;
  2134. mutex_lock(&g_smcinvoke_lock);
  2135. if (cb_reqs_inflight) {
  2136. pr_err("Failed to suspend smcinvoke driver\n");
  2137. ret = -EIO;
  2138. }
  2139. mutex_unlock(&g_smcinvoke_lock);
  2140. return ret;
  2141. }
  2142. static int __maybe_unused smcinvoke_resume(struct platform_device *pdev)
  2143. {
  2144. return 0;
  2145. }
  2146. static const struct of_device_id smcinvoke_match[] = {
  2147. {
  2148. .compatible = "qcom,smcinvoke",
  2149. },
  2150. {},
  2151. };
  2152. static struct platform_driver smcinvoke_plat_driver = {
  2153. .probe = smcinvoke_probe,
  2154. .remove = smcinvoke_remove,
  2155. .suspend = smcinvoke_suspend,
  2156. .resume = smcinvoke_resume,
  2157. .driver = {
  2158. .name = "smcinvoke",
  2159. .of_match_table = smcinvoke_match,
  2160. },
  2161. };
  2162. static int smcinvoke_init(void)
  2163. {
  2164. return platform_driver_register(&smcinvoke_plat_driver);
  2165. }
  2166. static void smcinvoke_exit(void)
  2167. {
  2168. platform_driver_unregister(&smcinvoke_plat_driver);
  2169. }
  2170. module_init(smcinvoke_init);
  2171. module_exit(smcinvoke_exit);
  2172. MODULE_LICENSE("GPL v2");
  2173. MODULE_DESCRIPTION("SMC Invoke driver");
  2174. MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);