123364Smckusick /* 247543Skarels * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California. 347817Skarels * All rights reserved. 423364Smckusick * 547817Skarels * %sccs.include.redist.c% 647817Skarels * 7*51453Sbostic * @(#)init_main.c 7.42 (Berkeley) 11/01/91 823364Smckusick */ 926Sbill 1017087Sbloom #include "param.h" 1145914Smckusick #include "filedesc.h" 1217087Sbloom #include "kernel.h" 1317087Sbloom #include "mount.h" 1417087Sbloom #include "map.h" 1517087Sbloom #include "proc.h" 1647654Skarels #include "resourcevar.h" 1747654Skarels #include "signalvar.h" 1847654Skarels #include "systm.h" 1937610Smckusick #include "vnode.h" 2017087Sbloom #include "seg.h" 2117087Sbloom #include "conf.h" 2217087Sbloom #include "buf.h" 2317087Sbloom #include "clist.h" 2437611Smckusick #include "malloc.h" 2517087Sbloom #include "protosw.h" 2630570Skarels #include "reboot.h" 2747654Skarels #include "user.h" 2847654Skarels 29*51453Sbostic #include "ufs/ufs/quota.h" 3026Sbill 3137522Smckusick #include "machine/cpu.h" 3237522Smckusick 3347543Skarels #include "vm/vm.h" 3445724Smckusick 3549382Skarels char copyright[] = 3649382Skarels "Copyright (c) 1982,1986,1989,1991 The Regents of the University of California.\nAll rights reserved.\n\n"; 3749382Skarels 3847543Skarels /* 3947543Skarels * Components of process 0; 4047543Skarels * never freed. 4147543Skarels */ 4247543Skarels struct session session0; 4347543Skarels struct pgrp pgrp0; 4447543Skarels struct proc proc0; 4547543Skarels struct pcred cred0; 4647654Skarels struct filedesc0 filedesc0; 4747543Skarels struct plimit limit0; 4847543Skarels struct vmspace vmspace0; 4947654Skarels struct proc *curproc = &proc0; 5047543Skarels struct proc *initproc, *pageproc; 5147543Skarels 5216807Skarels int cmask = CMASK; 5348981Skarels extern struct user *proc0paddr; 5437610Smckusick extern int (*mountroot)(); 5545880Swilliam 5649382Skarels struct vnode *rootvp, *swapdev_vp; 5749382Skarels int boothowto; 5849382Skarels 5926Sbill /* 6047543Skarels * System startup; initialize the world, create process 0, 6147543Skarels * mount root filesystem, and fork to create init and pagedaemon. 6247543Skarels * Most of the hard work is done in the lower-level initialization 6347543Skarels * routines including startup(), which does memory initialization 6447543Skarels * and autoconfiguration. 6526Sbill */ 6649533Skarels main() 6726Sbill { 68361Sbill register int i; 692451Swnj register struct proc *p; 7047654Skarels register struct filedesc0 *fdp; 7147543Skarels int s, rval[2]; 7226Sbill 7345880Swilliam /* 7447543Skarels * Initialize curproc before any possible traps/probes 7547543Skarels * to simplify trap processing. 7645880Swilliam */ 7747543Skarels p = &proc0; 7847543Skarels curproc = p; 7945724Smckusick /* 8047543Skarels * Attempt to find console and initialize 8147543Skarels * in case of early panic or other messages. 8245724Smckusick */ 8347543Skarels consinit(); 8449382Skarels printf(copyright); 8547543Skarels 8645724Smckusick vm_mem_init(); 8745724Smckusick kmeminit(); 8849533Skarels cpu_startup(); 8926Sbill 9026Sbill /* 9126Sbill * set up system process 0 (swapper) 9226Sbill */ 9347543Skarels p = &proc0; 9447543Skarels curproc = p; 9547543Skarels 9647543Skarels allproc = p; 9747543Skarels p->p_prev = &allproc; 9847543Skarels p->p_pgrp = &pgrp0; 9947543Skarels pgrphash[0] = &pgrp0; 10047543Skarels pgrp0.pg_mem = p; 10147543Skarels pgrp0.pg_session = &session0; 10247543Skarels session0.s_count = 1; 10347543Skarels session0.s_leader = p; 10447543Skarels 10547543Skarels p->p_flag = SLOAD|SSYS; 1062451Swnj p->p_stat = SRUN; 1072451Swnj p->p_nice = NZERO; 10847543Skarels bcopy("swapper", p->p_comm, sizeof ("swapper")); 10947543Skarels 11045724Smckusick /* 11147543Skarels * Setup credentials 11245724Smckusick */ 11349745Smckusick cred0.p_refcnt = 1; 11447543Skarels p->p_cred = &cred0; 11547543Skarels p->p_ucred = crget(); 11647543Skarels p->p_ucred->cr_ngroups = 1; /* group 0 */ 11729946Skarels 11845914Smckusick /* 11945914Smckusick * Create the file descriptor table for process 0. 12045914Smckusick */ 12147543Skarels fdp = &filedesc0; 12247654Skarels p->p_fd = &fdp->fd_fd; 12347654Skarels fdp->fd_fd.fd_refcnt = 1; 12447654Skarels fdp->fd_fd.fd_cmask = cmask; 12547654Skarels fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 12647654Skarels fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 12747654Skarels fdp->fd_fd.fd_nfiles = NDFILE; 12847543Skarels 12918278Smckusick /* 13047543Skarels * Set initial limits 13147543Skarels */ 13247543Skarels p->p_limit = &limit0; 13347543Skarels for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 13447543Skarels limit0.pl_rlimit[i].rlim_cur = 13547543Skarels limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 13647543Skarels limit0.pl_rlimit[RLIMIT_OFILE].rlim_cur = NOFILE; 13747543Skarels limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC; 13847543Skarels limit0.p_refcnt = 1; 13947543Skarels 14047543Skarels /* 14147543Skarels * Allocate a prototype map so we have something to fork 14247543Skarels */ 14347543Skarels p->p_vmspace = &vmspace0; 14447543Skarels vmspace0.vm_refcnt = 1; 14547543Skarels pmap_pinit(&vmspace0.vm_pmap); 14647543Skarels vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS), 14747543Skarels trunc_page(VM_MAX_ADDRESS), TRUE); 14847543Skarels vmspace0.vm_map.pmap = &vmspace0.vm_pmap; 14947543Skarels p->p_addr = proc0paddr; /* XXX */ 15047543Skarels 15147543Skarels /* 15247543Skarels * We continue to place resource usage info 15347543Skarels * and signal actions in the user struct so they're pageable. 15447543Skarels */ 15548981Skarels p->p_stats = &p->p_addr->u_stats; 15648981Skarels p->p_sigacts = &p->p_addr->u_sigacts; 15747543Skarels 15847543Skarels rqinit(); 15947543Skarels 16047543Skarels /* 16123534Skarels * configure virtual memory system, 16223534Skarels * set vm rlimits 16318278Smckusick */ 16447543Skarels vm_init_limits(p); 16523534Skarels 16637610Smckusick /* 16739431Smckusick * Initialize the file systems. 16839431Smckusick * 16949382Skarels * Get vnodes for swapdev and rootdev. 17037610Smckusick */ 17139431Smckusick vfsinit(); 17249382Skarels if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp)) 17337610Smckusick panic("can't setup bdevvp's"); 17437610Smckusick 17530256Ssam startrtclock(); 17629946Skarels #if defined(vax) 17712823Ssam #include "kg.h" 17812823Ssam #if NKG > 0 17911356Smckusick startkgclock(); 18011356Smckusick #endif 18129946Skarels #endif 18226Sbill 18326Sbill /* 1844821Swnj * Initialize tables, protocols, and set up well-known inodes. 18526Sbill */ 1864821Swnj mbinit(); 18721103Skarels cinit(); 18841565Smckusick #ifdef SYSVSHM 18941565Smckusick shminit(); 19041565Smckusick #endif 19126137Skarels #include "sl.h" 19226137Skarels #if NSL > 0 19326137Skarels slattach(); /* XXX */ 19426137Skarels #endif 19541565Smckusick #include "loop.h" 1965855Swnj #if NLOOP > 0 1975855Swnj loattach(); /* XXX */ 1985855Swnj #endif 1999156Ssam /* 2009156Ssam * Block reception of incoming packets 2019156Ssam * until protocols have been initialized. 2029156Ssam */ 2039156Ssam s = splimp(); 2045227Swnj ifinit(); 2058969Sroot domaininit(); 2069156Ssam splx(s); 20747543Skarels 2087419Sroot #ifdef GPROF 2097419Sroot kmstartup(); 2107419Sroot #endif 2117189Sroot 21247543Skarels /* kick off timeout driven events by calling first time */ 2138096Sroot roundrobin(); 2148096Sroot schedcpu(); 21545880Swilliam enablertclock(); /* enable realtime clock interrupts */ 2168096Sroot 21747543Skarels /* 21847543Skarels * Set up the root file system and vnode. 21947543Skarels */ 22037610Smckusick if ((*mountroot)()) 22137610Smckusick panic("cannot mount root"); 22237610Smckusick /* 22337610Smckusick * Get vnode for '/'. 22447654Skarels * Setup rootdir and fdp->fd_fd.fd_cdir to point to it. 22537610Smckusick */ 22637610Smckusick if (VFS_ROOT(rootfs, &rootdir)) 22737610Smckusick panic("cannot find root vnode"); 22847654Skarels fdp->fd_fd.fd_cdir = rootdir; 22947654Skarels VREF(fdp->fd_fd.fd_cdir); 23037726Smckusick VOP_UNLOCK(rootdir); 23147654Skarels fdp->fd_fd.fd_rdir = NULL; 23247543Skarels swapinit(); 23326Sbill 23426Sbill /* 23547543Skarels * Now can look at time, having had a chance 23647543Skarels * to verify the time from the file system. 23747543Skarels */ 23847543Skarels boottime = p->p_stats->p_start = time; 23947543Skarels 24047543Skarels /* 24116807Skarels * make init process 24216807Skarels */ 24347543Skarels siginit(p); 24447543Skarels if (fork(p, (void *) NULL, rval)) 24547543Skarels panic("fork init"); 24647543Skarels if (rval[1]) { 24747689Skarels static char initflags[] = "-sf"; 24847689Skarels char *ip = initflags + 1; 24945724Smckusick vm_offset_t addr = 0; 25049382Skarels extern int icode[]; /* user init code */ 25149382Skarels extern int szicode; /* size of icode */ 25245724Smckusick 25347543Skarels /* 25447543Skarels * Now in process 1. Set init flags into icode, 25547543Skarels * get a minimal address space, copy out "icode", 25647543Skarels * and return to it to do an exec of init. 25747543Skarels */ 25847543Skarels p = curproc; 25947543Skarels initproc = p; 26047543Skarels if (boothowto&RB_SINGLE) 26147543Skarels *ip++ = 's'; 26247543Skarels #ifdef notyet 26347543Skarels if (boothowto&RB_FASTBOOT) 26447543Skarels *ip++ = 'f'; 26547689Skarels #endif 26647543Skarels *ip++ = '\0'; 26747543Skarels 26847543Skarels if (vm_allocate(&p->p_vmspace->vm_map, &addr, 26947689Skarels round_page(szicode + sizeof(initflags)), FALSE) != 0 || 27047689Skarels addr != 0) 27145724Smckusick panic("init: couldn't allocate at zero"); 27245724Smckusick 27345724Smckusick /* need just enough stack to exec from */ 27448981Skarels addr = trunc_page(USRSTACK - PAGE_SIZE); 27547543Skarels if (vm_allocate(&p->p_vmspace->vm_map, &addr, 27647543Skarels PAGE_SIZE, FALSE) != KERN_SUCCESS) 27747543Skarels panic("vm_allocate init stack"); 27847543Skarels p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 27916807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 28047689Skarels (void) copyout(initflags, (caddr_t)szicode, sizeof(initflags)); 28147543Skarels return; /* returns to icode */ 28216807Skarels } 28347543Skarels 28416807Skarels /* 28545724Smckusick * Start up pageout daemon (process 2). 28626Sbill */ 28747543Skarels if (fork(p, (void *) NULL, rval)) 28847543Skarels panic("fork pager"); 28947543Skarels if (rval[1]) { 29047543Skarels /* 29147543Skarels * Now in process 2. 29247543Skarels */ 29347543Skarels p = curproc; 29447543Skarels pageproc = p; 29547543Skarels p->p_flag |= SLOAD|SSYS; /* XXX */ 29647543Skarels bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon")); 29745724Smckusick vm_pageout(); 2989025Sroot /*NOTREACHED*/ 29926Sbill } 30026Sbill 30126Sbill /* 30226Sbill * enter scheduling loop 30326Sbill */ 30426Sbill sched(); 30526Sbill } 306