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