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.23 (Berkeley) 04/27/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 #include "loop.h" 59 startup(firstaddr); 60 61 /* 62 * set up system process 0 (swapper) 63 */ 64 p = &proc[0]; 65 bcopy("swapper", p->p_comm, sizeof ("swapper")); 66 p->p_p0br = u.u_pcb.pcb_p0br; 67 p->p_szpt = 1; 68 p->p_addr = uaddr(p); 69 p->p_stat = SRUN; 70 p->p_flag |= SLOAD|SSYS; 71 p->p_nice = NZERO; 72 setredzone(p->p_addr, (caddr_t)&u); 73 u.u_procp = p; 74 MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp), 75 M_PGRP, M_NOWAIT); 76 if ((pg = pgrphash[0]) == NULL) 77 panic("no space to craft zero'th process group"); 78 pg->pg_id = 0; 79 pg->pg_hforw = 0; 80 pg->pg_mem = p; 81 pg->pg_jobc = 0; 82 p->p_pgrp = pg; 83 p->p_pgrpnxt = 0; 84 MALLOC(pg->pg_session, struct session *, sizeof (struct session), 85 M_SESSION, M_NOWAIT); 86 if (pg->pg_session == NULL) 87 panic("no space to craft zero'th session"); 88 pg->pg_session->s_count = 1; 89 pg->pg_session->s_leader = NULL; 90 pg->pg_session->s_ttyvp = NULL; 91 pg->pg_session->s_ttyp = NULL; 92 #ifdef KTRACE 93 p->p_tracep = NULL; 94 p->p_traceflag = 0; 95 #endif 96 /* 97 * These assume that the u. area is always mapped 98 * to the same virtual address. Otherwise must be 99 * handled when copying the u. area in newproc(). 100 */ 101 ndinit(&u.u_nd); 102 u.u_ap = u.u_arg; 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_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 #include "sl.h" 146 #if NSL > 0 147 slattach(); /* XXX */ 148 #endif 149 #if NLOOP > 0 150 loattach(); /* XXX */ 151 #endif 152 /* 153 * Block reception of incoming packets 154 * until protocols have been initialized. 155 */ 156 s = splimp(); 157 ifinit(); 158 domaininit(); 159 splx(s); 160 pqinit(); 161 xinit(); 162 swapinit(); 163 #ifdef GPROF 164 kmstartup(); 165 #endif 166 167 /* kick off timeout driven events by calling first time */ 168 roundrobin(); 169 schedcpu(); 170 schedpaging(); 171 172 /* set up the root file system */ 173 if ((*mountroot)()) 174 panic("cannot mount root"); 175 /* 176 * Get vnode for '/'. 177 * Setup rootdir and u.u_cdir to point to it. 178 */ 179 if (VFS_ROOT(rootfs, &rootdir)) 180 panic("cannot find root vnode"); 181 u.u_cdir = rootdir; 182 VREF(u.u_cdir); 183 VOP_UNLOCK(rootdir); 184 u.u_rdir = NULL; 185 boottime = time; 186 187 u.u_dmap = zdmap; 188 u.u_smap = zdmap; 189 190 enablertclock(); /* enable realtime clock interrupts */ 191 /* 192 * make init process 193 */ 194 195 siginit(&proc[0]); 196 proc[0].p_szpt = CLSIZE; 197 if (newproc(0)) { 198 expand(clrnd((int)btoc(szicode)), 0); 199 (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap); 200 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 201 /* 202 * Return goes to loc. 0 of user init 203 * code just copied out. 204 */ 205 return; 206 } 207 /* 208 * make page-out daemon (process 2) 209 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 210 * table so that it can map dirty pages into 211 * its address space during asychronous pushes. 212 */ 213 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 214 if (newproc(0)) { 215 proc[2].p_flag |= SLOAD|SSYS; 216 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 217 bcopy("pagedaemon", proc[2].p_comm, sizeof ("pagedaemon")); 218 pageout(); 219 /*NOTREACHED*/ 220 } 221 222 /* 223 * enter scheduling loop 224 */ 225 proc[0].p_szpt = 1; 226 sched(); 227 } 228 229 /* 230 * Initialize hash links for buffers. 231 */ 232 bhinit() 233 { 234 register int i; 235 register struct bufhd *bp; 236 237 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 238 bp->b_forw = bp->b_back = (struct buf *)bp; 239 } 240 241 /* 242 * Initialize the buffer I/O system by freeing 243 * all buffers and setting all device buffer lists to empty. 244 */ 245 binit() 246 { 247 register struct buf *bp, *dp; 248 register int i; 249 int base, residual; 250 251 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 252 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 253 dp->b_flags = B_HEAD; 254 } 255 base = bufpages / nbuf; 256 residual = bufpages % nbuf; 257 for (i = 0; i < nbuf; i++) { 258 bp = &buf[i]; 259 bp->b_dev = NODEV; 260 bp->b_bcount = 0; 261 bp->b_rcred = NOCRED; 262 bp->b_wcred = NOCRED; 263 bp->b_dirtyoff = 0; 264 bp->b_dirtyend = 0; 265 bp->b_un.b_addr = buffers + i * MAXBSIZE; 266 if (i < residual) 267 bp->b_bufsize = (base + 1) * CLBYTES; 268 else 269 bp->b_bufsize = base * CLBYTES; 270 binshash(bp, &bfreelist[BQ_AGE]); 271 bp->b_flags = B_BUSY|B_INVAL; 272 brelse(bp); 273 } 274 } 275 276 /* 277 * Set up swap devices. 278 * Initialize linked list of free swap 279 * headers. These do not actually point 280 * to buffers, but rather to pages that 281 * are being swapped in and out. 282 */ 283 swapinit() 284 { 285 register int i; 286 register struct buf *sp = swbuf; 287 struct swdevt *swp; 288 int error; 289 290 /* 291 * Count swap devices, and adjust total swap space available. 292 * Some of this space will not be available until a swapon() 293 * system is issued, usually when the system goes multi-user. 294 */ 295 nswdev = 0; 296 nswap = 0; 297 for (swp = swdevt; swp->sw_dev; swp++) { 298 nswdev++; 299 if (swp->sw_nblks > nswap) 300 nswap = swp->sw_nblks; 301 } 302 if (nswdev == 0) 303 panic("swapinit"); 304 if (nswdev > 1) 305 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 306 nswap *= nswdev; 307 /* 308 * If there are multiple swap areas, 309 * allow more paging operations per second. 310 */ 311 if (nswdev > 1) 312 maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 313 if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 314 panic("swapvp"); 315 if (error = swfree(0)) { 316 printf("swfree errno %d\n", error); /* XXX */ 317 panic("swapinit swfree 0"); 318 } 319 320 /* 321 * Now set up swap buffer headers. 322 */ 323 bswlist.av_forw = sp; 324 for (i=0; i<nswbuf-1; i++, sp++) 325 sp->av_forw = sp+1; 326 sp->av_forw = NULL; 327 } 328 329 /* 330 * Initialize clist by freeing all character blocks, then count 331 * number of character devices. (Once-only routine) 332 */ 333 cinit() 334 { 335 register int ccp; 336 register struct cblock *cp; 337 338 ccp = (int)cfree; 339 ccp = (ccp+CROUND) & ~CROUND; 340 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 341 cp->c_next = cfreelist; 342 cfreelist = cp; 343 cfreecount += CBSIZE; 344 } 345 } 346