xref: /netbsd/sys/uvm/uvm_io.c (revision 8d6196f9)
1*8d6196f9Schs /*	$NetBSD: uvm_io.c,v 1.29 2020/09/21 18:41:59 chs Exp $	*/
2f2caacc7Smrg 
3f2caacc7Smrg /*
4f2caacc7Smrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5f2caacc7Smrg  * All rights reserved.
6f2caacc7Smrg  *
7f2caacc7Smrg  * Redistribution and use in source and binary forms, with or without
8f2caacc7Smrg  * modification, are permitted provided that the following conditions
9f2caacc7Smrg  * are met:
10f2caacc7Smrg  * 1. Redistributions of source code must retain the above copyright
11f2caacc7Smrg  *    notice, this list of conditions and the following disclaimer.
12f2caacc7Smrg  * 2. Redistributions in binary form must reproduce the above copyright
13f2caacc7Smrg  *    notice, this list of conditions and the following disclaimer in the
14f2caacc7Smrg  *    documentation and/or other materials provided with the distribution.
15f2caacc7Smrg  *
16f2caacc7Smrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17f2caacc7Smrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18f2caacc7Smrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19f2caacc7Smrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20f2caacc7Smrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21f2caacc7Smrg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22f2caacc7Smrg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23f2caacc7Smrg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24f2caacc7Smrg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25f2caacc7Smrg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261f6b921cSmrg  *
271f6b921cSmrg  * from: Id: uvm_io.c,v 1.1.2.2 1997/12/30 12:02:00 mrg Exp
28f2caacc7Smrg  */
29f2caacc7Smrg 
30f2caacc7Smrg /*
31f2caacc7Smrg  * uvm_io.c: uvm i/o ops
32f2caacc7Smrg  */
33f2caacc7Smrg 
34b616d1caSlukem #include <sys/cdefs.h>
35*8d6196f9Schs __KERNEL_RCSID(0, "$NetBSD: uvm_io.c,v 1.29 2020/09/21 18:41:59 chs Exp $");
36b616d1caSlukem 
37f2caacc7Smrg #include <sys/param.h>
38f2caacc7Smrg #include <sys/systm.h>
39f2caacc7Smrg #include <sys/mman.h>
40f2caacc7Smrg #include <sys/uio.h>
41f2caacc7Smrg 
42f2caacc7Smrg #include <uvm/uvm.h>
43f2caacc7Smrg 
44f2caacc7Smrg /*
45f2caacc7Smrg  * functions
46f2caacc7Smrg  */
47f2caacc7Smrg 
48f2caacc7Smrg /*
49f2caacc7Smrg  * uvm_io: perform I/O on a map
50f2caacc7Smrg  *
51f2caacc7Smrg  * => caller must have a reference to "map" so that it doesn't go away
52f2caacc7Smrg  *    while we are working.
53f2caacc7Smrg  */
54f2caacc7Smrg 
558106d135Smrg int
uvm_io(struct vm_map * map,struct uio * uio,int flags)561b7c20bdSchristos uvm_io(struct vm_map *map, struct uio *uio, int flags)
57f2caacc7Smrg {
58a2dd74edSeeh 	vaddr_t baseva, endva, pageoffset, kva;
59a2dd74edSeeh 	vsize_t chunksz, togo, sz;
60821ec03eSchs 	struct vm_map_entry *dead_entries;
61f2caacc7Smrg 	int error;
62f2caacc7Smrg 
63f2caacc7Smrg 	/*
64f2caacc7Smrg 	 * step 0: sanity checks and set up for copy loop.  start with a
65f2caacc7Smrg 	 * large chunk size.  if we have trouble finding vm space we will
66f2caacc7Smrg 	 * reduce it.
67f2caacc7Smrg 	 */
68f2caacc7Smrg 
69f2caacc7Smrg 	if (uio->uio_resid == 0)
70f2caacc7Smrg 		return(0);
71f2caacc7Smrg 	togo = uio->uio_resid;
72f2caacc7Smrg 
73a2dd74edSeeh 	baseva = (vaddr_t) uio->uio_offset;
74f2caacc7Smrg 	endva = baseva + (togo - 1);
75f2caacc7Smrg 
76f2caacc7Smrg 	if (endva < baseva)   /* wrap around? */
77f2caacc7Smrg 		return(EIO);
78f2caacc7Smrg 
79f2caacc7Smrg 	if (baseva >= VM_MAXUSER_ADDRESS)
80f2caacc7Smrg 		return(0);
81f2caacc7Smrg 	if (endva >= VM_MAXUSER_ADDRESS)
828106d135Smrg 		/* EOF truncate */
838106d135Smrg 		togo = togo - (endva - VM_MAXUSER_ADDRESS + 1);
84f2caacc7Smrg 	pageoffset = baseva & PAGE_MASK;
85f2caacc7Smrg 	baseva = trunc_page(baseva);
8685c8cfb5Stls 	chunksz = MIN(round_page(togo + pageoffset), trunc_page(MAXPHYS));
87f2caacc7Smrg 	error = 0;
88f2caacc7Smrg 
891b7c20bdSchristos 	flags |= UVM_EXTRACT_QREF | UVM_EXTRACT_CONTIG | UVM_EXTRACT_FIXPROT;
90*8d6196f9Schs 
91*8d6196f9Schs 	/* XXX cannot use QREF with without AMAP_REFALL, and REFALL is unsafe */
92*8d6196f9Schs 	flags &= ~UVM_EXTRACT_QREF;
93*8d6196f9Schs 
94f2caacc7Smrg 	/*
95f2caacc7Smrg 	 * step 1: main loop...  while we've got data to move
96f2caacc7Smrg 	 */
97f2caacc7Smrg 
98f2caacc7Smrg 	for (/*null*/; togo > 0 ; pageoffset = 0) {
99f2caacc7Smrg 
100f2caacc7Smrg 		/*
101f2caacc7Smrg 		 * step 2: extract mappings from the map into kernel_map
102f2caacc7Smrg 		 */
103f2caacc7Smrg 
104f2caacc7Smrg 		error = uvm_map_extract(map, baseva, chunksz, kernel_map, &kva,
1051b7c20bdSchristos 		    flags);
106f2caacc7Smrg 		if (error) {
107f2caacc7Smrg 
108f2caacc7Smrg 			/* retry with a smaller chunk... */
109f2caacc7Smrg 			if (error == ENOMEM && chunksz > PAGE_SIZE) {
110f2caacc7Smrg 				chunksz = trunc_page(chunksz / 2);
111f2caacc7Smrg 				if (chunksz < PAGE_SIZE)
112f2caacc7Smrg 					chunksz = PAGE_SIZE;
113f2caacc7Smrg 				continue;
114f2caacc7Smrg 			}
115f2caacc7Smrg 
116f2caacc7Smrg 			break;
117f2caacc7Smrg 		}
118f2caacc7Smrg 
119f2caacc7Smrg 		/*
120f2caacc7Smrg 		 * step 3: move a chunk of data
121f2caacc7Smrg 		 */
122f2caacc7Smrg 
123f2caacc7Smrg 		sz = chunksz - pageoffset;
124f2caacc7Smrg 		if (sz > togo)
125f2caacc7Smrg 			sz = togo;
12653524e44Schristos 		error = uiomove((void *) (kva + pageoffset), sz, uio);
127f2caacc7Smrg 		togo -= sz;
128f2caacc7Smrg 		baseva += chunksz;
129f2caacc7Smrg 
130f2caacc7Smrg 		/*
131f2caacc7Smrg 		 * step 4: unmap the area of kernel memory
132f2caacc7Smrg 		 */
133f2caacc7Smrg 
134f2caacc7Smrg 		vm_map_lock(kernel_map);
1351207308bSyamt 		uvm_unmap_remove(kernel_map, kva, kva + chunksz, &dead_entries,
13655b033b1Spara 		   0);
137f2caacc7Smrg 		vm_map_unlock(kernel_map);
138f2caacc7Smrg 		if (dead_entries != NULL)
139f2caacc7Smrg 			uvm_unmap_detach(dead_entries, AMAP_REFALL);
140b794108bSchs 
141b794108bSchs 		if (error)
142b794108bSchs 			break;
143f2caacc7Smrg 	}
144f2caacc7Smrg 	return (error);
145f2caacc7Smrg }
146