2  * Copyright (c) 2000-2010 Apple Inc. All rights reserved. 
   4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 
   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. 
  15  * Please obtain a copy of the License at 
  16  * http://www.opensource.apple.com/apsl/ and read it before using this file. 
  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 
  20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  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. 
  26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 
  28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ 
  30  * Copyright (c) 1987, 1991, 1993 
  31  *      The Regents of the University of California.  All rights reserved. 
  33  * Redistribution and use in source and binary forms, with or without 
  34  * modification, are permitted provided that the following conditions 
  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. 
  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 
  61  *      @(#)kern_malloc.c       8.4 (Berkeley) 5/20/95 
  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, 
  70 #include <sys/param.h> 
  71 #include <sys/malloc.h> 
  73 #include <sys/socket.h> 
  74 #include <sys/socketvar.h> 
  76 #include <net/route.h> 
  78 #include <netinet/in.h> 
  79 #include <netinet/in_systm.h> 
  80 #include <netinet/ip.h> 
  81 #include <netinet/in_pcb.h> 
  83 #include <sys/event.h> 
  84 #include <sys/eventvar.h> 
  86 #include <sys/proc_internal.h> 
  87 #include <sys/mount_internal.h> 
  88 #include <sys/vnode_internal.h> 
  89 #include <sys/ubc_internal.h> 
  90 #include <sys/namei.h> 
  91 #include <sys/file_internal.h> 
  92 #include <sys/filedesc.h> 
  94 #include <sys/quota.h> 
  95 #include <sys/uio_internal.h> 
  96 #include <sys/resourcevar.h> 
  97 #include <sys/signalvar.h> 
  99 #include <hfs/hfs_cnode.h> 
 101 #include <miscfs/specfs/specdev.h> 
 103 #include <nfs/rpcv2.h> 
 104 #include <nfs/nfsproto.h> 
 105 #include <nfs/nfsnode.h> 
 106 #include <nfs/nfsmount.h> 
 108 #include <vfs/vfs_journal.h> 
 110 #include <mach/mach_types.h> 
 112 #include <kern/zalloc.h> 
 113 #include <kern/kalloc.h> 
 115 void kmeminit(void) __attribute__((section("__TEXT, initcode"))); 
 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) 
 126 const char *memname
