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_object.h
65 * Definitions for IPC objects, for which tasks have capabilities.
68 #ifndef _IPC_IPC_OBJECT_H_
69 #define _IPC_IPC_OBJECT_H_
74 #include <mach/kern_return.h>
75 #include <mach/message.h>
76 #include <kern/lock.h>
77 #include <kern/macro_help.h>
78 #include <kern/zalloc.h>
79 #include <ipc/ipc_types.h>
81 typedef natural_t ipc_object_refs_t
; /* for ipc/ipc_object.h */
82 typedef natural_t ipc_object_bits_t
;
83 typedef natural_t ipc_object_type_t
;
86 * There is no lock in the ipc_object; it is in the enclosing kernel
87 * data structure (rpc_common_data) used by both ipc_port and ipc_pset.
88 * The ipc_object is used to both tag and reference count these two data
89 * structures, and (Noto Bene!) pointers to either of these or the
90 * ipc_object at the head of these are freely cast back and forth; hence
91 * the ipc_object MUST BE FIRST in the ipc_common_data.
93 * If the RPC implementation enabled user-mode code to use kernel-level
94 * data structures (as ours used to), this peculiar structuring would
95 * avoid having anything in user code depend on the kernel configuration
96 * (with which lock size varies).
99 ipc_object_refs_t io_references
;
100 ipc_object_bits_t io_bits
;
101 mach_port_name_t io_receiver_name
;
102 decl_mutex_data(, io_lock_data
)
106 * Legacy defines. Should use IPC_OBJECT_NULL, etc...
108 #define IO_NULL ((ipc_object_t) 0)
109 #define IO_DEAD ((ipc_object_t) -1)
110 #define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD))
113 * IPC steals the high-order bits from the kotype to use
114 * for its own purposes. This allows IPC to record facts
115 * about ports that aren't otherwise obvious from the
116 * existing port fields. In particular, IPC can optionally
117 * mark a port for no more senders detection. Any change
118 * to IO_BITS_PORT_INFO must be coordinated with bitfield
119 * definitions in ipc_port.h.
121 #define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */
122 #define IO_BITS_KOTYPE 0x00000fff /* used by the object */
123 #define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */
124 #define IO_BITS_ACTIVE 0x80000000 /* is object alive? */
126 #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE)
128 #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
129 #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
131 #define io_makebits(active, otype, kotype) \
132 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
135 * Object types: ports, port sets, kernel-loaded ports
138 #define IOT_PORT_SET 1
139 #define IOT_NUMBER 2 /* number of types used */
141 extern zone_t ipc_object_zones
[IOT_NUMBER
];
143 #define io_alloc(otype) \
144 ((ipc_object_t) zalloc(ipc_object_zones[(otype)]))
148 * Call the routine for io_free so that checking can be performed.
152 ipc_object_t object
);
154 #else /* MACH_ASSERT */
155 #define io_free(otype, io) \
156 zfree(ipc_object_zones[(otype)], (io))
157 #endif /* MACH_ASSERT */
160 * Here we depend on the ipc_object being first within the ipc_common_data,
161 * which is first within the rpc_common_data, which in turn must be first
162 * within any kernel data structure needing to lock an ipc_object
163 * (ipc_port and ipc_pset).
165 #define io_lock_init(io) \
166 mutex_init(&(io)->io_lock_data, 0)
167 #define io_lock(io) \
168 mutex_lock(&(io)->io_lock_data)
169 #define io_lock_try(io) \
170 mutex_try(&(io)->io_lock_data)
171 #define io_unlock(io) \
172 mutex_unlock(&(io)->io_lock_data)
174 #define _VOLATILE_ volatile
176 #define io_check_unlock(io) \
178 _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \
182 io_free(io_otype(io), io); \
185 /* Sanity check the ref count. If it is 0, we may be doubly zfreeing.
186 * If it is larger than max int, it has been corrupted, probably by being
187 * modified into an address (this is architecture dependent, but it's
188 * safe to assume there cannot really be max int references).
190 * NOTE: The 0 test alone will not catch double zfreeing of ipc_port
191 * structs, because the io_references field is the first word of the struct,
192 * and zfree modifies that to point to the next free zone element.
194 #define IO_MAX_REFERENCES \
195 (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1)))
197 #define io_reference(io) \
199 assert((io)->io_references < IO_MAX_REFERENCES); \
200 (io)->io_references++; \
203 #define io_release(io) \
205 assert((io)->io_references > 0 && \
206 (io)->io_references <= IO_MAX_REFERENCES); \
207 (io)->io_references--; \
211 * Exported interfaces
214 /* Take a reference to an object */
215 extern void ipc_object_reference(
216 ipc_object_t object
);
218 /* Release a reference to an object */
219 extern void ipc_object_release(
220 ipc_object_t object
);
222 /* Look up an object in a space */
223 extern kern_return_t
ipc_object_translate(
225 mach_port_name_t name
,
226 mach_port_right_t right
,
227 ipc_object_t
*objectp
);
229 /* Look up two objects in a space, locking them in the order described */
230 extern kern_return_t
ipc_object_translate_two(
232 mach_port_name_t name1
,
233 mach_port_right_t right1
,
234 ipc_object_t
*objectp1
,
235 mach_port_name_t name2
,
236 mach_port_right_t right2
,
237 ipc_object_t
*objectp2
);
239 /* Allocate a dead-name entry */
241 ipc_object_alloc_dead(
243 mach_port_name_t
*namep
);
245 /* Allocate a dead-name entry, with a specific name */
246 extern kern_return_t
ipc_object_alloc_dead_name(
248 mach_port_name_t name
);
250 /* Allocate an object */
251 extern kern_return_t
ipc_object_alloc(
253 ipc_object_type_t otype
,
254 mach_port_type_t type
,
255 mach_port_urefs_t urefs
,
256 mach_port_name_t
*namep
,
257 ipc_object_t
*objectp
);
259 /* Allocate an object, with a specific name */
260 extern kern_return_t
ipc_object_alloc_name(
262 ipc_object_type_t otype
,
263 mach_port_type_t type
,
264 mach_port_urefs_t urefs
,
265 mach_port_name_t name
,
266 ipc_object_t
*objectp
);
268 /* Convert a send type name to a received type name */
269 extern mach_msg_type_name_t
ipc_object_copyin_type(
270 mach_msg_type_name_t msgt_name
);
272 /* Copyin a capability from a space */
273 extern kern_return_t
ipc_object_copyin(
275 mach_port_name_t name
,
276 mach_msg_type_name_t msgt_name
,
277 ipc_object_t
*objectp
);
279 /* Copyin a naked capability from the kernel */
280 extern void ipc_object_copyin_from_kernel(
282 mach_msg_type_name_t msgt_name
);
284 /* Destroy a naked capability */
285 extern void ipc_object_destroy(
287 mach_msg_type_name_t msgt_name
);
289 /* Copyout a capability, placing it into a space */
290 extern kern_return_t
ipc_object_copyout(
293 mach_msg_type_name_t msgt_name
,
295 mach_port_name_t
*namep
);
297 /* Copyout a capability with a name, placing it into a space */
298 extern kern_return_t
ipc_object_copyout_name(
301 mach_msg_type_name_t msgt_name
,
303 mach_port_name_t name
);
305 /* Translate/consume the destination right of a message */
306 extern void ipc_object_copyout_dest(
309 mach_msg_type_name_t msgt_name
,
310 mach_port_name_t
*namep
);
312 /* Rename an entry in a space */
313 extern kern_return_t
ipc_object_rename(
315 mach_port_name_t oname
,
316 mach_port_name_t nname
);
319 /* Pretty-print an ipc object */
321 extern void ipc_object_print(
322 ipc_object_t object
);
324 #endif /* MACH_KDB */
326 #endif /* _IPC_IPC_OBJECT_H_ */