xref: /csrg-svn/sys/kern/kern_physio.c (revision 34215)
123461Smckusick /*
229132Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323461Smckusick  * All rights reserved.  The Berkeley software License Agreement
423461Smckusick  * specifies the terms and conditions for redistribution.
523461Smckusick  *
6*34215Sbostic  *	@(#)kern_physio.c	7.4 (Berkeley) 05/06/88
723461Smckusick  */
88Sbill 
99766Ssam #include "../machine/pte.h"
109766Ssam 
1117108Sbloom #include "param.h"
1217108Sbloom #include "systm.h"
1317108Sbloom #include "dir.h"
1417108Sbloom #include "user.h"
1517108Sbloom #include "buf.h"
1617108Sbloom #include "conf.h"
1717108Sbloom #include "proc.h"
1817108Sbloom #include "seg.h"
1917108Sbloom #include "vm.h"
2017108Sbloom #include "trace.h"
2117108Sbloom #include "map.h"
2217108Sbloom #include "uio.h"
238Sbill 
2491Sbill /*
258Sbill  * Swap IO headers -
268Sbill  * They contain the necessary information for the swap I/O.
278Sbill  * At any given time, a swap header can be in three
288Sbill  * different lists. When free it is in the free list,
298Sbill  * when allocated and the I/O queued, it is on the swap
308Sbill  * device list, and finally, if the operation was a dirty
318Sbill  * page push, when the I/O completes, it is inserted
328Sbill  * in a list of cleaned pages to be processed by the pageout daemon.
338Sbill  */
342771Swnj struct	buf *swbuf;
358Sbill 
368Sbill /*
378Sbill  * swap I/O -
388Sbill  *
398Sbill  * If the flag indicates a dirty page push initiated
408Sbill  * by the pageout daemon, we map the page into the i th
418Sbill  * virtual page of process 2 (the daemon itself) where i is
428Sbill  * the index of the swap header that has been allocated.
438Sbill  * We simply initialize the header and queue the I/O but
448Sbill  * do not wait for completion. When the I/O completes,
4530750Skarels  * biodone() will link the header to a list of cleaned
468Sbill  * pages to be processed by the pageout daemon.
478Sbill  */
488Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
498Sbill 	struct proc *p;
508Sbill 	swblk_t dblkno;
518Sbill 	caddr_t addr;
528674S 	int nbytes, rdflg, flag;
538Sbill 	dev_t dev;
548674S 	u_int pfcent;
558Sbill {
568Sbill 	register struct buf *bp;
57*34215Sbostic 	register struct pte *dpte, *vpte;
588962Sroot 	register u_int c;
59*34215Sbostic 	int p2dp, s, error = 0;
60*34215Sbostic 	struct buf *getswbuf();
61*34215Sbostic 	int swdone();
628Sbill 
63*34215Sbostic 	bp = getswbuf(PSWP+1);
648Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
658Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
668Sbill 		if (rdflg == B_READ)
678Sbill 			sum.v_pswpin += btoc(nbytes);
688Sbill 		else
698Sbill 			sum.v_pswpout += btoc(nbytes);
708Sbill 	bp->b_proc = p;
718Sbill 	if (flag & B_DIRTY) {
728Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
738Sbill 		dpte = dptopte(&proc[2], p2dp);
748Sbill 		vpte = vtopte(p, btop(addr));
758Sbill 		for (c = 0; c < nbytes; c += NBPG) {
768Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
778Sbill 				panic("swap bad pte");
788Sbill 			*dpte++ = *vpte++;
798Sbill 		}
8012491Ssam 		bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp));
8112491Ssam 		bp->b_flags |= B_CALL;
8212491Ssam 		bp->b_iodone = swdone;
8312491Ssam 		bp->b_pfcent = pfcent;
848Sbill 	} else
858Sbill 		bp->b_un.b_addr = addr;
868Sbill 	while (nbytes > 0) {
878962Sroot 		bp->b_bcount = nbytes;
888962Sroot 		minphys(bp);
898962Sroot 		c = bp->b_bcount;
908Sbill 		bp->b_blkno = dblkno;
918Sbill 		bp->b_dev = dev;
924033Swnj #ifdef TRACE
934033Swnj 		trace(TR_SWAPIO, dev, bp->b_blkno);
944033Swnj #endif
95*34215Sbostic 		(*bdevsw[major(dev)].d_strategy)(bp);
96*34215Sbostic 		/* pageout daemon doesn't wait for pushed pages */
978Sbill 		if (flag & B_DIRTY) {
988Sbill 			if (c < nbytes)
998Sbill 				panic("big push");
10030750Skarels 			return (0);
101*34215Sbostic 		} else {
102*34215Sbostic 			s = splbio();
103*34215Sbostic 			while ((bp->b_flags & B_DONE) == 0)
104*34215Sbostic 				sleep((caddr_t)bp, PSWP);
105*34215Sbostic 			splx(s);
1068Sbill 		}
1078Sbill 		bp->b_un.b_addr += c;
1088Sbill 		bp->b_flags &= ~B_DONE;
1098Sbill 		if (bp->b_flags & B_ERROR) {
1108Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
1118Sbill 				panic("hard IO err in swap");
11218319Smckusick 			swkill(p, "swap: read error from swap device");
11326004Smckusick 			error = EIO;
1148Sbill 		}
1158Sbill 		nbytes -= c;
11612647Ssam 		dblkno += btodb(c);
1178Sbill 	}
1188Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
119*34215Sbostic 	freeswbuf(bp);
12026004Smckusick 	return (error);
1218Sbill }
1228Sbill 
1238Sbill /*
12412491Ssam  * Put a buffer on the clean list after I/O is done.
12512491Ssam  * Called from biodone.
12612491Ssam  */
12712491Ssam swdone(bp)
12812491Ssam 	register struct buf *bp;
12912491Ssam {
13012491Ssam 	register int s;
13112491Ssam 
13212491Ssam 	if (bp->b_flags & B_ERROR)
13312491Ssam 		panic("IO err in push");
13426315Skarels 	s = splbio();
13512491Ssam 	bp->av_forw = bclnlist;
13612491Ssam 	cnt.v_pgout++;
13712491Ssam 	cnt.v_pgpgout += bp->b_bcount / NBPG;
13812491Ssam 	bclnlist = bp;
13912491Ssam 	if (bswlist.b_flags & B_WANTED)
14012491Ssam 		wakeup((caddr_t)&proc[2]);
14112491Ssam 	splx(s);
14212491Ssam }
14312491Ssam 
14412491Ssam /*
1458Sbill  * If rout == 0 then killed on swap error, else
1468Sbill  * rout is the name of the routine where we ran out of
1478Sbill  * swap space.
1488Sbill  */
1498Sbill swkill(p, rout)
1508Sbill 	struct proc *p;
1518Sbill 	char *rout;
1528Sbill {
1538Sbill 
15424448Sbloom 	printf("pid %d: %s\n", p->p_pid, rout);
15524448Sbloom 	uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout);
1568Sbill 	/*
1578Sbill 	 * To be sure no looping (e.g. in vmsched trying to
1588Sbill 	 * swap out) mark process locked in core (as though
1598Sbill 	 * done by user) after killing it so noone will try
1608Sbill 	 * to swap it out.
1618Sbill 	 */
162165Sbill 	psignal(p, SIGKILL);
1638Sbill 	p->p_flag |= SULOCK;
1648Sbill }
1658Sbill 
1668Sbill /*
1678Sbill  * Raw I/O. The arguments are
1688Sbill  *	The strategy routine for the device
169*34215Sbostic  *	A buffer, which will either be a special buffer header owned
170*34215Sbostic  *	    exclusively by the device for this purpose, or NULL,
171*34215Sbostic  *	    indicating that we should use a swap buffer
1728Sbill  *	The device number
1738Sbill  *	Read/write flag
1748Sbill  * Essentially all the work is computing physical addresses and
1758Sbill  * validating them.
1768Sbill  * If the user has the proper access privilidges, the process is
1778Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
1788Sbill  * faulted and locked. After the completion of the I/O, the above pages
1798Sbill  * are unlocked.
1808Sbill  */
1817724Swnj physio(strat, bp, dev, rw, mincnt, uio)
1827724Swnj 	int (*strat)();
1837724Swnj 	register struct buf *bp;
1847724Swnj 	dev_t dev;
1857724Swnj 	int rw;
186*34215Sbostic 	u_int (*mincnt)();
1877724Swnj 	struct uio *uio;
1888Sbill {
18917313Skarels 	register struct iovec *iov;
1908Sbill 	register int c;
1918Sbill 	char *a;
192*34215Sbostic 	int s, allocbuf = 0, error = 0;
193*34215Sbostic 	struct buf *getswbuf();
1948Sbill 
195*34215Sbostic 	if (bp == NULL) {
196*34215Sbostic 		allocbuf = 1;
197*34215Sbostic 		bp = getswbuf(PRIBIO+1);
198*34215Sbostic 	}
199*34215Sbostic 	for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) {
20030750Skarels 		iov = uio->uio_iov;
201*34215Sbostic 		if (!useracc(iov->iov_base, (u_int)iov->iov_len,
202*34215Sbostic 		    rw == B_READ ? B_WRITE : B_READ)) {
203*34215Sbostic 			error = EFAULT;
204*34215Sbostic 			break;
20530750Skarels 		}
206*34215Sbostic 		if (!allocbuf) {	/* only if sharing caller's buffer */
207*34215Sbostic 			s = splbio();
208*34215Sbostic 			while (bp->b_flags&B_BUSY) {
209*34215Sbostic 				bp->b_flags |= B_WANTED;
210*34215Sbostic 				sleep((caddr_t)bp, PRIBIO+1);
211*34215Sbostic 			}
212*34215Sbostic 			splx(s);
213*34215Sbostic 		}
21430750Skarels 		bp->b_error = 0;
21530750Skarels 		bp->b_proc = u.u_procp;
21630750Skarels 		bp->b_un.b_addr = iov->iov_base;
21730750Skarels 		while (iov->iov_len > 0) {
218*34215Sbostic 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw;
21930750Skarels 			bp->b_dev = dev;
22030750Skarels 			bp->b_blkno = btodb(uio->uio_offset);
22130750Skarels 			bp->b_bcount = iov->iov_len;
22230750Skarels 			(*mincnt)(bp);
22330750Skarels 			c = bp->b_bcount;
22430750Skarels 			u.u_procp->p_flag |= SPHYSIO;
22530750Skarels 			vslock(a = bp->b_un.b_addr, c);
226*34215Sbostic 			(*strat)(bp);
227*34215Sbostic 			s = splbio();
228*34215Sbostic 			while ((bp->b_flags & B_DONE) == 0)
229*34215Sbostic 				sleep((caddr_t)bp, PRIBIO);
23030750Skarels 			vsunlock(a, c, rw);
23130750Skarels 			u.u_procp->p_flag &= ~SPHYSIO;
232*34215Sbostic 			if (bp->b_flags&B_WANTED)	/* rare */
23330750Skarels 				wakeup((caddr_t)bp);
23430750Skarels 			splx(s);
23530750Skarels 			c -= bp->b_resid;
23630750Skarels 			bp->b_un.b_addr += c;
23730750Skarels 			iov->iov_len -= c;
23830750Skarels 			uio->uio_resid -= c;
23930750Skarels 			uio->uio_offset += c;
24030750Skarels 			/* temp kludge for tape drives */
24130750Skarels 			if (bp->b_resid || (bp->b_flags&B_ERROR))
24230750Skarels 				break;
24330750Skarels 		}
244*34215Sbostic 		bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW);
24530750Skarels 		error = geterror(bp);
2469766Ssam 		/* temp kludge for tape drives */
24730750Skarels 		if (bp->b_resid || error)
248*34215Sbostic 			break;
2498Sbill 	}
250*34215Sbostic 	if (allocbuf)
251*34215Sbostic 		freeswbuf(bp);
252*34215Sbostic 	return (error);
2538Sbill }
2548Sbill 
255*34215Sbostic u_int
2568Sbill minphys(bp)
2577724Swnj 	struct buf *bp;
2588Sbill {
25910400Ssam 	if (bp->b_bcount > MAXPHYS)
26010400Ssam 		bp->b_bcount = MAXPHYS;
2618Sbill }
262*34215Sbostic 
263*34215Sbostic static
264*34215Sbostic struct buf *
265*34215Sbostic getswbuf(prio)
266*34215Sbostic 	int prio;
267*34215Sbostic {
268*34215Sbostic 	int s;
269*34215Sbostic 	struct buf *bp;
270*34215Sbostic 
271*34215Sbostic 	s = splbio();
272*34215Sbostic 	while (bswlist.av_forw == NULL) {
273*34215Sbostic 		bswlist.b_flags |= B_WANTED;
274*34215Sbostic 		sleep((caddr_t)&bswlist, prio);
275*34215Sbostic 	}
276*34215Sbostic 	bp = bswlist.av_forw;
277*34215Sbostic 	bswlist.av_forw = bp->av_forw;
278*34215Sbostic 	splx(s);
279*34215Sbostic 	return (bp);
280*34215Sbostic }
281*34215Sbostic 
282*34215Sbostic static
283*34215Sbostic freeswbuf(bp)
284*34215Sbostic 	struct buf *bp;
285*34215Sbostic {
286*34215Sbostic 	int s;
287*34215Sbostic 
288*34215Sbostic 	s = splbio();
289*34215Sbostic 	bp->av_forw = bswlist.av_forw;
290*34215Sbostic 	bswlist.av_forw = bp;
291*34215Sbostic 	if (bswlist.b_flags & B_WANTED) {
292*34215Sbostic 		bswlist.b_flags &= ~B_WANTED;
293*34215Sbostic 		wakeup((caddr_t)&bswlist);
294*34215Sbostic 		wakeup((caddr_t)&proc[2]);
295*34215Sbostic 	}
296*34215Sbostic 	splx(s);
297*34215Sbostic }
298*34215Sbostic 
299*34215Sbostic rawread(dev, uio)
300*34215Sbostic 	dev_t dev;
301*34215Sbostic 	struct uio *uio;
302*34215Sbostic {
303*34215Sbostic 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
304*34215Sbostic 	    dev, B_READ, minphys, uio));
305*34215Sbostic }
306*34215Sbostic 
307*34215Sbostic rawwrite(dev, uio)
308*34215Sbostic 	dev_t dev;
309*34215Sbostic 	struct uio *uio;
310*34215Sbostic {
311*34215Sbostic 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
312*34215Sbostic 	    dev, B_WRITE, minphys, uio));
313*34215Sbostic }
314