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