2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * File: ipc/ipc_port.h
58 * Definitions for ports.
61 #ifndef _IPC_IPC_PORT_H_
62 #define _IPC_IPC_PORT_H_
66 #include <mach_assert.h>
67 #include <mach_debug.h>
69 #include <mach/mach_types.h>
70 #include <mach/boolean.h>
71 #include <mach/kern_return.h>
72 #include <mach/port.h>
74 #include <kern/kern_types.h>
76 #include <ipc/ipc_types.h>
77 #include <ipc/ipc_object.h>
78 #include <ipc/ipc_mqueue.h>
79 #include <ipc/ipc_space.h>
82 * A receive right (port) can be in four states:
83 * 1) dead (not active, ip_timestamp has death time)
84 * 2) in a space (ip_receiver_name != 0, ip_receiver points
85 * to the space but doesn't hold a ref for it)
86 * 3) in transit (ip_receiver_name == 0, ip_destination points
87 * to the destination port and holds a ref for it)
88 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
90 * If the port is active, and ip_receiver points to some space,
91 * then ip_receiver_name != 0, and that space holds receive rights.
92 * If the port is not active, then ip_timestamp contains a timestamp
93 * taken when the port was destroyed.
96 typedef unsigned int ipc_port_timestamp_t
;
101 * Initial sub-structure in common with ipc_pset and rpc_port
102 * First element is an ipc_object
104 struct ipc_object ip_object
;
107 struct ipc_space
*receiver
;
108 struct ipc_port
*destination
;
109 ipc_port_timestamp_t timestamp
;
112 ipc_kobject_t ip_kobject
;
113 mach_port_mscount_t ip_mscount
;
114 mach_port_rights_t ip_srights
;
115 mach_port_rights_t ip_sorights
;
117 struct ipc_port
*ip_nsrequest
;
118 struct ipc_port
*ip_pdrequest
;
119 struct ipc_port_request
*ip_dnrequests
;
121 unsigned int ip_pset_count
;
122 struct ipc_mqueue ip_messages
;
123 struct ipc_kmsg
*ip_premsg
;
127 * These fields are needed for the use of XMM.
128 * Few ports need this information; it should
129 * be kept in XMM instead (TBD). XXX
131 long ip_norma_xmm_object_refs
;
132 struct ipc_port
*ip_norma_xmm_object
;
136 #define IP_NSPARES 10
137 #define IP_CALLSTACK_MAX 10
138 queue_chain_t ip_port_links
; /* all allocated ports */
139 thread_t ip_thread
; /* who made me? thread context */
140 unsigned long ip_timetrack
; /* give an idea of "when" created */
141 natural_t ip_callstack
[IP_CALLSTACK_MAX
]; /* stack trace */
142 unsigned long ip_spares
[IP_NSPARES
]; /* for debugging */
143 #endif /* MACH_ASSERT */
148 #define ip_references ip_object.io_references
149 #define ip_bits ip_object.io_bits
150 #define ip_receiver_name ip_object.io_receiver_name
152 #define ip_receiver data.receiver
153 #define ip_destination data.destination
154 #define ip_timestamp data.timestamp
156 #define IP_NULL IPC_PORT_NULL
157 #define IP_DEAD IPC_PORT_DEAD
158 #define IP_VALID(port) IPC_PORT_VALID(port)
160 #define ip_active(port) io_active(&(port)->ip_object)
161 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
162 #define ip_lock(port) io_lock(&(port)->ip_object)
163 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
164 #define ip_unlock(port) io_unlock(&(port)->ip_object)
165 #define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
167 #define ip_reference(port) io_reference(&(port)->ip_object)
168 #define ip_release(port) io_release(&(port)->ip_object)
170 #define ip_kotype(port) io_kotype(&(port)->ip_object)
173 * JMM - Preallocation flag
174 * This flag indicates that there is a message buffer preallocated for this
175 * port and we should use that when sending (from the kernel) rather than
176 * allocate a new one. This avoids deadlocks during notification message
177 * sends by critical system threads (which may be needed to free memory and
178 * therefore cannot be blocked waiting for memory themselves).
180 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
181 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
183 #define IP_SET_PREALLOC(port, kmsg) \
185 (port)->ip_bits |= IP_BIT_PREALLOC; \
186 (port)->ip_premsg = (kmsg); \
189 #define IP_CLEAR_PREALLOC(port, kmsg) \
191 assert((port)->ip_premsg == kmsg); \
192 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
193 (port)->ip_premsg = IKM_NULL; \
197 struct ipc_port_request
{
199 struct ipc_port
*port
;
200 ipc_port_request_index_t index
;
204 mach_port_name_t name
;
205 struct ipc_table_size
*size
;
209 #define ipr_next notify.index
210 #define ipr_size name.size
212 #define ipr_soright notify.port
213 #define ipr_name name.name
216 * Taking the ipc_port_multiple lock grants the privilege
217 * to lock multiple ports at once. No ports must locked
221 decl_mutex_data(extern,ipc_port_multiple_lock_data
)
223 #define ipc_port_multiple_lock_init() \
224 mutex_init(&ipc_port_multiple_lock_data, 0)
226 #define ipc_port_multiple_lock() \
227 mutex_lock(&ipc_port_multiple_lock_data)
229 #define ipc_port_multiple_unlock() \
230 mutex_unlock(&ipc_port_multiple_lock_data)
233 * The port timestamp facility provides timestamps
234 * for port destruction. It is used to serialize
235 * mach_port_names with port death.
238 decl_mutex_data(extern,ipc_port_timestamp_lock_data
)
239 extern ipc_port_timestamp_t ipc_port_timestamp_data
;
241 #define ipc_port_timestamp_lock_init() \
242 mutex_init(&ipc_port_timestamp_lock_data, 0)
244 #define ipc_port_timestamp_lock() \
245 mutex_lock(&ipc_port_timestamp_lock_data)
247 #define ipc_port_timestamp_unlock() \
248 mutex_unlock(&ipc_port_timestamp_lock_data)
250 /* Retrieve a port timestamp value */
251 extern ipc_port_timestamp_t
ipc_port_timestamp(void);
254 * Compares two timestamps, and returns TRUE if one
255 * happened before two. Note that this formulation
256 * works when the timestamp wraps around at 2^32,
257 * as long as one and two aren't too far apart.
260 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
262 #define ipc_port_translate_receive(space, name, portp) \
263 ipc_object_translate((space), (name), \
264 MACH_PORT_RIGHT_RECEIVE, \
265 (ipc_object_t *) (portp))
267 #define ipc_port_translate_send(space, name, portp) \
268 ipc_object_translate((space), (name), \
269 MACH_PORT_RIGHT_SEND, \
270 (ipc_object_t *) (portp))
272 /* Allocate a dead-name request slot */
276 mach_port_name_t name
,
278 ipc_port_request_index_t
*indexp
);
280 /* Grow a port's table of dead-name requests */
281 extern kern_return_t
ipc_port_dngrow(
283 ipc_table_elems_t target_size
);
285 /* Cancel a dead-name request and return the send-once right */
286 extern ipc_port_t
ipc_port_dncancel(
288 mach_port_name_t name
,
289 ipc_port_request_index_t index
);
291 #define ipc_port_dnrename(port, index, oname, nname) \
293 ipc_port_request_t ipr, table; \
295 assert(ip_active(port)); \
297 table = port->ip_dnrequests; \
298 assert(table != IPR_NULL); \
300 ipr = &table[index]; \
301 assert(ipr->ipr_name == oname); \
303 ipr->ipr_name = nname; \
306 /* Make a port-deleted request */
307 extern void ipc_port_pdrequest(
310 ipc_port_t
*previousp
);
312 /* Make a no-senders request */
313 extern void ipc_port_nsrequest(
315 mach_port_mscount_t sync
,
317 ipc_port_t
*previousp
);
319 #define ipc_port_set_mscount(port, mscount) \
321 assert(ip_active(port)); \
323 (port)->ip_mscount = (mscount); \
326 /* Prepare a receive right for transmission/destruction */
327 extern void ipc_port_clear_receiver(
330 /* Initialize a newly-allocated port */
331 extern void ipc_port_init(
334 mach_port_name_t name
);
336 /* Allocate a port */
337 extern kern_return_t
ipc_port_alloc(
339 mach_port_name_t
*namep
,
342 /* Allocate a port, with a specific name */
343 extern kern_return_t
ipc_port_alloc_name(
345 mach_port_name_t name
,
348 /* Generate dead name notifications */
349 extern void ipc_port_dnnotify(
351 ipc_port_request_t dnrequests
);
354 extern void ipc_port_destroy(
357 /* Check if queueing "port" in a message for "dest" would create a circular
358 group of ports and messages */
360 ipc_port_check_circularity(
364 /* Make a send-once notify port from a receive right */
365 extern ipc_port_t
ipc_port_lookup_notify(
367 mach_port_name_t name
);
369 /* Make a naked send right from a receive right - port locked and active */
370 extern ipc_port_t
ipc_port_make_send_locked(
373 /* Make a naked send right from a receive right */
374 extern ipc_port_t
ipc_port_make_send(
377 /* Make a naked send right from another naked send right */
378 extern ipc_port_t
ipc_port_copy_send(
381 /* Copyout a naked send right */
382 extern mach_port_name_t
ipc_port_copyout_send(
386 /* Release a (valid) naked send right */
387 extern void ipc_port_release_send(
390 /* Make a naked send-once right from a receive right */
391 extern ipc_port_t
ipc_port_make_sonce(
394 /* Release a naked send-once right */
395 extern void ipc_port_release_sonce(
398 /* Release a naked (in limbo or in transit) receive right */
399 extern void ipc_port_release_receive(
402 /* Allocate a port in a special space */
403 extern ipc_port_t
ipc_port_alloc_special(
406 /* Deallocate a port in a special space */
407 extern void ipc_port_dealloc_special(
412 /* Track low-level port deallocation */
413 extern void ipc_port_track_dealloc(
416 /* Initialize general port debugging state */
417 extern void ipc_port_debug_init(void);
418 #endif /* MACH_ASSERT */
420 #define ipc_port_alloc_kernel() \
421 ipc_port_alloc_special(ipc_space_kernel)
422 #define ipc_port_dealloc_kernel(port) \
423 ipc_port_dealloc_special((port), ipc_space_kernel)
425 #define ipc_port_alloc_reply() \
426 ipc_port_alloc_special(ipc_space_reply)
427 #define ipc_port_dealloc_reply(port) \
428 ipc_port_dealloc_special((port), ipc_space_reply)
430 #define ipc_port_reference(port) \
431 ipc_object_reference(&(port)->ip_object)
433 #define ipc_port_release(port) \
434 ipc_object_release(&(port)->ip_object)
436 #endif /* _IPC_IPC_PORT_H_ */