]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/unix_startup.c
xnu-2050.24.15.tar.gz
[apple/xnu.git] / bsd / dev / unix_startup.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2010 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>
1c79356b 55
6d2010ae 56extern uint32_t kern_maxvnodes;
91447636
A
57extern vm_map_t mb_map;
58
2d21ac55 59#if INET || INET6
b0d623f7
A
60extern uint32_t tcp_sendspace;
61extern uint32_t tcp_recvspace;
2d21ac55 62#endif
91447636 63
2d21ac55 64void bsd_bufferinit(void) __attribute__((section("__TEXT, initcode")));
91447636 65extern void md_prepare_for_shutdown(int, int, char *);
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;
c910b4d9 93static unsigned int mbuf_poolsz;
91447636
A
94
95vm_map_t buffer_map;
96vm_map_t bufferhdr_map;
b0d623f7 97static int vnodes_sized = 0;
91447636 98
2d21ac55 99extern void bsd_startupearly(void) __attribute__((section("__TEXT, initcode")));
1c79356b
A
100
101void
91447636 102bsd_startupearly(void)
1c79356b 103{
91447636
A
104 vm_offset_t firstaddr;
105 vm_size_t size;
106 kern_return_t ret;
1c79356b 107
0c530ab8
A
108 /* clip the number of buf headers upto 16k */
109 if (max_nbuf_headers == 0)
b0d623f7 110 max_nbuf_headers = atop_kernel(sane_size / 50); /* Get 2% of ram, but no more than we can map */
0c530ab8
A
111 if ((customnbuf == 0) && (max_nbuf_headers > 16384))
112 max_nbuf_headers = 16384;
2d21ac55
A
113 if (max_nbuf_headers < CONFIG_MIN_NBUF)
114 max_nbuf_headers = CONFIG_MIN_NBUF;
0c530ab8
A
115
116 /* clip the number of hash elements to 200000 */
117 if ( (customnbuf == 0 ) && nbuf_hashelements == 0) {
b0d623f7 118 nbuf_hashelements = atop_kernel(sane_size / 50);
0c530ab8
A
119 if (nbuf_hashelements > 200000)
120 nbuf_hashelements = 200000;
121 } else
122 nbuf_hashelements = max_nbuf_headers;
1c79356b 123
6d2010ae
A
124 if (niobuf_headers == 0) {
125 if (max_nbuf_headers < 4096)
126 niobuf_headers = max_nbuf_headers;
127 else
128 niobuf_headers = (max_nbuf_headers / 2) + 2048;
129 }
2d21ac55
A
130 if (niobuf_headers < CONFIG_MIN_NIOBUF)
131 niobuf_headers = CONFIG_MIN_NIOBUF;
1c79356b 132
2d21ac55 133 size = (max_nbuf_headers + niobuf_headers) * sizeof(struct buf);
91447636 134 size = round_page(size);
1c79356b
A
135
136 ret = kmem_suballoc(kernel_map,
91447636
A
137 &firstaddr,
138 size,
139 FALSE,
140 VM_FLAGS_ANYWHERE,
141 &bufferhdr_map);
1c79356b 142
91447636 143 if (ret != KERN_SUCCESS)
1c79356b 144 panic("Failed to create bufferhdr_map");
91447636 145
1c79356b 146 ret = kernel_memory_allocate(bufferhdr_map,
91447636
A
147 &firstaddr,
148 size,
149 0,
150 KMA_HERE | KMA_KOBJECT);
1c79356b
A
151
152 if (ret != KERN_SUCCESS)
153 panic("Failed to allocate bufferhdr_map");
154
2d21ac55
A
155 buf_headers = (struct buf *) firstaddr;
156 bzero(buf_headers, size);
1c79356b 157
2d21ac55 158#if SOCKETS
0c530ab8 159 {
593a1d5f 160 static const unsigned int maxspace = 128 * 1024;
91447636 161 int scale;
1c79356b 162
0b4c1975 163 nmbclusters = bsd_mbuf_cluster_reserve(NULL) / MCLBYTES;
0c530ab8 164
2d21ac55 165#if INET || INET6
1c79356b
A
166 if ((scale = nmbclusters / NMBCLUSTERS) > 1) {
167 tcp_sendspace *= scale;
168 tcp_recvspace *= scale;
169
593a1d5f
A
170 if (tcp_sendspace > maxspace)
171 tcp_sendspace = maxspace;
172 if (tcp_recvspace > maxspace)
173 tcp_recvspace = maxspace;
1c79356b 174 }
2d21ac55 175#endif /* INET || INET6 */
1c79356b 176 }
2d21ac55 177#endif /* SOCKETS */
0c530ab8 178
b0d623f7 179 if (vnodes_sized == 0) {
6d2010ae
A
180 if (!PE_get_default("kern.maxvnodes", &desiredvnodes, sizeof(desiredvnodes))) {
181 /*
182 * Size vnodes based on memory
183 * Number vnodes is (memsize/64k) + 1024
184 * This is the calculation that is used by launchd in tiger
185 * we are clipping the max based on 16G
186 * ie ((16*1024*1024*1024)/(64 *1024)) + 1024 = 263168;
187 * CONFIG_VNODES is set to 263168 for "medium" configurations (the default)
188 * but can be smaller or larger.
189 */
190 desiredvnodes = (sane_size/65536) + 1024;
191#ifdef CONFIG_VNODES
192 if (desiredvnodes > CONFIG_VNODES)
193 desiredvnodes = CONFIG_VNODES;
194#endif
195 }
196 vnodes_sized = 1;
b0d623f7 197 }
1c79356b
A
198}
199
200void
91447636 201bsd_bufferinit(void)
1c79356b 202{
91447636 203 kern_return_t ret;
1c79356b 204
b0d623f7
A
205 /*
206 * Note: Console device initialized in kminit() from bsd_autoconf()
207 * prior to call to us in bsd_init().
208 */
1c79356b
A
209
210 bsd_startupearly();
211
2d21ac55 212#if SOCKETS
91447636
A
213 ret = kmem_suballoc(kernel_map,
214 (vm_offset_t *) & mbutl,
215 (vm_size_t) (nmbclusters * MCLBYTES),
216 FALSE,
217 VM_FLAGS_ANYWHERE,
218 &mb_map);
1c79356b 219
91447636 220 if (ret != KERN_SUCCESS)
1c79356b 221 panic("Failed to allocate mb_map\n");
2d21ac55 222#endif /* SOCKETS */
0b4e3aa0 223
91447636
A
224 /*
225 * Set up buffers, so they can be used to read disk labels.
226 */
227 bufinit();
0b4e3aa0 228}
0c530ab8 229
b0d623f7
A
230/* 512 MB (K32) or 2 GB (K64) hard limit on size of the mbuf pool */
231#if !defined(__LP64__)
232#define MAX_MBUF_POOL (512 << MBSHIFT)
233#else
234#define MAX_MBUF_POOL (2ULL << GBSHIFT)
235#endif /* !__LP64__ */
236#define MAX_NCL (MAX_MBUF_POOL >> MCLSHIFT)
2d21ac55 237
0c530ab8
A
238/*
239 * this has been broken out into a separate routine that
240 * can be called from the x86 early vm initialization to
241 * determine how much lo memory to reserve on systems with
242 * DMA hardware that can't fully address all of the physical
243 * memory that is present.
244 */
b0d623f7 245unsigned int
0b4c1975 246bsd_mbuf_cluster_reserve(boolean_t *overridden)
0c530ab8 247{
b0d623f7 248 int mbuf_pool = 0;
0b4c1975 249 static boolean_t was_overridden = FALSE;
b0d623f7 250
c910b4d9 251 /* If called more than once, return the previously calculated size */
b0d623f7
A
252 if (mbuf_poolsz != 0)
253 goto done;
c910b4d9 254
b0d623f7
A
255 /*
256 * Some of these are parsed in parse_bsd_args(), but for x86 we get
257 * here early from i386_vm_init() and so we parse them now, in order
258 * to correctly compute the size of the low-memory VM pool. It is
259 * redundant but rather harmless.
260 */
b0d623f7
A
261 (void) PE_parse_boot_argn("ncl", &ncl, sizeof (ncl));
262 (void) PE_parse_boot_argn("mbuf_pool", &mbuf_pool, sizeof (mbuf_pool));
263
264 /*
265 * Convert "mbuf_pool" from MB to # of 2KB clusters; it is
266 * equivalent to "ncl", except that it uses different unit.
267 */
268 if (mbuf_pool != 0)
269 ncl = (mbuf_pool << MBSHIFT) >> MCLSHIFT;
0c530ab8 270
b0d623f7 271 if (sane_size > (64 * 1024 * 1024) || ncl != 0) {
0b4c1975 272
6d2010ae 273 if (ncl || serverperfmode)
0b4c1975
A
274 was_overridden = TRUE;
275
0c530ab8 276 if ((nmbclusters = ncl) == 0) {
b0d623f7 277 /* Auto-configure the mbuf pool size */
6d2010ae 278 nmbclusters = mbuf_default_ncl(serverperfmode, sane_size);
b0d623f7
A
279 } else {
280 /* Make sure it's not odd in case ncl is manually set */
281 if (nmbclusters & 0x1)
282 --nmbclusters;
283
284 /* And obey the upper limit */
285 if (nmbclusters > MAX_NCL)
286 nmbclusters = MAX_NCL;
0c530ab8 287 }
6d2010ae
A
288
289 /* Round it down to nearest multiple of 4KB clusters */
290 nmbclusters = P2ROUNDDOWN(nmbclusters, NCLPBG);
c910b4d9
A
291 }
292 mbuf_poolsz = nmbclusters << MCLSHIFT;
293done:
0b4c1975
A
294 if (overridden)
295 *overridden = was_overridden;
296
b0d623f7 297 return (mbuf_poolsz);
0c530ab8 298}
b0d623f7
A
299#if defined(__LP64__)
300extern int tcp_tcbhashsize;
301extern int max_cached_sock_count;
b0d623f7
A
302#endif
303
304
305void
6d2010ae 306bsd_scale_setup(int scale)
b0d623f7
A
307{
308#if defined(__LP64__)
6d2010ae
A
309 if ((scale > 0) && (serverperfmode == 0)) {
310 maxproc *= scale;
311 maxprocperuid = (maxproc * 2) / 3;
312 }
313 /* Apply server scaling rules */
314 if ((scale > 0) && (serverperfmode !=0)) {
b0d623f7
A
315 maxproc = 2500 * scale;
316 hard_maxproc = maxproc;
317 /* no fp usage */
318 maxprocperuid = (maxproc*3)/4;
319 maxfiles = (150000 * scale);
320 maxfilesperproc = maxfiles/2;
321 desiredvnodes = maxfiles;
322 vnodes_sized = 1;
323 if (scale > 4) {
324 /* clip them at 32G level */
325 somaxconn = 2048;
326 /* 64G or more the hash size is 32k */
327 if (scale > 7) {
328 /* clip at 64G level */
329 tcp_tcbhashsize = 16 *1024;
330 max_cached_sock_count = 165000;
331 } else {
332 tcp_tcbhashsize = 32 *1024;
333 max_cached_sock_count = 60000 + ((scale-1) * 15000);
334 }
335 } else {
336 somaxconn = 512*scale;
337 tcp_tcbhashsize = 4*1024*scale;
338 max_cached_sock_count = 60000 + ((scale-1) * 15000);
339 }
340 }
341#endif
342 bsd_exec_setup(scale);
343}
344