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