]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_malloc.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / bsd / kern / kern_malloc.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
17 *
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.
8f6c56a5 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>
77
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81#include <netinet/in_pcb.h>
82
55e303ae
A
83#include <sys/event.h>
84#include <sys/eventvar.h>
85
91447636
A
86#include <sys/proc_internal.h>
87#include <sys/mount_internal.h>
88#include <sys/vnode_internal.h>
89#include <sys/ubc_internal.h>
1c79356b 90#include <sys/namei.h>
91447636 91#include <sys/file_internal.h>
1c79356b
A
92#include <sys/filedesc.h>
93#include <sys/tty.h>
9bccf70c 94#include <sys/quota.h>
91447636 95#include <sys/uio_internal.h>
0c530ab8 96#include <sys/resourcevar.h>
2d21ac55 97#include <sys/signalvar.h>
1c79356b 98
9bccf70c 99#include <hfs/hfs_cnode.h>
1c79356b 100
55e303ae 101#include <miscfs/specfs/specdev.h>
1c79356b
A
102
103#include <nfs/rpcv2.h>
104#include <nfs/nfsproto.h>
105#include <nfs/nfsnode.h>
106#include <nfs/nfsmount.h>
1c79356b 107
d7e50217
A
108#include <vfs/vfs_journal.h>
109
1c79356b
A
110#include <mach/mach_types.h>
111
112#include <kern/zalloc.h>
113#include <kern/kalloc.h>
114
2d21ac55 115void kmeminit(void) __attribute__((section("__TEXT, initcode")));
1c79356b 116
2d21ac55
A
117/* Strings corresponding to types of memory.
118 * Must be in synch with the #defines is sys/malloc.h
119 * NOTE - the reason we pass null strings in some cases is to reduce of foot
120 * print as much as possible for systems where a tiny kernel is needed.
121 * todo - We should probably redsign this and use enums for our types and only
122 * include types needed for that configuration of the kernel. This can't be
123 * done without some kind of kpi since several types are hardwired and exported
124 * (for example see types M_HFSMNT, M_UDFMNT, M_TEMP, etc in sys/malloc.h)
125 */
126const char *memname[] = {
127 "free", /* 0 M_FREE */
128 "mbuf", /* 1 M_MBUF */
129 "devbuf", /* 2 M_DEVBUF */
130 "socket", /* 3 M_SOCKET */
131 "pcb", /* 4 M_PCB */
132 "routetbl", /* 5 M_RTABLE */
133 "hosttbl", /* 6 M_HTABLE */
134 "fragtbl", /* 7 M_FTABLE */
135 "zombie", /* 8 M_ZOMBIE */
136 "ifaddr", /* 9 M_IFADDR */
137 "soopts", /* 10 M_SOOPTS */
138 "soname", /* 11 M_SONAME */
139 "namei", /* 12 M_NAMEI */
140 "gprof", /* 13 M_GPROF */
141 "ioctlops", /* 14 M_IOCTLOPS */
142 "mapmem", /* 15 M_MAPMEM */
143 "cred", /* 16 M_CRED */
144 "pgrp", /* 17 M_PGRP */
145 "session", /* 18 M_SESSION */
146 "iov32", /* 19 M_IOV32 */
147 "mount", /* 20 M_MOUNT */
148 "fhandle", /* 21 M_FHANDLE */
149#if (NFSCLIENT || NFSSERVER)
150 "NFS req", /* 22 M_NFSREQ */
151 "NFS mount", /* 23 M_NFSMNT */
152 "NFS node", /* 24 M_NFSNODE */
153#else
154 "", /* 22 M_NFSREQ */
155 "", /* 23 M_NFSMNT */
156 "", /* 24 M_NFSNODE */
157#endif
158 "vnodes", /* 25 M_VNODE */
159 "namecache", /* 26 M_CACHE */
160#if QUOTA
161 "UFS quota", /* 27 M_DQUOT */
162#else
163 "", /* 27 M_DQUOT */
164#endif
2d21ac55 165 "", /* 28 M_UFSMNT */
2d21ac55
A
166#if (SYSV_SEM || SYSV_MSG || SYSV_SHM)
167 "shm", /* 29 M_SHM */
168#else
169 "", /* 29 M_SHM */
170#endif
171 "plimit", /* 30 M_VMMAP */
172 "sigacts", /* 31 M_VMMAPENT */
173 "VM object", /* 32 M_VMOBJ */
174 "VM objhash", /* 33 M_VMOBJHASH */
175 "VM pmap", /* 34 M_VMPMAP */
176 "VM pvmap", /* 35 M_VMPVENT */
177 "VM pager", /* 36 M_VMPAGER */
178 "VM pgdata", /* 37 M_VMPGDATA */
179 "fileproc", /* 38 M_FILEPROC */
180 "file desc", /* 39 M_FILEDESC */
181 "lockf", /* 40 M_LOCKF */
182 "proc", /* 41 M_PROC */
183 "pstats", /* 42 M_SUBPROC */
184 "LFS segment", /* 43 M_SEGMENT */
185 "LFS node", /* 44 M_LFSNODE */
2d21ac55 186 "", /* 45 M_FFSNODE */
2d21ac55
A
187 "MFS node", /* 46 M_MFSNODE */
188 "NQNFS Lease", /* 47 M_NQLEASE */
189 "NQNFS Host", /* 48 M_NQMHOST */
190 "Export Host", /* 49 M_NETADDR */
191#if (NFSCLIENT || NFSSERVER)
192 "NFS srvsock", /* 50 M_NFSSVC */
193 "NFS uid", /* 51 M_NFSUID */
194 "NFS daemon", /* 52 M_NFSD */
195#else
196 "", /* 50 M_NFSSVC */
197 "", /* 51 M_NFSUID */
198 "", /* 52 M_NFSD */
199#endif
200 "ip_moptions", /* 53 M_IPMOPTS */
201 "in_multi", /* 54 M_IPMADDR */
202 "ether_multi", /* 55 M_IFMADDR */
203 "mrt", /* 56 M_MRTABLE */
b0d623f7
A
204 "", /* 57 unused entry */
205 "", /* 58 unused entry */
2d21ac55
A
206#if (NFSCLIENT || NFSSERVER)
207 "NFSV3 srvdesc",/* 59 M_NFSRVDESC */
208 "NFSV3 diroff", /* 60 M_NFSDIROFF */
209 "NFSV3 bigfh", /* 61 M_NFSBIGFH */
210#else
211 "", /* 59 M_NFSRVDESC */
212 "", /* 60 M_NFSDIROFF */
213 "", /* 61 M_NFSBIGFH */
214#endif
215 "MSDOSFS mount",/* 62 M_MSDOSFSMNT */
216 "MSDOSFS fat", /* 63 M_MSDOSFSFAT */
217 "MSDOSFS node", /* 64 M_MSDOSFSNODE */
218 "ttys", /* 65 M_TTYS */
219 "exec", /* 66 M_EXEC */
220 "miscfs mount", /* 67 M_MISCFSMNT */
221 "miscfs node", /* 68 M_MISCFSNODE */
222 "adosfs mount", /* 69 M_ADOSFSMNT */
223 "adosfs node", /* 70 M_ADOSFSNODE */
224 "adosfs anode", /* 71 M_ANODE */
225 "buf hdrs", /* 72 M_BUFHDR */
226 "ofile tabl", /* 73 M_OFILETABL */
227 "mbuf clust", /* 74 M_MCLUST */
228#if HFS
229 "HFS mount", /* 75 M_HFSMNT */
230 "HFS node", /* 76 M_HFSNODE */
231 "HFS fork", /* 77 M_HFSFORK */
232#else
233 "", /* 75 M_HFSMNT */
234 "", /* 76 M_HFSNODE */
235 "", /* 77 M_HFSFORK */
236#endif
237 "ZFS mount", /* 78 M_ZFSFSMNT */
238 "ZFS node", /* 79 M_ZFSNODE */
239 "temp", /* 80 M_TEMP */
240 "key mgmt", /* 81 M_SECA */
241 "DEVFS", /* 82 M_DEVFS */
242 "IpFw/IpAcct", /* 83 M_IPFW */
243 "UDF node", /* 84 M_UDFNODE */
244 "UDF mount", /* 85 M_UDFMNT */
245#if INET6
246 "IPv6 NDP", /* 86 M_IP6NDP */
247 "IPv6 options", /* 87 M_IP6OPT */
248 "IPv6 Misc", /* 88 M_IP6MISC */
249#else
250 "", /* 86 M_IP6NDP */
251 "", /* 87 M_IP6OPT */
252 "", /* 88 M_IP6MISC */
253#endif
254 "TCP Segment Q",/* 89 M_TSEGQ */
255 "IGMP state", /* 90 M_IGMP */
256#if JOURNALING
257 "Journal", /* 91 M_JNL_JNL */
258 "Transaction", /* 92 M_JNL_TR */
259#else
260 "", /* 91 M_JNL_JNL */
261 "", /* 92 M_JNL_TR */
262#endif
263 "specinfo", /* 93 M_SPECINFO */
264 "kqueue", /* 94 M_KQUEUE */
265#if HFS
266 "HFS dirhint", /* 95 M_HFSDIRHINT */
267#else
268 "", /* 95 M_HFSDIRHINT */
269#endif
270 "cluster_read", /* 96 M_CLRDAHEAD */
271 "cluster_write",/* 97 M_CLWRBEHIND */
272 "iov64", /* 98 M_IOV64 */
273 "fileglob", /* 99 M_FILEGLOB */
274 "kauth", /* 100 M_KAUTH */
275 "dummynet", /* 101 M_DUMMYNET */
b0d623f7 276#ifndef __LP64__
2d21ac55 277 "unsafe_fsnode", /* 102 M_UNSAFEFS */
b0d623f7
A
278#else
279 "", /* 102 M_UNSAFEFS */
280#endif /* __LP64__ */
2d21ac55
A
281 "macpipelabel", /* 103 M_MACPIPELABEL */
282 "mactemp", /* 104 M_MACTEMP */
283 "sbuf", /* 105 M_SBUF */
284 "extattr", /* 106 M_EXTATTR */
285 "lctx", /* 107 M_LCTX */
286#if TRAFFIC_MGT
287 "traffic_mgt", /* 108 M_TRAFFIC_MGT */
288#else
289 "", /* 108 M_TRAFFIC_MGT */
290#endif
b0d623f7
A
291#if HFS_COMPRESSION
292 "decmpfs_cnode",/* 109 M_DECMPFS_CNODE */
293#else
294 "", /* 109 M_DECMPFS_CNODE */
295#endif /* HFS_COMPRESSION */
6d2010ae
A
296 "ipmfilter", /* 110 M_INMFILTER */
297 "ipmsource", /* 111 M_IPMSOURCE */
298 "in6mfilter", /* 112 M_IN6MFILTER */
299 "ip6mopts", /* 113 M_IP6MOPTS */
300 "ip6msource", /* 114 M_IP6MSOURCE */
2d21ac55
A
301};
302
303/* for use with kmzones.kz_zalloczone */
6d2010ae 304#define KMZ_CREATEZONE_ACCT ((void *)-3)
1c79356b
A
305#define KMZ_CREATEZONE ((void *)-2)
306#define KMZ_LOOKUPZONE ((void *)-1)
2d21ac55 307#define KMZ_MALLOC ((void *)0)
1c79356b 308#define KMZ_SHAREZONE ((void *)1)
2d21ac55
A
309
310struct kmzones {
311 size_t kz_elemsize;
312 void *kz_zalloczone;
0b4c1975 313 boolean_t kz_noencrypt;
1c79356b
A
314} kmzones[M_LAST] = {
315#define SOS(sname) sizeof (struct sname)
316#define SOX(sname) -1
0b4c1975
A
317 { -1, 0, FALSE }, /* 0 M_FREE */
318 { MSIZE, KMZ_CREATEZONE, FALSE }, /* 1 M_MBUF */
319 { 0, KMZ_MALLOC, FALSE }, /* 2 M_DEVBUF */
320 { SOS(socket), KMZ_CREATEZONE, TRUE }, /* 3 M_SOCKET */
321 { SOS(inpcb), KMZ_LOOKUPZONE, TRUE }, /* 4 M_PCB */
322 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 5 M_RTABLE */
323 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 6 M_HTABLE */
324 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 7 M_FTABLE */
325 { SOS(rusage), KMZ_CREATEZONE, TRUE }, /* 8 M_ZOMBIE */
326 { 0, KMZ_MALLOC, FALSE }, /* 9 M_IFADDR */
327 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 10 M_SOOPTS */
328 { 0, KMZ_MALLOC, FALSE }, /* 11 M_SONAME */
329 { MAXPATHLEN, KMZ_CREATEZONE, FALSE }, /* 12 M_NAMEI */
330 { 0, KMZ_MALLOC, FALSE }, /* 13 M_GPROF */
331 { 0, KMZ_MALLOC, FALSE }, /* 14 M_IOCTLOPS */
332 { 0, KMZ_MALLOC, FALSE }, /* 15 M_MAPMEM */
333 { SOS(ucred), KMZ_CREATEZONE, FALSE }, /* 16 M_CRED */
334 { SOS(pgrp), KMZ_CREATEZONE, FALSE }, /* 17 M_PGRP */
335 { SOS(session), KMZ_CREATEZONE, FALSE }, /* 18 M_SESSION */
336 { SOS(user32_iovec), KMZ_LOOKUPZONE, FALSE },/* 19 M_IOV32 */
337 { SOS(mount), KMZ_CREATEZONE, FALSE }, /* 20 M_MOUNT */
338 { 0, KMZ_MALLOC, FALSE }, /* 21 M_FHANDLE */
2d21ac55 339#if (NFSCLIENT || NFSSERVER)
0b4c1975 340 { SOS(nfsreq), KMZ_CREATEZONE, FALSE }, /* 22 M_NFSREQ */
6d2010ae 341 { SOS(nfsmount),KMZ_CREATEZONE, FALSE }, /* 23 M_NFSMNT */
0b4c1975 342 { SOS(nfsnode), KMZ_CREATEZONE, FALSE }, /* 24 M_NFSNODE */
2d21ac55 343#else
0b4c1975
A
344 { 0, KMZ_MALLOC, FALSE }, /* 22 M_NFSREQ */
345 { 0, KMZ_MALLOC, FALSE }, /* 23 M_NFSMNT */
346 { 0, KMZ_MALLOC, FALSE }, /* 24 M_NFSNODE */
2d21ac55 347#endif
0b4c1975 348 { SOS(vnode), KMZ_CREATEZONE, TRUE }, /* 25 M_VNODE */
6d2010ae 349 { SOS(namecache), KMZ_CREATEZONE, FALSE }, /* 26 M_CACHE */
2d21ac55 350#if QUOTA
0b4c1975 351 { SOX(dquot), KMZ_LOOKUPZONE, FALSE }, /* 27 M_DQUOT */
2d21ac55 352#else
0b4c1975 353 { 0, KMZ_MALLOC, FALSE }, /* 27 M_DQUOT */
2d21ac55 354#endif
0b4c1975 355 { 0, KMZ_MALLOC, FALSE }, /* 28 M_UFSMNT */
6d2010ae 356 { 0, KMZ_MALLOC, FALSE }, /* 29 M_SHM */
0b4c1975 357 { SOS(plimit), KMZ_CREATEZONE, TRUE }, /* 30 M_PLIMIT */
6d2010ae 358 { SOS(sigacts), KMZ_CREATEZONE_ACCT, TRUE }, /* 31 M_SIGACTS */
0b4c1975
A
359 { 0, KMZ_MALLOC, FALSE }, /* 32 M_VMOBJ */
360 { 0, KMZ_MALLOC, FALSE }, /* 33 M_VMOBJHASH */
361 { 0, KMZ_MALLOC, FALSE }, /* 34 M_VMPMAP */
362 { 0, KMZ_MALLOC, FALSE }, /* 35 M_VMPVENT */
363 { 0, KMZ_MALLOC, FALSE }, /* 36 M_VMPAGER */
364 { 0, KMZ_MALLOC, FALSE }, /* 37 M_VMPGDATA */
6d2010ae
A
365 { SOS(fileproc),KMZ_CREATEZONE_ACCT, TRUE }, /* 38 M_FILEPROC */
366 { SOS(filedesc),KMZ_CREATEZONE_ACCT, TRUE }, /* 39 M_FILEDESC */
367 { SOX(lockf), KMZ_CREATEZONE_ACCT, TRUE }, /* 40 M_LOCKF */
0b4c1975
A
368 { SOS(proc), KMZ_CREATEZONE, FALSE }, /* 41 M_PROC */
369 { SOS(pstats), KMZ_CREATEZONE, TRUE }, /* 42 M_PSTATS */
370 { 0, KMZ_MALLOC, FALSE }, /* 43 M_SEGMENT */
371 { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 44 M_LFSNODE */
372 { 0, KMZ_MALLOC, FALSE }, /* 45 M_FFSNODE */
373 { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 46 M_MFSNODE */
374 { 0, KMZ_MALLOC, FALSE }, /* 47 M_NQLEASE */
375 { 0, KMZ_MALLOC, FALSE }, /* 48 M_NQMHOST */
376 { 0, KMZ_MALLOC, FALSE }, /* 49 M_NETADDR */
2d21ac55
A
377#if (NFSCLIENT || NFSSERVER)
378 { SOX(nfsrv_sock),
6d2010ae 379 KMZ_CREATEZONE_ACCT, FALSE }, /* 50 M_NFSSVC */
0b4c1975 380 { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */
2d21ac55 381 { SOX(nfsrvcache),
6d2010ae 382 KMZ_CREATEZONE_ACCT, FALSE }, /* 52 M_NFSD */
2d21ac55 383#else
0b4c1975
A
384 { 0, KMZ_MALLOC, FALSE }, /* 50 M_NFSSVC */
385 { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */
386 { 0, KMZ_MALLOC, FALSE }, /* 52 M_NFSD */
2d21ac55
A
387#endif
388 { SOX(ip_moptions),
0b4c1975
A
389 KMZ_LOOKUPZONE, FALSE }, /* 53 M_IPMOPTS */
390 { SOX(in_multi),KMZ_LOOKUPZONE, FALSE }, /* 54 M_IPMADDR */
2d21ac55 391 { SOX(ether_multi),
0b4c1975
A
392 KMZ_LOOKUPZONE, FALSE }, /* 55 M_IFMADDR */
393 { SOX(mrt), KMZ_CREATEZONE, TRUE }, /* 56 M_MRTABLE */
394 { 0, KMZ_MALLOC, FALSE }, /* 57 unused entry */
395 { 0, KMZ_MALLOC, FALSE }, /* 58 unused entry */
2d21ac55
A
396#if (NFSCLIENT || NFSSERVER)
397 { SOS(nfsrv_descript),
6d2010ae 398 KMZ_CREATEZONE_ACCT, FALSE }, /* 59 M_NFSRVDESC */
0b4c1975
A
399 { SOS(nfsdmap), KMZ_CREATEZONE, FALSE }, /* 60 M_NFSDIROFF */
400 { SOS(fhandle), KMZ_LOOKUPZONE, FALSE }, /* 61 M_NFSBIGFH */
2d21ac55 401#else
0b4c1975
A
402 { 0, KMZ_MALLOC, FALSE }, /* 59 M_NFSRVDESC */
403 { 0, KMZ_MALLOC, FALSE }, /* 60 M_NFSDIROFF */
404 { 0, KMZ_MALLOC, FALSE }, /* 61 M_NFSBIGFH */
2d21ac55 405#endif
0b4c1975
A
406 { 0, KMZ_MALLOC, FALSE }, /* 62 M_MSDOSFSMNT */
407 { 0, KMZ_MALLOC, FALSE }, /* 63 M_MSDOSFSFAT */
408 { 0, KMZ_MALLOC, FALSE }, /* 64 M_MSDOSFSNODE */
409 { SOS(tty), KMZ_CREATEZONE, FALSE }, /* 65 M_TTYS */
410 { 0, KMZ_MALLOC, FALSE }, /* 66 M_EXEC */
411 { 0, KMZ_MALLOC, FALSE }, /* 67 M_MISCFSMNT */
412 { 0, KMZ_MALLOC, FALSE }, /* 68 M_MISCFSNODE */
413 { 0, KMZ_MALLOC, FALSE }, /* 69 M_ADOSFSMNT */
414 { 0, KMZ_MALLOC, FALSE }, /* 70 M_ADOSFSNODE */
415 { 0, KMZ_MALLOC, FALSE }, /* 71 M_ANODE */
6d2010ae 416 { 0, KMZ_MALLOC, TRUE }, /* 72 M_BUFHDR */
2d21ac55 417 { (NDFILE * OFILESIZE),
6d2010ae 418 KMZ_CREATEZONE_ACCT, FALSE }, /* 73 M_OFILETABL */
0b4c1975 419 { MCLBYTES, KMZ_CREATEZONE, FALSE }, /* 74 M_MCLUST */
2d21ac55 420#if HFS
0b4c1975
A
421 { SOX(hfsmount),KMZ_LOOKUPZONE, FALSE }, /* 75 M_HFSMNT */
422 { SOS(cnode), KMZ_CREATEZONE, TRUE }, /* 76 M_HFSNODE */
423 { SOS(filefork),KMZ_CREATEZONE, TRUE }, /* 77 M_HFSFORK */
2d21ac55 424#else
0b4c1975
A
425 { 0, KMZ_MALLOC, FALSE }, /* 75 M_HFSMNT */
426 { 0, KMZ_MALLOC, FALSE }, /* 76 M_HFSNODE */
427 { 0, KMZ_MALLOC, FALSE }, /* 77 M_HFSFORK */
2d21ac55 428#endif
0b4c1975
A
429 { 0, KMZ_MALLOC, FALSE }, /* 78 M_ZFSMNT */
430 { 0, KMZ_MALLOC, FALSE }, /* 79 M_ZFSNODE */
431 { 0, KMZ_MALLOC, FALSE }, /* 80 M_TEMP */
432 { 0, KMZ_MALLOC, FALSE }, /* 81 M_SECA */
433 { 0, KMZ_MALLOC, FALSE }, /* 82 M_DEVFS */
434 { 0, KMZ_MALLOC, FALSE }, /* 83 M_IPFW */
435 { 0, KMZ_MALLOC, FALSE }, /* 84 M_UDFNODE */
436 { 0, KMZ_MALLOC, FALSE }, /* 85 M_UDFMOUNT */
437 { 0, KMZ_MALLOC, FALSE }, /* 86 M_IP6NDP */
438 { 0, KMZ_MALLOC, FALSE }, /* 87 M_IP6OPT */
439 { 0, KMZ_MALLOC, FALSE }, /* 88 M_IP6MISC */
440 { 0, KMZ_MALLOC, FALSE }, /* 89 M_TSEGQ */
441 { 0, KMZ_MALLOC, FALSE }, /* 90 M_IGMP */
2d21ac55 442#if JOURNALING
0b4c1975
A
443 { SOS(journal), KMZ_CREATEZONE, FALSE }, /* 91 M_JNL_JNL */
444 { SOS(transaction), KMZ_CREATEZONE, FALSE }, /* 92 M_JNL_TR */
2d21ac55 445#else
6d2010ae
A
446 { 0, KMZ_MALLOC, FALSE }, /* 91 M_JNL_JNL */
447 { 0, KMZ_MALLOC, FALSE }, /* 92 M_JNL_TR */
2d21ac55 448#endif
6d2010ae
A
449 { SOS(specinfo),KMZ_CREATEZONE, TRUE }, /* 93 M_SPECINFO */
450 { SOS(kqueue), KMZ_CREATEZONE, FALSE }, /* 94 M_KQUEUE */
2d21ac55 451#if HFS
6d2010ae 452 { SOS(directoryhint), KMZ_CREATEZONE, TRUE }, /* 95 M_HFSDIRHINT */
2d21ac55 453#else
6d2010ae 454 { 0, KMZ_MALLOC, FALSE }, /* 95 M_HFSDIRHINT */
2d21ac55 455#endif
0b4c1975
A
456 { SOS(cl_readahead), KMZ_CREATEZONE, TRUE }, /* 96 M_CLRDAHEAD */
457 { SOS(cl_writebehind),KMZ_CREATEZONE, TRUE }, /* 97 M_CLWRBEHIND */
458 { SOS(user64_iovec), KMZ_LOOKUPZONE, FALSE },/* 98 M_IOV64 */
459 { SOS(fileglob), KMZ_CREATEZONE, TRUE }, /* 99 M_FILEGLOB */
460 { 0, KMZ_MALLOC, FALSE }, /* 100 M_KAUTH */
461 { 0, KMZ_MALLOC, FALSE }, /* 101 M_DUMMYNET */
b0d623f7 462#ifndef __LP64__
6d2010ae 463 { SOS(unsafe_fsnode),KMZ_CREATEZONE, TRUE }, /* 102 M_UNSAFEFS */
b0d623f7 464#else
0b4c1975 465 { 0, KMZ_MALLOC, FALSE }, /* 102 M_UNSAFEFS */
b0d623f7 466#endif /* __LP64__ */
0b4c1975
A
467 { 0, KMZ_MALLOC, FALSE }, /* 103 M_MACPIPELABEL */
468 { 0, KMZ_MALLOC, FALSE }, /* 104 M_MACTEMP */
469 { 0, KMZ_MALLOC, FALSE }, /* 105 M_SBUF */
470 { 0, KMZ_MALLOC, FALSE }, /* 106 M_HFS_EXTATTR */
471 { 0, KMZ_MALLOC, FALSE }, /* 107 M_LCTX */
472 { 0, KMZ_MALLOC, FALSE }, /* 108 M_TRAFFIC_MGT */
b0d623f7 473#if HFS_COMPRESSION
6d2010ae 474 { SOS(decmpfs_cnode),KMZ_CREATEZONE , FALSE}, /* 109 M_DECMPFS_CNODE */
b0d623f7 475#else
0b4c1975 476 { 0, KMZ_MALLOC, FALSE }, /* 109 M_DECMPFS_CNODE */
b0d623f7 477#endif /* HFS_COMPRESSION */
6d2010ae
A
478 { 0, KMZ_MALLOC, FALSE }, /* 110 M_INMFILTER */
479 { 0, KMZ_MALLOC, FALSE }, /* 111 M_IPMSOURCE */
480 { 0, KMZ_MALLOC, FALSE }, /* 112 M_IN6MFILTER */
481 { 0, KMZ_MALLOC, FALSE }, /* 113 M_IP6MOPTS */
482 { 0, KMZ_MALLOC, FALSE }, /* 114 M_IP6MSOURCE */
1c79356b
A
483#undef SOS
484#undef SOX
485};
486
55e303ae 487extern zone_t kalloc_zone(vm_size_t); /* XXX */
1c79356b
A
488
489/*
490 * Initialize the kernel memory allocator
491 */
492void
493kmeminit(void)
494{
495 struct kmzones *kmz;
496
d7e50217 497 if ((sizeof(kmzones)/sizeof(kmzones[0])) != (sizeof(memname)/sizeof(memname[0]))) {
2d21ac55 498 panic("kmeminit: kmzones has %lu elements but memname has %lu\n",
d7e50217
A
499 (sizeof(kmzones)/sizeof(kmzones[0])), (sizeof(memname)/sizeof(memname[0])));
500 }
501
1c79356b
A
502 kmz = kmzones;
503 while (kmz < &kmzones[M_LAST]) {
504/* XXX */
2d21ac55 505 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
506 ;
507 else
508/* XXX */
6d2010ae
A
509 if (kmz->kz_zalloczone == KMZ_CREATEZONE ||
510 kmz->kz_zalloczone == KMZ_CREATEZONE_ACCT) {
1c79356b
A
511 kmz->kz_zalloczone = zinit(kmz->kz_elemsize,
512 1024 * 1024, PAGE_SIZE,
513 memname[kmz - kmzones]);
6d2010ae
A
514 zone_change(kmz->kz_zalloczone, Z_CALLERACCT,
515 (kmz->kz_zalloczone == KMZ_CREATEZONE_ACCT));
516
0b4c1975
A
517 if (kmz->kz_noencrypt == TRUE)
518 zone_change(kmz->kz_zalloczone, Z_NOENCRYPT, TRUE);
1c79356b
A
519 }
520 else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE)
521 kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize);
522
523 kmz++;
524 }
525
526 kmz = kmzones;
527 while (kmz < &kmzones[M_LAST]) {
528/* XXX */
2d21ac55 529 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
530 ;
531 else
532/* XXX */
533 if (kmz->kz_zalloczone == KMZ_SHAREZONE) {
534 kmz->kz_zalloczone =
535 kmzones[kmz->kz_elemsize].kz_zalloczone;
536 kmz->kz_elemsize =
537 kmzones[kmz->kz_elemsize].kz_elemsize;
538 }
539
540 kmz++;
541 }
542}
543
1c79356b
A
544struct _mhead {
545 size_t mlen;
546 char dat[0];
547};
548
91447636
A
549void *
550_MALLOC(
1c79356b
A
551 size_t size,
552 int type,
553 int flags)
554{
6d2010ae
A
555 struct _mhead *hdr;
556 size_t memsize = sizeof (*hdr) + size;
1c79356b
A
557
558 if (type >= M_LAST)
559 panic("_malloc TYPE");
560
561 if (size == 0)
2d21ac55 562 return (NULL);
1c79356b
A
563
564 if (flags & M_NOWAIT) {
6d2010ae 565 hdr = (void *)kalloc_noblock(memsize);
1c79356b 566 } else {
6d2010ae 567 hdr = (void *)kalloc(memsize);
b0d623f7 568
6d2010ae 569 if (hdr == NULL) {
b0d623f7
A
570
571 /*
572 * We get here when the caller told us to block waiting for memory, but
573 * kalloc said there's no memory left to get. Generally, this means there's a
574 * leak or the caller asked for an impossibly large amount of memory. Since there's
575 * nothing left to wait for and the caller isn't expecting a NULL return code, we
576 * just panic. This is less than ideal, but returning NULL doesn't help since the
577 * majority of callers don't check the return value and will just dereference the pointer and
578 * trap anyway. We may as well get a more descriptive message out while we can.
579 */
580
581 panic("_MALLOC: kalloc returned NULL (potential leak), size %llu", (uint64_t) size);
582 }
1c79356b 583 }
6d2010ae 584 if (!hdr)
1c79356b
A
585 return (0);
586
6d2010ae 587 hdr->mlen = memsize;
1c79356b 588
55e303ae 589 if (flags & M_ZERO)
6d2010ae 590 bzero(hdr->dat, size);
55e303ae 591
6d2010ae 592 return (hdr->dat);
1c79356b
A
593}
594
91447636
A
595void
596_FREE(
1c79356b
A
597 void *addr,
598 int type)
599{
600 struct _mhead *hdr;
601
602 if (type >= M_LAST)
603 panic("_free TYPE");
604
9bccf70c
A
605 if (!addr)
606 return; /* correct (convenient bsd kernel legacy) */
1c79356b
A
607
608 hdr = addr; hdr--;
91447636 609 kfree(hdr, hdr->mlen);
1c79356b
A
610}
611
6d2010ae
A
612void *
613_REALLOC(
614 void *addr,
615 size_t size,
616 int type,
617 int flags)
618{
619 struct _mhead *hdr;
620 void *newaddr;
621 size_t alloc;
622
623 /* realloc(NULL, ...) is equivalent to malloc(...) */
624 if (addr == NULL)
625 return (_MALLOC(size, type, flags));
626
627 /* Allocate a new, bigger (or smaller) block */
628 if ((newaddr = _MALLOC(size, type, flags)) == NULL)
629 return (NULL);
630
631 hdr = addr;
632 --hdr;
633 alloc = hdr->mlen - sizeof (*hdr);
634
635 /* Copy over original contents */
636 bcopy(addr, newaddr, MIN(size, alloc));
637 _FREE(addr, type);
638
639 return (newaddr);
640}
641
91447636
A
642void *
643_MALLOC_ZONE(
1c79356b
A
644 size_t size,
645 int type,
646 int flags)
647{
648 struct kmzones *kmz;
649 void *elem;
650
651 if (type >= M_LAST)
652 panic("_malloc_zone TYPE");
653
654 kmz = &kmzones[type];
655 if (kmz->kz_zalloczone == KMZ_MALLOC)
91447636 656 panic("_malloc_zone ZONE: type = %d", type);
1c79356b
A
657
658/* XXX */
2d21ac55 659 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
660 panic("_malloc_zone XXX");
661/* XXX */
662 if (size == kmz->kz_elemsize)
663 if (flags & M_NOWAIT) {
664 elem = (void *)zalloc_noblock(kmz->kz_zalloczone);
665 } else {
666 elem = (void *)zalloc(kmz->kz_zalloczone);
667 }
668 else
669 if (flags & M_NOWAIT) {
670 elem = (void *)kalloc_noblock(size);
671 } else {
672 elem = (void *)kalloc(size);
673 }
674
675 return (elem);
676}
677
91447636
A
678void
679_FREE_ZONE(
1c79356b
A
680 void *elem,
681 size_t size,
682 int type)
683{
684 struct kmzones *kmz;
685
686 if (type >= M_LAST)
687 panic("FREE_SIZE");
688
689 kmz = &kmzones[type];
690 if (kmz->kz_zalloczone == KMZ_MALLOC)
691 panic("free_zone ZONE");
692
693/* XXX */
2d21ac55 694 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
695 panic("FREE_SIZE XXX");
696/* XXX */
697 if (size == kmz->kz_elemsize)
91447636 698 zfree(kmz->kz_zalloczone, elem);
1c79356b 699 else
91447636 700 kfree(elem, size);
1c79356b 701}
6d2010ae
A
702
703#if CONFIG_ZLEAKS
704
705SYSCTL_DECL(_kern_zleak);
706SYSCTL_NODE(_kern, OID_AUTO, zleak, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "zleak");
707
708/*
709 * kern.zleak.active
710 *
711 * Show the status of the zleak subsystem (0 = enabled, 1 = active,
712 * and -1 = failed), and if enabled, allow it to be activated immediately.
713 */
714static int
715sysctl_zleak_active SYSCTL_HANDLER_ARGS
716{
717#pragma unused(arg1, arg2)
718 int oldval, val, error;
719
720 val = oldval = get_zleak_state();
721 error = sysctl_handle_int(oidp, &val, 0, req);
722 if (error || !req->newptr)
723 return (error);
724 /*
725 * Can only be activated if it's off (and not failed.)
726 * Cannot be deactivated once it's on.
727 */
728 if (val == 1 && oldval == 0) {
729 kern_return_t kr = zleak_activate();
730
731 if (KERN_SUCCESS != kr)
732 printf("zleak_active: failed to activate "
733 "live zone leak debugging (%d).\n", kr);
734 } if (val == 0 && oldval == 1) {
735 printf("zleak_active: active, cannot be disabled.\n");
736 return (EINVAL);
737 }
738 return (0);
739}
740
741SYSCTL_PROC(_kern_zleak, OID_AUTO, active,
742 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
743 0, 0, sysctl_zleak_active, "I", "zleak activity");
744
745/*
746 * kern.zleak.max_zonemap_size
747 *
748 * Read the value of the maximum zonemap size in bytes; useful
749 * as the maximum size that zleak.global_threshold and
750 * zleak.zone_threshold should be set to.
751 */
752static int
753sysctl_zleak_max_zonemap_size SYSCTL_HANDLER_ARGS
754{
755 uint64_t zmap_max_size = *(vm_size_t *)arg1;
756
757 return sysctl_handle_quad(oidp, &zmap_max_size, arg2, req);
758}
759
760SYSCTL_PROC(_kern_zleak, OID_AUTO, max_zonemap_size,
761 CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
762 &zleak_max_zonemap_size, 0,
763 sysctl_zleak_max_zonemap_size, "Q", "zleak max zonemap size");
764
765
766static int
767sysctl_zleak_threshold SYSCTL_HANDLER_ARGS
768{
769#pragma unused(oidp, arg2)
770 int error;
771 uint64_t value = *(vm_size_t *)arg1;
772
773 error = sysctl_io_number(req, value, sizeof (value), &value, NULL);
774
775 if (error || !req->newptr)
776 return (error);
777
778 if (value > (uint64_t)zleak_max_zonemap_size)
779 return (ERANGE);
780
781 *(vm_size_t *)arg1 = value;
782 return (0);
783}
784
785/*
786 * kern.zleak.global_threshold
787 *
788 * Set the global zleak threshold size (in bytes). If the zone map
789 * grows larger than this value, zleaks are automatically activated.
790 *
791 * The default value is set in zleak_init().
792 */
793SYSCTL_PROC(_kern_zleak, OID_AUTO, global_threshold,
794 CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
795 &zleak_global_tracking_threshold, 0,
796 sysctl_zleak_threshold, "Q", "zleak global threshold");
797
798/*
799 * kern.zleak.zone_threshold
800 *
801 * Set the per-zone threshold size (in bytes) above which any
802 * zone will automatically start zleak tracking.
803 *
804 * The default value is set in zleak_init().
805 *
806 * Setting this variable will have no effect until zleak tracking is
807 * activated (See above.)
808 */
809SYSCTL_PROC(_kern_zleak, OID_AUTO, zone_threshold,
810 CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
811 &zleak_per_zone_tracking_threshold, 0,
812 sysctl_zleak_threshold, "Q", "zleak per-zone threshold");
813
814#endif /* CONFIG_ZLEAKS */