]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39236c6e | 2 | * Copyright (c) 2000-2013 Apple Inc. All rights reserved. |
5d5c5d0d | 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 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1987, 1991, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
41 | * 3. All advertising materials mentioning features or use of this software | |
42 | * must display the following acknowledgement: | |
43 | * This product includes software developed by the University of | |
44 | * California, Berkeley and its contributors. | |
45 | * 4. Neither the name of the University nor the names of its contributors | |
46 | * may be used to endorse or promote products derived from this software | |
47 | * without specific prior written permission. | |
48 | * | |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
59 | * SUCH DAMAGE. | |
60 | * | |
61 | * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95 | |
62 | */ | |
2d21ac55 A |
63 | /* |
64 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
65 | * support for mandatory and extensible security protections. This notice | |
66 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
67 | * Version 2.0. | |
68 | */ | |
1c79356b A |
69 | |
70 | #include <sys/param.h> | |
71 | #include <sys/malloc.h> | |
72 | ||
73 | #include <sys/socket.h> | |
74 | #include <sys/socketvar.h> | |
75 | ||
76 | #include <net/route.h> | |
fe8ab488 | 77 | #include <net/necp.h> |
1c79356b A |
78 | |
79 | #include <netinet/in.h> | |
80 | #include <netinet/in_systm.h> | |
81 | #include <netinet/ip.h> | |
82 | #include <netinet/in_pcb.h> | |
39236c6e | 83 | #include <netinet/flow_divert.h> |
1c79356b | 84 | |
55e303ae A |
85 | #include <sys/event.h> |
86 | #include <sys/eventvar.h> | |
87 | ||
91447636 A |
88 | #include <sys/proc_internal.h> |
89 | #include <sys/mount_internal.h> | |
90 | #include <sys/vnode_internal.h> | |
91 | #include <sys/ubc_internal.h> | |
1c79356b | 92 | #include <sys/namei.h> |
91447636 | 93 | #include <sys/file_internal.h> |
1c79356b A |
94 | #include <sys/filedesc.h> |
95 | #include <sys/tty.h> | |
9bccf70c | 96 | #include <sys/quota.h> |
91447636 | 97 | #include <sys/uio_internal.h> |
0c530ab8 | 98 | #include <sys/resourcevar.h> |
2d21ac55 | 99 | #include <sys/signalvar.h> |
39037602 | 100 | #include <sys/decmpfs.h> |
1c79356b | 101 | |
55e303ae | 102 | #include <miscfs/specfs/specdev.h> |
1c79356b A |
103 | |
104 | #include <nfs/rpcv2.h> | |
105 | #include <nfs/nfsproto.h> | |
106 | #include <nfs/nfsnode.h> | |
107 | #include <nfs/nfsmount.h> | |
1c79356b A |
108 | |
109 | #include <mach/mach_types.h> | |
110 | ||
111 | #include <kern/zalloc.h> | |
112 | #include <kern/kalloc.h> | |
113 | ||
39236c6e | 114 | void kmeminit(void); |
1c79356b | 115 | |
2d21ac55 | 116 | /* Strings corresponding to types of memory. |
0a7de745 | 117 | * Must be in synch with the #defines is sys/malloc.h |
2d21ac55 A |
118 | * NOTE - the reason we pass null strings in some cases is to reduce of foot |
119 | * print as much as possible for systems where a tiny kernel is needed. | |
39037602 | 120 | * todo - We should probably redesign this and use enums for our types and only |
2d21ac55 A |
121 | * include types needed for that configuration of the kernel. This can't be |
122 | * done without some kind of kpi since several types are hardwired and exported | |
39037602 | 123 | * (for example see types M_UDFMNT, M_TEMP, etc in sys/malloc.h) |
2d21ac55 A |
124 | */ |
125 | const char *memname[] = { | |
0a7de745 A |
126 | "free", /* 0 M_FREE */ |
127 | "mbuf", /* 1 M_MBUF */ | |
128 | "devbuf", /* 2 M_DEVBUF */ | |
129 | "socket", /* 3 M_SOCKET */ | |
130 | "pcb", /* 4 M_PCB */ | |
131 | "routetbl", /* 5 M_RTABLE */ | |
132 | "hosttbl", /* 6 M_HTABLE */ | |
133 | "fragtbl", /* 7 M_FTABLE */ | |
134 | "zombie", /* 8 M_ZOMBIE */ | |
135 | "ifaddr", /* 9 M_IFADDR */ | |
136 | "soopts", /* 10 M_SOOPTS */ | |
137 | "soname", /* 11 M_SONAME */ | |
138 | "namei", /* 12 M_NAMEI */ | |
139 | "gprof", /* 13 M_GPROF */ | |
140 | "ioctlops", /* 14 M_IOCTLOPS */ | |
141 | "mapmem", /* 15 M_MAPMEM */ | |
142 | "cred", /* 16 M_CRED */ | |
143 | "pgrp", /* 17 M_PGRP */ | |
144 | "session", /* 18 M_SESSION */ | |
145 | "iov32", /* 19 M_IOV32 */ | |
146 | "mount", /* 20 M_MOUNT */ | |
147 | "fhandle", /* 21 M_FHANDLE */ | |
2d21ac55 | 148 | #if (NFSCLIENT || NFSSERVER) |
0a7de745 A |
149 | "NFS req", /* 22 M_NFSREQ */ |
150 | "NFS mount", /* 23 M_NFSMNT */ | |
151 | "NFS node", /* 24 M_NFSNODE */ | |
2d21ac55 | 152 | #else |
0a7de745 A |
153 | "", /* 22 M_NFSREQ */ |
154 | "", /* 23 M_NFSMNT */ | |
155 | "", /* 24 M_NFSNODE */ | |
2d21ac55 | 156 | #endif |
0a7de745 A |
157 | "vnodes", /* 25 M_VNODE */ |
158 | "namecache", /* 26 M_CACHE */ | |
2d21ac55 | 159 | #if QUOTA |
0a7de745 | 160 | "UFS quota", /* 27 M_DQUOT */ |
2d21ac55 | 161 | #else |
0a7de745 | 162 | "", /* 27 M_DQUOT */ |
2d21ac55 | 163 | #endif |
0a7de745 | 164 | "proc uuid policy", /* 28 M_PROC_UUID_POLICY */ |
2d21ac55 | 165 | #if (SYSV_SEM || SYSV_MSG || SYSV_SHM) |
0a7de745 | 166 | "shm", /* 29 M_SHM */ |
2d21ac55 | 167 | #else |
0a7de745 | 168 | "", /* 29 M_SHM */ |
2d21ac55 | 169 | #endif |
0a7de745 A |
170 | "plimit", /* 30 M_VMMAP */ |
171 | "sigacts", /* 31 M_VMMAPENT */ | |
172 | "VM object", /* 32 M_VMOBJ */ | |
173 | "VM objhash", /* 33 M_VMOBJHASH */ | |
174 | "VM pmap", /* 34 M_VMPMAP */ | |
175 | "VM pvmap", /* 35 M_VMPVENT */ | |
176 | "VM pager", /* 36 M_VMPAGER */ | |
177 | "VM pgdata", /* 37 M_VMPGDATA */ | |
178 | "fileproc", /* 38 M_FILEPROC */ | |
179 | "file desc", /* 39 M_FILEDESC */ | |
180 | "lockf", /* 40 M_LOCKF */ | |
181 | "proc", /* 41 M_PROC */ | |
182 | "pstats", /* 42 M_SUBPROC */ | |
183 | "LFS segment", /* 43 M_SEGMENT */ | |
184 | "LFS node", /* 44 M_LFSNODE */ | |
185 | "", /* 45 M_FFSNODE */ | |
186 | "MFS node", /* 46 M_MFSNODE */ | |
187 | "NQNFS Lease", /* 47 M_NQLEASE */ | |
188 | "NQNFS Host", /* 48 M_NQMHOST */ | |
189 | "Export Host", /* 49 M_NETADDR */ | |
2d21ac55 | 190 | #if (NFSCLIENT || NFSSERVER) |
0a7de745 A |
191 | "NFS srvsock", /* 50 M_NFSSVC */ |
192 | "NFS uid", /* 51 M_NFSUID */ | |
193 | "NFS daemon", /* 52 M_NFSD */ | |
2d21ac55 | 194 | #else |
0a7de745 A |
195 | "", /* 50 M_NFSSVC */ |
196 | "", /* 51 M_NFSUID */ | |
197 | "", /* 52 M_NFSD */ | |
2d21ac55 | 198 | #endif |
0a7de745 A |
199 | "ip_moptions", /* 53 M_IPMOPTS */ |
200 | "in_multi", /* 54 M_IPMADDR */ | |
201 | "ether_multi", /* 55 M_IFMADDR */ | |
202 | "mrt", /* 56 M_MRTABLE */ | |
203 | "", /* 57 unused entry */ | |
204 | "", /* 58 unused entry */ | |
2d21ac55 | 205 | #if (NFSCLIENT || NFSSERVER) |
0a7de745 A |
206 | "NFSV3 srvdesc",/* 59 M_NFSRVDESC */ |
207 | "NFSV3 diroff", /* 60 M_NFSDIROFF */ | |
208 | "NFSV3 bigfh", /* 61 M_NFSBIGFH */ | |
2d21ac55 | 209 | #else |
0a7de745 A |
210 | "", /* 59 M_NFSRVDESC */ |
211 | "", /* 60 M_NFSDIROFF */ | |
212 | "", /* 61 M_NFSBIGFH */ | |
2d21ac55 | 213 | #endif |
0a7de745 A |
214 | "MSDOSFS mount",/* 62 M_MSDOSFSMNT */ |
215 | "MSDOSFS fat", /* 63 M_MSDOSFSFAT */ | |
216 | "MSDOSFS node", /* 64 M_MSDOSFSNODE */ | |
217 | "ttys", /* 65 M_TTYS */ | |
218 | "exec", /* 66 M_EXEC */ | |
219 | "miscfs mount", /* 67 M_MISCFSMNT */ | |
220 | "miscfs node", /* 68 M_MISCFSNODE */ | |
221 | "adosfs mount", /* 69 M_ADOSFSMNT */ | |
222 | "adosfs node", /* 70 M_ADOSFSNODE */ | |
223 | "adosfs anode", /* 71 M_ANODE */ | |
224 | "buf hdrs", /* 72 M_BUFHDR */ | |
225 | "ofile tabl", /* 73 M_OFILETABL */ | |
226 | "mbuf clust", /* 74 M_MCLUST */ | |
227 | "", /* 75 unused */ | |
228 | "", /* 76 unused */ | |
229 | "", /* 77 unused */ | |
230 | "", /* 78 unused */ | |
231 | "", /* 79 unused */ | |
232 | "temp", /* 80 M_TEMP */ | |
233 | "key mgmt", /* 81 M_SECA */ | |
234 | "DEVFS", /* 82 M_DEVFS */ | |
235 | "IpFw/IpAcct", /* 83 M_IPFW */ | |
236 | "UDF node", /* 84 M_UDFNODE */ | |
237 | "UDF mount", /* 85 M_UDFMNT */ | |
2d21ac55 | 238 | #if INET6 |
0a7de745 A |
239 | "IPv6 NDP", /* 86 M_IP6NDP */ |
240 | "IPv6 options", /* 87 M_IP6OPT */ | |
241 | "IPv6 Misc", /* 88 M_IP6MISC */ | |
2d21ac55 | 242 | #else |
0a7de745 A |
243 | "", /* 86 M_IP6NDP */ |
244 | "", /* 87 M_IP6OPT */ | |
245 | "", /* 88 M_IP6MISC */ | |
2d21ac55 A |
246 | #endif |
247 | "TCP Segment Q",/* 89 M_TSEGQ */ | |
0a7de745 A |
248 | "IGMP state", /* 90 M_IGMP */ |
249 | "", /* 91 unused */ | |
250 | "", /* 92 unused */ | |
251 | "specinfo", /* 93 M_SPECINFO */ | |
252 | "kqueue", /* 94 M_KQUEUE */ | |
253 | "", /* 95 unused */ | |
254 | "cluster_read", /* 96 M_CLRDAHEAD */ | |
255 | "cluster_write",/* 97 M_CLWRBEHIND */ | |
256 | "iov64", /* 98 M_IOV64 */ | |
257 | "fileglob", /* 99 M_FILEGLOB */ | |
258 | "kauth", /* 100 M_KAUTH */ | |
259 | "dummynet", /* 101 M_DUMMYNET */ | |
260 | "", /* 102 M_UNSAFEFS */ | |
2d21ac55 A |
261 | "macpipelabel", /* 103 M_MACPIPELABEL */ |
262 | "mactemp", /* 104 M_MACTEMP */ | |
263 | "sbuf", /* 105 M_SBUF */ | |
264 | "extattr", /* 106 M_EXTATTR */ | |
3e170ce0 | 265 | "select", /* 107 M_SELECT */ |
2d21ac55 A |
266 | #if TRAFFIC_MGT |
267 | "traffic_mgt", /* 108 M_TRAFFIC_MGT */ | |
268 | #else | |
269 | "", /* 108 M_TRAFFIC_MGT */ | |
270 | #endif | |
39037602 | 271 | #if FS_COMPRESSION |
b0d623f7 A |
272 | "decmpfs_cnode",/* 109 M_DECMPFS_CNODE */ |
273 | #else | |
274 | "", /* 109 M_DECMPFS_CNODE */ | |
39037602 | 275 | #endif /* FS_COMPRESSION */ |
0a7de745 A |
276 | "ipmfilter", /* 110 M_INMFILTER */ |
277 | "ipmsource", /* 111 M_IPMSOURCE */ | |
278 | "in6mfilter", /* 112 M_IN6MFILTER */ | |
279 | "ip6mopts", /* 113 M_IP6MOPTS */ | |
280 | "ip6msource", /* 114 M_IP6MSOURCE */ | |
39236c6e | 281 | #if FLOW_DIVERT |
0a7de745 A |
282 | "flow_divert_pcb", /* 115 M_FLOW_DIVERT_PCB */ |
283 | "flow_divert_group", /* 116 M_FLOW_DIVERT_GROUP */ | |
39236c6e | 284 | #else |
0a7de745 A |
285 | "", /* 115 M_FLOW_DIVERT_PCB */ |
286 | "", /* 116 M_FLOW_DIVERT_GROUP */ | |
39236c6e | 287 | #endif |
0a7de745 | 288 | "ip6cga", /* 117 M_IP6CGA */ |
fe8ab488 | 289 | #if NECP |
0a7de745 A |
290 | "necp", /* 118 M_NECP */ |
291 | "necp_session_policy", /* 119 M_NECP_SESSION_POLICY */ | |
292 | "necp_socket_policy", /* 120 M_NECP_SOCKET_POLICY */ | |
293 | "necp_ip_policy", /* 121 M_NECP_IP_POLICY */ | |
fe8ab488 | 294 | #else |
0a7de745 A |
295 | "", /* 118 M_NECP */ |
296 | "", /* 119 M_NECP_SESSION_POLICY */ | |
297 | "", /* 120 M_NECP_SOCKET_POLICY */ | |
298 | "", /* 121 M_NECP_IP_POLICY */ | |
fe8ab488 | 299 | #endif |
0a7de745 A |
300 | "fdvnodedata" /* 122 M_FD_VN_DATA */ |
301 | "fddirbuf", /* 123 M_FD_DIRBUF */ | |
302 | "netagent", /* 124 M_NETAGENT */ | |
5ba3f43e | 303 | "Event Handler",/* 125 M_EVENTHANDLER */ |
0a7de745 A |
304 | "Link Layer Table", /* 126 M_LLTABLE */ |
305 | "Network Work Queue", /* 127 M_NWKWQ */ | |
d9a64523 | 306 | "Content Filter", /* 128 M_CFIL */ |
0a7de745 | 307 | "" |
2d21ac55 A |
308 | }; |
309 | ||
310 | /* for use with kmzones.kz_zalloczone */ | |
0a7de745 A |
311 | #define KMZ_CREATEZONE_ACCT ((void *)-3) |
312 | #define KMZ_CREATEZONE ((void *)-2) | |
313 | #define KMZ_LOOKUPZONE ((void *)-1) | |
314 | #define KMZ_MALLOC ((void *)0) | |
315 | #define KMZ_SHAREZONE ((void *)1) | |
2d21ac55 A |
316 | |
317 | struct kmzones { | |
0a7de745 A |
318 | size_t kz_elemsize; |
319 | void *kz_zalloczone; | |
320 | boolean_t kz_noencrypt; | |
1c79356b | 321 | } kmzones[M_LAST] = { |
0a7de745 A |
322 | #define SOS(sname) sizeof (struct sname) |
323 | #define SOX(sname) -1 | |
324 | { -1, 0, FALSE }, /* 0 M_FREE */ | |
325 | { MSIZE, KMZ_CREATEZONE, FALSE }, /* 1 M_MBUF */ | |
326 | { 0, KMZ_MALLOC, FALSE }, /* 2 M_DEVBUF */ | |
327 | { SOS(socket), KMZ_CREATEZONE, TRUE }, /* 3 M_SOCKET */ | |
328 | { SOS(inpcb), KMZ_LOOKUPZONE, TRUE }, /* 4 M_PCB */ | |
329 | { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 5 M_RTABLE */ | |
330 | { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 6 M_HTABLE */ | |
331 | { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 7 M_FTABLE */ | |
332 | { SOS(rusage), KMZ_CREATEZONE, TRUE }, /* 8 M_ZOMBIE */ | |
333 | { 0, KMZ_MALLOC, FALSE }, /* 9 M_IFADDR */ | |
334 | { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 10 M_SOOPTS */ | |
335 | { 0, KMZ_MALLOC, FALSE }, /* 11 M_SONAME */ | |
336 | { MAXPATHLEN, KMZ_CREATEZONE, FALSE }, /* 12 M_NAMEI */ | |
337 | { 0, KMZ_MALLOC, FALSE }, /* 13 M_GPROF */ | |
338 | { 0, KMZ_MALLOC, FALSE }, /* 14 M_IOCTLOPS */ | |
339 | { 0, KMZ_MALLOC, FALSE }, /* 15 M_MAPMEM */ | |
340 | { SOS(ucred), KMZ_CREATEZONE, FALSE }, /* 16 M_CRED */ | |
341 | { SOS(pgrp), KMZ_CREATEZONE, FALSE }, /* 17 M_PGRP */ | |
342 | { SOS(session), KMZ_CREATEZONE, FALSE }, /* 18 M_SESSION */ | |
343 | { SOS(user32_iovec), KMZ_LOOKUPZONE, FALSE }, /* 19 M_IOV32 */ | |
344 | { SOS(mount), KMZ_CREATEZONE, FALSE }, /* 20 M_MOUNT */ | |
345 | { 0, KMZ_MALLOC, FALSE }, /* 21 M_FHANDLE */ | |
2d21ac55 | 346 | #if (NFSCLIENT || NFSSERVER) |
0a7de745 A |
347 | { SOS(nfsreq), KMZ_CREATEZONE, FALSE }, /* 22 M_NFSREQ */ |
348 | { SOS(nfsmount), KMZ_CREATEZONE, FALSE }, /* 23 M_NFSMNT */ | |
349 | { SOS(nfsnode), KMZ_CREATEZONE, FALSE }, /* 24 M_NFSNODE */ | |
2d21ac55 | 350 | #else |
0a7de745 A |
351 | { 0, KMZ_MALLOC, FALSE }, /* 22 M_NFSREQ */ |
352 | { 0, KMZ_MALLOC, FALSE }, /* 23 M_NFSMNT */ | |
353 | { 0, KMZ_MALLOC, FALSE }, /* 24 M_NFSNODE */ | |
2d21ac55 | 354 | #endif |
0a7de745 A |
355 | { SOS(vnode), KMZ_CREATEZONE, TRUE }, /* 25 M_VNODE */ |
356 | { SOS(namecache), KMZ_CREATEZONE, FALSE }, /* 26 M_CACHE */ | |
2d21ac55 | 357 | #if QUOTA |
0a7de745 | 358 | { SOX(dquot), KMZ_LOOKUPZONE, FALSE }, /* 27 M_DQUOT */ |
2d21ac55 | 359 | #else |
0a7de745 | 360 | { 0, KMZ_MALLOC, FALSE }, /* 27 M_DQUOT */ |
2d21ac55 | 361 | #endif |
0a7de745 A |
362 | { 0, KMZ_MALLOC, FALSE }, /* 28 M_PROC_UUID_POLICY */ |
363 | { 0, KMZ_MALLOC, FALSE }, /* 29 M_SHM */ | |
364 | { SOS(plimit), KMZ_CREATEZONE, TRUE }, /* 30 M_PLIMIT */ | |
365 | { SOS(sigacts), KMZ_CREATEZONE_ACCT, TRUE }, /* 31 M_SIGACTS */ | |
366 | { 0, KMZ_MALLOC, FALSE }, /* 32 M_VMOBJ */ | |
367 | { 0, KMZ_MALLOC, FALSE }, /* 33 M_VMOBJHASH */ | |
368 | { 0, KMZ_MALLOC, FALSE }, /* 34 M_VMPMAP */ | |
369 | { 0, KMZ_MALLOC, FALSE }, /* 35 M_VMPVENT */ | |
370 | { 0, KMZ_MALLOC, FALSE }, /* 36 M_VMPAGER */ | |
371 | { 0, KMZ_MALLOC, FALSE }, /* 37 M_VMPGDATA */ | |
372 | { SOS(fileproc), KMZ_CREATEZONE_ACCT, TRUE }, /* 38 M_FILEPROC */ | |
373 | { SOS(filedesc), KMZ_CREATEZONE_ACCT, TRUE }, /* 39 M_FILEDESC */ | |
374 | { SOX(lockf), KMZ_CREATEZONE_ACCT, TRUE }, /* 40 M_LOCKF */ | |
375 | { SOS(proc), KMZ_CREATEZONE, FALSE }, /* 41 M_PROC */ | |
376 | { SOS(pstats), KMZ_CREATEZONE, TRUE }, /* 42 M_PSTATS */ | |
377 | { 0, KMZ_MALLOC, FALSE }, /* 43 M_SEGMENT */ | |
378 | { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 44 M_LFSNODE */ | |
379 | { 0, KMZ_MALLOC, FALSE }, /* 45 M_FFSNODE */ | |
380 | { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 46 M_MFSNODE */ | |
381 | { 0, KMZ_MALLOC, FALSE }, /* 47 M_NQLEASE */ | |
382 | { 0, KMZ_MALLOC, FALSE }, /* 48 M_NQMHOST */ | |
383 | { 0, KMZ_MALLOC, FALSE }, /* 49 M_NETADDR */ | |
2d21ac55 A |
384 | #if (NFSCLIENT || NFSSERVER) |
385 | { SOX(nfsrv_sock), | |
0a7de745 A |
386 | KMZ_CREATEZONE_ACCT, FALSE }, /* 50 M_NFSSVC */ |
387 | { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */ | |
2d21ac55 | 388 | { SOX(nfsrvcache), |
0a7de745 | 389 | KMZ_CREATEZONE_ACCT, FALSE }, /* 52 M_NFSD */ |
2d21ac55 | 390 | #else |
0a7de745 A |
391 | { 0, KMZ_MALLOC, FALSE }, /* 50 M_NFSSVC */ |
392 | { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */ | |
393 | { 0, KMZ_MALLOC, FALSE }, /* 52 M_NFSD */ | |
2d21ac55 A |
394 | #endif |
395 | { SOX(ip_moptions), | |
0a7de745 A |
396 | KMZ_LOOKUPZONE, FALSE }, /* 53 M_IPMOPTS */ |
397 | { SOX(in_multi), KMZ_LOOKUPZONE, FALSE }, /* 54 M_IPMADDR */ | |
2d21ac55 | 398 | { SOX(ether_multi), |
0a7de745 A |
399 | KMZ_LOOKUPZONE, FALSE }, /* 55 M_IFMADDR */ |
400 | { SOX(mrt), KMZ_CREATEZONE, TRUE }, /* 56 M_MRTABLE */ | |
401 | { 0, KMZ_MALLOC, FALSE }, /* 57 unused entry */ | |
402 | { 0, KMZ_MALLOC, FALSE }, /* 58 unused entry */ | |
2d21ac55 A |
403 | #if (NFSCLIENT || NFSSERVER) |
404 | { SOS(nfsrv_descript), | |
0a7de745 A |
405 | KMZ_CREATEZONE_ACCT, FALSE }, /* 59 M_NFSRVDESC */ |
406 | { SOS(nfsdmap), KMZ_CREATEZONE, FALSE }, /* 60 M_NFSDIROFF */ | |
407 | { SOS(fhandle), KMZ_LOOKUPZONE, FALSE }, /* 61 M_NFSBIGFH */ | |
2d21ac55 | 408 | #else |
0a7de745 A |
409 | { 0, KMZ_MALLOC, FALSE }, /* 59 M_NFSRVDESC */ |
410 | { 0, KMZ_MALLOC, FALSE }, /* 60 M_NFSDIROFF */ | |
411 | { 0, KMZ_MALLOC, FALSE }, /* 61 M_NFSBIGFH */ | |
2d21ac55 | 412 | #endif |
0a7de745 A |
413 | { 0, KMZ_MALLOC, FALSE }, /* 62 M_MSDOSFSMNT */ |
414 | { 0, KMZ_MALLOC, FALSE }, /* 63 M_MSDOSFSFAT */ | |
415 | { 0, KMZ_MALLOC, FALSE }, /* 64 M_MSDOSFSNODE */ | |
416 | { SOS(tty), KMZ_CREATEZONE, FALSE }, /* 65 M_TTYS */ | |
417 | { 0, KMZ_MALLOC, FALSE }, /* 66 M_EXEC */ | |
418 | { 0, KMZ_MALLOC, FALSE }, /* 67 M_MISCFSMNT */ | |
419 | { 0, KMZ_MALLOC, FALSE }, /* 68 M_MISCFSNODE */ | |
420 | { 0, KMZ_MALLOC, FALSE }, /* 69 M_ADOSFSMNT */ | |
421 | { 0, KMZ_MALLOC, FALSE }, /* 70 M_ADOSFSNODE */ | |
422 | { 0, KMZ_MALLOC, FALSE }, /* 71 M_ANODE */ | |
423 | { 0, KMZ_MALLOC, TRUE }, /* 72 M_BUFHDR */ | |
2d21ac55 | 424 | { (NDFILE * OFILESIZE), |
0a7de745 A |
425 | KMZ_CREATEZONE_ACCT, FALSE }, /* 73 M_OFILETABL */ |
426 | { MCLBYTES, KMZ_CREATEZONE, FALSE }, /* 74 M_MCLUST */ | |
427 | { 0, KMZ_MALLOC, FALSE }, /* 75 unused */ | |
428 | { 0, KMZ_MALLOC, FALSE }, /* 76 unused */ | |
429 | { 0, KMZ_MALLOC, FALSE }, /* 77 unused */ | |
430 | { 0, KMZ_MALLOC, FALSE }, /* 78 unused */ | |
431 | { 0, KMZ_MALLOC, FALSE }, /* 79 unused */ | |
432 | { 0, KMZ_MALLOC, FALSE }, /* 80 M_TEMP */ | |
433 | { 0, KMZ_MALLOC, FALSE }, /* 81 M_SECA */ | |
434 | { 0, KMZ_MALLOC, FALSE }, /* 82 M_DEVFS */ | |
435 | { 0, KMZ_MALLOC, FALSE }, /* 83 M_IPFW */ | |
436 | { 0, KMZ_MALLOC, FALSE }, /* 84 M_UDFNODE */ | |
437 | { 0, KMZ_MALLOC, FALSE }, /* 85 M_UDFMOUNT */ | |
438 | { 0, KMZ_MALLOC, FALSE }, /* 86 M_IP6NDP */ | |
439 | { 0, KMZ_MALLOC, FALSE }, /* 87 M_IP6OPT */ | |
440 | { 0, KMZ_MALLOC, FALSE }, /* 88 M_IP6MISC */ | |
441 | { 0, KMZ_MALLOC, FALSE }, /* 89 M_TSEGQ */ | |
442 | { 0, KMZ_MALLOC, FALSE }, /* 90 M_IGMP */ | |
443 | { 0, KMZ_MALLOC, FALSE }, /* 91 unused */ | |
444 | { 0, KMZ_MALLOC, FALSE }, /* 92 unused */ | |
445 | { SOS(specinfo), KMZ_CREATEZONE, TRUE }, /* 93 M_SPECINFO */ | |
446 | { SOS(kqueue), KMZ_CREATEZONE, FALSE }, /* 94 M_KQUEUE */ | |
447 | { 0, KMZ_MALLOC, FALSE }, /* 95 unused */ | |
448 | { SOS(cl_readahead), KMZ_CREATEZONE, TRUE }, /* 96 M_CLRDAHEAD */ | |
449 | { SOS(cl_writebehind), KMZ_CREATEZONE, TRUE }, /* 97 M_CLWRBEHIND */ | |
450 | { SOS(user64_iovec), KMZ_LOOKUPZONE, FALSE }, /* 98 M_IOV64 */ | |
451 | { SOS(fileglob), KMZ_CREATEZONE, TRUE }, /* 99 M_FILEGLOB */ | |
452 | { 0, KMZ_MALLOC, FALSE }, /* 100 M_KAUTH */ | |
453 | { 0, KMZ_MALLOC, FALSE }, /* 101 M_DUMMYNET */ | |
454 | { 0, KMZ_MALLOC, FALSE }, /* 102 M_UNSAFEFS */ | |
455 | { 0, KMZ_MALLOC, FALSE }, /* 103 M_MACPIPELABEL */ | |
456 | { 0, KMZ_MALLOC, FALSE }, /* 104 M_MACTEMP */ | |
457 | { 0, KMZ_MALLOC, FALSE }, /* 105 M_SBUF */ | |
458 | { 0, KMZ_MALLOC, FALSE }, /* 106 M_HFS_EXTATTR */ | |
459 | { 0, KMZ_MALLOC, FALSE }, /* 107 M_SELECT */ | |
460 | { 0, KMZ_MALLOC, FALSE }, /* 108 M_TRAFFIC_MGT */ | |
39037602 | 461 | #if FS_COMPRESSION |
0a7de745 | 462 | { SOS(decmpfs_cnode), KMZ_CREATEZONE, FALSE}, /* 109 M_DECMPFS_CNODE */ |
b0d623f7 | 463 | #else |
0a7de745 | 464 | { 0, KMZ_MALLOC, FALSE }, /* 109 M_DECMPFS_CNODE */ |
39037602 | 465 | #endif /* FS_COMPRESSION */ |
0a7de745 A |
466 | { 0, KMZ_MALLOC, FALSE }, /* 110 M_INMFILTER */ |
467 | { 0, KMZ_MALLOC, FALSE }, /* 111 M_IPMSOURCE */ | |
468 | { 0, KMZ_MALLOC, FALSE }, /* 112 M_IN6MFILTER */ | |
469 | { 0, KMZ_MALLOC, FALSE }, /* 113 M_IP6MOPTS */ | |
470 | { 0, KMZ_MALLOC, FALSE }, /* 114 M_IP6MSOURCE */ | |
39236c6e | 471 | #if FLOW_DIVERT |
0a7de745 A |
472 | { SOS(flow_divert_pcb), KMZ_CREATEZONE, TRUE }, /* 115 M_FLOW_DIVERT_PCB */ |
473 | { SOS(flow_divert_group), KMZ_CREATEZONE, TRUE }, /* 116 M_FLOW_DIVERT_GROUP */ | |
39236c6e | 474 | #else |
0a7de745 A |
475 | { 0, KMZ_MALLOC, FALSE }, /* 115 M_FLOW_DIVERT_PCB */ |
476 | { 0, KMZ_MALLOC, FALSE }, /* 116 M_FLOW_DIVERT_GROUP */ | |
477 | #endif /* FLOW_DIVERT */ | |
478 | { 0, KMZ_MALLOC, FALSE }, /* 117 M_IP6CGA */ | |
479 | { 0, KMZ_MALLOC, FALSE }, /* 118 M_NECP */ | |
fe8ab488 | 480 | #if NECP |
0a7de745 A |
481 | { SOS(necp_session_policy), KMZ_CREATEZONE, TRUE }, /* 119 M_NECP_SESSION_POLICY */ |
482 | { SOS(necp_kernel_socket_policy), KMZ_CREATEZONE, TRUE }, /* 120 M_NECP_SOCKET_POLICY */ | |
483 | { SOS(necp_kernel_ip_output_policy), KMZ_CREATEZONE, TRUE }, /* 121 M_NECP_IP_POLICY */ | |
fe8ab488 | 484 | #else |
0a7de745 A |
485 | { 0, KMZ_MALLOC, FALSE }, /* 119 M_NECP_SESSION_POLICY */ |
486 | { 0, KMZ_MALLOC, FALSE }, /* 120 M_NECP_SOCKET_POLICY */ | |
487 | { 0, KMZ_MALLOC, FALSE }, /* 121 M_NECP_IP_POLICY */ | |
fe8ab488 | 488 | #endif /* NECP */ |
0a7de745 A |
489 | { 0, KMZ_MALLOC, FALSE }, /* 122 M_FD_VN_DATA */ |
490 | { 0, KMZ_MALLOC, FALSE }, /* 123 M_FD_DIRBUF */ | |
491 | { 0, KMZ_MALLOC, FALSE }, /* 124 M_NETAGENT */ | |
492 | { 0, KMZ_MALLOC, FALSE }, /* 125 M_EVENTHANDLER */ | |
493 | { 0, KMZ_MALLOC, FALSE }, /* 126 M_LLTABLE */ | |
494 | { 0, KMZ_MALLOC, FALSE }, /* 127 M_NWKWQ */ | |
495 | { 0, KMZ_MALLOC, FALSE }, /* 128 M_CFIL */ | |
496 | #undef SOS | |
497 | #undef SOX | |
1c79356b A |
498 | }; |
499 | ||
0a7de745 | 500 | extern zone_t kalloc_zone(vm_size_t); /* XXX */ |
1c79356b A |
501 | |
502 | /* | |
503 | * Initialize the kernel memory allocator | |
504 | */ | |
505 | void | |
506 | kmeminit(void) | |
507 | { | |
0a7de745 | 508 | struct kmzones *kmz; |
1c79356b | 509 | |
0a7de745 | 510 | if ((sizeof(kmzones) / sizeof(kmzones[0])) != (sizeof(memname) / sizeof(memname[0]))) { |
2d21ac55 | 511 | panic("kmeminit: kmzones has %lu elements but memname has %lu\n", |
0a7de745 | 512 | (sizeof(kmzones) / sizeof(kmzones[0])), (sizeof(memname) / sizeof(memname[0]))); |
d7e50217 A |
513 | } |
514 | ||
1c79356b A |
515 | kmz = kmzones; |
516 | while (kmz < &kmzones[M_LAST]) { | |
517 | /* XXX */ | |
0a7de745 | 518 | if (kmz->kz_elemsize == (size_t)(-1)) { |
1c79356b | 519 | ; |
0a7de745 | 520 | } else |
1c79356b | 521 | /* XXX */ |
6d2010ae A |
522 | if (kmz->kz_zalloczone == KMZ_CREATEZONE || |
523 | kmz->kz_zalloczone == KMZ_CREATEZONE_ACCT) { | |
1c79356b | 524 | kmz->kz_zalloczone = zinit(kmz->kz_elemsize, |
0a7de745 A |
525 | 1024 * 1024, PAGE_SIZE, |
526 | memname[kmz - kmzones]); | |
6d2010ae | 527 | zone_change(kmz->kz_zalloczone, Z_CALLERACCT, |
0a7de745 | 528 | (kmz->kz_zalloczone == KMZ_CREATEZONE_ACCT)); |
6d2010ae | 529 | |
0a7de745 | 530 | if (kmz->kz_noencrypt == TRUE) { |
0b4c1975 | 531 | zone_change(kmz->kz_zalloczone, Z_NOENCRYPT, TRUE); |
0a7de745 A |
532 | } |
533 | } else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE) { | |
1c79356b | 534 | kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize); |
0a7de745 | 535 | } |
1c79356b A |
536 | |
537 | kmz++; | |
538 | } | |
539 | ||
540 | kmz = kmzones; | |
541 | while (kmz < &kmzones[M_LAST]) { | |
542 | /* XXX */ | |
0a7de745 | 543 | if (kmz->kz_elemsize == (size_t)(-1)) { |
1c79356b | 544 | ; |
0a7de745 | 545 | } else |
1c79356b A |
546 | /* XXX */ |
547 | if (kmz->kz_zalloczone == KMZ_SHAREZONE) { | |
548 | kmz->kz_zalloczone = | |
0a7de745 | 549 | kmzones[kmz->kz_elemsize].kz_zalloczone; |
1c79356b | 550 | kmz->kz_elemsize = |
0a7de745 | 551 | kmzones[kmz->kz_elemsize].kz_elemsize; |
1c79356b A |
552 | } |
553 | ||
554 | kmz++; | |
555 | } | |
556 | } | |
557 | ||
91447636 | 558 | void * |
3e170ce0 | 559 | _MALLOC_external( |
0a7de745 A |
560 | size_t size, |
561 | int type, | |
562 | int flags); | |
3e170ce0 A |
563 | void * |
564 | _MALLOC_external( | |
0a7de745 A |
565 | size_t size, |
566 | int type, | |
567 | int flags) | |
3e170ce0 | 568 | { |
0a7de745 A |
569 | static vm_allocation_site_t site = { .tag = VM_KERN_MEMORY_KALLOC, .flags = VM_TAG_BT }; |
570 | return __MALLOC(size, type, flags, &site); | |
3e170ce0 A |
571 | } |
572 | ||
573 | void * | |
574 | __MALLOC( | |
0a7de745 A |
575 | size_t size, |
576 | int type, | |
577 | int flags, | |
3e170ce0 | 578 | vm_allocation_site_t *site) |
1c79356b | 579 | { |
0a7de745 A |
580 | void *addr = NULL; |
581 | vm_size_t msize = size; | |
1c79356b | 582 | |
0a7de745 | 583 | if (type >= M_LAST) { |
1c79356b | 584 | panic("_malloc TYPE"); |
0a7de745 | 585 | } |
1c79356b | 586 | |
0a7de745 A |
587 | if (size == 0) { |
588 | return NULL; | |
589 | } | |
1c79356b | 590 | |
39037602 A |
591 | if (msize != size) { |
592 | panic("Requested size to __MALLOC is too large (%llx)!\n", (uint64_t)size); | |
593 | } | |
594 | ||
1c79356b | 595 | if (flags & M_NOWAIT) { |
39037602 | 596 | addr = (void *)kalloc_canblock(&msize, FALSE, site); |
1c79356b | 597 | } else { |
39037602 A |
598 | addr = (void *)kalloc_canblock(&msize, TRUE, site); |
599 | if (addr == NULL) { | |
b0d623f7 A |
600 | /* |
601 | * We get here when the caller told us to block waiting for memory, but | |
0a7de745 | 602 | * kalloc said there's no memory left to get. Generally, this means there's a |
813fb2f6 | 603 | * leak or the caller asked for an impossibly large amount of memory. If the caller |
0a7de745 A |
604 | * is expecting a NULL return code then it should explicitly set the flag M_NULL. |
605 | * If the caller isn't expecting a NULL return code, we just panic. This is less | |
606 | * than ideal, but returning NULL when the caller isn't expecting it doesn't help | |
607 | * since the majority of callers don't check the return value and will just | |
608 | * dereference the pointer and trap anyway. We may as well get a more | |
813fb2f6 | 609 | * descriptive message out while we can. |
b0d623f7 | 610 | */ |
813fb2f6 A |
611 | if (flags & M_NULL) { |
612 | return NULL; | |
613 | } | |
b0d623f7 A |
614 | panic("_MALLOC: kalloc returned NULL (potential leak), size %llu", (uint64_t) size); |
615 | } | |
1c79356b | 616 | } |
0a7de745 A |
617 | if (!addr) { |
618 | return 0; | |
619 | } | |
1c79356b | 620 | |
0a7de745 | 621 | if (flags & M_ZERO) { |
39037602 | 622 | bzero(addr, size); |
0a7de745 | 623 | } |
55e303ae | 624 | |
0a7de745 | 625 | return addr; |
1c79356b A |
626 | } |
627 | ||
91447636 A |
628 | void |
629 | _FREE( | |
0a7de745 A |
630 | void *addr, |
631 | int type) | |
1c79356b | 632 | { |
0a7de745 | 633 | if (type >= M_LAST) { |
1c79356b | 634 | panic("_free TYPE"); |
0a7de745 | 635 | } |
1c79356b | 636 | |
0a7de745 | 637 | if (!addr) { |
9bccf70c | 638 | return; /* correct (convenient bsd kernel legacy) */ |
0a7de745 | 639 | } |
39037602 | 640 | kfree_addr(addr); |
1c79356b A |
641 | } |
642 | ||
6d2010ae | 643 | void * |
3e170ce0 | 644 | __REALLOC( |
0a7de745 A |
645 | void *addr, |
646 | size_t size, | |
647 | int type, | |
648 | int flags, | |
3e170ce0 | 649 | vm_allocation_site_t *site) |
6d2010ae | 650 | { |
0a7de745 A |
651 | void *newaddr; |
652 | size_t alloc; | |
6d2010ae A |
653 | |
654 | /* realloc(NULL, ...) is equivalent to malloc(...) */ | |
0a7de745 A |
655 | if (addr == NULL) { |
656 | return __MALLOC(size, type, flags, site); | |
657 | } | |
6d2010ae | 658 | |
39037602 | 659 | alloc = kalloc_size(addr); |
0a7de745 A |
660 | /* |
661 | * Find out the size of the bucket in which the new sized allocation | |
662 | * would land. If it matches the bucket of the original allocation, | |
39037602 A |
663 | * simply return the address. |
664 | */ | |
813fb2f6 | 665 | if (kalloc_bucket_size(size) == alloc) { |
0a7de745 A |
666 | if (flags & M_ZERO) { |
667 | if (alloc < size) { | |
5ba3f43e | 668 | bzero(addr + alloc, (size - alloc)); |
0a7de745 | 669 | } else { |
5ba3f43e | 670 | bzero(addr + size, (alloc - size)); |
0a7de745 | 671 | } |
813fb2f6 | 672 | } |
39037602 | 673 | return addr; |
813fb2f6 | 674 | } |
39037602 | 675 | |
6d2010ae | 676 | /* Allocate a new, bigger (or smaller) block */ |
0a7de745 A |
677 | if ((newaddr = __MALLOC(size, type, flags, site)) == NULL) { |
678 | return NULL; | |
679 | } | |
6d2010ae | 680 | |
6d2010ae A |
681 | /* Copy over original contents */ |
682 | bcopy(addr, newaddr, MIN(size, alloc)); | |
683 | _FREE(addr, type); | |
684 | ||
0a7de745 | 685 | return newaddr; |
6d2010ae A |
686 | } |
687 | ||
91447636 | 688 | void * |
3e170ce0 | 689 | _MALLOC_ZONE_external( |
0a7de745 A |
690 | size_t size, |
691 | int type, | |
692 | int flags); | |
3e170ce0 A |
693 | void * |
694 | _MALLOC_ZONE_external( | |
0a7de745 A |
695 | size_t size, |
696 | int type, | |
697 | int flags) | |
3e170ce0 | 698 | { |
0a7de745 | 699 | return __MALLOC_ZONE(size, type, flags, NULL); |
3e170ce0 A |
700 | } |
701 | ||
702 | void * | |
703 | __MALLOC_ZONE( | |
0a7de745 A |
704 | size_t size, |
705 | int type, | |
706 | int flags, | |
3e170ce0 | 707 | vm_allocation_site_t *site) |
1c79356b | 708 | { |
0a7de745 A |
709 | struct kmzones *kmz; |
710 | void *elem; | |
1c79356b | 711 | |
0a7de745 | 712 | if (type >= M_LAST) { |
1c79356b | 713 | panic("_malloc_zone TYPE"); |
0a7de745 | 714 | } |
1c79356b A |
715 | |
716 | kmz = &kmzones[type]; | |
0a7de745 | 717 | if (kmz->kz_zalloczone == KMZ_MALLOC) { |
91447636 | 718 | panic("_malloc_zone ZONE: type = %d", type); |
0a7de745 | 719 | } |
1c79356b A |
720 | |
721 | /* XXX */ | |
0a7de745 | 722 | if (kmz->kz_elemsize == (size_t)(-1)) { |
1c79356b | 723 | panic("_malloc_zone XXX"); |
0a7de745 | 724 | } |
1c79356b | 725 | /* XXX */ |
0a7de745 | 726 | if (size == kmz->kz_elemsize) { |
1c79356b | 727 | if (flags & M_NOWAIT) { |
0a7de745 | 728 | elem = (void *)zalloc_noblock(kmz->kz_zalloczone); |
1c79356b | 729 | } else { |
0a7de745 | 730 | elem = (void *)zalloc(kmz->kz_zalloczone); |
1c79356b | 731 | } |
0a7de745 | 732 | } else { |
39037602 A |
733 | vm_size_t kalloc_size = size; |
734 | if (size > kalloc_size) { | |
735 | elem = NULL; | |
736 | } else if (flags & M_NOWAIT) { | |
737 | elem = (void *)kalloc_canblock(&kalloc_size, FALSE, site); | |
1c79356b | 738 | } else { |
39037602 | 739 | elem = (void *)kalloc_canblock(&kalloc_size, TRUE, site); |
1c79356b | 740 | } |
39037602 | 741 | } |
1c79356b | 742 | |
0a7de745 | 743 | if (elem && (flags & M_ZERO)) { |
4d15aeb1 | 744 | bzero(elem, size); |
0a7de745 | 745 | } |
4d15aeb1 | 746 | |
0a7de745 | 747 | return elem; |
1c79356b A |
748 | } |
749 | ||
91447636 A |
750 | void |
751 | _FREE_ZONE( | |
0a7de745 A |
752 | void *elem, |
753 | size_t size, | |
754 | int type) | |
1c79356b | 755 | { |
0a7de745 | 756 | struct kmzones *kmz; |
1c79356b | 757 | |
0a7de745 | 758 | if (type >= M_LAST) { |
1c79356b | 759 | panic("FREE_SIZE"); |
0a7de745 | 760 | } |
1c79356b A |
761 | |
762 | kmz = &kmzones[type]; | |
0a7de745 | 763 | if (kmz->kz_zalloczone == KMZ_MALLOC) { |
1c79356b | 764 | panic("free_zone ZONE"); |
0a7de745 | 765 | } |
1c79356b A |
766 | |
767 | /* XXX */ | |
0a7de745 | 768 | if (kmz->kz_elemsize == (size_t)(-1)) { |
1c79356b | 769 | panic("FREE_SIZE XXX"); |
0a7de745 | 770 | } |
1c79356b | 771 | /* XXX */ |
0a7de745 | 772 | if (size == kmz->kz_elemsize) { |
91447636 | 773 | zfree(kmz->kz_zalloczone, elem); |
0a7de745 | 774 | } else { |
91447636 | 775 | kfree(elem, size); |
0a7de745 | 776 | } |
1c79356b | 777 | } |
6d2010ae | 778 | |
5ba3f43e A |
779 | #if DEBUG || DEVELOPMENT |
780 | ||
781 | extern unsigned int zone_map_jetsam_limit; | |
782 | ||
783 | static int | |
784 | sysctl_zone_map_jetsam_limit SYSCTL_HANDLER_ARGS | |
785 | { | |
786 | #pragma unused(oidp, arg1, arg2) | |
787 | int oldval = 0, val = 0, error = 0; | |
788 | ||
789 | oldval = zone_map_jetsam_limit; | |
790 | error = sysctl_io_number(req, oldval, sizeof(int), &val, NULL); | |
791 | if (error || !req->newptr) { | |
0a7de745 | 792 | return error; |
5ba3f43e A |
793 | } |
794 | ||
795 | if (val <= 0 || val > 100) { | |
796 | printf("sysctl_zone_map_jetsam_limit: new jetsam limit value is invalid.\n"); | |
797 | return EINVAL; | |
798 | } | |
799 | ||
800 | zone_map_jetsam_limit = val; | |
0a7de745 | 801 | return 0; |
5ba3f43e A |
802 | } |
803 | ||
0a7de745 A |
804 | SYSCTL_PROC(_kern, OID_AUTO, zone_map_jetsam_limit, CTLTYPE_INT | CTLFLAG_RW, 0, 0, |
805 | sysctl_zone_map_jetsam_limit, "I", "Zone map jetsam limit"); | |
5ba3f43e | 806 | |
a39ff7e2 A |
807 | |
808 | extern void get_zone_map_size(uint64_t *current_size, uint64_t *capacity); | |
809 | ||
810 | static int | |
811 | sysctl_zone_map_size_and_capacity SYSCTL_HANDLER_ARGS | |
812 | { | |
813 | #pragma unused(oidp, arg1, arg2) | |
814 | uint64_t zstats[2]; | |
815 | get_zone_map_size(&zstats[0], &zstats[1]); | |
816 | ||
817 | return SYSCTL_OUT(req, &zstats, sizeof(zstats)); | |
818 | } | |
819 | ||
820 | SYSCTL_PROC(_kern, OID_AUTO, zone_map_size_and_capacity, | |
0a7de745 A |
821 | CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED, |
822 | 0, 0, &sysctl_zone_map_size_and_capacity, "Q", "Current size and capacity of the zone map"); | |
a39ff7e2 A |
823 | |
824 | ||
5ba3f43e A |
825 | extern boolean_t run_zone_test(void); |
826 | ||
827 | static int | |
828 | sysctl_run_zone_test SYSCTL_HANDLER_ARGS | |
829 | { | |
830 | #pragma unused(oidp, arg1, arg2) | |
a39ff7e2 A |
831 | /* require setting this sysctl to prevent sysctl -a from running this */ |
832 | if (!req->newptr) { | |
833 | return 0; | |
834 | } | |
5ba3f43e | 835 | |
a39ff7e2 | 836 | int ret_val = run_zone_test(); |
5ba3f43e A |
837 | return SYSCTL_OUT(req, &ret_val, sizeof(ret_val)); |
838 | } | |
839 | ||
840 | SYSCTL_PROC(_kern, OID_AUTO, run_zone_test, | |
0a7de745 A |
841 | CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_MASKED | CTLFLAG_LOCKED, |
842 | 0, 0, &sysctl_run_zone_test, "I", "Test zone allocator KPI"); | |
5ba3f43e A |
843 | |
844 | #endif /* DEBUG || DEVELOPMENT */ | |
845 | ||
6d2010ae A |
846 | #if CONFIG_ZLEAKS |
847 | ||
848 | SYSCTL_DECL(_kern_zleak); | |
849 | SYSCTL_NODE(_kern, OID_AUTO, zleak, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "zleak"); | |
850 | ||
851 | /* | |
852 | * kern.zleak.active | |
853 | * | |
854 | * Show the status of the zleak subsystem (0 = enabled, 1 = active, | |
855 | * and -1 = failed), and if enabled, allow it to be activated immediately. | |
856 | */ | |
857 | static int | |
858 | sysctl_zleak_active SYSCTL_HANDLER_ARGS | |
859 | { | |
860 | #pragma unused(arg1, arg2) | |
861 | int oldval, val, error; | |
862 | ||
863 | val = oldval = get_zleak_state(); | |
864 | error = sysctl_handle_int(oidp, &val, 0, req); | |
0a7de745 A |
865 | if (error || !req->newptr) { |
866 | return error; | |
867 | } | |
6d2010ae A |
868 | /* |
869 | * Can only be activated if it's off (and not failed.) | |
870 | * Cannot be deactivated once it's on. | |
871 | */ | |
872 | if (val == 1 && oldval == 0) { | |
873 | kern_return_t kr = zleak_activate(); | |
874 | ||
0a7de745 | 875 | if (KERN_SUCCESS != kr) { |
6d2010ae A |
876 | printf("zleak_active: failed to activate " |
877 | "live zone leak debugging (%d).\n", kr); | |
0a7de745 A |
878 | } |
879 | } | |
880 | if (val == 0 && oldval == 1) { | |
6d2010ae | 881 | printf("zleak_active: active, cannot be disabled.\n"); |
0a7de745 | 882 | return EINVAL; |
6d2010ae | 883 | } |
0a7de745 | 884 | return 0; |
6d2010ae A |
885 | } |
886 | ||
887 | SYSCTL_PROC(_kern_zleak, OID_AUTO, active, | |
888 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, | |
889 | 0, 0, sysctl_zleak_active, "I", "zleak activity"); | |
890 | ||
891 | /* | |
892 | * kern.zleak.max_zonemap_size | |
893 | * | |
894 | * Read the value of the maximum zonemap size in bytes; useful | |
895 | * as the maximum size that zleak.global_threshold and | |
896 | * zleak.zone_threshold should be set to. | |
897 | */ | |
898 | static int | |
899 | sysctl_zleak_max_zonemap_size SYSCTL_HANDLER_ARGS | |
900 | { | |
901 | uint64_t zmap_max_size = *(vm_size_t *)arg1; | |
902 | ||
903 | return sysctl_handle_quad(oidp, &zmap_max_size, arg2, req); | |
904 | } | |
905 | ||
906 | SYSCTL_PROC(_kern_zleak, OID_AUTO, max_zonemap_size, | |
907 | CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED, | |
908 | &zleak_max_zonemap_size, 0, | |
909 | sysctl_zleak_max_zonemap_size, "Q", "zleak max zonemap size"); | |
910 | ||
911 | ||
912 | static int | |
913 | sysctl_zleak_threshold SYSCTL_HANDLER_ARGS | |
914 | { | |
915 | #pragma unused(oidp, arg2) | |
916 | int error; | |
917 | uint64_t value = *(vm_size_t *)arg1; | |
918 | ||
0a7de745 | 919 | error = sysctl_io_number(req, value, sizeof(value), &value, NULL); |
6d2010ae | 920 | |
0a7de745 A |
921 | if (error || !req->newptr) { |
922 | return error; | |
923 | } | |
6d2010ae | 924 | |
0a7de745 A |
925 | if (value > (uint64_t)zleak_max_zonemap_size) { |
926 | return ERANGE; | |
927 | } | |
6d2010ae A |
928 | |
929 | *(vm_size_t *)arg1 = value; | |
0a7de745 | 930 | return 0; |
6d2010ae A |
931 | } |
932 | ||
933 | /* | |
934 | * kern.zleak.global_threshold | |
935 | * | |
936 | * Set the global zleak threshold size (in bytes). If the zone map | |
937 | * grows larger than this value, zleaks are automatically activated. | |
938 | * | |
939 | * The default value is set in zleak_init(). | |
940 | */ | |
941 | SYSCTL_PROC(_kern_zleak, OID_AUTO, global_threshold, | |
942 | CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, | |
943 | &zleak_global_tracking_threshold, 0, | |
944 | sysctl_zleak_threshold, "Q", "zleak global threshold"); | |
945 | ||
946 | /* | |
947 | * kern.zleak.zone_threshold | |
948 | * | |
949 | * Set the per-zone threshold size (in bytes) above which any | |
950 | * zone will automatically start zleak tracking. | |
951 | * | |
952 | * The default value is set in zleak_init(). | |
953 | * | |
954 | * Setting this variable will have no effect until zleak tracking is | |
955 | * activated (See above.) | |
956 | */ | |
957 | SYSCTL_PROC(_kern_zleak, OID_AUTO, zone_threshold, | |
958 | CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, | |
959 | &zleak_per_zone_tracking_threshold, 0, | |
960 | sysctl_zleak_threshold, "Q", "zleak per-zone threshold"); | |
961 | ||
0a7de745 | 962 | #endif /* CONFIG_ZLEAKS */ |
5ba3f43e A |
963 | |
964 | extern uint64_t get_zones_collectable_bytes(void); | |
965 | ||
966 | static int | |
967 | sysctl_zones_collectable_bytes SYSCTL_HANDLER_ARGS | |
968 | { | |
969 | #pragma unused(oidp, arg1, arg2) | |
970 | uint64_t zones_free_mem = get_zones_collectable_bytes(); | |
971 | ||
972 | return SYSCTL_OUT(req, &zones_free_mem, sizeof(zones_free_mem)); | |
973 | } | |
974 | ||
975 | SYSCTL_PROC(_kern, OID_AUTO, zones_collectable_bytes, | |
0a7de745 A |
976 | CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED, |
977 | 0, 0, &sysctl_zones_collectable_bytes, "Q", "Collectable memory in zones"); |