Home | History | Annotate | Line # | Download | only in uvm
uvm_fault_i.h revision 1.21
      1  1.21    perry /*	$NetBSD: uvm_fault_i.h,v 1.21 2006/02/16 20:17:20 perry Exp $	*/
      2   1.1      mrg 
      3   1.1      mrg /*
      4   1.1      mrg  *
      5   1.1      mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      6   1.1      mrg  * All rights reserved.
      7   1.1      mrg  *
      8   1.1      mrg  * Redistribution and use in source and binary forms, with or without
      9   1.1      mrg  * modification, are permitted provided that the following conditions
     10   1.1      mrg  * are met:
     11   1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     12   1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     13   1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     15   1.1      mrg  *    documentation and/or other materials provided with the distribution.
     16   1.1      mrg  * 3. All advertising materials mentioning features or use of this software
     17   1.1      mrg  *    must display the following acknowledgement:
     18   1.1      mrg  *      This product includes software developed by Charles D. Cranor and
     19   1.1      mrg  *      Washington University.
     20   1.1      mrg  * 4. The name of the author may not be used to endorse or promote products
     21   1.1      mrg  *    derived from this software without specific prior written permission.
     22   1.1      mrg  *
     23   1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24   1.1      mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25   1.1      mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26   1.1      mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27   1.1      mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28   1.1      mrg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29   1.1      mrg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30   1.1      mrg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31   1.1      mrg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32   1.1      mrg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33   1.3      mrg  *
     34   1.3      mrg  * from: Id: uvm_fault_i.h,v 1.1.6.1 1997/12/08 16:07:12 chuck Exp
     35   1.1      mrg  */
     36   1.1      mrg 
     37   1.4    perry #ifndef _UVM_UVM_FAULT_I_H_
     38   1.4    perry #define _UVM_UVM_FAULT_I_H_
     39   1.4    perry 
     40   1.1      mrg /*
     41   1.1      mrg  * uvm_fault_i.h: fault inline functions
     42   1.1      mrg  */
     43   1.1      mrg 
     44   1.1      mrg /*
     45   1.1      mrg  * uvmfault_unlockmaps: unlock the maps
     46   1.1      mrg  */
     47   1.1      mrg 
     48  1.21    perry static __inline void
     49  1.18  thorpej uvmfault_unlockmaps(struct uvm_faultinfo *ufi, boolean_t write_locked)
     50   1.5      mrg {
     51  1.10      chs 	/*
     52  1.10      chs 	 * ufi can be NULL when this isn't really a fault,
     53  1.10      chs 	 * but merely paging in anon data.
     54  1.10      chs 	 */
     55  1.10      chs 
     56  1.10      chs 	if (ufi == NULL) {
     57  1.10      chs 		return;
     58  1.10      chs 	}
     59   1.1      mrg 
     60   1.5      mrg 	if (write_locked) {
     61   1.5      mrg 		vm_map_unlock(ufi->map);
     62   1.5      mrg 	} else {
     63   1.5      mrg 		vm_map_unlock_read(ufi->map);
     64   1.5      mrg 	}
     65   1.1      mrg }
     66   1.1      mrg 
     67   1.1      mrg /*
     68   1.1      mrg  * uvmfault_unlockall: unlock everything passed in.
     69   1.1      mrg  *
     70   1.1      mrg  * => maps must be read-locked (not write-locked).
     71   1.1      mrg  */
     72   1.1      mrg 
     73  1.21    perry static __inline void
     74  1.18  thorpej uvmfault_unlockall(struct uvm_faultinfo *ufi, struct vm_amap *amap,
     75  1.18  thorpej     struct uvm_object *uobj, struct vm_anon *anon)
     76   1.5      mrg {
     77   1.1      mrg 
     78   1.5      mrg 	if (anon)
     79   1.5      mrg 		simple_unlock(&anon->an_lock);
     80   1.5      mrg 	if (uobj)
     81   1.5      mrg 		simple_unlock(&uobj->vmobjlock);
     82   1.5      mrg 	if (amap)
     83   1.7    chuck 		amap_unlock(amap);
     84   1.5      mrg 	uvmfault_unlockmaps(ufi, FALSE);
     85   1.9  thorpej }
     86   1.9  thorpej 
     87   1.9  thorpej /*
     88   1.1      mrg  * uvmfault_lookup: lookup a virtual address in a map
     89   1.1      mrg  *
     90   1.6    chuck  * => caller must provide a uvm_faultinfo structure with the IN
     91   1.1      mrg  *	params properly filled in
     92   1.6    chuck  * => we will lookup the map entry (handling submaps) as we go
     93   1.1      mrg  * => if the lookup is a success we will return with the maps locked
     94   1.1      mrg  * => if "write_lock" is TRUE, we write_lock the map, otherwise we only
     95   1.1      mrg  *	get a read lock.
     96  1.12      chs  * => note that submaps can only appear in the kernel and they are
     97   1.6    chuck  *	required to use the same virtual addresses as the map they
     98   1.6    chuck  *	are referenced by (thus address translation between the main
     99   1.6    chuck  *	map and the submap is unnecessary).
    100   1.1      mrg  */
    101   1.1      mrg 
    102  1.21    perry static __inline boolean_t
    103  1.18  thorpej uvmfault_lookup(struct uvm_faultinfo *ufi, boolean_t write_lock)
    104   1.1      mrg {
    105  1.13      chs 	struct vm_map *tmpmap;
    106   1.1      mrg 
    107   1.5      mrg 	/*
    108   1.5      mrg 	 * init ufi values for lookup.
    109   1.5      mrg 	 */
    110   1.5      mrg 
    111   1.5      mrg 	ufi->map = ufi->orig_map;
    112   1.5      mrg 	ufi->size = ufi->orig_size;
    113   1.5      mrg 
    114   1.5      mrg 	/*
    115   1.5      mrg 	 * keep going down levels until we are done.   note that there can
    116   1.5      mrg 	 * only be two levels so we won't loop very long.
    117   1.5      mrg 	 */
    118   1.5      mrg 
    119  1.16    perry 	/*CONSTCOND*/
    120   1.5      mrg 	while (1) {
    121  1.14  thorpej 		/*
    122  1.14  thorpej 		 * Make sure this is not an "interrupt safe" map.
    123  1.14  thorpej 		 * Such maps are never supposed to be involved in
    124  1.14  thorpej 		 * a fault.
    125  1.14  thorpej 		 */
    126  1.14  thorpej 		if (ufi->map->flags & VM_MAP_INTRSAFE)
    127  1.14  thorpej 			return (FALSE);
    128   1.5      mrg 
    129   1.5      mrg 		/*
    130   1.5      mrg 		 * lock map
    131   1.5      mrg 		 */
    132   1.5      mrg 		if (write_lock) {
    133   1.5      mrg 			vm_map_lock(ufi->map);
    134   1.5      mrg 		} else {
    135   1.5      mrg 			vm_map_lock_read(ufi->map);
    136   1.5      mrg 		}
    137   1.5      mrg 
    138   1.5      mrg 		/*
    139   1.5      mrg 		 * lookup
    140   1.5      mrg 		 */
    141  1.12      chs 		if (!uvm_map_lookup_entry(ufi->map, ufi->orig_rvaddr,
    142   1.6    chuck 								&ufi->entry)) {
    143   1.5      mrg 			uvmfault_unlockmaps(ufi, write_lock);
    144   1.5      mrg 			return(FALSE);
    145   1.5      mrg 		}
    146   1.5      mrg 
    147   1.5      mrg 		/*
    148   1.5      mrg 		 * reduce size if necessary
    149   1.5      mrg 		 */
    150   1.6    chuck 		if (ufi->entry->end - ufi->orig_rvaddr < ufi->size)
    151   1.6    chuck 			ufi->size = ufi->entry->end - ufi->orig_rvaddr;
    152   1.5      mrg 
    153   1.5      mrg 		/*
    154   1.5      mrg 		 * submap?    replace map with the submap and lookup again.
    155   1.5      mrg 		 * note: VAs in submaps must match VAs in main map.
    156   1.5      mrg 		 */
    157   1.5      mrg 		if (UVM_ET_ISSUBMAP(ufi->entry)) {
    158   1.5      mrg 			tmpmap = ufi->entry->object.sub_map;
    159   1.5      mrg 			if (write_lock) {
    160   1.5      mrg 				vm_map_unlock(ufi->map);
    161   1.5      mrg 			} else {
    162   1.5      mrg 				vm_map_unlock_read(ufi->map);
    163   1.5      mrg 			}
    164   1.5      mrg 			ufi->map = tmpmap;
    165   1.5      mrg 			continue;
    166   1.5      mrg 		}
    167   1.5      mrg 
    168   1.5      mrg 		/*
    169   1.5      mrg 		 * got it!
    170   1.5      mrg 		 */
    171   1.1      mrg 
    172   1.5      mrg 		ufi->mapv = ufi->map->timestamp;
    173   1.5      mrg 		return(TRUE);
    174   1.1      mrg 
    175   1.5      mrg 	}	/* while loop */
    176   1.1      mrg 
    177   1.5      mrg 	/*NOTREACHED*/
    178   1.1      mrg }
    179   1.1      mrg 
    180   1.1      mrg /*
    181   1.1      mrg  * uvmfault_relock: attempt to relock the same version of the map
    182   1.1      mrg  *
    183   1.1      mrg  * => fault data structures should be unlocked before calling.
    184   1.1      mrg  * => if a success (TRUE) maps will be locked after call.
    185   1.1      mrg  */
    186   1.1      mrg 
    187  1.21    perry static __inline boolean_t
    188  1.18  thorpej uvmfault_relock(struct uvm_faultinfo *ufi)
    189   1.5      mrg {
    190  1.10      chs 	/*
    191  1.10      chs 	 * ufi can be NULL when this isn't really a fault,
    192  1.10      chs 	 * but merely paging in anon data.
    193  1.10      chs 	 */
    194  1.10      chs 
    195  1.10      chs 	if (ufi == NULL) {
    196  1.10      chs 		return TRUE;
    197  1.10      chs 	}
    198   1.1      mrg 
    199   1.5      mrg 	uvmexp.fltrelck++;
    200  1.10      chs 
    201   1.5      mrg 	/*
    202  1.12      chs 	 * relock map.   fail if version mismatch (in which case nothing
    203   1.6    chuck 	 * gets locked).
    204   1.5      mrg 	 */
    205   1.5      mrg 
    206   1.5      mrg 	vm_map_lock_read(ufi->map);
    207   1.5      mrg 	if (ufi->mapv != ufi->map->timestamp) {
    208   1.5      mrg 		vm_map_unlock_read(ufi->map);
    209   1.5      mrg 		return(FALSE);
    210   1.5      mrg 	}
    211   1.1      mrg 
    212   1.5      mrg 	uvmexp.fltrelckok++;
    213  1.15      chs 	return(TRUE);
    214   1.1      mrg }
    215   1.4    perry 
    216   1.4    perry #endif /* _UVM_UVM_FAULT_I_H_ */
    217