Home | History | Annotate | Line # | Download | only in uvm
uvm_fault_i.h revision 1.5
      1 /*	$NetBSD: uvm_fault_i.h,v 1.5 1998/03/09 00:58:56 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_fault_i.h,v 1.1.6.1 1997/12/08 16:07:12 chuck Exp
     39  */
     40 
     41 #ifndef _UVM_UVM_FAULT_I_H_
     42 #define _UVM_UVM_FAULT_I_H_
     43 
     44 /*
     45  * uvm_fault_i.h: fault inline functions
     46  */
     47 
     48 /*
     49  * uvmfault_unlockmaps: unlock the maps
     50  */
     51 
     52 static __inline void
     53 uvmfault_unlockmaps(ufi, write_locked)
     54 	struct uvm_faultinfo *ufi;
     55 	boolean_t write_locked;
     56 {
     57 
     58 	if (write_locked) {
     59 		vm_map_unlock(ufi->map);
     60 		if (ufi->parent_map) vm_map_unlock(ufi->parent_map);
     61 	} else {
     62 		vm_map_unlock_read(ufi->map);
     63 		if (ufi->parent_map) vm_map_unlock_read(ufi->parent_map);
     64 	}
     65 }
     66 
     67 /*
     68  * uvmfault_unlockall: unlock everything passed in.
     69  *
     70  * => maps must be read-locked (not write-locked).
     71  */
     72 
     73 static __inline void
     74 uvmfault_unlockall(ufi, amap, uobj, anon)
     75 	struct uvm_faultinfo *ufi;
     76 	struct vm_amap *amap;
     77 	struct uvm_object *uobj;
     78 	struct vm_anon *anon;
     79 {
     80 
     81 	if (anon)
     82 		simple_unlock(&anon->an_lock);
     83 	if (uobj)
     84 		simple_unlock(&uobj->vmobjlock);
     85 	if (amap)
     86 		simple_unlock(&amap->am_l);
     87 	uvmfault_unlockmaps(ufi, FALSE);
     88 }
     89 
     90 /*
     91  * uvmfault_lookup: lookup a virtual address in a map
     92  *
     93  * => caller must provide a uvm_faultinfo structure with the (IN)
     94  *	params properly filled in
     95  * => we will lookup the map entry and fill in parent_map, etc, as we go
     96  * => if the lookup is a success we will return with the maps locked
     97  * => if "write_lock" is TRUE, we write_lock the map, otherwise we only
     98  *	get a read lock.
     99  * => currently we require sharemaps to have the same virtual addresses
    100  *	as the main maps they are attached to.   in other words, the share
    101  *	map starts at zero and goes to the map user address.   the main
    102  *	map references it by setting its offset to be the same as the
    103  *	starting virtual address.    if we ever wanted to have sharemaps
    104  *	have different virtual addresses than main maps we would calculate
    105  *	it like:
    106  *		share_va = (rvaddr - entry->start) + entry->offset
    107  *	[i.e. offset from start of map entry plus offset of mapping]
    108  *	since (entry->start == entry->offset), share_va must equal rvaddr.
    109  *	if we need to change this we should store share_va in rvaddr
    110  *	and move rvaddr to orig_rvaddr.
    111  */
    112 
    113 static __inline boolean_t
    114 uvmfault_lookup(ufi, write_lock)
    115 	struct uvm_faultinfo *ufi;
    116 	boolean_t write_lock;
    117 {
    118 	vm_map_t tmpmap;
    119 
    120 	/*
    121 	 * init ufi values for lookup.
    122 	 */
    123 
    124 	ufi->map = ufi->orig_map;
    125 	ufi->rvaddr = ufi->orig_rvaddr;
    126 	ufi->parent_map = NULL;
    127 	ufi->size = ufi->orig_size;
    128 
    129 	/*
    130 	 * keep going down levels until we are done.   note that there can
    131 	 * only be two levels so we won't loop very long.
    132 	 */
    133 
    134 	while (1) {
    135 
    136 		/*
    137 		 * lock map
    138 		 */
    139 		if (write_lock) {
    140 			vm_map_lock(ufi->map);
    141 		} else {
    142 			vm_map_lock_read(ufi->map);
    143 		}
    144 
    145 		/*
    146 		 * lookup
    147 		 */
    148 		if (!uvm_map_lookup_entry(ufi->map, ufi->rvaddr, &ufi->entry)) {
    149 			uvmfault_unlockmaps(ufi, write_lock);
    150 			return(FALSE);
    151 		}
    152 
    153 		/*
    154 		 * reduce size if necessary
    155 		 */
    156 		if (ufi->entry->end - ufi->rvaddr < ufi->size)
    157 			ufi->size = ufi->entry->end - ufi->rvaddr;
    158 
    159 		/*
    160 		 * submap?    replace map with the submap and lookup again.
    161 		 * note: VAs in submaps must match VAs in main map.
    162 		 */
    163 		if (UVM_ET_ISSUBMAP(ufi->entry)) {
    164 			if (ufi->parent_map)
    165 				panic("uvmfault_lookup: submap inside a "
    166 				    "sharemap (illegal)");
    167 			tmpmap = ufi->entry->object.sub_map;
    168 			if (write_lock) {
    169 				vm_map_unlock(ufi->map);
    170 			} else {
    171 				vm_map_unlock_read(ufi->map);
    172 			}
    173 			ufi->map = tmpmap;
    174 			continue;
    175 		}
    176 
    177 		/*
    178 		 * share map?  drop down a level.   already taken care of
    179 		 * submap case.
    180 		 */
    181 		if (UVM_ET_ISMAP(ufi->entry)) {
    182 			if (ufi->parent_map)
    183 				panic("uvmfault_lookup: sharemap inside a "
    184 				    "sharemap (illegal)");
    185 			ufi->parent_map = ufi->map;
    186 			ufi->parentv = ufi->parent_map->timestamp;
    187 			ufi->map = ufi->entry->object.share_map;
    188 #ifdef DIAGNOSTIC
    189 			/* see note above */
    190 			if (ufi->entry->offset != ufi->entry->start)
    191 				panic("uvmfault_lookup: sharemap VA != "
    192 				    "mainmap VA (not supported)");
    193 #endif
    194 			continue;
    195 		}
    196 
    197 		/*
    198 		 * got it!
    199 		 */
    200 
    201 		ufi->mapv = ufi->map->timestamp;
    202 		return(TRUE);
    203 
    204 	}	/* while loop */
    205 
    206 	/*NOTREACHED*/
    207 }
    208 
    209 /*
    210  * uvmfault_relock: attempt to relock the same version of the map
    211  *
    212  * => fault data structures should be unlocked before calling.
    213  * => if a success (TRUE) maps will be locked after call.
    214  */
    215 
    216 static __inline boolean_t
    217 uvmfault_relock(ufi)
    218 	struct uvm_faultinfo *ufi;
    219 {
    220 
    221 	uvmexp.fltrelck++;
    222 	/*
    223 	 * simply relock parent (if any) then map in order.   fail if version
    224 	 * mismatch (in which case nothing gets locked).
    225 	 */
    226 
    227 	if (ufi->parent_map) {
    228 		vm_map_lock_read(ufi->parent_map);
    229 		if (ufi->parentv != ufi->parent_map->timestamp) {
    230 			vm_map_unlock_read(ufi->parent_map);
    231 			return(FALSE);
    232 		}
    233 	}
    234 
    235 	vm_map_lock_read(ufi->map);
    236 	if (ufi->mapv != ufi->map->timestamp) {
    237 		if (ufi->parent_map)
    238 			vm_map_unlock_read(ufi->parent_map);
    239 		vm_map_unlock_read(ufi->map);
    240 		return(FALSE);
    241 	}
    242 
    243 	uvmexp.fltrelckok++;
    244 	return(TRUE);		/* got it! */
    245 }
    246 
    247 #endif /* _UVM_UVM_FAULT_I_H_ */
    248