123364Smckusick /* 237610Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3*39480Smckusick * All rights reserved. The Berkeley software License Agreement 4*39480Smckusick * specifies the terms and conditions for redistribution. 523364Smckusick * 6*39480Smckusick * @(#)init_main.c 7.20 (Berkeley) 11/02/89 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(); 585227Swnj #include "loop.h" 5926Sbill startup(firstaddr); 6026Sbill 6126Sbill /* 6226Sbill * set up system process 0 (swapper) 6326Sbill */ 642451Swnj p = &proc[0]; 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; 8837611Smckusick pg->pg_session->s_leader = 0; 8937611Smckusick #ifdef KTRACE 9037611Smckusick p->p_tracep = NULL; 9137611Smckusick p->p_traceflag = 0; 9237611Smckusick #endif 9316690Smckusick /* 9418278Smckusick * These assume that the u. area is always mapped 9518278Smckusick * to the same virtual address. Otherwise must be 9616690Smckusick * handled when copying the u. area in newproc(). 9716690Smckusick */ 9838264Smckusick ndinit(&u.u_nd); 9918278Smckusick u.u_ap = u.u_arg; 10029946Skarels 10116807Skarels u.u_cmask = cmask; 10221103Skarels u.u_lastfile = -1; 1038027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 1048027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 1058027Sroot RLIM_INFINITY; 10618278Smckusick /* 10723534Skarels * configure virtual memory system, 10823534Skarels * set vm rlimits 10918278Smckusick */ 11018278Smckusick vminit(); 11123534Skarels 11237610Smckusick /* 11339431Smckusick * Initialize the file systems. 11439431Smckusick * 11537610Smckusick * Get vnodes for swapdev, argdev, and rootdev. 11637610Smckusick */ 11739431Smckusick vfsinit(); 11837610Smckusick if (bdevvp(swapdev, &swapdev_vp) || 11937610Smckusick bdevvp(argdev, &argdev_vp) || 12037610Smckusick bdevvp(rootdev, &rootvp)) 12137610Smckusick panic("can't setup bdevvp's"); 12237610Smckusick 12337610Smckusick /* 12437610Smckusick * Setup credentials 12537610Smckusick */ 12637610Smckusick u.u_cred = crget(); 12737610Smckusick u.u_ngroups = 1; 12837610Smckusick 12916526Skarels #if defined(QUOTA) 1307486Skre qtinit(); 1317486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 1327486Skre #endif 13330256Ssam startrtclock(); 13429946Skarels #if defined(vax) 13512823Ssam #include "kg.h" 13612823Ssam #if NKG > 0 13711356Smckusick startkgclock(); 13811356Smckusick #endif 13929946Skarels #endif 14026Sbill 14126Sbill /* 1424821Swnj * Initialize tables, protocols, and set up well-known inodes. 14326Sbill */ 1444821Swnj mbinit(); 14521103Skarels cinit(); 14626137Skarels #include "sl.h" 14726137Skarels #if NSL > 0 14826137Skarels slattach(); /* XXX */ 14926137Skarels #endif 1505855Swnj #if NLOOP > 0 1515855Swnj loattach(); /* XXX */ 1525855Swnj #endif 1539156Ssam /* 1549156Ssam * Block reception of incoming packets 1559156Ssam * until protocols have been initialized. 1569156Ssam */ 1579156Ssam s = splimp(); 1585227Swnj ifinit(); 1598969Sroot domaininit(); 1609156Ssam splx(s); 16116526Skarels pqinit(); 16225455Skarels xinit(); 16330531Skarels swapinit(); 1647419Sroot #ifdef GPROF 1657419Sroot kmstartup(); 1667419Sroot #endif 1677189Sroot 1688096Sroot /* kick off timeout driven events by calling first time */ 1698096Sroot roundrobin(); 1708096Sroot schedcpu(); 1718096Sroot schedpaging(); 1728096Sroot 1738096Sroot /* set up the root file system */ 17437610Smckusick if ((*mountroot)()) 17537610Smckusick panic("cannot mount root"); 17637610Smckusick /* 17737610Smckusick * Get vnode for '/'. 17837610Smckusick * Setup rootdir and u.u_cdir to point to it. 17937610Smckusick */ 18037610Smckusick if (VFS_ROOT(rootfs, &rootdir)) 18137610Smckusick panic("cannot find root vnode"); 18237610Smckusick u.u_cdir = rootdir; 18338347Smckusick VREF(u.u_cdir); 18437726Smckusick VOP_UNLOCK(rootdir); 1858096Sroot u.u_rdir = NULL; 18637610Smckusick boottime = time; 1877189Sroot 18826Sbill u.u_dmap = zdmap; 18926Sbill u.u_smap = zdmap; 19026Sbill 19130256Ssam enablertclock(); /* enable realtime clock interrupts */ 19226Sbill /* 19316807Skarels * make init process 19416807Skarels */ 19516807Skarels 19638927Skarels siginit(&proc[0]); 19716807Skarels proc[0].p_szpt = CLSIZE; 19816807Skarels if (newproc(0)) { 19916807Skarels expand(clrnd((int)btoc(szicode)), 0); 20026352Skarels (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap); 20116807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 20216807Skarels /* 20316807Skarels * Return goes to loc. 0 of user init 20416807Skarels * code just copied out. 20516807Skarels */ 20616807Skarels return; 20716807Skarels } 20816807Skarels /* 20926Sbill * make page-out daemon (process 2) 2102753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 21126Sbill * table so that it can map dirty pages into 21226Sbill * its address space during asychronous pushes. 21326Sbill */ 2142753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 21526Sbill if (newproc(0)) { 21626Sbill proc[2].p_flag |= SLOAD|SSYS; 2172753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 21826Sbill pageout(); 2199025Sroot /*NOTREACHED*/ 22026Sbill } 22126Sbill 22226Sbill /* 22326Sbill * enter scheduling loop 22426Sbill */ 22526Sbill proc[0].p_szpt = 1; 22626Sbill sched(); 22726Sbill } 22826Sbill 22926Sbill /* 2307001Smckusick * Initialize hash links for buffers. 2317001Smckusick */ 2327001Smckusick bhinit() 2337001Smckusick { 2347001Smckusick register int i; 2357001Smckusick register struct bufhd *bp; 2367001Smckusick 2377001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2387001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2397001Smckusick } 2407001Smckusick 2417001Smckusick /* 24226Sbill * Initialize the buffer I/O system by freeing 24326Sbill * all buffers and setting all device buffer lists to empty. 24426Sbill */ 24526Sbill binit() 24626Sbill { 2478554Sroot register struct buf *bp, *dp; 24826Sbill register int i; 24910336Smckusick int base, residual; 25026Sbill 2512322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2522322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2532322Swnj dp->b_flags = B_HEAD; 2542322Swnj } 25510336Smckusick base = bufpages / nbuf; 25610336Smckusick residual = bufpages % nbuf; 2576570Smckusic for (i = 0; i < nbuf; i++) { 25826Sbill bp = &buf[i]; 25926Sbill bp->b_dev = NODEV; 2609156Ssam bp->b_bcount = 0; 26138774Smckusick bp->b_rcred = NOCRED; 26238774Smckusick bp->b_wcred = NOCRED; 26338774Smckusick bp->b_dirtyoff = 0; 26438774Smckusick bp->b_dirtyend = 0; 2656570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 26610336Smckusick if (i < residual) 26710336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 26810336Smckusick else 26910336Smckusick bp->b_bufsize = base * CLBYTES; 2709750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2712322Swnj bp->b_flags = B_BUSY|B_INVAL; 27226Sbill brelse(bp); 27326Sbill } 27430531Skarels } 27530531Skarels 27630531Skarels /* 27730531Skarels * Set up swap devices. 27830531Skarels * Initialize linked list of free swap 27930531Skarels * headers. These do not actually point 28030531Skarels * to buffers, but rather to pages that 28130531Skarels * are being swapped in and out. 28230531Skarels */ 28330531Skarels swapinit() 28430531Skarels { 28530531Skarels register int i; 28630531Skarels register struct buf *sp = swbuf; 28730531Skarels struct swdevt *swp; 28837611Smckusick int error; 28930531Skarels 290306Sbill /* 291306Sbill * Count swap devices, and adjust total swap space available. 29230531Skarels * Some of this space will not be available until a swapon() 293306Sbill * system is issued, usually when the system goes multi-user. 294306Sbill */ 295306Sbill nswdev = 0; 29612489Ssam nswap = 0; 29712489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 298306Sbill nswdev++; 29912489Ssam if (swp->sw_nblks > nswap) 30012489Ssam nswap = swp->sw_nblks; 30112489Ssam } 302306Sbill if (nswdev == 0) 30330531Skarels panic("swapinit"); 3044132Secc if (nswdev > 1) 30512489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 306306Sbill nswap *= nswdev; 30723534Skarels /* 30823534Skarels * If there are multiple swap areas, 30923534Skarels * allow more paging operations per second. 31023534Skarels */ 31123534Skarels if (nswdev > 1) 31223534Skarels maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 31337610Smckusick if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 31437610Smckusick panic("swapvp"); 31537611Smckusick if (error = swfree(0)) { 31637611Smckusick printf("swfree errno %d\n", error); /* XXX */ 31737611Smckusick panic("swapinit swfree 0"); 31837611Smckusick } 31926Sbill 32030531Skarels /* 32130531Skarels * Now set up swap buffer headers. 32230531Skarels */ 3232753Swnj bswlist.av_forw = sp; 3242769Swnj for (i=0; i<nswbuf-1; i++, sp++) 3252753Swnj sp->av_forw = sp+1; 3262753Swnj sp->av_forw = NULL; 32726Sbill } 3282746Swnj 3292746Swnj /* 3302746Swnj * Initialize clist by freeing all character blocks, then count 3312746Swnj * number of character devices. (Once-only routine) 3322746Swnj */ 3332746Swnj cinit() 3342746Swnj { 3352746Swnj register int ccp; 3362746Swnj register struct cblock *cp; 3372746Swnj 3382746Swnj ccp = (int)cfree; 3392746Swnj ccp = (ccp+CROUND) & ~CROUND; 3402746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 3412746Swnj cp->c_next = cfreelist; 3422746Swnj cfreelist = cp; 3432746Swnj cfreecount += CBSIZE; 3442746Swnj } 3452746Swnj } 346