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 4294e9d7c1SPeter Wemm * $Id: init_main.c,v 1.97 1998/10/06 11:55:40 dfr 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; 86154c04e5SPoul-Henning Kamp static struct filedesc0 filedesc0; 87154c04e5SPoul-Henning Kamp static struct plimit limit0; 88154c04e5SPoul-Henning Kamp static struct vmspace vmspace0; 892b14f991SJulian Elischer struct proc *initproc; 90df8bae1dSRodney W. Grimes 91dedb7b62SPeter Wemm int cmask = CMASK; 92df8bae1dSRodney W. Grimes extern struct user *proc0paddr; 93df8bae1dSRodney W. Grimes 942976b7f1SDavid Greenman struct vnode *rootvp; 95c463cf1cSBruce Evans int boothowto = 0; /* initialized so that it can be patched */ 96946bb7a2SPoul-Henning Kamp 97df8bae1dSRodney W. Grimes struct timeval boottime; 98946bb7a2SPoul-Henning Kamp SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, 99c463cf1cSBruce Evans CTLFLAG_RD, &boottime, timeval, ""); 100946bb7a2SPoul-Henning Kamp 101f4e39ee7SDavid Nugent static int shutdowntimeout = 120; 102f4e39ee7SDavid Nugent SYSCTL_INT(_kern, OID_AUTO, shutdown_timeout, 103f4e39ee7SDavid Nugent CTLFLAG_RW, &shutdowntimeout, 0, ""); 104f4e39ee7SDavid Nugent 1052b14f991SJulian Elischer /* 1062b14f991SJulian Elischer * Promiscuous argument pass for start_init() 1072b14f991SJulian Elischer * 1082b14f991SJulian Elischer * This is a kludge because we use a return from main() rather than a call 10937b688c4SAlexander Langer * to a new routine in locore.s to kick the kernel alive from locore.s. 1102b14f991SJulian Elischer */ 1112b14f991SJulian Elischer static void *init_framep; 1122b14f991SJulian Elischer 113df8bae1dSRodney W. Grimes 11426f9a767SRodney W. Grimes #if __GNUC__ >= 2 11526f9a767SRodney W. Grimes void __main() {} 11626f9a767SRodney W. Grimes #endif 11726f9a767SRodney W. Grimes 1182b14f991SJulian Elischer 11926f9a767SRodney W. Grimes /* 1202b14f991SJulian Elischer * This ensures that there is at least one entry so that the sysinit_set 1212b14f991SJulian Elischer * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 1222b14f991SJulian Elischer * executed. 12326f9a767SRodney W. Grimes */ 1242b14f991SJulian Elischer SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL) 1258a129caeSDavid Greenman 12694e9d7c1SPeter Wemm /* 12794e9d7c1SPeter Wemm * The sysinit table itself. Items are checked off as the are run. 12894e9d7c1SPeter Wemm * If we want to register new sysinit types, add them to newsysinit. 12994e9d7c1SPeter Wemm */ 13094e9d7c1SPeter Wemm struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items; 13194e9d7c1SPeter Wemm struct sysinit **newsysinit; 13294e9d7c1SPeter Wemm 13394e9d7c1SPeter Wemm /* 13494e9d7c1SPeter Wemm * Merge a new sysinit set into the current set, reallocating it if 13594e9d7c1SPeter Wemm * necessary. This can only be called after malloc is running. 13694e9d7c1SPeter Wemm */ 13794e9d7c1SPeter Wemm void 13894e9d7c1SPeter Wemm sysinit_add(set) 13994e9d7c1SPeter Wemm struct sysinit **set; 14094e9d7c1SPeter Wemm { 14194e9d7c1SPeter Wemm struct sysinit **newset; 14294e9d7c1SPeter Wemm struct sysinit **sipp; 14394e9d7c1SPeter Wemm struct sysinit **xipp; 14494e9d7c1SPeter Wemm int count = 0; 14594e9d7c1SPeter Wemm 14694e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) 14794e9d7c1SPeter Wemm count++; 14894e9d7c1SPeter Wemm for (sipp = set; *sipp; sipp++) 14994e9d7c1SPeter Wemm count++; 15094e9d7c1SPeter Wemm if (newsysinit) 15194e9d7c1SPeter Wemm for (sipp = set; *sipp; sipp++) 15294e9d7c1SPeter Wemm count++; 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; 15794e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) 15894e9d7c1SPeter Wemm *xipp++ = *sipp; 15994e9d7c1SPeter Wemm for (sipp = set; *sipp; sipp++) 16094e9d7c1SPeter Wemm *xipp++ = *sipp; 16194e9d7c1SPeter Wemm if (newsysinit) { 16294e9d7c1SPeter Wemm for (sipp = newsysinit; *sipp; sipp++) 16394e9d7c1SPeter Wemm *xipp++ = *sipp; 16494e9d7c1SPeter Wemm free(newset, M_TEMP); 16594e9d7c1SPeter Wemm } 16694e9d7c1SPeter Wemm newsysinit = newset; 16794e9d7c1SPeter Wemm } 16826f9a767SRodney W. Grimes 169df8bae1dSRodney W. Grimes /* 170df8bae1dSRodney W. Grimes * System startup; initialize the world, create process 0, mount root 171df8bae1dSRodney W. Grimes * filesystem, and fork to create init and pagedaemon. Most of the 172df8bae1dSRodney W. Grimes * hard work is done in the lower-level initialization routines including 173df8bae1dSRodney W. Grimes * startup(), which does memory initialization and autoconfiguration. 1742b14f991SJulian Elischer * 1752b14f991SJulian Elischer * This allows simple addition of new kernel subsystems that require 1762b14f991SJulian Elischer * boot time initialization. It also allows substitution of subsystem 1772b14f991SJulian Elischer * (for instance, a scheduler, kernel profiler, or VM system) by object 178c5b193bfSPoul-Henning Kamp * module. Finally, it allows for optional "kernel threads". 179df8bae1dSRodney W. Grimes */ 18026f9a767SRodney W. Grimes void 181df8bae1dSRodney W. Grimes main(framep) 182df8bae1dSRodney W. Grimes void *framep; 183df8bae1dSRodney W. Grimes { 1842b14f991SJulian Elischer 1852b14f991SJulian Elischer register struct sysinit **sipp; /* system initialization*/ 1862b14f991SJulian Elischer register struct sysinit **xipp; /* interior loop of sort*/ 1872b14f991SJulian Elischer register struct sysinit *save; /* bubble*/ 1882b14f991SJulian Elischer 1892b14f991SJulian Elischer /* 190a2a1c95cSPeter Wemm * Copy the locore.s frame pointer for proc0, this is forked into 191a2a1c95cSPeter Wemm * all other processes. 1922b14f991SJulian Elischer */ 1932b14f991SJulian Elischer init_framep = framep; 1942b14f991SJulian Elischer 19594e9d7c1SPeter Wemm restart: 1962b14f991SJulian Elischer /* 1972b14f991SJulian Elischer * Perform a bubble sort of the system initialization objects by 1982b14f991SJulian Elischer * their subsystem (primary key) and order (secondary key). 1992b14f991SJulian Elischer */ 20094e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) { 2012b14f991SJulian Elischer for (xipp = sipp + 1; *xipp; xipp++) { 2022b14f991SJulian Elischer if ((*sipp)->subsystem < (*xipp)->subsystem || 2032b14f991SJulian Elischer ((*sipp)->subsystem == (*xipp)->subsystem && 2042b14f991SJulian Elischer (*sipp)->order < (*xipp)->order)) 2052b14f991SJulian Elischer continue; /* skip*/ 2062b14f991SJulian Elischer save = *sipp; 2072b14f991SJulian Elischer *sipp = *xipp; 2082b14f991SJulian Elischer *xipp = save; 2092b14f991SJulian Elischer } 2102b14f991SJulian Elischer } 2112b14f991SJulian Elischer 2122b14f991SJulian Elischer /* 2132b14f991SJulian Elischer * Traverse the (now) ordered list of system initialization tasks. 2142b14f991SJulian Elischer * Perform each task, and continue on to the next task. 2152b14f991SJulian Elischer * 2162b14f991SJulian Elischer * The last item on the list is expected to be the scheduler, 2172b14f991SJulian Elischer * which will not return. 2182b14f991SJulian Elischer */ 21994e9d7c1SPeter Wemm for (sipp = sysinit; *sipp; sipp++) { 22074b2192aSJohn Dyson 2212b14f991SJulian Elischer if ((*sipp)->subsystem == SI_SUB_DUMMY) 2222b14f991SJulian Elischer continue; /* skip dummy task(s)*/ 2232b14f991SJulian Elischer 22494e9d7c1SPeter Wemm if ((*sipp)->subsystem == SI_SUB_DONE) 22594e9d7c1SPeter Wemm continue; 22694e9d7c1SPeter Wemm 2272b14f991SJulian Elischer switch( (*sipp)->type) { 2282b14f991SJulian Elischer case SI_TYPE_DEFAULT: 2292b14f991SJulian Elischer /* no special processing*/ 2302b14f991SJulian Elischer (*((*sipp)->func))((*sipp)->udata); 2312b14f991SJulian Elischer break; 2322b14f991SJulian Elischer 2332b14f991SJulian Elischer case SI_TYPE_KTHREAD: 23474b2192aSJohn Dyson #if !defined(SMP) 2352b14f991SJulian Elischer /* kernel thread*/ 23674b2192aSJohn Dyson if (fork1(&proc0, RFMEM|RFFDG|RFPROC)) 23774b2192aSJohn Dyson panic("fork kernel thread"); 23874b2192aSJohn Dyson cpu_set_fork_handler(pfind(proc0.p_retval[0]), 23974b2192aSJohn Dyson (*sipp)->func, (*sipp)->udata); 24074b2192aSJohn Dyson break; 24174b2192aSJohn Dyson #endif 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) 281d841aaa7SBruce Evans void *udata; 2822b14f991SJulian Elischer { 283d841aaa7SBruce Evans 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 /* 358df8bae1dSRodney W. Grimes * Initialize the current process pointer (curproc) before 359df8bae1dSRodney W. Grimes * any possible traps/probes to simplify trap processing. 360df8bae1dSRodney W. Grimes */ 361df8bae1dSRodney W. Grimes p = &proc0; 3622b14f991SJulian Elischer curproc = p; /* XXX redundant*/ 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes /* 365a3bfb996SJeffrey Hsu * Initialize process and pgrp structures. 366a3bfb996SJeffrey Hsu */ 367a3bfb996SJeffrey Hsu procinit(); 368a3bfb996SJeffrey Hsu 369a3bfb996SJeffrey Hsu /* 370b1508c72SDavid Greenman * Initialize sleep queue hash table 371b1508c72SDavid Greenman */ 372b1508c72SDavid Greenman sleepinit(); 373b1508c72SDavid Greenman 374b1508c72SDavid Greenman /* 3753075778bSJohn Dyson * additional VM structures 3763075778bSJohn Dyson */ 3773075778bSJohn Dyson vm_init2(); 3783075778bSJohn Dyson 3793075778bSJohn Dyson /* 380df8bae1dSRodney W. Grimes * Create process 0 (the swapper). 381df8bae1dSRodney W. Grimes */ 382a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(&allproc, p, p_list); 383df8bae1dSRodney W. Grimes p->p_pgrp = &pgrp0; 384a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 385a3bfb996SJeffrey Hsu LIST_INIT(&pgrp0.pg_members); 386a3bfb996SJeffrey Hsu LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 387a3bfb996SJeffrey Hsu 388df8bae1dSRodney W. Grimes pgrp0.pg_session = &session0; 389df8bae1dSRodney W. Grimes session0.s_count = 1; 390df8bae1dSRodney W. Grimes session0.s_leader = p; 391df8bae1dSRodney W. Grimes 392f3f0ca60SSøren Schmidt p->p_sysent = &aout_sysvec; 393f3f0ca60SSøren Schmidt 394df8bae1dSRodney W. Grimes p->p_flag = P_INMEM | P_SYSTEM; 395df8bae1dSRodney W. Grimes p->p_stat = SRUN; 396df8bae1dSRodney W. Grimes p->p_nice = NZERO; 3977216391eSDavid Greenman p->p_rtprio.type = RTP_PRIO_NORMAL; 3987216391eSDavid Greenman p->p_rtprio.prio = 0; 399f992f480SDavid Greenman 4002c1011f7SJohn Dyson /* 4012c1011f7SJohn Dyson * Link for kernel based threads 4022c1011f7SJohn Dyson */ 4032c1011f7SJohn Dyson p->p_peers = 0; 4042c1011f7SJohn Dyson p->p_leader = p; 4052c1011f7SJohn Dyson 406df8bae1dSRodney W. Grimes bcopy("swapper", p->p_comm, sizeof ("swapper")); 407df8bae1dSRodney W. Grimes 408df8bae1dSRodney W. Grimes /* Create credentials. */ 409df8bae1dSRodney W. Grimes cred0.p_refcnt = 1; 410df8bae1dSRodney W. Grimes p->p_cred = &cred0; 411df8bae1dSRodney W. Grimes p->p_ucred = crget(); 412df8bae1dSRodney W. Grimes p->p_ucred->cr_ngroups = 1; /* group 0 */ 413df8bae1dSRodney W. Grimes 414df8bae1dSRodney W. Grimes /* Create the file descriptor table. */ 415df8bae1dSRodney W. Grimes fdp = &filedesc0; 416df8bae1dSRodney W. Grimes p->p_fd = &fdp->fd_fd; 417df8bae1dSRodney W. Grimes fdp->fd_fd.fd_refcnt = 1; 418df8bae1dSRodney W. Grimes fdp->fd_fd.fd_cmask = cmask; 419df8bae1dSRodney W. Grimes fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 420df8bae1dSRodney W. Grimes fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 421df8bae1dSRodney W. Grimes fdp->fd_fd.fd_nfiles = NDFILE; 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes /* Create the limits structures. */ 424df8bae1dSRodney W. Grimes p->p_limit = &limit0; 425df8bae1dSRodney W. Grimes for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 426df8bae1dSRodney W. Grimes limit0.pl_rlimit[i].rlim_cur = 427df8bae1dSRodney W. Grimes limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 428b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 429b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 430b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 431b3c5c18dSBruce Evans limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 432df8bae1dSRodney W. Grimes i = ptoa(cnt.v_free_count); 433df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 434df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 435df8bae1dSRodney W. Grimes limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 436e796e00dSPoul-Henning Kamp limit0.p_cpulimit = RLIM_INFINITY; 437df8bae1dSRodney W. Grimes limit0.p_refcnt = 1; 438df8bae1dSRodney W. Grimes 439e796e00dSPoul-Henning Kamp 440df8bae1dSRodney W. Grimes /* Allocate a prototype map so we have something to fork. */ 441a2481bbeSPoul-Henning Kamp pmap_pinit0(&vmspace0.vm_pmap); 442df8bae1dSRodney W. Grimes p->p_vmspace = &vmspace0; 443df8bae1dSRodney W. Grimes vmspace0.vm_refcnt = 1; 44426f9a767SRodney W. Grimes vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS), 4452d8acc0fSJohn Dyson trunc_page(VM_MAXUSER_ADDRESS)); 446df8bae1dSRodney W. Grimes vmspace0.vm_map.pmap = &vmspace0.vm_pmap; 447df8bae1dSRodney W. Grimes p->p_addr = proc0paddr; /* XXX */ 448df8bae1dSRodney W. Grimes 449ecbb00a2SDoug Rabson #ifndef __alpha__ /* XXX what is this? */ 4502b14f991SJulian Elischer #define INCOMPAT_LITES2 4512b14f991SJulian Elischer #ifdef INCOMPAT_LITES2 452df8bae1dSRodney W. Grimes /* 453dc733423SDag-Erling Smørgrav * proc0 needs to have a coherent frame base in its stack. 4542384fde5SDavid Greenman */ 4552b14f991SJulian Elischer cpu_set_init_frame(p, init_framep); /* XXX! */ 4562b14f991SJulian Elischer #endif /* INCOMPAT_LITES2*/ 457ecbb00a2SDoug Rabson #endif 4582384fde5SDavid Greenman 4592384fde5SDavid Greenman /* 460df8bae1dSRodney W. Grimes * We continue to place resource usage info and signal 461df8bae1dSRodney W. Grimes * actions in the user struct so they're pageable. 462df8bae1dSRodney W. Grimes */ 463df8bae1dSRodney W. Grimes p->p_stats = &p->p_addr->u_stats; 464df8bae1dSRodney W. Grimes p->p_sigacts = &p->p_addr->u_sigacts; 465df8bae1dSRodney W. Grimes 466df8bae1dSRodney W. Grimes /* 467a3bfb996SJeffrey Hsu * Charge root for one process. 468df8bae1dSRodney W. Grimes */ 469df8bae1dSRodney W. Grimes (void)chgproccnt(0, 1); 4702a024a2bSSean Eric Fagan 4712a024a2bSSean Eric Fagan /* 4722a024a2bSSean Eric Fagan * Initialize the procfs flags (to 0, of course) 4732a024a2bSSean Eric Fagan */ 4742a024a2bSSean Eric Fagan p->p_stops = p->p_stype = p->p_step = 0; 4752a024a2bSSean Eric Fagan 47626f9a767SRodney W. Grimes } 4772b14f991SJulian Elischer SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL) 4782b14f991SJulian Elischer 4792b14f991SJulian Elischer /* ARGSUSED*/ 480154c04e5SPoul-Henning Kamp static void proc0_post __P((void *dummy)); 481154c04e5SPoul-Henning Kamp static void 482d841aaa7SBruce Evans proc0_post(dummy) 483d841aaa7SBruce Evans void *dummy; 4842b14f991SJulian Elischer { 48500af9731SPoul-Henning Kamp struct timespec ts; 486a6fc8288SPeter Wemm 4872b14f991SJulian Elischer /* 4882b14f991SJulian Elischer * Now can look at time, having had a chance to verify the time 489e796e00dSPoul-Henning Kamp * from the file system. Reset p->p_runtime as it may have been 4905f88ec36SPoul-Henning Kamp * munched in mi_switch() after the time got set. Set 491e796e00dSPoul-Henning Kamp * p->p_switchtime to be consistent with this unmunching. 4922b14f991SJulian Elischer */ 4935f88ec36SPoul-Henning Kamp microtime(&proc0.p_stats->p_start); 494e796e00dSPoul-Henning Kamp proc0.p_runtime = 0; 495e796e00dSPoul-Henning Kamp microuptime(&proc0.p_switchtime); 4962b14f991SJulian Elischer 497a6fc8288SPeter Wemm /* 498a6fc8288SPeter Wemm * Give the ``random'' number generator a thump. 4995f88ec36SPoul-Henning Kamp * XXX: Does read_random() contain enough bits to be used here ? 500a6fc8288SPeter Wemm */ 50100af9731SPoul-Henning Kamp nanotime(&ts); 50200af9731SPoul-Henning Kamp srandom(ts.tv_sec ^ ts.tv_nsec); 503a6fc8288SPeter Wemm 5042b14f991SJulian Elischer /* Initialize signal state for process 0. */ 5052b14f991SJulian Elischer siginit(&proc0); 5062b14f991SJulian Elischer } 5072b14f991SJulian Elischer SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL) 5082b14f991SJulian Elischer 5092b14f991SJulian Elischer 5102b14f991SJulian Elischer 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes /* 5132b14f991SJulian Elischer *************************************************************************** 5142b14f991SJulian Elischer **** 5152b14f991SJulian Elischer **** The following SYSINIT's and glue code should be moved to the 5162b14f991SJulian Elischer **** respective files on a per subsystem basis. 5172b14f991SJulian Elischer **** 5182b14f991SJulian Elischer *************************************************************************** 519df8bae1dSRodney W. Grimes */ 520df8bae1dSRodney W. Grimes 5212b14f991SJulian Elischer /* ARGSUSED */ 522ab36c067SJustin T. Gibbs static void root_conf __P((void *dummy)); 523ab36c067SJustin T. Gibbs static void 524ab36c067SJustin T. Gibbs root_conf(dummy) 525ab36c067SJustin T. Gibbs void *dummy; 526ab36c067SJustin T. Gibbs { 527ab36c067SJustin T. Gibbs cpu_rootconf(); 528ab36c067SJustin T. Gibbs } 529ab36c067SJustin T. Gibbs SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL) 530ab36c067SJustin T. Gibbs 531ab36c067SJustin T. Gibbs /* ARGSUSED*/ 532154c04e5SPoul-Henning Kamp static void xxx_vfs_root_fdtab __P((void *dummy)); 533154c04e5SPoul-Henning Kamp static void 534d841aaa7SBruce Evans xxx_vfs_root_fdtab(dummy) 535d841aaa7SBruce Evans void *dummy; 5362b14f991SJulian Elischer { 5372b14f991SJulian Elischer register struct filedesc0 *fdp = &filedesc0; 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ 540628641f8SDavid Greenman if (VFS_ROOT(mountlist.cqh_first, &rootvnode)) 541df8bae1dSRodney W. Grimes panic("cannot find root vnode"); 542df8bae1dSRodney W. Grimes fdp->fd_fd.fd_cdir = rootvnode; 543df8bae1dSRodney W. Grimes VREF(fdp->fd_fd.fd_cdir); 544996c772fSJohn Dyson VOP_UNLOCK(rootvnode, 0, &proc0); 5459f24f214SJohn Dyson fdp->fd_fd.fd_rdir = rootvnode; 5462b14f991SJulian Elischer } 5472b14f991SJulian Elischer SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL) 5482b14f991SJulian Elischer 549df8bae1dSRodney W. Grimes 550df8bae1dSRodney W. Grimes /* 5512b14f991SJulian Elischer *************************************************************************** 5522b14f991SJulian Elischer **** 5532b14f991SJulian Elischer **** The following code probably belongs in another file, like 5542b14f991SJulian Elischer **** kern/init_init.c. It is here for two reasons only: 5552b14f991SJulian Elischer **** 5562b14f991SJulian Elischer **** 1) This code returns to startup the system; this is 5572b14f991SJulian Elischer **** abnormal for a kernel thread. 5582b14f991SJulian Elischer **** 2) This code promiscuously uses init_frame 5592b14f991SJulian Elischer **** 5602b14f991SJulian Elischer *************************************************************************** 561df8bae1dSRodney W. Grimes */ 562df8bae1dSRodney W. Grimes 563d841aaa7SBruce Evans static void kthread_init __P((void *dummy)); 56474b2192aSJohn Dyson SYSINIT_KP(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL) 5652b14f991SJulian Elischer 5662b14f991SJulian Elischer 567a2a1c95cSPeter Wemm extern void prepare_usermode __P((void)); 568a2a1c95cSPeter Wemm static void start_init __P((struct proc *p)); 5692b14f991SJulian Elischer 5702b14f991SJulian Elischer /* ARGSUSED*/ 5712b14f991SJulian Elischer static void 572d841aaa7SBruce Evans kthread_init(dummy) 573d841aaa7SBruce Evans void *dummy; 5742b14f991SJulian Elischer { 575df8bae1dSRodney W. Grimes /* Create process 1 (init(8)). */ 576a2a1c95cSPeter Wemm start_init(curproc); 577a2a1c95cSPeter Wemm 578a2a1c95cSPeter Wemm prepare_usermode(); 5792b14f991SJulian Elischer 5802b14f991SJulian Elischer /* 581a2a1c95cSPeter Wemm * This returns to the fork trampoline, then to user mode. 5822b14f991SJulian Elischer */ 583df8bae1dSRodney W. Grimes return; 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes 586df8bae1dSRodney W. Grimes 587df8bae1dSRodney W. Grimes /* 588df8bae1dSRodney W. Grimes * List of paths to try when searching for "init". 589df8bae1dSRodney W. Grimes */ 590df8bae1dSRodney W. Grimes static char *initpaths[] = { 591df8bae1dSRodney W. Grimes "/sbin/init", 592df8bae1dSRodney W. Grimes "/sbin/oinit", 593df8bae1dSRodney W. Grimes "/sbin/init.bak", 5942257b745SPoul-Henning Kamp "/stand/sysinstall", 595df8bae1dSRodney W. Grimes NULL, 596df8bae1dSRodney W. Grimes }; 597df8bae1dSRodney W. Grimes 598df8bae1dSRodney W. Grimes /* 599df8bae1dSRodney W. Grimes * Start the initial user process; try exec'ing each pathname in "initpaths". 600df8bae1dSRodney W. Grimes * The program is invoked with one argument containing the boot flags. 601df8bae1dSRodney W. Grimes */ 602df8bae1dSRodney W. Grimes static void 603a2a1c95cSPeter Wemm start_init(p) 604df8bae1dSRodney W. Grimes struct proc *p; 605df8bae1dSRodney W. Grimes { 606df8bae1dSRodney W. Grimes vm_offset_t addr; 607df8bae1dSRodney W. Grimes struct execve_args args; 6084a11ca4eSPoul-Henning Kamp int options, i, error; 609df8bae1dSRodney W. Grimes char **pathp, *path, *ucp, **uap, *arg0, *arg1; 610df8bae1dSRodney W. Grimes 611df8bae1dSRodney W. Grimes initproc = p; 612df8bae1dSRodney W. Grimes 613df8bae1dSRodney W. Grimes /* 614df8bae1dSRodney W. Grimes * Need just enough stack to hold the faked-up "execve()" arguments. 615df8bae1dSRodney W. Grimes */ 61626f9a767SRodney W. Grimes addr = trunc_page(VM_MAXUSER_ADDRESS - PAGE_SIZE); 617bd7e5f99SJohn Dyson if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) 618df8bae1dSRodney W. Grimes panic("init: couldn't allocate argument space"); 619df8bae1dSRodney W. Grimes p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 62026f9a767SRodney W. Grimes p->p_vmspace->vm_ssize = 1; 621df8bae1dSRodney W. Grimes 622df8bae1dSRodney W. Grimes for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 623df8bae1dSRodney W. Grimes /* 624df8bae1dSRodney W. Grimes * Move out the boot flag argument. 625df8bae1dSRodney W. Grimes */ 626df8bae1dSRodney W. Grimes options = 0; 627df8bae1dSRodney W. Grimes ucp = (char *)USRSTACK; 628df8bae1dSRodney W. Grimes (void)subyte(--ucp, 0); /* trailing zero */ 629df8bae1dSRodney W. Grimes if (boothowto & RB_SINGLE) { 630df8bae1dSRodney W. Grimes (void)subyte(--ucp, 's'); 631df8bae1dSRodney W. Grimes options = 1; 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes #ifdef notyet 634df8bae1dSRodney W. Grimes if (boothowto & RB_FASTBOOT) { 635df8bae1dSRodney W. Grimes (void)subyte(--ucp, 'f'); 636df8bae1dSRodney W. Grimes options = 1; 637df8bae1dSRodney W. Grimes } 638df8bae1dSRodney W. Grimes #endif 63917755ac8SPoul-Henning Kamp 64017755ac8SPoul-Henning Kamp #ifdef BOOTCDROM 64117755ac8SPoul-Henning Kamp (void)subyte(--ucp, 'C'); 64217755ac8SPoul-Henning Kamp options = 1; 64317755ac8SPoul-Henning Kamp #endif 644df8bae1dSRodney W. Grimes if (options == 0) 645df8bae1dSRodney W. Grimes (void)subyte(--ucp, '-'); 646df8bae1dSRodney W. Grimes (void)subyte(--ucp, '-'); /* leading hyphen */ 647df8bae1dSRodney W. Grimes arg1 = ucp; 648df8bae1dSRodney W. Grimes 649df8bae1dSRodney W. Grimes /* 650df8bae1dSRodney W. Grimes * Move out the file name (also arg 0). 651df8bae1dSRodney W. Grimes */ 652df8bae1dSRodney W. Grimes for (i = strlen(path) + 1; i >= 0; i--) 653df8bae1dSRodney W. Grimes (void)subyte(--ucp, path[i]); 654df8bae1dSRodney W. Grimes arg0 = ucp; 655df8bae1dSRodney W. Grimes 656df8bae1dSRodney W. Grimes /* 657df8bae1dSRodney W. Grimes * Move out the arg pointers. 658df8bae1dSRodney W. Grimes */ 659a20d7755SDoug Rabson uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 660c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)0); /* terminator */ 661c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 662c2da0fd9SBruce Evans (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes /* 665df8bae1dSRodney W. Grimes * Point at the arguments. 666df8bae1dSRodney W. Grimes */ 667df8bae1dSRodney W. Grimes args.fname = arg0; 66826f9a767SRodney W. Grimes args.argv = uap; 66926f9a767SRodney W. Grimes args.envv = NULL; 670df8bae1dSRodney W. Grimes 671df8bae1dSRodney W. Grimes /* 672df8bae1dSRodney W. Grimes * Now try to exec the program. If can't for any reason 673df8bae1dSRodney W. Grimes * other than it doesn't exist, complain. 6742b14f991SJulian Elischer * 6752b14f991SJulian Elischer * Otherwise return to main() which returns to btext 6762b14f991SJulian Elischer * which completes the system startup. 677df8bae1dSRodney W. Grimes */ 678cb226aaaSPoul-Henning Kamp if ((error = execve(p, &args)) == 0) 679df8bae1dSRodney W. Grimes return; 680df8bae1dSRodney W. Grimes if (error != ENOENT) 681df8bae1dSRodney W. Grimes printf("exec %s: error %d\n", path, error); 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes printf("init: not found\n"); 684df8bae1dSRodney W. Grimes panic("no init"); 685df8bae1dSRodney W. Grimes } 686