ipc_kmsg.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989 Carnegie Mellon University.
  4. * Copyright (c) 1993,1994 The University of Utah and
  5. * the Computer Systems Laboratory (CSL).
  6. * All rights reserved.
  7. *
  8. * Permission to use, copy, modify and distribute this software and its
  9. * documentation is hereby granted, provided that both the copyright
  10. * notice and this permission notice appear in all copies of the
  11. * software, derivative works or modified versions, and any portions
  12. * thereof, and that both notices appear in supporting documentation.
  13. *
  14. * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
  15. * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
  16. * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
  17. * THIS SOFTWARE.
  18. *
  19. * Carnegie Mellon requests users of this software to return to
  20. *
  21. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  22. * School of Computer Science
  23. * Carnegie Mellon University
  24. * Pittsburgh PA 15213-3890
  25. *
  26. * any improvements or extensions that they make and grant Carnegie Mellon
  27. * the rights to redistribute these changes.
  28. */
  29. /*
  30. * File: ipc/ipc_kmsg.c
  31. * Author: Rich Draves
  32. * Date: 1989
  33. *
  34. * Operations on kernel messages.
  35. */
  36. #include <glue/gnulinux.h>
  37. #include <kern/printf.h>
  38. #include <string.h>
  39. #include <mach/boolean.h>
  40. #include <mach/kern_return.h>
  41. #include <mach/message.h>
  42. #include <mach/port.h>
  43. #include <kern/assert.h>
  44. #include <kern/kalloc.h>
  45. #include <vm/vm_map.h>
  46. #include <vm/vm_object.h>
  47. #include <vm/vm_kern.h>
  48. #include <vm/vm_user.h>
  49. #include <ipc/port.h>
  50. #include <ipc/ipc_entry.h>
  51. #include <ipc/ipc_kmsg.h>
  52. #include <ipc/ipc_thread.h>
  53. #include <ipc/ipc_marequest.h>
  54. #include <ipc/ipc_notify.h>
  55. #include <ipc/ipc_object.h>
  56. #include <ipc/ipc_space.h>
  57. #include <ipc/ipc_port.h>
  58. #include <ipc/ipc_right.h>
  59. #include <ipc/ipc_machdep.h>
  60. #include <glue/locore.h>
  61. int ipc_reverse_lookup(void*,void*);
  62. void net_kmsg_put(void* mem){}
  63. #if MACH_KDB
  64. #include <ddb/db_output.h>
  65. #include <ipc/ipc_print.h>
  66. #endif
  67. #define is_misaligned(x) ( ((vm_offset_t)(x)) & (sizeof(vm_offset_t)-1) )
  68. #define ptr_align(x) \
  69. ( ( ((vm_offset_t)(x)) + (sizeof(vm_offset_t)-1) ) & ~(sizeof(vm_offset_t)-1) )
  70. ipc_kmsg_t ipc_kmsg_cache[NCPUS];
  71. /*
  72. * Routine: ipc_kmsg_enqueue
  73. * Purpose:
  74. * Enqueue a kmsg.
  75. */
  76. void
  77. ipc_kmsg_enqueue(
  78. ipc_kmsg_queue_t queue,
  79. ipc_kmsg_t kmsg)
  80. {
  81. ipc_kmsg_enqueue_macro(queue, kmsg);
  82. }
  83. /*
  84. * Routine: ipc_kmsg_dequeue
  85. * Purpose:
  86. * Dequeue and return a kmsg.
  87. */
  88. ipc_kmsg_t
  89. ipc_kmsg_dequeue(
  90. ipc_kmsg_queue_t queue)
  91. {
  92. ipc_kmsg_t first;
  93. first = ipc_kmsg_queue_first(queue);
  94. if (first != IKM_NULL)
  95. ipc_kmsg_rmqueue_first_macro(queue, first);
  96. return first;
  97. }
  98. /*
  99. * Routine: ipc_kmsg_rmqueue
  100. * Purpose:
  101. * Pull a kmsg out of a queue.
  102. */
  103. void
  104. ipc_kmsg_rmqueue(
  105. ipc_kmsg_queue_t queue,
  106. ipc_kmsg_t kmsg)
  107. {
  108. ipc_kmsg_t next, prev;
  109. assert(queue->ikmq_base != IKM_NULL);
  110. next = kmsg->ikm_next;
  111. prev = kmsg->ikm_prev;
  112. if (next == kmsg) {
  113. assert(prev == kmsg);
  114. assert(queue->ikmq_base == kmsg);
  115. queue->ikmq_base = IKM_NULL;
  116. } else {
  117. if (queue->ikmq_base == kmsg)
  118. queue->ikmq_base = next;
  119. next->ikm_prev = prev;
  120. prev->ikm_next = next;
  121. }
  122. ikm_mark_bogus (kmsg);
  123. }
  124. /*
  125. * Routine: ipc_kmsg_queue_next
  126. * Purpose:
  127. * Return the kmsg following the given kmsg.
  128. * (Or IKM_NULL if it is the last one in the queue.)
  129. */
  130. ipc_kmsg_t
  131. ipc_kmsg_queue_next(
  132. ipc_kmsg_queue_t queue,
  133. ipc_kmsg_t kmsg)
  134. {
  135. ipc_kmsg_t next;
  136. assert(queue->ikmq_base != IKM_NULL);
  137. next = kmsg->ikm_next;
  138. if (queue->ikmq_base == next)
  139. next = IKM_NULL;
  140. return next;
  141. }
  142. /*
  143. * Routine: ipc_kmsg_destroy
  144. * Purpose:
  145. * Destroys a kernel message. Releases all rights,
  146. * references, and memory held by the message.
  147. * Frees the message.
  148. * Conditions:
  149. * No locks held.
  150. */
  151. void
  152. ipc_kmsg_destroy(
  153. ipc_kmsg_t kmsg)
  154. {
  155. ipc_kmsg_queue_t queue;
  156. boolean_t empty;
  157. /*
  158. * ipc_kmsg_clean can cause more messages to be destroyed.
  159. * Curtail recursion by queueing messages. If a message
  160. * is already queued, then this is a recursive call.
  161. */
  162. queue = &current_thread()->ith_messages;
  163. empty = ipc_kmsg_queue_empty(queue);
  164. ipc_kmsg_enqueue(queue, kmsg);
  165. if (empty) {
  166. /* must leave kmsg in queue while cleaning it */
  167. while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) {
  168. ipc_kmsg_clean(kmsg);
  169. ipc_kmsg_rmqueue(queue, kmsg);
  170. ikm_free(kmsg);
  171. }
  172. }
  173. }
  174. /*
  175. * Routine: ipc_kmsg_clean_body
  176. * Purpose:
  177. * Cleans the body of a kernel message.
  178. * Releases all rights, references, and memory.
  179. *
  180. * The last type/data pair might stretch past eaddr.
  181. * (See the usage in ipc_kmsg_copyout.)
  182. * Conditions:
  183. * No locks held.
  184. */
  185. void
  186. ipc_kmsg_clean_body(
  187. vm_offset_t saddr,
  188. vm_offset_t eaddr)
  189. {
  190. while (saddr < eaddr) {
  191. mach_msg_type_long_t *type;
  192. mach_msg_type_name_t name;
  193. mach_msg_type_size_t size;
  194. mach_msg_type_number_t number;
  195. boolean_t is_inline, is_port;
  196. vm_size_t length;
  197. type = (mach_msg_type_long_t *) saddr;
  198. is_inline = ((mach_msg_type_t*)type)->msgt_inline;
  199. if (((mach_msg_type_t*)type)->msgt_longform) {
  200. /* This must be aligned */
  201. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  202. (is_misaligned(type))) {
  203. saddr = ptr_align(saddr);
  204. continue;
  205. }
  206. name = type->msgtl_name;
  207. size = type->msgtl_size;
  208. number = type->msgtl_number;
  209. saddr += sizeof(mach_msg_type_long_t);
  210. } else {
  211. name = ((mach_msg_type_t*)type)->msgt_name;
  212. size = ((mach_msg_type_t*)type)->msgt_size;
  213. number = ((mach_msg_type_t*)type)->msgt_number;
  214. saddr += sizeof(mach_msg_type_t);
  215. }
  216. /* padding (ptrs and ports) ? */
  217. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  218. ((size >> 3) == sizeof(natural_t)))
  219. saddr = ptr_align(saddr);
  220. /* calculate length of data in bytes, rounding up */
  221. length = ((number * size) + 7) >> 3;
  222. is_port = MACH_MSG_TYPE_PORT_ANY(name);
  223. if (is_port) {
  224. ipc_object_t *objects;
  225. mach_msg_type_number_t i;
  226. if (is_inline) {
  227. objects = (ipc_object_t *) saddr;
  228. /* sanity check */
  229. while (eaddr < (vm_offset_t)&objects[number]) number--;
  230. } else {
  231. objects = (ipc_object_t *)
  232. * (vm_offset_t *) saddr;
  233. }
  234. /* destroy port rights carried in the message */
  235. for (i = 0; i < number; i++) {
  236. ipc_object_t object = objects[i];
  237. if (!IO_VALID(object))
  238. continue;
  239. ipc_object_destroy(object, name);
  240. }
  241. }
  242. if (is_inline) {
  243. /* inline data sizes round up to int boundaries */
  244. saddr += (length + 3) &~ 3;
  245. } else {
  246. vm_offset_t data = * (vm_offset_t *) saddr;
  247. /* destroy memory carried in the message */
  248. if (length == 0)
  249. assert(data == 0);
  250. else if (is_port)
  251. kfree(data, length);
  252. else
  253. vm_map_copy_discard((vm_map_copy_t) data);
  254. saddr += sizeof(vm_offset_t);
  255. }
  256. }
  257. }
  258. /*
  259. * Routine: ipc_kmsg_clean
  260. * Purpose:
  261. * Cleans a kernel message. Releases all rights,
  262. * references, and memory held by the message.
  263. * Conditions:
  264. * No locks held.
  265. */
  266. void
  267. ipc_kmsg_clean(ipc_kmsg_t kmsg)
  268. {
  269. ipc_marequest_t marequest;
  270. ipc_object_t object;
  271. mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits;
  272. marequest = kmsg->ikm_marequest;
  273. if (marequest != IMAR_NULL)
  274. ipc_marequest_destroy(marequest);
  275. object = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
  276. if (IO_VALID(object))
  277. ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits));
  278. object = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
  279. if (IO_VALID(object))
  280. ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
  281. if (mbits & MACH_MSGH_BITS_COMPLEX) {
  282. vm_offset_t saddr, eaddr;
  283. saddr = (vm_offset_t) (&kmsg->ikm_header + 1);
  284. eaddr = (vm_offset_t) &kmsg->ikm_header +
  285. kmsg->ikm_header.msgh_size;
  286. ipc_kmsg_clean_body(saddr, eaddr);
  287. }
  288. }
  289. /*
  290. * Routine: ipc_kmsg_clean_partial
  291. * Purpose:
  292. * Cleans a partially-acquired kernel message.
  293. * eaddr is the address of the type specification
  294. * in the body of the message that contained the error.
  295. * If dolast, the memory and port rights in this last
  296. * type spec are also cleaned. In that case, number
  297. * specifies the number of port rights to clean.
  298. * Conditions:
  299. * Nothing locked.
  300. */
  301. void
  302. ipc_kmsg_clean_partial(
  303. ipc_kmsg_t kmsg,
  304. vm_offset_t eaddr,
  305. boolean_t dolast,
  306. mach_msg_type_number_t number)
  307. {
  308. ipc_object_t object;
  309. mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits;
  310. vm_offset_t saddr;
  311. assert(kmsg->ikm_marequest == IMAR_NULL);
  312. object = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
  313. assert(IO_VALID(object));
  314. ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits));
  315. object = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
  316. if (IO_VALID(object))
  317. ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
  318. saddr = (vm_offset_t) (&kmsg->ikm_header + 1);
  319. ipc_kmsg_clean_body(saddr, eaddr);
  320. if (dolast) {
  321. mach_msg_type_long_t *type;
  322. mach_msg_type_name_t name;
  323. mach_msg_type_size_t size;
  324. mach_msg_type_number_t rnumber;
  325. boolean_t is_inline, is_port;
  326. vm_size_t length;
  327. xxx: type = (mach_msg_type_long_t *) eaddr;
  328. is_inline = ((mach_msg_type_t*)type)->msgt_inline;
  329. if (((mach_msg_type_t*)type)->msgt_longform) {
  330. /* This must be aligned */
  331. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  332. (is_misaligned(type))) {
  333. eaddr = ptr_align(eaddr);
  334. goto xxx;
  335. }
  336. name = type->msgtl_name;
  337. size = type->msgtl_size;
  338. rnumber = type->msgtl_number;
  339. eaddr += sizeof(mach_msg_type_long_t);
  340. } else {
  341. name = ((mach_msg_type_t*)type)->msgt_name;
  342. size = ((mach_msg_type_t*)type)->msgt_size;
  343. rnumber = ((mach_msg_type_t*)type)->msgt_number;
  344. eaddr += sizeof(mach_msg_type_t);
  345. }
  346. /* padding (ptrs and ports) ? */
  347. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  348. ((size >> 3) == sizeof(natural_t)))
  349. eaddr = ptr_align(eaddr);
  350. /* calculate length of data in bytes, rounding up */
  351. length = ((rnumber * size) + 7) >> 3;
  352. is_port = MACH_MSG_TYPE_PORT_ANY(name);
  353. if (is_port) {
  354. ipc_object_t *objects;
  355. mach_msg_type_number_t i;
  356. objects = (ipc_object_t *)
  357. (is_inline ? eaddr : * (vm_offset_t *) eaddr);
  358. /* destroy port rights carried in the message */
  359. for (i = 0; i < number; i++) {
  360. ipc_object_t obj = objects[i];
  361. if (!IO_VALID(obj))
  362. continue;
  363. ipc_object_destroy(obj, name);
  364. }
  365. }
  366. if (!is_inline) {
  367. vm_offset_t data = * (vm_offset_t *) eaddr;
  368. /* destroy memory carried in the message */
  369. if (length == 0)
  370. assert(data == 0);
  371. else if (is_port)
  372. kfree(data, length);
  373. else
  374. vm_map_copy_discard((vm_map_copy_t) data);
  375. }
  376. }
  377. }
  378. /*
  379. * Routine: ipc_kmsg_free
  380. * Purpose:
  381. * Free a kernel message buffer.
  382. * Conditions:
  383. * Nothing locked.
  384. */
  385. void
  386. ipc_kmsg_free(ipc_kmsg_t kmsg)
  387. {
  388. vm_size_t size = kmsg->ikm_size;
  389. switch (size) {
  390. case IKM_SIZE_NETWORK:
  391. /* return it to the network code */
  392. net_kmsg_put(kmsg);
  393. break;
  394. default:
  395. kfree((vm_offset_t) kmsg, size);
  396. break;
  397. }
  398. }
  399. /*
  400. * Routine: ipc_kmsg_get
  401. * Purpose:
  402. * Allocates a kernel message buffer.
  403. * Copies a user message to the message buffer.
  404. * Conditions:
  405. * Nothing locked.
  406. * Returns:
  407. * MACH_MSG_SUCCESS Acquired a message buffer.
  408. * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
  409. * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
  410. * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
  411. * MACH_SEND_INVALID_DATA Couldn't copy message data.
  412. */
  413. mach_msg_return_t
  414. ipc_kmsg_get(
  415. mach_msg_header_t *msg,
  416. mach_msg_size_t size,
  417. ipc_kmsg_t *kmsgp)
  418. {
  419. ipc_kmsg_t kmsg;
  420. if ((size < sizeof(mach_msg_header_t)) || (size & 3))
  421. return MACH_SEND_MSG_TOO_SMALL;
  422. if (size <= IKM_SAVED_MSG_SIZE) {
  423. kmsg = ikm_cache();
  424. if (kmsg != IKM_NULL) {
  425. ikm_cache() = IKM_NULL;
  426. ikm_check_initialized(kmsg, IKM_SAVED_KMSG_SIZE);
  427. } else {
  428. kmsg = ikm_alloc(IKM_SAVED_MSG_SIZE);
  429. if (kmsg == IKM_NULL)
  430. return MACH_SEND_NO_BUFFER;
  431. ikm_init(kmsg, IKM_SAVED_MSG_SIZE);
  432. }
  433. } else {
  434. kmsg = ikm_alloc(size);
  435. if (kmsg == IKM_NULL)
  436. return MACH_SEND_NO_BUFFER;
  437. ikm_init(kmsg, size);
  438. }
  439. if (copyinmsg(msg, &kmsg->ikm_header, size)) {
  440. ikm_free(kmsg);
  441. return MACH_SEND_INVALID_DATA;
  442. }
  443. kmsg->ikm_header.msgh_size = size;
  444. *kmsgp = kmsg;
  445. return MACH_MSG_SUCCESS;
  446. }
  447. /*
  448. * Routine: ipc_kmsg_get_from_kernel
  449. * Purpose:
  450. * Allocates a kernel message buffer.
  451. * Copies a kernel message to the message buffer.
  452. * Only resource errors are allowed.
  453. * Conditions:
  454. * Nothing locked.
  455. * Returns:
  456. * MACH_MSG_SUCCESS Acquired a message buffer.
  457. * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
  458. */
  459. extern mach_msg_return_t
  460. ipc_kmsg_get_from_kernel(
  461. mach_msg_header_t *msg,
  462. mach_msg_size_t size,
  463. ipc_kmsg_t *kmsgp)
  464. {
  465. ipc_kmsg_t kmsg;
  466. assert(size >= sizeof(mach_msg_header_t));
  467. assert((size & 3) == 0);
  468. kmsg = ikm_alloc(size);
  469. if (kmsg == IKM_NULL)
  470. return MACH_SEND_NO_BUFFER;
  471. ikm_init(kmsg, size);
  472. memcpy(&kmsg->ikm_header, msg, size);
  473. kmsg->ikm_header.msgh_size = size;
  474. *kmsgp = kmsg;
  475. return MACH_MSG_SUCCESS;
  476. }
  477. /*
  478. * Routine: ipc_kmsg_put
  479. * Purpose:
  480. * Copies a message buffer to a user message.
  481. * Copies only the specified number of bytes.
  482. * Frees the message buffer.
  483. * Conditions:
  484. * Nothing locked. The message buffer must have clean
  485. * header (ikm_marequest) fields.
  486. * Returns:
  487. * MACH_MSG_SUCCESS Copied data out of message buffer.
  488. * MACH_RCV_INVALID_DATA Couldn't copy to user message.
  489. */
  490. mach_msg_return_t
  491. ipc_kmsg_put(
  492. mach_msg_header_t *msg,
  493. ipc_kmsg_t kmsg,
  494. mach_msg_size_t size)
  495. {
  496. mach_msg_return_t mr;
  497. printk(KERN_ERR,"ipc_kmsg_put is broken\n");
  498. return -1;//BUG here -- assert fails
  499. ikm_check_initialized(kmsg, kmsg->ikm_size);
  500. if (copyoutmsg(&kmsg->ikm_header, msg, size))
  501. mr = MACH_RCV_INVALID_DATA;
  502. else
  503. mr = MACH_MSG_SUCCESS;
  504. if ((kmsg->ikm_size == IKM_SAVED_KMSG_SIZE) &&
  505. (ikm_cache() == IKM_NULL))
  506. ikm_cache() = kmsg;
  507. else
  508. ikm_free(kmsg);
  509. return mr;
  510. }
  511. /*
  512. * Routine: ipc_kmsg_put_to_kernel
  513. * Purpose:
  514. * Copies a message buffer to a kernel message.
  515. * Frees the message buffer.
  516. * No errors allowed.
  517. * Conditions:
  518. * Nothing locked.
  519. */
  520. void
  521. ipc_kmsg_put_to_kernel(
  522. mach_msg_header_t *msg,
  523. ipc_kmsg_t kmsg,
  524. mach_msg_size_t size)
  525. {
  526. #if DIPC
  527. assert(!KMSG_IN_DIPC(kmsg));
  528. #endif /* DIPC */
  529. memcpy(msg, &kmsg->ikm_header, size);
  530. ikm_free(kmsg);
  531. }
  532. /*
  533. * Routine: ipc_kmsg_copyin_header
  534. * Purpose:
  535. * "Copy-in" port rights in the header of a message.
  536. * Operates atomically; if it doesn't succeed the
  537. * message header and the space are left untouched.
  538. * If it does succeed the remote/local port fields
  539. * contain object pointers instead of port names,
  540. * and the bits field is updated. The destination port
  541. * will be a valid port pointer.
  542. *
  543. * The notify argument implements the MACH_SEND_CANCEL option.
  544. * If it is not MACH_PORT_NULL, it should name a receive right.
  545. * If the processing of the destination port would generate
  546. * a port-deleted notification (because the right for the
  547. * destination port is destroyed and it had a request for
  548. * a dead-name notification registered), and the port-deleted
  549. * notification would be sent to the named receive right,
  550. * then it isn't sent and the send-once right for the notify
  551. * port is quietly destroyed.
  552. * Conditions:
  553. * Nothing locked.
  554. * Returns:
  555. * MACH_MSG_SUCCESS Successful copyin.
  556. * MACH_SEND_INVALID_HEADER
  557. * Illegal value in the message header bits.
  558. * MACH_SEND_INVALID_DEST The space is dead.
  559. * MACH_SEND_INVALID_NOTIFY
  560. * Notify is non-null and doesn't name a receive right.
  561. * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
  562. * MACH_SEND_INVALID_DEST Can't copyin destination port.
  563. * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
  564. * MACH_SEND_INVALID_REPLY Can't copyin reply port.
  565. * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
  566. */
  567. mach_msg_return_t
  568. ipc_kmsg_copyin_header(
  569. mach_msg_header_t *msg,
  570. ipc_space_t space,
  571. mach_port_t notify)
  572. {
  573. mach_msg_bits_t mbits = msg->msgh_bits &~ MACH_MSGH_BITS_CIRCULAR;
  574. mach_port_t dest_name = msg->msgh_remote_port;
  575. mach_port_t reply_name = msg->msgh_local_port;
  576. kern_return_t kr;
  577. #ifndef MIGRATING_THREADS
  578. /* first check for common cases */
  579. if (notify == MACH_PORT_NULL) switch (MACH_MSGH_BITS_PORTS(mbits)) {
  580. case MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0): {
  581. ipc_entry_t entry;
  582. ipc_entry_bits_t bits;
  583. ipc_port_t dest_port;
  584. /* sending an asynchronous message */
  585. if (reply_name != MACH_PORT_NULL)
  586. break;
  587. is_read_lock(space);
  588. if (!space->is_active)
  589. goto abort_async;
  590. entry = ipc_entry_lookup (space, dest_name);
  591. if (entry == IE_NULL)
  592. goto abort_async;
  593. bits = entry->ie_bits;
  594. /* check type bits */
  595. if (IE_BITS_TYPE (bits) != MACH_PORT_TYPE_SEND)
  596. goto abort_async;
  597. /* optimized ipc_right_copyin */
  598. assert(IE_BITS_UREFS(bits) > 0);
  599. dest_port = (ipc_port_t) entry->ie_object;
  600. assert(dest_port != IP_NULL);
  601. ip_lock(dest_port);
  602. /* can unlock space now without compromising atomicity */
  603. is_read_unlock(space);
  604. if (!ip_active(dest_port)) {
  605. ip_unlock(dest_port);
  606. break;
  607. }
  608. assert(dest_port->ip_srights > 0);
  609. dest_port->ip_srights++;
  610. ip_reference(dest_port);
  611. ip_unlock(dest_port);
  612. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  613. MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0));
  614. msg->msgh_remote_port = (mach_port_t) dest_port;
  615. return MACH_MSG_SUCCESS;
  616. abort_async:
  617. is_read_unlock(space);
  618. break;
  619. }
  620. case MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND,
  621. MACH_MSG_TYPE_MAKE_SEND_ONCE): {
  622. ipc_entry_t entry;
  623. ipc_entry_bits_t bits;
  624. ipc_port_t dest_port, reply_port;
  625. /* sending a request message */
  626. is_read_lock(space);
  627. if (!space->is_active)
  628. goto abort_request;
  629. entry = ipc_entry_lookup (space, dest_name);
  630. if (entry == IE_NULL)
  631. goto abort_request;
  632. bits = entry->ie_bits;
  633. /* check type bits */
  634. if (IE_BITS_TYPE (bits) != MACH_PORT_TYPE_SEND)
  635. goto abort_request;
  636. assert(IE_BITS_UREFS(bits) > 0);
  637. dest_port = (ipc_port_t) entry->ie_object;
  638. assert(dest_port != IP_NULL);
  639. entry = ipc_entry_lookup (space, reply_name);
  640. if (entry == IE_NULL)
  641. goto abort_request;
  642. bits = entry->ie_bits;
  643. /* check type bits */
  644. if (IE_BITS_TYPE (bits) != MACH_PORT_TYPE_RECEIVE)
  645. goto abort_request;
  646. reply_port = (ipc_port_t) entry->ie_object;
  647. assert(reply_port != IP_NULL);
  648. /*
  649. * To do an atomic copyin, need simultaneous
  650. * locks on both ports and the space. If
  651. * dest_port == reply_port, and simple locking is
  652. * enabled, then we will abort. Otherwise it's
  653. * OK to unlock twice.
  654. */
  655. ip_lock(dest_port);
  656. if (!ip_active(dest_port) || !ip_lock_try(reply_port)) {
  657. ip_unlock(dest_port);
  658. goto abort_request;
  659. }
  660. /* can unlock space now without compromising atomicity */
  661. is_read_unlock(space);
  662. assert(dest_port->ip_srights > 0);
  663. dest_port->ip_srights++;
  664. ip_reference(dest_port);
  665. ip_unlock(dest_port);
  666. assert(ip_active(reply_port));
  667. assert(reply_port->ip_receiver_name == reply_name);
  668. assert(reply_port->ip_receiver == space);
  669. reply_port->ip_sorights++;
  670. ip_reference(reply_port);
  671. ip_unlock(reply_port);
  672. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  673. MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND,
  674. MACH_MSG_TYPE_PORT_SEND_ONCE));
  675. msg->msgh_remote_port = (mach_port_t) dest_port;
  676. msg->msgh_local_port = (mach_port_t) reply_port;
  677. return MACH_MSG_SUCCESS;
  678. abort_request:
  679. is_read_unlock(space);
  680. break;
  681. }
  682. case MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0): {
  683. ipc_entry_t entry;
  684. ipc_entry_bits_t bits;
  685. ipc_port_t dest_port;
  686. /* sending a reply message */
  687. if (reply_name != MACH_PORT_NULL)
  688. break;
  689. is_write_lock(space);
  690. if (!space->is_active)
  691. goto abort_reply;
  692. entry = ipc_entry_lookup (space, dest_name);
  693. if (entry == IE_NULL)
  694. goto abort_reply;
  695. bits = entry->ie_bits;
  696. /* check and type bits */
  697. if (IE_BITS_TYPE (bits) != MACH_PORT_TYPE_SEND_ONCE)
  698. goto abort_reply;
  699. /* optimized ipc_right_copyin */
  700. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE);
  701. assert(IE_BITS_UREFS(bits) == 1);
  702. assert((bits & IE_BITS_MAREQUEST) == 0);
  703. if (entry->ie_request != 0)
  704. goto abort_reply;
  705. dest_port = (ipc_port_t) entry->ie_object;
  706. assert(dest_port != IP_NULL);
  707. ip_lock(dest_port);
  708. if (!ip_active(dest_port)) {
  709. ip_unlock(dest_port);
  710. goto abort_reply;
  711. }
  712. assert(dest_port->ip_sorights > 0);
  713. ip_unlock(dest_port);
  714. entry->ie_object = IO_NULL;
  715. ipc_entry_dealloc (space, dest_name, entry);
  716. is_write_unlock(space);
  717. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  718. MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE,
  719. 0));
  720. msg->msgh_remote_port = (mach_port_t) dest_port;
  721. return MACH_MSG_SUCCESS;
  722. abort_reply:
  723. is_write_unlock(space);
  724. break;
  725. }
  726. default:
  727. /* don't bother optimizing */
  728. break;
  729. }
  730. #endif /* MIGRATING_THREADS */
  731. {
  732. mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
  733. mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
  734. ipc_object_t dest_port, reply_port;
  735. ipc_port_t dest_soright, reply_soright;
  736. ipc_port_t notify_port = 0; /* '=0' to quiet gcc warnings */
  737. if (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type))
  738. return MACH_SEND_INVALID_HEADER;
  739. if ((reply_type == 0) ?
  740. (reply_name != MACH_PORT_NULL) :
  741. !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type))
  742. return MACH_SEND_INVALID_HEADER;
  743. is_write_lock(space);
  744. if (!space->is_active)
  745. goto invalid_dest;
  746. if (notify != MACH_PORT_NULL) {
  747. ipc_entry_t entry;
  748. if (((entry = ipc_entry_lookup(space, notify)) == IE_NULL) ||
  749. ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0)) {
  750. is_write_unlock(space);
  751. return MACH_SEND_INVALID_NOTIFY;
  752. }
  753. notify_port = (ipc_port_t) entry->ie_object;
  754. }
  755. if (dest_name == reply_name) {
  756. ipc_entry_t entry;
  757. mach_port_t name = dest_name;
  758. /*
  759. * Destination and reply ports are the same!
  760. * This is a little tedious to make atomic, because
  761. * there are 25 combinations of dest_type/reply_type.
  762. * However, most are easy. If either is move-sonce,
  763. * then there must be an error. If either are
  764. * make-send or make-sonce, then we must be looking
  765. * at a receive right so the port can't die.
  766. * The hard cases are the combinations of
  767. * copy-send and make-send.
  768. */
  769. entry = ipc_entry_lookup(space, name);
  770. if (entry == IE_NULL)
  771. goto invalid_dest;
  772. assert(reply_type != 0); /* because name not null */
  773. if (!ipc_right_copyin_check(space, name, entry, reply_type))
  774. goto invalid_reply;
  775. if ((dest_type == MACH_MSG_TYPE_MOVE_SEND_ONCE) ||
  776. (reply_type == MACH_MSG_TYPE_MOVE_SEND_ONCE)) {
  777. /*
  778. * Why must there be an error? To get a valid
  779. * destination, this entry must name a live
  780. * port (not a dead name or dead port). However
  781. * a successful move-sonce will destroy a
  782. * live entry. Therefore the other copyin,
  783. * whatever it is, would fail. We've already
  784. * checked for reply port errors above,
  785. * so report a destination error.
  786. */
  787. goto invalid_dest;
  788. } else if ((dest_type == MACH_MSG_TYPE_MAKE_SEND) ||
  789. (dest_type == MACH_MSG_TYPE_MAKE_SEND_ONCE) ||
  790. (reply_type == MACH_MSG_TYPE_MAKE_SEND) ||
  791. (reply_type == MACH_MSG_TYPE_MAKE_SEND_ONCE)) {
  792. kr = ipc_right_copyin(space, name, entry,
  793. dest_type, FALSE,
  794. &dest_port, &dest_soright);
  795. if (kr != KERN_SUCCESS)
  796. goto invalid_dest;
  797. /*
  798. * Either dest or reply needs a receive right.
  799. * We know the receive right is there, because
  800. * of the copyin_check and copyin calls. Hence
  801. * the port is not in danger of dying. If dest
  802. * used the receive right, then the right needed
  803. * by reply (and verified by copyin_check) will
  804. * still be there.
  805. */
  806. assert(IO_VALID(dest_port));
  807. assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
  808. assert(dest_soright == IP_NULL);
  809. kr = ipc_right_copyin(space, name, entry,
  810. reply_type, TRUE,
  811. &reply_port, &reply_soright);
  812. assert(kr == KERN_SUCCESS);
  813. assert(reply_port == dest_port);
  814. assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
  815. assert(reply_soright == IP_NULL);
  816. } else if ((dest_type == MACH_MSG_TYPE_COPY_SEND) &&
  817. (reply_type == MACH_MSG_TYPE_COPY_SEND)) {
  818. /*
  819. * To make this atomic, just do one copy-send,
  820. * and dup the send right we get out.
  821. */
  822. kr = ipc_right_copyin(space, name, entry,
  823. dest_type, FALSE,
  824. &dest_port, &dest_soright);
  825. if (kr != KERN_SUCCESS)
  826. goto invalid_dest;
  827. assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
  828. assert(dest_soright == IP_NULL);
  829. /*
  830. * It's OK if the port we got is dead now,
  831. * so reply_port is IP_DEAD, because the msg
  832. * won't go anywhere anyway.
  833. */
  834. reply_port = (ipc_object_t)
  835. ipc_port_copy_send((ipc_port_t) dest_port);
  836. reply_soright = IP_NULL;
  837. } else if ((dest_type == MACH_MSG_TYPE_MOVE_SEND) &&
  838. (reply_type == MACH_MSG_TYPE_MOVE_SEND)) {
  839. /*
  840. * This is an easy case. Just use our
  841. * handy-dandy special-purpose copyin call
  842. * to get two send rights for the price of one.
  843. */
  844. kr = ipc_right_copyin_two(space, name, entry,
  845. &dest_port, &dest_soright);
  846. if (kr != KERN_SUCCESS)
  847. goto invalid_dest;
  848. /* the entry might need to be deallocated */
  849. if (IE_BITS_TYPE(entry->ie_bits)
  850. == MACH_PORT_TYPE_NONE)
  851. ipc_entry_dealloc(space, name, entry);
  852. reply_port = dest_port;
  853. reply_soright = IP_NULL;
  854. } else {
  855. ipc_port_t soright;
  856. assert(((dest_type == MACH_MSG_TYPE_COPY_SEND) &&
  857. (reply_type == MACH_MSG_TYPE_MOVE_SEND)) ||
  858. ((dest_type == MACH_MSG_TYPE_MOVE_SEND) &&
  859. (reply_type == MACH_MSG_TYPE_COPY_SEND)));
  860. /*
  861. * To make this atomic, just do a move-send,
  862. * and dup the send right we get out.
  863. */
  864. kr = ipc_right_copyin(space, name, entry,
  865. MACH_MSG_TYPE_MOVE_SEND, FALSE,
  866. &dest_port, &soright);
  867. if (kr != KERN_SUCCESS)
  868. goto invalid_dest;
  869. /* the entry might need to be deallocated */
  870. if (IE_BITS_TYPE(entry->ie_bits)
  871. == MACH_PORT_TYPE_NONE)
  872. ipc_entry_dealloc(space, name, entry);
  873. /*
  874. * It's OK if the port we got is dead now,
  875. * so reply_port is IP_DEAD, because the msg
  876. * won't go anywhere anyway.
  877. */
  878. reply_port = (ipc_object_t)
  879. ipc_port_copy_send((ipc_port_t) dest_port);
  880. if (dest_type == MACH_MSG_TYPE_MOVE_SEND) {
  881. dest_soright = soright;
  882. reply_soright = IP_NULL;
  883. } else {
  884. dest_soright = IP_NULL;
  885. reply_soright = soright;
  886. }
  887. }
  888. } else if (!MACH_PORT_VALID(reply_name)) {
  889. ipc_entry_t entry;
  890. /*
  891. * No reply port! This is an easy case
  892. * to make atomic. Just copyin the destination.
  893. */
  894. entry = ipc_entry_lookup(space, dest_name);
  895. if (entry == IE_NULL)
  896. goto invalid_dest;
  897. kr = ipc_right_copyin(space, dest_name, entry,
  898. dest_type, FALSE,
  899. &dest_port, &dest_soright);
  900. if (kr != KERN_SUCCESS)
  901. goto invalid_dest;
  902. /* the entry might need to be deallocated */
  903. if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
  904. ipc_entry_dealloc(space, dest_name, entry);
  905. reply_port = (ipc_object_t) reply_name;
  906. reply_soright = IP_NULL;
  907. } else {
  908. ipc_entry_t dest_entry, reply_entry;
  909. ipc_port_t saved_reply;
  910. /*
  911. * This is the tough case to make atomic.
  912. * The difficult problem is serializing with port death.
  913. * At the time we copyin dest_port, it must be alive.
  914. * If reply_port is alive when we copyin it, then
  915. * we are OK, because we serialize before the death
  916. * of both ports. Assume reply_port is dead at copyin.
  917. * Then if dest_port dies/died after reply_port died,
  918. * we are OK, because we serialize between the death
  919. * of the two ports. So the bad case is when dest_port
  920. * dies after its copyin, reply_port dies before its
  921. * copyin, and dest_port dies before reply_port. Then
  922. * the copyins operated as if dest_port was alive
  923. * and reply_port was dead, which shouldn't have happened
  924. * because they died in the other order.
  925. *
  926. * We handle the bad case by undoing the copyins
  927. * (which is only possible because the ports are dead)
  928. * and failing with MACH_SEND_INVALID_DEST, serializing
  929. * after the death of the ports.
  930. *
  931. * Note that it is easy for a user task to tell if
  932. * a copyin happened before or after a port died.
  933. * For example, suppose both dest and reply are
  934. * send-once rights (types are both move-sonce) and
  935. * both rights have dead-name requests registered.
  936. * If a port dies before copyin, a dead-name notification
  937. * is generated and the dead name's urefs are incremented,
  938. * and if the copyin happens first, a port-deleted
  939. * notification is generated.
  940. *
  941. * Note that although the entries are different,
  942. * dest_port and reply_port might still be the same.
  943. */
  944. dest_entry = ipc_entry_lookup(space, dest_name);
  945. if (dest_entry == IE_NULL)
  946. goto invalid_dest;
  947. reply_entry = ipc_entry_lookup(space, reply_name);
  948. if (reply_entry == IE_NULL)
  949. goto invalid_reply;
  950. assert(dest_entry != reply_entry); /* names are not equal */
  951. assert(reply_type != 0); /* because reply_name not null */
  952. if (!ipc_right_copyin_check(space, reply_name, reply_entry,
  953. reply_type))
  954. goto invalid_reply;
  955. kr = ipc_right_copyin(space, dest_name, dest_entry,
  956. dest_type, FALSE,
  957. &dest_port, &dest_soright);
  958. if (kr != KERN_SUCCESS)
  959. goto invalid_dest;
  960. assert(IO_VALID(dest_port));
  961. saved_reply = (ipc_port_t) reply_entry->ie_object;
  962. /* might be IP_NULL, if this is a dead name */
  963. if (saved_reply != IP_NULL)
  964. ipc_port_reference(saved_reply);
  965. kr = ipc_right_copyin(space, reply_name, reply_entry,
  966. reply_type, TRUE,
  967. &reply_port, &reply_soright);
  968. assert(kr == KERN_SUCCESS);
  969. if ((saved_reply != IP_NULL) && (reply_port == IO_DEAD)) {
  970. ipc_port_t dest = (ipc_port_t) dest_port;
  971. ipc_port_timestamp_t timestamp;
  972. boolean_t must_undo;
  973. /*
  974. * The reply port died before copyin.
  975. * Check if dest port died before reply.
  976. */
  977. ip_lock(saved_reply);
  978. assert(!ip_active(saved_reply));
  979. timestamp = saved_reply->ip_timestamp;
  980. ip_unlock(saved_reply);
  981. ip_lock(dest);
  982. must_undo = (!ip_active(dest) &&
  983. IP_TIMESTAMP_ORDER(dest->ip_timestamp,
  984. timestamp));
  985. ip_unlock(dest);
  986. if (must_undo) {
  987. /*
  988. * Our worst nightmares are realized.
  989. * Both destination and reply ports
  990. * are dead, but in the wrong order,
  991. * so we must undo the copyins and
  992. * possibly generate a dead-name notif.
  993. */
  994. ipc_right_copyin_undo(
  995. space, dest_name, dest_entry,
  996. dest_type, dest_port,
  997. dest_soright);
  998. /* dest_entry may be deallocated now */
  999. ipc_right_copyin_undo(
  1000. space, reply_name, reply_entry,
  1001. reply_type, reply_port,
  1002. reply_soright);
  1003. /* reply_entry may be deallocated now */
  1004. is_write_unlock(space);
  1005. if (dest_soright != IP_NULL)
  1006. ipc_notify_dead_name(dest_soright,
  1007. dest_name);
  1008. assert(reply_soright == IP_NULL);
  1009. ipc_port_release(saved_reply);
  1010. return MACH_SEND_INVALID_DEST;
  1011. }
  1012. }
  1013. /* the entries might need to be deallocated */
  1014. if (IE_BITS_TYPE(reply_entry->ie_bits) == MACH_PORT_TYPE_NONE)
  1015. ipc_entry_dealloc(space, reply_name, reply_entry);
  1016. if (IE_BITS_TYPE(dest_entry->ie_bits) == MACH_PORT_TYPE_NONE)
  1017. ipc_entry_dealloc(space, dest_name, dest_entry);
  1018. if (saved_reply != IP_NULL)
  1019. ipc_port_release(saved_reply);
  1020. }
  1021. /*
  1022. * At this point, dest_port, reply_port,
  1023. * dest_soright, reply_soright are all initialized.
  1024. * Any defunct entries have been deallocated.
  1025. * The space is still write-locked, and we need to
  1026. * make the MACH_SEND_CANCEL check. The notify_port pointer
  1027. * is still usable, because the copyin code above won't ever
  1028. * deallocate a receive right, so its entry still exists
  1029. * and holds a ref. Note notify_port might even equal
  1030. * dest_port or reply_port.
  1031. */
  1032. if ((notify != MACH_PORT_NULL) &&
  1033. (dest_soright == notify_port)) {
  1034. ipc_port_release_sonce(dest_soright);
  1035. dest_soright = IP_NULL;
  1036. }
  1037. is_write_unlock(space);
  1038. if (dest_soright != IP_NULL)
  1039. ipc_notify_port_deleted(dest_soright, dest_name);
  1040. if (reply_soright != IP_NULL)
  1041. ipc_notify_port_deleted(reply_soright, reply_name);
  1042. dest_type = ipc_object_copyin_type(dest_type);
  1043. reply_type = ipc_object_copyin_type(reply_type);
  1044. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1045. MACH_MSGH_BITS(dest_type, reply_type));
  1046. msg->msgh_remote_port = (mach_port_t) dest_port;
  1047. msg->msgh_local_port = (mach_port_t) reply_port;
  1048. }
  1049. return MACH_MSG_SUCCESS;
  1050. invalid_dest:
  1051. is_write_unlock(space);
  1052. return MACH_SEND_INVALID_DEST;
  1053. invalid_reply:
  1054. is_write_unlock(space);
  1055. return MACH_SEND_INVALID_REPLY;
  1056. }
  1057. mach_msg_return_t
  1058. ipc_kmsg_copyin_body(
  1059. ipc_kmsg_t kmsg,
  1060. ipc_space_t space,
  1061. vm_map_t map)
  1062. {
  1063. ipc_object_t dest;
  1064. vm_offset_t saddr, eaddr;
  1065. boolean_t complex;
  1066. boolean_t use_page_lists, steal_pages;
  1067. dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
  1068. complex = FALSE;
  1069. use_page_lists = ipc_kobject_vm_page_list(ip_kotype((ipc_port_t)dest));
  1070. steal_pages = ipc_kobject_vm_page_steal(ip_kotype((ipc_port_t)dest));
  1071. saddr = (vm_offset_t) (&kmsg->ikm_header + 1);
  1072. eaddr = (vm_offset_t) &kmsg->ikm_header + kmsg->ikm_header.msgh_size;
  1073. while (saddr < eaddr) {
  1074. vm_offset_t taddr = saddr;
  1075. mach_msg_type_long_t *type;
  1076. mach_msg_type_name_t name;
  1077. mach_msg_type_size_t size;
  1078. mach_msg_type_number_t number;
  1079. boolean_t is_inline, longform, dealloc, is_port;
  1080. vm_offset_t data;
  1081. uint64_t length;
  1082. kern_return_t kr;
  1083. type = (mach_msg_type_long_t *) saddr;
  1084. if (((eaddr - saddr) < sizeof(mach_msg_type_t)) ||
  1085. ((longform = ((mach_msg_type_t*)type)->msgt_longform) &&
  1086. ((eaddr - saddr) < sizeof(mach_msg_type_long_t)))) {
  1087. ipc_kmsg_clean_partial(kmsg, taddr, FALSE, 0);
  1088. return MACH_SEND_MSG_TOO_SMALL;
  1089. }
  1090. is_inline = ((mach_msg_type_t*)type)->msgt_inline;
  1091. dealloc = ((mach_msg_type_t*)type)->msgt_deallocate;
  1092. if (longform) {
  1093. /* This must be aligned */
  1094. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  1095. (is_misaligned(type))) {
  1096. saddr = ptr_align(saddr);
  1097. continue;
  1098. }
  1099. name = type->msgtl_name;
  1100. size = type->msgtl_size;
  1101. number = type->msgtl_number;
  1102. saddr += sizeof(mach_msg_type_long_t);
  1103. } else {
  1104. name = ((mach_msg_type_t*)type)->msgt_name;
  1105. size = ((mach_msg_type_t*)type)->msgt_size;
  1106. number = ((mach_msg_type_t*)type)->msgt_number;
  1107. saddr += sizeof(mach_msg_type_t);
  1108. }
  1109. is_port = MACH_MSG_TYPE_PORT_ANY(name);
  1110. if ((is_port && (size != PORT_T_SIZE_IN_BITS)) ||
  1111. (longform && ((type->msgtl_header.msgt_name != 0) ||
  1112. (type->msgtl_header.msgt_size != 0) ||
  1113. (type->msgtl_header.msgt_number != 0))) ||
  1114. (((mach_msg_type_t*)type)->msgt_unused != 0) ||
  1115. (dealloc && is_inline)) {
  1116. ipc_kmsg_clean_partial(kmsg, taddr, FALSE, 0);
  1117. return MACH_SEND_INVALID_TYPE;
  1118. }
  1119. /* padding (ptrs and ports) ? */
  1120. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  1121. ((size >> 3) == sizeof(natural_t)))
  1122. saddr = ptr_align(saddr);
  1123. /* calculate length of data in bytes, rounding up */
  1124. length = (((uint64_t) number * size) + 7) >> 3;
  1125. if (is_inline) {
  1126. vm_size_t amount;
  1127. /* inline data sizes round up to int boundaries */
  1128. amount = (length + 3) &~ 3;
  1129. if ((eaddr - saddr) < amount) {
  1130. ipc_kmsg_clean_partial(kmsg, taddr, FALSE, 0);
  1131. return MACH_SEND_MSG_TOO_SMALL;
  1132. }
  1133. data = saddr;
  1134. saddr += amount;
  1135. } else {
  1136. vm_offset_t addr;
  1137. if (sizeof(vm_offset_t) > sizeof(mach_msg_type_t))
  1138. saddr = ptr_align(saddr);
  1139. if ((eaddr - saddr) < sizeof(vm_offset_t)) {
  1140. ipc_kmsg_clean_partial(kmsg, taddr, FALSE, 0);
  1141. return MACH_SEND_MSG_TOO_SMALL;
  1142. }
  1143. /* grab the out-of-line data */
  1144. addr = * (vm_offset_t *) saddr;
  1145. if (length == 0)
  1146. data = 0;
  1147. else if (is_port) {
  1148. data = kalloc(length);
  1149. if (data == 0)
  1150. goto invalid_memory;
  1151. if (copyinmap(map, (char *) addr,
  1152. (char *) data, length) ||
  1153. (dealloc &&
  1154. (vm_deallocate(map, addr, length) !=
  1155. KERN_SUCCESS))) {
  1156. kfree(data, length);
  1157. goto invalid_memory;
  1158. }
  1159. } else {
  1160. vm_map_copy_t copy;
  1161. if (use_page_lists) {
  1162. kr = vm_map_copyin_page_list(map,
  1163. addr, length, dealloc,
  1164. steal_pages, &copy, FALSE);
  1165. } else {
  1166. kr = vm_map_copyin(map, addr, length,
  1167. dealloc, &copy);
  1168. }
  1169. if (kr != KERN_SUCCESS) {
  1170. invalid_memory:
  1171. ipc_kmsg_clean_partial(kmsg, taddr,
  1172. FALSE, 0);
  1173. return MACH_SEND_INVALID_MEMORY;
  1174. }
  1175. data = (vm_offset_t) copy;
  1176. }
  1177. * (vm_offset_t *) saddr = data;
  1178. saddr += sizeof(vm_offset_t);
  1179. complex = TRUE;
  1180. }
  1181. if (is_port) {
  1182. mach_msg_type_name_t newname =
  1183. ipc_object_copyin_type(name);
  1184. ipc_object_t *objects = (ipc_object_t *) data;
  1185. mach_msg_type_number_t i;
  1186. if (longform)
  1187. type->msgtl_name = newname;
  1188. else
  1189. ((mach_msg_type_t*)type)->msgt_name = newname;
  1190. for (i = 0; i < number; i++) {
  1191. mach_port_t port = (mach_port_t) objects[i];
  1192. ipc_object_t object;
  1193. if (!MACH_PORT_VALID(port))
  1194. continue;
  1195. kr = ipc_object_copyin(space, port,
  1196. name, &object);
  1197. if (kr != KERN_SUCCESS) {
  1198. ipc_kmsg_clean_partial(kmsg, taddr,
  1199. TRUE, i);
  1200. return MACH_SEND_INVALID_RIGHT;
  1201. }
  1202. if ((newname == MACH_MSG_TYPE_PORT_RECEIVE) &&
  1203. ipc_port_check_circularity(
  1204. (ipc_port_t) object,
  1205. (ipc_port_t) dest))
  1206. kmsg->ikm_header.msgh_bits |=
  1207. MACH_MSGH_BITS_CIRCULAR;
  1208. objects[i] = object;
  1209. }
  1210. complex = TRUE;
  1211. }
  1212. }
  1213. if (!complex)
  1214. kmsg->ikm_header.msgh_bits &= ~MACH_MSGH_BITS_COMPLEX;
  1215. return MACH_MSG_SUCCESS;
  1216. }
  1217. /*
  1218. * Routine: ipc_kmsg_copyin
  1219. * Purpose:
  1220. * "Copy-in" port rights and out-of-line memory
  1221. * in the message.
  1222. *
  1223. * In all failure cases, the message is left holding
  1224. * no rights or memory. However, the message buffer
  1225. * is not deallocated. If successful, the message
  1226. * contains a valid destination port.
  1227. * Conditions:
  1228. * Nothing locked.
  1229. * Returns:
  1230. * MACH_MSG_SUCCESS Successful copyin.
  1231. * MACH_SEND_INVALID_HEADER
  1232. * Illegal value in the message header bits.
  1233. * MACH_SEND_INVALID_NOTIFY Bad notify port.
  1234. * MACH_SEND_INVALID_DEST Can't copyin destination port.
  1235. * MACH_SEND_INVALID_REPLY Can't copyin reply port.
  1236. * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
  1237. * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
  1238. * MACH_SEND_INVALID_TYPE Bad type specification.
  1239. * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
  1240. */
  1241. mach_msg_return_t
  1242. ipc_kmsg_copyin(
  1243. ipc_kmsg_t kmsg,
  1244. ipc_space_t space,
  1245. vm_map_t map,
  1246. mach_port_t notify)
  1247. {
  1248. mach_msg_return_t mr;
  1249. mr = ipc_kmsg_copyin_header(&kmsg->ikm_header, space, notify);
  1250. if (mr != MACH_MSG_SUCCESS)
  1251. return mr;
  1252. if ((kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) == 0)
  1253. return MACH_MSG_SUCCESS;
  1254. return ipc_kmsg_copyin_body(kmsg, space, map);
  1255. }
  1256. /*
  1257. * Routine: ipc_kmsg_copyin_from_kernel
  1258. * Purpose:
  1259. * "Copy-in" port rights and out-of-line memory
  1260. * in a message sent from the kernel.
  1261. *
  1262. * Because the message comes from the kernel,
  1263. * the implementation assumes there are no errors
  1264. * or peculiarities in the message.
  1265. *
  1266. * Returns TRUE if queueing the message
  1267. * would result in a circularity.
  1268. * Conditions:
  1269. * Nothing locked.
  1270. */
  1271. void
  1272. ipc_kmsg_copyin_from_kernel(ipc_kmsg_t kmsg)
  1273. {
  1274. mach_msg_bits_t bits = kmsg->ikm_header.msgh_bits;
  1275. mach_msg_type_name_t rname = MACH_MSGH_BITS_REMOTE(bits);
  1276. mach_msg_type_name_t lname = MACH_MSGH_BITS_LOCAL(bits);
  1277. ipc_object_t remote = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
  1278. ipc_object_t local = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
  1279. vm_offset_t saddr, eaddr;
  1280. /* translate the destination and reply ports */
  1281. ipc_object_copyin_from_kernel(remote, rname);
  1282. if (IO_VALID(local))
  1283. ipc_object_copyin_from_kernel(local, lname);
  1284. /*
  1285. * The common case is a complex message with no reply port,
  1286. * because that is what the memory_object interface uses.
  1287. */
  1288. if (bits == (MACH_MSGH_BITS_COMPLEX |
  1289. MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0))) {
  1290. bits = (MACH_MSGH_BITS_COMPLEX |
  1291. MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0));
  1292. kmsg->ikm_header.msgh_bits = bits;
  1293. } else {
  1294. bits = (MACH_MSGH_BITS_OTHER(bits) |
  1295. MACH_MSGH_BITS(ipc_object_copyin_type(rname),
  1296. ipc_object_copyin_type(lname)));
  1297. kmsg->ikm_header.msgh_bits = bits;
  1298. if ((bits & MACH_MSGH_BITS_COMPLEX) == 0)
  1299. return;
  1300. }
  1301. saddr = (vm_offset_t) (&kmsg->ikm_header + 1);
  1302. eaddr = (vm_offset_t) &kmsg->ikm_header + kmsg->ikm_header.msgh_size;
  1303. while (saddr < eaddr) {
  1304. mach_msg_type_long_t *type;
  1305. mach_msg_type_name_t name;
  1306. mach_msg_type_size_t size;
  1307. mach_msg_type_number_t number;
  1308. boolean_t is_inline, longform, is_port;
  1309. vm_offset_t data;
  1310. vm_size_t length;
  1311. type = (mach_msg_type_long_t *) saddr;
  1312. is_inline = ((mach_msg_type_t*)type)->msgt_inline;
  1313. longform = ((mach_msg_type_t*)type)->msgt_longform;
  1314. /* type->msgtl_header.msgt_deallocate not used */
  1315. if (longform) {
  1316. /* This must be aligned */
  1317. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  1318. (is_misaligned(type))) {
  1319. saddr = ptr_align(saddr);
  1320. continue;
  1321. }
  1322. name = type->msgtl_name;
  1323. size = type->msgtl_size;
  1324. number = type->msgtl_number;
  1325. saddr += sizeof(mach_msg_type_long_t);
  1326. } else {
  1327. name = ((mach_msg_type_t*)type)->msgt_name;
  1328. size = ((mach_msg_type_t*)type)->msgt_size;
  1329. number = ((mach_msg_type_t*)type)->msgt_number;
  1330. saddr += sizeof(mach_msg_type_t);
  1331. }
  1332. /* padding (ptrs and ports) ? */
  1333. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  1334. ((size >> 3) == sizeof(natural_t)))
  1335. saddr = ptr_align(saddr);
  1336. /* calculate length of data in bytes, rounding up */
  1337. length = ((number * size) + 7) >> 3;
  1338. is_port = MACH_MSG_TYPE_PORT_ANY(name);
  1339. if (is_inline) {
  1340. /* inline data sizes round up to int boundaries */
  1341. data = saddr;
  1342. saddr += (length + 3) &~ 3;
  1343. } else {
  1344. /*
  1345. * The sender should supply ready-made memory
  1346. * for us, so we don't need to do anything.
  1347. */
  1348. data = * (vm_offset_t *) saddr;
  1349. saddr += sizeof(vm_offset_t);
  1350. }
  1351. if (is_port) {
  1352. mach_msg_type_name_t newname =
  1353. ipc_object_copyin_type(name);
  1354. ipc_object_t *objects = (ipc_object_t *) data;
  1355. mach_msg_type_number_t i;
  1356. if (longform)
  1357. type->msgtl_name = newname;
  1358. else
  1359. ((mach_msg_type_t*)type)->msgt_name = newname;
  1360. for (i = 0; i < number; i++) {
  1361. ipc_object_t object = objects[i];
  1362. if (!IO_VALID(object))
  1363. continue;
  1364. ipc_object_copyin_from_kernel(object, name);
  1365. if ((newname == MACH_MSG_TYPE_PORT_RECEIVE) &&
  1366. ipc_port_check_circularity(
  1367. (ipc_port_t) object,
  1368. (ipc_port_t) remote))
  1369. kmsg->ikm_header.msgh_bits |=
  1370. MACH_MSGH_BITS_CIRCULAR;
  1371. }
  1372. }
  1373. }
  1374. }
  1375. /*
  1376. * Routine: ipc_kmsg_copyout_header
  1377. * Purpose:
  1378. * "Copy-out" port rights in the header of a message.
  1379. * Operates atomically; if it doesn't succeed the
  1380. * message header and the space are left untouched.
  1381. * If it does succeed the remote/local port fields
  1382. * contain port names instead of object pointers,
  1383. * and the bits field is updated.
  1384. *
  1385. * The notify argument implements the MACH_RCV_NOTIFY option.
  1386. * If it is not MACH_PORT_NULL, it should name a receive right.
  1387. * If the process of receiving the reply port creates a
  1388. * new right in the receiving task, then the new right is
  1389. * automatically registered for a dead-name notification,
  1390. * with the notify port supplying the send-once right.
  1391. * Conditions:
  1392. * Nothing locked.
  1393. * Returns:
  1394. * MACH_MSG_SUCCESS Copied out port rights.
  1395. * MACH_RCV_INVALID_NOTIFY
  1396. * Notify is non-null and doesn't name a receive right.
  1397. * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
  1398. * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
  1399. * The space is dead.
  1400. * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
  1401. * No room in space for another name.
  1402. * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
  1403. * Couldn't allocate memory for the reply port.
  1404. * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
  1405. * Couldn't allocate memory for the dead-name request.
  1406. */
  1407. mach_msg_return_t
  1408. ipc_kmsg_copyout_header(
  1409. mach_msg_header_t *msg,
  1410. ipc_space_t space,
  1411. mach_port_t notify)
  1412. {
  1413. mach_msg_bits_t mbits = msg->msgh_bits;
  1414. ipc_port_t dest = (ipc_port_t) msg->msgh_remote_port;
  1415. printk(KERN_ERR,"ipc_kmsg_copyout_header is broken\n");
  1416. return -1;//BUG here -- assert fails
  1417. assert(IP_VALID(dest));
  1418. #ifndef MIGRATING_THREADS
  1419. /* first check for common cases */
  1420. if (notify == MACH_PORT_NULL) switch (MACH_MSGH_BITS_PORTS(mbits)) {
  1421. case MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0): {
  1422. mach_port_t dest_name;
  1423. ipc_port_t nsrequest;
  1424. unsigned long payload;
  1425. /* receiving an asynchronous message */
  1426. ip_lock(dest);
  1427. if (!ip_active(dest)) {
  1428. ip_unlock(dest);
  1429. break;
  1430. }
  1431. /* optimized ipc_object_copyout_dest */
  1432. assert(dest->ip_srights > 0);
  1433. ip_release(dest);
  1434. if (dest->ip_receiver == space)
  1435. dest_name = dest->ip_receiver_name;
  1436. else
  1437. dest_name = MACH_PORT_NULL;
  1438. payload = dest->ip_protected_payload;
  1439. if ((--dest->ip_srights == 0) &&
  1440. ((nsrequest = dest->ip_nsrequest) != IP_NULL)) {
  1441. mach_port_mscount_t mscount;
  1442. dest->ip_nsrequest = IP_NULL;
  1443. mscount = dest->ip_mscount;
  1444. ip_unlock(dest);
  1445. ipc_notify_no_senders(nsrequest, mscount);
  1446. } else
  1447. ip_unlock(dest);
  1448. if (! ipc_port_flag_protected_payload(dest)) {
  1449. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1450. MACH_MSGH_BITS(0, MACH_MSG_TYPE_PORT_SEND));
  1451. msg->msgh_local_port = dest_name;
  1452. } else {
  1453. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1454. MACH_MSGH_BITS(
  1455. 0, MACH_MSG_TYPE_PROTECTED_PAYLOAD));
  1456. msg->msgh_protected_payload = payload;
  1457. }
  1458. msg->msgh_remote_port = MACH_PORT_NULL;
  1459. return MACH_MSG_SUCCESS;
  1460. }
  1461. case MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND,
  1462. MACH_MSG_TYPE_PORT_SEND_ONCE): {
  1463. ipc_entry_t entry;
  1464. ipc_port_t reply = (ipc_port_t) msg->msgh_local_port;
  1465. mach_port_t dest_name, reply_name;
  1466. ipc_port_t nsrequest;
  1467. unsigned long payload;
  1468. /* receiving a request message */
  1469. if (!IP_VALID(reply))
  1470. break;
  1471. is_write_lock(space);
  1472. if (!space->is_active /*|| space->is_free_list == NULL*/) {
  1473. printk(KERN_CRIT "!space->is_active || space->is_free_list == NULL\n");
  1474. is_write_unlock(space);
  1475. break;
  1476. }
  1477. /*
  1478. * To do an atomic copyout, need simultaneous
  1479. * locks on both ports and the space. If
  1480. * dest == reply, and simple locking is
  1481. * enabled, then we will abort. Otherwise it's
  1482. * OK to unlock twice.
  1483. */
  1484. ip_lock(dest);
  1485. if (!ip_active(dest) || !ip_lock_try(reply)) {
  1486. ip_unlock(dest);
  1487. is_write_unlock(space);
  1488. break;
  1489. }
  1490. if (!ip_active(reply)) {
  1491. ip_unlock(reply);
  1492. ip_unlock(dest);
  1493. is_write_unlock(space);
  1494. break;
  1495. }
  1496. assert(reply->ip_sorights > 0);
  1497. ip_unlock(reply);
  1498. kern_return_t kr;
  1499. kr = ipc_entry_get (space, &reply_name, &entry);
  1500. if (kr) {
  1501. ip_unlock(reply);
  1502. ip_unlock(dest);
  1503. is_write_unlock(space);
  1504. break;
  1505. }
  1506. {
  1507. mach_port_gen_t gen;
  1508. assert((entry->ie_bits &~ IE_BITS_GEN_MASK) == 0);
  1509. gen = entry->ie_bits + IE_BITS_GEN_ONE;
  1510. /* optimized ipc_right_copyout */
  1511. entry->ie_bits = gen | (MACH_PORT_TYPE_SEND_ONCE | 1);
  1512. }
  1513. assert(MACH_PORT_VALID(reply_name));
  1514. entry->ie_object = (ipc_object_t) reply;
  1515. is_write_unlock(space);
  1516. /* optimized ipc_object_copyout_dest */
  1517. assert(dest->ip_srights > 0);
  1518. ip_release(dest);
  1519. if (dest->ip_receiver == space)
  1520. dest_name = dest->ip_receiver_name;
  1521. else
  1522. dest_name = MACH_PORT_NULL;
  1523. payload = dest->ip_protected_payload;
  1524. if ((--dest->ip_srights == 0) &&
  1525. ((nsrequest = dest->ip_nsrequest) != IP_NULL)) {
  1526. mach_port_mscount_t mscount;
  1527. dest->ip_nsrequest = IP_NULL;
  1528. mscount = dest->ip_mscount;
  1529. ip_unlock(dest);
  1530. ipc_notify_no_senders(nsrequest, mscount);
  1531. } else
  1532. ip_unlock(dest);
  1533. if (! ipc_port_flag_protected_payload(dest)) {
  1534. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1535. MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE,
  1536. MACH_MSG_TYPE_PORT_SEND));
  1537. msg->msgh_local_port = dest_name;
  1538. } else {
  1539. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1540. MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE,
  1541. MACH_MSG_TYPE_PROTECTED_PAYLOAD));
  1542. msg->msgh_protected_payload = payload;
  1543. }
  1544. msg->msgh_remote_port = reply_name;
  1545. return MACH_MSG_SUCCESS;
  1546. }
  1547. case MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE, 0): {
  1548. mach_port_t dest_name;
  1549. unsigned long payload;
  1550. /* receiving a reply message */
  1551. ip_lock(dest);
  1552. if (!ip_active(dest)) {
  1553. ip_unlock(dest);
  1554. break;
  1555. }
  1556. /* optimized ipc_object_copyout_dest */
  1557. assert(dest->ip_sorights > 0);
  1558. payload = dest->ip_protected_payload;
  1559. if (dest->ip_receiver == space) {
  1560. ip_release(dest);
  1561. dest->ip_sorights--;
  1562. dest_name = dest->ip_receiver_name;
  1563. ip_unlock(dest);
  1564. } else {
  1565. ip_unlock(dest);
  1566. ipc_notify_send_once(dest);
  1567. dest_name = MACH_PORT_NULL;
  1568. }
  1569. if (! ipc_port_flag_protected_payload(dest)) {
  1570. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1571. MACH_MSGH_BITS(0,
  1572. MACH_MSG_TYPE_PORT_SEND_ONCE));
  1573. msg->msgh_local_port = dest_name;
  1574. } else {
  1575. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1576. MACH_MSGH_BITS(0,
  1577. MACH_MSG_TYPE_PROTECTED_PAYLOAD));
  1578. msg->msgh_protected_payload = payload;
  1579. }
  1580. msg->msgh_remote_port = MACH_PORT_NULL;
  1581. return MACH_MSG_SUCCESS;
  1582. }
  1583. default:
  1584. /* don't bother optimizing */
  1585. break;
  1586. }
  1587. #endif /* MIGRATING_THREADS */
  1588. {
  1589. mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
  1590. mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
  1591. ipc_port_t reply = (ipc_port_t) msg->msgh_local_port;
  1592. mach_port_t dest_name, reply_name;
  1593. unsigned long payload;
  1594. if (IP_VALID(reply)) {
  1595. ipc_port_t notify_port;
  1596. ipc_entry_t entry;
  1597. kern_return_t kr;
  1598. /*
  1599. * Handling notify (for MACH_RCV_NOTIFY) is tricky.
  1600. * The problem is atomically making a send-once right
  1601. * from the notify port and installing it for a
  1602. * dead-name request in the new entry, because this
  1603. * requires two port locks (on the notify port and
  1604. * the reply port). However, we can safely make
  1605. * and consume send-once rights for the notify port
  1606. * as long as we hold the space locked. This isn't
  1607. * an atomicity problem, because the only way
  1608. * to detect that a send-once right has been created
  1609. * and then consumed if it wasn't needed is by getting
  1610. * at the receive right to look at ip_sorights, and
  1611. * because the space is write-locked status calls can't
  1612. * lookup the notify port receive right. When we make
  1613. * the send-once right, we lock the notify port,
  1614. * so any status calls in progress will be done.
  1615. */
  1616. is_write_lock(space);
  1617. for (;;) {
  1618. ipc_port_request_index_t request;
  1619. if (!space->is_active) {
  1620. is_write_unlock(space);
  1621. return (MACH_RCV_HEADER_ERROR|
  1622. MACH_MSG_IPC_SPACE);
  1623. }
  1624. if (notify != MACH_PORT_NULL) {
  1625. notify_port = ipc_port_lookup_notify(space,
  1626. notify);
  1627. if (notify_port == IP_NULL) {
  1628. is_write_unlock(space);
  1629. return MACH_RCV_INVALID_NOTIFY;
  1630. }
  1631. } else
  1632. notify_port = IP_NULL;
  1633. if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
  1634. ipc_right_reverse(space, (ipc_object_t) reply,
  1635. &reply_name, &entry)) {
  1636. /* reply port is locked and active */
  1637. /*
  1638. * We don't need the notify_port
  1639. * send-once right, but we can't release
  1640. * it here because reply port is locked.
  1641. * Wait until after the copyout to
  1642. * release the notify port right.
  1643. */
  1644. assert(entry->ie_bits &
  1645. MACH_PORT_TYPE_SEND_RECEIVE);
  1646. break;
  1647. }
  1648. ip_lock(reply);
  1649. if (!ip_active(reply)) {
  1650. ip_release(reply);
  1651. ip_check_unlock(reply);
  1652. if (notify_port != IP_NULL)
  1653. ipc_port_release_sonce(notify_port);
  1654. ip_lock(dest);
  1655. is_write_unlock(space);
  1656. reply = IP_DEAD;
  1657. reply_name = MACH_PORT_DEAD;
  1658. goto copyout_dest;
  1659. }
  1660. kr = ipc_entry_alloc(space, &reply_name, &entry);
  1661. if (kr != KERN_SUCCESS) {
  1662. ip_unlock(reply);
  1663. if (notify_port != IP_NULL)
  1664. ipc_port_release_sonce(notify_port);
  1665. is_write_unlock(space);
  1666. if (kr == KERN_RESOURCE_SHORTAGE)
  1667. return (MACH_RCV_HEADER_ERROR|
  1668. MACH_MSG_IPC_KERNEL);
  1669. else
  1670. return (MACH_RCV_HEADER_ERROR|
  1671. MACH_MSG_IPC_SPACE);
  1672. }
  1673. assert(IE_BITS_TYPE(entry->ie_bits)
  1674. == MACH_PORT_TYPE_NONE);
  1675. assert(entry->ie_object == IO_NULL);
  1676. if (notify_port == IP_NULL) {
  1677. /* not making a dead-name request */
  1678. entry->ie_object = (ipc_object_t) reply;
  1679. break;
  1680. }
  1681. kr = ipc_port_dnrequest(reply, reply_name,
  1682. notify_port, &request);
  1683. if (kr != KERN_SUCCESS) {
  1684. ip_unlock(reply);
  1685. ipc_port_release_sonce(notify_port);
  1686. ipc_entry_dealloc(space, reply_name, entry);
  1687. is_write_unlock(space);
  1688. ip_lock(reply);
  1689. if (!ip_active(reply)) {
  1690. /* will fail next time around loop */
  1691. ip_unlock(reply);
  1692. is_write_lock(space);
  1693. continue;
  1694. }
  1695. kr = ipc_port_dngrow(reply);
  1696. /* port is unlocked */
  1697. if (kr != KERN_SUCCESS)
  1698. return (MACH_RCV_HEADER_ERROR|
  1699. MACH_MSG_IPC_KERNEL);
  1700. is_write_lock(space);
  1701. continue;
  1702. }
  1703. notify_port = IP_NULL; /* don't release right below */
  1704. entry->ie_object = (ipc_object_t) reply;
  1705. entry->ie_request = request;
  1706. break;
  1707. }
  1708. /* space and reply port are locked and active */
  1709. ip_reference(reply); /* hold onto the reply port */
  1710. kr = ipc_right_copyout(space, reply_name, entry,
  1711. reply_type, TRUE, (ipc_object_t) reply);
  1712. /* reply port is unlocked */
  1713. assert(kr == KERN_SUCCESS);
  1714. if (notify_port != IP_NULL)
  1715. ipc_port_release_sonce(notify_port);
  1716. ip_lock(dest);
  1717. is_write_unlock(space);
  1718. } else {
  1719. /*
  1720. * No reply port! This is an easy case.
  1721. * We only need to have the space locked
  1722. * when checking notify and when locking
  1723. * the destination (to ensure atomicity).
  1724. */
  1725. is_read_lock(space);
  1726. if (!space->is_active) {
  1727. is_read_unlock(space);
  1728. return MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE;
  1729. }
  1730. if (notify != MACH_PORT_NULL) {
  1731. ipc_entry_t entry;
  1732. /* must check notify even though it won't be used */
  1733. if (((entry = ipc_entry_lookup(space, notify))
  1734. == IE_NULL) ||
  1735. ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0)) {
  1736. is_read_unlock(space);
  1737. return MACH_RCV_INVALID_NOTIFY;
  1738. }
  1739. }
  1740. ip_lock(dest);
  1741. is_read_unlock(space);
  1742. reply_name = (mach_port_t) reply;
  1743. }
  1744. /*
  1745. * At this point, the space is unlocked and the destination
  1746. * port is locked. (Lock taken while space was locked.)
  1747. * reply_name is taken care of; we still need dest_name.
  1748. * We still hold a ref for reply (if it is valid).
  1749. *
  1750. * If the space holds receive rights for the destination,
  1751. * we return its name for the right. Otherwise the task
  1752. * managed to destroy or give away the receive right between
  1753. * receiving the message and this copyout. If the destination
  1754. * is dead, return MACH_PORT_DEAD, and if the receive right
  1755. * exists somewhere else (another space, in transit)
  1756. * return MACH_PORT_NULL.
  1757. *
  1758. * Making this copyout operation atomic with the previous
  1759. * copyout of the reply port is a bit tricky. If there was
  1760. * no real reply port (it wasn't IP_VALID) then this isn't
  1761. * an issue. If the reply port was dead at copyout time,
  1762. * then we are OK, because if dest is dead we serialize
  1763. * after the death of both ports and if dest is alive
  1764. * we serialize after reply died but before dest's (later) death.
  1765. * So assume reply was alive when we copied it out. If dest
  1766. * is alive, then we are OK because we serialize before
  1767. * the ports' deaths. So assume dest is dead when we look at it.
  1768. * If reply dies/died after dest, then we are OK because
  1769. * we serialize after dest died but before reply dies.
  1770. * So the hard case is when reply is alive at copyout,
  1771. * dest is dead at copyout, and reply died before dest died.
  1772. * In this case pretend that dest is still alive, so
  1773. * we serialize while both ports are alive.
  1774. *
  1775. * Because the space lock is held across the copyout of reply
  1776. * and locking dest, the receive right for dest can't move
  1777. * in or out of the space while the copyouts happen, so
  1778. * that isn't an atomicity problem. In the last hard case
  1779. * above, this implies that when dest is dead that the
  1780. * space couldn't have had receive rights for dest at
  1781. * the time reply was copied-out, so when we pretend
  1782. * that dest is still alive, we can return MACH_PORT_NULL.
  1783. *
  1784. * If dest == reply, then we have to make it look like
  1785. * either both copyouts happened before the port died,
  1786. * or both happened after the port died. This special
  1787. * case works naturally if the timestamp comparison
  1788. * is done correctly.
  1789. */
  1790. copyout_dest:
  1791. payload = dest->ip_protected_payload;
  1792. if (ip_active(dest)) {
  1793. ipc_object_copyout_dest(space, (ipc_object_t) dest,
  1794. dest_type, &dest_name);
  1795. /* dest is unlocked */
  1796. } else {
  1797. ipc_port_timestamp_t timestamp;
  1798. timestamp = dest->ip_timestamp;
  1799. ip_release(dest);
  1800. ip_check_unlock(dest);
  1801. if (IP_VALID(reply)) {
  1802. ip_lock(reply);
  1803. if (ip_active(reply) ||
  1804. IP_TIMESTAMP_ORDER(timestamp,
  1805. reply->ip_timestamp))
  1806. dest_name = MACH_PORT_DEAD;
  1807. else
  1808. dest_name = MACH_PORT_NULL;
  1809. ip_unlock(reply);
  1810. } else
  1811. dest_name = MACH_PORT_DEAD;
  1812. }
  1813. if (IP_VALID(reply))
  1814. ipc_port_release(reply);
  1815. if (! ipc_port_flag_protected_payload(dest)) {
  1816. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1817. MACH_MSGH_BITS(reply_type, dest_type));
  1818. msg->msgh_local_port = dest_name;
  1819. } else {
  1820. msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  1821. MACH_MSGH_BITS(reply_type,
  1822. MACH_MSG_TYPE_PROTECTED_PAYLOAD));
  1823. msg->msgh_protected_payload = payload;
  1824. }
  1825. msg->msgh_remote_port = reply_name;
  1826. }
  1827. return MACH_MSG_SUCCESS;
  1828. }
  1829. /*
  1830. * Routine: ipc_kmsg_copyout_object
  1831. * Purpose:
  1832. * Copy-out a port right. Always returns a name,
  1833. * even for unsuccessful return codes. Always
  1834. * consumes the supplied object.
  1835. * Conditions:
  1836. * Nothing locked.
  1837. * Returns:
  1838. * MACH_MSG_SUCCESS The space acquired the right
  1839. * (name is valid) or the object is dead (MACH_PORT_DEAD).
  1840. * MACH_MSG_IPC_SPACE No room in space for the right,
  1841. * or the space is dead. (Name is MACH_PORT_NULL.)
  1842. * MACH_MSG_IPC_KERNEL Kernel resource shortage.
  1843. * (Name is MACH_PORT_NULL.)
  1844. */
  1845. #if 0
  1846. mach_msg_return_t
  1847. ipc_kmsg_copyout_object(
  1848. ipc_space_t space,
  1849. ipc_object_t object,
  1850. mach_msg_type_name_t msgt_name,
  1851. mach_port_t *namep)
  1852. {
  1853. if (!IO_VALID(object)) {
  1854. *namep = (mach_port_t) object;
  1855. return MACH_MSG_SUCCESS;
  1856. }
  1857. #ifndef MIGRATING_THREADS
  1858. /*
  1859. * Attempt quick copyout of send rights. We optimize for a
  1860. * live port for which the receiver holds send (and not
  1861. * receive) rights in his local table.
  1862. */
  1863. if (msgt_name != MACH_MSG_TYPE_PORT_SEND)
  1864. goto slow_copyout;
  1865. {
  1866. ipc_port_t port = (ipc_port_t) object;
  1867. ipc_entry_t entry;
  1868. is_write_lock(space);
  1869. if (!space->is_active) {
  1870. is_write_unlock(space);
  1871. goto slow_copyout;
  1872. }
  1873. ip_lock(port);
  1874. if (!ip_active(port) ||
  1875. (entry = ipc_reverse_lookup(space,
  1876. (ipc_object_t) port)) == NULL) {
  1877. ip_unlock(port);
  1878. is_write_unlock(space);
  1879. goto slow_copyout;
  1880. }
  1881. *namep = entry->ie_name;
  1882. /*
  1883. * Copyout the send right, incrementing urefs
  1884. * unless it would overflow, and consume the right.
  1885. */
  1886. assert(port->ip_srights > 1);
  1887. port->ip_srights--;
  1888. ip_release(port);
  1889. ip_unlock(port);
  1890. assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
  1891. assert(IE_BITS_UREFS(entry->ie_bits) > 0);
  1892. assert(IE_BITS_UREFS(entry->ie_bits) < MACH_PORT_UREFS_MAX);
  1893. {
  1894. ipc_entry_bits_t bits = entry->ie_bits + 1;
  1895. if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX)
  1896. entry->ie_bits = bits;
  1897. }
  1898. is_write_unlock(space);
  1899. return MACH_MSG_SUCCESS;
  1900. }
  1901. slow_copyout:
  1902. #endif /* MIGRATING_THREADS */
  1903. {
  1904. kern_return_t kr;
  1905. kr = ipc_object_copyout(space, object, msgt_name, TRUE, namep);
  1906. if (kr != KERN_SUCCESS) {
  1907. ipc_object_destroy(object, msgt_name);
  1908. if (kr == KERN_INVALID_CAPABILITY)
  1909. *namep = MACH_PORT_DEAD;
  1910. else {
  1911. *namep = MACH_PORT_NULL;
  1912. if (kr == KERN_RESOURCE_SHORTAGE)
  1913. return MACH_MSG_IPC_KERNEL;
  1914. else
  1915. return MACH_MSG_IPC_SPACE;
  1916. }
  1917. }
  1918. return MACH_MSG_SUCCESS;
  1919. }
  1920. }
  1921. #endif
  1922. /*
  1923. * Routine: ipc_kmsg_copyout_body
  1924. * Purpose:
  1925. * "Copy-out" port rights and out-of-line memory
  1926. * in the body of a message.
  1927. *
  1928. * The error codes are a combination of special bits.
  1929. * The copyout proceeds despite errors.
  1930. * Conditions:
  1931. * Nothing locked.
  1932. * Returns:
  1933. * MACH_MSG_SUCCESS Successful copyout.
  1934. * MACH_MSG_IPC_SPACE No room for port right in name space.
  1935. * MACH_MSG_VM_SPACE No room for memory in address space.
  1936. * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
  1937. * MACH_MSG_VM_KERNEL Resource shortage handling memory.
  1938. */
  1939. mach_msg_return_t
  1940. ipc_kmsg_copyout_body(
  1941. vm_offset_t saddr,
  1942. vm_offset_t eaddr,
  1943. ipc_space_t space,
  1944. vm_map_t map)
  1945. {
  1946. mach_msg_return_t mr = MACH_MSG_SUCCESS;
  1947. kern_return_t kr;
  1948. while (saddr < eaddr) {
  1949. vm_offset_t taddr = saddr;
  1950. mach_msg_type_long_t *type;
  1951. mach_msg_type_name_t name;
  1952. mach_msg_type_size_t size;
  1953. mach_msg_type_number_t number;
  1954. boolean_t is_inline, longform, is_port;
  1955. uint64_t length;
  1956. vm_offset_t addr;
  1957. type = (mach_msg_type_long_t *) saddr;
  1958. is_inline = ((mach_msg_type_t*)type)->msgt_inline;
  1959. longform = ((mach_msg_type_t*)type)->msgt_longform;
  1960. if (longform) {
  1961. /* This must be aligned */
  1962. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  1963. (is_misaligned(type))) {
  1964. saddr = ptr_align(saddr);
  1965. continue;
  1966. }
  1967. name = type->msgtl_name;
  1968. size = type->msgtl_size;
  1969. number = type->msgtl_number;
  1970. saddr += sizeof(mach_msg_type_long_t);
  1971. } else {
  1972. name = ((mach_msg_type_t*)type)->msgt_name;
  1973. size = ((mach_msg_type_t*)type)->msgt_size;
  1974. number = ((mach_msg_type_t*)type)->msgt_number;
  1975. saddr += sizeof(mach_msg_type_t);
  1976. }
  1977. /* padding (ptrs and ports) ? */
  1978. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  1979. ((size >> 3) == sizeof(natural_t)))
  1980. saddr = ptr_align(saddr);
  1981. /* calculate length of data in bytes, rounding up */
  1982. length = (((uint64_t) number * size) + 7) >> 3;
  1983. is_port = MACH_MSG_TYPE_PORT_ANY(name);
  1984. if (is_port) {
  1985. mach_port_t *objects;
  1986. mach_msg_type_number_t i;
  1987. if (!is_inline && (length != 0)) {
  1988. /* first allocate memory in the map */
  1989. kr = vm_allocate(map, &addr, length, TRUE);
  1990. if (kr != KERN_SUCCESS) {
  1991. ipc_kmsg_clean_body(taddr, saddr);
  1992. goto vm_copyout_failure;
  1993. }
  1994. }
  1995. objects = (mach_port_t *)
  1996. (is_inline ? saddr : * (vm_offset_t *) saddr);
  1997. /* copyout port rights carried in the message */
  1998. for (i = 0; i < number; i++) {
  1999. ipc_object_t object =
  2000. (ipc_object_t) objects[i];
  2001. mr |= ipc_kmsg_copyout_object(space, object,
  2002. name, &objects[i]);
  2003. }
  2004. }
  2005. if (is_inline) {
  2006. /* inline data sizes round up to int boundaries */
  2007. ((mach_msg_type_t*)type)->msgt_deallocate = FALSE;
  2008. saddr += (length + 3) &~ 3;
  2009. } else {
  2010. vm_offset_t data;
  2011. if (sizeof(vm_offset_t) > sizeof(mach_msg_type_t))
  2012. saddr = ptr_align(saddr);
  2013. data = * (vm_offset_t *) saddr;
  2014. /* copyout memory carried in the message */
  2015. if (length == 0) {
  2016. assert(data == 0);
  2017. addr = 0;
  2018. } else if (is_port) {
  2019. /* copyout to memory allocated above */
  2020. (void) copyoutmap(map, (char *) data,
  2021. (char *) addr, length);
  2022. kfree(data, length);
  2023. } else {
  2024. vm_map_copy_t copy = (vm_map_copy_t) data;
  2025. kr = vm_map_copyout(map, &addr, copy);
  2026. if (kr != KERN_SUCCESS) {
  2027. vm_map_copy_discard(copy);
  2028. vm_copyout_failure:
  2029. addr = 0;
  2030. if (longform)
  2031. type->msgtl_size = 0;
  2032. else
  2033. ((mach_msg_type_t*)type)->msgt_size = 0;
  2034. if (kr == KERN_RESOURCE_SHORTAGE)
  2035. mr |= MACH_MSG_VM_KERNEL;
  2036. else
  2037. mr |= MACH_MSG_VM_SPACE;
  2038. }
  2039. }
  2040. ((mach_msg_type_t*)type)->msgt_deallocate = TRUE;
  2041. * (vm_offset_t *) saddr = addr;
  2042. saddr += sizeof(vm_offset_t);
  2043. }
  2044. }
  2045. return mr;
  2046. }
  2047. /*
  2048. * Routine: ipc_kmsg_copyout
  2049. * Purpose:
  2050. * "Copy-out" port rights and out-of-line memory
  2051. * in the message.
  2052. * Conditions:
  2053. * Nothing locked.
  2054. * Returns:
  2055. * MACH_MSG_SUCCESS Copied out all rights and memory.
  2056. * MACH_RCV_INVALID_NOTIFY Bad notify port.
  2057. * Rights and memory in the message are intact.
  2058. * MACH_RCV_HEADER_ERROR + special bits
  2059. * Rights and memory in the message are intact.
  2060. * MACH_RCV_BODY_ERROR + special bits
  2061. * The message header was successfully copied out.
  2062. * As much of the body was handled as possible.
  2063. */
  2064. mach_msg_return_t
  2065. ipc_kmsg_copyout(
  2066. ipc_kmsg_t kmsg,
  2067. ipc_space_t space,
  2068. vm_map_t map,
  2069. mach_port_t notify)
  2070. {
  2071. mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits;
  2072. mach_msg_return_t mr;
  2073. mr = ipc_kmsg_copyout_header(&kmsg->ikm_header, space, notify);
  2074. if (mr != MACH_MSG_SUCCESS)
  2075. return mr;
  2076. if (mbits & MACH_MSGH_BITS_COMPLEX) {
  2077. vm_offset_t saddr, eaddr;
  2078. saddr = (vm_offset_t) (&kmsg->ikm_header + 1);
  2079. eaddr = (vm_offset_t) &kmsg->ikm_header +
  2080. kmsg->ikm_header.msgh_size;
  2081. mr = ipc_kmsg_copyout_body(saddr, eaddr, space, map);
  2082. if (mr != MACH_MSG_SUCCESS)
  2083. mr |= MACH_RCV_BODY_ERROR;
  2084. }
  2085. return mr;
  2086. }
  2087. /*
  2088. * Routine: ipc_kmsg_copyout_pseudo
  2089. * Purpose:
  2090. * Does a pseudo-copyout of the message.
  2091. * This is like a regular copyout, except
  2092. * that the ports in the header are handled
  2093. * as if they are in the body. They aren't reversed.
  2094. *
  2095. * The error codes are a combination of special bits.
  2096. * The copyout proceeds despite errors.
  2097. * Conditions:
  2098. * Nothing locked.
  2099. * Returns:
  2100. * MACH_MSG_SUCCESS Successful copyout.
  2101. * MACH_MSG_IPC_SPACE No room for port right in name space.
  2102. * MACH_MSG_VM_SPACE No room for memory in address space.
  2103. * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
  2104. * MACH_MSG_VM_KERNEL Resource shortage handling memory.
  2105. */
  2106. mach_msg_return_t
  2107. ipc_kmsg_copyout_pseudo(
  2108. ipc_kmsg_t kmsg,
  2109. ipc_space_t space,
  2110. vm_map_t map)
  2111. {
  2112. mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits;
  2113. ipc_object_t dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
  2114. ipc_object_t reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
  2115. mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
  2116. mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
  2117. mach_port_t dest_name, reply_name;
  2118. mach_msg_return_t mr;
  2119. assert(IO_VALID(dest));
  2120. mr = (ipc_kmsg_copyout_object(space, dest, dest_type, &dest_name) |
  2121. ipc_kmsg_copyout_object(space, reply, reply_type, &reply_name));
  2122. kmsg->ikm_header.msgh_bits = mbits &~ MACH_MSGH_BITS_CIRCULAR;
  2123. kmsg->ikm_header.msgh_remote_port = dest_name;
  2124. kmsg->ikm_header.msgh_local_port = reply_name;
  2125. if (mbits & MACH_MSGH_BITS_COMPLEX) {
  2126. vm_offset_t saddr, eaddr;
  2127. saddr = (vm_offset_t) (&kmsg->ikm_header + 1);
  2128. eaddr = (vm_offset_t) &kmsg->ikm_header +
  2129. kmsg->ikm_header.msgh_size;
  2130. mr |= ipc_kmsg_copyout_body(saddr, eaddr, space, map);
  2131. }
  2132. return mr;
  2133. }
  2134. /*
  2135. * Routine: ipc_kmsg_copyout_dest
  2136. * Purpose:
  2137. * Copies out the destination port in the message.
  2138. * Destroys all other rights and memory in the message.
  2139. * Conditions:
  2140. * Nothing locked.
  2141. */
  2142. void
  2143. ipc_kmsg_copyout_dest(
  2144. ipc_kmsg_t kmsg,
  2145. ipc_space_t space)
  2146. {
  2147. mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits;
  2148. ipc_object_t dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
  2149. ipc_object_t reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
  2150. mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
  2151. mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
  2152. mach_port_t dest_name, reply_name;
  2153. printk(KERN_ERR,"ipc_kmsg_copyout_dest\n");
  2154. return -1;//BUG here -- assert fails
  2155. assert(IO_VALID(dest));
  2156. io_lock(dest);
  2157. if (io_active(dest)) {
  2158. ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
  2159. /* dest is unlocked */
  2160. } else {
  2161. io_release(dest);
  2162. io_check_unlock(dest);
  2163. dest_name = MACH_PORT_DEAD;
  2164. }
  2165. if (IO_VALID(reply)) {
  2166. ipc_object_destroy(reply, reply_type);
  2167. reply_name = MACH_PORT_NULL;
  2168. } else
  2169. reply_name = (mach_port_t) reply;
  2170. kmsg->ikm_header.msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
  2171. MACH_MSGH_BITS(reply_type, dest_type));
  2172. kmsg->ikm_header.msgh_local_port = dest_name;
  2173. kmsg->ikm_header.msgh_remote_port = reply_name;
  2174. if (mbits & MACH_MSGH_BITS_COMPLEX) {
  2175. vm_offset_t saddr, eaddr;
  2176. saddr = (vm_offset_t) (&kmsg->ikm_header + 1);
  2177. eaddr = (vm_offset_t) &kmsg->ikm_header +
  2178. kmsg->ikm_header.msgh_size;
  2179. ipc_kmsg_clean_body(saddr, eaddr);
  2180. }
  2181. }
  2182. #if MACH_KDB
  2183. char *
  2184. ipc_type_name(
  2185. int type_name,
  2186. boolean_t received)
  2187. {
  2188. switch (type_name) {
  2189. case MACH_MSG_TYPE_BOOLEAN:
  2190. return "boolean";
  2191. case MACH_MSG_TYPE_INTEGER_16:
  2192. return "short";
  2193. case MACH_MSG_TYPE_INTEGER_32:
  2194. return "int32";
  2195. case MACH_MSG_TYPE_INTEGER_64:
  2196. return "int64";
  2197. case MACH_MSG_TYPE_CHAR:
  2198. return "char";
  2199. case MACH_MSG_TYPE_BYTE:
  2200. return "byte";
  2201. case MACH_MSG_TYPE_REAL:
  2202. return "real";
  2203. case MACH_MSG_TYPE_STRING:
  2204. return "string";
  2205. case MACH_MSG_TYPE_PORT_NAME:
  2206. return "port_name";
  2207. case MACH_MSG_TYPE_MOVE_RECEIVE:
  2208. if (received) {
  2209. return "port_receive";
  2210. } else {
  2211. return "move_receive";
  2212. }
  2213. case MACH_MSG_TYPE_MOVE_SEND:
  2214. if (received) {
  2215. return "port_send";
  2216. } else {
  2217. return "move_send";
  2218. }
  2219. case MACH_MSG_TYPE_MOVE_SEND_ONCE:
  2220. if (received) {
  2221. return "port_send_once";
  2222. } else {
  2223. return "move_send_once";
  2224. }
  2225. case MACH_MSG_TYPE_COPY_SEND:
  2226. return "copy_send";
  2227. case MACH_MSG_TYPE_MAKE_SEND:
  2228. return "make_send";
  2229. case MACH_MSG_TYPE_MAKE_SEND_ONCE:
  2230. return "make_send_once";
  2231. default:
  2232. return (char *) 0;
  2233. }
  2234. }
  2235. void
  2236. ipc_print_type_name(
  2237. int type_name)
  2238. {
  2239. char *name = ipc_type_name(type_name, TRUE);
  2240. if (name) {
  2241. printf("%s", name);
  2242. } else {
  2243. printf("type%d", type_name);
  2244. }
  2245. }
  2246. /*
  2247. * ipc_kmsg_print [ debug ]
  2248. */
  2249. void
  2250. ipc_kmsg_print(ipc_kmsg_t kmsg)
  2251. {
  2252. db_printf("kmsg=0x%x\n", kmsg);
  2253. db_printf("ikm_next=0x%x,prev=0x%x,size=%d,marequest=0x%x",
  2254. kmsg->ikm_next,
  2255. kmsg->ikm_prev,
  2256. kmsg->ikm_size,
  2257. kmsg->ikm_marequest);
  2258. db_printf("\n");
  2259. ipc_msg_print(&kmsg->ikm_header);
  2260. }
  2261. /*
  2262. * ipc_msg_print [ debug ]
  2263. */
  2264. void
  2265. ipc_msg_print(mach_msg_header_t *msgh)
  2266. {
  2267. vm_offset_t saddr, eaddr;
  2268. db_printf("msgh_bits=0x%x: ", msgh->msgh_bits);
  2269. if (msgh->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
  2270. db_printf("complex,");
  2271. }
  2272. if (msgh->msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
  2273. db_printf("circular,");
  2274. }
  2275. if (msgh->msgh_bits & MACH_MSGH_BITS_COMPLEX_PORTS) {
  2276. db_printf("complex_ports,");
  2277. }
  2278. if (msgh->msgh_bits & MACH_MSGH_BITS_COMPLEX_DATA) {
  2279. db_printf("complex_data,");
  2280. }
  2281. if (msgh->msgh_bits & MACH_MSGH_BITS_MIGRATED) {
  2282. db_printf("migrated,");
  2283. }
  2284. if (msgh->msgh_bits & MACH_MSGH_BITS_UNUSED) {
  2285. db_printf("unused=0x%x,",
  2286. msgh->msgh_bits & MACH_MSGH_BITS_UNUSED);
  2287. }
  2288. db_printf("l=0x%x,r=0x%x\n",
  2289. MACH_MSGH_BITS_LOCAL(msgh->msgh_bits),
  2290. MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
  2291. db_printf("msgh_id=%d,size=%d,seqno=%d,",
  2292. msgh->msgh_id,
  2293. msgh->msgh_size,
  2294. msgh->msgh_seqno);
  2295. if (msgh->msgh_remote_port) {
  2296. db_printf("remote=0x%x(", msgh->msgh_remote_port);
  2297. ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
  2298. db_printf("),");
  2299. } else {
  2300. db_printf("remote=null,\n");
  2301. }
  2302. if (msgh->msgh_local_port) {
  2303. db_printf("local=0x%x(", msgh->msgh_local_port);
  2304. ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh->msgh_bits));
  2305. db_printf(")\n");
  2306. } else {
  2307. db_printf("local=null\n");
  2308. }
  2309. saddr = (vm_offset_t) (msgh + 1);
  2310. eaddr = (vm_offset_t) msgh + msgh->msgh_size;
  2311. while (saddr < eaddr) {
  2312. mach_msg_type_long_t *type;
  2313. mach_msg_type_name_t name;
  2314. mach_msg_type_size_t size;
  2315. mach_msg_type_number_t number;
  2316. boolean_t is_inline, longform, dealloc, is_port;
  2317. vm_size_t length;
  2318. type = (mach_msg_type_long_t *) saddr;
  2319. if (((eaddr - saddr) < sizeof(mach_msg_type_t)) ||
  2320. ((longform = ((mach_msg_type_t*)type)->msgt_longform) &&
  2321. ((eaddr - saddr) < sizeof(mach_msg_type_long_t)))) {
  2322. db_printf("*** msg too small\n");
  2323. return;
  2324. }
  2325. is_inline = ((mach_msg_type_t*)type)->msgt_inline;
  2326. dealloc = ((mach_msg_type_t*)type)->msgt_deallocate;
  2327. if (longform) {
  2328. /* This must be aligned */
  2329. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  2330. (is_misaligned(type))) {
  2331. saddr = ptr_align(saddr);
  2332. continue;
  2333. }
  2334. name = type->msgtl_name;
  2335. size = type->msgtl_size;
  2336. number = type->msgtl_number;
  2337. saddr += sizeof(mach_msg_type_long_t);
  2338. } else {
  2339. name = ((mach_msg_type_t*)type)->msgt_name;
  2340. size = ((mach_msg_type_t*)type)->msgt_size;
  2341. number = ((mach_msg_type_t*)type)->msgt_number;
  2342. saddr += sizeof(mach_msg_type_t);
  2343. }
  2344. db_printf("-- type=");
  2345. ipc_print_type_name(name);
  2346. if (! is_inline) {
  2347. db_printf(",ool");
  2348. }
  2349. if (dealloc) {
  2350. db_printf(",dealloc");
  2351. }
  2352. if (longform) {
  2353. db_printf(",longform");
  2354. }
  2355. db_printf(",size=%d,number=%d,addr=0x%x\n",
  2356. size,
  2357. number,
  2358. saddr);
  2359. is_port = MACH_MSG_TYPE_PORT_ANY(name);
  2360. if ((is_port && (size != PORT_T_SIZE_IN_BITS)) ||
  2361. (longform && ((type->msgtl_header.msgt_name != 0) ||
  2362. (type->msgtl_header.msgt_size != 0) ||
  2363. (type->msgtl_header.msgt_number != 0))) ||
  2364. (((mach_msg_type_t*)type)->msgt_unused != 0) ||
  2365. (dealloc && is_inline)) {
  2366. db_printf("*** invalid type\n");
  2367. return;
  2368. }
  2369. /* padding (ptrs and ports) ? */
  2370. if ((sizeof(natural_t) > sizeof(mach_msg_type_t)) &&
  2371. ((size >> 3) == sizeof(natural_t)))
  2372. saddr = ptr_align(saddr);
  2373. /* calculate length of data in bytes, rounding up */
  2374. length = ((number * size) + 7) >> 3;
  2375. if (is_inline) {
  2376. vm_size_t amount;
  2377. unsigned i, numwords;
  2378. /* inline data sizes round up to int boundaries */
  2379. amount = (length + 3) &~ 3;
  2380. if ((eaddr - saddr) < amount) {
  2381. db_printf("*** too small\n");
  2382. return;
  2383. }
  2384. numwords = amount / sizeof(int);
  2385. if (numwords > 8) {
  2386. numwords = 8;
  2387. }
  2388. for (i = 0; i < numwords; i++) {
  2389. db_printf("0x%x\n", ((int *) saddr)[i]);
  2390. }
  2391. if (numwords < amount / sizeof(int)) {
  2392. db_printf("...\n");
  2393. }
  2394. saddr += amount;
  2395. } else {
  2396. if ((eaddr - saddr) < sizeof(vm_offset_t)) {
  2397. db_printf("*** too small\n");
  2398. return;
  2399. }
  2400. db_printf("0x%x\n", * (vm_offset_t *) saddr);
  2401. saddr += sizeof(vm_offset_t);
  2402. }
  2403. }
  2404. }
  2405. #endif /* MACH_KDB */