| 1 | /* init_main.c 4.28 82/03/28 */ |
| 2 | |
| 3 | #include "../h/param.h" |
| 4 | #include "../h/systm.h" |
| 5 | #include "../h/dir.h" |
| 6 | #include "../h/user.h" |
| 7 | #include "../h/filsys.h" |
| 8 | #include "../h/mount.h" |
| 9 | #include "../h/map.h" |
| 10 | #include "../h/proc.h" |
| 11 | #include "../h/inode.h" |
| 12 | #include "../h/seg.h" |
| 13 | #include "../h/conf.h" |
| 14 | #include "../h/buf.h" |
| 15 | #include "../h/mtpr.h" |
| 16 | #include "../h/pte.h" |
| 17 | #include "../h/clock.h" |
| 18 | #include "../h/vm.h" |
| 19 | #include "../h/cmap.h" |
| 20 | #include "../h/text.h" |
| 21 | #include "../h/vlimit.h" |
| 22 | #include "../h/clist.h" |
| 23 | #ifdef INET |
| 24 | #include "../h/protosw.h" |
| 25 | #endif |
| 26 | |
| 27 | /* |
| 28 | * Initialization code. |
| 29 | * Called from cold start routine as |
| 30 | * soon as a stack and segmentation |
| 31 | * have been established. |
| 32 | * Functions: |
| 33 | * clear and free user core |
| 34 | * turn on clock |
| 35 | * hand craft 0th process |
| 36 | * call all initialization routines |
| 37 | * fork - process 0 to schedule |
| 38 | * - process 2 to page out |
| 39 | * - process 1 execute bootstrap |
| 40 | * |
| 41 | * loop at loc 13 (0xd) in user mode -- /etc/init |
| 42 | * cannot be executed. |
| 43 | */ |
| 44 | main(firstaddr) |
| 45 | { |
| 46 | register int i; |
| 47 | register struct proc *p; |
| 48 | |
| 49 | rqinit(); |
| 50 | #include "loop.h" |
| 51 | startup(firstaddr); |
| 52 | |
| 53 | /* |
| 54 | * set up system process 0 (swapper) |
| 55 | */ |
| 56 | p = &proc[0]; |
| 57 | p->p_p0br = (struct pte *)mfpr(P0BR); |
| 58 | p->p_szpt = 1; |
| 59 | p->p_addr = uaddr(p); |
| 60 | p->p_stat = SRUN; |
| 61 | p->p_flag |= SLOAD|SSYS; |
| 62 | p->p_nice = NZERO; |
| 63 | setredzone(p->p_addr, (caddr_t)&u); |
| 64 | u.u_procp = p; |
| 65 | u.u_cmask = CMASK; |
| 66 | for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++) |
| 67 | switch (i) { |
| 68 | |
| 69 | case LIM_STACK: |
| 70 | u.u_limit[i] = 512*1024; |
| 71 | continue; |
| 72 | case LIM_DATA: |
| 73 | u.u_limit[i] = ctob(MAXDSIZ); |
| 74 | continue; |
| 75 | default: |
| 76 | u.u_limit[i] = INFINITY; |
| 77 | continue; |
| 78 | } |
| 79 | p->p_maxrss = INFINITY/NBPG; |
| 80 | clkstart(); |
| 81 | |
| 82 | /* |
| 83 | * Initialize tables, protocols, and set up well-known inodes. |
| 84 | */ |
| 85 | mbinit(); |
| 86 | cinit(); /* needed by dmc-11 driver */ |
| 87 | #ifdef INET |
| 88 | #if NLOOP > 0 |
| 89 | loattach(); /* XXX */ |
| 90 | #endif |
| 91 | ifinit(); |
| 92 | pfinit(); /* must follow interfaces */ |
| 93 | #endif |
| 94 | ihinit(); |
| 95 | bhinit(); |
| 96 | binit(); |
| 97 | bswinit(); |
| 98 | iinit(); |
| 99 | rootdir = iget(rootdev, (ino_t)ROOTINO); |
| 100 | rootdir->i_flag &= ~ILOCK; |
| 101 | u.u_cdir = iget(rootdev, (ino_t)ROOTINO); |
| 102 | u.u_cdir->i_flag &= ~ILOCK; |
| 103 | u.u_rdir = NULL; |
| 104 | u.u_dmap = zdmap; |
| 105 | u.u_smap = zdmap; |
| 106 | |
| 107 | /* |
| 108 | * Set the scan rate and other parameters of the paging subsystem. |
| 109 | */ |
| 110 | setupclock(); |
| 111 | |
| 112 | /* |
| 113 | * make page-out daemon (process 2) |
| 114 | * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page |
| 115 | * table so that it can map dirty pages into |
| 116 | * its address space during asychronous pushes. |
| 117 | */ |
| 118 | mpid = 1; |
| 119 | proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); |
| 120 | proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ |
| 121 | if (newproc(0)) { |
| 122 | proc[2].p_flag |= SLOAD|SSYS; |
| 123 | proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; |
| 124 | pageout(); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * make init process and |
| 129 | * enter scheduling loop |
| 130 | */ |
| 131 | |
| 132 | mpid = 0; |
| 133 | proc[1].p_stat = 0; |
| 134 | proc[0].p_szpt = CLSIZE; |
| 135 | if (newproc(0)) { |
| 136 | expand(clrnd((int)btoc(szicode)), P0BR); |
| 137 | (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); |
| 138 | (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); |
| 139 | /* |
| 140 | * Return goes to loc. 0 of user init |
| 141 | * code just copied out. |
| 142 | */ |
| 143 | return; |
| 144 | } |
| 145 | proc[0].p_szpt = 1; |
| 146 | sched(); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * iinit is called once (from main) |
| 151 | * very early in initialization. |
| 152 | * It reads the root's super block |
| 153 | * and initializes the current date |
| 154 | * from the last modified date. |
| 155 | * |
| 156 | * panic: iinit -- cannot read the super |
| 157 | * block. Usually because of an IO error. |
| 158 | */ |
| 159 | iinit() |
| 160 | { |
| 161 | register struct buf *bp; |
| 162 | register struct filsys *fp; |
| 163 | register int i; |
| 164 | |
| 165 | (*bdevsw[major(rootdev)].d_open)(rootdev, 1); |
| 166 | bp = bread(rootdev, SUPERB); |
| 167 | if(u.u_error) |
| 168 | panic("iinit"); |
| 169 | bp->b_flags |= B_LOCKED; /* block can never be re-used */ |
| 170 | brelse(bp); |
| 171 | mount[0].m_dev = rootdev; |
| 172 | mount[0].m_bufp = bp; |
| 173 | fp = bp->b_un.b_filsys; |
| 174 | fp->s_flock = 0; |
| 175 | fp->s_ilock = 0; |
| 176 | fp->s_ronly = 0; |
| 177 | fp->s_lasti = 1; |
| 178 | fp->s_nbehind = 0; |
| 179 | fp->s_fsmnt[0] = '/'; |
| 180 | for (i = 1; i < sizeof(fp->s_fsmnt); i++) |
| 181 | fp->s_fsmnt[i] = 0; |
| 182 | clkinit(fp->s_time); |
| 183 | bootime = time; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Initialize the buffer I/O system by freeing |
| 188 | * all buffers and setting all device buffer lists to empty. |
| 189 | */ |
| 190 | binit() |
| 191 | { |
| 192 | register struct buf *bp; |
| 193 | register struct buf *dp; |
| 194 | register int i; |
| 195 | struct bdevsw *bdp; |
| 196 | struct swdevt *swp; |
| 197 | |
| 198 | for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { |
| 199 | dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; |
| 200 | dp->b_flags = B_HEAD; |
| 201 | } |
| 202 | dp--; /* dp = &bfreelist[BQUEUES-1]; */ |
| 203 | for (i=0; i<nbuf; i++) { |
| 204 | bp = &buf[i]; |
| 205 | bp->b_dev = NODEV; |
| 206 | bp->b_un.b_addr = buffers + i * BSIZE; |
| 207 | bp->b_back = dp; |
| 208 | bp->b_forw = dp->b_forw; |
| 209 | dp->b_forw->b_back = bp; |
| 210 | dp->b_forw = bp; |
| 211 | bp->b_flags = B_BUSY|B_INVAL; |
| 212 | brelse(bp); |
| 213 | } |
| 214 | for (bdp = bdevsw; bdp->d_open; bdp++) |
| 215 | nblkdev++; |
| 216 | /* |
| 217 | * Count swap devices, and adjust total swap space available. |
| 218 | * Some of this space will not be available until a vswapon() |
| 219 | * system is issued, usually when the system goes multi-user. |
| 220 | */ |
| 221 | nswdev = 0; |
| 222 | for (swp = swdevt; swp->sw_dev; swp++) |
| 223 | nswdev++; |
| 224 | if (nswdev == 0) |
| 225 | panic("binit"); |
| 226 | if (nswdev > 1) |
| 227 | nswap = (nswap/DMMAX)*DMMAX; |
| 228 | nswap *= nswdev; |
| 229 | maxpgio *= nswdev; |
| 230 | swfree(0); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Initialize linked list of free swap |
| 235 | * headers. These do not actually point |
| 236 | * to buffers, but rather to pages that |
| 237 | * are being swapped in and out. |
| 238 | */ |
| 239 | bswinit() |
| 240 | { |
| 241 | register int i; |
| 242 | register struct buf *sp = swbuf; |
| 243 | |
| 244 | bswlist.av_forw = sp; |
| 245 | for (i=0; i<nswbuf-1; i++, sp++) |
| 246 | sp->av_forw = sp+1; |
| 247 | sp->av_forw = NULL; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Initialize clist by freeing all character blocks, then count |
| 252 | * number of character devices. (Once-only routine) |
| 253 | */ |
| 254 | cinit() |
| 255 | { |
| 256 | register int ccp; |
| 257 | register struct cblock *cp; |
| 258 | register struct cdevsw *cdp; |
| 259 | |
| 260 | ccp = (int)cfree; |
| 261 | ccp = (ccp+CROUND) & ~CROUND; |
| 262 | for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { |
| 263 | cp->c_next = cfreelist; |
| 264 | cfreelist = cp; |
| 265 | cfreecount += CBSIZE; |
| 266 | } |
| 267 | ccp = 0; |
| 268 | for(cdp = cdevsw; cdp->d_open; cdp++) |
| 269 | ccp++; |
| 270 | nchrdev = ccp; |
| 271 | } |