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