2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 #include <kern/thread.h>
62 #include <vm/vm_fault.h>
63 #include <mach/kern_return.h>
64 #include <mach/vm_behavior.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_page.h>
70 #include <i386/intel_read_fault.h>
72 #include <kern/macro_help.h>
75 * Expansion of vm_fault for read fault in kernel mode.
76 * Must enter the mapping as writable, since the i386
77 * (and i860 in i386 compatability mode) ignores write
78 * protection in kernel mode.
80 * Note that this routine can be called for pmap's other
81 * than the kernel_pmap, in which case it just enters
82 * a read-only mapping. (See e.g. kernel_trap().)
89 vm_map_version_t version
; /* Map version for
91 vm_object_t object
; /* Top-level object */
92 vm_object_offset_t offset
; /* Top-level offset */
93 vm_prot_t prot
; /* Protection for mapping */
94 vm_behavior_t behavior
; /* Expected paging behavior */
95 vm_map_offset_t lo_offset
, hi_offset
;
96 vm_page_t result_page
; /* Result of vm_fault_page */
97 vm_page_t top_page
; /* Placeholder page */
98 boolean_t wired
; /* Is map region wired? */
100 register vm_page_t m
;
102 vm_map_t original_map
= map
;
104 boolean_t funnel_set
;
105 funnel_t
*curflock
= NULL
;
107 cur_thread
= current_thread();
108 if ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
) {
110 curflock
= cur_thread
->funnel_lock
;
111 thread_funnel_set( curflock
, FALSE
);
121 * Find the backing store object and offset into it
124 vm_map_lock_read(map
);
125 result
= vm_map_lookup_locked(&map
, vaddr
, VM_PROT_READ
, &version
,
126 &object
, &offset
, &prot
, &wired
,
127 &behavior
, &lo_offset
,
128 &hi_offset
, &map_pmap
);
130 vm_map_unlock_read(map
);
132 if (result
!= KERN_SUCCESS
) {
134 thread_funnel_set( curflock
, TRUE
);
138 if(map_pmap
!= map
) {
139 vm_map_reference(map_pmap
);
140 vm_map_unlock_read(map_pmap
);
144 * Make a reference to this object to prevent its
145 * disposal while we are playing with it.
147 assert(object
->ref_count
> 0);
149 vm_object_res_reference(object
);
150 vm_object_paging_begin(object
);
152 result
= vm_fault_page(object
, offset
, VM_PROT_READ
, FALSE
,
154 lo_offset
, hi_offset
, behavior
,
155 &prot
, &result_page
, &top_page
, (int *)0,
156 0, map
->no_zero_fill
, FALSE
, map
, vaddr
);
158 if (result
!= VM_FAULT_SUCCESS
) {
159 vm_object_deallocate(object
);
160 if(map_pmap
!= map
) {
161 vm_map_deallocate(map_pmap
);
167 case VM_FAULT_INTERRUPTED
:
169 thread_funnel_set( curflock
, TRUE
);
170 return (KERN_SUCCESS
);
171 case VM_FAULT_MEMORY_SHORTAGE
:
174 case VM_FAULT_FICTITIOUS_SHORTAGE
:
175 vm_page_more_fictitious();
177 case VM_FAULT_MEMORY_ERROR
:
178 return (KERN_MEMORY_ERROR
);
185 * How to clean up the result of vm_fault_page. This
186 * happens whether the mapping is entered or not.
189 #define UNLOCK_AND_DEALLOCATE \
191 vm_fault_cleanup(m->object, top_page); \
192 vm_object_deallocate(object); \
196 * What to do with the resulting page from vm_fault_page
197 * if it doesn't get entered into the physical map:
200 #define RELEASE_PAGE(m) \
202 PAGE_WAKEUP_DONE(m); \
203 vm_page_lock_queues(); \
204 if (!m->active && !m->inactive) \
205 vm_page_activate(m); \
206 vm_page_unlock_queues(); \
210 * We must verify that the maps have not changed.
212 vm_object_unlock(m
->object
);
214 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
215 vm_object_t retry_object
;
216 vm_object_offset_t retry_offset
;
217 vm_prot_t retry_prot
;
219 if (map
!= map_pmap
) {
220 vm_map_deallocate(map_pmap
);
224 vm_map_lock_read(map
);
226 result
= vm_map_lookup_locked(&map
, vaddr
, VM_PROT_READ
, &version
,
227 &retry_object
, &retry_offset
, &retry_prot
,
228 &wired
, &behavior
, &lo_offset
,
229 &hi_offset
, &map_pmap
);
231 if (result
!= KERN_SUCCESS
) {
232 vm_map_unlock_read(map
);
233 vm_object_lock(m
->object
);
235 UNLOCK_AND_DEALLOCATE
;
237 thread_funnel_set( curflock
, TRUE
);
241 if (map
!= map_pmap
) {
242 vm_map_reference(map_pmap
);
245 vm_object_unlock(retry_object
);
247 if (retry_object
!= object
|| retry_offset
!= offset
) {
248 vm_object_lock(m
->object
);
250 vm_map_unlock_read(map
);
251 if(map_pmap
!= map
) {
252 vm_map_unlock_read(map_pmap
);
253 vm_map_deallocate(map_pmap
);
255 UNLOCK_AND_DEALLOCATE
;
261 * Put the page in the physical map.
264 PMAP_ENTER(map_pmap
->pmap
, vaddr
, m
, VM_PROT_READ
, PMAP_DEFAULT_CACHE
, wired
);
266 if(map_pmap
!= map
) {
267 vm_map_unlock_read(map_pmap
);
268 vm_map_deallocate(map_pmap
);
271 vm_object_lock(m
->object
);
272 vm_page_lock_queues();
273 if (!m
->active
&& !m
->inactive
)
276 vm_page_unlock_queues();
278 vm_map_verify_done(map
, &version
);
281 UNLOCK_AND_DEALLOCATE
;
283 #undef UNLOCK_AND_DEALLOCATE
286 thread_funnel_set( curflock
, TRUE
);
287 return (KERN_SUCCESS
);