]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_physio.c
xnu-124.1.tar.gz
[apple/xnu.git] / bsd / kern / kern_physio.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*-
23 * Copyright (c) 1982, 1986, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
25 * (c) UNIX System Laboratories, Inc.
26 * All or some portions of this file are derived from material licensed
27 * to the University of California by American Telephone and Telegraph
28 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
29 * the permission of UNIX System Laboratories, Inc.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * from: @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
60 */
61/*
62 * HISTORY
63 * 27-July-97 Umesh Vaishampayan (umeshv@apple.com)
64 * Allow physio() to kernel space.
65 */
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/buf.h>
70#include <sys/conf.h>
71#include <sys/proc.h>
72
73int
74physio(strategy, bp, dev, flags, minphys, uio, blocksize)
75 void (*strategy)();
76 struct buf *bp;
77 dev_t dev;
78 int flags;
79 u_int (*minphys)();
80 struct uio *uio;
81 int blocksize;
82{
83 struct iovec *iovp;
84 struct proc *p = current_proc();
85 int error, done, i, nobuf, s, todo;
86
87 error = 0;
88 flags &= B_READ | B_WRITE;
89
90 /*
91 * [check user read/write access to the data buffer]
92 *
93 * Check each iov one by one. Note that we know if we're reading or
94 * writing, so we ignore the uio's rw parameter. Also note that if
95 * we're doing a read, that's a *write* to user-space.
96 */
97 for (i = 0; i < uio->uio_iovcnt; i++) {
98 if(uio->uio_segflg != UIO_SYSSPACE) {
99 if (!useracc(uio->uio_iov[i].iov_base,
100 uio->uio_iov[i].iov_len,
101 (flags == B_READ) ? B_WRITE : B_READ))
102 return (EFAULT);
103 }
104 }
105 /* Make sure we have a buffer, creating one if necessary. */
106 if (nobuf = (bp == NULL)) {
107// bp = getphysbuf();
108 panic("physio: null buf pointer\n");
109 }
110
111 /* [raise the processor priority level to splbio;] */
112 s = splbio();
113
114 /* [while the buffer is marked busy] */
115 while (bp->b_flags & B_BUSY) {
116 /* [mark the buffer wanted] */
117 bp->b_flags |= B_WANTED;
118 /* [wait until the buffer is available] */
119 tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0);
120 }
121
122 /* Mark it busy, so nobody else will use it. */
123 bp->b_flags |= B_BUSY;
124
125 /* [lower the priority level] */
126 splx(s);
127
128 /* [set up the fixed part of the buffer for a transfer] */
129 bp->b_dev = dev;
130 bp->b_error = 0;
131 bp->b_proc = p;
132
133 /*
134 * [while there are data to transfer and no I/O error]
135 * Note that I/O errors are handled with a 'goto' at the bottom
136 * of the 'while' loop.
137 */
138 for (i = 0; i < uio->uio_iovcnt; i++) {
139 iovp = &uio->uio_iov[i];
140 while (iovp->iov_len > 0) {
141 /*
142 * [mark the buffer busy for physical I/O]
143 * (i.e. set B_PHYS (because it's an I/O to user
144 * memory, and B_RAW, because B_RAW is to be
145 * "Set by physio for raw transfers.", in addition
146 * to the "busy" and read/write flag.)
147 */
148 s = splbio();
149 bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags;
150 splx(s);
151
152 /* [set up the buffer for a maximum-sized transfer] */
153 bp->b_blkno = uio->uio_offset / blocksize;
154 bp->b_bcount = iovp->iov_len;
155 bp->b_data = iovp->iov_base;
156
157 /*
158 * [call minphys to bound the tranfer size]
159 * and remember the amount of data to transfer,
160 * for later comparison.
161 */
162 (*minphys)(bp);
163 todo = bp->b_bcount;
164
165 /*
166 * [lock the part of the user address space involved
167 * in the transfer]
168 * Beware vmapbuf(); it clobbers b_data and
169 * saves it in b_saveaddr. However, vunmapbuf()
170 * restores it.
171 */
172
173 if(uio->uio_segflg != UIO_SYSSPACE)
174 vslock(bp->b_data, todo);
175
176#if 0
177 vmapbuf(bp, todo);
178#endif /* 0 */
179 /* [call strategy to start the transfer] */
180 (*strategy)(bp);
181
182 /*
183 * Note that the raise/wait/lower/get error
184 * steps below would be done by biowait(), but
185 * we want to unlock the address space before
186 * we lower the priority.
187 *
188 * [raise the priority level to splbio]
189 */
190 s = splbio();
191
192 /* [wait for the transfer to complete] */
193 while ((bp->b_flags & B_DONE) == 0)
194 tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0);
195
196 /*
197 * [unlock the part of the address space previously
198 * locked]
199 */
200#if 0
201 vunmapbuf(bp, todo);
202#endif /* 0 */
203 if(uio->uio_segflg != UIO_SYSSPACE)
204 vsunlock(bp->b_data, todo);
205
206 /* remember error value (save a splbio/splx pair) */
207 if (bp->b_flags & B_ERROR)
208 error = (bp->b_error ? bp->b_error : EIO);
209
210 /* [lower the priority level] */
211 splx(s);
212
213 /*
214 * [deduct the transfer size from the total number
215 * of data to transfer]
216 */
217 done = bp->b_bcount - bp->b_resid;
218 iovp->iov_len -= done;
219 iovp->iov_base += done;
220 uio->uio_offset += done;
221 uio->uio_resid -= done;
222
223 /*
224 * Now, check for an error.
225 * Also, handle weird end-of-disk semantics.
226 */
227 if (error || done < todo)
228 goto done;
229 }
230 }
231
232done:
233 /*
234 * [clean up the state of the buffer]
235 * Remember if somebody wants it, so we can wake them up below.
236 * Also, if we had to steal it, give it back.
237 */
238 s = splbio();
239 bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW);
240#if 0
241 if (nobuf)
242 putphysbuf(bp);
243
244 else
245#endif /* 0 */
246 {
247 /*
248 * [if another process is waiting for the raw I/O buffer,
249 * wake up processes waiting to do physical I/O;
250 */
251 if (bp->b_flags & B_WANTED) {
252 bp->b_flags &= ~B_WANTED;
253 wakeup(bp);
254 }
255 }
256 splx(s);
257
258 return (error);
259}
260
261/*
262 * Leffler, et al., says on p. 231:
263 * "The minphys() routine is called by physio() to adjust the
264 * size of each I/O transfer before the latter is passed to
265 * the strategy routine..."
266 *
267 * so, just adjust the buffer's count accounting to MAXPHYS here,
268 * and return the new count;
269 */
270u_int
271minphys(bp)
272 struct buf *bp;
273{
274
275 bp->b_bcount = min(MAXPHYS, bp->b_bcount);
276 return bp->b_bcount;
277}
278
279/*
280 * Do a read on a device for a user process.
281 */
282rawread(dev, uio)
283 dev_t dev;
284 struct uio *uio;
285{
286 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
287 dev, B_READ, minphys, uio, DEV_BSIZE));
288}
289
290/*
291 * Do a write on a device for a user process.
292 */
293rawwrite(dev, uio)
294 dev_t dev;
295 struct uio *uio;
296{
297 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
298 dev, B_WRITE, minphys, uio, DEV_BSIZE));
299}