kern_physio.c revision 2112
1/*
2 * Copyright (c) 1994 John S. Dyson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice immediately at the beginning of the file, without modification,
10 *    this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
15 *    John S. Dyson.
16 * 4. Modifications may be freely made to this file if the above conditions
17 *    are met.
18 *
19 * $Id: kern_physio.c,v 1.6 1994/08/08 13:53:55 davidg Exp $
20 */
21
22#include <sys/param.h>
23#include <sys/systm.h>
24#include <sys/buf.h>
25#include <sys/conf.h>
26#include <sys/proc.h>
27#include <vm/vm.h>
28
29static void physwakeup();
30
31int
32physio(strategy, bp, dev, rw, minp, uio)
33	int (*strategy)();
34	struct buf *bp;
35	dev_t dev;
36	int rw;
37	u_int (*minp)();
38	struct uio *uio;
39{
40	int i;
41	int bufflags = rw?B_READ:0;
42	int error;
43	int spl;
44	caddr_t sa;
45	int bp_alloc = (bp == 0);
46	struct buf *bpa;
47
48/*
49 * keep the process from being swapped
50 */
51	curproc->p_flag |= P_PHYSIO;
52
53	/* create and build a buffer header for a transfer */
54	bpa = (struct buf *)getpbuf();
55	if (!bp_alloc) {
56		spl = splbio();
57		while (bp->b_flags & B_BUSY) {
58			bp->b_flags |= B_WANTED;
59			tsleep((caddr_t)bp, PRIBIO, "physbw", 0);
60		}
61		bp->b_flags |= B_BUSY;
62		splx(spl);
63	} else {
64		bp = bpa;
65	}
66
67	/*
68	 * get a copy of the kva from the physical buffer
69	 */
70	sa = bpa->b_data;
71	bp->b_proc = curproc;
72	bp->b_dev = dev;
73	error = bp->b_error = 0;
74
75	for(i=0;i<uio->uio_iovcnt;i++) {
76		while( uio->uio_iov[i].iov_len) {
77			vm_offset_t v, lastv, pa;
78			caddr_t adr;
79
80			bp->b_bcount = uio->uio_iov[i].iov_len;
81			bp->b_bcount = minp( bp);
82			if( minp != minphys)
83				bp->b_bcount = minphys( bp);
84			bp->b_bufsize = bp->b_bcount;
85			bp->b_flags = B_BUSY | B_PHYS | B_CALL | bufflags;
86			bp->b_iodone = physwakeup;
87			bp->b_data = uio->uio_iov[i].iov_base;
88			/*
89			 * pass in the kva from the physical buffer
90			 * for the temporary kernel mapping.
91			 */
92			bp->b_saveaddr = sa;
93			bp->b_blkno = btodb(uio->uio_offset);
94
95
96			if (rw && !useracc(bp->b_data, bp->b_bufsize, B_WRITE)) {
97				error = EFAULT;
98				goto doerror;
99			}
100			if (!rw && !useracc(bp->b_data, bp->b_bufsize, B_READ)) {
101				error = EFAULT;
102				goto doerror;
103			}
104
105			vmapbuf(bp);
106
107			/* perform transfer */
108			(*strategy)(bp);
109
110			spl = splbio();
111			while ((bp->b_flags & B_DONE) == 0)
112				tsleep((caddr_t)bp, PRIBIO, "physstr", 0);
113			splx(spl);
114
115			vunmapbuf(bp);
116
117			/*
118			 * update the uio data
119			 */
120			{
121				int iolen = bp->b_bcount - bp->b_resid;
122
123				if (iolen == 0 && !(bp->b_flags & B_ERROR))
124					goto doerror;	/* EOF */
125				uio->uio_iov[i].iov_len -= iolen;
126				uio->uio_iov[i].iov_base += iolen;
127				uio->uio_resid -= iolen;
128				uio->uio_offset += iolen;
129			}
130
131			/*
132			 * check for an error
133			 */
134			if( bp->b_flags & B_ERROR) {
135				error = bp->b_error;
136				goto doerror;
137			}
138		}
139	}
140
141
142doerror:
143	relpbuf(bpa);
144	if (!bp_alloc) {
145		bp->b_flags &= ~(B_BUSY|B_PHYS);
146		if( bp->b_flags & B_WANTED) {
147			bp->b_flags &= ~B_WANTED;
148			wakeup((caddr_t)bp);
149		}
150	}
151/*
152 * allow the process to be swapped
153 */
154	curproc->p_flag &= ~P_PHYSIO;
155
156	return (error);
157}
158
159u_int
160minphys(struct buf *bp)
161{
162
163	if( bp->b_bcount > MAXBSIZE) {
164		bp->b_bcount = MAXBSIZE;
165	}
166	return bp->b_bcount;
167}
168
169int
170rawread(dev_t dev, struct uio *uio)
171{
172	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
173	    dev, 1, minphys, uio));
174}
175
176int
177rawwrite(dev_t dev, struct uio *uio)
178{
179	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
180	    dev, 0, minphys, uio));
181}
182
183static void
184physwakeup(bp)
185	struct buf *bp;
186{
187	wakeup((caddr_t) bp);
188	bp->b_flags &= ~B_CALL;
189}
190