[] = { 
 127         "free",         /* 0 M_FREE */ 
 128         "mbuf",         /* 1 M_MBUF */ 
 129         "devbuf",       /* 2 M_DEVBUF */  
 130         "socket",       /* 3 M_SOCKET */  
 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 */  
 154         "",                             /* 22 M_NFSREQ */  
 155         "",                             /* 23 M_NFSMNT */  
 156         "",                             /* 24 M_NFSNODE */  
 158         "vnodes",               /* 25 M_VNODE */  
 159         "namecache",    /* 26 M_CACHE */  
 161         "UFS quota",    /* 27 M_DQUOT */  
 165         "",                             /* 28 M_UFSMNT */  
 166 #if (SYSV_SEM || SYSV_MSG || SYSV_SHM) 
 167         "shm",                  /* 29 M_SHM */  
 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 */  
 186         "",                             /* 45 M_FFSNODE */  
 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 */  
 196         "",                             /* 50 M_NFSSVC */  
 197         "",                             /* 51 M_NFSUID */  
 200         "ip_moptions",  /* 53 M_IPMOPTS */  
 201         "in_multi",             /* 54 M_IPMADDR */  
 202         "ether_multi",  /* 55 M_IFMADDR */  
 203         "mrt",                  /* 56 M_MRTABLE */  
 204         "",             /* 57 unused entry */  
 205         "",             /* 58 unused entry */  
 206 #if (NFSCLIENT || NFSSERVER) 
 207         "NFSV3 srvdesc",/* 59 M_NFSRVDESC */  
 208         "NFSV3 diroff", /* 60 M_NFSDIROFF */  
 209         "NFSV3 bigfh",  /* 61 M_NFSBIGFH */  
 211         "",                             /* 59 M_NFSRVDESC */  
 212         "",                             /* 60 M_NFSDIROFF */  
 213         "",                             /* 61 M_NFSBIGFH */  
 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 */  
 229         "HFS mount",    /* 75 M_HFSMNT */  
 230         "HFS node",             /* 76 M_HFSNODE */  
 231         "HFS fork",             /* 77 M_HFSFORK */  
 233         "",                             /* 75 M_HFSMNT */  
 234         "",                             /* 76 M_HFSNODE */  
 235         "",                             /* 77 M_HFSFORK */  
 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 */  
 246         "IPv6 NDP",             /* 86 M_IP6NDP */  
 247         "IPv6 options", /* 87 M_IP6OPT */  
 248         "IPv6 Misc",    /* 88 M_IP6MISC */ 
 250         "",                             /* 86 M_IP6NDP */  
 251         "",                             /* 87 M_IP6OPT */  
 252         "",                             /* 88 M_IP6MISC */ 
 254         "TCP Segment Q",/* 89 M_TSEGQ */ 
 255         "IGMP state",   /* 90 M_IGMP */ 
 257         "Journal",              /* 91 M_JNL_JNL */ 
 258         "Transaction",  /* 92 M_JNL_TR */ 
 260         "",                     /* 91 M_JNL_JNL */ 
 261         "",                     /* 92 M_JNL_TR */ 
 263         "specinfo",             /* 93 M_SPECINFO */ 
 264         "kqueue",               /* 94 M_KQUEUE */ 
 266         "HFS dirhint",  /* 95 M_HFSDIRHINT */  
 268         "",                             /* 95 M_HFSDIRHINT */  
 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 */  
 277         "unsafe_fsnode",        /* 102 M_UNSAFEFS */  
 279         "",                     /* 102 M_UNSAFEFS */  
 280 #endif /* __LP64__ */ 
 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 */ 
 287         "traffic_mgt",   /* 108 M_TRAFFIC_MGT */ 
 289         "", /* 108 M_TRAFFIC_MGT */ 
 292         "decmpfs_cnode",/* 109 M_DECMPFS_CNODE */ 
 294         "",             /* 109 M_DECMPFS_CNODE */ 
 295 #endif /* HFS_COMPRESSION */ 
 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 */ 
 303 /* for use with kmzones.kz_zalloczone */ 
 304 #define KMZ_CREATEZONE_ACCT     ((void *)-3) 
 305 #define KMZ_CREATEZONE          ((void *)-2) 
 306 #define KMZ_LOOKUPZONE          ((void *)-1) 
 307 #define KMZ_MALLOC                      ((void *)0) 
 308 #define KMZ_SHAREZONE           ((void *)1) 
 313         boolean_t       kz_noencrypt
; 
 314 } kmzones
[M_LAST
] = { 
 315 #define SOS(sname)      sizeof (struct sname) 
 316 #define SOX(sname)      -1 
 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 */ 
 339 #if (NFSCLIENT || NFSSERVER) 
 340         { SOS(nfsreq
),  KMZ_CREATEZONE
, FALSE 
},        /* 22 M_NFSREQ */ 
 341         { SOS(nfsmount
),KMZ_CREATEZONE
, FALSE 
},        /* 23 M_NFSMNT */ 
 342         { SOS(nfsnode
), KMZ_CREATEZONE
, FALSE 
},        /* 24 M_NFSNODE */ 
 344         { 0,            KMZ_MALLOC
, FALSE 
},            /* 22 M_NFSREQ */ 
 345         { 0,            KMZ_MALLOC
, FALSE 
},            /* 23 M_NFSMNT */ 
 346         { 0,            KMZ_MALLOC
, FALSE 
},            /* 24 M_NFSNODE */ 
 348         { SOS(vnode
),   KMZ_CREATEZONE
, TRUE 
},         /* 25 M_VNODE */ 
 349         { SOS(namecache
), KMZ_CREATEZONE
, FALSE 
},      /* 26 M_CACHE */ 
 351         { SOX(dquot
),   KMZ_LOOKUPZONE
, FALSE 
},        /* 27 M_DQUOT */ 
 353         { 0,            KMZ_MALLOC
, FALSE 
},            /* 27 M_DQUOT */ 
 355         { 0,            KMZ_MALLOC
, FALSE 
},            /* 28 M_UFSMNT */ 
 356         { 0,            KMZ_MALLOC
, FALSE 
},            /* 29 M_SHM */ 
 357         { SOS(plimit
),  KMZ_CREATEZONE
, TRUE 
},         /* 30 M_PLIMIT */ 
 358         { SOS(sigacts
), KMZ_CREATEZONE_ACCT
, TRUE 
},    /* 31 M_SIGACTS */ 
 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 */ 
 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 */ 
 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 */ 
 377 #if (NFSCLIENT || NFSSERVER) 
 379                         KMZ_CREATEZONE_ACCT
, FALSE 
},   /* 50 M_NFSSVC */ 
 380         { 0,            KMZ_MALLOC
, FALSE 
},            /* 51 M_NFSUID */ 
 382                         KMZ_CREATEZONE_ACCT
