2 * Copyright (c) 2000-2008 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.
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
65 * File: ipc/ipc_port.h
69 * Definitions for ports.
72 #ifndef _IPC_IPC_PORT_H_
73 #define _IPC_IPC_PORT_H_
75 #if MACH_KERNEL_PRIVATE
79 #include <mach_assert.h>
80 #include <mach_debug.h>
82 #include <mach/mach_types.h>
83 #include <mach/boolean.h>
84 #include <mach/kern_return.h>
85 #include <mach/port.h>
87 #include <kern/kern_types.h>
89 #include <ipc/ipc_types.h>
90 #include <ipc/ipc_object.h>
91 #include <ipc/ipc_mqueue.h>
92 #include <ipc/ipc_space.h>
94 #include <security/_label.h>
97 * A receive right (port) can be in four states:
98 * 1) dead (not active, ip_timestamp has death time)
99 * 2) in a space (ip_receiver_name != 0, ip_receiver points
100 * to the space but doesn't hold a ref for it)
101 * 3) in transit (ip_receiver_name == 0, ip_destination points
102 * to the destination port and holds a ref for it)
103 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
105 * If the port is active, and ip_receiver points to some space,
106 * then ip_receiver_name != 0, and that space holds receive rights.
107 * If the port is not active, then ip_timestamp contains a timestamp
108 * taken when the port was destroyed.
111 typedef unsigned int ipc_port_timestamp_t
;
116 * Initial sub-structure in common with ipc_pset
117 * First element is an ipc_object second is a
120 struct ipc_object ip_object
;
121 struct ipc_mqueue ip_messages
;
124 struct ipc_space
*receiver
;
125 struct ipc_port
*destination
;
126 ipc_port_timestamp_t timestamp
;
129 ipc_kobject_t ip_kobject
;
130 mach_port_mscount_t ip_mscount
;
131 mach_port_rights_t ip_srights
;
132 mach_port_rights_t ip_sorights
;
134 struct ipc_port
*ip_nsrequest
;
135 struct ipc_port
*ip_pdrequest
;
136 struct ipc_port_request
*ip_dnrequests
;
138 unsigned int ip_pset_count
;
139 struct ipc_kmsg
*ip_premsg
;
140 mach_vm_address_t ip_context
;
144 * These fields are needed for the use of XMM.
145 * Few ports need this information; it should
146 * be kept in XMM instead (TBD). XXX
148 long ip_norma_xmm_object_refs
;
149 struct ipc_port
*ip_norma_xmm_object
;
154 #define IP_CALLSTACK_MAX 16
155 queue_chain_t ip_port_links
; /* all allocated ports */
156 thread_t ip_thread
; /* who made me? thread context */
157 unsigned long ip_timetrack
; /* give an idea of "when" created */
158 natural_t ip_callstack
[IP_CALLSTACK_MAX
]; /* stack trace */
159 unsigned long ip_spares
[IP_NSPARES
]; /* for debugging */
160 #endif /* MACH_ASSERT */
164 struct label ip_label
;
169 #define ip_references ip_object.io_references
170 #define ip_bits ip_object.io_bits
172 #define ip_receiver data.receiver
173 #define ip_destination data.destination
174 #define ip_timestamp data.timestamp
176 #define ip_receiver_name ip_messages.imq_receiver_name
178 #define IP_NULL IPC_PORT_NULL
179 #define IP_DEAD IPC_PORT_DEAD
180 #define IP_VALID(port) IPC_PORT_VALID(port)
182 #define ip_active(port) io_active(&(port)->ip_object)
183 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
184 #define ip_lock(port) io_lock(&(port)->ip_object)
185 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
186 #define ip_unlock(port) io_unlock(&(port)->ip_object)
187 #define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
189 #define ip_reference(port) io_reference(&(port)->ip_object)
190 #define ip_release(port) io_release(&(port)->ip_object)
192 #define ip_kotype(port) io_kotype(&(port)->ip_object)
195 * JMM - Preallocation flag
196 * This flag indicates that there is a message buffer preallocated for this
197 * port and we should use that when sending (from the kernel) rather than
198 * allocate a new one. This avoids deadlocks during notification message
199 * sends by critical system threads (which may be needed to free memory and
200 * therefore cannot be blocked waiting for memory themselves).
202 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
203 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
205 #define IP_SET_PREALLOC(port, kmsg) \
207 (port)->ip_bits |= IP_BIT_PREALLOC; \
208 (port)->ip_premsg = (kmsg); \
211 #define IP_CLEAR_PREALLOC(port, kmsg) \
213 assert((port)->ip_premsg == kmsg); \
214 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
215 (port)->ip_premsg = IKM_NULL; \
219 struct ipc_port_request
{
221 struct ipc_port
*port
;
222 ipc_port_request_index_t index
;
226 mach_port_name_t name
;
227 struct ipc_table_size
*size
;
231 #define ipr_next notify.index
232 #define ipr_size name.size
234 #define ipr_soright notify.port
235 #define ipr_name name.name
237 extern lck_grp_t ipc_lck_grp
;
238 extern lck_attr_t ipc_lck_attr
;
241 * Taking the ipc_port_multiple lock grants the privilege
242 * to lock multiple ports at once. No ports must locked
246 decl_lck_mtx_data(extern,ipc_port_multiple_lock_data
)
247 extern lck_mtx_ext_t ipc_port_multiple_lock_data_ext
;
249 #define ipc_port_multiple_lock_init() \
250 lck_mtx_init_ext(&ipc_port_multiple_lock_data, &ipc_port_multiple_lock_data_ext, &ipc_lck_grp, &ipc_lck_attr)
252 #define ipc_port_multiple_lock() \
253 lck_mtx_lock(&ipc_port_multiple_lock_data)
255 #define ipc_port_multiple_unlock() \
256 lck_mtx_unlock(&ipc_port_multiple_lock_data)
259 * The port timestamp facility provides timestamps
260 * for port destruction. It is used to serialize
261 * mach_port_names with port death.
264 decl_lck_mtx_data(extern,ipc_port_timestamp_lock_data
)
265 extern lck_mtx_ext_t ipc_port_timestamp_lock_data_ext
;
267 extern ipc_port_timestamp_t ipc_port_timestamp_data
;
269 #define ipc_port_timestamp_lock_init() \
270 lck_mtx_init_ext(&ipc_port_timestamp_lock_data, &ipc_port_timestamp_lock_data_ext, &ipc_lck_grp, &ipc_lck_attr)
272 #define ipc_port_timestamp_lock() \
273 lck_mtx_lock(&ipc_port_timestamp_lock_data)
275 #define ipc_port_timestamp_unlock() \
276 lck_mtx_unlock(&ipc_port_timestamp_lock_data)
278 /* Retrieve a port timestamp value */
279 extern ipc_port_timestamp_t
ipc_port_timestamp(void);
282 * Compares two timestamps, and returns TRUE if one
283 * happened before two. Note that this formulation
284 * works when the timestamp wraps around at 2^32,
285 * as long as one and two aren't too far apart.
288 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
290 #define ipc_port_translate_receive(space, name, portp) \
291 ipc_object_translate((space), (name), \
292 MACH_PORT_RIGHT_RECEIVE, \
293 (ipc_object_t *) (portp))
295 #define ipc_port_translate_send(space, name, portp) \
296 ipc_object_translate((space), (name), \
297 MACH_PORT_RIGHT_SEND, \
298 (ipc_object_t *) (portp))
300 /* Allocate a dead-name request slot */
304 mach_port_name_t name
,
306 ipc_port_request_index_t
*indexp
);
308 /* Grow a port's table of dead-name requests */
309 extern kern_return_t
ipc_port_dngrow(
311 ipc_table_elems_t target_size
);
313 /* Cancel a dead-name request and return the send-once right */
314 extern ipc_port_t
ipc_port_dncancel(
316 mach_port_name_t name
,
317 ipc_port_request_index_t index
);
319 #define ipc_port_dnrename(port, index, oname, nname) \
321 ipc_port_request_t ipr, table; \
323 assert(ip_active(port)); \
325 table = port->ip_dnrequests; \
326 assert(table != IPR_NULL); \
328 ipr = &table[index]; \
329 assert(ipr->ipr_name == oname); \
331 ipr->ipr_name = nname; \
334 /* Make a port-deleted request */
335 extern void ipc_port_pdrequest(
338 ipc_port_t
*previousp
);
340 /* Make a no-senders request */
341 extern void ipc_port_nsrequest(
343 mach_port_mscount_t sync
,
345 ipc_port_t
*previousp
);
347 #define ipc_port_set_mscount(port, mscount) \
349 assert(ip_active(port)); \
351 (port)->ip_mscount = (mscount); \
354 /* Prepare a receive right for transmission/destruction */
355 extern void ipc_port_clear_receiver(
358 /* Initialize a newly-allocated port */
359 extern void ipc_port_init(
362 mach_port_name_t name
);
364 /* Allocate a port */
365 extern kern_return_t
ipc_port_alloc(
367 mach_port_name_t
*namep
,
370 /* Allocate a port, with a specific name */
371 extern kern_return_t
ipc_port_alloc_name(
373 mach_port_name_t name
,
376 /* Generate dead name notifications */
377 extern void ipc_port_dnnotify(
379 ipc_port_request_t dnrequests
);
382 extern void ipc_port_destroy(
385 /* Check if queueing "port" in a message for "dest" would create a circular
386 group of ports and messages */
388 ipc_port_check_circularity(
392 /* Make a send-once notify port from a receive right */
393 extern ipc_port_t
ipc_port_lookup_notify(
395 mach_port_name_t name
);
397 /* Make a naked send right from a receive right - port locked and active */
398 extern ipc_port_t
ipc_port_make_send_locked(
401 /* Make a naked send right from a receive right */
402 extern ipc_port_t
ipc_port_make_send(
405 /* Make a naked send right from another naked send right */
406 extern ipc_port_t
ipc_port_copy_send(
409 /* Copyout a naked send right */
410 extern mach_port_name_t
ipc_port_copyout_send(
414 #endif /* MACH_KERNEL_PRIVATE */
418 /* Release a (valid) naked send right */
419 extern void ipc_port_release_send(
422 #endif /* KERNEL_PRIVATE */
424 #if MACH_KERNEL_PRIVATE
426 /* Make a naked send-once right from a receive right */
427 extern ipc_port_t
ipc_port_make_sonce(
430 /* Release a naked send-once right */
431 extern void ipc_port_release_sonce(
434 /* Release a naked (in limbo or in transit) receive right */
435 extern void ipc_port_release_receive(
438 /* Allocate a port in a special space */
439 extern ipc_port_t
ipc_port_alloc_special(
442 /* Deallocate a port in a special space */
443 extern void ipc_port_dealloc_special(
448 /* Track low-level port deallocation */
449 extern void ipc_port_track_dealloc(
452 /* Initialize general port debugging state */
453 extern void ipc_port_debug_init(void);
454 #endif /* MACH_ASSERT */
456 #define ipc_port_alloc_kernel() \
457 ipc_port_alloc_special(ipc_space_kernel)
458 #define ipc_port_dealloc_kernel(port) \
459 ipc_port_dealloc_special((port), ipc_space_kernel)
461 #define ipc_port_alloc_reply() \
462 ipc_port_alloc_special(ipc_space_reply)
463 #define ipc_port_dealloc_reply(port) \
464 ipc_port_dealloc_special((port), ipc_space_reply)
466 #define ipc_port_reference(port) \
467 ipc_object_reference(&(port)->ip_object)
469 #define ipc_port_release(port) \
470 ipc_object_release(&(port)->ip_object)
472 #endif /* MACH_KERNEL_PRIVATE */
474 #endif /* _IPC_IPC_PORT_H_ */