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