, FALSE 
},   /* 52 M_NFSD */ 
 384         { 0,            KMZ_MALLOC
, FALSE 
},            /* 50 M_NFSSVC */ 
 385         { 0,            KMZ_MALLOC
, FALSE 
},            /* 51 M_NFSUID */ 
 386         { 0,            KMZ_MALLOC
, FALSE 
},            /* 52 M_NFSD */ 
 389                         KMZ_LOOKUPZONE
, FALSE 
},        /* 53 M_IPMOPTS */ 
 390         { SOX(in_multi
),KMZ_LOOKUPZONE
, FALSE 
},        /* 54 M_IPMADDR */ 
 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 */ 
 396 #if (NFSCLIENT || NFSSERVER) 
 397         { SOS(nfsrv_descript
), 
 398                         KMZ_CREATEZONE_ACCT
, FALSE 
},   /* 59 M_NFSRVDESC */ 
 399         { SOS(nfsdmap
), KMZ_CREATEZONE
, FALSE 
},        /* 60 M_NFSDIROFF */ 
 400         { SOS(fhandle
), KMZ_LOOKUPZONE
, FALSE 
},        /* 61 M_NFSBIGFH */ 
 402         { 0,            KMZ_MALLOC
, FALSE 
},            /* 59 M_NFSRVDESC */ 
 403         { 0,            KMZ_MALLOC
, FALSE 
},            /* 60 M_NFSDIROFF */ 
 404         { 0,            KMZ_MALLOC
, FALSE 
},            /* 61 M_NFSBIGFH */ 
 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 */ 
 416         { 0,            KMZ_MALLOC
, TRUE 
},             /* 72 M_BUFHDR */ 
 417         { (NDFILE 
* OFILESIZE
), 
 418                         KMZ_CREATEZONE_ACCT
, FALSE 
},   /* 73 M_OFILETABL */ 
 419         { MCLBYTES
,     KMZ_CREATEZONE
, FALSE 
},        /* 74 M_MCLUST */ 
 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 */ 
 425         { 0,            KMZ_MALLOC
, FALSE 
},            /* 75 M_HFSMNT */ 
 426         { 0,            KMZ_MALLOC
, FALSE 
},            /* 76 M_HFSNODE */ 
 427         { 0,            KMZ_MALLOC
, FALSE 
},            /* 77 M_HFSFORK */ 
 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 */ 
 443         { SOS(journal
), KMZ_CREATEZONE
, FALSE 
},        /* 91 M_JNL_JNL */ 
 444         { SOS(transaction
), KMZ_CREATEZONE
, FALSE 
},    /* 92 M_JNL_TR */ 
 446         { 0,            KMZ_MALLOC
, FALSE 
},            /* 91 M_JNL_JNL */ 
 447         { 0,            KMZ_MALLOC
, FALSE 
},            /* 92 M_JNL_TR */ 
 449         { SOS(specinfo
),KMZ_CREATEZONE
, TRUE 
},         /* 93 M_SPECINFO */ 
 450         { SOS(kqueue
),  KMZ_CREATEZONE
, FALSE 
},        /* 94 M_KQUEUE */ 
 452         { SOS(directoryhint
), KMZ_CREATEZONE
, TRUE 
},   /* 95 M_HFSDIRHINT */ 
 454         { 0,            KMZ_MALLOC
, FALSE 
},            /* 95 M_HFSDIRHINT */ 
 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 */ 
 463         { SOS(unsafe_fsnode
),KMZ_CREATEZONE
, TRUE 
},    /* 102 M_UNSAFEFS */ 
 465         { 0,            KMZ_MALLOC
, FALSE 
},            /* 102 M_UNSAFEFS */ 
 466 #endif /* __LP64__ */ 
 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 */ 
 474         { SOS(decmpfs_cnode
),KMZ_CREATEZONE 
, FALSE
},   /* 109 M_DECMPFS_CNODE */ 
 476         { 0,            KMZ_MALLOC
, FALSE 
},            /* 109 M_DECMPFS_CNODE */ 
 477 #endif /* HFS_COMPRESSION */ 
 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 */ 
 487 extern zone_t 
