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