]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/unix_startup.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1992,7 NeXT Computer, Inc.
31 * Unix data structure initialization.
35 #include <mach/mach_types.h>
37 #include <vm/vm_kern.h>
38 #include <mach/vm_prot.h>
40 #include <sys/param.h>
41 #include <sys/buf_internal.h>
42 #include <sys/clist.h>
44 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 #include <sys/sysctl.h>
48 #include <dev/ppc/cons.h>
49 #include <pexpert/pexpert.h>
51 extern vm_map_t mb_map
;
54 extern u_long tcp_sendspace
;
55 extern u_long tcp_recvspace
;
58 void bsd_bufferinit(void) __attribute__((section("__TEXT, initcode")));
59 extern void md_prepare_for_shutdown(int, int, char *);
61 int bsd_mbuf_cluster_reserve(void);
64 * Declare these as initialized data so we can patch them.
68 int max_nbuf_headers
= NBUF
;
69 int niobuf_headers
= NBUF
/ 2;
70 int nbuf_hashelements
= NBUF
;
71 int nbuf_headers
= NBUF
;
73 int max_nbuf_headers
= 0;
74 int niobuf_headers
= 0;
75 int nbuf_hashelements
= 0;
79 SYSCTL_INT (_kern
, OID_AUTO
, nbuf
, CTLFLAG_RD
, &nbuf_headers
, 0, "");
80 SYSCTL_INT (_kern
, OID_AUTO
, maxnbuf
, CTLFLAG_RW
, &max_nbuf_headers
, 0, "");
82 __private_extern__
int customnbuf
= 0;
83 int srv
= 0; /* Flag indicates a server boot when set */
85 static unsigned int mbuf_poolsz
;
88 vm_map_t bufferhdr_map
;
91 extern void bsd_startupearly(void) __attribute__((section("__TEXT, initcode")));
94 bsd_startupearly(void)
96 vm_offset_t firstaddr
;
100 /* clip the number of buf headers upto 16k */
101 if (max_nbuf_headers
== 0)
102 max_nbuf_headers
= atop(sane_size
/ 50); /* Get 2% of ram, but no more than we can map */
103 if ((customnbuf
== 0) && (max_nbuf_headers
> 16384))
104 max_nbuf_headers
= 16384;
105 if (max_nbuf_headers
< CONFIG_MIN_NBUF
)
106 max_nbuf_headers
= CONFIG_MIN_NBUF
;
108 /* clip the number of hash elements to 200000 */
109 if ( (customnbuf
== 0 ) && nbuf_hashelements
== 0) {
110 nbuf_hashelements
= atop(sane_size
/ 50);
111 if (nbuf_hashelements
> 200000)
112 nbuf_hashelements
= 200000;
114 nbuf_hashelements
= max_nbuf_headers
;
116 if (niobuf_headers
== 0)
117 niobuf_headers
= max_nbuf_headers
;
118 if (niobuf_headers
> 4096)
119 niobuf_headers
= 4096;
120 if (niobuf_headers
< CONFIG_MIN_NIOBUF
)
121 niobuf_headers
= CONFIG_MIN_NIOBUF
;
123 size
= (max_nbuf_headers
+ niobuf_headers
) * sizeof(struct buf
);
124 size
= round_page(size
);
126 ret
= kmem_suballoc(kernel_map
,
133 if (ret
!= KERN_SUCCESS
)
134 panic("Failed to create bufferhdr_map");
136 ret
= kernel_memory_allocate(bufferhdr_map
,
140 KMA_HERE
| KMA_KOBJECT
);
142 if (ret
!= KERN_SUCCESS
)
143 panic("Failed to allocate bufferhdr_map");
145 buf_headers
= (struct buf
*) firstaddr
;
146 bzero(buf_headers
, size
);
150 #if CONFIG_USESOCKTHRESHOLD
151 static const unsigned int maxspace
= 64 * 1024;
153 static const unsigned int maxspace
= 128 * 1024;
157 nmbclusters
= bsd_mbuf_cluster_reserve() / MCLBYTES
;
160 if ((scale
= nmbclusters
/ NMBCLUSTERS
) > 1) {
161 tcp_sendspace
*= scale
;
162 tcp_recvspace
*= scale
;
164 if (tcp_sendspace
> maxspace
)
165 tcp_sendspace
= maxspace
;
166 if (tcp_recvspace
> maxspace
)
167 tcp_recvspace
= maxspace
;
169 #endif /* INET || INET6 */
174 * Size vnodes based on memory
175 * Number vnodes is (memsize/64k) + 1024
176 * This is the calculation that is used by launchd in tiger
177 * we are clipping the max based on 16G
178 * ie ((16*1024*1024*1024)/(64 *1024)) + 1024 = 263168;
179 * CONFIG_VNODES is set to 263168 for "medium" configurations (the default)
180 * but can be smaller or larger.
182 desiredvnodes
= (sane_size
/65536) + 1024;
183 if (desiredvnodes
> CONFIG_VNODES
)
184 desiredvnodes
= CONFIG_VNODES
;
192 cons
.t_dev
= makedev(12, 0);
197 ret
= kmem_suballoc(kernel_map
,
198 (vm_offset_t
*) & mbutl
,
199 (vm_size_t
) (nmbclusters
* MCLBYTES
),
204 if (ret
!= KERN_SUCCESS
)
205 panic("Failed to allocate mb_map\n");
209 * Set up buffers, so they can be used to read disk labels.
214 /* 512 MB hard limit on size of the mbuf pool */
215 #define MAX_MBUF_POOL (512 << MBSHIFT)
216 #define MAX_NCL (MAX_MBUF_POOL >> MCLSHIFT)
219 * this has been broken out into a separate routine that
220 * can be called from the x86 early vm initialization to
221 * determine how much lo memory to reserve on systems with
222 * DMA hardware that can't fully address all of the physical
223 * memory that is present.
226 bsd_mbuf_cluster_reserve(void)
228 /* If called more than once, return the previously calculated size */
229 if (mbuf_poolsz
!= 0)
232 PE_parse_boot_argn("ncl", &ncl
, sizeof (ncl
));
234 if (sane_size
> (64 * 1024 * 1024) || ncl
) {
235 if ((nmbclusters
= ncl
) == 0) {
236 if ((nmbclusters
= ((sane_size
/ 16)/MCLBYTES
)) > 32768)
239 /* Make sure it's not odd in case ncl is manually set */
240 if (nmbclusters
& 0x1)
243 /* And obey the upper limit */
244 if (nmbclusters
> MAX_NCL
)
245 nmbclusters
= MAX_NCL
;
248 mbuf_poolsz
= nmbclusters
<< MCLSHIFT
;
250 return (nmbclusters
* MCLBYTES
);