Home | History | Annotate | Line # | Download | only in uvm
uvm_fault_i.h revision 1.13
      1  1.13      chs /*	$NetBSD: uvm_fault_i.h,v 1.13 2001/06/02 18:09:26 chs 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.11      mrg static boolean_t uvmfault_check_intrsafe __P((struct uvm_faultinfo *));
     44  1.11      mrg static boolean_t uvmfault_lookup __P((struct uvm_faultinfo *, boolean_t));
     45  1.11      mrg static boolean_t uvmfault_relock __P((struct uvm_faultinfo *));
     46  1.11      mrg static void uvmfault_unlockall __P((struct uvm_faultinfo *, struct vm_amap *,
     47  1.11      mrg 			            struct uvm_object *, struct vm_anon *));
     48  1.11      mrg static void uvmfault_unlockmaps __P((struct uvm_faultinfo *, boolean_t));
     49   1.1      mrg 
     50   1.1      mrg /*
     51   1.1      mrg  * uvmfault_unlockmaps: unlock the maps
     52   1.1      mrg  */
     53   1.1      mrg 
     54   1.5      mrg static __inline void
     55   1.5      mrg uvmfault_unlockmaps(ufi, write_locked)
     56   1.5      mrg 	struct uvm_faultinfo *ufi;
     57   1.5      mrg 	boolean_t write_locked;
     58   1.5      mrg {
     59  1.10      chs 	/*
     60  1.10      chs 	 * ufi can be NULL when this isn't really a fault,
     61  1.10      chs 	 * but merely paging in anon data.
     62  1.10      chs 	 */
     63  1.10      chs 
     64  1.10      chs 	if (ufi == NULL) {
     65  1.10      chs 		return;
     66  1.10      chs 	}
     67   1.1      mrg 
     68   1.5      mrg 	if (write_locked) {
     69   1.5      mrg 		vm_map_unlock(ufi->map);
     70   1.5      mrg 	} else {
     71   1.5      mrg 		vm_map_unlock_read(ufi->map);
     72   1.5      mrg 	}
     73   1.1      mrg }
     74   1.1      mrg 
     75   1.1      mrg /*
     76   1.1      mrg  * uvmfault_unlockall: unlock everything passed in.
     77   1.1      mrg  *
     78   1.1      mrg  * => maps must be read-locked (not write-locked).
     79   1.1      mrg  */
     80   1.1      mrg 
     81   1.5      mrg static __inline void
     82   1.5      mrg uvmfault_unlockall(ufi, amap, uobj, anon)
     83   1.5      mrg 	struct uvm_faultinfo *ufi;
     84   1.5      mrg 	struct vm_amap *amap;
     85   1.5      mrg 	struct uvm_object *uobj;
     86   1.5      mrg 	struct vm_anon *anon;
     87   1.5      mrg {
     88   1.1      mrg 
     89   1.5      mrg 	if (anon)
     90   1.5      mrg 		simple_unlock(&anon->an_lock);
     91   1.5      mrg 	if (uobj)
     92   1.5      mrg 		simple_unlock(&uobj->vmobjlock);
     93   1.5      mrg 	if (amap)
     94   1.7    chuck 		amap_unlock(amap);
     95   1.5      mrg 	uvmfault_unlockmaps(ufi, FALSE);
     96   1.9  thorpej }
     97   1.9  thorpej 
     98   1.9  thorpej /*
     99   1.9  thorpej  * uvmfault_check_intrsafe: check for a virtual address managed by
    100   1.9  thorpej  * an interrupt-safe map.
    101   1.9  thorpej  *
    102   1.9  thorpej  * => caller must provide a uvm_faultinfo structure with the IN
    103   1.9  thorpej  *	params properly filled in
    104   1.9  thorpej  * => if we find an intersafe VA, we fill in ufi->map, and return TRUE
    105   1.9  thorpej  */
    106   1.9  thorpej 
    107   1.9  thorpej static __inline boolean_t
    108   1.9  thorpej uvmfault_check_intrsafe(ufi)
    109   1.9  thorpej 	struct uvm_faultinfo *ufi;
    110   1.9  thorpej {
    111   1.9  thorpej 	struct vm_map_intrsafe *vmi;
    112   1.9  thorpej 	int s;
    113   1.9  thorpej 
    114   1.9  thorpej 	s = vmi_list_lock();
    115   1.9  thorpej 	for (vmi = LIST_FIRST(&vmi_list); vmi != NULL;
    116   1.9  thorpej 	     vmi = LIST_NEXT(vmi, vmi_list)) {
    117   1.9  thorpej 		if (ufi->orig_rvaddr >= vm_map_min(&vmi->vmi_map) &&
    118   1.9  thorpej 		    ufi->orig_rvaddr < vm_map_max(&vmi->vmi_map))
    119   1.9  thorpej 			break;
    120   1.9  thorpej 	}
    121   1.9  thorpej 	vmi_list_unlock(s);
    122   1.9  thorpej 
    123   1.9  thorpej 	if (vmi != NULL) {
    124   1.9  thorpej 		ufi->map = &vmi->vmi_map;
    125   1.9  thorpej 		return (TRUE);
    126   1.9  thorpej 	}
    127   1.9  thorpej 
    128   1.9  thorpej 	return (FALSE);
    129   1.1      mrg }
    130   1.1      mrg 
    131   1.1      mrg /*
    132   1.1      mrg  * uvmfault_lookup: lookup a virtual address in a map
    133   1.1      mrg  *
    134   1.6    chuck  * => caller must provide a uvm_faultinfo structure with the IN
    135   1.1      mrg  *	params properly filled in
    136   1.6    chuck  * => we will lookup the map entry (handling submaps) as we go
    137   1.1      mrg  * => if the lookup is a success we will return with the maps locked
    138   1.1      mrg  * => if "write_lock" is TRUE, we write_lock the map, otherwise we only
    139   1.1      mrg  *	get a read lock.
    140  1.12      chs  * => note that submaps can only appear in the kernel and they are
    141   1.6    chuck  *	required to use the same virtual addresses as the map they
    142   1.6    chuck  *	are referenced by (thus address translation between the main
    143   1.6    chuck  *	map and the submap is unnecessary).
    144   1.1      mrg  */
    145   1.1      mrg 
    146   1.5      mrg static __inline boolean_t
    147   1.5      mrg uvmfault_lookup(ufi, write_lock)
    148   1.5      mrg 	struct uvm_faultinfo *ufi;
    149   1.5      mrg 	boolean_t write_lock;
    150   1.1      mrg {
    151  1.13      chs 	struct vm_map *tmpmap;
    152   1.1      mrg 
    153   1.5      mrg 	/*
    154   1.5      mrg 	 * init ufi values for lookup.
    155   1.5      mrg 	 */
    156   1.5      mrg 
    157   1.5      mrg 	ufi->map = ufi->orig_map;
    158   1.5      mrg 	ufi->size = ufi->orig_size;
    159   1.5      mrg 
    160   1.5      mrg 	/*
    161   1.5      mrg 	 * keep going down levels until we are done.   note that there can
    162   1.5      mrg 	 * only be two levels so we won't loop very long.
    163   1.5      mrg 	 */
    164   1.5      mrg 
    165   1.5      mrg 	while (1) {
    166   1.5      mrg 
    167   1.5      mrg 		/*
    168   1.5      mrg 		 * lock map
    169   1.5      mrg 		 */
    170   1.5      mrg 		if (write_lock) {
    171   1.5      mrg 			vm_map_lock(ufi->map);
    172   1.5      mrg 		} else {
    173   1.5      mrg 			vm_map_lock_read(ufi->map);
    174   1.5      mrg 		}
    175   1.5      mrg 
    176   1.5      mrg 		/*
    177   1.5      mrg 		 * lookup
    178   1.5      mrg 		 */
    179  1.12      chs 		if (!uvm_map_lookup_entry(ufi->map, ufi->orig_rvaddr,
    180   1.6    chuck 								&ufi->entry)) {
    181   1.5      mrg 			uvmfault_unlockmaps(ufi, write_lock);
    182   1.5      mrg 			return(FALSE);
    183   1.5      mrg 		}
    184   1.5      mrg 
    185   1.5      mrg 		/*
    186   1.5      mrg 		 * reduce size if necessary
    187   1.5      mrg 		 */
    188   1.6    chuck 		if (ufi->entry->end - ufi->orig_rvaddr < ufi->size)
    189   1.6    chuck 			ufi->size = ufi->entry->end - ufi->orig_rvaddr;
    190   1.5      mrg 
    191   1.5      mrg 		/*
    192   1.5      mrg 		 * submap?    replace map with the submap and lookup again.
    193   1.5      mrg 		 * note: VAs in submaps must match VAs in main map.
    194   1.5      mrg 		 */
    195   1.5      mrg 		if (UVM_ET_ISSUBMAP(ufi->entry)) {
    196   1.5      mrg 			tmpmap = ufi->entry->object.sub_map;
    197   1.5      mrg 			if (write_lock) {
    198   1.5      mrg 				vm_map_unlock(ufi->map);
    199   1.5      mrg 			} else {
    200   1.5      mrg 				vm_map_unlock_read(ufi->map);
    201   1.5      mrg 			}
    202   1.5      mrg 			ufi->map = tmpmap;
    203   1.5      mrg 			continue;
    204   1.5      mrg 		}
    205   1.5      mrg 
    206   1.5      mrg 		/*
    207   1.5      mrg 		 * got it!
    208   1.5      mrg 		 */
    209   1.1      mrg 
    210   1.5      mrg 		ufi->mapv = ufi->map->timestamp;
    211   1.5      mrg 		return(TRUE);
    212   1.1      mrg 
    213   1.5      mrg 	}	/* while loop */
    214   1.1      mrg 
    215   1.5      mrg 	/*NOTREACHED*/
    216   1.1      mrg }
    217   1.1      mrg 
    218   1.1      mrg /*
    219   1.1      mrg  * uvmfault_relock: attempt to relock the same version of the map
    220   1.1      mrg  *
    221   1.1      mrg  * => fault data structures should be unlocked before calling.
    222   1.1      mrg  * => if a success (TRUE) maps will be locked after call.
    223   1.1      mrg  */
    224   1.1      mrg 
    225   1.5      mrg static __inline boolean_t
    226   1.5      mrg uvmfault_relock(ufi)
    227   1.5      mrg 	struct uvm_faultinfo *ufi;
    228   1.5      mrg {
    229  1.10      chs 	/*
    230  1.10      chs 	 * ufi can be NULL when this isn't really a fault,
    231  1.10      chs 	 * but merely paging in anon data.
    232  1.10      chs 	 */
    233  1.10      chs 
    234  1.10      chs 	if (ufi == NULL) {
    235  1.10      chs 		return TRUE;
    236  1.10      chs 	}
    237   1.1      mrg 
    238   1.5      mrg 	uvmexp.fltrelck++;
    239  1.10      chs 
    240   1.5      mrg 	/*
    241  1.12      chs 	 * relock map.   fail if version mismatch (in which case nothing
    242   1.6    chuck 	 * gets locked).
    243   1.5      mrg 	 */
    244   1.5      mrg 
    245   1.5      mrg 	vm_map_lock_read(ufi->map);
    246   1.5      mrg 	if (ufi->mapv != ufi->map->timestamp) {
    247   1.5      mrg 		vm_map_unlock_read(ufi->map);
    248   1.5      mrg 		return(FALSE);
    249   1.5      mrg 	}
    250   1.1      mrg 
    251   1.5      mrg 	uvmexp.fltrelckok++;
    252   1.5      mrg 	return(TRUE);		/* got it! */
    253   1.1      mrg }
    254   1.4    perry 
    255   1.4    perry #endif /* _UVM_UVM_FAULT_I_H_ */
    256