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