[BACK]Return to init_main.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/init_main.c, Revision 1.379

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

CVSweb <webmaster@jp.NetBSD.org>