uvm_io.c revision 1.6 1 /* $NetBSD: uvm_io.c,v 1.6 1998/08/13 02:11:00 eeh Exp $ */
2
3 /*
4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 */
7 /*
8 *
9 * Copyright (c) 1997 Charles D. Cranor and Washington University.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Charles D. Cranor and
23 * Washington University.
24 * 4. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * from: Id: uvm_io.c,v 1.1.2.2 1997/12/30 12:02:00 mrg Exp
39 */
40
41 /*
42 * uvm_io.c: uvm i/o ops
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mman.h>
48 #include <sys/proc.h>
49 #include <sys/malloc.h>
50 #include <sys/uio.h>
51
52 #include <vm/vm.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_kern.h>
55
56 #include <uvm/uvm.h>
57
58 /*
59 * functions
60 */
61
62 /*
63 * uvm_io: perform I/O on a map
64 *
65 * => caller must have a reference to "map" so that it doesn't go away
66 * while we are working.
67 */
68
69 int
70 uvm_io(map, uio)
71 vm_map_t map;
72 struct uio *uio;
73 {
74 vaddr_t baseva, endva, pageoffset, kva;
75 vsize_t chunksz, togo, sz;
76 vm_map_entry_t dead_entries;
77 int error;
78
79 /*
80 * step 0: sanity checks and set up for copy loop. start with a
81 * large chunk size. if we have trouble finding vm space we will
82 * reduce it.
83 */
84
85 if (uio->uio_resid == 0)
86 return(0);
87 togo = uio->uio_resid;
88
89 baseva = (vaddr_t) uio->uio_offset;
90 endva = baseva + (togo - 1);
91
92 if (endva < baseva) /* wrap around? */
93 return(EIO);
94
95 if (baseva >= VM_MAXUSER_ADDRESS)
96 return(0);
97 if (endva >= VM_MAXUSER_ADDRESS)
98 /* EOF truncate */
99 togo = togo - (endva - VM_MAXUSER_ADDRESS + 1);
100 pageoffset = baseva & PAGE_MASK;
101 baseva = trunc_page(baseva);
102 chunksz = min(round_page(togo + pageoffset), MAXBSIZE);
103 error = 0;
104
105 /*
106 * step 1: main loop... while we've got data to move
107 */
108
109 for (/*null*/; togo > 0 ; pageoffset = 0) {
110
111 /*
112 * step 2: extract mappings from the map into kernel_map
113 */
114
115 error = uvm_map_extract(map, baseva, chunksz, kernel_map, &kva,
116 UVM_EXTRACT_QREF | UVM_EXTRACT_CONTIG |
117 UVM_EXTRACT_FIXPROT);
118 if (error) {
119
120 /* retry with a smaller chunk... */
121 if (error == ENOMEM && chunksz > PAGE_SIZE) {
122 chunksz = trunc_page(chunksz / 2);
123 if (chunksz < PAGE_SIZE)
124 chunksz = PAGE_SIZE;
125 continue;
126 }
127
128 break;
129 }
130
131 /*
132 * step 3: move a chunk of data
133 */
134
135 sz = chunksz - pageoffset;
136 if (sz > togo)
137 sz = togo;
138 error = uiomove((caddr_t) (kva + pageoffset), sz, uio);
139 if (error)
140 break;
141 togo -= sz;
142 baseva += chunksz;
143
144
145 /*
146 * step 4: unmap the area of kernel memory
147 */
148
149 vm_map_lock(kernel_map);
150 (void)uvm_unmap_remove(kernel_map, kva, kva+chunksz, 1,
151 &dead_entries);
152 vm_map_unlock(kernel_map);
153
154 if (dead_entries != NULL)
155 uvm_unmap_detach(dead_entries, AMAP_REFALL);
156 }
157
158 /*
159 * done
160 */
161
162 return (error);
163 }
164