2 * Copyright (c) 2000 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/boolean.h>
69 #include <mach/kern_return.h>
70 #include <mach_debug.h>
71 #include <mach/port.h>
72 #include <kern/lock.h>
73 #include <kern/ipc_kobject.h>
74 #include <kern/wait_queue.h>
75 #include <kern/thread_pool.h>
77 #include <ipc/ipc_object.h>
78 #include <ipc/ipc_mqueue.h>
79 #include <ipc/ipc_table.h>
80 #include <ipc/ipc_types.h>
81 #include <ipc/ipc_entry.h>
82 #include <ipc/ipc_space.h>
85 * A receive right (port) can be in four states:
86 * 1) dead (not active, ip_timestamp has death time)
87 * 2) in a space (ip_receiver_name != 0, ip_receiver points
88 * to the space but doesn't hold a ref for it)
89 * 3) in transit (ip_receiver_name == 0, ip_destination points
90 * to the destination port and holds a ref for it)
91 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
93 * If the port is active, and ip_receiver points to some space,
94 * then ip_receiver_name != 0, and that space holds receive rights.
95 * If the port is not active, then ip_timestamp contains a timestamp
96 * taken when the port was destroyed.
99 typedef unsigned int ipc_port_timestamp_t
;
101 typedef unsigned int ipc_port_flags_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 struct rpc_subsystem
* ip_subsystem
;
119 mach_port_mscount_t ip_mscount
;
120 mach_port_rights_t ip_srights
;
121 mach_port_rights_t ip_sorights
;
123 struct ipc_port
*ip_nsrequest
;
124 struct ipc_port
*ip_pdrequest
;
125 struct ipc_port_request
*ip_dnrequests
;
127 unsigned int ip_pset_count
;
128 struct ipc_mqueue ip_messages
;
129 struct ipc_kmsg
*ip_premsg
;
133 * These fields are needed for the use of XMM.
134 * Few ports need this information; it should
135 * be kept in XMM instead (TBD). XXX
137 long ip_norma_xmm_object_refs
;
138 struct ipc_port
*ip_norma_xmm_object
;
142 #define IP_NSPARES 10
143 #define IP_CALLSTACK_MAX 10
144 queue_chain_t ip_port_links
; /* all allocated ports */
145 natural_t ip_thread
; /* who made me? thread context */
146 unsigned long ip_timetrack
; /* give an idea of "when" created */
147 natural_t ip_callstack
[IP_CALLSTACK_MAX
]; /* stack trace */
148 unsigned long ip_spares
[IP_NSPARES
]; /* for debugging */
149 #endif /* MACH_ASSERT */
154 #define ip_references ip_object.io_references
155 #define ip_bits ip_object.io_bits
156 #define ip_receiver_name ip_object.io_receiver_name
158 #define ip_thread_pool ip_object.io_thread_pool
160 #define ip_receiver data.receiver
161 #define ip_destination data.destination
162 #define ip_timestamp data.timestamp
164 #define IP_NULL IPC_PORT_NULL
165 #define IP_DEAD IPC_PORT_DEAD
166 #define IP_VALID(port) IPC_PORT_VALID(port)
168 #define ip_active(port) io_active(&(port)->ip_object)
169 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
170 #define ip_lock(port) io_lock(&(port)->ip_object)
171 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
172 #define ip_unlock(port) io_unlock(&(port)->ip_object)
173 #define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
175 #define ip_reference(port) io_reference(&(port)->ip_object)
176 #define ip_release(port) io_release(&(port)->ip_object)
178 #define ip_kotype(port) io_kotype(&(port)->ip_object)
181 * JMM - Preallocation flag
182 * This flag indicates that there is a message buffer preallocated for this
183 * port and we should use that when sending (from the kernel) rather than
184 * allocate a new one. This avoids deadlocks during notification message
185 * sends by critical system threads (which may be needed to free memory and
186 * therefore cannot be blocked waiting for memory themselves).
188 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
189 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
191 #define IP_SET_PREALLOC(port, kmsg) \
193 (port)->ip_bits |= IP_BIT_PREALLOC; \
194 (port)->ip_premsg = (kmsg); \
197 #define IP_CLEAR_PREALLOC(port, kmsg) \
199 assert((port)->ip_premsg == kmsg); \
200 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
201 (port)->ip_premsg = IKM_NULL; \
204 typedef ipc_table_index_t ipc_port_request_index_t
;
206 typedef struct ipc_port_request
{
208 struct ipc_port
*port
;
209 ipc_port_request_index_t index
;
213 mach_port_name_t name
;
214 struct ipc_table_size
*size
;
216 } *ipc_port_request_t
;
218 #define ipr_next notify.index
219 #define ipr_size name.size
221 #define ipr_soright notify.port
222 #define ipr_name name.name
224 #define IPR_NULL ((ipc_port_request_t) 0)
227 * Taking the ipc_port_multiple lock grants the privilege
228 * to lock multiple ports at once. No ports must locked
232 decl_mutex_data(extern,ipc_port_multiple_lock_data
)
234 #define ipc_port_multiple_lock_init() \
235 mutex_init(&ipc_port_multiple_lock_data, ETAP_IPC_PORT_MULT)
237 #define ipc_port_multiple_lock() \
238 mutex_lock(&ipc_port_multiple_lock_data)
240 #define ipc_port_multiple_unlock() \
241 mutex_unlock(&ipc_port_multiple_lock_data)
244 * The port timestamp facility provides timestamps
245 * for port destruction. It is used to serialize
246 * mach_port_names with port death.
249 decl_mutex_data(extern,ipc_port_timestamp_lock_data
)
250 extern ipc_port_timestamp_t ipc_port_timestamp_data
;
252 #define ipc_port_timestamp_lock_init() \
253 mutex_init(&ipc_port_timestamp_lock_data, ETAP_IPC_PORT_TIME)
255 #define ipc_port_timestamp_lock() \
256 mutex_lock(&ipc_port_timestamp_lock_data)
258 #define ipc_port_timestamp_unlock() \
259 mutex_unlock(&ipc_port_timestamp_lock_data)
261 /* Retrieve a port timestamp value */
262 extern ipc_port_timestamp_t
ipc_port_timestamp(void);
265 * Compares two timestamps, and returns TRUE if one
266 * happened before two. Note that this formulation
267 * works when the timestamp wraps around at 2^32,
268 * as long as one and two aren't too far apart.
271 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
273 #define ipc_port_translate_receive(space, name, portp) \
274 ipc_object_translate((space), (name), \
275 MACH_PORT_RIGHT_RECEIVE, \
276 (ipc_object_t *) (portp))
278 #define ipc_port_translate_send(space, name, portp) \
279 ipc_object_translate((space), (name), \
280 MACH_PORT_RIGHT_SEND, \
281 (ipc_object_t *) (portp))
283 /* Allocate a dead-name request slot */
287 mach_port_name_t name
,
289 ipc_port_request_index_t
*indexp
);
291 /* Grow a port's table of dead-name requests */
292 extern kern_return_t
ipc_port_dngrow(
296 /* Cancel a dead-name request and return the send-once right */
297 extern ipc_port_t
ipc_port_dncancel(
299 mach_port_name_t name
,
300 ipc_port_request_index_t index
);
302 #define ipc_port_dnrename(port, index, oname, nname) \
304 ipc_port_request_t ipr, table; \
306 assert(ip_active(port)); \
308 table = port->ip_dnrequests; \
309 assert(table != IPR_NULL); \
311 ipr = &table[index]; \
312 assert(ipr->ipr_name == oname); \
314 ipr->ipr_name = nname; \
317 /* Make a port-deleted request */
318 extern void ipc_port_pdrequest(
321 ipc_port_t
*previousp
);
323 /* Make a no-senders request */
324 extern void ipc_port_nsrequest(
326 mach_port_mscount_t sync
,
328 ipc_port_t
*previousp
);
330 #define ipc_port_set_mscount(port, mscount) \
332 assert(ip_active(port)); \
334 (port)->ip_mscount = (mscount); \
337 /* Prepare a receive right for transmission/destruction */
338 extern void ipc_port_clear_receiver(
341 /* Initialize a newly-allocated port */
342 extern void ipc_port_init(
345 mach_port_name_t name
);
347 /* Allocate a port */
348 extern kern_return_t
ipc_port_alloc(
350 mach_port_name_t
*namep
,
353 /* Allocate a port, with a specific name */
354 extern kern_return_t
ipc_port_alloc_name(
356 mach_port_name_t name
,
359 /* Generate dead name notifications */
360 extern void ipc_port_dnnotify(
362 ipc_port_request_t dnrequests
);
365 extern void ipc_port_destroy(
368 /* Check if queueing "port" in a message for "dest" would create a circular
369 group of ports and messages */
371 ipc_port_check_circularity(
375 /* Make a send-once notify port from a receive right */
376 extern ipc_port_t
ipc_port_lookup_notify(
378 mach_port_name_t name
);
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_ */