2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: ipc/ipc_port.h
57 * Definitions for ports.
60 #ifndef _IPC_IPC_PORT_H_
61 #define _IPC_IPC_PORT_H_
65 #include <mach_assert.h>
66 #include <mach_debug.h>
68 #include <mach/mach_types.h>
69 #include <mach/boolean.h>
70 #include <mach/kern_return.h>
71 #include <mach/port.h>
73 #include <kern/kern_types.h>
75 #include <ipc/ipc_types.h>
76 #include <ipc/ipc_object.h>
77 #include <ipc/ipc_mqueue.h>
78 #include <ipc/ipc_space.h>
81 * A receive right (port) can be in four states:
82 * 1) dead (not active, ip_timestamp has death time)
83 * 2) in a space (ip_receiver_name != 0, ip_receiver points
84 * to the space but doesn't hold a ref for it)
85 * 3) in transit (ip_receiver_name == 0, ip_destination points
86 * to the destination port and holds a ref for it)
87 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
89 * If the port is active, and ip_receiver points to some space,
90 * then ip_receiver_name != 0, and that space holds receive rights.
91 * If the port is not active, then ip_timestamp contains a timestamp
92 * taken when the port was destroyed.
95 typedef unsigned int ipc_port_timestamp_t
;
100 * Initial sub-structure in common with ipc_pset and rpc_port
101 * First element is an ipc_object
103 struct ipc_object ip_object
;
106 struct ipc_space
*receiver
;
107 struct ipc_port
*destination
;
108 ipc_port_timestamp_t timestamp
;
111 ipc_kobject_t ip_kobject
;
112 mach_port_mscount_t ip_mscount
;
113 mach_port_rights_t ip_srights
;
114 mach_port_rights_t ip_sorights
;
116 struct ipc_port
*ip_nsrequest
;
117 struct ipc_port
*ip_pdrequest
;
118 struct ipc_port_request
*ip_dnrequests
;
120 unsigned int ip_pset_count
;
121 struct ipc_mqueue ip_messages
;
122 struct ipc_kmsg
*ip_premsg
;
126 * These fields are needed for the use of XMM.
127 * Few ports need this information; it should
128 * be kept in XMM instead (TBD). XXX
130 long ip_norma_xmm_object_refs
;
131 struct ipc_port
*ip_norma_xmm_object
;
135 #define IP_NSPARES 10
136 #define IP_CALLSTACK_MAX 10
137 queue_chain_t ip_port_links
; /* all allocated ports */
138 thread_t ip_thread
; /* who made me? thread context */
139 unsigned long ip_timetrack
; /* give an idea of "when" created */
140 natural_t ip_callstack
[IP_CALLSTACK_MAX
]; /* stack trace */
141 unsigned long ip_spares
[IP_NSPARES
]; /* for debugging */
142 #endif /* MACH_ASSERT */
147 #define ip_references ip_object.io_references
148 #define ip_bits ip_object.io_bits
149 #define ip_receiver_name ip_object.io_receiver_name
151 #define ip_receiver data.receiver
152 #define ip_destination data.destination
153 #define ip_timestamp data.timestamp
155 #define IP_NULL IPC_PORT_NULL
156 #define IP_DEAD IPC_PORT_DEAD
157 #define IP_VALID(port) IPC_PORT_VALID(port)
159 #define ip_active(port) io_active(&(port)->ip_object)
160 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
161 #define ip_lock(port) io_lock(&(port)->ip_object)
162 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
163 #define ip_unlock(port) io_unlock(&(port)->ip_object)
164 #define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
166 #define ip_reference(port) io_reference(&(port)->ip_object)
167 #define ip_release(port) io_release(&(port)->ip_object)
169 #define ip_kotype(port) io_kotype(&(port)->ip_object)
172 * JMM - Preallocation flag
173 * This flag indicates that there is a message buffer preallocated for this
174 * port and we should use that when sending (from the kernel) rather than
175 * allocate a new one. This avoids deadlocks during notification message
176 * sends by critical system threads (which may be needed to free memory and
177 * therefore cannot be blocked waiting for memory themselves).
179 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
180 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
182 #define IP_SET_PREALLOC(port, kmsg) \
184 (port)->ip_bits |= IP_BIT_PREALLOC; \
185 (port)->ip_premsg = (kmsg); \
188 #define IP_CLEAR_PREALLOC(port, kmsg) \
190 assert((port)->ip_premsg == kmsg); \
191 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
192 (port)->ip_premsg = IKM_NULL; \
196 struct ipc_port_request
{
198 struct ipc_port
*port
;
199 ipc_port_request_index_t index
;
203 mach_port_name_t name
;
204 struct ipc_table_size
*size
;
208 #define ipr_next notify.index
209 #define ipr_size name.size
211 #define ipr_soright notify.port
212 #define ipr_name name.name
215 * Taking the ipc_port_multiple lock grants the privilege
216 * to lock multiple ports at once. No ports must locked
220 decl_mutex_data(extern,ipc_port_multiple_lock_data
)
222 #define ipc_port_multiple_lock_init() \
223 mutex_init(&ipc_port_multiple_lock_data, 0)
225 #define ipc_port_multiple_lock() \
226 mutex_lock(&ipc_port_multiple_lock_data)
228 #define ipc_port_multiple_unlock() \
229 mutex_unlock(&ipc_port_multiple_lock_data)
232 * The port timestamp facility provides timestamps
233 * for port destruction. It is used to serialize
234 * mach_port_names with port death.
237 decl_mutex_data(extern,ipc_port_timestamp_lock_data
)
238 extern ipc_port_timestamp_t ipc_port_timestamp_data
;
240 #define ipc_port_timestamp_lock_init() \
241 mutex_init(&ipc_port_timestamp_lock_data, 0)
243 #define ipc_port_timestamp_lock() \
244 mutex_lock(&ipc_port_timestamp_lock_data)
246 #define ipc_port_timestamp_unlock() \
247 mutex_unlock(&ipc_port_timestamp_lock_data)
249 /* Retrieve a port timestamp value */
250 extern ipc_port_timestamp_t
ipc_port_timestamp(void);
253 * Compares two timestamps, and returns TRUE if one
254 * happened before two. Note that this formulation
255 * works when the timestamp wraps around at 2^32,
256 * as long as one and two aren't too far apart.
259 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
261 #define ipc_port_translate_receive(space, name, portp) \
262 ipc_object_translate((space), (name), \
263 MACH_PORT_RIGHT_RECEIVE, \
264 (ipc_object_t *) (portp))
266 #define ipc_port_translate_send(space, name, portp) \
267 ipc_object_translate((space), (name), \
268 MACH_PORT_RIGHT_SEND, \
269 (ipc_object_t *) (portp))
271 /* Allocate a dead-name request slot */
275 mach_port_name_t name
,
277 ipc_port_request_index_t
*indexp
);
279 /* Grow a port's table of dead-name requests */
280 extern kern_return_t
ipc_port_dngrow(
282 ipc_table_elems_t target_size
);
284 /* Cancel a dead-name request and return the send-once right */
285 extern ipc_port_t
ipc_port_dncancel(
287 mach_port_name_t name
,
288 ipc_port_request_index_t index
);
290 #define ipc_port_dnrename(port, index, oname, nname) \
292 ipc_port_request_t ipr, table; \
294 assert(ip_active(port)); \
296 table = port->ip_dnrequests; \
297 assert(table != IPR_NULL); \
299 ipr = &table[index]; \
300 assert(ipr->ipr_name == oname); \
302 ipr->ipr_name = nname; \
305 /* Make a port-deleted request */
306 extern void ipc_port_pdrequest(
309 ipc_port_t
*previousp
);
311 /* Make a no-senders request */
312 extern void ipc_port_nsrequest(
314 mach_port_mscount_t sync
,
316 ipc_port_t
*previousp
);
318 #define ipc_port_set_mscount(port, mscount) \
320 assert(ip_active(port)); \
322 (port)->ip_mscount = (mscount); \
325 /* Prepare a receive right for transmission/destruction */
326 extern void ipc_port_clear_receiver(
329 /* Initialize a newly-allocated port */
330 extern void ipc_port_init(
333 mach_port_name_t name
);
335 /* Allocate a port */
336 extern kern_return_t
ipc_port_alloc(
338 mach_port_name_t
*namep
,
341 /* Allocate a port, with a specific name */
342 extern kern_return_t
ipc_port_alloc_name(
344 mach_port_name_t name
,
347 /* Generate dead name notifications */
348 extern void ipc_port_dnnotify(
350 ipc_port_request_t dnrequests
);
353 extern void ipc_port_destroy(
356 /* Check if queueing "port" in a message for "dest" would create a circular
357 group of ports and messages */
359 ipc_port_check_circularity(
363 /* Make a send-once notify port from a receive right */
364 extern ipc_port_t
ipc_port_lookup_notify(
366 mach_port_name_t name
);
368 /* Make a naked send right from a receive right - port locked and active */
369 extern ipc_port_t
ipc_port_make_send_locked(
372 /* Make a naked send right from a receive right */
373 extern ipc_port_t
ipc_port_make_send(
376 /* Make a naked send right from another naked send right */
377 extern ipc_port_t
ipc_port_copy_send(
380 /* Copyout a naked send right */
381 extern mach_port_name_t
ipc_port_copyout_send(
385 /* Release a (valid) naked send right */
386 extern void ipc_port_release_send(
389 /* Make a naked send-once right from a receive right */
390 extern ipc_port_t
ipc_port_make_sonce(
393 /* Release a naked send-once right */
394 extern void ipc_port_release_sonce(
397 /* Release a naked (in limbo or in transit) receive right */
398 extern void ipc_port_release_receive(
401 /* Allocate a port in a special space */
402 extern ipc_port_t
ipc_port_alloc_special(
405 /* Deallocate a port in a special space */
406 extern void ipc_port_dealloc_special(
411 /* Track low-level port deallocation */
412 extern void ipc_port_track_dealloc(
415 /* Initialize general port debugging state */
416 extern void ipc_port_debug_init(void);
417 #endif /* MACH_ASSERT */
419 #define ipc_port_alloc_kernel() \
420 ipc_port_alloc_special(ipc_space_kernel)
421 #define ipc_port_dealloc_kernel(port) \
422 ipc_port_dealloc_special((port), ipc_space_kernel)
424 #define ipc_port_alloc_reply() \
425 ipc_port_alloc_special(ipc_space_reply)
426 #define ipc_port_dealloc_reply(port) \
427 ipc_port_dealloc_special((port), ipc_space_reply)
429 #define ipc_port_reference(port) \
430 ipc_object_reference(&(port)->ip_object)
432 #define ipc_port_release(port) \
433 ipc_object_release(&(port)->ip_object)
435 #endif /* _IPC_IPC_PORT_H_ */