The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/init_main.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: init_main.c,v 1.541 2022/10/26 23:20:47 riastradh Exp $        */
    2 
    3 /*-
    4  * Copyright (c) 2008, 2009, 2019 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  */
   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.
   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.
   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  *
   96  *      @(#)init_main.c 8.16 (Berkeley) 5/14/95
   97  */
   98 
   99 #include <sys/cdefs.h>
  100 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.541 2022/10/26 23:20:47 riastradh Exp $");
  101 
  102 #include "opt_cnmagic.h"
  103 #include "opt_ddb.h"
  104 #include "opt_inet.h"
  105 #include "opt_ipsec.h"
  106 #include "opt_modular.h"
  107 #include "opt_ntp.h"
  108 #include "opt_pipe.h"
  109 #include "opt_syscall_debug.h"
  110 #include "opt_sysv.h"
  111 #include "opt_fileassoc.h"
  112 #include "opt_ktrace.h"
  113 #include "opt_pax.h"
  114 #include "opt_compat_netbsd.h"
  115 #include "opt_ptrace.h"
  116 #include "opt_splash.h"
  117 #include "opt_kernhist.h"
  118 #include "opt_gprof.h"
  119 
  120 #if defined(SPLASHSCREEN) && defined(makeoptions_SPLASHSCREEN_IMAGE)
  121 extern void *_binary_splash_image_start;
  122 extern void *_binary_splash_image_end;
  123 #endif
  124 
  125 #include "ksyms.h"
  126 
  127 #include "veriexec.h"
  128 
  129 #include <sys/param.h>
  130 #include <sys/acct.h>
  131 #include <sys/filedesc.h>
  132 #include <sys/file.h>
  133 #include <sys/errno.h>
  134 #include <sys/callout.h>
  135 #include <sys/cpu.h>
  136 #include <sys/cpufreq.h>
  137 #include <sys/spldebug.h>
  138 #include <sys/kernel.h>
  139 #include <sys/mount.h>
  140 #include <sys/proc.h>
  141 #include <sys/lwp.h>
  142 #include <sys/kthread.h>
  143 #include <sys/resourcevar.h>
  144 #include <sys/signalvar.h>
  145 #include <sys/systm.h>
  146 #include <sys/vnode.h>
  147 #include <sys/fstrans.h>
  148 #include <sys/tty.h>
  149 #include <sys/conf.h>
  150 #include <sys/disklabel.h>
  151 #include <sys/buf.h>
  152 #include <sys/device.h>
  153 #include <sys/exec.h>
  154 #include <sys/socketvar.h>
  155 #include <sys/protosw.h>
  156 #include <sys/percpu.h>
  157 #include <sys/pserialize.h>
  158 #include <sys/pset.h>
  159 #include <sys/sysctl.h>
  160 #include <sys/reboot.h>
  161 #include <sys/event.h>
  162 #include <sys/mbuf.h>
  163 #include <sys/sched.h>
  164 #include <sys/sleepq.h>
  165 #include <sys/ipi.h>
  166 #include <sys/iostat.h>
  167 #include <sys/vmem.h>
  168 #include <sys/uuid.h>
  169 #include <sys/extent.h>
  170 #include <sys/disk.h>
  171 #include <sys/msgbuf.h>
  172 #include <sys/module.h>
  173 #include <sys/module_hook.h>
  174 #include <sys/event.h>
  175 #include <sys/lockf.h>
  176 #include <sys/once.h>
  177 #include <sys/kcpuset.h>
  178 #include <sys/ksyms.h>
  179 #include <sys/uidinfo.h>
  180 #include <sys/kprintf.h>
  181 #include <sys/bufq.h>
  182 #include <sys/threadpool.h>
  183 #include <sys/futex.h>
  184 #ifdef IPSEC
  185 #include <netipsec/ipsec.h>
  186 #endif
  187 #include <sys/domain.h>
  188 #include <sys/namei.h>
  189 #include <sys/rnd.h>
  190 #include <sys/pipe.h>
  191 #if NVERIEXEC > 0
  192 #include <sys/verified_exec.h>
  193 #endif /* NVERIEXEC > 0 */
  194 #ifdef KTRACE
  195 #include <sys/ktrace.h>
  196 #endif
  197 #include <sys/kauth.h>
  198 #include <net80211/ieee80211_netbsd.h>
  199 #include <sys/cprng.h>
  200 #include <sys/psref.h>
  201 #include <sys/radixtree.h>
  202 
  203 #include <sys/syscall.h>
  204 #include <sys/syscallargs.h>
  205 
  206 #include <sys/pax.h>
  207 
  208 #include <dev/clock_subr.h>
  209 
  210 #include <secmodel/secmodel.h>
  211 
  212 #include <ufs/ufs/quota.h>
  213 
  214 #include <miscfs/genfs/genfs.h>
  215 #include <miscfs/specfs/specdev.h>
  216 
  217 #include <sys/cpu.h>
  218 
  219 #include <uvm/uvm.h>    /* extern struct uvm uvm */
  220 
  221 #include <dev/cons.h>
  222 #include <dev/splash/splash.h>
  223 
  224 #include <net/bpf.h>
  225 #include <net/if.h>
  226 #include <net/pfil.h>
  227 #include <net/raw_cb.h>
  228 #include <net/if_llatbl.h>
  229 
  230 #include <prop/proplib.h>
  231 
  232 #include <sys/userconf.h>
  233 
  234 extern time_t rootfstime;
  235 
  236 #ifndef curlwp
  237 struct  lwp *curlwp = &lwp0;
  238 #endif
  239 struct  proc *initproc;
  240 
  241 struct  vnode *rootvp, *swapdev_vp;
  242 int     boothowto;
  243 int     cold __read_mostly = 1;         /* still working on startup */
  244 int     shutting_down __read_mostly;    /* system is shutting down */
  245 
  246 int     start_init_exec;                /* semaphore for start_init() */
  247 
  248 static void check_console(struct lwp *l);
  249 static void start_init(void *);
  250 static void configure(void);
  251 static void configure2(void);
  252 static void configure3(void);
  253 void main(void);
  254 
  255 /*
  256  * System startup; initialize the world, create process 0, mount root
  257  * filesystem, and fork to create init and pagedaemon.  Most of the
  258  * hard work is done in the lower-level initialization routines including
  259  * startup(), which does memory initialization and autoconfiguration.
  260  */
  261 void
  262 main(void)
  263 {
  264         struct timespec time;
  265         struct lwp *l;
  266         struct proc *p;
  267         int s, error;
  268 #ifdef NVNODE_IMPLICIT
  269         int usevnodes;
  270 #endif
  271         CPU_INFO_ITERATOR cii;
  272         struct cpu_info *ci;
  273 
  274 #ifdef DIAGNOSTIC
  275         /*
  276          * Verify that CPU_INFO_FOREACH() knows about the boot CPU
  277          * and only the boot CPU at this point.
  278          */
  279         int cpucount = 0;
  280         for (CPU_INFO_FOREACH(cii, ci)) {
  281                 KASSERT(ci == curcpu());
  282                 cpucount++;
  283         }
  284         KASSERT(cpucount == 1);
  285 #endif
  286 
  287         l = &lwp0;
  288 #ifndef LWP0_CPU_INFO
  289         l->l_cpu = curcpu();
  290 #endif
  291         l->l_pflag |= LP_RUNNING;
  292 
  293         /*
  294          * Attempt to find console and initialize
  295          * in case of early panic or other messages.
  296          */
  297         consinit();
  298 #ifdef CNMAGIC
  299         cn_set_magic(CNMAGIC);
  300 #endif
  301 
  302         kernel_lock_init();
  303         once_init();
  304         todr_init();
  305 
  306         mi_cpu_init();
  307         kernconfig_lock_init();
  308         kthread_sysinit();
  309 
  310         /* Initialize the device switch tables. */
  311         devsw_init();
  312 
  313         /* Initialize event counters. */
  314         evcnt_init();
  315 
  316         uvm_init();
  317         ubchist_init();
  318         kcpuset_sysinit();
  319 
  320         prop_kern_init();
  321 
  322 #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
  323         ksyms_init();
  324 #endif
  325         kprintf_init();
  326 
  327         percpu_init();
  328 
  329         /* Initialize lock caches. */
  330         mutex_obj_init();
  331 
  332         /* Initialize radix trees (used by numerous subsystems). */
  333         radix_tree_init();
  334 
  335         /* Passive serialization. */
  336         pserialize_init();
  337 
  338         /* Initialize the extent manager. */
  339         extent_init();
  340 
  341         /* Do machine-dependent initialization. */
  342         cpu_startup();
  343 
  344         /* Initialize the sysctl subsystem. */
  345         sysctl_init();
  346 
  347         /* Initialize callouts, part 1. */
  348         callout_startup();
  349 
  350         /* Initialize the kernel authorization subsystem. */
  351         kauth_init();
  352 
  353         secmodel_init();
  354 
  355         spec_init();
  356 
  357         /*
  358          * Set BPF op vector.  Can't do this in bpf attach, since
  359          * network drivers attach before bpf.
  360          */
  361         bpf_setops();
  362 
  363         /* Initialize what we can in ipi(9) before CPUs are detected. */
  364         ipi_sysinit();
  365 
  366         /* Start module system. */
  367         module_init();
  368         module_hook_init();
  369 
  370         /*
  371          * Initialize the kernel authorization subsystem and start the
  372          * default security model, if any. We need to do this early
  373          * enough so that subsystems relying on any of the aforementioned
  374          * can work properly. Since the security model may dictate the
  375          * credential inheritance policy, it is needed at least before
  376          * any process is created, specifically proc0.
  377          */
  378         module_init_class(MODULE_CLASS_SECMODEL);
  379 
  380         /* Initialize the buffer cache */
  381         bufinit();
  382         biohist_init();
  383 
  384 #ifdef KERNHIST
  385         sysctl_kernhist_init();
  386 #endif
  387 
  388 
  389 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_IMAGE)
  390         size_t splash_size = (&_binary_splash_image_end -
  391             &_binary_splash_image_start) * sizeof(void *);
  392         splash_setimage(&_binary_splash_image_start, splash_size);
  393 #endif
  394 
  395         /* Initialize sockets. */
  396         soinit();
  397 
  398         /*
  399          * The following things must be done before autoconfiguration.
  400          */
  401         rnd_init();             /* initialize entropy pool */
  402 
  403         cprng_init();           /* initialize cryptographic PRNG */
  404 
  405         /* Initialize process and pgrp structures. */
  406         procinit();
  407         lwpinit();
  408 
  409         /* Must be called after lwpinit (lwpinit_specificdata) */
  410         psref_init();
  411 
  412         /* Initialize signal-related data structures. */
  413         signal_init();
  414 
  415         /* Initialize resource management. */
  416         resource_init();
  417 
  418         /* Create process 0. */
  419         proc0_init();
  420         lwp0_init();
  421 
  422         /* Disable preemption during boot. */
  423         kpreempt_disable();
  424 
  425         /* Initialize the threadpool system. */
  426         threadpools_init();
  427 
  428         /* Initialize the UID hash table. */
  429         uid_init();
  430 
  431         /* Charge root for one process. */
  432         (void)chgproccnt(0, 1);
  433 
  434         /* Initialize the run queues, turnstiles and sleep queues. */
  435         sched_rqinit();
  436         turnstile_init();
  437         sleeptab_init(&sleeptab);
  438 
  439         sched_init();
  440 
  441         /* Initialize processor-sets */
  442         psets_init();
  443 
  444         /* Initialize cpufreq(9) */
  445         cpufreq_init();
  446 
  447         /* MI initialization of the boot cpu */
  448         error = mi_cpu_attach(curcpu());
  449         KASSERT(error == 0);
  450 
  451         /* Initialize timekeeping. */
  452         time_init();
  453 
  454         /*
  455          * Initialize mbuf's.  Do this now because we might attempt to
  456          * allocate mbufs or mbuf clusters during autoconfiguration.
  457          */
  458         mbinit();
  459 
  460         /* Initialize I/O statistics. */
  461         iostat_init();
  462 
  463         /* Initialize the log device. */
  464         loginit();
  465 
  466         /* Second part of module system initialization. */
  467         module_start_unload_thread();
  468 
  469         /* Initialize autoconf data structures before any modules are loaded */
  470         config_init_mi();
  471 
  472         /* Initialize the file systems. */
  473 #ifdef NVNODE_IMPLICIT
  474         /*
  475          * If maximum number of vnodes in namei vnode cache is not explicitly
  476          * defined in kernel config, adjust the number such as we use roughly
  477          * 10% of memory for vnodes and associated data structures in the
  478          * assumed worst case.  Do not provide fewer than NVNODE vnodes.
  479          */
  480         usevnodes = calc_cache_size(vmem_size(kmem_arena, VMEM_FREE|VMEM_ALLOC),
  481             10, VNODE_KMEM_MAXPCT) / VNODE_COST;
  482         if (usevnodes > desiredvnodes)
  483                 desiredvnodes = usevnodes;
  484 #endif /* NVNODE_IMPLICIT */
  485 #ifdef MAXFILES_IMPLICIT
  486         /*
  487          * If maximum number of files is not explicitly defined in
  488          * kernel config, adjust the number so that it is somewhat
  489          * more reasonable on machines with larger memory sizes.
  490          * Arbitrary numbers are 20,000 files for 16GB RAM or more
  491          * and 10,000 files for 1GB RAM or more.
  492          *
  493          * XXXtodo: adjust this and other values totally dynamically
  494          */
  495         if (ctob((uint64_t)physmem) >= 16ULL * 1024 * 1024 * 1024)
  496                 maxfiles = MAX(maxfiles, 20000);
  497         if (ctob((uint64_t)physmem) >= 1024 * 1024 * 1024)
  498                 maxfiles = MAX(maxfiles, 10000);
  499 #endif /* MAXFILES_IMPLICIT */
  500 
  501         /* Initialize fstrans. */
  502         fstrans_init();
  503 
  504         vfsinit();
  505         lf_init();
  506 
  507         /* Initialize the file descriptor system. */
  508         fd_sys_init();
  509 
  510         /* Initialize cwd structures */
  511         cwd_sys_init();
  512 
  513         /* Initialize kqueue. */
  514         kqueue_init();
  515 
  516         inittimecounter();
  517         ntp_init();
  518 
  519         /* Initialize tty subsystem. */
  520         tty_init();
  521         ttyldisc_init();
  522 
  523         /* Initialize the buffer cache, part 2. */
  524         bufinit2();
  525 
  526         /* Initialize the disk wedge subsystem. */
  527         dkwedge_init();
  528 
  529         /* Initialize pfil */
  530         pfil_init();
  531 
  532         /* Initialize interfaces. */
  533         ifinit1();
  534 
  535         spldebug_start();
  536 
  537         /* Initialize sockets thread(s) */
  538         soinit1();
  539 
  540         /*
  541          * Initialize the bufq strategy sub-system and any built-in
  542          * strategy modules - they may be needed by some devices during
  543          * auto-configuration
  544          */
  545         bufq_init();
  546         module_init_class(MODULE_CLASS_BUFQ);
  547 
  548         /* Configure the system hardware.  This will enable interrupts. */
  549         configure();
  550 #ifdef __HAVE_LEGACY_INTRCNT
  551         evcnt_attach_legacy_intrcnt();
  552 #endif
  553 
  554         /* Enable deferred processing of RNG samples */
  555         rnd_init_softint();
  556 
  557         /* Once all CPUs are detected, initialize the per-CPU cprng_fast.  */
  558         cprng_fast_init();
  559 
  560         ssp_init();
  561 
  562         ubc_init();             /* must be after autoconfig */
  563 
  564         mm_init();
  565 
  566         configure2();
  567 
  568         /* Initialize the rest of ipi(9) after CPUs have been detected. */
  569         ipi_percpu_init();
  570 
  571         futex_sys_init();
  572 
  573         /* Now timer is working.  Enable preemption. */
  574         kpreempt_enable();
  575 
  576         /* Get the threads going and into any sleeps before continuing. */
  577         yield();
  578 
  579         vmem_rehash_start();    /* must be before exec_init */
  580 
  581         /* Initialize exec structures */
  582         exec_init(1);           /* seminit calls exithook_establish() */
  583 
  584 #if NVERIEXEC > 0
  585         /*
  586          * Initialise the Veriexec subsystem.
  587          */
  588         veriexec_init();
  589 #endif /* NVERIEXEC > 0 */
  590 
  591         pax_init();
  592 
  593 #ifdef  IPSEC
  594         /* Attach network crypto subsystem */
  595         ipsec_attach();
  596 #endif
  597 
  598         /*
  599          * Initialize protocols.  Block reception of incoming packets
  600          * until everything is ready.
  601          */
  602         s = splnet();
  603         ifinit();
  604 #if defined(INET) || defined(INET6)
  605         lltableinit();
  606 #endif
  607         domaininit(true);
  608         ifinit_post();
  609         if_attachdomain();
  610         splx(s);
  611 
  612 #ifdef GPROF
  613         /* Initialize kernel profiling. */
  614         kmstartup();
  615 #endif
  616 
  617         /* Initialize system accounting. */
  618         acct_init();
  619 
  620 #ifndef PIPE_SOCKETPAIR
  621         /* Initialize pipes. */
  622         pipe_init();
  623 #endif
  624 
  625 #ifdef KTRACE
  626         /* Initialize ktrace. */
  627         ktrinit();
  628 #endif
  629 
  630         machdep_init();
  631 
  632         procinit_sysctl();
  633 
  634         scdebug_init();
  635 
  636         /*
  637          * Create process 1 (init(8)).  We do this now, as Unix has
  638          * historically had init be process 1, and changing this would
  639          * probably upset a lot of people.
  640          *
  641          * Note that process 1 won't immediately exec init(8), but will
  642          * wait for us to inform it that the root file system has been
  643          * mounted.
  644          */
  645         if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL))
  646                 panic("fork init");
  647 
  648         /*
  649          * The initproc variable cannot be initialized in start_init as there
  650          * is a race between vfs_mountroot and start_init.
  651          */
  652         mutex_enter(&proc_lock);
  653         initproc = proc_find_raw(1);
  654         mutex_exit(&proc_lock);
  655 
  656         /*
  657          * Load any remaining builtin modules, and hand back temporary
  658          * storage to the VM system.  Then require force when loading any
  659          * remaining un-init'ed built-in modules to avoid later surprises.
  660          */
  661         module_init_class(MODULE_CLASS_ANY);
  662         module_builtin_require_force();
  663 
  664         /*
  665          * Finalize configuration now that all real devices have been
  666          * found.  This needs to be done before the root device is
  667          * selected, since finalization may create the root device.
  668          */
  669         config_finalize();
  670 
  671         sysctl_finalize();
  672 
  673         /*
  674          * Now that autoconfiguration has completed, we can determine
  675          * the root and dump devices.
  676          */
  677         cpu_rootconf();
  678         cpu_dumpconf();
  679 
  680         /* Mount the root file system. */
  681         do {
  682                 domountroothook(root_device);
  683                 if ((error = vfs_mountroot())) {
  684                         printf("cannot mount root, error = %d\n", error);
  685                         boothowto |= RB_ASKNAME;
  686                         setroot(root_device,
  687                             (rootdev != NODEV) ? DISKPART(rootdev) : 0);
  688                 }
  689         } while (error != 0);
  690         mountroothook_destroy();
  691 
  692         configure3();
  693 
  694         /*
  695          * Initialise the time-of-day clock, passing the time recorded
  696          * in the root filesystem (if any) for use by systems that
  697          * don't have a non-volatile time-of-day device.
  698          */
  699         inittodr(rootfstime);
  700 
  701         /*
  702          * Now can look at time, having had a chance to verify the time
  703          * from the file system.  Reset l->l_rtime as it may have been
  704          * munched in mi_switch() after the time got set.
  705          */
  706         getnanotime(&time);
  707 
  708         mutex_enter(&proc_lock);
  709         LIST_FOREACH(p, &allproc, p_list) {
  710                 KASSERT((p->p_flag & PK_MARKER) == 0);
  711                 mutex_enter(p->p_lock);
  712                 TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
  713                 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
  714                         lwp_lock(l);
  715                         memset(&l->l_rtime, 0, sizeof(l->l_rtime));
  716                         lwp_unlock(l);
  717                 }
  718                 mutex_exit(p->p_lock);
  719         }
  720         mutex_exit(&proc_lock);
  721         binuptime(&curlwp->l_stime);
  722 
  723         for (CPU_INFO_FOREACH(cii, ci)) {
  724                 ci->ci_schedstate.spc_lastmod = time_second;
  725         }
  726 
  727         /* Create the pageout daemon kernel thread. */
  728         uvm_swap_init();
  729         if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
  730             NULL, NULL, "pgdaemon"))
  731                 panic("fork pagedaemon");
  732 
  733         /* Create the filesystem syncer kernel thread. */
  734         if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
  735             NULL, NULL, "ioflush"))
  736                 panic("fork syncer");
  737 
  738         /* Wait for final configure threads to complete. */
  739         config_finalize_mountroot();
  740 
  741         /*
  742          * Okay, now we can let init(8) exec!  It's off to userland!
  743          */
  744         mutex_enter(&proc_lock);
  745         start_init_exec = 1;
  746         cv_broadcast(&lbolt);
  747         mutex_exit(&proc_lock);
  748 
  749         /* The scheduler is an infinite loop. */
  750         uvm_scheduler();
  751         /* NOTREACHED */
  752 }
  753 
  754 /*
  755  * Configure the system's hardware.
  756  */
  757 static void
  758 configure(void)
  759 {
  760 
  761         /*
  762          * XXX
  763          * callout_setfunc() requires mutex(9) so it can't be in config_init()
  764          * on amiga and atari which use config_init() and autoconf(9) functions
  765          * to initialize console devices.
  766          */
  767         config_twiddle_init();
  768 
  769         pmf_init();
  770 
  771         /* Initialize driver modules */
  772         module_init_class(MODULE_CLASS_DRIVER);
  773 
  774         userconf_init();
  775         if (boothowto & RB_USERCONF)
  776                 userconf_prompt();
  777 
  778         if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
  779                 printf_nolog("Detecting hardware...");
  780         }
  781 
  782         /*
  783          * Do the machine-dependent portion of autoconfiguration.  This
  784          * sets the configuration machinery here in motion by "finding"
  785          * the root bus.  When this function returns, we expect interrupts
  786          * to be enabled.
  787          */
  788         cpu_configure();
  789 }
  790 
  791 static void
  792 configure2(void)
  793 {
  794         CPU_INFO_ITERATOR cii;
  795         struct cpu_info *ci;
  796         int s;
  797 
  798         /* Fix up CPU topology info, which has all been collected by now. */
  799         cpu_topology_init();
  800 
  801         /*
  802          * Now that we've found all the hardware, start the real time
  803          * and statistics clocks.
  804          */
  805         initclocks();
  806 
  807         cold = 0;       /* clocks are running, we're warm now! */
  808         s = splsched();
  809         curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
  810         splx(s);
  811 
  812         /* Setup the runqueues and scheduler. */
  813         runq_init();
  814         synch_init();
  815 
  816         /* Boot the secondary processors. */
  817         for (CPU_INFO_FOREACH(cii, ci)) {
  818                 uvm_cpu_attach(ci);
  819         }
  820 
  821         /* Decide how to partition free memory. */
  822         uvm_page_rebucket();
  823 
  824         mp_online = true;
  825 #if defined(MULTIPROCESSOR)
  826         cpu_boot_secondary_processors();
  827 #endif
  828 
  829         /*
  830          * Bus scans can make it appear as if the system has paused, so
  831          * twiddle constantly while config_interrupts() jobs are running.
  832          */
  833         config_twiddle_fn(NULL);
  834 
  835         /*
  836          * Create threads to call back and finish configuration for
  837          * devices that want interrupts enabled.
  838          */
  839         config_create_interruptthreads();
  840 }
  841 
  842 static void
  843 configure3(void)
  844 {
  845 
  846         /*
  847          * Create threads to call back and finish configuration for
  848          * devices that want the mounted root file system.
  849          */
  850         config_create_mountrootthreads();
  851 
  852         /* Get the threads going and into any sleeps before continuing. */
  853         yield();
  854 }
  855 
  856 static void
  857 rootconf_handle_wedges(void)
  858 {
  859         struct disklabel label;
  860         struct partition *p;
  861         struct vnode *vp;
  862         daddr_t startblk;
  863         uint64_t nblks;
  864         device_t dev;
  865         int error;
  866 
  867         if (booted_nblks) {
  868                 /*
  869                  * bootloader passed geometry
  870                  */
  871                 dev      = booted_device;
  872                 startblk = booted_startblk;
  873                 nblks    = booted_nblks;
  874 
  875                 /*
  876                  * keep booted_device and booted_partition
  877                  * in case the kernel doesn't identify a wedge
  878                  */
  879         } else {
  880                 /*
  881                  * bootloader passed partition number
  882                  *
  883                  * We cannot ask the partition device directly when it is
  884                  * covered by a wedge. Instead we look up the geometry in
  885                  * the disklabel.
  886                  */
  887                 vp = opendisk(booted_device);
  888 
  889                 if (vp == NULL)
  890                         return;
  891 
  892                 VOP_UNLOCK(vp);
  893                 error = VOP_IOCTL(vp, DIOCGDINFO, &label, FREAD, NOCRED);
  894                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  895                 VOP_CLOSE(vp, FREAD, NOCRED);
  896                 vput(vp);
  897                 if (error)
  898                         return;
  899 
  900                 KASSERT(booted_partition >= 0
  901                         && booted_partition < MAXPARTITIONS);
  902 
  903                 p = &label.d_partitions[booted_partition];
  904 
  905                 dev      = booted_device;
  906                 startblk = p->p_offset;
  907                 nblks    = p->p_size;
  908         }
  909 
  910         dev = dkwedge_find_partition(dev, startblk, nblks);
  911         if (dev != NULL) {
  912                 booted_device = dev;
  913                 booted_partition = 0;
  914         }
  915 }
  916 
  917 void
  918 rootconf(void)
  919 {
  920         if (booted_device != NULL)
  921                 rootconf_handle_wedges();
  922 
  923         setroot(booted_device, booted_partition);
  924 }
  925 
  926 static void
  927 check_console(struct lwp *l)
  928 {
  929         struct vnode *vp;
  930         int error;
  931 
  932         error = namei_simple_kernel("/dev/console",
  933                                 NSM_FOLLOW_NOEMULROOT, &vp);
  934         if (error == 0) {
  935                 vrele(vp);
  936         } else if (error == ENOENT) {
  937                 if (boothowto & (AB_VERBOSE|AB_DEBUG))
  938                         printf("warning: no /dev/console\n");
  939         } else {
  940                 printf("warning: lookup /dev/console: error %d\n", error);
  941         }
  942 }
  943 
  944 /*
  945  * List of paths to try when searching for "init".
  946  */
  947 static const char * const initpaths[] = {
  948         "/sbin/init",
  949         "/sbin/oinit",
  950         "/sbin/init.bak",
  951         "/rescue/init",
  952         NULL,
  953 };
  954 
  955 /*
  956  * Start the initial user process; try exec'ing each pathname in "initpaths".
  957  * The program is invoked with one argument containing the boot flags.
  958  */
  959 static void
  960 start_init(void *arg)
  961 {
  962         struct lwp *l = arg;
  963         struct proc *p = l->l_proc;
  964         vaddr_t addr;
  965         struct sys_execve_args /* {
  966                 syscallarg(const char *) path;
  967                 syscallarg(char * const *) argp;
  968                 syscallarg(char * const *) envp;
  969         } */ args;
  970         int options, i, error;
  971         register_t retval[2];
  972         char flags[4], *flagsp;
  973         const char *path, *slash;
  974         char *ucp, **uap, *arg0, *arg1, *argv[3];
  975         char ipath[129];
  976         int ipx, len;
  977 
  978         /*
  979          * Now in process 1.
  980          */
  981         strncpy(p->p_comm, "init", MAXCOMLEN);
  982 
  983         /*
  984          * Wait for main() to tell us that it's safe to exec.
  985          */
  986         mutex_enter(&proc_lock);
  987         while (start_init_exec == 0)
  988                 cv_wait(&lbolt, &proc_lock);
  989         mutex_exit(&proc_lock);
  990 
  991         /*
  992          * This is not the right way to do this.  We really should
  993          * hand-craft a descriptor onto /dev/console to hand to init,
  994          * but that's a _lot_ more work, and the benefit from this easy
  995          * hack makes up for the "good is the enemy of the best" effect.
  996          */
  997         check_console(l);
  998 
  999         /*
 1000          * Need just enough stack to hold the faked-up "execve()" arguments.
 1001          */
 1002         addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
 1003         if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
 1004             NULL, UVM_UNKNOWN_OFFSET, 0,
 1005             UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_COPY,
 1006             UVM_ADV_NORMAL,
 1007             UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
 1008                 panic("init: couldn't allocate argument space");
 1009         p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
 1010 
 1011         ipx = 0;
 1012         while (1) {
 1013                 if (boothowto & RB_ASKNAME) {
 1014                         printf("init path");
 1015                         if (initpaths[ipx])
 1016                                 printf(" (default %s)", initpaths[ipx]);
 1017                         printf(": ");
 1018                         len = cngetsn(ipath, sizeof(ipath)-1);
 1019                         if (len == 4 && strcmp(ipath, "halt") == 0) {
 1020                                 kern_reboot(RB_HALT, NULL);
 1021                         } else if (len == 6 && strcmp(ipath, "reboot") == 0) {
 1022                                 kern_reboot(0, NULL);
 1023 #if defined(DDB)
 1024                         } else if (len == 3 && strcmp(ipath, "ddb") == 0) {
 1025                                 console_debugger();
 1026                                 continue;
 1027 #endif
 1028                         } else if (len > 0 && ipath[0] == '/') {
 1029                                 ipath[len] = '\0';
 1030                                 path = ipath;
 1031                         } else if (len == 0 && initpaths[ipx] != NULL) {
 1032                                 path = initpaths[ipx++];
 1033                         } else {
 1034                                 printf("use absolute path, ");
 1035 #if defined(DDB)
 1036                                 printf("\"ddb\", ");
 1037 #endif
 1038                                 printf("\"halt\", or \"reboot\"\n");
 1039                                 continue;
 1040                         }
 1041                 } else {
 1042                         if ((path = initpaths[ipx++]) == NULL) {
 1043                                 ipx = 0;
 1044                                 boothowto |= RB_ASKNAME;
 1045                                 continue;
 1046                         }
 1047                 }
 1048 
 1049                 ucp = (char *)USRSTACK;
 1050 
 1051                 /*
 1052                  * Construct the boot flag argument.
 1053                  */
 1054                 flagsp = flags;
 1055                 *flagsp++ = '-';
 1056                 options = 0;
 1057 
 1058                 if (boothowto & RB_SINGLE) {
 1059                         *flagsp++ = 's';
 1060                         options = 1;
 1061                 }
 1062 #ifdef notyet
 1063                 if (boothowto & RB_FASTBOOT) {
 1064                         *flagsp++ = 'f';
 1065                         options = 1;
 1066                 }
 1067 #endif
 1068 
 1069                 /*
 1070                  * Move out the flags (arg 1), if necessary.
 1071                  */
 1072                 if (options != 0) {
 1073                         *flagsp++ = '\0';
 1074                         i = flagsp - flags;
 1075 #ifdef DEBUG
 1076                         aprint_normal("init: copying out flags `%s' %d\n", flags, i);
 1077 #endif
 1078                         arg1 = STACK_ALLOC(ucp, i);
 1079                         ucp = STACK_MAX(arg1, i);
 1080                         if ((error = copyout((void *)flags, arg1, i)) != 0)
 1081                                 goto copyerr;
 1082                 } else
 1083                         arg1 = NULL;
 1084 
 1085                 /*
 1086                  * Move out the file name (also arg 0).
 1087                  */
 1088                 i = strlen(path) + 1;
 1089 #ifdef DEBUG
 1090                 aprint_normal("init: copying out path `%s' %d\n", path, i);
 1091 #else
 1092                 if (boothowto & RB_ASKNAME || path != initpaths[0])
 1093                         printf("init: trying %s\n", path);
 1094 #endif
 1095                 arg0 = STACK_ALLOC(ucp, i);
 1096                 ucp = STACK_MAX(arg0, i);
 1097                 if ((error = copyout(path, arg0, i)) != 0)
 1098                         goto copyerr;
 1099 
 1100                 /*
 1101                  * Move out the arg pointers.
 1102                  */
 1103                 ucp = (void *)STACK_ALIGN(ucp, STACK_ALIGNBYTES);
 1104                 uap = (char **)STACK_ALLOC(ucp, sizeof(argv));
 1105                 SCARG(&args, path) = arg0;
 1106                 SCARG(&args, argp) = uap;
 1107                 SCARG(&args, envp) = NULL;
 1108                 slash = strrchr(path, '/');
 1109 
 1110                 argv[0] = slash ? arg0 + (slash + 1 - path) : arg0;
 1111                 argv[1] = arg1;
 1112                 argv[2] = NULL;
 1113                 if ((error = copyout(argv, uap, sizeof(argv))) != 0)
 1114                         goto copyerr;
 1115 
 1116                 /*
 1117                  * Now try to exec the program.  If it can't for any reason
 1118                  * other than it doesn't exist, complain.
 1119                  */
 1120                 error = sys_execve(l, &args, retval);
 1121                 if (error == 0 || error == EJUSTRETURN) {
 1122                         KERNEL_UNLOCK_LAST(l);
 1123                         return;
 1124                 }
 1125                 printf("exec %s: error %d\n", path, error);
 1126         }
 1127         printf("init: not found\n");
 1128         panic("no init");
 1129 copyerr:
 1130         panic("copyout %d", error);
 1131 }
 1132 
 1133 /*
 1134  * calculate cache size (in bytes) from physmem and vsize.
 1135  */
 1136 vaddr_t
 1137 calc_cache_size(vsize_t vsize, int pct, int va_pct)
 1138 {
 1139         paddr_t t;
 1140 
 1141         /* XXX should consider competing cache if any */
 1142         /* XXX should consider submaps */
 1143         t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
 1144         if (vsize != 0) {
 1145                 vsize = (uintmax_t)vsize * va_pct / 100;
 1146                 if (t > vsize) {
 1147                         t = vsize;
 1148                 }
 1149         }
 1150         return t;
 1151 }
 1152 
 1153 /*
 1154  * Print the system start up banner.
 1155  *
 1156  * - Print a limited banner if AB_SILENT.
 1157  * - Always send normal banner to the log.
 1158  */
 1159 #define MEM_PBUFSIZE    sizeof("99999 MB")
 1160 
 1161 void
 1162 banner(void)
 1163 {
 1164         static char notice[] = " Notice: this software is "
 1165             "protected by copyright";
 1166         char pbuf[81];
 1167         void (*pr)(const char *, ...) __printflike(1, 2);
 1168         int i;
 1169 
 1170         if ((boothowto & AB_SILENT) != 0) {
 1171                 snprintf(pbuf, sizeof(pbuf), "%s %s (%s)",
 1172                     ostype, osrelease, kernel_ident);
 1173                 printf_nolog("%s", pbuf);
 1174                 for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--)
 1175                         printf(" ");
 1176                 printf_nolog("%s\n", notice);
 1177                 pr = aprint_normal;
 1178         } else {
 1179                 pr = printf;
 1180         }
 1181 
 1182         memset(pbuf, 0, sizeof(pbuf));
 1183         (*pr)("%s%s", copyright, version);
 1184         format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem));
 1185         (*pr)("total memory = %s\n", pbuf);
 1186         format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvm_availmem(false)));
 1187         (*pr)("avail memory = %s\n", pbuf);
 1188 }

Cache object: 0dfb3ffd67c46e906807473bae9d1f9d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.