Annotation of src/sys/kern/init_main.c, Revision 1.488
1.488 ! pgoyette 1: /* $NetBSD: init_main.c,v 1.487 2016/11/16 12:31:33 pgoyette Exp $ */
1.347 ad 2:
3: /*-
1.384 ad 4: * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
1.347 ad 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.488 ! pgoyette 100: __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.487 2016/11/16 12:31:33 pgoyette Exp $");
1.115 mrg 101:
1.370 tsutsui 102: #include "opt_ddb.h"
1.475 ozaki-r 103: #include "opt_inet.h"
1.223 jonathan 104: #include "opt_ipsec.h"
1.382 apb 105: #include "opt_modular.h"
1.267 kardel 106: #include "opt_ntp.h"
1.198 jdolecek 107: #include "opt_pipe.h"
1.172 soren 108: #include "opt_syscall_debug.h"
1.267 kardel 109: #include "opt_sysv.h"
1.292 ad 110: #include "opt_fileassoc.h"
111: #include "opt_ktrace.h"
1.281 elad 112: #include "opt_pax.h"
1.380 christos 113: #include "opt_compat_netbsd.h"
1.361 simonb 114: #include "opt_wapbl.h"
1.404 elad 115: #include "opt_ptrace.h"
1.458 tls 116: #include "opt_rnd_printf.h"
1.465 nat 117: #include "opt_splash.h"
118:
1.470 uebayasi 119: #if defined(SPLASHSCREEN) && defined(makeoptions_SPLASHSCREEN_IMAGE)
1.465 nat 120: extern void *_binary_splash_image_start;
121: extern void *_binary_splash_image_end;
122: #endif
1.61 mycroft 123:
1.377 he 124: #include "ksyms.h"
1.441 tls 125:
1.276 dogcow 126: #include "veriexec.h"
1.106 explorer 127:
1.61 mycroft 128: #include <sys/param.h>
1.164 enami 129: #include <sys/acct.h>
1.61 mycroft 130: #include <sys/filedesc.h>
1.131 thorpej 131: #include <sys/file.h>
1.61 mycroft 132: #include <sys/errno.h>
1.162 thorpej 133: #include <sys/callout.h>
1.302 yamt 134: #include <sys/cpu.h>
1.436 jruoho 135: #include <sys/cpufreq.h>
1.408 dyoung 136: #include <sys/spldebug.h>
1.61 mycroft 137: #include <sys/kernel.h>
138: #include <sys/mount.h>
139: #include <sys/proc.h>
1.136 thorpej 140: #include <sys/kthread.h>
1.61 mycroft 141: #include <sys/resourcevar.h>
142: #include <sys/signalvar.h>
143: #include <sys/systm.h>
144: #include <sys/vnode.h>
1.288 hannken 145: #include <sys/fstrans.h>
1.87 thorpej 146: #include <sys/tty.h>
1.61 mycroft 147: #include <sys/conf.h>
1.95 thorpej 148: #include <sys/disklabel.h>
1.61 mycroft 149: #include <sys/buf.h>
150: #include <sys/device.h>
1.186 jdolecek 151: #include <sys/exec.h>
1.128 thorpej 152: #include <sys/socketvar.h>
1.61 mycroft 153: #include <sys/protosw.h>
1.338 yamt 154: #include <sys/percpu.h>
1.434 christos 155: #include <sys/pserialize.h>
1.342 rmind 156: #include <sys/pset.h>
1.338 yamt 157: #include <sys/sysctl.h>
1.61 mycroft 158: #include <sys/reboot.h>
1.209 jdolecek 159: #include <sys/event.h>
1.227 jonathan 160: #include <sys/mbuf.h>
1.302 yamt 161: #include <sys/sched.h>
1.292 ad 162: #include <sys/sleepq.h>
1.455 rmind 163: #include <sys/ipi.h>
1.284 ad 164: #include <sys/iostat.h>
1.304 yamt 165: #include <sys/vmem.h>
1.307 ad 166: #include <sys/uuid.h>
167: #include <sys/extent.h>
1.309 ad 168: #include <sys/disk.h>
1.325 ad 169: #include <sys/msgbuf.h>
1.340 ad 170: #include <sys/module.h>
1.343 ad 171: #include <sys/event.h>
1.357 ad 172: #include <sys/lockf.h>
1.364 pooka 173: #include <sys/once.h>
1.435 rmind 174: #include <sys/kcpuset.h>
1.367 ad 175: #include <sys/ksyms.h>
1.365 pooka 176: #include <sys/uidinfo.h>
1.379 pooka 177: #include <sys/kprintf.h>
1.485 pgoyette 178: #include <sys/bufq.h>
1.449 christos 179: #ifdef IPSEC
1.223 jonathan 180: #include <netipsec/ipsec.h>
181: #endif
1.82 christos 182: #include <sys/domain.h>
1.94 mouse 183: #include <sys/namei.h>
1.103 explorer 184: #include <sys/rnd.h>
1.192 jdolecek 185: #include <sys/pipe.h>
1.273 elad 186: #if NVERIEXEC > 0
1.245 blymn 187: #include <sys/verified_exec.h>
1.273 elad 188: #endif /* NVERIEXEC > 0 */
1.292 ad 189: #ifdef KTRACE
190: #include <sys/ktrace.h>
191: #endif
1.266 elad 192: #include <sys/kauth.h>
1.252 skrll 193: #include <net80211/ieee80211_netbsd.h>
1.437 tls 194: #include <sys/cprng.h>
1.61 mycroft 195:
1.77 christos 196: #include <sys/syscall.h>
1.68 cgd 197: #include <sys/syscallargs.h>
198:
1.281 elad 199: #include <sys/pax.h>
1.334 elad 200:
1.438 jym 201: #include <secmodel/secmodel.h>
202:
1.61 mycroft 203: #include <ufs/ufs/quota.h>
204:
1.159 fvdl 205: #include <miscfs/genfs/genfs.h>
1.410 elad 206: #include <miscfs/specfs/specdev.h>
1.159 fvdl 207:
1.323 ad 208: #include <sys/cpu.h>
1.61 mycroft 209:
1.425 uebayasi 210: #include <uvm/uvm.h> /* extern struct uvm uvm */
1.114 mrg 211:
1.202 lukem 212: #include <dev/cons.h>
1.465 nat 213: #include <dev/splash/splash.h>
1.310 xtraeme 214:
1.414 pooka 215: #include <net/bpf.h>
1.81 christos 216: #include <net/if.h>
217: #include <net/raw_cb.h>
1.468 ozaki-r 218: #include <net/if_llatbl.h>
1.61 mycroft 219:
1.399 pooka 220: #include <prop/proplib.h>
221:
1.398 pooka 222: #include <sys/userconf.h>
223:
1.251 junyoung 224: extern struct lwp lwp0;
1.311 pooka 225: extern time_t rootfstime;
1.251 junyoung 226:
1.216 thorpej 227: #ifndef curlwp
228: struct lwp *curlwp = &lwp0;
1.133 pk 229: #endif
1.105 mycroft 230: struct proc *initproc;
1.61 mycroft 231:
232: struct vnode *rootvp, *swapdev_vp;
233: int boothowto;
1.156 thorpej 234: int cold = 1; /* still working on startup */
1.380 christos 235: struct timespec boottime; /* time at system startup - will only follow settime deltas */
1.61 mycroft 236:
1.356 ad 237: int start_init_exec; /* semaphore for start_init() */
1.163 thorpej 238:
1.437 tls 239: cprng_strong_t *kern_cprng;
240:
1.260 christos 241: static void check_console(struct lwp *l);
1.176 thorpej 242: static void start_init(void *);
1.398 pooka 243: static void configure(void);
244: static void configure2(void);
1.421 tsutsui 245: static void configure3(void);
1.176 thorpej 246: void main(void);
1.280 christos 247:
1.61 mycroft 248: /*
249: * System startup; initialize the world, create process 0, mount root
250: * filesystem, and fork to create init and pagedaemon. Most of the
251: * hard work is done in the lower-level initialization routines including
252: * startup(), which does memory initialization and autoconfiguration.
253: */
1.81 christos 254: void
1.176 thorpej 255: main(void)
1.61 mycroft 256: {
1.380 christos 257: struct timespec time;
1.216 thorpej 258: struct lwp *l;
1.144 thorpej 259: struct proc *p;
1.204 thorpej 260: int s, error;
1.175 jdolecek 261: #ifdef NVNODE_IMPLICIT
262: int usevnodes;
263: #endif
1.312 ad 264: CPU_INFO_ITERATOR cii;
265: struct cpu_info *ci;
1.61 mycroft 266:
1.216 thorpej 267: l = &lwp0;
1.327 matt 268: #ifndef LWP0_CPU_INFO
1.216 thorpej 269: l->l_cpu = curcpu();
1.327 matt 270: #endif
1.394 yamt 271: l->l_pflag |= LP_RUNNING;
1.233 junyoung 272:
1.317 ad 273: /*
1.61 mycroft 274: * Attempt to find console and initialize
275: * in case of early panic or other messages.
276: */
277: consinit();
278:
1.321 ad 279: kernel_lock_init();
1.364 pooka 280: once_init();
1.434 christos 281:
1.440 rmind 282: mi_cpu_init();
1.423 pgoyette 283: kernconfig_lock_init();
1.429 rmind 284: kthread_sysinit();
1.179 thorpej 285:
1.397 pooka 286: /* Initialize the device switch tables. */
287: devsw_init();
288:
1.432 rmind 289: /* Initialize event counters. */
290: evcnt_init();
291:
1.114 mrg 292: uvm_init();
1.457 riastrad 293: ubchist_init();
1.435 rmind 294: kcpuset_sysinit();
1.127 thorpej 295:
1.399 pooka 296: prop_kern_init();
297:
1.377 he 298: #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
1.376 martin 299: ksyms_init();
1.377 he 300: #endif
1.379 pooka 301: kprintf_init();
1.376 martin 302:
1.338 yamt 303: percpu_init();
304:
1.347 ad 305: /* Initialize lock caches. */
306: mutex_obj_init();
1.389 ad 307: rw_obj_init();
1.347 ad 308:
1.434 christos 309: /* Passive serialization. */
310: pserialize_init();
311:
1.307 ad 312: /* Initialize the extent manager. */
313: extent_init();
314:
1.145 thorpej 315: /* Do machine-dependent initialization. */
316: cpu_startup();
1.162 thorpej 317:
1.378 pooka 318: /* Initialize the sysctl subsystem. */
319: sysctl_init();
320:
1.307 ad 321: /* Initialize callouts, part 1. */
1.166 enami 322: callout_startup();
1.145 thorpej 323:
1.409 elad 324: /* Initialize the kernel authorization subsystem. */
325: kauth_init();
326:
1.438 jym 327: secmodel_init();
328:
1.409 elad 329: spec_init();
330:
1.414 pooka 331: /*
332: * Set BPF op vector. Can't do this in bpf attach, since
333: * network drivers attach before bpf.
334: */
335: bpf_setops();
336:
1.403 elad 337: /* Start module system. */
1.417 pooka 338: module_init();
1.403 elad 339:
1.291 elad 340: /*
341: * Initialize the kernel authorization subsystem and start the
342: * default security model, if any. We need to do this early
343: * enough so that subsystems relying on any of the aforementioned
344: * can work properly. Since the security model may dictate the
345: * credential inheritance policy, it is needed at least before
346: * any process is created, specifically proc0.
347: */
1.403 elad 348: module_init_class(MODULE_CLASS_SECMODEL);
1.290 elad 349:
1.228 pk 350: /* Initialize the buffer cache */
351: bufinit();
1.488 ! pgoyette 352: biohist_init();
1.228 pk 353:
1.465 nat 354:
355: #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_IMAGE)
356: size_t splash_size = (&_binary_splash_image_end -
357: &_binary_splash_image_start) * sizeof(void *);
358: splash_setimage(&_binary_splash_image_start, splash_size);
359: #endif
360:
1.128 thorpej 361: /* Initialize sockets. */
362: soinit();
1.127 thorpej 363:
1.156 thorpej 364: /*
1.212 thorpej 365: * The following things must be done before autoconfiguration.
1.156 thorpej 366: */
1.437 tls 367: rnd_init(); /* initialize entropy pool */
368:
369: cprng_init(); /* initialize cryptographic PRNG */
1.176 thorpej 370:
1.236 simonb 371: /* Initialize process and pgrp structures. */
1.63 mycroft 372: procinit();
1.279 thorpej 373: lwpinit();
1.210 thorpej 374:
1.251 junyoung 375: /* Initialize signal-related data structures. */
376: signal_init();
1.61 mycroft 377:
1.333 ad 378: /* Initialize resource management. */
379: resource_init();
380:
1.407 rmind 381: /* Create process 0. */
1.251 junyoung 382: proc0_init();
1.420 pooka 383: lwp0_init();
1.61 mycroft 384:
1.383 yamt 385: /* Disable preemption during boot. */
386: kpreempt_disable();
387:
1.307 ad 388: /* Initialize the UID hash table. */
389: uid_init();
390:
391: /* Charge root for one process. */
1.61 mycroft 392: (void)chgproccnt(0, 1);
393:
1.330 ad 394: /* Initialize timekeeping. */
395: time_init();
396:
1.292 ad 397: /* Initialize the run queues, turnstiles and sleep queues. */
1.302 yamt 398: sched_rqinit();
1.292 ad 399: turnstile_init();
400: sleeptab_init(&sleeptab);
401:
1.405 elad 402: sched_init();
403:
1.339 rmind 404: /* Initialize processor-sets */
405: psets_init();
406:
1.436 jruoho 407: /* Initialize cpufreq(9) */
408: cpufreq_init();
409:
1.302 yamt 410: /* MI initialization of the boot cpu */
411: error = mi_cpu_attach(curcpu());
412: KASSERT(error == 0);
413:
1.350 ad 414: /* Initialize timekeeping, part 2. */
415: time_init2();
416:
1.338 yamt 417: /*
418: * Initialize mbuf's. Do this now because we might attempt to
419: * allocate mbufs or mbuf clusters during autoconfiguration.
420: */
421: mbinit();
422:
1.284 ad 423: /* Initialize I/O statistics. */
424: iostat_init();
425:
1.325 ad 426: /* Initialize the log device. */
427: loginit();
428:
1.403 elad 429: /* Second part of module system initialization. */
1.422 pgoyette 430: module_start_unload_thread();
1.355 ad 431:
1.61 mycroft 432: /* Initialize the file systems. */
1.194 matt 433: #ifdef NVNODE_IMPLICIT
434: /*
435: * If maximum number of vnodes in namei vnode cache is not explicitly
436: * defined in kernel config, adjust the number such as we use roughly
1.359 ad 437: * 10% of memory for vnodes and associated data structures in the
438: * assumed worst case. Do not provide fewer than NVNODE vnodes.
1.194 matt 439: */
1.448 para 440: usevnodes = calc_cache_size(vmem_size(kmem_arena, VMEM_FREE|VMEM_ALLOC),
441: 10, VNODE_KMEM_MAXPCT) / VNODE_COST;
1.205 sommerfe 442: if (usevnodes > desiredvnodes)
1.194 matt 443: desiredvnodes = usevnodes;
444: #endif
1.61 mycroft 445: vfsinit();
1.357 ad 446: lf_init();
1.61 mycroft 447:
1.288 hannken 448: /* Initialize fstrans. */
449: fstrans_init();
1.267 kardel 450:
1.319 ad 451: /* Initialize the file descriptor system. */
1.343 ad 452: fd_sys_init();
1.319 ad 453:
1.375 pooka 454: /* Initialize cwd structures */
455: cwd_sys_init();
456:
1.344 ad 457: /* Initialize kqueue. */
1.343 ad 458: kqueue_init();
1.307 ad 459:
1.268 kardel 460: inittimecounter();
1.267 kardel 461: ntp_init();
462:
1.326 ad 463: /* Initialize tty subsystem. */
464: tty_init();
465: ttyldisc_init();
466:
1.336 ad 467: /* Initialize the buffer cache, part 2. */
468: bufinit2();
469:
1.309 ad 470: /* Initialize the disk wedge subsystem. */
471: dkwedge_init();
472:
1.437 tls 473: /* Initialize the kernel strong PRNG. */
474: kern_cprng = cprng_strong_create("kernel", IPL_VM,
475: CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
1.458 tls 476:
1.360 yamt 477: /* Initialize interfaces. */
478: ifinit1();
479:
1.408 dyoung 480: spldebug_start();
481:
1.433 bouyer 482: /* Initialize sockets thread(s) */
483: soinit1();
484:
1.486 pgoyette 485: /*
1.487 pgoyette 486: * Initialize the bufq strategy sub-system and any built-in
487: * strategy modules - they may be needed by some devices during
488: * auto-configuration
1.486 pgoyette 489: */
1.487 pgoyette 490: bufq_init();
1.486 pgoyette 491: module_init_class(MODULE_CLASS_BUFQ);
492:
1.156 thorpej 493: /* Configure the system hardware. This will enable interrupts. */
494: configure();
1.61 mycroft 495:
1.459 riastrad 496: /* Once all CPUs are detected, initialize the per-CPU cprng_fast. */
497: cprng_fast_init();
498:
1.381 christos 499: ssp_init();
500:
1.424 eeh 501: ubc_init(); /* must be after autoconfig */
502:
1.432 rmind 503: mm_init();
504:
1.381 christos 505: configure2();
1.456 rmind 506:
507: ipi_sysinit();
508:
1.383 yamt 509: /* Now timer is working. Enable preemption. */
510: kpreempt_enable();
511:
1.461 uebayasi 512: /* Get the threads going and into any sleeps before continuing. */
513: yield();
514:
1.453 riastrad 515: /* Enable deferred processing of RNG samples */
516: rnd_init_softint();
517:
1.458 tls 518: #ifdef RND_PRINTF
519: /* Enable periodic injection of console output into entropy pool */
520: kprintf_init_callout();
521: #endif
522:
1.418 cegger 523: vmem_rehash_start(); /* must be before exec_init */
524:
525: /* Initialize exec structures */
526: exec_init(1); /* seminit calls exithook_establish() */
527:
1.273 elad 528: #if NVERIEXEC > 0
1.283 elad 529: /*
530: * Initialise the Veriexec subsystem.
531: */
532: veriexec_init();
1.273 elad 533: #endif /* NVERIEXEC > 0 */
1.251 junyoung 534:
1.281 elad 535: pax_init();
536:
1.449 christos 537: #ifdef IPSEC
1.223 jonathan 538: /* Attach network crypto subsystem */
539: ipsec_attach();
540: #endif
1.61 mycroft 541:
542: /*
543: * Initialize protocols. Block reception of incoming packets
544: * until everything is ready.
545: */
1.190 thorpej 546: s = splnet();
1.61 mycroft 547: ifinit();
1.475 ozaki-r 548: #if defined(INET) || defined(INET6)
1.468 ozaki-r 549: lltableinit();
1.469 ozaki-r 550: #endif
1.390 pooka 551: domaininit(true);
1.200 itojun 552: if_attachdomain();
1.61 mycroft 553: splx(s);
554:
555: #ifdef GPROF
556: /* Initialize kernel profiling. */
557: kmstartup();
558: #endif
1.164 enami 559:
1.351 sborrill 560: /* Initialize system accounting. */
1.164 enami 561: acct_init();
1.61 mycroft 562:
1.301 ad 563: #ifndef PIPE_SOCKETPAIR
1.300 ad 564: /* Initialize pipes. */
565: pipe_init();
1.301 ad 566: #endif
1.300 ad 567:
1.292 ad 568: #ifdef KTRACE
569: /* Initialize ktrace. */
570: ktrinit();
571: #endif
572:
1.406 elad 573: machdep_init();
574:
1.427 pooka 575: procinit_sysctl();
576:
1.472 mrg 577: scdebug_init();
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
1.422 pgoyette 593: * storage to the VM system. Then require force when loading any
594: * remaining un-init'ed built-in modules to avoid later surprises.
1.340 ad 595: */
596: module_init_class(MODULE_CLASS_ANY);
1.422 pgoyette 597: module_builtin_require_force();
1.340 ad 598:
599: /*
1.208 thorpej 600: * Finalize configuration now that all real devices have been
601: * found. This needs to be done before the root device is
602: * selected, since finalization may create the root device.
603: */
604: config_finalize();
1.163 thorpej 605:
1.400 pooka 606: sysctl_finalize();
607:
1.163 thorpej 608: /*
609: * Now that autoconfiguration has completed, we can determine
610: * the root and dump devices.
611: */
1.445 mlelstv 612: cpu_rootconf();
1.101 thorpej 613: cpu_dumpconf();
1.61 mycroft 614:
615: /* Mount the root file system. */
1.95 thorpej 616: do {
1.416 pooka 617: domountroothook(root_device);
1.95 thorpej 618: if ((error = vfs_mountroot())) {
1.97 thorpej 619: printf("cannot mount root, error = %d\n", error);
1.95 thorpej 620: boothowto |= RB_ASKNAME;
621: setroot(root_device,
1.152 thorpej 622: (rootdev != NODEV) ? DISKPART(rootdev) : 0);
1.95 thorpej 623: }
624: } while (error != 0);
625: mountroothook_destroy();
626:
1.421 tsutsui 627: configure3();
628:
1.239 pk 629: /*
630: * Initialise the time-of-day clock, passing the time recorded
631: * in the root filesystem (if any) for use by systems that
632: * don't have a non-volatile time-of-day device.
633: */
634: inittodr(rootfstime);
635:
1.61 mycroft 636: /*
637: * Now can look at time, having had a chance to verify the time
1.292 ad 638: * from the file system. Reset l->l_rtime as it may have been
1.61 mycroft 639: * munched in mi_switch() after the time got set.
640: */
1.380 christos 641: getnanotime(&time);
1.267 kardel 642: boottime = time;
1.447 pgoyette 643:
1.352 ad 644: mutex_enter(proc_lock);
1.225 jdolecek 645: LIST_FOREACH(p, &allproc, p_list) {
1.295 pavel 646: KASSERT((p->p_flag & PK_MARKER) == 0);
1.353 ad 647: mutex_enter(p->p_lock);
1.380 christos 648: TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
1.225 jdolecek 649: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.292 ad 650: lwp_lock(l);
1.332 yamt 651: memset(&l->l_rtime, 0, sizeof(l->l_rtime));
1.292 ad 652: lwp_unlock(l);
1.225 jdolecek 653: }
1.353 ad 654: mutex_exit(p->p_lock);
1.163 thorpej 655: }
1.352 ad 656: mutex_exit(proc_lock);
1.332 yamt 657: binuptime(&curlwp->l_stime);
1.61 mycroft 658:
1.312 ad 659: for (CPU_INFO_FOREACH(cii, ci)) {
660: ci->ci_schedstate.spc_lastmod = time_second;
661: }
662:
1.163 thorpej 663: /* Create the pageout daemon kernel thread. */
664: uvm_swap_init();
1.336 ad 665: if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
1.307 ad 666: NULL, NULL, "pgdaemon"))
1.135 thorpej 667: panic("fork pagedaemon");
1.61 mycroft 668:
1.163 thorpej 669: /* Create the filesystem syncer kernel thread. */
1.336 ad 670: if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
671: NULL, NULL, "ioflush"))
1.159 fvdl 672: panic("fork syncer");
1.185 chs 673:
674: /* Create the aiodone daemon kernel thread. */
1.286 yamt 675: if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
1.336 ad 676: uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
1.185 chs 677: panic("fork aiodoned");
1.137 thorpej 678:
1.462 mrg 679: /* Wait for final configure threads to complete. */
680: config_finalize_mountroot();
681:
1.163 thorpej 682: /*
683: * Okay, now we can let init(8) exec! It's off to userland!
684: */
1.356 ad 685: mutex_enter(proc_lock);
1.163 thorpej 686: start_init_exec = 1;
1.356 ad 687: cv_broadcast(&lbolt);
688: mutex_exit(proc_lock);
1.163 thorpej 689:
1.61 mycroft 690: /* The scheduler is an infinite loop. */
1.114 mrg 691: uvm_scheduler();
1.61 mycroft 692: /* NOTREACHED */
693: }
694:
1.398 pooka 695: /*
696: * Configure the system's hardware.
697: */
698: static void
699: configure(void)
700: {
701:
702: /* Initialize autoconf data structures. */
1.401 pooka 703: config_init_mi();
1.398 pooka 704: /*
705: * XXX
706: * callout_setfunc() requires mutex(9) so it can't be in config_init()
707: * on amiga and atari which use config_init() and autoconf(9) fucntions
708: * to initialize console devices.
709: */
710: config_twiddle_init();
711:
712: pmf_init();
713:
1.471 jmcneill 714: /* Initialize driver modules */
715: module_init_class(MODULE_CLASS_DRIVER);
716:
1.430 uebayasi 717: userconf_init();
1.398 pooka 718: if (boothowto & RB_USERCONF)
1.430 uebayasi 719: userconf_prompt();
1.398 pooka 720:
721: if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
722: printf_nolog("Detecting hardware...");
723: }
724:
725: /*
726: * Do the machine-dependent portion of autoconfiguration. This
727: * sets the configuration machinery here in motion by "finding"
728: * the root bus. When this function returns, we expect interrupts
729: * to be enabled.
730: */
731: cpu_configure();
732: }
733:
734: static void
735: configure2(void)
736: {
737: CPU_INFO_ITERATOR cii;
738: struct cpu_info *ci;
739: int s;
740:
741: /*
742: * Now that we've found all the hardware, start the real time
743: * and statistics clocks.
744: */
745: initclocks();
746:
747: cold = 0; /* clocks are running, we're warm now! */
748: s = splsched();
749: curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
750: splx(s);
751:
752: /* Boot the secondary processors. */
753: for (CPU_INFO_FOREACH(cii, ci)) {
754: uvm_cpu_attach(ci);
755: }
756: mp_online = true;
757: #if defined(MULTIPROCESSOR)
758: cpu_boot_secondary_processors();
759: #endif
760:
761: /* Setup the runqueues and scheduler. */
762: runq_init();
1.405 elad 763: synch_init();
1.398 pooka 764:
765: /*
766: * Bus scans can make it appear as if the system has paused, so
767: * twiddle constantly while config_interrupts() jobs are running.
768: */
769: config_twiddle_fn(NULL);
770:
771: /*
772: * Create threads to call back and finish configuration for
773: * devices that want interrupts enabled.
774: */
775: config_create_interruptthreads();
776: }
777:
1.93 mouse 778: static void
1.421 tsutsui 779: configure3(void)
780: {
781:
782: /*
783: * Create threads to call back and finish configuration for
784: * devices that want the mounted root file system.
785: */
786: config_create_mountrootthreads();
787:
788: /* Get the threads going and into any sleeps before continuing. */
789: yield();
790: }
791:
792: static void
1.443 mlelstv 793: rootconf_handle_wedges(void)
794: {
1.477 christos 795: struct disklabel label;
1.443 mlelstv 796: struct partition *p;
797: struct vnode *vp;
798: daddr_t startblk;
799: uint64_t nblks;
1.482 msaitoh 800: device_t dev;
1.443 mlelstv 801: int error;
802:
803: if (booted_nblks) {
804: /*
805: * bootloader passed geometry
806: */
807: dev = booted_device;
808: startblk = booted_startblk;
809: nblks = booted_nblks;
810:
811: /*
812: * keep booted_device and booted_partition
813: * in case the kernel doesn't identify a wedge
814: */
815: } else {
816: /*
817: * bootloader passed partition number
818: *
819: * We cannot ask the partition device directly when it is
820: * covered by a wedge. Instead we look up the geometry in
821: * the disklabel.
822: */
823: vp = opendisk(booted_device);
1.444 martin 824:
825: if (vp == NULL)
826: return;
827:
1.477 christos 828: error = VOP_IOCTL(vp, DIOCGDINFO, &label, FREAD, NOCRED);
1.443 mlelstv 829: VOP_CLOSE(vp, FREAD, NOCRED);
830: vput(vp);
831: if (error)
832: return;
833:
834: KASSERT(booted_partition >= 0
835: && booted_partition < MAXPARTITIONS);
836:
1.477 christos 837: p = &label.d_partitions[booted_partition];
1.443 mlelstv 838:
839: dev = booted_device;
840: startblk = p->p_offset;
841: nblks = p->p_size;
842: }
843:
844: dev = dkwedge_find_partition(dev, startblk, nblks);
845: if (dev != NULL) {
846: booted_device = dev;
847: booted_partition = 0;
848: }
849: }
850:
1.445 mlelstv 851: void
1.443 mlelstv 852: rootconf(void)
853: {
1.445 mlelstv 854: if (booted_device != NULL)
855: rootconf_handle_wedges();
1.443 mlelstv 856:
857: setroot(booted_device, booted_partition);
858: }
859:
860: static void
1.260 christos 861: check_console(struct lwp *l)
1.93 mouse 862: {
1.391 dholland 863: struct vnode *vp;
1.93 mouse 864: int error;
865:
1.391 dholland 866: error = namei_simple_kernel("/dev/console",
867: NSM_FOLLOW_NOEMULROOT, &vp);
1.96 cgd 868: if (error == 0)
1.391 dholland 869: vrele(vp);
1.96 cgd 870: else if (error == ENOENT)
871: printf("warning: no /dev/console\n");
1.93 mouse 872: else
1.96 cgd 873: printf("warning: lookup /dev/console: error %d\n", error);
1.93 mouse 874: }
875:
1.61 mycroft 876: /*
877: * List of paths to try when searching for "init".
878: */
1.327 matt 879: static const char * const initpaths[] = {
1.61 mycroft 880: "/sbin/init",
881: "/sbin/oinit",
882: "/sbin/init.bak",
1.454 apb 883: "/rescue/init",
1.61 mycroft 884: NULL,
885: };
886:
887: /*
888: * Start the initial user process; try exec'ing each pathname in "initpaths".
889: * The program is invoked with one argument containing the boot flags.
890: */
891: static void
1.176 thorpej 892: start_init(void *arg)
1.61 mycroft 893: {
1.216 thorpej 894: struct lwp *l = arg;
895: struct proc *p = l->l_proc;
1.130 eeh 896: vaddr_t addr;
1.78 mycroft 897: struct sys_execve_args /* {
1.108 mycroft 898: syscallarg(const char *) path;
1.92 cgd 899: syscallarg(char * const *) argp;
900: syscallarg(char * const *) envp;
1.68 cgd 901: } */ args;
902: int options, i, error;
903: register_t retval[2];
1.66 mycroft 904: char flags[4], *flagsp;
1.202 lukem 905: const char *path, *slash;
1.474 christos 906: char *ucp, **uap, *arg0, *arg1, *argv[3];
1.202 lukem 907: char ipath[129];
908: int ipx, len;
1.61 mycroft 909:
1.76 cgd 910: /*
911: * Now in process 1.
912: */
1.146 gwr 913: strncpy(p->p_comm, "init", MAXCOMLEN);
1.163 thorpej 914:
915: /*
916: * Wait for main() to tell us that it's safe to exec.
917: */
1.356 ad 918: mutex_enter(proc_lock);
1.163 thorpej 919: while (start_init_exec == 0)
1.356 ad 920: cv_wait(&lbolt, proc_lock);
921: mutex_exit(proc_lock);
1.93 mouse 922:
923: /*
924: * This is not the right way to do this. We really should
925: * hand-craft a descriptor onto /dev/console to hand to init,
926: * but that's a _lot_ more work, and the benefit from this easy
927: * hack makes up for the "good is the enemy of the best" effect.
928: */
1.260 christos 929: check_console(l);
1.61 mycroft 930:
931: /*
932: * Need just enough stack to hold the faked-up "execve()" arguments.
933: */
1.211 chs 934: addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
1.205 sommerfe 935: if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
1.483 maxv 936: NULL, UVM_UNKNOWN_OFFSET, 0,
937: UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_COPY,
938: UVM_ADV_NORMAL,
939: UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
1.114 mrg 940: panic("init: couldn't allocate argument space");
1.297 christos 941: p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
1.61 mycroft 942:
1.202 lukem 943: ipx = 0;
944: while (1) {
945: if (boothowto & RB_ASKNAME) {
946: printf("init path");
947: if (initpaths[ipx])
948: printf(" (default %s)", initpaths[ipx]);
949: printf(": ");
950: len = cngetsn(ipath, sizeof(ipath)-1);
1.371 tsutsui 951: if (len == 4 && strcmp(ipath, "halt") == 0) {
1.370 tsutsui 952: cpu_reboot(RB_HALT, NULL);
953: } else if (len == 6 && strcmp(ipath, "reboot") == 0) {
954: cpu_reboot(0, NULL);
955: #if defined(DDB)
956: } else if (len == 3 && strcmp(ipath, "ddb") == 0) {
957: console_debugger();
958: continue;
959: #endif
1.371 tsutsui 960: } else if (len > 0 && ipath[0] == '/') {
1.202 lukem 961: ipath[len] = '\0';
962: path = ipath;
1.371 tsutsui 963: } else if (len == 0 && initpaths[ipx] != NULL) {
964: path = initpaths[ipx++];
965: } else {
966: printf("use absolute path, ");
967: #if defined(DDB)
968: printf("\"ddb\", ");
969: #endif
970: printf("\"halt\", or \"reboot\"\n");
971: continue;
1.202 lukem 972: }
973: } else {
1.370 tsutsui 974: if ((path = initpaths[ipx++]) == NULL) {
975: ipx = 0;
976: boothowto |= RB_ASKNAME;
977: continue;
978: }
1.202 lukem 979: }
980:
1.211 chs 981: ucp = (char *)USRSTACK;
1.64 mycroft 982:
1.61 mycroft 983: /*
1.64 mycroft 984: * Construct the boot flag argument.
1.61 mycroft 985: */
1.66 mycroft 986: flagsp = flags;
987: *flagsp++ = '-';
1.61 mycroft 988: options = 0;
1.66 mycroft 989:
1.61 mycroft 990: if (boothowto & RB_SINGLE) {
1.64 mycroft 991: *flagsp++ = 's';
1.61 mycroft 992: options = 1;
993: }
994: #ifdef notyet
995: if (boothowto & RB_FASTBOOT) {
1.64 mycroft 996: *flagsp++ = 'f';
1.61 mycroft 997: options = 1;
998: }
999: #endif
1.64 mycroft 1000:
1001: /*
1002: * Move out the flags (arg 1), if necessary.
1003: */
1004: if (options != 0) {
1005: *flagsp++ = '\0';
1006: i = flagsp - flags;
1007: #ifdef DEBUG
1.415 hubertf 1008: aprint_normal("init: copying out flags `%s' %d\n", flags, i);
1.64 mycroft 1009: #endif
1.211 chs 1010: arg1 = STACK_ALLOC(ucp, i);
1011: ucp = STACK_MAX(arg1, i);
1.474 christos 1012: if ((error = copyout((void *)flags, arg1, i)) != 0)
1013: goto copyerr;
1014: } else
1015: arg1 = NULL;
1.61 mycroft 1016:
1017: /*
1018: * Move out the file name (also arg 0).
1019: */
1.64 mycroft 1020: i = strlen(path) + 1;
1021: #ifdef DEBUG
1.415 hubertf 1022: aprint_normal("init: copying out path `%s' %d\n", path, i);
1.202 lukem 1023: #else
1.203 lukem 1024: if (boothowto & RB_ASKNAME || path != initpaths[0])
1.202 lukem 1025: printf("init: trying %s\n", path);
1.64 mycroft 1026: #endif
1.211 chs 1027: arg0 = STACK_ALLOC(ucp, i);
1028: ucp = STACK_MAX(arg0, i);
1.474 christos 1029: if ((error = copyout(path, arg0, i)) != 0)
1030: goto copyerr;
1.61 mycroft 1031:
1032: /*
1033: * Move out the arg pointers.
1034: */
1.439 christos 1035: ucp = (void *)STACK_ALIGN(ucp, STACK_ALIGNBYTES);
1.479 macallan 1036: uap = (char **)STACK_ALLOC(ucp, sizeof(argv));
1.211 chs 1037: SCARG(&args, path) = arg0;
1038: SCARG(&args, argp) = uap;
1039: SCARG(&args, envp) = NULL;
1.142 mycroft 1040: slash = strrchr(path, '/');
1.474 christos 1041:
1042: argv[0] = slash ? arg0 + (slash + 1 - path) : arg0;
1043: argv[1] = arg1;
1044: argv[2] = NULL;
1.479 macallan 1045: if ((error = copyout(argv, uap, sizeof(argv))) != 0)
1.474 christos 1046: goto copyerr;
1.61 mycroft 1047:
1048: /*
1.481 palle 1049: * Now try to exec the program. If it can't for any reason
1.61 mycroft 1050: * other than it doesn't exist, complain.
1051: */
1.260 christos 1052: error = sys_execve(l, &args, retval);
1.179 thorpej 1053: if (error == 0 || error == EJUSTRETURN) {
1.292 ad 1054: KERNEL_UNLOCK_LAST(l);
1.61 mycroft 1055: return;
1.179 thorpej 1056: }
1.202 lukem 1057: printf("exec %s: error %d\n", path, error);
1.61 mycroft 1058: }
1.90 christos 1059: printf("init: not found\n");
1.61 mycroft 1060: panic("no init");
1.474 christos 1061: copyerr:
1062: panic("copyout %d", error);
1.61 mycroft 1063: }
1.345 yamt 1064:
1065: /*
1.448 para 1066: * calculate cache size (in bytes) from physmem and vsize.
1.345 yamt 1067: */
1068: vaddr_t
1.448 para 1069: calc_cache_size(vsize_t vsize, int pct, int va_pct)
1.345 yamt 1070: {
1071: paddr_t t;
1072:
1073: /* XXX should consider competing cache if any */
1074: /* XXX should consider submaps */
1.346 yamt 1075: t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
1.448 para 1076: if (vsize != 0) {
1.346 yamt 1077: vsize = (uintmax_t)vsize * va_pct / 100;
1078: if (t > vsize) {
1079: t = vsize;
1080: }
1.345 yamt 1081: }
1082: return t;
1083: }
1.385 ad 1084:
1085: /*
1086: * Print the system start up banner.
1087: *
1088: * - Print a limited banner if AB_SILENT.
1089: * - Always send normal banner to the log.
1090: */
1.388 tsutsui 1091: #define MEM_PBUFSIZE sizeof("99999 MB")
1092:
1.385 ad 1093: void
1094: banner(void)
1095: {
1096: static char notice[] = " Notice: this software is "
1097: "protected by copyright";
1.386 ad 1098: char pbuf[81];
1.446 christos 1099: void (*pr)(const char *, ...) __printflike(1, 2);
1.385 ad 1100: int i;
1101:
1102: if ((boothowto & AB_SILENT) != 0) {
1103: snprintf(pbuf, sizeof(pbuf), "%s %s (%s)",
1104: ostype, osrelease, kernel_ident);
1.392 ad 1105: printf_nolog("%s", pbuf);
1.387 ad 1106: for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--)
1.385 ad 1107: printf(" ");
1.392 ad 1108: printf_nolog("%s\n", notice);
1.385 ad 1109: pr = aprint_normal;
1110: } else {
1111: pr = printf;
1112: }
1113:
1114: memset(pbuf, 0, sizeof(pbuf));
1115: (*pr)("%s%s", copyright, version);
1.388 tsutsui 1116: format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem));
1.385 ad 1117: (*pr)("total memory = %s\n", pbuf);
1.388 tsutsui 1118: format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvmexp.free));
1.385 ad 1119: (*pr)("avail memory = %s\n", pbuf);
1120: }
CVSweb <webmaster@jp.NetBSD.org>