]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_physio.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1986, 1990, 1993
27 * The Regents of the University of California. All rights reserved.
28 * (c) UNIX System Laboratories, Inc.
29 * All or some portions of this file are derived from material licensed
30 * to the University of California by American Telephone and Telegraph
31 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
32 * the permission of UNIX System Laboratories, Inc.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * from: @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
66 * 27-July-97 Umesh Vaishampayan (umeshv@apple.com)
67 * Allow physio() to kernel space.
70 #include <sys/param.h>
71 #include <sys/systm.h>
77 physio(strategy
, bp
, dev
, flags
, minphys
, uio
, blocksize
)
87 struct proc
*p
= current_proc();
88 int error
, done
, i
, nobuf
, s
, todo
;
91 flags
&= B_READ
| B_WRITE
;
94 * [check user read/write access to the data buffer]
96 * Check each iov one by one. Note that we know if we're reading or
97 * writing, so we ignore the uio's rw parameter. Also note that if
98 * we're doing a read, that's a *write* to user-space.
100 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
101 if(uio
->uio_segflg
!= UIO_SYSSPACE
) {
102 if (!useracc(uio
->uio_iov
[i
].iov_base
,
103 uio
->uio_iov
[i
].iov_len
,
104 (flags
== B_READ
) ? B_WRITE
: B_READ
))
108 /* Make sure we have a buffer, creating one if necessary. */
109 if (nobuf
= (bp
== NULL
)) {
110 // bp = getphysbuf();
111 panic("physio: null buf pointer\n");
114 /* [raise the processor priority level to splbio;] */
117 /* [while the buffer is marked busy] */
118 while (bp
->b_flags
& B_BUSY
) {
119 /* [mark the buffer wanted] */
120 bp
->b_flags
|= B_WANTED
;
121 /* [wait until the buffer is available] */
122 tsleep((caddr_t
)bp
, PRIBIO
+1, "physbuf", 0);
125 /* Mark it busy, so nobody else will use it. */
126 bp
->b_flags
|= B_BUSY
;
128 /* [lower the priority level] */
131 /* [set up the fixed part of the buffer for a transfer] */
137 * [while there are data to transfer and no I/O error]
138 * Note that I/O errors are handled with a 'goto' at the bottom
139 * of the 'while' loop.
141 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
142 iovp
= &uio
->uio_iov
[i
];
143 while (iovp
->iov_len
> 0) {
145 * [mark the buffer busy for physical I/O]
146 * (i.e. set B_PHYS (because it's an I/O to user
147 * memory, and B_RAW, because B_RAW is to be
148 * "Set by physio for raw transfers.", in addition
149 * to the "busy" and read/write flag.)
152 bp
->b_flags
= B_BUSY
| B_PHYS
| B_RAW
| flags
;
155 /* [set up the buffer for a maximum-sized transfer] */
156 bp
->b_blkno
= uio
->uio_offset
/ blocksize
;
157 bp
->b_bcount
= iovp
->iov_len
;
158 bp
->b_data
= iovp
->iov_base
;
161 * [call minphys to bound the tranfer size]
162 * and remember the amount of data to transfer,
163 * for later comparison.
169 * [lock the part of the user address space involved
171 * Beware vmapbuf(); it clobbers b_data and
172 * saves it in b_saveaddr. However, vunmapbuf()
176 if(uio
->uio_segflg
!= UIO_SYSSPACE
)
177 vslock(bp
->b_data
, todo
);
182 /* [call strategy to start the transfer] */
186 * Note that the raise/wait/lower/get error
187 * steps below would be done by biowait(), but
188 * we want to unlock the address space before
189 * we lower the priority.
191 * [raise the priority level to splbio]
195 /* [wait for the transfer to complete] */
196 while ((bp
->b_flags
& B_DONE
) == 0)
197 tsleep((caddr_t
) bp
, PRIBIO
+ 1, "physio", 0);
200 * [unlock the part of the address space previously
206 if(uio
->uio_segflg
!= UIO_SYSSPACE
)
207 vsunlock(bp
->b_data
, todo
);
209 /* remember error value (save a splbio/splx pair) */
210 if (bp
->b_flags
& B_ERROR
)
211 error
= (bp
->b_error
? bp
->b_error
: EIO
);
213 /* [lower the priority level] */
217 * [deduct the transfer size from the total number
218 * of data to transfer]
220 done
= bp
->b_bcount
- bp
->b_resid
;
221 iovp
->iov_len
-= done
;
222 iovp
->iov_base
+= done
;
223 uio
->uio_offset
+= done
;
224 uio
->uio_resid
-= done
;
227 * Now, check for an error.
228 * Also, handle weird end-of-disk semantics.
230 if (error
|| done
< todo
)
237 * [clean up the state of the buffer]
238 * Remember if somebody wants it, so we can wake them up below.
239 * Also, if we had to steal it, give it back.
242 bp
->b_flags
&= ~(B_BUSY
| B_PHYS
| B_RAW
);
251 * [if another process is waiting for the raw I/O buffer,
252 * wake up processes waiting to do physical I/O;
254 if (bp
->b_flags
& B_WANTED
) {
255 bp
->b_flags
&= ~B_WANTED
;
265 * Leffler, et al., says on p. 231:
266 * "The minphys() routine is called by physio() to adjust the
267 * size of each I/O transfer before the latter is passed to
268 * the strategy routine..."
270 * so, just adjust the buffer's count accounting to MAXPHYS here,
271 * and return the new count;
278 bp
->b_bcount
= min(MAXPHYS
, bp
->b_bcount
);
283 * Do a read on a device for a user process.
289 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
290 dev
, B_READ
, minphys
, uio
, DEV_BSIZE
));
294 * Do a write on a device for a user process.
300 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
301 dev
, B_WRITE
, minphys
, uio
, DEV_BSIZE
));