]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/read_fault.c
xnu-123.5.tar.gz
[apple/xnu.git] / osfmk / i386 / read_fault.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52
53 #include <kern/thread.h>
54 #include <vm/vm_fault.h>
55 #include <mach/kern_return.h>
56 #include <mach/vm_behavior.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/pmap.h>
61
62 #include <i386/intel_read_fault.h>
63
64 #include <kern/macro_help.h>
65
66 /*
67 * Expansion of vm_fault for read fault in kernel mode.
68 * Must enter the mapping as writable, since the i386
69 * (and i860 in i386 compatability mode) ignores write
70 * protection in kernel mode.
71 *
72 * Note that this routine can be called for pmap's other
73 * than the kernel_pmap, in which case it just enters
74 * a read-only mapping. (See e.g. kernel_trap().)
75 */
76 kern_return_t
77 intel_read_fault(
78 vm_map_t map,
79 vm_offset_t vaddr)
80 {
81 vm_map_version_t version; /* Map version for
82 verification */
83 vm_object_t object; /* Top-level object */
84 vm_object_offset_t offset; /* Top-level offset */
85 vm_prot_t prot; /* Protection for mapping */
86 vm_behavior_t behavior; /* Expected paging behavior */
87 vm_object_offset_t lo_offset, hi_offset;
88 vm_page_t result_page; /* Result of vm_fault_page */
89 vm_page_t top_page; /* Placeholder page */
90 boolean_t wired; /* Is map region wired? */
91 kern_return_t result;
92 register vm_page_t m;
93 vm_map_t pmap_map;
94 vm_map_t original_map = map;
95
96 RetryFault:
97
98 map = original_map;
99
100 /*
101 * Find the backing store object and offset into it
102 * to begin search.
103 */
104 vm_map_lock_read(map);
105 result = vm_map_lookup_locked(&map, vaddr, VM_PROT_READ, &version,
106 &object, &offset, &prot, &wired,
107 &behavior, &lo_offset,
108 &hi_offset, &pmap_map);
109
110 vm_map_unlock_read(map);
111
112 if (result != KERN_SUCCESS) {
113 return (result);
114 }
115
116 if(pmap_map != map) {
117 vm_map_reference(pmap_map);
118 vm_map_unlock_read(pmap_map);
119 }
120
121 /*
122 * Make a reference to this object to prevent its
123 * disposal while we are playing with it.
124 */
125 assert(object->ref_count > 0);
126 object->ref_count++;
127 vm_object_res_reference(object);
128 vm_object_paging_begin(object);
129
130 result = vm_fault_page(object, offset, VM_PROT_READ, FALSE,
131 THREAD_ABORTSAFE,
132 lo_offset, hi_offset, behavior,
133 &prot, &result_page, &top_page, (int *)0,
134 0, map->no_zero_fill, FALSE);
135
136 if (result != VM_FAULT_SUCCESS) {
137 vm_object_deallocate(object);
138 if(pmap_map != map) {
139 vm_map_deallocate(pmap_map);
140 }
141
142 switch (result) {
143 case VM_FAULT_RETRY:
144 goto RetryFault;
145 case VM_FAULT_INTERRUPTED:
146 return (KERN_SUCCESS);
147 case VM_FAULT_MEMORY_SHORTAGE:
148 VM_PAGE_WAIT();
149 goto RetryFault;
150 case VM_FAULT_FICTITIOUS_SHORTAGE:
151 vm_page_more_fictitious();
152 goto RetryFault;
153 case VM_FAULT_MEMORY_ERROR:
154 return (KERN_MEMORY_ERROR);
155 }
156 }
157
158 m = result_page;
159
160 /*
161 * How to clean up the result of vm_fault_page. This
162 * happens whether the mapping is entered or not.
163 */
164
165 #define UNLOCK_AND_DEALLOCATE \
166 MACRO_BEGIN \
167 vm_fault_cleanup(m->object, top_page); \
168 vm_object_deallocate(object); \
169 MACRO_END
170
171 /*
172 * What to do with the resulting page from vm_fault_page
173 * if it doesn't get entered into the physical map:
174 */
175
176 #define RELEASE_PAGE(m) \
177 MACRO_BEGIN \
178 PAGE_WAKEUP_DONE(m); \
179 vm_page_lock_queues(); \
180 if (!m->active && !m->inactive) \
181 vm_page_activate(m); \
182 vm_page_unlock_queues(); \
183 MACRO_END
184
185 /*
186 * We must verify that the maps have not changed.
187 */
188 vm_object_unlock(m->object);
189
190 if ((map != original_map) || !vm_map_verify(map, &version)) {
191 vm_object_t retry_object;
192 vm_object_offset_t retry_offset;
193 vm_prot_t retry_prot;
194
195 if (map != pmap_map) {
196 vm_map_deallocate(pmap_map);
197 }
198
199 map = original_map;
200 vm_map_lock_read(map);
201
202 result = vm_map_lookup_locked(&map, vaddr, VM_PROT_READ, &version,
203 &retry_object, &retry_offset, &retry_prot,
204 &wired, &behavior, &lo_offset,
205 &hi_offset, &pmap_map);
206
207 if (result != KERN_SUCCESS) {
208 vm_map_unlock_read(map);
209 vm_object_lock(m->object);
210 RELEASE_PAGE(m);
211 UNLOCK_AND_DEALLOCATE;
212 return (result);
213 }
214
215 if (map != pmap_map) {
216 vm_map_reference(pmap_map);
217 }
218
219 vm_object_unlock(retry_object);
220
221 if (retry_object != object || retry_offset != offset) {
222 vm_object_lock(m->object);
223 RELEASE_PAGE(m);
224 vm_map_unlock_read(map);
225 if(pmap_map != map) {
226 vm_map_unlock_read(pmap_map);
227 vm_map_deallocate(pmap_map);
228 }
229 UNLOCK_AND_DEALLOCATE;
230 goto RetryFault;
231 }
232 }
233
234 /*
235 * Put the page in the physical map.
236 */
237
238 PMAP_ENTER(pmap_map->pmap, vaddr, m, VM_PROT_READ, wired);
239
240 if(pmap_map != map) {
241 vm_map_unlock_read(pmap_map);
242 vm_map_deallocate(pmap_map);
243 }
244
245 vm_object_lock(m->object);
246 vm_page_lock_queues();
247 if (!m->active && !m->inactive)
248 vm_page_activate(m);
249 m->reference = TRUE;
250 vm_page_unlock_queues();
251
252 vm_map_verify_done(map, &version);
253 PAGE_WAKEUP_DONE(m);
254
255 UNLOCK_AND_DEALLOCATE;
256
257 #undef UNLOCK_AND_DEALLOCATE
258 #undef RELEASE_PAGE
259 return (KERN_SUCCESS);
260 }
261