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