xref: /freebsd/sys/kern/init_main.c (revision c678bc4f13a340ad88debe321afd0097db2590cb)
1 /*
2  * Copyright (c) 1995 Terrence R. Lambert
3  * All rights reserved.
4  *
5  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
42  * $FreeBSD$
43  */
44 
45 #include "opt_init_path.h"
46 
47 #include <sys/param.h>
48 #include <sys/file.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/ktr.h>
52 #include <sys/lock.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysctl.h>
56 #include <sys/proc.h>
57 #include <sys/resourcevar.h>
58 #include <sys/systm.h>
59 #include <sys/signalvar.h>
60 #include <sys/vnode.h>
61 #include <sys/sysent.h>
62 #include <sys/reboot.h>
63 #include <sys/sx.h>
64 #include <sys/sysproto.h>
65 #include <sys/vmmeter.h>
66 #include <sys/unistd.h>
67 #include <sys/malloc.h>
68 #include <sys/conf.h>
69 
70 #include <machine/cpu.h>
71 #include <machine/globals.h>
72 
73 #include <vm/vm.h>
74 #include <vm/vm_param.h>
75 #include <vm/pmap.h>
76 #include <vm/vm_map.h>
77 #include <sys/user.h>
78 #include <sys/copyright.h>
79 
80 extern struct linker_set	sysinit_set;	/* XXX */
81 
82 void mi_startup(void);				/* Should be elsewhere */
83 
84 /* Components of the first process -- never freed. */
85 static struct session session0;
86 static struct pgrp pgrp0;
87 struct	proc proc0;
88 static struct procsig procsig0;
89 static struct filedesc0 filedesc0;
90 static struct plimit limit0;
91 static struct vmspace vmspace0;
92 struct	proc *initproc;
93 
94 int cmask = CMASK;
95 extern	struct user *proc0paddr;
96 extern int fallback_elf_brand;
97 
98 struct	vnode *rootvp;
99 int	boothowto = 0;		/* initialized so that it can be patched */
100 SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, "");
101 int	bootverbose;
102 SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0, "");
103 
104 /*
105  * This ensures that there is at least one entry so that the sysinit_set
106  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
107  * executed.
108  */
109 SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL)
110 
111 /*
112  * The sysinit table itself.  Items are checked off as the are run.
113  * If we want to register new sysinit types, add them to newsysinit.
114  */
115 struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items;
116 struct sysinit **newsysinit;
117 
118 /*
119  * Merge a new sysinit set into the current set, reallocating it if
120  * necessary.  This can only be called after malloc is running.
121  */
122 void
123 sysinit_add(struct sysinit **set)
124 {
125 	struct sysinit **newset;
126 	struct sysinit **sipp;
127 	struct sysinit **xipp;
128 	int count = 0;
129 
130 	if (newsysinit)
131 		for (sipp = newsysinit; *sipp; sipp++)
132 			count++;
133 	else
134 		for (sipp = sysinit; *sipp; sipp++)
135 			count++;
136 	for (sipp = set; *sipp; sipp++)
137 		count++;
138 	count++;		/* Trailing NULL */
139 	newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
140 	if (newset == NULL)
141 		panic("cannot malloc for sysinit");
142 	xipp = newset;
143 	if (newsysinit)
144 		for (sipp = newsysinit; *sipp; sipp++)
145 			*xipp++ = *sipp;
146 	else
147 		for (sipp = sysinit; *sipp; sipp++)
148 			*xipp++ = *sipp;
149 	for (sipp = set; *sipp; sipp++)
150 		*xipp++ = *sipp;
151 	*xipp = NULL;
152 	if (newsysinit)
153 		free(newsysinit, M_TEMP);
154 	newsysinit = newset;
155 }
156 
157 /*
158  * System startup; initialize the world, create process 0, mount root
159  * filesystem, and fork to create init and pagedaemon.  Most of the
160  * hard work is done in the lower-level initialization routines including
161  * startup(), which does memory initialization and autoconfiguration.
162  *
163  * This allows simple addition of new kernel subsystems that require
164  * boot time initialization.  It also allows substitution of subsystem
165  * (for instance, a scheduler, kernel profiler, or VM system) by object
166  * module.  Finally, it allows for optional "kernel threads".
167  */
168 void
169 mi_startup(void)
170 {
171 
172 	register struct sysinit **sipp;		/* system initialization*/
173 	register struct sysinit **xipp;		/* interior loop of sort*/
174 	register struct sysinit *save;		/* bubble*/
175 
176 restart:
177 	/*
178 	 * Perform a bubble sort of the system initialization objects by
179 	 * their subsystem (primary key) and order (secondary key).
180 	 */
181 	for (sipp = sysinit; *sipp; sipp++) {
182 		for (xipp = sipp + 1; *xipp; xipp++) {
183 			if ((*sipp)->subsystem < (*xipp)->subsystem ||
184 			     ((*sipp)->subsystem == (*xipp)->subsystem &&
185 			      (*sipp)->order <= (*xipp)->order))
186 				continue;	/* skip*/
187 			save = *sipp;
188 			*sipp = *xipp;
189 			*xipp = save;
190 		}
191 	}
192 
193 	/*
194 	 * Traverse the (now) ordered list of system initialization tasks.
195 	 * Perform each task, and continue on to the next task.
196 	 *
197 	 * The last item on the list is expected to be the scheduler,
198 	 * which will not return.
199 	 */
200 	for (sipp = sysinit; *sipp; sipp++) {
201 
202 		if ((*sipp)->subsystem == SI_SUB_DUMMY)
203 			continue;	/* skip dummy task(s)*/
204 
205 		if ((*sipp)->subsystem == SI_SUB_DONE)
206 			continue;
207 
208 		/* Call function */
209 		(*((*sipp)->func))((*sipp)->udata);
210 
211 		/* Check off the one we're just done */
212 		(*sipp)->subsystem = SI_SUB_DONE;
213 
214 		/* Check if we've installed more sysinit items via KLD */
215 		if (newsysinit != NULL) {
216 			if (sysinit != (struct sysinit **)sysinit_set.ls_items)
217 				free(sysinit, M_TEMP);
218 			sysinit = newsysinit;
219 			newsysinit = NULL;
220 			goto restart;
221 		}
222 	}
223 
224 	panic("Shouldn't get here!");
225 	/* NOTREACHED*/
226 }
227 
228 
229 /*
230  ***************************************************************************
231  ****
232  **** The following SYSINIT's belong elsewhere, but have not yet
233  **** been moved.
234  ****
235  ***************************************************************************
236  */
237 static void
238 print_caddr_t(void *data __unused)
239 {
240 	printf("%s", (char *)data);
241 }
242 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
243 SYSINIT(version, SI_SUB_COPYRIGHT, SI_ORDER_SECOND, print_caddr_t, version)
244 
245 static void
246 set_boot_verbose(void *data __unused)
247 {
248 
249 	if (boothowto & RB_VERBOSE)
250 		bootverbose++;
251 }
252 SYSINIT(boot_verbose, SI_SUB_TUNABLES, SI_ORDER_ANY, set_boot_verbose, NULL)
253 
254 /*
255  ***************************************************************************
256  ****
257  **** The two following SYSINT's are proc0 specific glue code.  I am not
258  **** convinced that they can not be safely combined, but their order of
259  **** operation has been maintained as the same as the original init_main.c
260  **** for right now.
261  ****
262  **** These probably belong in init_proc.c or kern_proc.c, since they
263  **** deal with proc0 (the fork template process).
264  ****
265  ***************************************************************************
266  */
267 /* ARGSUSED*/
268 static void
269 proc0_init(void *dummy __unused)
270 {
271 	register struct proc		*p;
272 	register struct filedesc0	*fdp;
273 	register unsigned i;
274 
275 	p = &proc0;
276 
277 	/*
278 	 * Initialize magic number.
279 	 */
280 	p->p_magic = P_MAGIC;
281 
282 	/*
283 	 * Initialize process and pgrp structures.
284 	 */
285 	procinit();
286 
287 	/*
288 	 * Initialize sleep queue hash table
289 	 */
290 	sleepinit();
291 
292 	/*
293 	 * additional VM structures
294 	 */
295 	vm_init2();
296 
297 	/*
298 	 * Create process 0 (the swapper).
299 	 */
300 	LIST_INSERT_HEAD(&allproc, p, p_list);
301 	LIST_INSERT_HEAD(PIDHASH(0), p, p_hash);
302 	p->p_pgrp = &pgrp0;
303 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
304 	LIST_INIT(&pgrp0.pg_members);
305 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
306 
307 	pgrp0.pg_session = &session0;
308 	session0.s_count = 1;
309 	session0.s_leader = p;
310 
311 #ifdef __ELF__
312 	p->p_sysent = &elf_freebsd_sysvec;
313 #else
314 	p->p_sysent = &aout_sysvec;
315 #endif
316 
317 	p->p_flag = P_SYSTEM;
318 	p->p_sflag = PS_INMEM;
319 	p->p_stat = SRUN;
320 	p->p_nice = NZERO;
321 	p->p_pri.pri_class = PRI_TIMESHARE;
322 	p->p_pri.pri_level = PVM;
323 	p->p_pri.pri_native = PUSER;
324 	p->p_pri.pri_user = PUSER;
325 
326 	p->p_peers = 0;
327 	p->p_leader = p;
328 
329 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
330 
331 	callout_init(&p->p_itcallout, 0);
332 	callout_init(&p->p_slpcallout, 1);
333 
334 	/* Create credentials. */
335 	p->p_ucred = crget();
336 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
337 	p->p_ucred->cr_uidinfo = uifind(0);
338 	p->p_ucred->cr_ruidinfo = uifind(0);
339 	p->p_ucred->cr_prison = NULL;	/* Don't jail it. */
340 
341 	/* Create procsig. */
342 	p->p_procsig = &procsig0;
343 	p->p_procsig->ps_refcnt = 1;
344 
345 	/* Initialize signal state for process 0. */
346 	siginit(&proc0);
347 
348 	/* Create the file descriptor table. */
349 	fdp = &filedesc0;
350 	p->p_fd = &fdp->fd_fd;
351 	fdp->fd_fd.fd_refcnt = 1;
352 	fdp->fd_fd.fd_cmask = cmask;
353 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
354 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
355 	fdp->fd_fd.fd_nfiles = NDFILE;
356 
357 	/* Create the limits structures. */
358 	p->p_limit = &limit0;
359 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
360 		limit0.pl_rlimit[i].rlim_cur =
361 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
362 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
363 	    limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
364 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
365 	    limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
366 	i = ptoa(cnt.v_free_count);
367 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
368 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
369 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
370 	limit0.p_cpulimit = RLIM_INFINITY;
371 	limit0.p_refcnt = 1;
372 
373 	/* Allocate a prototype map so we have something to fork. */
374 	mtx_lock(&vm_mtx);
375 	pmap_pinit0(vmspace_pmap(&vmspace0));
376 	p->p_vmspace = &vmspace0;
377 	vmspace0.vm_refcnt = 1;
378 	vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
379 	    trunc_page(VM_MAXUSER_ADDRESS));
380 	vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0);
381 	mtx_unlock(&vm_mtx);
382 	p->p_addr = proc0paddr;				/* XXX */
383 
384 	/*
385 	 * We continue to place resource usage info and signal
386 	 * actions in the user struct so they're pageable.
387 	 */
388 	p->p_stats = &p->p_addr->u_stats;
389 	p->p_sigacts = &p->p_addr->u_sigacts;
390 
391 	/*
392 	 * Charge root for one process.
393 	 */
394 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0);
395 }
396 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
397 
398 /* ARGSUSED*/
399 static void
400 proc0_post(void *dummy __unused)
401 {
402 	struct timespec ts;
403 	struct proc *p;
404 
405 	/*
406 	 * Now we can look at the time, having had a chance to verify the
407 	 * time from the file system.  Pretend that proc0 started now.
408 	 */
409 	sx_slock(&allproc_lock);
410 	LIST_FOREACH(p, &allproc, p_list) {
411 		microtime(&p->p_stats->p_start);
412 		p->p_runtime = 0;
413 	}
414 	sx_sunlock(&allproc_lock);
415 	microuptime(PCPU_PTR(switchtime));
416 	PCPU_SET(switchticks, ticks);
417 
418 	/*
419 	 * Give the ``random'' number generator a thump.
420 	 */
421 	nanotime(&ts);
422 	srandom(ts.tv_sec ^ ts.tv_nsec);
423 }
424 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
425 
426 /*
427  ***************************************************************************
428  ****
429  **** The following SYSINIT's and glue code should be moved to the
430  **** respective files on a per subsystem basis.
431  ****
432  ***************************************************************************
433  */
434 
435 
436 /*
437  ***************************************************************************
438  ****
439  **** The following code probably belongs in another file, like
440  **** kern/init_init.c.
441  ****
442  ***************************************************************************
443  */
444 
445 /*
446  * List of paths to try when searching for "init".
447  */
448 static char init_path[MAXPATHLEN] =
449 #ifdef	INIT_PATH
450     __XSTRING(INIT_PATH);
451 #else
452     "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall";
453 #endif
454 SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, "");
455 
456 /*
457  * Start the initial user process; try exec'ing each pathname in init_path.
458  * The program is invoked with one argument containing the boot flags.
459  */
460 static void
461 start_init(void *dummy)
462 {
463 	vm_offset_t addr;
464 	struct execve_args args;
465 	int options, error;
466 	char *var, *path, *next, *s;
467 	char *ucp, **uap, *arg0, *arg1;
468 	struct proc *p;
469 
470 	mtx_lock(&Giant);
471 
472 	p = curproc;
473 
474 	/* Get the vnode for '/'.  Set p->p_fd->fd_cdir to reference it. */
475 	if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode))
476 		panic("cannot find root vnode");
477 	p->p_fd->fd_cdir = rootvnode;
478 	VREF(p->p_fd->fd_cdir);
479 	p->p_fd->fd_rdir = rootvnode;
480 	VREF(p->p_fd->fd_rdir);
481 	VOP_UNLOCK(rootvnode, 0, p);
482 
483 	/*
484 	 * Need just enough stack to hold the faked-up "execve()" arguments.
485 	 */
486 	addr = trunc_page(USRSTACK - PAGE_SIZE);
487 	mtx_lock(&vm_mtx);
488 	if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE,
489 			FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
490 		panic("init: couldn't allocate argument space");
491 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
492 	p->p_vmspace->vm_ssize = 1;
493 	mtx_unlock(&vm_mtx);
494 
495 	if ((var = getenv("init_path")) != NULL) {
496 		strncpy(init_path, var, sizeof init_path);
497 		init_path[sizeof init_path - 1] = 0;
498 	}
499 	if ((var = getenv("kern.fallback_elf_brand")) != NULL)
500 		fallback_elf_brand = strtol(var, NULL, 0);
501 
502 	for (path = init_path; *path != '\0'; path = next) {
503 		while (*path == ':')
504 			path++;
505 		if (*path == '\0')
506 			break;
507 		for (next = path; *next != '\0' && *next != ':'; next++)
508 			/* nothing */ ;
509 		if (bootverbose)
510 			printf("start_init: trying %.*s\n", (int)(next - path),
511 			    path);
512 
513 		/*
514 		 * Move out the boot flag argument.
515 		 */
516 		options = 0;
517 		ucp = (char *)USRSTACK;
518 		(void)subyte(--ucp, 0);		/* trailing zero */
519 		if (boothowto & RB_SINGLE) {
520 			(void)subyte(--ucp, 's');
521 			options = 1;
522 		}
523 #ifdef notyet
524                 if (boothowto & RB_FASTBOOT) {
525 			(void)subyte(--ucp, 'f');
526 			options = 1;
527 		}
528 #endif
529 
530 #ifdef BOOTCDROM
531 		(void)subyte(--ucp, 'C');
532 		options = 1;
533 #endif
534 		if (devfs_present) {
535 			(void)subyte(--ucp, 'd');
536 			options = 1;
537 		}
538 
539 		if (options == 0)
540 			(void)subyte(--ucp, '-');
541 		(void)subyte(--ucp, '-');		/* leading hyphen */
542 		arg1 = ucp;
543 
544 		/*
545 		 * Move out the file name (also arg 0).
546 		 */
547 		(void)subyte(--ucp, 0);
548 		for (s = next - 1; s >= path; s--)
549 			(void)subyte(--ucp, *s);
550 		arg0 = ucp;
551 
552 		/*
553 		 * Move out the arg pointers.
554 		 */
555 		uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
556 		(void)suword((caddr_t)--uap, (long)0);	/* terminator */
557 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg1);
558 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg0);
559 
560 		/*
561 		 * Point at the arguments.
562 		 */
563 		args.fname = arg0;
564 		args.argv = uap;
565 		args.envv = NULL;
566 
567 		/*
568 		 * Now try to exec the program.  If can't for any reason
569 		 * other than it doesn't exist, complain.
570 		 *
571 		 * Otherwise, return via fork_trampoline() all the way
572 		 * to user mode as init!
573 		 */
574 		if ((error = execve(p, &args)) == 0) {
575 			mtx_unlock(&Giant);
576 			return;
577 		}
578 		if (error != ENOENT)
579 			printf("exec %.*s: error %d\n", (int)(next - path),
580 			    path, error);
581 	}
582 	printf("init: not found in path %s\n", init_path);
583 	panic("no init");
584 }
585 
586 /*
587  * Like kthread_create(), but runs in it's own address space.
588  * We do this early to reserve pid 1.
589  *
590  * Note special case - do not make it runnable yet.  Other work
591  * in progress will change this more.
592  */
593 static void
594 create_init(const void *udata __unused)
595 {
596 	int error;
597 
598 	error = fork1(&proc0, RFFDG | RFPROC | RFSTOPPED, &initproc);
599 	if (error)
600 		panic("cannot fork init: %d\n", error);
601 	PROC_LOCK(initproc);
602 	initproc->p_flag |= P_SYSTEM;
603 	PROC_UNLOCK(initproc);
604 	mtx_lock_spin(&sched_lock);
605 	initproc->p_sflag |= PS_INMEM;
606 	mtx_unlock_spin(&sched_lock);
607 	cpu_set_fork_handler(initproc, start_init, NULL);
608 }
609 SYSINIT(init, SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL)
610 
611 /*
612  * Make it runnable now.
613  */
614 static void
615 kick_init(const void *udata __unused)
616 {
617 
618 	mtx_lock_spin(&sched_lock);
619 	initproc->p_stat = SRUN;
620 	setrunqueue(initproc);
621 	mtx_unlock_spin(&sched_lock);
622 }
623 SYSINIT(kickinit, SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL)
624