2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: ipc/ipc_port.h
63 * Definitions for ports.
66 #ifndef _IPC_IPC_PORT_H_
67 #define _IPC_IPC_PORT_H_
71 #include <mach_assert.h>
72 #include <mach_debug.h>
74 #include <mach/mach_types.h>
75 #include <mach/boolean.h>
76 #include <mach/kern_return.h>
77 #include <mach/port.h>
79 #include <kern/kern_types.h>
81 #include <ipc/ipc_types.h>
82 #include <ipc/ipc_object.h>
83 #include <ipc/ipc_mqueue.h>
84 #include <ipc/ipc_space.h>
87 * A receive right (port) can be in four states:
88 * 1) dead (not active, ip_timestamp has death time)
89 * 2) in a space (ip_receiver_name != 0, ip_receiver points
90 * to the space but doesn't hold a ref for it)
91 * 3) in transit (ip_receiver_name == 0, ip_destination points
92 * to the destination port and holds a ref for it)
93 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
95 * If the port is active, and ip_receiver points to some space,
96 * then ip_receiver_name != 0, and that space holds receive rights.
97 * If the port is not active, then ip_timestamp contains a timestamp
98 * taken when the port was destroyed.
101 typedef unsigned int ipc_port_timestamp_t
;
106 * Initial sub-structure in common with ipc_pset and rpc_port
107 * First element is an ipc_object
109 struct ipc_object ip_object
;
112 struct ipc_space
*receiver
;
113 struct ipc_port
*destination
;
114 ipc_port_timestamp_t timestamp
;
117 ipc_kobject_t ip_kobject
;
118 mach_port_mscount_t ip_mscount
;
119 mach_port_rights_t ip_srights
;
120 mach_port_rights_t ip_sorights
;
122 struct ipc_port
*ip_nsrequest
;
123 struct ipc_port
*ip_pdrequest
;
124 struct ipc_port_request
*ip_dnrequests
;
126 unsigned int ip_pset_count
;
127 struct ipc_mqueue ip_messages
;
128 struct ipc_kmsg
*ip_premsg
;
132 * These fields are needed for the use of XMM.
133 * Few ports need this information; it should
134 * be kept in XMM instead (TBD). XXX
136 long ip_norma_xmm_object_refs
;
137 struct ipc_port
*ip_norma_xmm_object
;
141 #define IP_NSPARES 10
142 #define IP_CALLSTACK_MAX 10
143 queue_chain_t ip_port_links
; /* all allocated ports */
144 thread_t ip_thread
; /* who made me? thread context */
145 unsigned long ip_timetrack
; /* give an idea of "when" created */
146 natural_t ip_callstack
[IP_CALLSTACK_MAX
]; /* stack trace */
147 unsigned long ip_spares
[IP_NSPARES
]; /* for debugging */
148 #endif /* MACH_ASSERT */
153 #define ip_references ip_object.io_references
154 #define ip_bits ip_object.io_bits
155 #define ip_receiver_name ip_object.io_receiver_name
157 #define ip_receiver data.receiver
158 #define ip_destination data.destination
159 #define ip_timestamp data.timestamp
161 #define IP_NULL IPC_PORT_NULL
162 #define IP_DEAD IPC_PORT_DEAD
163 #define IP_VALID(port) IPC_PORT_VALID(port)
165 #define ip_active(port) io_active(&(port)->ip_object)
166 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
167 #define ip_lock(port) io_lock(&(port)->ip_object)
168 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
169 #define ip_unlock(port) io_unlock(&(port)->ip_object)
170 #define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
172 #define ip_reference(port) io_reference(&(port)->ip_object)
173 #define ip_release(port) io_release(&(port)->ip_object)
175 #define ip_kotype(port) io_kotype(&(port)->ip_object)
178 * JMM - Preallocation flag
179 * This flag indicates that there is a message buffer preallocated for this
180 * port and we should use that when sending (from the kernel) rather than
181 * allocate a new one. This avoids deadlocks during notification message
182 * sends by critical system threads (which may be needed to free memory and
183 * therefore cannot be blocked waiting for memory themselves).
185 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
186 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
188 #define IP_SET_PREALLOC(port, kmsg) \
190 (port)->ip_bits |= IP_BIT_PREALLOC; \
191 (port)->ip_premsg = (kmsg); \
194 #define IP_CLEAR_PREALLOC(port, kmsg) \
196 assert((port)->ip_premsg == kmsg); \
197 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
198 (port)->ip_premsg = IKM_NULL; \
202 struct ipc_port_request
{
204 struct ipc_port
*port
;
205 ipc_port_request_index_t index
;
209 mach_port_name_t name
;
210 struct ipc_table_size
*size
;
214 #define ipr_next notify.index
215 #define ipr_size name.size
217 #define ipr_soright notify.port
218 #define ipr_name name.name
221 * Taking the ipc_port_multiple lock grants the privilege
222 * to lock multiple ports at once. No ports must locked
226 decl_mutex_data(extern,ipc_port_multiple_lock_data
)
228 #define ipc_port_multiple_lock_init() \
229 mutex_init(&ipc_port_multiple_lock_data, 0)
231 #define ipc_port_multiple_lock() \
232 mutex_lock(&ipc_port_multiple_lock_data)
234 #define ipc_port_multiple_unlock() \
235 mutex_unlock(&ipc_port_multiple_lock_data)
238 * The port timestamp facility provides timestamps
239 * for port destruction. It is used to serialize
240 * mach_port_names with port death.
243 decl_mutex_data(extern,ipc_port_timestamp_lock_data
)
244 extern ipc_port_timestamp_t ipc_port_timestamp_data
;
246 #define ipc_port_timestamp_lock_init() \
247 mutex_init(&ipc_port_timestamp_lock_data, 0)
249 #define ipc_port_timestamp_lock() \
250 mutex_lock(&ipc_port_timestamp_lock_data)
252 #define ipc_port_timestamp_unlock() \
253 mutex_unlock(&ipc_port_timestamp_lock_data)
255 /* Retrieve a port timestamp value */
256 extern ipc_port_timestamp_t
ipc_port_timestamp(void);
259 * Compares two timestamps, and returns TRUE if one
260 * happened before two. Note that this formulation
261 * works when the timestamp wraps around at 2^32,
262 * as long as one and two aren't too far apart.
265 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
267 #define ipc_port_translate_receive(space, name, portp) \
268 ipc_object_translate((space), (name), \
269 MACH_PORT_RIGHT_RECEIVE, \
270 (ipc_object_t *) (portp))
272 #define ipc_port_translate_send(space, name, portp) \
273 ipc_object_translate((space), (name), \
274 MACH_PORT_RIGHT_SEND, \
275 (ipc_object_t *) (portp))
277 /* Allocate a dead-name request slot */
281 mach_port_name_t name
,
283 ipc_port_request_index_t
*indexp
);
285 /* Grow a port's table of dead-name requests */
286 extern kern_return_t
ipc_port_dngrow(
288 ipc_table_elems_t target_size
);
290 /* Cancel a dead-name request and return the send-once right */
291 extern ipc_port_t
ipc_port_dncancel(
293 mach_port_name_t name
,
294 ipc_port_request_index_t index
);
296 #define ipc_port_dnrename(port, index, oname, nname) \
298 ipc_port_request_t ipr, table; \
300 assert(ip_active(port)); \
302 table = port->ip_dnrequests; \
303 assert(table != IPR_NULL); \
305 ipr = &table[index]; \
306 assert(ipr->ipr_name == oname); \
308 ipr->ipr_name = nname; \
311 /* Make a port-deleted request */
312 extern void ipc_port_pdrequest(
315 ipc_port_t
*previousp
);
317 /* Make a no-senders request */
318 extern void ipc_port_nsrequest(
320 mach_port_mscount_t sync
,
322 ipc_port_t
*previousp
);
324 #define ipc_port_set_mscount(port, mscount) \
326 assert(ip_active(port)); \
328 (port)->ip_mscount = (mscount); \
331 /* Prepare a receive right for transmission/destruction */
332 extern void ipc_port_clear_receiver(
335 /* Initialize a newly-allocated port */
336 extern void ipc_port_init(
339 mach_port_name_t name
);
341 /* Allocate a port */
342 extern kern_return_t
ipc_port_alloc(
344 mach_port_name_t
*namep
,
347 /* Allocate a port, with a specific name */
348 extern kern_return_t
ipc_port_alloc_name(
350 mach_port_name_t name
,
353 /* Generate dead name notifications */
354 extern void ipc_port_dnnotify(
356 ipc_port_request_t dnrequests
);
359 extern void ipc_port_destroy(
362 /* Check if queueing "port" in a message for "dest" would create a circular
363 group of ports and messages */
365 ipc_port_check_circularity(
369 /* Make a send-once notify port from a receive right */
370 extern ipc_port_t
ipc_port_lookup_notify(
372 mach_port_name_t name
);
374 /* Make a naked send right from a receive right - port locked and active */
375 extern ipc_port_t
ipc_port_make_send_locked(
378 /* Make a naked send right from a receive right */
379 extern ipc_port_t
ipc_port_make_send(
382 /* Make a naked send right from another naked send right */
383 extern ipc_port_t
ipc_port_copy_send(
386 /* Copyout a naked send right */
387 extern mach_port_name_t
ipc_port_copyout_send(
391 /* Release a (valid) naked send right */
392 extern void ipc_port_release_send(
395 /* Make a naked send-once right from a receive right */
396 extern ipc_port_t
ipc_port_make_sonce(
399 /* Release a naked send-once right */
400 extern void ipc_port_release_sonce(
403 /* Release a naked (in limbo or in transit) receive right */
404 extern void ipc_port_release_receive(
407 /* Allocate a port in a special space */
408 extern ipc_port_t
ipc_port_alloc_special(
411 /* Deallocate a port in a special space */
412 extern void ipc_port_dealloc_special(
417 /* Track low-level port deallocation */
418 extern void ipc_port_track_dealloc(
421 /* Initialize general port debugging state */
422 extern void ipc_port_debug_init(void);
423 #endif /* MACH_ASSERT */
425 #define ipc_port_alloc_kernel() \
426 ipc_port_alloc_special(ipc_space_kernel)
427 #define ipc_port_dealloc_kernel(port) \
428 ipc_port_dealloc_special((port), ipc_space_kernel)
430 #define ipc_port_alloc_reply() \
431 ipc_port_alloc_special(ipc_space_reply)
432 #define ipc_port_dealloc_reply(port) \
433 ipc_port_dealloc_special((port), ipc_space_reply)
435 #define ipc_port_reference(port) \
436 ipc_object_reference(&(port)->ip_object)
438 #define ipc_port_release(port) \
439 ipc_object_release(&(port)->ip_object)
441 #endif /* _IPC_IPC_PORT_H_ */