xref: /csrg-svn/sys/kern/kern_physio.c (revision 26315)
123461Smckusick /*
223461Smckusick  * Copyright (c) 1982 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*26315Skarels  *	@(#)kern_physio.c	6.9 (Berkeley) 02/21/86
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,
458Sbill  * iodone() 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;
578962Sroot 	register u_int c;
588Sbill 	int p2dp;
598Sbill 	register struct pte *dpte, *vpte;
605431Sroot 	int s;
6112491Ssam 	extern swdone();
6226004Smckusick 	int error = 0;
638Sbill 
64*26315Skarels 	s = splbio();
658Sbill 	while (bswlist.av_forw == NULL) {
668Sbill 		bswlist.b_flags |= B_WANTED;
678Sbill 		sleep((caddr_t)&bswlist, PSWP+1);
688Sbill 	}
698Sbill 	bp = bswlist.av_forw;
708Sbill 	bswlist.av_forw = bp->av_forw;
715431Sroot 	splx(s);
728Sbill 
738Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
748Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
758Sbill 		if (rdflg == B_READ)
768Sbill 			sum.v_pswpin += btoc(nbytes);
778Sbill 		else
788Sbill 			sum.v_pswpout += btoc(nbytes);
798Sbill 	bp->b_proc = p;
808Sbill 	if (flag & B_DIRTY) {
818Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
828Sbill 		dpte = dptopte(&proc[2], p2dp);
838Sbill 		vpte = vtopte(p, btop(addr));
848Sbill 		for (c = 0; c < nbytes; c += NBPG) {
858Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
868Sbill 				panic("swap bad pte");
878Sbill 			*dpte++ = *vpte++;
888Sbill 		}
8912491Ssam 		bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp));
9012491Ssam 		bp->b_flags |= B_CALL;
9112491Ssam 		bp->b_iodone = swdone;
9212491Ssam 		bp->b_pfcent = pfcent;
938Sbill 	} else
948Sbill 		bp->b_un.b_addr = addr;
958Sbill 	while (nbytes > 0) {
968962Sroot 		bp->b_bcount = nbytes;
978962Sroot 		minphys(bp);
988962Sroot 		c = bp->b_bcount;
998Sbill 		bp->b_blkno = dblkno;
1008Sbill 		bp->b_dev = dev;
1014033Swnj #ifdef TRACE
1024033Swnj 		trace(TR_SWAPIO, dev, bp->b_blkno);
1034033Swnj #endif
1049011Sroot 		physstrat(bp, bdevsw[major(dev)].d_strategy, PSWP);
1058Sbill 		if (flag & B_DIRTY) {
1068Sbill 			if (c < nbytes)
1078Sbill 				panic("big push");
10826004Smckusick 			return (error);
1098Sbill 		}
1108Sbill 		bp->b_un.b_addr += c;
1118Sbill 		bp->b_flags &= ~B_DONE;
1128Sbill 		if (bp->b_flags & B_ERROR) {
1138Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
1148Sbill 				panic("hard IO err in swap");
11518319Smckusick 			swkill(p, "swap: read error from swap device");
11626004Smckusick 			error = EIO;
1178Sbill 		}
1188Sbill 		nbytes -= c;
11912647Ssam 		dblkno += btodb(c);
1208Sbill 	}
121*26315Skarels 	s = splbio();
1228Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
1238Sbill 	bp->av_forw = bswlist.av_forw;
1248Sbill 	bswlist.av_forw = bp;
1258Sbill 	if (bswlist.b_flags & B_WANTED) {
1268Sbill 		bswlist.b_flags &= ~B_WANTED;
1278Sbill 		wakeup((caddr_t)&bswlist);
1288Sbill 		wakeup((caddr_t)&proc[2]);
1298Sbill 	}
1305431Sroot 	splx(s);
13126004Smckusick 	return (error);
1328Sbill }
1338Sbill 
1348Sbill /*
13512491Ssam  * Put a buffer on the clean list after I/O is done.
13612491Ssam  * Called from biodone.
13712491Ssam  */
13812491Ssam swdone(bp)
13912491Ssam 	register struct buf *bp;
14012491Ssam {
14112491Ssam 	register int s;
14212491Ssam 
14312491Ssam 	if (bp->b_flags & B_ERROR)
14412491Ssam 		panic("IO err in push");
145*26315Skarels 	s = splbio();
14612491Ssam 	bp->av_forw = bclnlist;
14712491Ssam 	cnt.v_pgout++;
14812491Ssam 	cnt.v_pgpgout += bp->b_bcount / NBPG;
14912491Ssam 	bclnlist = bp;
15012491Ssam 	if (bswlist.b_flags & B_WANTED)
15112491Ssam 		wakeup((caddr_t)&proc[2]);
15212491Ssam 	splx(s);
15312491Ssam }
15412491Ssam 
15512491Ssam /*
1568Sbill  * If rout == 0 then killed on swap error, else
1578Sbill  * rout is the name of the routine where we ran out of
1588Sbill  * swap space.
1598Sbill  */
1608Sbill swkill(p, rout)
1618Sbill 	struct proc *p;
1628Sbill 	char *rout;
1638Sbill {
1648Sbill 
16524448Sbloom 	printf("pid %d: %s\n", p->p_pid, rout);
16624448Sbloom 	uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout);
1678Sbill 	/*
1688Sbill 	 * To be sure no looping (e.g. in vmsched trying to
1698Sbill 	 * swap out) mark process locked in core (as though
1708Sbill 	 * done by user) after killing it so noone will try
1718Sbill 	 * to swap it out.
1728Sbill 	 */
173165Sbill 	psignal(p, SIGKILL);
1748Sbill 	p->p_flag |= SULOCK;
1758Sbill }
1768Sbill 
1778Sbill /*
1788Sbill  * Raw I/O. The arguments are
1798Sbill  *	The strategy routine for the device
1808Sbill  *	A buffer, which will always be a special buffer
1818Sbill  *	  header owned exclusively by the device for this purpose
1828Sbill  *	The device number
1838Sbill  *	Read/write flag
1848Sbill  * Essentially all the work is computing physical addresses and
1858Sbill  * validating them.
1868Sbill  * If the user has the proper access privilidges, the process is
1878Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
1888Sbill  * faulted and locked. After the completion of the I/O, the above pages
1898Sbill  * are unlocked.
1908Sbill  */
1917724Swnj physio(strat, bp, dev, rw, mincnt, uio)
1927724Swnj 	int (*strat)();
1937724Swnj 	register struct buf *bp;
1947724Swnj 	dev_t dev;
1957724Swnj 	int rw;
1967724Swnj 	unsigned (*mincnt)();
1977724Swnj 	struct uio *uio;
1988Sbill {
19917313Skarels 	register struct iovec *iov;
2008Sbill 	register int c;
2018Sbill 	char *a;
2027724Swnj 	int s, error = 0;
2038Sbill 
2047724Swnj nextiov:
2057830Sroot 	if (uio->uio_iovcnt == 0)
2067724Swnj 		return (0);
20717313Skarels 	iov = uio->uio_iov;
2087830Sroot 	if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL)
2097724Swnj 		return (EFAULT);
210*26315Skarels 	s = splbio();
2118Sbill 	while (bp->b_flags&B_BUSY) {
2128Sbill 		bp->b_flags |= B_WANTED;
2138Sbill 		sleep((caddr_t)bp, PRIBIO+1);
2148Sbill 	}
2156319Swnj 	splx(s);
2168Sbill 	bp->b_error = 0;
2178Sbill 	bp->b_proc = u.u_procp;
2187724Swnj 	bp->b_un.b_addr = iov->iov_base;
2197724Swnj 	while (iov->iov_len > 0) {
2208Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
2218Sbill 		bp->b_dev = dev;
22212647Ssam 		bp->b_blkno = btodb(uio->uio_offset);
2237724Swnj 		bp->b_bcount = iov->iov_len;
2248Sbill 		(*mincnt)(bp);
2258Sbill 		c = bp->b_bcount;
2268Sbill 		u.u_procp->p_flag |= SPHYSIO;
2278Sbill 		vslock(a = bp->b_un.b_addr, c);
2288962Sroot 		physstrat(bp, strat, PRIBIO);
229*26315Skarels 		(void) splbio();
2308Sbill 		vsunlock(a, c, rw);
2318Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
2328Sbill 		if (bp->b_flags&B_WANTED)
2338Sbill 			wakeup((caddr_t)bp);
2345431Sroot 		splx(s);
2357724Swnj 		c -= bp->b_resid;
2368Sbill 		bp->b_un.b_addr += c;
2377724Swnj 		iov->iov_len -= c;
2387724Swnj 		uio->uio_resid -= c;
2397724Swnj 		uio->uio_offset += c;
2409766Ssam 		/* temp kludge for tape drives */
24110400Ssam 		if (bp->b_resid || (bp->b_flags&B_ERROR))
2423667Swnj 			break;
2438Sbill 	}
2448Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
2457724Swnj 	error = geterror(bp);
2469766Ssam 	/* temp kludge for tape drives */
2479766Ssam 	if (bp->b_resid || error)
2487724Swnj 		return (error);
2497724Swnj 	uio->uio_iov++;
2507724Swnj 	uio->uio_iovcnt--;
2517724Swnj 	goto nextiov;
2528Sbill }
2538Sbill 
25410400Ssam #define	MAXPHYS	(63 * 1024)
25510400Ssam 
2568Sbill unsigned
2578Sbill minphys(bp)
2587724Swnj 	struct buf *bp;
2598Sbill {
2608Sbill 
26110400Ssam 	if (bp->b_bcount > MAXPHYS)
26210400Ssam 		bp->b_bcount = MAXPHYS;
2638Sbill }
264