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