xref: /freebsd/sys/kern/init_main.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
39  * $Id: init_main.c,v 1.24 1995/05/14 02:59:55 davidg Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/filedesc.h>
44 #include <sys/errno.h>
45 #include <sys/exec.h>
46 #include <sys/kernel.h>
47 #ifdef GPROF
48 #include <sys/gmon.h>
49 #endif
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
54 #include <sys/systm.h>
55 #include <sys/vnode.h>
56 #include <sys/sysent.h>
57 #include <sys/conf.h>
58 #include <sys/buf.h>
59 #include <sys/clist.h>
60 #include <sys/device.h>
61 #include <sys/msg.h>
62 #include <sys/protosw.h>
63 #include <sys/reboot.h>
64 #include <sys/sem.h>
65 #include <sys/shm.h>
66 #include <sys/user.h>
67 
68 #include <ufs/ufs/quota.h>
69 
70 #include <machine/cpu.h>
71 
72 #include <vm/vm.h>
73 #include <vm/vm_pageout.h>
74 
75 #ifdef HPFPLIB
76 char	copyright[] =
77 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California.\nCopyright (c) 1992 Hewlett-Packard Company\nCopyright (c) 1992 Motorola Inc.\nAll rights reserved.\n\n";
78 #else
79 char	copyright[] =
80 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California.  All rights reserved.\n\n";
81 #endif
82 
83 /* Components of the first process -- never freed. */
84 struct	session session0;
85 struct	pgrp pgrp0;
86 struct	proc proc0;
87 struct	pcred cred0;
88 struct	filedesc0 filedesc0;
89 struct	plimit limit0;
90 struct	vmspace vmspace0;
91 struct	proc *curproc = &proc0;
92 struct	proc *initproc, *pageproc, *updateproc, *vmproc;
93 
94 int	cmask = CMASK;
95 extern	struct user *proc0paddr;
96 
97 struct	vnode *rootvp;
98 int	boothowto;
99 struct	timeval boottime;
100 struct	timeval runtime;
101 
102 static void start_init __P((struct proc *p, void *framep));
103 
104 #if __GNUC__ >= 2
105 void __main() {}
106 #endif
107 
108 /*
109  * This table is filled in by the linker with functions that need to be
110  * called to initialize various pseudo-devices and whatnot.
111  */
112 
113 static void dummyinit() {}
114 TEXT_SET(pseudo_set, dummyinit);
115 
116 typedef void (*pseudo_func_t)(void);
117 extern const struct linker_set pseudo_set;
118 static const pseudo_func_t *pseudos =
119 	(const pseudo_func_t *)&pseudo_set.ls_items[0];
120 
121 /*
122  * System startup; initialize the world, create process 0, mount root
123  * filesystem, and fork to create init and pagedaemon.  Most of the
124  * hard work is done in the lower-level initialization routines including
125  * startup(), which does memory initialization and autoconfiguration.
126  */
127 void
128 main(framep)
129 	void *framep;
130 {
131 	register struct proc *p;
132 	register struct filedesc0 *fdp;
133 	register int i;
134 	int s, rval[2];
135 
136 	/*
137 	 * Initialize the current process pointer (curproc) before
138 	 * any possible traps/probes to simplify trap processing.
139 	 */
140 	p = &proc0;
141 	curproc = p;
142 	printf(copyright);
143 
144 	vm_mem_init();
145 	kmeminit();
146 	cpu_startup();
147 
148 	/*
149 	 * Create process 0 (the swapper).
150 	 */
151 	allproc = (volatile struct proc *)p;
152 	p->p_prev = (struct proc **)&allproc;
153 	p->p_pgrp = &pgrp0;
154 	pgrphash[0] = &pgrp0;
155 	pgrp0.pg_mem = p;
156 	pgrp0.pg_session = &session0;
157 	session0.s_count = 1;
158 	session0.s_leader = p;
159 
160 	p->p_sysent = &aout_sysvec;
161 
162 	p->p_flag = P_INMEM | P_SYSTEM;
163 	p->p_stat = SRUN;
164 	p->p_nice = NZERO;
165 	p->p_rtprio.type = RTP_PRIO_NORMAL;
166 	p->p_rtprio.prio = 0;
167 
168 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
169 
170 	/* Create credentials. */
171 	cred0.p_refcnt = 1;
172 	p->p_cred = &cred0;
173 	p->p_ucred = crget();
174 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
175 
176 	/* Create the file descriptor table. */
177 	fdp = &filedesc0;
178 	p->p_fd = &fdp->fd_fd;
179 	fdp->fd_fd.fd_refcnt = 1;
180 	fdp->fd_fd.fd_cmask = cmask;
181 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
182 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
183 	fdp->fd_fd.fd_nfiles = NDFILE;
184 
185 	/* Create the limits structures. */
186 	p->p_limit = &limit0;
187 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
188 		limit0.pl_rlimit[i].rlim_cur =
189 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
190 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
191 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
192 	i = ptoa(cnt.v_free_count);
193 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
194 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
195 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
196 	limit0.p_refcnt = 1;
197 
198 	/* Allocate a prototype map so we have something to fork. */
199 	p->p_vmspace = &vmspace0;
200 	vmspace0.vm_refcnt = 1;
201 	pmap_pinit(&vmspace0.vm_pmap);
202 	vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
203 	    trunc_page(VM_MAX_ADDRESS), TRUE);
204 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
205 	p->p_addr = proc0paddr;				/* XXX */
206 
207 	/*
208 	 * proc0 needs to have a coherent frame base, too.
209 	 * This probably makes the identical call for the init proc
210 	 * that happens later unnecessary since it should inherit
211 	 * it during the fork.
212 	 */
213 	cpu_set_init_frame(p, framep);			/* XXX! */
214 
215 	/*
216 	 * We continue to place resource usage info and signal
217 	 * actions in the user struct so they're pageable.
218 	 */
219 	p->p_stats = &p->p_addr->u_stats;
220 	p->p_sigacts = &p->p_addr->u_sigacts;
221 
222 	/*
223 	 * Initialize per uid information structure and charge
224 	 * root for one process.
225 	 */
226 	usrinfoinit();
227 	(void)chgproccnt(0, 1);
228 
229 	rqinit();
230 
231 	/* Configure virtual memory system, set vm rlimits. */
232 	vm_init_limits(p);
233 
234 	/* Initialize the file systems. */
235 	vfsinit();
236 
237 	/* Start real time and statistics clocks. */
238 	initclocks();
239 
240 	/* Initialize mbuf's. */
241 	mbinit();
242 
243 	/* Initialize clists. */
244 	clist_init();
245 
246 #ifdef SYSVSHM
247 	/* Initialize System V style shared memory. */
248 	shminit();
249 #endif
250 
251 #ifdef SYSVSEM
252 	/* Initialize System V style semaphores. */
253 	seminit();
254 #endif
255 
256 #ifdef SYSVMSG
257 	/* Initialize System V style message queues. */
258 	msginit();
259 #endif
260 
261 	/*
262 	 * Attach pseudo-devices.
263 	 */
264 	while(*pseudos) {
265 		(**pseudos++)();
266 	}
267 
268 	/*
269 	 * Initialize protocols.  Block reception of incoming packets
270 	 * until everything is ready.
271 	 */
272 	s = splimp();
273 	ifinit();
274 	domaininit();
275 	splx(s);
276 
277 #ifdef GPROF
278 	/* Initialize kernel profiling. */
279 	kmstartup();
280 #endif
281 
282 	/* Kick off timeout driven events by calling first time. */
283 	roundrobin(NULL);
284 	schedcpu(NULL);
285 
286 	/* Mount the root file system. */
287 	if ((*mountroot)())
288 		panic("cannot mount root");
289 
290 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
291 	if (VFS_ROOT(mountlist.tqh_first, &rootvnode))
292 		panic("cannot find root vnode");
293 	fdp->fd_fd.fd_cdir = rootvnode;
294 	VREF(fdp->fd_fd.fd_cdir);
295 	VOP_UNLOCK(rootvnode);
296 	fdp->fd_fd.fd_rdir = NULL;
297 
298 	/*
299 	 * Now can look at time, having had a chance to verify the time
300 	 * from the file system.  Reset p->p_rtime as it may have been
301 	 * munched in mi_switch() after the time got set.
302 	 */
303 	p->p_stats->p_start = runtime = mono_time = boottime = time;
304 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
305 
306 	/* Initialize signal state for process 0. */
307 	siginit(p);
308 
309 	/* Create process 1 (init(8)). */
310 	if (fork(p, NULL, rval))
311 		panic("fork init");
312 	if (rval[1]) {
313 		start_init(curproc, framep);
314 		return;
315 	}
316 
317 	/* Create process 2 (the pageout daemon). */
318 	if (fork(p, NULL, rval))
319 		panic("fork pager");
320 	if (rval[1]) {
321 		/*
322 		 * Now in process 2.
323 		 */
324 		p = curproc;
325 		pageproc = p;
326 		p->p_flag |= P_INMEM | P_SYSTEM;	/* XXX */
327 		bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
328 		vm_pageout();
329 		/* NOTREACHED */
330 	}
331 
332 	/*
333 	 * Start high level vm daemon (process 3).
334 	 */
335 	if (fork(p, (void *) NULL, rval))
336 		panic("failed fork vm daemon");
337 	if (rval[1]) {
338 		p = curproc;
339 		vmproc = p;
340 		p->p_flag |= P_INMEM | P_SYSTEM;
341 		bcopy("vmdaemon", p->p_comm, sizeof("vmdaemon"));
342 		vm_daemon();
343 		/*NOTREACHED*/
344 	}
345 
346 	/*
347 	 * Start update daemon (process 4).
348 	 */
349 	if (fork(p, (void *) NULL, rval))
350 		panic("failed fork update daemon");
351 	if (rval[1]) {
352 		p = curproc;
353 		updateproc = p;
354 		p->p_flag |= P_INMEM | P_SYSTEM;
355 		bcopy("update", p->p_comm, sizeof("update"));
356 		vfs_update();
357 		/*NOTREACHED*/
358 	}
359 
360 	/* The scheduler is an infinite loop. */
361 	scheduler();
362 	/* NOTREACHED */
363 }
364 
365 /*
366  * List of paths to try when searching for "init".
367  */
368 static char *initpaths[] = {
369 	"/sbin/init",
370 	"/sbin/oinit",
371 	"/sbin/init.bak",
372 	"/stand/sysinstall",
373 	NULL,
374 };
375 
376 /*
377  * Start the initial user process; try exec'ing each pathname in "initpaths".
378  * The program is invoked with one argument containing the boot flags.
379  */
380 static void
381 start_init(p, framep)
382 	struct proc *p;
383 	void *framep;
384 {
385 	vm_offset_t addr;
386 	struct execve_args args;
387 	int options, i, retval[2], error;
388 	char **pathp, *path, *ucp, **uap, *arg0, *arg1;
389 
390 	initproc = p;
391 
392 	/*
393 	 * We need to set the system call frame as if we were entered through
394 	 * a syscall() so that when we call execve() below, it will be able
395 	 * to set the entry point (see setregs) when it tries to exec.  The
396 	 * startup code in "locore.s" has allocated space for the frame and
397 	 * passed a pointer to that space as main's argument.
398 	 */
399 	cpu_set_init_frame(p, framep);
400 
401 	/*
402 	 * Need just enough stack to hold the faked-up "execve()" arguments.
403 	 */
404 	addr = trunc_page(VM_MAXUSER_ADDRESS - PAGE_SIZE);
405 	if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, FALSE) != 0)
406 		panic("init: couldn't allocate argument space");
407 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
408 	p->p_vmspace->vm_ssize = 1;
409 
410 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
411 		/*
412 		 * Move out the boot flag argument.
413 		 */
414 		options = 0;
415 		ucp = (char *)USRSTACK;
416 		(void)subyte(--ucp, 0);		/* trailing zero */
417 		if (boothowto & RB_SINGLE) {
418 			(void)subyte(--ucp, 's');
419 			options = 1;
420 		}
421 #ifdef notyet
422                 if (boothowto & RB_FASTBOOT) {
423 			(void)subyte(--ucp, 'f');
424 			options = 1;
425 		}
426 #endif
427 
428 #ifdef BOOTCDROM
429 		(void)subyte(--ucp, 'C');
430 		options = 1;
431 #endif
432 		if (options == 0)
433 			(void)subyte(--ucp, '-');
434 		(void)subyte(--ucp, '-');		/* leading hyphen */
435 		arg1 = ucp;
436 
437 		/*
438 		 * Move out the file name (also arg 0).
439 		 */
440 		for (i = strlen(path) + 1; i >= 0; i--)
441 			(void)subyte(--ucp, path[i]);
442 		arg0 = ucp;
443 
444 		/*
445 		 * Move out the arg pointers.
446 		 */
447 		uap = (char **)((int)ucp & ~(NBPW-1));
448 		(void)suword((caddr_t)--uap, 0);	/* terminator */
449 		(void)suword((caddr_t)--uap, (int)arg1);
450 		(void)suword((caddr_t)--uap, (int)arg0);
451 
452 		/*
453 		 * Point at the arguments.
454 		 */
455 		args.fname = arg0;
456 		args.argv = uap;
457 		args.envv = NULL;
458 
459 		/*
460 		 * Now try to exec the program.  If can't for any reason
461 		 * other than it doesn't exist, complain.
462 		 */
463 		if ((error = execve(p, &args, &retval[0])) == 0)
464 			return;
465 		if (error != ENOENT)
466 			printf("exec %s: error %d\n", path, error);
467 	}
468 	printf("init: not found\n");
469 	panic("no init");
470 }
471