xref: /csrg-svn/sys/kern/kern_physio.c (revision 8962)
1*8962Sroot /*	kern_physio.c	4.35	82/10/31	*/
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"
147724Swnj #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;
468674S 	int nbytes, rdflg, flag;
478Sbill 	dev_t dev;
488674S 	u_int pfcent;
498Sbill {
508Sbill 	register struct buf *bp;
51*8962Sroot 	register u_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) {
85*8962Sroot 		bp->b_bcount = nbytes;
86*8962Sroot 		minphys(bp);
87*8962Sroot 		c = bp->b_bcount;
888Sbill 		bp->b_blkno = dblkno;
898Sbill 		bp->b_dev = dev;
90718Sbill 		if (flag & B_DIRTY) {
91718Sbill 			swpf[bp - swbuf] = pfcent;
92718Sbill 			swsize[bp - swbuf] = nbytes;
93718Sbill 		}
944033Swnj #ifdef TRACE
954033Swnj 		trace(TR_SWAPIO, dev, bp->b_blkno);
964033Swnj #endif
97*8962Sroot 		physstrat(bp, bdevsw[major(dev).d_strategy, PSWP);
988Sbill 		if (flag & B_DIRTY) {
998Sbill 			if (c < nbytes)
1008Sbill 				panic("big push");
1018Sbill 			return;
1028Sbill 		}
1038Sbill 		bp->b_un.b_addr += c;
1048Sbill 		bp->b_flags &= ~B_DONE;
1058Sbill 		if (bp->b_flags & B_ERROR) {
1068Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
1078Sbill 				panic("hard IO err in swap");
1088Sbill 			swkill(p, (char *)0);
1098Sbill 		}
1108Sbill 		nbytes -= c;
111*8962Sroot 		dblkno += c / DEV_BSIZE;
1128Sbill 	}
1135431Sroot 	s = spl6();
1148Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
1158Sbill 	bp->av_forw = bswlist.av_forw;
1168Sbill 	bswlist.av_forw = bp;
1178Sbill 	if (bswlist.b_flags & B_WANTED) {
1188Sbill 		bswlist.b_flags &= ~B_WANTED;
1198Sbill 		wakeup((caddr_t)&bswlist);
1208Sbill 		wakeup((caddr_t)&proc[2]);
1218Sbill 	}
1225431Sroot 	splx(s);
1238Sbill }
1248Sbill 
1258Sbill /*
1268Sbill  * If rout == 0 then killed on swap error, else
1278Sbill  * rout is the name of the routine where we ran out of
1288Sbill  * swap space.
1298Sbill  */
1308Sbill swkill(p, rout)
1318Sbill 	struct proc *p;
1328Sbill 	char *rout;
1338Sbill {
1342922Swnj 	char *mesg;
1358Sbill 
1362922Swnj 	printf("pid %d: ", p->p_pid);
1378Sbill 	if (rout)
1382922Swnj 		printf(mesg = "killed due to no swap space\n");
1398Sbill 	else
1402922Swnj 		printf(mesg = "killed on swap error\n");
1412922Swnj 	uprintf("sorry, pid %d was %s", p->p_pid, mesg);
1428Sbill 	/*
1438Sbill 	 * To be sure no looping (e.g. in vmsched trying to
1448Sbill 	 * swap out) mark process locked in core (as though
1458Sbill 	 * done by user) after killing it so noone will try
1468Sbill 	 * to swap it out.
1478Sbill 	 */
148165Sbill 	psignal(p, SIGKILL);
1498Sbill 	p->p_flag |= SULOCK;
1508Sbill }
1518Sbill 
1528Sbill /*
1538Sbill  * Raw I/O. The arguments are
1548Sbill  *	The strategy routine for the device
1558Sbill  *	A buffer, which will always be a special buffer
1568Sbill  *	  header owned exclusively by the device for this purpose
1578Sbill  *	The device number
1588Sbill  *	Read/write flag
1598Sbill  * Essentially all the work is computing physical addresses and
1608Sbill  * validating them.
1618Sbill  * If the user has the proper access privilidges, the process is
1628Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
1638Sbill  * faulted and locked. After the completion of the I/O, the above pages
1648Sbill  * are unlocked.
1658Sbill  */
1667724Swnj physio(strat, bp, dev, rw, mincnt, uio)
1677724Swnj 	int (*strat)();
1687724Swnj 	register struct buf *bp;
1697724Swnj 	dev_t dev;
1707724Swnj 	int rw;
1717724Swnj 	unsigned (*mincnt)();
1727724Swnj 	struct uio *uio;
1738Sbill {
1747830Sroot 	register struct iovec *iov = uio->uio_iov;
1758Sbill 	register int c;
1768Sbill 	char *a;
1777724Swnj 	int s, error = 0;
1788Sbill 
1797724Swnj nextiov:
1807830Sroot 	if (uio->uio_iovcnt == 0)
1817724Swnj 		return (0);
1827830Sroot 	if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL)
1837724Swnj 		return (EFAULT);
1845431Sroot 	s = spl6();
1858Sbill 	while (bp->b_flags&B_BUSY) {
1868Sbill 		bp->b_flags |= B_WANTED;
1878Sbill 		sleep((caddr_t)bp, PRIBIO+1);
1888Sbill 	}
1896319Swnj 	splx(s);
1908Sbill 	bp->b_error = 0;
1918Sbill 	bp->b_proc = u.u_procp;
1927724Swnj 	bp->b_un.b_addr = iov->iov_base;
1937724Swnj 	while (iov->iov_len > 0) {
1948Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
1958Sbill 		bp->b_dev = dev;
196*8962Sroot 		bp->b_blkno = uio->uio_offset / DEV_BSIZE;
1977724Swnj 		bp->b_bcount = iov->iov_len;
1988Sbill 		(*mincnt)(bp);
1998Sbill 		c = bp->b_bcount;
2008Sbill 		u.u_procp->p_flag |= SPHYSIO;
2018Sbill 		vslock(a = bp->b_un.b_addr, c);
202*8962Sroot 		physstrat(bp, strat, PRIBIO);
203124Sbill 		(void) spl6();
2048Sbill 		vsunlock(a, c, rw);
2058Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
2068Sbill 		if (bp->b_flags&B_WANTED)
2078Sbill 			wakeup((caddr_t)bp);
2085431Sroot 		splx(s);
2097724Swnj 		c -= bp->b_resid;
2108Sbill 		bp->b_un.b_addr += c;
2117724Swnj 		iov->iov_len -= c;
2127724Swnj 		uio->uio_resid -= c;
2137724Swnj 		uio->uio_offset += c;
2143667Swnj 		if (bp->b_flags&B_ERROR)
2153667Swnj 			break;
2168Sbill 	}
2178Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
2187724Swnj 	error = geterror(bp);
2197830Sroot 	if (error)
2207724Swnj 		return (error);
2217724Swnj 	uio->uio_iov++;
2227724Swnj 	uio->uio_iovcnt--;
2237724Swnj 	goto nextiov;
2248Sbill }
2258Sbill 
2268Sbill unsigned
2278Sbill minphys(bp)
2287724Swnj 	struct buf *bp;
2298Sbill {
2308Sbill 
2316379Swnj 	if (bp->b_bcount > 63 * 1024)
2326379Swnj 		bp->b_bcount = 63 * 1024;
2338Sbill }
2348Sbill 
235