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