2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
31 * Revision 1.1.1.1 1998/09/22 21:05:28 wsanchez
32 * Import of Mac OS X kernel (~semeria)
34 * Revision 1.2 1998/06/01 17:29:25 youngwor
35 * Added infrastructure for shared port space support
37 * Revision 1.1.1.1 1998/03/07 02:26:16 wsanchez
38 * Import of OSF Mach kernel (~mburg)
40 * Revision 1.2.10.1 1994/09/23 02:12:16 ezf
41 * change marker to not FREE
42 * [1994/09/22 21:30:49 ezf]
44 * Revision 1.2.2.3 1993/07/22 16:17:30 rod
45 * Add ANSI prototypes. CR #9523.
46 * [1993/07/22 13:33:29 rod]
48 * Revision 1.2.2.2 1993/06/02 23:33:55 jeffc
49 * Added to OSF/1 R1.3 from NMK15.0.
50 * [1993/06/02 21:11:14 jeffc]
52 * Revision 1.2 1992/11/25 01:09:56 robert
53 * integrate changes below for norma_14
55 * Philippe Bernadat (bernadat) at gr.osf.org
56 * Limit ipc table allocation chunks to 8 pages, otherwise
57 * the kernel might dead lock because of VM_PAGE_FREE_RESERVED
58 * limited to 15. [dlb@osf.org & barbou@gr.osf.org]
59 * [1992/11/13 19:31:46 robert]
61 * Revision 1.1 1992/09/30 02:08:13 robert
68 * Revision 2.6 91/10/09 16:11:08 af
69 * Revision 2.5.2.1 91/09/16 10:16:06 rpd
70 * Removed unused variables.
73 * Revision 2.5.2.1 91/09/16 10:16:06 rpd
74 * Removed unused variables.
77 * Revision 2.5 91/05/14 16:37:35 mrt
78 * Correcting copyright
80 * Revision 2.4 91/03/16 14:48:52 rpd
81 * Added ipc_table_realloc and ipc_table_reallocable.
84 * Revision 2.3 91/02/05 17:24:15 mrt
85 * Changed to new Mach copyright
86 * [91/02/01 15:52:05 mrt]
88 * Revision 2.2 90/06/02 14:51:58 rpd
89 * Created for new IPC.
90 * [90/03/26 21:04:20 rpd]
95 * Mach Operating System
96 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
97 * All Rights Reserved.
99 * Permission to use, copy, modify and distribute this software and its
100 * documentation is hereby granted, provided that both the copyright
101 * notice and this permission notice appear in all copies of the
102 * software, derivative works or modified versions, and any portions
103 * thereof, and that both notices appear in supporting documentation.
105 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
106 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
107 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
109 * Carnegie Mellon requests users of this software to return to
111 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
112 * School of Computer Science
113 * Carnegie Mellon University
114 * Pittsburgh PA 15213-3890
116 * any improvements or extensions that they make and grant Carnegie Mellon
117 * the rights to redistribute these changes.
122 * File: ipc/ipc_table.c
123 * Author: Rich Draves
126 * Functions to manipulate tables of IPC capabilities.
129 #include <mach/kern_return.h>
130 #include <mach/vm_param.h>
131 #include <ipc/ipc_table.h>
132 #include <ipc/ipc_port.h>
133 #include <ipc/ipc_entry.h>
134 #include <kern/kalloc.h>
135 #include <vm/vm_kern.h>
138 * Forward declarations
141 ipc_table_size_t its
,
147 * We borrow the kalloc map, rather than creating
148 * yet another submap of the kernel map.
151 extern vm_map_t kalloc_map
;
153 ipc_table_size_t ipc_table_entries
;
154 unsigned int ipc_table_entries_size
= 512;
156 ipc_table_size_t ipc_table_dnrequests
;
157 unsigned int ipc_table_dnrequests_size
= 64;
161 ipc_table_size_t its
, /* array to fill */
162 unsigned int num
, /* size of array */
163 unsigned int min
, /* at least this many elements */
164 vm_size_t elemsize
) /* size of elements */
167 vm_size_t minsize
= min
* elemsize
;
171 /* first use powers of two, up to the page size */
173 for (index
= 0, size
= 1;
174 (index
< num
) && (size
< PAGE_SIZE
);
176 if (size
>= minsize
) {
177 its
[index
].its_size
= size
/ elemsize
;
182 /* then increments of a page, then two pages, etc. */
184 for (incrsize
= PAGE_SIZE
; index
< num
;) {
188 (period
< 15) && (index
< num
);
189 period
++, size
+= incrsize
) {
190 if (size
>= minsize
) {
191 its
[index
].its_size
= size
/ elemsize
;
195 if (incrsize
< (PAGE_SIZE
<< 3))
203 ipc_table_entries
= (ipc_table_size_t
)
204 kalloc(sizeof(struct ipc_table_size
) *
205 ipc_table_entries_size
);
206 assert(ipc_table_entries
!= ITS_NULL
);
208 ipc_table_fill(ipc_table_entries
, ipc_table_entries_size
- 1,
209 16, sizeof(struct ipc_entry
));
211 /* the last two elements should have the same size */
213 ipc_table_entries
[ipc_table_entries_size
- 1].its_size
=
214 ipc_table_entries
[ipc_table_entries_size
- 2].its_size
;
217 ipc_table_dnrequests
= (ipc_table_size_t
)
218 kalloc(sizeof(struct ipc_table_size
) *
219 ipc_table_dnrequests_size
);
220 assert(ipc_table_dnrequests
!= ITS_NULL
);
222 ipc_table_fill(ipc_table_dnrequests
, ipc_table_dnrequests_size
- 1,
223 2, sizeof(struct ipc_port_request
));
225 /* the last element should have zero size */
227 ipc_table_dnrequests
[ipc_table_dnrequests_size
- 1].its_size
= 0;
231 * Routine: ipc_table_alloc
244 if (size
< PAGE_SIZE
)
245 table
= kalloc(size
);
247 if (kmem_alloc(kalloc_map
, &table
, size
) != KERN_SUCCESS
)
254 * Routine: ipc_table_realloc
256 * Reallocate a big table.
258 * The new table remaps the old table,
259 * so copying is not necessary.
261 * Only works for page-size or bigger tables.
268 vm_offset_t old_table
,
271 vm_offset_t new_table
;
273 if (kmem_realloc(kalloc_map
, old_table
, old_size
,
274 &new_table
, new_size
) != KERN_SUCCESS
)
281 * Routine: ipc_table_free
283 * Free a table allocated with ipc_table_alloc or
294 if (size
< PAGE_SIZE
)
297 kmem_free(kalloc_map
, table
, size
);