init_main.c revision 1.194 1 /* $NetBSD: init_main.c,v 1.194 2001/07/18 00:51:54 matt Exp $ */
2
3 /*
4 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
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.16 (Berkeley) 5/14/95
42 */
43
44 #include "fs_nfs.h"
45 #include "opt_nfsserver.h"
46 #include "opt_sysv.h"
47 #include "opt_maxuprc.h"
48 #include "opt_multiprocessor.h"
49 #include "opt_new_pipe.h"
50 #include "opt_syscall_debug.h"
51
52 #include "rnd.h"
53
54 #include <sys/param.h>
55 #include <sys/acct.h>
56 #include <sys/filedesc.h>
57 #include <sys/file.h>
58 #include <sys/errno.h>
59 #include <sys/callout.h>
60 #include <sys/kernel.h>
61 #include <sys/mount.h>
62 #include <sys/map.h>
63 #include <sys/proc.h>
64 #include <sys/kthread.h>
65 #include <sys/resourcevar.h>
66 #include <sys/signalvar.h>
67 #include <sys/systm.h>
68 #include <sys/vnode.h>
69 #include <sys/tty.h>
70 #include <sys/conf.h>
71 #include <sys/disklabel.h>
72 #include <sys/buf.h>
73 #include <sys/device.h>
74 #include <sys/exec.h>
75 #include <sys/socketvar.h>
76 #include <sys/protosw.h>
77 #include <sys/reboot.h>
78 #include <sys/user.h>
79 #include <sys/sysctl.h>
80 #ifdef SYSVSHM
81 #include <sys/shm.h>
82 #endif
83 #ifdef SYSVSEM
84 #include <sys/sem.h>
85 #endif
86 #ifdef SYSVMSG
87 #include <sys/msg.h>
88 #endif
89 #include <sys/domain.h>
90 #include <sys/mbuf.h>
91 #include <sys/namei.h>
92 #if NRND > 0
93 #include <sys/rnd.h>
94 #endif
95 #ifdef NEW_PIPE
96 #include <sys/pipe.h>
97 #endif
98
99 #include <sys/syscall.h>
100 #include <sys/syscallargs.h>
101
102 #include <ufs/ufs/quota.h>
103
104 #include <miscfs/genfs/genfs.h>
105 #include <miscfs/syncfs/syncfs.h>
106
107 #include <machine/cpu.h>
108
109 #include <uvm/uvm.h>
110
111 #include <net/if.h>
112 #include <net/raw_cb.h>
113
114 const char copyright[] =
115 "Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001\n"
116 " The NetBSD Foundation, Inc. All rights reserved.\n"
117 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
118 " The Regents of the University of California. All rights reserved.\n"
119 "\n";
120
121 /* Components of the first process -- never freed. */
122 struct session session0;
123 struct pgrp pgrp0;
124 struct proc proc0;
125 struct pcred cred0;
126 struct filedesc0 filedesc0;
127 struct cwdinfo cwdi0;
128 struct plimit limit0;
129 struct vmspace vmspace0;
130 struct sigacts sigacts0;
131 #ifndef curproc
132 struct proc *curproc = &proc0;
133 #endif
134 struct proc *initproc;
135
136 int cmask = CMASK;
137 extern struct user *proc0paddr;
138
139 struct vnode *rootvp, *swapdev_vp;
140 int boothowto;
141 int cold = 1; /* still working on startup */
142 struct timeval boottime;
143
144 __volatile int start_init_exec; /* semaphore for start_init() */
145
146 static void check_console(struct proc *p);
147 static void start_init(void *);
148 void main(void);
149
150 extern const struct emul emul_netbsd; /* defined in kern_exec.c */
151
152 /*
153 * System startup; initialize the world, create process 0, mount root
154 * filesystem, and fork to create init and pagedaemon. Most of the
155 * hard work is done in the lower-level initialization routines including
156 * startup(), which does memory initialization and autoconfiguration.
157 */
158 void
159 main(void)
160 {
161 struct proc *p;
162 struct pdevinit *pdev;
163 int i, s, error;
164 rlim_t lim;
165 extern struct pdevinit pdevinit[];
166 extern void schedcpu(void *);
167 extern void disk_init(void);
168 #if defined(NFSSERVER) || defined(NFS)
169 extern void nfs_init(void);
170 #endif
171 #ifdef NVNODE_IMPLICIT
172 int usevnodes;
173 #endif
174
175 /*
176 * Initialize the current process pointer (curproc) before
177 * any possible traps/probes to simplify trap processing.
178 */
179 p = &proc0;
180 curproc = p;
181 p->p_cpu = curcpu();
182 /*
183 * Attempt to find console and initialize
184 * in case of early panic or other messages.
185 */
186 consinit();
187 printf("%s", copyright);
188
189 KERNEL_LOCK_INIT();
190
191 uvm_init();
192
193 /* Do machine-dependent initialization. */
194 cpu_startup();
195
196 /* Initialize callouts. */
197 callout_startup();
198
199 /*
200 * Initialize mbuf's. Do this now because we might attempt to
201 * allocate mbufs or mbuf clusters during autoconfiguration.
202 */
203 mbinit();
204
205 /* Initialize sockets. */
206 soinit();
207
208 /*
209 * The following 3 things must be done before autoconfiguration.
210 */
211 disk_init(); /* initialize disk list */
212 tty_init(); /* initialize tty list */
213 #if NRND > 0
214 rnd_init(); /* initialize RNG */
215 #endif
216
217 /* Initialize the sysctl subsystem. */
218 sysctl_init();
219
220 /*
221 * Initialize process and pgrp structures.
222 */
223 procinit();
224
225 /*
226 * Create process 0 (the swapper).
227 */
228 s = proclist_lock_write();
229 LIST_INSERT_HEAD(&allproc, p, p_list);
230 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
231 proclist_unlock_write(s);
232
233 p->p_pgrp = &pgrp0;
234 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
235 LIST_INIT(&pgrp0.pg_members);
236 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
237
238 pgrp0.pg_session = &session0;
239 session0.s_count = 1;
240 session0.s_sid = p->p_pid;
241 session0.s_leader = p;
242
243 /*
244 * Set P_NOCLDWAIT so that kernel threads are reparented to
245 * init(8) when they exit. init(8) can easily wait them out
246 * for us.
247 */
248 p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT;
249 p->p_stat = SONPROC;
250 p->p_nice = NZERO;
251 p->p_emul = &emul_netbsd;
252 #ifdef __HAVE_SYSCALL_INTERN
253 (*p->p_emul->e_syscall_intern)(p);
254 #endif
255 strncpy(p->p_comm, "swapper", MAXCOMLEN);
256
257 callout_init(&p->p_realit_ch);
258 callout_init(&p->p_tsleep_ch);
259
260 /* Create credentials. */
261 cred0.p_refcnt = 1;
262 p->p_cred = &cred0;
263 p->p_ucred = crget();
264 p->p_ucred->cr_ngroups = 1; /* group 0 */
265
266 /* Create the file descriptor table. */
267 finit();
268 p->p_fd = &filedesc0.fd_fd;
269 fdinit1(&filedesc0);
270
271 /* Create the CWD info. */
272 p->p_cwdi = &cwdi0;
273 cwdi0.cwdi_cmask = cmask;
274 cwdi0.cwdi_refcnt = 1;
275
276 /* Create the limits structures. */
277 p->p_limit = &limit0;
278 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
279 limit0.pl_rlimit[i].rlim_cur =
280 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
281
282 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
283 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
284 maxfiles < NOFILE ? maxfiles : NOFILE;
285
286 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
287 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
288 maxproc < MAXUPRC ? maxproc : MAXUPRC;
289
290 lim = ptoa(uvmexp.free);
291 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
292 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
293 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
294 limit0.pl_corename = defcorename;
295 limit0.p_refcnt = 1;
296
297 /*
298 * Initialize proc0's vmspace, which uses the kernel pmap.
299 * All kernel processes (which never have user space mappings)
300 * share proc0's vmspace, and thus, the kernel pmap.
301 */
302 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
303 trunc_page(VM_MAX_ADDRESS), TRUE);
304 p->p_vmspace = &vmspace0;
305
306 p->p_addr = proc0paddr; /* XXX */
307
308 /*
309 * We continue to place resource usage info in the
310 * user struct so they're pageable.
311 */
312 p->p_stats = &p->p_addr->u_stats;
313
314 /*
315 * Charge root for one process.
316 */
317 (void)chgproccnt(0, 1);
318
319 rqinit();
320
321 /* Configure virtual memory system, set vm rlimits. */
322 uvm_init_limits(p);
323
324 /* Initialize the file systems. */
325 #if defined(NFSSERVER) || defined(NFS)
326 nfs_init(); /* initialize server/shared data */
327 #endif
328 #ifdef NVNODE_IMPLICIT
329 /*
330 * If maximum number of vnodes in namei vnode cache is not explicitly
331 * defined in kernel config, adjust the number such as we use roughly
332 * 0.5% of memory for vnode cache (but not less than NVNODE vnodes).
333 */
334 usevnodes = (ptoa((unsigned)physmem) / 200) / sizeof(struct vnode);
335 if (usevnodes > desiredvnodes)
336 desiredvnodes = usevnodes;
337 #endif
338 vfsinit();
339
340 /* Configure the system hardware. This will enable interrupts. */
341 configure();
342
343 ubc_init(); /* must be after autoconfig */
344
345 /* Lock the kernel on behalf of proc0. */
346 KERNEL_PROC_LOCK(p);
347
348 #ifdef SYSVSHM
349 /* Initialize System V style shared memory. */
350 shminit();
351 #endif
352
353 #ifdef SYSVSEM
354 /* Initialize System V style semaphores. */
355 seminit();
356 #endif
357
358 #ifdef SYSVMSG
359 /* Initialize System V style message queues. */
360 msginit();
361 #endif
362
363 /* Attach pseudo-devices. */
364 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
365 (*pdev->pdev_attach)(pdev->pdev_count);
366
367 /*
368 * Initialize protocols. Block reception of incoming packets
369 * until everything is ready.
370 */
371 s = splnet();
372 ifinit();
373 domaininit();
374 splx(s);
375
376 #ifdef GPROF
377 /* Initialize kernel profiling. */
378 kmstartup();
379 #endif
380
381 /* Initialize system accouting. */
382 acct_init();
383
384 /*
385 * Initialize signal-related data structures, and signal state
386 * for proc0.
387 */
388 signal_init();
389 p->p_sigacts = &sigacts0;
390 siginit(p);
391
392 /* Kick off timeout driven events by calling first time. */
393 schedcpu(NULL);
394
395 /*
396 * Create process 1 (init(8)). We do this now, as Unix has
397 * historically had init be process 1, and changing this would
398 * probably upset a lot of people.
399 *
400 * Note that process 1 won't immediately exec init(8), but will
401 * wait for us to inform it that the root file system has been
402 * mounted.
403 */
404 if (fork1(p, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
405 panic("fork init");
406
407 /*
408 * Create any kernel threads who's creation was deferred because
409 * initproc had not yet been created.
410 */
411 kthread_run_deferred_queue();
412
413 /*
414 * Now that device driver threads have been created, wait for
415 * them to finish any deferred autoconfiguration. Note we don't
416 * need to lock this semaphore, since we haven't booted any
417 * secondary processors, yet.
418 */
419 while (config_pending)
420 (void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
421
422 /*
423 * Now that autoconfiguration has completed, we can determine
424 * the root and dump devices.
425 */
426 cpu_rootconf();
427 cpu_dumpconf();
428
429 /* Mount the root file system. */
430 do {
431 domountroothook();
432 if ((error = vfs_mountroot())) {
433 printf("cannot mount root, error = %d\n", error);
434 boothowto |= RB_ASKNAME;
435 setroot(root_device,
436 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
437 }
438 } while (error != 0);
439 mountroothook_destroy();
440
441 mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
442 mountlist.cqh_first->mnt_op->vfs_refcount++;
443
444 /*
445 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
446 * reference it.
447 */
448 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
449 panic("cannot find root vnode");
450 cwdi0.cwdi_cdir = rootvnode;
451 VREF(cwdi0.cwdi_cdir);
452 VOP_UNLOCK(rootvnode, 0);
453 cwdi0.cwdi_rdir = NULL;
454
455 /*
456 * Now that root is mounted, we can fixup initproc's CWD
457 * info. All other processes are kthreads, which merely
458 * share proc0's CWD info.
459 */
460 initproc->p_cwdi->cwdi_cdir = rootvnode;
461 VREF(initproc->p_cwdi->cwdi_cdir);
462 initproc->p_cwdi->cwdi_rdir = NULL;
463
464 /*
465 * Now can look at time, having had a chance to verify the time
466 * from the file system. Reset p->p_rtime as it may have been
467 * munched in mi_switch() after the time got set.
468 */
469 proclist_lock_read();
470 s = splsched();
471 for (p = LIST_FIRST(&allproc); p != NULL;
472 p = LIST_NEXT(p, p_list)) {
473 p->p_stats->p_start = mono_time = boottime = time;
474 if (p->p_cpu != NULL)
475 p->p_cpu->ci_schedstate.spc_runtime = time;
476 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
477 }
478 splx(s);
479 proclist_unlock_read();
480
481 /* Create the pageout daemon kernel thread. */
482 uvm_swap_init();
483 if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
484 panic("fork pagedaemon");
485
486 /* Create the process reaper kernel thread. */
487 if (kthread_create1(reaper, NULL, NULL, "reaper"))
488 panic("fork reaper");
489
490 /* Create the filesystem syncer kernel thread. */
491 if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
492 panic("fork syncer");
493
494 /* Create the aiodone daemon kernel thread. */
495 if (kthread_create1(uvm_aiodone_daemon, NULL, NULL, "aiodoned"))
496 panic("fork aiodoned");
497
498 #if defined(MULTIPROCESSOR)
499 /* Boot the secondary processors. */
500 cpu_boot_secondary_processors();
501 #endif
502
503 /* Initialize exec structures */
504 exec_init(1);
505
506 #ifdef NEW_PIPE
507 /* Initialize pipe structures */
508 pipe_init();
509 #endif
510
511 /*
512 * Okay, now we can let init(8) exec! It's off to userland!
513 */
514 start_init_exec = 1;
515 wakeup((void *)&start_init_exec);
516
517 /* The scheduler is an infinite loop. */
518 uvm_scheduler();
519 /* NOTREACHED */
520 }
521
522 static void
523 check_console(struct proc *p)
524 {
525 struct nameidata nd;
526 int error;
527
528 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
529 error = namei(&nd);
530 if (error == 0)
531 vrele(nd.ni_vp);
532 else if (error == ENOENT)
533 printf("warning: no /dev/console\n");
534 else
535 printf("warning: lookup /dev/console: error %d\n", error);
536 }
537
538 /*
539 * List of paths to try when searching for "init".
540 */
541 static const char *initpaths[] = {
542 "/sbin/init",
543 "/sbin/oinit",
544 "/sbin/init.bak",
545 NULL,
546 };
547
548 /*
549 * Start the initial user process; try exec'ing each pathname in "initpaths".
550 * The program is invoked with one argument containing the boot flags.
551 */
552 static void
553 start_init(void *arg)
554 {
555 struct proc *p = arg;
556 vaddr_t addr;
557 struct sys_execve_args /* {
558 syscallarg(const char *) path;
559 syscallarg(char * const *) argp;
560 syscallarg(char * const *) envp;
561 } */ args;
562 int options, i, error;
563 register_t retval[2];
564 char flags[4], *flagsp;
565 const char **pathp, *path, *slash;
566 char *ucp, **uap, *arg0, *arg1 = NULL;
567
568 /*
569 * Now in process 1.
570 */
571 strncpy(p->p_comm, "init", MAXCOMLEN);
572
573 /*
574 * Wait for main() to tell us that it's safe to exec.
575 */
576 while (start_init_exec == 0)
577 (void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
578
579 /*
580 * This is not the right way to do this. We really should
581 * hand-craft a descriptor onto /dev/console to hand to init,
582 * but that's a _lot_ more work, and the benefit from this easy
583 * hack makes up for the "good is the enemy of the best" effect.
584 */
585 check_console(p);
586
587 /*
588 * Need just enough stack to hold the faked-up "execve()" arguments.
589 */
590 addr = USRSTACK - PAGE_SIZE;
591 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
592 NULL, UVM_UNKNOWN_OFFSET, 0,
593 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
594 UVM_ADV_NORMAL,
595 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
596 panic("init: couldn't allocate argument space");
597 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
598
599 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
600 ucp = (char *)(addr + PAGE_SIZE);
601
602 /*
603 * Construct the boot flag argument.
604 */
605 flagsp = flags;
606 *flagsp++ = '-';
607 options = 0;
608
609 if (boothowto & RB_SINGLE) {
610 *flagsp++ = 's';
611 options = 1;
612 }
613 #ifdef notyet
614 if (boothowto & RB_FASTBOOT) {
615 *flagsp++ = 'f';
616 options = 1;
617 }
618 #endif
619
620 /*
621 * Move out the flags (arg 1), if necessary.
622 */
623 if (options != 0) {
624 *flagsp++ = '\0';
625 i = flagsp - flags;
626 #ifdef DEBUG
627 printf("init: copying out flags `%s' %d\n", flags, i);
628 #endif
629 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
630 arg1 = ucp;
631 }
632
633 /*
634 * Move out the file name (also arg 0).
635 */
636 i = strlen(path) + 1;
637 #ifdef DEBUG
638 printf("init: copying out path `%s' %d\n", path, i);
639 #endif
640 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
641 arg0 = ucp;
642
643 /*
644 * Move out the arg pointers.
645 */
646 uap = (char **)((long)ucp & ~ALIGNBYTES);
647 (void)suword((caddr_t)--uap, 0); /* terminator */
648 if (options != 0)
649 (void)suword((caddr_t)--uap, (long)arg1);
650 slash = strrchr(path, '/');
651 if (slash)
652 (void)suword((caddr_t)--uap,
653 (long)arg0 + (slash + 1 - path));
654 else
655 (void)suword((caddr_t)--uap, (long)arg0);
656
657 /*
658 * Point at the arguments.
659 */
660 SCARG(&args, path) = arg0;
661 SCARG(&args, argp) = uap;
662 SCARG(&args, envp) = NULL;
663
664 /*
665 * Now try to exec the program. If can't for any reason
666 * other than it doesn't exist, complain.
667 */
668 error = sys_execve(p, &args, retval);
669 if (error == 0 || error == EJUSTRETURN) {
670 KERNEL_PROC_UNLOCK(p);
671 return;
672 }
673 if (error != ENOENT)
674 printf("exec %s: error %d\n", path, error);
675 }
676 printf("init: not found\n");
677 panic("no init");
678 }
679