Annotation of src/sys/kern/init_main.c, Revision 1.340
1.340 ! ad 1: /* $NetBSD: init_main.c,v 1.339 2008/01/15 03:37:10 rmind 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.340 ! ad 74: __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.339 2008/01/15 03:37:10 rmind Exp $");
1.115 mrg 75:
1.223 jonathan 76: #include "opt_ipsec.h"
1.267 kardel 77: #include "opt_ntp.h"
1.198 jdolecek 78: #include "opt_pipe.h"
1.267 kardel 79: #include "opt_posix.h"
1.172 soren 80: #include "opt_syscall_debug.h"
1.267 kardel 81: #include "opt_sysv.h"
1.292 ad 82: #include "opt_fileassoc.h"
83: #include "opt_ktrace.h"
1.281 elad 84: #include "opt_pax.h"
1.61 mycroft 85:
1.106 explorer 86: #include "rnd.h"
1.305 xtraeme 87: #include "sysmon_envsys.h"
88: #include "sysmon_power.h"
1.313 xtraeme 89: #include "sysmon_taskq.h"
90: #include "sysmon_wdog.h"
1.276 dogcow 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.302 yamt 99: #include <sys/cpu.h>
1.61 mycroft 100: #include <sys/kernel.h>
1.269 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.288 hannken 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.338 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.302 yamt 125: #include <sys/sched.h>
1.292 ad 126: #include <sys/sleepq.h>
1.284 ad 127: #include <sys/iostat.h>
1.304 yamt 128: #include <sys/vmem.h>
1.307 ad 129: #include <sys/uuid.h>
130: #include <sys/extent.h>
1.309 ad 131: #include <sys/disk.h>
1.314 rmind 132: #include <sys/mqueue.h>
1.325 ad 133: #include <sys/msgbuf.h>
1.340 ! ad 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.273 elad 159: #if NVERIEXEC > 0
1.245 blymn 160: #include <sys/verified_exec.h>
1.273 elad 161: #endif /* NVERIEXEC > 0 */
1.292 ad 162: #ifdef KTRACE
163: #include <sys/ktrace.h>
164: #endif
1.266 elad 165: #include <sys/kauth.h>
1.252 skrll 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.334 elad 171: #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
1.281 elad 172: #include <sys/pax.h>
1.334 elad 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.323 ad 180: #include <sys/cpu.h>
1.61 mycroft 181:
1.114 mrg 182: #include <uvm/uvm.h>
183:
1.310 xtraeme 184: #if NSYSMON_TASKQ > 0
185: #include <dev/sysmon/sysmon_taskq.h>
186: #endif
187:
1.202 lukem 188: #include <dev/cons.h>
1.310 xtraeme 189:
1.313 xtraeme 190: #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
1.305 xtraeme 191: #include <dev/sysmon/sysmonvar.h>
192: #endif
1.202 lukem 193:
1.81 christos 194: #include <net/if.h>
195: #include <net/raw_cb.h>
1.61 mycroft 196:
1.277 elad 197: #include <secmodel/secmodel.h>
198:
1.251 junyoung 199: extern struct proc proc0;
200: extern struct lwp lwp0;
201: extern struct cwdinfo cwdi0;
1.311 pooka 202: extern time_t rootfstime;
1.251 junyoung 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.272 kardel 212: struct timeval boottime; /* time at system startup - will only follow settime deltas */
1.61 mycroft 213:
1.261 perry 214: volatile int start_init_exec; /* semaphore for start_init() */
1.163 thorpej 215:
1.260 christos 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.280 christos 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.287 elad 231: void __secmodel_none(void);
1.296 itohy 232: __weak_alias(secmodel_start,__secmodel_none);
1.287 elad 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.267 kardel 248: #ifdef __HAVE_TIMECOUNTER
249: struct timeval time;
250: #endif
1.216 thorpej 251: struct lwp *l;
1.144 thorpej 252: struct proc *p;
1.111 thorpej 253: struct pdevinit *pdev;
1.204 thorpej 254: int s, error;
1.61 mycroft 255: extern struct pdevinit pdevinit[];
1.175 jdolecek 256: #ifdef NVNODE_IMPLICIT
257: int usevnodes;
258: #endif
1.312 ad 259: CPU_INFO_ITERATOR cii;
260: struct cpu_info *ci;
1.61 mycroft 261:
1.216 thorpej 262: l = &lwp0;
1.327 matt 263: #ifndef LWP0_CPU_INFO
1.216 thorpej 264: l->l_cpu = curcpu();
1.327 matt 265: #endif
1.233 junyoung 266:
1.317 ad 267: /*
1.61 mycroft 268: * Attempt to find console and initialize
269: * in case of early panic or other messages.
270: */
271: consinit();
272:
1.321 ad 273: kernel_lock_init();
1.179 thorpej 274:
1.114 mrg 275: uvm_init();
1.127 thorpej 276:
1.269 yamt 277: kmem_init();
278:
1.338 yamt 279: percpu_init();
280:
1.307 ad 281: /* Initialize the extent manager. */
282: extent_init();
283:
1.145 thorpej 284: /* Do machine-dependent initialization. */
285: cpu_startup();
1.162 thorpej 286:
1.340 ! ad 287: /* Start module system. */
! 288: module_init();
! 289:
1.307 ad 290: /* Initialize callouts, part 1. */
1.166 enami 291: callout_startup();
1.145 thorpej 292:
1.291 elad 293: /*
294: * Initialize the kernel authorization subsystem and start the
295: * default security model, if any. We need to do this early
296: * enough so that subsystems relying on any of the aforementioned
297: * can work properly. Since the security model may dictate the
298: * credential inheritance policy, it is needed at least before
299: * any process is created, specifically proc0.
300: */
1.289 elad 301: kauth_init();
1.290 elad 302: secmodel_start();
303:
1.228 pk 304: /* Initialize the buffer cache */
305: bufinit();
306:
1.128 thorpej 307: /* Initialize sockets. */
308: soinit();
1.127 thorpej 309:
1.156 thorpej 310: /*
1.212 thorpej 311: * The following things must be done before autoconfiguration.
1.156 thorpej 312: */
1.212 thorpej 313: evcnt_init(); /* initialize event counters */
1.106 explorer 314: #if NRND > 0
1.156 thorpej 315: rnd_init(); /* initialize RNG */
1.106 explorer 316: #endif
1.176 thorpej 317:
1.236 simonb 318: /* Initialize process and pgrp structures. */
1.63 mycroft 319: procinit();
1.279 thorpej 320: lwpinit();
1.210 thorpej 321:
1.251 junyoung 322: /* Initialize signal-related data structures. */
323: signal_init();
1.61 mycroft 324:
1.333 ad 325: /* Initialize resource management. */
326: resource_init();
327:
1.251 junyoung 328: /* Create process 0 (the swapper). */
329: proc0_init();
1.61 mycroft 330:
1.307 ad 331: /* Initialize the UID hash table. */
332: uid_init();
333:
334: /* Charge root for one process. */
1.61 mycroft 335: (void)chgproccnt(0, 1);
336:
1.330 ad 337: /* Initialize timekeeping. */
338: time_init();
339:
1.292 ad 340: /* Initialize the run queues, turnstiles and sleep queues. */
1.312 ad 341: mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
1.302 yamt 342: sched_rqinit();
1.292 ad 343: turnstile_init();
344: sleeptab_init(&sleeptab);
345:
1.339 rmind 346: /* Initialize processor-sets */
347: psets_init();
348:
1.302 yamt 349: /* MI initialization of the boot cpu */
350: error = mi_cpu_attach(curcpu());
351: KASSERT(error == 0);
352:
1.338 yamt 353: /*
354: * Initialize mbuf's. Do this now because we might attempt to
355: * allocate mbufs or mbuf clusters during autoconfiguration.
356: */
357: mbinit();
358:
1.292 ad 359: /* Initialize the sysctl subsystem. */
360: sysctl_init();
1.61 mycroft 361:
1.284 ad 362: /* Initialize I/O statistics. */
363: iostat_init();
364:
1.325 ad 365: /* Initialize the log device. */
366: loginit();
367:
1.61 mycroft 368: /* Initialize the file systems. */
1.194 matt 369: #ifdef NVNODE_IMPLICIT
370: /*
371: * If maximum number of vnodes in namei vnode cache is not explicitly
372: * defined in kernel config, adjust the number such as we use roughly
1.232 tls 373: * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
1.194 matt 374: */
1.232 tls 375: usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
1.205 sommerfe 376: if (usevnodes > desiredvnodes)
1.194 matt 377: desiredvnodes = usevnodes;
378: #endif
1.61 mycroft 379: vfsinit();
380:
1.288 hannken 381: /* Initialize fstrans. */
382: fstrans_init();
1.267 kardel 383:
1.319 ad 384: /* Initialize the file descriptor system. */
385: filedesc_init();
386:
1.307 ad 387: /* Initialize the select()/poll() system calls. */
388: selsysinit();
389:
1.303 rmind 390: /* Initialize asynchronous I/O. */
391: aio_sysinit();
392:
1.314 rmind 393: /* Initialize message queues. */
394: mqueue_sysinit();
395:
1.313 xtraeme 396: /* Initialize the system monitor subsystems. */
1.310 xtraeme 397: #if NSYSMON_TASKQ > 0
398: sysmon_task_queue_preinit();
399: #endif
400:
1.305 xtraeme 401: #if NSYSMON_ENVSYS > 0
402: sysmon_envsys_init();
403: #endif
1.310 xtraeme 404:
1.305 xtraeme 405: #if NSYSMON_POWER > 0
406: sysmon_power_init();
407: #endif
1.313 xtraeme 408:
409: #if NSYSMON_WDOG > 0
410: sysmon_wdog_init();
411: #endif
412:
1.267 kardel 413: #ifdef __HAVE_TIMECOUNTER
1.268 kardel 414: inittimecounter();
1.267 kardel 415: ntp_init();
416: #endif /* __HAVE_TIMECOUNTER */
417:
1.307 ad 418: /* Initialize the device switch tables. */
419: devsw_init();
420:
1.326 ad 421: /* Initialize tty subsystem. */
422: tty_init();
423: ttyldisc_init();
424:
1.336 ad 425: /* Initialize the buffer cache, part 2. */
426: bufinit2();
427:
1.309 ad 428: /* Initialize the disk wedge subsystem. */
429: dkwedge_init();
430:
1.156 thorpej 431: /* Configure the system hardware. This will enable interrupts. */
432: configure();
1.61 mycroft 433:
1.280 christos 434: #if defined(__SSP__) || defined(__SSP_ALL__)
435: {
436: #ifdef DIAGNOSTIC
437: printf("Initializing SSP:");
438: #endif
439: /*
440: * We initialize ssp here carefully:
441: * 1. after we got some entropy
442: * 2. without calling a function
443: */
444: size_t i;
445: long guard[__arraycount(__stack_chk_guard)];
446:
447: arc4randbytes(guard, sizeof(guard));
448: for (i = 0; i < __arraycount(guard); i++)
449: __stack_chk_guard[i] = guard[i];
450: #ifdef DIAGNOSTIC
451: for (i = 0; i < __arraycount(guard); i++)
452: printf("%lx ", guard[i]);
453: printf("\n");
454: #endif
455: }
456: #endif
1.185 chs 457: ubc_init(); /* must be after autoconfig */
458:
1.179 thorpej 459: /* Lock the kernel on behalf of proc0. */
1.292 ad 460: KERNEL_LOCK(1, l);
461:
1.61 mycroft 462: #ifdef SYSVSHM
463: /* Initialize System V style shared memory. */
464: shminit();
465: #endif
466:
467: #ifdef SYSVSEM
468: /* Initialize System V style semaphores. */
469: seminit();
470: #endif
471:
472: #ifdef SYSVMSG
473: /* Initialize System V style message queues. */
474: msginit();
475: #endif
476:
1.217 christos 477: #ifdef P1003_1B_SEMAPHORE
478: /* Initialize posix semaphores */
479: ksem_init();
480: #endif
1.245 blymn 481:
1.273 elad 482: #if NVERIEXEC > 0
1.283 elad 483: /*
484: * Initialise the Veriexec subsystem.
485: */
486: veriexec_init();
1.273 elad 487: #endif /* NVERIEXEC > 0 */
1.251 junyoung 488:
1.334 elad 489: #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
1.281 elad 490: pax_init();
1.334 elad 491: #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
1.281 elad 492:
1.61 mycroft 493: /* Attach pseudo-devices. */
494: for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
495: (*pdev->pdev_attach)(pdev->pdev_count);
1.223 jonathan 496:
497: #ifdef FAST_IPSEC
498: /* Attach network crypto subsystem */
499: ipsec_attach();
500: #endif
1.61 mycroft 501:
502: /*
503: * Initialize protocols. Block reception of incoming packets
504: * until everything is ready.
505: */
1.190 thorpej 506: s = splnet();
1.61 mycroft 507: ifinit();
508: domaininit();
1.200 itojun 509: if_attachdomain();
1.61 mycroft 510: splx(s);
511:
512: #ifdef GPROF
513: /* Initialize kernel profiling. */
514: kmstartup();
515: #endif
1.164 enami 516:
517: /* Initialize system accouting. */
518: acct_init();
1.61 mycroft 519:
1.301 ad 520: #ifndef PIPE_SOCKETPAIR
1.300 ad 521: /* Initialize pipes. */
522: pipe_init();
1.301 ad 523: #endif
1.300 ad 524:
1.302 yamt 525: /* Setup the scheduler */
1.307 ad 526: sched_init();
1.98 gwr 527:
1.292 ad 528: #ifdef KTRACE
529: /* Initialize ktrace. */
530: ktrinit();
531: #endif
532:
1.307 ad 533: /* Initialize the UUID system calls. */
534: uuid_init();
535:
1.163 thorpej 536: /*
537: * Create process 1 (init(8)). We do this now, as Unix has
538: * historically had init be process 1, and changing this would
539: * probably upset a lot of people.
540: *
541: * Note that process 1 won't immediately exec init(8), but will
542: * wait for us to inform it that the root file system has been
543: * mounted.
544: */
1.216 thorpej 545: if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
1.163 thorpej 546: panic("fork init");
547:
548: /*
549: * Now that device driver threads have been created, wait for
1.329 ad 550: * them to finish any deferred autoconfiguration.
1.163 thorpej 551: */
552: while (config_pending)
1.329 ad 553: (void) tsleep(&config_pending, PWAIT, "cfpend", hz);
1.208 thorpej 554:
555: /*
1.340 ! ad 556: * Load any remaining builtin modules, and hand back temporary
! 557: * storage to the VM system.
! 558: */
! 559: module_init_class(MODULE_CLASS_ANY);
! 560: module_jettison();
! 561:
! 562: /*
1.208 thorpej 563: * Finalize configuration now that all real devices have been
564: * found. This needs to be done before the root device is
565: * selected, since finalization may create the root device.
566: */
567: config_finalize();
1.163 thorpej 568:
569: /*
570: * Now that autoconfiguration has completed, we can determine
571: * the root and dump devices.
572: */
1.98 gwr 573: cpu_rootconf();
1.101 thorpej 574: cpu_dumpconf();
1.61 mycroft 575:
576: /* Mount the root file system. */
1.95 thorpej 577: do {
578: domountroothook();
579: if ((error = vfs_mountroot())) {
1.97 thorpej 580: printf("cannot mount root, error = %d\n", error);
1.95 thorpej 581: boothowto |= RB_ASKNAME;
582: setroot(root_device,
1.152 thorpej 583: (rootdev != NODEV) ? DISKPART(rootdev) : 0);
1.95 thorpej 584: }
585: } while (error != 0);
586: mountroothook_destroy();
587:
1.239 pk 588: /*
589: * Initialise the time-of-day clock, passing the time recorded
590: * in the root filesystem (if any) for use by systems that
591: * don't have a non-volatile time-of-day device.
592: */
593: inittodr(rootfstime);
594:
1.206 matt 595: CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
596: CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
1.61 mycroft 597:
1.111 thorpej 598: /*
599: * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
600: * reference it.
601: */
1.243 mycroft 602: error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
603: if (error)
604: panic("cannot find root vnode, error=%d", error);
1.148 thorpej 605: cwdi0.cwdi_cdir = rootvnode;
606: VREF(cwdi0.cwdi_cdir);
1.118 fvdl 607: VOP_UNLOCK(rootvnode, 0);
1.148 thorpej 608: cwdi0.cwdi_rdir = NULL;
1.163 thorpej 609:
610: /*
611: * Now that root is mounted, we can fixup initproc's CWD
612: * info. All other processes are kthreads, which merely
613: * share proc0's CWD info.
614: */
615: initproc->p_cwdi->cwdi_cdir = rootvnode;
616: VREF(initproc->p_cwdi->cwdi_cdir);
617: initproc->p_cwdi->cwdi_rdir = NULL;
1.61 mycroft 618:
619: /*
620: * Now can look at time, having had a chance to verify the time
1.292 ad 621: * from the file system. Reset l->l_rtime as it may have been
1.61 mycroft 622: * munched in mi_switch() after the time got set.
623: */
1.267 kardel 624: #ifdef __HAVE_TIMECOUNTER
625: getmicrotime(&time);
626: #else
627: mono_time = time;
628: #endif
629: boottime = time;
1.298 ad 630: mutex_enter(&proclist_lock);
1.225 jdolecek 631: LIST_FOREACH(p, &allproc, p_list) {
1.295 pavel 632: KASSERT((p->p_flag & PK_MARKER) == 0);
1.292 ad 633: mutex_enter(&p->p_smutex);
1.267 kardel 634: p->p_stats->p_start = time;
1.225 jdolecek 635: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.292 ad 636: lwp_lock(l);
1.332 yamt 637: memset(&l->l_rtime, 0, sizeof(l->l_rtime));
1.292 ad 638: lwp_unlock(l);
1.225 jdolecek 639: }
1.292 ad 640: mutex_exit(&p->p_smutex);
1.163 thorpej 641: }
1.298 ad 642: mutex_exit(&proclist_lock);
1.332 yamt 643: binuptime(&curlwp->l_stime);
1.61 mycroft 644:
1.312 ad 645: for (CPU_INFO_FOREACH(cii, ci)) {
646: ci->ci_schedstate.spc_lastmod = time_second;
647: }
648:
1.163 thorpej 649: /* Create the pageout daemon kernel thread. */
650: uvm_swap_init();
1.336 ad 651: if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
1.307 ad 652: NULL, NULL, "pgdaemon"))
1.135 thorpej 653: panic("fork pagedaemon");
1.61 mycroft 654:
1.163 thorpej 655: /* Create the filesystem syncer kernel thread. */
1.336 ad 656: if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
657: NULL, NULL, "ioflush"))
1.159 fvdl 658: panic("fork syncer");
1.185 chs 659:
660: /* Create the aiodone daemon kernel thread. */
1.286 yamt 661: if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
1.336 ad 662: uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
1.185 chs 663: panic("fork aiodoned");
1.137 thorpej 664:
1.304 yamt 665: vmem_rehash_start();
666:
1.186 jdolecek 667: /* Initialize exec structures */
668: exec_init(1);
1.192 jdolecek 669:
1.163 thorpej 670: /*
671: * Okay, now we can let init(8) exec! It's off to userland!
672: */
673: start_init_exec = 1;
1.247 christos 674: wakeup(&start_init_exec);
1.163 thorpej 675:
1.61 mycroft 676: /* The scheduler is an infinite loop. */
1.114 mrg 677: uvm_scheduler();
1.61 mycroft 678: /* NOTREACHED */
679: }
680:
1.93 mouse 681: static void
1.260 christos 682: check_console(struct lwp *l)
1.93 mouse 683: {
684: struct nameidata nd;
685: int error;
686:
1.331 pooka 687: NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console");
1.93 mouse 688: error = namei(&nd);
1.96 cgd 689: if (error == 0)
690: vrele(nd.ni_vp);
691: else if (error == ENOENT)
692: printf("warning: no /dev/console\n");
1.93 mouse 693: else
1.96 cgd 694: printf("warning: lookup /dev/console: error %d\n", error);
1.93 mouse 695: }
696:
1.61 mycroft 697: /*
698: * List of paths to try when searching for "init".
699: */
1.327 matt 700: static const char * const initpaths[] = {
1.61 mycroft 701: "/sbin/init",
702: "/sbin/oinit",
703: "/sbin/init.bak",
704: NULL,
705: };
706:
707: /*
708: * Start the initial user process; try exec'ing each pathname in "initpaths".
709: * The program is invoked with one argument containing the boot flags.
710: */
711: static void
1.176 thorpej 712: start_init(void *arg)
1.61 mycroft 713: {
1.216 thorpej 714: struct lwp *l = arg;
715: struct proc *p = l->l_proc;
1.130 eeh 716: vaddr_t addr;
1.78 mycroft 717: struct sys_execve_args /* {
1.108 mycroft 718: syscallarg(const char *) path;
1.92 cgd 719: syscallarg(char * const *) argp;
720: syscallarg(char * const *) envp;
1.68 cgd 721: } */ args;
722: int options, i, error;
723: register_t retval[2];
1.66 mycroft 724: char flags[4], *flagsp;
1.202 lukem 725: const char *path, *slash;
1.176 thorpej 726: char *ucp, **uap, *arg0, *arg1 = NULL;
1.202 lukem 727: char ipath[129];
728: int ipx, len;
1.61 mycroft 729:
1.76 cgd 730: /*
731: * Now in process 1.
732: */
1.146 gwr 733: strncpy(p->p_comm, "init", MAXCOMLEN);
1.163 thorpej 734:
735: /*
736: * Wait for main() to tell us that it's safe to exec.
737: */
738: while (start_init_exec == 0)
1.247 christos 739: (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
1.93 mouse 740:
741: /*
742: * This is not the right way to do this. We really should
743: * hand-craft a descriptor onto /dev/console to hand to init,
744: * but that's a _lot_ more work, and the benefit from this easy
745: * hack makes up for the "good is the enemy of the best" effect.
746: */
1.260 christos 747: check_console(l);
1.61 mycroft 748:
749: /*
750: * Need just enough stack to hold the faked-up "execve()" arguments.
751: */
1.211 chs 752: addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
1.205 sommerfe 753: if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
1.181 thorpej 754: NULL, UVM_UNKNOWN_OFFSET, 0,
1.114 mrg 755: UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
756: UVM_ADV_NORMAL,
1.189 chs 757: UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
1.114 mrg 758: panic("init: couldn't allocate argument space");
1.297 christos 759: p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
1.61 mycroft 760:
1.202 lukem 761: ipx = 0;
762: while (1) {
763: if (boothowto & RB_ASKNAME) {
764: printf("init path");
765: if (initpaths[ipx])
766: printf(" (default %s)", initpaths[ipx]);
767: printf(": ");
768: len = cngetsn(ipath, sizeof(ipath)-1);
769: if (len == 0) {
770: if (initpaths[ipx])
771: path = initpaths[ipx++];
772: else
773: continue;
774: } else {
775: ipath[len] = '\0';
776: path = ipath;
777: }
778: } else {
779: if ((path = initpaths[ipx++]) == NULL)
780: break;
781: }
782:
1.211 chs 783: ucp = (char *)USRSTACK;
1.64 mycroft 784:
1.61 mycroft 785: /*
1.64 mycroft 786: * Construct the boot flag argument.
1.61 mycroft 787: */
1.66 mycroft 788: flagsp = flags;
789: *flagsp++ = '-';
1.61 mycroft 790: options = 0;
1.66 mycroft 791:
1.61 mycroft 792: if (boothowto & RB_SINGLE) {
1.64 mycroft 793: *flagsp++ = 's';
1.61 mycroft 794: options = 1;
795: }
796: #ifdef notyet
797: if (boothowto & RB_FASTBOOT) {
1.64 mycroft 798: *flagsp++ = 'f';
1.61 mycroft 799: options = 1;
800: }
801: #endif
1.64 mycroft 802:
803: /*
804: * Move out the flags (arg 1), if necessary.
805: */
806: if (options != 0) {
807: *flagsp++ = '\0';
808: i = flagsp - flags;
809: #ifdef DEBUG
1.90 christos 810: printf("init: copying out flags `%s' %d\n", flags, i);
1.64 mycroft 811: #endif
1.211 chs 812: arg1 = STACK_ALLOC(ucp, i);
813: ucp = STACK_MAX(arg1, i);
1.297 christos 814: (void)copyout((void *)flags, arg1, i);
1.64 mycroft 815: }
1.61 mycroft 816:
817: /*
818: * Move out the file name (also arg 0).
819: */
1.64 mycroft 820: i = strlen(path) + 1;
821: #ifdef DEBUG
1.90 christos 822: printf("init: copying out path `%s' %d\n", path, i);
1.202 lukem 823: #else
1.203 lukem 824: if (boothowto & RB_ASKNAME || path != initpaths[0])
1.202 lukem 825: printf("init: trying %s\n", path);
1.64 mycroft 826: #endif
1.211 chs 827: arg0 = STACK_ALLOC(ucp, i);
828: ucp = STACK_MAX(arg0, i);
1.247 christos 829: (void)copyout(path, arg0, i);
1.61 mycroft 830:
831: /*
832: * Move out the arg pointers.
833: */
1.297 christos 834: ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
1.211 chs 835: uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
836: SCARG(&args, path) = arg0;
837: SCARG(&args, argp) = uap;
838: SCARG(&args, envp) = NULL;
1.142 mycroft 839: slash = strrchr(path, '/');
840: if (slash)
1.297 christos 841: (void)suword((void *)uap++,
1.142 mycroft 842: (long)arg0 + (slash + 1 - path));
843: else
1.297 christos 844: (void)suword((void *)uap++, (long)arg0);
1.211 chs 845: if (options != 0)
1.297 christos 846: (void)suword((void *)uap++, (long)arg1);
847: (void)suword((void *)uap++, 0); /* terminator */
1.61 mycroft 848:
849: /*
850: * Now try to exec the program. If can't for any reason
851: * other than it doesn't exist, complain.
852: */
1.260 christos 853: error = sys_execve(l, &args, retval);
1.179 thorpej 854: if (error == 0 || error == EJUSTRETURN) {
1.292 ad 855: KERNEL_UNLOCK_LAST(l);
1.61 mycroft 856: return;
1.179 thorpej 857: }
1.202 lukem 858: printf("exec %s: error %d\n", path, error);
1.61 mycroft 859: }
1.90 christos 860: printf("init: not found\n");
1.61 mycroft 861: panic("no init");
862: }
CVSweb <webmaster@jp.NetBSD.org>