]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_object.h
67a0e4910580f2a464c967e93b0a117220abaacd
[apple/xnu.git] / osfmk / ipc / ipc_object.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: ipc/ipc_object.h
54 * Author: Rich Draves
55 * Date: 1989
56 *
57 * Definitions for IPC objects, for which tasks have capabilities.
58 */
59
60 #ifndef _IPC_IPC_OBJECT_H_
61 #define _IPC_IPC_OBJECT_H_
62
63 #include <mach_rt.h>
64 #include <cpus.h>
65 #include <mach_kdb.h>
66
67 #include <mach/kern_return.h>
68 #include <mach/message.h>
69 #include <kern/lock.h>
70 #include <kern/macro_help.h>
71 #include <kern/thread_pool.h>
72 #include <kern/zalloc.h>
73 #include <ipc/ipc_types.h>
74
75 typedef natural_t ipc_object_refs_t; /* for ipc/ipc_object.h */
76 typedef natural_t ipc_object_bits_t;
77 typedef natural_t ipc_object_type_t;
78
79 /*
80 * There is no lock in the ipc_object; it is in the enclosing kernel
81 * data structure (rpc_common_data) used by both ipc_port and ipc_pset.
82 * The ipc_object is used to both tag and reference count these two data
83 * structures, and (Noto Bene!) pointers to either of these or the
84 * ipc_object at the head of these are freely cast back and forth; hence
85 * the ipc_object MUST BE FIRST in the ipc_common_data.
86 *
87 * If the RPC implementation enabled user-mode code to use kernel-level
88 * data structures (as ours used to), this peculiar structuring would
89 * avoid having anything in user code depend on the kernel configuration
90 * (with which lock size varies).
91 */
92 struct ipc_object {
93 ipc_object_refs_t io_references;
94 ipc_object_bits_t io_bits;
95 port_name_t io_receiver_name;
96 struct thread_pool io_thread_pool;
97 #if NCPUS == 1
98 usimple_lock_data_t io_lock_data;
99 #else
100 decl_mutex_data(, io_lock_data)
101 #endif
102 };
103
104 /*
105 * Legacy defines. Should use IPC_OBJECT_NULL, etc...
106 */
107 #define IO_NULL ((ipc_object_t) 0)
108 #define IO_DEAD ((ipc_object_t) -1)
109 #define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD))
110
111 /*
112 * IPC steals the high-order bits from the kotype to use
113 * for its own purposes. This allows IPC to record facts
114 * about ports that aren't otherwise obvious from the
115 * existing port fields. In particular, IPC can optionally
116 * mark a port for no more senders detection. Any change
117 * to IO_BITS_PORT_INFO must be coordinated with bitfield
118 * definitions in ipc_port.h.
119 */
120 #define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */
121 #define IO_BITS_KOTYPE 0x00000fff /* used by the object */
122 #define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */
123 #define IO_BITS_ACTIVE 0x80000000 /* is object alive? */
124
125 #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE)
126
127 #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
128 #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
129
130 #define io_makebits(active, otype, kotype) \
131 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
132
133 /*
134 * Object types: ports, port sets, kernel-loaded ports
135 */
136 #define IOT_PORT 0
137 #define IOT_PORT_SET 1
138 #define IOT_NUMBER 2 /* number of types used */
139
140 extern zone_t ipc_object_zones[IOT_NUMBER];
141
142 #define io_alloc(otype) \
143 ((ipc_object_t) zalloc(ipc_object_zones[(otype)]))
144
145 #if MACH_ASSERT
146 /*
147 * Call the routine for io_free so that checking can be performed.
148 */
149 extern void io_free(
150 unsigned int otype,
151 ipc_object_t object);
152
153 #else /* MACH_ASSERT */
154 #define io_free(otype, io) \
155 zfree(ipc_object_zones[(otype)], (vm_offset_t) (io))
156 #endif /* MACH_ASSERT */
157
158 /*
159 * Here we depend on the ipc_object being first within the ipc_common_data,
160 * which is first within the rpc_common_data, which in turn must be first
161 * within any kernel data structure needing to lock an ipc_object
162 * (ipc_port and ipc_pset).
163 */
164 #if NCPUS == 1
165
166 #define io_lock_init(io) \
167 usimple_lock_init(&(io)-io_lock_data, ETAP_IPC_OBJECT)
168 #define io_lock(io) \
169 usimple_lock(&(io)->io_lock_data)
170 #define io_lock_try(io) \
171 usimple_lock_try(&(io)->io_lock_data)
172 #define io_unlock(io) \
173 usimple_unlock(&(io)->io_lock_data)
174
175 #else /* NCPUS == 1 */
176
177 #define io_lock_init(io) \
178 mutex_init(&(io)->io_lock_data, ETAP_IPC_OBJECT)
179 #define io_lock(io) \
180 mutex_lock(&(io)->io_lock_data)
181 #define io_lock_try(io) \
182 mutex_try(&(io)->io_lock_data)
183 #define io_unlock(io) \
184 mutex_unlock(&(io)->io_lock_data)
185
186 #endif /* NCPUS == 1 */
187
188 #if NCPUS > 1
189 #define _VOLATILE_ volatile
190 #else /* NCPUS > 1 */
191 #define _VOLATILE_
192 #endif /* NCPUS > 1 */
193
194 #define io_check_unlock(io) \
195 MACRO_BEGIN \
196 _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \
197 \
198 io_unlock(io); \
199 if (_refs == 0) \
200 io_free(io_otype(io), io); \
201 MACRO_END
202
203 /* Sanity check the ref count. If it is 0, we may be doubly zfreeing.
204 * If it is larger than max int, it has been corrupted, probably by being
205 * modified into an address (this is architecture dependent, but it's
206 * safe to assume there cannot really be max int references).
207 *
208 * NOTE: The 0 test alone will not catch double zfreeing of ipc_port
209 * structs, because the io_references field is the first word of the struct,
210 * and zfree modifies that to point to the next free zone element.
211 */
212 #define IO_MAX_REFERENCES \
213 (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1)))
214
215 #define io_reference(io) \
216 MACRO_BEGIN \
217 assert((io)->io_references < IO_MAX_REFERENCES); \
218 (io)->io_references++; \
219 MACRO_END
220
221 #define io_release(io) \
222 MACRO_BEGIN \
223 assert((io)->io_references > 0 && \
224 (io)->io_references <= IO_MAX_REFERENCES); \
225 (io)->io_references--; \
226 MACRO_END
227
228 /*
229 * Exported interfaces
230 */
231
232 /* Take a reference to an object */
233 extern void ipc_object_reference(
234 ipc_object_t object);
235
236 /* Release a reference to an object */
237 extern void ipc_object_release(
238 ipc_object_t object);
239
240 /* Look up an object in a space */
241 extern kern_return_t ipc_object_translate(
242 ipc_space_t space,
243 mach_port_name_t name,
244 mach_port_right_t right,
245 ipc_object_t *objectp);
246
247 /* Look up two objects in a space, locking them in the order described */
248 extern kern_return_t ipc_object_translate_two(
249 ipc_space_t space,
250 mach_port_name_t name1,
251 mach_port_right_t right1,
252 ipc_object_t *objectp1,
253 mach_port_name_t name2,
254 mach_port_right_t right2,
255 ipc_object_t *objectp2);
256
257 /* Allocate a dead-name entry */
258 extern kern_return_t
259 ipc_object_alloc_dead(
260 ipc_space_t space,
261 mach_port_name_t *namep);
262
263 /* Allocate a dead-name entry, with a specific name */
264 extern kern_return_t ipc_object_alloc_dead_name(
265 ipc_space_t space,
266 mach_port_name_t name);
267
268 /* Allocate an object */
269 extern kern_return_t ipc_object_alloc(
270 ipc_space_t space,
271 ipc_object_type_t otype,
272 mach_port_type_t type,
273 mach_port_urefs_t urefs,
274 mach_port_name_t *namep,
275 ipc_object_t *objectp);
276
277 /* Allocate an object, with a specific name */
278 extern kern_return_t ipc_object_alloc_name(
279 ipc_space_t space,
280 ipc_object_type_t otype,
281 mach_port_type_t type,
282 mach_port_urefs_t urefs,
283 mach_port_name_t name,
284 ipc_object_t *objectp);
285
286 /* Convert a send type name to a received type name */
287 extern mach_msg_type_name_t ipc_object_copyin_type(
288 mach_msg_type_name_t msgt_name);
289
290 /* Copyin a capability from a space */
291 extern kern_return_t ipc_object_copyin(
292 ipc_space_t space,
293 mach_port_name_t name,
294 mach_msg_type_name_t msgt_name,
295 ipc_object_t *objectp);
296
297 /* Copyin a naked capability from the kernel */
298 extern void ipc_object_copyin_from_kernel(
299 ipc_object_t object,
300 mach_msg_type_name_t msgt_name);
301
302 /* Destroy a naked capability */
303 extern void ipc_object_destroy(
304 ipc_object_t object,
305 mach_msg_type_name_t msgt_name);
306
307 /* Copyout a capability, placing it into a space */
308 extern kern_return_t ipc_object_copyout(
309 ipc_space_t space,
310 ipc_object_t object,
311 mach_msg_type_name_t msgt_name,
312 boolean_t overflow,
313 mach_port_name_t *namep);
314
315 /* Copyout a capability with a name, placing it into a space */
316 extern kern_return_t ipc_object_copyout_name(
317 ipc_space_t space,
318 ipc_object_t object,
319 mach_msg_type_name_t msgt_name,
320 boolean_t overflow,
321 mach_port_name_t name);
322
323 /* Translate/consume the destination right of a message */
324 extern void ipc_object_copyout_dest(
325 ipc_space_t space,
326 ipc_object_t object,
327 mach_msg_type_name_t msgt_name,
328 mach_port_name_t *namep);
329
330 /* Rename an entry in a space */
331 extern kern_return_t ipc_object_rename(
332 ipc_space_t space,
333 mach_port_name_t oname,
334 mach_port_name_t nname);
335
336 #if MACH_KDB
337 /* Pretty-print an ipc object */
338
339 extern void ipc_object_print(
340 ipc_object_t object);
341
342 #endif /* MACH_KDB */
343
344 #endif /* _IPC_IPC_OBJECT_H_ */