1df8bae1dSRodney W. Grimes /* 22b14f991SJulian Elischer * Copyright (c) 1995 Terrence R. Lambert 32b14f991SJulian Elischer * All rights reserved. 42b14f991SJulian Elischer * 5df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 6df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 7df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 8df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 9df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 10df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 12df8bae1dSRodney W. Grimes * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 22df8bae1dSRodney W. Grimes * must display the following acknowledgement: 23df8bae1dSRodney W. Grimes * This product includes software developed by the University of 24df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 25df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 26df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 27df8bae1dSRodney W. Grimes * without specific prior written permission. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39df8bae1dSRodney W. Grimes * SUCH DAMAGE. 40df8bae1dSRodney W. Grimes * 41df8bae1dSRodney W. Grimes * @(#)init_main.c 8.9 (Berkeley) 1/21/94 4267481196SJohn Birrell * $Id: init_main.c,v 1.117 1999/05/03 23:57:19 billf Exp $ 43df8bae1dSRodney W. Grimes */ 44df8bae1dSRodney W. Grimes 4519060a3aSPoul-Henning Kamp #include "opt_devfs.h" 4667481196SJohn Birrell #include "opt_init_path.h" 474bd49128SPeter Wemm 48df8bae1dSRodney W. Grimes #include <sys/param.h> 49b3c5c18dSBruce Evans #include <sys/file.h> 50df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 51df8bae1dSRodney W. Grimes #include <sys/kernel.h> 52ac0ad63fSBruce Evans #include <sys/mount.h> 53946bb7a2SPoul-Henning Kamp #include <sys/sysctl.h> 54df8bae1dSRodney W. Grimes #include <sys/proc.h> 55df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 56df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 57df8bae1dSRodney W. Grimes #include <sys/systm.h> 58df8bae1dSRodney W. Grimes #include <sys/vnode.h> 59f3f0ca60SSøren Schmidt #include <sys/sysent.h> 60df8bae1dSRodney W. Grimes #include <sys/reboot.h> 61ad7507e2SSteven Wallace #include <sys/sysproto.h> 62efeaf95aSDavid Greenman #include <sys/vmmeter.h> 6374b2192aSJohn Dyson #include <sys/unistd.h> 6494e9d7c1SPeter Wemm #include <sys/malloc.h> 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes #include <machine/cpu.h> 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes #include <vm/vm.h> 69efeaf95aSDavid Greenman #include <vm/vm_param.h> 70efeaf95aSDavid Greenman #include <vm/vm_prot.h> 71996c772fSJohn Dyson #include <sys/lock.h> 72efeaf95aSDavid Greenman #include <vm/pmap.h> 73efeaf95aSDavid Greenman #include <vm/vm_map.h> 74efeaf95aSDavid Greenman #include <sys/user.h> 756faaa756SWolfram Schneider #include <sys/copyright.h> 76df8bae1dSRodney W. Grimes 779ef6c28aSBruce Evans extern struct linker_set sysinit_set; /* XXX */ 789ef6c28aSBruce Evans 799ef6c28aSBruce Evans extern void __main __P((void)); 809ef6c28aSBruce Evans extern void main __P((void *framep)); 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes /* Components of the first process -- never freed. */ 83154c04e5SPoul-Henning Kamp static struct session session0; 84154c04e5SPoul-Henning Kamp static struct pgrp pgrp0; 85df8bae1dSRodney W. Grimes struct proc proc0; 86154c04e5SPoul-Henning Kamp static struct pcred cred0; 876626c604SJulian Elischer static struct procsig procsig0; 88154c04e5SPoul-Henning Kamp static struct filedesc0 filedesc0; 89154c04e5SPoul-Henning Kamp static struct plimit limit0; 90154c04e5SPoul-Henning Kamp static struct vmspace vmspace0; 912b14f991SJulian Elischer struct proc *initproc; 92df8bae1dSRodney W. Grimes 93dedb7b62SPeter Wemm int cmask = CMASK; 94df8bae1dSRodney W. Grimes extern struct user *proc0paddr; 95df8bae1dSRodney W. Grimes 962976b7f1SDavid Greenman struct vnode *rootvp; 97c463cf1cSBruce Evans int boothowto = 0; /* initialized so that it can be patched */ 98946bb7a2SPoul-Henning Kamp 99df8bae1dSRodney W. Grimes struct timeval boottime; 1003d177f46SBill Fumerola SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD, 1013d177f46SBill Fumerola &boottime, timeval, "System boottime"); 102946bb7a2SPoul-Henning Kamp 1032b14f991SJulian Elischer /* 1042b14f991SJulian Elischer * Promiscuous argument pass for start_init() 1052b14f991SJulian Elischer * 1062b14f991SJulian Elischer * This is a kludge because we use a return from main() rather than a call 10737b688c4SAlexander Langer * to a new routine in locore.s to kick the kernel alive from locore.s. 1082b14f991SJulian Elischer */ 1092b14f991SJulian Elischer static void *init_framep; 1102b14f991SJulian Elischer 111df8bae1dSRodney W. Grimes 11226f9a767SRodney W. Grimes #if __GNUC__ >= 2 11326f9a767SRodney W. Grimes void __main() {} 11426f9a767SRodney W. Grimes #endif 11526f9a767SRodney W. Grimes 1162b14f991SJulian Elischer 11726f9a767SRodney W. Grimes /* 1182b14f991SJulian Elischer * This ensures that there is at least one entry so that the sysinit_set 1192b14f991SJulian Elischer * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 1202b14f991SJulian Elischer * executed. 12126f9a767SRodney W. Grimes */ 1222b14f991SJulian Elischer SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL) 1238a129caeSDavid Greenman 12494e9d7c1SPeter Wemm /* 12594e9d7c1SPeter Wemm * The sysinit table itself. Items are checked off as the are run. 12694e9d7c1SPeter Wemm * If we want to register new sysinit types, add them to newsysinit. 12794e9d7c1SPeter Wemm */ 12894e9d7c1SPeter Wemm struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items; 12994e9d7c1SPeter Wemm struct sysinit **newsysinit; 13094e9d7c1SPeter Wemm 13194e9d7c1SPeter Wemm /* 13294e9d7c1SPeter Wemm * Merge a new sysinit set into the current set, reallocating it if 13394e9d7c1SPeter Wemm * necessary. This can only be called after malloc is running. 13494e9d7c1SPeter Wemm */ 13594e9d7c1SPeter Wemm void 13694e9d7c1SPeter Wemm sysinit_add(set) 13794e9d7c1SPeter Wemm struct sysinit **set; 13894e9d7c1SPeter Wemm { 13994e9d7c1SPeter Wemm struct sysinit **newset; 14094e9d7c1SPeter Wemm struct sysinit **sipp; 14194e9d7c1SPeter Wemm struct sysinit **xipp; 14294e9d7c1SPeter Wemm int count = 0; 14394e9d7c1SPeter Wemm 144ddd62546SPeter Wemm if (newsysinit) 145ddd62546SPeter Wemm for (sipp = newsysinit; *sipp; sipp++) 146ddd62546SPeter Wemm count++; 147ddd62546SPeter Wemm else 14894e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) 14994e9d7c1SPeter Wemm count++; 15094e9d7c1SPeter Wemm for (sipp = set; *sipp; sipp++) 15194e9d7c1SPeter Wemm count++; 152ddd62546SPeter Wemm count++; /* Trailing NULL */ 15394e9d7c1SPeter Wemm newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); 15494e9d7c1SPeter Wemm if (newset == NULL) 15594e9d7c1SPeter Wemm panic("cannot malloc for sysinit"); 15694e9d7c1SPeter Wemm xipp = newset; 157ddd62546SPeter Wemm if (newsysinit) 158ddd62546SPeter Wemm for (sipp = newsysinit; *sipp; sipp++) 159ddd62546SPeter Wemm *xipp++ = *sipp; 160ddd62546SPeter Wemm else 16194e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) 16294e9d7c1SPeter Wemm *xipp++ = *sipp; 16394e9d7c1SPeter Wemm for (sipp = set; *sipp; sipp++) 16494e9d7c1SPeter Wemm *xipp++ = *sipp; 165ddd62546SPeter Wemm *xipp = NULL; 166ddd62546SPeter Wemm if (newsysinit) 167ddd62546SPeter Wemm free(newsysinit, M_TEMP); 16894e9d7c1SPeter Wemm newsysinit = newset; 16994e9d7c1SPeter Wemm } 17026f9a767SRodney W. Grimes 171df8bae1dSRodney W. Grimes /* 172df8bae1dSRodney W. Grimes * System startup; initialize the world, create process 0, mount root 173df8bae1dSRodney W. Grimes * filesystem, and fork to create init and pagedaemon. Most of the 174df8bae1dSRodney W. Grimes * hard work is done in the lower-level initialization routines including 175df8bae1dSRodney W. Grimes * startup(), which does memory initialization and autoconfiguration. 1762b14f991SJulian Elischer * 1772b14f991SJulian Elischer * This allows simple addition of new kernel subsystems that require 1782b14f991SJulian Elischer * boot time initialization. It also allows substitution of subsystem 1792b14f991SJulian Elischer * (for instance, a scheduler, kernel profiler, or VM system) by object 180c5b193bfSPoul-Henning Kamp * module. Finally, it allows for optional "kernel threads". 181df8bae1dSRodney W. Grimes */ 18226f9a767SRodney W. Grimes void 183df8bae1dSRodney W. Grimes main(framep) 184df8bae1dSRodney W. Grimes void *framep; 185df8bae1dSRodney W. Grimes { 1862b14f991SJulian Elischer 1872b14f991SJulian Elischer register struct sysinit **sipp; /* system initialization*/ 1882b14f991SJulian Elischer register struct sysinit **xipp; /* interior loop of sort*/ 1892b14f991SJulian Elischer register struct sysinit *save; /* bubble*/ 1902b14f991SJulian Elischer 1912b14f991SJulian Elischer /* 192a2a1c95cSPeter Wemm * Copy the locore.s frame pointer for proc0, this is forked into 193a2a1c95cSPeter Wemm * all other processes. 1942b14f991SJulian Elischer */ 1952b14f991SJulian Elischer init_framep = framep; 1962b14f991SJulian Elischer 19794e9d7c1SPeter Wemm restart: 1982b14f991SJulian Elischer /* 1992b14f991SJulian Elischer * Perform a bubble sort of the system initialization objects by 2002b14f991SJulian Elischer * their subsystem (primary key) and order (secondary key). 2012b14f991SJulian Elischer */ 20294e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) { 2032b14f991SJulian Elischer for (xipp = sipp + 1; *xipp; xipp++) { 2042b14f991SJulian Elischer if ((*sipp)->subsystem < (*xipp)->subsystem || 2052b14f991SJulian Elischer ((*sipp)->subsystem == (*xipp)->subsystem && 2062b14f991SJulian Elischer (*sipp)->order < (*xipp)->order)) 2072b14f991SJulian Elischer continue; /* skip*/ 2082b14f991SJulian Elischer save = *sipp; 2092b14f991SJulian Elischer *sipp = *xipp; 2102b14f991SJulian Elischer *xipp = save; 2112b14f991SJulian Elischer } 2122b14f991SJulian Elischer } 2132b14f991SJulian Elischer 2142b14f991SJulian Elischer /* 2152b14f991SJulian Elischer * Traverse the (now) ordered list of system initialization tasks. 2162b14f991SJulian Elischer * Perform each task, and continue on to the next task. 2172b14f991SJulian Elischer * 2182b14f991SJulian Elischer * The last item on the list is expected to be the scheduler, 2192b14f991SJulian Elischer * which will not return. 2202b14f991SJulian Elischer */ 22194e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) { 22274b2192aSJohn Dyson 2232b14f991SJulian Elischer if ((*sipp)->subsystem == SI_SUB_DUMMY) 2242b14f991SJulian Elischer continue; /* skip dummy task(s)*/ 2252b14f991SJulian Elischer 22694e9d7c1SPeter Wemm if ((*sipp)->subsystem == SI_SUB_DONE) 22794e9d7c1SPeter Wemm continue; 22894e9d7c1SPeter Wemm 2292b14f991SJulian Elischer switch( (*sipp)->type) { 2302b14f991SJulian Elischer case SI_TYPE_DEFAULT: 2312b14f991SJulian Elischer /* no special processing*/ 2322b14f991SJulian Elischer (*((*sipp)->func))((*sipp)->udata); 2332b14f991SJulian Elischer break; 2342b14f991SJulian Elischer 2352b14f991SJulian Elischer case SI_TYPE_KTHREAD: 2362b14f991SJulian Elischer /* kernel thread*/ 23774b2192aSJohn Dyson if (fork1(&proc0, RFMEM|RFFDG|RFPROC)) 23874b2192aSJohn Dyson panic("fork kernel thread"); 23974b2192aSJohn Dyson cpu_set_fork_handler(pfind(proc0.p_retval[0]), 24074b2192aSJohn Dyson (*sipp)->func, (*sipp)->udata); 24174b2192aSJohn Dyson break; 24274b2192aSJohn Dyson 24374b2192aSJohn Dyson case SI_TYPE_KPROCESS: 24474b2192aSJohn Dyson if (fork1(&proc0, RFFDG|RFPROC)) 2452b14f991SJulian Elischer panic("fork kernel process"); 246cb226aaaSPoul-Henning Kamp cpu_set_fork_handler(pfind(proc0.p_retval[0]), 247cb226aaaSPoul-Henning Kamp (*sipp)->func, (*sipp)->udata); 2482b14f991SJulian Elischer break; 2492b14f991SJulian Elischer 2502b14f991SJulian Elischer default: 2512b14f991SJulian Elischer panic("init_main: unrecognized init type"); 2522b14f991SJulian Elischer } 25394e9d7c1SPeter Wemm 25494e9d7c1SPeter Wemm /* Check off the one we're just done */ 25594e9d7c1SPeter Wemm (*sipp)->subsystem = SI_SUB_DONE; 25694e9d7c1SPeter Wemm 25794e9d7c1SPeter Wemm /* Check if we've installed more sysinit items via KLD */ 25894e9d7c1SPeter Wemm if (newsysinit != NULL) { 25994e9d7c1SPeter Wemm if (sysinit != (struct sysinit **)sysinit_set.ls_items) 26094e9d7c1SPeter Wemm free(sysinit, M_TEMP); 26194e9d7c1SPeter Wemm sysinit = newsysinit; 26294e9d7c1SPeter Wemm newsysinit = NULL; 26394e9d7c1SPeter Wemm goto restart; 26494e9d7c1SPeter Wemm } 2652b14f991SJulian Elischer } 2662b14f991SJulian Elischer 267477a642cSPeter Wemm panic("Shouldn't get here!"); 2682b14f991SJulian Elischer /* NOTREACHED*/ 2692b14f991SJulian Elischer } 2702b14f991SJulian Elischer 2712b14f991SJulian Elischer 2722b14f991SJulian Elischer /* 2732b14f991SJulian Elischer * Start a kernel process. This is called after a fork() call in 2742b14f991SJulian Elischer * main() in the file kern/init_main.c. 2752b14f991SJulian Elischer * 2762b14f991SJulian Elischer * This function is used to start "internal" daemons. 2772b14f991SJulian Elischer */ 2782b14f991SJulian Elischer /* ARGSUSED*/ 2792b14f991SJulian Elischer void 2802b14f991SJulian Elischer kproc_start(udata) 281bc814931SMatthew Dillon const void *udata; 2822b14f991SJulian Elischer { 283bc814931SMatthew Dillon const struct kproc_desc *kp = udata; 2842b14f991SJulian Elischer struct proc *p = curproc; 2852b14f991SJulian Elischer 286477a642cSPeter Wemm #ifdef DIAGNOSTIC 287477a642cSPeter Wemm printf("Start pid=%d <%s>\n",p->p_pid, kp->arg0); 288477a642cSPeter Wemm #endif 289477a642cSPeter Wemm 2902b14f991SJulian Elischer /* save a global descriptor, if desired*/ 2912b14f991SJulian Elischer if( kp->global_procpp != NULL) 2922b14f991SJulian Elischer *kp->global_procpp = p; 2932b14f991SJulian Elischer 2942b14f991SJulian Elischer /* this is a non-swapped system process*/ 2952b14f991SJulian Elischer p->p_flag |= P_INMEM | P_SYSTEM; 2962b14f991SJulian Elischer 2972b14f991SJulian Elischer /* set up arg0 for 'ps', et al*/ 2982b14f991SJulian Elischer strcpy( p->p_comm, kp->arg0); 2992b14f991SJulian Elischer 3002b14f991SJulian Elischer /* call the processes' main()...*/ 3012b14f991SJulian Elischer (*kp->func)(); 3022b14f991SJulian Elischer 3032b14f991SJulian Elischer /* NOTREACHED */ 3042b14f991SJulian Elischer panic("kproc_start: %s", kp->arg0); 3052b14f991SJulian Elischer } 3062b14f991SJulian Elischer 3072b14f991SJulian Elischer 3082b14f991SJulian Elischer /* 3092b14f991SJulian Elischer *************************************************************************** 3102b14f991SJulian Elischer **** 3112b14f991SJulian Elischer **** The following SYSINIT's belong elsewhere, but have not yet 3122b14f991SJulian Elischer **** been moved. 3132b14f991SJulian Elischer **** 3142b14f991SJulian Elischer *************************************************************************** 3152b14f991SJulian Elischer */ 3162b14f991SJulian Elischer #ifdef OMIT 3172b14f991SJulian Elischer /* 3182b14f991SJulian Elischer * Handled by vfs_mountroot (bad idea) at this time... should be 3192b14f991SJulian Elischer * done the same as 4.4Lite2. 3202b14f991SJulian Elischer */ 3212b14f991SJulian Elischer SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL) 3222b14f991SJulian Elischer #endif /* OMIT*/ 3232b14f991SJulian Elischer 3244590fd3aSDavid Greenman static void print_caddr_t __P((void *data)); 3259ef6c28aSBruce Evans static void 3269ef6c28aSBruce Evans print_caddr_t(data) 3274590fd3aSDavid Greenman void *data; 3289ef6c28aSBruce Evans { 3299ef6c28aSBruce Evans printf("%s", (char *)data); 3309ef6c28aSBruce Evans } 331d841aaa7SBruce Evans SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright) 3322b14f991SJulian Elischer 3332b14f991SJulian Elischer 3342b14f991SJulian Elischer /* 3352b14f991SJulian Elischer *************************************************************************** 3362b14f991SJulian Elischer **** 3372b14f991SJulian Elischer **** The two following SYSINT's are proc0 specific glue code. I am not 3382b14f991SJulian Elischer **** convinced that they can not be safely combined, but their order of 3392b14f991SJulian Elischer **** operation has been maintained as the same as the original init_main.c 3402b14f991SJulian Elischer **** for right now. 3412b14f991SJulian Elischer **** 3422b14f991SJulian Elischer **** These probably belong in init_proc.c or kern_proc.c, since they 3432b14f991SJulian Elischer **** deal with proc0 (the fork template process). 3442b14f991SJulian Elischer **** 3452b14f991SJulian Elischer *************************************************************************** 3462b14f991SJulian Elischer */ 3472b14f991SJulian Elischer /* ARGSUSED*/ 348154c04e5SPoul-Henning Kamp static void proc0_init __P((void *dummy)); 349154c04e5SPoul-Henning Kamp static void 350d841aaa7SBruce Evans proc0_init(dummy) 351d841aaa7SBruce Evans void *dummy; 3522b14f991SJulian Elischer { 353df8bae1dSRodney W. Grimes register struct proc *p; 354df8bae1dSRodney W. Grimes register struct filedesc0 *fdp; 35592579404SAlexander Langer register unsigned i; 356df8bae1dSRodney W. Grimes 357df8bae1dSRodney W. Grimes p = &proc0; 358df8bae1dSRodney W. Grimes 359df8bae1dSRodney W. Grimes /* 360a3bfb996SJeffrey Hsu * Initialize process and pgrp structures. 361a3bfb996SJeffrey Hsu */ 362a3bfb996SJeffrey Hsu procinit(); 363a3bfb996SJeffrey Hsu 364a3bfb996SJeffrey Hsu /* 365b1508c72SDavid Greenman * Initialize sleep queue hash table 366b1508c72SDavid Greenman */ 367b1508c72SDavid Greenman sleepinit(); 368b1508c72SDavid Greenman 369b1508c72SDavid Greenman /* 3703075778bSJohn Dyson * additional VM structures 3713075778bSJohn Dyson */ 3723075778bSJohn Dyson vm_init2(); 3733075778bSJohn Dyson 3743075778bSJohn Dyson /* 375df8bae1dSRodney W. Grimes * Create process 0 (the swapper). 376df8bae1dSRodney W. Grimes */ 377a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(&allproc, p, p_list); 378df8bae1dSRodney W. Grimes p->p_pgrp = &pgrp0; 379a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 380a3bfb996SJeffrey Hsu LIST_INIT(&pgrp0.pg_members); 381a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 382a3bfb996SJeffrey Hsu 383df8bae1dSRodney W. Grimes pgrp0.pg_session = &session0; 384df8bae1dSRodney W. Grimes session0.s_count = 1; 385df8bae1dSRodney W. Grimes session0.s_leader = p; 386df8bae1dSRodney W. Grimes 387f3f0ca60SSøren Schmidt p->p_sysent = &aout_sysvec; 388f3f0ca60SSøren Schmidt 389df8bae1dSRodney W. Grimes p->p_flag = P_INMEM | P_SYSTEM; 390df8bae1dSRodney W. Grimes p->p_stat = SRUN; 391df8bae1dSRodney W. Grimes p->p_nice = NZERO; 3927216391eSDavid Greenman p->p_rtprio.type = RTP_PRIO_NORMAL; 3937216391eSDavid Greenman p->p_rtprio.prio = 0; 394f992f480SDavid Greenman 3952c1011f7SJohn Dyson /* 3962c1011f7SJohn Dyson * Link for kernel based threads 3972c1011f7SJohn Dyson */ 3982c1011f7SJohn Dyson p->p_peers = 0; 3992c1011f7SJohn Dyson p->p_leader = p; 4002c1011f7SJohn Dyson 401df8bae1dSRodney W. Grimes bcopy("swapper", p->p_comm, sizeof ("swapper")); 402df8bae1dSRodney W. Grimes 403df8bae1dSRodney W. Grimes /* Create credentials. */ 404df8bae1dSRodney W. Grimes cred0.p_refcnt = 1; 405df8bae1dSRodney W. Grimes p->p_cred = &cred0; 406df8bae1dSRodney W. Grimes p->p_ucred = crget(); 407df8bae1dSRodney W. Grimes p->p_ucred->cr_ngroups = 1; /* group 0 */ 408df8bae1dSRodney W. Grimes 40975c13541SPoul-Henning Kamp /* Don't jail it */ 41075c13541SPoul-Henning Kamp p->p_prison = 0; 41175c13541SPoul-Henning Kamp 4126626c604SJulian Elischer /* Create procsig. */ 4136626c604SJulian Elischer p->p_procsig = &procsig0; 41475ffaf59SLuoqi Chen p->p_procsig->ps_refcnt = 1; 4156626c604SJulian Elischer 416df8bae1dSRodney W. Grimes /* Create the file descriptor table. */ 417df8bae1dSRodney W. Grimes fdp = &filedesc0; 418df8bae1dSRodney W. Grimes p->p_fd = &fdp->fd_fd; 419df8bae1dSRodney W. Grimes fdp->fd_fd.fd_refcnt = 1; 420df8bae1dSRodney W. Grimes fdp->fd_fd.fd_cmask = cmask; 421df8bae1dSRodney W. Grimes fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 422df8bae1dSRodney W. Grimes fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 423df8bae1dSRodney W. Grimes fdp->fd_fd.fd_nfiles = NDFILE; 424df8bae1dSRodney W. Grimes 425df8bae1dSRodney W. Grimes /* Create the limits structures. */ 426df8bae1dSRodney W. Grimes p->p_limit = &limit0; 427df8bae1dSRodney W. Grimes for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 428df8bae1dSRodney W. Grimes limit0.pl_rlimit[i].rlim_cur = 429df8bae1dSRodney W. Grimes limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 430b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 431b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 432b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 433b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 434df8bae1dSRodney W. Grimes i = ptoa(cnt.v_free_count); 435df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 436df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 437df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 438e796e00dSPoul-Henning Kamp limit0.p_cpulimit = RLIM_INFINITY; 439df8bae1dSRodney W. Grimes limit0.p_refcnt = 1; 440df8bae1dSRodney W. Grimes 441e796e00dSPoul-Henning Kamp 442df8bae1dSRodney W. Grimes /* Allocate a prototype map so we have something to fork. */ 443b1028ad1SLuoqi Chen pmap_pinit0(vmspace_pmap(&vmspace0)); 444df8bae1dSRodney W. Grimes p->p_vmspace = &vmspace0; 445df8bae1dSRodney W. Grimes vmspace0.vm_refcnt = 1; 44626f9a767SRodney W. Grimes vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS), 4472d8acc0fSJohn Dyson trunc_page(VM_MAXUSER_ADDRESS)); 448b1028ad1SLuoqi Chen vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0); 449df8bae1dSRodney W. Grimes p->p_addr = proc0paddr; /* XXX */ 450df8bae1dSRodney W. Grimes 451ecbb00a2SDoug Rabson #ifndef __alpha__ /* XXX what is this? */ 4522b14f991SJulian Elischer #define INCOMPAT_LITES2 4532b14f991SJulian Elischer #ifdef INCOMPAT_LITES2 454df8bae1dSRodney W. Grimes /* 455dc733423SDag-Erling Smørgrav * proc0 needs to have a coherent frame base in its stack. 4562384fde5SDavid Greenman */ 4572b14f991SJulian Elischer cpu_set_init_frame(p, init_framep); /* XXX! */ 4582b14f991SJulian Elischer #endif /* INCOMPAT_LITES2*/ 459ecbb00a2SDoug Rabson #endif 4602384fde5SDavid Greenman 46139fb8e6bSJulian Elischer /* 462df8bae1dSRodney W. Grimes * We continue to place resource usage info and signal 463df8bae1dSRodney W. Grimes * actions in the user struct so they're pageable. 464df8bae1dSRodney W. Grimes */ 465df8bae1dSRodney W. Grimes p->p_stats = &p->p_addr->u_stats; 466df8bae1dSRodney W. Grimes p->p_sigacts = &p->p_addr->u_sigacts; 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes /* 469a3bfb996SJeffrey Hsu * Charge root for one process. 470df8bae1dSRodney W. Grimes */ 471df8bae1dSRodney W. Grimes (void)chgproccnt(0, 1); 4722a024a2bSSean Eric Fagan 4732a024a2bSSean Eric Fagan /* 474188554bbSDmitrij Tejblum * Initialize the current process pointer (curproc) before 475188554bbSDmitrij Tejblum * any possible traps/probes to simplify trap processing. 4762a024a2bSSean Eric Fagan */ 477188554bbSDmitrij Tejblum SET_CURPROC(p); 4782a024a2bSSean Eric Fagan 47926f9a767SRodney W. Grimes } 4802b14f991SJulian Elischer SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL) 4812b14f991SJulian Elischer 4822b14f991SJulian Elischer /* ARGSUSED*/ 483154c04e5SPoul-Henning Kamp static void proc0_post __P((void *dummy)); 484154c04e5SPoul-Henning Kamp static void 485d841aaa7SBruce Evans proc0_post(dummy) 486d841aaa7SBruce Evans void *dummy; 4872b14f991SJulian Elischer { 48800af9731SPoul-Henning Kamp struct timespec ts; 489a6fc8288SPeter Wemm 4902b14f991SJulian Elischer /* 4911b0b259eSBruce Evans * Now we can look at the time, having had a chance to verify the 4921b0b259eSBruce Evans * time from the file system. Pretend that proc0 started now. 4932b14f991SJulian Elischer */ 4945f88ec36SPoul-Henning Kamp microtime(&proc0.p_stats->p_start); 495e796e00dSPoul-Henning Kamp proc0.p_runtime = 0; 496e7ba67f2SBruce Evans microuptime(&switchtime); 4971b0b259eSBruce Evans switchticks = ticks; 4982b14f991SJulian Elischer 499a6fc8288SPeter Wemm /* 500a6fc8288SPeter Wemm * Give the ``random'' number generator a thump. 5015f88ec36SPoul-Henning Kamp * XXX: Does read_random() contain enough bits to be used here ? 502a6fc8288SPeter Wemm */ 50300af9731SPoul-Henning Kamp nanotime(&ts); 50400af9731SPoul-Henning Kamp srandom(ts.tv_sec ^ ts.tv_nsec); 505a6fc8288SPeter Wemm 5062b14f991SJulian Elischer /* Initialize signal state for process 0. */ 5072b14f991SJulian Elischer siginit(&proc0); 5082b14f991SJulian Elischer } 5092b14f991SJulian Elischer SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL) 5102b14f991SJulian Elischer 5112b14f991SJulian Elischer 5122b14f991SJulian Elischer 513df8bae1dSRodney W. Grimes 514df8bae1dSRodney W. Grimes /* 5152b14f991SJulian Elischer *************************************************************************** 5162b14f991SJulian Elischer **** 5172b14f991SJulian Elischer **** The following SYSINIT's and glue code should be moved to the 5182b14f991SJulian Elischer **** respective files on a per subsystem basis. 5192b14f991SJulian Elischer **** 5202b14f991SJulian Elischer *************************************************************************** 521df8bae1dSRodney W. Grimes */ 522df8bae1dSRodney W. Grimes 5232b14f991SJulian Elischer /* ARGSUSED */ 524ab36c067SJustin T. Gibbs static void root_conf __P((void *dummy)); 525ab36c067SJustin T. Gibbs static void 526ab36c067SJustin T. Gibbs root_conf(dummy) 527ab36c067SJustin T. Gibbs void *dummy; 528ab36c067SJustin T. Gibbs { 529ab36c067SJustin T. Gibbs cpu_rootconf(); 530ab36c067SJustin T. Gibbs } 531ab36c067SJustin T. Gibbs SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL) 532ab36c067SJustin T. Gibbs 533ab36c067SJustin T. Gibbs /* ARGSUSED*/ 534154c04e5SPoul-Henning Kamp static void xxx_vfs_root_fdtab __P((void *dummy)); 535154c04e5SPoul-Henning Kamp static void 536d841aaa7SBruce Evans xxx_vfs_root_fdtab(dummy) 537d841aaa7SBruce Evans void *dummy; 5382b14f991SJulian Elischer { 5392b14f991SJulian Elischer register struct filedesc0 *fdp = &filedesc0; 540df8bae1dSRodney W. Grimes 541df8bae1dSRodney W. Grimes /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ 542628641f8SDavid Greenman if (VFS_ROOT(mountlist.cqh_first, &rootvnode)) 543df8bae1dSRodney W. Grimes panic("cannot find root vnode"); 544df8bae1dSRodney W. Grimes fdp->fd_fd.fd_cdir = rootvnode; 545df8bae1dSRodney W. Grimes VREF(fdp->fd_fd.fd_cdir); 546996c772fSJohn Dyson VOP_UNLOCK(rootvnode, 0, &proc0); 5479f24f214SJohn Dyson fdp->fd_fd.fd_rdir = rootvnode; 5482b14f991SJulian Elischer } 5492b14f991SJulian Elischer SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL) 5502b14f991SJulian Elischer 551df8bae1dSRodney W. Grimes 552df8bae1dSRodney W. Grimes /* 5532b14f991SJulian Elischer *************************************************************************** 5542b14f991SJulian Elischer **** 5552b14f991SJulian Elischer **** The following code probably belongs in another file, like 5562b14f991SJulian Elischer **** kern/init_init.c. It is here for two reasons only: 5572b14f991SJulian Elischer **** 5582b14f991SJulian Elischer **** 1) This code returns to startup the system; this is 5592b14f991SJulian Elischer **** abnormal for a kernel thread. 5602b14f991SJulian Elischer **** 2) This code promiscuously uses init_frame 5612b14f991SJulian Elischer **** 5622b14f991SJulian Elischer *************************************************************************** 563df8bae1dSRodney W. Grimes */ 564df8bae1dSRodney W. Grimes 5653cfc69e6SMatthew Dillon static void kthread_init __P((const void *dummy)); 56674b2192aSJohn Dyson SYSINIT_KP(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL) 5672b14f991SJulian Elischer 5682b14f991SJulian Elischer 569a2a1c95cSPeter Wemm extern void prepare_usermode __P((void)); 570a2a1c95cSPeter Wemm static void start_init __P((struct proc *p)); 5712b14f991SJulian Elischer 5722b14f991SJulian Elischer /* ARGSUSED*/ 5732b14f991SJulian Elischer static void 574d841aaa7SBruce Evans kthread_init(dummy) 5753cfc69e6SMatthew Dillon const void *dummy; 5762b14f991SJulian Elischer { 577df8bae1dSRodney W. Grimes /* Create process 1 (init(8)). */ 578a2a1c95cSPeter Wemm start_init(curproc); 579a2a1c95cSPeter Wemm 580a2a1c95cSPeter Wemm prepare_usermode(); 5812b14f991SJulian Elischer 5822b14f991SJulian Elischer /* 583a2a1c95cSPeter Wemm * This returns to the fork trampoline, then to user mode. 5842b14f991SJulian Elischer */ 585df8bae1dSRodney W. Grimes return; 586df8bae1dSRodney W. Grimes } 587df8bae1dSRodney W. Grimes 588df8bae1dSRodney W. Grimes 589df8bae1dSRodney W. Grimes /* 590df8bae1dSRodney W. Grimes * List of paths to try when searching for "init". 591df8bae1dSRodney W. Grimes */ 5925f967b24SDag-Erling Smørgrav static char init_path[MAXPATHLEN] = 59367481196SJohn Birrell #ifdef INIT_PATH 59467481196SJohn Birrell __XSTRING(INIT_PATH); 59567481196SJohn Birrell #else 5965f967b24SDag-Erling Smørgrav "/sbin/init;/sbin/oinit;/sbin/init.bak;/stand/sysinstall"; 59767481196SJohn Birrell #endif 5985f967b24SDag-Erling Smørgrav SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, ""); 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes /* 6015f967b24SDag-Erling Smørgrav * Start the initial user process; try exec'ing each pathname in init_path. 602df8bae1dSRodney W. Grimes * The program is invoked with one argument containing the boot flags. 603df8bae1dSRodney W. Grimes */ 604df8bae1dSRodney W. Grimes static void 605a2a1c95cSPeter Wemm start_init(p) 606df8bae1dSRodney W. Grimes struct proc *p; 607df8bae1dSRodney W. Grimes { 608df8bae1dSRodney W. Grimes vm_offset_t addr; 609df8bae1dSRodney W. Grimes struct execve_args args; 6105f967b24SDag-Erling Smørgrav int options, error; 6115f967b24SDag-Erling Smørgrav char *var, *path, *next, *s; 6125f967b24SDag-Erling Smørgrav char *ucp, **uap, *arg0, *arg1; 613df8bae1dSRodney W. Grimes 614df8bae1dSRodney W. Grimes initproc = p; 615df8bae1dSRodney W. Grimes 616df8bae1dSRodney W. Grimes /* 617df8bae1dSRodney W. Grimes * Need just enough stack to hold the faked-up "execve()" arguments. 618df8bae1dSRodney W. Grimes */ 6199c0fed3dSDoug Rabson addr = trunc_page(USRSTACK - PAGE_SIZE); 6205f967b24SDag-Erling Smørgrav if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, 6215f967b24SDag-Erling Smørgrav FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) 622df8bae1dSRodney W. Grimes panic("init: couldn't allocate argument space"); 623df8bae1dSRodney W. Grimes p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 62426f9a767SRodney W. Grimes p->p_vmspace->vm_ssize = 1; 625df8bae1dSRodney W. Grimes 6265f967b24SDag-Erling Smørgrav if ((var = getenv("init_path")) != NULL) { 6275f967b24SDag-Erling Smørgrav strncpy(init_path, var, MAXPATHLEN); 6285f967b24SDag-Erling Smørgrav init_path[sizeof init_path - 1] = 0; 6295f967b24SDag-Erling Smørgrav } 6305f967b24SDag-Erling Smørgrav 63167481196SJohn Birrell for (path = init_path; *path != '\0'; path = next) { 6325f967b24SDag-Erling Smørgrav while (*path == ';') 6335f967b24SDag-Erling Smørgrav path++; 63467481196SJohn Birrell if (*path == '\0') 6355f967b24SDag-Erling Smørgrav break; 6365f967b24SDag-Erling Smørgrav for (next = path; *next != '\0' && *next != ';'; next++) 6375f967b24SDag-Erling Smørgrav /* nothing */ ; 6385f967b24SDag-Erling Smørgrav if (bootverbose) 639ba41a07dSDmitrij Tejblum printf("start_init: trying %.*s\n", (int)(next - path), 640ba41a07dSDmitrij Tejblum path); 6415f967b24SDag-Erling Smørgrav 642df8bae1dSRodney W. Grimes /* 643df8bae1dSRodney W. Grimes * Move out the boot flag argument. 644df8bae1dSRodney W. Grimes */ 645df8bae1dSRodney W. Grimes options = 0; 646df8bae1dSRodney W. Grimes ucp = (char *)USRSTACK; 647df8bae1dSRodney W. Grimes (void)subyte(--ucp, 0); /* trailing zero */ 648df8bae1dSRodney W. Grimes if (boothowto & RB_SINGLE) { 649df8bae1dSRodney W. Grimes (void)subyte(--ucp, 's'); 650df8bae1dSRodney W. Grimes options = 1; 651df8bae1dSRodney W. Grimes } 652df8bae1dSRodney W. Grimes #ifdef notyet 653df8bae1dSRodney W. Grimes if (boothowto & RB_FASTBOOT) { 654df8bae1dSRodney W. Grimes (void)subyte(--ucp, 'f'); 655df8bae1dSRodney W. Grimes options = 1; 656df8bae1dSRodney W. Grimes } 657df8bae1dSRodney W. Grimes #endif 65817755ac8SPoul-Henning Kamp 65917755ac8SPoul-Henning Kamp #ifdef BOOTCDROM 66017755ac8SPoul-Henning Kamp (void)subyte(--ucp, 'C'); 66117755ac8SPoul-Henning Kamp options = 1; 66217755ac8SPoul-Henning Kamp #endif 663df8bae1dSRodney W. Grimes if (options == 0) 664df8bae1dSRodney W. Grimes (void)subyte(--ucp, '-'); 665df8bae1dSRodney W. Grimes (void)subyte(--ucp, '-'); /* leading hyphen */ 666df8bae1dSRodney W. Grimes arg1 = ucp; 667df8bae1dSRodney W. Grimes 668df8bae1dSRodney W. Grimes /* 669df8bae1dSRodney W. Grimes * Move out the file name (also arg 0). 670df8bae1dSRodney W. Grimes */ 6715f967b24SDag-Erling Smørgrav (void)subyte(--ucp, 0); 6725f967b24SDag-Erling Smørgrav for (s = next - 1; s >= path; s--) 6735f967b24SDag-Erling Smørgrav (void)subyte(--ucp, *s); 674df8bae1dSRodney W. Grimes arg0 = ucp; 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes /* 677df8bae1dSRodney W. Grimes * Move out the arg pointers. 678df8bae1dSRodney W. Grimes */ 679a20d7755SDoug Rabson uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 680c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)0); /* terminator */ 681c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 682c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 683df8bae1dSRodney W. Grimes 684df8bae1dSRodney W. Grimes /* 685df8bae1dSRodney W. Grimes * Point at the arguments. 686df8bae1dSRodney W. Grimes */ 687df8bae1dSRodney W. Grimes args.fname = arg0; 68826f9a767SRodney W. Grimes args.argv = uap; 68926f9a767SRodney W. Grimes args.envv = NULL; 690df8bae1dSRodney W. Grimes 691df8bae1dSRodney W. Grimes /* 692df8bae1dSRodney W. Grimes * Now try to exec the program. If can't for any reason 693df8bae1dSRodney W. Grimes * other than it doesn't exist, complain. 6942b14f991SJulian Elischer * 6952b14f991SJulian Elischer * Otherwise return to main() which returns to btext 6962b14f991SJulian Elischer * which completes the system startup. 697df8bae1dSRodney W. Grimes */ 698cb226aaaSPoul-Henning Kamp if ((error = execve(p, &args)) == 0) 699df8bae1dSRodney W. Grimes return; 700df8bae1dSRodney W. Grimes if (error != ENOENT) 701ba41a07dSDmitrij Tejblum printf("exec %.*s: error %d\n", (int)(next - path), 702ba41a07dSDmitrij Tejblum path, error); 703df8bae1dSRodney W. Grimes } 704df8bae1dSRodney W. Grimes printf("init: not found\n"); 705df8bae1dSRodney W. Grimes panic("no init"); 706df8bae1dSRodney W. Grimes } 707