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