xref: /csrg-svn/sys/kern/kern_physio.c (revision 47540)
123461Smckusick /*
245730Smckusick  * Copyright (c) 1982, 1986, 1990 Regents of the University of California.
345730Smckusick  * All rights reserved.
423461Smckusick  *
545730Smckusick  * %sccs.include.redist.c%
645730Smckusick  *
7*47540Skarels  *	@(#)kern_physio.c	7.17 (Berkeley) 03/17/91
823461Smckusick  */
98Sbill 
1017108Sbloom #include "param.h"
1117108Sbloom #include "systm.h"
1217108Sbloom #include "user.h"
1317108Sbloom #include "buf.h"
1417108Sbloom #include "conf.h"
1517108Sbloom #include "proc.h"
1617108Sbloom #include "seg.h"
1717108Sbloom #include "trace.h"
1817108Sbloom #include "map.h"
1937729Smckusick #include "vnode.h"
2040652Smckusick #include "specdev.h"
218Sbill 
2291Sbill /*
238Sbill  * Raw I/O. The arguments are
248Sbill  *	The strategy routine for the device
2534215Sbostic  *	A buffer, which will either be a special buffer header owned
2634215Sbostic  *	    exclusively by the device for this purpose, or NULL,
2734215Sbostic  *	    indicating that we should use a swap buffer
288Sbill  *	The device number
298Sbill  *	Read/write flag
308Sbill  * Essentially all the work is computing physical addresses and
318Sbill  * validating them.
328Sbill  * If the user has the proper access privilidges, the process is
338Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
348Sbill  * faulted and locked. After the completion of the I/O, the above pages
358Sbill  * are unlocked.
368Sbill  */
377724Swnj physio(strat, bp, dev, rw, mincnt, uio)
387724Swnj 	int (*strat)();
397724Swnj 	register struct buf *bp;
407724Swnj 	dev_t dev;
417724Swnj 	int rw;
4234215Sbostic 	u_int (*mincnt)();
437724Swnj 	struct uio *uio;
448Sbill {
4517313Skarels 	register struct iovec *iov;
4638794Skarels 	register int requested, done;
47*47540Skarels 	register struct proc *p = curproc;
488Sbill 	char *a;
4934215Sbostic 	int s, allocbuf = 0, error = 0;
5034215Sbostic 	struct buf *getswbuf();
518Sbill 
5234215Sbostic 	if (bp == NULL) {
5334215Sbostic 		allocbuf = 1;
5434215Sbostic 		bp = getswbuf(PRIBIO+1);
5534215Sbostic 	}
5634215Sbostic 	for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) {
5730750Skarels 		iov = uio->uio_iov;
5834215Sbostic 		if (!useracc(iov->iov_base, (u_int)iov->iov_len,
5934215Sbostic 		    rw == B_READ ? B_WRITE : B_READ)) {
6034215Sbostic 			error = EFAULT;
6134215Sbostic 			break;
6230750Skarels 		}
6334215Sbostic 		if (!allocbuf) {	/* only if sharing caller's buffer */
6434215Sbostic 			s = splbio();
6534215Sbostic 			while (bp->b_flags&B_BUSY) {
6634215Sbostic 				bp->b_flags |= B_WANTED;
6734215Sbostic 				sleep((caddr_t)bp, PRIBIO+1);
6834215Sbostic 			}
6934215Sbostic 			splx(s);
7034215Sbostic 		}
7130750Skarels 		bp->b_error = 0;
72*47540Skarels 		bp->b_proc = p;
7342001Smckusick #ifdef HPUXCOMPAT
7442001Smckusick 		if (ISHPMMADDR(iov->iov_base))
7542001Smckusick 			bp->b_un.b_addr = (caddr_t)HPMMBASEADDR(iov->iov_base);
7642001Smckusick 		else
7742001Smckusick #endif
7830750Skarels 		bp->b_un.b_addr = iov->iov_base;
7930750Skarels 		while (iov->iov_len > 0) {
8034215Sbostic 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw;
8130750Skarels 			bp->b_dev = dev;
8230750Skarels 			bp->b_blkno = btodb(uio->uio_offset);
8330750Skarels 			bp->b_bcount = iov->iov_len;
8430750Skarels 			(*mincnt)(bp);
8538794Skarels 			requested = bp->b_bcount;
86*47540Skarels 			p->p_flag |= SPHYSIO;
8738794Skarels 			vslock(a = bp->b_un.b_addr, requested);
8844773Swilliam #if defined(hp300) || defined(i386)
8942001Smckusick 			vmapbuf(bp);
9042001Smckusick #endif
9134215Sbostic 			(*strat)(bp);
9234215Sbostic 			s = splbio();
9334215Sbostic 			while ((bp->b_flags & B_DONE) == 0)
9434215Sbostic 				sleep((caddr_t)bp, PRIBIO);
9544773Swilliam #if defined(hp300) || defined(i386)
9642001Smckusick 			vunmapbuf(bp);
9742001Smckusick #endif
9838794Skarels 			vsunlock(a, requested, rw);
99*47540Skarels 			p->p_flag &= ~SPHYSIO;
10034215Sbostic 			if (bp->b_flags&B_WANTED)	/* rare */
10130750Skarels 				wakeup((caddr_t)bp);
10230750Skarels 			splx(s);
10338794Skarels 			done = bp->b_bcount - bp->b_resid;
10438794Skarels 			bp->b_un.b_addr += done;
10538794Skarels 			iov->iov_len -= done;
10638794Skarels 			uio->uio_resid -= done;
10738794Skarels 			uio->uio_offset += done;
10838794Skarels 			/* temp kludge for disk drives */
10938794Skarels 			if (done < requested || bp->b_flags & B_ERROR)
11030750Skarels 				break;
11130750Skarels 		}
11234215Sbostic 		bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW);
11337729Smckusick 		error = biowait(bp);
11438794Skarels 		/* temp kludge for disk drives */
11538794Skarels 		if (done < requested || bp->b_flags & B_ERROR)
11634215Sbostic 			break;
1178Sbill 	}
11842001Smckusick #if defined(hp300)
11942001Smckusick 	DCIU();
12042001Smckusick #endif
12134215Sbostic 	if (allocbuf)
12234215Sbostic 		freeswbuf(bp);
12334215Sbostic 	return (error);
1248Sbill }
1258Sbill 
12634215Sbostic u_int
1278Sbill minphys(bp)
1287724Swnj 	struct buf *bp;
1298Sbill {
13010400Ssam 	if (bp->b_bcount > MAXPHYS)
13110400Ssam 		bp->b_bcount = MAXPHYS;
1328Sbill }
13334215Sbostic 
13434215Sbostic static
13534215Sbostic struct buf *
13634215Sbostic getswbuf(prio)
13734215Sbostic 	int prio;
13834215Sbostic {
13934215Sbostic 	int s;
14034215Sbostic 	struct buf *bp;
14134215Sbostic 
14234215Sbostic 	s = splbio();
14334215Sbostic 	while (bswlist.av_forw == NULL) {
14434215Sbostic 		bswlist.b_flags |= B_WANTED;
14534215Sbostic 		sleep((caddr_t)&bswlist, prio);
14634215Sbostic 	}
14734215Sbostic 	bp = bswlist.av_forw;
14834215Sbostic 	bswlist.av_forw = bp->av_forw;
14934215Sbostic 	splx(s);
15034215Sbostic 	return (bp);
15134215Sbostic }
15234215Sbostic 
15334215Sbostic static
15434215Sbostic freeswbuf(bp)
15534215Sbostic 	struct buf *bp;
15634215Sbostic {
15734215Sbostic 	int s;
15834215Sbostic 
15934215Sbostic 	s = splbio();
16034215Sbostic 	bp->av_forw = bswlist.av_forw;
16134215Sbostic 	bswlist.av_forw = bp;
16239148Smckusick 	if (bp->b_vp)
16339148Smckusick 		brelvp(bp);
16434215Sbostic 	if (bswlist.b_flags & B_WANTED) {
16534215Sbostic 		bswlist.b_flags &= ~B_WANTED;
16634215Sbostic 		wakeup((caddr_t)&bswlist);
167*47540Skarels 		wakeup((caddr_t)pageproc);
16834215Sbostic 	}
16934215Sbostic 	splx(s);
17034215Sbostic }
17134215Sbostic 
17234215Sbostic rawread(dev, uio)
17334215Sbostic 	dev_t dev;
17434215Sbostic 	struct uio *uio;
17534215Sbostic {
17634215Sbostic 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
17734215Sbostic 	    dev, B_READ, minphys, uio));
17834215Sbostic }
17934215Sbostic 
18034215Sbostic rawwrite(dev, uio)
18134215Sbostic 	dev_t dev;
18234215Sbostic 	struct uio *uio;
18334215Sbostic {
18434215Sbostic 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
18534215Sbostic 	    dev, B_WRITE, minphys, uio));
18634215Sbostic }
187