]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/unix_startup.c
xnu-3789.31.2.tar.gz
[apple/xnu.git] / bsd / dev / unix_startup.c
CommitLineData
1c79356b 1/*
fe8ab488 2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55
A
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1992,7 NeXT Computer, Inc.
30 *
31 * Unix data structure initialization.
91447636 32 *
1c79356b
A
33 */
34
35#include <mach/mach_types.h>
36
37#include <vm/vm_kern.h>
38#include <mach/vm_prot.h>
39
40#include <sys/param.h>
91447636 41#include <sys/buf_internal.h>
b0d623f7
A
42#include <sys/file_internal.h>
43#include <sys/proc_internal.h>
1c79356b 44#include <sys/clist.h>
6d2010ae 45#include <sys/mcache.h>
1c79356b
A
46#include <sys/mbuf.h>
47#include <sys/systm.h>
48#include <sys/tty.h>
0c530ab8
A
49#include <sys/vnode.h>
50#include <sys/sysctl.h>
b0d623f7 51#include <machine/cons.h>
c910b4d9 52#include <pexpert/pexpert.h>
b0d623f7 53#include <sys/socketvar.h>
6d2010ae 54#include <pexpert/pexpert.h>
3e170ce0 55#include <netinet/tcp_var.h>
1c79356b 56
6d2010ae 57extern uint32_t kern_maxvnodes;
91447636
A
58extern vm_map_t mb_map;
59
2d21ac55 60#if INET || INET6
b0d623f7
A
61extern uint32_t tcp_sendspace;
62extern uint32_t tcp_recvspace;
2d21ac55 63#endif
91447636 64
39236c6e 65void bsd_bufferinit(void);
1c79356b 66
0b4c1975 67unsigned int bsd_mbuf_cluster_reserve(boolean_t *);
6d2010ae 68void bsd_scale_setup(int);
b0d623f7 69void bsd_exec_setup(int);
0c530ab8 70
1c79356b
A
71/*
72 * Declare these as initialized data so we can patch them.
73 */
74
75#ifdef NBUF
0c530ab8 76int max_nbuf_headers = NBUF;
6d2010ae 77int niobuf_headers = (NBUF / 2) + 2048;
0c530ab8 78int nbuf_hashelements = NBUF;
2d21ac55 79int nbuf_headers = NBUF;
1c79356b 80#else
0c530ab8 81int max_nbuf_headers = 0;
2d21ac55 82int niobuf_headers = 0;
0c530ab8 83int nbuf_hashelements = 0;
2d21ac55 84int nbuf_headers = 0;
6601e61a 85#endif
4452a7af 86
6d2010ae
A
87SYSCTL_INT (_kern, OID_AUTO, nbuf, CTLFLAG_RD | CTLFLAG_LOCKED, &nbuf_headers, 0, "");
88SYSCTL_INT (_kern, OID_AUTO, maxnbuf, CTLFLAG_RW | CTLFLAG_LOCKED, &max_nbuf_headers, 0, "");
0c530ab8
A
89
90__private_extern__ int customnbuf = 0;
6d2010ae 91int serverperfmode = 0; /* Flag indicates a server boot when set */
91447636 92int ncl = 0;
39236c6e
A
93
94#if SOCKETS
c910b4d9 95static unsigned int mbuf_poolsz;
39236c6e 96#endif
91447636
A
97
98vm_map_t buffer_map;
99vm_map_t bufferhdr_map;
b0d623f7 100static int vnodes_sized = 0;
91447636 101
39236c6e 102extern void bsd_startupearly(void);
1c79356b
A
103
104void
91447636 105bsd_startupearly(void)
1c79356b 106{
91447636
A
107 vm_offset_t firstaddr;
108 vm_size_t size;
109 kern_return_t ret;
1c79356b 110
0c530ab8
A
111 /* clip the number of buf headers upto 16k */
112 if (max_nbuf_headers == 0)
b0d623f7 113 max_nbuf_headers = atop_kernel(sane_size / 50); /* Get 2% of ram, but no more than we can map */
0c530ab8
A
114 if ((customnbuf == 0) && (max_nbuf_headers > 16384))
115 max_nbuf_headers = 16384;
2d21ac55
A
116 if (max_nbuf_headers < CONFIG_MIN_NBUF)
117 max_nbuf_headers = CONFIG_MIN_NBUF;
0c530ab8
A
118
119 /* clip the number of hash elements to 200000 */
120 if ( (customnbuf == 0 ) && nbuf_hashelements == 0) {
b0d623f7 121 nbuf_hashelements = atop_kernel(sane_size / 50);
0c530ab8
A
122 if (nbuf_hashelements > 200000)
123 nbuf_hashelements = 200000;
124 } else
125 nbuf_hashelements = max_nbuf_headers;
1c79356b 126
6d2010ae
A
127 if (niobuf_headers == 0) {
128 if (max_nbuf_headers < 4096)
129 niobuf_headers = max_nbuf_headers;
130 else
131 niobuf_headers = (max_nbuf_headers / 2) + 2048;
132 }
2d21ac55
A
133 if (niobuf_headers < CONFIG_MIN_NIOBUF)
134 niobuf_headers = CONFIG_MIN_NIOBUF;
1c79356b 135
2d21ac55 136 size = (max_nbuf_headers + niobuf_headers) * sizeof(struct buf);
91447636 137 size = round_page(size);
1c79356b
A
138
139 ret = kmem_suballoc(kernel_map,
91447636
A
140 &firstaddr,
141 size,
142 FALSE,
3e170ce0 143 VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_FILE),
91447636 144 &bufferhdr_map);
1c79356b 145
91447636 146 if (ret != KERN_SUCCESS)
1c79356b 147 panic("Failed to create bufferhdr_map");
91447636 148
1c79356b 149 ret = kernel_memory_allocate(bufferhdr_map,
91447636
A
150 &firstaddr,
151 size,
152 0,
3e170ce0
A
153 KMA_HERE | KMA_KOBJECT,
154 VM_KERN_MEMORY_FILE);
1c79356b
A
155
156 if (ret != KERN_SUCCESS)
157 panic("Failed to allocate bufferhdr_map");
158
2d21ac55
A
159 buf_headers = (struct buf *) firstaddr;
160 bzero(buf_headers, size);
1c79356b 161
2d21ac55 162#if SOCKETS
0c530ab8 163 {
593a1d5f 164 static const unsigned int maxspace = 128 * 1024;
91447636 165 int scale;
1c79356b 166
0b4c1975 167 nmbclusters = bsd_mbuf_cluster_reserve(NULL) / MCLBYTES;
0c530ab8 168
2d21ac55 169#if INET || INET6
1c79356b
A
170 if ((scale = nmbclusters / NMBCLUSTERS) > 1) {
171 tcp_sendspace *= scale;
172 tcp_recvspace *= scale;
173
593a1d5f
A
174 if (tcp_sendspace > maxspace)
175 tcp_sendspace = maxspace;
176 if (tcp_recvspace > maxspace)
177 tcp_recvspace = maxspace;
1c79356b 178 }
2d21ac55 179#endif /* INET || INET6 */
1c79356b 180 }
2d21ac55 181#endif /* SOCKETS */
0c530ab8 182
b0d623f7 183 if (vnodes_sized == 0) {
6d2010ae
A
184 if (!PE_get_default("kern.maxvnodes", &desiredvnodes, sizeof(desiredvnodes))) {
185 /*
186 * Size vnodes based on memory
187 * Number vnodes is (memsize/64k) + 1024
188 * This is the calculation that is used by launchd in tiger
189 * we are clipping the max based on 16G
190 * ie ((16*1024*1024*1024)/(64 *1024)) + 1024 = 263168;
191 * CONFIG_VNODES is set to 263168 for "medium" configurations (the default)
192 * but can be smaller or larger.
193 */
194 desiredvnodes = (sane_size/65536) + 1024;
195#ifdef CONFIG_VNODES
196 if (desiredvnodes > CONFIG_VNODES)
197 desiredvnodes = CONFIG_VNODES;
198#endif
199 }
200 vnodes_sized = 1;
b0d623f7 201 }
1c79356b
A
202}
203
204void
91447636 205bsd_bufferinit(void)
1c79356b 206{
39236c6e 207#if SOCKETS
91447636 208 kern_return_t ret;
39236c6e 209#endif
b0d623f7
A
210 /*
211 * Note: Console device initialized in kminit() from bsd_autoconf()
212 * prior to call to us in bsd_init().
213 */
1c79356b
A
214
215 bsd_startupearly();
216
2d21ac55 217#if SOCKETS
91447636 218 ret = kmem_suballoc(kernel_map,
3e170ce0 219 (vm_offset_t *) &mbutl,
91447636
A
220 (vm_size_t) (nmbclusters * MCLBYTES),
221 FALSE,
3e170ce0 222 VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_MBUF),
91447636 223 &mb_map);
1c79356b 224
91447636 225 if (ret != KERN_SUCCESS)
1c79356b 226 panic("Failed to allocate mb_map\n");
2d21ac55 227#endif /* SOCKETS */
0b4e3aa0 228
91447636
A
229 /*
230 * Set up buffers, so they can be used to read disk labels.
231 */
232 bufinit();
0b4e3aa0 233}
0c530ab8 234
b0d623f7
A
235/* 512 MB (K32) or 2 GB (K64) hard limit on size of the mbuf pool */
236#if !defined(__LP64__)
237#define MAX_MBUF_POOL (512 << MBSHIFT)
238#else
239#define MAX_MBUF_POOL (2ULL << GBSHIFT)
240#endif /* !__LP64__ */
241#define MAX_NCL (MAX_MBUF_POOL >> MCLSHIFT)
2d21ac55 242
39236c6e 243#if SOCKETS
0c530ab8
A
244/*
245 * this has been broken out into a separate routine that
246 * can be called from the x86 early vm initialization to
247 * determine how much lo memory to reserve on systems with
248 * DMA hardware that can't fully address all of the physical
249 * memory that is present.
250 */
b0d623f7 251unsigned int
0b4c1975 252bsd_mbuf_cluster_reserve(boolean_t *overridden)
0c530ab8 253{
b0d623f7 254 int mbuf_pool = 0;
0b4c1975 255 static boolean_t was_overridden = FALSE;
b0d623f7 256
c910b4d9 257 /* If called more than once, return the previously calculated size */
b0d623f7
A
258 if (mbuf_poolsz != 0)
259 goto done;
c910b4d9 260
b0d623f7
A
261 /*
262 * Some of these are parsed in parse_bsd_args(), but for x86 we get
263 * here early from i386_vm_init() and so we parse them now, in order
264 * to correctly compute the size of the low-memory VM pool. It is
265 * redundant but rather harmless.
266 */
b0d623f7
A
267 (void) PE_parse_boot_argn("ncl", &ncl, sizeof (ncl));
268 (void) PE_parse_boot_argn("mbuf_pool", &mbuf_pool, sizeof (mbuf_pool));
269
270 /*
271 * Convert "mbuf_pool" from MB to # of 2KB clusters; it is
272 * equivalent to "ncl", except that it uses different unit.
273 */
274 if (mbuf_pool != 0)
275 ncl = (mbuf_pool << MBSHIFT) >> MCLSHIFT;
0c530ab8 276
b0d623f7 277 if (sane_size > (64 * 1024 * 1024) || ncl != 0) {
0b4c1975 278
6d2010ae 279 if (ncl || serverperfmode)
0b4c1975
A
280 was_overridden = TRUE;
281
0c530ab8 282 if ((nmbclusters = ncl) == 0) {
b0d623f7 283 /* Auto-configure the mbuf pool size */
6d2010ae 284 nmbclusters = mbuf_default_ncl(serverperfmode, sane_size);
b0d623f7
A
285 } else {
286 /* Make sure it's not odd in case ncl is manually set */
287 if (nmbclusters & 0x1)
288 --nmbclusters;
289
290 /* And obey the upper limit */
291 if (nmbclusters > MAX_NCL)
292 nmbclusters = MAX_NCL;
0c530ab8 293 }
6d2010ae 294
3e170ce0
A
295 /* Round it down to nearest multiple of PAGE_SIZE */
296 nmbclusters = P2ROUNDDOWN(nmbclusters, NCLPG);
c910b4d9
A
297 }
298 mbuf_poolsz = nmbclusters << MCLSHIFT;
299done:
0b4c1975
A
300 if (overridden)
301 *overridden = was_overridden;
302
b0d623f7 303 return (mbuf_poolsz);
0c530ab8 304}
39236c6e
A
305#endif
306
b0d623f7
A
307#if defined(__LP64__)
308extern int tcp_tcbhashsize;
309extern int max_cached_sock_count;
b0d623f7
A
310#endif
311
312
313void
6d2010ae 314bsd_scale_setup(int scale)
b0d623f7
A
315{
316#if defined(__LP64__)
6d2010ae
A
317 if ((scale > 0) && (serverperfmode == 0)) {
318 maxproc *= scale;
319 maxprocperuid = (maxproc * 2) / 3;
320 }
321 /* Apply server scaling rules */
322 if ((scale > 0) && (serverperfmode !=0)) {
b0d623f7
A
323 maxproc = 2500 * scale;
324 hard_maxproc = maxproc;
325 /* no fp usage */
326 maxprocperuid = (maxproc*3)/4;
327 maxfiles = (150000 * scale);
328 maxfilesperproc = maxfiles/2;
329 desiredvnodes = maxfiles;
330 vnodes_sized = 1;
3e170ce0 331 tcp_tfo_backlog = 100 * scale;
b0d623f7 332 if (scale > 4) {
fe8ab488 333 /* clip somaxconn at 32G level */
b0d623f7 334 somaxconn = 2048;
3e170ce0
A
335 /*
336 * For scale > 4 (> 32G), clip
fe8ab488
A
337 * tcp_tcbhashsize to 32K
338 */
339 tcp_tcbhashsize = 32 *1024;
3e170ce0 340
b0d623f7
A
341 if (scale > 7) {
342 /* clip at 64G level */
b0d623f7
A
343 max_cached_sock_count = 165000;
344 } else {
b0d623f7
A
345 max_cached_sock_count = 60000 + ((scale-1) * 15000);
346 }
347 } else {
348 somaxconn = 512*scale;
349 tcp_tcbhashsize = 4*1024*scale;
350 max_cached_sock_count = 60000 + ((scale-1) * 15000);
351 }
352 }
39037602
A
353
354 if(maxproc > hard_maxproc) {
355 hard_maxproc = maxproc;
356 }
b0d623f7
A
357#endif
358 bsd_exec_setup(scale);
359}
360