]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2018 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5ba3f43e | 5 | * |
2d21ac55 A |
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 License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
5ba3f43e | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
5ba3f43e | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
5ba3f43e | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
5ba3f43e | 31 | /* |
1c79356b A |
32 | * Mach Operating System |
33 | * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
5ba3f43e | 35 | * |
1c79356b A |
36 | * Permission to use, copy, modify and distribute this software and its |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
5ba3f43e | 41 | * |
1c79356b A |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
5ba3f43e | 45 | * |
1c79356b | 46 | * Carnegie Mellon requests users of this software to return to |
5ba3f43e | 47 | * |
1c79356b A |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
5ba3f43e | 52 | * |
1c79356b A |
53 | * any improvements or extensions that they make and grant Carnegie Mellon |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | /* | |
59 | * File: vm_fault.c | |
60 | * Author: Avadis Tevanian, Jr., Michael Wayne Young | |
61 | * | |
62 | * Page fault handling module. | |
63 | */ | |
1c79356b A |
64 | |
65 | #include <mach_cluster_stats.h> | |
66 | #include <mach_pagemap.h> | |
2d21ac55 | 67 | #include <libkern/OSAtomic.h> |
1c79356b | 68 | |
91447636 | 69 | #include <mach/mach_types.h> |
1c79356b | 70 | #include <mach/kern_return.h> |
0a7de745 | 71 | #include <mach/message.h> /* for error codes */ |
91447636 A |
72 | #include <mach/vm_param.h> |
73 | #include <mach/vm_behavior.h> | |
74 | #include <mach/memory_object.h> | |
0a7de745 | 75 | /* For memory_object_data_{request,unlock} */ |
2d21ac55 | 76 | #include <mach/sdt.h> |
91447636 A |
77 | |
78 | #include <kern/kern_types.h> | |
1c79356b A |
79 | #include <kern/host_statistics.h> |
80 | #include <kern/counters.h> | |
81 | #include <kern/task.h> | |
82 | #include <kern/thread.h> | |
83 | #include <kern/sched_prim.h> | |
84 | #include <kern/host.h> | |
91447636 A |
85 | #include <kern/mach_param.h> |
86 | #include <kern/macro_help.h> | |
87 | #include <kern/zalloc.h> | |
88 | #include <kern/misc_protos.h> | |
39037602 | 89 | #include <kern/policy_internal.h> |
91447636 | 90 | |
39236c6e A |
91 | #include <vm/vm_compressor.h> |
92 | #include <vm/vm_compressor_pager.h> | |
91447636 | 93 | #include <vm/vm_fault.h> |
1c79356b A |
94 | #include <vm/vm_map.h> |
95 | #include <vm/vm_object.h> | |
96 | #include <vm/vm_page.h> | |
55e303ae | 97 | #include <vm/vm_kern.h> |
1c79356b A |
98 | #include <vm/pmap.h> |
99 | #include <vm/vm_pageout.h> | |
91447636 | 100 | #include <vm/vm_protos.h> |
2d21ac55 A |
101 | #include <vm/vm_external.h> |
102 | #include <vm/memory_object.h> | |
0a7de745 | 103 | #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */ |
6d2010ae | 104 | #include <vm/vm_shared_region.h> |
1c79356b | 105 | |
39236c6e | 106 | #include <sys/codesign.h> |
39037602 A |
107 | #include <sys/reason.h> |
108 | #include <sys/signalvar.h> | |
39236c6e | 109 | |
5ba3f43e | 110 | #include <san/kasan.h> |
15129b1c | 111 | |
0a7de745 | 112 | #define VM_FAULT_CLASSIFY 0 |
1c79356b | 113 | |
2d21ac55 | 114 | #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */ |
1c79356b | 115 | |
cb323159 A |
116 | int vm_protect_privileged_from_untrusted = 1; |
117 | ||
0a7de745 | 118 | unsigned int vm_object_pagein_throttle = 16; |
1c79356b | 119 | |
b0d623f7 | 120 | /* |
5ba3f43e | 121 | * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which |
b0d623f7 A |
122 | * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts |
123 | * of memory if they're buggy and can run the system completely out of swap space. If this happens, we | |
124 | * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps | |
5ba3f43e | 125 | * keep the UI active so that the user has a chance to kill the offending task before the system |
b0d623f7 A |
126 | * completely hangs. |
127 | * | |
128 | * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied | |
129 | * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold | |
130 | * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a | |
131 | * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again. | |
132 | */ | |
133 | ||
99c3a104 | 134 | extern void throttle_lowpri_io(int); |
b0d623f7 | 135 | |
39037602 A |
136 | extern struct vnode *vnode_pager_lookup_vnode(memory_object_t); |
137 | ||
b0d623f7 A |
138 | uint64_t vm_hard_throttle_threshold; |
139 | ||
b0d623f7 | 140 | |
bca245ac A |
141 | OS_ALWAYS_INLINE |
142 | boolean_t | |
143 | NEED_TO_HARD_THROTTLE_THIS_TASK(void) | |
144 | { | |
145 | return vm_wants_task_throttled(current_task()) || | |
146 | ((vm_page_free_count < vm_page_throttle_limit || | |
147 | HARD_THROTTLE_LIMIT_REACHED()) && | |
148 | proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) >= THROTTLE_LEVEL_THROTTLED); | |
149 | } | |
b0d623f7 | 150 | |
0a7de745 A |
151 | #define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */ |
152 | #define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */ | |
04b8595b | 153 | |
0a7de745 A |
154 | #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6 |
155 | #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000 | |
04b8595b | 156 | |
1c79356b | 157 | |
cb323159 A |
158 | #define VM_STAT_DECOMPRESSIONS() \ |
159 | MACRO_BEGIN \ | |
160 | VM_STAT_INCR(decompressions); \ | |
161 | current_thread()->decompressions++; \ | |
162 | MACRO_END | |
163 | ||
b0d623f7 | 164 | boolean_t current_thread_aborted(void); |
91447636 | 165 | |
1c79356b | 166 | /* Forward declarations of internal routines. */ |
3e170ce0 | 167 | static kern_return_t vm_fault_wire_fast( |
0a7de745 A |
168 | vm_map_t map, |
169 | vm_map_offset_t va, | |
170 | vm_prot_t prot, | |
171 | vm_tag_t wire_tag, | |
172 | vm_map_entry_t entry, | |
173 | pmap_t pmap, | |
174 | vm_map_offset_t pmap_addr, | |
175 | ppnum_t *physpage_p); | |
1c79356b | 176 | |
3e170ce0 | 177 | static kern_return_t vm_fault_internal( |
0a7de745 A |
178 | vm_map_t map, |
179 | vm_map_offset_t vaddr, | |
180 | vm_prot_t caller_prot, | |
181 | boolean_t change_wiring, | |
182 | vm_tag_t wire_tag, | |
183 | int interruptible, | |
184 | pmap_t pmap, | |
185 | vm_map_offset_t pmap_addr, | |
186 | ppnum_t *physpage_p); | |
3e170ce0 A |
187 | |
188 | static void vm_fault_copy_cleanup( | |
0a7de745 A |
189 | vm_page_t page, |
190 | vm_page_t top_page); | |
1c79356b | 191 | |
3e170ce0 | 192 | static void vm_fault_copy_dst_cleanup( |
0a7de745 | 193 | vm_page_t page); |
1c79356b | 194 | |
0a7de745 A |
195 | #if VM_FAULT_CLASSIFY |
196 | extern void vm_fault_classify(vm_object_t object, | |
197 | vm_object_offset_t offset, | |
198 | vm_prot_t fault_type); | |
1c79356b A |
199 | |
200 | extern void vm_fault_classify_init(void); | |
201 | #endif | |
202 | ||
d1ecb069 | 203 | unsigned long vm_pmap_enter_blocked = 0; |
316670eb | 204 | unsigned long vm_pmap_enter_retried = 0; |
4a3eedf9 A |
205 | |
206 | unsigned long vm_cs_validates = 0; | |
207 | unsigned long vm_cs_revalidates = 0; | |
208 | unsigned long vm_cs_query_modified = 0; | |
209 | unsigned long vm_cs_validated_dirtied = 0; | |
6d2010ae | 210 | unsigned long vm_cs_bitmap_validated = 0; |
d9a64523 A |
211 | #if PMAP_CS |
212 | uint64_t vm_cs_defer_to_pmap_cs = 0; | |
213 | uint64_t vm_cs_defer_to_pmap_cs_not = 0; | |
214 | #endif /* PMAP_CS */ | |
593a1d5f | 215 | |
cb323159 | 216 | void vm_pre_fault(vm_map_offset_t, vm_prot_t); |
fe8ab488 | 217 | |
3e170ce0 | 218 | extern char *kdp_compressor_decompressed_page; |
0a7de745 A |
219 | extern addr64_t kdp_compressor_decompressed_page_paddr; |
220 | extern ppnum_t kdp_compressor_decompressed_page_ppnum; | |
3e170ce0 | 221 | |
d9a64523 A |
222 | struct vmrtfr { |
223 | int vmrtfr_maxi; | |
224 | int vmrtfr_curi; | |
225 | int64_t vmrtf_total; | |
226 | vm_rtfault_record_t *vm_rtf_records; | |
227 | } vmrtfrs; | |
228 | #define VMRTF_DEFAULT_BUFSIZE (4096) | |
229 | #define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t)) | |
230 | int vmrtf_num_records = VMRTF_NUM_RECORDS_DEFAULT; | |
231 | ||
232 | static void vm_rtfrecord_lock(void); | |
233 | static void vm_rtfrecord_unlock(void); | |
234 | static void vm_record_rtfault(thread_t, uint64_t, vm_map_offset_t, int); | |
235 | ||
236 | lck_spin_t vm_rtfr_slock; | |
237 | extern lck_grp_t vm_page_lck_grp_bucket; | |
238 | extern lck_attr_t vm_page_lck_attr; | |
239 | ||
1c79356b A |
240 | /* |
241 | * Routine: vm_fault_init | |
242 | * Purpose: | |
243 | * Initialize our private data structures. | |
244 | */ | |
245 | void | |
246 | vm_fault_init(void) | |
247 | { | |
39236c6e A |
248 | int i, vm_compressor_temp; |
249 | boolean_t need_default_val = TRUE; | |
b0d623f7 A |
250 | /* |
251 | * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is | |
252 | * computed as a percentage of available memory, and the percentage used is scaled inversely with | |
39236c6e | 253 | * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems |
b0d623f7 A |
254 | * and reduce the value down to 10% for very large memory configurations. This helps give us a |
255 | * definition of a memory hog that makes more sense relative to the amount of ram in the machine. | |
256 | * The formula here simply uses the number of gigabytes of ram to adjust the percentage. | |
257 | */ | |
258 | ||
0a7de745 | 259 | vm_hard_throttle_threshold = sane_size * (35 - MIN((int)(sane_size / (1024 * 1024 * 1024)), 25)) / 100; |
39236c6e A |
260 | |
261 | /* | |
262 | * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry. | |
263 | */ | |
264 | ||
0a7de745 A |
265 | if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp, sizeof(vm_compressor_temp))) { |
266 | for (i = 0; i < VM_PAGER_MAX_MODES; i++) { | |
5ba3f43e | 267 | if (vm_compressor_temp > 0 && |
0a7de745 | 268 | ((vm_compressor_temp & (1 << i)) == vm_compressor_temp)) { |
39236c6e A |
269 | need_default_val = FALSE; |
270 | vm_compressor_mode = vm_compressor_temp; | |
271 | break; | |
272 | } | |
273 | } | |
0a7de745 | 274 | if (need_default_val) { |
39236c6e | 275 | printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp); |
0a7de745 | 276 | } |
5ba3f43e | 277 | } |
39236c6e A |
278 | if (need_default_val) { |
279 | /* If no boot arg or incorrect boot arg, try device tree. */ | |
280 | PE_get_default("kern.vm_compressor", &vm_compressor_mode, sizeof(vm_compressor_mode)); | |
281 | } | |
39236c6e | 282 | printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode); |
cb323159 A |
283 | |
284 | PE_parse_boot_argn("vm_protect_privileged_from_untrusted", &vm_protect_privileged_from_untrusted, sizeof(vm_protect_privileged_from_untrusted)); | |
1c79356b A |
285 | } |
286 | ||
0a7de745 A |
287 | void |
288 | vm_rtfault_record_init(void) | |
289 | { | |
d9a64523 A |
290 | PE_parse_boot_argn("vm_rtfault_records", &vmrtf_num_records, sizeof(vmrtf_num_records)); |
291 | ||
292 | assert(vmrtf_num_records >= 1); | |
293 | vmrtf_num_records = MAX(vmrtf_num_records, 1); | |
294 | size_t kallocsz = vmrtf_num_records * sizeof(vm_rtfault_record_t); | |
295 | vmrtfrs.vm_rtf_records = kalloc(kallocsz); | |
296 | bzero(vmrtfrs.vm_rtf_records, kallocsz); | |
297 | vmrtfrs.vmrtfr_maxi = vmrtf_num_records - 1; | |
298 | lck_spin_init(&vm_rtfr_slock, &vm_page_lck_grp_bucket, &vm_page_lck_attr); | |
299 | } | |
1c79356b A |
300 | /* |
301 | * Routine: vm_fault_cleanup | |
302 | * Purpose: | |
303 | * Clean up the result of vm_fault_page. | |
304 | * Results: | |
305 | * The paging reference for "object" is released. | |
306 | * "object" is unlocked. | |
307 | * If "top_page" is not null, "top_page" is | |
308 | * freed and the paging reference for the object | |
309 | * containing it is released. | |
310 | * | |
311 | * In/out conditions: | |
312 | * "object" must be locked. | |
313 | */ | |
314 | void | |
315 | vm_fault_cleanup( | |
0a7de745 A |
316 | vm_object_t object, |
317 | vm_page_t top_page) | |
1c79356b A |
318 | { |
319 | vm_object_paging_end(object); | |
0a7de745 | 320 | vm_object_unlock(object); |
1c79356b A |
321 | |
322 | if (top_page != VM_PAGE_NULL) { | |
0a7de745 | 323 | object = VM_PAGE_OBJECT(top_page); |
2d21ac55 A |
324 | |
325 | vm_object_lock(object); | |
326 | VM_PAGE_FREE(top_page); | |
327 | vm_object_paging_end(object); | |
328 | vm_object_unlock(object); | |
1c79356b A |
329 | } |
330 | } | |
331 | ||
55e303ae A |
332 | #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0) |
333 | ||
334 | ||
0a7de745 | 335 | boolean_t vm_page_deactivate_behind = TRUE; |
5ba3f43e A |
336 | /* |
337 | * default sizes given VM_BEHAVIOR_DEFAULT reference behavior | |
1c79356b | 338 | */ |
0a7de745 A |
339 | #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128 |
340 | #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */ | |
b0d623f7 A |
341 | /* we use it to size an array on the stack */ |
342 | ||
343 | int vm_default_behind = VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW; | |
55e303ae | 344 | |
0a7de745 | 345 | #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024) |
2d21ac55 A |
346 | |
347 | /* | |
348 | * vm_page_is_sequential | |
349 | * | |
350 | * Determine if sequential access is in progress | |
351 | * in accordance with the behavior specified. | |
352 | * Update state to indicate current access pattern. | |
353 | * | |
354 | * object must have at least the shared lock held | |
355 | */ | |
356 | static | |
357 | void | |
358 | vm_fault_is_sequential( | |
0a7de745 A |
359 | vm_object_t object, |
360 | vm_object_offset_t offset, | |
361 | vm_behavior_t behavior) | |
2d21ac55 | 362 | { |
0a7de745 A |
363 | vm_object_offset_t last_alloc; |
364 | int sequential; | |
365 | int orig_sequential; | |
2d21ac55 | 366 | |
0a7de745 | 367 | last_alloc = object->last_alloc; |
2d21ac55 A |
368 | sequential = object->sequential; |
369 | orig_sequential = sequential; | |
370 | ||
371 | switch (behavior) { | |
372 | case VM_BEHAVIOR_RANDOM: | |
0a7de745 | 373 | /* |
2d21ac55 A |
374 | * reset indicator of sequential behavior |
375 | */ | |
0a7de745 A |
376 | sequential = 0; |
377 | break; | |
2d21ac55 A |
378 | |
379 | case VM_BEHAVIOR_SEQUENTIAL: | |
0a7de745 A |
380 | if (offset && last_alloc == offset - PAGE_SIZE_64) { |
381 | /* | |
2d21ac55 A |
382 | * advance indicator of sequential behavior |
383 | */ | |
0a7de745 A |
384 | if (sequential < MAX_SEQUENTIAL_RUN) { |
385 | sequential += PAGE_SIZE; | |
386 | } | |
2d21ac55 | 387 | } else { |
0a7de745 | 388 | /* |
2d21ac55 A |
389 | * reset indicator of sequential behavior |
390 | */ | |
0a7de745 | 391 | sequential = 0; |
2d21ac55 | 392 | } |
0a7de745 | 393 | break; |
2d21ac55 A |
394 | |
395 | case VM_BEHAVIOR_RSEQNTL: | |
0a7de745 A |
396 | if (last_alloc && last_alloc == offset + PAGE_SIZE_64) { |
397 | /* | |
2d21ac55 A |
398 | * advance indicator of sequential behavior |
399 | */ | |
0a7de745 A |
400 | if (sequential > -MAX_SEQUENTIAL_RUN) { |
401 | sequential -= PAGE_SIZE; | |
402 | } | |
2d21ac55 | 403 | } else { |
0a7de745 | 404 | /* |
2d21ac55 A |
405 | * reset indicator of sequential behavior |
406 | */ | |
0a7de745 | 407 | sequential = 0; |
2d21ac55 | 408 | } |
0a7de745 | 409 | break; |
2d21ac55 A |
410 | |
411 | case VM_BEHAVIOR_DEFAULT: | |
412 | default: | |
0a7de745 A |
413 | if (offset && last_alloc == (offset - PAGE_SIZE_64)) { |
414 | /* | |
2d21ac55 A |
415 | * advance indicator of sequential behavior |
416 | */ | |
0a7de745 A |
417 | if (sequential < 0) { |
418 | sequential = 0; | |
419 | } | |
420 | if (sequential < MAX_SEQUENTIAL_RUN) { | |
421 | sequential += PAGE_SIZE; | |
422 | } | |
2d21ac55 | 423 | } else if (last_alloc && last_alloc == (offset + PAGE_SIZE_64)) { |
0a7de745 | 424 | /* |
2d21ac55 A |
425 | * advance indicator of sequential behavior |
426 | */ | |
0a7de745 A |
427 | if (sequential > 0) { |
428 | sequential = 0; | |
429 | } | |
430 | if (sequential > -MAX_SEQUENTIAL_RUN) { | |
431 | sequential -= PAGE_SIZE; | |
432 | } | |
2d21ac55 | 433 | } else { |
0a7de745 | 434 | /* |
2d21ac55 A |
435 | * reset indicator of sequential behavior |
436 | */ | |
0a7de745 | 437 | sequential = 0; |
2d21ac55 | 438 | } |
0a7de745 | 439 | break; |
2d21ac55 A |
440 | } |
441 | if (sequential != orig_sequential) { | |
0a7de745 A |
442 | if (!OSCompareAndSwap(orig_sequential, sequential, (UInt32 *)&object->sequential)) { |
443 | /* | |
2d21ac55 A |
444 | * if someone else has already updated object->sequential |
445 | * don't bother trying to update it or object->last_alloc | |
446 | */ | |
0a7de745 | 447 | return; |
2d21ac55 A |
448 | } |
449 | } | |
450 | /* | |
451 | * I'd like to do this with a OSCompareAndSwap64, but that | |
452 | * doesn't exist for PPC... however, it shouldn't matter | |
453 | * that much... last_alloc is maintained so that we can determine | |
454 | * if a sequential access pattern is taking place... if only | |
455 | * one thread is banging on this object, no problem with the unprotected | |
456 | * update... if 2 or more threads are banging away, we run the risk of | |
457 | * someone seeing a mangled update... however, in the face of multiple | |
458 | * accesses, no sequential access pattern can develop anyway, so we | |
459 | * haven't lost any real info. | |
460 | */ | |
461 | object->last_alloc = offset; | |
462 | } | |
463 | ||
464 | ||
b0d623f7 A |
465 | int vm_page_deactivate_behind_count = 0; |
466 | ||
55e303ae | 467 | /* |
2d21ac55 A |
468 | * vm_page_deactivate_behind |
469 | * | |
470 | * Determine if sequential access is in progress | |
471 | * in accordance with the behavior specified. If | |
472 | * so, compute a potential page to deactivate and | |
473 | * deactivate it. | |
55e303ae | 474 | * |
2d21ac55 | 475 | * object must be locked. |
55e303ae | 476 | * |
2d21ac55 | 477 | * return TRUE if we actually deactivate a page |
55e303ae A |
478 | */ |
479 | static | |
480 | boolean_t | |
481 | vm_fault_deactivate_behind( | |
0a7de745 A |
482 | vm_object_t object, |
483 | vm_object_offset_t offset, | |
484 | vm_behavior_t behavior) | |
55e303ae | 485 | { |
0a7de745 A |
486 | int n; |
487 | int pages_in_run = 0; | |
488 | int max_pages_in_run = 0; | |
489 | int sequential_run; | |
490 | int sequential_behavior = VM_BEHAVIOR_SEQUENTIAL; | |
491 | vm_object_offset_t run_offset = 0; | |
492 | vm_object_offset_t pg_offset = 0; | |
493 | vm_page_t m; | |
494 | vm_page_t page_run[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER]; | |
55e303ae | 495 | |
b0d623f7 | 496 | pages_in_run = 0; |
55e303ae | 497 | #if TRACEFAULTPAGE |
0a7de745 | 498 | dbgTrace(0xBEEF0018, (unsigned int) object, (unsigned int) vm_fault_deactivate_behind); /* (TEST/DEBUG) */ |
55e303ae A |
499 | #endif |
500 | ||
2d21ac55 | 501 | if (object == kernel_object || vm_page_deactivate_behind == FALSE) { |
91447636 A |
502 | /* |
503 | * Do not deactivate pages from the kernel object: they | |
504 | * are not intended to become pageable. | |
2d21ac55 | 505 | * or we've disabled the deactivate behind mechanism |
91447636 A |
506 | */ |
507 | return FALSE; | |
508 | } | |
2d21ac55 | 509 | if ((sequential_run = object->sequential)) { |
0a7de745 A |
510 | if (sequential_run < 0) { |
511 | sequential_behavior = VM_BEHAVIOR_RSEQNTL; | |
512 | sequential_run = 0 - sequential_run; | |
513 | } else { | |
514 | sequential_behavior = VM_BEHAVIOR_SEQUENTIAL; | |
515 | } | |
2d21ac55 | 516 | } |
55e303ae A |
517 | switch (behavior) { |
518 | case VM_BEHAVIOR_RANDOM: | |
55e303ae A |
519 | break; |
520 | case VM_BEHAVIOR_SEQUENTIAL: | |
0a7de745 | 521 | if (sequential_run >= (int)PAGE_SIZE) { |
b0d623f7 A |
522 | run_offset = 0 - PAGE_SIZE_64; |
523 | max_pages_in_run = 1; | |
524 | } | |
55e303ae A |
525 | break; |
526 | case VM_BEHAVIOR_RSEQNTL: | |
0a7de745 | 527 | if (sequential_run >= (int)PAGE_SIZE) { |
b0d623f7 A |
528 | run_offset = PAGE_SIZE_64; |
529 | max_pages_in_run = 1; | |
530 | } | |
55e303ae A |
531 | break; |
532 | case VM_BEHAVIOR_DEFAULT: | |
533 | default: | |
0a7de745 | 534 | { vm_object_offset_t behind = vm_default_behind * PAGE_SIZE_64; |
2d21ac55 | 535 | |
0a7de745 | 536 | /* |
2d21ac55 A |
537 | * determine if the run of sequential accesss has been |
538 | * long enough on an object with default access behavior | |
539 | * to consider it for deactivation | |
540 | */ | |
b0d623f7 A |
541 | if ((uint64_t)sequential_run >= behind && (sequential_run % (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER * PAGE_SIZE)) == 0) { |
542 | /* | |
543 | * the comparisons between offset and behind are done | |
544 | * in this kind of odd fashion in order to prevent wrap around | |
545 | * at the end points | |
546 | */ | |
0a7de745 A |
547 | if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL) { |
548 | if (offset >= behind) { | |
b0d623f7 A |
549 | run_offset = 0 - behind; |
550 | pg_offset = PAGE_SIZE_64; | |
551 | max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER; | |
552 | } | |
2d21ac55 | 553 | } else { |
0a7de745 | 554 | if (offset < -behind) { |
b0d623f7 A |
555 | run_offset = behind; |
556 | pg_offset = 0 - PAGE_SIZE_64; | |
557 | max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER; | |
558 | } | |
2d21ac55 | 559 | } |
55e303ae | 560 | } |
0a7de745 | 561 | break;} |
2d21ac55 | 562 | } |
0a7de745 | 563 | for (n = 0; n < max_pages_in_run; n++) { |
b0d623f7 A |
564 | m = vm_page_lookup(object, offset + run_offset + (n * pg_offset)); |
565 | ||
d9a64523 | 566 | if (m && !m->vmp_laundry && !m->vmp_busy && !m->vmp_no_cache && (m->vmp_q_state != VM_PAGE_ON_THROTTLED_Q) && !m->vmp_fictitious && !m->vmp_absent) { |
b0d623f7 | 567 | page_run[pages_in_run++] = m; |
39236c6e A |
568 | |
569 | /* | |
570 | * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise... | |
571 | * | |
572 | * a TLB flush isn't really needed here since at worst we'll miss the reference bit being | |
573 | * updated in the PTE if a remote processor still has this mapping cached in its TLB when the | |
574 | * new reference happens. If no futher references happen on the page after that remote TLB flushes | |
575 | * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue | |
576 | * by pageout_scan, which is just fine since the last reference would have happened quite far | |
577 | * in the past (TLB caches don't hang around for very long), and of course could just as easily | |
578 | * have happened before we did the deactivate_behind. | |
579 | */ | |
39037602 | 580 | pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m), VM_MEM_REFERENCED, PMAP_OPTIONS_NOFLUSH, (void *)NULL); |
b0d623f7 A |
581 | } |
582 | } | |
583 | if (pages_in_run) { | |
584 | vm_page_lockspin_queues(); | |
585 | ||
586 | for (n = 0; n < pages_in_run; n++) { | |
b0d623f7 A |
587 | m = page_run[n]; |
588 | ||
589 | vm_page_deactivate_internal(m, FALSE); | |
590 | ||
591 | vm_page_deactivate_behind_count++; | |
55e303ae | 592 | #if TRACEFAULTPAGE |
0a7de745 | 593 | dbgTrace(0xBEEF0019, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */ |
55e303ae A |
594 | #endif |
595 | } | |
b0d623f7 A |
596 | vm_page_unlock_queues(); |
597 | ||
598 | return TRUE; | |
55e303ae A |
599 | } |
600 | return FALSE; | |
601 | } | |
1c79356b | 602 | |
1c79356b | 603 | |
04b8595b | 604 | #if (DEVELOPMENT || DEBUG) |
0a7de745 A |
605 | uint32_t vm_page_creation_throttled_hard = 0; |
606 | uint32_t vm_page_creation_throttled_soft = 0; | |
607 | uint64_t vm_page_creation_throttle_avoided = 0; | |
04b8595b A |
608 | #endif /* DEVELOPMENT || DEBUG */ |
609 | ||
6d2010ae | 610 | static int |
04b8595b | 611 | vm_page_throttled(boolean_t page_kept) |
b0d623f7 | 612 | { |
0a7de745 A |
613 | clock_sec_t elapsed_sec; |
614 | clock_sec_t tv_sec; | |
615 | clock_usec_t tv_usec; | |
5ba3f43e | 616 | |
b0d623f7 | 617 | thread_t thread = current_thread(); |
5ba3f43e | 618 | |
0a7de745 A |
619 | if (thread->options & TH_OPT_VMPRIV) { |
620 | return 0; | |
621 | } | |
b0d623f7 | 622 | |
04b8595b A |
623 | if (thread->t_page_creation_throttled) { |
624 | thread->t_page_creation_throttled = 0; | |
5ba3f43e | 625 | |
0a7de745 | 626 | if (page_kept == FALSE) { |
04b8595b | 627 | goto no_throttle; |
0a7de745 | 628 | } |
04b8595b A |
629 | } |
630 | if (NEED_TO_HARD_THROTTLE_THIS_TASK()) { | |
631 | #if (DEVELOPMENT || DEBUG) | |
632 | thread->t_page_creation_throttled_hard++; | |
633 | OSAddAtomic(1, &vm_page_creation_throttled_hard); | |
634 | #endif /* DEVELOPMENT || DEBUG */ | |
0a7de745 | 635 | return HARD_THROTTLE_DELAY; |
04b8595b | 636 | } |
b0d623f7 | 637 | |
39037602 | 638 | if ((vm_page_free_count < vm_page_throttle_limit || (VM_CONFIG_COMPRESSOR_IS_PRESENT && SWAPPER_NEEDS_TO_UNTHROTTLE())) && |
04b8595b | 639 | thread->t_page_creation_count > (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS * VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC)) { |
3e170ce0 A |
640 | if (vm_page_free_wanted == 0 && vm_page_free_wanted_privileged == 0) { |
641 | #if (DEVELOPMENT || DEBUG) | |
642 | OSAddAtomic64(1, &vm_page_creation_throttle_avoided); | |
643 | #endif | |
644 | goto no_throttle; | |
645 | } | |
b0d623f7 A |
646 | clock_get_system_microtime(&tv_sec, &tv_usec); |
647 | ||
648 | elapsed_sec = tv_sec - thread->t_page_creation_time; | |
649 | ||
04b8595b A |
650 | if (elapsed_sec <= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS || |
651 | (thread->t_page_creation_count / elapsed_sec) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC) { | |
04b8595b | 652 | if (elapsed_sec >= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS)) { |
b0d623f7 A |
653 | /* |
654 | * we'll reset our stats to give a well behaved app | |
655 | * that was unlucky enough to accumulate a bunch of pages | |
656 | * over a long period of time a chance to get out of | |
657 | * the throttled state... we reset the counter and timestamp | |
658 | * so that if it stays under the rate limit for the next second | |
5ba3f43e | 659 | * it will be back in our good graces... if it exceeds it, it |
b0d623f7 A |
660 | * will remain in the throttled state |
661 | */ | |
662 | thread->t_page_creation_time = tv_sec; | |
04b8595b | 663 | thread->t_page_creation_count = VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC * (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS - 1); |
b0d623f7 | 664 | } |
d9a64523 | 665 | VM_PAGEOUT_DEBUG(vm_page_throttle_count, 1); |
b0d623f7 | 666 | |
04b8595b A |
667 | thread->t_page_creation_throttled = 1; |
668 | ||
39037602 | 669 | if (VM_CONFIG_COMPRESSOR_IS_PRESENT && HARD_THROTTLE_LIMIT_REACHED()) { |
04b8595b A |
670 | #if (DEVELOPMENT || DEBUG) |
671 | thread->t_page_creation_throttled_hard++; | |
672 | OSAddAtomic(1, &vm_page_creation_throttled_hard); | |
673 | #endif /* DEVELOPMENT || DEBUG */ | |
0a7de745 | 674 | return HARD_THROTTLE_DELAY; |
04b8595b A |
675 | } else { |
676 | #if (DEVELOPMENT || DEBUG) | |
677 | thread->t_page_creation_throttled_soft++; | |
678 | OSAddAtomic(1, &vm_page_creation_throttled_soft); | |
679 | #endif /* DEVELOPMENT || DEBUG */ | |
0a7de745 | 680 | return SOFT_THROTTLE_DELAY; |
04b8595b | 681 | } |
b0d623f7 A |
682 | } |
683 | thread->t_page_creation_time = tv_sec; | |
684 | thread->t_page_creation_count = 0; | |
685 | } | |
04b8595b A |
686 | no_throttle: |
687 | thread->t_page_creation_count++; | |
688 | ||
0a7de745 | 689 | return 0; |
b0d623f7 A |
690 | } |
691 | ||
3e170ce0 | 692 | |
2d21ac55 A |
693 | /* |
694 | * check for various conditions that would | |
695 | * prevent us from creating a ZF page... | |
696 | * cleanup is based on being called from vm_fault_page | |
697 | * | |
698 | * object must be locked | |
d9a64523 | 699 | * object == m->vmp_object |
2d21ac55 A |
700 | */ |
701 | static vm_fault_return_t | |
d9a64523 | 702 | vm_fault_check(vm_object_t object, vm_page_t m, vm_page_t first_m, wait_interrupt_t interruptible_state, boolean_t page_throttle) |
2d21ac55 | 703 | { |
6d2010ae A |
704 | int throttle_delay; |
705 | ||
0a7de745 | 706 | if (object->shadow_severed || |
b0d623f7 | 707 | VM_OBJECT_PURGEABLE_FAULT_ERROR(object)) { |
0a7de745 | 708 | /* |
b0d623f7 A |
709 | * Either: |
710 | * 1. the shadow chain was severed, | |
711 | * 2. the purgeable object is volatile or empty and is marked | |
712 | * to fault on access while volatile. | |
713 | * Just have to return an error at this point | |
2d21ac55 | 714 | */ |
0a7de745 A |
715 | if (m != VM_PAGE_NULL) { |
716 | VM_PAGE_FREE(m); | |
717 | } | |
2d21ac55 A |
718 | vm_fault_cleanup(object, first_m); |
719 | ||
720 | thread_interrupt_level(interruptible_state); | |
721 | ||
0a7de745 | 722 | return VM_FAULT_MEMORY_ERROR; |
2d21ac55 | 723 | } |
3e170ce0 A |
724 | if (page_throttle == TRUE) { |
725 | if ((throttle_delay = vm_page_throttled(FALSE))) { | |
726 | /* | |
727 | * we're throttling zero-fills... | |
728 | * treat this as if we couldn't grab a page | |
729 | */ | |
0a7de745 | 730 | if (m != VM_PAGE_NULL) { |
3e170ce0 | 731 | VM_PAGE_FREE(m); |
0a7de745 | 732 | } |
3e170ce0 | 733 | vm_fault_cleanup(object, first_m); |
2d21ac55 | 734 | |
3e170ce0 | 735 | VM_DEBUG_EVENT(vmf_check_zfdelay, VMF_CHECK_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); |
b0d623f7 | 736 | |
3e170ce0 | 737 | delay(throttle_delay); |
b0d623f7 | 738 | |
3e170ce0 A |
739 | if (current_thread_aborted()) { |
740 | thread_interrupt_level(interruptible_state); | |
741 | return VM_FAULT_INTERRUPTED; | |
742 | } | |
6d2010ae | 743 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 744 | |
0a7de745 | 745 | return VM_FAULT_MEMORY_SHORTAGE; |
3e170ce0 | 746 | } |
2d21ac55 | 747 | } |
0a7de745 | 748 | return VM_FAULT_SUCCESS; |
2d21ac55 A |
749 | } |
750 | ||
751 | ||
752 | /* | |
753 | * do the work to zero fill a page and | |
754 | * inject it into the correct paging queue | |
755 | * | |
d9a64523 | 756 | * m->vmp_object must be locked |
2d21ac55 A |
757 | * page queue lock must NOT be held |
758 | */ | |
759 | static int | |
760 | vm_fault_zero_page(vm_page_t m, boolean_t no_zero_fill) | |
761 | { | |
0a7de745 A |
762 | int my_fault = DBG_ZERO_FILL_FAULT; |
763 | vm_object_t object; | |
39037602 A |
764 | |
765 | object = VM_PAGE_OBJECT(m); | |
2d21ac55 A |
766 | |
767 | /* | |
768 | * This is is a zero-fill page fault... | |
769 | * | |
770 | * Checking the page lock is a waste of | |
771 | * time; this page was absent, so | |
772 | * it can't be page locked by a pager. | |
773 | * | |
774 | * we also consider it undefined | |
775 | * with respect to instruction | |
776 | * execution. i.e. it is the responsibility | |
777 | * of higher layers to call for an instruction | |
778 | * sync after changing the contents and before | |
5ba3f43e | 779 | * sending a program into this area. We |
2d21ac55 A |
780 | * choose this approach for performance |
781 | */ | |
d9a64523 | 782 | m->vmp_pmapped = TRUE; |
2d21ac55 | 783 | |
d9a64523 A |
784 | m->vmp_cs_validated = FALSE; |
785 | m->vmp_cs_tainted = FALSE; | |
786 | m->vmp_cs_nx = FALSE; | |
2d21ac55 | 787 | |
6d2010ae A |
788 | if (no_zero_fill == TRUE) { |
789 | my_fault = DBG_NZF_PAGE_FAULT; | |
fe8ab488 | 790 | |
0a7de745 A |
791 | if (m->vmp_absent && m->vmp_busy) { |
792 | return my_fault; | |
793 | } | |
6d2010ae | 794 | } else { |
2d21ac55 A |
795 | vm_page_zero_fill(m); |
796 | ||
797 | VM_STAT_INCR(zero_fill_count); | |
798 | DTRACE_VM2(zfod, int, 1, (uint64_t *), NULL); | |
799 | } | |
d9a64523 | 800 | assert(!m->vmp_laundry); |
39037602 | 801 | assert(object != kernel_object); |
d9a64523 | 802 | //assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0); |
2d21ac55 | 803 | |
39037602 | 804 | if (!VM_DYNAMIC_PAGING_ENABLED() && |
0a7de745 A |
805 | (object->purgable == VM_PURGABLE_DENY || |
806 | object->purgable == VM_PURGABLE_NONVOLATILE || | |
807 | object->purgable == VM_PURGABLE_VOLATILE)) { | |
b0d623f7 | 808 | vm_page_lockspin_queues(); |
2d21ac55 | 809 | |
39037602 | 810 | if (!VM_DYNAMIC_PAGING_ENABLED()) { |
39236c6e | 811 | assert(!VM_PAGE_WIRED(m)); |
6d2010ae | 812 | |
39236c6e A |
813 | /* |
814 | * can't be on the pageout queue since we don't | |
815 | * have a pager to try and clean to | |
816 | */ | |
39037602 | 817 | vm_page_queues_remove(m, TRUE); |
3e170ce0 | 818 | vm_page_check_pageable_safe(m); |
0a7de745 | 819 | vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq); |
d9a64523 | 820 | m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q; |
39236c6e A |
821 | vm_page_throttled_count++; |
822 | } | |
2d21ac55 | 823 | vm_page_unlock_queues(); |
2d21ac55 | 824 | } |
0a7de745 | 825 | return my_fault; |
2d21ac55 A |
826 | } |
827 | ||
828 | ||
1c79356b A |
829 | /* |
830 | * Routine: vm_fault_page | |
831 | * Purpose: | |
832 | * Find the resident page for the virtual memory | |
833 | * specified by the given virtual memory object | |
834 | * and offset. | |
835 | * Additional arguments: | |
836 | * The required permissions for the page is given | |
837 | * in "fault_type". Desired permissions are included | |
2d21ac55 | 838 | * in "protection". |
5ba3f43e | 839 | * fault_info is passed along to determine pagein cluster |
2d21ac55 A |
840 | * limits... it contains the expected reference pattern, |
841 | * cluster size if available, etc... | |
1c79356b A |
842 | * |
843 | * If the desired page is known to be resident (for | |
844 | * example, because it was previously wired down), asserting | |
845 | * the "unwiring" parameter will speed the search. | |
846 | * | |
847 | * If the operation can be interrupted (by thread_abort | |
848 | * or thread_terminate), then the "interruptible" | |
849 | * parameter should be asserted. | |
850 | * | |
851 | * Results: | |
852 | * The page containing the proper data is returned | |
853 | * in "result_page". | |
854 | * | |
855 | * In/out conditions: | |
856 | * The source object must be locked and referenced, | |
857 | * and must donate one paging reference. The reference | |
858 | * is not affected. The paging reference and lock are | |
859 | * consumed. | |
860 | * | |
861 | * If the call succeeds, the object in which "result_page" | |
862 | * resides is left locked and holding a paging reference. | |
863 | * If this is not the original object, a busy page in the | |
864 | * original object is returned in "top_page", to prevent other | |
865 | * callers from pursuing this same data, along with a paging | |
866 | * reference for the original object. The "top_page" should | |
867 | * be destroyed when this guarantee is no longer required. | |
868 | * The "result_page" is also left busy. It is not removed | |
869 | * from the pageout queues. | |
b0d623f7 | 870 | * Special Case: |
5ba3f43e | 871 | * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the |
b0d623f7 | 872 | * fault succeeded but there's no VM page (i.e. the VM object |
0a7de745 | 873 | * does not actually hold VM pages, but device memory or |
b0d623f7 A |
874 | * large pages). The object is still locked and we still hold a |
875 | * paging_in_progress reference. | |
1c79356b | 876 | */ |
b0d623f7 | 877 | unsigned int vm_fault_page_blocked_access = 0; |
316670eb | 878 | unsigned int vm_fault_page_forced_retry = 0; |
1c79356b A |
879 | |
880 | vm_fault_return_t | |
881 | vm_fault_page( | |
882 | /* Arguments: */ | |
0a7de745 A |
883 | vm_object_t first_object, /* Object to begin search */ |
884 | vm_object_offset_t first_offset, /* Offset into object */ | |
885 | vm_prot_t fault_type, /* What access is requested */ | |
886 | boolean_t must_be_resident,/* Must page be resident? */ | |
887 | boolean_t caller_lookup, /* caller looked up page */ | |
1c79356b | 888 | /* Modifies in place: */ |
0a7de745 A |
889 | vm_prot_t *protection, /* Protection for mapping */ |
890 | vm_page_t *result_page, /* Page found, if successful */ | |
39236c6e | 891 | /* Returns: */ |
0a7de745 A |
892 | vm_page_t *top_page, /* Page in top object, if |
893 | * not result_page. */ | |
1c79356b | 894 | int *type_of_fault, /* if non-null, fill in with type of fault |
0a7de745 | 895 | * COW, zero-fill, etc... returned in trace point */ |
1c79356b | 896 | /* More arguments: */ |
0a7de745 A |
897 | kern_return_t *error_code, /* code if page is in error */ |
898 | boolean_t no_zero_fill, /* don't zero fill absent pages */ | |
899 | boolean_t data_supply, /* treat as data_supply if | |
900 | * it is a write fault and a full | |
901 | * page is provided */ | |
2d21ac55 | 902 | vm_object_fault_info_t fault_info) |
1c79356b | 903 | { |
0a7de745 A |
904 | vm_page_t m; |
905 | vm_object_t object; | |
906 | vm_object_offset_t offset; | |
907 | vm_page_t first_m; | |
908 | vm_object_t next_object; | |
909 | vm_object_t copy_object; | |
910 | boolean_t look_for_page; | |
911 | boolean_t force_fault_retry = FALSE; | |
912 | vm_prot_t access_required = fault_type; | |
913 | vm_prot_t wants_copy_flag; | |
914 | kern_return_t wait_result; | |
915 | wait_interrupt_t interruptible_state; | |
916 | boolean_t data_already_requested = FALSE; | |
917 | vm_behavior_t orig_behavior; | |
918 | vm_size_t orig_cluster_size; | |
919 | vm_fault_return_t error; | |
920 | int my_fault; | |
921 | uint32_t try_failed_count; | |
922 | int interruptible; /* how may fault be interrupted? */ | |
923 | int external_state = VM_EXTERNAL_STATE_UNKNOWN; | |
924 | memory_object_t pager; | |
925 | vm_fault_return_t retval; | |
926 | int grab_options; | |
1c79356b | 927 | |
1c79356b | 928 | /* |
5ba3f43e | 929 | * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is |
39037602 | 930 | * marked as paged out in the compressor pager or the pager doesn't exist. |
5ba3f43e A |
931 | * Note also that if the pager for an internal object |
932 | * has not been created, the pager is not invoked regardless of the value | |
39037602 | 933 | * of MUST_ASK_PAGER(). |
1c79356b A |
934 | * |
935 | * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset | |
39037602 | 936 | * is marked as paged out in the compressor pager. |
1c79356b A |
937 | * PAGED_OUT() is used to determine if a page has already been pushed |
938 | * into a copy object in order to avoid a redundant page out operation. | |
939 | */ | |
0a7de745 | 940 | #define MUST_ASK_PAGER(o, f, s) \ |
39236c6e | 941 | ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT) |
39037602 | 942 | |
39236c6e A |
943 | #define PAGED_OUT(o, f) \ |
944 | (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS) | |
1c79356b A |
945 | |
946 | /* | |
947 | * Recovery actions | |
948 | */ | |
0a7de745 A |
949 | #define RELEASE_PAGE(m) \ |
950 | MACRO_BEGIN \ | |
951 | PAGE_WAKEUP_DONE(m); \ | |
952 | if ( !VM_PAGE_PAGEABLE(m)) { \ | |
953 | vm_page_lockspin_queues(); \ | |
954 | if ( !VM_PAGE_PAGEABLE(m)) { \ | |
955 | if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) \ | |
956 | vm_page_deactivate(m); \ | |
957 | else \ | |
958 | vm_page_activate(m); \ | |
959 | } \ | |
960 | vm_page_unlock_queues(); \ | |
961 | } \ | |
1c79356b A |
962 | MACRO_END |
963 | ||
964 | #if TRACEFAULTPAGE | |
0a7de745 | 965 | dbgTrace(0xBEEF0002, (unsigned int) first_object, (unsigned int) first_offset); /* (TEST/DEBUG) */ |
1c79356b A |
966 | #endif |
967 | ||
2d21ac55 | 968 | interruptible = fault_info->interruptible; |
9bccf70c | 969 | interruptible_state = thread_interrupt_level(interruptible); |
5ba3f43e | 970 | |
1c79356b A |
971 | /* |
972 | * INVARIANTS (through entire routine): | |
973 | * | |
974 | * 1) At all times, we must either have the object | |
975 | * lock or a busy page in some object to prevent | |
976 | * some other thread from trying to bring in | |
977 | * the same page. | |
978 | * | |
979 | * Note that we cannot hold any locks during the | |
980 | * pager access or when waiting for memory, so | |
981 | * we use a busy page then. | |
982 | * | |
1c79356b A |
983 | * 2) To prevent another thread from racing us down the |
984 | * shadow chain and entering a new page in the top | |
985 | * object before we do, we must keep a busy page in | |
986 | * the top object while following the shadow chain. | |
987 | * | |
988 | * 3) We must increment paging_in_progress on any object | |
2d21ac55 A |
989 | * for which we have a busy page before dropping |
990 | * the object lock | |
1c79356b A |
991 | * |
992 | * 4) We leave busy pages on the pageout queues. | |
993 | * If the pageout daemon comes across a busy page, | |
994 | * it will remove the page from the pageout queues. | |
995 | */ | |
996 | ||
1c79356b A |
997 | object = first_object; |
998 | offset = first_offset; | |
999 | first_m = VM_PAGE_NULL; | |
1000 | access_required = fault_type; | |
1001 | ||
1c79356b | 1002 | /* |
2d21ac55 | 1003 | * default type of fault |
1c79356b | 1004 | */ |
2d21ac55 | 1005 | my_fault = DBG_CACHE_HIT_FAULT; |
1c79356b A |
1006 | |
1007 | while (TRUE) { | |
1008 | #if TRACEFAULTPAGE | |
0a7de745 | 1009 | dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */ |
1c79356b | 1010 | #endif |
39037602 A |
1011 | |
1012 | grab_options = 0; | |
1013 | #if CONFIG_SECLUDED_MEMORY | |
1014 | if (object->can_grab_secluded) { | |
1015 | grab_options |= VM_PAGE_GRAB_SECLUDED; | |
1016 | } | |
1017 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
1018 | ||
1c79356b | 1019 | if (!object->alive) { |
0a7de745 | 1020 | /* |
2d21ac55 A |
1021 | * object is no longer valid |
1022 | * clean up and return error | |
1023 | */ | |
1c79356b | 1024 | vm_fault_cleanup(object, first_m); |
9bccf70c | 1025 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 1026 | |
0a7de745 | 1027 | return VM_FAULT_MEMORY_ERROR; |
1c79356b | 1028 | } |
2d21ac55 | 1029 | |
b0d623f7 A |
1030 | if (!object->pager_created && object->phys_contiguous) { |
1031 | /* | |
1032 | * A physically-contiguous object without a pager: | |
1033 | * must be a "large page" object. We do not deal | |
1034 | * with VM pages for this object. | |
1035 | */ | |
39236c6e | 1036 | caller_lookup = FALSE; |
b0d623f7 A |
1037 | m = VM_PAGE_NULL; |
1038 | goto phys_contig_object; | |
1039 | } | |
1040 | ||
1041 | if (object->blocked_access) { | |
1042 | /* | |
1043 | * Access to this VM object has been blocked. | |
1044 | * Replace our "paging_in_progress" reference with | |
1045 | * a "activity_in_progress" reference and wait for | |
1046 | * access to be unblocked. | |
1047 | */ | |
39236c6e | 1048 | caller_lookup = FALSE; /* no longer valid after sleep */ |
b0d623f7 A |
1049 | vm_object_activity_begin(object); |
1050 | vm_object_paging_end(object); | |
1051 | while (object->blocked_access) { | |
1052 | vm_object_sleep(object, | |
0a7de745 A |
1053 | VM_OBJECT_EVENT_UNBLOCKED, |
1054 | THREAD_UNINT); | |
b0d623f7 A |
1055 | } |
1056 | vm_fault_page_blocked_access++; | |
1057 | vm_object_paging_begin(object); | |
1058 | vm_object_activity_end(object); | |
1059 | } | |
1060 | ||
2d21ac55 A |
1061 | /* |
1062 | * See whether the page at 'offset' is resident | |
1063 | */ | |
39236c6e A |
1064 | if (caller_lookup == TRUE) { |
1065 | /* | |
1066 | * The caller has already looked up the page | |
1067 | * and gave us the result in "result_page". | |
1068 | * We can use this for the first lookup but | |
1069 | * it loses its validity as soon as we unlock | |
1070 | * the object. | |
1071 | */ | |
1072 | m = *result_page; | |
1073 | caller_lookup = FALSE; /* no longer valid after that */ | |
1074 | } else { | |
1075 | m = vm_page_lookup(object, offset); | |
1076 | } | |
1c79356b | 1077 | #if TRACEFAULTPAGE |
0a7de745 | 1078 | dbgTrace(0xBEEF0004, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */ |
1c79356b A |
1079 | #endif |
1080 | if (m != VM_PAGE_NULL) { | |
d9a64523 | 1081 | if (m->vmp_busy) { |
0a7de745 | 1082 | /* |
2d21ac55 A |
1083 | * The page is being brought in, |
1084 | * wait for it and then retry. | |
2d21ac55 | 1085 | */ |
1c79356b | 1086 | #if TRACEFAULTPAGE |
0a7de745 | 1087 | dbgTrace(0xBEEF0005, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ |
1c79356b | 1088 | #endif |
316670eb | 1089 | wait_result = PAGE_SLEEP(object, m, interruptible); |
1c79356b | 1090 | |
316670eb | 1091 | counter(c_vm_fault_page_block_busy_kernel++); |
2d21ac55 | 1092 | |
316670eb A |
1093 | if (wait_result != THREAD_AWAKENED) { |
1094 | vm_fault_cleanup(object, first_m); | |
1095 | thread_interrupt_level(interruptible_state); | |
6d2010ae | 1096 | |
0a7de745 A |
1097 | if (wait_result == THREAD_RESTART) { |
1098 | return VM_FAULT_RETRY; | |
1099 | } else { | |
1100 | return VM_FAULT_INTERRUPTED; | |
1101 | } | |
1c79356b | 1102 | } |
316670eb | 1103 | continue; |
1c79356b | 1104 | } |
d9a64523 A |
1105 | if (m->vmp_laundry) { |
1106 | m->vmp_free_when_done = FALSE; | |
1c79356b | 1107 | |
0a7de745 | 1108 | if (!m->vmp_cleaning) { |
316670eb | 1109 | vm_pageout_steal_laundry(m, FALSE); |
0a7de745 | 1110 | } |
316670eb | 1111 | } |
39037602 | 1112 | if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) { |
91447636 | 1113 | /* |
2d21ac55 | 1114 | * Guard page: off limits ! |
91447636 | 1115 | */ |
2d21ac55 A |
1116 | if (fault_type == VM_PROT_NONE) { |
1117 | /* | |
1118 | * The fault is not requesting any | |
1119 | * access to the guard page, so it must | |
1120 | * be just to wire or unwire it. | |
1121 | * Let's pretend it succeeded... | |
1122 | */ | |
d9a64523 | 1123 | m->vmp_busy = TRUE; |
2d21ac55 A |
1124 | *result_page = m; |
1125 | assert(first_m == VM_PAGE_NULL); | |
1126 | *top_page = first_m; | |
0a7de745 | 1127 | if (type_of_fault) { |
2d21ac55 | 1128 | *type_of_fault = DBG_GUARD_FAULT; |
0a7de745 | 1129 | } |
99c3a104 | 1130 | thread_interrupt_level(interruptible_state); |
2d21ac55 A |
1131 | return VM_FAULT_SUCCESS; |
1132 | } else { | |
1133 | /* | |
1134 | * The fault requests access to the | |
1135 | * guard page: let's deny that ! | |
1136 | */ | |
1137 | vm_fault_cleanup(object, first_m); | |
1138 | thread_interrupt_level(interruptible_state); | |
1139 | return VM_FAULT_MEMORY_ERROR; | |
1140 | } | |
91447636 | 1141 | } |
1c79356b | 1142 | |
d9a64523 | 1143 | if (m->vmp_error) { |
0a7de745 | 1144 | /* |
2d21ac55 A |
1145 | * The page is in error, give up now. |
1146 | */ | |
1c79356b | 1147 | #if TRACEFAULTPAGE |
0a7de745 | 1148 | dbgTrace(0xBEEF0006, (unsigned int) m, (unsigned int) error_code); /* (TEST/DEBUG) */ |
1c79356b | 1149 | #endif |
0a7de745 A |
1150 | if (error_code) { |
1151 | *error_code = KERN_MEMORY_ERROR; | |
1152 | } | |
1c79356b | 1153 | VM_PAGE_FREE(m); |
2d21ac55 | 1154 | |
1c79356b | 1155 | vm_fault_cleanup(object, first_m); |
9bccf70c | 1156 | thread_interrupt_level(interruptible_state); |
1c79356b | 1157 | |
0a7de745 | 1158 | return VM_FAULT_MEMORY_ERROR; |
2d21ac55 | 1159 | } |
d9a64523 | 1160 | if (m->vmp_restart) { |
0a7de745 | 1161 | /* |
2d21ac55 A |
1162 | * The pager wants us to restart |
1163 | * at the top of the chain, | |
1164 | * typically because it has moved the | |
1165 | * page to another pager, then do so. | |
1166 | */ | |
1c79356b | 1167 | #if TRACEFAULTPAGE |
0a7de745 | 1168 | dbgTrace(0xBEEF0007, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ |
1c79356b A |
1169 | #endif |
1170 | VM_PAGE_FREE(m); | |
2d21ac55 | 1171 | |
1c79356b | 1172 | vm_fault_cleanup(object, first_m); |
9bccf70c | 1173 | thread_interrupt_level(interruptible_state); |
1c79356b | 1174 | |
0a7de745 | 1175 | return VM_FAULT_RETRY; |
2d21ac55 | 1176 | } |
d9a64523 | 1177 | if (m->vmp_absent) { |
0a7de745 | 1178 | /* |
2d21ac55 A |
1179 | * The page isn't busy, but is absent, |
1180 | * therefore it's deemed "unavailable". | |
1181 | * | |
1c79356b A |
1182 | * Remove the non-existent page (unless it's |
1183 | * in the top object) and move on down to the | |
1184 | * next object (if there is one). | |
1185 | */ | |
1186 | #if TRACEFAULTPAGE | |
0a7de745 | 1187 | dbgTrace(0xBEEF0008, (unsigned int) m, (unsigned int) object->shadow); /* (TEST/DEBUG) */ |
1c79356b | 1188 | #endif |
1c79356b | 1189 | next_object = object->shadow; |
1c79356b | 1190 | |
2d21ac55 | 1191 | if (next_object == VM_OBJECT_NULL) { |
1c79356b A |
1192 | /* |
1193 | * Absent page at bottom of shadow | |
1194 | * chain; zero fill the page we left | |
2d21ac55 A |
1195 | * busy in the first object, and free |
1196 | * the absent page. | |
1c79356b | 1197 | */ |
2d21ac55 | 1198 | assert(!must_be_resident); |
55e303ae A |
1199 | |
1200 | /* | |
2d21ac55 A |
1201 | * check for any conditions that prevent |
1202 | * us from creating a new zero-fill page | |
5ba3f43e | 1203 | * vm_fault_check will do all of the |
2d21ac55 A |
1204 | * fault cleanup in the case of an error condition |
1205 | * including resetting the thread_interrupt_level | |
55e303ae | 1206 | */ |
04b8595b | 1207 | error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE); |
55e303ae | 1208 | |
0a7de745 A |
1209 | if (error != VM_FAULT_SUCCESS) { |
1210 | return error; | |
1211 | } | |
55e303ae | 1212 | |
1c79356b | 1213 | if (object != first_object) { |
0a7de745 | 1214 | /* |
2d21ac55 A |
1215 | * free the absent page we just found |
1216 | */ | |
1c79356b | 1217 | VM_PAGE_FREE(m); |
2d21ac55 A |
1218 | |
1219 | /* | |
1220 | * drop reference and lock on current object | |
1221 | */ | |
1c79356b A |
1222 | vm_object_paging_end(object); |
1223 | vm_object_unlock(object); | |
2d21ac55 A |
1224 | |
1225 | /* | |
5ba3f43e | 1226 | * grab the original page we |
2d21ac55 A |
1227 | * 'soldered' in place and |
1228 | * retake lock on 'first_object' | |
1229 | */ | |
1c79356b A |
1230 | m = first_m; |
1231 | first_m = VM_PAGE_NULL; | |
1c79356b | 1232 | |
2d21ac55 A |
1233 | object = first_object; |
1234 | offset = first_offset; | |
0b4e3aa0 | 1235 | |
1c79356b | 1236 | vm_object_lock(object); |
9bccf70c | 1237 | } else { |
0a7de745 | 1238 | /* |
2d21ac55 A |
1239 | * we're going to use the absent page we just found |
1240 | * so convert it to a 'busy' page | |
1241 | */ | |
0a7de745 | 1242 | m->vmp_absent = FALSE; |
d9a64523 | 1243 | m->vmp_busy = TRUE; |
0b4e3aa0 | 1244 | } |
0a7de745 | 1245 | if (fault_info->mark_zf_absent && no_zero_fill == TRUE) { |
d9a64523 | 1246 | m->vmp_absent = TRUE; |
0a7de745 | 1247 | } |
2d21ac55 A |
1248 | /* |
1249 | * zero-fill the page and put it on | |
1250 | * the correct paging queue | |
1251 | */ | |
1252 | my_fault = vm_fault_zero_page(m, no_zero_fill); | |
1253 | ||
1c79356b A |
1254 | break; |
1255 | } else { | |
0a7de745 | 1256 | if (must_be_resident) { |
1c79356b | 1257 | vm_object_paging_end(object); |
0a7de745 | 1258 | } else if (object != first_object) { |
1c79356b A |
1259 | vm_object_paging_end(object); |
1260 | VM_PAGE_FREE(m); | |
1261 | } else { | |
1262 | first_m = m; | |
d9a64523 A |
1263 | m->vmp_absent = FALSE; |
1264 | m->vmp_busy = TRUE; | |
1c79356b | 1265 | |
2d21ac55 | 1266 | vm_page_lockspin_queues(); |
39037602 | 1267 | vm_page_queues_remove(m, FALSE); |
1c79356b A |
1268 | vm_page_unlock_queues(); |
1269 | } | |
2d21ac55 | 1270 | |
6d2010ae A |
1271 | offset += object->vo_shadow_offset; |
1272 | fault_info->lo_offset += object->vo_shadow_offset; | |
1273 | fault_info->hi_offset += object->vo_shadow_offset; | |
1c79356b | 1274 | access_required = VM_PROT_READ; |
2d21ac55 | 1275 | |
1c79356b A |
1276 | vm_object_lock(next_object); |
1277 | vm_object_unlock(object); | |
1278 | object = next_object; | |
1279 | vm_object_paging_begin(object); | |
5ba3f43e | 1280 | |
2d21ac55 A |
1281 | /* |
1282 | * reset to default type of fault | |
1283 | */ | |
1284 | my_fault = DBG_CACHE_HIT_FAULT; | |
1285 | ||
1c79356b A |
1286 | continue; |
1287 | } | |
1288 | } | |
d9a64523 | 1289 | if ((m->vmp_cleaning) |
2d21ac55 A |
1290 | && ((object != first_object) || (object->copy != VM_OBJECT_NULL)) |
1291 | && (fault_type & VM_PROT_WRITE)) { | |
1c79356b A |
1292 | /* |
1293 | * This is a copy-on-write fault that will | |
1294 | * cause us to revoke access to this page, but | |
1295 | * this page is in the process of being cleaned | |
1296 | * in a clustered pageout. We must wait until | |
1297 | * the cleaning operation completes before | |
1298 | * revoking access to the original page, | |
1299 | * otherwise we might attempt to remove a | |
1300 | * wired mapping. | |
1301 | */ | |
1302 | #if TRACEFAULTPAGE | |
0a7de745 | 1303 | dbgTrace(0xBEEF0009, (unsigned int) m, (unsigned int) offset); /* (TEST/DEBUG) */ |
1c79356b | 1304 | #endif |
2d21ac55 A |
1305 | /* |
1306 | * take an extra ref so that object won't die | |
1307 | */ | |
1308 | vm_object_reference_locked(object); | |
1309 | ||
1c79356b | 1310 | vm_fault_cleanup(object, first_m); |
5ba3f43e | 1311 | |
1c79356b A |
1312 | counter(c_vm_fault_page_block_backoff_kernel++); |
1313 | vm_object_lock(object); | |
1314 | assert(object->ref_count > 0); | |
2d21ac55 | 1315 | |
1c79356b | 1316 | m = vm_page_lookup(object, offset); |
2d21ac55 | 1317 | |
d9a64523 | 1318 | if (m != VM_PAGE_NULL && m->vmp_cleaning) { |
1c79356b | 1319 | PAGE_ASSERT_WAIT(m, interruptible); |
2d21ac55 | 1320 | |
1c79356b | 1321 | vm_object_unlock(object); |
9bccf70c | 1322 | wait_result = thread_block(THREAD_CONTINUE_NULL); |
1c79356b | 1323 | vm_object_deallocate(object); |
2d21ac55 | 1324 | |
1c79356b A |
1325 | goto backoff; |
1326 | } else { | |
1327 | vm_object_unlock(object); | |
2d21ac55 | 1328 | |
1c79356b | 1329 | vm_object_deallocate(object); |
9bccf70c | 1330 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 1331 | |
0a7de745 | 1332 | return VM_FAULT_RETRY; |
1c79356b A |
1333 | } |
1334 | } | |
d9a64523 | 1335 | if (type_of_fault == NULL && (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) && |
b0d623f7 | 1336 | !(fault_info != NULL && fault_info->stealth)) { |
0a7de745 | 1337 | /* |
2d21ac55 A |
1338 | * If we were passed a non-NULL pointer for |
1339 | * "type_of_fault", than we came from | |
1340 | * vm_fault... we'll let it deal with | |
1341 | * this condition, since it | |
d9a64523 | 1342 | * needs to see m->vmp_speculative to correctly |
2d21ac55 A |
1343 | * account the pageins, otherwise... |
1344 | * take it off the speculative queue, we'll | |
1345 | * let the caller of vm_fault_page deal | |
1346 | * with getting it onto the correct queue | |
b0d623f7 A |
1347 | * |
1348 | * If the caller specified in fault_info that | |
1349 | * it wants a "stealth" fault, we also leave | |
1350 | * the page in the speculative queue. | |
2d21ac55 | 1351 | */ |
0a7de745 A |
1352 | vm_page_lockspin_queues(); |
1353 | if (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) { | |
39037602 | 1354 | vm_page_queues_remove(m, FALSE); |
0a7de745 A |
1355 | } |
1356 | vm_page_unlock_queues(); | |
2d21ac55 | 1357 | } |
39037602 | 1358 | assert(object == VM_PAGE_OBJECT(m)); |
1c79356b | 1359 | |
39037602 | 1360 | if (object->code_signed) { |
2d21ac55 A |
1361 | /* |
1362 | * CODE SIGNING: | |
1363 | * We just paged in a page from a signed | |
1364 | * memory object but we don't need to | |
1365 | * validate it now. We'll validate it if | |
1366 | * when it gets mapped into a user address | |
1367 | * space for the first time or when the page | |
1368 | * gets copied to another object as a result | |
1369 | * of a copy-on-write. | |
1370 | */ | |
1c79356b | 1371 | } |
2d21ac55 | 1372 | |
1c79356b | 1373 | /* |
2d21ac55 A |
1374 | * We mark the page busy and leave it on |
1375 | * the pageout queues. If the pageout | |
1376 | * deamon comes across it, then it will | |
1377 | * remove the page from the queue, but not the object | |
1c79356b | 1378 | */ |
1c79356b | 1379 | #if TRACEFAULTPAGE |
0a7de745 | 1380 | dbgTrace(0xBEEF000B, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ |
1c79356b | 1381 | #endif |
d9a64523 A |
1382 | assert(!m->vmp_busy); |
1383 | assert(!m->vmp_absent); | |
2d21ac55 | 1384 | |
d9a64523 | 1385 | m->vmp_busy = TRUE; |
1c79356b A |
1386 | break; |
1387 | } | |
5ba3f43e | 1388 | |
1c79356b | 1389 | |
2d21ac55 A |
1390 | /* |
1391 | * we get here when there is no page present in the object at | |
1392 | * the offset we're interested in... we'll allocate a page | |
1393 | * at this point if the pager associated with | |
1394 | * this object can provide the data or we're the top object... | |
1395 | * object is locked; m == NULL | |
1396 | */ | |
5ba3f43e | 1397 | |
39236c6e A |
1398 | if (must_be_resident) { |
1399 | if (fault_type == VM_PROT_NONE && | |
1400 | object == kernel_object) { | |
1401 | /* | |
1402 | * We've been called from vm_fault_unwire() | |
1403 | * while removing a map entry that was allocated | |
1404 | * with KMA_KOBJECT and KMA_VAONLY. This page | |
1405 | * is not present and there's nothing more to | |
1406 | * do here (nothing to unwire). | |
1407 | */ | |
1408 | vm_fault_cleanup(object, first_m); | |
1409 | thread_interrupt_level(interruptible_state); | |
1410 | ||
1411 | return VM_FAULT_MEMORY_ERROR; | |
1412 | } | |
1413 | ||
316670eb | 1414 | goto dont_look_for_page; |
39236c6e | 1415 | } |
5ba3f43e A |
1416 | |
1417 | /* Don't expect to fault pages into the kernel object. */ | |
1418 | assert(object != kernel_object); | |
1419 | ||
39236c6e | 1420 | data_supply = FALSE; |
39236c6e | 1421 | |
0a7de745 | 1422 | look_for_page = (object->pager_created && (MUST_ASK_PAGER(object, offset, external_state) == TRUE) && !data_supply); |
5ba3f43e | 1423 | |
1c79356b | 1424 | #if TRACEFAULTPAGE |
0a7de745 | 1425 | dbgTrace(0xBEEF000C, (unsigned int) look_for_page, (unsigned int) object); /* (TEST/DEBUG) */ |
1c79356b | 1426 | #endif |
316670eb | 1427 | if (!look_for_page && object == first_object && !object->phys_contiguous) { |
1c79356b | 1428 | /* |
316670eb | 1429 | * Allocate a new page for this object/offset pair as a placeholder |
1c79356b | 1430 | */ |
39037602 | 1431 | m = vm_page_grab_options(grab_options); |
1c79356b | 1432 | #if TRACEFAULTPAGE |
0a7de745 | 1433 | dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */ |
1c79356b A |
1434 | #endif |
1435 | if (m == VM_PAGE_NULL) { | |
1436 | vm_fault_cleanup(object, first_m); | |
9bccf70c | 1437 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 1438 | |
0a7de745 | 1439 | return VM_FAULT_MEMORY_SHORTAGE; |
1c79356b | 1440 | } |
316670eb A |
1441 | |
1442 | if (fault_info && fault_info->batch_pmap_op == TRUE) { | |
3e170ce0 | 1443 | vm_page_insert_internal(m, object, offset, VM_KERN_MEMORY_NONE, FALSE, TRUE, TRUE, FALSE, NULL); |
316670eb A |
1444 | } else { |
1445 | vm_page_insert(m, object, offset); | |
1446 | } | |
1c79356b | 1447 | } |
316670eb | 1448 | if (look_for_page) { |
0a7de745 A |
1449 | kern_return_t rc; |
1450 | int my_fault_type; | |
1c79356b A |
1451 | |
1452 | /* | |
1453 | * If the memory manager is not ready, we | |
1454 | * cannot make requests. | |
1455 | */ | |
1456 | if (!object->pager_ready) { | |
1457 | #if TRACEFAULTPAGE | |
0a7de745 | 1458 | dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */ |
1c79356b | 1459 | #endif |
0a7de745 A |
1460 | if (m != VM_PAGE_NULL) { |
1461 | VM_PAGE_FREE(m); | |
1462 | } | |
2d21ac55 | 1463 | |
2d21ac55 A |
1464 | /* |
1465 | * take an extra ref so object won't die | |
1466 | */ | |
1467 | vm_object_reference_locked(object); | |
1c79356b A |
1468 | vm_fault_cleanup(object, first_m); |
1469 | counter(c_vm_fault_page_block_backoff_kernel++); | |
2d21ac55 | 1470 | |
1c79356b A |
1471 | vm_object_lock(object); |
1472 | assert(object->ref_count > 0); | |
2d21ac55 | 1473 | |
1c79356b | 1474 | if (!object->pager_ready) { |
2d21ac55 A |
1475 | wait_result = vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGER_READY, interruptible); |
1476 | ||
1c79356b | 1477 | vm_object_unlock(object); |
0a7de745 | 1478 | if (wait_result == THREAD_WAITING) { |
9bccf70c | 1479 | wait_result = thread_block(THREAD_CONTINUE_NULL); |
0a7de745 | 1480 | } |
1c79356b | 1481 | vm_object_deallocate(object); |
2d21ac55 | 1482 | |
1c79356b A |
1483 | goto backoff; |
1484 | } else { | |
1485 | vm_object_unlock(object); | |
1486 | vm_object_deallocate(object); | |
9bccf70c | 1487 | thread_interrupt_level(interruptible_state); |
1c79356b | 1488 | |
0a7de745 | 1489 | return VM_FAULT_RETRY; |
0b4e3aa0 | 1490 | } |
0b4e3aa0 | 1491 | } |
2d21ac55 | 1492 | if (!object->internal && !object->phys_contiguous && object->paging_in_progress > vm_object_pagein_throttle) { |
1c79356b | 1493 | /* |
2d21ac55 A |
1494 | * If there are too many outstanding page |
1495 | * requests pending on this external object, we | |
1496 | * wait for them to be resolved now. | |
1c79356b | 1497 | */ |
1c79356b | 1498 | #if TRACEFAULTPAGE |
0a7de745 | 1499 | dbgTrace(0xBEEF0010, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ |
1c79356b | 1500 | #endif |
0a7de745 | 1501 | if (m != VM_PAGE_NULL) { |
1c79356b | 1502 | VM_PAGE_FREE(m); |
0a7de745 | 1503 | } |
1c79356b | 1504 | /* |
2d21ac55 | 1505 | * take an extra ref so object won't die |
1c79356b | 1506 | */ |
2d21ac55 | 1507 | vm_object_reference_locked(object); |
1c79356b | 1508 | |
1c79356b | 1509 | vm_fault_cleanup(object, first_m); |
2d21ac55 | 1510 | |
1c79356b | 1511 | counter(c_vm_fault_page_block_backoff_kernel++); |
2d21ac55 | 1512 | |
1c79356b A |
1513 | vm_object_lock(object); |
1514 | assert(object->ref_count > 0); | |
2d21ac55 | 1515 | |
6d2010ae | 1516 | if (object->paging_in_progress >= vm_object_pagein_throttle) { |
0a7de745 | 1517 | vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS, interruptible); |
2d21ac55 | 1518 | |
1c79356b | 1519 | vm_object_unlock(object); |
9bccf70c | 1520 | wait_result = thread_block(THREAD_CONTINUE_NULL); |
1c79356b | 1521 | vm_object_deallocate(object); |
2d21ac55 | 1522 | |
1c79356b A |
1523 | goto backoff; |
1524 | } else { | |
1525 | vm_object_unlock(object); | |
1526 | vm_object_deallocate(object); | |
9bccf70c | 1527 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 1528 | |
0a7de745 | 1529 | return VM_FAULT_RETRY; |
1c79356b A |
1530 | } |
1531 | } | |
39037602 | 1532 | if (object->internal) { |
fe8ab488 | 1533 | int compressed_count_delta; |
39236c6e | 1534 | |
39037602 A |
1535 | assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); |
1536 | ||
39236c6e A |
1537 | if (m == VM_PAGE_NULL) { |
1538 | /* | |
1539 | * Allocate a new page for this object/offset pair as a placeholder | |
1540 | */ | |
39037602 | 1541 | m = vm_page_grab_options(grab_options); |
39236c6e | 1542 | #if TRACEFAULTPAGE |
0a7de745 | 1543 | dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */ |
39236c6e A |
1544 | #endif |
1545 | if (m == VM_PAGE_NULL) { | |
39236c6e A |
1546 | vm_fault_cleanup(object, first_m); |
1547 | thread_interrupt_level(interruptible_state); | |
1548 | ||
0a7de745 | 1549 | return VM_FAULT_MEMORY_SHORTAGE; |
39236c6e A |
1550 | } |
1551 | ||
d9a64523 | 1552 | m->vmp_absent = TRUE; |
39236c6e | 1553 | if (fault_info && fault_info->batch_pmap_op == TRUE) { |
3e170ce0 | 1554 | vm_page_insert_internal(m, object, offset, VM_KERN_MEMORY_NONE, FALSE, TRUE, TRUE, FALSE, NULL); |
39236c6e A |
1555 | } else { |
1556 | vm_page_insert(m, object, offset); | |
1557 | } | |
1558 | } | |
d9a64523 | 1559 | assert(m->vmp_busy); |
5ba3f43e | 1560 | |
d9a64523 | 1561 | m->vmp_absent = TRUE; |
39236c6e A |
1562 | pager = object->pager; |
1563 | ||
fe8ab488 | 1564 | assert(object->paging_in_progress > 0); |
39236c6e A |
1565 | vm_object_unlock(object); |
1566 | ||
fe8ab488 A |
1567 | rc = vm_compressor_pager_get( |
1568 | pager, | |
1569 | offset + object->paging_offset, | |
39037602 | 1570 | VM_PAGE_GET_PHYS_PAGE(m), |
fe8ab488 A |
1571 | &my_fault_type, |
1572 | 0, | |
1573 | &compressed_count_delta); | |
39236c6e | 1574 | |
04b8595b | 1575 | if (type_of_fault == NULL) { |
0a7de745 | 1576 | int throttle_delay; |
04b8595b A |
1577 | |
1578 | /* | |
1579 | * we weren't called from vm_fault, so we | |
1580 | * need to apply page creation throttling | |
1581 | * do it before we re-acquire any locks | |
1582 | */ | |
1583 | if (my_fault_type == DBG_COMPRESSOR_FAULT) { | |
1584 | if ((throttle_delay = vm_page_throttled(TRUE))) { | |
1585 | VM_DEBUG_EVENT(vmf_compressordelay, VMF_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 1, 0); | |
1586 | delay(throttle_delay); | |
1587 | } | |
1588 | } | |
1589 | } | |
39236c6e | 1590 | vm_object_lock(object); |
fe8ab488 A |
1591 | assert(object->paging_in_progress > 0); |
1592 | ||
1593 | vm_compressor_pager_count( | |
1594 | pager, | |
1595 | compressed_count_delta, | |
1596 | FALSE, /* shared_lock */ | |
1597 | object); | |
39236c6e A |
1598 | |
1599 | switch (rc) { | |
1600 | case KERN_SUCCESS: | |
d9a64523 A |
1601 | m->vmp_absent = FALSE; |
1602 | m->vmp_dirty = TRUE; | |
39037602 | 1603 | if ((object->wimg_bits & |
0a7de745 | 1604 | VM_WIMG_MASK) != |
39236c6e A |
1605 | VM_WIMG_USE_DEFAULT) { |
1606 | /* | |
1607 | * If the page is not cacheable, | |
1608 | * we can't let its contents | |
1609 | * linger in the data cache | |
1610 | * after the decompression. | |
1611 | */ | |
1612 | pmap_sync_page_attributes_phys( | |
39037602 | 1613 | VM_PAGE_GET_PHYS_PAGE(m)); |
fe8ab488 | 1614 | } else { |
d9a64523 | 1615 | m->vmp_written_by_kernel = TRUE; |
fe8ab488 A |
1616 | } |
1617 | ||
1618 | /* | |
1619 | * If the object is purgeable, its | |
1620 | * owner's purgeable ledgers have been | |
1621 | * updated in vm_page_insert() but the | |
1622 | * page was also accounted for in a | |
1623 | * "compressed purgeable" ledger, so | |
1624 | * update that now. | |
1625 | */ | |
d9a64523 | 1626 | if (((object->purgable != |
0a7de745 A |
1627 | VM_PURGABLE_DENY) || |
1628 | object->vo_ledger_tag) && | |
d9a64523 | 1629 | (object->vo_owner != |
0a7de745 | 1630 | NULL)) { |
fe8ab488 A |
1631 | /* |
1632 | * One less compressed | |
d9a64523 | 1633 | * purgeable/tagged page. |
fe8ab488 | 1634 | */ |
d9a64523 | 1635 | vm_object_owner_compressed_update( |
fe8ab488 A |
1636 | object, |
1637 | -1); | |
1638 | } | |
1639 | ||
39236c6e A |
1640 | break; |
1641 | case KERN_MEMORY_FAILURE: | |
d9a64523 A |
1642 | m->vmp_unusual = TRUE; |
1643 | m->vmp_error = TRUE; | |
1644 | m->vmp_absent = FALSE; | |
39236c6e A |
1645 | break; |
1646 | case KERN_MEMORY_ERROR: | |
d9a64523 | 1647 | assert(m->vmp_absent); |
39236c6e A |
1648 | break; |
1649 | default: | |
fe8ab488 | 1650 | panic("vm_fault_page(): unexpected " |
0a7de745 A |
1651 | "error %d from " |
1652 | "vm_compressor_pager_get()\n", | |
1653 | rc); | |
39236c6e A |
1654 | } |
1655 | PAGE_WAKEUP_DONE(m); | |
1656 | ||
1657 | rc = KERN_SUCCESS; | |
1658 | goto data_requested; | |
1659 | } | |
1660 | my_fault_type = DBG_PAGEIN_FAULT; | |
5ba3f43e | 1661 | |
2d21ac55 | 1662 | if (m != VM_PAGE_NULL) { |
316670eb A |
1663 | VM_PAGE_FREE(m); |
1664 | m = VM_PAGE_NULL; | |
0b4e3aa0 | 1665 | } |
1c79356b | 1666 | |
1c79356b | 1667 | #if TRACEFAULTPAGE |
0a7de745 | 1668 | dbgTrace(0xBEEF0012, (unsigned int) object, (unsigned int) 0); /* (TEST/DEBUG) */ |
1c79356b | 1669 | #endif |
2d21ac55 | 1670 | |
1c79356b | 1671 | /* |
2d21ac55 | 1672 | * It's possible someone called vm_object_destroy while we weren't |
5ba3f43e | 1673 | * holding the object lock. If that has happened, then bail out |
2d21ac55 | 1674 | * here. |
1c79356b | 1675 | */ |
2d21ac55 A |
1676 | |
1677 | pager = object->pager; | |
1678 | ||
1679 | if (pager == MEMORY_OBJECT_NULL) { | |
1680 | vm_fault_cleanup(object, first_m); | |
1681 | thread_interrupt_level(interruptible_state); | |
1682 | return VM_FAULT_MEMORY_ERROR; | |
1683 | } | |
1c79356b A |
1684 | |
1685 | /* | |
2d21ac55 A |
1686 | * We have an absent page in place for the faulting offset, |
1687 | * so we can release the object lock. | |
1c79356b A |
1688 | */ |
1689 | ||
d9a64523 | 1690 | if (object->object_is_shared_cache) { |
5ba3f43e A |
1691 | set_thread_rwlock_boost(); |
1692 | } | |
1693 | ||
2d21ac55 | 1694 | vm_object_unlock(object); |
1c79356b A |
1695 | |
1696 | /* | |
2d21ac55 A |
1697 | * If this object uses a copy_call strategy, |
1698 | * and we are interested in a copy of this object | |
1699 | * (having gotten here only by following a | |
1700 | * shadow chain), then tell the memory manager | |
1701 | * via a flag added to the desired_access | |
1702 | * parameter, so that it can detect a race | |
1703 | * between our walking down the shadow chain | |
1704 | * and its pushing pages up into a copy of | |
1705 | * the object that it manages. | |
1c79356b | 1706 | */ |
0a7de745 | 1707 | if (object->copy_strategy == MEMORY_OBJECT_COPY_CALL && object != first_object) { |
1c79356b | 1708 | wants_copy_flag = VM_PROT_WANTS_COPY; |
0a7de745 | 1709 | } else { |
1c79356b | 1710 | wants_copy_flag = VM_PROT_NONE; |
0a7de745 | 1711 | } |
1c79356b | 1712 | |
316670eb A |
1713 | if (object->copy == first_object) { |
1714 | /* | |
1715 | * if we issue the memory_object_data_request in | |
1716 | * this state, we are subject to a deadlock with | |
1717 | * the underlying filesystem if it is trying to | |
1718 | * shrink the file resulting in a push of pages | |
1719 | * into the copy object... that push will stall | |
1720 | * on the placeholder page, and if the pushing thread | |
1721 | * is holding a lock that is required on the pagein | |
1722 | * path (such as a truncate lock), we'll deadlock... | |
1723 | * to avoid this potential deadlock, we throw away | |
1724 | * our placeholder page before calling memory_object_data_request | |
1725 | * and force this thread to retry the vm_fault_page after | |
1726 | * we have issued the I/O. the second time through this path | |
1727 | * we will find the page already in the cache (presumably still | |
1728 | * busy waiting for the I/O to complete) and then complete | |
1729 | * the fault w/o having to go through memory_object_data_request again | |
1730 | */ | |
1731 | assert(first_m != VM_PAGE_NULL); | |
39037602 | 1732 | assert(VM_PAGE_OBJECT(first_m) == first_object); |
5ba3f43e | 1733 | |
316670eb A |
1734 | vm_object_lock(first_object); |
1735 | VM_PAGE_FREE(first_m); | |
1736 | vm_object_paging_end(first_object); | |
1737 | vm_object_unlock(first_object); | |
1738 | ||
1739 | first_m = VM_PAGE_NULL; | |
1740 | force_fault_retry = TRUE; | |
1741 | ||
1742 | vm_fault_page_forced_retry++; | |
1743 | } | |
1744 | ||
1745 | if (data_already_requested == TRUE) { | |
1746 | orig_behavior = fault_info->behavior; | |
1747 | orig_cluster_size = fault_info->cluster_size; | |
1748 | ||
1749 | fault_info->behavior = VM_BEHAVIOR_RANDOM; | |
1750 | fault_info->cluster_size = PAGE_SIZE; | |
1751 | } | |
2d21ac55 A |
1752 | /* |
1753 | * Call the memory manager to retrieve the data. | |
1754 | */ | |
1755 | rc = memory_object_data_request( | |
1756 | pager, | |
1757 | offset + object->paging_offset, | |
1758 | PAGE_SIZE, | |
1759 | access_required | wants_copy_flag, | |
1760 | (memory_object_fault_info_t)fault_info); | |
1c79356b | 1761 | |
316670eb A |
1762 | if (data_already_requested == TRUE) { |
1763 | fault_info->behavior = orig_behavior; | |
1764 | fault_info->cluster_size = orig_cluster_size; | |
0a7de745 | 1765 | } else { |
316670eb | 1766 | data_already_requested = TRUE; |
0a7de745 | 1767 | } |
316670eb | 1768 | |
fe8ab488 | 1769 | DTRACE_VM2(maj_fault, int, 1, (uint64_t *), NULL); |
1c79356b | 1770 | #if TRACEFAULTPAGE |
0a7de745 | 1771 | dbgTrace(0xBEEF0013, (unsigned int) object, (unsigned int) rc); /* (TEST/DEBUG) */ |
1c79356b | 1772 | #endif |
2d21ac55 A |
1773 | vm_object_lock(object); |
1774 | ||
d9a64523 | 1775 | if (object->object_is_shared_cache) { |
5ba3f43e A |
1776 | clear_thread_rwlock_boost(); |
1777 | } | |
1778 | ||
0a7de745 | 1779 | data_requested: |
1c79356b | 1780 | if (rc != KERN_SUCCESS) { |
1c79356b | 1781 | vm_fault_cleanup(object, first_m); |
9bccf70c | 1782 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 1783 | |
0a7de745 A |
1784 | return (rc == MACH_SEND_INTERRUPTED) ? |
1785 | VM_FAULT_INTERRUPTED : | |
1786 | VM_FAULT_MEMORY_ERROR; | |
b0d623f7 A |
1787 | } else { |
1788 | clock_sec_t tv_sec; | |
1789 | clock_usec_t tv_usec; | |
39236c6e A |
1790 | |
1791 | if (my_fault_type == DBG_PAGEIN_FAULT) { | |
1792 | clock_get_system_microtime(&tv_sec, &tv_usec); | |
1793 | current_thread()->t_page_creation_time = tv_sec; | |
1794 | current_thread()->t_page_creation_count = 0; | |
1795 | } | |
1c79356b | 1796 | } |
6d2010ae | 1797 | if ((interruptible != THREAD_UNINT) && (current_thread()->sched_flags & TH_SFLAG_ABORT)) { |
1c79356b | 1798 | vm_fault_cleanup(object, first_m); |
9bccf70c | 1799 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 1800 | |
0a7de745 | 1801 | return VM_FAULT_INTERRUPTED; |
1c79356b | 1802 | } |
316670eb | 1803 | if (force_fault_retry == TRUE) { |
316670eb A |
1804 | vm_fault_cleanup(object, first_m); |
1805 | thread_interrupt_level(interruptible_state); | |
1806 | ||
0a7de745 | 1807 | return VM_FAULT_RETRY; |
316670eb | 1808 | } |
2d21ac55 | 1809 | if (m == VM_PAGE_NULL && object->phys_contiguous) { |
91447636 A |
1810 | /* |
1811 | * No page here means that the object we | |
5ba3f43e | 1812 | * initially looked up was "physically |
91447636 A |
1813 | * contiguous" (i.e. device memory). However, |
1814 | * with Virtual VRAM, the object might not | |
1815 | * be backed by that device memory anymore, | |
1816 | * so we're done here only if the object is | |
1817 | * still "phys_contiguous". | |
1818 | * Otherwise, if the object is no longer | |
1819 | * "phys_contiguous", we need to retry the | |
1820 | * page fault against the object's new backing | |
1821 | * store (different memory object). | |
1822 | */ | |
0a7de745 | 1823 | phys_contig_object: |
b0d623f7 | 1824 | goto done; |
91447636 | 1825 | } |
2d21ac55 A |
1826 | /* |
1827 | * potentially a pagein fault | |
1828 | * if we make it through the state checks | |
1829 | * above, than we'll count it as such | |
1830 | */ | |
39236c6e | 1831 | my_fault = my_fault_type; |
91447636 A |
1832 | |
1833 | /* | |
1834 | * Retry with same object/offset, since new data may | |
1835 | * be in a different page (i.e., m is meaningless at | |
1836 | * this point). | |
1837 | */ | |
1c79356b A |
1838 | continue; |
1839 | } | |
316670eb | 1840 | dont_look_for_page: |
1c79356b | 1841 | /* |
5ba3f43e | 1842 | * We get here if the object has no pager, or an existence map |
2d21ac55 A |
1843 | * exists and indicates the page isn't present on the pager |
1844 | * or we're unwiring a page. If a pager exists, but there | |
d9a64523 | 1845 | * is no existence map, then the m->vmp_absent case above handles |
2d21ac55 | 1846 | * the ZF case when the pager can't provide the page |
1c79356b A |
1847 | */ |
1848 | #if TRACEFAULTPAGE | |
0a7de745 | 1849 | dbgTrace(0xBEEF0014, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */ |
1c79356b | 1850 | #endif |
0a7de745 | 1851 | if (object == first_object) { |
1c79356b | 1852 | first_m = m; |
0a7de745 | 1853 | } else { |
1c79356b | 1854 | assert(m == VM_PAGE_NULL); |
0a7de745 | 1855 | } |
1c79356b | 1856 | |
1c79356b | 1857 | next_object = object->shadow; |
2d21ac55 | 1858 | |
1c79356b | 1859 | if (next_object == VM_OBJECT_NULL) { |
1c79356b | 1860 | /* |
2d21ac55 A |
1861 | * we've hit the bottom of the shadown chain, |
1862 | * fill the page in the top object with zeros. | |
1c79356b | 1863 | */ |
2d21ac55 | 1864 | assert(!must_be_resident); |
1c79356b A |
1865 | |
1866 | if (object != first_object) { | |
1867 | vm_object_paging_end(object); | |
1868 | vm_object_unlock(object); | |
1869 | ||
1870 | object = first_object; | |
1871 | offset = first_offset; | |
1872 | vm_object_lock(object); | |
1873 | } | |
1c79356b | 1874 | m = first_m; |
39037602 | 1875 | assert(VM_PAGE_OBJECT(m) == object); |
1c79356b A |
1876 | first_m = VM_PAGE_NULL; |
1877 | ||
55e303ae | 1878 | /* |
2d21ac55 A |
1879 | * check for any conditions that prevent |
1880 | * us from creating a new zero-fill page | |
5ba3f43e | 1881 | * vm_fault_check will do all of the |
2d21ac55 A |
1882 | * fault cleanup in the case of an error condition |
1883 | * including resetting the thread_interrupt_level | |
55e303ae | 1884 | */ |
04b8595b | 1885 | error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE); |
55e303ae | 1886 | |
0a7de745 A |
1887 | if (error != VM_FAULT_SUCCESS) { |
1888 | return error; | |
1889 | } | |
55e303ae | 1890 | |
2d21ac55 | 1891 | if (m == VM_PAGE_NULL) { |
39037602 | 1892 | m = vm_page_grab_options(grab_options); |
1c79356b | 1893 | |
2d21ac55 A |
1894 | if (m == VM_PAGE_NULL) { |
1895 | vm_fault_cleanup(object, VM_PAGE_NULL); | |
1896 | thread_interrupt_level(interruptible_state); | |
55e303ae | 1897 | |
0a7de745 | 1898 | return VM_FAULT_MEMORY_SHORTAGE; |
2d21ac55 A |
1899 | } |
1900 | vm_page_insert(m, object, offset); | |
0b4e3aa0 | 1901 | } |
0a7de745 | 1902 | if (fault_info->mark_zf_absent && no_zero_fill == TRUE) { |
d9a64523 | 1903 | m->vmp_absent = TRUE; |
0a7de745 | 1904 | } |
fe8ab488 A |
1905 | |
1906 | my_fault = vm_fault_zero_page(m, no_zero_fill); | |
1907 | ||
1c79356b | 1908 | break; |
2d21ac55 | 1909 | } else { |
0a7de745 | 1910 | /* |
2d21ac55 A |
1911 | * Move on to the next object. Lock the next |
1912 | * object before unlocking the current one. | |
1913 | */ | |
0a7de745 | 1914 | if ((object != first_object) || must_be_resident) { |
1c79356b | 1915 | vm_object_paging_end(object); |
0a7de745 | 1916 | } |
2d21ac55 | 1917 | |
6d2010ae A |
1918 | offset += object->vo_shadow_offset; |
1919 | fault_info->lo_offset += object->vo_shadow_offset; | |
1920 | fault_info->hi_offset += object->vo_shadow_offset; | |
1c79356b | 1921 | access_required = VM_PROT_READ; |
2d21ac55 | 1922 | |
1c79356b A |
1923 | vm_object_lock(next_object); |
1924 | vm_object_unlock(object); | |
2d21ac55 | 1925 | |
1c79356b A |
1926 | object = next_object; |
1927 | vm_object_paging_begin(object); | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | /* | |
1932 | * PAGE HAS BEEN FOUND. | |
1933 | * | |
1934 | * This page (m) is: | |
1935 | * busy, so that we can play with it; | |
1936 | * not absent, so that nobody else will fill it; | |
1937 | * possibly eligible for pageout; | |
1938 | * | |
1939 | * The top-level page (first_m) is: | |
1940 | * VM_PAGE_NULL if the page was found in the | |
1941 | * top-level object; | |
1942 | * busy, not absent, and ineligible for pageout. | |
1943 | * | |
1944 | * The current object (object) is locked. A paging | |
1945 | * reference is held for the current and top-level | |
1946 | * objects. | |
1947 | */ | |
1948 | ||
1949 | #if TRACEFAULTPAGE | |
0a7de745 | 1950 | dbgTrace(0xBEEF0015, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */ |
1c79356b | 1951 | #endif |
0a7de745 | 1952 | #if EXTRA_ASSERTIONS |
d9a64523 | 1953 | assert(m->vmp_busy && !m->vmp_absent); |
b0d623f7 | 1954 | assert((first_m == VM_PAGE_NULL) || |
0a7de745 A |
1955 | (first_m->vmp_busy && !first_m->vmp_absent && |
1956 | !first_m->vmp_active && !first_m->vmp_inactive && !first_m->vmp_secluded)); | |
1957 | #endif /* EXTRA_ASSERTIONS */ | |
1c79356b | 1958 | |
1c79356b | 1959 | /* |
2d21ac55 A |
1960 | * If the page is being written, but isn't |
1961 | * already owned by the top-level object, | |
1962 | * we have to copy it into a new page owned | |
1963 | * by the top-level object. | |
1c79356b | 1964 | */ |
b0d623f7 | 1965 | if (object != first_object) { |
1c79356b | 1966 | #if TRACEFAULTPAGE |
0a7de745 | 1967 | dbgTrace(0xBEEF0016, (unsigned int) object, (unsigned int) fault_type); /* (TEST/DEBUG) */ |
1c79356b | 1968 | #endif |
0a7de745 | 1969 | if (fault_type & VM_PROT_WRITE) { |
1c79356b A |
1970 | vm_page_t copy_m; |
1971 | ||
2d21ac55 A |
1972 | /* |
1973 | * We only really need to copy if we | |
1974 | * want to write it. | |
1975 | */ | |
1c79356b A |
1976 | assert(!must_be_resident); |
1977 | ||
1978 | /* | |
2d21ac55 A |
1979 | * If we try to collapse first_object at this |
1980 | * point, we may deadlock when we try to get | |
1981 | * the lock on an intermediate object (since we | |
1982 | * have the bottom object locked). We can't | |
1983 | * unlock the bottom object, because the page | |
1984 | * we found may move (by collapse) if we do. | |
1c79356b | 1985 | * |
2d21ac55 A |
1986 | * Instead, we first copy the page. Then, when |
1987 | * we have no more use for the bottom object, | |
1988 | * we unlock it and try to collapse. | |
1c79356b | 1989 | * |
2d21ac55 A |
1990 | * Note that we copy the page even if we didn't |
1991 | * need to... that's the breaks. | |
1c79356b A |
1992 | */ |
1993 | ||
1994 | /* | |
2d21ac55 | 1995 | * Allocate a page for the copy |
1c79356b | 1996 | */ |
39037602 | 1997 | copy_m = vm_page_grab_options(grab_options); |
2d21ac55 | 1998 | |
1c79356b A |
1999 | if (copy_m == VM_PAGE_NULL) { |
2000 | RELEASE_PAGE(m); | |
2d21ac55 | 2001 | |
1c79356b | 2002 | vm_fault_cleanup(object, first_m); |
9bccf70c | 2003 | thread_interrupt_level(interruptible_state); |
1c79356b | 2004 | |
0a7de745 | 2005 | return VM_FAULT_MEMORY_SHORTAGE; |
2d21ac55 | 2006 | } |
2d21ac55 | 2007 | |
1c79356b A |
2008 | vm_page_copy(m, copy_m); |
2009 | ||
2010 | /* | |
2d21ac55 A |
2011 | * If another map is truly sharing this |
2012 | * page with us, we have to flush all | |
2013 | * uses of the original page, since we | |
2014 | * can't distinguish those which want the | |
2015 | * original from those which need the | |
2016 | * new copy. | |
1c79356b | 2017 | * |
2d21ac55 A |
2018 | * XXXO If we know that only one map has |
2019 | * access to this page, then we could | |
2020 | * avoid the pmap_disconnect() call. | |
1c79356b | 2021 | */ |
0a7de745 A |
2022 | if (m->vmp_pmapped) { |
2023 | pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); | |
2024 | } | |
1c79356b | 2025 | |
d9a64523 | 2026 | if (m->vmp_clustered) { |
fe8ab488 A |
2027 | VM_PAGE_COUNT_AS_PAGEIN(m); |
2028 | VM_PAGE_CONSUME_CLUSTERED(m); | |
2029 | } | |
d9a64523 | 2030 | assert(!m->vmp_cleaning); |
1c79356b A |
2031 | |
2032 | /* | |
2d21ac55 | 2033 | * We no longer need the old page or object. |
1c79356b | 2034 | */ |
39236c6e A |
2035 | RELEASE_PAGE(m); |
2036 | ||
39037602 A |
2037 | /* |
2038 | * This check helps with marking the object as having a sequential pattern | |
2039 | * Normally we'll miss doing this below because this fault is about COW to | |
2040 | * the first_object i.e. bring page in from disk, push to object above but | |
2041 | * don't update the file object's sequential pattern. | |
2042 | */ | |
0a7de745 | 2043 | if (object->internal == FALSE) { |
39037602 A |
2044 | vm_fault_is_sequential(object, offset, fault_info->behavior); |
2045 | } | |
2046 | ||
1c79356b A |
2047 | vm_object_paging_end(object); |
2048 | vm_object_unlock(object); | |
2049 | ||
2d21ac55 A |
2050 | my_fault = DBG_COW_FAULT; |
2051 | VM_STAT_INCR(cow_faults); | |
2052 | DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL); | |
1c79356b | 2053 | current_task()->cow_faults++; |
2d21ac55 | 2054 | |
1c79356b A |
2055 | object = first_object; |
2056 | offset = first_offset; | |
2057 | ||
2058 | vm_object_lock(object); | |
2d21ac55 A |
2059 | /* |
2060 | * get rid of the place holder | |
2061 | * page that we soldered in earlier | |
2062 | */ | |
1c79356b A |
2063 | VM_PAGE_FREE(first_m); |
2064 | first_m = VM_PAGE_NULL; | |
5ba3f43e | 2065 | |
2d21ac55 A |
2066 | /* |
2067 | * and replace it with the | |
2068 | * page we just copied into | |
2069 | */ | |
d9a64523 | 2070 | assert(copy_m->vmp_busy); |
1c79356b | 2071 | vm_page_insert(copy_m, object, offset); |
316670eb | 2072 | SET_PAGE_DIRTY(copy_m, TRUE); |
1c79356b | 2073 | |
2d21ac55 | 2074 | m = copy_m; |
1c79356b | 2075 | /* |
2d21ac55 A |
2076 | * Now that we've gotten the copy out of the |
2077 | * way, let's try to collapse the top object. | |
2078 | * But we have to play ugly games with | |
2079 | * paging_in_progress to do that... | |
5ba3f43e A |
2080 | */ |
2081 | vm_object_paging_end(object); | |
0c530ab8 | 2082 | vm_object_collapse(object, offset, TRUE); |
1c79356b | 2083 | vm_object_paging_begin(object); |
0a7de745 A |
2084 | } else { |
2085 | *protection &= (~VM_PROT_WRITE); | |
2086 | } | |
1c79356b | 2087 | } |
1c79356b | 2088 | /* |
2d21ac55 A |
2089 | * Now check whether the page needs to be pushed into the |
2090 | * copy object. The use of asymmetric copy on write for | |
2091 | * shared temporary objects means that we may do two copies to | |
2092 | * satisfy the fault; one above to get the page from a | |
2093 | * shadowed object, and one here to push it into the copy. | |
1c79356b | 2094 | */ |
2d21ac55 | 2095 | try_failed_count = 0; |
1c79356b | 2096 | |
b0d623f7 | 2097 | while ((copy_object = first_object->copy) != VM_OBJECT_NULL) { |
0a7de745 A |
2098 | vm_object_offset_t copy_offset; |
2099 | vm_page_t copy_m; | |
1c79356b A |
2100 | |
2101 | #if TRACEFAULTPAGE | |
0a7de745 | 2102 | dbgTrace(0xBEEF0017, (unsigned int) copy_object, (unsigned int) fault_type); /* (TEST/DEBUG) */ |
1c79356b A |
2103 | #endif |
2104 | /* | |
2d21ac55 A |
2105 | * If the page is being written, but hasn't been |
2106 | * copied to the copy-object, we have to copy it there. | |
1c79356b | 2107 | */ |
1c79356b A |
2108 | if ((fault_type & VM_PROT_WRITE) == 0) { |
2109 | *protection &= ~VM_PROT_WRITE; | |
2110 | break; | |
2111 | } | |
2112 | ||
2113 | /* | |
2d21ac55 A |
2114 | * If the page was guaranteed to be resident, |
2115 | * we must have already performed the copy. | |
1c79356b | 2116 | */ |
0a7de745 | 2117 | if (must_be_resident) { |
1c79356b | 2118 | break; |
0a7de745 | 2119 | } |
1c79356b A |
2120 | |
2121 | /* | |
2d21ac55 | 2122 | * Try to get the lock on the copy_object. |
1c79356b A |
2123 | */ |
2124 | if (!vm_object_lock_try(copy_object)) { | |
2d21ac55 A |
2125 | vm_object_unlock(object); |
2126 | try_failed_count++; | |
1c79356b | 2127 | |
0a7de745 | 2128 | mutex_pause(try_failed_count); /* wait a bit */ |
1c79356b | 2129 | vm_object_lock(object); |
2d21ac55 | 2130 | |
1c79356b A |
2131 | continue; |
2132 | } | |
2d21ac55 | 2133 | try_failed_count = 0; |
1c79356b A |
2134 | |
2135 | /* | |
2d21ac55 A |
2136 | * Make another reference to the copy-object, |
2137 | * to keep it from disappearing during the | |
2138 | * copy. | |
1c79356b | 2139 | */ |
2d21ac55 | 2140 | vm_object_reference_locked(copy_object); |
1c79356b A |
2141 | |
2142 | /* | |
2d21ac55 | 2143 | * Does the page exist in the copy? |
1c79356b | 2144 | */ |
6d2010ae | 2145 | copy_offset = first_offset - copy_object->vo_shadow_offset; |
2d21ac55 | 2146 | |
0a7de745 | 2147 | if (copy_object->vo_size <= copy_offset) { |
1c79356b A |
2148 | /* |
2149 | * Copy object doesn't cover this page -- do nothing. | |
2150 | */ | |
2151 | ; | |
0a7de745 | 2152 | } else if ((copy_m = vm_page_lookup(copy_object, copy_offset)) != VM_PAGE_NULL) { |
2d21ac55 A |
2153 | /* |
2154 | * Page currently exists in the copy object | |
2155 | */ | |
d9a64523 | 2156 | if (copy_m->vmp_busy) { |
1c79356b | 2157 | /* |
2d21ac55 A |
2158 | * If the page is being brought |
2159 | * in, wait for it and then retry. | |
1c79356b A |
2160 | */ |
2161 | RELEASE_PAGE(m); | |
2d21ac55 A |
2162 | |
2163 | /* | |
2164 | * take an extra ref so object won't die | |
2165 | */ | |
2166 | vm_object_reference_locked(copy_object); | |
1c79356b A |
2167 | vm_object_unlock(copy_object); |
2168 | vm_fault_cleanup(object, first_m); | |
2169 | counter(c_vm_fault_page_block_backoff_kernel++); | |
2d21ac55 | 2170 | |
1c79356b A |
2171 | vm_object_lock(copy_object); |
2172 | assert(copy_object->ref_count > 0); | |
2173 | VM_OBJ_RES_DECR(copy_object); | |
2d21ac55 | 2174 | vm_object_lock_assert_exclusive(copy_object); |
1c79356b A |
2175 | copy_object->ref_count--; |
2176 | assert(copy_object->ref_count > 0); | |
2177 | copy_m = vm_page_lookup(copy_object, copy_offset); | |
5ba3f43e | 2178 | |
d9a64523 | 2179 | if (copy_m != VM_PAGE_NULL && copy_m->vmp_busy) { |
1c79356b | 2180 | PAGE_ASSERT_WAIT(copy_m, interruptible); |
2d21ac55 | 2181 | |
1c79356b | 2182 | vm_object_unlock(copy_object); |
9bccf70c | 2183 | wait_result = thread_block(THREAD_CONTINUE_NULL); |
1c79356b | 2184 | vm_object_deallocate(copy_object); |
2d21ac55 | 2185 | |
1c79356b A |
2186 | goto backoff; |
2187 | } else { | |
2188 | vm_object_unlock(copy_object); | |
2189 | vm_object_deallocate(copy_object); | |
9bccf70c | 2190 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 2191 | |
0a7de745 | 2192 | return VM_FAULT_RETRY; |
1c79356b A |
2193 | } |
2194 | } | |
0a7de745 | 2195 | } else if (!PAGED_OUT(copy_object, copy_offset)) { |
1c79356b A |
2196 | /* |
2197 | * If PAGED_OUT is TRUE, then the page used to exist | |
2198 | * in the copy-object, and has already been paged out. | |
2199 | * We don't need to repeat this. If PAGED_OUT is | |
2200 | * FALSE, then either we don't know (!pager_created, | |
2201 | * for example) or it hasn't been paged out. | |
2202 | * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT) | |
2203 | * We must copy the page to the copy object. | |
d9a64523 | 2204 | * |
2d21ac55 | 2205 | * Allocate a page for the copy |
1c79356b A |
2206 | */ |
2207 | copy_m = vm_page_alloc(copy_object, copy_offset); | |
2d21ac55 | 2208 | |
1c79356b A |
2209 | if (copy_m == VM_PAGE_NULL) { |
2210 | RELEASE_PAGE(m); | |
2d21ac55 | 2211 | |
1c79356b | 2212 | VM_OBJ_RES_DECR(copy_object); |
2d21ac55 | 2213 | vm_object_lock_assert_exclusive(copy_object); |
1c79356b A |
2214 | copy_object->ref_count--; |
2215 | assert(copy_object->ref_count > 0); | |
2d21ac55 | 2216 | |
1c79356b A |
2217 | vm_object_unlock(copy_object); |
2218 | vm_fault_cleanup(object, first_m); | |
9bccf70c | 2219 | thread_interrupt_level(interruptible_state); |
1c79356b | 2220 | |
0a7de745 | 2221 | return VM_FAULT_MEMORY_SHORTAGE; |
2d21ac55 | 2222 | } |
1c79356b | 2223 | /* |
2d21ac55 | 2224 | * Must copy page into copy-object. |
1c79356b | 2225 | */ |
1c79356b | 2226 | vm_page_copy(m, copy_m); |
5ba3f43e | 2227 | |
1c79356b | 2228 | /* |
2d21ac55 A |
2229 | * If the old page was in use by any users |
2230 | * of the copy-object, it must be removed | |
2231 | * from all pmaps. (We can't know which | |
2232 | * pmaps use it.) | |
1c79356b | 2233 | */ |
0a7de745 A |
2234 | if (m->vmp_pmapped) { |
2235 | pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); | |
2236 | } | |
1c79356b | 2237 | |
d9a64523 | 2238 | if (m->vmp_clustered) { |
fe8ab488 A |
2239 | VM_PAGE_COUNT_AS_PAGEIN(m); |
2240 | VM_PAGE_CONSUME_CLUSTERED(m); | |
2241 | } | |
1c79356b | 2242 | /* |
2d21ac55 A |
2243 | * If there's a pager, then immediately |
2244 | * page out this page, using the "initialize" | |
2245 | * option. Else, we use the copy. | |
1c79356b | 2246 | */ |
0a7de745 | 2247 | if ((!copy_object->pager_ready) |
39236c6e | 2248 | || VM_COMPRESSOR_PAGER_STATE_GET(copy_object, copy_offset) == VM_EXTERNAL_STATE_ABSENT |
0a7de745 | 2249 | ) { |
2d21ac55 | 2250 | vm_page_lockspin_queues(); |
d9a64523 | 2251 | assert(!m->vmp_cleaning); |
1c79356b A |
2252 | vm_page_activate(copy_m); |
2253 | vm_page_unlock_queues(); | |
2d21ac55 | 2254 | |
316670eb | 2255 | SET_PAGE_DIRTY(copy_m, TRUE); |
1c79356b | 2256 | PAGE_WAKEUP_DONE(copy_m); |
316670eb | 2257 | } else { |
d9a64523 A |
2258 | assert(copy_m->vmp_busy == TRUE); |
2259 | assert(!m->vmp_cleaning); | |
1c79356b A |
2260 | |
2261 | /* | |
2d21ac55 | 2262 | * dirty is protected by the object lock |
1c79356b | 2263 | */ |
316670eb | 2264 | SET_PAGE_DIRTY(copy_m, TRUE); |
1c79356b | 2265 | |
2d21ac55 A |
2266 | /* |
2267 | * The page is already ready for pageout: | |
2268 | * not on pageout queues and busy. | |
2269 | * Unlock everything except the | |
2270 | * copy_object itself. | |
2271 | */ | |
1c79356b A |
2272 | vm_object_unlock(object); |
2273 | ||
2274 | /* | |
2d21ac55 A |
2275 | * Write the page to the copy-object, |
2276 | * flushing it from the kernel. | |
1c79356b | 2277 | */ |
1c79356b A |
2278 | vm_pageout_initialize_page(copy_m); |
2279 | ||
2280 | /* | |
2d21ac55 A |
2281 | * Since the pageout may have |
2282 | * temporarily dropped the | |
2283 | * copy_object's lock, we | |
2284 | * check whether we'll have | |
2285 | * to deallocate the hard way. | |
1c79356b | 2286 | */ |
2d21ac55 | 2287 | if ((copy_object->shadow != object) || (copy_object->ref_count == 1)) { |
1c79356b A |
2288 | vm_object_unlock(copy_object); |
2289 | vm_object_deallocate(copy_object); | |
2290 | vm_object_lock(object); | |
2d21ac55 | 2291 | |
1c79356b A |
2292 | continue; |
2293 | } | |
1c79356b | 2294 | /* |
2d21ac55 A |
2295 | * Pick back up the old object's |
2296 | * lock. [It is safe to do so, | |
2297 | * since it must be deeper in the | |
2298 | * object tree.] | |
1c79356b | 2299 | */ |
1c79356b A |
2300 | vm_object_lock(object); |
2301 | } | |
316670eb | 2302 | |
1c79356b | 2303 | /* |
2d21ac55 A |
2304 | * Because we're pushing a page upward |
2305 | * in the object tree, we must restart | |
2306 | * any faults that are waiting here. | |
2307 | * [Note that this is an expansion of | |
2308 | * PAGE_WAKEUP that uses the THREAD_RESTART | |
2309 | * wait result]. Can't turn off the page's | |
2310 | * busy bit because we're not done with it. | |
1c79356b | 2311 | */ |
d9a64523 A |
2312 | if (m->vmp_wanted) { |
2313 | m->vmp_wanted = FALSE; | |
2d21ac55 | 2314 | thread_wakeup_with_result((event_t) m, THREAD_RESTART); |
1c79356b A |
2315 | } |
2316 | } | |
1c79356b | 2317 | /* |
2d21ac55 A |
2318 | * The reference count on copy_object must be |
2319 | * at least 2: one for our extra reference, | |
2320 | * and at least one from the outside world | |
2321 | * (we checked that when we last locked | |
2322 | * copy_object). | |
1c79356b | 2323 | */ |
2d21ac55 | 2324 | vm_object_lock_assert_exclusive(copy_object); |
1c79356b A |
2325 | copy_object->ref_count--; |
2326 | assert(copy_object->ref_count > 0); | |
2d21ac55 | 2327 | |
5ba3f43e | 2328 | VM_OBJ_RES_DECR(copy_object); |
1c79356b A |
2329 | vm_object_unlock(copy_object); |
2330 | ||
2331 | break; | |
2332 | } | |
b0d623f7 A |
2333 | |
2334 | done: | |
1c79356b A |
2335 | *result_page = m; |
2336 | *top_page = first_m; | |
2337 | ||
2d21ac55 | 2338 | if (m != VM_PAGE_NULL) { |
39037602 A |
2339 | assert(VM_PAGE_OBJECT(m) == object); |
2340 | ||
b0d623f7 | 2341 | retval = VM_FAULT_SUCCESS; |
fe8ab488 | 2342 | |
2d21ac55 | 2343 | if (my_fault == DBG_PAGEIN_FAULT) { |
fe8ab488 | 2344 | VM_PAGE_COUNT_AS_PAGEIN(m); |
2d21ac55 | 2345 | |
0a7de745 | 2346 | if (object->internal) { |
b0d623f7 | 2347 | my_fault = DBG_PAGEIND_FAULT; |
0a7de745 | 2348 | } else { |
b0d623f7 | 2349 | my_fault = DBG_PAGEINV_FAULT; |
0a7de745 | 2350 | } |
2d21ac55 | 2351 | |
0a7de745 | 2352 | /* |
2d21ac55 A |
2353 | * evaluate access pattern and update state |
2354 | * vm_fault_deactivate_behind depends on the | |
2355 | * state being up to date | |
2356 | */ | |
0a7de745 | 2357 | vm_fault_is_sequential(object, offset, fault_info->behavior); |
d9a64523 | 2358 | vm_fault_deactivate_behind(object, offset, fault_info->behavior); |
d9a64523 | 2359 | } else if (type_of_fault == NULL && my_fault == DBG_CACHE_HIT_FAULT) { |
0a7de745 | 2360 | /* |
d9a64523 A |
2361 | * we weren't called from vm_fault, so handle the |
2362 | * accounting here for hits in the cache | |
2363 | */ | |
0a7de745 A |
2364 | if (m->vmp_clustered) { |
2365 | VM_PAGE_COUNT_AS_PAGEIN(m); | |
d9a64523 A |
2366 | VM_PAGE_CONSUME_CLUSTERED(m); |
2367 | } | |
0a7de745 | 2368 | vm_fault_is_sequential(object, offset, fault_info->behavior); |
2d21ac55 | 2369 | vm_fault_deactivate_behind(object, offset, fault_info->behavior); |
39236c6e | 2370 | } else if (my_fault == DBG_COMPRESSOR_FAULT || my_fault == DBG_COMPRESSOR_SWAPIN_FAULT) { |
cb323159 | 2371 | VM_STAT_DECOMPRESSIONS(); |
2d21ac55 | 2372 | } |
0a7de745 A |
2373 | if (type_of_fault) { |
2374 | *type_of_fault = my_fault; | |
2375 | } | |
b0d623f7 A |
2376 | } else { |
2377 | retval = VM_FAULT_SUCCESS_NO_VM_PAGE; | |
2378 | assert(first_m == VM_PAGE_NULL); | |
2379 | assert(object == first_object); | |
2380 | } | |
2d21ac55 | 2381 | |
55e303ae A |
2382 | thread_interrupt_level(interruptible_state); |
2383 | ||
1c79356b | 2384 | #if TRACEFAULTPAGE |
0a7de745 | 2385 | dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS, 0); /* (TEST/DEBUG) */ |
1c79356b | 2386 | #endif |
b0d623f7 | 2387 | return retval; |
1c79356b | 2388 | |
2d21ac55 | 2389 | backoff: |
9bccf70c | 2390 | thread_interrupt_level(interruptible_state); |
2d21ac55 | 2391 | |
0a7de745 A |
2392 | if (wait_result == THREAD_INTERRUPTED) { |
2393 | return VM_FAULT_INTERRUPTED; | |
2394 | } | |
2395 | return VM_FAULT_RETRY; | |
1c79356b | 2396 | |
0a7de745 | 2397 | #undef RELEASE_PAGE |
1c79356b A |
2398 | } |
2399 | ||
2d21ac55 A |
2400 | |
2401 | ||
593a1d5f A |
2402 | /* |
2403 | * CODE SIGNING: | |
2404 | * When soft faulting a page, we have to validate the page if: | |
2405 | * 1. the page is being mapped in user space | |
2406 | * 2. the page hasn't already been found to be "tainted" | |
2407 | * 3. the page belongs to a code-signed object | |
2408 | * 4. the page has not been validated yet or has been mapped for write. | |
2409 | */ | |
0a7de745 A |
2410 | #define VM_FAULT_NEED_CS_VALIDATION(pmap, page, page_obj) \ |
2411 | ((pmap) != kernel_pmap /*1*/ && \ | |
2412 | !(page)->vmp_cs_tainted /*2*/ && \ | |
2413 | (page_obj)->code_signed /*3*/ && \ | |
2414 | (!(page)->vmp_cs_validated || (page)->vmp_wpmapped /*4*/ )) | |
593a1d5f A |
2415 | |
2416 | ||
55e303ae | 2417 | /* |
2d21ac55 | 2418 | * page queue lock must NOT be held |
d9a64523 | 2419 | * m->vmp_object must be locked |
2d21ac55 | 2420 | * |
d9a64523 | 2421 | * NOTE: m->vmp_object could be locked "shared" only if we are called |
2d21ac55 A |
2422 | * from vm_fault() as part of a soft fault. If so, we must be |
2423 | * careful not to modify the VM object in any way that is not | |
2424 | * legal under a shared lock... | |
55e303ae | 2425 | */ |
39037602 | 2426 | extern int panic_on_cs_killed; |
15129b1c A |
2427 | extern int proc_selfpid(void); |
2428 | extern char *proc_name_address(void *p); | |
2d21ac55 A |
2429 | unsigned long cs_enter_tainted_rejected = 0; |
2430 | unsigned long cs_enter_tainted_accepted = 0; | |
2431 | kern_return_t | |
2432 | vm_fault_enter(vm_page_t m, | |
0a7de745 A |
2433 | pmap_t pmap, |
2434 | vm_map_offset_t vaddr, | |
2435 | vm_prot_t prot, | |
2436 | vm_prot_t caller_prot, | |
2437 | boolean_t wired, | |
2438 | boolean_t change_wiring, | |
2439 | vm_tag_t wire_tag, | |
2440 | vm_object_fault_info_t fault_info, | |
2441 | boolean_t *need_retry, | |
2442 | int *type_of_fault) | |
55e303ae | 2443 | { |
0a7de745 A |
2444 | kern_return_t kr, pe_result; |
2445 | boolean_t previously_pmapped = m->vmp_pmapped; | |
2446 | boolean_t must_disconnect = 0; | |
2447 | boolean_t map_is_switched, map_is_switch_protected; | |
2448 | boolean_t cs_violation; | |
2449 | int cs_enforcement_enabled; | |
3e170ce0 | 2450 | vm_prot_t fault_type; |
0a7de745 A |
2451 | vm_object_t object; |
2452 | boolean_t no_cache = fault_info->no_cache; | |
2453 | boolean_t cs_bypass = fault_info->cs_bypass; | |
2454 | int pmap_options = fault_info->pmap_options; | |
5ba3f43e | 2455 | |
3e170ce0 | 2456 | fault_type = change_wiring ? VM_PROT_NONE : caller_prot; |
39037602 | 2457 | object = VM_PAGE_OBJECT(m); |
3e170ce0 | 2458 | |
39037602 | 2459 | vm_object_lock_assert_held(object); |
5ba3f43e A |
2460 | |
2461 | #if KASAN | |
2462 | if (pmap == kernel_pmap) { | |
2463 | kasan_notify_address(vaddr, PAGE_SIZE); | |
2464 | } | |
2465 | #endif | |
2466 | ||
39037602 | 2467 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED); |
2d21ac55 | 2468 | |
39037602 | 2469 | if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) { |
d9a64523 | 2470 | assert(m->vmp_fictitious); |
2d21ac55 A |
2471 | return KERN_SUCCESS; |
2472 | } | |
2473 | ||
6d2010ae | 2474 | if (*type_of_fault == DBG_ZERO_FILL_FAULT) { |
39037602 | 2475 | vm_object_lock_assert_exclusive(object); |
d9a64523 | 2476 | } else if ((fault_type & VM_PROT_WRITE) == 0 && |
0a7de745 | 2477 | (!m->vmp_wpmapped |
d9a64523 | 2478 | #if VM_OBJECT_ACCESS_TRACKING |
0a7de745 | 2479 | || object->access_tracking |
d9a64523 | 2480 | #endif /* VM_OBJECT_ACCESS_TRACKING */ |
0a7de745 | 2481 | )) { |
2d21ac55 | 2482 | /* |
6d2010ae A |
2483 | * This is not a "write" fault, so we |
2484 | * might not have taken the object lock | |
2485 | * exclusively and we might not be able | |
2486 | * to update the "wpmapped" bit in | |
2487 | * vm_fault_enter(). | |
2488 | * Let's just grant read access to | |
2489 | * the page for now and we'll | |
2490 | * soft-fault again if we need write | |
2491 | * access later... | |
2d21ac55 | 2492 | */ |
5ba3f43e A |
2493 | |
2494 | /* This had better not be a JIT page. */ | |
2495 | if (!pmap_has_prot_policy(prot)) { | |
2496 | prot &= ~VM_PROT_WRITE; | |
2497 | } else { | |
2498 | assert(cs_bypass); | |
2499 | } | |
fe8ab488 | 2500 | } |
d9a64523 | 2501 | if (m->vmp_pmapped == FALSE) { |
d9a64523 | 2502 | if (m->vmp_clustered) { |
fe8ab488 A |
2503 | if (*type_of_fault == DBG_CACHE_HIT_FAULT) { |
2504 | /* | |
2505 | * found it in the cache, but this | |
d9a64523 | 2506 | * is the first fault-in of the page (m->vmp_pmapped == FALSE) |
fe8ab488 A |
2507 | * so it must have come in as part of |
2508 | * a cluster... account 1 pagein against it | |
2509 | */ | |
0a7de745 | 2510 | if (object->internal) { |
fe8ab488 | 2511 | *type_of_fault = DBG_PAGEIND_FAULT; |
0a7de745 | 2512 | } else { |
fe8ab488 | 2513 | *type_of_fault = DBG_PAGEINV_FAULT; |
0a7de745 | 2514 | } |
5ba3f43e | 2515 | |
fe8ab488 | 2516 | VM_PAGE_COUNT_AS_PAGEIN(m); |
55e303ae | 2517 | } |
fe8ab488 | 2518 | VM_PAGE_CONSUME_CLUSTERED(m); |
2d21ac55 | 2519 | } |
6d2010ae | 2520 | } |
2d21ac55 A |
2521 | |
2522 | if (*type_of_fault != DBG_COW_FAULT) { | |
2523 | DTRACE_VM2(as_fault, int, 1, (uint64_t *), NULL); | |
2524 | ||
2525 | if (pmap == kernel_pmap) { | |
2526 | DTRACE_VM2(kernel_asflt, int, 1, (uint64_t *), NULL); | |
2527 | } | |
2528 | } | |
2529 | ||
b0d623f7 | 2530 | /* Validate code signature if necessary. */ |
d9a64523 A |
2531 | if (!cs_bypass && |
2532 | VM_FAULT_NEED_CS_VALIDATION(pmap, m, object)) { | |
39037602 | 2533 | vm_object_lock_assert_exclusive(object); |
593a1d5f | 2534 | |
d9a64523 | 2535 | if (m->vmp_cs_validated) { |
593a1d5f A |
2536 | vm_cs_revalidates++; |
2537 | } | |
2538 | ||
5ba3f43e | 2539 | /* VM map is locked, so 1 ref will remain on VM object - |
b0d623f7 | 2540 | * so no harm if vm_page_validate_cs drops the object lock */ |
d9a64523 A |
2541 | |
2542 | #if PMAP_CS | |
2543 | if (fault_info->pmap_cs_associated && | |
2544 | pmap_cs_enforced(pmap) && | |
2545 | !m->vmp_cs_validated && | |
2546 | !m->vmp_cs_tainted && | |
2547 | !m->vmp_cs_nx && | |
2548 | (prot & VM_PROT_EXECUTE) && | |
2549 | (caller_prot & VM_PROT_EXECUTE)) { | |
2550 | /* | |
2551 | * With pmap_cs, the pmap layer will validate the | |
2552 | * code signature for any executable pmap mapping. | |
2553 | * No need for us to validate this page too: | |
2554 | * in pmap_cs we trust... | |
2555 | */ | |
2556 | vm_cs_defer_to_pmap_cs++; | |
2557 | } else { | |
2558 | vm_cs_defer_to_pmap_cs_not++; | |
2559 | vm_page_validate_cs(m); | |
2560 | } | |
2561 | #else /* PMAP_CS */ | |
593a1d5f | 2562 | vm_page_validate_cs(m); |
d9a64523 | 2563 | #endif /* PMAP_CS */ |
593a1d5f A |
2564 | } |
2565 | ||
0a7de745 | 2566 | #define page_immutable(m, prot) ((m)->vmp_cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/ ) |
d9a64523 | 2567 | #define page_nx(m) ((m)->vmp_cs_nx) |
b0d623f7 A |
2568 | |
2569 | map_is_switched = ((pmap != vm_map_pmap(current_task()->map)) && | |
0a7de745 | 2570 | (pmap == vm_map_pmap(current_thread()->map))); |
b0d623f7 | 2571 | map_is_switch_protected = current_thread()->map->switch_protect; |
5ba3f43e | 2572 | |
b0d623f7 | 2573 | /* If the map is switched, and is switch-protected, we must protect |
5ba3f43e | 2574 | * some pages from being write-faulted: immutable pages because by |
b0d623f7 A |
2575 | * definition they may not be written, and executable pages because that |
2576 | * would provide a way to inject unsigned code. | |
2577 | * If the page is immutable, we can simply return. However, we can't | |
2578 | * immediately determine whether a page is executable anywhere. But, | |
2579 | * we can disconnect it everywhere and remove the executable protection | |
5ba3f43e | 2580 | * from the current map. We do that below right before we do the |
b0d623f7 A |
2581 | * PMAP_ENTER. |
2582 | */ | |
eb6b6ca3 A |
2583 | if (pmap == kernel_pmap) { |
2584 | /* kernel fault: cs_process_enforcement() does not apply */ | |
2585 | cs_enforcement_enabled = 0; | |
2586 | } else { | |
2587 | cs_enforcement_enabled = cs_process_enforcement(NULL); | |
2588 | } | |
39236c6e | 2589 | |
0a7de745 A |
2590 | if (cs_enforcement_enabled && map_is_switched && |
2591 | map_is_switch_protected && page_immutable(m, prot) && | |
2592 | (prot & VM_PROT_WRITE)) { | |
b0d623f7 A |
2593 | return KERN_CODESIGN_ERROR; |
2594 | } | |
2595 | ||
c18c124e | 2596 | if (cs_enforcement_enabled && page_nx(m) && (prot & VM_PROT_EXECUTE)) { |
0a7de745 | 2597 | if (cs_debug) { |
c18c124e | 2598 | printf("page marked to be NX, not letting it be mapped EXEC\n"); |
0a7de745 | 2599 | } |
c18c124e A |
2600 | return KERN_CODESIGN_ERROR; |
2601 | } | |
2602 | ||
b0d623f7 A |
2603 | /* A page could be tainted, or pose a risk of being tainted later. |
2604 | * Check whether the receiving process wants it, and make it feel | |
2605 | * the consequences (that hapens in cs_invalid_page()). | |
5ba3f43e A |
2606 | * For CS Enforcement, two other conditions will |
2607 | * cause that page to be tainted as well: | |
b0d623f7 A |
2608 | * - pmapping an unsigned page executable - this means unsigned code; |
2609 | * - writeable mapping of a validated page - the content of that page | |
2610 | * can be changed without the kernel noticing, therefore unsigned | |
2611 | * code can be created | |
2612 | */ | |
d9a64523 A |
2613 | if (cs_bypass) { |
2614 | /* code-signing is bypassed */ | |
2615 | cs_violation = FALSE; | |
2616 | } else if (m->vmp_cs_tainted) { | |
2617 | /* tainted page */ | |
2618 | cs_violation = TRUE; | |
2619 | } else if (!cs_enforcement_enabled) { | |
2620 | /* no further code-signing enforcement */ | |
2621 | cs_violation = FALSE; | |
2622 | } else if (page_immutable(m, prot) && | |
0a7de745 A |
2623 | ((prot & VM_PROT_WRITE) || |
2624 | m->vmp_wpmapped)) { | |
d9a64523 A |
2625 | /* |
2626 | * The page should be immutable, but is in danger of being | |
2627 | * modified. | |
2628 | * This is the case where we want policy from the code | |
2629 | * directory - is the page immutable or not? For now we have | |
2630 | * to assume that code pages will be immutable, data pages not. | |
2631 | * We'll assume a page is a code page if it has a code directory | |
2632 | * and we fault for execution. | |
2633 | * That is good enough since if we faulted the code page for | |
2634 | * writing in another map before, it is wpmapped; if we fault | |
2635 | * it for writing in this map later it will also be faulted for | |
2636 | * executing at the same time; and if we fault for writing in | |
2637 | * another map later, we will disconnect it from this pmap so | |
2638 | * we'll notice the change. | |
2639 | */ | |
2640 | cs_violation = TRUE; | |
2641 | } else if (!m->vmp_cs_validated && | |
0a7de745 | 2642 | (prot & VM_PROT_EXECUTE) |
d9a64523 | 2643 | #if PMAP_CS |
0a7de745 A |
2644 | /* |
2645 | * Executable pages will be validated by pmap_cs; | |
2646 | * in pmap_cs we trust... | |
2647 | * If pmap_cs is turned off, this is a code-signing | |
2648 | * violation. | |
2649 | */ | |
2650 | && !(pmap_cs_enforced(pmap)) | |
d9a64523 | 2651 | #endif /* PMAP_CS */ |
0a7de745 | 2652 | ) { |
d9a64523 A |
2653 | cs_violation = TRUE; |
2654 | } else { | |
2655 | cs_violation = FALSE; | |
2656 | } | |
2657 | ||
2658 | if (cs_violation) { | |
b0d623f7 A |
2659 | /* We will have a tainted page. Have to handle the special case |
2660 | * of a switched map now. If the map is not switched, standard | |
2661 | * procedure applies - call cs_invalid_page(). | |
2662 | * If the map is switched, the real owner is invalid already. | |
2663 | * There is no point in invalidating the switching process since | |
2664 | * it will not be executing from the map. So we don't call | |
2665 | * cs_invalid_page() in that case. */ | |
39037602 | 2666 | boolean_t reject_page, cs_killed; |
0a7de745 A |
2667 | if (map_is_switched) { |
2668 | assert(pmap == vm_map_pmap(current_thread()->map)); | |
b0d623f7 A |
2669 | assert(!(prot & VM_PROT_WRITE) || (map_is_switch_protected == FALSE)); |
2670 | reject_page = FALSE; | |
2671 | } else { | |
0a7de745 | 2672 | if (cs_debug > 5) { |
d9a64523 | 2673 | printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s prot: 0x%x\n", |
0a7de745 A |
2674 | object->code_signed ? "yes" : "no", |
2675 | m->vmp_cs_validated ? "yes" : "no", | |
2676 | m->vmp_cs_tainted ? "yes" : "no", | |
2677 | m->vmp_wpmapped ? "yes" : "no", | |
2678 | (int)prot); | |
2679 | } | |
39037602 | 2680 | reject_page = cs_invalid_page((addr64_t) vaddr, &cs_killed); |
b0d623f7 | 2681 | } |
5ba3f43e | 2682 | |
b0d623f7 | 2683 | if (reject_page) { |
fe8ab488 | 2684 | /* reject the invalid page: abort the page fault */ |
0a7de745 A |
2685 | int pid; |
2686 | const char *procname; | |
2687 | task_t task; | |
2688 | vm_object_t file_object, shadow; | |
2689 | vm_object_offset_t file_offset; | |
2690 | char *pathname, *filename; | |
2691 | vm_size_t pathname_len, filename_len; | |
2692 | boolean_t truncated_path; | |
15129b1c | 2693 | #define __PATH_MAX 1024 |
0a7de745 A |
2694 | struct timespec mtime, cs_mtime; |
2695 | int shadow_depth; | |
2696 | os_reason_t codesigning_exit_reason = OS_REASON_NULL; | |
15129b1c | 2697 | |
b0d623f7 A |
2698 | kr = KERN_CODESIGN_ERROR; |
2699 | cs_enter_tainted_rejected++; | |
15129b1c A |
2700 | |
2701 | /* get process name and pid */ | |
2702 | procname = "?"; | |
2703 | task = current_task(); | |
2704 | pid = proc_selfpid(); | |
0a7de745 | 2705 | if (task->bsd_info != NULL) { |
15129b1c | 2706 | procname = proc_name_address(task->bsd_info); |
0a7de745 | 2707 | } |
15129b1c A |
2708 | |
2709 | /* get file's VM object */ | |
39037602 | 2710 | file_object = object; |
d9a64523 | 2711 | file_offset = m->vmp_offset; |
39037602 | 2712 | for (shadow = file_object->shadow, |
0a7de745 A |
2713 | shadow_depth = 0; |
2714 | shadow != VM_OBJECT_NULL; | |
2715 | shadow = file_object->shadow, | |
2716 | shadow_depth++) { | |
15129b1c | 2717 | vm_object_lock_shared(shadow); |
39037602 | 2718 | if (file_object != object) { |
15129b1c A |
2719 | vm_object_unlock(file_object); |
2720 | } | |
2721 | file_offset += file_object->vo_shadow_offset; | |
2722 | file_object = shadow; | |
2723 | } | |
2724 | ||
2725 | mtime.tv_sec = 0; | |
2726 | mtime.tv_nsec = 0; | |
2727 | cs_mtime.tv_sec = 0; | |
2728 | cs_mtime.tv_nsec = 0; | |
2729 | ||
2730 | /* get file's pathname and/or filename */ | |
2731 | pathname = NULL; | |
2732 | filename = NULL; | |
2733 | pathname_len = 0; | |
2734 | filename_len = 0; | |
2735 | truncated_path = FALSE; | |
3e170ce0 A |
2736 | /* no pager -> no file -> no pathname, use "<nil>" in that case */ |
2737 | if (file_object->pager != NULL) { | |
15129b1c A |
2738 | pathname = (char *)kalloc(__PATH_MAX * 2); |
2739 | if (pathname) { | |
fe8ab488 | 2740 | pathname[0] = '\0'; |
15129b1c A |
2741 | pathname_len = __PATH_MAX; |
2742 | filename = pathname + pathname_len; | |
2743 | filename_len = __PATH_MAX; | |
cb323159 A |
2744 | |
2745 | if (vnode_pager_get_object_name(file_object->pager, | |
2746 | pathname, | |
2747 | pathname_len, | |
2748 | filename, | |
2749 | filename_len, | |
2750 | &truncated_path) == KERN_SUCCESS) { | |
2751 | /* safety first... */ | |
2752 | pathname[__PATH_MAX - 1] = '\0'; | |
2753 | filename[__PATH_MAX - 1] = '\0'; | |
2754 | ||
2755 | vnode_pager_get_object_mtime(file_object->pager, | |
2756 | &mtime, | |
2757 | &cs_mtime); | |
2758 | } else { | |
2759 | kfree(pathname, __PATH_MAX * 2); | |
2760 | pathname = NULL; | |
2761 | filename = NULL; | |
2762 | pathname_len = 0; | |
2763 | filename_len = 0; | |
2764 | truncated_path = FALSE; | |
2765 | } | |
15129b1c | 2766 | } |
15129b1c A |
2767 | } |
2768 | printf("CODE SIGNING: process %d[%s]: " | |
0a7de745 A |
2769 | "rejecting invalid page at address 0x%llx " |
2770 | "from offset 0x%llx in file \"%s%s%s\" " | |
2771 | "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) " | |
2772 | "(signed:%d validated:%d tainted:%d nx:%d " | |
2773 | "wpmapped:%d dirty:%d depth:%d)\n", | |
2774 | pid, procname, (addr64_t) vaddr, | |
2775 | file_offset, | |
2776 | (pathname ? pathname : "<nil>"), | |
2777 | (truncated_path ? "/.../" : ""), | |
2778 | (truncated_path ? filename : ""), | |
2779 | cs_mtime.tv_sec, cs_mtime.tv_nsec, | |
2780 | ((cs_mtime.tv_sec == mtime.tv_sec && | |
2781 | cs_mtime.tv_nsec == mtime.tv_nsec) | |
2782 | ? "==" | |
2783 | : "!="), | |
2784 | mtime.tv_sec, mtime.tv_nsec, | |
2785 | object->code_signed, | |
2786 | m->vmp_cs_validated, | |
2787 | m->vmp_cs_tainted, | |
2788 | m->vmp_cs_nx, | |
2789 | m->vmp_wpmapped, | |
2790 | m->vmp_dirty, | |
2791 | shadow_depth); | |
39037602 A |
2792 | |
2793 | /* | |
2794 | * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page | |
2795 | * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the | |
2796 | * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler | |
2797 | * will deal with the segmentation fault. | |
2798 | */ | |
2799 | if (cs_killed) { | |
2800 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE, | |
0a7de745 | 2801 | pid, OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE, 0, 0); |
39037602 A |
2802 | |
2803 | codesigning_exit_reason = os_reason_create(OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE); | |
2804 | if (codesigning_exit_reason == NULL) { | |
2805 | printf("vm_fault_enter: failed to allocate codesigning exit reason\n"); | |
2806 | } else { | |
2807 | mach_vm_address_t data_addr = 0; | |
2808 | struct codesigning_exit_reason_info *ceri = NULL; | |
2809 | uint32_t reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(1, sizeof(*ceri)); | |
2810 | ||
d190cdc3 | 2811 | if (os_reason_alloc_buffer_noblock(codesigning_exit_reason, reason_buffer_size_estimate)) { |
39037602 A |
2812 | printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n"); |
2813 | } else { | |
2814 | if (KERN_SUCCESS == kcdata_get_memory_addr(&codesigning_exit_reason->osr_kcd_descriptor, | |
0a7de745 | 2815 | EXIT_REASON_CODESIGNING_INFO, sizeof(*ceri), &data_addr)) { |
39037602 A |
2816 | ceri = (struct codesigning_exit_reason_info *)data_addr; |
2817 | static_assert(__PATH_MAX == sizeof(ceri->ceri_pathname)); | |
2818 | ||
2819 | ceri->ceri_virt_addr = vaddr; | |
2820 | ceri->ceri_file_offset = file_offset; | |
0a7de745 | 2821 | if (pathname) { |
39037602 | 2822 | strncpy((char *)&ceri->ceri_pathname, pathname, sizeof(ceri->ceri_pathname)); |
0a7de745 | 2823 | } else { |
39037602 | 2824 | ceri->ceri_pathname[0] = '\0'; |
0a7de745 A |
2825 | } |
2826 | if (filename) { | |
39037602 | 2827 | strncpy((char *)&ceri->ceri_filename, filename, sizeof(ceri->ceri_filename)); |
0a7de745 | 2828 | } else { |
39037602 | 2829 | ceri->ceri_filename[0] = '\0'; |
0a7de745 | 2830 | } |
39037602 A |
2831 | ceri->ceri_path_truncated = (truncated_path); |
2832 | ceri->ceri_codesig_modtime_secs = cs_mtime.tv_sec; | |
2833 | ceri->ceri_codesig_modtime_nsecs = cs_mtime.tv_nsec; | |
2834 | ceri->ceri_page_modtime_secs = mtime.tv_sec; | |
2835 | ceri->ceri_page_modtime_nsecs = mtime.tv_nsec; | |
2836 | ceri->ceri_object_codesigned = (object->code_signed); | |
d9a64523 A |
2837 | ceri->ceri_page_codesig_validated = (m->vmp_cs_validated); |
2838 | ceri->ceri_page_codesig_tainted = (m->vmp_cs_tainted); | |
2839 | ceri->ceri_page_codesig_nx = (m->vmp_cs_nx); | |
2840 | ceri->ceri_page_wpmapped = (m->vmp_wpmapped); | |
2841 | ceri->ceri_page_slid = 0; | |
2842 | ceri->ceri_page_dirty = (m->vmp_dirty); | |
39037602 A |
2843 | ceri->ceri_page_shadow_depth = shadow_depth; |
2844 | } else { | |
2845 | #if DEBUG || DEVELOPMENT | |
2846 | panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason"); | |
2847 | #else | |
2848 | printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n"); | |
2849 | #endif /* DEBUG || DEVELOPMENT */ | |
2850 | /* Free the buffer */ | |
d190cdc3 | 2851 | os_reason_alloc_buffer_noblock(codesigning_exit_reason, 0); |
39037602 A |
2852 | } |
2853 | } | |
2854 | } | |
2855 | ||
2856 | set_thread_exit_reason(current_thread(), codesigning_exit_reason, FALSE); | |
2857 | } | |
2858 | if (panic_on_cs_killed && | |
d9a64523 | 2859 | object->object_is_shared_cache) { |
cb323159 A |
2860 | char *tainted_contents; |
2861 | vm_map_offset_t src_vaddr; | |
2862 | src_vaddr = (vm_map_offset_t) phystokv((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m) << PAGE_SHIFT); | |
2863 | tainted_contents = kalloc(PAGE_SIZE); | |
2864 | bcopy((const char *)src_vaddr, tainted_contents, PAGE_SIZE); | |
2865 | printf("CODE SIGNING: tainted page %p phys 0x%x phystokv 0x%llx copied to %p\n", m, VM_PAGE_GET_PHYS_PAGE(m), (uint64_t)src_vaddr, tainted_contents); | |
39037602 | 2866 | panic("CODE SIGNING: process %d[%s]: " |
cb323159 | 2867 | "rejecting invalid page (phys#0x%x) at address 0x%llx " |
0a7de745 A |
2868 | "from offset 0x%llx in file \"%s%s%s\" " |
2869 | "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) " | |
2870 | "(signed:%d validated:%d tainted:%d nx:%d" | |
2871 | "wpmapped:%d dirty:%d depth:%d)\n", | |
cb323159 A |
2872 | pid, procname, |
2873 | VM_PAGE_GET_PHYS_PAGE(m), | |
2874 | (addr64_t) vaddr, | |
0a7de745 A |
2875 | file_offset, |
2876 | (pathname ? pathname : "<nil>"), | |
2877 | (truncated_path ? "/.../" : ""), | |
2878 | (truncated_path ? filename : ""), | |
2879 | cs_mtime.tv_sec, cs_mtime.tv_nsec, | |
2880 | ((cs_mtime.tv_sec == mtime.tv_sec && | |
2881 | cs_mtime.tv_nsec == mtime.tv_nsec) | |
2882 | ? "==" | |
2883 | : "!="), | |
2884 | mtime.tv_sec, mtime.tv_nsec, | |
2885 | object->code_signed, | |
2886 | m->vmp_cs_validated, | |
2887 | m->vmp_cs_tainted, | |
2888 | m->vmp_cs_nx, | |
2889 | m->vmp_wpmapped, | |
2890 | m->vmp_dirty, | |
2891 | shadow_depth); | |
39037602 A |
2892 | } |
2893 | ||
2894 | if (file_object != object) { | |
15129b1c A |
2895 | vm_object_unlock(file_object); |
2896 | } | |
2897 | if (pathname_len != 0) { | |
2898 | kfree(pathname, __PATH_MAX * 2); | |
2899 | pathname = NULL; | |
2900 | filename = NULL; | |
2901 | } | |
b0d623f7 | 2902 | } else { |
fe8ab488 | 2903 | /* proceed with the invalid page */ |
b0d623f7 | 2904 | kr = KERN_SUCCESS; |
d9a64523 | 2905 | if (!m->vmp_cs_validated && |
39037602 | 2906 | !object->code_signed) { |
fe8ab488 | 2907 | /* |
490019cf A |
2908 | * This page has not been (fully) validated but |
2909 | * does not belong to a code-signed object | |
2910 | * so it should not be forcefully considered | |
fe8ab488 A |
2911 | * as tainted. |
2912 | * We're just concerned about it here because | |
2913 | * we've been asked to "execute" it but that | |
2914 | * does not mean that it should cause other | |
2915 | * accesses to fail. | |
2916 | * This happens when a debugger sets a | |
2917 | * breakpoint and we then execute code in | |
2918 | * that page. Marking the page as "tainted" | |
2919 | * would cause any inspection tool ("leaks", | |
2920 | * "vmmap", "CrashReporter", ...) to get killed | |
2921 | * due to code-signing violation on that page, | |
2922 | * even though they're just reading it and not | |
2923 | * executing from it. | |
2924 | */ | |
fe8ab488 A |
2925 | } else { |
2926 | /* | |
2927 | * Page might have been tainted before or not; | |
2928 | * now it definitively is. If the page wasn't | |
2929 | * tainted, we must disconnect it from all | |
2930 | * pmaps later, to force existing mappings | |
2931 | * through that code path for re-consideration | |
2932 | * of the validity of that page. | |
2933 | */ | |
d9a64523 A |
2934 | must_disconnect = !m->vmp_cs_tainted; |
2935 | m->vmp_cs_tainted = TRUE; | |
fe8ab488 | 2936 | } |
b0d623f7 | 2937 | cs_enter_tainted_accepted++; |
2d21ac55 | 2938 | } |
15129b1c A |
2939 | if (kr != KERN_SUCCESS) { |
2940 | if (cs_debug) { | |
2941 | printf("CODESIGNING: vm_fault_enter(0x%llx): " | |
0a7de745 A |
2942 | "*** INVALID PAGE ***\n", |
2943 | (long long)vaddr); | |
15129b1c | 2944 | } |
39236c6e | 2945 | #if !SECURE_KERNEL |
15129b1c | 2946 | if (cs_enforcement_panic) { |
39236c6e A |
2947 | panic("CODESIGNING: panicking on invalid page\n"); |
2948 | } | |
2949 | #endif | |
2d21ac55 A |
2950 | } |
2951 | } else { | |
2952 | /* proceed with the valid page */ | |
2953 | kr = KERN_SUCCESS; | |
2954 | } | |
2955 | ||
0a7de745 A |
2956 | boolean_t page_queues_locked = FALSE; |
2957 | #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \ | |
2958 | MACRO_BEGIN \ | |
2959 | if (! page_queues_locked) { \ | |
2960 | page_queues_locked = TRUE; \ | |
2961 | vm_page_lockspin_queues(); \ | |
2962 | } \ | |
39236c6e | 2963 | MACRO_END |
0a7de745 A |
2964 | #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \ |
2965 | MACRO_BEGIN \ | |
2966 | if (page_queues_locked) { \ | |
2967 | page_queues_locked = FALSE; \ | |
2968 | vm_page_unlock_queues(); \ | |
2969 | } \ | |
39236c6e A |
2970 | MACRO_END |
2971 | ||
2972 | /* | |
2973 | * Hold queues lock to manipulate | |
2974 | * the page queues. Change wiring | |
2975 | * case is obvious. | |
2976 | */ | |
d9a64523 | 2977 | assert((m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || object != compressor_object); |
39037602 A |
2978 | |
2979 | #if CONFIG_BACKGROUND_QUEUE | |
2980 | vm_page_update_background_state(m); | |
2981 | #endif | |
d9a64523 | 2982 | if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) { |
39236c6e A |
2983 | /* |
2984 | * Compressor pages are neither wired | |
2985 | * nor pageable and should never change. | |
2986 | */ | |
39037602 | 2987 | assert(object == compressor_object); |
39236c6e | 2988 | } else if (change_wiring) { |
0a7de745 | 2989 | __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED(); |
39236c6e A |
2990 | |
2991 | if (wired) { | |
2992 | if (kr == KERN_SUCCESS) { | |
5ba3f43e | 2993 | vm_page_wire(m, wire_tag, TRUE); |
39236c6e A |
2994 | } |
2995 | } else { | |
0a7de745 | 2996 | vm_page_unwire(m, TRUE); |
39236c6e A |
2997 | } |
2998 | /* we keep the page queues lock, if we need it later */ | |
39236c6e | 2999 | } else { |
39037602 A |
3000 | if (object->internal == TRUE) { |
3001 | /* | |
3002 | * don't allow anonymous pages on | |
3003 | * the speculative queues | |
3004 | */ | |
3005 | no_cache = FALSE; | |
3006 | } | |
0a7de745 A |
3007 | if (kr != KERN_SUCCESS) { |
3008 | __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED(); | |
3009 | vm_page_deactivate(m); | |
39236c6e | 3010 | /* we keep the page queues lock, if we need it later */ |
d9a64523 | 3011 | } else if (((m->vmp_q_state == VM_PAGE_NOT_ON_Q) || |
0a7de745 A |
3012 | (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) || |
3013 | (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) || | |
3014 | ((m->vmp_q_state != VM_PAGE_ON_THROTTLED_Q) && no_cache)) && | |
3015 | !VM_PAGE_WIRED(m)) { | |
5ba3f43e | 3016 | if (vm_page_local_q && |
39236c6e | 3017 | (*type_of_fault == DBG_COW_FAULT || |
0a7de745 A |
3018 | *type_of_fault == DBG_ZERO_FILL_FAULT)) { |
3019 | struct vpl *lq; | |
3020 | uint32_t lid; | |
39236c6e | 3021 | |
d9a64523 | 3022 | assert(m->vmp_q_state == VM_PAGE_NOT_ON_Q); |
39037602 | 3023 | |
39236c6e | 3024 | __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED(); |
39037602 | 3025 | vm_object_lock_assert_exclusive(object); |
39236c6e A |
3026 | |
3027 | /* | |
3028 | * we got a local queue to stuff this | |
3029 | * new page on... | |
3030 | * its safe to manipulate local and | |
3031 | * local_id at this point since we're | |
3032 | * behind an exclusive object lock and | |
3033 | * the page is not on any global queue. | |
3034 | * | |
3035 | * we'll use the current cpu number to | |
3036 | * select the queue note that we don't | |
3037 | * need to disable preemption... we're | |
39037602 | 3038 | * going to be behind the local queue's |
39236c6e A |
3039 | * lock to do the real work |
3040 | */ | |
3041 | lid = cpu_number(); | |
3042 | ||
3043 | lq = &vm_page_local_q[lid].vpl_un.vpl; | |
3044 | ||
3045 | VPL_LOCK(&lq->vpl_lock); | |
3046 | ||
3e170ce0 | 3047 | vm_page_check_pageable_safe(m); |
0a7de745 | 3048 | vm_page_queue_enter(&lq->vpl_queue, m, vmp_pageq); |
d9a64523 A |
3049 | m->vmp_q_state = VM_PAGE_ON_ACTIVE_LOCAL_Q; |
3050 | m->vmp_local_id = lid; | |
39236c6e | 3051 | lq->vpl_count++; |
5ba3f43e | 3052 | |
0a7de745 | 3053 | if (object->internal) { |
39236c6e | 3054 | lq->vpl_internal_count++; |
0a7de745 | 3055 | } else { |
39236c6e | 3056 | lq->vpl_external_count++; |
0a7de745 | 3057 | } |
39236c6e A |
3058 | |
3059 | VPL_UNLOCK(&lq->vpl_lock); | |
3060 | ||
0a7de745 | 3061 | if (lq->vpl_count > vm_page_local_q_soft_limit) { |
39236c6e A |
3062 | /* |
3063 | * we're beyond the soft limit | |
3064 | * for the local queue | |
3065 | * vm_page_reactivate_local will | |
3066 | * 'try' to take the global page | |
3067 | * queue lock... if it can't | |
3068 | * that's ok... we'll let the | |
3069 | * queue continue to grow up | |
3070 | * to the hard limit... at that | |
3071 | * point we'll wait for the | |
3072 | * lock... once we've got the | |
3073 | * lock, we'll transfer all of | |
3074 | * the pages from the local | |
3075 | * queue to the global active | |
3076 | * queue | |
3077 | */ | |
3078 | vm_page_reactivate_local(lid, FALSE, FALSE); | |
3079 | } | |
3080 | } else { | |
39236c6e A |
3081 | __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED(); |
3082 | ||
3083 | /* | |
3084 | * test again now that we hold the | |
3085 | * page queue lock | |
3086 | */ | |
3087 | if (!VM_PAGE_WIRED(m)) { | |
d9a64523 | 3088 | if (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) { |
39037602 | 3089 | vm_page_queues_remove(m, FALSE); |
39236c6e | 3090 | |
d9a64523 A |
3091 | VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated, 1); |
3092 | VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated, 1); | |
39236c6e A |
3093 | } |
3094 | ||
0a7de745 A |
3095 | if (!VM_PAGE_ACTIVE_OR_INACTIVE(m) || |
3096 | no_cache) { | |
39236c6e A |
3097 | /* |
3098 | * If this is a no_cache mapping | |
3099 | * and the page has never been | |
3100 | * mapped before or was | |
3101 | * previously a no_cache page, | |
3102 | * then we want to leave pages | |
3103 | * in the speculative state so | |
3104 | * that they can be readily | |
3105 | * recycled if free memory runs | |
3106 | * low. Otherwise the page is | |
5ba3f43e | 3107 | * activated as normal. |
39236c6e A |
3108 | */ |
3109 | ||
3110 | if (no_cache && | |
3111 | (!previously_pmapped || | |
0a7de745 | 3112 | m->vmp_no_cache)) { |
d9a64523 | 3113 | m->vmp_no_cache = TRUE; |
39236c6e | 3114 | |
0a7de745 | 3115 | if (m->vmp_q_state != VM_PAGE_ON_SPECULATIVE_Q) { |
39236c6e | 3116 | vm_page_speculate(m, FALSE); |
0a7de745 A |
3117 | } |
3118 | } else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m)) { | |
39236c6e A |
3119 | vm_page_activate(m); |
3120 | } | |
3121 | } | |
3122 | } | |
3123 | /* we keep the page queues lock, if we need it later */ | |
3124 | } | |
3125 | } | |
3126 | } | |
39236c6e A |
3127 | /* we're done with the page queues lock, if we ever took it */ |
3128 | __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED(); | |
3129 | ||
3130 | ||
b0d623f7 A |
3131 | /* If we have a KERN_SUCCESS from the previous checks, we either have |
3132 | * a good page, or a tainted page that has been accepted by the process. | |
3133 | * In both cases the page will be entered into the pmap. | |
3134 | * If the page is writeable, we need to disconnect it from other pmaps | |
3135 | * now so those processes can take note. | |
3136 | */ | |
2d21ac55 | 3137 | if (kr == KERN_SUCCESS) { |
0a7de745 | 3138 | /* |
2d21ac55 | 3139 | * NOTE: we may only hold the vm_object lock SHARED |
5ba3f43e | 3140 | * at this point, so we need the phys_page lock to |
fe8ab488 A |
3141 | * properly serialize updating the pmapped and |
3142 | * xpmapped bits | |
2d21ac55 | 3143 | */ |
d9a64523 | 3144 | if ((prot & VM_PROT_EXECUTE) && !m->vmp_xpmapped) { |
0a7de745 | 3145 | ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m); |
fe8ab488 | 3146 | |
39037602 | 3147 | pmap_lock_phys_page(phys_page); |
fe8ab488 A |
3148 | /* |
3149 | * go ahead and take the opportunity | |
3150 | * to set 'pmapped' here so that we don't | |
3151 | * need to grab this lock a 2nd time | |
3152 | * just below | |
3153 | */ | |
d9a64523 | 3154 | m->vmp_pmapped = TRUE; |
5ba3f43e | 3155 | |
d9a64523 | 3156 | if (!m->vmp_xpmapped) { |
d9a64523 | 3157 | m->vmp_xpmapped = TRUE; |
fe8ab488 | 3158 | |
39037602 | 3159 | pmap_unlock_phys_page(phys_page); |
fe8ab488 | 3160 | |
0a7de745 | 3161 | if (!object->internal) { |
fe8ab488 | 3162 | OSAddAtomic(1, &vm_page_xpmapped_external_count); |
0a7de745 | 3163 | } |
fe8ab488 | 3164 | |
5ba3f43e A |
3165 | #if defined(__arm__) || defined(__arm64__) |
3166 | pmap_sync_page_data_phys(phys_page); | |
3167 | #else | |
39037602 A |
3168 | if (object->internal && |
3169 | object->pager != NULL) { | |
fe8ab488 A |
3170 | /* |
3171 | * This page could have been | |
3172 | * uncompressed by the | |
3173 | * compressor pager and its | |
3174 | * contents might be only in | |
3175 | * the data cache. | |
3176 | * Since it's being mapped for | |
3177 | * "execute" for the fist time, | |
3178 | * make sure the icache is in | |
3179 | * sync. | |
3180 | */ | |
39037602 A |
3181 | assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); |
3182 | pmap_sync_page_data_phys(phys_page); | |
fe8ab488 | 3183 | } |
5ba3f43e | 3184 | #endif |
0a7de745 | 3185 | } else { |
39037602 | 3186 | pmap_unlock_phys_page(phys_page); |
0a7de745 | 3187 | } |
fe8ab488 | 3188 | } else { |
d9a64523 | 3189 | if (m->vmp_pmapped == FALSE) { |
0a7de745 | 3190 | ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m); |
39037602 A |
3191 | |
3192 | pmap_lock_phys_page(phys_page); | |
d9a64523 | 3193 | m->vmp_pmapped = TRUE; |
39037602 | 3194 | pmap_unlock_phys_page(phys_page); |
fe8ab488 A |
3195 | } |
3196 | } | |
6d2010ae A |
3197 | |
3198 | if (fault_type & VM_PROT_WRITE) { | |
d9a64523 | 3199 | if (m->vmp_wpmapped == FALSE) { |
39037602 A |
3200 | vm_object_lock_assert_exclusive(object); |
3201 | if (!object->internal && object->pager) { | |
3202 | task_update_logical_writes(current_task(), PAGE_SIZE, TASK_WRITE_DEFERRED, vnode_pager_lookup_vnode(object->pager)); | |
3203 | } | |
d9a64523 | 3204 | m->vmp_wpmapped = TRUE; |
6d2010ae A |
3205 | } |
3206 | if (must_disconnect) { | |
3207 | /* | |
5ba3f43e | 3208 | * We can only get here |
6d2010ae A |
3209 | * because of the CSE logic |
3210 | */ | |
39236c6e | 3211 | assert(cs_enforcement_enabled); |
39037602 | 3212 | pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); |
5ba3f43e | 3213 | /* |
6d2010ae | 3214 | * If we are faulting for a write, we can clear |
b0d623f7 A |
3215 | * the execute bit - that will ensure the page is |
3216 | * checked again before being executable, which | |
3217 | * protects against a map switch. | |
3218 | * This only happens the first time the page | |
5ba3f43e | 3219 | * gets tainted, so we won't get stuck here |
6d2010ae A |
3220 | * to make an already writeable page executable. |
3221 | */ | |
0a7de745 | 3222 | if (!cs_bypass) { |
5ba3f43e | 3223 | assert(!pmap_has_prot_policy(prot)); |
6d2010ae A |
3224 | prot &= ~VM_PROT_EXECUTE; |
3225 | } | |
b0d623f7 | 3226 | } |
4a3eedf9 | 3227 | } |
39037602 | 3228 | assert(VM_PAGE_OBJECT(m) == object); |
d1ecb069 | 3229 | |
d9a64523 A |
3230 | #if VM_OBJECT_ACCESS_TRACKING |
3231 | if (object->access_tracking) { | |
3232 | DTRACE_VM2(access_tracking, vm_map_offset_t, vaddr, int, fault_type); | |
3233 | if (fault_type & VM_PROT_WRITE) { | |
3234 | object->access_tracking_writes++; | |
3235 | vm_object_access_tracking_writes++; | |
3236 | } else { | |
3237 | object->access_tracking_reads++; | |
3238 | vm_object_access_tracking_reads++; | |
3239 | } | |
3240 | } | |
3241 | #endif /* VM_OBJECT_ACCESS_TRACKING */ | |
3242 | ||
cb323159 | 3243 | |
d9a64523 | 3244 | #if PMAP_CS |
cb323159 | 3245 | pmap_enter_retry: |
d9a64523 | 3246 | #endif |
d1ecb069 A |
3247 | /* Prevent a deadlock by not |
3248 | * holding the object lock if we need to wait for a page in | |
3249 | * pmap_enter() - <rdar://problem/7138958> */ | |
316670eb | 3250 | PMAP_ENTER_OPTIONS(pmap, vaddr, m, prot, fault_type, 0, |
0a7de745 A |
3251 | wired, |
3252 | pmap_options | PMAP_OPTIONS_NOWAIT, | |
3253 | pe_result); | |
cb323159 A |
3254 | #if PMAP_CS |
3255 | /* | |
3256 | * Retry without execute permission if we encountered a codesigning | |
3257 | * failure on a non-execute fault. This allows applications which | |
3258 | * don't actually need to execute code to still map it for read access. | |
3259 | */ | |
3260 | if ((pe_result == KERN_CODESIGN_ERROR) && pmap_cs_enforced(pmap) && | |
3261 | (prot & VM_PROT_EXECUTE) && !(caller_prot & VM_PROT_EXECUTE)) { | |
3262 | prot &= ~VM_PROT_EXECUTE; | |
3263 | goto pmap_enter_retry; | |
3264 | } | |
3265 | #endif | |
5ba3f43e A |
3266 | #if __x86_64__ |
3267 | if (pe_result == KERN_INVALID_ARGUMENT && | |
3268 | pmap == PMAP_NULL && | |
3269 | wired) { | |
3270 | /* | |
3271 | * Wiring a page in a pmap-less VM map: | |
3272 | * VMware's "vmmon" kernel extension does this | |
3273 | * to grab pages. | |
3274 | * Let it proceed even though the PMAP_ENTER() failed. | |
3275 | */ | |
3276 | pe_result = KERN_SUCCESS; | |
3277 | } | |
3278 | #endif /* __x86_64__ */ | |
d1ecb069 | 3279 | |
0a7de745 | 3280 | if (pe_result == KERN_RESOURCE_SHORTAGE) { |
316670eb A |
3281 | if (need_retry) { |
3282 | /* | |
3283 | * this will be non-null in the case where we hold the lock | |
3284 | * on the top-object in this chain... we can't just drop | |
3285 | * the lock on the object we're inserting the page into | |
3286 | * and recall the PMAP_ENTER since we can still cause | |
5ba3f43e | 3287 | * a deadlock if one of the critical paths tries to |
316670eb A |
3288 | * acquire the lock on the top-object and we're blocked |
3289 | * in PMAP_ENTER waiting for memory... our only recourse | |
5ba3f43e | 3290 | * is to deal with it at a higher level where we can |
316670eb A |
3291 | * drop both locks. |
3292 | */ | |
3293 | *need_retry = TRUE; | |
3294 | vm_pmap_enter_retried++; | |
3295 | goto after_the_pmap_enter; | |
3296 | } | |
d1ecb069 | 3297 | /* The nonblocking version of pmap_enter did not succeed. |
316670eb | 3298 | * and we don't need to drop other locks and retry |
5ba3f43e | 3299 | * at the level above us, so |
316670eb | 3300 | * use the blocking version instead. Requires marking |
d1ecb069 | 3301 | * the page busy and unlocking the object */ |
d9a64523 | 3302 | boolean_t was_busy = m->vmp_busy; |
39236c6e | 3303 | |
39037602 | 3304 | vm_object_lock_assert_exclusive(object); |
39236c6e | 3305 | |
d9a64523 | 3306 | m->vmp_busy = TRUE; |
39037602 | 3307 | vm_object_unlock(object); |
5ba3f43e | 3308 | |
fe8ab488 | 3309 | PMAP_ENTER_OPTIONS(pmap, vaddr, m, prot, fault_type, |
0a7de745 A |
3310 | 0, wired, |
3311 | pmap_options, pe_result); | |
5ba3f43e | 3312 | |
39037602 A |
3313 | assert(VM_PAGE_OBJECT(m) == object); |
3314 | ||
d1ecb069 | 3315 | /* Take the object lock again. */ |
39037602 | 3316 | vm_object_lock(object); |
5ba3f43e | 3317 | |
d1ecb069 A |
3318 | /* If the page was busy, someone else will wake it up. |
3319 | * Otherwise, we have to do it now. */ | |
d9a64523 | 3320 | assert(m->vmp_busy); |
0a7de745 | 3321 | if (!was_busy) { |
d1ecb069 A |
3322 | PAGE_WAKEUP_DONE(m); |
3323 | } | |
3324 | vm_pmap_enter_blocked++; | |
3325 | } | |
5ba3f43e A |
3326 | |
3327 | kr = pe_result; | |
2d21ac55 A |
3328 | } |
3329 | ||
6d2010ae | 3330 | after_the_pmap_enter: |
2d21ac55 | 3331 | return kr; |
55e303ae A |
3332 | } |
3333 | ||
fe8ab488 | 3334 | void |
cb323159 | 3335 | vm_pre_fault(vm_map_offset_t vaddr, vm_prot_t prot) |
fe8ab488 A |
3336 | { |
3337 | if (pmap_find_phys(current_map()->pmap, vaddr) == 0) { | |
5ba3f43e | 3338 | vm_fault(current_map(), /* map */ |
0a7de745 | 3339 | vaddr, /* vaddr */ |
cb323159 | 3340 | prot, /* fault_type */ |
0a7de745 A |
3341 | FALSE, /* change_wiring */ |
3342 | VM_KERN_MEMORY_NONE, /* tag - not wiring */ | |
3343 | THREAD_UNINT, /* interruptible */ | |
3344 | NULL, /* caller_pmap */ | |
3345 | 0 /* caller_pmap_addr */); | |
fe8ab488 A |
3346 | } |
3347 | } | |
3348 | ||
2d21ac55 | 3349 | |
1c79356b A |
3350 | /* |
3351 | * Routine: vm_fault | |
3352 | * Purpose: | |
3353 | * Handle page faults, including pseudo-faults | |
3354 | * used to change the wiring status of pages. | |
3355 | * Returns: | |
3356 | * Explicit continuations have been removed. | |
3357 | * Implementation: | |
3358 | * vm_fault and vm_fault_page save mucho state | |
3359 | * in the moral equivalent of a closure. The state | |
3360 | * structure is allocated when first entering vm_fault | |
3361 | * and deallocated when leaving vm_fault. | |
3362 | */ | |
3363 | ||
91447636 | 3364 | extern int _map_enter_debug; |
0a7de745 | 3365 | extern uint64_t get_current_unique_pid(void); |
91447636 | 3366 | |
2d21ac55 A |
3367 | unsigned long vm_fault_collapse_total = 0; |
3368 | unsigned long vm_fault_collapse_skipped = 0; | |
3369 | ||
39236c6e | 3370 | |
1c79356b | 3371 | kern_return_t |
5ba3f43e | 3372 | vm_fault_external( |
0a7de745 A |
3373 | vm_map_t map, |
3374 | vm_map_offset_t vaddr, | |
3375 | vm_prot_t fault_type, | |
3376 | boolean_t change_wiring, | |
3377 | int interruptible, | |
3378 | pmap_t caller_pmap, | |
3379 | vm_map_offset_t caller_pmap_addr) | |
fe8ab488 | 3380 | { |
5ba3f43e | 3381 | return vm_fault_internal(map, vaddr, fault_type, change_wiring, vm_tag_bt(), |
0a7de745 A |
3382 | interruptible, caller_pmap, caller_pmap_addr, |
3383 | NULL); | |
fe8ab488 A |
3384 | } |
3385 | ||
5ba3f43e A |
3386 | kern_return_t |
3387 | vm_fault( | |
0a7de745 A |
3388 | vm_map_t map, |
3389 | vm_map_offset_t vaddr, | |
3390 | vm_prot_t fault_type, | |
3391 | boolean_t change_wiring, | |
3392 | vm_tag_t wire_tag, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */ | |
3393 | int interruptible, | |
3394 | pmap_t caller_pmap, | |
3395 | vm_map_offset_t caller_pmap_addr) | |
5ba3f43e A |
3396 | { |
3397 | return vm_fault_internal(map, vaddr, fault_type, change_wiring, wire_tag, | |
0a7de745 A |
3398 | interruptible, caller_pmap, caller_pmap_addr, |
3399 | NULL); | |
5ba3f43e | 3400 | } |
3e170ce0 | 3401 | |
cb323159 A |
3402 | static boolean_t |
3403 | current_proc_is_privileged(void) | |
3404 | { | |
3405 | return csproc_get_platform_binary(current_proc()); | |
3406 | } | |
3407 | ||
3408 | uint64_t vm_copied_on_read = 0; | |
3409 | ||
fe8ab488 A |
3410 | kern_return_t |
3411 | vm_fault_internal( | |
0a7de745 A |
3412 | vm_map_t map, |
3413 | vm_map_offset_t vaddr, | |
3414 | vm_prot_t caller_prot, | |
3415 | boolean_t change_wiring, | |
3416 | vm_tag_t wire_tag, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */ | |
3417 | int interruptible, | |
3418 | pmap_t caller_pmap, | |
3419 | vm_map_offset_t caller_pmap_addr, | |
3420 | ppnum_t *physpage_p) | |
1c79356b | 3421 | { |
0a7de745 A |
3422 | vm_map_version_t version; /* Map version for verificiation */ |
3423 | boolean_t wired; /* Should mapping be wired down? */ | |
3424 | vm_object_t object; /* Top-level object */ | |
3425 | vm_object_offset_t offset; /* Top-level offset */ | |
3426 | vm_prot_t prot; /* Protection for mapping */ | |
3427 | vm_object_t old_copy_object; /* Saved copy object */ | |
3428 | vm_page_t result_page; /* Result of vm_fault_page */ | |
3429 | vm_page_t top_page; /* Placeholder page */ | |
3430 | kern_return_t kr; | |
3431 | ||
3432 | vm_page_t m; /* Fast access to result_page */ | |
3433 | kern_return_t error_code; | |
3434 | vm_object_t cur_object; | |
3435 | vm_object_t m_object = NULL; | |
3436 | vm_object_offset_t cur_offset; | |
3437 | vm_page_t cur_m; | |
3438 | vm_object_t new_object; | |
1c79356b | 3439 | int type_of_fault; |
0a7de745 A |
3440 | pmap_t pmap; |
3441 | wait_interrupt_t interruptible_state; | |
3442 | vm_map_t real_map = map; | |
3443 | vm_map_t original_map = map; | |
3444 | boolean_t object_locks_dropped = FALSE; | |
3445 | vm_prot_t fault_type; | |
3446 | vm_prot_t original_fault_type; | |
d9a64523 | 3447 | struct vm_object_fault_info fault_info = {}; |
0a7de745 A |
3448 | boolean_t need_collapse = FALSE; |
3449 | boolean_t need_retry = FALSE; | |
3450 | boolean_t *need_retry_ptr = NULL; | |
3451 | int object_lock_type = 0; | |
3452 | int cur_object_lock_type; | |
3453 | vm_object_t top_object = VM_OBJECT_NULL; | |
3454 | vm_object_t written_on_object = VM_OBJECT_NULL; | |
3455 | memory_object_t written_on_pager = NULL; | |
3456 | vm_object_offset_t written_on_offset = 0; | |
3457 | int throttle_delay; | |
3458 | int compressed_count_delta; | |
3459 | int grab_options; | |
cb323159 A |
3460 | boolean_t need_copy; |
3461 | boolean_t need_copy_on_read; | |
0a7de745 A |
3462 | vm_map_offset_t trace_vaddr; |
3463 | vm_map_offset_t trace_real_vaddr; | |
0a7de745 | 3464 | vm_map_offset_t real_vaddr; |
cb323159 A |
3465 | boolean_t resilient_media_retry = FALSE; |
3466 | vm_object_t resilient_media_object = VM_OBJECT_NULL; | |
3467 | vm_object_offset_t resilient_media_offset = (vm_object_offset_t)-1; | |
1c79356b | 3468 | |
39037602 | 3469 | real_vaddr = vaddr; |
d190cdc3 | 3470 | trace_real_vaddr = vaddr; |
39037602 | 3471 | vaddr = vm_map_trunc_page(vaddr, PAGE_MASK); |
de355530 | 3472 | |
d190cdc3 | 3473 | if (map == kernel_map) { |
5ba3f43e A |
3474 | trace_vaddr = VM_KERNEL_ADDRHIDE(vaddr); |
3475 | trace_real_vaddr = VM_KERNEL_ADDRHIDE(trace_real_vaddr); | |
d190cdc3 A |
3476 | } else { |
3477 | trace_vaddr = vaddr; | |
3478 | } | |
3479 | ||
5ba3f43e | 3480 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
0a7de745 A |
3481 | (MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_START, |
3482 | ((uint64_t)trace_vaddr >> 32), | |
3483 | trace_vaddr, | |
3484 | (map == kernel_map), | |
3485 | 0, | |
3486 | 0); | |
1c79356b | 3487 | |
0c530ab8 | 3488 | if (get_preemption_level() != 0) { |
0a7de745 A |
3489 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
3490 | (MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END, | |
3491 | ((uint64_t)trace_vaddr >> 32), | |
3492 | trace_vaddr, | |
3493 | KERN_FAILURE, | |
3494 | 0, | |
3495 | 0); | |
3496 | ||
3497 | return KERN_FAILURE; | |
9bccf70c | 3498 | } |
5ba3f43e | 3499 | |
d9a64523 A |
3500 | thread_t cthread = current_thread(); |
3501 | boolean_t rtfault = (cthread->sched_mode == TH_MODE_REALTIME); | |
3502 | uint64_t fstart = 0; | |
3503 | ||
3504 | if (rtfault) { | |
3505 | fstart = mach_continuous_time(); | |
3506 | } | |
3507 | ||
9bccf70c | 3508 | interruptible_state = thread_interrupt_level(interruptible); |
1c79356b | 3509 | |
3e170ce0 A |
3510 | fault_type = (change_wiring ? VM_PROT_NONE : caller_prot); |
3511 | ||
2d21ac55 A |
3512 | VM_STAT_INCR(faults); |
3513 | current_task()->faults++; | |
3514 | original_fault_type = fault_type; | |
3515 | ||
cb323159 | 3516 | need_copy = FALSE; |
0a7de745 | 3517 | if (fault_type & VM_PROT_WRITE) { |
cb323159 A |
3518 | need_copy = TRUE; |
3519 | } | |
3520 | ||
3521 | if (need_copy) { | |
0a7de745 A |
3522 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
3523 | } else { | |
3524 | object_lock_type = OBJECT_LOCK_SHARED; | |
3525 | } | |
2d21ac55 A |
3526 | |
3527 | cur_object_lock_type = OBJECT_LOCK_SHARED; | |
3528 | ||
5ba3f43e A |
3529 | if ((map == kernel_map) && (caller_prot & VM_PROT_WRITE)) { |
3530 | if (compressor_map) { | |
3531 | if ((vaddr >= vm_map_min(compressor_map)) && (vaddr < vm_map_max(compressor_map))) { | |
3532 | panic("Write fault on compressor map, va: %p type: %u bounds: %p->%p", (void *) vaddr, caller_prot, (void *) vm_map_min(compressor_map), (void *) vm_map_max(compressor_map)); | |
5ba3f43e A |
3533 | } |
3534 | } | |
3535 | } | |
2d21ac55 | 3536 | RetryFault: |
d9a64523 A |
3537 | assert(written_on_object == VM_OBJECT_NULL); |
3538 | ||
1c79356b A |
3539 | /* |
3540 | * assume we will hit a page in the cache | |
3541 | * otherwise, explicitly override with | |
3542 | * the real fault type once we determine it | |
3543 | */ | |
3544 | type_of_fault = DBG_CACHE_HIT_FAULT; | |
3545 | ||
1c79356b A |
3546 | /* |
3547 | * Find the backing store object and offset into | |
3548 | * it to begin the search. | |
3549 | */ | |
0c530ab8 | 3550 | fault_type = original_fault_type; |
1c79356b A |
3551 | map = original_map; |
3552 | vm_map_lock_read(map); | |
1c79356b | 3553 | |
cb323159 A |
3554 | if (resilient_media_retry) { |
3555 | /* | |
3556 | * If we have to insert a fake zero-filled page to hide | |
3557 | * a media failure to provide the real page, we need to | |
3558 | * resolve any pending copy-on-write on this mapping. | |
3559 | * VM_PROT_COPY tells vm_map_lookup_locked() to deal | |
3560 | * with that even if this is not a "write" fault. | |
3561 | */ | |
3562 | need_copy = TRUE; | |
3563 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; | |
3564 | } | |
3565 | ||
3566 | kr = vm_map_lookup_locked(&map, vaddr, | |
3567 | (fault_type | (need_copy ? VM_PROT_COPY : 0)), | |
0a7de745 A |
3568 | object_lock_type, &version, |
3569 | &object, &offset, &prot, &wired, | |
3570 | &fault_info, | |
3571 | &real_map); | |
1c79356b A |
3572 | |
3573 | if (kr != KERN_SUCCESS) { | |
3574 | vm_map_unlock_read(map); | |
3575 | goto done; | |
3576 | } | |
2d21ac55 A |
3577 | pmap = real_map->pmap; |
3578 | fault_info.interruptible = interruptible; | |
b0d623f7 | 3579 | fault_info.stealth = FALSE; |
6d2010ae | 3580 | fault_info.io_sync = FALSE; |
0b4c1975 | 3581 | fault_info.mark_zf_absent = FALSE; |
316670eb | 3582 | fault_info.batch_pmap_op = FALSE; |
1c79356b | 3583 | |
cb323159 A |
3584 | if (resilient_media_retry) { |
3585 | /* | |
3586 | * We're retrying this fault after having detected a media | |
3587 | * failure from a "resilient_media" mapping. | |
3588 | * Check that the mapping is still pointing at the object | |
3589 | * that just failed to provide a page. | |
3590 | */ | |
3591 | assert(resilient_media_object != VM_OBJECT_NULL); | |
3592 | assert(resilient_media_offset != (vm_object_offset_t)-1); | |
3593 | if (object != VM_OBJECT_NULL && | |
3594 | object == resilient_media_object && | |
3595 | offset == resilient_media_offset && | |
3596 | fault_info.resilient_media) { | |
3597 | /* | |
3598 | * This mapping still points at the same object | |
3599 | * and is still "resilient_media": proceed in | |
3600 | * "recovery-from-media-failure" mode, where we'll | |
3601 | * insert a zero-filled page in the top object. | |
3602 | */ | |
3603 | // printf("RESILIENT_MEDIA %s:%d recovering for object %p offset 0x%llx\n", __FUNCTION__, __LINE__, object, offset); | |
3604 | } else { | |
3605 | /* not recovering: reset state */ | |
3606 | // printf("RESILIENT_MEDIA %s:%d no recovery resilient %d object %p/%p offset 0x%llx/0x%llx\n", __FUNCTION__, __LINE__, fault_info.resilient_media, object, resilient_media_object, offset, resilient_media_offset); | |
3607 | resilient_media_retry = FALSE; | |
3608 | /* release our extra reference on failed object */ | |
3609 | // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object); | |
3610 | vm_object_deallocate(resilient_media_object); | |
3611 | resilient_media_object = VM_OBJECT_NULL; | |
3612 | resilient_media_offset = (vm_object_offset_t)-1; | |
3613 | } | |
3614 | } else { | |
3615 | assert(resilient_media_object == VM_OBJECT_NULL); | |
3616 | resilient_media_offset = (vm_object_offset_t)-1; | |
3617 | } | |
3618 | ||
1c79356b | 3619 | /* |
2d21ac55 A |
3620 | * If the page is wired, we must fault for the current protection |
3621 | * value, to avoid further faults. | |
1c79356b | 3622 | */ |
2d21ac55 | 3623 | if (wired) { |
1c79356b | 3624 | fault_type = prot | VM_PROT_WRITE; |
cb323159 A |
3625 | } |
3626 | if (wired || need_copy) { | |
2d21ac55 A |
3627 | /* |
3628 | * since we're treating this fault as a 'write' | |
3629 | * we must hold the top object lock exclusively | |
3630 | */ | |
3631 | if (object_lock_type == OBJECT_LOCK_SHARED) { | |
0a7de745 | 3632 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
2d21ac55 A |
3633 | |
3634 | if (vm_object_lock_upgrade(object) == FALSE) { | |
0a7de745 | 3635 | /* |
2d21ac55 A |
3636 | * couldn't upgrade, so explictly |
3637 | * take the lock exclusively | |
3638 | */ | |
0a7de745 | 3639 | vm_object_lock(object); |
2d21ac55 A |
3640 | } |
3641 | } | |
3642 | } | |
1c79356b | 3643 | |
0a7de745 | 3644 | #if VM_FAULT_CLASSIFY |
1c79356b A |
3645 | /* |
3646 | * Temporary data gathering code | |
3647 | */ | |
3648 | vm_fault_classify(object, offset, fault_type); | |
3649 | #endif | |
3650 | /* | |
3651 | * Fast fault code. The basic idea is to do as much as | |
3652 | * possible while holding the map lock and object locks. | |
3653 | * Busy pages are not used until the object lock has to | |
3654 | * be dropped to do something (copy, zero fill, pmap enter). | |
3655 | * Similarly, paging references aren't acquired until that | |
3656 | * point, and object references aren't used. | |
3657 | * | |
3658 | * If we can figure out what to do | |
3659 | * (zero fill, copy on write, pmap enter) while holding | |
3660 | * the locks, then it gets done. Otherwise, we give up, | |
3661 | * and use the original fault path (which doesn't hold | |
3662 | * the map lock, and relies on busy pages). | |
3663 | * The give up cases include: | |
0a7de745 | 3664 | * - Have to talk to pager. |
1c79356b A |
3665 | * - Page is busy, absent or in error. |
3666 | * - Pager has locked out desired access. | |
3667 | * - Fault needs to be restarted. | |
3668 | * - Have to push page into copy object. | |
3669 | * | |
3670 | * The code is an infinite loop that moves one level down | |
3671 | * the shadow chain each time. cur_object and cur_offset | |
0a7de745 | 3672 | * refer to the current object being examined. object and offset |
1c79356b A |
3673 | * are the original object from the map. The loop is at the |
3674 | * top level if and only if object and cur_object are the same. | |
3675 | * | |
3676 | * Invariants: Map lock is held throughout. Lock is held on | |
3677 | * original object and cur_object (if different) when | |
3678 | * continuing or exiting loop. | |
3679 | * | |
3680 | */ | |
3681 | ||
5ba3f43e A |
3682 | #if defined(__arm64__) |
3683 | /* | |
3684 | * Fail if reading an execute-only page in a | |
3685 | * pmap that enforces execute-only protection. | |
3686 | */ | |
3687 | if (fault_type == VM_PROT_READ && | |
0a7de745 A |
3688 | (prot & VM_PROT_EXECUTE) && |
3689 | !(prot & VM_PROT_READ) && | |
3690 | pmap_enforces_execute_only(pmap)) { | |
3691 | vm_object_unlock(object); | |
3692 | vm_map_unlock_read(map); | |
3693 | if (real_map != map) { | |
3694 | vm_map_unlock(real_map); | |
3695 | } | |
3696 | kr = KERN_PROTECTION_FAILURE; | |
3697 | goto done; | |
5ba3f43e A |
3698 | } |
3699 | #endif | |
1c79356b A |
3700 | |
3701 | /* | |
2d21ac55 A |
3702 | * If this page is to be inserted in a copy delay object |
3703 | * for writing, and if the object has a copy, then the | |
3704 | * copy delay strategy is implemented in the slow fault page. | |
1c79356b | 3705 | */ |
2d21ac55 | 3706 | if (object->copy_strategy == MEMORY_OBJECT_COPY_DELAY && |
0a7de745 A |
3707 | object->copy != VM_OBJECT_NULL && (fault_type & VM_PROT_WRITE)) { |
3708 | goto handle_copy_delay; | |
3709 | } | |
2d21ac55 | 3710 | |
1c79356b A |
3711 | cur_object = object; |
3712 | cur_offset = offset; | |
3713 | ||
39037602 A |
3714 | grab_options = 0; |
3715 | #if CONFIG_SECLUDED_MEMORY | |
3716 | if (object->can_grab_secluded) { | |
3717 | grab_options |= VM_PAGE_GRAB_SECLUDED; | |
3718 | } | |
3719 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
3720 | ||
1c79356b | 3721 | while (TRUE) { |
b0d623f7 | 3722 | if (!cur_object->pager_created && |
0a7de745 | 3723 | cur_object->phys_contiguous) { /* superpage */ |
b0d623f7 | 3724 | break; |
0a7de745 | 3725 | } |
b0d623f7 A |
3726 | |
3727 | if (cur_object->blocked_access) { | |
3728 | /* | |
3729 | * Access to this VM object has been blocked. | |
3730 | * Let the slow path handle it. | |
3731 | */ | |
3732 | break; | |
3733 | } | |
3734 | ||
1c79356b | 3735 | m = vm_page_lookup(cur_object, cur_offset); |
39037602 | 3736 | m_object = NULL; |
2d21ac55 | 3737 | |
1c79356b | 3738 | if (m != VM_PAGE_NULL) { |
39037602 A |
3739 | m_object = cur_object; |
3740 | ||
d9a64523 | 3741 | if (m->vmp_busy) { |
0a7de745 | 3742 | wait_result_t result; |
143cc14e | 3743 | |
2d21ac55 A |
3744 | /* |
3745 | * in order to do the PAGE_ASSERT_WAIT, we must | |
3746 | * have object that 'm' belongs to locked exclusively | |
3747 | */ | |
3748 | if (object != cur_object) { | |
2d21ac55 | 3749 | if (cur_object_lock_type == OBJECT_LOCK_SHARED) { |
0a7de745 | 3750 | cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
2d21ac55 A |
3751 | |
3752 | if (vm_object_lock_upgrade(cur_object) == FALSE) { | |
0a7de745 | 3753 | /* |
2d21ac55 | 3754 | * couldn't upgrade so go do a full retry |
39236c6e A |
3755 | * immediately since we can no longer be |
3756 | * certain about cur_object (since we | |
3757 | * don't hold a reference on it)... | |
3758 | * first drop the top object lock | |
2d21ac55 | 3759 | */ |
39236c6e A |
3760 | vm_object_unlock(object); |
3761 | ||
0a7de745 A |
3762 | vm_map_unlock_read(map); |
3763 | if (real_map != map) { | |
3764 | vm_map_unlock(real_map); | |
3765 | } | |
2d21ac55 A |
3766 | |
3767 | goto RetryFault; | |
3768 | } | |
3769 | } | |
3770 | } else if (object_lock_type == OBJECT_LOCK_SHARED) { | |
0a7de745 | 3771 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
2d21ac55 A |
3772 | |
3773 | if (vm_object_lock_upgrade(object) == FALSE) { | |
0a7de745 | 3774 | /* |
2d21ac55 A |
3775 | * couldn't upgrade, so explictly take the lock |
3776 | * exclusively and go relookup the page since we | |
3777 | * will have dropped the object lock and | |
3778 | * a different thread could have inserted | |
3779 | * a page at this offset | |
3780 | * no need for a full retry since we're | |
3781 | * at the top level of the object chain | |
3782 | */ | |
0a7de745 | 3783 | vm_object_lock(object); |
2d21ac55 A |
3784 | |
3785 | continue; | |
3786 | } | |
3787 | } | |
d9a64523 | 3788 | if ((m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) && m_object->internal) { |
39236c6e | 3789 | /* |
d9a64523 | 3790 | * m->vmp_busy == TRUE and the object is locked exclusively |
39236c6e A |
3791 | * if m->pageout_queue == TRUE after we acquire the |
3792 | * queues lock, we are guaranteed that it is stable on | |
3793 | * the pageout queue and therefore reclaimable | |
3794 | * | |
3795 | * NOTE: this is only true for the internal pageout queue | |
3796 | * in the compressor world | |
3797 | */ | |
39037602 A |
3798 | assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); |
3799 | ||
39236c6e A |
3800 | vm_page_lock_queues(); |
3801 | ||
d9a64523 | 3802 | if (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) { |
39236c6e A |
3803 | vm_pageout_throttle_up(m); |
3804 | vm_page_unlock_queues(); | |
3805 | ||
3806 | PAGE_WAKEUP_DONE(m); | |
3807 | goto reclaimed_from_pageout; | |
3808 | } | |
3809 | vm_page_unlock_queues(); | |
3810 | } | |
0a7de745 | 3811 | if (object != cur_object) { |
39236c6e | 3812 | vm_object_unlock(object); |
0a7de745 | 3813 | } |
39236c6e | 3814 | |
143cc14e | 3815 | vm_map_unlock_read(map); |
0a7de745 A |
3816 | if (real_map != map) { |
3817 | vm_map_unlock(real_map); | |
3818 | } | |
143cc14e | 3819 | |
143cc14e | 3820 | result = PAGE_ASSERT_WAIT(m, interruptible); |
1c79356b | 3821 | |
143cc14e A |
3822 | vm_object_unlock(cur_object); |
3823 | ||
3824 | if (result == THREAD_WAITING) { | |
0a7de745 | 3825 | result = thread_block(THREAD_CONTINUE_NULL); |
143cc14e A |
3826 | |
3827 | counter(c_vm_fault_page_block_busy_kernel++); | |
3828 | } | |
0a7de745 A |
3829 | if (result == THREAD_AWAKENED || result == THREAD_RESTART) { |
3830 | goto RetryFault; | |
3831 | } | |
143cc14e A |
3832 | |
3833 | kr = KERN_ABORTED; | |
3834 | goto done; | |
3835 | } | |
39236c6e | 3836 | reclaimed_from_pageout: |
d9a64523 | 3837 | if (m->vmp_laundry) { |
316670eb A |
3838 | if (object != cur_object) { |
3839 | if (cur_object_lock_type == OBJECT_LOCK_SHARED) { | |
3840 | cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; | |
3841 | ||
3842 | vm_object_unlock(object); | |
3843 | vm_object_unlock(cur_object); | |
3844 | ||
3845 | vm_map_unlock_read(map); | |
0a7de745 | 3846 | if (real_map != map) { |
316670eb | 3847 | vm_map_unlock(real_map); |
0a7de745 | 3848 | } |
316670eb A |
3849 | |
3850 | goto RetryFault; | |
3851 | } | |
316670eb | 3852 | } else if (object_lock_type == OBJECT_LOCK_SHARED) { |
316670eb A |
3853 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
3854 | ||
3855 | if (vm_object_lock_upgrade(object) == FALSE) { | |
3856 | /* | |
3857 | * couldn't upgrade, so explictly take the lock | |
3858 | * exclusively and go relookup the page since we | |
3859 | * will have dropped the object lock and | |
3860 | * a different thread could have inserted | |
3861 | * a page at this offset | |
3862 | * no need for a full retry since we're | |
3863 | * at the top level of the object chain | |
3864 | */ | |
3865 | vm_object_lock(object); | |
3866 | ||
3867 | continue; | |
3868 | } | |
3869 | } | |
316670eb A |
3870 | vm_pageout_steal_laundry(m, FALSE); |
3871 | } | |
3872 | ||
39037602 | 3873 | if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) { |
2d21ac55 A |
3874 | /* |
3875 | * Guard page: let the slow path deal with it | |
3876 | */ | |
3877 | break; | |
3878 | } | |
d9a64523 | 3879 | if (m->vmp_unusual && (m->vmp_error || m->vmp_restart || m->vmp_private || m->vmp_absent)) { |
0a7de745 | 3880 | /* |
2d21ac55 | 3881 | * Unusual case... let the slow path deal with it |
1c79356b A |
3882 | */ |
3883 | break; | |
3884 | } | |
39037602 | 3885 | if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object)) { |
0a7de745 | 3886 | if (object != cur_object) { |
b0d623f7 | 3887 | vm_object_unlock(object); |
0a7de745 | 3888 | } |
b0d623f7 | 3889 | vm_map_unlock_read(map); |
0a7de745 A |
3890 | if (real_map != map) { |
3891 | vm_map_unlock(real_map); | |
3892 | } | |
b0d623f7 A |
3893 | vm_object_unlock(cur_object); |
3894 | kr = KERN_MEMORY_ERROR; | |
3895 | goto done; | |
3896 | } | |
39037602 | 3897 | assert(m_object == VM_PAGE_OBJECT(m)); |
6d2010ae | 3898 | |
39037602 | 3899 | if (VM_FAULT_NEED_CS_VALIDATION(map->pmap, m, m_object) || |
fe8ab488 | 3900 | (physpage_p != NULL && (prot & VM_PROT_WRITE))) { |
cb323159 | 3901 | upgrade_lock_and_retry: |
2d21ac55 | 3902 | /* |
4a3eedf9 | 3903 | * We might need to validate this page |
2d21ac55 A |
3904 | * against its code signature, so we |
3905 | * want to hold the VM object exclusively. | |
3906 | */ | |
0a7de745 | 3907 | if (object != cur_object) { |
2d21ac55 A |
3908 | if (cur_object_lock_type == OBJECT_LOCK_SHARED) { |
3909 | vm_object_unlock(object); | |
3910 | vm_object_unlock(cur_object); | |
3911 | ||
0a7de745 | 3912 | cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
2d21ac55 A |
3913 | |
3914 | vm_map_unlock_read(map); | |
0a7de745 | 3915 | if (real_map != map) { |
2d21ac55 | 3916 | vm_map_unlock(real_map); |
0a7de745 | 3917 | } |
2d21ac55 A |
3918 | |
3919 | goto RetryFault; | |
3920 | } | |
2d21ac55 | 3921 | } else if (object_lock_type == OBJECT_LOCK_SHARED) { |
0a7de745 | 3922 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
2d21ac55 A |
3923 | |
3924 | if (vm_object_lock_upgrade(object) == FALSE) { | |
0a7de745 | 3925 | /* |
2d21ac55 A |
3926 | * couldn't upgrade, so explictly take the lock |
3927 | * exclusively and go relookup the page since we | |
3928 | * will have dropped the object lock and | |
3929 | * a different thread could have inserted | |
3930 | * a page at this offset | |
3931 | * no need for a full retry since we're | |
3932 | * at the top level of the object chain | |
3933 | */ | |
0a7de745 | 3934 | vm_object_lock(object); |
2d21ac55 A |
3935 | |
3936 | continue; | |
3937 | } | |
3938 | } | |
3939 | } | |
1c79356b A |
3940 | /* |
3941 | * Two cases of map in faults: | |
3942 | * - At top level w/o copy object. | |
3943 | * - Read fault anywhere. | |
3944 | * --> must disallow write. | |
3945 | */ | |
3946 | ||
4a3eedf9 | 3947 | if (object == cur_object && object->copy == VM_OBJECT_NULL) { |
2d21ac55 | 3948 | goto FastPmapEnter; |
4a3eedf9 | 3949 | } |
1c79356b | 3950 | |
cb323159 A |
3951 | if (!need_copy && |
3952 | !fault_info.no_copy_on_read && | |
3953 | cur_object != object && | |
3954 | !cur_object->internal && | |
3955 | !cur_object->pager_trusted && | |
3956 | vm_protect_privileged_from_untrusted && | |
3957 | !((prot & VM_PROT_EXECUTE) && | |
3958 | cur_object->code_signed && | |
3959 | cs_process_enforcement(NULL)) && | |
3960 | current_proc_is_privileged()) { | |
3961 | /* | |
3962 | * We're faulting on a page in "object" and | |
3963 | * went down the shadow chain to "cur_object" | |
3964 | * to find out that "cur_object"'s pager | |
3965 | * is not "trusted", i.e. we can not trust it | |
3966 | * to always return the same contents. | |
3967 | * Since the target is a "privileged" process, | |
3968 | * let's treat this as a copy-on-read fault, as | |
3969 | * if it was a copy-on-write fault. | |
3970 | * Once "object" gets a copy of this page, it | |
3971 | * won't have to rely on "cur_object" to | |
3972 | * provide the contents again. | |
3973 | * | |
3974 | * This is done by setting "need_copy" and | |
3975 | * retrying the fault from the top with the | |
3976 | * appropriate locking. | |
3977 | * | |
3978 | * Special case: if the mapping is executable | |
3979 | * and the untrusted object is code-signed and | |
3980 | * the process is "cs_enforced", we do not | |
3981 | * copy-on-read because that would break | |
3982 | * code-signing enforcement expectations (an | |
3983 | * executable page must belong to a code-signed | |
3984 | * object) and we can rely on code-signing | |
3985 | * to re-validate the page if it gets evicted | |
3986 | * and paged back in. | |
3987 | */ | |
3988 | // printf("COPY-ON-READ %s:%d map %p va 0x%llx page %p object %p offset 0x%llx UNTRUSTED: need copy-on-read!\n", __FUNCTION__, __LINE__, map, (uint64_t)vaddr, m, VM_PAGE_OBJECT(m), m->vmp_offset); | |
3989 | vm_copied_on_read++; | |
3990 | need_copy = TRUE; | |
3991 | ||
3992 | vm_object_unlock(object); | |
3993 | vm_object_unlock(cur_object); | |
3994 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; | |
3995 | vm_map_unlock_read(map); | |
3996 | if (real_map != map) { | |
3997 | vm_map_unlock(real_map); | |
3998 | } | |
3999 | goto RetryFault; | |
4000 | } | |
4001 | ||
4002 | if (!(fault_type & VM_PROT_WRITE) && !need_copy) { | |
5ba3f43e A |
4003 | if (!pmap_has_prot_policy(prot)) { |
4004 | prot &= ~VM_PROT_WRITE; | |
4005 | } else { | |
4006 | /* | |
4007 | * For a protection that the pmap cares | |
4008 | * about, we must hand over the full | |
4009 | * set of protections (so that the pmap | |
4010 | * layer can apply any desired policy). | |
4011 | * This means that cs_bypass must be | |
4012 | * set, as this can force us to pass | |
4013 | * RWX. | |
4014 | */ | |
4015 | assert(fault_info.cs_bypass); | |
4016 | } | |
39037602 | 4017 | |
0a7de745 A |
4018 | if (object != cur_object) { |
4019 | /* | |
c910b4d9 A |
4020 | * We still need to hold the top object |
4021 | * lock here to prevent a race between | |
4022 | * a read fault (taking only "shared" | |
4023 | * locks) and a write fault (taking | |
4024 | * an "exclusive" lock on the top | |
4025 | * object. | |
4026 | * Otherwise, as soon as we release the | |
4027 | * top lock, the write fault could | |
4028 | * proceed and actually complete before | |
4029 | * the read fault, and the copied page's | |
4030 | * translation could then be overwritten | |
4031 | * by the read fault's translation for | |
4032 | * the original page. | |
4033 | * | |
4034 | * Let's just record what the top object | |
4035 | * is and we'll release it later. | |
2d21ac55 | 4036 | */ |
c910b4d9 | 4037 | top_object = object; |
2d21ac55 A |
4038 | |
4039 | /* | |
4040 | * switch to the object that has the new page | |
4041 | */ | |
1c79356b | 4042 | object = cur_object; |
2d21ac55 | 4043 | object_lock_type = cur_object_lock_type; |
1c79356b | 4044 | } |
1c79356b | 4045 | FastPmapEnter: |
39037602 A |
4046 | assert(m_object == VM_PAGE_OBJECT(m)); |
4047 | ||
1c79356b | 4048 | /* |
2d21ac55 A |
4049 | * prepare for the pmap_enter... |
4050 | * object and map are both locked | |
4051 | * m contains valid data | |
d9a64523 | 4052 | * object == m->vmp_object |
2d21ac55 A |
4053 | * cur_object == NULL or it's been unlocked |
4054 | * no paging references on either object or cur_object | |
1c79356b | 4055 | */ |
0a7de745 | 4056 | if (top_object != VM_OBJECT_NULL || object_lock_type != OBJECT_LOCK_EXCLUSIVE) { |
39236c6e | 4057 | need_retry_ptr = &need_retry; |
0a7de745 | 4058 | } else { |
39236c6e | 4059 | need_retry_ptr = NULL; |
0a7de745 | 4060 | } |
39236c6e | 4061 | |
2d21ac55 | 4062 | if (caller_pmap) { |
0a7de745 A |
4063 | kr = vm_fault_enter(m, |
4064 | caller_pmap, | |
4065 | caller_pmap_addr, | |
4066 | prot, | |
4067 | caller_prot, | |
4068 | wired, | |
4069 | change_wiring, | |
4070 | wire_tag, | |
4071 | &fault_info, | |
4072 | need_retry_ptr, | |
4073 | &type_of_fault); | |
9bccf70c | 4074 | } else { |
0a7de745 A |
4075 | kr = vm_fault_enter(m, |
4076 | pmap, | |
4077 | vaddr, | |
4078 | prot, | |
4079 | caller_prot, | |
4080 | wired, | |
4081 | change_wiring, | |
4082 | wire_tag, | |
4083 | &fault_info, | |
4084 | need_retry_ptr, | |
4085 | &type_of_fault); | |
9bccf70c | 4086 | } |
39037602 | 4087 | { |
0a7de745 | 4088 | int event_code = 0; |
39037602 | 4089 | |
0a7de745 A |
4090 | if (m_object->internal) { |
4091 | event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_INTERNAL)); | |
4092 | } else if (m_object->object_is_shared_cache) { | |
4093 | event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_SHAREDCACHE)); | |
4094 | } else { | |
4095 | event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_EXTERNAL)); | |
4096 | } | |
0b4e3aa0 | 4097 | |
0a7de745 | 4098 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, event_code, trace_real_vaddr, (fault_info.user_tag << 16) | (caller_prot << 8) | type_of_fault, m->vmp_offset, get_current_unique_pid(), 0); |
94ff46dc A |
4099 | if (need_retry == FALSE) { |
4100 | KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_FAST), get_current_unique_pid(), 0, 0, 0, 0); | |
4101 | } | |
0a7de745 | 4102 | DTRACE_VM6(real_fault, vm_map_offset_t, real_vaddr, vm_map_offset_t, m->vmp_offset, int, event_code, int, caller_prot, int, type_of_fault, int, fault_info.user_tag); |
39037602 | 4103 | } |
fe8ab488 A |
4104 | if (kr == KERN_SUCCESS && |
4105 | physpage_p != NULL) { | |
4106 | /* for vm_map_wire_and_extract() */ | |
39037602 | 4107 | *physpage_p = VM_PAGE_GET_PHYS_PAGE(m); |
fe8ab488 | 4108 | if (prot & VM_PROT_WRITE) { |
39037602 | 4109 | vm_object_lock_assert_exclusive(m_object); |
d9a64523 | 4110 | m->vmp_dirty = TRUE; |
fe8ab488 A |
4111 | } |
4112 | } | |
4113 | ||
c910b4d9 A |
4114 | if (top_object != VM_OBJECT_NULL) { |
4115 | /* | |
4116 | * It's safe to drop the top object | |
4117 | * now that we've done our | |
4118 | * vm_fault_enter(). Any other fault | |
4119 | * in progress for that virtual | |
4120 | * address will either find our page | |
4121 | * and translation or put in a new page | |
4122 | * and translation. | |
4123 | */ | |
4124 | vm_object_unlock(top_object); | |
4125 | top_object = VM_OBJECT_NULL; | |
4126 | } | |
4127 | ||
0a7de745 A |
4128 | if (need_collapse == TRUE) { |
4129 | vm_object_collapse(object, offset, TRUE); | |
4130 | } | |
5ba3f43e | 4131 | |
316670eb A |
4132 | if (need_retry == FALSE && |
4133 | (type_of_fault == DBG_PAGEIND_FAULT || type_of_fault == DBG_PAGEINV_FAULT || type_of_fault == DBG_CACHE_HIT_FAULT)) { | |
0a7de745 | 4134 | /* |
2d21ac55 A |
4135 | * evaluate access pattern and update state |
4136 | * vm_fault_deactivate_behind depends on the | |
4137 | * state being up to date | |
4138 | */ | |
0a7de745 | 4139 | vm_fault_is_sequential(m_object, cur_offset, fault_info.behavior); |
0c530ab8 | 4140 | |
d9a64523 | 4141 | vm_fault_deactivate_behind(m_object, cur_offset, fault_info.behavior); |
1c79356b | 4142 | } |
1c79356b | 4143 | /* |
2d21ac55 | 4144 | * That's it, clean up and return. |
1c79356b | 4145 | */ |
0a7de745 A |
4146 | if (m->vmp_busy) { |
4147 | PAGE_WAKEUP_DONE(m); | |
4148 | } | |
6601e61a | 4149 | |
d9a64523 | 4150 | if (need_retry == FALSE && !m_object->internal && (fault_type & VM_PROT_WRITE)) { |
d9a64523 A |
4151 | vm_object_paging_begin(m_object); |
4152 | ||
4153 | assert(written_on_object == VM_OBJECT_NULL); | |
4154 | written_on_object = m_object; | |
4155 | written_on_pager = m_object->pager; | |
4156 | written_on_offset = m_object->paging_offset + m->vmp_offset; | |
4157 | } | |
1c79356b | 4158 | vm_object_unlock(object); |
143cc14e | 4159 | |
1c79356b | 4160 | vm_map_unlock_read(map); |
0a7de745 | 4161 | if (real_map != map) { |
91447636 | 4162 | vm_map_unlock(real_map); |
0a7de745 | 4163 | } |
1c79356b | 4164 | |
316670eb A |
4165 | if (need_retry == TRUE) { |
4166 | /* | |
4167 | * vm_fault_enter couldn't complete the PMAP_ENTER... | |
4168 | * at this point we don't hold any locks so it's safe | |
4169 | * to ask the pmap layer to expand the page table to | |
4170 | * accommodate this mapping... once expanded, we'll | |
4171 | * re-drive the fault which should result in vm_fault_enter | |
4172 | * being able to successfully enter the mapping this time around | |
4173 | */ | |
fe8ab488 A |
4174 | (void)pmap_enter_options( |
4175 | pmap, vaddr, 0, 0, 0, 0, 0, | |
4176 | PMAP_OPTIONS_NOENTER, NULL); | |
5ba3f43e | 4177 | |
316670eb A |
4178 | need_retry = FALSE; |
4179 | goto RetryFault; | |
4180 | } | |
2d21ac55 | 4181 | goto done; |
1c79356b | 4182 | } |
1c79356b | 4183 | /* |
2d21ac55 | 4184 | * COPY ON WRITE FAULT |
b0d623f7 A |
4185 | */ |
4186 | assert(object_lock_type == OBJECT_LOCK_EXCLUSIVE); | |
4187 | ||
0a7de745 | 4188 | /* |
2d21ac55 A |
4189 | * If objects match, then |
4190 | * object->copy must not be NULL (else control | |
4191 | * would be in previous code block), and we | |
4192 | * have a potential push into the copy object | |
4193 | * with which we can't cope with here. | |
1c79356b | 4194 | */ |
2d21ac55 | 4195 | if (cur_object == object) { |
0a7de745 | 4196 | /* |
2d21ac55 A |
4197 | * must take the slow path to |
4198 | * deal with the copy push | |
4199 | */ | |
1c79356b | 4200 | break; |
2d21ac55 | 4201 | } |
5ba3f43e | 4202 | |
1c79356b | 4203 | /* |
2d21ac55 A |
4204 | * This is now a shadow based copy on write |
4205 | * fault -- it requires a copy up the shadow | |
4206 | * chain. | |
6d2010ae | 4207 | */ |
39037602 | 4208 | assert(m_object == VM_PAGE_OBJECT(m)); |
5ba3f43e | 4209 | |
6d2010ae | 4210 | if ((cur_object_lock_type == OBJECT_LOCK_SHARED) && |
39037602 | 4211 | VM_FAULT_NEED_CS_VALIDATION(NULL, m, m_object)) { |
cb323159 | 4212 | goto upgrade_lock_and_retry; |
6d2010ae A |
4213 | } |
4214 | ||
4215 | /* | |
2d21ac55 A |
4216 | * Allocate a page in the original top level |
4217 | * object. Give up if allocate fails. Also | |
4218 | * need to remember current page, as it's the | |
4219 | * source of the copy. | |
1c79356b | 4220 | * |
5ba3f43e | 4221 | * at this point we hold locks on both |
2d21ac55 A |
4222 | * object and cur_object... no need to take |
4223 | * paging refs or mark pages BUSY since | |
4224 | * we don't drop either object lock until | |
4225 | * the page has been copied and inserted | |
1c79356b A |
4226 | */ |
4227 | cur_m = m; | |
39037602 A |
4228 | m = vm_page_grab_options(grab_options); |
4229 | m_object = NULL; | |
2d21ac55 | 4230 | |
1c79356b | 4231 | if (m == VM_PAGE_NULL) { |
0a7de745 | 4232 | /* |
2d21ac55 A |
4233 | * no free page currently available... |
4234 | * must take the slow path | |
4235 | */ | |
1c79356b A |
4236 | break; |
4237 | } | |
1c79356b | 4238 | /* |
2d21ac55 | 4239 | * Now do the copy. Mark the source page busy... |
1c79356b A |
4240 | * |
4241 | * NOTE: This code holds the map lock across | |
4242 | * the page copy. | |
4243 | */ | |
1c79356b A |
4244 | vm_page_copy(cur_m, m); |
4245 | vm_page_insert(m, object, offset); | |
39037602 | 4246 | m_object = object; |
316670eb | 4247 | SET_PAGE_DIRTY(m, FALSE); |
1c79356b A |
4248 | |
4249 | /* | |
2d21ac55 | 4250 | * Now cope with the source page and object |
1c79356b | 4251 | */ |
0a7de745 A |
4252 | if (object->ref_count > 1 && cur_m->vmp_pmapped) { |
4253 | pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m)); | |
4254 | } | |
5ba3f43e | 4255 | |
d9a64523 | 4256 | if (cur_m->vmp_clustered) { |
fe8ab488 A |
4257 | VM_PAGE_COUNT_AS_PAGEIN(cur_m); |
4258 | VM_PAGE_CONSUME_CLUSTERED(cur_m); | |
39037602 | 4259 | vm_fault_is_sequential(cur_object, cur_offset, fault_info.behavior); |
fe8ab488 | 4260 | } |
2d21ac55 | 4261 | need_collapse = TRUE; |
1c79356b | 4262 | |
2d21ac55 A |
4263 | if (!cur_object->internal && |
4264 | cur_object->copy_strategy == MEMORY_OBJECT_COPY_DELAY) { | |
0a7de745 | 4265 | /* |
2d21ac55 A |
4266 | * The object from which we've just |
4267 | * copied a page is most probably backed | |
4268 | * by a vnode. We don't want to waste too | |
4269 | * much time trying to collapse the VM objects | |
4270 | * and create a bottleneck when several tasks | |
4271 | * map the same file. | |
4272 | */ | |
0a7de745 A |
4273 | if (cur_object->copy == object) { |
4274 | /* | |
2d21ac55 A |
4275 | * Shared mapping or no COW yet. |
4276 | * We can never collapse a copy | |
4277 | * object into its backing object. | |
4278 | */ | |
0a7de745 | 4279 | need_collapse = FALSE; |
2d21ac55 | 4280 | } else if (cur_object->copy == object->shadow && |
0a7de745 A |
4281 | object->shadow->resident_page_count == 0) { |
4282 | /* | |
2d21ac55 A |
4283 | * Shared mapping after a COW occurred. |
4284 | */ | |
0a7de745 | 4285 | need_collapse = FALSE; |
2d21ac55 A |
4286 | } |
4287 | } | |
1c79356b A |
4288 | vm_object_unlock(cur_object); |
4289 | ||
0a7de745 A |
4290 | if (need_collapse == FALSE) { |
4291 | vm_fault_collapse_skipped++; | |
4292 | } | |
2d21ac55 A |
4293 | vm_fault_collapse_total++; |
4294 | ||
4295 | type_of_fault = DBG_COW_FAULT; | |
4296 | VM_STAT_INCR(cow_faults); | |
4297 | DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL); | |
4298 | current_task()->cow_faults++; | |
1c79356b A |
4299 | |
4300 | goto FastPmapEnter; | |
2d21ac55 | 4301 | } else { |
1c79356b | 4302 | /* |
2d21ac55 | 4303 | * No page at cur_object, cur_offset... m == NULL |
1c79356b | 4304 | */ |
1c79356b | 4305 | if (cur_object->pager_created) { |
0a7de745 | 4306 | int compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN; |
39236c6e | 4307 | |
0a7de745 A |
4308 | if (MUST_ASK_PAGER(cur_object, cur_offset, compressor_external_state) == TRUE) { |
4309 | int my_fault_type; | |
4310 | int c_flags = C_DONT_BLOCK; | |
4311 | boolean_t insert_cur_object = FALSE; | |
39236c6e | 4312 | |
0a7de745 | 4313 | /* |
2d21ac55 | 4314 | * May have to talk to a pager... |
39236c6e A |
4315 | * if so, take the slow path by |
4316 | * doing a 'break' from the while (TRUE) loop | |
4317 | * | |
4318 | * external_state will only be set to VM_EXTERNAL_STATE_EXISTS | |
4319 | * if the compressor is active and the page exists there | |
2d21ac55 | 4320 | */ |
0a7de745 | 4321 | if (compressor_external_state != VM_EXTERNAL_STATE_EXISTS) { |
39236c6e | 4322 | break; |
0a7de745 | 4323 | } |
39236c6e A |
4324 | |
4325 | if (map == kernel_map || real_map == kernel_map) { | |
4326 | /* | |
4327 | * can't call into the compressor with the kernel_map | |
4328 | * lock held, since the compressor may try to operate | |
4329 | * on the kernel map in order to return an empty c_segment | |
4330 | */ | |
4331 | break; | |
4332 | } | |
4333 | if (object != cur_object) { | |
0a7de745 | 4334 | if (fault_type & VM_PROT_WRITE) { |
39236c6e | 4335 | c_flags |= C_KEEP; |
0a7de745 | 4336 | } else { |
39236c6e | 4337 | insert_cur_object = TRUE; |
0a7de745 | 4338 | } |
39236c6e A |
4339 | } |
4340 | if (insert_cur_object == TRUE) { | |
39236c6e | 4341 | if (cur_object_lock_type == OBJECT_LOCK_SHARED) { |
39236c6e A |
4342 | cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
4343 | ||
4344 | if (vm_object_lock_upgrade(cur_object) == FALSE) { | |
4345 | /* | |
4346 | * couldn't upgrade so go do a full retry | |
4347 | * immediately since we can no longer be | |
4348 | * certain about cur_object (since we | |
4349 | * don't hold a reference on it)... | |
4350 | * first drop the top object lock | |
4351 | */ | |
4352 | vm_object_unlock(object); | |
4353 | ||
4354 | vm_map_unlock_read(map); | |
0a7de745 | 4355 | if (real_map != map) { |
39236c6e | 4356 | vm_map_unlock(real_map); |
0a7de745 | 4357 | } |
39236c6e A |
4358 | |
4359 | goto RetryFault; | |
4360 | } | |
4361 | } | |
4362 | } else if (object_lock_type == OBJECT_LOCK_SHARED) { | |
39236c6e A |
4363 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
4364 | ||
4365 | if (object != cur_object) { | |
4366 | /* | |
4367 | * we can't go for the upgrade on the top | |
4368 | * lock since the upgrade may block waiting | |
4369 | * for readers to drain... since we hold | |
4370 | * cur_object locked at this point, waiting | |
4371 | * for the readers to drain would represent | |
4372 | * a lock order inversion since the lock order | |
4373 | * for objects is the reference order in the | |
4374 | * shadown chain | |
4375 | */ | |
4376 | vm_object_unlock(object); | |
4377 | vm_object_unlock(cur_object); | |
4378 | ||
4379 | vm_map_unlock_read(map); | |
0a7de745 | 4380 | if (real_map != map) { |
39236c6e | 4381 | vm_map_unlock(real_map); |
0a7de745 | 4382 | } |
39236c6e A |
4383 | |
4384 | goto RetryFault; | |
4385 | } | |
4386 | if (vm_object_lock_upgrade(object) == FALSE) { | |
4387 | /* | |
4388 | * couldn't upgrade, so explictly take the lock | |
4389 | * exclusively and go relookup the page since we | |
4390 | * will have dropped the object lock and | |
4391 | * a different thread could have inserted | |
4392 | * a page at this offset | |
4393 | * no need for a full retry since we're | |
4394 | * at the top level of the object chain | |
4395 | */ | |
4396 | vm_object_lock(object); | |
5ba3f43e | 4397 | |
39236c6e A |
4398 | continue; |
4399 | } | |
4400 | } | |
39037602 A |
4401 | m = vm_page_grab_options(grab_options); |
4402 | m_object = NULL; | |
39236c6e A |
4403 | |
4404 | if (m == VM_PAGE_NULL) { | |
4405 | /* | |
4406 | * no free page currently available... | |
4407 | * must take the slow path | |
4408 | */ | |
4409 | break; | |
4410 | } | |
fe8ab488 A |
4411 | |
4412 | /* | |
4413 | * The object is and remains locked | |
4414 | * so no need to take a | |
4415 | * "paging_in_progress" reference. | |
4416 | */ | |
4417 | boolean_t shared_lock; | |
4418 | if ((object == cur_object && | |
0a7de745 | 4419 | object_lock_type == OBJECT_LOCK_EXCLUSIVE) || |
fe8ab488 | 4420 | (object != cur_object && |
0a7de745 | 4421 | cur_object_lock_type == OBJECT_LOCK_EXCLUSIVE)) { |
fe8ab488 A |
4422 | shared_lock = FALSE; |
4423 | } else { | |
4424 | shared_lock = TRUE; | |
4425 | } | |
4426 | ||
4427 | kr = vm_compressor_pager_get( | |
4428 | cur_object->pager, | |
4429 | (cur_offset + | |
0a7de745 | 4430 | cur_object->paging_offset), |
39037602 | 4431 | VM_PAGE_GET_PHYS_PAGE(m), |
fe8ab488 A |
4432 | &my_fault_type, |
4433 | c_flags, | |
4434 | &compressed_count_delta); | |
4435 | ||
4436 | vm_compressor_pager_count( | |
4437 | cur_object->pager, | |
4438 | compressed_count_delta, | |
4439 | shared_lock, | |
4440 | cur_object); | |
4441 | ||
4442 | if (kr != KERN_SUCCESS) { | |
39037602 A |
4443 | vm_page_release(m, FALSE); |
4444 | m = VM_PAGE_NULL; | |
39236c6e A |
4445 | break; |
4446 | } | |
d9a64523 | 4447 | m->vmp_dirty = TRUE; |
39236c6e | 4448 | |
fe8ab488 A |
4449 | /* |
4450 | * If the object is purgeable, its | |
4451 | * owner's purgeable ledgers will be | |
4452 | * updated in vm_page_insert() but the | |
4453 | * page was also accounted for in a | |
4454 | * "compressed purgeable" ledger, so | |
4455 | * update that now. | |
4456 | */ | |
4457 | if (object != cur_object && | |
4458 | !insert_cur_object) { | |
4459 | /* | |
4460 | * We're not going to insert | |
4461 | * the decompressed page into | |
4462 | * the object it came from. | |
4463 | * | |
4464 | * We're dealing with a | |
4465 | * copy-on-write fault on | |
4466 | * "object". | |
4467 | * We're going to decompress | |
4468 | * the page directly into the | |
4469 | * target "object" while | |
4470 | * keepin the compressed | |
4471 | * page for "cur_object", so | |
4472 | * no ledger update in that | |
4473 | * case. | |
4474 | */ | |
d9a64523 | 4475 | } else if (((cur_object->purgable == |
0a7de745 A |
4476 | VM_PURGABLE_DENY) && |
4477 | (!cur_object->vo_ledger_tag)) || | |
4478 | (cur_object->vo_owner == | |
4479 | NULL)) { | |
fe8ab488 A |
4480 | /* |
4481 | * "cur_object" is not purgeable | |
d9a64523 A |
4482 | * and is not ledger-taged, or |
4483 | * there's no owner for it, | |
4484 | * so no owner's ledgers to | |
4485 | * update. | |
fe8ab488 A |
4486 | */ |
4487 | } else { | |
4488 | /* | |
4489 | * One less compressed | |
d9a64523 | 4490 | * purgeable/tagged page for |
fe8ab488 A |
4491 | * cur_object's owner. |
4492 | */ | |
d9a64523 | 4493 | vm_object_owner_compressed_update( |
fe8ab488 A |
4494 | cur_object, |
4495 | -1); | |
4496 | } | |
4497 | ||
4498 | if (insert_cur_object) { | |
39236c6e | 4499 | vm_page_insert(m, cur_object, cur_offset); |
39037602 | 4500 | m_object = cur_object; |
fe8ab488 | 4501 | } else { |
39236c6e | 4502 | vm_page_insert(m, object, offset); |
39037602 | 4503 | m_object = object; |
fe8ab488 | 4504 | } |
39236c6e | 4505 | |
39037602 | 4506 | if ((m_object->wimg_bits & VM_WIMG_MASK) != VM_WIMG_USE_DEFAULT) { |
0a7de745 | 4507 | /* |
39236c6e A |
4508 | * If the page is not cacheable, |
4509 | * we can't let its contents | |
4510 | * linger in the data cache | |
4511 | * after the decompression. | |
4512 | */ | |
39037602 | 4513 | pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m)); |
39236c6e | 4514 | } |
fe8ab488 | 4515 | |
39236c6e A |
4516 | type_of_fault = my_fault_type; |
4517 | ||
cb323159 | 4518 | VM_STAT_DECOMPRESSIONS(); |
39236c6e A |
4519 | |
4520 | if (cur_object != object) { | |
4521 | if (insert_cur_object) { | |
4522 | top_object = object; | |
4523 | /* | |
4524 | * switch to the object that has the new page | |
4525 | */ | |
4526 | object = cur_object; | |
4527 | object_lock_type = cur_object_lock_type; | |
4528 | } else { | |
4529 | vm_object_unlock(cur_object); | |
4530 | cur_object = object; | |
4531 | } | |
4532 | } | |
4533 | goto FastPmapEnter; | |
2d21ac55 | 4534 | } |
1c79356b | 4535 | /* |
2d21ac55 A |
4536 | * existence map present and indicates |
4537 | * that the pager doesn't have this page | |
1c79356b | 4538 | */ |
1c79356b | 4539 | } |
cb323159 A |
4540 | if (cur_object->shadow == VM_OBJECT_NULL || |
4541 | resilient_media_retry) { | |
2d21ac55 A |
4542 | /* |
4543 | * Zero fill fault. Page gets | |
4544 | * inserted into the original object. | |
4545 | */ | |
b0d623f7 | 4546 | if (cur_object->shadow_severed || |
39037602 A |
4547 | VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object) || |
4548 | cur_object == compressor_object || | |
4549 | cur_object == kernel_object || | |
4550 | cur_object == vm_submap_object) { | |
0a7de745 A |
4551 | if (object != cur_object) { |
4552 | vm_object_unlock(cur_object); | |
4553 | } | |
1c79356b | 4554 | vm_object_unlock(object); |
2d21ac55 | 4555 | |
1c79356b | 4556 | vm_map_unlock_read(map); |
0a7de745 | 4557 | if (real_map != map) { |
91447636 | 4558 | vm_map_unlock(real_map); |
0a7de745 | 4559 | } |
1c79356b | 4560 | |
2d21ac55 A |
4561 | kr = KERN_MEMORY_ERROR; |
4562 | goto done; | |
4563 | } | |
0a7de745 | 4564 | if (cur_object != object) { |
2d21ac55 | 4565 | vm_object_unlock(cur_object); |
1c79356b | 4566 | |
2d21ac55 | 4567 | cur_object = object; |
55e303ae | 4568 | } |
2d21ac55 | 4569 | if (object_lock_type == OBJECT_LOCK_SHARED) { |
0a7de745 | 4570 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
2d21ac55 A |
4571 | |
4572 | if (vm_object_lock_upgrade(object) == FALSE) { | |
0a7de745 | 4573 | /* |
2d21ac55 A |
4574 | * couldn't upgrade so do a full retry on the fault |
4575 | * since we dropped the object lock which | |
4576 | * could allow another thread to insert | |
4577 | * a page at this offset | |
4578 | */ | |
0a7de745 A |
4579 | vm_map_unlock_read(map); |
4580 | if (real_map != map) { | |
4581 | vm_map_unlock(real_map); | |
4582 | } | |
2d21ac55 A |
4583 | |
4584 | goto RetryFault; | |
4585 | } | |
1c79356b | 4586 | } |
cb323159 A |
4587 | if (!object->internal) { |
4588 | panic("%s:%d should not zero-fill page at offset 0x%llx in external object %p", __FUNCTION__, __LINE__, (uint64_t)offset, object); | |
4589 | } | |
1c79356b | 4590 | m = vm_page_alloc(object, offset); |
39037602 | 4591 | m_object = NULL; |
2d21ac55 | 4592 | |
1c79356b | 4593 | if (m == VM_PAGE_NULL) { |
0a7de745 | 4594 | /* |
2d21ac55 A |
4595 | * no free page currently available... |
4596 | * must take the slow path | |
4597 | */ | |
1c79356b A |
4598 | break; |
4599 | } | |
39037602 | 4600 | m_object = object; |
1c79356b | 4601 | |
1c79356b | 4602 | /* |
2d21ac55 | 4603 | * Now zero fill page... |
5ba3f43e | 4604 | * the page is probably going to |
2d21ac55 A |
4605 | * be written soon, so don't bother |
4606 | * to clear the modified bit | |
1c79356b | 4607 | * |
2d21ac55 A |
4608 | * NOTE: This code holds the map |
4609 | * lock across the zero fill. | |
1c79356b | 4610 | */ |
2d21ac55 | 4611 | type_of_fault = vm_fault_zero_page(m, map->no_zero_fill); |
143cc14e | 4612 | |
1c79356b | 4613 | goto FastPmapEnter; |
0a7de745 | 4614 | } |
1c79356b | 4615 | /* |
2d21ac55 | 4616 | * On to the next level in the shadow chain |
1c79356b | 4617 | */ |
6d2010ae | 4618 | cur_offset += cur_object->vo_shadow_offset; |
1c79356b | 4619 | new_object = cur_object->shadow; |
2d21ac55 A |
4620 | |
4621 | /* | |
4622 | * take the new_object's lock with the indicated state | |
4623 | */ | |
0a7de745 A |
4624 | if (cur_object_lock_type == OBJECT_LOCK_SHARED) { |
4625 | vm_object_lock_shared(new_object); | |
4626 | } else { | |
4627 | vm_object_lock(new_object); | |
4628 | } | |
2d21ac55 | 4629 | |
0a7de745 | 4630 | if (cur_object != object) { |
1c79356b | 4631 | vm_object_unlock(cur_object); |
0a7de745 | 4632 | } |
2d21ac55 | 4633 | |
1c79356b A |
4634 | cur_object = new_object; |
4635 | ||
4636 | continue; | |
4637 | } | |
4638 | } | |
1c79356b | 4639 | /* |
2d21ac55 A |
4640 | * Cleanup from fast fault failure. Drop any object |
4641 | * lock other than original and drop map lock. | |
1c79356b | 4642 | */ |
0a7de745 | 4643 | if (object != cur_object) { |
1c79356b | 4644 | vm_object_unlock(cur_object); |
0a7de745 | 4645 | } |
2d21ac55 A |
4646 | |
4647 | /* | |
4648 | * must own the object lock exclusively at this point | |
4649 | */ | |
4650 | if (object_lock_type == OBJECT_LOCK_SHARED) { | |
0a7de745 | 4651 | object_lock_type = OBJECT_LOCK_EXCLUSIVE; |
2d21ac55 A |
4652 | |
4653 | if (vm_object_lock_upgrade(object) == FALSE) { | |
0a7de745 | 4654 | /* |
2d21ac55 A |
4655 | * couldn't upgrade, so explictly |
4656 | * take the lock exclusively | |
4657 | * no need to retry the fault at this | |
4658 | * point since "vm_fault_page" will | |
4659 | * completely re-evaluate the state | |
4660 | */ | |
0a7de745 | 4661 | vm_object_lock(object); |
2d21ac55 | 4662 | } |
1c79356b | 4663 | } |
143cc14e | 4664 | |
2d21ac55 A |
4665 | handle_copy_delay: |
4666 | vm_map_unlock_read(map); | |
0a7de745 | 4667 | if (real_map != map) { |
91447636 | 4668 | vm_map_unlock(real_map); |
0a7de745 | 4669 | } |
1c79356b | 4670 | |
813fb2f6 | 4671 | if (__improbable(object == compressor_object || |
0a7de745 A |
4672 | object == kernel_object || |
4673 | object == vm_submap_object)) { | |
813fb2f6 A |
4674 | /* |
4675 | * These objects are explicitly managed and populated by the | |
4676 | * kernel. The virtual ranges backed by these objects should | |
4677 | * either have wired pages or "holes" that are not supposed to | |
4678 | * be accessed at all until they get explicitly populated. | |
4679 | * We should never have to resolve a fault on a mapping backed | |
4680 | * by one of these VM objects and providing a zero-filled page | |
4681 | * would be wrong here, so let's fail the fault and let the | |
4682 | * caller crash or recover. | |
4683 | */ | |
4684 | vm_object_unlock(object); | |
4685 | kr = KERN_MEMORY_ERROR; | |
4686 | goto done; | |
4687 | } | |
4688 | ||
39037602 A |
4689 | assert(object != compressor_object); |
4690 | assert(object != kernel_object); | |
4691 | assert(object != vm_submap_object); | |
4692 | ||
cb323159 A |
4693 | if (resilient_media_retry) { |
4694 | /* | |
4695 | * We could get here if we failed to get a free page | |
4696 | * to zero-fill and had to take the slow path again. | |
4697 | * Reset our "recovery-from-failed-media" state. | |
4698 | */ | |
4699 | assert(resilient_media_object != VM_OBJECT_NULL); | |
4700 | assert(resilient_media_offset != (vm_object_offset_t)-1); | |
4701 | /* release our extra reference on failed object */ | |
4702 | // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object); | |
4703 | vm_object_deallocate(resilient_media_object); | |
4704 | resilient_media_object = VM_OBJECT_NULL; | |
4705 | resilient_media_offset = (vm_object_offset_t)-1; | |
4706 | resilient_media_retry = FALSE; | |
4707 | } | |
4708 | ||
0a7de745 | 4709 | /* |
2d21ac55 A |
4710 | * Make a reference to this object to |
4711 | * prevent its disposal while we are messing with | |
4712 | * it. Once we have the reference, the map is free | |
4713 | * to be diddled. Since objects reference their | |
4714 | * shadows (and copies), they will stay around as well. | |
1c79356b | 4715 | */ |
2d21ac55 | 4716 | vm_object_reference_locked(object); |
1c79356b A |
4717 | vm_object_paging_begin(object); |
4718 | ||
cb323159 | 4719 | set_thread_pagein_error(cthread, 0); |
2d21ac55 | 4720 | error_code = 0; |
55e303ae | 4721 | |
39236c6e | 4722 | result_page = VM_PAGE_NULL; |
1c79356b | 4723 | kr = vm_fault_page(object, offset, fault_type, |
0a7de745 A |
4724 | (change_wiring && !wired), |
4725 | FALSE, /* page not looked up */ | |
4726 | &prot, &result_page, &top_page, | |
4727 | &type_of_fault, | |
4728 | &error_code, map->no_zero_fill, | |
4729 | FALSE, &fault_info); | |
1c79356b A |
4730 | |
4731 | /* | |
2d21ac55 A |
4732 | * if kr != VM_FAULT_SUCCESS, then the paging reference |
4733 | * has been dropped and the object unlocked... the ref_count | |
4734 | * is still held | |
4735 | * | |
4736 | * if kr == VM_FAULT_SUCCESS, then the paging reference | |
4737 | * is still held along with the ref_count on the original object | |
4738 | * | |
b0d623f7 | 4739 | * the object is returned locked with a paging reference |
2d21ac55 | 4740 | * |
5ba3f43e | 4741 | * if top_page != NULL, then it's BUSY and the |
2d21ac55 A |
4742 | * object it belongs to has a paging reference |
4743 | * but is returned unlocked | |
1c79356b | 4744 | */ |
b0d623f7 A |
4745 | if (kr != VM_FAULT_SUCCESS && |
4746 | kr != VM_FAULT_SUCCESS_NO_VM_PAGE) { | |
cb323159 A |
4747 | if (kr == VM_FAULT_MEMORY_ERROR && |
4748 | fault_info.resilient_media) { | |
4749 | assertf(object->internal, "object %p", object); | |
4750 | /* | |
4751 | * This fault failed but the mapping was | |
4752 | * "media resilient", so we'll retry the fault in | |
4753 | * recovery mode to get a zero-filled page in the | |
4754 | * top object. | |
4755 | * Keep the reference on the failing object so | |
4756 | * that we can check that the mapping is still | |
4757 | * pointing to it when we retry the fault. | |
4758 | */ | |
4759 | // printf("RESILIENT_MEDIA %s:%d: object %p offset 0x%llx recover from media error 0x%x kr 0x%x top_page %p result_page %p\n", __FUNCTION__, __LINE__, object, offset, error_code, kr, top_page, result_page); | |
4760 | assert(!resilient_media_retry); /* no double retry */ | |
4761 | assert(resilient_media_object == VM_OBJECT_NULL); | |
4762 | assert(resilient_media_offset == (vm_object_offset_t)-1); | |
4763 | resilient_media_retry = TRUE; | |
4764 | resilient_media_object = object; | |
4765 | resilient_media_offset = offset; | |
4766 | // printf("FBDP %s:%d resilient_media_object %p offset 0x%llx kept reference\n", __FUNCTION__, __LINE__, resilient_media_object, resilient_mmedia_offset); | |
4767 | goto RetryFault; | |
4768 | } else { | |
4769 | /* | |
4770 | * we didn't succeed, lose the object reference | |
4771 | * immediately. | |
4772 | */ | |
4773 | vm_object_deallocate(object); | |
4774 | object = VM_OBJECT_NULL; /* no longer valid */ | |
4775 | } | |
1c79356b | 4776 | |
2d21ac55 A |
4777 | /* |
4778 | * See why we failed, and take corrective action. | |
4779 | */ | |
4780 | switch (kr) { | |
1c79356b | 4781 | case VM_FAULT_MEMORY_SHORTAGE: |
5ba3f43e | 4782 | if (vm_page_wait((change_wiring) ? |
0a7de745 A |
4783 | THREAD_UNINT : |
4784 | THREAD_ABORTSAFE)) { | |
1c79356b | 4785 | goto RetryFault; |
0a7de745 A |
4786 | } |
4787 | /* | |
4788 | * fall thru | |
4789 | */ | |
1c79356b A |
4790 | case VM_FAULT_INTERRUPTED: |
4791 | kr = KERN_ABORTED; | |
4792 | goto done; | |
4793 | case VM_FAULT_RETRY: | |
4794 | goto RetryFault; | |
1c79356b | 4795 | case VM_FAULT_MEMORY_ERROR: |
0a7de745 | 4796 | if (error_code) { |
1c79356b | 4797 | kr = error_code; |
0a7de745 | 4798 | } else { |
1c79356b | 4799 | kr = KERN_MEMORY_ERROR; |
0a7de745 | 4800 | } |
1c79356b | 4801 | goto done; |
b0d623f7 A |
4802 | default: |
4803 | panic("vm_fault: unexpected error 0x%x from " | |
0a7de745 | 4804 | "vm_fault_page()\n", kr); |
2d21ac55 | 4805 | } |
1c79356b | 4806 | } |
1c79356b | 4807 | m = result_page; |
39037602 | 4808 | m_object = NULL; |
1c79356b | 4809 | |
2d21ac55 | 4810 | if (m != VM_PAGE_NULL) { |
39037602 | 4811 | m_object = VM_PAGE_OBJECT(m); |
0b4e3aa0 | 4812 | assert((change_wiring && !wired) ? |
0a7de745 A |
4813 | (top_page == VM_PAGE_NULL) : |
4814 | ((top_page == VM_PAGE_NULL) == (m_object == object))); | |
0b4e3aa0 | 4815 | } |
1c79356b A |
4816 | |
4817 | /* | |
2d21ac55 A |
4818 | * What to do with the resulting page from vm_fault_page |
4819 | * if it doesn't get entered into the physical map: | |
1c79356b | 4820 | */ |
0a7de745 A |
4821 | #define RELEASE_PAGE(m) \ |
4822 | MACRO_BEGIN \ | |
4823 | PAGE_WAKEUP_DONE(m); \ | |
4824 | if ( !VM_PAGE_PAGEABLE(m)) { \ | |
4825 | vm_page_lockspin_queues(); \ | |
4826 | if ( !VM_PAGE_PAGEABLE(m)) \ | |
4827 | vm_page_activate(m); \ | |
4828 | vm_page_unlock_queues(); \ | |
4829 | } \ | |
1c79356b A |
4830 | MACRO_END |
4831 | ||
5ba3f43e A |
4832 | |
4833 | object_locks_dropped = FALSE; | |
1c79356b | 4834 | /* |
2d21ac55 | 4835 | * We must verify that the maps have not changed |
5ba3f43e A |
4836 | * since our last lookup. vm_map_verify() needs the |
4837 | * map lock (shared) but we are holding object locks. | |
4838 | * So we do a try_lock() first and, if that fails, we | |
4839 | * drop the object locks and go in for the map lock again. | |
1c79356b | 4840 | */ |
5ba3f43e | 4841 | if (!vm_map_try_lock_read(original_map)) { |
5ba3f43e A |
4842 | if (m != VM_PAGE_NULL) { |
4843 | old_copy_object = m_object->copy; | |
4844 | vm_object_unlock(m_object); | |
4845 | } else { | |
4846 | old_copy_object = VM_OBJECT_NULL; | |
4847 | vm_object_unlock(object); | |
4848 | } | |
4849 | ||
4850 | object_locks_dropped = TRUE; | |
4851 | ||
4852 | vm_map_lock_read(original_map); | |
b0d623f7 | 4853 | } |
2d21ac55 | 4854 | |
1c79356b | 4855 | if ((map != original_map) || !vm_map_verify(map, &version)) { |
5ba3f43e A |
4856 | if (object_locks_dropped == FALSE) { |
4857 | if (m != VM_PAGE_NULL) { | |
4858 | old_copy_object = m_object->copy; | |
4859 | vm_object_unlock(m_object); | |
4860 | } else { | |
4861 | old_copy_object = VM_OBJECT_NULL; | |
4862 | vm_object_unlock(object); | |
4863 | } | |
0a7de745 | 4864 | |
5ba3f43e A |
4865 | object_locks_dropped = TRUE; |
4866 | } | |
4867 | ||
4868 | /* | |
4869 | * no object locks are held at this point | |
4870 | */ | |
0a7de745 A |
4871 | vm_object_t retry_object; |
4872 | vm_object_offset_t retry_offset; | |
4873 | vm_prot_t retry_prot; | |
1c79356b A |
4874 | |
4875 | /* | |
2d21ac55 A |
4876 | * To avoid trying to write_lock the map while another |
4877 | * thread has it read_locked (in vm_map_pageable), we | |
4878 | * do not try for write permission. If the page is | |
4879 | * still writable, we will get write permission. If it | |
4880 | * is not, or has been marked needs_copy, we enter the | |
4881 | * mapping without write permission, and will merely | |
4882 | * take another fault. | |
1c79356b A |
4883 | */ |
4884 | map = original_map; | |
2d21ac55 | 4885 | |
1c79356b | 4886 | kr = vm_map_lookup_locked(&map, vaddr, |
0a7de745 A |
4887 | fault_type & ~VM_PROT_WRITE, |
4888 | OBJECT_LOCK_EXCLUSIVE, &version, | |
4889 | &retry_object, &retry_offset, &retry_prot, | |
4890 | &wired, | |
4891 | &fault_info, | |
4892 | &real_map); | |
91447636 | 4893 | pmap = real_map->pmap; |
1c79356b A |
4894 | |
4895 | if (kr != KERN_SUCCESS) { | |
4896 | vm_map_unlock_read(map); | |
2d21ac55 A |
4897 | |
4898 | if (m != VM_PAGE_NULL) { | |
39037602 A |
4899 | assert(VM_PAGE_OBJECT(m) == m_object); |
4900 | ||
0a7de745 | 4901 | /* |
2d21ac55 A |
4902 | * retake the lock so that |
4903 | * we can drop the paging reference | |
4904 | * in vm_fault_cleanup and do the | |
4905 | * PAGE_WAKEUP_DONE in RELEASE_PAGE | |
4906 | */ | |
39037602 | 4907 | vm_object_lock(m_object); |
2d21ac55 | 4908 | |
0b4e3aa0 | 4909 | RELEASE_PAGE(m); |
2d21ac55 | 4910 | |
39037602 | 4911 | vm_fault_cleanup(m_object, top_page); |
0b4e3aa0 | 4912 | } else { |
0a7de745 | 4913 | /* |
2d21ac55 A |
4914 | * retake the lock so that |
4915 | * we can drop the paging reference | |
4916 | * in vm_fault_cleanup | |
4917 | */ | |
0a7de745 | 4918 | vm_object_lock(object); |
2d21ac55 | 4919 | |
0a7de745 | 4920 | vm_fault_cleanup(object, top_page); |
0b4e3aa0 | 4921 | } |
2d21ac55 A |
4922 | vm_object_deallocate(object); |
4923 | ||
1c79356b A |
4924 | goto done; |
4925 | } | |
1c79356b | 4926 | vm_object_unlock(retry_object); |
1c79356b | 4927 | |
2d21ac55 | 4928 | if ((retry_object != object) || (retry_offset != offset)) { |
1c79356b | 4929 | vm_map_unlock_read(map); |
0a7de745 | 4930 | if (real_map != map) { |
91447636 | 4931 | vm_map_unlock(real_map); |
0a7de745 | 4932 | } |
2d21ac55 A |
4933 | |
4934 | if (m != VM_PAGE_NULL) { | |
39037602 A |
4935 | assert(VM_PAGE_OBJECT(m) == m_object); |
4936 | ||
0a7de745 | 4937 | /* |
2d21ac55 A |
4938 | * retake the lock so that |
4939 | * we can drop the paging reference | |
4940 | * in vm_fault_cleanup and do the | |
4941 | * PAGE_WAKEUP_DONE in RELEASE_PAGE | |
4942 | */ | |
0a7de745 | 4943 | vm_object_lock(m_object); |
2d21ac55 | 4944 | |
0b4e3aa0 | 4945 | RELEASE_PAGE(m); |
2d21ac55 | 4946 | |
39037602 | 4947 | vm_fault_cleanup(m_object, top_page); |
0b4e3aa0 | 4948 | } else { |
0a7de745 | 4949 | /* |
2d21ac55 A |
4950 | * retake the lock so that |
4951 | * we can drop the paging reference | |
4952 | * in vm_fault_cleanup | |
4953 | */ | |
0a7de745 | 4954 | vm_object_lock(object); |
2d21ac55 | 4955 | |
0a7de745 | 4956 | vm_fault_cleanup(object, top_page); |
0b4e3aa0 | 4957 | } |
2d21ac55 A |
4958 | vm_object_deallocate(object); |
4959 | ||
1c79356b A |
4960 | goto RetryFault; |
4961 | } | |
1c79356b | 4962 | /* |
2d21ac55 A |
4963 | * Check whether the protection has changed or the object |
4964 | * has been copied while we left the map unlocked. | |
1c79356b | 4965 | */ |
5ba3f43e A |
4966 | if (pmap_has_prot_policy(retry_prot)) { |
4967 | /* If the pmap layer cares, pass the full set. */ | |
4968 | prot = retry_prot; | |
4969 | } else { | |
4970 | prot &= retry_prot; | |
4971 | } | |
0b4e3aa0 | 4972 | } |
1c79356b | 4973 | |
5ba3f43e A |
4974 | if (object_locks_dropped == TRUE) { |
4975 | if (m != VM_PAGE_NULL) { | |
4976 | vm_object_lock(m_object); | |
4977 | ||
4978 | if (m_object->copy != old_copy_object) { | |
4979 | /* | |
4980 | * The copy object changed while the top-level object | |
4981 | * was unlocked, so take away write permission. | |
4982 | */ | |
4983 | assert(!pmap_has_prot_policy(prot)); | |
4984 | prot &= ~VM_PROT_WRITE; | |
4985 | } | |
0a7de745 | 4986 | } else { |
5ba3f43e | 4987 | vm_object_lock(object); |
0a7de745 | 4988 | } |
5ba3f43e A |
4989 | |
4990 | object_locks_dropped = FALSE; | |
4991 | } | |
1c79356b | 4992 | |
cb323159 A |
4993 | if (!need_copy && |
4994 | !fault_info.no_copy_on_read && | |
4995 | m != VM_PAGE_NULL && | |
4996 | VM_PAGE_OBJECT(m) != object && | |
4997 | !VM_PAGE_OBJECT(m)->pager_trusted && | |
4998 | vm_protect_privileged_from_untrusted && | |
4999 | !((prot & VM_PROT_EXECUTE) && | |
5000 | VM_PAGE_OBJECT(m)->code_signed && | |
5001 | cs_process_enforcement(NULL)) && | |
5002 | current_proc_is_privileged()) { | |
5003 | /* | |
5004 | * We found the page we want in an "untrusted" VM object | |
5005 | * down the shadow chain. Since the target is "privileged" | |
5006 | * we want to perform a copy-on-read of that page, so that the | |
5007 | * mapped object gets a stable copy and does not have to | |
5008 | * rely on the "untrusted" object to provide the same | |
5009 | * contents if the page gets reclaimed and has to be paged | |
5010 | * in again later on. | |
5011 | * | |
5012 | * Special case: if the mapping is executable and the untrusted | |
5013 | * object is code-signed and the process is "cs_enforced", we | |
5014 | * do not copy-on-read because that would break code-signing | |
5015 | * enforcement expectations (an executable page must belong | |
5016 | * to a code-signed object) and we can rely on code-signing | |
5017 | * to re-validate the page if it gets evicted and paged back in. | |
5018 | */ | |
5019 | // printf("COPY-ON-READ %s:%d map %p vaddr 0x%llx obj %p offset 0x%llx found page %p (obj %p offset 0x%llx) UNTRUSTED -> need copy-on-read\n", __FUNCTION__, __LINE__, map, (uint64_t)vaddr, object, offset, m, VM_PAGE_OBJECT(m), m->vmp_offset); | |
5020 | vm_copied_on_read++; | |
5021 | need_copy_on_read = TRUE; | |
5022 | need_copy = TRUE; | |
5023 | } else { | |
5024 | need_copy_on_read = FALSE; | |
5025 | } | |
5026 | ||
1c79356b | 5027 | /* |
2d21ac55 A |
5028 | * If we want to wire down this page, but no longer have |
5029 | * adequate permissions, we must start all over. | |
cb323159 | 5030 | * If we decided to copy-on-read, we must also start all over. |
1c79356b | 5031 | */ |
cb323159 A |
5032 | if ((wired && (fault_type != (prot | VM_PROT_WRITE))) || |
5033 | need_copy_on_read) { | |
5ba3f43e | 5034 | vm_map_unlock_read(map); |
0a7de745 | 5035 | if (real_map != map) { |
91447636 | 5036 | vm_map_unlock(real_map); |
0a7de745 | 5037 | } |
1c79356b | 5038 | |
2d21ac55 | 5039 | if (m != VM_PAGE_NULL) { |
39037602 A |
5040 | assert(VM_PAGE_OBJECT(m) == m_object); |
5041 | ||
2d21ac55 | 5042 | RELEASE_PAGE(m); |
91447636 | 5043 | |
39037602 | 5044 | vm_fault_cleanup(m_object, top_page); |
0a7de745 A |
5045 | } else { |
5046 | vm_fault_cleanup(object, top_page); | |
5047 | } | |
0b4e3aa0 | 5048 | |
2d21ac55 | 5049 | vm_object_deallocate(object); |
55e303ae | 5050 | |
2d21ac55 A |
5051 | goto RetryFault; |
5052 | } | |
5053 | if (m != VM_PAGE_NULL) { | |
55e303ae | 5054 | /* |
2d21ac55 A |
5055 | * Put this page into the physical map. |
5056 | * We had to do the unlock above because pmap_enter | |
5057 | * may cause other faults. The page may be on | |
5058 | * the pageout queues. If the pageout daemon comes | |
5059 | * across the page, it will remove it from the queues. | |
55e303ae | 5060 | */ |
2d21ac55 A |
5061 | if (caller_pmap) { |
5062 | kr = vm_fault_enter(m, | |
0a7de745 A |
5063 | caller_pmap, |
5064 | caller_pmap_addr, | |
5065 | prot, | |
5066 | caller_prot, | |
5067 | wired, | |
5068 | change_wiring, | |
5069 | wire_tag, | |
5070 | &fault_info, | |
5071 | NULL, | |
5072 | &type_of_fault); | |
2d21ac55 A |
5073 | } else { |
5074 | kr = vm_fault_enter(m, | |
0a7de745 A |
5075 | pmap, |
5076 | vaddr, | |
5077 | prot, | |
5078 | caller_prot, | |
5079 | wired, | |
5080 | change_wiring, | |
5081 | wire_tag, | |
5082 | &fault_info, | |
5083 | NULL, | |
5084 | &type_of_fault); | |
2d21ac55 | 5085 | } |
39037602 A |
5086 | assert(VM_PAGE_OBJECT(m) == m_object); |
5087 | ||
0a7de745 A |
5088 | { |
5089 | int event_code = 0; | |
39037602 | 5090 | |
0a7de745 A |
5091 | if (m_object->internal) { |
5092 | event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_INTERNAL)); | |
5093 | } else if (m_object->object_is_shared_cache) { | |
5094 | event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_SHAREDCACHE)); | |
5095 | } else { | |
5096 | event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_EXTERNAL)); | |
5097 | } | |
39037602 | 5098 | |
0a7de745 | 5099 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, event_code, trace_real_vaddr, (fault_info.user_tag << 16) | (caller_prot << 8) | type_of_fault, m->vmp_offset, get_current_unique_pid(), 0); |
94ff46dc | 5100 | KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_SLOW), get_current_unique_pid(), 0, 0, 0, 0); |
39037602 | 5101 | |
0a7de745 A |
5102 | DTRACE_VM6(real_fault, vm_map_offset_t, real_vaddr, vm_map_offset_t, m->vmp_offset, int, event_code, int, caller_prot, int, type_of_fault, int, fault_info.user_tag); |
5103 | } | |
2d21ac55 A |
5104 | if (kr != KERN_SUCCESS) { |
5105 | /* abort this page fault */ | |
5ba3f43e | 5106 | vm_map_unlock_read(map); |
0a7de745 | 5107 | if (real_map != map) { |
2d21ac55 | 5108 | vm_map_unlock(real_map); |
0a7de745 | 5109 | } |
2d21ac55 | 5110 | PAGE_WAKEUP_DONE(m); |
39037602 | 5111 | vm_fault_cleanup(m_object, top_page); |
2d21ac55 A |
5112 | vm_object_deallocate(object); |
5113 | goto done; | |
0b4e3aa0 | 5114 | } |
fe8ab488 A |
5115 | if (physpage_p != NULL) { |
5116 | /* for vm_map_wire_and_extract() */ | |
39037602 | 5117 | *physpage_p = VM_PAGE_GET_PHYS_PAGE(m); |
fe8ab488 | 5118 | if (prot & VM_PROT_WRITE) { |
39037602 | 5119 | vm_object_lock_assert_exclusive(m_object); |
d9a64523 | 5120 | m->vmp_dirty = TRUE; |
fe8ab488 A |
5121 | } |
5122 | } | |
0b4e3aa0 | 5123 | } else { |
0a7de745 A |
5124 | vm_map_entry_t entry; |
5125 | vm_map_offset_t laddr; | |
5126 | vm_map_offset_t ldelta, hdelta; | |
143cc14e | 5127 | |
5ba3f43e | 5128 | /* |
0b4e3aa0 | 5129 | * do a pmap block mapping from the physical address |
5ba3f43e | 5130 | * in the object |
0b4e3aa0 | 5131 | */ |
9bccf70c | 5132 | |
0a7de745 | 5133 | if (real_map != map) { |
91447636 | 5134 | vm_map_unlock(real_map); |
0a7de745 | 5135 | } |
2d21ac55 | 5136 | |
9bccf70c A |
5137 | if (original_map != map) { |
5138 | vm_map_unlock_read(map); | |
5139 | vm_map_lock_read(original_map); | |
5140 | map = original_map; | |
5141 | } | |
91447636 | 5142 | real_map = map; |
9bccf70c A |
5143 | |
5144 | laddr = vaddr; | |
5145 | hdelta = 0xFFFFF000; | |
5146 | ldelta = 0xFFFFF000; | |
5147 | ||
2d21ac55 | 5148 | while (vm_map_lookup_entry(map, laddr, &entry)) { |
0a7de745 | 5149 | if (ldelta > (laddr - entry->vme_start)) { |
9bccf70c | 5150 | ldelta = laddr - entry->vme_start; |
0a7de745 A |
5151 | } |
5152 | if (hdelta > (entry->vme_end - laddr)) { | |
9bccf70c | 5153 | hdelta = entry->vme_end - laddr; |
0a7de745 | 5154 | } |
2d21ac55 | 5155 | if (entry->is_sub_map) { |
5ba3f43e | 5156 | laddr = ((laddr - entry->vme_start) |
0a7de745 | 5157 | + VME_OFFSET(entry)); |
3e170ce0 | 5158 | vm_map_lock_read(VME_SUBMAP(entry)); |
2d21ac55 | 5159 | |
0a7de745 | 5160 | if (map != real_map) { |
9bccf70c | 5161 | vm_map_unlock_read(map); |
0a7de745 | 5162 | } |
2d21ac55 | 5163 | if (entry->use_pmap) { |
91447636 | 5164 | vm_map_unlock_read(real_map); |
3e170ce0 | 5165 | real_map = VME_SUBMAP(entry); |
9bccf70c | 5166 | } |
3e170ce0 | 5167 | map = VME_SUBMAP(entry); |
9bccf70c A |
5168 | } else { |
5169 | break; | |
5170 | } | |
5171 | } | |
5172 | ||
5ba3f43e | 5173 | if (vm_map_lookup_entry(map, laddr, &entry) && |
3e170ce0 A |
5174 | (VME_OBJECT(entry) != NULL) && |
5175 | (VME_OBJECT(entry) == object)) { | |
5176 | int superpage; | |
2d21ac55 | 5177 | |
3e170ce0 | 5178 | if (!object->pager_created && |
4bd07ac2 A |
5179 | object->phys_contiguous && |
5180 | VME_OFFSET(entry) == 0 && | |
5181 | (entry->vme_end - entry->vme_start == object->vo_size) && | |
0a7de745 | 5182 | VM_MAP_PAGE_ALIGNED(entry->vme_start, (object->vo_size - 1))) { |
3e170ce0 A |
5183 | superpage = VM_MEM_SUPERPAGE; |
5184 | } else { | |
5185 | superpage = 0; | |
5186 | } | |
fe8ab488 A |
5187 | |
5188 | if (superpage && physpage_p) { | |
5189 | /* for vm_map_wire_and_extract() */ | |
3e170ce0 | 5190 | *physpage_p = (ppnum_t) |
0a7de745 A |
5191 | ((((vm_map_offset_t) |
5192 | object->vo_shadow_offset) | |
5193 | + VME_OFFSET(entry) | |
5194 | + (laddr - entry->vme_start)) | |
5195 | >> PAGE_SHIFT); | |
fe8ab488 A |
5196 | } |
5197 | ||
2d21ac55 A |
5198 | if (caller_pmap) { |
5199 | /* | |
5200 | * Set up a block mapped area | |
5201 | */ | |
fe8ab488 | 5202 | assert((uint32_t)((ldelta + hdelta) >> PAGE_SHIFT) == ((ldelta + hdelta) >> PAGE_SHIFT)); |
5ba3f43e | 5203 | kr = pmap_map_block(caller_pmap, |
0a7de745 A |
5204 | (addr64_t)(caller_pmap_addr - ldelta), |
5205 | (ppnum_t)((((vm_map_offset_t) (VME_OBJECT(entry)->vo_shadow_offset)) + | |
5206 | VME_OFFSET(entry) + (laddr - entry->vme_start) - ldelta) >> PAGE_SHIFT), | |
5207 | (uint32_t)((ldelta + hdelta) >> PAGE_SHIFT), prot, | |
5208 | (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0); | |
5ba3f43e A |
5209 | |
5210 | if (kr != KERN_SUCCESS) { | |
5211 | goto cleanup; | |
5212 | } | |
5213 | } else { | |
2d21ac55 A |
5214 | /* |
5215 | * Set up a block mapped area | |
5216 | */ | |
fe8ab488 | 5217 | assert((uint32_t)((ldelta + hdelta) >> PAGE_SHIFT) == ((ldelta + hdelta) >> PAGE_SHIFT)); |
5ba3f43e | 5218 | kr = pmap_map_block(real_map->pmap, |
0a7de745 A |
5219 | (addr64_t)(vaddr - ldelta), |
5220 | (ppnum_t)((((vm_map_offset_t)(VME_OBJECT(entry)->vo_shadow_offset)) + | |
5221 | VME_OFFSET(entry) + (laddr - entry->vme_start) - ldelta) >> PAGE_SHIFT), | |
5222 | (uint32_t)((ldelta + hdelta) >> PAGE_SHIFT), prot, | |
5223 | (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0); | |
5ba3f43e A |
5224 | |
5225 | if (kr != KERN_SUCCESS) { | |
5226 | goto cleanup; | |
5227 | } | |
9bccf70c A |
5228 | } |
5229 | } | |
0b4e3aa0 | 5230 | } |
1c79356b | 5231 | |
5ba3f43e A |
5232 | /* |
5233 | * Success | |
5234 | */ | |
5235 | kr = KERN_SUCCESS; | |
5236 | ||
5237 | /* | |
5238 | * TODO: could most of the done cases just use cleanup? | |
5239 | */ | |
5240 | cleanup: | |
1c79356b | 5241 | /* |
2d21ac55 | 5242 | * Unlock everything, and return |
1c79356b | 5243 | */ |
5ba3f43e | 5244 | vm_map_unlock_read(map); |
0a7de745 | 5245 | if (real_map != map) { |
91447636 | 5246 | vm_map_unlock(real_map); |
0a7de745 | 5247 | } |
2d21ac55 A |
5248 | |
5249 | if (m != VM_PAGE_NULL) { | |
39037602 A |
5250 | assert(VM_PAGE_OBJECT(m) == m_object); |
5251 | ||
d9a64523 | 5252 | if (!m_object->internal && (fault_type & VM_PROT_WRITE)) { |
d9a64523 A |
5253 | vm_object_paging_begin(m_object); |
5254 | ||
5255 | assert(written_on_object == VM_OBJECT_NULL); | |
5256 | written_on_object = m_object; | |
5257 | written_on_pager = m_object->pager; | |
5258 | written_on_offset = m_object->paging_offset + m->vmp_offset; | |
5259 | } | |
0b4e3aa0 | 5260 | PAGE_WAKEUP_DONE(m); |
1c79356b | 5261 | |
39037602 | 5262 | vm_fault_cleanup(m_object, top_page); |
0a7de745 A |
5263 | } else { |
5264 | vm_fault_cleanup(object, top_page); | |
5265 | } | |
1c79356b | 5266 | |
2d21ac55 A |
5267 | vm_object_deallocate(object); |
5268 | ||
0a7de745 | 5269 | #undef RELEASE_PAGE |
91447636 | 5270 | |
2d21ac55 | 5271 | done: |
9bccf70c | 5272 | thread_interrupt_level(interruptible_state); |
1c79356b | 5273 | |
cb323159 A |
5274 | if (resilient_media_object != VM_OBJECT_NULL) { |
5275 | assert(resilient_media_retry); | |
5276 | assert(resilient_media_offset != (vm_object_offset_t)-1); | |
5277 | /* release extra reference on failed object */ | |
5278 | // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object); | |
5279 | vm_object_deallocate(resilient_media_object); | |
5280 | resilient_media_object = VM_OBJECT_NULL; | |
5281 | resilient_media_offset = (vm_object_offset_t)-1; | |
5282 | resilient_media_retry = FALSE; | |
5283 | } | |
5284 | assert(!resilient_media_retry); | |
5285 | ||
39236c6e | 5286 | /* |
04b8595b | 5287 | * Only I/O throttle on faults which cause a pagein/swapin. |
39236c6e A |
5288 | */ |
5289 | if ((type_of_fault == DBG_PAGEIND_FAULT) || (type_of_fault == DBG_PAGEINV_FAULT) || (type_of_fault == DBG_COMPRESSOR_SWAPIN_FAULT)) { | |
5290 | throttle_lowpri_io(1); | |
04b8595b A |
5291 | } else { |
5292 | if (kr == KERN_SUCCESS && type_of_fault != DBG_CACHE_HIT_FAULT && type_of_fault != DBG_GUARD_FAULT) { | |
04b8595b | 5293 | if ((throttle_delay = vm_page_throttled(TRUE))) { |
04b8595b | 5294 | if (vm_debug_events) { |
0a7de745 | 5295 | if (type_of_fault == DBG_COMPRESSOR_FAULT) { |
04b8595b | 5296 | VM_DEBUG_EVENT(vmf_compressordelay, VMF_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); |
0a7de745 | 5297 | } else if (type_of_fault == DBG_COW_FAULT) { |
04b8595b | 5298 | VM_DEBUG_EVENT(vmf_cowdelay, VMF_COWDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); |
0a7de745 | 5299 | } else { |
04b8595b | 5300 | VM_DEBUG_EVENT(vmf_zfdelay, VMF_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); |
0a7de745 | 5301 | } |
04b8595b A |
5302 | } |
5303 | delay(throttle_delay); | |
5304 | } | |
5305 | } | |
5306 | } | |
d9a64523 A |
5307 | |
5308 | if (written_on_object) { | |
d9a64523 A |
5309 | vnode_pager_dirtied(written_on_pager, written_on_offset, written_on_offset + PAGE_SIZE_64); |
5310 | ||
5311 | vm_object_lock(written_on_object); | |
5312 | vm_object_paging_end(written_on_object); | |
5313 | vm_object_unlock(written_on_object); | |
5314 | ||
5315 | written_on_object = VM_OBJECT_NULL; | |
5316 | } | |
5317 | ||
5318 | if (rtfault) { | |
5319 | vm_record_rtfault(cthread, fstart, trace_vaddr, type_of_fault); | |
5320 | } | |
5321 | ||
5ba3f43e | 5322 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
0a7de745 A |
5323 | (MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END, |
5324 | ((uint64_t)trace_vaddr >> 32), | |
5325 | trace_vaddr, | |
5326 | kr, | |
5327 | type_of_fault, | |
5328 | 0); | |
5329 | ||
5330 | return kr; | |
1c79356b A |
5331 | } |
5332 | ||
5333 | /* | |
5334 | * vm_fault_wire: | |
5335 | * | |
5336 | * Wire down a range of virtual addresses in a map. | |
5337 | */ | |
5338 | kern_return_t | |
5339 | vm_fault_wire( | |
0a7de745 A |
5340 | vm_map_t map, |
5341 | vm_map_entry_t entry, | |
3e170ce0 | 5342 | vm_prot_t prot, |
5ba3f43e | 5343 | vm_tag_t wire_tag, |
0a7de745 A |
5344 | pmap_t pmap, |
5345 | vm_map_offset_t pmap_addr, | |
5346 | ppnum_t *physpage_p) | |
1c79356b | 5347 | { |
0a7de745 A |
5348 | vm_map_offset_t va; |
5349 | vm_map_offset_t end_addr = entry->vme_end; | |
5350 | kern_return_t rc; | |
1c79356b A |
5351 | |
5352 | assert(entry->in_transition); | |
5353 | ||
5ba3f43e A |
5354 | if ((VME_OBJECT(entry) != NULL) && |
5355 | !entry->is_sub_map && | |
3e170ce0 | 5356 | VME_OBJECT(entry)->phys_contiguous) { |
9bccf70c A |
5357 | return KERN_SUCCESS; |
5358 | } | |
5359 | ||
1c79356b A |
5360 | /* |
5361 | * Inform the physical mapping system that the | |
5362 | * range of addresses may not fault, so that | |
5363 | * page tables and such can be locked down as well. | |
5364 | */ | |
5365 | ||
5ba3f43e | 5366 | pmap_pageable(pmap, pmap_addr, |
0a7de745 | 5367 | pmap_addr + (end_addr - entry->vme_start), FALSE); |
1c79356b A |
5368 | |
5369 | /* | |
5370 | * We simulate a fault to get the page and enter it | |
5371 | * in the physical map. | |
5372 | */ | |
5373 | ||
5374 | for (va = entry->vme_start; va < end_addr; va += PAGE_SIZE) { | |
5ba3f43e | 5375 | rc = vm_fault_wire_fast(map, va, prot, wire_tag, entry, pmap, |
0a7de745 A |
5376 | pmap_addr + (va - entry->vme_start), |
5377 | physpage_p); | |
fe8ab488 | 5378 | if (rc != KERN_SUCCESS) { |
5ba3f43e | 5379 | rc = vm_fault_internal(map, va, prot, TRUE, wire_tag, |
0a7de745 A |
5380 | ((pmap == kernel_pmap) |
5381 | ? THREAD_UNINT | |
5382 | : THREAD_ABORTSAFE), | |
5383 | pmap, | |
5384 | (pmap_addr + | |
5385 | (va - entry->vme_start)), | |
5386 | physpage_p); | |
2d21ac55 | 5387 | DTRACE_VM2(softlock, int, 1, (uint64_t *), NULL); |
1c79356b A |
5388 | } |
5389 | ||
5390 | if (rc != KERN_SUCCESS) { | |
0a7de745 | 5391 | struct vm_map_entry tmp_entry = *entry; |
1c79356b A |
5392 | |
5393 | /* unwire wired pages */ | |
5394 | tmp_entry.vme_end = va; | |
5ba3f43e | 5395 | vm_fault_unwire(map, |
0a7de745 | 5396 | &tmp_entry, FALSE, pmap, pmap_addr); |
1c79356b A |
5397 | |
5398 | return rc; | |
5399 | } | |
5400 | } | |
5401 | return KERN_SUCCESS; | |
5402 | } | |
5403 | ||
5404 | /* | |
5405 | * vm_fault_unwire: | |
5406 | * | |
5407 | * Unwire a range of virtual addresses in a map. | |
5408 | */ | |
5409 | void | |
5410 | vm_fault_unwire( | |
0a7de745 A |
5411 | vm_map_t map, |
5412 | vm_map_entry_t entry, | |
5413 | boolean_t deallocate, | |
5414 | pmap_t pmap, | |
5415 | vm_map_offset_t pmap_addr) | |
1c79356b | 5416 | { |
0a7de745 A |
5417 | vm_map_offset_t va; |
5418 | vm_map_offset_t end_addr = entry->vme_end; | |
5419 | vm_object_t object; | |
d9a64523 | 5420 | struct vm_object_fault_info fault_info = {}; |
5ba3f43e | 5421 | unsigned int unwired_pages; |
1c79356b | 5422 | |
3e170ce0 | 5423 | object = (entry->is_sub_map) ? VM_OBJECT_NULL : VME_OBJECT(entry); |
1c79356b | 5424 | |
2d21ac55 A |
5425 | /* |
5426 | * If it's marked phys_contiguous, then vm_fault_wire() didn't actually | |
5427 | * do anything since such memory is wired by default. So we don't have | |
5428 | * anything to undo here. | |
5429 | */ | |
5430 | ||
0a7de745 | 5431 | if (object != VM_OBJECT_NULL && object->phys_contiguous) { |
2d21ac55 | 5432 | return; |
0a7de745 | 5433 | } |
2d21ac55 A |
5434 | |
5435 | fault_info.interruptible = THREAD_UNINT; | |
5436 | fault_info.behavior = entry->behavior; | |
3e170ce0 | 5437 | fault_info.user_tag = VME_ALIAS(entry); |
fe8ab488 A |
5438 | if (entry->iokit_acct || |
5439 | (!entry->is_sub_map && !entry->use_pmap)) { | |
5440 | fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT; | |
5441 | } | |
3e170ce0 A |
5442 | fault_info.lo_offset = VME_OFFSET(entry); |
5443 | fault_info.hi_offset = (entry->vme_end - entry->vme_start) + VME_OFFSET(entry); | |
2d21ac55 | 5444 | fault_info.no_cache = entry->no_cache; |
b0d623f7 | 5445 | fault_info.stealth = TRUE; |
2d21ac55 | 5446 | |
5ba3f43e A |
5447 | unwired_pages = 0; |
5448 | ||
1c79356b A |
5449 | /* |
5450 | * Since the pages are wired down, we must be able to | |
5451 | * get their mappings from the physical map system. | |
5452 | */ | |
5453 | ||
5454 | for (va = entry->vme_start; va < end_addr; va += PAGE_SIZE) { | |
1c79356b | 5455 | if (object == VM_OBJECT_NULL) { |
593a1d5f | 5456 | if (pmap) { |
5ba3f43e | 5457 | pmap_change_wiring(pmap, |
0a7de745 | 5458 | pmap_addr + (va - entry->vme_start), FALSE); |
593a1d5f | 5459 | } |
5ba3f43e | 5460 | (void) vm_fault(map, va, VM_PROT_NONE, |
0a7de745 | 5461 | TRUE, VM_KERN_MEMORY_NONE, THREAD_UNINT, pmap, pmap_addr); |
1c79356b | 5462 | } else { |
0a7de745 A |
5463 | vm_prot_t prot; |
5464 | vm_page_t result_page; | |
5465 | vm_page_t top_page; | |
5466 | vm_object_t result_object; | |
1c79356b A |
5467 | vm_fault_return_t result; |
5468 | ||
d9a64523 A |
5469 | /* cap cluster size at maximum UPL size */ |
5470 | upl_size_t cluster_size; | |
5471 | if (os_sub_overflow(end_addr, va, &cluster_size)) { | |
5472 | cluster_size = 0 - (upl_size_t)PAGE_SIZE; | |
b0d623f7 | 5473 | } |
d9a64523 | 5474 | fault_info.cluster_size = cluster_size; |
2d21ac55 | 5475 | |
1c79356b A |
5476 | do { |
5477 | prot = VM_PROT_NONE; | |
5478 | ||
5479 | vm_object_lock(object); | |
5480 | vm_object_paging_begin(object); | |
39236c6e | 5481 | result_page = VM_PAGE_NULL; |
0a7de745 | 5482 | result = vm_fault_page( |
2d21ac55 | 5483 | object, |
3e170ce0 | 5484 | (VME_OFFSET(entry) + |
0a7de745 | 5485 | (va - entry->vme_start)), |
2d21ac55 | 5486 | VM_PROT_NONE, TRUE, |
39236c6e | 5487 | FALSE, /* page not looked up */ |
2d21ac55 A |
5488 | &prot, &result_page, &top_page, |
5489 | (int *)0, | |
5ba3f43e | 5490 | NULL, map->no_zero_fill, |
2d21ac55 | 5491 | FALSE, &fault_info); |
1c79356b A |
5492 | } while (result == VM_FAULT_RETRY); |
5493 | ||
2d21ac55 A |
5494 | /* |
5495 | * If this was a mapping to a file on a device that has been forcibly | |
5496 | * unmounted, then we won't get a page back from vm_fault_page(). Just | |
5497 | * move on to the next one in case the remaining pages are mapped from | |
5498 | * different objects. During a forced unmount, the object is terminated | |
5499 | * so the alive flag will be false if this happens. A forced unmount will | |
5ba3f43e | 5500 | * will occur when an external disk is unplugged before the user does an |
2d21ac55 A |
5501 | * eject, so we don't want to panic in that situation. |
5502 | */ | |
5503 | ||
0a7de745 | 5504 | if (result == VM_FAULT_MEMORY_ERROR && !object->alive) { |
2d21ac55 | 5505 | continue; |
0a7de745 | 5506 | } |
2d21ac55 | 5507 | |
39236c6e A |
5508 | if (result == VM_FAULT_MEMORY_ERROR && |
5509 | object == kernel_object) { | |
5510 | /* | |
5511 | * This must have been allocated with | |
5512 | * KMA_KOBJECT and KMA_VAONLY and there's | |
5513 | * no physical page at this offset. | |
5514 | * We're done (no page to free). | |
5515 | */ | |
5516 | assert(deallocate); | |
5517 | continue; | |
5518 | } | |
5519 | ||
0a7de745 | 5520 | if (result != VM_FAULT_SUCCESS) { |
1c79356b | 5521 | panic("vm_fault_unwire: failure"); |
0a7de745 | 5522 | } |
1c79356b | 5523 | |
39037602 | 5524 | result_object = VM_PAGE_OBJECT(result_page); |
2d21ac55 | 5525 | |
1c79356b | 5526 | if (deallocate) { |
39037602 | 5527 | assert(VM_PAGE_GET_PHYS_PAGE(result_page) != |
0a7de745 | 5528 | vm_page_fictitious_addr); |
39037602 | 5529 | pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page)); |
5ba3f43e A |
5530 | if (VM_PAGE_WIRED(result_page)) { |
5531 | unwired_pages++; | |
5532 | } | |
1c79356b A |
5533 | VM_PAGE_FREE(result_page); |
5534 | } else { | |
0a7de745 | 5535 | if ((pmap) && (VM_PAGE_GET_PHYS_PAGE(result_page) != vm_page_guard_addr)) { |
5ba3f43e | 5536 | pmap_change_wiring(pmap, |
6d2010ae | 5537 | pmap_addr + (va - entry->vme_start), FALSE); |
0a7de745 | 5538 | } |
6d2010ae A |
5539 | |
5540 | ||
b0d623f7 A |
5541 | if (VM_PAGE_WIRED(result_page)) { |
5542 | vm_page_lockspin_queues(); | |
0b4c1975 | 5543 | vm_page_unwire(result_page, TRUE); |
b0d623f7 | 5544 | vm_page_unlock_queues(); |
5ba3f43e | 5545 | unwired_pages++; |
b0d623f7 | 5546 | } |
0a7de745 | 5547 | if (entry->zero_wired_pages) { |
39037602 | 5548 | pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page)); |
b0d623f7 A |
5549 | entry->zero_wired_pages = FALSE; |
5550 | } | |
5551 | ||
1c79356b A |
5552 | PAGE_WAKEUP_DONE(result_page); |
5553 | } | |
1c79356b A |
5554 | vm_fault_cleanup(result_object, top_page); |
5555 | } | |
5556 | } | |
5557 | ||
5558 | /* | |
5559 | * Inform the physical mapping system that the range | |
5560 | * of addresses may fault, so that page tables and | |
5561 | * such may be unwired themselves. | |
5562 | */ | |
5563 | ||
5ba3f43e | 5564 | pmap_pageable(pmap, pmap_addr, |
0a7de745 | 5565 | pmap_addr + (end_addr - entry->vme_start), TRUE); |
1c79356b | 5566 | |
5ba3f43e | 5567 | if (kernel_object == object) { |
0a7de745 | 5568 | vm_tag_update_size(fault_info.user_tag, -ptoa_64(unwired_pages)); |
5ba3f43e | 5569 | } |
1c79356b A |
5570 | } |
5571 | ||
5572 | /* | |
5573 | * vm_fault_wire_fast: | |
5574 | * | |
5575 | * Handle common case of a wire down page fault at the given address. | |
5576 | * If successful, the page is inserted into the associated physical map. | |
5577 | * The map entry is passed in to avoid the overhead of a map lookup. | |
5578 | * | |
5579 | * NOTE: the given address should be truncated to the | |
5580 | * proper page address. | |
5581 | * | |
5582 | * KERN_SUCCESS is returned if the page fault is handled; otherwise, | |
5583 | * a standard error specifying why the fault is fatal is returned. | |
5584 | * | |
5585 | * The map in question must be referenced, and remains so. | |
5586 | * Caller has a read lock on the map. | |
5587 | * | |
5588 | * This is a stripped version of vm_fault() for wiring pages. Anything | |
5589 | * other than the common case will return KERN_FAILURE, and the caller | |
5590 | * is expected to call vm_fault(). | |
5591 | */ | |
3e170ce0 | 5592 | static kern_return_t |
1c79356b | 5593 | vm_fault_wire_fast( |
0a7de745 A |
5594 | __unused vm_map_t map, |
5595 | vm_map_offset_t va, | |
5ba3f43e | 5596 | __unused vm_prot_t caller_prot, |
0a7de745 A |
5597 | vm_tag_t wire_tag, |
5598 | vm_map_entry_t entry, | |
5599 | pmap_t pmap, | |
5600 | vm_map_offset_t pmap_addr, | |
5601 | ppnum_t *physpage_p) | |
1c79356b | 5602 | { |
0a7de745 A |
5603 | vm_object_t object; |
5604 | vm_object_offset_t offset; | |
5605 | vm_page_t m; | |
5606 | vm_prot_t prot; | |
5607 | thread_t thread = current_thread(); | |
5608 | int type_of_fault; | |
5609 | kern_return_t kr; | |
d9a64523 | 5610 | struct vm_object_fault_info fault_info = {}; |
1c79356b | 5611 | |
2d21ac55 | 5612 | VM_STAT_INCR(faults); |
1c79356b | 5613 | |
0a7de745 A |
5614 | if (thread != THREAD_NULL && thread->task != TASK_NULL) { |
5615 | thread->task->faults++; | |
5616 | } | |
1c79356b A |
5617 | |
5618 | /* | |
5619 | * Recovery actions | |
5620 | */ | |
5621 | ||
0a7de745 A |
5622 | #undef RELEASE_PAGE |
5623 | #define RELEASE_PAGE(m) { \ | |
5624 | PAGE_WAKEUP_DONE(m); \ | |
5625 | vm_page_lockspin_queues(); \ | |
5626 | vm_page_unwire(m, TRUE); \ | |
5627 | vm_page_unlock_queues(); \ | |
1c79356b A |
5628 | } |
5629 | ||
5630 | ||
0a7de745 A |
5631 | #undef UNLOCK_THINGS |
5632 | #define UNLOCK_THINGS { \ | |
5633 | vm_object_paging_end(object); \ | |
5634 | vm_object_unlock(object); \ | |
1c79356b A |
5635 | } |
5636 | ||
0a7de745 A |
5637 | #undef UNLOCK_AND_DEALLOCATE |
5638 | #define UNLOCK_AND_DEALLOCATE { \ | |
5639 | UNLOCK_THINGS; \ | |
5640 | vm_object_deallocate(object); \ | |
1c79356b A |
5641 | } |
5642 | /* | |
5643 | * Give up and have caller do things the hard way. | |
5644 | */ | |
5645 | ||
0a7de745 A |
5646 | #define GIVE_UP { \ |
5647 | UNLOCK_AND_DEALLOCATE; \ | |
5648 | return(KERN_FAILURE); \ | |
1c79356b A |
5649 | } |
5650 | ||
5651 | ||
5652 | /* | |
5653 | * If this entry is not directly to a vm_object, bail out. | |
5654 | */ | |
fe8ab488 A |
5655 | if (entry->is_sub_map) { |
5656 | assert(physpage_p == NULL); | |
0a7de745 | 5657 | return KERN_FAILURE; |
fe8ab488 | 5658 | } |
1c79356b A |
5659 | |
5660 | /* | |
5661 | * Find the backing store object and offset into it. | |
5662 | */ | |
5663 | ||
3e170ce0 A |
5664 | object = VME_OBJECT(entry); |
5665 | offset = (va - entry->vme_start) + VME_OFFSET(entry); | |
1c79356b A |
5666 | prot = entry->protection; |
5667 | ||
0a7de745 | 5668 | /* |
1c79356b A |
5669 | * Make a reference to this object to prevent its |
5670 | * disposal while we are messing with it. | |
5671 | */ | |
5672 | ||
5673 | vm_object_lock(object); | |
2d21ac55 | 5674 | vm_object_reference_locked(object); |
ff6e181a | 5675 | vm_object_paging_begin(object); |
1c79356b A |
5676 | |
5677 | /* | |
5678 | * INVARIANTS (through entire routine): | |
5679 | * | |
5680 | * 1) At all times, we must either have the object | |
5681 | * lock or a busy page in some object to prevent | |
5682 | * some other thread from trying to bring in | |
5683 | * the same page. | |
5684 | * | |
5685 | * 2) Once we have a busy page, we must remove it from | |
5686 | * the pageout queues, so that the pageout daemon | |
5687 | * will not grab it away. | |
5688 | * | |
5689 | */ | |
5690 | ||
5691 | /* | |
5692 | * Look for page in top-level object. If it's not there or | |
5693 | * there's something going on, give up. | |
5694 | */ | |
5695 | m = vm_page_lookup(object, offset); | |
d9a64523 | 5696 | if ((m == VM_PAGE_NULL) || (m->vmp_busy) || |
0a7de745 | 5697 | (m->vmp_unusual && (m->vmp_error || m->vmp_restart || m->vmp_absent))) { |
1c79356b A |
5698 | GIVE_UP; |
5699 | } | |
d9a64523 | 5700 | if (m->vmp_fictitious && |
39037602 | 5701 | VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) { |
2d21ac55 A |
5702 | /* |
5703 | * Guard pages are fictitious pages and are never | |
5704 | * entered into a pmap, so let's say it's been wired... | |
5705 | */ | |
5706 | kr = KERN_SUCCESS; | |
5707 | goto done; | |
5708 | } | |
5709 | ||
1c79356b A |
5710 | /* |
5711 | * Wire the page down now. All bail outs beyond this | |
5ba3f43e | 5712 | * point must unwire the page. |
1c79356b A |
5713 | */ |
5714 | ||
2d21ac55 | 5715 | vm_page_lockspin_queues(); |
5ba3f43e | 5716 | vm_page_wire(m, wire_tag, TRUE); |
1c79356b A |
5717 | vm_page_unlock_queues(); |
5718 | ||
5719 | /* | |
5720 | * Mark page busy for other threads. | |
5721 | */ | |
d9a64523 A |
5722 | assert(!m->vmp_busy); |
5723 | m->vmp_busy = TRUE; | |
5724 | assert(!m->vmp_absent); | |
1c79356b A |
5725 | |
5726 | /* | |
5727 | * Give up if the page is being written and there's a copy object | |
5728 | */ | |
5729 | if ((object->copy != VM_OBJECT_NULL) && (prot & VM_PROT_WRITE)) { | |
5730 | RELEASE_PAGE(m); | |
5731 | GIVE_UP; | |
5732 | } | |
5733 | ||
d9a64523 A |
5734 | fault_info.user_tag = VME_ALIAS(entry); |
5735 | fault_info.pmap_options = 0; | |
5736 | if (entry->iokit_acct || | |
5737 | (!entry->is_sub_map && !entry->use_pmap)) { | |
5738 | fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT; | |
5739 | } | |
5740 | ||
1c79356b A |
5741 | /* |
5742 | * Put this page into the physical map. | |
1c79356b | 5743 | */ |
2d21ac55 A |
5744 | type_of_fault = DBG_CACHE_HIT_FAULT; |
5745 | kr = vm_fault_enter(m, | |
0a7de745 A |
5746 | pmap, |
5747 | pmap_addr, | |
5748 | prot, | |
5749 | prot, | |
5750 | TRUE, /* wired */ | |
5751 | FALSE, /* change_wiring */ | |
5752 | wire_tag, | |
5753 | &fault_info, | |
5754 | NULL, | |
5755 | &type_of_fault); | |
39037602 A |
5756 | if (kr != KERN_SUCCESS) { |
5757 | RELEASE_PAGE(m); | |
5758 | GIVE_UP; | |
5759 | } | |
2d21ac55 A |
5760 | |
5761 | done: | |
1c79356b A |
5762 | /* |
5763 | * Unlock everything, and return | |
5764 | */ | |
5765 | ||
fe8ab488 A |
5766 | if (physpage_p) { |
5767 | /* for vm_map_wire_and_extract() */ | |
5768 | if (kr == KERN_SUCCESS) { | |
39037602 A |
5769 | assert(object == VM_PAGE_OBJECT(m)); |
5770 | *physpage_p = VM_PAGE_GET_PHYS_PAGE(m); | |
fe8ab488 | 5771 | if (prot & VM_PROT_WRITE) { |
39037602 | 5772 | vm_object_lock_assert_exclusive(object); |
d9a64523 | 5773 | m->vmp_dirty = TRUE; |
fe8ab488 A |
5774 | } |
5775 | } else { | |
5776 | *physpage_p = 0; | |
5777 | } | |
5778 | } | |
5779 | ||
1c79356b A |
5780 | PAGE_WAKEUP_DONE(m); |
5781 | UNLOCK_AND_DEALLOCATE; | |
5782 | ||
2d21ac55 | 5783 | return kr; |
1c79356b A |
5784 | } |
5785 | ||
5786 | /* | |
5787 | * Routine: vm_fault_copy_cleanup | |
5788 | * Purpose: | |
5789 | * Release a page used by vm_fault_copy. | |
5790 | */ | |
5791 | ||
3e170ce0 | 5792 | static void |
1c79356b | 5793 | vm_fault_copy_cleanup( |
0a7de745 A |
5794 | vm_page_t page, |
5795 | vm_page_t top_page) | |
1c79356b | 5796 | { |
0a7de745 | 5797 | vm_object_t object = VM_PAGE_OBJECT(page); |
1c79356b A |
5798 | |
5799 | vm_object_lock(object); | |
5800 | PAGE_WAKEUP_DONE(page); | |
0a7de745 | 5801 | if (!VM_PAGE_PAGEABLE(page)) { |
b0d623f7 | 5802 | vm_page_lockspin_queues(); |
0a7de745 | 5803 | if (!VM_PAGE_PAGEABLE(page)) { |
b0d623f7 | 5804 | vm_page_activate(page); |
39037602 | 5805 | } |
b0d623f7 A |
5806 | vm_page_unlock_queues(); |
5807 | } | |
1c79356b A |
5808 | vm_fault_cleanup(object, top_page); |
5809 | } | |
5810 | ||
3e170ce0 | 5811 | static void |
1c79356b | 5812 | vm_fault_copy_dst_cleanup( |
0a7de745 | 5813 | vm_page_t page) |
1c79356b | 5814 | { |
0a7de745 | 5815 | vm_object_t object; |
1c79356b A |
5816 | |
5817 | if (page != VM_PAGE_NULL) { | |
39037602 | 5818 | object = VM_PAGE_OBJECT(page); |
1c79356b | 5819 | vm_object_lock(object); |
2d21ac55 | 5820 | vm_page_lockspin_queues(); |
0b4c1975 | 5821 | vm_page_unwire(page, TRUE); |
1c79356b | 5822 | vm_page_unlock_queues(); |
5ba3f43e | 5823 | vm_object_paging_end(object); |
1c79356b A |
5824 | vm_object_unlock(object); |
5825 | } | |
5826 | } | |
5827 | ||
5828 | /* | |
5829 | * Routine: vm_fault_copy | |
5830 | * | |
5831 | * Purpose: | |
5832 | * Copy pages from one virtual memory object to another -- | |
5833 | * neither the source nor destination pages need be resident. | |
5834 | * | |
5835 | * Before actually copying a page, the version associated with | |
5836 | * the destination address map wil be verified. | |
5837 | * | |
5838 | * In/out conditions: | |
5839 | * The caller must hold a reference, but not a lock, to | |
5840 | * each of the source and destination objects and to the | |
5841 | * destination map. | |
5842 | * | |
5843 | * Results: | |
5844 | * Returns KERN_SUCCESS if no errors were encountered in | |
5845 | * reading or writing the data. Returns KERN_INTERRUPTED if | |
5846 | * the operation was interrupted (only possible if the | |
5847 | * "interruptible" argument is asserted). Other return values | |
5848 | * indicate a permanent error in copying the data. | |
5849 | * | |
5850 | * The actual amount of data copied will be returned in the | |
5851 | * "copy_size" argument. In the event that the destination map | |
5852 | * verification failed, this amount may be less than the amount | |
5853 | * requested. | |
5854 | */ | |
5855 | kern_return_t | |
5856 | vm_fault_copy( | |
0a7de745 A |
5857 | vm_object_t src_object, |
5858 | vm_object_offset_t src_offset, | |
5859 | vm_map_size_t *copy_size, /* INOUT */ | |
5860 | vm_object_t dst_object, | |
5861 | vm_object_offset_t dst_offset, | |
5862 | vm_map_t dst_map, | |
5863 | vm_map_version_t *dst_version, | |
5864 | int interruptible) | |
1c79356b | 5865 | { |
0a7de745 | 5866 | vm_page_t result_page; |
5ba3f43e | 5867 | |
0a7de745 A |
5868 | vm_page_t src_page; |
5869 | vm_page_t src_top_page; | |
5870 | vm_prot_t src_prot; | |
1c79356b | 5871 | |
0a7de745 A |
5872 | vm_page_t dst_page; |
5873 | vm_page_t dst_top_page; | |
5874 | vm_prot_t dst_prot; | |
1c79356b | 5875 | |
0a7de745 A |
5876 | vm_map_size_t amount_left; |
5877 | vm_object_t old_copy_object; | |
5878 | vm_object_t result_page_object = NULL; | |
5879 | kern_return_t error = 0; | |
5880 | vm_fault_return_t result; | |
1c79356b | 5881 | |
0a7de745 | 5882 | vm_map_size_t part_size; |
d9a64523 A |
5883 | struct vm_object_fault_info fault_info_src = {}; |
5884 | struct vm_object_fault_info fault_info_dst = {}; | |
1c79356b A |
5885 | |
5886 | /* | |
5887 | * In order not to confuse the clustered pageins, align | |
5888 | * the different offsets on a page boundary. | |
5889 | */ | |
1c79356b | 5890 | |
0a7de745 A |
5891 | #define RETURN(x) \ |
5892 | MACRO_BEGIN \ | |
5893 | *copy_size -= amount_left; \ | |
5894 | MACRO_RETURN(x); \ | |
1c79356b A |
5895 | MACRO_END |
5896 | ||
91447636 | 5897 | amount_left = *copy_size; |
2d21ac55 A |
5898 | |
5899 | fault_info_src.interruptible = interruptible; | |
5900 | fault_info_src.behavior = VM_BEHAVIOR_SEQUENTIAL; | |
2d21ac55 A |
5901 | fault_info_src.lo_offset = vm_object_trunc_page(src_offset); |
5902 | fault_info_src.hi_offset = fault_info_src.lo_offset + amount_left; | |
b0d623f7 | 5903 | fault_info_src.stealth = TRUE; |
2d21ac55 A |
5904 | |
5905 | fault_info_dst.interruptible = interruptible; | |
5906 | fault_info_dst.behavior = VM_BEHAVIOR_SEQUENTIAL; | |
2d21ac55 A |
5907 | fault_info_dst.lo_offset = vm_object_trunc_page(dst_offset); |
5908 | fault_info_dst.hi_offset = fault_info_dst.lo_offset + amount_left; | |
b0d623f7 | 5909 | fault_info_dst.stealth = TRUE; |
2d21ac55 | 5910 | |
1c79356b A |
5911 | do { /* while (amount_left > 0) */ |
5912 | /* | |
5913 | * There may be a deadlock if both source and destination | |
5914 | * pages are the same. To avoid this deadlock, the copy must | |
5915 | * start by getting the destination page in order to apply | |
5916 | * COW semantics if any. | |
5917 | */ | |
5918 | ||
0a7de745 | 5919 | RetryDestinationFault:; |
1c79356b | 5920 | |
0a7de745 | 5921 | dst_prot = VM_PROT_WRITE | VM_PROT_READ; |
1c79356b A |
5922 | |
5923 | vm_object_lock(dst_object); | |
5924 | vm_object_paging_begin(dst_object); | |
5925 | ||
d9a64523 A |
5926 | /* cap cluster size at maximum UPL size */ |
5927 | upl_size_t cluster_size; | |
5928 | if (os_convert_overflow(amount_left, &cluster_size)) { | |
5929 | cluster_size = 0 - (upl_size_t)PAGE_SIZE; | |
b0d623f7 | 5930 | } |
d9a64523 | 5931 | fault_info_dst.cluster_size = cluster_size; |
2d21ac55 | 5932 | |
39236c6e | 5933 | dst_page = VM_PAGE_NULL; |
b0d623f7 | 5934 | result = vm_fault_page(dst_object, |
0a7de745 A |
5935 | vm_object_trunc_page(dst_offset), |
5936 | VM_PROT_WRITE | VM_PROT_READ, | |
5937 | FALSE, | |
5938 | FALSE, /* page not looked up */ | |
5939 | &dst_prot, &dst_page, &dst_top_page, | |
5940 | (int *)0, | |
5941 | &error, | |
5942 | dst_map->no_zero_fill, | |
5943 | FALSE, &fault_info_dst); | |
b0d623f7 | 5944 | switch (result) { |
1c79356b A |
5945 | case VM_FAULT_SUCCESS: |
5946 | break; | |
5947 | case VM_FAULT_RETRY: | |
5948 | goto RetryDestinationFault; | |
5949 | case VM_FAULT_MEMORY_SHORTAGE: | |
0a7de745 | 5950 | if (vm_page_wait(interruptible)) { |
1c79356b | 5951 | goto RetryDestinationFault; |
0a7de745 A |
5952 | } |
5953 | /* fall thru */ | |
1c79356b A |
5954 | case VM_FAULT_INTERRUPTED: |
5955 | RETURN(MACH_SEND_INTERRUPTED); | |
b0d623f7 A |
5956 | case VM_FAULT_SUCCESS_NO_VM_PAGE: |
5957 | /* success but no VM page: fail the copy */ | |
5958 | vm_object_paging_end(dst_object); | |
5959 | vm_object_unlock(dst_object); | |
0a7de745 | 5960 | /*FALLTHROUGH*/ |
1c79356b | 5961 | case VM_FAULT_MEMORY_ERROR: |
0a7de745 A |
5962 | if (error) { |
5963 | return error; | |
5964 | } else { | |
5965 | return KERN_MEMORY_ERROR; | |
5966 | } | |
b0d623f7 A |
5967 | default: |
5968 | panic("vm_fault_copy: unexpected error 0x%x from " | |
0a7de745 | 5969 | "vm_fault_page()\n", result); |
1c79356b | 5970 | } |
0a7de745 | 5971 | assert((dst_prot & VM_PROT_WRITE) != VM_PROT_NONE); |
1c79356b | 5972 | |
39037602 A |
5973 | assert(dst_object == VM_PAGE_OBJECT(dst_page)); |
5974 | old_copy_object = dst_object->copy; | |
1c79356b A |
5975 | |
5976 | /* | |
5977 | * There exists the possiblity that the source and | |
5978 | * destination page are the same. But we can't | |
5979 | * easily determine that now. If they are the | |
5980 | * same, the call to vm_fault_page() for the | |
5981 | * destination page will deadlock. To prevent this we | |
5982 | * wire the page so we can drop busy without having | |
5ba3f43e | 5983 | * the page daemon steal the page. We clean up the |
1c79356b A |
5984 | * top page but keep the paging reference on the object |
5985 | * holding the dest page so it doesn't go away. | |
5986 | */ | |
5987 | ||
2d21ac55 | 5988 | vm_page_lockspin_queues(); |
3e170ce0 | 5989 | vm_page_wire(dst_page, VM_KERN_MEMORY_OSFMK, TRUE); |
1c79356b A |
5990 | vm_page_unlock_queues(); |
5991 | PAGE_WAKEUP_DONE(dst_page); | |
39037602 | 5992 | vm_object_unlock(dst_object); |
1c79356b A |
5993 | |
5994 | if (dst_top_page != VM_PAGE_NULL) { | |
5995 | vm_object_lock(dst_object); | |
5996 | VM_PAGE_FREE(dst_top_page); | |
5997 | vm_object_paging_end(dst_object); | |
5998 | vm_object_unlock(dst_object); | |
5999 | } | |
6000 | ||
0a7de745 | 6001 | RetrySourceFault:; |
1c79356b A |
6002 | |
6003 | if (src_object == VM_OBJECT_NULL) { | |
6004 | /* | |
6005 | * No source object. We will just | |
6006 | * zero-fill the page in dst_object. | |
6007 | */ | |
6008 | src_page = VM_PAGE_NULL; | |
e3027f41 | 6009 | result_page = VM_PAGE_NULL; |
1c79356b A |
6010 | } else { |
6011 | vm_object_lock(src_object); | |
6012 | src_page = vm_page_lookup(src_object, | |
0a7de745 | 6013 | vm_object_trunc_page(src_offset)); |
e3027f41 | 6014 | if (src_page == dst_page) { |
1c79356b | 6015 | src_prot = dst_prot; |
e3027f41 A |
6016 | result_page = VM_PAGE_NULL; |
6017 | } else { | |
1c79356b A |
6018 | src_prot = VM_PROT_READ; |
6019 | vm_object_paging_begin(src_object); | |
6020 | ||
d9a64523 A |
6021 | /* cap cluster size at maximum UPL size */ |
6022 | if (os_convert_overflow(amount_left, &cluster_size)) { | |
6023 | cluster_size = 0 - (upl_size_t)PAGE_SIZE; | |
b0d623f7 | 6024 | } |
d9a64523 | 6025 | fault_info_src.cluster_size = cluster_size; |
2d21ac55 | 6026 | |
39236c6e | 6027 | result_page = VM_PAGE_NULL; |
b0d623f7 | 6028 | result = vm_fault_page( |
5ba3f43e | 6029 | src_object, |
b0d623f7 A |
6030 | vm_object_trunc_page(src_offset), |
6031 | VM_PROT_READ, FALSE, | |
39236c6e | 6032 | FALSE, /* page not looked up */ |
5ba3f43e | 6033 | &src_prot, |
b0d623f7 A |
6034 | &result_page, &src_top_page, |
6035 | (int *)0, &error, FALSE, | |
6036 | FALSE, &fault_info_src); | |
6037 | ||
6038 | switch (result) { | |
1c79356b A |
6039 | case VM_FAULT_SUCCESS: |
6040 | break; | |
6041 | case VM_FAULT_RETRY: | |
6042 | goto RetrySourceFault; | |
6043 | case VM_FAULT_MEMORY_SHORTAGE: | |
0a7de745 | 6044 | if (vm_page_wait(interruptible)) { |
1c79356b | 6045 | goto RetrySourceFault; |
0a7de745 A |
6046 | } |
6047 | /* fall thru */ | |
1c79356b A |
6048 | case VM_FAULT_INTERRUPTED: |
6049 | vm_fault_copy_dst_cleanup(dst_page); | |
6050 | RETURN(MACH_SEND_INTERRUPTED); | |
b0d623f7 A |
6051 | case VM_FAULT_SUCCESS_NO_VM_PAGE: |
6052 | /* success but no VM page: fail */ | |
6053 | vm_object_paging_end(src_object); | |
6054 | vm_object_unlock(src_object); | |
0a7de745 | 6055 | /*FALLTHROUGH*/ |
1c79356b A |
6056 | case VM_FAULT_MEMORY_ERROR: |
6057 | vm_fault_copy_dst_cleanup(dst_page); | |
0a7de745 A |
6058 | if (error) { |
6059 | return error; | |
6060 | } else { | |
6061 | return KERN_MEMORY_ERROR; | |
6062 | } | |
b0d623f7 A |
6063 | default: |
6064 | panic("vm_fault_copy(2): unexpected " | |
0a7de745 A |
6065 | "error 0x%x from " |
6066 | "vm_fault_page()\n", result); | |
1c79356b A |
6067 | } |
6068 | ||
39037602 | 6069 | result_page_object = VM_PAGE_OBJECT(result_page); |
1c79356b | 6070 | assert((src_top_page == VM_PAGE_NULL) == |
0a7de745 | 6071 | (result_page_object == src_object)); |
1c79356b | 6072 | } |
0a7de745 | 6073 | assert((src_prot & VM_PROT_READ) != VM_PROT_NONE); |
39037602 | 6074 | vm_object_unlock(result_page_object); |
1c79356b A |
6075 | } |
6076 | ||
5ba3f43e A |
6077 | vm_map_lock_read(dst_map); |
6078 | ||
1c79356b | 6079 | if (!vm_map_verify(dst_map, dst_version)) { |
5ba3f43e | 6080 | vm_map_unlock_read(dst_map); |
0a7de745 | 6081 | if (result_page != VM_PAGE_NULL && src_page != dst_page) { |
e3027f41 | 6082 | vm_fault_copy_cleanup(result_page, src_top_page); |
0a7de745 | 6083 | } |
1c79356b A |
6084 | vm_fault_copy_dst_cleanup(dst_page); |
6085 | break; | |
6086 | } | |
39037602 | 6087 | assert(dst_object == VM_PAGE_OBJECT(dst_page)); |
1c79356b | 6088 | |
39037602 | 6089 | vm_object_lock(dst_object); |
1c79356b | 6090 | |
39037602 A |
6091 | if (dst_object->copy != old_copy_object) { |
6092 | vm_object_unlock(dst_object); | |
5ba3f43e | 6093 | vm_map_unlock_read(dst_map); |
0a7de745 | 6094 | if (result_page != VM_PAGE_NULL && src_page != dst_page) { |
e3027f41 | 6095 | vm_fault_copy_cleanup(result_page, src_top_page); |
0a7de745 | 6096 | } |
1c79356b A |
6097 | vm_fault_copy_dst_cleanup(dst_page); |
6098 | break; | |
6099 | } | |
39037602 | 6100 | vm_object_unlock(dst_object); |
1c79356b A |
6101 | |
6102 | /* | |
6103 | * Copy the page, and note that it is dirty | |
6104 | * immediately. | |
6105 | */ | |
6106 | ||
6107 | if (!page_aligned(src_offset) || | |
0a7de745 A |
6108 | !page_aligned(dst_offset) || |
6109 | !page_aligned(amount_left)) { | |
6110 | vm_object_offset_t src_po, | |
6111 | dst_po; | |
1c79356b | 6112 | |
91447636 A |
6113 | src_po = src_offset - vm_object_trunc_page(src_offset); |
6114 | dst_po = dst_offset - vm_object_trunc_page(dst_offset); | |
1c79356b A |
6115 | |
6116 | if (dst_po > src_po) { | |
6117 | part_size = PAGE_SIZE - dst_po; | |
6118 | } else { | |
6119 | part_size = PAGE_SIZE - src_po; | |
6120 | } | |
0a7de745 | 6121 | if (part_size > (amount_left)) { |
1c79356b A |
6122 | part_size = amount_left; |
6123 | } | |
6124 | ||
e3027f41 | 6125 | if (result_page == VM_PAGE_NULL) { |
b0d623f7 A |
6126 | assert((vm_offset_t) dst_po == dst_po); |
6127 | assert((vm_size_t) part_size == part_size); | |
1c79356b | 6128 | vm_page_part_zero_fill(dst_page, |
0a7de745 A |
6129 | (vm_offset_t) dst_po, |
6130 | (vm_size_t) part_size); | |
1c79356b | 6131 | } else { |
b0d623f7 A |
6132 | assert((vm_offset_t) src_po == src_po); |
6133 | assert((vm_offset_t) dst_po == dst_po); | |
6134 | assert((vm_size_t) part_size == part_size); | |
6135 | vm_page_part_copy(result_page, | |
0a7de745 A |
6136 | (vm_offset_t) src_po, |
6137 | dst_page, | |
6138 | (vm_offset_t) dst_po, | |
6139 | (vm_size_t)part_size); | |
6140 | if (!dst_page->vmp_dirty) { | |
1c79356b | 6141 | vm_object_lock(dst_object); |
316670eb | 6142 | SET_PAGE_DIRTY(dst_page, TRUE); |
39037602 | 6143 | vm_object_unlock(dst_object); |
1c79356b | 6144 | } |
1c79356b A |
6145 | } |
6146 | } else { | |
6147 | part_size = PAGE_SIZE; | |
6148 | ||
0a7de745 | 6149 | if (result_page == VM_PAGE_NULL) { |
1c79356b | 6150 | vm_page_zero_fill(dst_page); |
0a7de745 | 6151 | } else { |
39037602 | 6152 | vm_object_lock(result_page_object); |
e3027f41 | 6153 | vm_page_copy(result_page, dst_page); |
39037602 | 6154 | vm_object_unlock(result_page_object); |
316670eb | 6155 | |
0a7de745 | 6156 | if (!dst_page->vmp_dirty) { |
1c79356b | 6157 | vm_object_lock(dst_object); |
316670eb | 6158 | SET_PAGE_DIRTY(dst_page, TRUE); |
39037602 | 6159 | vm_object_unlock(dst_object); |
1c79356b A |
6160 | } |
6161 | } | |
1c79356b A |
6162 | } |
6163 | ||
6164 | /* | |
6165 | * Unlock everything, and return | |
6166 | */ | |
6167 | ||
5ba3f43e | 6168 | vm_map_unlock_read(dst_map); |
1c79356b | 6169 | |
0a7de745 | 6170 | if (result_page != VM_PAGE_NULL && src_page != dst_page) { |
e3027f41 | 6171 | vm_fault_copy_cleanup(result_page, src_top_page); |
0a7de745 | 6172 | } |
1c79356b A |
6173 | vm_fault_copy_dst_cleanup(dst_page); |
6174 | ||
6175 | amount_left -= part_size; | |
6176 | src_offset += part_size; | |
6177 | dst_offset += part_size; | |
6178 | } while (amount_left > 0); | |
6179 | ||
6180 | RETURN(KERN_SUCCESS); | |
0a7de745 | 6181 | #undef RETURN |
1c79356b | 6182 | |
5ba3f43e | 6183 | /*NOTREACHED*/ |
1c79356b A |
6184 | } |
6185 | ||
0a7de745 | 6186 | #if VM_FAULT_CLASSIFY |
1c79356b A |
6187 | /* |
6188 | * Temporary statistics gathering support. | |
6189 | */ | |
6190 | ||
6191 | /* | |
6192 | * Statistics arrays: | |
6193 | */ | |
0a7de745 A |
6194 | #define VM_FAULT_TYPES_MAX 5 |
6195 | #define VM_FAULT_LEVEL_MAX 8 | |
1c79356b | 6196 | |
0a7de745 | 6197 | int vm_fault_stats[VM_FAULT_TYPES_MAX][VM_FAULT_LEVEL_MAX]; |
1c79356b | 6198 | |
0a7de745 A |
6199 | #define VM_FAULT_TYPE_ZERO_FILL 0 |
6200 | #define VM_FAULT_TYPE_MAP_IN 1 | |
6201 | #define VM_FAULT_TYPE_PAGER 2 | |
6202 | #define VM_FAULT_TYPE_COPY 3 | |
6203 | #define VM_FAULT_TYPE_OTHER 4 | |
1c79356b A |
6204 | |
6205 | ||
6206 | void | |
0a7de745 A |
6207 | vm_fault_classify(vm_object_t object, |
6208 | vm_object_offset_t offset, | |
6209 | vm_prot_t fault_type) | |
1c79356b | 6210 | { |
0a7de745 A |
6211 | int type, level = 0; |
6212 | vm_page_t m; | |
1c79356b A |
6213 | |
6214 | while (TRUE) { | |
6215 | m = vm_page_lookup(object, offset); | |
5ba3f43e | 6216 | if (m != VM_PAGE_NULL) { |
0a7de745 | 6217 | if (m->vmp_busy || m->vmp_error || m->vmp_restart || m->vmp_absent) { |
1c79356b A |
6218 | type = VM_FAULT_TYPE_OTHER; |
6219 | break; | |
6220 | } | |
6221 | if (((fault_type & VM_PROT_WRITE) == 0) || | |
6222 | ((level == 0) && object->copy == VM_OBJECT_NULL)) { | |
6223 | type = VM_FAULT_TYPE_MAP_IN; | |
5ba3f43e | 6224 | break; |
1c79356b A |
6225 | } |
6226 | type = VM_FAULT_TYPE_COPY; | |
6227 | break; | |
0a7de745 | 6228 | } else { |
1c79356b A |
6229 | if (object->pager_created) { |
6230 | type = VM_FAULT_TYPE_PAGER; | |
6231 | break; | |
6232 | } | |
6233 | if (object->shadow == VM_OBJECT_NULL) { | |
6234 | type = VM_FAULT_TYPE_ZERO_FILL; | |
6235 | break; | |
0a7de745 | 6236 | } |
1c79356b | 6237 | |
6d2010ae | 6238 | offset += object->vo_shadow_offset; |
1c79356b A |
6239 | object = object->shadow; |
6240 | level++; | |
6241 | continue; | |
6242 | } | |
6243 | } | |
6244 | ||
0a7de745 | 6245 | if (level > VM_FAULT_LEVEL_MAX) { |
1c79356b | 6246 | level = VM_FAULT_LEVEL_MAX; |
0a7de745 | 6247 | } |
1c79356b A |
6248 | |
6249 | vm_fault_stats[type][level] += 1; | |
6250 | ||
6251 | return; | |
6252 | } | |
6253 | ||
6254 | /* cleanup routine to call from debugger */ | |
6255 | ||
6256 | void | |
6257 | vm_fault_classify_init(void) | |
6258 | { | |
6259 | int type, level; | |
6260 | ||
6261 | for (type = 0; type < VM_FAULT_TYPES_MAX; type++) { | |
6262 | for (level = 0; level < VM_FAULT_LEVEL_MAX; level++) { | |
6263 | vm_fault_stats[type][level] = 0; | |
6264 | } | |
6265 | } | |
6266 | ||
6267 | return; | |
6268 | } | |
0a7de745 | 6269 | #endif /* VM_FAULT_CLASSIFY */ |
2d21ac55 | 6270 | |
3e170ce0 | 6271 | vm_offset_t |
39037602 | 6272 | kdp_lightweight_fault(vm_map_t map, vm_offset_t cur_target_addr) |
3e170ce0 | 6273 | { |
0a7de745 A |
6274 | vm_map_entry_t entry; |
6275 | vm_object_t object; | |
6276 | vm_offset_t object_offset; | |
6277 | vm_page_t m; | |
6278 | int compressor_external_state, compressed_count_delta; | |
6279 | int compressor_flags = (C_DONT_BLOCK | C_KEEP | C_KDP); | |
6280 | int my_fault_type = VM_PROT_READ; | |
6281 | kern_return_t kr; | |
3e170ce0 | 6282 | |
3e170ce0 A |
6283 | if (not_in_kdp) { |
6284 | panic("kdp_lightweight_fault called from outside of debugger context"); | |
6285 | } | |
6286 | ||
6287 | assert(map != VM_MAP_NULL); | |
6288 | ||
6289 | assert((cur_target_addr & PAGE_MASK) == 0); | |
6290 | if ((cur_target_addr & PAGE_MASK) != 0) { | |
6291 | return 0; | |
6292 | } | |
6293 | ||
6294 | if (kdp_lck_rw_lock_is_acquired_exclusive(&map->lock)) { | |
6295 | return 0; | |
6296 | } | |
6297 | ||
6298 | if (!vm_map_lookup_entry(map, cur_target_addr, &entry)) { | |
6299 | return 0; | |
6300 | } | |
6301 | ||
6302 | if (entry->is_sub_map) { | |
6303 | return 0; | |
6304 | } | |
6305 | ||
6306 | object = VME_OBJECT(entry); | |
6307 | if (object == VM_OBJECT_NULL) { | |
6308 | return 0; | |
6309 | } | |
6310 | ||
6311 | object_offset = cur_target_addr - entry->vme_start + VME_OFFSET(entry); | |
6312 | ||
6313 | while (TRUE) { | |
6314 | if (kdp_lck_rw_lock_is_acquired_exclusive(&object->Lock)) { | |
6315 | return 0; | |
6316 | } | |
6317 | ||
6318 | if (object->pager_created && (object->paging_in_progress || | |
0a7de745 | 6319 | object->activity_in_progress)) { |
3e170ce0 A |
6320 | return 0; |
6321 | } | |
6322 | ||
6323 | m = kdp_vm_page_lookup(object, object_offset); | |
6324 | ||
6325 | if (m != VM_PAGE_NULL) { | |
3e170ce0 A |
6326 | if ((object->wimg_bits & VM_WIMG_MASK) != VM_WIMG_DEFAULT) { |
6327 | return 0; | |
6328 | } | |
6329 | ||
d9a64523 | 6330 | if (m->vmp_laundry || m->vmp_busy || m->vmp_free_when_done || m->vmp_absent || m->vmp_error || m->vmp_cleaning || |
0a7de745 | 6331 | m->vmp_overwriting || m->vmp_restart || m->vmp_unusual) { |
3e170ce0 A |
6332 | return 0; |
6333 | } | |
6334 | ||
d9a64523 A |
6335 | assert(!m->vmp_private); |
6336 | if (m->vmp_private) { | |
3e170ce0 A |
6337 | return 0; |
6338 | } | |
6339 | ||
d9a64523 A |
6340 | assert(!m->vmp_fictitious); |
6341 | if (m->vmp_fictitious) { | |
3e170ce0 A |
6342 | return 0; |
6343 | } | |
6344 | ||
d9a64523 A |
6345 | assert(m->vmp_q_state != VM_PAGE_USED_BY_COMPRESSOR); |
6346 | if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) { | |
3e170ce0 A |
6347 | return 0; |
6348 | } | |
2d21ac55 | 6349 | |
39037602 | 6350 | return ptoa(VM_PAGE_GET_PHYS_PAGE(m)); |
3e170ce0 A |
6351 | } |
6352 | ||
6353 | compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN; | |
6354 | ||
6355 | if (object->pager_created && MUST_ASK_PAGER(object, object_offset, compressor_external_state)) { | |
6356 | if (compressor_external_state == VM_EXTERNAL_STATE_EXISTS) { | |
6357 | kr = vm_compressor_pager_get(object->pager, (object_offset + object->paging_offset), | |
0a7de745 A |
6358 | kdp_compressor_decompressed_page_ppnum, &my_fault_type, |
6359 | compressor_flags, &compressed_count_delta); | |
3e170ce0 | 6360 | if (kr == KERN_SUCCESS) { |
3e170ce0 A |
6361 | return kdp_compressor_decompressed_page_paddr; |
6362 | } else { | |
6363 | return 0; | |
6364 | } | |
6365 | } | |
6366 | } | |
6367 | ||
6368 | if (object->shadow == VM_OBJECT_NULL) { | |
6369 | return 0; | |
6370 | } | |
6371 | ||
6372 | object_offset += object->vo_shadow_offset; | |
6373 | object = object->shadow; | |
6374 | } | |
39037602 | 6375 | } |
3e170ce0 | 6376 | |
d9a64523 A |
6377 | /* |
6378 | * vm_page_validate_cs_fast(): | |
6379 | * Performs a few quick checks to determine if the page's code signature | |
6380 | * really needs to be fully validated. It could: | |
6381 | * 1. have been modified (i.e. automatically tainted), | |
6382 | * 2. have already been validated, | |
6383 | * 3. have already been found to be tainted, | |
6384 | * 4. no longer have a backing store. | |
6385 | * Returns FALSE if the page needs to be fully validated. | |
6386 | */ | |
6387 | static boolean_t | |
6388 | vm_page_validate_cs_fast( | |
0a7de745 | 6389 | vm_page_t page) |
593a1d5f | 6390 | { |
0a7de745 | 6391 | vm_object_t object; |
593a1d5f | 6392 | |
39037602 | 6393 | object = VM_PAGE_OBJECT(page); |
d9a64523 | 6394 | vm_object_lock_assert_held(object); |
593a1d5f | 6395 | |
d9a64523 | 6396 | if (page->vmp_wpmapped && !page->vmp_cs_tainted) { |
593a1d5f A |
6397 | /* |
6398 | * This page was mapped for "write" access sometime in the | |
6399 | * past and could still be modifiable in the future. | |
6400 | * Consider it tainted. | |
6401 | * [ If the page was already found to be "tainted", no | |
6402 | * need to re-validate. ] | |
6403 | */ | |
d9a64523 A |
6404 | vm_object_lock_assert_exclusive(object); |
6405 | page->vmp_cs_validated = TRUE; | |
6406 | page->vmp_cs_tainted = TRUE; | |
593a1d5f | 6407 | if (cs_debug) { |
d9a64523 | 6408 | printf("CODESIGNING: %s: " |
0a7de745 A |
6409 | "page %p obj %p off 0x%llx " |
6410 | "was modified\n", | |
6411 | __FUNCTION__, | |
6412 | page, object, page->vmp_offset); | |
593a1d5f A |
6413 | } |
6414 | vm_cs_validated_dirtied++; | |
6415 | } | |
6416 | ||
d9a64523 A |
6417 | if (page->vmp_cs_validated || page->vmp_cs_tainted) { |
6418 | return TRUE; | |
593a1d5f | 6419 | } |
d9a64523 | 6420 | vm_object_lock_assert_exclusive(object); |
593a1d5f | 6421 | |
d9a64523 A |
6422 | #if CHECK_CS_VALIDATION_BITMAP |
6423 | kern_return_t kr; | |
593a1d5f | 6424 | |
d9a64523 A |
6425 | kr = vnode_pager_cs_check_validation_bitmap( |
6426 | object->pager, | |
6427 | page->vmp_offset + object->paging_offset, | |
6428 | CS_BITMAP_CHECK); | |
6429 | if (kr == KERN_SUCCESS) { | |
6430 | page->vmp_cs_validated = TRUE; | |
6431 | page->vmp_cs_tainted = FALSE; | |
6432 | vm_cs_bitmap_validated++; | |
6433 | return TRUE; | |
6434 | } | |
6435 | #endif /* CHECK_CS_VALIDATION_BITMAP */ | |
593a1d5f A |
6436 | |
6437 | if (!object->alive || object->terminating || object->pager == NULL) { | |
6438 | /* | |
6439 | * The object is terminating and we don't have its pager | |
6440 | * so we can't validate the data... | |
6441 | */ | |
d9a64523 | 6442 | return TRUE; |
593a1d5f | 6443 | } |
d9a64523 A |
6444 | |
6445 | /* we need to really validate this page */ | |
6446 | vm_object_lock_assert_exclusive(object); | |
6447 | return FALSE; | |
6448 | } | |
6449 | ||
6450 | void | |
6451 | vm_page_validate_cs_mapped_slow( | |
0a7de745 A |
6452 | vm_page_t page, |
6453 | const void *kaddr) | |
d9a64523 | 6454 | { |
0a7de745 A |
6455 | vm_object_t object; |
6456 | memory_object_offset_t mo_offset; | |
6457 | memory_object_t pager; | |
6458 | struct vnode *vnode; | |
6459 | boolean_t validated; | |
6460 | unsigned tainted; | |
d9a64523 A |
6461 | |
6462 | assert(page->vmp_busy); | |
6463 | object = VM_PAGE_OBJECT(page); | |
6464 | vm_object_lock_assert_exclusive(object); | |
6465 | ||
6466 | vm_cs_validates++; | |
6467 | ||
593a1d5f A |
6468 | /* |
6469 | * Since we get here to validate a page that was brought in by | |
6470 | * the pager, we know that this pager is all setup and ready | |
6471 | * by now. | |
6472 | */ | |
d9a64523 | 6473 | assert(object->code_signed); |
593a1d5f A |
6474 | assert(!object->internal); |
6475 | assert(object->pager != NULL); | |
6476 | assert(object->pager_ready); | |
6477 | ||
6478 | pager = object->pager; | |
b0d623f7 | 6479 | assert(object->paging_in_progress); |
39037602 | 6480 | vnode = vnode_pager_lookup_vnode(pager); |
d9a64523 | 6481 | mo_offset = page->vmp_offset + object->paging_offset; |
593a1d5f A |
6482 | |
6483 | /* verify the SHA1 hash for this page */ | |
39037602 A |
6484 | tainted = 0; |
6485 | validated = cs_validate_range(vnode, | |
0a7de745 A |
6486 | pager, |
6487 | mo_offset, | |
6488 | (const void *)((const char *)kaddr), | |
6489 | PAGE_SIZE_64, | |
6490 | &tainted); | |
39037602 A |
6491 | |
6492 | if (tainted & CS_VALIDATE_TAINTED) { | |
d9a64523 | 6493 | page->vmp_cs_tainted = TRUE; |
39037602 A |
6494 | } |
6495 | if (tainted & CS_VALIDATE_NX) { | |
d9a64523 | 6496 | page->vmp_cs_nx = TRUE; |
3e170ce0 | 6497 | } |
39037602 | 6498 | if (validated) { |
d9a64523 A |
6499 | page->vmp_cs_validated = TRUE; |
6500 | } | |
6501 | ||
6502 | #if CHECK_CS_VALIDATION_BITMAP | |
6503 | if (page->vmp_cs_validated && !page->vmp_cs_tainted) { | |
6504 | vnode_pager_cs_check_validation_bitmap(object->pager, | |
0a7de745 A |
6505 | mo_offset, |
6506 | CS_BITMAP_SET); | |
d9a64523 A |
6507 | } |
6508 | #endif /* CHECK_CS_VALIDATION_BITMAP */ | |
6509 | } | |
6510 | ||
6511 | void | |
6512 | vm_page_validate_cs_mapped( | |
0a7de745 A |
6513 | vm_page_t page, |
6514 | const void *kaddr) | |
d9a64523 A |
6515 | { |
6516 | if (!vm_page_validate_cs_fast(page)) { | |
6517 | vm_page_validate_cs_mapped_slow(page, kaddr); | |
593a1d5f A |
6518 | } |
6519 | } | |
6520 | ||
2d21ac55 A |
6521 | void |
6522 | vm_page_validate_cs( | |
0a7de745 | 6523 | vm_page_t page) |
2d21ac55 | 6524 | { |
0a7de745 A |
6525 | vm_object_t object; |
6526 | vm_object_offset_t offset; | |
6527 | vm_map_offset_t koffset; | |
6528 | vm_map_size_t ksize; | |
6529 | vm_offset_t kaddr; | |
6530 | kern_return_t kr; | |
6531 | boolean_t busy_page; | |
6532 | boolean_t need_unmap; | |
2d21ac55 | 6533 | |
39037602 A |
6534 | object = VM_PAGE_OBJECT(page); |
6535 | vm_object_lock_assert_held(object); | |
2d21ac55 | 6536 | |
d9a64523 | 6537 | if (vm_page_validate_cs_fast(page)) { |
6d2010ae A |
6538 | return; |
6539 | } | |
39037602 | 6540 | vm_object_lock_assert_exclusive(object); |
4a3eedf9 | 6541 | |
2d21ac55 | 6542 | assert(object->code_signed); |
d9a64523 | 6543 | offset = page->vmp_offset; |
2d21ac55 | 6544 | |
d9a64523 | 6545 | busy_page = page->vmp_busy; |
2d21ac55 A |
6546 | if (!busy_page) { |
6547 | /* keep page busy while we map (and unlock) the VM object */ | |
d9a64523 | 6548 | page->vmp_busy = TRUE; |
2d21ac55 | 6549 | } |
5ba3f43e | 6550 | |
2d21ac55 A |
6551 | /* |
6552 | * Take a paging reference on the VM object | |
6553 | * to protect it from collapse or bypass, | |
6554 | * and keep it from disappearing too. | |
6555 | */ | |
6556 | vm_object_paging_begin(object); | |
6557 | ||
6558 | /* map the page in the kernel address space */ | |
2d21ac55 | 6559 | ksize = PAGE_SIZE_64; |
39236c6e A |
6560 | koffset = 0; |
6561 | need_unmap = FALSE; | |
6562 | kr = vm_paging_map_object(page, | |
0a7de745 A |
6563 | object, |
6564 | offset, | |
6565 | VM_PROT_READ, | |
6566 | FALSE, /* can't unlock object ! */ | |
6567 | &ksize, | |
6568 | &koffset, | |
6569 | &need_unmap); | |
2d21ac55 | 6570 | if (kr != KERN_SUCCESS) { |
d9a64523 | 6571 | panic("%s: could not map page: 0x%x\n", __FUNCTION__, kr); |
2d21ac55 A |
6572 | } |
6573 | kaddr = CAST_DOWN(vm_offset_t, koffset); | |
6574 | ||
593a1d5f | 6575 | /* validate the mapped page */ |
d9a64523 | 6576 | vm_page_validate_cs_mapped_slow(page, (const void *) kaddr); |
2d21ac55 | 6577 | |
d9a64523 | 6578 | assert(page->vmp_busy); |
39037602 | 6579 | assert(object == VM_PAGE_OBJECT(page)); |
2d21ac55 A |
6580 | vm_object_lock_assert_exclusive(object); |
6581 | ||
2d21ac55 A |
6582 | if (!busy_page) { |
6583 | PAGE_WAKEUP_DONE(page); | |
6584 | } | |
39236c6e | 6585 | if (need_unmap) { |
2d21ac55 A |
6586 | /* unmap the map from the kernel address space */ |
6587 | vm_paging_unmap_object(object, koffset, koffset + ksize); | |
6588 | koffset = 0; | |
6589 | ksize = 0; | |
6590 | kaddr = 0; | |
6591 | } | |
6592 | vm_object_paging_end(object); | |
6593 | } | |
3e170ce0 A |
6594 | |
6595 | void | |
6596 | vm_page_validate_cs_mapped_chunk( | |
0a7de745 A |
6597 | vm_page_t page, |
6598 | const void *kaddr, | |
6599 | vm_offset_t chunk_offset, | |
6600 | vm_size_t chunk_size, | |
6601 | boolean_t *validated_p, | |
6602 | unsigned *tainted_p) | |
3e170ce0 | 6603 | { |
0a7de745 A |
6604 | vm_object_t object; |
6605 | vm_object_offset_t offset, offset_in_page; | |
6606 | memory_object_t pager; | |
6607 | struct vnode *vnode; | |
6608 | boolean_t validated; | |
6609 | unsigned tainted; | |
3e170ce0 A |
6610 | |
6611 | *validated_p = FALSE; | |
6612 | *tainted_p = 0; | |
6613 | ||
d9a64523 | 6614 | assert(page->vmp_busy); |
39037602 A |
6615 | object = VM_PAGE_OBJECT(page); |
6616 | vm_object_lock_assert_exclusive(object); | |
3e170ce0 | 6617 | |
3e170ce0 | 6618 | assert(object->code_signed); |
d9a64523 | 6619 | offset = page->vmp_offset; |
3e170ce0 A |
6620 | |
6621 | if (!object->alive || object->terminating || object->pager == NULL) { | |
6622 | /* | |
6623 | * The object is terminating and we don't have its pager | |
6624 | * so we can't validate the data... | |
6625 | */ | |
6626 | return; | |
6627 | } | |
6628 | /* | |
6629 | * Since we get here to validate a page that was brought in by | |
6630 | * the pager, we know that this pager is all setup and ready | |
6631 | * by now. | |
6632 | */ | |
6633 | assert(!object->internal); | |
6634 | assert(object->pager != NULL); | |
6635 | assert(object->pager_ready); | |
6636 | ||
6637 | pager = object->pager; | |
6638 | assert(object->paging_in_progress); | |
39037602 | 6639 | vnode = vnode_pager_lookup_vnode(pager); |
3e170ce0 A |
6640 | |
6641 | /* verify the signature for this chunk */ | |
6642 | offset_in_page = chunk_offset; | |
6643 | assert(offset_in_page < PAGE_SIZE); | |
3e170ce0 A |
6644 | |
6645 | tainted = 0; | |
39037602 | 6646 | validated = cs_validate_range(vnode, |
0a7de745 A |
6647 | pager, |
6648 | (object->paging_offset + | |
6649 | offset + | |
6650 | offset_in_page), | |
6651 | (const void *)((const char *)kaddr | |
6652 | + offset_in_page), | |
6653 | chunk_size, | |
6654 | &tainted); | |
3e170ce0 A |
6655 | if (validated) { |
6656 | *validated_p = TRUE; | |
6657 | } | |
6658 | if (tainted) { | |
6659 | *tainted_p = tainted; | |
6660 | } | |
6661 | } | |
d9a64523 | 6662 | |
0a7de745 A |
6663 | static void |
6664 | vm_rtfrecord_lock(void) | |
6665 | { | |
d9a64523 A |
6666 | lck_spin_lock(&vm_rtfr_slock); |
6667 | } | |
6668 | ||
0a7de745 A |
6669 | static void |
6670 | vm_rtfrecord_unlock(void) | |
6671 | { | |
d9a64523 A |
6672 | lck_spin_unlock(&vm_rtfr_slock); |
6673 | } | |
6674 | ||
0a7de745 A |
6675 | unsigned int |
6676 | vmrtfaultinfo_bufsz(void) | |
6677 | { | |
6678 | return vmrtf_num_records * sizeof(vm_rtfault_record_t); | |
d9a64523 A |
6679 | } |
6680 | ||
6681 | #include <kern/backtrace.h> | |
6682 | ||
0a7de745 A |
6683 | static void |
6684 | vm_record_rtfault(thread_t cthread, uint64_t fstart, vm_map_offset_t fault_vaddr, int type_of_fault) | |
6685 | { | |
d9a64523 A |
6686 | uint64_t fend = mach_continuous_time(); |
6687 | ||
6688 | uint64_t cfpc = 0; | |
6689 | uint64_t ctid = cthread->thread_id; | |
6690 | uint64_t cupid = get_current_unique_pid(); | |
6691 | ||
6692 | uintptr_t bpc = 0; | |
ea3f0419 | 6693 | int btr = 0; |
d9a64523 A |
6694 | bool u64 = false; |
6695 | ||
6696 | /* Capture a single-frame backtrace; this extracts just the program | |
6697 | * counter at the point of the fault into "bpc", and should perform no | |
6698 | * further user stack traversals, thus avoiding copyin()s and further | |
6699 | * faults. | |
6700 | */ | |
ea3f0419 | 6701 | unsigned int bfrs = backtrace_thread_user(cthread, &bpc, 1U, &btr, &u64, NULL); |
d9a64523 A |
6702 | |
6703 | if ((btr == 0) && (bfrs > 0)) { | |
6704 | cfpc = bpc; | |
6705 | } | |
6706 | ||
6707 | assert((fstart != 0) && fend >= fstart); | |
6708 | vm_rtfrecord_lock(); | |
6709 | assert(vmrtfrs.vmrtfr_curi <= vmrtfrs.vmrtfr_maxi); | |
6710 | ||
6711 | vmrtfrs.vmrtf_total++; | |
6712 | vm_rtfault_record_t *cvmr = &vmrtfrs.vm_rtf_records[vmrtfrs.vmrtfr_curi++]; | |
6713 | ||
6714 | cvmr->rtfabstime = fstart; | |
6715 | cvmr->rtfduration = fend - fstart; | |
6716 | cvmr->rtfaddr = fault_vaddr; | |
6717 | cvmr->rtfpc = cfpc; | |
6718 | cvmr->rtftype = type_of_fault; | |
6719 | cvmr->rtfupid = cupid; | |
6720 | cvmr->rtftid = ctid; | |
6721 | ||
6722 | if (vmrtfrs.vmrtfr_curi > vmrtfrs.vmrtfr_maxi) { | |
6723 | vmrtfrs.vmrtfr_curi = 0; | |
6724 | } | |
6725 | ||
6726 | vm_rtfrecord_unlock(); | |
6727 | } | |
6728 | ||
0a7de745 A |
6729 | int |
6730 | vmrtf_extract(uint64_t cupid, __unused boolean_t isroot, int vrecordsz, void *vrecords, int *vmrtfrv) | |
6731 | { | |
d9a64523 A |
6732 | vm_rtfault_record_t *cvmrd = vrecords; |
6733 | size_t residue = vrecordsz; | |
6734 | int numextracted = 0; | |
6735 | boolean_t early_exit = FALSE; | |
6736 | ||
6737 | vm_rtfrecord_lock(); | |
6738 | ||
6739 | for (int vmfi = 0; vmfi <= vmrtfrs.vmrtfr_maxi; vmfi++) { | |
d9a64523 A |
6740 | if (residue < sizeof(vm_rtfault_record_t)) { |
6741 | early_exit = TRUE; | |
6742 | break; | |
6743 | } | |
6744 | ||
6745 | if (vmrtfrs.vm_rtf_records[vmfi].rtfupid != cupid) { | |
0a7de745 | 6746 | #if DEVELOPMENT || DEBUG |
d9a64523 A |
6747 | if (isroot == FALSE) { |
6748 | continue; | |
6749 | } | |
6750 | #else | |
6751 | continue; | |
6752 | #endif /* DEVDEBUG */ | |
6753 | } | |
6754 | ||
6755 | *cvmrd = vmrtfrs.vm_rtf_records[vmfi]; | |
6756 | cvmrd++; | |
6757 | residue -= sizeof(vm_rtfault_record_t); | |
6758 | numextracted++; | |
6759 | } | |
6760 | ||
6761 | vm_rtfrecord_unlock(); | |
6762 | ||
6763 | *vmrtfrv = numextracted; | |
0a7de745 | 6764 | return early_exit; |
d9a64523 | 6765 | } |