1*7724Swnj /* kern_physio.c 4.31 82/08/13 */ 28Sbill 38Sbill #include "../h/param.h" 48Sbill #include "../h/systm.h" 58Sbill #include "../h/dir.h" 68Sbill #include "../h/user.h" 78Sbill #include "../h/buf.h" 88Sbill #include "../h/conf.h" 98Sbill #include "../h/proc.h" 108Sbill #include "../h/seg.h" 118Sbill #include "../h/pte.h" 128Sbill #include "../h/vm.h" 132045Swnj #include "../h/trace.h" 14*7724Swnj #include "../h/uio.h" 158Sbill 1691Sbill /* 178Sbill * Swap IO headers - 188Sbill * They contain the necessary information for the swap I/O. 198Sbill * At any given time, a swap header can be in three 208Sbill * different lists. When free it is in the free list, 218Sbill * when allocated and the I/O queued, it is on the swap 228Sbill * device list, and finally, if the operation was a dirty 238Sbill * page push, when the I/O completes, it is inserted 248Sbill * in a list of cleaned pages to be processed by the pageout daemon. 258Sbill */ 262771Swnj struct buf *swbuf; 272771Swnj short *swsize; /* CAN WE JUST USE B_BCOUNT? */ 282771Swnj int *swpf; 298Sbill 308Sbill /* 318Sbill * swap I/O - 328Sbill * 338Sbill * If the flag indicates a dirty page push initiated 348Sbill * by the pageout daemon, we map the page into the i th 358Sbill * virtual page of process 2 (the daemon itself) where i is 368Sbill * the index of the swap header that has been allocated. 378Sbill * We simply initialize the header and queue the I/O but 388Sbill * do not wait for completion. When the I/O completes, 398Sbill * iodone() will link the header to a list of cleaned 408Sbill * pages to be processed by the pageout daemon. 418Sbill */ 428Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent) 438Sbill struct proc *p; 448Sbill swblk_t dblkno; 458Sbill caddr_t addr; 468Sbill int flag, nbytes; 478Sbill dev_t dev; 488Sbill unsigned pfcent; 498Sbill { 508Sbill register struct buf *bp; 518Sbill register int c; 528Sbill int p2dp; 538Sbill register struct pte *dpte, *vpte; 545431Sroot int s; 558Sbill 565431Sroot s = spl6(); 578Sbill while (bswlist.av_forw == NULL) { 588Sbill bswlist.b_flags |= B_WANTED; 598Sbill sleep((caddr_t)&bswlist, PSWP+1); 608Sbill } 618Sbill bp = bswlist.av_forw; 628Sbill bswlist.av_forw = bp->av_forw; 635431Sroot splx(s); 648Sbill 658Sbill bp->b_flags = B_BUSY | B_PHYS | rdflg | flag; 668Sbill if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0) 678Sbill if (rdflg == B_READ) 688Sbill sum.v_pswpin += btoc(nbytes); 698Sbill else 708Sbill sum.v_pswpout += btoc(nbytes); 718Sbill bp->b_proc = p; 728Sbill if (flag & B_DIRTY) { 738Sbill p2dp = ((bp - swbuf) * CLSIZE) * KLMAX; 748Sbill dpte = dptopte(&proc[2], p2dp); 758Sbill vpte = vtopte(p, btop(addr)); 768Sbill for (c = 0; c < nbytes; c += NBPG) { 778Sbill if (vpte->pg_pfnum == 0 || vpte->pg_fod) 788Sbill panic("swap bad pte"); 798Sbill *dpte++ = *vpte++; 808Sbill } 818Sbill bp->b_un.b_addr = (caddr_t)ctob(p2dp); 828Sbill } else 838Sbill bp->b_un.b_addr = addr; 848Sbill while (nbytes > 0) { 858Sbill c = imin(ctob(120), nbytes); 868Sbill bp->b_bcount = c; 878Sbill bp->b_blkno = dblkno; 888Sbill bp->b_dev = dev; 89718Sbill if (flag & B_DIRTY) { 90718Sbill swpf[bp - swbuf] = pfcent; 91718Sbill swsize[bp - swbuf] = nbytes; 92718Sbill } 934033Swnj #ifdef TRACE 944033Swnj trace(TR_SWAPIO, dev, bp->b_blkno); 954033Swnj #endif 968Sbill (*bdevsw[major(dev)].d_strategy)(bp); 978Sbill if (flag & B_DIRTY) { 988Sbill if (c < nbytes) 998Sbill panic("big push"); 1008Sbill return; 1018Sbill } 1025431Sroot s = spl6(); 1038Sbill while((bp->b_flags&B_DONE)==0) 1048Sbill sleep((caddr_t)bp, PSWP); 1055431Sroot splx(s); 1068Sbill bp->b_un.b_addr += c; 1078Sbill bp->b_flags &= ~B_DONE; 1088Sbill if (bp->b_flags & B_ERROR) { 1098Sbill if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 1108Sbill panic("hard IO err in swap"); 1118Sbill swkill(p, (char *)0); 1128Sbill } 1138Sbill nbytes -= c; 1148Sbill dblkno += btoc(c); 1158Sbill } 1165431Sroot s = spl6(); 1178Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 1188Sbill bp->av_forw = bswlist.av_forw; 1198Sbill bswlist.av_forw = bp; 1208Sbill if (bswlist.b_flags & B_WANTED) { 1218Sbill bswlist.b_flags &= ~B_WANTED; 1228Sbill wakeup((caddr_t)&bswlist); 1238Sbill wakeup((caddr_t)&proc[2]); 1248Sbill } 1255431Sroot splx(s); 1268Sbill } 1278Sbill 1288Sbill /* 1298Sbill * If rout == 0 then killed on swap error, else 1308Sbill * rout is the name of the routine where we ran out of 1318Sbill * swap space. 1328Sbill */ 1338Sbill swkill(p, rout) 1348Sbill struct proc *p; 1358Sbill char *rout; 1368Sbill { 1372922Swnj char *mesg; 1388Sbill 1392922Swnj printf("pid %d: ", p->p_pid); 1408Sbill if (rout) 1412922Swnj printf(mesg = "killed due to no swap space\n"); 1428Sbill else 1432922Swnj printf(mesg = "killed on swap error\n"); 1442922Swnj uprintf("sorry, pid %d was %s", p->p_pid, mesg); 1458Sbill /* 1468Sbill * To be sure no looping (e.g. in vmsched trying to 1478Sbill * swap out) mark process locked in core (as though 1488Sbill * done by user) after killing it so noone will try 1498Sbill * to swap it out. 1508Sbill */ 151165Sbill psignal(p, SIGKILL); 1528Sbill p->p_flag |= SULOCK; 1538Sbill } 1548Sbill 1558Sbill /* 1568Sbill * Raw I/O. The arguments are 1578Sbill * The strategy routine for the device 1588Sbill * A buffer, which will always be a special buffer 1598Sbill * header owned exclusively by the device for this purpose 1608Sbill * The device number 1618Sbill * Read/write flag 1628Sbill * Essentially all the work is computing physical addresses and 1638Sbill * validating them. 1648Sbill * If the user has the proper access privilidges, the process is 1658Sbill * marked 'delayed unlock' and the pages involved in the I/O are 1668Sbill * faulted and locked. After the completion of the I/O, the above pages 1678Sbill * are unlocked. 1688Sbill */ 169*7724Swnj physio(strat, bp, dev, rw, mincnt, uio) 170*7724Swnj int (*strat)(); 171*7724Swnj register struct buf *bp; 172*7724Swnj dev_t dev; 173*7724Swnj int rw; 174*7724Swnj unsigned (*mincnt)(); 175*7724Swnj struct uio *uio; 1768Sbill { 1778Sbill register int c; 178*7724Swnj struct uio auio; 179*7724Swnj register struct iovec *iov; 180*7724Swnj struct iovec aiov; 1818Sbill char *a; 182*7724Swnj int s, error = 0; 1838Sbill 184*7724Swnj if (uio == 0) { 185*7724Swnj uio = &auio; 186*7724Swnj uio->uio_iov = &aiov; 187*7724Swnj uio->uio_iovcnt = 1; 188*7724Swnj uio->uio_offset = u.u_offset; 189*7724Swnj uio->uio_segflg = u.u_segflg; 190*7724Swnj iov = &aiov; 191*7724Swnj iov->iov_base = u.u_base; 192*7724Swnj iov->iov_len = u.u_count; 193*7724Swnj uio->uio_resid = iov->iov_len; 194*7724Swnj } else 195*7724Swnj iov = uio->uio_iov; 196*7724Swnj nextiov: 197*7724Swnj if (uio->uio_iovcnt == 0) { 198*7724Swnj u.u_count = uio->uio_resid; 199*7724Swnj return (0); 200*7724Swnj } 201*7724Swnj if (useracc(iov->iov_base,iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL) { 202*7724Swnj u.u_count = uio->uio_resid; 2038Sbill u.u_error = EFAULT; 204*7724Swnj return (EFAULT); 2058Sbill } 2065431Sroot s = spl6(); 2078Sbill while (bp->b_flags&B_BUSY) { 2088Sbill bp->b_flags |= B_WANTED; 2098Sbill sleep((caddr_t)bp, PRIBIO+1); 2108Sbill } 2116319Swnj splx(s); 2128Sbill bp->b_error = 0; 2138Sbill bp->b_proc = u.u_procp; 214*7724Swnj bp->b_un.b_addr = iov->iov_base; 215*7724Swnj while (iov->iov_len > 0) { 2168Sbill bp->b_flags = B_BUSY | B_PHYS | rw; 2178Sbill bp->b_dev = dev; 218*7724Swnj bp->b_blkno = uio->uio_offset >> PGSHIFT; 219*7724Swnj bp->b_bcount = iov->iov_len; 2208Sbill (*mincnt)(bp); 2218Sbill c = bp->b_bcount; 2228Sbill u.u_procp->p_flag |= SPHYSIO; 2238Sbill vslock(a = bp->b_un.b_addr, c); 2248Sbill (*strat)(bp); 225124Sbill (void) spl6(); 2268Sbill while ((bp->b_flags&B_DONE) == 0) 2278Sbill sleep((caddr_t)bp, PRIBIO); 2288Sbill vsunlock(a, c, rw); 2298Sbill u.u_procp->p_flag &= ~SPHYSIO; 2308Sbill if (bp->b_flags&B_WANTED) 2318Sbill wakeup((caddr_t)bp); 2325431Sroot splx(s); 233*7724Swnj c -= bp->b_resid; 2348Sbill bp->b_un.b_addr += c; 235*7724Swnj iov->iov_len -= c; 236*7724Swnj uio->uio_resid -= c; 237*7724Swnj uio->uio_offset += c; 2383667Swnj if (bp->b_flags&B_ERROR) 2393667Swnj break; 2408Sbill } 2418Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS); 242*7724Swnj error = geterror(bp); 243*7724Swnj if (error) { 244*7724Swnj u.u_error = error; 245*7724Swnj u.u_count = uio->uio_resid; 246*7724Swnj return (error); 247*7724Swnj } 248*7724Swnj uio->uio_iov++; 249*7724Swnj uio->uio_iovcnt--; 250*7724Swnj goto nextiov; 2518Sbill } 2528Sbill 2538Sbill /*ARGSUSED*/ 2548Sbill unsigned 2558Sbill minphys(bp) 256*7724Swnj struct buf *bp; 2578Sbill { 2588Sbill 2596379Swnj if (bp->b_bcount > 63 * 1024) 2606379Swnj bp->b_bcount = 63 * 1024; 2618Sbill } 2628Sbill 263