Add diclaimer of copyright to _osname() manual page.
[unix-history] / sys / kern / kern__physio.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1989, 1990, 1991, 1992 William F. Jolitz, TeleMuse
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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This software is a component of "386BSD" developed by
16 William F. Jolitz, TeleMuse.
17 * 4. Neither the name of the developer nor the name "386BSD"
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
25 * NOT MAKE USE THIS WORK.
26 *
27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
29 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
ce619eaa 48 * $Id: kern__physio.c,v 1.6 1994/01/14 16:24:47 davidg Exp $
15637ed4 49 */
15637ed4
RG
50
51#include "param.h"
52#include "systm.h"
53#include "buf.h"
54#include "conf.h"
55#include "proc.h"
56#include "malloc.h"
57#include "vnode.h"
58#include "vm/vm.h"
59#include "specdev.h"
60
15637ed4
RG
61/*
62 * Driver interface to do "raw" I/O in the address space of a
63 * user process directly for read and write operations..
64 */
65
4c45483e 66int
15637ed4 67rawread(dev, uio)
1d7f5b47 68 dev_t dev; struct uio *uio;
15637ed4 69{
1d7f5b47
DG
70 return (uioapply(physio, (caddr_t) cdevsw[major(dev)].d_strategy,
71 (caddr_t) (u_long) dev, uio));
15637ed4
RG
72}
73
4c45483e 74int
15637ed4 75rawwrite(dev, uio)
1d7f5b47 76 dev_t dev; struct uio *uio;
15637ed4 77{
1d7f5b47
DG
78 return (uioapply(physio, (caddr_t) cdevsw[major(dev)].d_strategy,
79 (caddr_t) (u_long) dev, uio));
15637ed4
RG
80}
81
1d7f5b47
DG
82
83int physio(strat, dev, bp, off, rw, base, len, p)
84 d_strategy_t strat;
15637ed4 85 dev_t dev;
519fb2b7 86 struct buf *bp;
15637ed4
RG
87 int rw, off;
88 caddr_t base;
89 int *len;
90 struct proc *p;
91{
1d7f5b47
DG
92 int amttodo = *len;
93 int error, amtdone;
15637ed4 94 vm_prot_t ftype;
1d7f5b47 95 vm_offset_t v, lastv;
15637ed4 96 caddr_t adr;
1d7f5b47
DG
97 int oldflags;
98 int s;
99
519fb2b7 100 int bp_alloc = (bp == 0);
15637ed4 101
1d7f5b47
DG
102/*
103 * keep the process from being swapped
104 */
105 oldflags = p->p_flag;
106 p->p_flag |= SPHYSIO;
107
15637ed4
RG
108 rw = rw == UIO_READ ? B_READ : 0;
109
110 /* create and build a buffer header for a transfer */
519fb2b7
RG
111
112 if (bp_alloc) {
1d7f5b47 113 bp = (struct buf *)getpbuf();
519fb2b7 114 bzero((char *)bp, sizeof(*bp)); /* 09 Sep 92*/
1d7f5b47
DG
115 } else {
116 s = splbio();
ce619eaa 117 while (bp->b_flags & B_BUSY) {
1d7f5b47
DG
118 bp->b_flags |= B_WANTED;
119 tsleep((caddr_t)bp, PRIBIO, "physbw", 0);
120 }
121 bp->b_flags |= B_BUSY;
122 splx(s);
519fb2b7 123 }
1d7f5b47 124
15637ed4
RG
125 bp->b_flags = B_BUSY | B_PHYS | rw;
126 bp->b_proc = p;
127 bp->b_dev = dev;
128 bp->b_error = 0;
129 bp->b_blkno = off/DEV_BSIZE;
130 amtdone = 0;
131
132 /* iteratively do I/O on as large a chunk as possible */
133 do {
134 bp->b_flags &= ~B_DONE;
135 bp->b_un.b_addr = base;
136 /* XXX limit */
137 bp->b_bcount = min (256*1024, amttodo);
138
139 /* first, check if accessible */
140 if (rw == B_READ && !useracc(base, bp->b_bcount, B_WRITE)) {
1d7f5b47
DG
141 error = EFAULT;
142 goto errrtn;
15637ed4
RG
143 }
144 if (rw == B_WRITE && !useracc(base, bp->b_bcount, B_READ)) {
1d7f5b47
DG
145 error = EFAULT;
146 goto errrtn;
15637ed4
RG
147 }
148
149 /* update referenced and dirty bits, handle copy objects */
150 if (rw == B_READ)
151 ftype = VM_PROT_READ | VM_PROT_WRITE;
152 else
153 ftype = VM_PROT_READ;
1d7f5b47
DG
154
155 lastv = 0;
156 for (adr = (caddr_t)trunc_page(base); adr < base + bp->b_bcount;
15637ed4 157 adr += NBPG) {
1d7f5b47
DG
158
159/*
160 * make sure that the pde is valid and wired
161 */
ce619eaa
DG
162 v = trunc_page(((vm_offset_t)vtopte(adr)));
163 if (v != lastv) {
1d7f5b47
DG
164 vm_map_pageable(&p->p_vmspace->vm_map, v,
165 round_page(v+1), FALSE);
166 lastv = v;
167 }
168
169/*
ce619eaa
DG
170 * do the vm_fault if needed, do the copy-on-write thing when
171 * reading stuff off device into memory.
1d7f5b47 172 */
ce619eaa
DG
173 if (ftype & VM_PROT_WRITE) {
174 /*
175 * properly handle copy-on-write
176 */
1d7f5b47 177 *(volatile int *) adr += 0;
ce619eaa
DG
178 }
179#if 0
180 else {
181 /*
182 * this clause is not really necessary because
183 * vslock does a vm_map_pageable FALSE.
184 * It is not optimally efficient to reference the
185 * page with the possiblity of it being paged out, but
186 * if this page is faulted here, it will be placed on the
187 * active queue, with the probability of it being paged
188 * out being very very low. This is here primarily for
189 * "symmetry".
190 */
1d7f5b47 191 *(volatile int *) adr;
ce619eaa
DG
192 }
193#endif
15637ed4 194 }
ce619eaa
DG
195/*
196 * if the process has been blocked by the wiring of the page table pages
197 * above or faults of other pages, then the vm_map_pageable contained in the
198 * vslock will fault the pages back in if they have been paged out since
199 * being referenced in the loop above. (vm_map_pageable calls vm_fault_wire
200 * which calls vm_fault to get the pages if needed.)
201 */
15637ed4 202
ce619eaa 203 /* lock in core (perform vm_map_pageable, FALSE) */
15637ed4
RG
204 vslock (base, bp->b_bcount);
205
206 /* perform transfer */
207 physstrat(bp, strat, PRIBIO);
208
ce619eaa 209 /* unlock (perform vm_map_pageable, TRUE) */
15637ed4 210 vsunlock (base, bp->b_bcount, 0);
1d7f5b47
DG
211
212 lastv = 0;
213
214/*
215 * unwire the pde
216 */
217 for (adr = (caddr_t)trunc_page(base); adr < base + bp->b_bcount;
218 adr += NBPG) {
ce619eaa
DG
219 v = trunc_page(((vm_offset_t)vtopte(adr)));
220 if (v != lastv) {
1d7f5b47
DG
221 vm_map_pageable(&p->p_vmspace->vm_map, v, round_page(v+1), TRUE);
222 lastv = v;
223 }
224 }
225
226
15637ed4
RG
227 amtdone = bp->b_bcount - bp->b_resid;
228 amttodo -= amtdone;
229 base += amtdone;
230 bp->b_blkno += amtdone/DEV_BSIZE;
231 } while (amttodo && (bp->b_flags & B_ERROR) == 0 && amtdone > 0);
232
233 error = bp->b_error;
1d7f5b47
DG
234errrtn:
235 if (bp_alloc) {
236 relpbuf(bp);
237 } else {
238 bp->b_flags &= ~B_BUSY;
239 wakeup((caddr_t)bp);
240 }
15637ed4 241 *len = amttodo;
1d7f5b47
DG
242
243/*
244 * allow the process to be swapped
245 */
246 p->p_flag &= ~SPHYSIO;
247 p->p_flag |= (oldflags & SPHYSIO);
248
15637ed4
RG
249 return (error);
250}