]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_table.c
xnu-344.34.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_table.c
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 * HISTORY
27 *
28 * Revision 1.1.1.1 1998/09/22 21:05:28 wsanchez
29 * Import of Mac OS X kernel (~semeria)
30 *
31 * Revision 1.2 1998/06/01 17:29:25 youngwor
32 * Added infrastructure for shared port space support
33 *
34 * Revision 1.1.1.1 1998/03/07 02:26:16 wsanchez
35 * Import of OSF Mach kernel (~mburg)
36 *
37 * Revision 1.2.10.1 1994/09/23 02:12:16 ezf
38 * change marker to not FREE
39 * [1994/09/22 21:30:49 ezf]
40 *
41 * Revision 1.2.2.3 1993/07/22 16:17:30 rod
42 * Add ANSI prototypes. CR #9523.
43 * [1993/07/22 13:33:29 rod]
44 *
45 * Revision 1.2.2.2 1993/06/02 23:33:55 jeffc
46 * Added to OSF/1 R1.3 from NMK15.0.
47 * [1993/06/02 21:11:14 jeffc]
48 *
49 * Revision 1.2 1992/11/25 01:09:56 robert
50 * integrate changes below for norma_14
51 *
52 * Philippe Bernadat (bernadat) at gr.osf.org
53 * Limit ipc table allocation chunks to 8 pages, otherwise
54 * the kernel might dead lock because of VM_PAGE_FREE_RESERVED
55 * limited to 15. [dlb@osf.org & barbou@gr.osf.org]
56 * [1992/11/13 19:31:46 robert]
57 *
58 * Revision 1.1 1992/09/30 02:08:13 robert
59 * Initial revision
60 *
61 * $EndLog$
62 */
63 /* CMU_HIST */
64 /*
65 * Revision 2.6 91/10/09 16:11:08 af
66 * Revision 2.5.2.1 91/09/16 10:16:06 rpd
67 * Removed unused variables.
68 * [91/09/02 rpd]
69 *
70 * Revision 2.5.2.1 91/09/16 10:16:06 rpd
71 * Removed unused variables.
72 * [91/09/02 rpd]
73 *
74 * Revision 2.5 91/05/14 16:37:35 mrt
75 * Correcting copyright
76 *
77 * Revision 2.4 91/03/16 14:48:52 rpd
78 * Added ipc_table_realloc and ipc_table_reallocable.
79 * [91/03/04 rpd]
80 *
81 * Revision 2.3 91/02/05 17:24:15 mrt
82 * Changed to new Mach copyright
83 * [91/02/01 15:52:05 mrt]
84 *
85 * Revision 2.2 90/06/02 14:51:58 rpd
86 * Created for new IPC.
87 * [90/03/26 21:04:20 rpd]
88 *
89 */
90 /* CMU_ENDHIST */
91 /*
92 * Mach Operating System
93 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
94 * All Rights Reserved.
95 *
96 * Permission to use, copy, modify and distribute this software and its
97 * documentation is hereby granted, provided that both the copyright
98 * notice and this permission notice appear in all copies of the
99 * software, derivative works or modified versions, and any portions
100 * thereof, and that both notices appear in supporting documentation.
101 *
102 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
103 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
104 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
105 *
106 * Carnegie Mellon requests users of this software to return to
107 *
108 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
109 * School of Computer Science
110 * Carnegie Mellon University
111 * Pittsburgh PA 15213-3890
112 *
113 * any improvements or extensions that they make and grant Carnegie Mellon
114 * the rights to redistribute these changes.
115 */
116 /*
117 */
118 /*
119 * File: ipc/ipc_table.c
120 * Author: Rich Draves
121 * Date: 1989
122 *
123 * Functions to manipulate tables of IPC capabilities.
124 */
125
126 #include <mach/kern_return.h>
127 #include <mach/vm_param.h>
128 #include <ipc/ipc_table.h>
129 #include <ipc/ipc_port.h>
130 #include <ipc/ipc_entry.h>
131 #include <kern/kalloc.h>
132 #include <vm/vm_kern.h>
133
134 /*
135 * Forward declarations
136 */
137 void ipc_table_fill(
138 ipc_table_size_t its,
139 unsigned int num,
140 unsigned int min,
141 vm_size_t elemsize);
142
143 /*
144 * We borrow the kalloc map, rather than creating
145 * yet another submap of the kernel map.
146 */
147
148 extern vm_map_t kalloc_map;
149
150 ipc_table_size_t ipc_table_entries;
151 unsigned int ipc_table_entries_size = 512;
152
153 ipc_table_size_t ipc_table_dnrequests;
154 unsigned int ipc_table_dnrequests_size = 64;
155
156 void
157 ipc_table_fill(
158 ipc_table_size_t its, /* array to fill */
159 unsigned int num, /* size of array */
160 unsigned int min, /* at least this many elements */
161 vm_size_t elemsize) /* size of elements */
162 {
163 unsigned int index;
164 vm_size_t minsize = min * elemsize;
165 vm_size_t size;
166 vm_size_t incrsize;
167
168 /* first use powers of two, up to the page size */
169
170 for (index = 0, size = 1;
171 (index < num) && (size < PAGE_SIZE);
172 size <<= 1) {
173 if (size >= minsize) {
174 its[index].its_size = size / elemsize;
175 index++;
176 }
177 }
178
179 /* then increments of a page, then two pages, etc. */
180
181 for (incrsize = PAGE_SIZE; index < num;) {
182 unsigned int period;
183
184 for (period = 0;
185 (period < 15) && (index < num);
186 period++, size += incrsize) {
187 if (size >= minsize) {
188 its[index].its_size = size / elemsize;
189 index++;
190 }
191 }
192 if (incrsize < (PAGE_SIZE << 3))
193 incrsize <<= 1;
194 }
195 }
196
197 void
198 ipc_table_init(void)
199 {
200 ipc_table_entries = (ipc_table_size_t)
201 kalloc(sizeof(struct ipc_table_size) *
202 ipc_table_entries_size);
203 assert(ipc_table_entries != ITS_NULL);
204
205 ipc_table_fill(ipc_table_entries, ipc_table_entries_size - 1,
206 16, sizeof(struct ipc_entry));
207
208 /* the last two elements should have the same size */
209
210 ipc_table_entries[ipc_table_entries_size - 1].its_size =
211 ipc_table_entries[ipc_table_entries_size - 2].its_size;
212
213
214 ipc_table_dnrequests = (ipc_table_size_t)
215 kalloc(sizeof(struct ipc_table_size) *
216 ipc_table_dnrequests_size);
217 assert(ipc_table_dnrequests != ITS_NULL);
218
219 ipc_table_fill(ipc_table_dnrequests, ipc_table_dnrequests_size - 1,
220 2, sizeof(struct ipc_port_request));
221
222 /* the last element should have zero size */
223
224 ipc_table_dnrequests[ipc_table_dnrequests_size - 1].its_size = 0;
225 }
226
227 /*
228 * Routine: ipc_table_alloc
229 * Purpose:
230 * Allocate a table.
231 * Conditions:
232 * May block.
233 */
234
235 vm_offset_t
236 ipc_table_alloc(
237 vm_size_t size)
238 {
239 vm_offset_t table;
240
241 if (size < PAGE_SIZE)
242 table = kalloc(size);
243 else
244 if (kmem_alloc(kalloc_map, &table, size) != KERN_SUCCESS)
245 table = 0;
246
247 return table;
248 }
249
250 /*
251 * Routine: ipc_table_realloc
252 * Purpose:
253 * Reallocate a big table.
254 *
255 * The new table remaps the old table,
256 * so copying is not necessary.
257 * Conditions:
258 * Only works for page-size or bigger tables.
259 * May block.
260 */
261
262 vm_offset_t
263 ipc_table_realloc(
264 vm_size_t old_size,
265 vm_offset_t old_table,
266 vm_size_t new_size)
267 {
268 vm_offset_t new_table;
269
270 if (kmem_realloc(kalloc_map, old_table, old_size,
271 &new_table, new_size) != KERN_SUCCESS)
272 new_table = 0;
273
274 return new_table;
275 }
276
277 /*
278 * Routine: ipc_table_free
279 * Purpose:
280 * Free a table allocated with ipc_table_alloc or
281 * ipc_table_realloc.
282 * Conditions:
283 * May block.
284 */
285
286 void
287 ipc_table_free(
288 vm_size_t size,
289 vm_offset_t table)
290 {
291 if (size < PAGE_SIZE)
292 kfree(table, size);
293 else
294 kmem_free(kalloc_map, table, size);
295 }