]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_object.h
xnu-6153.81.5.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_object.h
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
1c79356b
A
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.
0a7de745 41 *
1c79356b
A
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.
0a7de745 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
1c79356b
A
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
2d21ac55
A
56/*
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,
60 * Version 2.0.
61 */
1c79356b
A
62/*
63 */
64/*
65 * File: ipc/ipc_object.h
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Definitions for IPC objects, for which tasks have capabilities.
70 */
71
0a7de745 72#ifndef _IPC_IPC_OBJECT_H_
1c79356b
A
73#define _IPC_IPC_OBJECT_H_
74
1c79356b
A
75#include <mach/kern_return.h>
76#include <mach/message.h>
fe8ab488 77#include <kern/locks.h>
1c79356b 78#include <kern/macro_help.h>
fe8ab488 79#include <kern/assert.h>
1c79356b
A
80#include <kern/zalloc.h>
81#include <ipc/ipc_types.h>
316670eb 82#include <libkern/OSAtomic.h>
1c79356b 83
0a7de745 84typedef natural_t ipc_object_refs_t; /* for ipc/ipc_object.h */
1c79356b
A
85typedef natural_t ipc_object_bits_t;
86typedef natural_t ipc_object_type_t;
87
88/*
1c79356b
A
89 * The ipc_object is used to both tag and reference count these two data
90 * structures, and (Noto Bene!) pointers to either of these or the
91 * ipc_object at the head of these are freely cast back and forth; hence
92 * the ipc_object MUST BE FIRST in the ipc_common_data.
0a7de745 93 *
1c79356b
A
94 * If the RPC implementation enabled user-mode code to use kernel-level
95 * data structures (as ours used to), this peculiar structuring would
96 * avoid having anything in user code depend on the kernel configuration
97 * (with which lock size varies).
98 */
99struct ipc_object {
b0d623f7 100 ipc_object_bits_t io_bits;
1c79356b 101 ipc_object_refs_t io_references;
cb323159
A
102 lck_spin_t io_lock_data;
103} __attribute__((aligned(8)));
b0d623f7
A
104
105/*
106 * If another object type needs to participate in io_kotype()-based
107 * dispatching, it must include a stub structure as the first
108 * element
109 */
110struct ipc_object_header {
1c79356b 111 ipc_object_bits_t io_bits;
b0d623f7
A
112#ifdef __LP64__
113 natural_t io_padding; /* pad to natural boundary */
114#endif
1c79356b
A
115};
116
117/*
118 * Legacy defines. Should use IPC_OBJECT_NULL, etc...
119 */
0a7de745
A
120#define IO_NULL ((ipc_object_t) 0)
121#define IO_DEAD ((ipc_object_t) ~0UL)
122#define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD))
1c79356b
A
123
124/*
125 * IPC steals the high-order bits from the kotype to use
126 * for its own purposes. This allows IPC to record facts
127 * about ports that aren't otherwise obvious from the
128 * existing port fields. In particular, IPC can optionally
129 * mark a port for no more senders detection. Any change
130 * to IO_BITS_PORT_INFO must be coordinated with bitfield
131 * definitions in ipc_port.h.
132 */
0a7de745 133#define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */
cb323159
A
134#define IO_BITS_KOTYPE 0x000007ff /* used by the object */
135#define IO_BITS_KOBJECT 0x00000800 /* port belongs to a kobject */
0a7de745
A
136#define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */
137#define IO_BITS_ACTIVE 0x80000000 /* is object alive? */
1c79356b 138
0a7de745 139#define io_active(io) (((io)->io_bits & IO_BITS_ACTIVE) != 0)
1c79356b 140
0a7de745
A
141#define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
142#define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
cb323159 143#define io_is_kobject(io) (((io)->io_bits & IO_BITS_KOBJECT) != IKOT_NONE)
1c79356b 144
0a7de745 145#define io_makebits(active, otype, kotype) \
1c79356b
A
146 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
147
148/*
149 * Object types: ports, port sets, kernel-loaded ports
150 */
0a7de745
A
151#define IOT_PORT 0
152#define IOT_PORT_SET 1
153#define IOT_NUMBER 2 /* number of types used */
1c79356b
A
154
155extern zone_t ipc_object_zones[IOT_NUMBER];
cb323159 156extern lck_grp_t ipc_lck_grp;
1c79356b 157
0a7de745
A
158#define io_alloc(otype) \
159 ((ipc_object_t) zalloc(ipc_object_zones[(otype)]))
1c79356b 160
0a7de745
A
161extern void io_free(
162 unsigned int otype,
163 ipc_object_t object);
1c79356b 164
1c79356b 165/*
b0d623f7 166 * Here we depend on the ipc_object being first within the kernel struct
1c79356b
A
167 * (ipc_port and ipc_pset).
168 */
1c79356b 169#define io_lock_init(io) \
316670eb 170 lck_spin_init(&(io)->io_lock_data, &ipc_lck_grp, &ipc_lck_attr)
b0d623f7 171#define io_lock_destroy(io) \
316670eb 172 lck_spin_destroy(&(io)->io_lock_data, &ipc_lck_grp)
cb323159
A
173#define io_lock_held(io) \
174 LCK_SPIN_ASSERT(&(io)->io_lock_data, LCK_ASSERT_OWNED)
813fb2f6
A
175#define io_lock_held_kdp(io) \
176 kdp_lck_spin_is_acquired(&(io)->io_lock_data)
0a7de745 177#define io_unlock(io) \
316670eb 178 lck_spin_unlock(&(io)->io_lock_data)
1c79356b 179
cb323159
A
180extern void io_lock(
181 ipc_object_t io);
182extern boolean_t io_lock_try(
183 ipc_object_t io);
184
1c79356b 185#define _VOLATILE_ volatile
1c79356b 186
1c79356b 187/* Sanity check the ref count. If it is 0, we may be doubly zfreeing.
d190cdc3
A
188 * If it is larger than max int, it has been corrupted or leaked,
189 * probably by being modified into an address (this is architecture
190 * dependent, but it's safe to assume there cannot really be max int
191 * references unless some code is leaking the io_reference without leaking
192 * object). Saturate the io_reference on release kernel if it reaches
193 * max int to avoid use after free.
1c79356b
A
194 *
195 * NOTE: The 0 test alone will not catch double zfreeing of ipc_port
196 * structs, because the io_references field is the first word of the struct,
197 * and zfree modifies that to point to the next free zone element.
198 */
0a7de745 199#define IO_MAX_REFERENCES \
cb323159 200 (unsigned)(~0 ^ (1U << (sizeof(int)*BYTE_SIZE - 1)))
1c79356b 201
316670eb 202static inline void
0a7de745
A
203io_reference(ipc_object_t io)
204{
d190cdc3
A
205 ipc_object_refs_t new_io_references;
206 ipc_object_refs_t old_io_references;
207
cb323159
A
208 if ((io)->io_references == 0 ||
209 (io)->io_references >= IO_MAX_REFERENCES) {
210 panic("%s: reference count %u is invalid\n", __func__, (io)->io_references);
211 }
d190cdc3
A
212
213 do {
214 old_io_references = (io)->io_references;
215 new_io_references = old_io_references + 1;
216 if (old_io_references == IO_MAX_REFERENCES) {
217 break;
218 }
219 } while (OSCompareAndSwap(old_io_references, new_io_references,
0a7de745 220 &((io)->io_references)) == FALSE);
316670eb
A
221}
222
223
224static inline void
0a7de745
A
225io_release(ipc_object_t io)
226{
d190cdc3
A
227 ipc_object_refs_t new_io_references;
228 ipc_object_refs_t old_io_references;
229
cb323159
A
230 if ((io)->io_references == 0 ||
231 (io)->io_references >= IO_MAX_REFERENCES) {
232 panic("%s: reference count %u is invalid\n", __func__, (io)->io_references);
233 }
d190cdc3
A
234
235 do {
236 old_io_references = (io)->io_references;
237 new_io_references = old_io_references - 1;
238 if (old_io_references == IO_MAX_REFERENCES) {
239 break;
240 }
241 } while (OSCompareAndSwap(old_io_references, new_io_references,
0a7de745 242 &((io)->io_references)) == FALSE);
d190cdc3 243
0a7de745 244 /* If we just removed the last reference count */
d190cdc3 245 if (1 == old_io_references) {
316670eb
A
246 /* Free the object */
247 io_free(io_otype((io)), (io));
248 }
249}
1c79356b 250
d9a64523 251/*
2d21ac55
A
252 * Retrieve a label for use in a kernel call that takes a security
253 * label as a parameter. If necessary, io_getlabel acquires internal
254 * (not io_lock) locks, and io_unlocklabel releases them.
255 */
256
257struct label;
0a7de745 258extern struct label *io_getlabel(ipc_object_t obj);
2d21ac55
A
259#define io_unlocklabel(obj)
260
1c79356b
A
261/*
262 * Exported interfaces
263 */
264
265/* Take a reference to an object */
266extern void ipc_object_reference(
0a7de745 267 ipc_object_t object);
1c79356b
A
268
269/* Release a reference to an object */
270extern void ipc_object_release(
0a7de745 271 ipc_object_t object);
1c79356b
A
272
273/* Look up an object in a space */
274extern kern_return_t ipc_object_translate(
0a7de745
A
275 ipc_space_t space,
276 mach_port_name_t name,
277 mach_port_right_t right,
278 ipc_object_t *objectp);
1c79356b
A
279
280/* Look up two objects in a space, locking them in the order described */
281extern kern_return_t ipc_object_translate_two(
0a7de745
A
282 ipc_space_t space,
283 mach_port_name_t name1,
284 mach_port_right_t right1,
285 ipc_object_t *objectp1,
286 mach_port_name_t name2,
287 mach_port_right_t right2,
288 ipc_object_t *objectp2);
1c79356b 289
cb323159
A
290/* Validate an object as belonging to the correct zone */
291extern void ipc_object_validate(
292 ipc_object_t object);
293
1c79356b
A
294/* Allocate a dead-name entry */
295extern kern_return_t
296ipc_object_alloc_dead(
0a7de745
A
297 ipc_space_t space,
298 mach_port_name_t *namep);
1c79356b
A
299
300/* Allocate a dead-name entry, with a specific name */
301extern kern_return_t ipc_object_alloc_dead_name(
0a7de745
A
302 ipc_space_t space,
303 mach_port_name_t name);
1c79356b
A
304
305/* Allocate an object */
306extern kern_return_t ipc_object_alloc(
0a7de745
A
307 ipc_space_t space,
308 ipc_object_type_t otype,
309 mach_port_type_t type,
310 mach_port_urefs_t urefs,
311 mach_port_name_t *namep,
312 ipc_object_t *objectp);
1c79356b
A
313
314/* Allocate an object, with a specific name */
315extern kern_return_t ipc_object_alloc_name(
0a7de745
A
316 ipc_space_t space,
317 ipc_object_type_t otype,
318 mach_port_type_t type,
319 mach_port_urefs_t urefs,
320 mach_port_name_t name,
321 ipc_object_t *objectp);
1c79356b
A
322
323/* Convert a send type name to a received type name */
324extern mach_msg_type_name_t ipc_object_copyin_type(
0a7de745 325 mach_msg_type_name_t msgt_name);
1c79356b
A
326
327/* Copyin a capability from a space */
328extern kern_return_t ipc_object_copyin(
0a7de745
A
329 ipc_space_t space,
330 mach_port_name_t name,
331 mach_msg_type_name_t msgt_name,
cb323159
A
332 ipc_object_t *objectp,
333 mach_port_context_t context,
334 mach_msg_guard_flags_t *guard_flags,
335 uint32_t kmsg_flags);
1c79356b
A
336
337/* Copyin a naked capability from the kernel */
338extern void ipc_object_copyin_from_kernel(
0a7de745
A
339 ipc_object_t object,
340 mach_msg_type_name_t msgt_name);
1c79356b
A
341
342/* Destroy a naked capability */
343extern void ipc_object_destroy(
0a7de745
A
344 ipc_object_t object,
345 mach_msg_type_name_t msgt_name);
1c79356b 346
6d2010ae
A
347/* Destroy a naked destination capability */
348extern void ipc_object_destroy_dest(
0a7de745
A
349 ipc_object_t object,
350 mach_msg_type_name_t msgt_name);
6d2010ae 351
cb323159
A
352/* Insert a send right into an object already in the current space */
353extern kern_return_t ipc_object_insert_send_right(
354 ipc_space_t space,
355 mach_port_name_t name,
356 mach_msg_type_name_t msgt_name);
357
1c79356b
A
358/* Copyout a capability, placing it into a space */
359extern kern_return_t ipc_object_copyout(
0a7de745
A
360 ipc_space_t space,
361 ipc_object_t object,
362 mach_msg_type_name_t msgt_name,
cb323159
A
363 mach_port_context_t *context,
364 mach_msg_guard_flags_t *guard_flags,
0a7de745 365 mach_port_name_t *namep);
1c79356b
A
366
367/* Copyout a capability with a name, placing it into a space */
368extern kern_return_t ipc_object_copyout_name(
0a7de745
A
369 ipc_space_t space,
370 ipc_object_t object,
371 mach_msg_type_name_t msgt_name,
0a7de745 372 mach_port_name_t name);
1c79356b
A
373
374/* Translate/consume the destination right of a message */
375extern void ipc_object_copyout_dest(
0a7de745
A
376 ipc_space_t space,
377 ipc_object_t object,
378 mach_msg_type_name_t msgt_name,
379 mach_port_name_t *namep);
1c79356b 380
0a7de745 381#endif /* _IPC_IPC_OBJECT_H_ */