xnu-2050.48.11.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_space.h
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
8f6c56a5 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.
8f6c56a5 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.
17 *
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
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_space.h
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Definitions for IPC spaces of capabilities.
70 */
71
72#ifndef _IPC_IPC_SPACE_H_
73#define _IPC_IPC_SPACE_H_
74
75
76#include <mach/mach_types.h>
77#include <mach/boolean.h>
78#include <mach/kern_return.h>
79#include <mach/vm_types.h>
80
9bccf70c
A
81#include <sys/appleapiopts.h>
82
83#ifdef __APPLE_API_PRIVATE
2d21ac55 84#if MACH_KERNEL_PRIVATE
1c79356b 85#include <kern/macro_help.h>
9bccf70c 86#include <kern/kern_types.h>
1c79356b
A
87#include <kern/lock.h>
88#include <kern/task.h>
89#include <kern/zalloc.h>
90#include <ipc/ipc_entry.h>
1c79356b
A
91#include <ipc/ipc_types.h>
92
316670eb
A
93#include <libkern/OSAtomic.h>
94
1c79356b
A
95/*
96 * Every task has a space of IPC capabilities.
97 * IPC operations like send and receive use this space.
98 * IPC kernel calls manipulate the space of the target task.
99 *
100 * Every space has a non-NULL is_table with is_table_size entries.
1c79356b 101 *
316670eb
A
102 * Only one thread can be growing the space at a time. Others
103 * that need it grown wait for the first. We do almost all the
104 * work with the space unlocked, so lookups proceed pretty much
105 * unaffected while the grow operation is underway.
1c79356b
A
106 */
107
108typedef natural_t ipc_space_refs_t;
316670eb
A
109#define IS_REFS_MAX 0x0fffffff
110#define IS_INACTIVE 0x40000000 /* space is inactive */
111#define IS_GROWING 0x20000000 /* space is growing */
1c79356b
A
112
113struct ipc_space {
316670eb
A
114 lck_spin_t is_lock_data;
115 ipc_space_refs_t is_bits; /* holds refs, active, growing */
1c79356b 116 ipc_entry_num_t is_table_size; /* current size of table */
316670eb 117 ipc_entry_t is_table; /* an array of entries */
2d21ac55 118 task_t is_task; /* associated task */
316670eb
A
119 struct ipc_table_size *is_table_next; /* info for larger table */
120 ipc_entry_num_t is_low_mod; /* lowest modified entry during growth */
121 ipc_entry_num_t is_high_mod; /* highest modified entry during growth */
1c79356b
A
122};
123
124#define IS_NULL ((ipc_space_t) 0)
125
316670eb
A
126#define is_active(is) (((is)->is_bits & IS_INACTIVE) != IS_INACTIVE)
127
128static inline void
129is_mark_inactive(ipc_space_t is)
130{
131 assert(is_active(is));
132 OSBitOrAtomic(IS_INACTIVE, &is->is_bits);
133}
134
135#define is_growing(is) (((is)->is_bits & IS_GROWING) == IS_GROWING)
136
137static inline void
138is_start_growing(ipc_space_t is)
139{
140 assert(!is_growing(is));
141 OSBitOrAtomic(IS_GROWING, &is->is_bits);
142}
143
144static inline void
145is_done_growing(ipc_space_t is)
146{
147 assert(is_growing(is));
148 OSBitAndAtomic(~IS_GROWING, &is->is_bits);
149}
150
1c79356b
A
151extern zone_t ipc_space_zone;
152
153#define is_alloc() ((ipc_space_t) zalloc(ipc_space_zone))
91447636 154#define is_free(is) zfree(ipc_space_zone, (is))
1c79356b
A
155
156extern ipc_space_t ipc_space_kernel;
157extern ipc_space_t ipc_space_reply;
158#if DIPC
159extern ipc_space_t ipc_space_remote;
160#endif /* DIPC */
316670eb 161#if DIPC
1c79356b 162extern ipc_space_t default_pager_space;
316670eb
A
163#endif /* DIPC */
164
165extern lck_grp_t ipc_lck_grp;
166extern lck_attr_t ipc_lck_attr;
167
168#define is_lock_init(is) lck_spin_init(&(is)->is_lock_data, &ipc_lck_grp, &ipc_lck_attr)
169#define is_lock_destroy(is) lck_spin_destroy(&(is)->is_lock_data, &ipc_lck_grp)
170
171#define is_read_lock(is) lck_spin_lock(&(is)->is_lock_data)
172#define is_read_unlock(is) lck_spin_unlock(&(is)->is_lock_data)
173#define is_read_sleep(is) lck_spin_sleep(&(is)->is_lock_data, \
b0d623f7
A
174 LCK_SLEEP_DEFAULT, \
175 (event_t)(is), \
176 THREAD_UNINT)
177
316670eb
A
178#define is_write_lock(is) lck_spin_lock(&(is)->is_lock_data)
179#define is_write_lock_try(is) lck_spin_try_lock(&(is)->is_lock_data)
180#define is_write_unlock(is) lck_spin_unlock(&(is)->is_lock_data)
181#define is_write_sleep(is) lck_spin_sleep(&(is)->is_lock_data, \
b0d623f7
A
182 LCK_SLEEP_DEFAULT, \
183 (event_t)(is), \
184 THREAD_UNINT)
1c79356b 185
316670eb
A
186#define is_refs(is) ((is)->is_bits & IS_REFS_MAX)
187
188static inline void
189is_reference(ipc_space_t is)
190{
191 assert(is_refs(is) > 0 && is_refs(is) < IS_REFS_MAX);
192 OSIncrementAtomic(&(is->is_bits));
193}
194
1c79356b 195
316670eb
A
196static inline void
197is_release(ipc_space_t is) {
198 assert(is_refs(is) > 0);
1c79356b 199
316670eb
A
200 /* If we just removed the last reference count */
201 if ( 1 == (OSDecrementAtomic(&(is->is_bits)) & IS_REFS_MAX)) {
202 is_lock_destroy(is);
203 is_free(is);
204 }
205}
206
1c79356b
A
207#define current_space_fast() (current_task_fast()->itk_space)
208#define current_space() (current_space_fast())
209
9bccf70c
A
210/* Create a special IPC space */
211extern kern_return_t ipc_space_create_special(
212 ipc_space_t *spacep);
1c79356b 213
9bccf70c
A
214/* Create new IPC space */
215extern kern_return_t ipc_space_create(
216 ipc_table_size_t initial,
217 ipc_space_t *spacep);
218
219/* Mark a space as dead and cleans up the entries*/
316670eb 220extern void ipc_space_terminate(
9bccf70c 221 ipc_space_t space);
1c79356b 222
91447636
A
223/* Clean up the entries - but leave the space alive */
224extern void ipc_space_clean(
225 ipc_space_t space);
226
1c79356b 227#endif /* MACH_KERNEL_PRIVATE */
9bccf70c
A
228#endif /* __APPLE_API_PRIVATE */
229
230#ifdef __APPLE_API_UNSTABLE
231#ifndef MACH_KERNEL_PRIVATE
232
233extern ipc_space_t current_space(void);
234
235#endif /* !MACH_KERNEL_PRIVATE */
236#endif /* __APPLE_API_UNSTABLE */
1c79356b
A
237
238/* Take a reference on a space */
239extern void ipc_space_reference(
240 ipc_space_t space);
241
242/* Realase a reference on a space */
243extern void ipc_space_release(
244 ipc_space_t space);
245
1c79356b 246#endif /* _IPC_IPC_SPACE_H_ */