xref: /original-bsd/sys/kern/init_main.c (revision 95a66346)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)init_main.c	7.35 (Berkeley) 03/25/91
7  */
8 
9 #include "param.h"
10 #include "filedesc.h"
11 #include "kernel.h"
12 #include "mount.h"
13 #include "map.h"
14 #include "proc.h"
15 #include "resourcevar.h"
16 #include "signalvar.h"
17 #include "systm.h"
18 #include "vnode.h"
19 #include "seg.h"
20 #include "conf.h"
21 #include "buf.h"
22 #include "clist.h"
23 #include "malloc.h"
24 #include "protosw.h"
25 #include "reboot.h"
26 #include "user.h"
27 
28 #include "ufs/quota.h"
29 
30 #include "machine/cpu.h"
31 
32 #include "vm/vm.h"
33 
34 /*
35  * Components of process 0;
36  * never freed.
37  */
38 struct	session session0;
39 struct	pgrp pgrp0;
40 struct	proc proc0;
41 struct	pcred cred0;
42 struct	filedesc0 filedesc0;
43 struct	plimit limit0;
44 struct	vmspace vmspace0;
45 struct	proc *curproc = &proc0;
46 struct	proc *initproc, *pageproc;
47 
48 int	cmask = CMASK;
49 extern	caddr_t proc0paddr;
50 extern	int (*mountroot)();
51 extern	char initflags[];
52 
53 /*
54  * System startup; initialize the world, create process 0,
55  * mount root filesystem, and fork to create init and pagedaemon.
56  * Most of the hard work is done in the lower-level initialization
57  * routines including startup(), which does memory initialization
58  * and autoconfiguration.
59  */
60 main(firstaddr)
61 	int firstaddr;
62 {
63 	register int i;
64 	register struct proc *p;
65 	register struct filedesc0 *fdp;
66 	int s, rval[2];
67 
68 	/*
69 	 * Initialize curproc before any possible traps/probes
70 	 * to simplify trap processing.
71 	 */
72 	p = &proc0;
73 	curproc = p;
74 	/*
75 	 * Attempt to find console and initialize
76 	 * in case of early panic or other messages.
77 	 */
78 	consinit();
79 
80 	vm_mem_init();
81 	kmeminit();
82 	startup(firstaddr);
83 
84 	/*
85 	 * set up system process 0 (swapper)
86 	 */
87 	p = &proc0;
88 	curproc = p;
89 
90 	allproc = p;
91 	p->p_prev = &allproc;
92 	p->p_pgrp = &pgrp0;
93 	pgrphash[0] = &pgrp0;
94 	pgrp0.pg_mem = p;
95 	pgrp0.pg_session = &session0;
96 	session0.s_count = 1;
97 	session0.s_leader = p;
98 
99 	p->p_flag = SLOAD|SSYS;
100 	p->p_stat = SRUN;
101 	p->p_nice = NZERO;
102 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
103 
104 	/*
105 	 * Setup credentials
106 	 */
107 	p->p_cred = &cred0;
108 	p->p_ucred = crget();
109 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
110 
111 	/*
112 	 * Create the file descriptor table for process 0.
113 	 */
114 	fdp = &filedesc0;
115 	p->p_fd = &fdp->fd_fd;
116 	fdp->fd_fd.fd_refcnt = 1;
117 	fdp->fd_fd.fd_cmask = cmask;
118 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
119 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
120 	fdp->fd_fd.fd_nfiles = NDFILE;
121 
122 	/*
123 	 * Set initial limits
124 	 */
125 	p->p_limit = &limit0;
126 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
127 		limit0.pl_rlimit[i].rlim_cur =
128 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
129 	limit0.pl_rlimit[RLIMIT_OFILE].rlim_cur = NOFILE;
130 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
131 	limit0.p_refcnt = 1;
132 
133 	/*
134 	 * Allocate a prototype map so we have something to fork
135 	 */
136 	p->p_vmspace = &vmspace0;
137 	vmspace0.vm_refcnt = 1;
138 	pmap_pinit(&vmspace0.vm_pmap);
139 	vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
140 	    trunc_page(VM_MAX_ADDRESS), TRUE);
141 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
142 	p->p_addr = proc0paddr;				/* XXX */
143 
144 	/*
145 	 * We continue to place resource usage info
146 	 * and signal actions in the user struct so they're pageable.
147 	 */
148 	p->p_stats = &((struct user *)p->p_addr)->u_stats;
149 	p->p_sigacts = &((struct user *)p->p_addr)->u_sigacts;
150 
151 	rqinit();
152 
153 	/*
154 	 * configure virtual memory system,
155 	 * set vm rlimits
156 	 */
157 	vm_init_limits(p);
158 
159 	/*
160 	 * Initialize the file systems.
161 	 *
162 	 * Get vnodes for swapdev, argdev, and rootdev.
163 	 */
164 	vfsinit();
165 	if (bdevvp(swapdev, &swapdev_vp) ||
166 	    bdevvp(argdev, &argdev_vp) ||
167 	    bdevvp(rootdev, &rootvp))
168 		panic("can't setup bdevvp's");
169 
170 	startrtclock();
171 #if defined(vax)
172 #include "kg.h"
173 #if NKG > 0
174 	startkgclock();
175 #endif
176 #endif
177 
178 	/*
179 	 * Initialize tables, protocols, and set up well-known inodes.
180 	 */
181 	mbinit();
182 	cinit();
183 #ifdef SYSVSHM
184 	shminit();
185 #endif
186 #include "sl.h"
187 #if NSL > 0
188 	slattach();			/* XXX */
189 #endif
190 #include "loop.h"
191 #if NLOOP > 0
192 	loattach();			/* XXX */
193 #endif
194 	/*
195 	 * Block reception of incoming packets
196 	 * until protocols have been initialized.
197 	 */
198 	s = splimp();
199 	ifinit();
200 	domaininit();
201 	splx(s);
202 
203 #ifdef GPROF
204 	kmstartup();
205 #endif
206 
207 	/* kick off timeout driven events by calling first time */
208 	roundrobin();
209 	schedcpu();
210 	enablertclock();		/* enable realtime clock interrupts */
211 
212 	/*
213 	 * Set up the root file system and vnode.
214 	 */
215 	if ((*mountroot)())
216 		panic("cannot mount root");
217 	/*
218 	 * Get vnode for '/'.
219 	 * Setup rootdir and fdp->fd_fd.fd_cdir to point to it.
220 	 */
221 	if (VFS_ROOT(rootfs, &rootdir))
222 		panic("cannot find root vnode");
223 	fdp->fd_fd.fd_cdir = rootdir;
224 	VREF(fdp->fd_fd.fd_cdir);
225 	VOP_UNLOCK(rootdir);
226 	fdp->fd_fd.fd_rdir = NULL;
227 	swapinit();
228 
229 	/*
230 	 * Now can look at time, having had a chance
231 	 * to verify the time from the file system.
232 	 */
233 	boottime = p->p_stats->p_start = time;
234 
235 	/*
236 	 * make init process
237 	 */
238 	siginit(p);
239 	if (fork(p, (void *) NULL, rval))
240 		panic("fork init");
241 	if (rval[1]) {
242 		char *ip = initflags;
243 		vm_offset_t addr = 0;
244 
245 		/*
246 		 * Now in process 1.  Set init flags into icode,
247 		 * get a minimal address space, copy out "icode",
248 		 * and return to it to do an exec of init.
249 		 */
250 		p = curproc;
251 		initproc = p;
252 		*ip++ = '-';
253 		if (boothowto&RB_SINGLE)
254 			*ip++ = 's';
255 #ifdef notyet
256 		if (boothowto&RB_FASTBOOT)
257 			*ip++ = 'f';
258 		*ip++ = '\0';
259 #endif
260 
261 		if (vm_allocate(&p->p_vmspace->vm_map, &addr,
262 		    round_page(szicode), FALSE) != KERN_SUCCESS || addr != 0)
263 			panic("init: couldn't allocate at zero");
264 
265 		/* need just enough stack to exec from */
266 		addr = trunc_page(VM_MAX_ADDRESS - PAGE_SIZE);
267 		if (vm_allocate(&p->p_vmspace->vm_map, &addr,
268 		    PAGE_SIZE, FALSE) != KERN_SUCCESS)
269 			panic("vm_allocate init stack");
270 		p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
271 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
272 		return;			/* returns to icode */
273 	}
274 
275 	/*
276 	 * Start up pageout daemon (process 2).
277 	 */
278 	if (fork(p, (void *) NULL, rval))
279 		panic("fork pager");
280 	if (rval[1]) {
281 		/*
282 		 * Now in process 2.
283 		 */
284 		p = curproc;
285 		pageproc = p;
286 		p->p_flag |= SLOAD|SSYS;		/* XXX */
287 		bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
288 		vm_pageout();
289 		/*NOTREACHED*/
290 	}
291 
292 	/*
293 	 * enter scheduling loop
294 	 */
295 	sched();
296 }
297 
298 /* MOVE TO vfs_bio.c (bufinit) XXX */
299 /*
300  * Initialize buffers and hash links for buffers.
301  */
302 bufinit()
303 {
304 	register int i;
305 	register struct buf *bp, *dp;
306 	register struct bufhd *hp;
307 	int base, residual;
308 
309 	for (hp = bufhash, i = 0; i < BUFHSZ; i++, hp++)
310 		hp->b_forw = hp->b_back = (struct buf *)hp;
311 
312 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
313 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
314 		dp->b_flags = B_HEAD;
315 	}
316 	base = bufpages / nbuf;
317 	residual = bufpages % nbuf;
318 	for (i = 0; i < nbuf; i++) {
319 		bp = &buf[i];
320 		bp->b_dev = NODEV;
321 		bp->b_bcount = 0;
322 		bp->b_rcred = NOCRED;
323 		bp->b_wcred = NOCRED;
324 		bp->b_dirtyoff = 0;
325 		bp->b_dirtyend = 0;
326 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
327 		if (i < residual)
328 			bp->b_bufsize = (base + 1) * CLBYTES;
329 		else
330 			bp->b_bufsize = base * CLBYTES;
331 		binshash(bp, &bfreelist[BQ_AGE]);
332 		bp->b_flags = B_BUSY|B_INVAL;
333 		brelse(bp);
334 	}
335 }
336