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