ipc_port.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/ipc_port.h
  33. * Author: Rich Draves
  34. * Date: 1989
  35. *
  36. * Definitions for ports.
  37. */
  38. #ifndef _IPC_IPC_PORT_H_
  39. #define _IPC_IPC_PORT_H_
  40. #include <mach/boolean.h>
  41. #include <mach/kern_return.h>
  42. #include <mach/port.h>
  43. #include <kern/lock.h>
  44. #include <kern/macros.h>
  45. #include <kern/ipc_kobject.h>
  46. #include <ipc/ipc_mqueue.h>
  47. #include <ipc/ipc_table.h>
  48. #include <ipc/ipc_thread.h>
  49. #include <ipc/ipc_object.h>
  50. #include "ipc_target.h"
  51. #include <mach/rpc.h>
  52. /*
  53. * A receive right (port) can be in four states:
  54. * 1) dead (not active, ip_timestamp has death time)
  55. * 2) in a space (ip_receiver_name != 0, ip_receiver points
  56. * to the space but doesn't hold a ref for it)
  57. * 3) in transit (ip_receiver_name == 0, ip_destination points
  58. * to the destination port and holds a ref for it)
  59. * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
  60. *
  61. * If the port is active, and ip_receiver points to some space,
  62. * then ip_receiver_name != 0, and that space holds receive rights.
  63. * If the port is not active, then ip_timestamp contains a timestamp
  64. * taken when the port was destroyed.
  65. */
  66. typedef unsigned int ipc_port_timestamp_t;
  67. struct ipc_port {
  68. struct ipc_target ip_target;
  69. /* This points to the ip_target above if this port isn't on a port set;
  70. otherwise it points to the port set's ips_target. */
  71. struct ipc_target *ip_cur_target;
  72. union {
  73. struct ipc_space *receiver;
  74. struct ipc_port *destination;
  75. ipc_port_timestamp_t timestamp;
  76. } data;
  77. ipc_kobject_t ip_kobject;
  78. mach_port_mscount_t ip_mscount;
  79. mach_port_rights_t ip_srights;
  80. mach_port_rights_t ip_sorights;
  81. struct ipc_port *ip_nsrequest;
  82. struct ipc_port *ip_pdrequest;
  83. struct ipc_port_request *ip_dnrequests;
  84. struct ipc_pset *ip_pset;
  85. mach_port_seqno_t ip_seqno; /* locked by message queue */
  86. mach_port_msgcount_t ip_msgcount;
  87. mach_port_msgcount_t ip_qlimit;
  88. struct ipc_thread_queue ip_blocked;
  89. unsigned long ip_protected_payload;
  90. };
  91. #define ip_object ip_target.ipt_object
  92. #define ip_receiver_name ip_target.ipt_name
  93. #define ip_messages ip_target.ipt_messages
  94. #define ip_references ip_object.io_references
  95. #define ip_bits ip_object.io_bits
  96. #define ip_receiver data.receiver
  97. #define ip_destination data.destination
  98. #define ip_timestamp data.timestamp
  99. #define IP_NULL ((ipc_port_t) IO_NULL)
  100. #define IP_DEAD ((ipc_port_t) IO_DEAD)
  101. #define IP_VALID(port) IO_VALID(&(port)->ip_object)
  102. #define ip_active(port) io_active(&(port)->ip_object)
  103. #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
  104. #define ip_lock(port) io_lock(&(port)->ip_object)
  105. #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
  106. #define ip_unlock(port) io_unlock(&(port)->ip_object)
  107. #define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
  108. #define ip_reference(port) io_reference(&(port)->ip_object)
  109. #define ip_release(port) io_release(&(port)->ip_object)
  110. #define ip_alloc() ((ipc_port_t) io_alloc(IOT_PORT))
  111. #define ip_free(port) io_free(IOT_PORT, &(port)->ip_object)
  112. #define ip_kotype(port) io_kotype(&(port)->ip_object)
  113. typedef ipc_table_index_t ipc_port_request_index_t;
  114. typedef struct ipc_port_request {
  115. union {
  116. struct ipc_port *port;
  117. ipc_port_request_index_t index;
  118. } notify;
  119. union {
  120. mach_port_t name;
  121. struct ipc_table_size *size;
  122. } name;
  123. } *ipc_port_request_t;
  124. #define ipr_next notify.index
  125. #define ipr_size name.size
  126. #define ipr_soright notify.port
  127. #define ipr_name name.name
  128. #define IPR_NULL ((ipc_port_request_t) 0)
  129. /*
  130. * Taking the ipc_port_multiple lock grants the privilege
  131. * to lock multiple ports at once. No ports must locked
  132. * when it is taken.
  133. */
  134. decl_simple_lock_data(extern, ipc_port_multiple_lock_data)
  135. #define ipc_port_multiple_lock_init() \
  136. simple_lock_init(&ipc_port_multiple_lock_data)
  137. #define ipc_port_multiple_lock() \
  138. simple_lock(&ipc_port_multiple_lock_data)
  139. #define ipc_port_multiple_unlock() \
  140. simple_unlock(&ipc_port_multiple_lock_data)
  141. /*
  142. * The port timestamp facility provides timestamps
  143. * for port destruction. It is used to serialize
  144. * mach_port_names with port death.
  145. */
  146. decl_simple_lock_data(extern, ipc_port_timestamp_lock_data)
  147. extern ipc_port_timestamp_t ipc_port_timestamp_data;
  148. #define ipc_port_timestamp_lock_init() \
  149. simple_lock_init(&ipc_port_timestamp_lock_data)
  150. #define ipc_port_timestamp_lock() \
  151. simple_lock(&ipc_port_timestamp_lock_data)
  152. #define ipc_port_timestamp_unlock() \
  153. simple_unlock(&ipc_port_timestamp_lock_data)
  154. extern ipc_port_timestamp_t
  155. ipc_port_timestamp(void);
  156. /*
  157. * Compares two timestamps, and returns TRUE if one
  158. * happened before two. Note that this formulation
  159. * works when the timestamp wraps around at 2^32,
  160. * as long as one and two aren't too far apart.
  161. */
  162. #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
  163. #define ipc_port_translate_receive(space, name, portp) \
  164. ipc_object_translate((space), (name), \
  165. MACH_PORT_RIGHT_RECEIVE, \
  166. (ipc_object_t *) (portp))
  167. #define ipc_port_translate_send(space, name, portp) \
  168. ipc_object_translate((space), (name), \
  169. MACH_PORT_RIGHT_SEND, \
  170. (ipc_object_t *) (portp))
  171. extern kern_return_t
  172. ipc_port_dnrequest(ipc_port_t, mach_port_t, ipc_port_t,
  173. ipc_port_request_index_t *);
  174. extern kern_return_t
  175. ipc_port_dngrow(ipc_port_t);
  176. extern ipc_port_t
  177. ipc_port_dncancel(ipc_port_t, mach_port_t, ipc_port_request_index_t);
  178. #define ipc_port_dnrename(port, index, oname, nname) \
  179. MACRO_BEGIN \
  180. ipc_port_request_t ipr, table; \
  181. \
  182. assert(ip_active(port)); \
  183. \
  184. table = port->ip_dnrequests; \
  185. assert(table != IPR_NULL); \
  186. \
  187. ipr = &table[index]; \
  188. assert(ipr->ipr_name == oname); \
  189. \
  190. ipr->ipr_name = nname; \
  191. MACRO_END
  192. /* Make a port-deleted request */
  193. extern void ipc_port_pdrequest(
  194. ipc_port_t port,
  195. ipc_port_t notify,
  196. ipc_port_t *previousp);
  197. /* Make a no-senders request */
  198. extern void ipc_port_nsrequest(
  199. ipc_port_t port,
  200. mach_port_mscount_t sync,
  201. ipc_port_t notify,
  202. ipc_port_t *previousp);
  203. /* Change a port's queue limit */
  204. extern void ipc_port_set_qlimit(
  205. ipc_port_t port,
  206. mach_port_msgcount_t qlimit);
  207. #define ipc_port_set_mscount(port, mscount) \
  208. MACRO_BEGIN \
  209. assert(ip_active(port)); \
  210. \
  211. (port)->ip_mscount = (mscount); \
  212. MACRO_END
  213. extern struct ipc_mqueue *
  214. ipc_port_lock_mqueue(ipc_port_t);
  215. extern void
  216. ipc_port_set_seqno(ipc_port_t, mach_port_seqno_t);
  217. extern void
  218. ipc_port_set_protected_payload(ipc_port_t, unsigned long);
  219. extern void
  220. ipc_port_clear_protected_payload(ipc_port_t);
  221. extern void
  222. ipc_port_clear_receiver(ipc_port_t);
  223. extern void
  224. ipc_port_init(ipc_port_t, ipc_space_t, mach_port_t);
  225. extern kern_return_t
  226. ipc_port_alloc(ipc_space_t, mach_port_t *, ipc_port_t *);
  227. extern kern_return_t
  228. ipc_port_alloc_name(ipc_space_t, mach_port_t, ipc_port_t *);
  229. extern void
  230. ipc_port_destroy(ipc_port_t);
  231. extern boolean_t
  232. ipc_port_check_circularity(ipc_port_t, ipc_port_t);
  233. extern ipc_port_t
  234. ipc_port_lookup_notify(ipc_space_t, mach_port_t);
  235. extern ipc_port_t
  236. ipc_port_make_send(ipc_port_t);
  237. extern ipc_port_t
  238. ipc_port_copy_send(ipc_port_t);
  239. extern mach_port_t
  240. ipc_port_copyout_send(ipc_port_t, ipc_space_t);
  241. extern void
  242. ipc_port_release_send(ipc_port_t);
  243. extern ipc_port_t
  244. ipc_port_make_sonce(ipc_port_t);
  245. extern void
  246. ipc_port_release_sonce(ipc_port_t);
  247. extern void
  248. ipc_port_release_receive(ipc_port_t);
  249. extern ipc_port_t
  250. ipc_port_alloc_special(ipc_space_t);
  251. extern void
  252. ipc_port_dealloc_special(ipc_port_t, ipc_space_t);
  253. #define ipc_port_alloc_kernel() \
  254. ipc_port_alloc_special(ipc_space_kernel)
  255. #define ipc_port_dealloc_kernel(port) \
  256. ipc_port_dealloc_special((port), ipc_space_kernel)
  257. #define ipc_port_alloc_reply() \
  258. ipc_port_alloc_special(ipc_space_reply)
  259. #define ipc_port_dealloc_reply(port) \
  260. ipc_port_dealloc_special((port), ipc_space_reply)
  261. #define ipc_port_reference(port) \
  262. ipc_object_reference(&(port)->ip_object)
  263. #define ipc_port_release(port) \
  264. ipc_object_release(&(port)->ip_object)
  265. static inline boolean_t
  266. ipc_port_flag_protected_payload(const struct ipc_port *port)
  267. {
  268. return !! (port->ip_target.ipt_object.io_bits
  269. & IO_BITS_PROTECTED_PAYLOAD);
  270. }
  271. static inline void
  272. ipc_port_flag_protected_payload_set(struct ipc_port *port)
  273. {
  274. port->ip_target.ipt_object.io_bits |= IO_BITS_PROTECTED_PAYLOAD;
  275. }
  276. static inline void
  277. ipc_port_flag_protected_payload_clear(struct ipc_port *port)
  278. {
  279. port->ip_target.ipt_object.io_bits &= ~IO_BITS_PROTECTED_PAYLOAD;
  280. }
  281. #endif /* _IPC_IPC_PORT_H_ */