Annotation of src/sys/kern/init_main.c, Revision 1.248.2.8
1.248.2.8! yamt 1: /* $NetBSD: init_main.c,v 1.248.2.7 2007/12/07 17:32:35 yamt Exp $ */
1.61 mycroft 2:
3: /*
4: * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
5: * The Regents of the University of California. All rights reserved.
6: * (c) UNIX System Laboratories, Inc.
7: * All or some portions of this file are derived from material licensed
8: * to the University of California by American Telephone and Telegraph
9: * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10: * the permission of UNIX System Laboratories, Inc.
11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
15: * 1. Redistributions of source code must retain the above copyright
16: * notice, this list of conditions and the following disclaimer.
17: * 2. Redistributions in binary form must reproduce the above copyright
18: * notice, this list of conditions and the following disclaimer in the
19: * documentation and/or other materials provided with the distribution.
1.224 agc 20: * 3. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: *
36: * @(#)init_main.c 8.16 (Berkeley) 5/14/95
37: */
38:
39: /*
40: * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
41: *
42: * Redistribution and use in source and binary forms, with or without
43: * modification, are permitted provided that the following conditions
44: * are met:
45: * 1. Redistributions of source code must retain the above copyright
46: * notice, this list of conditions and the following disclaimer.
47: * 2. Redistributions in binary form must reproduce the above copyright
48: * notice, this list of conditions and the following disclaimer in the
49: * documentation and/or other materials provided with the distribution.
1.61 mycroft 50: * 3. All advertising materials mentioning features or use of this software
51: * must display the following acknowledgement:
52: * This product includes software developed by the University of
53: * California, Berkeley and its contributors.
54: * 4. Neither the name of the University nor the names of its contributors
55: * may be used to endorse or promote products derived from this software
56: * without specific prior written permission.
57: *
58: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68: * SUCH DAMAGE.
69: *
1.118 fvdl 70: * @(#)init_main.c 8.16 (Berkeley) 5/14/95
1.61 mycroft 71: */
1.196 lukem 72:
73: #include <sys/cdefs.h>
1.248.2.8! yamt 74: __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.248.2.7 2007/12/07 17:32:35 yamt Exp $");
1.115 mrg 75:
1.223 jonathan 76: #include "opt_ipsec.h"
1.248.2.1 yamt 77: #include "opt_ntp.h"
1.198 jdolecek 78: #include "opt_pipe.h"
1.217 christos 79: #include "opt_posix.h"
1.248.2.1 yamt 80: #include "opt_syscall_debug.h"
81: #include "opt_sysv.h"
1.248.2.3 yamt 82: #include "opt_fileassoc.h"
83: #include "opt_ktrace.h"
1.248.2.2 yamt 84: #include "opt_pax.h"
1.61 mycroft 85:
1.106 explorer 86: #include "rnd.h"
1.248.2.4 yamt 87: #include "sysmon_envsys.h"
88: #include "sysmon_power.h"
89: #include "sysmon_taskq.h"
90: #include "sysmon_wdog.h"
1.248.2.2 yamt 91: #include "veriexec.h"
1.106 explorer 92:
1.61 mycroft 93: #include <sys/param.h>
1.164 enami 94: #include <sys/acct.h>
1.61 mycroft 95: #include <sys/filedesc.h>
1.131 thorpej 96: #include <sys/file.h>
1.61 mycroft 97: #include <sys/errno.h>
1.162 thorpej 98: #include <sys/callout.h>
1.248.2.4 yamt 99: #include <sys/cpu.h>
1.61 mycroft 100: #include <sys/kernel.h>
1.248.2.2 yamt 101: #include <sys/kmem.h>
1.61 mycroft 102: #include <sys/mount.h>
103: #include <sys/proc.h>
1.136 thorpej 104: #include <sys/kthread.h>
1.61 mycroft 105: #include <sys/resourcevar.h>
106: #include <sys/signalvar.h>
107: #include <sys/systm.h>
108: #include <sys/vnode.h>
1.248.2.3 yamt 109: #include <sys/fstrans.h>
1.87 thorpej 110: #include <sys/tty.h>
1.61 mycroft 111: #include <sys/conf.h>
1.95 thorpej 112: #include <sys/disklabel.h>
1.61 mycroft 113: #include <sys/buf.h>
114: #include <sys/device.h>
1.186 jdolecek 115: #include <sys/exec.h>
1.128 thorpej 116: #include <sys/socketvar.h>
1.61 mycroft 117: #include <sys/protosw.h>
1.248.2.8! yamt 118: #include <sys/percpu.h>
! 119: #include <sys/sysctl.h>
1.61 mycroft 120: #include <sys/reboot.h>
121: #include <sys/user.h>
1.176 thorpej 122: #include <sys/sysctl.h>
1.209 jdolecek 123: #include <sys/event.h>
1.227 jonathan 124: #include <sys/mbuf.h>
1.248.2.4 yamt 125: #include <sys/sched.h>
1.248.2.3 yamt 126: #include <sys/sleepq.h>
1.248.2.2 yamt 127: #include <sys/iostat.h>
1.248.2.4 yamt 128: #include <sys/vmem.h>
129: #include <sys/uuid.h>
130: #include <sys/extent.h>
131: #include <sys/disk.h>
1.248.2.5 yamt 132: #include <sys/mqueue.h>
1.248.2.6 yamt 133: #include <sys/msgbuf.h>
1.248.2.8! yamt 134: #include <sys/module.h>
1.233 junyoung 135: #ifdef FAST_IPSEC
1.223 jonathan 136: #include <netipsec/ipsec.h>
137: #endif
1.82 christos 138: #ifdef SYSVSHM
139: #include <sys/shm.h>
140: #endif
1.233 junyoung 141: #ifdef SYSVSEM
1.82 christos 142: #include <sys/sem.h>
143: #endif
144: #ifdef SYSVMSG
145: #include <sys/msg.h>
146: #endif
1.217 christos 147: #ifdef P1003_1B_SEMAPHORE
148: #include <sys/ksem.h>
149: #endif
1.82 christos 150: #include <sys/domain.h>
1.94 mouse 151: #include <sys/namei.h>
1.106 explorer 152: #if NRND > 0
1.103 explorer 153: #include <sys/rnd.h>
1.106 explorer 154: #endif
1.192 jdolecek 155: #include <sys/pipe.h>
1.210 thorpej 156: #ifdef LKM
157: #include <sys/lkm.h>
158: #endif
1.248.2.2 yamt 159: #if NVERIEXEC > 0
1.245 blymn 160: #include <sys/verified_exec.h>
1.248.2.2 yamt 161: #endif /* NVERIEXEC > 0 */
1.248.2.3 yamt 162: #ifdef KTRACE
163: #include <sys/ktrace.h>
164: #endif
1.248.2.1 yamt 165: #include <sys/kauth.h>
166: #include <net80211/ieee80211_netbsd.h>
1.61 mycroft 167:
1.77 christos 168: #include <sys/syscall.h>
1.68 cgd 169: #include <sys/syscallargs.h>
170:
1.248.2.8! yamt 171: #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
1.248.2.2 yamt 172: #include <sys/pax.h>
1.248.2.8! yamt 173: #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
! 174:
1.61 mycroft 175: #include <ufs/ufs/quota.h>
176:
1.159 fvdl 177: #include <miscfs/genfs/genfs.h>
178: #include <miscfs/syncfs/syncfs.h>
179:
1.248.2.5 yamt 180: #include <sys/cpu.h>
1.61 mycroft 181:
1.114 mrg 182: #include <uvm/uvm.h>
183:
1.248.2.4 yamt 184: #if NSYSMON_TASKQ > 0
185: #include <dev/sysmon/sysmon_taskq.h>
186: #endif
187:
1.202 lukem 188: #include <dev/cons.h>
189:
1.248.2.4 yamt 190: #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
191: #include <dev/sysmon/sysmonvar.h>
192: #endif
193:
1.81 christos 194: #include <net/if.h>
195: #include <net/raw_cb.h>
1.61 mycroft 196:
1.248.2.2 yamt 197: #include <secmodel/secmodel.h>
198:
1.248.2.1 yamt 199: extern struct proc proc0;
200: extern struct lwp lwp0;
201: extern struct cwdinfo cwdi0;
1.248.2.4 yamt 202: extern time_t rootfstime;
1.248.2.1 yamt 203:
1.216 thorpej 204: #ifndef curlwp
205: struct lwp *curlwp = &lwp0;
1.133 pk 206: #endif
1.105 mycroft 207: struct proc *initproc;
1.61 mycroft 208:
209: struct vnode *rootvp, *swapdev_vp;
210: int boothowto;
1.156 thorpej 211: int cold = 1; /* still working on startup */
1.248.2.2 yamt 212: struct timeval boottime; /* time at system startup - will only follow settime deltas */
1.61 mycroft 213:
1.248.2.1 yamt 214: volatile int start_init_exec; /* semaphore for start_init() */
1.163 thorpej 215:
1.248.2.1 yamt 216: static void check_console(struct lwp *l);
1.176 thorpej 217: static void start_init(void *);
218: void main(void);
1.76 cgd 219:
1.248.2.2 yamt 220: #if defined(__SSP__) || defined(__SSP_ALL__)
221: long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
222: void __stack_chk_fail(void);
223:
224: void
225: __stack_chk_fail(void)
226: {
227: panic("stack overflow detected; terminated");
228: }
229: #endif
230:
1.248.2.3 yamt 231: void __secmodel_none(void);
1.248.2.4 yamt 232: __weak_alias(secmodel_start,__secmodel_none);
1.248.2.3 yamt 233: void
234: __secmodel_none(void)
235: {
236: return;
237: }
238:
1.61 mycroft 239: /*
240: * System startup; initialize the world, create process 0, mount root
241: * filesystem, and fork to create init and pagedaemon. Most of the
242: * hard work is done in the lower-level initialization routines including
243: * startup(), which does memory initialization and autoconfiguration.
244: */
1.81 christos 245: void
1.176 thorpej 246: main(void)
1.61 mycroft 247: {
1.248.2.1 yamt 248: struct timeval time;
1.216 thorpej 249: struct lwp *l;
1.144 thorpej 250: struct proc *p;
1.111 thorpej 251: struct pdevinit *pdev;
1.204 thorpej 252: int s, error;
1.61 mycroft 253: extern struct pdevinit pdevinit[];
1.175 jdolecek 254: #ifdef NVNODE_IMPLICIT
255: int usevnodes;
256: #endif
1.248.2.3 yamt 257: CPU_INFO_ITERATOR cii;
258: struct cpu_info *ci;
1.61 mycroft 259:
1.216 thorpej 260: l = &lwp0;
1.248.2.6 yamt 261: #ifndef LWP0_CPU_INFO
1.216 thorpej 262: l->l_cpu = curcpu();
1.248.2.6 yamt 263: #endif
1.233 junyoung 264:
1.61 mycroft 265: /*
266: * Attempt to find console and initialize
267: * in case of early panic or other messages.
268: */
269: consinit();
270:
1.248.2.5 yamt 271: kernel_lock_init();
1.179 thorpej 272:
1.114 mrg 273: uvm_init();
1.127 thorpej 274:
1.248.2.2 yamt 275: kmem_init();
276:
1.248.2.8! yamt 277: percpu_init();
! 278:
1.248.2.4 yamt 279: /* Initialize the extent manager. */
280: extent_init();
281:
1.145 thorpej 282: /* Do machine-dependent initialization. */
283: cpu_startup();
1.162 thorpej 284:
1.248.2.8! yamt 285: /* Start module system. */
! 286: module_init();
! 287:
1.248.2.4 yamt 288: /* Initialize callouts, part 1. */
1.166 enami 289: callout_startup();
1.145 thorpej 290:
1.248.2.3 yamt 291: /*
292: * Initialize the kernel authorization subsystem and start the
293: * default security model, if any. We need to do this early
294: * enough so that subsystems relying on any of the aforementioned
295: * can work properly. Since the security model may dictate the
296: * credential inheritance policy, it is needed at least before
297: * any process is created, specifically proc0.
298: */
299: kauth_init();
300: secmodel_start();
301:
1.228 pk 302: /* Initialize the buffer cache */
303: bufinit();
304:
1.128 thorpej 305: /* Initialize sockets. */
306: soinit();
1.127 thorpej 307:
1.156 thorpej 308: /*
1.212 thorpej 309: * The following things must be done before autoconfiguration.
1.156 thorpej 310: */
1.212 thorpej 311: evcnt_init(); /* initialize event counters */
1.106 explorer 312: #if NRND > 0
1.156 thorpej 313: rnd_init(); /* initialize RNG */
1.106 explorer 314: #endif
1.176 thorpej 315:
1.236 simonb 316: /* Initialize process and pgrp structures. */
1.63 mycroft 317: procinit();
1.248.2.2 yamt 318: lwpinit();
1.210 thorpej 319:
1.248.2.1 yamt 320: /* Initialize signal-related data structures. */
321: signal_init();
1.61 mycroft 322:
1.248.2.8! yamt 323: /* Initialize resource management. */
! 324: resource_init();
! 325:
1.248.2.1 yamt 326: /* Create process 0 (the swapper). */
327: proc0_init();
1.61 mycroft 328:
1.248.2.4 yamt 329: /* Initialize the UID hash table. */
330: uid_init();
331:
332: /* Charge root for one process. */
1.61 mycroft 333: (void)chgproccnt(0, 1);
334:
1.248.2.7 yamt 335: /* Initialize timekeeping. */
336: time_init();
337:
1.248.2.3 yamt 338: /* Initialize the run queues, turnstiles and sleep queues. */
1.248.2.4 yamt 339: mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
340: sched_rqinit();
1.248.2.3 yamt 341: turnstile_init();
342: sleeptab_init(&sleeptab);
343:
1.248.2.8! yamt 344: /* Initialize processor-sets */
! 345: psets_init();
! 346:
1.248.2.4 yamt 347: /* MI initialization of the boot cpu */
348: error = mi_cpu_attach(curcpu());
349: KASSERT(error == 0);
350:
1.248.2.8! yamt 351: /*
! 352: * Initialize mbuf's. Do this now because we might attempt to
! 353: * allocate mbufs or mbuf clusters during autoconfiguration.
! 354: */
! 355: mbinit();
! 356:
1.248.2.3 yamt 357: /* Initialize the sysctl subsystem. */
358: sysctl_init();
1.61 mycroft 359:
1.248.2.2 yamt 360: /* Initialize I/O statistics. */
361: iostat_init();
362:
1.248.2.6 yamt 363: /* Initialize the log device. */
364: loginit();
365:
1.61 mycroft 366: /* Initialize the file systems. */
1.194 matt 367: #ifdef NVNODE_IMPLICIT
368: /*
369: * If maximum number of vnodes in namei vnode cache is not explicitly
370: * defined in kernel config, adjust the number such as we use roughly
1.232 tls 371: * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
1.194 matt 372: */
1.232 tls 373: usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
1.205 sommerfe 374: if (usevnodes > desiredvnodes)
1.194 matt 375: desiredvnodes = usevnodes;
376: #endif
1.61 mycroft 377: vfsinit();
378:
1.248.2.3 yamt 379: /* Initialize fstrans. */
380: fstrans_init();
1.248.2.1 yamt 381:
1.248.2.5 yamt 382: /* Initialize the file descriptor system. */
383: filedesc_init();
384:
1.248.2.4 yamt 385: /* Initialize the select()/poll() system calls. */
386: selsysinit();
387:
388: /* Initialize asynchronous I/O. */
389: aio_sysinit();
390:
1.248.2.5 yamt 391: /* Initialize message queues. */
392: mqueue_sysinit();
393:
1.248.2.4 yamt 394: /* Initialize the system monitor subsystems. */
395: #if NSYSMON_TASKQ > 0
396: sysmon_task_queue_preinit();
397: #endif
398:
399: #if NSYSMON_ENVSYS > 0
400: sysmon_envsys_init();
401: #endif
402:
403: #if NSYSMON_POWER > 0
404: sysmon_power_init();
405: #endif
406:
407: #if NSYSMON_WDOG > 0
408: sysmon_wdog_init();
409: #endif
410:
1.248.2.1 yamt 411: inittimecounter();
412: ntp_init();
413:
1.248.2.4 yamt 414: /* Initialize the device switch tables. */
415: devsw_init();
416:
1.248.2.6 yamt 417: /* Initialize tty subsystem. */
418: tty_init();
419: ttyldisc_init();
420:
1.248.2.8! yamt 421: /* Initialize the buffer cache, part 2. */
! 422: bufinit2();
! 423:
1.248.2.4 yamt 424: /* Initialize the disk wedge subsystem. */
425: dkwedge_init();
426:
1.156 thorpej 427: /* Configure the system hardware. This will enable interrupts. */
428: configure();
1.61 mycroft 429:
1.248.2.2 yamt 430: #if defined(__SSP__) || defined(__SSP_ALL__)
431: {
432: #ifdef DIAGNOSTIC
433: printf("Initializing SSP:");
434: #endif
435: /*
436: * We initialize ssp here carefully:
437: * 1. after we got some entropy
438: * 2. without calling a function
439: */
440: size_t i;
441: long guard[__arraycount(__stack_chk_guard)];
442:
443: arc4randbytes(guard, sizeof(guard));
444: for (i = 0; i < __arraycount(guard); i++)
445: __stack_chk_guard[i] = guard[i];
446: #ifdef DIAGNOSTIC
447: for (i = 0; i < __arraycount(guard); i++)
448: printf("%lx ", guard[i]);
449: printf("\n");
450: #endif
451: }
452: #endif
1.185 chs 453: ubc_init(); /* must be after autoconfig */
454:
1.179 thorpej 455: /* Lock the kernel on behalf of proc0. */
1.248.2.3 yamt 456: KERNEL_LOCK(1, l);
457:
1.61 mycroft 458: #ifdef SYSVSHM
459: /* Initialize System V style shared memory. */
460: shminit();
461: #endif
462:
463: #ifdef SYSVSEM
464: /* Initialize System V style semaphores. */
465: seminit();
466: #endif
467:
468: #ifdef SYSVMSG
469: /* Initialize System V style message queues. */
470: msginit();
471: #endif
472:
1.217 christos 473: #ifdef P1003_1B_SEMAPHORE
474: /* Initialize posix semaphores */
475: ksem_init();
476: #endif
1.245 blymn 477:
1.248.2.2 yamt 478: #if NVERIEXEC > 0
479: /*
480: * Initialise the Veriexec subsystem.
481: */
482: veriexec_init();
483: #endif /* NVERIEXEC > 0 */
484:
1.248.2.8! yamt 485: #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
1.248.2.2 yamt 486: pax_init();
1.248.2.8! yamt 487: #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
1.248.2.1 yamt 488:
1.61 mycroft 489: /* Attach pseudo-devices. */
490: for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
491: (*pdev->pdev_attach)(pdev->pdev_count);
1.223 jonathan 492:
493: #ifdef FAST_IPSEC
494: /* Attach network crypto subsystem */
495: ipsec_attach();
496: #endif
1.61 mycroft 497:
498: /*
499: * Initialize protocols. Block reception of incoming packets
500: * until everything is ready.
501: */
1.190 thorpej 502: s = splnet();
1.61 mycroft 503: ifinit();
504: domaininit();
1.200 itojun 505: if_attachdomain();
1.61 mycroft 506: splx(s);
507:
508: #ifdef GPROF
509: /* Initialize kernel profiling. */
510: kmstartup();
511: #endif
1.164 enami 512:
513: /* Initialize system accouting. */
514: acct_init();
1.61 mycroft 515:
1.248.2.4 yamt 516: #ifndef PIPE_SOCKETPAIR
517: /* Initialize pipes. */
518: pipe_init();
519: #endif
520:
521: /* Setup the scheduler */
522: sched_init();
1.98 gwr 523:
1.248.2.3 yamt 524: #ifdef KTRACE
525: /* Initialize ktrace. */
526: ktrinit();
527: #endif
528:
1.248.2.4 yamt 529: /* Initialize the UUID system calls. */
530: uuid_init();
531:
1.163 thorpej 532: /*
533: * Create process 1 (init(8)). We do this now, as Unix has
534: * historically had init be process 1, and changing this would
535: * probably upset a lot of people.
536: *
537: * Note that process 1 won't immediately exec init(8), but will
538: * wait for us to inform it that the root file system has been
539: * mounted.
540: */
1.216 thorpej 541: if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
1.163 thorpej 542: panic("fork init");
543:
544: /*
545: * Now that device driver threads have been created, wait for
1.248.2.6 yamt 546: * them to finish any deferred autoconfiguration.
1.163 thorpej 547: */
548: while (config_pending)
1.248.2.6 yamt 549: (void) tsleep(&config_pending, PWAIT, "cfpend", hz);
1.208 thorpej 550:
551: /*
1.248.2.8! yamt 552: * Load any remaining builtin modules, and hand back temporary
! 553: * storage to the VM system.
! 554: */
! 555: module_init_class(MODULE_CLASS_ANY);
! 556: module_jettison();
! 557:
! 558: /*
1.208 thorpej 559: * Finalize configuration now that all real devices have been
560: * found. This needs to be done before the root device is
561: * selected, since finalization may create the root device.
562: */
563: config_finalize();
1.163 thorpej 564:
565: /*
566: * Now that autoconfiguration has completed, we can determine
567: * the root and dump devices.
568: */
1.98 gwr 569: cpu_rootconf();
1.101 thorpej 570: cpu_dumpconf();
1.61 mycroft 571:
572: /* Mount the root file system. */
1.95 thorpej 573: do {
574: domountroothook();
575: if ((error = vfs_mountroot())) {
1.97 thorpej 576: printf("cannot mount root, error = %d\n", error);
1.95 thorpej 577: boothowto |= RB_ASKNAME;
578: setroot(root_device,
1.152 thorpej 579: (rootdev != NODEV) ? DISKPART(rootdev) : 0);
1.95 thorpej 580: }
581: } while (error != 0);
582: mountroothook_destroy();
583:
1.239 pk 584: /*
585: * Initialise the time-of-day clock, passing the time recorded
586: * in the root filesystem (if any) for use by systems that
587: * don't have a non-volatile time-of-day device.
588: */
589: inittodr(rootfstime);
590:
1.206 matt 591: CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
592: CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
1.61 mycroft 593:
1.111 thorpej 594: /*
595: * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
596: * reference it.
597: */
1.243 mycroft 598: error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
599: if (error)
600: panic("cannot find root vnode, error=%d", error);
1.148 thorpej 601: cwdi0.cwdi_cdir = rootvnode;
602: VREF(cwdi0.cwdi_cdir);
1.118 fvdl 603: VOP_UNLOCK(rootvnode, 0);
1.148 thorpej 604: cwdi0.cwdi_rdir = NULL;
1.163 thorpej 605:
606: /*
607: * Now that root is mounted, we can fixup initproc's CWD
608: * info. All other processes are kthreads, which merely
609: * share proc0's CWD info.
610: */
611: initproc->p_cwdi->cwdi_cdir = rootvnode;
612: VREF(initproc->p_cwdi->cwdi_cdir);
613: initproc->p_cwdi->cwdi_rdir = NULL;
1.61 mycroft 614:
615: /*
616: * Now can look at time, having had a chance to verify the time
1.248.2.3 yamt 617: * from the file system. Reset l->l_rtime as it may have been
1.61 mycroft 618: * munched in mi_switch() after the time got set.
619: */
1.248.2.1 yamt 620: getmicrotime(&time);
621: boottime = time;
1.248.2.4 yamt 622: mutex_enter(&proclist_lock);
1.225 jdolecek 623: LIST_FOREACH(p, &allproc, p_list) {
1.248.2.3 yamt 624: KASSERT((p->p_flag & PK_MARKER) == 0);
625: mutex_enter(&p->p_smutex);
1.248.2.1 yamt 626: p->p_stats->p_start = time;
1.225 jdolecek 627: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.248.2.3 yamt 628: lwp_lock(l);
1.248.2.8! yamt 629: memset(&l->l_rtime, 0, sizeof(l->l_rtime));
1.248.2.3 yamt 630: lwp_unlock(l);
1.225 jdolecek 631: }
1.248.2.3 yamt 632: mutex_exit(&p->p_smutex);
1.163 thorpej 633: }
1.248.2.4 yamt 634: mutex_exit(&proclist_lock);
1.248.2.8! yamt 635: binuptime(&curlwp->l_stime);
1.248.2.4 yamt 636:
637: for (CPU_INFO_FOREACH(cii, ci)) {
638: ci->ci_schedstate.spc_lastmod = time_second;
639: }
1.61 mycroft 640:
1.163 thorpej 641: /* Create the pageout daemon kernel thread. */
642: uvm_swap_init();
1.248.2.8! yamt 643: if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
1.248.2.4 yamt 644: NULL, NULL, "pgdaemon"))
1.135 thorpej 645: panic("fork pagedaemon");
1.61 mycroft 646:
1.163 thorpej 647: /* Create the filesystem syncer kernel thread. */
1.248.2.8! yamt 648: if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
! 649: NULL, NULL, "ioflush"))
1.159 fvdl 650: panic("fork syncer");
1.185 chs 651:
652: /* Create the aiodone daemon kernel thread. */
1.248.2.2 yamt 653: if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
1.248.2.8! yamt 654: uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
1.185 chs 655: panic("fork aiodoned");
1.137 thorpej 656:
1.248.2.4 yamt 657: vmem_rehash_start();
658:
1.186 jdolecek 659: /* Initialize exec structures */
660: exec_init(1);
1.192 jdolecek 661:
1.163 thorpej 662: /*
663: * Okay, now we can let init(8) exec! It's off to userland!
664: */
665: start_init_exec = 1;
1.247 christos 666: wakeup(&start_init_exec);
1.163 thorpej 667:
1.61 mycroft 668: /* The scheduler is an infinite loop. */
1.114 mrg 669: uvm_scheduler();
1.61 mycroft 670: /* NOTREACHED */
671: }
672:
1.93 mouse 673: static void
1.248.2.1 yamt 674: check_console(struct lwp *l)
1.93 mouse 675: {
676: struct nameidata nd;
677: int error;
678:
1.248.2.8! yamt 679: NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console");
1.93 mouse 680: error = namei(&nd);
1.96 cgd 681: if (error == 0)
682: vrele(nd.ni_vp);
683: else if (error == ENOENT)
684: printf("warning: no /dev/console\n");
1.93 mouse 685: else
1.96 cgd 686: printf("warning: lookup /dev/console: error %d\n", error);
1.93 mouse 687: }
688:
1.61 mycroft 689: /*
690: * List of paths to try when searching for "init".
691: */
1.248.2.6 yamt 692: static const char * const initpaths[] = {
1.61 mycroft 693: "/sbin/init",
694: "/sbin/oinit",
695: "/sbin/init.bak",
696: NULL,
697: };
698:
699: /*
700: * Start the initial user process; try exec'ing each pathname in "initpaths".
701: * The program is invoked with one argument containing the boot flags.
702: */
703: static void
1.176 thorpej 704: start_init(void *arg)
1.61 mycroft 705: {
1.216 thorpej 706: struct lwp *l = arg;
707: struct proc *p = l->l_proc;
1.130 eeh 708: vaddr_t addr;
1.78 mycroft 709: struct sys_execve_args /* {
1.108 mycroft 710: syscallarg(const char *) path;
1.92 cgd 711: syscallarg(char * const *) argp;
712: syscallarg(char * const *) envp;
1.68 cgd 713: } */ args;
714: int options, i, error;
715: register_t retval[2];
1.66 mycroft 716: char flags[4], *flagsp;
1.202 lukem 717: const char *path, *slash;
1.176 thorpej 718: char *ucp, **uap, *arg0, *arg1 = NULL;
1.202 lukem 719: char ipath[129];
720: int ipx, len;
1.61 mycroft 721:
1.76 cgd 722: /*
723: * Now in process 1.
724: */
1.146 gwr 725: strncpy(p->p_comm, "init", MAXCOMLEN);
1.163 thorpej 726:
727: /*
728: * Wait for main() to tell us that it's safe to exec.
729: */
730: while (start_init_exec == 0)
1.247 christos 731: (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
1.93 mouse 732:
733: /*
734: * This is not the right way to do this. We really should
735: * hand-craft a descriptor onto /dev/console to hand to init,
736: * but that's a _lot_ more work, and the benefit from this easy
737: * hack makes up for the "good is the enemy of the best" effect.
738: */
1.248.2.1 yamt 739: check_console(l);
1.61 mycroft 740:
741: /*
742: * Need just enough stack to hold the faked-up "execve()" arguments.
743: */
1.211 chs 744: addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
1.205 sommerfe 745: if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
1.181 thorpej 746: NULL, UVM_UNKNOWN_OFFSET, 0,
1.114 mrg 747: UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
748: UVM_ADV_NORMAL,
1.189 chs 749: UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
1.114 mrg 750: panic("init: couldn't allocate argument space");
1.248.2.4 yamt 751: p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
1.61 mycroft 752:
1.202 lukem 753: ipx = 0;
754: while (1) {
755: if (boothowto & RB_ASKNAME) {
756: printf("init path");
757: if (initpaths[ipx])
758: printf(" (default %s)", initpaths[ipx]);
759: printf(": ");
760: len = cngetsn(ipath, sizeof(ipath)-1);
761: if (len == 0) {
762: if (initpaths[ipx])
763: path = initpaths[ipx++];
764: else
765: continue;
766: } else {
767: ipath[len] = '\0';
768: path = ipath;
769: }
770: } else {
771: if ((path = initpaths[ipx++]) == NULL)
772: break;
773: }
774:
1.211 chs 775: ucp = (char *)USRSTACK;
1.64 mycroft 776:
1.61 mycroft 777: /*
1.64 mycroft 778: * Construct the boot flag argument.
1.61 mycroft 779: */
1.66 mycroft 780: flagsp = flags;
781: *flagsp++ = '-';
1.61 mycroft 782: options = 0;
1.66 mycroft 783:
1.61 mycroft 784: if (boothowto & RB_SINGLE) {
1.64 mycroft 785: *flagsp++ = 's';
1.61 mycroft 786: options = 1;
787: }
788: #ifdef notyet
789: if (boothowto & RB_FASTBOOT) {
1.64 mycroft 790: *flagsp++ = 'f';
1.61 mycroft 791: options = 1;
792: }
793: #endif
1.64 mycroft 794:
795: /*
796: * Move out the flags (arg 1), if necessary.
797: */
798: if (options != 0) {
799: *flagsp++ = '\0';
800: i = flagsp - flags;
801: #ifdef DEBUG
1.90 christos 802: printf("init: copying out flags `%s' %d\n", flags, i);
1.64 mycroft 803: #endif
1.211 chs 804: arg1 = STACK_ALLOC(ucp, i);
805: ucp = STACK_MAX(arg1, i);
1.248.2.4 yamt 806: (void)copyout((void *)flags, arg1, i);
1.64 mycroft 807: }
1.61 mycroft 808:
809: /*
810: * Move out the file name (also arg 0).
811: */
1.64 mycroft 812: i = strlen(path) + 1;
813: #ifdef DEBUG
1.90 christos 814: printf("init: copying out path `%s' %d\n", path, i);
1.202 lukem 815: #else
1.203 lukem 816: if (boothowto & RB_ASKNAME || path != initpaths[0])
1.202 lukem 817: printf("init: trying %s\n", path);
1.64 mycroft 818: #endif
1.211 chs 819: arg0 = STACK_ALLOC(ucp, i);
820: ucp = STACK_MAX(arg0, i);
1.247 christos 821: (void)copyout(path, arg0, i);
1.61 mycroft 822:
823: /*
824: * Move out the arg pointers.
825: */
1.248.2.4 yamt 826: ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
1.211 chs 827: uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
828: SCARG(&args, path) = arg0;
829: SCARG(&args, argp) = uap;
830: SCARG(&args, envp) = NULL;
1.142 mycroft 831: slash = strrchr(path, '/');
832: if (slash)
1.248.2.4 yamt 833: (void)suword((void *)uap++,
1.142 mycroft 834: (long)arg0 + (slash + 1 - path));
835: else
1.248.2.4 yamt 836: (void)suword((void *)uap++, (long)arg0);
1.211 chs 837: if (options != 0)
1.248.2.4 yamt 838: (void)suword((void *)uap++, (long)arg1);
839: (void)suword((void *)uap++, 0); /* terminator */
1.61 mycroft 840:
841: /*
842: * Now try to exec the program. If can't for any reason
843: * other than it doesn't exist, complain.
844: */
1.248.2.1 yamt 845: error = sys_execve(l, &args, retval);
1.179 thorpej 846: if (error == 0 || error == EJUSTRETURN) {
1.248.2.3 yamt 847: KERNEL_UNLOCK_LAST(l);
1.61 mycroft 848: return;
1.179 thorpej 849: }
1.202 lukem 850: printf("exec %s: error %d\n", path, error);
1.61 mycroft 851: }
1.90 christos 852: printf("init: not found\n");
1.61 mycroft 853: panic("no init");
854: }
CVSweb <webmaster@jp.NetBSD.org>