2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: ipc/ipc_port.h
65 * Definitions for ports.
68 #ifndef _IPC_IPC_PORT_H_
69 #define _IPC_IPC_PORT_H_
73 #include <mach_assert.h>
74 #include <mach_debug.h>
76 #include <mach/mach_types.h>
77 #include <mach/boolean.h>
78 #include <mach/kern_return.h>
79 #include <mach/port.h>
81 #include <kern/kern_types.h>
83 #include <ipc/ipc_types.h>
84 #include <ipc/ipc_object.h>
85 #include <ipc/ipc_mqueue.h>
86 #include <ipc/ipc_space.h>
89 * A receive right (port) can be in four states:
90 * 1) dead (not active, ip_timestamp has death time)
91 * 2) in a space (ip_receiver_name != 0, ip_receiver points
92 * to the space but doesn't hold a ref for it)
93 * 3) in transit (ip_receiver_name == 0, ip_destination points
94 * to the destination port and holds a ref for it)
95 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
97 * If the port is active, and ip_receiver points to some space,
98 * then ip_receiver_name != 0, and that space holds receive rights.
99 * If the port is not active, then ip_timestamp contains a timestamp
100 * taken when the port was destroyed.
103 typedef unsigned int ipc_port_timestamp_t
;
108 * Initial sub-structure in common with ipc_pset and rpc_port
109 * First element is an ipc_object
111 struct ipc_object ip_object
;
114 struct ipc_space
*receiver
;
115 struct ipc_port
*destination
;
116 ipc_port_timestamp_t timestamp
;
119 ipc_kobject_t ip_kobject
;
120 mach_port_mscount_t ip_mscount
;
121 mach_port_rights_t ip_srights
;
122 mach_port_rights_t ip_sorights
;
124 struct ipc_port
*ip_nsrequest
;
125 struct ipc_port
*ip_pdrequest
;
126 struct ipc_port_request
*ip_dnrequests
;
128 unsigned int ip_pset_count
;
129 struct ipc_mqueue ip_messages
;
130 struct ipc_kmsg
*ip_premsg
;
134 * These fields are needed for the use of XMM.
135 * Few ports need this information; it should
136 * be kept in XMM instead (TBD). XXX
138 long ip_norma_xmm_object_refs
;
139 struct ipc_port
*ip_norma_xmm_object
;
143 #define IP_NSPARES 10
144 #define IP_CALLSTACK_MAX 10
145 queue_chain_t ip_port_links
; /* all allocated ports */
146 thread_t ip_thread
; /* who made me? thread context */
147 unsigned long ip_timetrack
; /* give an idea of "when" created */
148 natural_t ip_callstack
[IP_CALLSTACK_MAX
]; /* stack trace */
149 unsigned long ip_spares
[IP_NSPARES
]; /* for debugging */
150 #endif /* MACH_ASSERT */
155 #define ip_references ip_object.io_references
156 #define ip_bits ip_object.io_bits
157 #define ip_receiver_name ip_object.io_receiver_name
159 #define ip_receiver data.receiver
160 #define ip_destination data.destination
161 #define ip_timestamp data.timestamp
163 #define IP_NULL IPC_PORT_NULL
164 #define IP_DEAD IPC_PORT_DEAD
165 #define IP_VALID(port) IPC_PORT_VALID(port)
167 #define ip_active(port) io_active(&(port)->ip_object)
168 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
169 #define ip_lock(port) io_lock(&(port)->ip_object)
170 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
171 #define ip_unlock(port) io_unlock(&(port)->ip_object)
172 #define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
174 #define ip_reference(port) io_reference(&(port)->ip_object)
175 #define ip_release(port) io_release(&(port)->ip_object)
177 #define ip_kotype(port) io_kotype(&(port)->ip_object)
180 * JMM - Preallocation flag
181 * This flag indicates that there is a message buffer preallocated for this
182 * port and we should use that when sending (from the kernel) rather than
183 * allocate a new one. This avoids deadlocks during notification message
184 * sends by critical system threads (which may be needed to free memory and
185 * therefore cannot be blocked waiting for memory themselves).
187 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
188 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
190 #define IP_SET_PREALLOC(port, kmsg) \
192 (port)->ip_bits |= IP_BIT_PREALLOC; \
193 (port)->ip_premsg = (kmsg); \
196 #define IP_CLEAR_PREALLOC(port, kmsg) \
198 assert((port)->ip_premsg == kmsg); \
199 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
200 (port)->ip_premsg = IKM_NULL; \
204 struct ipc_port_request
{
206 struct ipc_port
*port
;
207 ipc_port_request_index_t index
;
211 mach_port_name_t name
;
212 struct ipc_table_size
*size
;
216 #define ipr_next notify.index
217 #define ipr_size name.size
219 #define ipr_soright notify.port
220 #define ipr_name name.name
223 * Taking the ipc_port_multiple lock grants the privilege
224 * to lock multiple ports at once. No ports must locked
228 decl_mutex_data(extern,ipc_port_multiple_lock_data
)
230 #define ipc_port_multiple_lock_init() \
231 mutex_init(&ipc_port_multiple_lock_data, 0)
233 #define ipc_port_multiple_lock() \
234 mutex_lock(&ipc_port_multiple_lock_data)
236 #define ipc_port_multiple_unlock() \
237 mutex_unlock(&ipc_port_multiple_lock_data)
240 * The port timestamp facility provides timestamps
241 * for port destruction. It is used to serialize
242 * mach_port_names with port death.
245 decl_mutex_data(extern,ipc_port_timestamp_lock_data
)
246 extern ipc_port_timestamp_t ipc_port_timestamp_data
;
248 #define ipc_port_timestamp_lock_init() \
249 mutex_init(&ipc_port_timestamp_lock_data, 0)
251 #define ipc_port_timestamp_lock() \
252 mutex_lock(&ipc_port_timestamp_lock_data)
254 #define ipc_port_timestamp_unlock() \
255 mutex_unlock(&ipc_port_timestamp_lock_data)
257 /* Retrieve a port timestamp value */
258 extern ipc_port_timestamp_t
ipc_port_timestamp(void);
261 * Compares two timestamps, and returns TRUE if one
262 * happened before two. Note that this formulation
263 * works when the timestamp wraps around at 2^32,
264 * as long as one and two aren't too far apart.
267 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
269 #define ipc_port_translate_receive(space, name, portp) \
270 ipc_object_translate((space), (name), \
271 MACH_PORT_RIGHT_RECEIVE, \
272 (ipc_object_t *) (portp))
274 #define ipc_port_translate_send(space, name, portp) \
275 ipc_object_translate((space), (name), \
276 MACH_PORT_RIGHT_SEND, \
277 (ipc_object_t *) (portp))
279 /* Allocate a dead-name request slot */
283 mach_port_name_t name
,
285 ipc_port_request_index_t
*indexp
);
287 /* Grow a port's table of dead-name requests */
288 extern kern_return_t
ipc_port_dngrow(
290 ipc_table_elems_t target_size
);
292 /* Cancel a dead-name request and return the send-once right */
293 extern ipc_port_t
ipc_port_dncancel(
295 mach_port_name_t name
,
296 ipc_port_request_index_t index
);
298 #define ipc_port_dnrename(port, index, oname, nname) \
300 ipc_port_request_t ipr, table; \
302 assert(ip_active(port)); \
304 table = port->ip_dnrequests; \
305 assert(table != IPR_NULL); \
307 ipr = &table[index]; \
308 assert(ipr->ipr_name == oname); \
310 ipr->ipr_name = nname; \
313 /* Make a port-deleted request */
314 extern void ipc_port_pdrequest(
317 ipc_port_t
*previousp
);
319 /* Make a no-senders request */
320 extern void ipc_port_nsrequest(
322 mach_port_mscount_t sync
,
324 ipc_port_t
*previousp
);
326 #define ipc_port_set_mscount(port, mscount) \
328 assert(ip_active(port)); \
330 (port)->ip_mscount = (mscount); \
333 /* Prepare a receive right for transmission/destruction */
334 extern void ipc_port_clear_receiver(
337 /* Initialize a newly-allocated port */
338 extern void ipc_port_init(
341 mach_port_name_t name
);
343 /* Allocate a port */
344 extern kern_return_t
ipc_port_alloc(
346 mach_port_name_t
*namep
,
349 /* Allocate a port, with a specific name */
350 extern kern_return_t
ipc_port_alloc_name(
352 mach_port_name_t name
,
355 /* Generate dead name notifications */
356 extern void ipc_port_dnnotify(
358 ipc_port_request_t dnrequests
);
361 extern void ipc_port_destroy(
364 /* Check if queueing "port" in a message for "dest" would create a circular
365 group of ports and messages */
367 ipc_port_check_circularity(
371 /* Make a send-once notify port from a receive right */
372 extern ipc_port_t
ipc_port_lookup_notify(
374 mach_port_name_t name
);
376 /* Make a naked send right from a receive right - port locked and active */
377 extern ipc_port_t
ipc_port_make_send_locked(
380 /* Make a naked send right from a receive right */
381 extern ipc_port_t
ipc_port_make_send(
384 /* Make a naked send right from another naked send right */
385 extern ipc_port_t
ipc_port_copy_send(
388 /* Copyout a naked send right */
389 extern mach_port_name_t
ipc_port_copyout_send(
393 /* Release a (valid) naked send right */
394 extern void ipc_port_release_send(
397 /* Make a naked send-once right from a receive right */
398 extern ipc_port_t
ipc_port_make_sonce(
401 /* Release a naked send-once right */
402 extern void ipc_port_release_sonce(
405 /* Release a naked (in limbo or in transit) receive right */
406 extern void ipc_port_release_receive(
409 /* Allocate a port in a special space */
410 extern ipc_port_t
ipc_port_alloc_special(
413 /* Deallocate a port in a special space */
414 extern void ipc_port_dealloc_special(
419 /* Track low-level port deallocation */
420 extern void ipc_port_track_dealloc(
423 /* Initialize general port debugging state */
424 extern void ipc_port_debug_init(void);
425 #endif /* MACH_ASSERT */
427 #define ipc_port_alloc_kernel() \
428 ipc_port_alloc_special(ipc_space_kernel)
429 #define ipc_port_dealloc_kernel(port) \
430 ipc_port_dealloc_special((port), ipc_space_kernel)
432 #define ipc_port_alloc_reply() \
433 ipc_port_alloc_special(ipc_space_reply)
434 #define ipc_port_dealloc_reply(port) \
435 ipc_port_dealloc_special((port), ipc_space_reply)
437 #define ipc_port_reference(port) \
438 ipc_object_reference(&(port)->ip_object)
440 #define ipc_port_release(port) \
441 ipc_object_release(&(port)->ip_object)
443 #endif /* _IPC_IPC_PORT_H_ */