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.28 (Berkeley) 05/24/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 103 u.u_cmask = cmask; 104 u.u_lastfile = -1; 105 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 106 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 107 RLIM_INFINITY; 108 /* 109 * configure virtual memory system, 110 * set vm rlimits 111 */ 112 vminit(); 113 114 /* 115 * Initialize the file systems. 116 * 117 * Get vnodes for swapdev, argdev, and rootdev. 118 */ 119 vfsinit(); 120 if (bdevvp(swapdev, &swapdev_vp) || 121 bdevvp(argdev, &argdev_vp) || 122 bdevvp(rootdev, &rootvp)) 123 panic("can't setup bdevvp's"); 124 125 /* 126 * Setup credentials 127 */ 128 u.u_cred = crget(); 129 u.u_cred->cr_ngroups = 1; 130 131 startrtclock(); 132 #if defined(vax) 133 #include "kg.h" 134 #if NKG > 0 135 startkgclock(); 136 #endif 137 #endif 138 139 /* 140 * Initialize tables, protocols, and set up well-known inodes. 141 */ 142 mbinit(); 143 cinit(); 144 #ifdef SYSVSHM 145 shminit(); 146 #endif 147 #include "sl.h" 148 #if NSL > 0 149 slattach(); /* XXX */ 150 #endif 151 #include "loop.h" 152 #if NLOOP > 0 153 loattach(); /* XXX */ 154 #endif 155 /* 156 * Block reception of incoming packets 157 * until protocols have been initialized. 158 */ 159 s = splimp(); 160 ifinit(); 161 domaininit(); 162 splx(s); 163 pqinit(); 164 xinit(); 165 swapinit(); 166 #ifdef GPROF 167 kmstartup(); 168 #endif 169 170 /* kick off timeout driven events by calling first time */ 171 roundrobin(); 172 schedcpu(); 173 schedpaging(); 174 175 /* set up the root file system */ 176 if ((*mountroot)()) 177 panic("cannot mount root"); 178 /* 179 * Get vnode for '/'. 180 * Setup rootdir and u.u_cdir to point to it. 181 */ 182 if (VFS_ROOT(rootfs, &rootdir)) 183 panic("cannot find root vnode"); 184 u.u_cdir = rootdir; 185 VREF(u.u_cdir); 186 VOP_UNLOCK(rootdir); 187 u.u_rdir = NULL; 188 boottime = u.u_start = time; 189 u.u_dmap = zdmap; 190 u.u_smap = zdmap; 191 192 enablertclock(); /* enable realtime clock interrupts */ 193 /* 194 * make init process 195 */ 196 197 siginit(&proc[0]); 198 proc[0].p_szpt = CLSIZE; 199 if (newproc(0)) { 200 expand(clrnd((int)btoc(szicode)), 0); 201 (void) swpexpand(u.u_dsize, (segsz_t)0, &u.u_dmap, &u.u_smap); 202 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 203 /* 204 * Return goes to loc. 0 of user init 205 * code just copied out. 206 */ 207 return; 208 } 209 /* 210 * make page-out daemon (process 2) 211 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 212 * table so that it can map dirty pages into 213 * its address space during asychronous pushes. 214 */ 215 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + HIGHPAGES)); 216 if (newproc(0)) { 217 proc[2].p_flag |= SLOAD|SSYS; 218 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 219 bcopy("pagedaemon", proc[2].p_comm, sizeof ("pagedaemon")); 220 pageout(); 221 /*NOTREACHED*/ 222 } 223 224 /* 225 * enter scheduling loop 226 */ 227 proc[0].p_szpt = 1; 228 sched(); 229 } 230 231 /* 232 * Initialize hash links for buffers. 233 */ 234 bhinit() 235 { 236 register int i; 237 register struct bufhd *bp; 238 239 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 240 bp->b_forw = bp->b_back = (struct buf *)bp; 241 } 242 243 /* 244 * Initialize the buffer I/O system by freeing 245 * all buffers and setting all device buffer lists to empty. 246 */ 247 binit() 248 { 249 register struct buf *bp, *dp; 250 register int i; 251 int base, residual; 252 253 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 254 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 255 dp->b_flags = B_HEAD; 256 } 257 base = bufpages / nbuf; 258 residual = bufpages % nbuf; 259 for (i = 0; i < nbuf; i++) { 260 bp = &buf[i]; 261 bp->b_dev = NODEV; 262 bp->b_bcount = 0; 263 bp->b_rcred = NOCRED; 264 bp->b_wcred = NOCRED; 265 bp->b_dirtyoff = 0; 266 bp->b_dirtyend = 0; 267 bp->b_un.b_addr = buffers + i * MAXBSIZE; 268 if (i < residual) 269 bp->b_bufsize = (base + 1) * CLBYTES; 270 else 271 bp->b_bufsize = base * CLBYTES; 272 binshash(bp, &bfreelist[BQ_AGE]); 273 bp->b_flags = B_BUSY|B_INVAL; 274 brelse(bp); 275 } 276 } 277 278 /* 279 * Set up swap devices. 280 * Initialize linked list of free swap 281 * headers. These do not actually point 282 * to buffers, but rather to pages that 283 * are being swapped in and out. 284 */ 285 swapinit() 286 { 287 register int i; 288 register struct buf *sp = swbuf; 289 struct swdevt *swp; 290 int error; 291 292 /* 293 * Count swap devices, and adjust total swap space available. 294 * Some of this space will not be available until a swapon() 295 * system is issued, usually when the system goes multi-user. 296 */ 297 nswdev = 0; 298 nswap = 0; 299 for (swp = swdevt; swp->sw_dev; swp++) { 300 nswdev++; 301 if (swp->sw_nblks > nswap) 302 nswap = swp->sw_nblks; 303 } 304 if (nswdev == 0) 305 panic("swapinit"); 306 if (nswdev > 1) 307 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 308 nswap *= nswdev; 309 /* 310 * If there are multiple swap areas, 311 * allow more paging operations per second. 312 */ 313 if (nswdev > 1) 314 maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 315 if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 316 panic("swapvp"); 317 if (error = swfree(0)) { 318 printf("swfree errno %d\n", error); /* XXX */ 319 panic("swapinit swfree 0"); 320 } 321 322 /* 323 * Now set up swap buffer headers. 324 */ 325 bswlist.av_forw = sp; 326 for (i=0; i<nswbuf-1; i++, sp++) 327 sp->av_forw = sp+1; 328 sp->av_forw = NULL; 329 } 330 331 /* 332 * Initialize clist by freeing all character blocks, then count 333 * number of character devices. (Once-only routine) 334 */ 335 cinit() 336 { 337 register int ccp; 338 register struct cblock *cp; 339 340 ccp = (int)cfree; 341 ccp = (ccp+CROUND) & ~CROUND; 342 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 343 cp->c_next = cfreelist; 344 cfreelist = cp; 345 cfreecount += CBSIZE; 346 } 347 } 348