]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39037602 | 2 | * Copyright (c) 2000-2016 Apple Computer, 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_space.h | |
66 | * Author: Rich Draves | |
67 | * Date: 1989 | |
68 | * | |
69 | * Definitions for IPC spaces of capabilities. | |
70 | */ | |
71 | ||
0a7de745 | 72 | #ifndef _IPC_IPC_SPACE_H_ |
1c79356b A |
73 | #define _IPC_IPC_SPACE_H_ |
74 | ||
75 | ||
5c9f4661 | 76 | #include <prng/random.h> |
1c79356b A |
77 | #include <mach/mach_types.h> |
78 | #include <mach/boolean.h> | |
79 | #include <mach/kern_return.h> | |
80 | #include <mach/vm_types.h> | |
81 | ||
9bccf70c A |
82 | #include <sys/appleapiopts.h> |
83 | ||
84 | #ifdef __APPLE_API_PRIVATE | |
39037602 | 85 | #ifdef MACH_KERNEL_PRIVATE |
1c79356b | 86 | #include <kern/macro_help.h> |
9bccf70c | 87 | #include <kern/kern_types.h> |
fe8ab488 | 88 | #include <kern/locks.h> |
1c79356b A |
89 | #include <kern/task.h> |
90 | #include <kern/zalloc.h> | |
91 | #include <ipc/ipc_entry.h> | |
1c79356b A |
92 | #include <ipc/ipc_types.h> |
93 | ||
316670eb A |
94 | #include <libkern/OSAtomic.h> |
95 | ||
1c79356b A |
96 | /* |
97 | * Every task has a space of IPC capabilities. | |
98 | * IPC operations like send and receive use this space. | |
99 | * IPC kernel calls manipulate the space of the target task. | |
100 | * | |
101 | * Every space has a non-NULL is_table with is_table_size entries. | |
1c79356b | 102 | * |
316670eb A |
103 | * Only one thread can be growing the space at a time. Others |
104 | * that need it grown wait for the first. We do almost all the | |
105 | * work with the space unlocked, so lookups proceed pretty much | |
106 | * unaffected while the grow operation is underway. | |
1c79356b A |
107 | */ |
108 | ||
109 | typedef natural_t ipc_space_refs_t; | |
0a7de745 A |
110 | #define IS_REFS_MAX 0x0fffffff |
111 | #define IS_INACTIVE 0x40000000 /* space is inactive */ | |
112 | #define IS_GROWING 0x20000000 /* space is growing */ | |
113 | #define IS_ENTROPY_CNT 1 /* per-space entropy pool size */ | |
1c79356b A |
114 | |
115 | struct ipc_space { | |
0a7de745 A |
116 | lck_spin_t is_lock_data; |
117 | ipc_space_refs_t is_bits; /* holds refs, active, growing */ | |
118 | ipc_entry_num_t is_table_size; /* current size of table */ | |
119 | ipc_entry_num_t is_table_hashed;/* count of hashed elements */ | |
120 | ipc_entry_num_t is_table_free; /* count of free elements */ | |
121 | ipc_entry_t is_table; /* an array of entries */ | |
316670eb | 122 | struct ipc_table_size *is_table_next; /* info for larger table */ |
ea3f0419 A |
123 | task_t is_task; /* associated task */ |
124 | ipc_label_t is_label; /* [private] mandatory access label */ | |
0a7de745 A |
125 | ipc_entry_num_t is_low_mod; /* lowest modified entry during growth */ |
126 | ipc_entry_num_t is_high_mod; /* highest modified entry during growth */ | |
5c9f4661 A |
127 | struct bool_gen bool_gen; /* state for boolean RNG */ |
128 | unsigned int is_entropy[IS_ENTROPY_CNT]; /* pool of entropy taken from RNG */ | |
0a7de745 | 129 | int is_node_id; /* HOST_LOCAL_NODE, or remote node if proxy space */ |
1c79356b A |
130 | }; |
131 | ||
0a7de745 A |
132 | #define IS_NULL ((ipc_space_t) 0) |
133 | #define IS_INSPECT_NULL ((ipc_space_inspect_t) 0) | |
1c79356b | 134 | |
0a7de745 | 135 | #define is_active(is) (((is)->is_bits & IS_INACTIVE) != IS_INACTIVE) |
316670eb | 136 | |
0a7de745 | 137 | static inline void |
316670eb A |
138 | is_mark_inactive(ipc_space_t is) |
139 | { | |
140 | assert(is_active(is)); | |
141 | OSBitOrAtomic(IS_INACTIVE, &is->is_bits); | |
142 | } | |
143 | ||
0a7de745 | 144 | #define is_growing(is) (((is)->is_bits & IS_GROWING) == IS_GROWING) |
316670eb A |
145 | |
146 | static inline void | |
147 | is_start_growing(ipc_space_t is) | |
148 | { | |
149 | assert(!is_growing(is)); | |
150 | OSBitOrAtomic(IS_GROWING, &is->is_bits); | |
151 | } | |
152 | ||
153 | static inline void | |
0a7de745 | 154 | is_done_growing(ipc_space_t is) |
316670eb A |
155 | { |
156 | assert(is_growing(is)); | |
157 | OSBitAndAtomic(~IS_GROWING, &is->is_bits); | |
158 | } | |
159 | ||
1c79356b A |
160 | extern zone_t ipc_space_zone; |
161 | ||
0a7de745 A |
162 | #define is_alloc() ((ipc_space_t) zalloc(ipc_space_zone)) |
163 | #define is_free(is) zfree(ipc_space_zone, (is)) | |
1c79356b A |
164 | |
165 | extern ipc_space_t ipc_space_kernel; | |
166 | extern ipc_space_t ipc_space_reply; | |
0a7de745 | 167 | #if DIPC |
1c79356b | 168 | extern ipc_space_t ipc_space_remote; |
0a7de745 A |
169 | #endif /* DIPC */ |
170 | #if DIPC | |
1c79356b | 171 | extern ipc_space_t default_pager_space; |
0a7de745 | 172 | #endif /* DIPC */ |
316670eb | 173 | |
0a7de745 A |
174 | extern lck_grp_t ipc_lck_grp; |
175 | extern lck_attr_t ipc_lck_attr; | |
316670eb | 176 | |
0a7de745 A |
177 | #define is_lock_init(is) lck_spin_init(&(is)->is_lock_data, &ipc_lck_grp, &ipc_lck_attr) |
178 | #define is_lock_destroy(is) lck_spin_destroy(&(is)->is_lock_data, &ipc_lck_grp) | |
316670eb | 179 | |
0a7de745 A |
180 | #define is_read_lock(is) lck_spin_lock_grp(&(is)->is_lock_data, &ipc_lck_grp) |
181 | #define is_read_unlock(is) lck_spin_unlock(&(is)->is_lock_data) | |
182 | #define is_read_sleep(is) lck_spin_sleep_grp(&(is)->is_lock_data, \ | |
183 | LCK_SLEEP_DEFAULT, \ | |
184 | (event_t)(is), \ | |
185 | THREAD_UNINT, \ | |
186 | &ipc_lck_grp) | |
b0d623f7 | 187 | |
0a7de745 | 188 | #define is_write_lock(is) lck_spin_lock_grp(&(is)->is_lock_data, &ipc_lck_grp) |
0a7de745 A |
189 | #define is_write_unlock(is) lck_spin_unlock(&(is)->is_lock_data) |
190 | #define is_write_sleep(is) lck_spin_sleep_grp(&(is)->is_lock_data, \ | |
191 | LCK_SLEEP_DEFAULT, \ | |
192 | (event_t)(is), \ | |
193 | THREAD_UNINT, \ | |
194 | &ipc_lck_grp) | |
1c79356b | 195 | |
0a7de745 | 196 | #define is_refs(is) ((is)->is_bits & IS_REFS_MAX) |
316670eb A |
197 | |
198 | static inline void | |
199 | is_reference(ipc_space_t is) | |
200 | { | |
201 | assert(is_refs(is) > 0 && is_refs(is) < IS_REFS_MAX); | |
202 | OSIncrementAtomic(&(is->is_bits)); | |
203 | } | |
204 | ||
1c79356b | 205 | |
316670eb | 206 | static inline void |
0a7de745 A |
207 | is_release(ipc_space_t is) |
208 | { | |
316670eb | 209 | assert(is_refs(is) > 0); |
1c79356b | 210 | |
0a7de745 A |
211 | /* If we just removed the last reference count */ |
212 | if (1 == (OSDecrementAtomic(&(is->is_bits)) & IS_REFS_MAX)) { | |
39236c6e | 213 | assert(!is_active(is)); |
316670eb A |
214 | is_lock_destroy(is); |
215 | is_free(is); | |
216 | } | |
217 | } | |
0a7de745 A |
218 | |
219 | #define current_space_fast() (current_task_fast()->itk_space) | |
220 | #define current_space() (current_space_fast()) | |
1c79356b | 221 | |
9bccf70c A |
222 | /* Create a special IPC space */ |
223 | extern kern_return_t ipc_space_create_special( | |
0a7de745 | 224 | ipc_space_t *spacep); |
1c79356b | 225 | |
5c9f4661 | 226 | /* Create a new IPC space */ |
9bccf70c | 227 | extern kern_return_t ipc_space_create( |
0a7de745 | 228 | ipc_table_size_t initial, |
ea3f0419 | 229 | ipc_label_t label, |
0a7de745 | 230 | ipc_space_t *spacep); |
9bccf70c | 231 | |
ea3f0419 A |
232 | /* Change the label on an existing space */ |
233 | extern kern_return_t ipc_space_label( | |
234 | ipc_space_t space, | |
235 | ipc_label_t label); | |
236 | ||
237 | /* Add a label to an existing space */ | |
238 | extern kern_return_t ipc_space_add_label( | |
239 | ipc_space_t space, | |
240 | ipc_label_t label); | |
241 | ||
9bccf70c | 242 | /* Mark a space as dead and cleans up the entries*/ |
316670eb | 243 | extern void ipc_space_terminate( |
0a7de745 | 244 | ipc_space_t space); |
1c79356b | 245 | |
91447636 A |
246 | /* Clean up the entries - but leave the space alive */ |
247 | extern void ipc_space_clean( | |
0a7de745 | 248 | ipc_space_t space); |
91447636 | 249 | |
5c9f4661 A |
250 | /* Permute the order of a range within an IPC space */ |
251 | extern void ipc_space_rand_freelist( | |
0a7de745 A |
252 | ipc_space_t space, |
253 | ipc_entry_t table, | |
254 | mach_port_index_t bottom, | |
255 | mach_port_index_t top); | |
5c9f4661 A |
256 | |
257 | /* Generate a new gencount rollover point from a space's entropy pool */ | |
258 | extern ipc_entry_bits_t ipc_space_get_rollpoint(ipc_space_t space); | |
cb323159 A |
259 | |
260 | /* Allocate a new port/set/dead-name in a space */ | |
261 | extern kern_return_t mach_port_allocate_internal( | |
262 | ipc_space_t space, | |
263 | mach_port_right_t right, | |
264 | mach_port_qos_t *qosp, | |
265 | mach_port_name_t *namep); | |
266 | ||
1c79356b | 267 | #endif /* MACH_KERNEL_PRIVATE */ |
9bccf70c A |
268 | #endif /* __APPLE_API_PRIVATE */ |
269 | ||
270 | #ifdef __APPLE_API_UNSTABLE | |
271 | #ifndef MACH_KERNEL_PRIVATE | |
272 | ||
0a7de745 | 273 | extern ipc_space_t current_space(void); |
9bccf70c A |
274 | |
275 | #endif /* !MACH_KERNEL_PRIVATE */ | |
276 | #endif /* __APPLE_API_UNSTABLE */ | |
1c79356b A |
277 | |
278 | /* Take a reference on a space */ | |
279 | extern void ipc_space_reference( | |
0a7de745 | 280 | ipc_space_t space); |
1c79356b A |
281 | |
282 | /* Realase a reference on a space */ | |
283 | extern void ipc_space_release( | |
0a7de745 | 284 | ipc_space_t space); |
1c79356b | 285 | |
0a7de745 | 286 | #endif /* _IPC_IPC_SPACE_H_ */ |