xref: /csrg-svn/sys/kern/kern_physio.c (revision 18319)
1*18319Smckusick /*	kern_physio.c	6.5	85/03/12	*/
28Sbill 
39766Ssam #include "../machine/pte.h"
49766Ssam 
517108Sbloom #include "param.h"
617108Sbloom #include "systm.h"
717108Sbloom #include "dir.h"
817108Sbloom #include "user.h"
917108Sbloom #include "buf.h"
1017108Sbloom #include "conf.h"
1117108Sbloom #include "proc.h"
1217108Sbloom #include "seg.h"
1317108Sbloom #include "vm.h"
1417108Sbloom #include "trace.h"
1517108Sbloom #include "map.h"
1617108Sbloom #include "uio.h"
178Sbill 
1891Sbill /*
198Sbill  * Swap IO headers -
208Sbill  * They contain the necessary information for the swap I/O.
218Sbill  * At any given time, a swap header can be in three
228Sbill  * different lists. When free it is in the free list,
238Sbill  * when allocated and the I/O queued, it is on the swap
248Sbill  * device list, and finally, if the operation was a dirty
258Sbill  * page push, when the I/O completes, it is inserted
268Sbill  * in a list of cleaned pages to be processed by the pageout daemon.
278Sbill  */
282771Swnj struct	buf *swbuf;
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;
518962Sroot 	register u_int c;
528Sbill 	int p2dp;
538Sbill 	register struct pte *dpte, *vpte;
545431Sroot 	int s;
5512491Ssam 	extern swdone();
568Sbill 
575431Sroot 	s = spl6();
588Sbill 	while (bswlist.av_forw == NULL) {
598Sbill 		bswlist.b_flags |= B_WANTED;
608Sbill 		sleep((caddr_t)&bswlist, PSWP+1);
618Sbill 	}
628Sbill 	bp = bswlist.av_forw;
638Sbill 	bswlist.av_forw = bp->av_forw;
645431Sroot 	splx(s);
658Sbill 
668Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
678Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
688Sbill 		if (rdflg == B_READ)
698Sbill 			sum.v_pswpin += btoc(nbytes);
708Sbill 		else
718Sbill 			sum.v_pswpout += btoc(nbytes);
728Sbill 	bp->b_proc = p;
738Sbill 	if (flag & B_DIRTY) {
748Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
758Sbill 		dpte = dptopte(&proc[2], p2dp);
768Sbill 		vpte = vtopte(p, btop(addr));
778Sbill 		for (c = 0; c < nbytes; c += NBPG) {
788Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
798Sbill 				panic("swap bad pte");
808Sbill 			*dpte++ = *vpte++;
818Sbill 		}
8212491Ssam 		bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp));
8312491Ssam 		bp->b_flags |= B_CALL;
8412491Ssam 		bp->b_iodone = swdone;
8512491Ssam 		bp->b_pfcent = pfcent;
868Sbill 	} else
878Sbill 		bp->b_un.b_addr = addr;
888Sbill 	while (nbytes > 0) {
898962Sroot 		bp->b_bcount = nbytes;
908962Sroot 		minphys(bp);
918962Sroot 		c = bp->b_bcount;
928Sbill 		bp->b_blkno = dblkno;
938Sbill 		bp->b_dev = dev;
944033Swnj #ifdef TRACE
954033Swnj 		trace(TR_SWAPIO, dev, bp->b_blkno);
964033Swnj #endif
979011Sroot 		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");
108*18319Smckusick 			swkill(p, "swap: read error from swap device");
1098Sbill 		}
1108Sbill 		nbytes -= c;
11112647Ssam 		dblkno += btodb(c);
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 /*
12612491Ssam  * Put a buffer on the clean list after I/O is done.
12712491Ssam  * Called from biodone.
12812491Ssam  */
12912491Ssam swdone(bp)
13012491Ssam 	register struct buf *bp;
13112491Ssam {
13212491Ssam 	register int s;
13312491Ssam 
13412491Ssam 	if (bp->b_flags & B_ERROR)
13512491Ssam 		panic("IO err in push");
13612491Ssam 	s = spl6();
13712491Ssam 	bp->av_forw = bclnlist;
13812491Ssam 	cnt.v_pgout++;
13912491Ssam 	cnt.v_pgpgout += bp->b_bcount / NBPG;
14012491Ssam 	bclnlist = bp;
14112491Ssam 	if (bswlist.b_flags & B_WANTED)
14212491Ssam 		wakeup((caddr_t)&proc[2]);
14312491Ssam 	splx(s);
14412491Ssam }
14512491Ssam 
14612491Ssam /*
1478Sbill  * If rout == 0 then killed on swap error, else
1488Sbill  * rout is the name of the routine where we ran out of
1498Sbill  * swap space.
1508Sbill  */
1518Sbill swkill(p, rout)
1528Sbill 	struct proc *p;
1538Sbill 	char *rout;
1548Sbill {
1558Sbill 
156*18319Smckusick 	printf("pid %d: %s", p->p_pid, rout);
157*18319Smckusick 	uprintf("sorry, pid %d was killed in %s", p->p_pid, rout);
1588Sbill 	/*
1598Sbill 	 * To be sure no looping (e.g. in vmsched trying to
1608Sbill 	 * swap out) mark process locked in core (as though
1618Sbill 	 * done by user) after killing it so noone will try
1628Sbill 	 * to swap it out.
1638Sbill 	 */
164165Sbill 	psignal(p, SIGKILL);
1658Sbill 	p->p_flag |= SULOCK;
1668Sbill }
1678Sbill 
1688Sbill /*
1698Sbill  * Raw I/O. The arguments are
1708Sbill  *	The strategy routine for the device
1718Sbill  *	A buffer, which will always be a special buffer
1728Sbill  *	  header owned exclusively by the device for this purpose
1738Sbill  *	The device number
1748Sbill  *	Read/write flag
1758Sbill  * Essentially all the work is computing physical addresses and
1768Sbill  * validating them.
1778Sbill  * If the user has the proper access privilidges, the process is
1788Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
1798Sbill  * faulted and locked. After the completion of the I/O, the above pages
1808Sbill  * are unlocked.
1818Sbill  */
1827724Swnj physio(strat, bp, dev, rw, mincnt, uio)
1837724Swnj 	int (*strat)();
1847724Swnj 	register struct buf *bp;
1857724Swnj 	dev_t dev;
1867724Swnj 	int rw;
1877724Swnj 	unsigned (*mincnt)();
1887724Swnj 	struct uio *uio;
1898Sbill {
19017313Skarels 	register struct iovec *iov;
1918Sbill 	register int c;
1928Sbill 	char *a;
1937724Swnj 	int s, error = 0;
1948Sbill 
1957724Swnj nextiov:
1967830Sroot 	if (uio->uio_iovcnt == 0)
1977724Swnj 		return (0);
19817313Skarels 	iov = uio->uio_iov;
1997830Sroot 	if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL)
2007724Swnj 		return (EFAULT);
2015431Sroot 	s = spl6();
2028Sbill 	while (bp->b_flags&B_BUSY) {
2038Sbill 		bp->b_flags |= B_WANTED;
2048Sbill 		sleep((caddr_t)bp, PRIBIO+1);
2058Sbill 	}
2066319Swnj 	splx(s);
2078Sbill 	bp->b_error = 0;
2088Sbill 	bp->b_proc = u.u_procp;
2097724Swnj 	bp->b_un.b_addr = iov->iov_base;
2107724Swnj 	while (iov->iov_len > 0) {
2118Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
2128Sbill 		bp->b_dev = dev;
21312647Ssam 		bp->b_blkno = btodb(uio->uio_offset);
2147724Swnj 		bp->b_bcount = iov->iov_len;
2158Sbill 		(*mincnt)(bp);
2168Sbill 		c = bp->b_bcount;
2178Sbill 		u.u_procp->p_flag |= SPHYSIO;
2188Sbill 		vslock(a = bp->b_un.b_addr, c);
2198962Sroot 		physstrat(bp, strat, PRIBIO);
220124Sbill 		(void) spl6();
2218Sbill 		vsunlock(a, c, rw);
2228Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
2238Sbill 		if (bp->b_flags&B_WANTED)
2248Sbill 			wakeup((caddr_t)bp);
2255431Sroot 		splx(s);
2267724Swnj 		c -= bp->b_resid;
2278Sbill 		bp->b_un.b_addr += c;
2287724Swnj 		iov->iov_len -= c;
2297724Swnj 		uio->uio_resid -= c;
2307724Swnj 		uio->uio_offset += c;
2319766Ssam 		/* temp kludge for tape drives */
23210400Ssam 		if (bp->b_resid || (bp->b_flags&B_ERROR))
2333667Swnj 			break;
2348Sbill 	}
2358Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
2367724Swnj 	error = geterror(bp);
2379766Ssam 	/* temp kludge for tape drives */
2389766Ssam 	if (bp->b_resid || error)
2397724Swnj 		return (error);
2407724Swnj 	uio->uio_iov++;
2417724Swnj 	uio->uio_iovcnt--;
2427724Swnj 	goto nextiov;
2438Sbill }
2448Sbill 
24510400Ssam #define	MAXPHYS	(63 * 1024)
24610400Ssam 
2478Sbill unsigned
2488Sbill minphys(bp)
2497724Swnj 	struct buf *bp;
2508Sbill {
2518Sbill 
25210400Ssam 	if (bp->b_bcount > MAXPHYS)
25310400Ssam 		bp->b_bcount = MAXPHYS;
2548Sbill }
255