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