123364Smckusick /* 237610Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337610Smckusick * All rights reserved. 423364Smckusick * 537610Smckusick * Redistribution and use in source and binary forms are permitted 637610Smckusick * provided that the above copyright notice and this paragraph are 737610Smckusick * duplicated in all such forms and that any documentation, 837610Smckusick * advertising materials, and other materials related to such 937610Smckusick * distribution and use acknowledge that the software was developed 1037610Smckusick * by the University of California, Berkeley. The name of the 1137610Smckusick * University may not be used to endorse or promote products derived 1237610Smckusick * from this software without specific prior written permission. 1337610Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437610Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537610Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637610Smckusick * 17*37726Smckusick * @(#)init_main.c 7.11 (Berkeley) 05/09/89 1823364Smckusick */ 1926Sbill 2017087Sbloom #include "param.h" 2117087Sbloom #include "systm.h" 2217087Sbloom #include "user.h" 2317087Sbloom #include "kernel.h" 2417087Sbloom #include "mount.h" 2517087Sbloom #include "map.h" 2617087Sbloom #include "proc.h" 2737610Smckusick #include "vnode.h" 2817087Sbloom #include "seg.h" 2917087Sbloom #include "conf.h" 3017087Sbloom #include "buf.h" 3117087Sbloom #include "vm.h" 3217087Sbloom #include "cmap.h" 3317087Sbloom #include "text.h" 3417087Sbloom #include "clist.h" 3537611Smckusick #include "malloc.h" 3617087Sbloom #include "protosw.h" 3730570Skarels #include "reboot.h" 3837610Smckusick #include "../ufs/quota.h" 3926Sbill 4037522Smckusick #include "machine/pte.h" 4137522Smckusick #include "machine/reg.h" 4237522Smckusick #include "machine/cpu.h" 4337522Smckusick 4416807Skarels int cmask = CMASK; 4537610Smckusick extern int (*mountroot)(); 4626Sbill /* 4726Sbill * Initialization code. 4826Sbill * Called from cold start routine as 4926Sbill * soon as a stack and segmentation 5026Sbill * have been established. 5126Sbill * Functions: 5226Sbill * clear and free user core 5326Sbill * turn on clock 5426Sbill * hand craft 0th process 5526Sbill * call all initialization routines 5626Sbill * fork - process 0 to schedule 5721103Skarels * - process 1 execute bootstrap 5826Sbill * - process 2 to page out 5926Sbill */ 6026Sbill main(firstaddr) 618969Sroot int firstaddr; 6226Sbill { 63361Sbill register int i; 642451Swnj register struct proc *p; 6537611Smckusick register struct pgrp *pg; 669156Ssam int s; 6726Sbill 6826Sbill rqinit(); 695227Swnj #include "loop.h" 7026Sbill startup(firstaddr); 7126Sbill 7226Sbill /* 7326Sbill * set up system process 0 (swapper) 7426Sbill */ 752451Swnj p = &proc[0]; 768441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 772451Swnj p->p_szpt = 1; 782451Swnj p->p_addr = uaddr(p); 792451Swnj p->p_stat = SRUN; 802451Swnj p->p_flag |= SLOAD|SSYS; 812451Swnj p->p_nice = NZERO; 822451Swnj setredzone(p->p_addr, (caddr_t)&u); 832451Swnj u.u_procp = p; 8437611Smckusick MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp), 8537611Smckusick M_PGRP, M_NOWAIT); 8637611Smckusick if ((pg = pgrphash[0]) == NULL) 8737611Smckusick panic("no space to craft zero'th process group"); 8837611Smckusick pg->pg_id = 0; 8937611Smckusick pg->pg_hforw = 0; 9037611Smckusick pg->pg_mem = p; 9137611Smckusick pg->pg_jobc = 0; 9237611Smckusick p->p_pgrp = pg; 9337611Smckusick p->p_pgrpnxt = 0; 9437611Smckusick MALLOC(pg->pg_session, struct session *, sizeof (struct session), 9537611Smckusick M_SESSION, M_NOWAIT); 9637611Smckusick if (pg->pg_session == NULL) 9737611Smckusick panic("no space to craft zero'th session"); 9837611Smckusick pg->pg_session->s_count = 1; 9937611Smckusick pg->pg_session->s_leader = 0; 10037611Smckusick #ifdef KTRACE 10137611Smckusick p->p_tracep = NULL; 10237611Smckusick p->p_traceflag = 0; 10337611Smckusick #endif 10416690Smckusick /* 10518278Smckusick * These assume that the u. area is always mapped 10618278Smckusick * to the same virtual address. Otherwise must be 10716690Smckusick * handled when copying the u. area in newproc(). 10816690Smckusick */ 10937610Smckusick u.u_nd.ni_iov = &u.u_nd.ni_nd.nd_iovec; 11018278Smckusick u.u_ap = u.u_arg; 11116690Smckusick u.u_nd.ni_iovcnt = 1; 11229946Skarels 11316807Skarels u.u_cmask = cmask; 11421103Skarels u.u_lastfile = -1; 1158027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 1168027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 1178027Sroot RLIM_INFINITY; 11818278Smckusick /* 11923534Skarels * configure virtual memory system, 12023534Skarels * set vm rlimits 12118278Smckusick */ 12218278Smckusick vminit(); 12323534Skarels 12437610Smckusick /* 12537610Smckusick * Get vnodes for swapdev, argdev, and rootdev. 12637610Smckusick */ 12737610Smckusick ihinit(); 12837610Smckusick nchinit(); 12937610Smckusick if (bdevvp(swapdev, &swapdev_vp) || 13037610Smckusick bdevvp(argdev, &argdev_vp) || 13137610Smckusick bdevvp(rootdev, &rootvp)) 13237610Smckusick panic("can't setup bdevvp's"); 13337610Smckusick 13437610Smckusick /* 13537610Smckusick * Setup credentials 13637610Smckusick */ 13737610Smckusick u.u_cred = crget(); 13837610Smckusick u.u_ngroups = 1; 13937610Smckusick 14016526Skarels #if defined(QUOTA) 1417486Skre qtinit(); 1427486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 1437486Skre #endif 14430256Ssam startrtclock(); 14529946Skarels #if defined(vax) 14612823Ssam #include "kg.h" 14712823Ssam #if NKG > 0 14811356Smckusick startkgclock(); 14911356Smckusick #endif 15029946Skarels #endif 15126Sbill 15226Sbill /* 1534821Swnj * Initialize tables, protocols, and set up well-known inodes. 15426Sbill */ 1554821Swnj mbinit(); 15621103Skarels cinit(); 15726137Skarels #include "sl.h" 15826137Skarels #if NSL > 0 15926137Skarels slattach(); /* XXX */ 16026137Skarels #endif 1615855Swnj #if NLOOP > 0 1625855Swnj loattach(); /* XXX */ 1635855Swnj #endif 1649156Ssam /* 1659156Ssam * Block reception of incoming packets 1669156Ssam * until protocols have been initialized. 1679156Ssam */ 1689156Ssam s = splimp(); 1695227Swnj ifinit(); 1708969Sroot domaininit(); 1719156Ssam splx(s); 17216526Skarels pqinit(); 17325455Skarels xinit(); 17430531Skarels swapinit(); 1757419Sroot #ifdef GPROF 1767419Sroot kmstartup(); 1777419Sroot #endif 17837610Smckusick #ifdef NFS 17937610Smckusick nfsinit(); 18037610Smckusick #endif 1817189Sroot 1828096Sroot /* kick off timeout driven events by calling first time */ 1838096Sroot roundrobin(); 1848096Sroot schedcpu(); 1858096Sroot schedpaging(); 1868096Sroot 1878096Sroot /* set up the root file system */ 18837610Smckusick if ((*mountroot)()) 18937610Smckusick panic("cannot mount root"); 19037610Smckusick /* 19137610Smckusick * Get vnode for '/'. 19237610Smckusick * Setup rootdir and u.u_cdir to point to it. 19337610Smckusick */ 19437610Smckusick if (VFS_ROOT(rootfs, &rootdir)) 19537610Smckusick panic("cannot find root vnode"); 19637610Smckusick u.u_cdir = rootdir; 19737610Smckusick u.u_cdir->v_count++; 198*37726Smckusick VOP_UNLOCK(rootdir); 1998096Sroot u.u_rdir = NULL; 20037610Smckusick boottime = time; 2017189Sroot 20226Sbill u.u_dmap = zdmap; 20326Sbill u.u_smap = zdmap; 20426Sbill 20530256Ssam enablertclock(); /* enable realtime clock interrupts */ 20626Sbill /* 20716807Skarels * make init process 20816807Skarels */ 20916807Skarels 21016807Skarels proc[0].p_szpt = CLSIZE; 21116807Skarels if (newproc(0)) { 21216807Skarels expand(clrnd((int)btoc(szicode)), 0); 21326352Skarels (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap); 21416807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 21516807Skarels /* 21616807Skarels * Return goes to loc. 0 of user init 21716807Skarels * code just copied out. 21816807Skarels */ 21916807Skarels return; 22016807Skarels } 22116807Skarels /* 22226Sbill * make page-out daemon (process 2) 2232753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 22426Sbill * table so that it can map dirty pages into 22526Sbill * its address space during asychronous pushes. 22626Sbill */ 2272753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 22826Sbill if (newproc(0)) { 22926Sbill proc[2].p_flag |= SLOAD|SSYS; 2302753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 23126Sbill pageout(); 2329025Sroot /*NOTREACHED*/ 23326Sbill } 23426Sbill 23526Sbill /* 23626Sbill * enter scheduling loop 23726Sbill */ 23826Sbill proc[0].p_szpt = 1; 23926Sbill sched(); 24026Sbill } 24126Sbill 24226Sbill /* 2437001Smckusick * Initialize hash links for buffers. 2447001Smckusick */ 2457001Smckusick bhinit() 2467001Smckusick { 2477001Smckusick register int i; 2487001Smckusick register struct bufhd *bp; 2497001Smckusick 2507001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2517001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2527001Smckusick } 2537001Smckusick 2547001Smckusick /* 25526Sbill * Initialize the buffer I/O system by freeing 25626Sbill * all buffers and setting all device buffer lists to empty. 25726Sbill */ 25826Sbill binit() 25926Sbill { 2608554Sroot register struct buf *bp, *dp; 26126Sbill register int i; 26210336Smckusick int base, residual; 26326Sbill 2642322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2652322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2662322Swnj dp->b_flags = B_HEAD; 2672322Swnj } 26810336Smckusick base = bufpages / nbuf; 26910336Smckusick residual = bufpages % nbuf; 2706570Smckusic for (i = 0; i < nbuf; i++) { 27126Sbill bp = &buf[i]; 27226Sbill bp->b_dev = NODEV; 2739156Ssam bp->b_bcount = 0; 2746570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 27510336Smckusick if (i < residual) 27610336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 27710336Smckusick else 27810336Smckusick bp->b_bufsize = base * CLBYTES; 2799750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2802322Swnj bp->b_flags = B_BUSY|B_INVAL; 28126Sbill brelse(bp); 28226Sbill } 28330531Skarels } 28430531Skarels 28530531Skarels /* 28630531Skarels * Set up swap devices. 28730531Skarels * Initialize linked list of free swap 28830531Skarels * headers. These do not actually point 28930531Skarels * to buffers, but rather to pages that 29030531Skarels * are being swapped in and out. 29130531Skarels */ 29230531Skarels swapinit() 29330531Skarels { 29430531Skarels register int i; 29530531Skarels register struct buf *sp = swbuf; 29630531Skarels struct swdevt *swp; 29737611Smckusick int error; 29830531Skarels 299306Sbill /* 300306Sbill * Count swap devices, and adjust total swap space available. 30130531Skarels * Some of this space will not be available until a swapon() 302306Sbill * system is issued, usually when the system goes multi-user. 303306Sbill */ 304306Sbill nswdev = 0; 30512489Ssam nswap = 0; 30612489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 307306Sbill nswdev++; 30812489Ssam if (swp->sw_nblks > nswap) 30912489Ssam nswap = swp->sw_nblks; 31012489Ssam } 311306Sbill if (nswdev == 0) 31230531Skarels panic("swapinit"); 3134132Secc if (nswdev > 1) 31412489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 315306Sbill nswap *= nswdev; 31623534Skarels /* 31723534Skarels * If there are multiple swap areas, 31823534Skarels * allow more paging operations per second. 31923534Skarels */ 32023534Skarels if (nswdev > 1) 32123534Skarels maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 32237610Smckusick if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 32337610Smckusick panic("swapvp"); 32437611Smckusick if (error = swfree(0)) { 32537611Smckusick printf("swfree errno %d\n", error); /* XXX */ 32637611Smckusick panic("swapinit swfree 0"); 32737611Smckusick } 32826Sbill 32930531Skarels /* 33030531Skarels * Now set up swap buffer headers. 33130531Skarels */ 3322753Swnj bswlist.av_forw = sp; 3332769Swnj for (i=0; i<nswbuf-1; i++, sp++) 3342753Swnj sp->av_forw = sp+1; 3352753Swnj sp->av_forw = NULL; 33626Sbill } 3372746Swnj 3382746Swnj /* 3392746Swnj * Initialize clist by freeing all character blocks, then count 3402746Swnj * number of character devices. (Once-only routine) 3412746Swnj */ 3422746Swnj cinit() 3432746Swnj { 3442746Swnj register int ccp; 3452746Swnj register struct cblock *cp; 3462746Swnj 3472746Swnj ccp = (int)cfree; 3482746Swnj ccp = (ccp+CROUND) & ~CROUND; 3492746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 3502746Swnj cp->c_next = cfreelist; 3512746Swnj cfreelist = cp; 3522746Swnj cfreecount += CBSIZE; 3532746Swnj } 3542746Swnj } 355