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 421b0b259eSBruce Evans * $Id: init_main.c,v 1.109 1999/02/19 14:25:34 luoqi Exp $ 43df8bae1dSRodney W. Grimes */ 44df8bae1dSRodney W. Grimes 4519060a3aSPoul-Henning Kamp #include "opt_devfs.h" 464bd49128SPeter Wemm 47df8bae1dSRodney W. Grimes #include <sys/param.h> 48b3c5c18dSBruce Evans #include <sys/file.h> 49df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 50df8bae1dSRodney W. Grimes #include <sys/kernel.h> 51ac0ad63fSBruce Evans #include <sys/mount.h> 52946bb7a2SPoul-Henning Kamp #include <sys/sysctl.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 54df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 55df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 56df8bae1dSRodney W. Grimes #include <sys/systm.h> 57df8bae1dSRodney W. Grimes #include <sys/vnode.h> 58f3f0ca60SSøren Schmidt #include <sys/sysent.h> 59df8bae1dSRodney W. Grimes #include <sys/reboot.h> 60ad7507e2SSteven Wallace #include <sys/sysproto.h> 61efeaf95aSDavid Greenman #include <sys/vmmeter.h> 6274b2192aSJohn Dyson #include <sys/unistd.h> 6394e9d7c1SPeter Wemm #include <sys/malloc.h> 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes #include <machine/cpu.h> 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes #include <vm/vm.h> 68efeaf95aSDavid Greenman #include <vm/vm_param.h> 69efeaf95aSDavid Greenman #include <vm/vm_prot.h> 70996c772fSJohn Dyson #include <sys/lock.h> 71efeaf95aSDavid Greenman #include <vm/pmap.h> 72efeaf95aSDavid Greenman #include <vm/vm_map.h> 73efeaf95aSDavid Greenman #include <sys/user.h> 746faaa756SWolfram Schneider #include <sys/copyright.h> 75df8bae1dSRodney W. Grimes 769ef6c28aSBruce Evans extern struct linker_set sysinit_set; /* XXX */ 779ef6c28aSBruce Evans 789ef6c28aSBruce Evans extern void __main __P((void)); 799ef6c28aSBruce Evans extern void main __P((void *framep)); 80df8bae1dSRodney W. Grimes 81df8bae1dSRodney W. Grimes /* Components of the first process -- never freed. */ 82154c04e5SPoul-Henning Kamp static struct session session0; 83154c04e5SPoul-Henning Kamp static struct pgrp pgrp0; 84df8bae1dSRodney W. Grimes struct proc proc0; 85154c04e5SPoul-Henning Kamp static struct pcred cred0; 866626c604SJulian Elischer static struct procsig procsig0; 87154c04e5SPoul-Henning Kamp static struct filedesc0 filedesc0; 88154c04e5SPoul-Henning Kamp static struct plimit limit0; 89154c04e5SPoul-Henning Kamp static struct vmspace vmspace0; 902b14f991SJulian Elischer struct proc *initproc; 91df8bae1dSRodney W. Grimes 92dedb7b62SPeter Wemm int cmask = CMASK; 93df8bae1dSRodney W. Grimes extern struct user *proc0paddr; 94df8bae1dSRodney W. Grimes 952976b7f1SDavid Greenman struct vnode *rootvp; 96c463cf1cSBruce Evans int boothowto = 0; /* initialized so that it can be patched */ 97946bb7a2SPoul-Henning Kamp 98df8bae1dSRodney W. Grimes struct timeval boottime; 99946bb7a2SPoul-Henning Kamp SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, 100c463cf1cSBruce Evans CTLFLAG_RD, &boottime, timeval, ""); 101946bb7a2SPoul-Henning Kamp 1022b14f991SJulian Elischer /* 1032b14f991SJulian Elischer * Promiscuous argument pass for start_init() 1042b14f991SJulian Elischer * 1052b14f991SJulian Elischer * This is a kludge because we use a return from main() rather than a call 10637b688c4SAlexander Langer * to a new routine in locore.s to kick the kernel alive from locore.s. 1072b14f991SJulian Elischer */ 1082b14f991SJulian Elischer static void *init_framep; 1092b14f991SJulian Elischer 110df8bae1dSRodney W. Grimes 11126f9a767SRodney W. Grimes #if __GNUC__ >= 2 11226f9a767SRodney W. Grimes void __main() {} 11326f9a767SRodney W. Grimes #endif 11426f9a767SRodney W. Grimes 1152b14f991SJulian Elischer 11626f9a767SRodney W. Grimes /* 1172b14f991SJulian Elischer * This ensures that there is at least one entry so that the sysinit_set 1182b14f991SJulian Elischer * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 1192b14f991SJulian Elischer * executed. 12026f9a767SRodney W. Grimes */ 1212b14f991SJulian Elischer SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL) 1228a129caeSDavid Greenman 12394e9d7c1SPeter Wemm /* 12494e9d7c1SPeter Wemm * The sysinit table itself. Items are checked off as the are run. 12594e9d7c1SPeter Wemm * If we want to register new sysinit types, add them to newsysinit. 12694e9d7c1SPeter Wemm */ 12794e9d7c1SPeter Wemm struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items; 12894e9d7c1SPeter Wemm struct sysinit **newsysinit; 12994e9d7c1SPeter Wemm 13094e9d7c1SPeter Wemm /* 13194e9d7c1SPeter Wemm * Merge a new sysinit set into the current set, reallocating it if 13294e9d7c1SPeter Wemm * necessary. This can only be called after malloc is running. 13394e9d7c1SPeter Wemm */ 13494e9d7c1SPeter Wemm void 13594e9d7c1SPeter Wemm sysinit_add(set) 13694e9d7c1SPeter Wemm struct sysinit **set; 13794e9d7c1SPeter Wemm { 13894e9d7c1SPeter Wemm struct sysinit **newset; 13994e9d7c1SPeter Wemm struct sysinit **sipp; 14094e9d7c1SPeter Wemm struct sysinit **xipp; 14194e9d7c1SPeter Wemm int count = 0; 14294e9d7c1SPeter Wemm 143ddd62546SPeter Wemm if (newsysinit) 144ddd62546SPeter Wemm for (sipp = newsysinit; *sipp; sipp++) 145ddd62546SPeter Wemm count++; 146ddd62546SPeter Wemm else 14794e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) 14894e9d7c1SPeter Wemm count++; 14994e9d7c1SPeter Wemm for (sipp = set; *sipp; sipp++) 15094e9d7c1SPeter Wemm count++; 151ddd62546SPeter Wemm count++; /* Trailing NULL */ 15294e9d7c1SPeter Wemm newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); 15394e9d7c1SPeter Wemm if (newset == NULL) 15494e9d7c1SPeter Wemm panic("cannot malloc for sysinit"); 15594e9d7c1SPeter Wemm xipp = newset; 156ddd62546SPeter Wemm if (newsysinit) 157ddd62546SPeter Wemm for (sipp = newsysinit; *sipp; sipp++) 158ddd62546SPeter Wemm *xipp++ = *sipp; 159ddd62546SPeter Wemm else 16094e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) 16194e9d7c1SPeter Wemm *xipp++ = *sipp; 16294e9d7c1SPeter Wemm for (sipp = set; *sipp; sipp++) 16394e9d7c1SPeter Wemm *xipp++ = *sipp; 164ddd62546SPeter Wemm *xipp = NULL; 165ddd62546SPeter Wemm if (newsysinit) 166ddd62546SPeter Wemm free(newsysinit, M_TEMP); 16794e9d7c1SPeter Wemm newsysinit = newset; 16894e9d7c1SPeter Wemm } 16926f9a767SRodney W. Grimes 170df8bae1dSRodney W. Grimes /* 171df8bae1dSRodney W. Grimes * System startup; initialize the world, create process 0, mount root 172df8bae1dSRodney W. Grimes * filesystem, and fork to create init and pagedaemon. Most of the 173df8bae1dSRodney W. Grimes * hard work is done in the lower-level initialization routines including 174df8bae1dSRodney W. Grimes * startup(), which does memory initialization and autoconfiguration. 1752b14f991SJulian Elischer * 1762b14f991SJulian Elischer * This allows simple addition of new kernel subsystems that require 1772b14f991SJulian Elischer * boot time initialization. It also allows substitution of subsystem 1782b14f991SJulian Elischer * (for instance, a scheduler, kernel profiler, or VM system) by object 179c5b193bfSPoul-Henning Kamp * module. Finally, it allows for optional "kernel threads". 180df8bae1dSRodney W. Grimes */ 18126f9a767SRodney W. Grimes void 182df8bae1dSRodney W. Grimes main(framep) 183df8bae1dSRodney W. Grimes void *framep; 184df8bae1dSRodney W. Grimes { 1852b14f991SJulian Elischer 1862b14f991SJulian Elischer register struct sysinit **sipp; /* system initialization*/ 1872b14f991SJulian Elischer register struct sysinit **xipp; /* interior loop of sort*/ 1882b14f991SJulian Elischer register struct sysinit *save; /* bubble*/ 1892b14f991SJulian Elischer 1902b14f991SJulian Elischer /* 191a2a1c95cSPeter Wemm * Copy the locore.s frame pointer for proc0, this is forked into 192a2a1c95cSPeter Wemm * all other processes. 1932b14f991SJulian Elischer */ 1942b14f991SJulian Elischer init_framep = framep; 1952b14f991SJulian Elischer 19694e9d7c1SPeter Wemm restart: 1972b14f991SJulian Elischer /* 1982b14f991SJulian Elischer * Perform a bubble sort of the system initialization objects by 1992b14f991SJulian Elischer * their subsystem (primary key) and order (secondary key). 2002b14f991SJulian Elischer */ 20194e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) { 2022b14f991SJulian Elischer for (xipp = sipp + 1; *xipp; xipp++) { 2032b14f991SJulian Elischer if ((*sipp)->subsystem < (*xipp)->subsystem || 2042b14f991SJulian Elischer ((*sipp)->subsystem == (*xipp)->subsystem && 2052b14f991SJulian Elischer (*sipp)->order < (*xipp)->order)) 2062b14f991SJulian Elischer continue; /* skip*/ 2072b14f991SJulian Elischer save = *sipp; 2082b14f991SJulian Elischer *sipp = *xipp; 2092b14f991SJulian Elischer *xipp = save; 2102b14f991SJulian Elischer } 2112b14f991SJulian Elischer } 2122b14f991SJulian Elischer 2132b14f991SJulian Elischer /* 2142b14f991SJulian Elischer * Traverse the (now) ordered list of system initialization tasks. 2152b14f991SJulian Elischer * Perform each task, and continue on to the next task. 2162b14f991SJulian Elischer * 2172b14f991SJulian Elischer * The last item on the list is expected to be the scheduler, 2182b14f991SJulian Elischer * which will not return. 2192b14f991SJulian Elischer */ 22094e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) { 22174b2192aSJohn Dyson 2222b14f991SJulian Elischer if ((*sipp)->subsystem == SI_SUB_DUMMY) 2232b14f991SJulian Elischer continue; /* skip dummy task(s)*/ 2242b14f991SJulian Elischer 22594e9d7c1SPeter Wemm if ((*sipp)->subsystem == SI_SUB_DONE) 22694e9d7c1SPeter Wemm continue; 22794e9d7c1SPeter Wemm 2282b14f991SJulian Elischer switch( (*sipp)->type) { 2292b14f991SJulian Elischer case SI_TYPE_DEFAULT: 2302b14f991SJulian Elischer /* no special processing*/ 2312b14f991SJulian Elischer (*((*sipp)->func))((*sipp)->udata); 2322b14f991SJulian Elischer break; 2332b14f991SJulian Elischer 2342b14f991SJulian Elischer case SI_TYPE_KTHREAD: 23574b2192aSJohn Dyson #if !defined(SMP) 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 #endif 24374b2192aSJohn Dyson 24474b2192aSJohn Dyson case SI_TYPE_KPROCESS: 24574b2192aSJohn Dyson if (fork1(&proc0, RFFDG|RFPROC)) 2462b14f991SJulian Elischer panic("fork kernel process"); 247cb226aaaSPoul-Henning Kamp cpu_set_fork_handler(pfind(proc0.p_retval[0]), 248cb226aaaSPoul-Henning Kamp (*sipp)->func, (*sipp)->udata); 2492b14f991SJulian Elischer break; 2502b14f991SJulian Elischer 2512b14f991SJulian Elischer default: 2522b14f991SJulian Elischer panic("init_main: unrecognized init type"); 2532b14f991SJulian Elischer } 25494e9d7c1SPeter Wemm 25594e9d7c1SPeter Wemm /* Check off the one we're just done */ 25694e9d7c1SPeter Wemm (*sipp)->subsystem = SI_SUB_DONE; 25794e9d7c1SPeter Wemm 25894e9d7c1SPeter Wemm /* Check if we've installed more sysinit items via KLD */ 25994e9d7c1SPeter Wemm if (newsysinit != NULL) { 26094e9d7c1SPeter Wemm if (sysinit != (struct sysinit **)sysinit_set.ls_items) 26194e9d7c1SPeter Wemm free(sysinit, M_TEMP); 26294e9d7c1SPeter Wemm sysinit = newsysinit; 26394e9d7c1SPeter Wemm newsysinit = NULL; 26494e9d7c1SPeter Wemm goto restart; 26594e9d7c1SPeter Wemm } 2662b14f991SJulian Elischer } 2672b14f991SJulian Elischer 268477a642cSPeter Wemm panic("Shouldn't get here!"); 2692b14f991SJulian Elischer /* NOTREACHED*/ 2702b14f991SJulian Elischer } 2712b14f991SJulian Elischer 2722b14f991SJulian Elischer 2732b14f991SJulian Elischer /* 2742b14f991SJulian Elischer * Start a kernel process. This is called after a fork() call in 2752b14f991SJulian Elischer * main() in the file kern/init_main.c. 2762b14f991SJulian Elischer * 2772b14f991SJulian Elischer * This function is used to start "internal" daemons. 2782b14f991SJulian Elischer */ 2792b14f991SJulian Elischer /* ARGSUSED*/ 2802b14f991SJulian Elischer void 2812b14f991SJulian Elischer kproc_start(udata) 282bc814931SMatthew Dillon const void *udata; 2832b14f991SJulian Elischer { 284bc814931SMatthew Dillon const struct kproc_desc *kp = udata; 2852b14f991SJulian Elischer struct proc *p = curproc; 2862b14f991SJulian Elischer 287477a642cSPeter Wemm #ifdef DIAGNOSTIC 288477a642cSPeter Wemm printf("Start pid=%d <%s>\n",p->p_pid, kp->arg0); 289477a642cSPeter Wemm #endif 290477a642cSPeter Wemm 2912b14f991SJulian Elischer /* save a global descriptor, if desired*/ 2922b14f991SJulian Elischer if( kp->global_procpp != NULL) 2932b14f991SJulian Elischer *kp->global_procpp = p; 2942b14f991SJulian Elischer 2952b14f991SJulian Elischer /* this is a non-swapped system process*/ 2962b14f991SJulian Elischer p->p_flag |= P_INMEM | P_SYSTEM; 2972b14f991SJulian Elischer 2982b14f991SJulian Elischer /* set up arg0 for 'ps', et al*/ 2992b14f991SJulian Elischer strcpy( p->p_comm, kp->arg0); 3002b14f991SJulian Elischer 3012b14f991SJulian Elischer /* call the processes' main()...*/ 3022b14f991SJulian Elischer (*kp->func)(); 3032b14f991SJulian Elischer 3042b14f991SJulian Elischer /* NOTREACHED */ 3052b14f991SJulian Elischer panic("kproc_start: %s", kp->arg0); 3062b14f991SJulian Elischer } 3072b14f991SJulian Elischer 3082b14f991SJulian Elischer 3092b14f991SJulian Elischer /* 3102b14f991SJulian Elischer *************************************************************************** 3112b14f991SJulian Elischer **** 3122b14f991SJulian Elischer **** The following SYSINIT's belong elsewhere, but have not yet 3132b14f991SJulian Elischer **** been moved. 3142b14f991SJulian Elischer **** 3152b14f991SJulian Elischer *************************************************************************** 3162b14f991SJulian Elischer */ 3172b14f991SJulian Elischer #ifdef OMIT 3182b14f991SJulian Elischer /* 3192b14f991SJulian Elischer * Handled by vfs_mountroot (bad idea) at this time... should be 3202b14f991SJulian Elischer * done the same as 4.4Lite2. 3212b14f991SJulian Elischer */ 3222b14f991SJulian Elischer SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL) 3232b14f991SJulian Elischer #endif /* OMIT*/ 3242b14f991SJulian Elischer 3254590fd3aSDavid Greenman static void print_caddr_t __P((void *data)); 3269ef6c28aSBruce Evans static void 3279ef6c28aSBruce Evans print_caddr_t(data) 3284590fd3aSDavid Greenman void *data; 3299ef6c28aSBruce Evans { 3309ef6c28aSBruce Evans printf("%s", (char *)data); 3319ef6c28aSBruce Evans } 332d841aaa7SBruce Evans SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright) 3332b14f991SJulian Elischer 3342b14f991SJulian Elischer 3352b14f991SJulian Elischer /* 3362b14f991SJulian Elischer *************************************************************************** 3372b14f991SJulian Elischer **** 3382b14f991SJulian Elischer **** The two following SYSINT's are proc0 specific glue code. I am not 3392b14f991SJulian Elischer **** convinced that they can not be safely combined, but their order of 3402b14f991SJulian Elischer **** operation has been maintained as the same as the original init_main.c 3412b14f991SJulian Elischer **** for right now. 3422b14f991SJulian Elischer **** 3432b14f991SJulian Elischer **** These probably belong in init_proc.c or kern_proc.c, since they 3442b14f991SJulian Elischer **** deal with proc0 (the fork template process). 3452b14f991SJulian Elischer **** 3462b14f991SJulian Elischer *************************************************************************** 3472b14f991SJulian Elischer */ 3482b14f991SJulian Elischer /* ARGSUSED*/ 349154c04e5SPoul-Henning Kamp static void proc0_init __P((void *dummy)); 350154c04e5SPoul-Henning Kamp static void 351d841aaa7SBruce Evans proc0_init(dummy) 352d841aaa7SBruce Evans void *dummy; 3532b14f991SJulian Elischer { 354df8bae1dSRodney W. Grimes register struct proc *p; 355df8bae1dSRodney W. Grimes register struct filedesc0 *fdp; 35692579404SAlexander Langer register unsigned i; 357df8bae1dSRodney W. Grimes 358df8bae1dSRodney W. Grimes /* 359df8bae1dSRodney W. Grimes * Initialize the current process pointer (curproc) before 360df8bae1dSRodney W. Grimes * any possible traps/probes to simplify trap processing. 361df8bae1dSRodney W. Grimes */ 362df8bae1dSRodney W. Grimes p = &proc0; 3632b14f991SJulian Elischer curproc = p; /* XXX redundant*/ 364df8bae1dSRodney W. Grimes 365df8bae1dSRodney W. Grimes /* 366a3bfb996SJeffrey Hsu * Initialize process and pgrp structures. 367a3bfb996SJeffrey Hsu */ 368a3bfb996SJeffrey Hsu procinit(); 369a3bfb996SJeffrey Hsu 370a3bfb996SJeffrey Hsu /* 371b1508c72SDavid Greenman * Initialize sleep queue hash table 372b1508c72SDavid Greenman */ 373b1508c72SDavid Greenman sleepinit(); 374b1508c72SDavid Greenman 375b1508c72SDavid Greenman /* 3763075778bSJohn Dyson * additional VM structures 3773075778bSJohn Dyson */ 3783075778bSJohn Dyson vm_init2(); 3793075778bSJohn Dyson 3803075778bSJohn Dyson /* 381df8bae1dSRodney W. Grimes * Create process 0 (the swapper). 382df8bae1dSRodney W. Grimes */ 383a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(&allproc, p, p_list); 384df8bae1dSRodney W. Grimes p->p_pgrp = &pgrp0; 385a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 386a3bfb996SJeffrey Hsu LIST_INIT(&pgrp0.pg_members); 387a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 388a3bfb996SJeffrey Hsu 389df8bae1dSRodney W. Grimes pgrp0.pg_session = &session0; 390df8bae1dSRodney W. Grimes session0.s_count = 1; 391df8bae1dSRodney W. Grimes session0.s_leader = p; 392df8bae1dSRodney W. Grimes 393f3f0ca60SSøren Schmidt p->p_sysent = &aout_sysvec; 394f3f0ca60SSøren Schmidt 395df8bae1dSRodney W. Grimes p->p_flag = P_INMEM | P_SYSTEM; 396df8bae1dSRodney W. Grimes p->p_stat = SRUN; 397df8bae1dSRodney W. Grimes p->p_nice = NZERO; 3987216391eSDavid Greenman p->p_rtprio.type = RTP_PRIO_NORMAL; 3997216391eSDavid Greenman p->p_rtprio.prio = 0; 400f992f480SDavid Greenman 4012c1011f7SJohn Dyson /* 4022c1011f7SJohn Dyson * Link for kernel based threads 4032c1011f7SJohn Dyson */ 4042c1011f7SJohn Dyson p->p_peers = 0; 4052c1011f7SJohn Dyson p->p_leader = p; 4062c1011f7SJohn Dyson 407df8bae1dSRodney W. Grimes bcopy("swapper", p->p_comm, sizeof ("swapper")); 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes /* Create credentials. */ 410df8bae1dSRodney W. Grimes cred0.p_refcnt = 1; 411df8bae1dSRodney W. Grimes p->p_cred = &cred0; 412df8bae1dSRodney W. Grimes p->p_ucred = crget(); 413df8bae1dSRodney W. Grimes p->p_ucred->cr_ngroups = 1; /* group 0 */ 414df8bae1dSRodney W. Grimes 4156626c604SJulian Elischer /* Create procsig. */ 4166626c604SJulian Elischer p->p_procsig = &procsig0; 41775ffaf59SLuoqi Chen p->p_procsig->ps_refcnt = 1; 4186626c604SJulian Elischer 419df8bae1dSRodney W. Grimes /* Create the file descriptor table. */ 420df8bae1dSRodney W. Grimes fdp = &filedesc0; 421df8bae1dSRodney W. Grimes p->p_fd = &fdp->fd_fd; 422df8bae1dSRodney W. Grimes fdp->fd_fd.fd_refcnt = 1; 423df8bae1dSRodney W. Grimes fdp->fd_fd.fd_cmask = cmask; 424df8bae1dSRodney W. Grimes fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 425df8bae1dSRodney W. Grimes fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 426df8bae1dSRodney W. Grimes fdp->fd_fd.fd_nfiles = NDFILE; 427df8bae1dSRodney W. Grimes 428df8bae1dSRodney W. Grimes /* Create the limits structures. */ 429df8bae1dSRodney W. Grimes p->p_limit = &limit0; 430df8bae1dSRodney W. Grimes for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 431df8bae1dSRodney W. Grimes limit0.pl_rlimit[i].rlim_cur = 432df8bae1dSRodney W. Grimes limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 433b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 434b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 435b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 436b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 437df8bae1dSRodney W. Grimes i = ptoa(cnt.v_free_count); 438df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 439df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 440df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 441e796e00dSPoul-Henning Kamp limit0.p_cpulimit = RLIM_INFINITY; 442df8bae1dSRodney W. Grimes limit0.p_refcnt = 1; 443df8bae1dSRodney W. Grimes 444e796e00dSPoul-Henning Kamp 445df8bae1dSRodney W. Grimes /* Allocate a prototype map so we have something to fork. */ 446b1028ad1SLuoqi Chen pmap_pinit0(vmspace_pmap(&vmspace0)); 447df8bae1dSRodney W. Grimes p->p_vmspace = &vmspace0; 448df8bae1dSRodney W. Grimes vmspace0.vm_refcnt = 1; 44926f9a767SRodney W. Grimes vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS), 4502d8acc0fSJohn Dyson trunc_page(VM_MAXUSER_ADDRESS)); 451b1028ad1SLuoqi Chen vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0); 452df8bae1dSRodney W. Grimes p->p_addr = proc0paddr; /* XXX */ 453df8bae1dSRodney W. Grimes 454ecbb00a2SDoug Rabson #ifndef __alpha__ /* XXX what is this? */ 4552b14f991SJulian Elischer #define INCOMPAT_LITES2 4562b14f991SJulian Elischer #ifdef INCOMPAT_LITES2 457df8bae1dSRodney W. Grimes /* 458dc733423SDag-Erling Smørgrav * proc0 needs to have a coherent frame base in its stack. 4592384fde5SDavid Greenman */ 4602b14f991SJulian Elischer cpu_set_init_frame(p, init_framep); /* XXX! */ 4612b14f991SJulian Elischer #endif /* INCOMPAT_LITES2*/ 462ecbb00a2SDoug Rabson #endif 4632384fde5SDavid Greenman 46439fb8e6bSJulian Elischer /* 465df8bae1dSRodney W. Grimes * We continue to place resource usage info and signal 466df8bae1dSRodney W. Grimes * actions in the user struct so they're pageable. 467df8bae1dSRodney W. Grimes */ 468df8bae1dSRodney W. Grimes p->p_stats = &p->p_addr->u_stats; 469df8bae1dSRodney W. Grimes p->p_sigacts = &p->p_addr->u_sigacts; 470df8bae1dSRodney W. Grimes 471df8bae1dSRodney W. Grimes /* 472a3bfb996SJeffrey Hsu * Charge root for one process. 473df8bae1dSRodney W. Grimes */ 474df8bae1dSRodney W. Grimes (void)chgproccnt(0, 1); 4752a024a2bSSean Eric Fagan 4762a024a2bSSean Eric Fagan /* 4772a024a2bSSean Eric Fagan * Initialize the procfs flags (to 0, of course) 4782a024a2bSSean Eric Fagan */ 4792a024a2bSSean Eric Fagan p->p_stops = p->p_stype = p->p_step = 0; 4802a024a2bSSean Eric Fagan 48126f9a767SRodney W. Grimes } 4822b14f991SJulian Elischer SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL) 4832b14f991SJulian Elischer 4842b14f991SJulian Elischer /* ARGSUSED*/ 485154c04e5SPoul-Henning Kamp static void proc0_post __P((void *dummy)); 486154c04e5SPoul-Henning Kamp static void 487d841aaa7SBruce Evans proc0_post(dummy) 488d841aaa7SBruce Evans void *dummy; 4892b14f991SJulian Elischer { 49000af9731SPoul-Henning Kamp struct timespec ts; 491a6fc8288SPeter Wemm 4922b14f991SJulian Elischer /* 4931b0b259eSBruce Evans * Now we can look at the time, having had a chance to verify the 4941b0b259eSBruce Evans * time from the file system. Pretend that proc0 started now. 4952b14f991SJulian Elischer */ 4965f88ec36SPoul-Henning Kamp microtime(&proc0.p_stats->p_start); 497e796e00dSPoul-Henning Kamp proc0.p_runtime = 0; 498e796e00dSPoul-Henning Kamp microuptime(&proc0.p_switchtime); 4991b0b259eSBruce Evans switchticks = ticks; 5002b14f991SJulian Elischer 501a6fc8288SPeter Wemm /* 502a6fc8288SPeter Wemm * Give the ``random'' number generator a thump. 5035f88ec36SPoul-Henning Kamp * XXX: Does read_random() contain enough bits to be used here ? 504a6fc8288SPeter Wemm */ 50500af9731SPoul-Henning Kamp nanotime(&ts); 50600af9731SPoul-Henning Kamp srandom(ts.tv_sec ^ ts.tv_nsec); 507a6fc8288SPeter Wemm 5082b14f991SJulian Elischer /* Initialize signal state for process 0. */ 5092b14f991SJulian Elischer siginit(&proc0); 5102b14f991SJulian Elischer } 5112b14f991SJulian Elischer SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL) 5122b14f991SJulian Elischer 5132b14f991SJulian Elischer 5142b14f991SJulian Elischer 515df8bae1dSRodney W. Grimes 516df8bae1dSRodney W. Grimes /* 5172b14f991SJulian Elischer *************************************************************************** 5182b14f991SJulian Elischer **** 5192b14f991SJulian Elischer **** The following SYSINIT's and glue code should be moved to the 5202b14f991SJulian Elischer **** respective files on a per subsystem basis. 5212b14f991SJulian Elischer **** 5222b14f991SJulian Elischer *************************************************************************** 523df8bae1dSRodney W. Grimes */ 524df8bae1dSRodney W. Grimes 5252b14f991SJulian Elischer /* ARGSUSED */ 526ab36c067SJustin T. Gibbs static void root_conf __P((void *dummy)); 527ab36c067SJustin T. Gibbs static void 528ab36c067SJustin T. Gibbs root_conf(dummy) 529ab36c067SJustin T. Gibbs void *dummy; 530ab36c067SJustin T. Gibbs { 531ab36c067SJustin T. Gibbs cpu_rootconf(); 532ab36c067SJustin T. Gibbs } 533ab36c067SJustin T. Gibbs SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL) 534ab36c067SJustin T. Gibbs 535ab36c067SJustin T. Gibbs /* ARGSUSED*/ 536154c04e5SPoul-Henning Kamp static void xxx_vfs_root_fdtab __P((void *dummy)); 537154c04e5SPoul-Henning Kamp static void 538d841aaa7SBruce Evans xxx_vfs_root_fdtab(dummy) 539d841aaa7SBruce Evans void *dummy; 5402b14f991SJulian Elischer { 5412b14f991SJulian Elischer register struct filedesc0 *fdp = &filedesc0; 542df8bae1dSRodney W. Grimes 543df8bae1dSRodney W. Grimes /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ 544628641f8SDavid Greenman if (VFS_ROOT(mountlist.cqh_first, &rootvnode)) 545df8bae1dSRodney W. Grimes panic("cannot find root vnode"); 546df8bae1dSRodney W. Grimes fdp->fd_fd.fd_cdir = rootvnode; 547df8bae1dSRodney W. Grimes VREF(fdp->fd_fd.fd_cdir); 548996c772fSJohn Dyson VOP_UNLOCK(rootvnode, 0, &proc0); 5499f24f214SJohn Dyson fdp->fd_fd.fd_rdir = rootvnode; 5502b14f991SJulian Elischer } 5512b14f991SJulian Elischer SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL) 5522b14f991SJulian Elischer 553df8bae1dSRodney W. Grimes 554df8bae1dSRodney W. Grimes /* 5552b14f991SJulian Elischer *************************************************************************** 5562b14f991SJulian Elischer **** 5572b14f991SJulian Elischer **** The following code probably belongs in another file, like 5582b14f991SJulian Elischer **** kern/init_init.c. It is here for two reasons only: 5592b14f991SJulian Elischer **** 5602b14f991SJulian Elischer **** 1) This code returns to startup the system; this is 5612b14f991SJulian Elischer **** abnormal for a kernel thread. 5622b14f991SJulian Elischer **** 2) This code promiscuously uses init_frame 5632b14f991SJulian Elischer **** 5642b14f991SJulian Elischer *************************************************************************** 565df8bae1dSRodney W. Grimes */ 566df8bae1dSRodney W. Grimes 5673cfc69e6SMatthew Dillon static void kthread_init __P((const void *dummy)); 56874b2192aSJohn Dyson SYSINIT_KP(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL) 5692b14f991SJulian Elischer 5702b14f991SJulian Elischer 571a2a1c95cSPeter Wemm extern void prepare_usermode __P((void)); 572a2a1c95cSPeter Wemm static void start_init __P((struct proc *p)); 5732b14f991SJulian Elischer 5742b14f991SJulian Elischer /* ARGSUSED*/ 5752b14f991SJulian Elischer static void 576d841aaa7SBruce Evans kthread_init(dummy) 5773cfc69e6SMatthew Dillon const void *dummy; 5782b14f991SJulian Elischer { 579df8bae1dSRodney W. Grimes /* Create process 1 (init(8)). */ 580a2a1c95cSPeter Wemm start_init(curproc); 581a2a1c95cSPeter Wemm 582a2a1c95cSPeter Wemm prepare_usermode(); 5832b14f991SJulian Elischer 5842b14f991SJulian Elischer /* 585a2a1c95cSPeter Wemm * This returns to the fork trampoline, then to user mode. 5862b14f991SJulian Elischer */ 587df8bae1dSRodney W. Grimes return; 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes /* 592df8bae1dSRodney W. Grimes * List of paths to try when searching for "init". 593df8bae1dSRodney W. Grimes */ 594df8bae1dSRodney W. Grimes static char *initpaths[] = { 595df8bae1dSRodney W. Grimes "/sbin/init", 596df8bae1dSRodney W. Grimes "/sbin/oinit", 597df8bae1dSRodney W. Grimes "/sbin/init.bak", 5982257b745SPoul-Henning Kamp "/stand/sysinstall", 599df8bae1dSRodney W. Grimes NULL, 600df8bae1dSRodney W. Grimes }; 601df8bae1dSRodney W. Grimes 602df8bae1dSRodney W. Grimes /* 603df8bae1dSRodney W. Grimes * Start the initial user process; try exec'ing each pathname in "initpaths". 604df8bae1dSRodney W. Grimes * The program is invoked with one argument containing the boot flags. 605df8bae1dSRodney W. Grimes */ 606df8bae1dSRodney W. Grimes static void 607a2a1c95cSPeter Wemm start_init(p) 608df8bae1dSRodney W. Grimes struct proc *p; 609df8bae1dSRodney W. Grimes { 610df8bae1dSRodney W. Grimes vm_offset_t addr; 611df8bae1dSRodney W. Grimes struct execve_args args; 6124a11ca4eSPoul-Henning Kamp int options, i, error; 613df8bae1dSRodney W. Grimes char **pathp, *path, *ucp, **uap, *arg0, *arg1; 614df8bae1dSRodney W. Grimes 615df8bae1dSRodney W. Grimes initproc = p; 616df8bae1dSRodney W. Grimes 617df8bae1dSRodney W. Grimes /* 618df8bae1dSRodney W. Grimes * Need just enough stack to hold the faked-up "execve()" arguments. 619df8bae1dSRodney W. Grimes */ 6209c0fed3dSDoug Rabson addr = trunc_page(USRSTACK - PAGE_SIZE); 621bd7e5f99SJohn Dyson if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, 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 626df8bae1dSRodney W. Grimes for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 627df8bae1dSRodney W. Grimes /* 628df8bae1dSRodney W. Grimes * Move out the boot flag argument. 629df8bae1dSRodney W. Grimes */ 630df8bae1dSRodney W. Grimes options = 0; 631df8bae1dSRodney W. Grimes ucp = (char *)USRSTACK; 632df8bae1dSRodney W. Grimes (void)subyte(--ucp, 0); /* trailing zero */ 633df8bae1dSRodney W. Grimes if (boothowto & RB_SINGLE) { 634df8bae1dSRodney W. Grimes (void)subyte(--ucp, 's'); 635df8bae1dSRodney W. Grimes options = 1; 636df8bae1dSRodney W. Grimes } 637df8bae1dSRodney W. Grimes #ifdef notyet 638df8bae1dSRodney W. Grimes if (boothowto & RB_FASTBOOT) { 639df8bae1dSRodney W. Grimes (void)subyte(--ucp, 'f'); 640df8bae1dSRodney W. Grimes options = 1; 641df8bae1dSRodney W. Grimes } 642df8bae1dSRodney W. Grimes #endif 64317755ac8SPoul-Henning Kamp 64417755ac8SPoul-Henning Kamp #ifdef BOOTCDROM 64517755ac8SPoul-Henning Kamp (void)subyte(--ucp, 'C'); 64617755ac8SPoul-Henning Kamp options = 1; 64717755ac8SPoul-Henning Kamp #endif 648df8bae1dSRodney W. Grimes if (options == 0) 649df8bae1dSRodney W. Grimes (void)subyte(--ucp, '-'); 650df8bae1dSRodney W. Grimes (void)subyte(--ucp, '-'); /* leading hyphen */ 651df8bae1dSRodney W. Grimes arg1 = ucp; 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes /* 654df8bae1dSRodney W. Grimes * Move out the file name (also arg 0). 655df8bae1dSRodney W. Grimes */ 656df8bae1dSRodney W. Grimes for (i = strlen(path) + 1; i >= 0; i--) 657df8bae1dSRodney W. Grimes (void)subyte(--ucp, path[i]); 658df8bae1dSRodney W. Grimes arg0 = ucp; 659df8bae1dSRodney W. Grimes 660df8bae1dSRodney W. Grimes /* 661df8bae1dSRodney W. Grimes * Move out the arg pointers. 662df8bae1dSRodney W. Grimes */ 663a20d7755SDoug Rabson uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 664c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)0); /* terminator */ 665c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 666c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 667df8bae1dSRodney W. Grimes 668df8bae1dSRodney W. Grimes /* 669df8bae1dSRodney W. Grimes * Point at the arguments. 670df8bae1dSRodney W. Grimes */ 671df8bae1dSRodney W. Grimes args.fname = arg0; 67226f9a767SRodney W. Grimes args.argv = uap; 67326f9a767SRodney W. Grimes args.envv = NULL; 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes /* 676df8bae1dSRodney W. Grimes * Now try to exec the program. If can't for any reason 677df8bae1dSRodney W. Grimes * other than it doesn't exist, complain. 6782b14f991SJulian Elischer * 6792b14f991SJulian Elischer * Otherwise return to main() which returns to btext 6802b14f991SJulian Elischer * which completes the system startup. 681df8bae1dSRodney W. Grimes */ 682cb226aaaSPoul-Henning Kamp if ((error = execve(p, &args)) == 0) 683df8bae1dSRodney W. Grimes return; 684df8bae1dSRodney W. Grimes if (error != ENOENT) 685df8bae1dSRodney W. Grimes printf("exec %s: error %d\n", path, error); 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes printf("init: not found\n"); 688df8bae1dSRodney W. Grimes panic("no init"); 689df8bae1dSRodney W. Grimes } 690