1*8c7187e8Sderaadt /* $OpenBSD: uvm_io.c,v 1.30 2022/10/07 14:59:39 deraadt Exp $ */
21414b0faSart /* $NetBSD: uvm_io.c,v 1.12 2000/06/27 17:29:23 mrg Exp $ */
3cd7ee8acSart
4cd7ee8acSart /*
5cd7ee8acSart * Copyright (c) 1997 Charles D. Cranor and Washington University.
6cd7ee8acSart * All rights reserved.
7cd7ee8acSart *
8cd7ee8acSart * Redistribution and use in source and binary forms, with or without
9cd7ee8acSart * modification, are permitted provided that the following conditions
10cd7ee8acSart * are met:
11cd7ee8acSart * 1. Redistributions of source code must retain the above copyright
12cd7ee8acSart * notice, this list of conditions and the following disclaimer.
13cd7ee8acSart * 2. Redistributions in binary form must reproduce the above copyright
14cd7ee8acSart * notice, this list of conditions and the following disclaimer in the
15cd7ee8acSart * documentation and/or other materials provided with the distribution.
16cd7ee8acSart *
17cd7ee8acSart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18cd7ee8acSart * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19cd7ee8acSart * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20cd7ee8acSart * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21cd7ee8acSart * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22cd7ee8acSart * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23cd7ee8acSart * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24cd7ee8acSart * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25cd7ee8acSart * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26cd7ee8acSart * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27cd7ee8acSart *
28cd7ee8acSart * from: Id: uvm_io.c,v 1.1.2.2 1997/12/30 12:02:00 mrg Exp
29cd7ee8acSart */
30cd7ee8acSart
31cd7ee8acSart /*
32cd7ee8acSart * uvm_io.c: uvm i/o ops
33cd7ee8acSart */
34cd7ee8acSart
35cd7ee8acSart #include <sys/param.h>
36cd7ee8acSart #include <sys/systm.h>
37cd7ee8acSart #include <sys/mman.h>
38cd7ee8acSart #include <sys/uio.h>
39cd7ee8acSart
40cd7ee8acSart #include <uvm/uvm.h>
41cd7ee8acSart
42cd7ee8acSart /*
43cd7ee8acSart * functions
44cd7ee8acSart */
45cd7ee8acSart
46cd7ee8acSart /*
47cd7ee8acSart * uvm_io: perform I/O on a map
48cd7ee8acSart *
49cd7ee8acSart * => caller must have a reference to "map" so that it doesn't go away
50cd7ee8acSart * while we are working.
51cd7ee8acSart */
52cd7ee8acSart
53cd7ee8acSart int
uvm_io(vm_map_t map,struct uio * uio,int flags)54e00eec33Smiod uvm_io(vm_map_t map, struct uio *uio, int flags)
55cd7ee8acSart {
56cd7ee8acSart vaddr_t baseva, endva, pageoffset, kva;
57cd7ee8acSart vsize_t chunksz, togo, sz;
58181c6205Sariane struct uvm_map_deadq dead_entries;
59e00eec33Smiod int error, extractflags;
60cd7ee8acSart
61cd7ee8acSart /*
62cd7ee8acSart * step 0: sanity checks and set up for copy loop. start with a
63cd7ee8acSart * large chunk size. if we have trouble finding vm space we will
64cd7ee8acSart * reduce it.
65cd7ee8acSart */
66cd7ee8acSart if (uio->uio_resid == 0)
67cd7ee8acSart return(0);
68cd7ee8acSart togo = uio->uio_resid;
69cd7ee8acSart
70cd7ee8acSart baseva = (vaddr_t) uio->uio_offset;
71cd7ee8acSart endva = baseva + (togo - 1);
72cd7ee8acSart
73cd7ee8acSart if (endva < baseva) /* wrap around? */
74cd7ee8acSart return(EIO);
75cd7ee8acSart
76cd7ee8acSart if (baseva >= VM_MAXUSER_ADDRESS)
77cd7ee8acSart return(0);
78cd7ee8acSart if (endva >= VM_MAXUSER_ADDRESS)
79cd7ee8acSart /* EOF truncate */
80cd7ee8acSart togo = togo - (endva - VM_MAXUSER_ADDRESS + 1);
81cd7ee8acSart pageoffset = baseva & PAGE_MASK;
82cd7ee8acSart baseva = trunc_page(baseva);
83cd7ee8acSart chunksz = min(round_page(togo + pageoffset), MAXBSIZE);
84cd7ee8acSart error = 0;
85cd7ee8acSart
86181c6205Sariane extractflags = 0;
87e00eec33Smiod if (flags & UVM_IO_FIXPROT)
88e00eec33Smiod extractflags |= UVM_EXTRACT_FIXPROT;
89e00eec33Smiod
9052887a38Smpi /*
9152887a38Smpi * step 1: main loop... while we've got data to move
9252887a38Smpi */
93cd7ee8acSart for (/*null*/; togo > 0 ; pageoffset = 0) {
9452887a38Smpi
9552887a38Smpi /*
9652887a38Smpi * step 2: extract mappings from the map into kernel_map
9752887a38Smpi */
98181c6205Sariane error = uvm_map_extract(map, baseva, chunksz, &kva,
99e00eec33Smiod extractflags);
100cd7ee8acSart if (error) {
101cd7ee8acSart
102cd7ee8acSart /* retry with a smaller chunk... */
103cd7ee8acSart if (error == ENOMEM && chunksz > PAGE_SIZE) {
104cd7ee8acSart chunksz = trunc_page(chunksz / 2);
105cd7ee8acSart if (chunksz < PAGE_SIZE)
106cd7ee8acSart chunksz = PAGE_SIZE;
107cd7ee8acSart continue;
108cd7ee8acSart }
109cd7ee8acSart
110cd7ee8acSart break;
111cd7ee8acSart }
112cd7ee8acSart
11352887a38Smpi /*
11452887a38Smpi * step 3: move a chunk of data
11552887a38Smpi */
116cd7ee8acSart sz = chunksz - pageoffset;
117cd7ee8acSart if (sz > togo)
118cd7ee8acSart sz = togo;
1190c006d07Skettenis error = uiomove((caddr_t) (kva + pageoffset), sz, uio);
120cd7ee8acSart togo -= sz;
121cd7ee8acSart baseva += chunksz;
122cd7ee8acSart
12352887a38Smpi
12452887a38Smpi /*
12552887a38Smpi * step 4: unmap the area of kernel memory
12652887a38Smpi */
127cd7ee8acSart vm_map_lock(kernel_map);
128181c6205Sariane TAILQ_INIT(&dead_entries);
12993cbfefaSart uvm_unmap_remove(kernel_map, kva, kva+chunksz,
130*8c7187e8Sderaadt &dead_entries, FALSE, TRUE, FALSE);
131cd7ee8acSart vm_map_unlock(kernel_map);
132181c6205Sariane uvm_unmap_detach(&dead_entries, AMAP_REFALL);
1334136a7edSart
1344136a7edSart if (error)
1354136a7edSart break;
136cd7ee8acSart }
137cd7ee8acSart
138cd7ee8acSart return (error);
139cd7ee8acSart }
140