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