]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/unix_startup.c
daef72f73baabf8d71b5e21ea0f9847c05eac504
[apple/xnu.git] / bsd / dev / unix_startup.c
1 /*
2 * Copyright (c) 2000-2004 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/clist.h>
43 #include <sys/mbuf.h>
44 #include <sys/systm.h>
45 #include <sys/tty.h>
46 #include <sys/vnode.h>
47 #include <sys/sysctl.h>
48 #include <dev/ppc/cons.h>
49
50 extern vm_map_t mb_map;
51
52 extern u_long tcp_sendspace;
53 extern u_long tcp_recvspace;
54
55 void bsd_bufferinit(void);
56 extern void md_prepare_for_shutdown(int, int, char *);
57
58 int bsd_mbuf_cluster_reserve(void);
59
60 /*
61 * Declare these as initialized data so we can patch them.
62 */
63
64 #ifdef NBUF
65 int max_nbuf_headers = NBUF;
66 int niobuf = NBUF / 2;
67 int nbuf_hashelements = NBUF;
68 int nbuf = NBUF;
69 #else
70 int max_nbuf_headers = 0;
71 int niobuf = 0;
72 int nbuf_hashelements = 0;
73 int nbuf = 0;
74 #endif
75
76 SYSCTL_INT (_kern, OID_AUTO, nbuf, CTLFLAG_RD, &nbuf, 0, "");
77 SYSCTL_INT (_kern, OID_AUTO, maxnbuf, CTLFLAG_RW, &max_nbuf_headers, 0, "");
78
79 __private_extern__ int customnbuf = 0;
80 int srv = 0; /* Flag indicates a server boot when set */
81 int ncl = 0;
82
83 vm_map_t buffer_map;
84 vm_map_t bufferhdr_map;
85
86
87 extern void bsd_startupearly(void);
88
89 void
90 bsd_startupearly(void)
91 {
92 vm_offset_t firstaddr;
93 vm_size_t size;
94 kern_return_t ret;
95
96 /* clip the number of buf headers upto 16k */
97 if (max_nbuf_headers == 0)
98 max_nbuf_headers = atop(sane_size / 50); /* Get 2% of ram, but no more than we can map */
99 if ((customnbuf == 0) && (max_nbuf_headers > 16384))
100 max_nbuf_headers = 16384;
101 if (max_nbuf_headers < 256)
102 max_nbuf_headers = 256;
103
104 /* clip the number of hash elements to 200000 */
105 if ( (customnbuf == 0 ) && nbuf_hashelements == 0) {
106 nbuf_hashelements = atop(sane_size / 50);
107 if (nbuf_hashelements > 200000)
108 nbuf_hashelements = 200000;
109 } else
110 nbuf_hashelements = max_nbuf_headers;
111
112 if (niobuf == 0)
113 niobuf = max_nbuf_headers;
114 if (niobuf > 4096)
115 niobuf = 4096;
116 if (niobuf < 128)
117 niobuf = 128;
118
119 size = (max_nbuf_headers + niobuf) * sizeof(struct buf);
120 size = round_page(size);
121
122 ret = kmem_suballoc(kernel_map,
123 &firstaddr,
124 size,
125 FALSE,
126 VM_FLAGS_ANYWHERE,
127 &bufferhdr_map);
128
129 if (ret != KERN_SUCCESS)
130 panic("Failed to create bufferhdr_map");
131
132 ret = kernel_memory_allocate(bufferhdr_map,
133 &firstaddr,
134 size,
135 0,
136 KMA_HERE | KMA_KOBJECT);
137
138 if (ret != KERN_SUCCESS)
139 panic("Failed to allocate bufferhdr_map");
140
141 buf = (struct buf *) firstaddr;
142 bzero(buf, size);
143
144 {
145 int scale;
146
147 nmbclusters = bsd_mbuf_cluster_reserve() / MCLBYTES;
148
149 if ((scale = nmbclusters / NMBCLUSTERS) > 1) {
150 tcp_sendspace *= scale;
151 tcp_recvspace *= scale;
152
153 if (tcp_sendspace > (32 * 1024))
154 tcp_sendspace = 32 * 1024;
155 if (tcp_recvspace > (32 * 1024))
156 tcp_recvspace = 32 * 1024;
157 }
158 }
159
160 /*
161 * Size vnodes based on memory
162 * Number vnodes is (memsize/64k) + 1024
163 * This is the calculation that is used by launchd in tiger
164 * we are clipping the max based on 16G
165 * ie ((16*1024*1024*1024)/(64 *1024)) + 1024 = 263168;
166 */
167 desiredvnodes = (sane_size/65536) + 1024;
168 if (desiredvnodes > 263168)
169 desiredvnodes = 263168;
170 }
171
172 void
173 bsd_bufferinit(void)
174 {
175 kern_return_t ret;
176
177 cons.t_dev = makedev(12, 0);
178
179 bsd_startupearly();
180
181 ret = kmem_suballoc(kernel_map,
182 (vm_offset_t *) & mbutl,
183 (vm_size_t) (nmbclusters * MCLBYTES),
184 FALSE,
185 VM_FLAGS_ANYWHERE,
186 &mb_map);
187
188 if (ret != KERN_SUCCESS)
189 panic("Failed to allocate mb_map\n");
190
191 /*
192 * Set up buffers, so they can be used to read disk labels.
193 */
194 bufinit();
195 }
196
197 /*
198 * this has been broken out into a separate routine that
199 * can be called from the x86 early vm initialization to
200 * determine how much lo memory to reserve on systems with
201 * DMA hardware that can't fully address all of the physical
202 * memory that is present.
203 */
204 int
205 bsd_mbuf_cluster_reserve(void)
206 {
207 if (sane_size > (64 * 1024 * 1024) || ncl) {
208
209 if ((nmbclusters = ncl) == 0) {
210 if ((nmbclusters = ((sane_size / 16)/MCLBYTES)) > 32768)
211 nmbclusters = 32768;
212 }
213 }
214
215 return (nmbclusters * MCLBYTES);
216 }