]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/unix_startup.c
91f81d6db162651d3080019bd96d20219746caa6
[apple/xnu.git] / bsd / dev / unix_startup.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1992,7 NeXT Computer, Inc.
32 *
33 * Unix data structure initialization.
34 *
35 */
36
37 #include <mach/mach_types.h>
38
39 #include <vm/vm_kern.h>
40 #include <mach/vm_prot.h>
41
42 #include <sys/param.h>
43 #include <sys/buf_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 <dev/ppc/cons.h>
51
52 extern vm_map_t mb_map;
53
54 extern u_long tcp_sendspace;
55 extern u_long tcp_recvspace;
56
57 void bsd_bufferinit(void);
58 extern void md_prepare_for_shutdown(int, int, char *);
59
60 int bsd_mbuf_cluster_reserve(void);
61
62 /*
63 * Declare these as initialized data so we can patch them.
64 */
65
66 #ifdef NBUF
67 int max_nbuf_headers = NBUF;
68 int niobuf = NBUF / 2;
69 int nbuf_hashelements = NBUF;
70 int nbuf = NBUF;
71 #else
72 int max_nbuf_headers = 0;
73 int niobuf = 0;
74 int nbuf_hashelements = 0;
75 int nbuf = 0;
76 #endif
77
78 SYSCTL_INT (_kern, OID_AUTO, nbuf, CTLFLAG_RD, &nbuf, 0, "");
79 SYSCTL_INT (_kern, OID_AUTO, maxnbuf, CTLFLAG_RW, &max_nbuf_headers, 0, "");
80
81 __private_extern__ int customnbuf = 0;
82 int srv = 0; /* Flag indicates a server boot when set */
83 int ncl = 0;
84
85 vm_map_t buffer_map;
86 vm_map_t bufferhdr_map;
87
88
89 extern void bsd_startupearly(void);
90
91 void
92 bsd_startupearly(void)
93 {
94 vm_offset_t firstaddr;
95 vm_size_t size;
96 kern_return_t ret;
97
98 /* clip the number of buf headers upto 16k */
99 if (max_nbuf_headers == 0)
100 max_nbuf_headers = atop(sane_size / 50); /* Get 2% of ram, but no more than we can map */
101 if ((customnbuf == 0) && (max_nbuf_headers > 16384))
102 max_nbuf_headers = 16384;
103 if (max_nbuf_headers < 256)
104 max_nbuf_headers = 256;
105
106 /* clip the number of hash elements to 200000 */
107 if ( (customnbuf == 0 ) && nbuf_hashelements == 0) {
108 nbuf_hashelements = atop(sane_size / 50);
109 if (nbuf_hashelements > 200000)
110 nbuf_hashelements = 200000;
111 } else
112 nbuf_hashelements = max_nbuf_headers;
113
114 if (niobuf == 0)
115 niobuf = max_nbuf_headers;
116 if (niobuf > 4096)
117 niobuf = 4096;
118 if (niobuf < 128)
119 niobuf = 128;
120
121 size = (max_nbuf_headers + niobuf) * sizeof(struct buf);
122 size = round_page(size);
123
124 ret = kmem_suballoc(kernel_map,
125 &firstaddr,
126 size,
127 FALSE,
128 VM_FLAGS_ANYWHERE,
129 &bufferhdr_map);
130
131 if (ret != KERN_SUCCESS)
132 panic("Failed to create bufferhdr_map");
133
134 ret = kernel_memory_allocate(bufferhdr_map,
135 &firstaddr,
136 size,
137 0,
138 KMA_HERE | KMA_KOBJECT);
139
140 if (ret != KERN_SUCCESS)
141 panic("Failed to allocate bufferhdr_map");
142
143 buf = (struct buf *) firstaddr;
144 bzero(buf, size);
145
146 {
147 int scale;
148
149 nmbclusters = bsd_mbuf_cluster_reserve() / MCLBYTES;
150
151 if ((scale = nmbclusters / NMBCLUSTERS) > 1) {
152 tcp_sendspace *= scale;
153 tcp_recvspace *= scale;
154
155 if (tcp_sendspace > (32 * 1024))
156 tcp_sendspace = 32 * 1024;
157 if (tcp_recvspace > (32 * 1024))
158 tcp_recvspace = 32 * 1024;
159 }
160 }
161
162 /*
163 * Size vnodes based on memory
164 * Number vnodes is (memsize/64k) + 1024
165 * This is the calculation that is used by launchd in tiger
166 * we are clipping the max based on 16G
167 * ie ((16*1024*1024*1024)/(64 *1024)) + 1024 = 263168;
168 */
169 desiredvnodes = (sane_size/65536) + 1024;
170 if (desiredvnodes > 263168)
171 desiredvnodes = 263168;
172 }
173
174 void
175 bsd_bufferinit(void)
176 {
177 kern_return_t ret;
178
179 cons.t_dev = makedev(12, 0);
180
181 bsd_startupearly();
182
183 ret = kmem_suballoc(kernel_map,
184 (vm_offset_t *) & mbutl,
185 (vm_size_t) (nmbclusters * MCLBYTES),
186 FALSE,
187 VM_FLAGS_ANYWHERE,
188 &mb_map);
189
190 if (ret != KERN_SUCCESS)
191 panic("Failed to allocate mb_map\n");
192
193 /*
194 * Set up buffers, so they can be used to read disk labels.
195 */
196 bufinit();
197 }
198
199 /*
200 * this has been broken out into a separate routine that
201 * can be called from the x86 early vm initialization to
202 * determine how much lo memory to reserve on systems with
203 * DMA hardware that can't fully address all of the physical
204 * memory that is present.
205 */
206 int
207 bsd_mbuf_cluster_reserve(void)
208 {
209 if (sane_size > (64 * 1024 * 1024) || ncl) {
210
211 if ((nmbclusters = ncl) == 0) {
212 if ((nmbclusters = ((sane_size / 16)/MCLBYTES)) > 32768)
213 nmbclusters = 32768;
214 }
215 }
216
217 return (nmbclusters * MCLBYTES);
218 }