mach_port.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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. */
  31. /*
  32. * File: ipc/mach_port.c
  33. * Author: Rich Draves
  34. * Date: 1989
  35. *
  36. * Exported kernel calls. See mach/mach_port.defs.
  37. */
  38. #pragma GCC diagnostic error "-Wundef"
  39. #include <glue/gnulinux.h>
  40. #include <linux/printk.h>
  41. #include <machine/pmap.h>
  42. #include <kern/debug.h>
  43. #include <kern/printf.h>
  44. #include <mach/port.h>
  45. #include <mach/kern_return.h>
  46. #include <mach/notify.h>
  47. #include <mach/mach_param.h>
  48. #include <mach/vm_param.h>
  49. #include <mach/vm_prot.h>
  50. #ifdef MIGRATING_THREADS
  51. #include <mach/rpc.h>
  52. #include <kern/task.h>
  53. #include <kern/act.h>
  54. #endif /* MIGRATING_THREADS */
  55. #include <vm/vm_map.h>
  56. #include <vm/vm_kern.h>
  57. #include <vm/vm_user.h>
  58. #include <ipc/ipc_entry.h>
  59. #include <ipc/ipc_space.h>
  60. #include <ipc/ipc_object.h>
  61. #include <ipc/ipc_notify.h>
  62. #include <ipc/ipc_port.h>
  63. #include <ipc/ipc_pset.h>
  64. #include <ipc/ipc_right.h>
  65. #include <ipc/mach_port.h>
  66. /*
  67. * Routine: mach_port_names_helper
  68. * Purpose:
  69. * A helper function for mach_port_names.
  70. */
  71. void
  72. mach_port_names_helper(
  73. ipc_port_timestamp_t timestamp,
  74. ipc_entry_t entry,
  75. mach_port_t name,
  76. mach_port_t *names,
  77. mach_port_type_t *types,
  78. ipc_entry_num_t *actualp)
  79. {
  80. ipc_entry_bits_t bits = entry->ie_bits;
  81. ipc_port_request_index_t request = entry->ie_request;
  82. mach_port_type_t type;
  83. ipc_entry_num_t actual;
  84. if (bits & MACH_PORT_TYPE_SEND_RIGHTS) {
  85. ipc_port_t port;
  86. boolean_t died;
  87. port = (ipc_port_t) entry->ie_object;
  88. assert(port != IP_NULL);
  89. /*
  90. * The timestamp serializes mach_port_names
  91. * with ipc_port_destroy. If the port died,
  92. * but after mach_port_names started, pretend
  93. * that it isn't dead.
  94. */
  95. ip_lock(port);
  96. died = (!ip_active(port) &&
  97. IP_TIMESTAMP_ORDER(port->ip_timestamp, timestamp));
  98. ip_unlock(port);
  99. if (died) {
  100. /* pretend this is a dead-name entry */
  101. bits &= ~(IE_BITS_TYPE_MASK|IE_BITS_MAREQUEST);
  102. bits |= MACH_PORT_TYPE_DEAD_NAME;
  103. if (request != 0)
  104. bits++;
  105. request = 0;
  106. }
  107. }
  108. type = IE_BITS_TYPE(bits);
  109. if (request != 0)
  110. type |= MACH_PORT_TYPE_DNREQUEST;
  111. if (bits & IE_BITS_MAREQUEST)
  112. type |= MACH_PORT_TYPE_MAREQUEST;
  113. actual = *actualp;
  114. names[actual] = name;
  115. types[actual] = type;
  116. *actualp = actual+1;
  117. }
  118. /*
  119. * Routine: mach_port_names [kernel call]
  120. * Purpose:
  121. * Retrieves a list of the rights present in the space,
  122. * along with type information. (Same as returned
  123. * by mach_port_type.) The names are returned in
  124. * no particular order, but they (and the type info)
  125. * are an accurate snapshot of the space.
  126. * Conditions:
  127. * Nothing locked.
  128. * Returns:
  129. * KERN_SUCCESS Arrays of names and types returned.
  130. * KERN_INVALID_TASK The space is null.
  131. * KERN_INVALID_TASK The space is dead.
  132. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  133. */
  134. #if 0
  135. kern_return_t
  136. mach_port_names(
  137. ipc_space_t space,
  138. mach_port_t **namesp,
  139. mach_msg_type_number_t *namesCnt,
  140. mach_port_type_t **typesp,
  141. mach_msg_type_number_t *typesCnt)
  142. {
  143. ipc_entry_num_t actual; /* this many names */
  144. ipc_port_timestamp_t timestamp; /* logical time of this operation */
  145. mach_port_t *names;
  146. mach_port_type_t *types;
  147. kern_return_t kr;
  148. vm_size_t size; /* size of allocated memory */
  149. vm_offset_t addr1; /* allocated memory, for names */
  150. vm_offset_t addr2; /* allocated memory, for types */
  151. vm_map_copy_t memory1; /* copied-in memory, for names */
  152. vm_map_copy_t memory2; /* copied-in memory, for types */
  153. /* safe simplifying assumption */
  154. assert_static(sizeof(mach_port_t) == sizeof(mach_port_type_t));
  155. if (space == IS_NULL)
  156. return KERN_INVALID_TASK;
  157. size = 0;
  158. for (;;) {
  159. ipc_entry_num_t bound;
  160. vm_size_t size_needed;
  161. is_read_lock(space);
  162. if (!space->is_active) {
  163. is_read_unlock(space);
  164. if (size != 0) {
  165. kmem_free(ipc_kernel_map, addr1, size);
  166. kmem_free(ipc_kernel_map, addr2, size);
  167. }
  168. return KERN_INVALID_TASK;
  169. }
  170. /* upper bound on number of names in the space */
  171. bound = space->is_size;
  172. size_needed = round_page(bound * sizeof(mach_port_t));
  173. if (size_needed <= size)
  174. break;
  175. is_read_unlock(space);
  176. if (size != 0) {
  177. kmem_free(ipc_kernel_map, addr1, size);
  178. kmem_free(ipc_kernel_map, addr2, size);
  179. }
  180. size = size_needed;
  181. kr = vm_allocate(ipc_kernel_map, &addr1, size, TRUE);
  182. if (kr != KERN_SUCCESS) {
  183. printf_once("no more room in ipc_kernel_map\n");
  184. return KERN_RESOURCE_SHORTAGE;
  185. }
  186. kr = vm_allocate(ipc_kernel_map, &addr2, size, TRUE);
  187. if (kr != KERN_SUCCESS) {
  188. printf_once("no more room in ipc_kernel_map\n");
  189. kmem_free(ipc_kernel_map, addr1, size);
  190. return KERN_RESOURCE_SHORTAGE;
  191. }
  192. /* can't fault while we hold locks */
  193. kr = vm_map_pageable(ipc_kernel_map, addr1, addr1 + size,
  194. VM_PROT_READ|VM_PROT_WRITE, TRUE, TRUE);
  195. assert(kr == KERN_SUCCESS);
  196. kr = vm_map_pageable(ipc_kernel_map, addr2, addr2 + size,
  197. VM_PROT_READ|VM_PROT_WRITE, TRUE, TRUE);
  198. assert(kr == KERN_SUCCESS);
  199. }
  200. /* space is read-locked and active */
  201. names = (mach_port_t *) addr1;
  202. types = (mach_port_type_t *) addr2;
  203. actual = 0;
  204. timestamp = ipc_port_timestamp();
  205. ipc_entry_t entry;
  206. struct rdxtree_iter iter;
  207. rdxtree_for_each(&space->is_map, &iter, entry) {
  208. ipc_entry_bits_t bits = entry->ie_bits;
  209. if (IE_BITS_TYPE(bits) != MACH_PORT_TYPE_NONE) {
  210. mach_port_names_helper(timestamp, entry, entry->ie_name,
  211. names, types, &actual);
  212. }
  213. }
  214. is_read_unlock(space);
  215. if (actual == 0) {
  216. memory1 = VM_MAP_COPY_NULL;
  217. memory2 = VM_MAP_COPY_NULL;
  218. if (size != 0) {
  219. kmem_free(ipc_kernel_map, addr1, size);
  220. kmem_free(ipc_kernel_map, addr2, size);
  221. }
  222. } else {
  223. vm_size_t size_used;
  224. size_used = round_page(actual * sizeof(mach_port_t));
  225. /*
  226. * Make used memory pageable and get it into
  227. * copied-in form. Free any unused memory.
  228. */
  229. kr = vm_map_pageable(ipc_kernel_map,
  230. addr1, addr1 + size_used,
  231. VM_PROT_NONE, TRUE, TRUE);
  232. assert(kr == KERN_SUCCESS);
  233. kr = vm_map_pageable(ipc_kernel_map,
  234. addr2, addr2 + size_used,
  235. VM_PROT_NONE, TRUE, TRUE);
  236. assert(kr == KERN_SUCCESS);
  237. kr = vm_map_copyin(ipc_kernel_map, addr1, size_used,
  238. TRUE, &memory1);
  239. assert(kr == KERN_SUCCESS);
  240. kr = vm_map_copyin(ipc_kernel_map, addr2, size_used,
  241. TRUE, &memory2);
  242. assert(kr == KERN_SUCCESS);
  243. if (size_used != size) {
  244. kmem_free(ipc_kernel_map,
  245. addr1 + size_used, size - size_used);
  246. kmem_free(ipc_kernel_map,
  247. addr2 + size_used, size - size_used);
  248. }
  249. }
  250. *namesp = (mach_port_t *) memory1;
  251. *namesCnt = actual;
  252. *typesp = (mach_port_type_t *) memory2;
  253. *typesCnt = actual;
  254. return KERN_SUCCESS;
  255. }
  256. #endif
  257. /*
  258. * Routine: mach_port_type [kernel call]
  259. * Purpose:
  260. * Retrieves the type of a right in the space.
  261. * The type is a bitwise combination of one or more
  262. * of the following type bits:
  263. * MACH_PORT_TYPE_SEND
  264. * MACH_PORT_TYPE_RECEIVE
  265. * MACH_PORT_TYPE_SEND_ONCE
  266. * MACH_PORT_TYPE_PORT_SET
  267. * MACH_PORT_TYPE_DEAD_NAME
  268. * In addition, the following pseudo-type bits may be present:
  269. * MACH_PORT_TYPE_DNREQUEST
  270. * A dead-name notification is requested.
  271. * MACH_PORT_TYPE_MAREQUEST
  272. * The send/receive right is blocked;
  273. * a msg-accepted notification is outstanding.
  274. * MACH_PORT_TYPE_COMPAT
  275. * This is a compatibility-mode right;
  276. * when the port dies, it will disappear
  277. * instead of turning into a dead-name.
  278. * Conditions:
  279. * Nothing locked.
  280. * Returns:
  281. * KERN_SUCCESS Type is returned.
  282. * KERN_INVALID_TASK The space is null.
  283. * KERN_INVALID_TASK The space is dead.
  284. * KERN_INVALID_NAME The name doesn't denote a right.
  285. */
  286. kern_return_t
  287. mach_port_type(
  288. ipc_space_t space,
  289. mach_port_t name,
  290. mach_port_type_t *typep)
  291. {
  292. mach_port_urefs_t urefs;
  293. ipc_entry_t entry;
  294. kern_return_t kr;
  295. if (space == IS_NULL)
  296. return KERN_INVALID_TASK;
  297. kr = ipc_right_lookup_write(space, name, &entry);
  298. if (kr != KERN_SUCCESS)
  299. return kr;
  300. /* space is write-locked and active */
  301. kr = ipc_right_info(space, name, entry, typep, &urefs);
  302. if (kr == KERN_SUCCESS)
  303. is_write_unlock(space);
  304. /* space is unlocked */
  305. return kr;
  306. }
  307. /*
  308. * Routine: mach_port_rename [kernel call]
  309. * Purpose:
  310. * Changes the name denoting a right,
  311. * from oname to nname.
  312. * Conditions:
  313. * Nothing locked.
  314. * Returns:
  315. * KERN_SUCCESS The right is renamed.
  316. * KERN_INVALID_TASK The space is null.
  317. * KERN_INVALID_TASK The space is dead.
  318. * KERN_INVALID_NAME The oname doesn't denote a right.
  319. * KERN_INVALID_VALUE The nname isn't a legal name.
  320. * KERN_NAME_EXISTS The nname already denotes a right.
  321. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  322. */
  323. kern_return_t
  324. mach_port_rename(
  325. ipc_space_t space,
  326. mach_port_t oname,
  327. mach_port_t nname)
  328. {
  329. if (space == IS_NULL)
  330. return KERN_INVALID_TASK;
  331. if (!MACH_PORT_VALID(nname))
  332. return KERN_INVALID_VALUE;
  333. return ipc_object_rename(space, oname, nname);
  334. }
  335. /*
  336. * Routine: mach_port_allocate_name [kernel call]
  337. * Purpose:
  338. * Allocates a right in a space, using a specific name
  339. * for the new right. Possible rights:
  340. * MACH_PORT_RIGHT_RECEIVE
  341. * MACH_PORT_RIGHT_PORT_SET
  342. * MACH_PORT_RIGHT_DEAD_NAME
  343. *
  344. * A new port (allocated with MACH_PORT_RIGHT_RECEIVE)
  345. * has no extant send or send-once rights and no queued
  346. * messages. Its queue limit is MACH_PORT_QLIMIT_DEFAULT
  347. * and its make-send count is 0. It is not a member of
  348. * a port set. It has no registered no-senders or
  349. * port-destroyed notification requests.
  350. *
  351. * A new port set has no members.
  352. *
  353. * A new dead name has one user reference.
  354. * Conditions:
  355. * Nothing locked.
  356. * Returns:
  357. * KERN_SUCCESS The right is allocated.
  358. * KERN_INVALID_TASK The space is null.
  359. * KERN_INVALID_TASK The space is dead.
  360. * KERN_INVALID_VALUE The name isn't a legal name.
  361. * KERN_INVALID_VALUE "right" isn't a legal kind of right.
  362. * KERN_NAME_EXISTS The name already denotes a right.
  363. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  364. */
  365. kern_return_t
  366. mach_port_allocate_name(
  367. ipc_space_t space,
  368. mach_port_right_t right,
  369. mach_port_t name)
  370. {
  371. kern_return_t kr;
  372. if (space == IS_NULL)
  373. return KERN_INVALID_TASK;
  374. if (!MACH_PORT_VALID(name))
  375. return KERN_INVALID_VALUE;
  376. switch (right) {
  377. case MACH_PORT_RIGHT_RECEIVE: {
  378. ipc_port_t port;
  379. kr = ipc_port_alloc_name(space, name, &port);
  380. if (kr == KERN_SUCCESS)
  381. ip_unlock(port);
  382. break;
  383. }
  384. case MACH_PORT_RIGHT_PORT_SET: {
  385. ipc_pset_t pset;
  386. kr = ipc_pset_alloc_name(space, name, &pset);
  387. if (kr == KERN_SUCCESS)
  388. ips_unlock(pset);
  389. break;
  390. }
  391. case MACH_PORT_RIGHT_DEAD_NAME:
  392. kr = ipc_object_alloc_dead_name(space, name);
  393. break;
  394. default:
  395. kr = KERN_INVALID_VALUE;
  396. break;
  397. }
  398. return kr;
  399. }
  400. /*
  401. * Routine: mach_port_allocate [kernel call]
  402. * Purpose:
  403. * Allocates a right in a space. Like mach_port_allocate_name,
  404. * except that the implementation picks a name for the right.
  405. * The name may be any legal name in the space that doesn't
  406. * currently denote a right.
  407. * Conditions:
  408. * Nothing locked.
  409. * Returns:
  410. * KERN_SUCCESS The right is allocated.
  411. * KERN_INVALID_TASK The space is null.
  412. * KERN_INVALID_TASK The space is dead.
  413. * KERN_INVALID_VALUE "right" isn't a legal kind of right.
  414. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  415. * KERN_NO_SPACE No room in space for another right.
  416. */
  417. kern_return_t
  418. mach_port_allocate(
  419. ipc_space_t space,
  420. mach_port_right_t right,
  421. mach_port_t *namep)
  422. {
  423. kern_return_t kr;
  424. if (space == IS_NULL)
  425. return KERN_INVALID_TASK;
  426. switch (right) {
  427. case MACH_PORT_RIGHT_RECEIVE: {
  428. ipc_port_t port;
  429. kr = ipc_port_alloc(space, namep, &port);
  430. if (kr == KERN_SUCCESS)
  431. ip_unlock(port);
  432. break;
  433. }
  434. case MACH_PORT_RIGHT_PORT_SET: {
  435. ipc_pset_t pset;
  436. kr = ipc_pset_alloc(space, namep, &pset);
  437. if (kr == KERN_SUCCESS)
  438. ips_unlock(pset);
  439. break;
  440. }
  441. case MACH_PORT_RIGHT_DEAD_NAME:
  442. kr = ipc_object_alloc_dead(space, namep);
  443. break;
  444. default:
  445. kr = KERN_INVALID_VALUE;
  446. break;
  447. }
  448. return (kr);
  449. }
  450. /*
  451. * Routine: mach_port_destroy [kernel call]
  452. * Purpose:
  453. * Cleans up and destroys all rights denoted by a name
  454. * in a space. The destruction of a receive right
  455. * destroys the port, unless a port-destroyed request
  456. * has been made for it; the destruction of a port-set right
  457. * destroys the port set.
  458. * Conditions:
  459. * Nothing locked.
  460. * Returns:
  461. * KERN_SUCCESS The name is destroyed.
  462. * KERN_INVALID_TASK The space is null.
  463. * KERN_INVALID_TASK The space is dead.
  464. * KERN_INVALID_NAME The name doesn't denote a right.
  465. */
  466. static volatile boolean_t mach_port_deallocate_debug = FALSE;
  467. kern_return_t
  468. mach_port_destroy(
  469. ipc_space_t space,
  470. mach_port_t name)
  471. {
  472. ipc_entry_t entry;
  473. kern_return_t kr;
  474. if (space == IS_NULL)
  475. return KERN_INVALID_TASK;
  476. kr = ipc_right_lookup_write(space, name, &entry);
  477. if (kr != KERN_SUCCESS) {
  478. if (MACH_PORT_VALID (name) && space == current_space()) {
  479. printf("task %.*s destroying a bogus port %lu, most probably a bug.\n", sizeof current_task()->name, current_task()->name, name);
  480. if (mach_port_deallocate_debug)
  481. SoftDebugger("mach_port_deallocate");
  482. }
  483. return kr;
  484. }
  485. /* space is write-locked and active */
  486. kr = ipc_right_destroy(space, name, entry); /* unlocks space */
  487. return kr;
  488. }
  489. /*
  490. * Routine: mach_port_deallocate [kernel call]
  491. * Purpose:
  492. * Deallocates a user reference from a send right,
  493. * send-once right, or a dead-name right. May
  494. * deallocate the right, if this is the last uref,
  495. * and destroy the name, if it doesn't denote
  496. * other rights.
  497. * Conditions:
  498. * Nothing locked.
  499. * Returns:
  500. * KERN_SUCCESS The uref is deallocated.
  501. * KERN_INVALID_TASK The space is null.
  502. * KERN_INVALID_TASK The space is dead.
  503. * KERN_INVALID_NAME The name doesn't denote a right.
  504. * KERN_INVALID_RIGHT The right isn't correct.
  505. */
  506. kern_return_t
  507. mach_port_deallocate(
  508. ipc_space_t space,
  509. mach_port_t name)
  510. {
  511. ipc_entry_t entry;
  512. kern_return_t kr;
  513. if (space == IS_NULL)
  514. return KERN_INVALID_TASK;
  515. kr = ipc_right_lookup_write(space, name, &entry);
  516. if (kr != KERN_SUCCESS) {
  517. if (MACH_PORT_VALID (name) && space == current_space()) {
  518. printf("task %.*s deallocating a bogus port %lu, most probably a bug.\n", sizeof current_task()->name, current_task()->name, name);
  519. if (mach_port_deallocate_debug)
  520. SoftDebugger("mach_port_deallocate");
  521. }
  522. return kr;
  523. }
  524. /* space is write-locked */
  525. kr = ipc_right_dealloc(space, name, entry); /* unlocks space */
  526. return kr;
  527. }
  528. /*
  529. * Routine: mach_port_get_refs [kernel call]
  530. * Purpose:
  531. * Retrieves the number of user references held by a right.
  532. * Receive rights, port-set rights, and send-once rights
  533. * always have one user reference. Returns zero if the
  534. * name denotes a right, but not the queried right.
  535. * Conditions:
  536. * Nothing locked.
  537. * Returns:
  538. * KERN_SUCCESS Number of urefs returned.
  539. * KERN_INVALID_TASK The space is null.
  540. * KERN_INVALID_TASK The space is dead.
  541. * KERN_INVALID_VALUE "right" isn't a legal value.
  542. * KERN_INVALID_NAME The name doesn't denote a right.
  543. */
  544. kern_return_t
  545. mach_port_get_refs(
  546. ipc_space_t space,
  547. mach_port_t name,
  548. mach_port_right_t right,
  549. mach_port_urefs_t *urefsp)
  550. {
  551. mach_port_type_t type;
  552. mach_port_urefs_t urefs;
  553. ipc_entry_t entry;
  554. kern_return_t kr;
  555. if (space == IS_NULL)
  556. return KERN_INVALID_TASK;
  557. if (right >= MACH_PORT_RIGHT_NUMBER)
  558. return KERN_INVALID_VALUE;
  559. kr = ipc_right_lookup_write(space, name, &entry);
  560. if (kr != KERN_SUCCESS)
  561. return kr;
  562. /* space is write-locked and active */
  563. kr = ipc_right_info(space, name, entry, &type, &urefs); /* unlocks */
  564. if (kr != KERN_SUCCESS)
  565. return kr; /* space is unlocked */
  566. is_write_unlock(space);
  567. if (type & MACH_PORT_TYPE(right))
  568. switch (right) {
  569. case MACH_PORT_RIGHT_SEND_ONCE:
  570. assert(urefs == 1);
  571. /* fall-through */
  572. case MACH_PORT_RIGHT_PORT_SET:
  573. case MACH_PORT_RIGHT_RECEIVE:
  574. *urefsp = 1;
  575. break;
  576. case MACH_PORT_RIGHT_DEAD_NAME:
  577. case MACH_PORT_RIGHT_SEND:
  578. assert(urefs > 0);
  579. *urefsp = urefs;
  580. break;
  581. default:
  582. panic("mach_port_get_refs: strange rights");
  583. }
  584. else
  585. *urefsp = 0;
  586. return kr;
  587. }
  588. /*
  589. * Routine: mach_port_mod_refs
  590. * Purpose:
  591. * Modifies the number of user references held by a right.
  592. * The resulting number of user references must be non-negative.
  593. * If it is zero, the right is deallocated. If the name
  594. * doesn't denote other rights, it is destroyed.
  595. * Conditions:
  596. * Nothing locked.
  597. * Returns:
  598. * KERN_SUCCESS Modified number of urefs.
  599. * KERN_INVALID_TASK The space is null.
  600. * KERN_INVALID_TASK The space is dead.
  601. * KERN_INVALID_VALUE "right" isn't a legal value.
  602. * KERN_INVALID_NAME The name doesn't denote a right.
  603. * KERN_INVALID_RIGHT Name doesn't denote specified right.
  604. * KERN_INVALID_VALUE Impossible modification to urefs.
  605. * KERN_UREFS_OVERFLOW Urefs would overflow.
  606. */
  607. kern_return_t
  608. mach_port_mod_refs(
  609. ipc_space_t space,
  610. mach_port_t name,
  611. mach_port_right_t right,
  612. mach_port_delta_t delta)
  613. {
  614. ipc_entry_t entry;
  615. kern_return_t kr;
  616. if (space == IS_NULL)
  617. return KERN_INVALID_TASK;
  618. if (right >= MACH_PORT_RIGHT_NUMBER)
  619. return KERN_INVALID_VALUE;
  620. kr = ipc_right_lookup_write(space, name, &entry);
  621. if (kr != KERN_SUCCESS) {
  622. if (MACH_PORT_VALID (name) && space == current_space()) {
  623. printf("task %.*s %screasing a bogus port "
  624. "%lu by %d, most probably a bug.\n",
  625. sizeof current_task()->name,
  626. current_task()->name,
  627. delta < 0 ? "de" : "in", name,
  628. delta < 0 ? -delta : delta);
  629. if (mach_port_deallocate_debug)
  630. SoftDebugger("mach_port_mod_refs");
  631. }
  632. return kr;
  633. }
  634. /* space is write-locked and active */
  635. kr = ipc_right_delta(space, name, entry, right, delta); /* unlocks */
  636. return kr;
  637. }
  638. /*
  639. * Routine: mach_port_set_qlimit [kernel call]
  640. * Purpose:
  641. * Changes a receive right's queue limit.
  642. * The new queue limit must be between 0 and
  643. * MACH_PORT_QLIMIT_MAX, inclusive.
  644. * Conditions:
  645. * Nothing locked.
  646. * Returns:
  647. * KERN_SUCCESS Set queue limit.
  648. * KERN_INVALID_TASK The space is null.
  649. * KERN_INVALID_TASK The space is dead.
  650. * KERN_INVALID_NAME The name doesn't denote a right.
  651. * KERN_INVALID_RIGHT Name doesn't denote receive rights.
  652. * KERN_INVALID_VALUE Illegal queue limit.
  653. */
  654. kern_return_t
  655. mach_port_set_qlimit(
  656. ipc_space_t space,
  657. mach_port_t name,
  658. mach_port_msgcount_t qlimit)
  659. {
  660. ipc_port_t port;
  661. kern_return_t kr;
  662. if (space == IS_NULL)
  663. return KERN_INVALID_TASK;
  664. if (qlimit > MACH_PORT_QLIMIT_MAX)
  665. return KERN_INVALID_VALUE;
  666. kr = ipc_port_translate_receive(space, name, &port);
  667. if (kr != KERN_SUCCESS)
  668. return kr;
  669. /* port is locked and active */
  670. ipc_port_set_qlimit(port, qlimit);
  671. ip_unlock(port);
  672. return KERN_SUCCESS;
  673. }
  674. /*
  675. * Routine: mach_port_set_mscount [kernel call]
  676. * Purpose:
  677. * Changes a receive right's make-send count.
  678. * Conditions:
  679. * Nothing locked.
  680. * Returns:
  681. * KERN_SUCCESS Set make-send count.
  682. * KERN_INVALID_TASK The space is null.
  683. * KERN_INVALID_TASK The space is dead.
  684. * KERN_INVALID_NAME The name doesn't denote a right.
  685. * KERN_INVALID_RIGHT Name doesn't denote receive rights.
  686. */
  687. kern_return_t
  688. mach_port_set_mscount(
  689. ipc_space_t space,
  690. mach_port_t name,
  691. mach_port_mscount_t mscount)
  692. {
  693. ipc_port_t port;
  694. kern_return_t kr;
  695. if (space == IS_NULL)
  696. return KERN_INVALID_TASK;
  697. kr = ipc_port_translate_receive(space, name, &port);
  698. if (kr != KERN_SUCCESS)
  699. return kr;
  700. /* port is locked and active */
  701. ipc_port_set_mscount(port, mscount);
  702. ip_unlock(port);
  703. return KERN_SUCCESS;
  704. }
  705. /*
  706. * Routine: mach_port_set_seqno [kernel call]
  707. * Purpose:
  708. * Changes a receive right's sequence number.
  709. * Conditions:
  710. * Nothing locked.
  711. * Returns:
  712. * KERN_SUCCESS Set sequence number.
  713. * KERN_INVALID_TASK The space is null.
  714. * KERN_INVALID_TASK The space is dead.
  715. * KERN_INVALID_NAME The name doesn't denote a right.
  716. * KERN_INVALID_RIGHT Name doesn't denote receive rights.
  717. */
  718. kern_return_t
  719. mach_port_set_seqno(
  720. ipc_space_t space,
  721. mach_port_t name,
  722. mach_port_seqno_t seqno)
  723. {
  724. ipc_port_t port;
  725. kern_return_t kr;
  726. if (space == IS_NULL)
  727. return KERN_INVALID_TASK;
  728. kr = ipc_port_translate_receive(space, name, &port);
  729. if (kr != KERN_SUCCESS)
  730. return kr;
  731. /* port is locked and active */
  732. ipc_port_set_seqno(port, seqno);
  733. ip_unlock(port);
  734. return KERN_SUCCESS;
  735. }
  736. /*
  737. * Routine: mach_port_gst_helper
  738. * Purpose:
  739. * A helper function for mach_port_get_set_status.
  740. */
  741. void
  742. mach_port_gst_helper(
  743. ipc_pset_t pset,
  744. ipc_port_t port,
  745. ipc_entry_num_t maxnames,
  746. mach_port_t *names,
  747. ipc_entry_num_t *actualp)
  748. {
  749. ipc_pset_t ip_pset;
  750. mach_port_t name;
  751. assert(port != IP_NULL);
  752. ip_lock(port);
  753. assert(ip_active(port));
  754. name = port->ip_receiver_name;
  755. assert(name != MACH_PORT_NULL);
  756. ip_pset = port->ip_pset;
  757. ip_unlock(port);
  758. if (pset == ip_pset) {
  759. ipc_entry_num_t actual = *actualp;
  760. if (actual < maxnames)
  761. names[actual] = name;
  762. *actualp = actual+1;
  763. }
  764. }
  765. /*
  766. * Routine: mach_port_get_set_status [kernel call]
  767. * Purpose:
  768. * Retrieves a list of members in a port set.
  769. * Returns the space's name for each receive right member.
  770. * Conditions:
  771. * Nothing locked.
  772. * Returns:
  773. * KERN_SUCCESS Retrieved list of members.
  774. * KERN_INVALID_TASK The space is null.
  775. * KERN_INVALID_TASK The space is dead.
  776. * KERN_INVALID_NAME The name doesn't denote a right.
  777. * KERN_INVALID_RIGHT Name doesn't denote a port set.
  778. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  779. */
  780. #if 0
  781. kern_return_t
  782. mach_port_get_set_status(
  783. ipc_space_t space,
  784. mach_port_t name,
  785. mach_port_t **members,
  786. mach_msg_type_number_t *membersCnt)
  787. {
  788. ipc_entry_num_t actual; /* this many members */
  789. ipc_entry_num_t maxnames; /* space for this many members */
  790. kern_return_t kr;
  791. vm_size_t size; /* size of allocated memory */
  792. vm_offset_t addr; /* allocated memory */
  793. vm_map_copy_t memory; /* copied-in memory */
  794. if (space == IS_NULL)
  795. return KERN_INVALID_TASK;
  796. size = PAGE_SIZE; /* initial guess */
  797. for (;;) {
  798. ipc_entry_t entry;
  799. mach_port_t *names;
  800. ipc_pset_t pset;
  801. kr = vm_allocate(ipc_kernel_map, &addr, size, TRUE);
  802. if (kr != KERN_SUCCESS) {
  803. printf_once("no more room in ipc_kernel_map\n");
  804. return KERN_RESOURCE_SHORTAGE;
  805. }
  806. /* can't fault while we hold locks */
  807. kr = vm_map_pageable(ipc_kernel_map, addr, addr + size,
  808. VM_PROT_READ|VM_PROT_WRITE, TRUE, TRUE);
  809. assert(kr == KERN_SUCCESS);
  810. kr = ipc_right_lookup_read(space, name, &entry);
  811. if (kr != KERN_SUCCESS) {
  812. kmem_free(ipc_kernel_map, addr, size);
  813. return kr;
  814. }
  815. /* space is read-locked and active */
  816. if (IE_BITS_TYPE(entry->ie_bits) != MACH_PORT_TYPE_PORT_SET) {
  817. is_read_unlock(space);
  818. kmem_free(ipc_kernel_map, addr, size);
  819. return KERN_INVALID_RIGHT;
  820. }
  821. pset = (ipc_pset_t) entry->ie_object;
  822. assert(pset != IPS_NULL);
  823. /* the port set must be active */
  824. names = (mach_port_t *) addr;
  825. maxnames = size / sizeof(mach_port_t);
  826. actual = 0;
  827. ipc_entry_t ientry;
  828. struct rdxtree_iter iter;
  829. rdxtree_for_each(&space->is_map, &iter, ientry) {
  830. ipc_entry_bits_t bits = ientry->ie_bits;
  831. if (bits & MACH_PORT_TYPE_RECEIVE) {
  832. ipc_port_t port =
  833. (ipc_port_t) ientry->ie_object;
  834. mach_port_gst_helper(pset, port, maxnames,
  835. names, &actual);
  836. }
  837. }
  838. is_read_unlock(space);
  839. if (actual <= maxnames)
  840. break;
  841. /* didn't have enough memory; allocate more */
  842. kmem_free(ipc_kernel_map, addr, size);
  843. size = round_page(actual * sizeof(mach_port_t)) + PAGE_SIZE;
  844. }
  845. if (actual == 0) {
  846. memory = VM_MAP_COPY_NULL;
  847. kmem_free(ipc_kernel_map, addr, size);
  848. } else {
  849. vm_size_t size_used;
  850. size_used = round_page(actual * sizeof(mach_port_t));
  851. /*
  852. * Make used memory pageable and get it into
  853. * copied-in form. Free any unused memory.
  854. */
  855. kr = vm_map_pageable(ipc_kernel_map,
  856. addr, addr + size_used,
  857. VM_PROT_NONE, TRUE, TRUE);
  858. assert(kr == KERN_SUCCESS);
  859. kr = vm_map_copyin(ipc_kernel_map, addr, size_used,
  860. TRUE, &memory);
  861. assert(kr == KERN_SUCCESS);
  862. if (size_used != size)
  863. kmem_free(ipc_kernel_map,
  864. addr + size_used, size - size_used);
  865. }
  866. *members = (mach_port_t *) memory;
  867. *membersCnt = actual;
  868. return KERN_SUCCESS;
  869. }
  870. #endif
  871. /*
  872. * Routine: mach_port_move_member [kernel call]
  873. * Purpose:
  874. * If after is MACH_PORT_NULL, removes member
  875. * from the port set it is in. Otherwise, adds
  876. * member to after, removing it from any set
  877. * it might already be in.
  878. * Conditions:
  879. * Nothing locked.
  880. * Returns:
  881. * KERN_SUCCESS Moved the port.
  882. * KERN_INVALID_TASK The space is null.
  883. * KERN_INVALID_TASK The space is dead.
  884. * KERN_INVALID_NAME Member didn't denote a right.
  885. * KERN_INVALID_RIGHT Member didn't denote a receive right.
  886. * KERN_INVALID_NAME After didn't denote a right.
  887. * KERN_INVALID_RIGHT After didn't denote a port set right.
  888. * KERN_NOT_IN_SET
  889. * After is MACH_PORT_NULL and Member isn't in a port set.
  890. */
  891. kern_return_t
  892. mach_port_move_member(
  893. ipc_space_t space,
  894. mach_port_t member,
  895. mach_port_t after)
  896. {
  897. ipc_entry_t entry;
  898. ipc_port_t port;
  899. ipc_pset_t nset;
  900. kern_return_t kr;
  901. if (space == IS_NULL)
  902. return KERN_INVALID_TASK;
  903. kr = ipc_right_lookup_read(space, member, &entry);
  904. if (kr != KERN_SUCCESS)
  905. return kr;
  906. /* space is read-locked and active */
  907. if ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) {
  908. is_read_unlock(space);
  909. return KERN_INVALID_RIGHT;
  910. }
  911. port = (ipc_port_t) entry->ie_object;
  912. assert(port != IP_NULL);
  913. if (after == MACH_PORT_NULL)
  914. nset = IPS_NULL;
  915. else {
  916. entry = ipc_entry_lookup(space, after);
  917. if (entry == IE_NULL) {
  918. is_read_unlock(space);
  919. return KERN_INVALID_NAME;
  920. }
  921. if ((entry->ie_bits & MACH_PORT_TYPE_PORT_SET) == 0) {
  922. is_read_unlock(space);
  923. return KERN_INVALID_RIGHT;
  924. }
  925. nset = (ipc_pset_t) entry->ie_object;
  926. assert(nset != IPS_NULL);
  927. }
  928. kr = ipc_pset_move(space, port, nset);
  929. /* space is unlocked */
  930. return kr;
  931. }
  932. /*
  933. * Routine: mach_port_request_notification [kernel call]
  934. * Purpose:
  935. * Requests a notification. The caller supplies
  936. * a send-once right for the notification to use,
  937. * and the call returns the previously registered
  938. * send-once right, if any. Possible types:
  939. *
  940. * MACH_NOTIFY_PORT_DESTROYED
  941. * Requests a port-destroyed notification
  942. * for a receive right. Sync should be zero.
  943. * MACH_NOTIFY_NO_SENDERS
  944. * Requests a no-senders notification for a
  945. * receive right. If there are currently no
  946. * senders, sync is less than or equal to the
  947. * current make-send count, and a send-once right
  948. * is supplied, then an immediate no-senders
  949. * notification is generated.
  950. * MACH_NOTIFY_DEAD_NAME
  951. * Requests a dead-name notification for a send
  952. * or receive right. If the name is already a
  953. * dead name, sync is non-zero, and a send-once
  954. * right is supplied, then an immediate dead-name
  955. * notification is generated.
  956. * Conditions:
  957. * Nothing locked.
  958. * Returns:
  959. * KERN_SUCCESS Requested a notification.
  960. * KERN_INVALID_TASK The space is null.
  961. * KERN_INVALID_TASK The space is dead.
  962. * KERN_INVALID_VALUE Bad id value.
  963. * KERN_INVALID_NAME Name doesn't denote a right.
  964. * KERN_INVALID_RIGHT Name doesn't denote appropriate right.
  965. * KERN_INVALID_CAPABILITY The notify port is dead.
  966. * MACH_NOTIFY_PORT_DESTROYED:
  967. * KERN_INVALID_VALUE Sync isn't zero.
  968. * MACH_NOTIFY_DEAD_NAME:
  969. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  970. * KERN_INVALID_ARGUMENT Name denotes dead name, but
  971. * sync is zero or notify is IP_NULL.
  972. * KERN_UREFS_OVERFLOW Name denotes dead name, but
  973. * generating immediate notif. would overflow urefs.
  974. */
  975. kern_return_t
  976. mach_port_request_notification(
  977. ipc_space_t space,
  978. mach_port_t name,
  979. mach_msg_id_t id,
  980. mach_port_mscount_t sync,
  981. ipc_port_t notify,
  982. ipc_port_t *previousp)
  983. {
  984. kern_return_t kr;
  985. if (space == IS_NULL)
  986. return KERN_INVALID_TASK;
  987. if (notify == IP_DEAD)
  988. return KERN_INVALID_CAPABILITY;
  989. switch (id) {
  990. case MACH_NOTIFY_PORT_DESTROYED: {
  991. ipc_port_t port, previous;
  992. if (sync != 0)
  993. return KERN_INVALID_VALUE;
  994. kr = ipc_port_translate_receive(space, name, &port);
  995. if (kr != KERN_SUCCESS)
  996. return kr;
  997. /* port is locked and active */
  998. ipc_port_pdrequest(port, notify, &previous);
  999. /* port is unlocked */
  1000. *previousp = previous;
  1001. break;
  1002. }
  1003. case MACH_NOTIFY_NO_SENDERS: {
  1004. ipc_port_t port;
  1005. kr = ipc_port_translate_receive(space, name, &port);
  1006. if (kr != KERN_SUCCESS)
  1007. return kr;
  1008. /* port is locked and active */
  1009. ipc_port_nsrequest(port, sync, notify, previousp);
  1010. /* port is unlocked */
  1011. break;
  1012. }
  1013. case MACH_NOTIFY_DEAD_NAME:
  1014. kr = ipc_right_dnrequest(space, name, sync != 0,
  1015. notify, previousp);
  1016. if (kr != KERN_SUCCESS)
  1017. return kr;
  1018. break;
  1019. default:
  1020. return KERN_INVALID_VALUE;
  1021. }
  1022. return KERN_SUCCESS;
  1023. }
  1024. /*
  1025. * Routine: mach_port_insert_right [kernel call]
  1026. * Purpose:
  1027. * Inserts a right into a space, as if the space
  1028. * voluntarily received the right in a message,
  1029. * except that the right gets the specified name.
  1030. * Conditions:
  1031. * Nothing locked.
  1032. * Returns:
  1033. * KERN_SUCCESS Inserted the right.
  1034. * KERN_INVALID_TASK The space is null.
  1035. * KERN_INVALID_TASK The space is dead.
  1036. * KERN_INVALID_VALUE The name isn't a legal name.
  1037. * KERN_NAME_EXISTS The name already denotes a right.
  1038. * KERN_INVALID_VALUE Message doesn't carry a port right.
  1039. * KERN_INVALID_CAPABILITY Port is null or dead.
  1040. * KERN_UREFS_OVERFLOW Urefs limit would be exceeded.
  1041. * KERN_RIGHT_EXISTS Space has rights under another name.
  1042. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  1043. */
  1044. kern_return_t
  1045. mach_port_insert_right(
  1046. ipc_space_t space,
  1047. mach_port_t name,
  1048. ipc_port_t poly,
  1049. mach_msg_type_name_t polyPoly)
  1050. {
  1051. if (space == IS_NULL)
  1052. return KERN_INVALID_TASK;
  1053. if (!MACH_PORT_VALID(name) ||
  1054. !MACH_MSG_TYPE_PORT_ANY_RIGHT(polyPoly))
  1055. return KERN_INVALID_VALUE;
  1056. if (!IO_VALID((ipc_object_t)poly))
  1057. return KERN_INVALID_CAPABILITY;
  1058. return ipc_object_copyout_name(space, (ipc_object_t)poly,
  1059. polyPoly, FALSE, name);
  1060. }
  1061. /*
  1062. * Routine: mach_port_extract_right [kernel call]
  1063. * Purpose:
  1064. * Extracts a right from a space, as if the space
  1065. * voluntarily sent the right to the caller.
  1066. * Conditions:
  1067. * Nothing locked.
  1068. * Returns:
  1069. * KERN_SUCCESS Extracted the right.
  1070. * KERN_INVALID_TASK The space is null.
  1071. * KERN_INVALID_TASK The space is dead.
  1072. * KERN_INVALID_VALUE Requested type isn't a port right.
  1073. * KERN_INVALID_NAME Name doesn't denote a right.
  1074. * KERN_INVALID_RIGHT Name doesn't denote appropriate right.
  1075. */
  1076. kern_return_t
  1077. mach_port_extract_right(
  1078. ipc_space_t space,
  1079. mach_port_t name,
  1080. mach_msg_type_name_t msgt_name,
  1081. ipc_port_t *poly,
  1082. mach_msg_type_name_t *polyPoly)
  1083. {
  1084. kern_return_t kr;
  1085. if (space == IS_NULL)
  1086. return KERN_INVALID_TASK;
  1087. if (!MACH_MSG_TYPE_PORT_ANY(msgt_name))
  1088. return KERN_INVALID_VALUE;
  1089. kr = ipc_object_copyin(space, name, msgt_name, (ipc_object_t *) poly);
  1090. if (kr == KERN_SUCCESS)
  1091. *polyPoly = ipc_object_copyin_type(msgt_name);
  1092. return kr;
  1093. }
  1094. /*
  1095. * Routine: mach_port_get_receive_status [kernel call]
  1096. * Purpose:
  1097. * Retrieves mucho info about a receive right.
  1098. * Conditions:
  1099. * Nothing locked.
  1100. * Returns:
  1101. * KERN_SUCCESS Retrieved status.
  1102. * KERN_INVALID_TASK The space is null.
  1103. * KERN_INVALID_TASK The space is dead.
  1104. * KERN_INVALID_NAME The name doesn't denote a right.
  1105. * KERN_INVALID_RIGHT Name doesn't denote receive rights.
  1106. */
  1107. kern_return_t
  1108. mach_port_get_receive_status(
  1109. ipc_space_t space,
  1110. mach_port_t name,
  1111. mach_port_status_t *statusp)
  1112. {
  1113. ipc_port_t port;
  1114. kern_return_t kr;
  1115. if (space == IS_NULL)
  1116. return KERN_INVALID_TASK;
  1117. kr = ipc_port_translate_receive(space, name, &port);
  1118. if (kr != KERN_SUCCESS)
  1119. return kr;
  1120. /* port is locked and active */
  1121. if (port->ip_pset != IPS_NULL) {
  1122. ipc_pset_t pset = port->ip_pset;
  1123. ips_lock(pset);
  1124. if (!ips_active(pset)) {
  1125. ipc_pset_remove(pset, port);
  1126. ips_check_unlock(pset);
  1127. goto no_port_set;
  1128. } else {
  1129. statusp->mps_pset = pset->ips_local_name;
  1130. imq_lock(&pset->ips_messages);
  1131. statusp->mps_seqno = port->ip_seqno;
  1132. imq_unlock(&pset->ips_messages);
  1133. ips_unlock(pset);
  1134. assert(MACH_PORT_VALID(statusp->mps_pset));
  1135. }
  1136. } else {
  1137. no_port_set:
  1138. statusp->mps_pset = MACH_PORT_NULL;
  1139. imq_lock(&port->ip_messages);
  1140. statusp->mps_seqno = port->ip_seqno;
  1141. imq_unlock(&port->ip_messages);
  1142. }
  1143. statusp->mps_mscount = port->ip_mscount;
  1144. statusp->mps_qlimit = port->ip_qlimit;
  1145. statusp->mps_msgcount = port->ip_msgcount;
  1146. statusp->mps_sorights = port->ip_sorights;
  1147. statusp->mps_srights = port->ip_srights > 0;
  1148. statusp->mps_pdrequest = port->ip_pdrequest != IP_NULL;
  1149. statusp->mps_nsrequest = port->ip_nsrequest != IP_NULL;
  1150. ip_unlock(port);
  1151. return KERN_SUCCESS;
  1152. }
  1153. #ifdef MIGRATING_THREADS
  1154. kern_return_t
  1155. mach_port_set_rpcinfo(
  1156. ipc_space_t space,
  1157. mach_port_t name,
  1158. void *rpc_info,
  1159. unsigned int rpc_info_count)
  1160. {
  1161. ipc_target_t target;
  1162. ipc_object_t object;
  1163. kern_return_t kr;
  1164. if (space == IS_NULL)
  1165. return KERN_INVALID_TASK;
  1166. kr = ipc_object_translate(space, name,
  1167. MACH_PORT_RIGHT_PORT_SET, &object);
  1168. if (kr == KERN_SUCCESS)
  1169. target = &((ipc_pset_t)object)->ips_target;
  1170. else {
  1171. kr = ipc_object_translate(space, name,
  1172. MACH_PORT_RIGHT_RECEIVE, &object);
  1173. if (kr != KERN_SUCCESS)
  1174. return kr;
  1175. target = &((ipc_port_t)object)->ip_target;
  1176. }
  1177. /* port/pset is locked and active */
  1178. kr = port_machine_set_rpcinfo(target, rpc_info, rpc_info_count);
  1179. io_unlock(object);
  1180. return kr;
  1181. }
  1182. #if 1
  1183. int sacts, maxsacts;
  1184. #endif
  1185. void sact_count(void)
  1186. {
  1187. printf("%d server activations in use, %d max\n", sacts, maxsacts);
  1188. }
  1189. kern_return_t
  1190. mach_port_create_act(
  1191. task_t task,
  1192. mach_port_t name,
  1193. vm_offset_t user_stack,
  1194. vm_offset_t user_rbuf,
  1195. vm_size_t user_rbuf_size,
  1196. Act **out_act)
  1197. {
  1198. ipc_target_t target;
  1199. ipc_space_t space;
  1200. ipc_object_t object;
  1201. kern_return_t kr;
  1202. Act *act;
  1203. if (task == 0)
  1204. return KERN_INVALID_TASK;
  1205. /* First create the new activation. */
  1206. kr = act_create(task, user_stack, user_rbuf, user_rbuf_size, &act);
  1207. if (kr != KERN_SUCCESS)
  1208. return kr;
  1209. space = task->itk_space;
  1210. kr = ipc_object_translate(space, name,
  1211. MACH_PORT_RIGHT_PORT_SET, &object);
  1212. if (kr == KERN_SUCCESS)
  1213. target = &((ipc_pset_t)object)->ips_target;
  1214. else {
  1215. kr = ipc_object_translate(space, name,
  1216. MACH_PORT_RIGHT_RECEIVE, &object);
  1217. if (kr != KERN_SUCCESS) {
  1218. act_terminate(act);
  1219. act_deallocate(act);
  1220. return kr;
  1221. }
  1222. target = &((ipc_port_t)object)->ip_target;
  1223. }
  1224. /* port/pset is locked and active */
  1225. #if 0
  1226. printf("act port/pset %08x ipc_target %08x stack %08x act %08x\n",
  1227. object, target, user_stack, act);
  1228. #endif
  1229. /* Assign the activation to the port's actpool. */
  1230. kr = act_set_target(act, target);
  1231. if (kr != KERN_SUCCESS) {
  1232. io_unlock(object);
  1233. act_terminate(act);
  1234. act_deallocate(act);
  1235. return kr;
  1236. }
  1237. #if 0
  1238. printf(" actpool %08x act %08x\n", target->ip_actpool, act);
  1239. #endif
  1240. io_unlock(object);
  1241. /* Pass our reference to the activation back to the user. */
  1242. *out_act = act;
  1243. #if 1
  1244. sacts++;
  1245. if (sacts > maxsacts)
  1246. maxsacts = sacts;
  1247. act->mact.pcb->ss.mpsfu_high = 0x69;
  1248. #endif
  1249. return KERN_SUCCESS;
  1250. }
  1251. #ifdef RPCKERNELSIG
  1252. kern_return_t
  1253. mach_port_set_syscall_right(
  1254. task_t task,
  1255. mach_port_t name)
  1256. {
  1257. ipc_entry_t entry;
  1258. kern_return_t kr;
  1259. if (task == IS_NULL)
  1260. return KERN_INVALID_TASK;
  1261. kr = ipc_right_lookup_write(task, name, &entry);
  1262. if (kr != KERN_SUCCESS) {
  1263. return kr;
  1264. }
  1265. if (!(entry->ie_bits & MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND))) {
  1266. is_write_unlock(space);
  1267. return KERN_INVALID_RIGHT;
  1268. }
  1269. task->syscall_ipc_entry = *entry;
  1270. is_write_unlock(space);
  1271. return KERN_SUCCESS;
  1272. }
  1273. #endif
  1274. #endif /* MIGRATING_THREADS */
  1275. /*
  1276. * Routine: mach_port_set_protected_payload [kernel call]
  1277. * Purpose:
  1278. * Changes a receive right's protected payload.
  1279. * Conditions:
  1280. * Nothing locked.
  1281. * Returns:
  1282. * KERN_SUCCESS Set protected payload.
  1283. * KERN_INVALID_TASK The space is null.
  1284. * KERN_INVALID_TASK The space is dead.
  1285. * KERN_INVALID_NAME The name doesn't denote a right.
  1286. * KERN_INVALID_RIGHT Name doesn't denote receive rights.
  1287. */
  1288. kern_return_t
  1289. mach_port_set_protected_payload(
  1290. ipc_space_t space,
  1291. mach_port_t name,
  1292. unsigned long payload)
  1293. {
  1294. ipc_port_t port;
  1295. kern_return_t kr;
  1296. if (space == IS_NULL)
  1297. return KERN_INVALID_TASK;
  1298. kr = ipc_port_translate_receive(space, name, &port);
  1299. if (kr != KERN_SUCCESS)
  1300. return kr;
  1301. /* port is locked and active */
  1302. ipc_port_set_protected_payload(port, payload);
  1303. ip_unlock(port);
  1304. return KERN_SUCCESS;
  1305. }
  1306. /*
  1307. * Routine: mach_port_clear_protected_payload [kernel call]
  1308. * Purpose:
  1309. * Clears a receive right's protected payload.
  1310. * Conditions:
  1311. * Nothing locked.
  1312. * Returns:
  1313. * KERN_SUCCESS Clear protected payload.
  1314. * KERN_INVALID_TASK The space is null.
  1315. * KERN_INVALID_TASK The space is dead.
  1316. * KERN_INVALID_NAME The name doesn't denote a right.
  1317. * KERN_INVALID_RIGHT Name doesn't denote receive rights.
  1318. */
  1319. kern_return_t
  1320. mach_port_clear_protected_payload(
  1321. ipc_space_t space,
  1322. mach_port_t name)
  1323. {
  1324. ipc_port_t port;
  1325. kern_return_t kr;
  1326. if (space == IS_NULL)
  1327. return KERN_INVALID_TASK;
  1328. kr = ipc_port_translate_receive(space, name, &port);
  1329. if (kr != KERN_SUCCESS)
  1330. return kr;
  1331. /* port is locked and active */
  1332. ipc_port_clear_protected_payload(port);
  1333. ip_unlock(port);
  1334. return KERN_SUCCESS;
  1335. }
  1336. #if MACH_KDB
  1337. void
  1338. db_debug_port_references (boolean_t enable)
  1339. {
  1340. mach_port_deallocate_debug = enable;
  1341. }
  1342. #endif /* MACH_KDB */
  1343. void test_ports(void)
  1344. {
  1345. kern_return_t kr;
  1346. ipc_space_t space;
  1347. kr = ipc_space_create(&ipc_table_entries[0],&space);
  1348. if(kr != KERN_SUCCESS)
  1349. {
  1350. printk(KERN_CRIT "ipc_space_create failed with %i\n",kr);
  1351. return;
  1352. }
  1353. int i;
  1354. for(i=0;i<10000;i++)
  1355. {
  1356. mach_port_t name=0;
  1357. kr = mach_port_allocate(space,MACH_PORT_RIGHT_RECEIVE,&name);
  1358. if(kr != KERN_SUCCESS)
  1359. {
  1360. printk(KERN_CRIT "mach_port_allocate failed with %i\n",kr);
  1361. break;
  1362. }
  1363. }
  1364. }