uvm_io.c revision 1.2 1 /* $NetBSD: uvm_io.c,v 1.2 1998/02/06 22:31:53 thorpej 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
39 /*
40 * uvm_io.c: uvm i/o ops
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mount.h>
46 #include <sys/mman.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/uio.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_kern.h>
54
55 #include <sys/syscallargs.h>
56
57 #include <uvm/uvm.h>
58
59 /*
60 * functions
61 */
62
63 /*
64 * uvm_io: perform I/O on a map
65 *
66 * => caller must have a reference to "map" so that it doesn't go away
67 * while we are working.
68 */
69
70 int uvm_io(map, uio)
71
72 vm_map_t map;
73 struct uio *uio;
74
75 {
76 vm_offset_t baseva, endva, pageoffset, kva;
77 vm_size_t chunksz, togo, sz;
78 vm_map_entry_t dead_entries;
79 int error;
80
81 /*
82 * step 0: sanity checks and set up for copy loop. start with a
83 * large chunk size. if we have trouble finding vm space we will
84 * reduce it.
85 */
86
87 if (uio->uio_resid == 0)
88 return(0);
89 togo = uio->uio_resid;
90
91 baseva = (vm_offset_t) uio->uio_offset;
92 endva = baseva + (togo - 1);
93
94 if (endva < baseva) /* wrap around? */
95 return(EIO);
96
97 if (baseva >= VM_MAXUSER_ADDRESS)
98 return(0);
99 if (endva >= VM_MAXUSER_ADDRESS)
100 togo = togo - (endva - VM_MAXUSER_ADDRESS + 1); /* EOF truncate */
101 pageoffset = baseva & PAGE_MASK;
102 baseva = trunc_page(baseva);
103 chunksz = min(round_page(togo + pageoffset), MAXBSIZE);
104 error = 0;
105
106 /*
107 * step 1: main loop... while we've got data to move
108 */
109
110 for (/*null*/; togo > 0 ; pageoffset = 0) {
111
112 /*
113 * step 2: extract mappings from the map into kernel_map
114 */
115
116 error = uvm_map_extract(map, baseva, chunksz, kernel_map, &kva,
117 UVM_EXTRACT_QREF | UVM_EXTRACT_CONTIG |
118 UVM_EXTRACT_FIXPROT);
119 if (error) {
120
121 /* retry with a smaller chunk... */
122 if (error == ENOMEM && chunksz > PAGE_SIZE) {
123 chunksz = trunc_page(chunksz / 2);
124 if (chunksz < PAGE_SIZE)
125 chunksz = PAGE_SIZE;
126 continue;
127 }
128
129 break;
130 }
131
132 /*
133 * step 3: move a chunk of data
134 */
135
136 sz = chunksz - pageoffset;
137 if (sz > togo)
138 sz = togo;
139 error = uiomove((caddr_t) (kva + pageoffset), sz, uio);
140 if (error)
141 break;
142 togo -= sz;
143 baseva += chunksz;
144
145
146 /*
147 * step 4: unmap the area of kernel memory
148 */
149
150 vm_map_lock(kernel_map);
151 (void) uvm_unmap_remove(kernel_map, kva, kva+chunksz, 1, &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 /*
160 * done
161 */
162
163 return(error);
164 }
165