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