]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_malloc.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_malloc.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, 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 */
2d21ac55
A
296};
297
298/* for use with kmzones.kz_zalloczone */
1c79356b
A
299#define KMZ_CREATEZONE ((void *)-2)
300#define KMZ_LOOKUPZONE ((void *)-1)
2d21ac55 301#define KMZ_MALLOC ((void *)0)
1c79356b 302#define KMZ_SHAREZONE ((void *)1)
2d21ac55
A
303
304struct kmzones {
305 size_t kz_elemsize;
306 void *kz_zalloczone;
0b4c1975 307 boolean_t kz_noencrypt;
1c79356b
A
308} kmzones[M_LAST] = {
309#define SOS(sname) sizeof (struct sname)
310#define SOX(sname) -1
0b4c1975
A
311 { -1, 0, FALSE }, /* 0 M_FREE */
312 { MSIZE, KMZ_CREATEZONE, FALSE }, /* 1 M_MBUF */
313 { 0, KMZ_MALLOC, FALSE }, /* 2 M_DEVBUF */
314 { SOS(socket), KMZ_CREATEZONE, TRUE }, /* 3 M_SOCKET */
315 { SOS(inpcb), KMZ_LOOKUPZONE, TRUE }, /* 4 M_PCB */
316 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 5 M_RTABLE */
317 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 6 M_HTABLE */
318 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 7 M_FTABLE */
319 { SOS(rusage), KMZ_CREATEZONE, TRUE }, /* 8 M_ZOMBIE */
320 { 0, KMZ_MALLOC, FALSE }, /* 9 M_IFADDR */
321 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 10 M_SOOPTS */
322 { 0, KMZ_MALLOC, FALSE }, /* 11 M_SONAME */
323 { MAXPATHLEN, KMZ_CREATEZONE, FALSE }, /* 12 M_NAMEI */
324 { 0, KMZ_MALLOC, FALSE }, /* 13 M_GPROF */
325 { 0, KMZ_MALLOC, FALSE }, /* 14 M_IOCTLOPS */
326 { 0, KMZ_MALLOC, FALSE }, /* 15 M_MAPMEM */
327 { SOS(ucred), KMZ_CREATEZONE, FALSE }, /* 16 M_CRED */
328 { SOS(pgrp), KMZ_CREATEZONE, FALSE }, /* 17 M_PGRP */
329 { SOS(session), KMZ_CREATEZONE, FALSE }, /* 18 M_SESSION */
330 { SOS(user32_iovec), KMZ_LOOKUPZONE, FALSE },/* 19 M_IOV32 */
331 { SOS(mount), KMZ_CREATEZONE, FALSE }, /* 20 M_MOUNT */
332 { 0, KMZ_MALLOC, FALSE }, /* 21 M_FHANDLE */
2d21ac55 333#if (NFSCLIENT || NFSSERVER)
0b4c1975
A
334 { SOS(nfsreq), KMZ_CREATEZONE, FALSE }, /* 22 M_NFSREQ */
335 { SOS(nfsmount), KMZ_CREATEZONE, FALSE },/* 23 M_NFSMNT */
336 { SOS(nfsnode), KMZ_CREATEZONE, FALSE }, /* 24 M_NFSNODE */
2d21ac55 337#else
0b4c1975
A
338 { 0, KMZ_MALLOC, FALSE }, /* 22 M_NFSREQ */
339 { 0, KMZ_MALLOC, FALSE }, /* 23 M_NFSMNT */
340 { 0, KMZ_MALLOC, FALSE }, /* 24 M_NFSNODE */
2d21ac55 341#endif
0b4c1975
A
342 { SOS(vnode), KMZ_CREATEZONE, TRUE }, /* 25 M_VNODE */
343 { SOS(namecache),KMZ_CREATEZONE, FALSE }, /* 26 M_CACHE */
2d21ac55 344#if QUOTA
0b4c1975 345 { SOX(dquot), KMZ_LOOKUPZONE, FALSE }, /* 27 M_DQUOT */
2d21ac55 346#else
0b4c1975 347 { 0, KMZ_MALLOC, FALSE }, /* 27 M_DQUOT */
2d21ac55 348#endif
0b4c1975
A
349 { 0, KMZ_MALLOC, FALSE }, /* 28 M_UFSMNT */
350 { 0, KMZ_MALLOC, FALSE }, /* 29 M_CGSUM */
351 { SOS(plimit), KMZ_CREATEZONE, TRUE }, /* 30 M_PLIMIT */
352 { SOS(sigacts), KMZ_CREATEZONE, TRUE }, /* 31 M_SIGACTS */
353 { 0, KMZ_MALLOC, FALSE }, /* 32 M_VMOBJ */
354 { 0, KMZ_MALLOC, FALSE }, /* 33 M_VMOBJHASH */
355 { 0, KMZ_MALLOC, FALSE }, /* 34 M_VMPMAP */
356 { 0, KMZ_MALLOC, FALSE }, /* 35 M_VMPVENT */
357 { 0, KMZ_MALLOC, FALSE }, /* 36 M_VMPAGER */
358 { 0, KMZ_MALLOC, FALSE }, /* 37 M_VMPGDATA */
359 { SOS(fileproc),KMZ_CREATEZONE, TRUE }, /* 38 M_FILEPROC */
360 { SOS(filedesc),KMZ_CREATEZONE, TRUE }, /* 39 M_FILEDESC */
361 { SOX(lockf), KMZ_CREATEZONE, TRUE }, /* 40 M_LOCKF */
362 { SOS(proc), KMZ_CREATEZONE, FALSE }, /* 41 M_PROC */
363 { SOS(pstats), KMZ_CREATEZONE, TRUE }, /* 42 M_PSTATS */
364 { 0, KMZ_MALLOC, FALSE }, /* 43 M_SEGMENT */
365 { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 44 M_LFSNODE */
366 { 0, KMZ_MALLOC, FALSE }, /* 45 M_FFSNODE */
367 { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 46 M_MFSNODE */
368 { 0, KMZ_MALLOC, FALSE }, /* 47 M_NQLEASE */
369 { 0, KMZ_MALLOC, FALSE }, /* 48 M_NQMHOST */
370 { 0, KMZ_MALLOC, FALSE }, /* 49 M_NETADDR */
2d21ac55
A
371#if (NFSCLIENT || NFSSERVER)
372 { SOX(nfsrv_sock),
0b4c1975
A
373 KMZ_CREATEZONE, FALSE }, /* 50 M_NFSSVC */
374 { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */
2d21ac55 375 { SOX(nfsrvcache),
0b4c1975 376 KMZ_CREATEZONE, FALSE }, /* 52 M_NFSD */
2d21ac55 377#else
0b4c1975
A
378 { 0, KMZ_MALLOC, FALSE }, /* 50 M_NFSSVC */
379 { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */
380 { 0, KMZ_MALLOC, FALSE }, /* 52 M_NFSD */
2d21ac55
A
381#endif
382 { SOX(ip_moptions),
0b4c1975
A
383 KMZ_LOOKUPZONE, FALSE }, /* 53 M_IPMOPTS */
384 { SOX(in_multi),KMZ_LOOKUPZONE, FALSE }, /* 54 M_IPMADDR */
2d21ac55 385 { SOX(ether_multi),
0b4c1975
A
386 KMZ_LOOKUPZONE, FALSE }, /* 55 M_IFMADDR */
387 { SOX(mrt), KMZ_CREATEZONE, TRUE }, /* 56 M_MRTABLE */
388 { 0, KMZ_MALLOC, FALSE }, /* 57 unused entry */
389 { 0, KMZ_MALLOC, FALSE }, /* 58 unused entry */
2d21ac55
A
390#if (NFSCLIENT || NFSSERVER)
391 { SOS(nfsrv_descript),
0b4c1975
A
392 KMZ_CREATEZONE, FALSE }, /* 59 M_NFSRVDESC */
393 { SOS(nfsdmap), KMZ_CREATEZONE, FALSE }, /* 60 M_NFSDIROFF */
394 { SOS(fhandle), KMZ_LOOKUPZONE, FALSE }, /* 61 M_NFSBIGFH */
2d21ac55 395#else
0b4c1975
A
396 { 0, KMZ_MALLOC, FALSE }, /* 59 M_NFSRVDESC */
397 { 0, KMZ_MALLOC, FALSE }, /* 60 M_NFSDIROFF */
398 { 0, KMZ_MALLOC, FALSE }, /* 61 M_NFSBIGFH */
2d21ac55 399#endif
0b4c1975
A
400 { 0, KMZ_MALLOC, FALSE }, /* 62 M_MSDOSFSMNT */
401 { 0, KMZ_MALLOC, FALSE }, /* 63 M_MSDOSFSFAT */
402 { 0, KMZ_MALLOC, FALSE }, /* 64 M_MSDOSFSNODE */
403 { SOS(tty), KMZ_CREATEZONE, FALSE }, /* 65 M_TTYS */
404 { 0, KMZ_MALLOC, FALSE }, /* 66 M_EXEC */
405 { 0, KMZ_MALLOC, FALSE }, /* 67 M_MISCFSMNT */
406 { 0, KMZ_MALLOC, FALSE }, /* 68 M_MISCFSNODE */
407 { 0, KMZ_MALLOC, FALSE }, /* 69 M_ADOSFSMNT */
408 { 0, KMZ_MALLOC, FALSE }, /* 70 M_ADOSFSNODE */
409 { 0, KMZ_MALLOC, FALSE }, /* 71 M_ANODE */
410 { SOX(buf), KMZ_CREATEZONE, TRUE }, /* 72 M_BUFHDR */
2d21ac55 411 { (NDFILE * OFILESIZE),
0b4c1975
A
412 KMZ_CREATEZONE, FALSE }, /* 73 M_OFILETABL */
413 { MCLBYTES, KMZ_CREATEZONE, FALSE }, /* 74 M_MCLUST */
2d21ac55 414#if HFS
0b4c1975
A
415 { SOX(hfsmount),KMZ_LOOKUPZONE, FALSE }, /* 75 M_HFSMNT */
416 { SOS(cnode), KMZ_CREATEZONE, TRUE }, /* 76 M_HFSNODE */
417 { SOS(filefork),KMZ_CREATEZONE, TRUE }, /* 77 M_HFSFORK */
2d21ac55 418#else
0b4c1975
A
419 { 0, KMZ_MALLOC, FALSE }, /* 75 M_HFSMNT */
420 { 0, KMZ_MALLOC, FALSE }, /* 76 M_HFSNODE */
421 { 0, KMZ_MALLOC, FALSE }, /* 77 M_HFSFORK */
2d21ac55 422#endif
0b4c1975
A
423 { 0, KMZ_MALLOC, FALSE }, /* 78 M_ZFSMNT */
424 { 0, KMZ_MALLOC, FALSE }, /* 79 M_ZFSNODE */
425 { 0, KMZ_MALLOC, FALSE }, /* 80 M_TEMP */
426 { 0, KMZ_MALLOC, FALSE }, /* 81 M_SECA */
427 { 0, KMZ_MALLOC, FALSE }, /* 82 M_DEVFS */
428 { 0, KMZ_MALLOC, FALSE }, /* 83 M_IPFW */
429 { 0, KMZ_MALLOC, FALSE }, /* 84 M_UDFNODE */
430 { 0, KMZ_MALLOC, FALSE }, /* 85 M_UDFMOUNT */
431 { 0, KMZ_MALLOC, FALSE }, /* 86 M_IP6NDP */
432 { 0, KMZ_MALLOC, FALSE }, /* 87 M_IP6OPT */
433 { 0, KMZ_MALLOC, FALSE }, /* 88 M_IP6MISC */
434 { 0, KMZ_MALLOC, FALSE }, /* 89 M_TSEGQ */
435 { 0, KMZ_MALLOC, FALSE }, /* 90 M_IGMP */
2d21ac55 436#if JOURNALING
0b4c1975
A
437 { SOS(journal), KMZ_CREATEZONE, FALSE }, /* 91 M_JNL_JNL */
438 { SOS(transaction), KMZ_CREATEZONE, FALSE }, /* 92 M_JNL_TR */
2d21ac55 439#else
0b4c1975
A
440 { 0, KMZ_MALLOC, FALSE }, /* 91 M_JNL_JNL */
441 { 0, KMZ_MALLOC, FALSE }, /* 92 M_JNL_TR */
2d21ac55 442#endif
0b4c1975
A
443 { SOS(specinfo), KMZ_CREATEZONE, TRUE }, /* 93 M_SPECINFO */
444 { SOS(kqueue), KMZ_CREATEZONE, FALSE }, /* 94 M_KQUEUE */
2d21ac55 445#if HFS
0b4c1975 446 { SOS(directoryhint), KMZ_CREATEZONE, FALSE }, /* 95 M_HFSDIRHINT */
2d21ac55 447#else
0b4c1975 448 { 0, KMZ_MALLOC, FALSE }, /* 95 M_HFSDIRHINT */
2d21ac55 449#endif
0b4c1975
A
450 { SOS(cl_readahead), KMZ_CREATEZONE, TRUE }, /* 96 M_CLRDAHEAD */
451 { SOS(cl_writebehind),KMZ_CREATEZONE, TRUE }, /* 97 M_CLWRBEHIND */
452 { SOS(user64_iovec), KMZ_LOOKUPZONE, FALSE },/* 98 M_IOV64 */
453 { SOS(fileglob), KMZ_CREATEZONE, TRUE }, /* 99 M_FILEGLOB */
454 { 0, KMZ_MALLOC, FALSE }, /* 100 M_KAUTH */
455 { 0, KMZ_MALLOC, FALSE }, /* 101 M_DUMMYNET */
b0d623f7 456#ifndef __LP64__
0b4c1975 457 { SOS(unsafe_fsnode),KMZ_CREATEZONE, FALSE }, /* 102 M_UNSAFEFS */
b0d623f7 458#else
0b4c1975 459 { 0, KMZ_MALLOC, FALSE }, /* 102 M_UNSAFEFS */
b0d623f7 460#endif /* __LP64__ */
0b4c1975
A
461 { 0, KMZ_MALLOC, FALSE }, /* 103 M_MACPIPELABEL */
462 { 0, KMZ_MALLOC, FALSE }, /* 104 M_MACTEMP */
463 { 0, KMZ_MALLOC, FALSE }, /* 105 M_SBUF */
464 { 0, KMZ_MALLOC, FALSE }, /* 106 M_HFS_EXTATTR */
465 { 0, KMZ_MALLOC, FALSE }, /* 107 M_LCTX */
466 { 0, KMZ_MALLOC, FALSE }, /* 108 M_TRAFFIC_MGT */
b0d623f7 467#if HFS_COMPRESSION
0b4c1975 468 { SOS(decmpfs_cnode),KMZ_CREATEZONE, FALSE }, /* 109 M_DECMPFS_CNODE */
b0d623f7 469#else
0b4c1975 470 { 0, KMZ_MALLOC, FALSE }, /* 109 M_DECMPFS_CNODE */
b0d623f7 471#endif /* HFS_COMPRESSION */
1c79356b
A
472#undef SOS
473#undef SOX
474};
475
55e303ae 476extern zone_t kalloc_zone(vm_size_t); /* XXX */
1c79356b
A
477
478/*
479 * Initialize the kernel memory allocator
480 */
481void
482kmeminit(void)
483{
484 struct kmzones *kmz;
485
d7e50217 486 if ((sizeof(kmzones)/sizeof(kmzones[0])) != (sizeof(memname)/sizeof(memname[0]))) {
2d21ac55 487 panic("kmeminit: kmzones has %lu elements but memname has %lu\n",
d7e50217
A
488 (sizeof(kmzones)/sizeof(kmzones[0])), (sizeof(memname)/sizeof(memname[0])));
489 }
490
1c79356b
A
491 kmz = kmzones;
492 while (kmz < &kmzones[M_LAST]) {
493/* XXX */
2d21ac55 494 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
495 ;
496 else
497/* XXX */
498 if (kmz->kz_zalloczone == KMZ_CREATEZONE) {
499 kmz->kz_zalloczone = zinit(kmz->kz_elemsize,
500 1024 * 1024, PAGE_SIZE,
501 memname[kmz - kmzones]);
0b4c1975
A
502 if (kmz->kz_noencrypt == TRUE)
503 zone_change(kmz->kz_zalloczone, Z_NOENCRYPT, TRUE);
1c79356b
A
504 }
505 else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE)
506 kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize);
507
508 kmz++;
509 }
510
511 kmz = kmzones;
512 while (kmz < &kmzones[M_LAST]) {
513/* XXX */
2d21ac55 514 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
515 ;
516 else
517/* XXX */
518 if (kmz->kz_zalloczone == KMZ_SHAREZONE) {
519 kmz->kz_zalloczone =
520 kmzones[kmz->kz_elemsize].kz_zalloczone;
521 kmz->kz_elemsize =
522 kmzones[kmz->kz_elemsize].kz_elemsize;
523 }
524
525 kmz++;
526 }
527}
528
529#define MDECL(reqlen) \
530union { \
531 struct _mhead hdr; \
532 char _m[(reqlen) + sizeof (struct _mhead)]; \
533}
534
535struct _mhead {
536 size_t mlen;
537 char dat[0];
538};
539
91447636
A
540void *
541_MALLOC(
1c79356b
A
542 size_t size,
543 int type,
544 int flags)
545{
546 MDECL(size) *mem;
547 size_t memsize = sizeof (*mem);
548
549 if (type >= M_LAST)
550 panic("_malloc TYPE");
551
552 if (size == 0)
2d21ac55 553 return (NULL);
1c79356b
A
554
555 if (flags & M_NOWAIT) {
556 mem = (void *)kalloc_noblock(memsize);
557 } else {
558 mem = (void *)kalloc(memsize);
b0d623f7
A
559
560 if (mem == NULL) {
561
562 /*
563 * We get here when the caller told us to block waiting for memory, but
564 * kalloc said there's no memory left to get. Generally, this means there's a
565 * leak or the caller asked for an impossibly large amount of memory. Since there's
566 * nothing left to wait for and the caller isn't expecting a NULL return code, we
567 * just panic. This is less than ideal, but returning NULL doesn't help since the
568 * majority of callers don't check the return value and will just dereference the pointer and
569 * trap anyway. We may as well get a more descriptive message out while we can.
570 */
571
572 panic("_MALLOC: kalloc returned NULL (potential leak), size %llu", (uint64_t) size);
573 }
1c79356b
A
574 }
575 if (!mem)
576 return (0);
577
578 mem->hdr.mlen = memsize;
579
55e303ae
A
580 if (flags & M_ZERO)
581 bzero(mem->hdr.dat, size);
582
1c79356b
A
583 return (mem->hdr.dat);
584}
585
91447636
A
586void
587_FREE(
1c79356b
A
588 void *addr,
589 int type)
590{
591 struct _mhead *hdr;
592
593 if (type >= M_LAST)
594 panic("_free TYPE");
595
9bccf70c
A
596 if (!addr)
597 return; /* correct (convenient bsd kernel legacy) */
1c79356b
A
598
599 hdr = addr; hdr--;
91447636 600 kfree(hdr, hdr->mlen);
1c79356b
A
601}
602
91447636
A
603void *
604_MALLOC_ZONE(
1c79356b
A
605 size_t size,
606 int type,
607 int flags)
608{
609 struct kmzones *kmz;
610 void *elem;
611
612 if (type >= M_LAST)
613 panic("_malloc_zone TYPE");
614
615 kmz = &kmzones[type];
616 if (kmz->kz_zalloczone == KMZ_MALLOC)
91447636 617 panic("_malloc_zone ZONE: type = %d", type);
1c79356b
A
618
619/* XXX */
2d21ac55 620 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
621 panic("_malloc_zone XXX");
622/* XXX */
623 if (size == kmz->kz_elemsize)
624 if (flags & M_NOWAIT) {
625 elem = (void *)zalloc_noblock(kmz->kz_zalloczone);
626 } else {
627 elem = (void *)zalloc(kmz->kz_zalloczone);
628 }
629 else
630 if (flags & M_NOWAIT) {
631 elem = (void *)kalloc_noblock(size);
632 } else {
633 elem = (void *)kalloc(size);
634 }
635
636 return (elem);
637}
638
91447636
A
639void
640_FREE_ZONE(
1c79356b
A
641 void *elem,
642 size_t size,
643 int type)
644{
645 struct kmzones *kmz;
646
647 if (type >= M_LAST)
648 panic("FREE_SIZE");
649
650 kmz = &kmzones[type];
651 if (kmz->kz_zalloczone == KMZ_MALLOC)
652 panic("free_zone ZONE");
653
654/* XXX */
2d21ac55 655 if (kmz->kz_elemsize == (size_t)(-1))
1c79356b
A
656 panic("FREE_SIZE XXX");
657/* XXX */
658 if (size == kmz->kz_elemsize)
91447636 659 zfree(kmz->kz_zalloczone, elem);
1c79356b 660 else
91447636 661 kfree(elem, size);
1c79356b 662}