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