xref: /openbsd-src/sys/kern/init_main.c (revision 8445c53715e7030056b779e8ab40efb7820981f2)
1 /*	$OpenBSD: init_main.c,v 1.77 2001/09/17 19:17:30 gluk Exp $	*/
2 /*	$NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
6  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
43  */
44 
45 #include <sys/param.h>
46 #include <sys/filedesc.h>
47 #include <sys/file.h>
48 #include <sys/errno.h>
49 #include <sys/exec.h>
50 #include <sys/kernel.h>
51 #include <sys/kthread.h>
52 #include <sys/mount.h>
53 #include <sys/map.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/signalvar.h>
57 #include <sys/systm.h>
58 #include <sys/namei.h>
59 #include <sys/vnode.h>
60 #include <sys/tty.h>
61 #include <sys/conf.h>
62 #include <sys/buf.h>
63 #include <sys/device.h>
64 #include <sys/protosw.h>
65 #include <sys/reboot.h>
66 #include <sys/user.h>
67 #ifdef SYSVSHM
68 #include <sys/shm.h>
69 #endif
70 #ifdef SYSVSEM
71 #include <sys/sem.h>
72 #endif
73 #ifdef SYSVMSG
74 #include <sys/msg.h>
75 #endif
76 #include <sys/domain.h>
77 #include <sys/mbuf.h>
78 #include <sys/pipe.h>
79 
80 #include <sys/syscall.h>
81 #include <sys/syscallargs.h>
82 
83 #include <ufs/ufs/quota.h>
84 
85 #include <machine/cpu.h>
86 
87 #include <vm/vm.h>
88 
89 #include <uvm/uvm.h>
90 
91 #include <net/if.h>
92 #include <net/raw_cb.h>
93 
94 #if defined(CRYPTO)
95 #include <crypto/cryptodev.h>
96 #include <crypto/cryptosoft.h>
97 #endif
98 
99 #if defined(NFSSERVER) || defined(NFSCLIENT)
100 extern void nfs_init __P((void));
101 #endif
102 
103 char	copyright[] =
104 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
105 "\tThe Regents of the University of California.  All rights reserved.\n"
106 "Copyright (c) 1995-2001 OpenBSD. All rights reserved.  http://www.OpenBSD.org\n";
107 
108 /* Components of the first process -- never freed. */
109 struct	session session0;
110 struct	pgrp pgrp0;
111 struct	proc proc0;
112 struct	pcred cred0;
113 struct	filedesc0 filedesc0;
114 struct	plimit limit0;
115 struct	vmspace vmspace0;
116 struct	sigacts sigacts0;
117 #ifndef curproc
118 struct	proc *curproc;
119 #endif
120 struct	proc *initproc;
121 
122 int	cmask = CMASK;
123 extern	struct user *proc0paddr;
124 
125 void	(*md_diskconf) __P((void)) = NULL;
126 struct	vnode *rootvp, *swapdev_vp;
127 int	boothowto;
128 struct	timeval boottime;
129 struct	timeval runtime;
130 
131 /* XXX return int so gcc -Werror won't complain */
132 int	main __P((void *));
133 void	check_console __P((struct proc *));
134 void	start_init __P((void *));
135 void	start_pagedaemon __P((void *));
136 void	start_flusher __P((void *));
137 void	start_update __P((void *));
138 void	start_reaper __P((void *));
139 void    start_crypto __P((void *));
140 
141 extern char sigcode[], esigcode[];
142 #ifdef SYSCALL_DEBUG
143 extern char *syscallnames[];
144 #endif
145 
146 struct emul emul_native = {
147 	"native",
148 	NULL,
149 	sendsig,
150 	SYS_syscall,
151 	SYS_MAXSYSCALL,
152 	sysent,
153 #ifdef SYSCALL_DEBUG
154 	syscallnames,
155 #else
156 	NULL,
157 #endif
158 	0,
159 	copyargs,
160 	setregs,
161 	NULL,
162 	sigcode,
163 	esigcode,
164 };
165 
166 /*
167  * System startup; initialize the world, create process 0, mount root
168  * filesystem, and fork to create init and pagedaemon.  Most of the
169  * hard work is done in the lower-level initialization routines including
170  * startup(), which does memory initialization and autoconfiguration.
171  */
172 /* XXX return int, so gcc -Werror won't complain */
173 int
174 main(framep)
175 	void *framep;				/* XXX should go away */
176 {
177 	register struct proc *p;
178 	register struct pdevinit *pdev;
179 	struct timeval rtv;
180 	register int i;
181 	int s;
182 	register_t rval[2];
183 	extern struct pdevinit pdevinit[];
184 	extern void scheduler_start __P((void));
185 	extern void disk_init __P((void));
186 	extern void endtsleep __P((void *));
187 	extern void realitexpire __P((void *));
188 
189 	/*
190 	 * Initialize the current process pointer (curproc) before
191 	 * any possible traps/probes to simplify trap processing.
192 	 */
193 	p = &proc0;
194 	curproc = p;
195 
196 	/*
197 	 * Attempt to find console and initialize
198 	 * in case of early panic or other messages.
199 	 */
200 	config_init();		/* init autoconfiguration data structures */
201 	consinit();
202 	printf(copyright);
203 	printf("\n");
204 
205 	uvm_init();
206 	disk_init();		/* must come before autoconfiguration */
207 	tty_init();		/* initialise tty's */
208 	cpu_startup();
209 
210 	/*
211 	 * Initialize mbuf's.  Do this now because we might attempt to
212 	 * allocate mbufs or mbuf clusters during autoconfiguration.
213 	 */
214 	mbinit();
215 
216 	/*
217 	 * Initialize timeouts.
218 	 */
219 	timeout_startup();
220 
221 	cpu_configure();
222 
223 	/* Initialize sysctls (must be done before any processes run) */
224 	sysctl_init();
225 
226 	/*
227 	 * Initialize process and pgrp structures.
228 	 */
229 	procinit();
230 
231 	/*
232 	 * Initialize filedescriptors.
233 	 */
234 	filedesc_init();
235 
236 	/*
237 	 * Initialize pipes.
238 	 */
239 	pipe_init();
240 
241 	/*
242 	 * Create process 0 (the swapper).
243 	 */
244 	LIST_INSERT_HEAD(&allproc, p, p_list);
245 	p->p_pgrp = &pgrp0;
246 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
247 	LIST_INIT(&pgrp0.pg_members);
248 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
249 
250 	pgrp0.pg_session = &session0;
251 	session0.s_count = 1;
252 	session0.s_leader = p;
253 
254 	p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT;
255 	p->p_stat = SRUN;
256 	p->p_nice = NZERO;
257 	p->p_emul = &emul_native;
258 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
259 
260 	/* Init timeouts. */
261 	timeout_set(&p->p_sleep_to, endtsleep, p);
262 	timeout_set(&p->p_realit_to, realitexpire, p);
263 
264 	/* Create credentials. */
265 	cred0.p_refcnt = 1;
266 	p->p_cred = &cred0;
267 	p->p_ucred = crget();
268 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
269 
270 	/* Create the file descriptor table. */
271 	p->p_fd = &filedesc0.fd_fd;
272 	filedesc0.fd_fd.fd_refcnt = 1;
273 	filedesc0.fd_fd.fd_cmask = cmask;
274 	filedesc0.fd_fd.fd_ofiles = filedesc0.fd_dfiles;
275 	filedesc0.fd_fd.fd_ofileflags = filedesc0.fd_dfileflags;
276 	filedesc0.fd_fd.fd_nfiles = NDFILE;
277 	filedesc0.fd_fd.fd_himap = filedesc0.fd_dhimap;
278 	filedesc0.fd_fd.fd_lomap = filedesc0.fd_dlomap;
279 
280 	/* Create the limits structures. */
281 	p->p_limit = &limit0;
282 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
283 		limit0.pl_rlimit[i].rlim_cur =
284 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
285 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
286 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = MIN(NOFILE_MAX,
287 	    (maxfiles - NOFILE > NOFILE) ?  maxfiles - NOFILE : NOFILE);
288 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
289 	i = ptoa(uvmexp.free);
290 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
291 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
292 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
293 	limit0.p_refcnt = 1;
294 
295 	/* Allocate a prototype map so we have something to fork. */
296 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
297 	    trunc_page(VM_MAX_ADDRESS), TRUE);
298 	p->p_vmspace = &vmspace0;
299 
300 	p->p_addr = proc0paddr;				/* XXX */
301 
302 	/*
303 	 * We continue to place resource usage info in the
304 	 * user struct so they're pageable.
305 	 */
306 	p->p_stats = &p->p_addr->u_stats;
307 
308 	/*
309 	 * Charge root for one process.
310 	 */
311 	(void)chgproccnt(0, 1);
312 
313 	rqinit();
314 
315 	/* Configure virtual memory system, set vm rlimits. */
316 	uvm_init_limits(p);
317 
318 	/* Initialize the file systems. */
319 #if defined(NFSSERVER) || defined(NFSCLIENT)
320 	nfs_init();			/* initialize server/shared data */
321 #endif
322 	vfsinit();
323 
324 	/* Start real time and statistics clocks. */
325 	initclocks();
326 
327 #ifdef SYSVSHM
328 	/* Initialize System V style shared memory. */
329 	shminit();
330 #endif
331 
332 #ifdef SYSVSEM
333 	/* Initialize System V style semaphores. */
334 	seminit();
335 #endif
336 
337 #ifdef SYSVMSG
338 	/* Initialize System V style message queues. */
339 	msginit();
340 #endif
341 
342 	/* Attach pseudo-devices. */
343 	randomattach();
344 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
345 		if (pdev->pdev_count > 0)
346 			(*pdev->pdev_attach)(pdev->pdev_count);
347 
348 #ifdef CRYPTO
349 	swcr_init();
350 #endif /* CRYPTO */
351 
352 	/*
353 	 * Initialize protocols.  Block reception of incoming packets
354 	 * until everything is ready.
355 	 */
356 	s = splimp();
357 	ifinit();
358 	domaininit();
359 	splx(s);
360 
361 #ifdef GPROF
362 	/* Initialize kernel profiling. */
363 	kmstartup();
364 #endif
365 
366 	/* Start the scheduler */
367 	scheduler_start();
368 
369 	dostartuphooks();
370 
371 	/* Configure root/swap devices */
372 	if (md_diskconf)
373 		(*md_diskconf)();
374 
375 	/* Mount the root file system. */
376 	if (vfs_mountroot())
377 		panic("cannot mount root");
378 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
379 
380 	/* Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to reference it. */
381 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
382 		panic("cannot find root vnode");
383 	filedesc0.fd_fd.fd_cdir = rootvnode;
384 	VREF(filedesc0.fd_fd.fd_cdir);
385 	VOP_UNLOCK(rootvnode, 0, p);
386 	filedesc0.fd_fd.fd_rdir = NULL;
387 
388 	uvm_swap_init();
389 
390 	/*
391 	 * Now can look at time, having had a chance to verify the time
392 	 * from the file system.  Reset p->p_rtime as it may have been
393 	 * munched in mi_switch() after the time got set.
394 	 */
395 	p->p_stats->p_start = runtime = mono_time = boottime = time;
396 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
397 
398 	/* Initialize signal state for process 0. */
399 	signal_init();
400 	p->p_sigacts = &sigacts0;
401 	siginit(p);
402 
403 	/* Create process 1 (init(8)). */
404 	if (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, rval))
405 		panic("fork init");
406 	initproc = pfind(rval[0]);
407 	cpu_set_kpc(initproc, start_init, initproc);
408 
409 	/* Create process 2, the pageout daemon kernel thread. */
410 	if (kthread_create(start_pagedaemon, NULL, NULL, "pagedaemon"))
411 		panic("fork pagedaemon");
412 
413 	/* Create process 3, the reaper daemon kernel thread. */
414 	if (kthread_create(start_reaper, NULL, NULL, "reaper"))
415 		panic("fork reaper");
416 
417 	/* Create process 4, the flusher daemon kernel thread. */
418 	if (kthread_create(start_flusher, NULL, NULL, "flusher"))
419 		panic("fork flusher");
420 
421 	/* Create process 5, the update daemon kernel thread. */
422 	if (kthread_create(start_update, NULL, NULL, "update"))
423 		panic("fork update");
424 
425 #ifdef CRYPTO
426 	/* Create process 6, the crypto kernel thread. */
427 	if (kthread_create(start_crypto, NULL, NULL, "crypto"))
428 		panic("crypto thread");
429 #endif /* CRYPTO */
430 
431 	/* Create any other deferred kernel threads. */
432 	kthread_run_deferred_queue();
433 
434 	microtime(&rtv);
435 	srandom((u_long)(rtv.tv_sec ^ rtv.tv_usec));
436 
437 	randompid = 1;
438 	/* The scheduler is an infinite loop. */
439 	uvm_scheduler();
440 	/* NOTREACHED */
441 }
442 
443 /*
444  * List of paths to try when searching for "init".
445  */
446 static char *initpaths[] = {
447 	"/sbin/init",
448 	"/sbin/oinit",
449 	"/sbin/init.bak",
450 	NULL,
451 };
452 
453 void
454 check_console(p)
455 	struct proc *p;
456 {
457 	struct nameidata nd;
458 	int error;
459 
460 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
461 	error = namei(&nd);
462 	if (error) {
463 		if (error == ENOENT)
464 			printf("warning: /dev/console does not exist\n");
465 		else
466 			printf("warning: /dev/console error %d\n", error);
467 	} else
468 		vrele(nd.ni_vp);
469 }
470 
471 /*
472  * Start the initial user process; try exec'ing each pathname in "initpaths".
473  * The program is invoked with one argument containing the boot flags.
474  */
475 void
476 start_init(arg)
477 	void *arg;
478 {
479 	struct proc *p = arg;
480 	vaddr_t addr;
481 	struct sys_execve_args /* {
482 		syscallarg(char *) path;
483 		syscallarg(char **) argp;
484 		syscallarg(char **) envp;
485 	} */ args;
486 	int options, i, error;
487 	register_t retval[2];
488 	char flags[4], *flagsp;
489 	char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
490 
491 	/*
492 	 * Now in process 1.
493 	 */
494 	check_console(p);
495 
496 	/*
497 	 * Need just enough stack to hold the faked-up "execve()" arguments.
498 	 */
499 #ifdef MACHINE_STACK_GROWS_UP
500 	addr = USRSTACK;
501 #else
502 	addr = USRSTACK - PAGE_SIZE;
503 #endif
504 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
505 	    NULL, UVM_UNKNOWN_OFFSET,
506 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
507 	    UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW))
508 	    != KERN_SUCCESS)
509 		panic("init: couldn't allocate argument space");
510 #ifdef MACHINE_STACK_GROWS_UP
511 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr + PAGE_SIZE;
512 #else
513 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
514 #endif
515 
516 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
517 #ifdef MACHINE_STACK_GROWS_UP
518 		ucp = (char *)addr;
519 #else
520 		ucp = (char *)(addr + PAGE_SIZE);
521 #endif
522 		/*
523 		 * Construct the boot flag argument.
524 		 */
525 		flagsp = flags;
526 		*flagsp++ = '-';
527 		options = 0;
528 
529 		if (boothowto & RB_SINGLE) {
530 			*flagsp++ = 's';
531 			options = 1;
532 		}
533 #ifdef notyet
534 		if (boothowto & RB_FASTBOOT) {
535 			*flagsp++ = 'f';
536 			options = 1;
537 		}
538 #endif
539 
540 		/*
541 		 * Move out the flags (arg 1), if necessary.
542 		 */
543 		if (options != 0) {
544 			*flagsp++ = '\0';
545 			i = flagsp - flags;
546 #ifdef DEBUG
547 			printf("init: copying out flags `%s' %d\n", flags, i);
548 #endif
549 #ifdef MACHINE_STACK_GROWS_UP
550 			arg1 = ucp;
551 			(void)copyout((caddr_t)flags, (caddr_t)ucp, i);
552 			ucp += i;
553 #else
554 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
555 			arg1 = ucp;
556 #endif
557 		}
558 
559 		/*
560 		 * Move out the file name (also arg 0).
561 		 */
562 		i = strlen(path) + 1;
563 #ifdef DEBUG
564 		printf("init: copying out path `%s' %d\n", path, i);
565 #endif
566 #ifdef MACHINE_STACK_GROWS_UP
567 		arg0 = ucp;
568 		(void)copyout((caddr_t)path, (caddr_t)ucp, i);
569 		ucp += i;
570 		ucp = (caddr_t)ALIGN((u_long)ucp);
571 		uap = (char **)ucp + 3;
572 #else
573 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
574 		arg0 = ucp;
575 		uap = (char **)((u_long)ucp & ~ALIGNBYTES);
576 #endif
577 
578 		/*
579 		 * Move out the arg pointers.
580 		 */
581 		(void)suword((caddr_t)--uap, 0);	/* terminator */
582 		if (options != 0)
583 			(void)suword((caddr_t)--uap, (long)arg1);
584 		(void)suword((caddr_t)--uap, (long)arg0);
585 
586 		/*
587 		 * Point at the arguments.
588 		 */
589 		SCARG(&args, path) = arg0;
590 		SCARG(&args, argp) = uap;
591 		SCARG(&args, envp) = NULL;
592 
593 		/*
594 		 * Now try to exec the program.  If can't for any reason
595 		 * other than it doesn't exist, complain.
596 		 */
597 		if ((error = sys_execve(p, &args, retval)) == 0)
598 			return;
599 		if (error != ENOENT)
600 			printf("exec %s: error %d\n", path, error);
601 	}
602 	printf("init: not found\n");
603 	panic("no init");
604 }
605 
606 void
607 start_pagedaemon(arg)
608 	void *arg;
609 {
610 	uvm_pageout();
611 	/* NOTREACHED */
612 }
613 
614 void
615 start_update(arg)
616 	void *arg;
617 {
618 	sched_sync(curproc);
619 	/* NOTREACHED */
620 }
621 
622 void
623 start_flusher(arg)
624 	void *arg;
625 {
626 	buf_daemon(curproc);
627 	/* NOTREACHED */
628 }
629 
630 void
631 start_reaper(arg)
632 	void *arg;
633 {
634 	reaper();
635 	/* NOTREACHED */
636 }
637 
638 #ifdef CRYPTO
639 void
640 start_crypto(arg)
641 	void *arg;
642 {
643 	crypto_thread();
644 	/* NOTREACHED */
645 }
646 #endif /* CRYPTO */
647