kalloc_zone(vm_size_t
);   /* XXX */ 
 490  * Initialize the kernel memory allocator 
 497         if ((sizeof(kmzones
)/sizeof(kmzones
[0])) != (sizeof(memname
)/sizeof(memname
[0]))) { 
 498                 panic("kmeminit: kmzones has %lu elements but memname has %lu\n", 
 499                           (sizeof(kmzones
)/sizeof(kmzones
[0])), (sizeof(memname
)/sizeof(memname
[0]))); 
 503         while (kmz 
< &kmzones
[M_LAST
]) { 
 505                 if (kmz
->kz_elemsize 
== (size_t)(-1)) 
 509                 if (kmz
->kz_zalloczone 
== KMZ_CREATEZONE 
|| 
 510                     kmz
->kz_zalloczone 
== KMZ_CREATEZONE_ACCT
) { 
 511                         kmz
->kz_zalloczone 
= zinit(kmz
->kz_elemsize
, 
 512                                                 1024 * 1024, PAGE_SIZE
, 
 513                                                 memname
[kmz 
- kmzones
]); 
 514                         zone_change(kmz
->kz_zalloczone
, Z_CALLERACCT
, 
 515                                     (kmz
->kz_zalloczone 
== KMZ_CREATEZONE_ACCT
)); 
 517                         if (kmz
->kz_noencrypt 
== TRUE
) 
 518                                 zone_change(kmz
->kz_zalloczone
, Z_NOENCRYPT
, TRUE
); 
 520                 else if (kmz
->kz_zalloczone 
== KMZ_LOOKUPZONE
) 
 521                         kmz
->kz_zalloczone 
= kalloc_zone(kmz
->kz_elemsize
); 
 527         while (kmz 
< &kmzones
[M_LAST
]) { 
 529                 if (kmz
->kz_elemsize 
== (size_t)(-1)) 
 533                 if (kmz
->kz_zalloczone 
== KMZ_SHAREZONE
) { 
 535                                 kmzones
[kmz
->kz_elemsize
].kz_zalloczone
; 
 537                                 kmzones
[kmz
->kz_elemsize
].kz_elemsize
; 
 556         size_t          memsize 
= sizeof (*hdr
) + size
; 
 559                 panic("_malloc TYPE"); 
 564         if (flags 
& M_NOWAIT
) { 
 565                 hdr 
= (void *)kalloc_noblock(memsize
); 
 567                 hdr 
= (void *)kalloc(memsize
); 
 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. 
 581                         panic("_MALLOC: kalloc returned NULL (potential leak), size %llu", (uint64_t) size
); 
 590                 bzero(hdr
->dat
, size
); 
 606                 return; /* correct (convenient bsd kernel legacy) */ 
 609         kfree(hdr
, hdr
->mlen
); 
 623         /* realloc(NULL, ...) is equivalent to malloc(...) */ 
 625                 return (_MALLOC(size
, type
, flags
)); 
 627         /* Allocate a new, bigger (or smaller) block */ 
 628         if ((newaddr 
= _MALLOC(size
, type
, flags
)) == NULL
) 
 633         alloc 
= hdr
->mlen 
- sizeof (*hdr
); 
 635         /* Copy over original contents */ 
 636         bcopy(addr
, newaddr
, MIN(size
, alloc
)); 
 652                 panic("_malloc_zone TYPE"); 
 654         kmz 
= &kmzones
[type
]; 
 655         if (kmz
->kz_zalloczone 
== KMZ_MALLOC
) 
 656                 panic("_malloc_zone ZONE: type = %d", type
); 
 659         if (kmz
->kz_elemsize 
== (size_t)(-1)) 
 660                 panic("_malloc_zone XXX"); 
 662         if (size 
== kmz
->kz_elemsize
) 
 663                 if (flags 
& M_NOWAIT
) { 
 664                         elem 
= (void *)zalloc_noblock(kmz
->kz_zalloczone
); 
 666                         elem 
= (void *)zalloc(kmz
->kz_zalloczone
); 
 669                 if (flags 
& M_NOWAIT
) { 
 670                         elem 
= (void *)kalloc_noblock(size
); 
 672                         elem 
= (void *)kalloc(size
); 
 689         kmz 
= &kmzones
[type
]; 
 690         if (kmz
->kz_zalloczone 
== KMZ_MALLOC
) 
 691                 panic("free_zone ZONE"); 
 694         if (kmz
->kz_elemsize 
== (size_t)(-1)) 
 695                 panic("FREE_SIZE XXX"); 
 697         if (size 
== kmz
->kz_elemsize
) 
 698                 zfree(kmz
->kz_zalloczone
, elem
); 
 705 SYSCTL_DECL(_kern_zleak
); 
 706 SYSCTL_NODE(_kern
, OID_AUTO
, zleak
, CTLFLAG_RW 
| CTLFLAG_LOCKED
, 0, "zleak"); 
 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. 
 715 sysctl_zleak_active SYSCTL_HANDLER_ARGS
 
 717 #pragma unused(arg1, arg2) 
 718         int oldval
, val
, error
; 
 720         val 
= oldval 
= get_zleak_state(); 
 721         error 
= sysctl_handle_int(oidp
, &val
, 0, req
); 
 722         if (error 
|| !req
->newptr
) 
 725          * Can only be activated if it's off (and not failed.) 
 726          * Cannot be deactivated once it's on. 
 728         if (val 
== 1 && oldval 
== 0) { 
 729                 kern_return_t kr 
= zleak_activate(); 
 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"); 
 741 SYSCTL_PROC(_kern_zleak
, OID_AUTO
, active
, 
 742     CTLTYPE_INT 
| CTLFLAG_RW 
| CTLFLAG_LOCKED
, 
 743     0, 0, sysctl_zleak_active
, "I", "zleak activity"); 
 746  * kern.zleak.max_zonemap_size 
 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. 
 753 sysctl_zleak_max_zonemap_size SYSCTL_HANDLER_ARGS
 
 755         uint64_t zmap_max_size 
= *(vm_size_t 
*)arg1
; 
 757         return sysctl_handle_quad(oidp
, &zmap_max_size
, arg2
, req
); 
 760 SYSCTL_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"); 
 767 sysctl_zleak_threshold SYSCTL_HANDLER_ARGS
 
 769 #pragma unused(oidp, arg2) 
 771         uint64_t value 
= *(vm_size_t 
*)arg1
; 
 773         error 
= sysctl_io_number(req
, value
, sizeof (value
), &value
, NULL
); 
 775         if (error 
|| !req
->newptr
) 
 778         if (value 
> (uint64_t)zleak_max_zonemap_size
) 
 781         *(vm_size_t 
*)arg1 
= value
; 
 786  * kern.zleak.global_threshold 
 788  * Set the global zleak threshold size (in bytes).  If the zone map 
 789  * grows larger than this value, zleaks are automatically activated. 
 791  * The default value is set in zleak_init(). 
 793 SYSCTL_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"); 
 799  * kern.zleak.zone_threshold 
 801  * Set the per-zone threshold size (in bytes) above which any 
 802  * zone will automatically start zleak tracking. 
 804  * The default value is set in zleak_init(). 
 806  * Setting this variable will have no effect until zleak tracking is 
 807  * activated (See above.) 
 809 SYSCTL_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"); 
 814 #endif  /* CONFIG_ZLEAKS */