alloc tables at boot time version
[unix-history] / usr / src / sys / kern / init_main.c
CommitLineData
0a34b6fd 1/* init_main.c 4.10 %G% */
c4708522
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/dir.h"
6#include "../h/user.h"
7#include "../h/filsys.h"
8#include "../h/mount.h"
9#include "../h/map.h"
10#include "../h/proc.h"
11#include "../h/inode.h"
12#include "../h/seg.h"
13#include "../h/conf.h"
14#include "../h/buf.h"
15#include "../h/mtpr.h"
16#include "../h/pte.h"
17#include "../h/clock.h"
18#include "../h/vm.h"
19#include "../h/cmap.h"
831e498e 20#include "../h/text.h"
00ea40c7 21#include "../h/vlimit.h"
0a34b6fd 22#include "../h/clist.h"
c4708522
BJ
23
24/*
25 * Initialization code.
26 * Called from cold start routine as
27 * soon as a stack and segmentation
28 * have been established.
29 * Functions:
30 * clear and free user core
31 * turn on clock
32 * hand craft 0th process
33 * call all initialization routines
34 * fork - process 0 to schedule
35 * - process 2 to page out
36 * - process 1 execute bootstrap
37 *
38 * loop at loc 13 (0xd) in user mode -- /etc/init
39 * cannot be executed.
40 */
41main(firstaddr)
42{
73248996 43 register int i;
dd4b582c 44 register struct proc *p;
c4708522 45
c4708522 46 rqinit();
c4708522
BJ
47 startup(firstaddr);
48 if (lotsfree == 0)
49 lotsfree = LOTSFREE;
50
51 /*
52 * set up system process 0 (swapper)
53 */
dd4b582c
BJ
54 p = &proc[0];
55 p->p_p0br = (struct pte *)mfpr(P0BR);
56 p->p_szpt = 1;
57 p->p_addr = uaddr(p);
58 p->p_stat = SRUN;
59 p->p_flag |= SLOAD|SSYS;
60 p->p_nice = NZERO;
61 setredzone(p->p_addr, (caddr_t)&u);
62 u.u_procp = p;
c4708522 63 u.u_cmask = CMASK;
73248996 64 for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
403556fe
BJ
65 switch (i) {
66
67 case LIM_STACK:
68 u.u_limit[i] = 512*1024;
69 continue;
70 case LIM_DATA:
71 u.u_limit[i] = ctob(MAXDSIZ);
72 continue;
73 default:
74 u.u_limit[i] = INFINITY;
75 continue;
76 }
c4708522
BJ
77 clkstart();
78
79 /*
80 * Initialize devices and
81 * set up 'known' i-nodes
82 */
83
84 ihinit();
0d5a507c 85 bhinit();
c4708522
BJ
86 cinit();
87 binit();
88 bswinit();
89 iinit();
90 rootdir = iget(rootdev, (ino_t)ROOTINO);
91 rootdir->i_flag &= ~ILOCK;
92 u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
93 u.u_cdir->i_flag &= ~ILOCK;
94 u.u_rdir = NULL;
95 u.u_dmap = zdmap;
96 u.u_smap = zdmap;
97
98 /*
99 * make page-out daemon (process 2)
c34926db 100 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
c4708522
BJ
101 * table so that it can map dirty pages into
102 * its address space during asychronous pushes.
103 */
104
105 mpid = 1;
c34926db 106 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
c4708522
BJ
107 proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */
108 if (newproc(0)) {
109 proc[2].p_flag |= SLOAD|SSYS;
c34926db 110 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
c4708522
BJ
111 pageout();
112 }
113
114 /*
115 * make init process and
116 * enter scheduling loop
117 */
118
119 mpid = 0;
120 proc[1].p_stat = 0;
121 proc[0].p_szpt = CLSIZE;
122 if (newproc(0)) {
123 expand(clrnd((int)btoc(szicode)), P0BR);
934e4ecf 124 (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
81263dba 125 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
c4708522
BJ
126 /*
127 * Return goes to loc. 0 of user init
128 * code just copied out.
129 */
130 return;
131 }
132 proc[0].p_szpt = 1;
133 sched();
134}
135
136/*
137 * iinit is called once (from main)
138 * very early in initialization.
139 * It reads the root's super block
140 * and initializes the current date
141 * from the last modified date.
142 *
143 * panic: iinit -- cannot read the super
144 * block. Usually because of an IO error.
145 */
146iinit()
147{
5b564702 148 register struct buf *bp;
c4708522 149 register struct filsys *fp;
c4708522
BJ
150
151 (*bdevsw[major(rootdev)].d_open)(rootdev, 1);
152 bp = bread(rootdev, SUPERB);
c4708522
BJ
153 if(u.u_error)
154 panic("iinit");
63e97a1d 155 bp->b_flags |= B_LOCKED; /* block can never be re-used */
c4708522 156 brelse(bp);
c4708522 157 mount[0].m_dev = rootdev;
63e97a1d
BJ
158 mount[0].m_bufp = bp;
159 fp = bp->b_un.b_filsys;
c4708522
BJ
160 fp->s_flock = 0;
161 fp->s_ilock = 0;
162 fp->s_ronly = 0;
163 fp->s_lasti = 1;
164 fp->s_nbehind = 0;
403556fe 165 clkinit(fp->s_time);
c4708522
BJ
166 bootime = time;
167}
168
c4708522
BJ
169/*
170 * Initialize the buffer I/O system by freeing
171 * all buffers and setting all device buffer lists to empty.
c4708522
BJ
172 */
173binit()
174{
175 register struct buf *bp;
176 register struct buf *dp;
177 register int i;
178 struct bdevsw *bdp;
41888f16 179 struct swdevt *swp;
c4708522 180
63e97a1d
BJ
181 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
182 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
183 dp->b_flags = B_HEAD;
184 }
185 dp--; /* dp = &bfreelist[BQUEUES-1]; */
0a34b6fd 186 for (i=0; i<nbuf; i++) {
c4708522
BJ
187 bp = &buf[i];
188 bp->b_dev = NODEV;
0a34b6fd 189 bp->b_un.b_addr = buffers + i * BSIZE;
63e97a1d
BJ
190 bp->b_back = dp;
191 bp->b_forw = dp->b_forw;
192 dp->b_forw->b_back = bp;
193 dp->b_forw = bp;
194 bp->b_flags = B_BUSY|B_INVAL;
c4708522
BJ
195 brelse(bp);
196 }
5b564702 197 for (bdp = bdevsw; bdp->d_open; bdp++)
c4708522 198 nblkdev++;
41888f16
BJ
199 /*
200 * Count swap devices, and adjust total swap space available.
201 * Some of this space will not be available until a vswapon()
202 * system is issued, usually when the system goes multi-user.
203 */
204 nswdev = 0;
205 for (swp = swdevt; swp->sw_dev; swp++)
206 nswdev++;
207 if (nswdev == 0)
208 panic("binit");
209 nswap *= nswdev;
210 maxpgio *= nswdev;
211 swfree(0);
c4708522
BJ
212}
213
214/*
215 * Initialize linked list of free swap
216 * headers. These do not actually point
217 * to buffers, but rather to pages that
218 * are being swapped in and out.
219 */
220bswinit()
221{
222 register int i;
0a34b6fd 223 register struct buf *sp = swbuf;
c4708522 224
c34926db 225 bswlist.av_forw = sp;
0a34b6fd 226 for (i=0; i<nswbuf-1; i++, sp++)
c34926db
BJ
227 sp->av_forw = sp+1;
228 sp->av_forw = NULL;
c4708522 229}
e7c7d88e
BJ
230
231/*
232 * Initialize clist by freeing all character blocks, then count
233 * number of character devices. (Once-only routine)
234 */
235cinit()
236{
237 register int ccp;
238 register struct cblock *cp;
239 register struct cdevsw *cdp;
240
241 ccp = (int)cfree;
242 ccp = (ccp+CROUND) & ~CROUND;
243 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
244 cp->c_next = cfreelist;
245 cfreelist = cp;
246 cfreecount += CBSIZE;
247 }
248 ccp = 0;
249 for(cdp = cdevsw; cdp->d_open; cdp++)
250 ccp++;
251 nchrdev = ccp;
252}