2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
28 * Revision 1.1.1.1 1998/09/22 21:05:28 wsanchez
29 * Import of Mac OS X kernel (~semeria)
31 * Revision 1.2 1998/06/01 17:29:25 youngwor
32 * Added infrastructure for shared port space support
34 * Revision 1.1.1.1 1998/03/07 02:26:16 wsanchez
35 * Import of OSF Mach kernel (~mburg)
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]
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]
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]
49 * Revision 1.2 1992/11/25 01:09:56 robert
50 * integrate changes below for norma_14
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]
58 * Revision 1.1 1992/09/30 02:08:13 robert
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.
70 * Revision 2.5.2.1 91/09/16 10:16:06 rpd
71 * Removed unused variables.
74 * Revision 2.5 91/05/14 16:37:35 mrt
75 * Correcting copyright
77 * Revision 2.4 91/03/16 14:48:52 rpd
78 * Added ipc_table_realloc and ipc_table_reallocable.
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]
85 * Revision 2.2 90/06/02 14:51:58 rpd
86 * Created for new IPC.
87 * [90/03/26 21:04:20 rpd]
92 * Mach Operating System
93 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
94 * All Rights Reserved.
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.
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.
106 * Carnegie Mellon requests users of this software to return to
108 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
109 * School of Computer Science
110 * Carnegie Mellon University
111 * Pittsburgh PA 15213-3890
113 * any improvements or extensions that they make and grant Carnegie Mellon
114 * the rights to redistribute these changes.
119 * File: ipc/ipc_table.c
120 * Author: Rich Draves
123 * Functions to manipulate tables of IPC capabilities.
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>
135 * Forward declarations
138 ipc_table_size_t its
,
144 * We borrow the kalloc map, rather than creating
145 * yet another submap of the kernel map.
148 extern vm_map_t kalloc_map
;
150 ipc_table_size_t ipc_table_entries
;
151 unsigned int ipc_table_entries_size
= 512;
153 ipc_table_size_t ipc_table_dnrequests
;
154 unsigned int ipc_table_dnrequests_size
= 64;
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 */
164 vm_size_t minsize
= min
* elemsize
;
168 /* first use powers of two, up to the page size */
170 for (index
= 0, size
= 1;
171 (index
< num
) && (size
< PAGE_SIZE
);
173 if (size
>= minsize
) {
174 its
[index
].its_size
= size
/ elemsize
;
179 /* then increments of a page, then two pages, etc. */
181 for (incrsize
= PAGE_SIZE
; index
< num
;) {
185 (period
< 15) && (index
< num
);
186 period
++, size
+= incrsize
) {
187 if (size
>= minsize
) {
188 its
[index
].its_size
= size
/ elemsize
;
192 if (incrsize
< (PAGE_SIZE
<< 3))
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
);
205 ipc_table_fill(ipc_table_entries
, ipc_table_entries_size
- 1,
206 16, sizeof(struct ipc_entry
));
208 /* the last two elements should have the same size */
210 ipc_table_entries
[ipc_table_entries_size
- 1].its_size
=
211 ipc_table_entries
[ipc_table_entries_size
- 2].its_size
;
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
);
219 ipc_table_fill(ipc_table_dnrequests
, ipc_table_dnrequests_size
- 1,
220 2, sizeof(struct ipc_port_request
));
222 /* the last element should have zero size */
224 ipc_table_dnrequests
[ipc_table_dnrequests_size
- 1].its_size
= 0;
228 * Routine: ipc_table_alloc
241 if (size
< PAGE_SIZE
)
242 table
= kalloc(size
);
244 if (kmem_alloc(kalloc_map
, &table
, size
) != KERN_SUCCESS
)
251 * Routine: ipc_table_realloc
253 * Reallocate a big table.
255 * The new table remaps the old table,
256 * so copying is not necessary.
258 * Only works for page-size or bigger tables.
265 vm_offset_t old_table
,
268 vm_offset_t new_table
;
270 if (kmem_realloc(kalloc_map
, old_table
, old_size
,
271 &new_table
, new_size
) != KERN_SUCCESS
)
278 * Routine: ipc_table_free
280 * Free a table allocated with ipc_table_alloc or
291 if (size
< PAGE_SIZE
)
294 kmem_free(kalloc_map
, table
, size
);