123364Smckusick /* 237610Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 339480Smckusick * All rights reserved. The Berkeley software License Agreement 439480Smckusick * specifies the terms and conditions for redistribution. 523364Smckusick * 6*42303Smarc * @(#)init_main.c 7.28 (Berkeley) 05/24/90 723364Smckusick */ 826Sbill 917087Sbloom #include "param.h" 1017087Sbloom #include "systm.h" 1117087Sbloom #include "user.h" 1217087Sbloom #include "kernel.h" 1317087Sbloom #include "mount.h" 1417087Sbloom #include "map.h" 1517087Sbloom #include "proc.h" 1637610Smckusick #include "vnode.h" 1717087Sbloom #include "seg.h" 1817087Sbloom #include "conf.h" 1917087Sbloom #include "buf.h" 2017087Sbloom #include "vm.h" 2117087Sbloom #include "cmap.h" 2217087Sbloom #include "text.h" 2317087Sbloom #include "clist.h" 2437611Smckusick #include "malloc.h" 2517087Sbloom #include "protosw.h" 2630570Skarels #include "reboot.h" 2737610Smckusick #include "../ufs/quota.h" 2826Sbill 2937522Smckusick #include "machine/pte.h" 3037522Smckusick #include "machine/reg.h" 3137522Smckusick #include "machine/cpu.h" 3237522Smckusick 3316807Skarels int cmask = CMASK; 3437610Smckusick extern int (*mountroot)(); 3526Sbill /* 3626Sbill * Initialization code. 3726Sbill * Called from cold start routine as 3826Sbill * soon as a stack and segmentation 3926Sbill * have been established. 4026Sbill * Functions: 4126Sbill * clear and free user core 4226Sbill * turn on clock 4326Sbill * hand craft 0th process 4426Sbill * call all initialization routines 4526Sbill * fork - process 0 to schedule 4621103Skarels * - process 1 execute bootstrap 4726Sbill * - process 2 to page out 4826Sbill */ 4926Sbill main(firstaddr) 508969Sroot int firstaddr; 5126Sbill { 52361Sbill register int i; 532451Swnj register struct proc *p; 5437611Smckusick register struct pgrp *pg; 559156Ssam int s; 5626Sbill 5726Sbill rqinit(); 5826Sbill startup(firstaddr); 5926Sbill 6026Sbill /* 6126Sbill * set up system process 0 (swapper) 6226Sbill */ 632451Swnj p = &proc[0]; 6440806Smarc bcopy("swapper", p->p_comm, sizeof ("swapper")); 658441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 662451Swnj p->p_szpt = 1; 672451Swnj p->p_addr = uaddr(p); 682451Swnj p->p_stat = SRUN; 692451Swnj p->p_flag |= SLOAD|SSYS; 702451Swnj p->p_nice = NZERO; 712451Swnj setredzone(p->p_addr, (caddr_t)&u); 722451Swnj u.u_procp = p; 7337611Smckusick MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp), 7437611Smckusick M_PGRP, M_NOWAIT); 7537611Smckusick if ((pg = pgrphash[0]) == NULL) 7637611Smckusick panic("no space to craft zero'th process group"); 7737611Smckusick pg->pg_id = 0; 7837611Smckusick pg->pg_hforw = 0; 7937611Smckusick pg->pg_mem = p; 8037611Smckusick pg->pg_jobc = 0; 8137611Smckusick p->p_pgrp = pg; 8237611Smckusick p->p_pgrpnxt = 0; 8337611Smckusick MALLOC(pg->pg_session, struct session *, sizeof (struct session), 8437611Smckusick M_SESSION, M_NOWAIT); 8537611Smckusick if (pg->pg_session == NULL) 8637611Smckusick panic("no space to craft zero'th session"); 8737611Smckusick pg->pg_session->s_count = 1; 8839559Smarc pg->pg_session->s_leader = NULL; 8939559Smarc pg->pg_session->s_ttyvp = NULL; 9039559Smarc pg->pg_session->s_ttyp = NULL; 9137611Smckusick #ifdef KTRACE 9237611Smckusick p->p_tracep = NULL; 9337611Smckusick p->p_traceflag = 0; 9437611Smckusick #endif 9516690Smckusick /* 9618278Smckusick * These assume that the u. area is always mapped 9718278Smckusick * to the same virtual address. Otherwise must be 9816690Smckusick * handled when copying the u. area in newproc(). 9916690Smckusick */ 10038264Smckusick ndinit(&u.u_nd); 10118278Smckusick u.u_ap = u.u_arg; 10229946Skarels 10316807Skarels u.u_cmask = cmask; 10421103Skarels u.u_lastfile = -1; 1058027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 1068027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 1078027Sroot RLIM_INFINITY; 10818278Smckusick /* 10923534Skarels * configure virtual memory system, 11023534Skarels * set vm rlimits 11118278Smckusick */ 11218278Smckusick vminit(); 11323534Skarels 11437610Smckusick /* 11539431Smckusick * Initialize the file systems. 11639431Smckusick * 11737610Smckusick * Get vnodes for swapdev, argdev, and rootdev. 11837610Smckusick */ 11939431Smckusick vfsinit(); 12037610Smckusick if (bdevvp(swapdev, &swapdev_vp) || 12137610Smckusick bdevvp(argdev, &argdev_vp) || 12237610Smckusick bdevvp(rootdev, &rootvp)) 12337610Smckusick panic("can't setup bdevvp's"); 12437610Smckusick 12537610Smckusick /* 12637610Smckusick * Setup credentials 12737610Smckusick */ 12837610Smckusick u.u_cred = crget(); 12941513Smckusick u.u_cred->cr_ngroups = 1; 13037610Smckusick 13130256Ssam startrtclock(); 13229946Skarels #if defined(vax) 13312823Ssam #include "kg.h" 13412823Ssam #if NKG > 0 13511356Smckusick startkgclock(); 13611356Smckusick #endif 13729946Skarels #endif 13826Sbill 13926Sbill /* 1404821Swnj * Initialize tables, protocols, and set up well-known inodes. 14126Sbill */ 1424821Swnj mbinit(); 14321103Skarels cinit(); 14441565Smckusick #ifdef SYSVSHM 14541565Smckusick shminit(); 14641565Smckusick #endif 14726137Skarels #include "sl.h" 14826137Skarels #if NSL > 0 14926137Skarels slattach(); /* XXX */ 15026137Skarels #endif 15141565Smckusick #include "loop.h" 1525855Swnj #if NLOOP > 0 1535855Swnj loattach(); /* XXX */ 1545855Swnj #endif 1559156Ssam /* 1569156Ssam * Block reception of incoming packets 1579156Ssam * until protocols have been initialized. 1589156Ssam */ 1599156Ssam s = splimp(); 1605227Swnj ifinit(); 1618969Sroot domaininit(); 1629156Ssam splx(s); 16316526Skarels pqinit(); 16425455Skarels xinit(); 16530531Skarels swapinit(); 1667419Sroot #ifdef GPROF 1677419Sroot kmstartup(); 1687419Sroot #endif 1697189Sroot 1708096Sroot /* kick off timeout driven events by calling first time */ 1718096Sroot roundrobin(); 1728096Sroot schedcpu(); 1738096Sroot schedpaging(); 1748096Sroot 1758096Sroot /* set up the root file system */ 17637610Smckusick if ((*mountroot)()) 17737610Smckusick panic("cannot mount root"); 17837610Smckusick /* 17937610Smckusick * Get vnode for '/'. 18037610Smckusick * Setup rootdir and u.u_cdir to point to it. 18137610Smckusick */ 18237610Smckusick if (VFS_ROOT(rootfs, &rootdir)) 18337610Smckusick panic("cannot find root vnode"); 18437610Smckusick u.u_cdir = rootdir; 18538347Smckusick VREF(u.u_cdir); 18637726Smckusick VOP_UNLOCK(rootdir); 1878096Sroot u.u_rdir = NULL; 188*42303Smarc boottime = u.u_start = time; 18926Sbill u.u_dmap = zdmap; 19026Sbill u.u_smap = zdmap; 19126Sbill 19230256Ssam enablertclock(); /* enable realtime clock interrupts */ 19326Sbill /* 19416807Skarels * make init process 19516807Skarels */ 19616807Skarels 19738927Skarels siginit(&proc[0]); 19816807Skarels proc[0].p_szpt = CLSIZE; 19916807Skarels if (newproc(0)) { 20016807Skarels expand(clrnd((int)btoc(szicode)), 0); 20142087Sbostic (void) swpexpand(u.u_dsize, (segsz_t)0, &u.u_dmap, &u.u_smap); 20216807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 20316807Skarels /* 20416807Skarels * Return goes to loc. 0 of user init 20516807Skarels * code just copied out. 20616807Skarels */ 20716807Skarels return; 20816807Skarels } 20916807Skarels /* 21026Sbill * make page-out daemon (process 2) 2112753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 21226Sbill * table so that it can map dirty pages into 21326Sbill * its address space during asychronous pushes. 21426Sbill */ 21541565Smckusick proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + HIGHPAGES)); 21626Sbill if (newproc(0)) { 21726Sbill proc[2].p_flag |= SLOAD|SSYS; 2182753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 21940806Smarc bcopy("pagedaemon", proc[2].p_comm, sizeof ("pagedaemon")); 22026Sbill pageout(); 2219025Sroot /*NOTREACHED*/ 22226Sbill } 22326Sbill 22426Sbill /* 22526Sbill * enter scheduling loop 22626Sbill */ 22726Sbill proc[0].p_szpt = 1; 22826Sbill sched(); 22926Sbill } 23026Sbill 23126Sbill /* 2327001Smckusick * Initialize hash links for buffers. 2337001Smckusick */ 2347001Smckusick bhinit() 2357001Smckusick { 2367001Smckusick register int i; 2377001Smckusick register struct bufhd *bp; 2387001Smckusick 2397001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2407001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2417001Smckusick } 2427001Smckusick 2437001Smckusick /* 24426Sbill * Initialize the buffer I/O system by freeing 24526Sbill * all buffers and setting all device buffer lists to empty. 24626Sbill */ 24726Sbill binit() 24826Sbill { 2498554Sroot register struct buf *bp, *dp; 25026Sbill register int i; 25110336Smckusick int base, residual; 25226Sbill 2532322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2542322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2552322Swnj dp->b_flags = B_HEAD; 2562322Swnj } 25710336Smckusick base = bufpages / nbuf; 25810336Smckusick residual = bufpages % nbuf; 2596570Smckusic for (i = 0; i < nbuf; i++) { 26026Sbill bp = &buf[i]; 26126Sbill bp->b_dev = NODEV; 2629156Ssam bp->b_bcount = 0; 26338774Smckusick bp->b_rcred = NOCRED; 26438774Smckusick bp->b_wcred = NOCRED; 26538774Smckusick bp->b_dirtyoff = 0; 26638774Smckusick bp->b_dirtyend = 0; 2676570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 26810336Smckusick if (i < residual) 26910336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 27010336Smckusick else 27110336Smckusick bp->b_bufsize = base * CLBYTES; 2729750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2732322Swnj bp->b_flags = B_BUSY|B_INVAL; 27426Sbill brelse(bp); 27526Sbill } 27630531Skarels } 27730531Skarels 27830531Skarels /* 27930531Skarels * Set up swap devices. 28030531Skarels * Initialize linked list of free swap 28130531Skarels * headers. These do not actually point 28230531Skarels * to buffers, but rather to pages that 28330531Skarels * are being swapped in and out. 28430531Skarels */ 28530531Skarels swapinit() 28630531Skarels { 28730531Skarels register int i; 28830531Skarels register struct buf *sp = swbuf; 28930531Skarels struct swdevt *swp; 29037611Smckusick int error; 29130531Skarels 292306Sbill /* 293306Sbill * Count swap devices, and adjust total swap space available. 29430531Skarels * Some of this space will not be available until a swapon() 295306Sbill * system is issued, usually when the system goes multi-user. 296306Sbill */ 297306Sbill nswdev = 0; 29812489Ssam nswap = 0; 29912489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 300306Sbill nswdev++; 30112489Ssam if (swp->sw_nblks > nswap) 30212489Ssam nswap = swp->sw_nblks; 30312489Ssam } 304306Sbill if (nswdev == 0) 30530531Skarels panic("swapinit"); 3064132Secc if (nswdev > 1) 30712489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 308306Sbill nswap *= nswdev; 30923534Skarels /* 31023534Skarels * If there are multiple swap areas, 31123534Skarels * allow more paging operations per second. 31223534Skarels */ 31323534Skarels if (nswdev > 1) 31423534Skarels maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 31537610Smckusick if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 31637610Smckusick panic("swapvp"); 31737611Smckusick if (error = swfree(0)) { 31837611Smckusick printf("swfree errno %d\n", error); /* XXX */ 31937611Smckusick panic("swapinit swfree 0"); 32037611Smckusick } 32126Sbill 32230531Skarels /* 32330531Skarels * Now set up swap buffer headers. 32430531Skarels */ 3252753Swnj bswlist.av_forw = sp; 3262769Swnj for (i=0; i<nswbuf-1; i++, sp++) 3272753Swnj sp->av_forw = sp+1; 3282753Swnj sp->av_forw = NULL; 32926Sbill } 3302746Swnj 3312746Swnj /* 3322746Swnj * Initialize clist by freeing all character blocks, then count 3332746Swnj * number of character devices. (Once-only routine) 3342746Swnj */ 3352746Swnj cinit() 3362746Swnj { 3372746Swnj register int ccp; 3382746Swnj register struct cblock *cp; 3392746Swnj 3402746Swnj ccp = (int)cfree; 3412746Swnj ccp = (ccp+CROUND) & ~CROUND; 3422746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 3432746Swnj cp->c_next = cfreelist; 3442746Swnj cfreelist = cp; 3452746Swnj cfreecount += CBSIZE; 3462746Swnj } 3472746Swnj } 348