1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)init_main.c 7.5.1.1 (Berkeley) 05/01/89 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "user.h" 23 #include "kernel.h" 24 #include "mount.h" 25 #include "map.h" 26 #include "proc.h" 27 #include "vnode.h" 28 #include "seg.h" 29 #include "conf.h" 30 #include "buf.h" 31 #include "vm.h" 32 #include "cmap.h" 33 #include "text.h" 34 #include "clist.h" 35 #include "protosw.h" 36 #include "reboot.h" 37 #include "../ufs/quota.h" 38 39 #include "machine/pte.h" 40 #include "machine/reg.h" 41 #include "machine/cpu.h" 42 43 int cmask = CMASK; 44 extern int (*mountroot)(); 45 /* 46 * Initialization code. 47 * Called from cold start routine as 48 * soon as a stack and segmentation 49 * have been established. 50 * Functions: 51 * clear and free user core 52 * turn on clock 53 * hand craft 0th process 54 * call all initialization routines 55 * fork - process 0 to schedule 56 * - process 1 execute bootstrap 57 * - process 2 to page out 58 */ 59 main(firstaddr) 60 int firstaddr; 61 { 62 register int i; 63 register struct proc *p; 64 struct fs *fs; 65 int s; 66 67 rqinit(); 68 #include "loop.h" 69 startup(firstaddr); 70 71 /* 72 * set up system process 0 (swapper) 73 */ 74 p = &proc[0]; 75 p->p_p0br = u.u_pcb.pcb_p0br; 76 p->p_szpt = 1; 77 p->p_addr = uaddr(p); 78 p->p_stat = SRUN; 79 p->p_flag |= SLOAD|SSYS; 80 p->p_nice = NZERO; 81 setredzone(p->p_addr, (caddr_t)&u); 82 u.u_procp = p; 83 /* 84 * These assume that the u. area is always mapped 85 * to the same virtual address. Otherwise must be 86 * handled when copying the u. area in newproc(). 87 */ 88 u.u_nd.ni_iov = &u.u_nd.ni_nd.nd_iovec; 89 u.u_ap = u.u_arg; 90 u.u_nd.ni_iovcnt = 1; 91 92 u.u_cmask = cmask; 93 u.u_lastfile = -1; 94 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 95 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 96 RLIM_INFINITY; 97 /* 98 * configure virtual memory system, 99 * set vm rlimits 100 */ 101 vminit(); 102 103 /* 104 * Get vnodes for swapdev, argdev, and rootdev. 105 */ 106 ihinit(); 107 nchinit(); 108 if (bdevvp(swapdev, &swapdev_vp) || 109 bdevvp(argdev, &argdev_vp) || 110 bdevvp(rootdev, &rootvp)) 111 panic("can't setup bdevvp's"); 112 113 /* 114 * Setup credentials 115 */ 116 u.u_cred = crget(); 117 u.u_ngroups = 1; 118 119 #if defined(QUOTA) 120 qtinit(); 121 p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 122 #endif 123 startrtclock(); 124 #if defined(vax) 125 #include "kg.h" 126 #if NKG > 0 127 startkgclock(); 128 #endif 129 #endif 130 131 /* 132 * Initialize tables, protocols, and set up well-known inodes. 133 */ 134 mbinit(); 135 cinit(); 136 #include "sl.h" 137 #if NSL > 0 138 slattach(); /* XXX */ 139 #endif 140 #if NLOOP > 0 141 loattach(); /* XXX */ 142 #endif 143 /* 144 * Block reception of incoming packets 145 * until protocols have been initialized. 146 */ 147 s = splimp(); 148 ifinit(); 149 domaininit(); 150 splx(s); 151 pqinit(); 152 xinit(); 153 swapinit(); 154 #ifdef GPROF 155 kmstartup(); 156 #endif 157 #ifdef NFS 158 nfsinit(); 159 #endif 160 161 /* kick off timeout driven events by calling first time */ 162 roundrobin(); 163 schedcpu(); 164 schedpaging(); 165 166 /* set up the root file system */ 167 if ((*mountroot)()) 168 panic("cannot mount root"); 169 /* 170 * Get vnode for '/'. 171 * Setup rootdir and u.u_cdir to point to it. 172 */ 173 if (VFS_ROOT(rootfs, &rootdir)) 174 panic("cannot find root vnode"); 175 u.u_cdir = rootdir; 176 u.u_cdir->v_count++; 177 vop_unlock(rootdir); 178 u.u_rdir = NULL; 179 boottime = time; 180 181 u.u_dmap = zdmap; 182 u.u_smap = zdmap; 183 184 enablertclock(); /* enable realtime clock interrupts */ 185 /* 186 * make init process 187 */ 188 189 proc[0].p_szpt = CLSIZE; 190 if (newproc(0)) { 191 expand(clrnd((int)btoc(szicode)), 0); 192 (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap); 193 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 194 /* 195 * Return goes to loc. 0 of user init 196 * code just copied out. 197 */ 198 return; 199 } 200 /* 201 * make page-out daemon (process 2) 202 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 203 * table so that it can map dirty pages into 204 * its address space during asychronous pushes. 205 */ 206 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 207 if (newproc(0)) { 208 proc[2].p_flag |= SLOAD|SSYS; 209 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 210 pageout(); 211 /*NOTREACHED*/ 212 } 213 214 /* 215 * enter scheduling loop 216 */ 217 proc[0].p_szpt = 1; 218 sched(); 219 } 220 221 /* 222 * Initialize hash links for buffers. 223 */ 224 bhinit() 225 { 226 register int i; 227 register struct bufhd *bp; 228 229 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 230 bp->b_forw = bp->b_back = (struct buf *)bp; 231 } 232 233 /* 234 * Initialize the buffer I/O system by freeing 235 * all buffers and setting all device buffer lists to empty. 236 */ 237 binit() 238 { 239 register struct buf *bp, *dp; 240 register int i; 241 int base, residual; 242 243 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 244 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 245 dp->b_flags = B_HEAD; 246 } 247 base = bufpages / nbuf; 248 residual = bufpages % nbuf; 249 for (i = 0; i < nbuf; i++) { 250 bp = &buf[i]; 251 bp->b_dev = NODEV; 252 bp->b_bcount = 0; 253 bp->b_un.b_addr = buffers + i * MAXBSIZE; 254 if (i < residual) 255 bp->b_bufsize = (base + 1) * CLBYTES; 256 else 257 bp->b_bufsize = base * CLBYTES; 258 binshash(bp, &bfreelist[BQ_AGE]); 259 bp->b_flags = B_BUSY|B_INVAL; 260 brelse(bp); 261 } 262 } 263 264 /* 265 * Set up swap devices. 266 * Initialize linked list of free swap 267 * headers. These do not actually point 268 * to buffers, but rather to pages that 269 * are being swapped in and out. 270 */ 271 swapinit() 272 { 273 register int i; 274 register struct buf *sp = swbuf; 275 struct swdevt *swp; 276 277 /* 278 * Count swap devices, and adjust total swap space available. 279 * Some of this space will not be available until a swapon() 280 * system is issued, usually when the system goes multi-user. 281 */ 282 nswdev = 0; 283 nswap = 0; 284 for (swp = swdevt; swp->sw_dev; swp++) { 285 nswdev++; 286 if (swp->sw_nblks > nswap) 287 nswap = swp->sw_nblks; 288 } 289 if (nswdev == 0) 290 panic("swapinit"); 291 if (nswdev > 1) 292 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 293 nswap *= nswdev; 294 /* 295 * If there are multiple swap areas, 296 * allow more paging operations per second. 297 */ 298 if (nswdev > 1) 299 maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 300 if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 301 panic("swapvp"); 302 swfree(0); 303 304 /* 305 * Now set up swap buffer headers. 306 */ 307 bswlist.av_forw = sp; 308 for (i=0; i<nswbuf-1; i++, sp++) 309 sp->av_forw = sp+1; 310 sp->av_forw = NULL; 311 } 312 313 /* 314 * Initialize clist by freeing all character blocks, then count 315 * number of character devices. (Once-only routine) 316 */ 317 cinit() 318 { 319 register int ccp; 320 register struct cblock *cp; 321 322 ccp = (int)cfree; 323 ccp = (ccp+CROUND) & ~CROUND; 324 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 325 cp->c_next = cfreelist; 326 cfreelist = cp; 327 cfreecount += CBSIZE; 328 } 329 } 330