]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2000-2020 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
0a7de745 | 31 | /* |
1c79356b A |
32 | * Mach Operating System |
33 | * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 45 | * |
1c79356b | 46 | * Carnegie Mellon requests users of this software to return to |
0a7de745 | 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 | |
0a7de745 | 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/vm_page.c | |
60 | * Author: Avadis Tevanian, Jr., Michael Wayne Young | |
61 | * | |
62 | * Resident memory management module. | |
63 | */ | |
64 | ||
91447636 | 65 | #include <debug.h> |
2d21ac55 | 66 | #include <libkern/OSAtomic.h> |
3e170ce0 | 67 | #include <libkern/OSDebug.h> |
91447636 | 68 | |
9bccf70c | 69 | #include <mach/clock_types.h> |
1c79356b A |
70 | #include <mach/vm_prot.h> |
71 | #include <mach/vm_statistics.h> | |
2d21ac55 | 72 | #include <mach/sdt.h> |
1c79356b | 73 | #include <kern/counters.h> |
f427ee49 | 74 | #include <kern/host_statistics.h> |
1c79356b | 75 | #include <kern/sched_prim.h> |
39037602 | 76 | #include <kern/policy_internal.h> |
1c79356b A |
77 | #include <kern/task.h> |
78 | #include <kern/thread.h> | |
b0d623f7 | 79 | #include <kern/kalloc.h> |
f427ee49 | 80 | #include <kern/zalloc_internal.h> |
fe8ab488 | 81 | #include <kern/ledger.h> |
1c79356b A |
82 | #include <vm/pmap.h> |
83 | #include <vm/vm_init.h> | |
84 | #include <vm/vm_map.h> | |
85 | #include <vm/vm_page.h> | |
86 | #include <vm/vm_pageout.h> | |
0a7de745 | 87 | #include <vm/vm_kern.h> /* kernel_memory_allocate() */ |
1c79356b | 88 | #include <kern/misc_protos.h> |
3e170ce0 | 89 | #include <mach_debug/zone_info.h> |
1c79356b | 90 | #include <vm/cpm.h> |
6d2010ae | 91 | #include <pexpert/pexpert.h> |
5ba3f43e | 92 | #include <san/kasan.h> |
55e303ae | 93 | |
91447636 | 94 | #include <vm/vm_protos.h> |
2d21ac55 A |
95 | #include <vm/memory_object.h> |
96 | #include <vm/vm_purgeable_internal.h> | |
39236c6e | 97 | #include <vm/vm_compressor.h> |
cb323159 A |
98 | #if defined (__x86_64__) |
99 | #include <i386/misc_protos.h> | |
100 | #endif | |
2d21ac55 | 101 | |
fe8ab488 A |
102 | #if CONFIG_PHANTOM_CACHE |
103 | #include <vm/vm_phantom_cache.h> | |
104 | #endif | |
105 | ||
f427ee49 | 106 | #if HIBERNATION |
b0d623f7 | 107 | #include <IOKit/IOHibernatePrivate.h> |
f427ee49 A |
108 | #include <machine/pal_hibernate.h> |
109 | #endif /* HIBERNATION */ | |
b0d623f7 | 110 | |
b0d623f7 A |
111 | #include <sys/kdebug.h> |
112 | ||
cb323159 A |
113 | #if defined(HAS_APPLE_PAC) |
114 | #include <ptrauth.h> | |
115 | #endif | |
f427ee49 A |
116 | #if defined(__arm64__) |
117 | #include <arm/cpu_internal.h> | |
118 | #endif /* defined(__arm64__) */ | |
39037602 | 119 | |
0a7de745 A |
120 | #if MACH_ASSERT |
121 | ||
122 | #define ASSERT_PMAP_FREE(mem) pmap_assert_free(VM_PAGE_GET_PHYS_PAGE(mem)) | |
123 | ||
124 | #else /* MACH_ASSERT */ | |
125 | ||
126 | #define ASSERT_PMAP_FREE(mem) /* nothing */ | |
d9a64523 | 127 | |
0a7de745 A |
128 | #endif /* MACH_ASSERT */ |
129 | ||
cb323159 A |
130 | extern boolean_t vm_pageout_running; |
131 | extern thread_t vm_pageout_scan_thread; | |
132 | extern boolean_t vps_dynamic_priority_enabled; | |
133 | ||
0a7de745 A |
134 | char vm_page_inactive_states[VM_PAGE_Q_STATE_ARRAY_SIZE]; |
135 | char vm_page_pageable_states[VM_PAGE_Q_STATE_ARRAY_SIZE]; | |
136 | char vm_page_non_speculative_pageable_states[VM_PAGE_Q_STATE_ARRAY_SIZE]; | |
137 | char vm_page_active_or_inactive_states[VM_PAGE_Q_STATE_ARRAY_SIZE]; | |
39037602 A |
138 | |
139 | #if CONFIG_SECLUDED_MEMORY | |
140 | struct vm_page_secluded_data vm_page_secluded; | |
141 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
142 | ||
f427ee49 A |
143 | #if DEVELOPMENT || DEBUG |
144 | extern struct memory_object_pager_ops shared_region_pager_ops; | |
145 | unsigned int shared_region_pagers_resident_count = 0; | |
146 | unsigned int shared_region_pagers_resident_peak = 0; | |
147 | #endif /* DEVELOPMENT || DEBUG */ | |
148 | ||
149 | int PERCPU_DATA(start_color); | |
150 | vm_page_t PERCPU_DATA(free_pages); | |
0a7de745 A |
151 | boolean_t hibernate_cleaning_in_progress = FALSE; |
152 | boolean_t vm_page_free_verify = TRUE; | |
b0d623f7 | 153 | |
0a7de745 A |
154 | uint32_t vm_lopage_free_count = 0; |
155 | uint32_t vm_lopage_free_limit = 0; | |
156 | uint32_t vm_lopage_lowater = 0; | |
157 | boolean_t vm_lopage_refill = FALSE; | |
158 | boolean_t vm_lopage_needed = FALSE; | |
0b4c1975 | 159 | |
0a7de745 A |
160 | lck_mtx_ext_t vm_page_queue_lock_ext; |
161 | lck_mtx_ext_t vm_page_queue_free_lock_ext; | |
162 | lck_mtx_ext_t vm_purgeable_queue_lock_ext; | |
2d21ac55 | 163 | |
0a7de745 A |
164 | int speculative_age_index = 0; |
165 | int speculative_steal_index = 0; | |
2d21ac55 A |
166 | struct vm_speculative_age_q vm_page_queue_speculative[VM_PAGE_MAX_SPECULATIVE_AGE_Q + 1]; |
167 | ||
0a7de745 A |
168 | boolean_t hibernation_vmqueues_inspection = FALSE; /* Tracks if the hibernation code is looking at the VM queues. |
169 | * Updated and checked behind the vm_page_queues_lock. */ | |
0b4e3aa0 | 170 | |
0a7de745 A |
171 | static void vm_page_free_prepare(vm_page_t page); |
172 | static vm_page_t vm_page_grab_fictitious_common(ppnum_t phys_addr); | |
6d2010ae | 173 | |
3e170ce0 | 174 | static void vm_tag_init(void); |
b0d623f7 | 175 | |
f427ee49 A |
176 | /* for debugging purposes */ |
177 | SECURITY_READ_ONLY_EARLY(vm_packing_params_t) vm_page_packing_params = | |
178 | VM_PACKING_PARAMS(VM_PAGE_PACKED_PTR); | |
b0d623f7 | 179 | |
1c79356b A |
180 | /* |
181 | * Associated with page of user-allocatable memory is a | |
182 | * page structure. | |
183 | */ | |
184 | ||
185 | /* | |
186 | * These variables record the values returned by vm_page_bootstrap, | |
187 | * for debugging purposes. The implementation of pmap_steal_memory | |
188 | * and pmap_startup here also uses them internally. | |
189 | */ | |
190 | ||
191 | vm_offset_t virtual_space_start; | |
192 | vm_offset_t virtual_space_end; | |
0a7de745 | 193 | uint32_t vm_page_pages; |
1c79356b A |
194 | |
195 | /* | |
196 | * The vm_page_lookup() routine, which provides for fast | |
197 | * (virtual memory object, offset) to page lookup, employs | |
198 | * the following hash table. The vm_page_{insert,remove} | |
199 | * routines install and remove associations in the table. | |
200 | * [This table is often called the virtual-to-physical, | |
201 | * or VP, table.] | |
202 | */ | |
203 | typedef struct { | |
fe8ab488 | 204 | vm_page_packed_t page_list; |
0a7de745 A |
205 | #if MACH_PAGE_HASH_STATS |
206 | int cur_count; /* current count */ | |
207 | int hi_count; /* high water mark */ | |
1c79356b A |
208 | #endif /* MACH_PAGE_HASH_STATS */ |
209 | } vm_page_bucket_t; | |
210 | ||
b0d623f7 | 211 | |
0a7de745 | 212 | #define BUCKETS_PER_LOCK 16 |
b0d623f7 | 213 | |
0a7de745 A |
214 | vm_page_bucket_t *vm_page_buckets; /* Array of buckets */ |
215 | unsigned int vm_page_bucket_count = 0; /* How big is array? */ | |
216 | unsigned int vm_page_hash_mask; /* Mask for hash function */ | |
217 | unsigned int vm_page_hash_shift; /* Shift for hash function */ | |
218 | uint32_t vm_page_bucket_hash; /* Basic bucket hash */ | |
219 | unsigned int vm_page_bucket_lock_count = 0; /* How big is array of locks? */ | |
b0d623f7 | 220 | |
5ba3f43e A |
221 | #ifndef VM_TAG_ACTIVE_UPDATE |
222 | #error VM_TAG_ACTIVE_UPDATE | |
223 | #endif | |
224 | #ifndef VM_MAX_TAG_ZONES | |
225 | #error VM_MAX_TAG_ZONES | |
226 | #endif | |
227 | ||
228 | boolean_t vm_tag_active_update = VM_TAG_ACTIVE_UPDATE; | |
0a7de745 | 229 | lck_spin_t *vm_page_bucket_locks; |
1c79356b | 230 | |
5ba3f43e A |
231 | vm_allocation_site_t vm_allocation_sites_static[VM_KERN_MEMORY_FIRST_DYNAMIC + 1]; |
232 | vm_allocation_site_t * vm_allocation_sites[VM_MAX_TAG_VALUE]; | |
233 | #if VM_MAX_TAG_ZONES | |
234 | vm_allocation_zone_total_t ** vm_allocation_zone_totals; | |
235 | #endif /* VM_MAX_TAG_ZONES */ | |
236 | ||
237 | vm_tag_t vm_allocation_tag_highest; | |
238 | ||
15129b1c A |
239 | #if VM_PAGE_BUCKETS_CHECK |
240 | boolean_t vm_page_buckets_check_ready = FALSE; | |
241 | #if VM_PAGE_FAKE_BUCKETS | |
0a7de745 | 242 | vm_page_bucket_t *vm_page_fake_buckets; /* decoy buckets */ |
15129b1c A |
243 | vm_map_offset_t vm_page_fake_buckets_start, vm_page_fake_buckets_end; |
244 | #endif /* VM_PAGE_FAKE_BUCKETS */ | |
245 | #endif /* VM_PAGE_BUCKETS_CHECK */ | |
91447636 | 246 | |
3e170ce0 A |
247 | |
248 | ||
0a7de745 | 249 | #if MACH_PAGE_HASH_STATS |
1c79356b A |
250 | /* This routine is only for debug. It is intended to be called by |
251 | * hand by a developer using a kernel debugger. This routine prints | |
252 | * out vm_page_hash table statistics to the kernel debug console. | |
253 | */ | |
254 | void | |
255 | hash_debug(void) | |
256 | { | |
0a7de745 A |
257 | int i; |
258 | int numbuckets = 0; | |
259 | int highsum = 0; | |
260 | int maxdepth = 0; | |
1c79356b A |
261 | |
262 | for (i = 0; i < vm_page_bucket_count; i++) { | |
263 | if (vm_page_buckets[i].hi_count) { | |
264 | numbuckets++; | |
265 | highsum += vm_page_buckets[i].hi_count; | |
0a7de745 | 266 | if (vm_page_buckets[i].hi_count > maxdepth) { |
1c79356b | 267 | maxdepth = vm_page_buckets[i].hi_count; |
0a7de745 | 268 | } |
1c79356b A |
269 | } |
270 | } | |
271 | printf("Total number of buckets: %d\n", vm_page_bucket_count); | |
272 | printf("Number used buckets: %d = %d%%\n", | |
0a7de745 | 273 | numbuckets, 100 * numbuckets / vm_page_bucket_count); |
1c79356b | 274 | printf("Number unused buckets: %d = %d%%\n", |
0a7de745 A |
275 | vm_page_bucket_count - numbuckets, |
276 | 100 * (vm_page_bucket_count - numbuckets) / vm_page_bucket_count); | |
1c79356b A |
277 | printf("Sum of bucket max depth: %d\n", highsum); |
278 | printf("Average bucket depth: %d.%2d\n", | |
0a7de745 A |
279 | highsum / vm_page_bucket_count, |
280 | highsum % vm_page_bucket_count); | |
1c79356b A |
281 | printf("Maximum bucket depth: %d\n", maxdepth); |
282 | } | |
283 | #endif /* MACH_PAGE_HASH_STATS */ | |
284 | ||
285 | /* | |
286 | * The virtual page size is currently implemented as a runtime | |
287 | * variable, but is constant once initialized using vm_set_page_size. | |
288 | * This initialization must be done in the machine-dependent | |
289 | * bootstrap sequence, before calling other machine-independent | |
290 | * initializations. | |
291 | * | |
292 | * All references to the virtual page size outside this | |
293 | * module must use the PAGE_SIZE, PAGE_MASK and PAGE_SHIFT | |
294 | * constants. | |
295 | */ | |
5ba3f43e | 296 | #if defined(__arm__) || defined(__arm64__) |
0a7de745 A |
297 | vm_size_t page_size; |
298 | vm_size_t page_mask; | |
299 | int page_shift; | |
5ba3f43e | 300 | #else |
0a7de745 A |
301 | vm_size_t page_size = PAGE_SIZE; |
302 | vm_size_t page_mask = PAGE_MASK; | |
303 | int page_shift = PAGE_SHIFT; | |
5ba3f43e | 304 | #endif |
1c79356b | 305 | |
f427ee49 A |
306 | SECURITY_READ_ONLY_LATE(vm_page_t) vm_pages = VM_PAGE_NULL; |
307 | SECURITY_READ_ONLY_LATE(vm_page_t) vm_page_array_beginning_addr; | |
308 | vm_page_t vm_page_array_ending_addr; | |
39037602 | 309 | |
0a7de745 | 310 | unsigned int vm_pages_count = 0; |
2d21ac55 | 311 | |
1c79356b A |
312 | /* |
313 | * Resident pages that represent real memory | |
2d21ac55 A |
314 | * are allocated from a set of free lists, |
315 | * one per color. | |
1c79356b | 316 | */ |
0a7de745 A |
317 | unsigned int vm_colors; |
318 | unsigned int vm_color_mask; /* mask is == (vm_colors-1) */ | |
319 | unsigned int vm_cache_geometry_colors = 0; /* set by hw dependent code during startup */ | |
320 | unsigned int vm_free_magazine_refill_limit = 0; | |
39037602 A |
321 | |
322 | ||
323 | struct vm_page_queue_free_head { | |
0a7de745 | 324 | vm_page_queue_head_t qhead; |
f427ee49 | 325 | } VM_PAGE_PACKED_ALIGNED; |
39037602 | 326 | |
0a7de745 | 327 | struct vm_page_queue_free_head vm_page_queue_free[MAX_COLORS]; |
39037602 A |
328 | |
329 | ||
0a7de745 A |
330 | unsigned int vm_page_free_wanted; |
331 | unsigned int vm_page_free_wanted_privileged; | |
39037602 | 332 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 333 | unsigned int vm_page_free_wanted_secluded; |
39037602 | 334 | #endif /* CONFIG_SECLUDED_MEMORY */ |
0a7de745 | 335 | unsigned int vm_page_free_count; |
1c79356b | 336 | |
1c79356b A |
337 | /* |
338 | * Occasionally, the virtual memory system uses | |
339 | * resident page structures that do not refer to | |
340 | * real pages, for example to leave a page with | |
341 | * important state information in the VP table. | |
342 | * | |
343 | * These page structures are allocated the way | |
344 | * most other kernel structures are. | |
345 | */ | |
f427ee49 | 346 | SECURITY_READ_ONLY_LATE(zone_t) vm_page_zone; |
b0d623f7 | 347 | vm_locks_array_t vm_page_locks; |
316670eb | 348 | |
f427ee49 A |
349 | LCK_ATTR_DECLARE(vm_page_lck_attr, 0, 0); |
350 | LCK_GRP_DECLARE(vm_page_lck_grp_free, "vm_page_free"); | |
351 | LCK_GRP_DECLARE(vm_page_lck_grp_queue, "vm_page_queue"); | |
352 | LCK_GRP_DECLARE(vm_page_lck_grp_local, "vm_page_queue_local"); | |
353 | LCK_GRP_DECLARE(vm_page_lck_grp_purge, "vm_page_purge"); | |
354 | LCK_GRP_DECLARE(vm_page_lck_grp_alloc, "vm_page_alloc"); | |
355 | LCK_GRP_DECLARE(vm_page_lck_grp_bucket, "vm_page_bucket"); | |
356 | LCK_MTX_EARLY_DECLARE_ATTR(vm_page_alloc_lock, &vm_page_lck_grp_alloc, &vm_page_lck_attr); | |
357 | LCK_SPIN_DECLARE_ATTR(vm_objects_wired_lock, &vm_page_lck_grp_bucket, &vm_page_lck_attr); | |
358 | LCK_SPIN_DECLARE_ATTR(vm_allocation_sites_lock, &vm_page_lck_grp_bucket, &vm_page_lck_attr); | |
359 | ||
0a7de745 A |
360 | unsigned int vm_page_local_q_soft_limit = 250; |
361 | unsigned int vm_page_local_q_hard_limit = 500; | |
f427ee49 | 362 | struct vpl *__zpercpu vm_page_local_q; |
b0d623f7 | 363 | |
316670eb A |
364 | /* N.B. Guard and fictitious pages must not |
365 | * be assigned a zero phys_page value. | |
366 | */ | |
1c79356b A |
367 | /* |
368 | * Fictitious pages don't have a physical address, | |
55e303ae | 369 | * but we must initialize phys_page to something. |
1c79356b A |
370 | * For debugging, this should be a strange value |
371 | * that the pmap module can recognize in assertions. | |
372 | */ | |
5ba3f43e | 373 | const ppnum_t vm_page_fictitious_addr = (ppnum_t) -1; |
1c79356b | 374 | |
2d21ac55 A |
375 | /* |
376 | * Guard pages are not accessible so they don't | |
0a7de745 | 377 | * need a physical address, but we need to enter |
2d21ac55 A |
378 | * one in the pmap. |
379 | * Let's make it recognizable and make sure that | |
380 | * we don't use a real physical page with that | |
381 | * physical address. | |
382 | */ | |
5ba3f43e | 383 | const ppnum_t vm_page_guard_addr = (ppnum_t) -2; |
2d21ac55 | 384 | |
1c79356b A |
385 | /* |
386 | * Resident page structures are also chained on | |
387 | * queues that are used by the page replacement | |
388 | * system (pageout daemon). These queues are | |
389 | * defined here, but are shared by the pageout | |
0a7de745 A |
390 | * module. The inactive queue is broken into |
391 | * file backed and anonymous for convenience as the | |
392 | * pageout daemon often assignes a higher | |
39236c6e | 393 | * importance to anonymous pages (less likely to pick) |
1c79356b | 394 | */ |
f427ee49 A |
395 | vm_page_queue_head_t vm_page_queue_active VM_PAGE_PACKED_ALIGNED; |
396 | vm_page_queue_head_t vm_page_queue_inactive VM_PAGE_PACKED_ALIGNED; | |
39037602 | 397 | #if CONFIG_SECLUDED_MEMORY |
f427ee49 | 398 | vm_page_queue_head_t vm_page_queue_secluded VM_PAGE_PACKED_ALIGNED; |
39037602 | 399 | #endif /* CONFIG_SECLUDED_MEMORY */ |
f427ee49 A |
400 | vm_page_queue_head_t vm_page_queue_anonymous VM_PAGE_PACKED_ALIGNED; /* inactive memory queue for anonymous pages */ |
401 | vm_page_queue_head_t vm_page_queue_throttled VM_PAGE_PACKED_ALIGNED; | |
2d21ac55 | 402 | |
0a7de745 | 403 | queue_head_t vm_objects_wired; |
3e170ce0 | 404 | |
d9a64523 A |
405 | void vm_update_darkwake_mode(boolean_t); |
406 | ||
39037602 | 407 | #if CONFIG_BACKGROUND_QUEUE |
f427ee49 | 408 | vm_page_queue_head_t vm_page_queue_background VM_PAGE_PACKED_ALIGNED; |
0a7de745 A |
409 | uint32_t vm_page_background_target; |
410 | uint32_t vm_page_background_target_snapshot; | |
411 | uint32_t vm_page_background_count; | |
412 | uint64_t vm_page_background_promoted_count; | |
39037602 | 413 | |
0a7de745 A |
414 | uint32_t vm_page_background_internal_count; |
415 | uint32_t vm_page_background_external_count; | |
39037602 | 416 | |
0a7de745 A |
417 | uint32_t vm_page_background_mode; |
418 | uint32_t vm_page_background_exclude_external; | |
39037602 A |
419 | #endif |
420 | ||
0a7de745 A |
421 | unsigned int vm_page_active_count; |
422 | unsigned int vm_page_inactive_count; | |
f427ee49 | 423 | unsigned int vm_page_kernelcache_count; |
39037602 | 424 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 A |
425 | unsigned int vm_page_secluded_count; |
426 | unsigned int vm_page_secluded_count_free; | |
427 | unsigned int vm_page_secluded_count_inuse; | |
cb323159 | 428 | unsigned int vm_page_secluded_count_over_target; |
39037602 | 429 | #endif /* CONFIG_SECLUDED_MEMORY */ |
0a7de745 A |
430 | unsigned int vm_page_anonymous_count; |
431 | unsigned int vm_page_throttled_count; | |
432 | unsigned int vm_page_speculative_count; | |
433 | ||
434 | unsigned int vm_page_wire_count; | |
435 | unsigned int vm_page_wire_count_on_boot = 0; | |
436 | unsigned int vm_page_stolen_count = 0; | |
437 | unsigned int vm_page_wire_count_initial; | |
438 | unsigned int vm_page_gobble_count = 0; | |
cb323159 A |
439 | unsigned int vm_page_kern_lpage_count = 0; |
440 | ||
441 | uint64_t booter_size; /* external so it can be found in core dumps */ | |
0a7de745 A |
442 | |
443 | #define VM_PAGE_WIRE_COUNT_WARNING 0 | |
444 | #define VM_PAGE_GOBBLE_COUNT_WARNING 0 | |
445 | ||
446 | unsigned int vm_page_purgeable_count = 0; /* # of pages purgeable now */ | |
447 | unsigned int vm_page_purgeable_wired_count = 0; /* # of purgeable pages that are wired now */ | |
448 | uint64_t vm_page_purged_count = 0; /* total count of purged pages */ | |
449 | ||
450 | unsigned int vm_page_xpmapped_external_count = 0; | |
451 | unsigned int vm_page_external_count = 0; | |
452 | unsigned int vm_page_internal_count = 0; | |
453 | unsigned int vm_page_pageable_external_count = 0; | |
454 | unsigned int vm_page_pageable_internal_count = 0; | |
39236c6e | 455 | |
b0d623f7 | 456 | #if DEVELOPMENT || DEBUG |
0a7de745 A |
457 | unsigned int vm_page_speculative_recreated = 0; |
458 | unsigned int vm_page_speculative_created = 0; | |
459 | unsigned int vm_page_speculative_used = 0; | |
b0d623f7 | 460 | #endif |
2d21ac55 | 461 | |
f427ee49 | 462 | vm_page_queue_head_t vm_page_queue_cleaned VM_PAGE_PACKED_ALIGNED; |
316670eb | 463 | |
0a7de745 | 464 | unsigned int vm_page_cleaned_count = 0; |
316670eb | 465 | |
0a7de745 A |
466 | uint64_t max_valid_dma_address = 0xffffffffffffffffULL; |
467 | ppnum_t max_valid_low_ppnum = PPNUM_MAX; | |
0c530ab8 A |
468 | |
469 | ||
1c79356b A |
470 | /* |
471 | * Several page replacement parameters are also | |
472 | * shared with this module, so that page allocation | |
473 | * (done here in vm_page_alloc) can trigger the | |
474 | * pageout daemon. | |
475 | */ | |
0a7de745 A |
476 | unsigned int vm_page_free_target = 0; |
477 | unsigned int vm_page_free_min = 0; | |
478 | unsigned int vm_page_throttle_limit = 0; | |
479 | unsigned int vm_page_inactive_target = 0; | |
39037602 | 480 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 481 | unsigned int vm_page_secluded_target = 0; |
39037602 | 482 | #endif /* CONFIG_SECLUDED_MEMORY */ |
0a7de745 A |
483 | unsigned int vm_page_anonymous_min = 0; |
484 | unsigned int vm_page_free_reserved = 0; | |
1c79356b | 485 | |
316670eb | 486 | |
1c79356b A |
487 | /* |
488 | * The VM system has a couple of heuristics for deciding | |
489 | * that pages are "uninteresting" and should be placed | |
490 | * on the inactive queue as likely candidates for replacement. | |
491 | * These variables let the heuristics be controlled at run-time | |
492 | * to make experimentation easier. | |
493 | */ | |
494 | ||
495 | boolean_t vm_page_deactivate_hint = TRUE; | |
496 | ||
b0d623f7 | 497 | struct vm_page_stats_reusable vm_page_stats_reusable; |
0a7de745 | 498 | |
1c79356b A |
499 | /* |
500 | * vm_set_page_size: | |
501 | * | |
502 | * Sets the page size, perhaps based upon the memory | |
503 | * size. Must be called before any use of page-size | |
504 | * dependent functions. | |
505 | * | |
506 | * Sets page_shift and page_mask from page_size. | |
507 | */ | |
508 | void | |
509 | vm_set_page_size(void) | |
510 | { | |
fe8ab488 A |
511 | page_size = PAGE_SIZE; |
512 | page_mask = PAGE_MASK; | |
513 | page_shift = PAGE_SHIFT; | |
1c79356b | 514 | |
0a7de745 | 515 | if ((page_mask & page_size) != 0) { |
1c79356b | 516 | panic("vm_set_page_size: page size not a power of two"); |
0a7de745 | 517 | } |
1c79356b | 518 | |
0a7de745 A |
519 | for (page_shift = 0;; page_shift++) { |
520 | if ((1U << page_shift) == page_size) { | |
1c79356b | 521 | break; |
0a7de745 A |
522 | } |
523 | } | |
1c79356b A |
524 | } |
525 | ||
5ba3f43e A |
526 | #if defined (__x86_64__) |
527 | ||
528 | #define MAX_CLUMP_SIZE 16 | |
529 | #define DEFAULT_CLUMP_SIZE 4 | |
530 | ||
531 | unsigned int vm_clump_size, vm_clump_mask, vm_clump_shift, vm_clump_promote_threshold; | |
532 | ||
533 | #if DEVELOPMENT || DEBUG | |
0a7de745 | 534 | unsigned long vm_clump_stats[MAX_CLUMP_SIZE + 1]; |
5ba3f43e A |
535 | unsigned long vm_clump_allocs, vm_clump_inserts, vm_clump_inrange, vm_clump_promotes; |
536 | ||
0a7de745 A |
537 | static inline void |
538 | vm_clump_update_stats(unsigned int c) | |
539 | { | |
540 | assert(c <= vm_clump_size); | |
541 | if (c > 0 && c <= vm_clump_size) { | |
542 | vm_clump_stats[c] += c; | |
543 | } | |
544 | vm_clump_allocs += c; | |
5ba3f43e A |
545 | } |
546 | #endif /* if DEVELOPMENT || DEBUG */ | |
547 | ||
548 | /* Called once to setup the VM clump knobs */ | |
549 | static void | |
550 | vm_page_setup_clump( void ) | |
551 | { | |
0a7de745 | 552 | unsigned int override, n; |
5ba3f43e | 553 | |
0a7de745 A |
554 | vm_clump_size = DEFAULT_CLUMP_SIZE; |
555 | if (PE_parse_boot_argn("clump_size", &override, sizeof(override))) { | |
556 | vm_clump_size = override; | |
557 | } | |
5ba3f43e | 558 | |
0a7de745 A |
559 | if (vm_clump_size > MAX_CLUMP_SIZE) { |
560 | panic("vm_page_setup_clump:: clump_size is too large!"); | |
561 | } | |
562 | if (vm_clump_size < 1) { | |
563 | panic("vm_page_setup_clump:: clump_size must be >= 1"); | |
564 | } | |
565 | if ((vm_clump_size & (vm_clump_size - 1)) != 0) { | |
566 | panic("vm_page_setup_clump:: clump_size must be a power of 2"); | |
567 | } | |
5ba3f43e | 568 | |
0a7de745 A |
569 | vm_clump_promote_threshold = vm_clump_size; |
570 | vm_clump_mask = vm_clump_size - 1; | |
571 | for (vm_clump_shift = 0, n = vm_clump_size; n > 1; n >>= 1, vm_clump_shift++) { | |
572 | ; | |
573 | } | |
5ba3f43e A |
574 | |
575 | #if DEVELOPMENT || DEBUG | |
0a7de745 A |
576 | bzero(vm_clump_stats, sizeof(vm_clump_stats)); |
577 | vm_clump_allocs = vm_clump_inserts = vm_clump_inrange = vm_clump_promotes = 0; | |
5ba3f43e A |
578 | #endif /* if DEVELOPMENT || DEBUG */ |
579 | } | |
fe8ab488 | 580 | |
0a7de745 | 581 | #endif /* #if defined (__x86_64__) */ |
5ba3f43e | 582 | |
0a7de745 | 583 | #define COLOR_GROUPS_TO_STEAL 4 |
2d21ac55 A |
584 | |
585 | /* Called once during statup, once the cache geometry is known. | |
586 | */ | |
587 | static void | |
588 | vm_page_set_colors( void ) | |
589 | { | |
0a7de745 | 590 | unsigned int n, override; |
5ba3f43e | 591 | |
0a7de745 | 592 | #if defined (__x86_64__) |
5ba3f43e A |
593 | /* adjust #colors because we need to color outside the clump boundary */ |
594 | vm_cache_geometry_colors >>= vm_clump_shift; | |
595 | #endif | |
0a7de745 A |
596 | if (PE_parse_boot_argn("colors", &override, sizeof(override))) { /* colors specified as a boot-arg? */ |
597 | n = override; | |
598 | } else if (vm_cache_geometry_colors) { /* do we know what the cache geometry is? */ | |
2d21ac55 | 599 | n = vm_cache_geometry_colors; |
0a7de745 A |
600 | } else { |
601 | n = DEFAULT_COLORS; /* use default if all else fails */ | |
602 | } | |
603 | if (n == 0) { | |
2d21ac55 | 604 | n = 1; |
0a7de745 A |
605 | } |
606 | if (n > MAX_COLORS) { | |
2d21ac55 | 607 | n = MAX_COLORS; |
0a7de745 A |
608 | } |
609 | ||
2d21ac55 | 610 | /* the count must be a power of 2 */ |
0a7de745 A |
611 | if ((n & (n - 1)) != 0) { |
612 | n = DEFAULT_COLORS; /* use default if all else fails */ | |
613 | } | |
2d21ac55 A |
614 | vm_colors = n; |
615 | vm_color_mask = n - 1; | |
fe8ab488 A |
616 | |
617 | vm_free_magazine_refill_limit = vm_colors * COLOR_GROUPS_TO_STEAL; | |
5ba3f43e A |
618 | |
619 | #if defined (__x86_64__) | |
0a7de745 A |
620 | /* adjust for reduction in colors due to clumping and multiple cores */ |
621 | if (real_ncpus) { | |
5ba3f43e | 622 | vm_free_magazine_refill_limit *= (vm_clump_size * real_ncpus); |
0a7de745 A |
623 | } |
624 | #endif | |
625 | } | |
626 | ||
627 | /* | |
628 | * During single threaded early boot we don't initialize all pages. | |
629 | * This avoids some delay during boot. They'll be initialized and | |
630 | * added to the free list as needed or after we are multithreaded by | |
631 | * what becomes the pageout thread. | |
632 | */ | |
633 | static boolean_t fill = FALSE; | |
634 | static unsigned int fillval; | |
635 | uint_t vm_delayed_count = 0; /* when non-zero, indicates we may have more pages to init */ | |
636 | ppnum_t delay_above_pnum = PPNUM_MAX; | |
637 | ||
638 | /* | |
639 | * For x86 first 8 Gig initializes quickly and gives us lots of lowmem + mem above to start off with. | |
640 | * If ARM ever uses delayed page initialization, this value may need to be quite different. | |
641 | */ | |
642 | #define DEFAULT_DELAY_ABOVE_PHYS_GB (8) | |
643 | ||
644 | /* | |
645 | * When we have to dip into more delayed pages due to low memory, free up | |
646 | * a large chunk to get things back to normal. This avoids contention on the | |
647 | * delayed code allocating page by page. | |
648 | */ | |
649 | #define VM_DELAY_PAGE_CHUNK ((1024 * 1024 * 1024) / PAGE_SIZE) | |
650 | ||
651 | /* | |
652 | * Get and initialize the next delayed page. | |
653 | */ | |
654 | static vm_page_t | |
655 | vm_get_delayed_page(int grab_options) | |
656 | { | |
657 | vm_page_t p; | |
658 | ppnum_t pnum; | |
659 | ||
660 | /* | |
661 | * Get a new page if we have one. | |
662 | */ | |
663 | lck_mtx_lock(&vm_page_queue_free_lock); | |
664 | if (vm_delayed_count == 0) { | |
665 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
666 | return NULL; | |
667 | } | |
668 | if (!pmap_next_page(&pnum)) { | |
669 | vm_delayed_count = 0; | |
670 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
671 | return NULL; | |
672 | } | |
673 | ||
674 | assert(vm_delayed_count > 0); | |
675 | --vm_delayed_count; | |
676 | ||
cb323159 A |
677 | #if defined(__x86_64__) |
678 | /* x86 cluster code requires increasing phys_page in vm_pages[] */ | |
679 | if (vm_pages_count > 0) { | |
680 | assert(pnum > vm_pages[vm_pages_count - 1].vmp_phys_page); | |
681 | } | |
682 | #endif | |
0a7de745 A |
683 | p = &vm_pages[vm_pages_count]; |
684 | assert(p < vm_page_array_ending_addr); | |
685 | vm_page_init(p, pnum, FALSE); | |
686 | ++vm_pages_count; | |
687 | ++vm_page_pages; | |
688 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
689 | ||
690 | /* | |
691 | * These pages were initially counted as wired, undo that now. | |
692 | */ | |
693 | if (grab_options & VM_PAGE_GRAB_Q_LOCK_HELD) { | |
694 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
695 | } else { | |
696 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED); | |
697 | vm_page_lockspin_queues(); | |
698 | } | |
699 | --vm_page_wire_count; | |
700 | --vm_page_wire_count_initial; | |
701 | if (vm_page_wire_count_on_boot != 0) { | |
702 | --vm_page_wire_count_on_boot; | |
703 | } | |
704 | if (!(grab_options & VM_PAGE_GRAB_Q_LOCK_HELD)) { | |
705 | vm_page_unlock_queues(); | |
706 | } | |
707 | ||
708 | ||
709 | if (fill) { | |
710 | fillPage(pnum, fillval); | |
711 | } | |
712 | return p; | |
713 | } | |
714 | ||
715 | static void vm_page_module_init_delayed(void); | |
716 | ||
717 | /* | |
718 | * Free all remaining delayed pages to the free lists. | |
719 | */ | |
720 | void | |
721 | vm_free_delayed_pages(void) | |
722 | { | |
723 | vm_page_t p; | |
724 | vm_page_t list = NULL; | |
725 | uint_t cnt = 0; | |
cb323159 A |
726 | vm_offset_t start_free_va; |
727 | int64_t free_size; | |
0a7de745 A |
728 | |
729 | while ((p = vm_get_delayed_page(VM_PAGE_GRAB_OPTIONS_NONE)) != NULL) { | |
730 | if (vm_himemory_mode) { | |
731 | vm_page_release(p, FALSE); | |
732 | } else { | |
733 | p->vmp_snext = list; | |
734 | list = p; | |
735 | } | |
736 | ++cnt; | |
737 | } | |
738 | ||
739 | /* | |
740 | * Free the pages in reverse order if not himemory mode. | |
741 | * Hence the low memory pages will be first on free lists. (LIFO) | |
742 | */ | |
743 | while (list != NULL) { | |
744 | p = list; | |
745 | list = p->vmp_snext; | |
746 | p->vmp_snext = NULL; | |
747 | vm_page_release(p, FALSE); | |
748 | } | |
749 | #if DEVELOPMENT || DEBUG | |
cb323159 | 750 | kprintf("vm_free_delayed_pages: initialized %d free pages\n", cnt); |
0a7de745 A |
751 | #endif |
752 | ||
753 | /* | |
754 | * Free up any unused full pages at the end of the vm_pages[] array | |
755 | */ | |
cb323159 A |
756 | start_free_va = round_page((vm_offset_t)&vm_pages[vm_pages_count]); |
757 | ||
758 | #if defined(__x86_64__) | |
759 | /* | |
760 | * Since x86 might have used large pages for vm_pages[], we can't | |
761 | * free starting in the middle of a partially used large page. | |
762 | */ | |
763 | if (pmap_query_pagesize(kernel_pmap, start_free_va) == I386_LPGBYTES) { | |
764 | start_free_va = ((start_free_va + I386_LPGMASK) & ~I386_LPGMASK); | |
765 | } | |
5ba3f43e | 766 | #endif |
cb323159 A |
767 | if (start_free_va < (vm_offset_t)vm_page_array_ending_addr) { |
768 | free_size = trunc_page((vm_offset_t)vm_page_array_ending_addr - start_free_va); | |
769 | if (free_size > 0) { | |
770 | ml_static_mfree(start_free_va, (vm_offset_t)free_size); | |
771 | vm_page_array_ending_addr = (void *)start_free_va; | |
0a7de745 A |
772 | |
773 | /* | |
774 | * Note there's no locking here, as only this thread will ever change this value. | |
775 | * The reader, vm_page_diagnose, doesn't grab any locks for the counts it looks at. | |
776 | */ | |
cb323159 A |
777 | vm_page_stolen_count -= (free_size >> PAGE_SHIFT); |
778 | ||
779 | #if DEVELOPMENT || DEBUG | |
780 | kprintf("Freeing final unused %ld bytes from vm_pages[] at 0x%lx\n", | |
781 | (long)free_size, (long)start_free_va); | |
782 | #endif | |
0a7de745 A |
783 | } |
784 | } | |
785 | ||
786 | ||
787 | /* | |
788 | * now we can create the VM page array zone | |
789 | */ | |
790 | vm_page_module_init_delayed(); | |
791 | } | |
792 | ||
793 | /* | |
794 | * Try and free up enough delayed pages to match a contig memory allocation. | |
795 | */ | |
796 | static void | |
797 | vm_free_delayed_pages_contig( | |
798 | uint_t npages, | |
799 | ppnum_t max_pnum, | |
800 | ppnum_t pnum_mask) | |
801 | { | |
802 | vm_page_t p; | |
803 | ppnum_t pnum; | |
804 | uint_t cnt = 0; | |
805 | ||
806 | /* | |
807 | * Treat 0 as the absolute max page number. | |
808 | */ | |
809 | if (max_pnum == 0) { | |
810 | max_pnum = PPNUM_MAX; | |
811 | } | |
812 | ||
813 | /* | |
814 | * Free till we get a properly aligned start page | |
815 | */ | |
816 | for (;;) { | |
817 | p = vm_get_delayed_page(VM_PAGE_GRAB_OPTIONS_NONE); | |
818 | if (p == NULL) { | |
819 | return; | |
820 | } | |
821 | pnum = VM_PAGE_GET_PHYS_PAGE(p); | |
822 | vm_page_release(p, FALSE); | |
823 | if (pnum >= max_pnum) { | |
824 | return; | |
825 | } | |
826 | if ((pnum & pnum_mask) == 0) { | |
827 | break; | |
828 | } | |
829 | } | |
830 | ||
831 | /* | |
832 | * Having a healthy pool of free pages will help performance. We don't | |
833 | * want to fall back to the delayed code for every page allocation. | |
834 | */ | |
835 | if (vm_page_free_count < VM_DELAY_PAGE_CHUNK) { | |
836 | npages += VM_DELAY_PAGE_CHUNK; | |
837 | } | |
838 | ||
839 | /* | |
840 | * Now free up the pages | |
841 | */ | |
842 | for (cnt = 1; cnt < npages; ++cnt) { | |
843 | p = vm_get_delayed_page(VM_PAGE_GRAB_OPTIONS_NONE); | |
844 | if (p == NULL) { | |
845 | return; | |
846 | } | |
847 | vm_page_release(p, FALSE); | |
848 | } | |
2d21ac55 A |
849 | } |
850 | ||
5c9f4661 A |
851 | #define ROUNDUP_NEXTP2(X) (1U << (32 - __builtin_clz((X) - 1))) |
852 | ||
b0d623f7 | 853 | void |
f427ee49 | 854 | vm_page_init_local_q(unsigned int num_cpus) |
b0d623f7 | 855 | { |
f427ee49 | 856 | struct vpl *t_local_q; |
b0d623f7 A |
857 | |
858 | /* | |
859 | * no point in this for a uni-processor system | |
860 | */ | |
861 | if (num_cpus >= 2) { | |
f427ee49 A |
862 | ml_cpu_info_t cpu_info; |
863 | ||
864 | /* | |
865 | * Force the allocation alignment to a cacheline, | |
866 | * because the `vpl` struct has a lock and will be taken | |
867 | * cross CPU so we want to isolate the rest of the per-CPU | |
868 | * data to avoid false sharing due to this lock being taken. | |
869 | */ | |
870 | ||
871 | ml_cpu_get_info(&cpu_info); | |
b0d623f7 | 872 | |
f427ee49 A |
873 | t_local_q = zalloc_percpu_permanent(sizeof(struct vpl), |
874 | cpu_info.cache_line_size - 1); | |
b0d623f7 | 875 | |
f427ee49 | 876 | zpercpu_foreach(lq, t_local_q) { |
b0d623f7 | 877 | VPL_LOCK_INIT(lq, &vm_page_lck_grp_local, &vm_page_lck_attr); |
39037602 | 878 | vm_page_queue_init(&lq->vpl_queue); |
b0d623f7 | 879 | } |
b0d623f7 | 880 | |
f427ee49 A |
881 | /* make the initialization visible to all cores */ |
882 | os_atomic_store(&vm_page_local_q, t_local_q, release); | |
b0d623f7 A |
883 | } |
884 | } | |
885 | ||
5ba3f43e A |
886 | /* |
887 | * vm_init_before_launchd | |
888 | * | |
889 | * This should be called right before launchd is loaded. | |
890 | */ | |
891 | void | |
892 | vm_init_before_launchd() | |
893 | { | |
0a7de745 | 894 | vm_page_lockspin_queues(); |
5ba3f43e | 895 | vm_page_wire_count_on_boot = vm_page_wire_count; |
0a7de745 | 896 | vm_page_unlock_queues(); |
5ba3f43e A |
897 | } |
898 | ||
b0d623f7 | 899 | |
1c79356b A |
900 | /* |
901 | * vm_page_bootstrap: | |
902 | * | |
903 | * Initializes the resident memory module. | |
904 | * | |
905 | * Allocates memory for the page cells, and | |
906 | * for the object/offset-to-page hash table headers. | |
907 | * Each page cell is initialized and placed on the free list. | |
908 | * Returns the range of available kernel virtual memory. | |
909 | */ | |
f427ee49 | 910 | __startup_func |
1c79356b A |
911 | void |
912 | vm_page_bootstrap( | |
0a7de745 A |
913 | vm_offset_t *startp, |
914 | vm_offset_t *endp) | |
1c79356b | 915 | { |
0a7de745 A |
916 | unsigned int i; |
917 | unsigned int log1; | |
918 | unsigned int log2; | |
919 | unsigned int size; | |
1c79356b | 920 | |
1c79356b A |
921 | /* |
922 | * Initialize the page queues. | |
923 | */ | |
0a7de745 | 924 | |
b0d623f7 A |
925 | lck_mtx_init_ext(&vm_page_queue_free_lock, &vm_page_queue_free_lock_ext, &vm_page_lck_grp_free, &vm_page_lck_attr); |
926 | lck_mtx_init_ext(&vm_page_queue_lock, &vm_page_queue_lock_ext, &vm_page_lck_grp_queue, &vm_page_lck_attr); | |
927 | lck_mtx_init_ext(&vm_purgeable_queue_lock, &vm_purgeable_queue_lock_ext, &vm_page_lck_grp_purge, &vm_page_lck_attr); | |
0a7de745 | 928 | |
2d21ac55 A |
929 | for (i = 0; i < PURGEABLE_Q_TYPE_MAX; i++) { |
930 | int group; | |
931 | ||
932 | purgeable_queues[i].token_q_head = 0; | |
933 | purgeable_queues[i].token_q_tail = 0; | |
0a7de745 A |
934 | for (group = 0; group < NUM_VOLATILE_GROUPS; group++) { |
935 | queue_init(&purgeable_queues[i].objq[group]); | |
936 | } | |
2d21ac55 A |
937 | |
938 | purgeable_queues[i].type = i; | |
939 | purgeable_queues[i].new_pages = 0; | |
940 | #if MACH_ASSERT | |
941 | purgeable_queues[i].debug_count_tokens = 0; | |
942 | purgeable_queues[i].debug_count_objects = 0; | |
943 | #endif | |
0a7de745 A |
944 | } |
945 | ; | |
fe8ab488 A |
946 | purgeable_nonvolatile_count = 0; |
947 | queue_init(&purgeable_nonvolatile_queue); | |
0a7de745 A |
948 | |
949 | for (i = 0; i < MAX_COLORS; i++) { | |
39037602 | 950 | vm_page_queue_init(&vm_page_queue_free[i].qhead); |
0a7de745 | 951 | } |
39037602 A |
952 | |
953 | vm_page_queue_init(&vm_lopage_queue_free); | |
954 | vm_page_queue_init(&vm_page_queue_active); | |
955 | vm_page_queue_init(&vm_page_queue_inactive); | |
956 | #if CONFIG_SECLUDED_MEMORY | |
957 | vm_page_queue_init(&vm_page_queue_secluded); | |
958 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
959 | vm_page_queue_init(&vm_page_queue_cleaned); | |
960 | vm_page_queue_init(&vm_page_queue_throttled); | |
961 | vm_page_queue_init(&vm_page_queue_anonymous); | |
3e170ce0 | 962 | queue_init(&vm_objects_wired); |
1c79356b | 963 | |
0a7de745 | 964 | for (i = 0; i <= VM_PAGE_MAX_SPECULATIVE_AGE_Q; i++) { |
39037602 | 965 | vm_page_queue_init(&vm_page_queue_speculative[i].age_q); |
2d21ac55 A |
966 | |
967 | vm_page_queue_speculative[i].age_ts.tv_sec = 0; | |
968 | vm_page_queue_speculative[i].age_ts.tv_nsec = 0; | |
969 | } | |
39037602 A |
970 | #if CONFIG_BACKGROUND_QUEUE |
971 | vm_page_queue_init(&vm_page_queue_background); | |
972 | ||
973 | vm_page_background_count = 0; | |
974 | vm_page_background_internal_count = 0; | |
975 | vm_page_background_external_count = 0; | |
976 | vm_page_background_promoted_count = 0; | |
977 | ||
978 | vm_page_background_target = (unsigned int)(atop_64(max_mem) / 25); | |
979 | ||
0a7de745 | 980 | if (vm_page_background_target > VM_PAGE_BACKGROUND_TARGET_MAX) { |
39037602 | 981 | vm_page_background_target = VM_PAGE_BACKGROUND_TARGET_MAX; |
0a7de745 | 982 | } |
39037602 A |
983 | |
984 | vm_page_background_mode = VM_PAGE_BG_LEVEL_1; | |
985 | vm_page_background_exclude_external = 0; | |
986 | ||
987 | PE_parse_boot_argn("vm_page_bg_mode", &vm_page_background_mode, sizeof(vm_page_background_mode)); | |
988 | PE_parse_boot_argn("vm_page_bg_exclude_external", &vm_page_background_exclude_external, sizeof(vm_page_background_exclude_external)); | |
989 | PE_parse_boot_argn("vm_page_bg_target", &vm_page_background_target, sizeof(vm_page_background_target)); | |
39037602 | 990 | |
0a7de745 | 991 | if (vm_page_background_mode > VM_PAGE_BG_LEVEL_1) { |
39037602 | 992 | vm_page_background_mode = VM_PAGE_BG_LEVEL_1; |
0a7de745 | 993 | } |
39037602 | 994 | #endif |
1c79356b | 995 | vm_page_free_wanted = 0; |
2d21ac55 | 996 | vm_page_free_wanted_privileged = 0; |
39037602 A |
997 | #if CONFIG_SECLUDED_MEMORY |
998 | vm_page_free_wanted_secluded = 0; | |
999 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
0a7de745 | 1000 | |
5ba3f43e A |
1001 | #if defined (__x86_64__) |
1002 | /* this must be called before vm_page_set_colors() */ | |
1003 | vm_page_setup_clump(); | |
1004 | #endif | |
1005 | ||
2d21ac55 A |
1006 | vm_page_set_colors(); |
1007 | ||
39037602 A |
1008 | bzero(vm_page_inactive_states, sizeof(vm_page_inactive_states)); |
1009 | vm_page_inactive_states[VM_PAGE_ON_INACTIVE_INTERNAL_Q] = 1; | |
1010 | vm_page_inactive_states[VM_PAGE_ON_INACTIVE_EXTERNAL_Q] = 1; | |
1011 | vm_page_inactive_states[VM_PAGE_ON_INACTIVE_CLEANED_Q] = 1; | |
1012 | ||
1013 | bzero(vm_page_pageable_states, sizeof(vm_page_pageable_states)); | |
1014 | vm_page_pageable_states[VM_PAGE_ON_INACTIVE_INTERNAL_Q] = 1; | |
1015 | vm_page_pageable_states[VM_PAGE_ON_INACTIVE_EXTERNAL_Q] = 1; | |
1016 | vm_page_pageable_states[VM_PAGE_ON_INACTIVE_CLEANED_Q] = 1; | |
1017 | vm_page_pageable_states[VM_PAGE_ON_ACTIVE_Q] = 1; | |
1018 | vm_page_pageable_states[VM_PAGE_ON_SPECULATIVE_Q] = 1; | |
1019 | vm_page_pageable_states[VM_PAGE_ON_THROTTLED_Q] = 1; | |
1020 | #if CONFIG_SECLUDED_MEMORY | |
1021 | vm_page_pageable_states[VM_PAGE_ON_SECLUDED_Q] = 1; | |
1022 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
1023 | ||
1024 | bzero(vm_page_non_speculative_pageable_states, sizeof(vm_page_non_speculative_pageable_states)); | |
1025 | vm_page_non_speculative_pageable_states[VM_PAGE_ON_INACTIVE_INTERNAL_Q] = 1; | |
1026 | vm_page_non_speculative_pageable_states[VM_PAGE_ON_INACTIVE_EXTERNAL_Q] = 1; | |
1027 | vm_page_non_speculative_pageable_states[VM_PAGE_ON_INACTIVE_CLEANED_Q] = 1; | |
1028 | vm_page_non_speculative_pageable_states[VM_PAGE_ON_ACTIVE_Q] = 1; | |
1029 | vm_page_non_speculative_pageable_states[VM_PAGE_ON_THROTTLED_Q] = 1; | |
1030 | #if CONFIG_SECLUDED_MEMORY | |
1031 | vm_page_non_speculative_pageable_states[VM_PAGE_ON_SECLUDED_Q] = 1; | |
1032 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
0a7de745 | 1033 | |
39037602 A |
1034 | bzero(vm_page_active_or_inactive_states, sizeof(vm_page_active_or_inactive_states)); |
1035 | vm_page_active_or_inactive_states[VM_PAGE_ON_INACTIVE_INTERNAL_Q] = 1; | |
1036 | vm_page_active_or_inactive_states[VM_PAGE_ON_INACTIVE_EXTERNAL_Q] = 1; | |
1037 | vm_page_active_or_inactive_states[VM_PAGE_ON_INACTIVE_CLEANED_Q] = 1; | |
1038 | vm_page_active_or_inactive_states[VM_PAGE_ON_ACTIVE_Q] = 1; | |
1039 | #if CONFIG_SECLUDED_MEMORY | |
1040 | vm_page_active_or_inactive_states[VM_PAGE_ON_SECLUDED_Q] = 1; | |
1041 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
1042 | ||
f427ee49 A |
1043 | for (vm_tag_t t = 0; t < VM_KERN_MEMORY_FIRST_DYNAMIC; t++) { |
1044 | vm_allocation_sites_static[t].refcount = 2; | |
1045 | vm_allocation_sites_static[t].tag = t; | |
1046 | vm_allocation_sites[t] = &vm_allocation_sites_static[t]; | |
5ba3f43e A |
1047 | } |
1048 | vm_allocation_sites_static[VM_KERN_MEMORY_FIRST_DYNAMIC].refcount = 2; | |
1049 | vm_allocation_sites_static[VM_KERN_MEMORY_FIRST_DYNAMIC].tag = VM_KERN_MEMORY_ANY; | |
1050 | vm_allocation_sites[VM_KERN_MEMORY_ANY] = &vm_allocation_sites_static[VM_KERN_MEMORY_FIRST_DYNAMIC]; | |
1c79356b A |
1051 | |
1052 | /* | |
1053 | * Steal memory for the map and zone subsystems. | |
1054 | */ | |
f427ee49 | 1055 | kernel_startup_initialize_upto(STARTUP_SUB_PMAP_STEAL); |
1c79356b A |
1056 | |
1057 | /* | |
1058 | * Allocate (and initialize) the virtual-to-physical | |
1059 | * table hash buckets. | |
1060 | * | |
1061 | * The number of buckets should be a power of two to | |
1062 | * get a good hash function. The following computation | |
1063 | * chooses the first power of two that is greater | |
1064 | * than the number of physical pages in the system. | |
1065 | */ | |
1066 | ||
1c79356b A |
1067 | if (vm_page_bucket_count == 0) { |
1068 | unsigned int npages = pmap_free_pages(); | |
1069 | ||
1070 | vm_page_bucket_count = 1; | |
0a7de745 | 1071 | while (vm_page_bucket_count < npages) { |
1c79356b | 1072 | vm_page_bucket_count <<= 1; |
0a7de745 | 1073 | } |
1c79356b | 1074 | } |
b0d623f7 | 1075 | vm_page_bucket_lock_count = (vm_page_bucket_count + BUCKETS_PER_LOCK - 1) / BUCKETS_PER_LOCK; |
1c79356b A |
1076 | |
1077 | vm_page_hash_mask = vm_page_bucket_count - 1; | |
1078 | ||
1079 | /* | |
1080 | * Calculate object shift value for hashing algorithm: | |
1081 | * O = log2(sizeof(struct vm_object)) | |
1082 | * B = log2(vm_page_bucket_count) | |
1083 | * hash shifts the object left by | |
1084 | * B/2 - O | |
1085 | */ | |
1086 | size = vm_page_bucket_count; | |
0a7de745 | 1087 | for (log1 = 0; size > 1; log1++) { |
1c79356b | 1088 | size /= 2; |
0a7de745 | 1089 | } |
1c79356b | 1090 | size = sizeof(struct vm_object); |
0a7de745 | 1091 | for (log2 = 0; size > 1; log2++) { |
1c79356b | 1092 | size /= 2; |
0a7de745 A |
1093 | } |
1094 | vm_page_hash_shift = log1 / 2 - log2 + 1; | |
1095 | ||
1096 | vm_page_bucket_hash = 1 << ((log1 + 1) >> 1); /* Get (ceiling of sqrt of table size) */ | |
1097 | vm_page_bucket_hash |= 1 << ((log1 + 1) >> 2); /* Get (ceiling of quadroot of table size) */ | |
1098 | vm_page_bucket_hash |= 1; /* Set bit and add 1 - always must be 1 to insure unique series */ | |
1c79356b | 1099 | |
0a7de745 | 1100 | if (vm_page_hash_mask & vm_page_bucket_count) { |
1c79356b | 1101 | printf("vm_page_bootstrap: WARNING -- strange page hash\n"); |
0a7de745 | 1102 | } |
1c79356b | 1103 | |
15129b1c A |
1104 | #if VM_PAGE_BUCKETS_CHECK |
1105 | #if VM_PAGE_FAKE_BUCKETS | |
1106 | /* | |
1107 | * Allocate a decoy set of page buckets, to detect | |
1108 | * any stomping there. | |
1109 | */ | |
1110 | vm_page_fake_buckets = (vm_page_bucket_t *) | |
0a7de745 A |
1111 | pmap_steal_memory(vm_page_bucket_count * |
1112 | sizeof(vm_page_bucket_t)); | |
15129b1c A |
1113 | vm_page_fake_buckets_start = (vm_map_offset_t) vm_page_fake_buckets; |
1114 | vm_page_fake_buckets_end = | |
0a7de745 A |
1115 | vm_map_round_page((vm_page_fake_buckets_start + |
1116 | (vm_page_bucket_count * | |
1117 | sizeof(vm_page_bucket_t))), | |
1118 | PAGE_MASK); | |
15129b1c A |
1119 | char *cp; |
1120 | for (cp = (char *)vm_page_fake_buckets_start; | |
0a7de745 A |
1121 | cp < (char *)vm_page_fake_buckets_end; |
1122 | cp++) { | |
15129b1c A |
1123 | *cp = 0x5a; |
1124 | } | |
1125 | #endif /* VM_PAGE_FAKE_BUCKETS */ | |
1126 | #endif /* VM_PAGE_BUCKETS_CHECK */ | |
1127 | ||
39037602 | 1128 | kernel_debug_string_early("vm_page_buckets"); |
1c79356b | 1129 | vm_page_buckets = (vm_page_bucket_t *) |
0a7de745 A |
1130 | pmap_steal_memory(vm_page_bucket_count * |
1131 | sizeof(vm_page_bucket_t)); | |
1c79356b | 1132 | |
39037602 | 1133 | kernel_debug_string_early("vm_page_bucket_locks"); |
b0d623f7 | 1134 | vm_page_bucket_locks = (lck_spin_t *) |
0a7de745 A |
1135 | pmap_steal_memory(vm_page_bucket_lock_count * |
1136 | sizeof(lck_spin_t)); | |
b0d623f7 | 1137 | |
1c79356b | 1138 | for (i = 0; i < vm_page_bucket_count; i++) { |
39037602 | 1139 | vm_page_bucket_t *bucket = &vm_page_buckets[i]; |
1c79356b | 1140 | |
fe8ab488 | 1141 | bucket->page_list = VM_PAGE_PACK_PTR(VM_PAGE_NULL); |
1c79356b A |
1142 | #if MACH_PAGE_HASH_STATS |
1143 | bucket->cur_count = 0; | |
1144 | bucket->hi_count = 0; | |
1145 | #endif /* MACH_PAGE_HASH_STATS */ | |
1146 | } | |
1147 | ||
0a7de745 A |
1148 | for (i = 0; i < vm_page_bucket_lock_count; i++) { |
1149 | lck_spin_init(&vm_page_bucket_locks[i], &vm_page_lck_grp_bucket, &vm_page_lck_attr); | |
1150 | } | |
b0d623f7 | 1151 | |
3e170ce0 A |
1152 | vm_tag_init(); |
1153 | ||
15129b1c A |
1154 | #if VM_PAGE_BUCKETS_CHECK |
1155 | vm_page_buckets_check_ready = TRUE; | |
1156 | #endif /* VM_PAGE_BUCKETS_CHECK */ | |
1157 | ||
1c79356b A |
1158 | /* |
1159 | * Machine-dependent code allocates the resident page table. | |
1160 | * It uses vm_page_init to initialize the page frames. | |
1161 | * The code also returns to us the virtual space available | |
1162 | * to the kernel. We don't trust the pmap module | |
1163 | * to get the alignment right. | |
1164 | */ | |
1165 | ||
39037602 | 1166 | kernel_debug_string_early("pmap_startup"); |
1c79356b | 1167 | pmap_startup(&virtual_space_start, &virtual_space_end); |
91447636 A |
1168 | virtual_space_start = round_page(virtual_space_start); |
1169 | virtual_space_end = trunc_page(virtual_space_end); | |
1c79356b A |
1170 | |
1171 | *startp = virtual_space_start; | |
1172 | *endp = virtual_space_end; | |
1173 | ||
1174 | /* | |
1175 | * Compute the initial "wire" count. | |
0a7de745 | 1176 | * Up until now, the pages which have been set aside are not under |
1c79356b A |
1177 | * the VM system's control, so although they aren't explicitly |
1178 | * wired, they nonetheless can't be moved. At this moment, | |
1179 | * all VM managed pages are "free", courtesy of pmap_startup. | |
1180 | */ | |
b0d623f7 | 1181 | assert((unsigned int) atop_64(max_mem) == atop_64(max_mem)); |
0a7de745 A |
1182 | vm_page_wire_count = ((unsigned int) atop_64(max_mem)) - |
1183 | vm_page_free_count - vm_lopage_free_count; | |
39037602 A |
1184 | #if CONFIG_SECLUDED_MEMORY |
1185 | vm_page_wire_count -= vm_page_secluded_count; | |
1186 | #endif | |
0b4c1975 | 1187 | vm_page_wire_count_initial = vm_page_wire_count; |
91447636 | 1188 | |
cb323159 A |
1189 | /* capture this for later use */ |
1190 | booter_size = ml_get_booter_memory_size(); | |
1191 | ||
0a7de745 A |
1192 | printf("vm_page_bootstrap: %d free pages, %d wired pages, (up to %d of which are delayed free)\n", |
1193 | vm_page_free_count, vm_page_wire_count, vm_delayed_count); | |
2d21ac55 | 1194 | |
39037602 | 1195 | kernel_debug_string_early("vm_page_bootstrap complete"); |
1c79356b A |
1196 | } |
1197 | ||
0a7de745 | 1198 | #ifndef MACHINE_PAGES |
1c79356b | 1199 | /* |
cb323159 A |
1200 | * This is the early boot time allocator for data structures needed to bootstrap the VM system. |
1201 | * On x86 it will allocate large pages if size is sufficiently large. We don't need to do this | |
1202 | * on ARM yet, due to the combination of a large base page size and smaller RAM devices. | |
1c79356b | 1203 | */ |
cb323159 A |
1204 | static void * |
1205 | pmap_steal_memory_internal( | |
1206 | vm_size_t size, | |
1207 | boolean_t might_free) | |
1c79356b | 1208 | { |
5ba3f43e | 1209 | kern_return_t kr; |
cb323159 A |
1210 | vm_offset_t addr; |
1211 | vm_offset_t map_addr; | |
5ba3f43e | 1212 | ppnum_t phys_page; |
1c79356b A |
1213 | |
1214 | /* | |
cb323159 | 1215 | * Size needs to be aligned to word size. |
1c79356b | 1216 | */ |
0a7de745 | 1217 | size = (size + sizeof(void *) - 1) & ~(sizeof(void *) - 1); |
1c79356b A |
1218 | |
1219 | /* | |
cb323159 A |
1220 | * On the first call, get the initial values for virtual address space |
1221 | * and page align them. | |
1c79356b | 1222 | */ |
1c79356b A |
1223 | if (virtual_space_start == virtual_space_end) { |
1224 | pmap_virtual_space(&virtual_space_start, &virtual_space_end); | |
cb323159 A |
1225 | virtual_space_start = round_page(virtual_space_start); |
1226 | virtual_space_end = trunc_page(virtual_space_end); | |
1c79356b | 1227 | |
cb323159 | 1228 | #if defined(__x86_64__) |
1c79356b | 1229 | /* |
cb323159 A |
1230 | * Release remaining unused section of preallocated KVA and the 4K page tables |
1231 | * that map it. This makes the VA available for large page mappings. | |
1c79356b | 1232 | */ |
cb323159 A |
1233 | Idle_PTs_release(virtual_space_start, virtual_space_end); |
1234 | #endif | |
1c79356b A |
1235 | } |
1236 | ||
1237 | /* | |
cb323159 A |
1238 | * Allocate the virtual space for this request. On x86, we'll align to a large page |
1239 | * address if the size is big enough to back with at least 1 large page. | |
1c79356b | 1240 | */ |
cb323159 A |
1241 | #if defined(__x86_64__) |
1242 | if (size >= I386_LPGBYTES) { | |
1243 | virtual_space_start = ((virtual_space_start + I386_LPGMASK) & ~I386_LPGMASK); | |
1244 | } | |
1245 | #endif | |
1c79356b A |
1246 | addr = virtual_space_start; |
1247 | virtual_space_start += size; | |
1248 | ||
6d2010ae | 1249 | //kprintf("pmap_steal_memory: %08lX - %08lX; size=%08lX\n", (long)addr, (long)virtual_space_start, (long)size); /* (TEST/DEBUG) */ |
1c79356b A |
1250 | |
1251 | /* | |
cb323159 | 1252 | * Allocate and map physical pages to back the new virtual space. |
1c79356b | 1253 | */ |
cb323159 A |
1254 | map_addr = round_page(addr); |
1255 | while (map_addr < addr + size) { | |
1256 | #if defined(__x86_64__) | |
1257 | /* | |
1258 | * Back with a large page if properly aligned on x86 | |
1259 | */ | |
1260 | if ((map_addr & I386_LPGMASK) == 0 && | |
1261 | map_addr + I386_LPGBYTES <= addr + size && | |
1262 | pmap_pre_expand_large(kernel_pmap, map_addr) == KERN_SUCCESS && | |
1263 | pmap_next_page_large(&phys_page) == KERN_SUCCESS) { | |
1264 | kr = pmap_enter(kernel_pmap, map_addr, phys_page, | |
1265 | VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, | |
1266 | VM_WIMG_USE_DEFAULT | VM_MEM_SUPERPAGE, FALSE); | |
1267 | ||
1268 | if (kr != KERN_SUCCESS) { | |
1269 | panic("pmap_steal_memory: pmap_enter() large failed, new_addr=%#lx, phys_page=%u", | |
1270 | (unsigned long)map_addr, phys_page); | |
1271 | } | |
1272 | map_addr += I386_LPGBYTES; | |
1273 | vm_page_wire_count += I386_LPGBYTES >> PAGE_SHIFT; | |
1274 | vm_page_stolen_count += I386_LPGBYTES >> PAGE_SHIFT; | |
1275 | vm_page_kern_lpage_count++; | |
1276 | continue; | |
1277 | } | |
1278 | #endif | |
1c79356b | 1279 | |
cb323159 | 1280 | if (!pmap_next_page_hi(&phys_page, might_free)) { |
39037602 | 1281 | panic("pmap_steal_memory() size: 0x%llx\n", (uint64_t)size); |
0a7de745 | 1282 | } |
1c79356b | 1283 | |
cb323159 A |
1284 | #if defined(__x86_64__) |
1285 | pmap_pre_expand(kernel_pmap, map_addr); | |
5ba3f43e A |
1286 | #endif |
1287 | ||
cb323159 | 1288 | kr = pmap_enter(kernel_pmap, map_addr, phys_page, |
0a7de745 A |
1289 | VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, |
1290 | VM_WIMG_USE_DEFAULT, FALSE); | |
5ba3f43e A |
1291 | |
1292 | if (kr != KERN_SUCCESS) { | |
cb323159 A |
1293 | panic("pmap_steal_memory() pmap_enter failed, map_addr=%#lx, phys_page=%u", |
1294 | (unsigned long)map_addr, phys_page); | |
5ba3f43e | 1295 | } |
cb323159 | 1296 | map_addr += PAGE_SIZE; |
1c79356b | 1297 | |
1c79356b A |
1298 | /* |
1299 | * Account for newly stolen memory | |
1300 | */ | |
1301 | vm_page_wire_count++; | |
3e170ce0 | 1302 | vm_page_stolen_count++; |
1c79356b A |
1303 | } |
1304 | ||
cb323159 A |
1305 | #if defined(__x86_64__) |
1306 | /* | |
1307 | * The call with might_free is currently the last use of pmap_steal_memory*(). | |
1308 | * Notify the pmap layer to record which high pages were allocated so far. | |
1309 | */ | |
1310 | if (might_free) { | |
1311 | pmap_hi_pages_done(); | |
1312 | } | |
1313 | #endif | |
5ba3f43e A |
1314 | #if KASAN |
1315 | kasan_notify_address(round_page(addr), size); | |
1316 | #endif | |
91447636 | 1317 | return (void *) addr; |
1c79356b A |
1318 | } |
1319 | ||
cb323159 A |
1320 | void * |
1321 | pmap_steal_memory( | |
1322 | vm_size_t size) | |
1323 | { | |
1324 | return pmap_steal_memory_internal(size, FALSE); | |
1325 | } | |
1326 | ||
1327 | void * | |
1328 | pmap_steal_freeable_memory( | |
1329 | vm_size_t size) | |
1330 | { | |
1331 | return pmap_steal_memory_internal(size, TRUE); | |
1332 | } | |
1333 | ||
39037602 A |
1334 | #if CONFIG_SECLUDED_MEMORY |
1335 | /* boot-args to control secluded memory */ | |
0a7de745 A |
1336 | unsigned int secluded_mem_mb = 0; /* # of MBs of RAM to seclude */ |
1337 | int secluded_for_iokit = 1; /* IOKit can use secluded memory */ | |
1338 | int secluded_for_apps = 1; /* apps can use secluded memory */ | |
1339 | int secluded_for_filecache = 2; /* filecache can use seclude memory */ | |
39037602 A |
1340 | #if 11 |
1341 | int secluded_for_fbdp = 0; | |
1342 | #endif | |
d9a64523 | 1343 | uint64_t secluded_shutoff_trigger = 0; |
f427ee49 | 1344 | uint64_t secluded_shutoff_headroom = 150 * 1024 * 1024; /* original value from N56 */ |
39037602 A |
1345 | #endif /* CONFIG_SECLUDED_MEMORY */ |
1346 | ||
1347 | ||
5ba3f43e A |
1348 | #if defined(__arm__) || defined(__arm64__) |
1349 | extern void patch_low_glo_vm_page_info(void *, void *, uint32_t); | |
1350 | unsigned int vm_first_phys_ppnum = 0; | |
1351 | #endif | |
39037602 | 1352 | |
fe8ab488 | 1353 | void vm_page_release_startup(vm_page_t mem); |
1c79356b A |
1354 | void |
1355 | pmap_startup( | |
0a7de745 A |
1356 | vm_offset_t *startp, |
1357 | vm_offset_t *endp) | |
1c79356b | 1358 | { |
0a7de745 A |
1359 | unsigned int i, npages; |
1360 | ppnum_t phys_page; | |
1361 | uint64_t mem_sz; | |
1362 | uint64_t start_ns; | |
1363 | uint64_t now_ns; | |
1364 | uint_t low_page_count = 0; | |
1c79356b | 1365 | |
fe8ab488 | 1366 | #if defined(__LP64__) |
fe8ab488 A |
1367 | /* |
1368 | * make sure we are aligned on a 64 byte boundary | |
1369 | * for VM_PAGE_PACK_PTR (it clips off the low-order | |
1370 | * 6 bits of the pointer) | |
1371 | */ | |
0a7de745 | 1372 | if (virtual_space_start != virtual_space_end) { |
fe8ab488 | 1373 | virtual_space_start = round_page(virtual_space_start); |
0a7de745 | 1374 | } |
fe8ab488 A |
1375 | #endif |
1376 | ||
1c79356b | 1377 | /* |
0a7de745 A |
1378 | * We calculate how many page frames we will have |
1379 | * and then allocate the page structures in one chunk. | |
1380 | * | |
1381 | * Note that the calculation here doesn't take into account | |
1382 | * the memory needed to map what's being allocated, i.e. the page | |
1383 | * table entries. So the actual number of pages we get will be | |
1384 | * less than this. To do someday: include that in the computation. | |
1c79356b | 1385 | */ |
0a7de745 A |
1386 | mem_sz = pmap_free_pages() * (uint64_t)PAGE_SIZE; |
1387 | mem_sz += round_page(virtual_space_start) - virtual_space_start; /* Account for any slop */ | |
1388 | npages = (uint_t)(mem_sz / (PAGE_SIZE + sizeof(*vm_pages))); /* scaled to include the vm_page_ts */ | |
1c79356b | 1389 | |
cb323159 | 1390 | vm_pages = (vm_page_t) pmap_steal_freeable_memory(npages * sizeof *vm_pages); |
1c79356b | 1391 | |
0c530ab8 A |
1392 | /* |
1393 | * Check if we want to initialize pages to a known value | |
1394 | */ | |
0a7de745 A |
1395 | if (PE_parse_boot_argn("fill", &fillval, sizeof(fillval))) { |
1396 | fill = TRUE; | |
1397 | } | |
1398 | #if DEBUG | |
316670eb A |
1399 | /* This slows down booting the DEBUG kernel, particularly on |
1400 | * large memory systems, but is worthwhile in deterministically | |
1401 | * trapping uninitialized memory usage. | |
1402 | */ | |
0a7de745 A |
1403 | if (!fill) { |
1404 | fill = TRUE; | |
316670eb A |
1405 | fillval = 0xDEB8F177; |
1406 | } | |
1407 | #endif | |
0a7de745 | 1408 | if (fill) { |
316670eb | 1409 | kprintf("Filling vm_pages with pattern: 0x%x\n", fillval); |
0a7de745 | 1410 | } |
39037602 A |
1411 | |
1412 | #if CONFIG_SECLUDED_MEMORY | |
0a7de745 A |
1413 | /* |
1414 | * Figure out how much secluded memory to have before we start | |
1415 | * release pages to free lists. | |
1416 | * The default, if specified nowhere else, is no secluded mem. | |
1417 | */ | |
39037602 | 1418 | secluded_mem_mb = 0; |
0a7de745 | 1419 | if (max_mem > 1 * 1024 * 1024 * 1024) { |
39037602 A |
1420 | /* default to 90MB for devices with > 1GB of RAM */ |
1421 | secluded_mem_mb = 90; | |
1422 | } | |
1423 | /* override with value from device tree, if provided */ | |
1424 | PE_get_default("kern.secluded_mem_mb", | |
0a7de745 | 1425 | &secluded_mem_mb, sizeof(secluded_mem_mb)); |
39037602 A |
1426 | /* override with value from boot-args, if provided */ |
1427 | PE_parse_boot_argn("secluded_mem_mb", | |
0a7de745 A |
1428 | &secluded_mem_mb, |
1429 | sizeof(secluded_mem_mb)); | |
39037602 A |
1430 | |
1431 | vm_page_secluded_target = (unsigned int) | |
0a7de745 | 1432 | ((secluded_mem_mb * 1024ULL * 1024ULL) / PAGE_SIZE); |
39037602 | 1433 | PE_parse_boot_argn("secluded_for_iokit", |
0a7de745 A |
1434 | &secluded_for_iokit, |
1435 | sizeof(secluded_for_iokit)); | |
39037602 | 1436 | PE_parse_boot_argn("secluded_for_apps", |
0a7de745 A |
1437 | &secluded_for_apps, |
1438 | sizeof(secluded_for_apps)); | |
39037602 | 1439 | PE_parse_boot_argn("secluded_for_filecache", |
0a7de745 A |
1440 | &secluded_for_filecache, |
1441 | sizeof(secluded_for_filecache)); | |
39037602 A |
1442 | #if 11 |
1443 | PE_parse_boot_argn("secluded_for_fbdp", | |
0a7de745 A |
1444 | &secluded_for_fbdp, |
1445 | sizeof(secluded_for_fbdp)); | |
39037602 | 1446 | #endif |
39037602 | 1447 | |
d9a64523 | 1448 | /* |
f427ee49 | 1449 | * Allow a really large app to effectively use secluded memory until it exits. |
d9a64523 | 1450 | */ |
f427ee49 | 1451 | if (vm_page_secluded_target != 0) { |
d9a64523 | 1452 | /* |
f427ee49 A |
1453 | * Get an amount from boot-args, else use 1/2 of max_mem. |
1454 | * 1/2 max_mem was chosen from a Peace daemon tentpole test which | |
1455 | * used munch to induce jetsam thrashing of false idle daemons on N56. | |
d9a64523 A |
1456 | */ |
1457 | int secluded_shutoff_mb; | |
1458 | if (PE_parse_boot_argn("secluded_shutoff_mb", &secluded_shutoff_mb, | |
0a7de745 | 1459 | sizeof(secluded_shutoff_mb))) { |
d9a64523 | 1460 | secluded_shutoff_trigger = (uint64_t)secluded_shutoff_mb * 1024 * 1024; |
0a7de745 | 1461 | } else { |
f427ee49 | 1462 | secluded_shutoff_trigger = max_mem / 2; |
0a7de745 | 1463 | } |
d9a64523 | 1464 | |
f427ee49 A |
1465 | /* ensure the headroom value is sensible and avoid underflows */ |
1466 | assert(secluded_shutoff_trigger == 0 || secluded_shutoff_trigger > secluded_shutoff_headroom); | |
0c530ab8 | 1467 | } |
d9a64523 A |
1468 | |
1469 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
0c530ab8 | 1470 | |
0a7de745 A |
1471 | #if defined(__x86_64__) |
1472 | ||
1c79356b | 1473 | /* |
0a7de745 | 1474 | * Decide how much memory we delay freeing at boot time. |
1c79356b | 1475 | */ |
0a7de745 A |
1476 | uint32_t delay_above_gb; |
1477 | if (!PE_parse_boot_argn("delay_above_gb", &delay_above_gb, sizeof(delay_above_gb))) { | |
1478 | delay_above_gb = DEFAULT_DELAY_ABOVE_PHYS_GB; | |
1479 | } | |
1480 | ||
1481 | if (delay_above_gb == 0) { | |
1482 | delay_above_pnum = PPNUM_MAX; | |
d9a64523 | 1483 | } else { |
0a7de745 | 1484 | delay_above_pnum = delay_above_gb * (1024 * 1024 * 1024 / PAGE_SIZE); |
1c79356b A |
1485 | } |
1486 | ||
0a7de745 A |
1487 | /* make sure we have sane breathing room: 1G above low memory */ |
1488 | if (delay_above_pnum <= max_valid_low_ppnum) { | |
1489 | delay_above_pnum = max_valid_low_ppnum + ((1024 * 1024 * 1024) >> PAGE_SHIFT); | |
1490 | } | |
2d21ac55 | 1491 | |
0a7de745 A |
1492 | if (delay_above_pnum < PPNUM_MAX) { |
1493 | printf("pmap_startup() delaying init/free of page nums > 0x%x\n", delay_above_pnum); | |
55e303ae | 1494 | } |
55e303ae | 1495 | |
0a7de745 | 1496 | #endif /* defined(__x86_64__) */ |
55e303ae | 1497 | |
1c79356b | 1498 | /* |
0a7de745 | 1499 | * Initialize and release the page frames. |
1c79356b | 1500 | */ |
f427ee49 | 1501 | kernel_debug_string_early("page_frame_init"); |
1c79356b | 1502 | |
0a7de745 A |
1503 | vm_page_array_beginning_addr = &vm_pages[0]; |
1504 | vm_page_array_ending_addr = &vm_pages[npages]; /* used by ptr packing/unpacking code */ | |
f427ee49 A |
1505 | #if VM_PAGE_PACKED_FROM_ARRAY |
1506 | if (npages >= VM_PAGE_PACKED_FROM_ARRAY) { | |
1507 | panic("pmap_startup(): too many pages to support vm_page packing"); | |
1508 | } | |
1509 | #endif | |
1c79356b | 1510 | |
0a7de745 | 1511 | vm_delayed_count = 0; |
1c79356b | 1512 | |
0a7de745 A |
1513 | absolutetime_to_nanoseconds(mach_absolute_time(), &start_ns); |
1514 | vm_pages_count = 0; | |
1515 | for (i = 0; i < npages; i++) { | |
1516 | /* Did we run out of pages? */ | |
1517 | if (!pmap_next_page(&phys_page)) { | |
1518 | break; | |
1519 | } | |
1c79356b | 1520 | |
0a7de745 A |
1521 | if (phys_page < max_valid_low_ppnum) { |
1522 | ++low_page_count; | |
1523 | } | |
1c79356b | 1524 | |
0a7de745 A |
1525 | /* Are we at high enough pages to delay the rest? */ |
1526 | if (low_page_count > vm_lopage_free_limit && phys_page > delay_above_pnum) { | |
1527 | vm_delayed_count = pmap_free_pages(); | |
1528 | break; | |
1529 | } | |
1530 | ||
1531 | #if defined(__arm__) || defined(__arm64__) | |
1532 | if (i == 0) { | |
1533 | vm_first_phys_ppnum = phys_page; | |
1534 | patch_low_glo_vm_page_info((void *)vm_page_array_beginning_addr, | |
1535 | (void *)vm_page_array_ending_addr, vm_first_phys_ppnum); | |
1536 | } | |
1537 | assert((i + vm_first_phys_ppnum) == phys_page); | |
1538 | #endif | |
1539 | ||
cb323159 A |
1540 | #if defined(__x86_64__) |
1541 | /* The x86 clump freeing code requires increasing ppn's to work correctly */ | |
1542 | if (i > 0) { | |
1543 | assert(phys_page > vm_pages[i - 1].vmp_phys_page); | |
1544 | } | |
1545 | #endif | |
0a7de745 A |
1546 | ++vm_pages_count; |
1547 | vm_page_init(&vm_pages[i], phys_page, FALSE); | |
1548 | if (fill) { | |
1549 | fillPage(phys_page, fillval); | |
1550 | } | |
1551 | if (vm_himemory_mode) { | |
1552 | vm_page_release_startup(&vm_pages[i]); | |
1553 | } | |
1554 | } | |
1555 | vm_page_pages = vm_pages_count; /* used to report to user space */ | |
1556 | ||
1557 | if (!vm_himemory_mode) { | |
1558 | do { | |
1559 | vm_page_release_startup(&vm_pages[--i]); | |
1560 | } while (i != 0); | |
1561 | } | |
1562 | ||
1563 | absolutetime_to_nanoseconds(mach_absolute_time(), &now_ns); | |
1564 | printf("pmap_startup() init/release time: %lld microsec\n", (now_ns - start_ns) / NSEC_PER_USEC); | |
1565 | printf("pmap_startup() delayed init/release of %d pages\n", vm_delayed_count); | |
1566 | ||
f427ee49 | 1567 | #if defined(__LP64__) |
0a7de745 A |
1568 | if ((vm_page_t)(VM_PAGE_UNPACK_PTR(VM_PAGE_PACK_PTR(&vm_pages[0]))) != &vm_pages[0]) { |
1569 | panic("VM_PAGE_PACK_PTR failed on &vm_pages[0] - %p", (void *)&vm_pages[0]); | |
1570 | } | |
1571 | ||
1572 | if ((vm_page_t)(VM_PAGE_UNPACK_PTR(VM_PAGE_PACK_PTR(&vm_pages[vm_pages_count - 1]))) != &vm_pages[vm_pages_count - 1]) { | |
1573 | panic("VM_PAGE_PACK_PTR failed on &vm_pages[vm_pages_count-1] - %p", (void *)&vm_pages[vm_pages_count - 1]); | |
1574 | } | |
1575 | #endif | |
1576 | ||
1577 | VM_CHECK_MEMORYSTATUS; | |
1578 | ||
1579 | /* | |
1580 | * We have to re-align virtual_space_start, | |
1581 | * because pmap_steal_memory has been using it. | |
1582 | */ | |
1583 | virtual_space_start = round_page(virtual_space_start); | |
1584 | *startp = virtual_space_start; | |
1585 | *endp = virtual_space_end; | |
1586 | } | |
1587 | #endif /* MACHINE_PAGES */ | |
1588 | ||
1589 | /* | |
1590 | * Create the zone that represents the vm_pages[] array. Nothing ever allocates | |
1591 | * or frees to this zone. It's just here for reporting purposes via zprint command. | |
1592 | * This needs to be done after all initially delayed pages are put on the free lists. | |
1593 | */ | |
1594 | static void | |
1595 | vm_page_module_init_delayed(void) | |
1596 | { | |
f427ee49 A |
1597 | (void)zone_create_ext("vm pages array", sizeof(struct vm_page), |
1598 | ZC_NOGZALLOC, ZONE_ID_ANY, ^(zone_t z) { | |
1599 | uint64_t vm_page_zone_pages, vm_page_array_zone_data_size; | |
0a7de745 | 1600 | |
f427ee49 A |
1601 | zone_set_exhaustible(z, 0); |
1602 | /* | |
1603 | * Reflect size and usage information for vm_pages[]. | |
1604 | */ | |
0a7de745 | 1605 | |
f427ee49 A |
1606 | z->countavail = (uint32_t)(vm_page_array_ending_addr - vm_pages); |
1607 | z->countfree = z->countavail - vm_pages_count; | |
1608 | zpercpu_get_cpu(z->z_stats, 0)->zs_mem_allocated = | |
1609 | vm_pages_count * sizeof(struct vm_page); | |
1610 | vm_page_array_zone_data_size = (uintptr_t)((void *)vm_page_array_ending_addr - (void *)vm_pages); | |
1611 | vm_page_zone_pages = atop(round_page((vm_offset_t)vm_page_array_zone_data_size)); | |
1612 | z->page_count += vm_page_zone_pages; | |
1613 | /* since zone accounts for these, take them out of stolen */ | |
1614 | VM_PAGE_MOVE_STOLEN(vm_page_zone_pages); | |
1615 | }); | |
0a7de745 A |
1616 | } |
1617 | ||
1618 | /* | |
1619 | * Create the vm_pages zone. This is used for the vm_page structures for the pages | |
1620 | * that are scavanged from other boot time usages by ml_static_mfree(). As such, | |
1621 | * this needs to happen in early VM bootstrap. | |
1622 | */ | |
f427ee49 A |
1623 | |
1624 | __startup_func | |
1625 | static void | |
0a7de745 A |
1626 | vm_page_module_init(void) |
1627 | { | |
1628 | vm_size_t vm_page_with_ppnum_size; | |
39037602 | 1629 | |
0a7de745 A |
1630 | /* |
1631 | * Since the pointers to elements in this zone will be packed, they | |
1632 | * must have appropriate size. Not strictly what sizeof() reports. | |
1633 | */ | |
1634 | vm_page_with_ppnum_size = | |
f427ee49 A |
1635 | (sizeof(struct vm_page_with_ppnum) + (VM_PAGE_PACKED_PTR_ALIGNMENT - 1)) & |
1636 | ~(VM_PAGE_PACKED_PTR_ALIGNMENT - 1); | |
1637 | ||
1638 | vm_page_zone = zone_create_ext("vm pages", vm_page_with_ppnum_size, | |
1639 | ZC_ALLOW_FOREIGN | ZC_NOGZALLOC | ZC_ALIGNMENT_REQUIRED | | |
1640 | ZC_NOCALLOUT, ZONE_ID_ANY, ^(zone_t z) { | |
1641 | #if defined(__LP64__) | |
1642 | zone_set_submap_idx(z, Z_SUBMAP_IDX_VA_RESTRICTED_MAP); | |
1643 | #endif | |
1644 | zone_set_exhaustible(z, 0); | |
1645 | }); | |
1c79356b | 1646 | } |
f427ee49 | 1647 | STARTUP(ZALLOC, STARTUP_RANK_SECOND, vm_page_module_init); |
1c79356b A |
1648 | |
1649 | /* | |
1650 | * Routine: vm_page_create | |
1651 | * Purpose: | |
1652 | * After the VM system is up, machine-dependent code | |
1653 | * may stumble across more physical memory. For example, | |
1654 | * memory that it was reserving for a frame buffer. | |
1655 | * vm_page_create turns this memory into available pages. | |
1656 | */ | |
1657 | ||
1658 | void | |
1659 | vm_page_create( | |
55e303ae A |
1660 | ppnum_t start, |
1661 | ppnum_t end) | |
1c79356b | 1662 | { |
0a7de745 A |
1663 | ppnum_t phys_page; |
1664 | vm_page_t m; | |
1c79356b | 1665 | |
55e303ae | 1666 | for (phys_page = start; |
0a7de745 A |
1667 | phys_page < end; |
1668 | phys_page++) { | |
6d2010ae | 1669 | while ((m = (vm_page_t) vm_page_grab_fictitious_common(phys_page)) |
0a7de745 | 1670 | == VM_PAGE_NULL) { |
1c79356b | 1671 | vm_page_more_fictitious(); |
0a7de745 | 1672 | } |
1c79356b | 1673 | |
d9a64523 | 1674 | m->vmp_fictitious = FALSE; |
0b4c1975 | 1675 | pmap_clear_noencrypt(phys_page); |
6d2010ae | 1676 | |
0a7de745 | 1677 | lck_mtx_lock(&vm_page_queue_free_lock); |
1c79356b | 1678 | vm_page_pages++; |
0a7de745 | 1679 | lck_mtx_unlock(&vm_page_queue_free_lock); |
39037602 | 1680 | vm_page_release(m, FALSE); |
1c79356b A |
1681 | } |
1682 | } | |
1683 | ||
1684 | /* | |
1685 | * vm_page_hash: | |
1686 | * | |
1687 | * Distributes the object/offset key pair among hash buckets. | |
1688 | * | |
55e303ae | 1689 | * NOTE: The bucket count must be a power of 2 |
1c79356b A |
1690 | */ |
1691 | #define vm_page_hash(object, offset) (\ | |
b0d623f7 | 1692 | ( (natural_t)((uintptr_t)object * vm_page_bucket_hash) + ((uint32_t)atop_64(offset) ^ vm_page_bucket_hash))\ |
1c79356b A |
1693 | & vm_page_hash_mask) |
1694 | ||
2d21ac55 | 1695 | |
1c79356b A |
1696 | /* |
1697 | * vm_page_insert: [ internal use only ] | |
1698 | * | |
1699 | * Inserts the given mem entry into the object/object-page | |
1700 | * table and object list. | |
1701 | * | |
1702 | * The object must be locked. | |
1703 | */ | |
1c79356b A |
1704 | void |
1705 | vm_page_insert( | |
0a7de745 A |
1706 | vm_page_t mem, |
1707 | vm_object_t object, | |
1708 | vm_object_offset_t offset) | |
2d21ac55 | 1709 | { |
3e170ce0 A |
1710 | vm_page_insert_internal(mem, object, offset, VM_KERN_MEMORY_NONE, FALSE, TRUE, FALSE, FALSE, NULL); |
1711 | } | |
1712 | ||
1713 | void | |
1714 | vm_page_insert_wired( | |
0a7de745 A |
1715 | vm_page_t mem, |
1716 | vm_object_t object, | |
1717 | vm_object_offset_t offset, | |
3e170ce0 A |
1718 | vm_tag_t tag) |
1719 | { | |
1720 | vm_page_insert_internal(mem, object, offset, tag, FALSE, TRUE, FALSE, FALSE, NULL); | |
2d21ac55 A |
1721 | } |
1722 | ||
4a3eedf9 | 1723 | void |
2d21ac55 | 1724 | vm_page_insert_internal( |
0a7de745 A |
1725 | vm_page_t mem, |
1726 | vm_object_t object, | |
1727 | vm_object_offset_t offset, | |
3e170ce0 | 1728 | vm_tag_t tag, |
0a7de745 A |
1729 | boolean_t queues_lock_held, |
1730 | boolean_t insert_in_hash, | |
1731 | boolean_t batch_pmap_op, | |
1732 | boolean_t batch_accounting, | |
1733 | uint64_t *delayed_ledger_update) | |
1734 | { | |
1735 | vm_page_bucket_t *bucket; | |
1736 | lck_spin_t *bucket_lock; | |
1737 | int hash_id; | |
1738 | task_t owner; | |
1739 | int ledger_idx_volatile; | |
1740 | int ledger_idx_nonvolatile; | |
1741 | int ledger_idx_volatile_compressed; | |
1742 | int ledger_idx_nonvolatile_compressed; | |
1743 | boolean_t do_footprint; | |
1744 | ||
316670eb A |
1745 | #if 0 |
1746 | /* | |
1747 | * we may not hold the page queue lock | |
1748 | * so this check isn't safe to make | |
1749 | */ | |
1c79356b | 1750 | VM_PAGE_CHECK(mem); |
316670eb | 1751 | #endif |
1c79356b | 1752 | |
f427ee49 | 1753 | assertf(page_aligned(offset), "0x%llx\n", offset); |
39236c6e | 1754 | |
d9a64523 | 1755 | assert(!VM_PAGE_WIRED(mem) || mem->vmp_private || mem->vmp_fictitious || (tag != VM_KERN_MEMORY_NONE)); |
3e170ce0 | 1756 | |
fe8ab488 A |
1757 | /* the vm_submap_object is only a placeholder for submaps */ |
1758 | assert(object != vm_submap_object); | |
2d21ac55 A |
1759 | |
1760 | vm_object_lock_assert_exclusive(object); | |
39037602 | 1761 | LCK_MTX_ASSERT(&vm_page_queue_lock, |
0a7de745 A |
1762 | queues_lock_held ? LCK_MTX_ASSERT_OWNED |
1763 | : LCK_MTX_ASSERT_NOTOWNED); | |
5ba3f43e | 1764 | |
0a7de745 | 1765 | if (queues_lock_held == FALSE) { |
39037602 | 1766 | assert(!VM_PAGE_PAGEABLE(mem)); |
0a7de745 | 1767 | } |
3e170ce0 | 1768 | |
b0d623f7 | 1769 | if (insert_in_hash == TRUE) { |
f427ee49 | 1770 | #if DEBUG || VM_PAGE_BUCKETS_CHECK |
0a7de745 | 1771 | if (mem->vmp_tabled || mem->vmp_object) { |
b0d623f7 | 1772 | panic("vm_page_insert: page %p for (obj=%p,off=0x%llx) " |
0a7de745 A |
1773 | "already in (obj=%p,off=0x%llx)", |
1774 | mem, object, offset, VM_PAGE_OBJECT(mem), mem->vmp_offset); | |
1775 | } | |
91447636 | 1776 | #endif |
5ba3f43e A |
1777 | if (object->internal && (offset >= object->vo_size)) { |
1778 | panic("vm_page_insert_internal: (page=%p,obj=%p,off=0x%llx,size=0x%llx) inserted at offset past object bounds", | |
0a7de745 | 1779 | mem, object, offset, object->vo_size); |
5ba3f43e A |
1780 | } |
1781 | ||
b0d623f7 | 1782 | assert(vm_page_lookup(object, offset) == VM_PAGE_NULL); |
0a7de745 | 1783 | |
b0d623f7 A |
1784 | /* |
1785 | * Record the object/offset pair in this page | |
1786 | */ | |
1c79356b | 1787 | |
d9a64523 A |
1788 | mem->vmp_object = VM_PAGE_PACK_OBJECT(object); |
1789 | mem->vmp_offset = offset; | |
1c79356b | 1790 | |
39037602 A |
1791 | #if CONFIG_SECLUDED_MEMORY |
1792 | if (object->eligible_for_secluded) { | |
1793 | vm_page_secluded.eligible_for_secluded++; | |
1794 | } | |
1795 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
1796 | ||
b0d623f7 A |
1797 | /* |
1798 | * Insert it into the object_object/offset hash table | |
1799 | */ | |
1800 | hash_id = vm_page_hash(object, offset); | |
1801 | bucket = &vm_page_buckets[hash_id]; | |
1802 | bucket_lock = &vm_page_bucket_locks[hash_id / BUCKETS_PER_LOCK]; | |
0a7de745 A |
1803 | |
1804 | lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket); | |
1c79356b | 1805 | |
d9a64523 | 1806 | mem->vmp_next_m = bucket->page_list; |
fe8ab488 | 1807 | bucket->page_list = VM_PAGE_PACK_PTR(mem); |
39037602 | 1808 | assert(mem == (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list))); |
fe8ab488 | 1809 | |
1c79356b | 1810 | #if MACH_PAGE_HASH_STATS |
0a7de745 | 1811 | if (++bucket->cur_count > bucket->hi_count) { |
b0d623f7 | 1812 | bucket->hi_count = bucket->cur_count; |
0a7de745 | 1813 | } |
1c79356b | 1814 | #endif /* MACH_PAGE_HASH_STATS */ |
d9a64523 | 1815 | mem->vmp_hashed = TRUE; |
b0d623f7 A |
1816 | lck_spin_unlock(bucket_lock); |
1817 | } | |
6d2010ae | 1818 | |
0a7de745 | 1819 | { |
316670eb | 1820 | unsigned int cache_attr; |
6d2010ae A |
1821 | |
1822 | cache_attr = object->wimg_bits & VM_WIMG_MASK; | |
1823 | ||
1824 | if (cache_attr != VM_WIMG_USE_DEFAULT) { | |
316670eb | 1825 | PMAP_SET_CACHE_ATTR(mem, object, cache_attr, batch_pmap_op); |
6d2010ae A |
1826 | } |
1827 | } | |
1c79356b A |
1828 | /* |
1829 | * Now link into the object's list of backed pages. | |
1830 | */ | |
0a7de745 | 1831 | vm_page_queue_enter(&object->memq, mem, vmp_listq); |
3e170ce0 | 1832 | object->memq_hint = mem; |
d9a64523 | 1833 | mem->vmp_tabled = TRUE; |
1c79356b A |
1834 | |
1835 | /* | |
1836 | * Show that the object has one more resident page. | |
1837 | */ | |
1838 | ||
1839 | object->resident_page_count++; | |
b0d623f7 | 1840 | if (VM_PAGE_WIRED(mem)) { |
0a7de745 A |
1841 | assert(mem->vmp_wire_count > 0); |
1842 | VM_OBJECT_WIRED_PAGE_UPDATE_START(object); | |
1843 | VM_OBJECT_WIRED_PAGE_ADD(object, mem); | |
1844 | VM_OBJECT_WIRED_PAGE_UPDATE_END(object, tag); | |
b0d623f7 A |
1845 | } |
1846 | assert(object->resident_page_count >= object->wired_page_count); | |
91447636 | 1847 | |
f427ee49 A |
1848 | #if DEVELOPMENT || DEBUG |
1849 | if (object->object_is_shared_cache && | |
1850 | object->pager != NULL && | |
1851 | object->pager->mo_pager_ops == &shared_region_pager_ops) { | |
1852 | int new, old; | |
1853 | assert(!object->internal); | |
1854 | new = OSAddAtomic(+1, &shared_region_pagers_resident_count); | |
1855 | do { | |
1856 | old = shared_region_pagers_resident_peak; | |
1857 | } while (old < new && | |
1858 | !OSCompareAndSwap(old, new, &shared_region_pagers_resident_peak)); | |
1859 | } | |
1860 | #endif /* DEVELOPMENT || DEBUG */ | |
1861 | ||
0a7de745 | 1862 | if (batch_accounting == FALSE) { |
3e170ce0 A |
1863 | if (object->internal) { |
1864 | OSAddAtomic(1, &vm_page_internal_count); | |
1865 | } else { | |
1866 | OSAddAtomic(1, &vm_page_external_count); | |
1867 | } | |
39236c6e A |
1868 | } |
1869 | ||
1870 | /* | |
1871 | * It wouldn't make sense to insert a "reusable" page in | |
1872 | * an object (the page would have been marked "reusable" only | |
1873 | * at the time of a madvise(MADV_FREE_REUSABLE) if it was already | |
1874 | * in the object at that time). | |
1875 | * But a page could be inserted in a "all_reusable" object, if | |
1876 | * something faults it in (a vm_read() from another task or a | |
1877 | * "use-after-free" issue in user space, for example). It can | |
1878 | * also happen if we're relocating a page from that object to | |
1879 | * a different physical page during a physically-contiguous | |
1880 | * allocation. | |
1881 | */ | |
d9a64523 | 1882 | assert(!mem->vmp_reusable); |
39037602 | 1883 | if (object->all_reusable) { |
39236c6e A |
1884 | OSAddAtomic(+1, &vm_page_stats_reusable.reusable_count); |
1885 | } | |
2d21ac55 | 1886 | |
d9a64523 | 1887 | if (object->purgable == VM_PURGABLE_DENY && |
0a7de745 | 1888 | !object->vo_ledger_tag) { |
fe8ab488 A |
1889 | owner = TASK_NULL; |
1890 | } else { | |
d9a64523 A |
1891 | owner = VM_OBJECT_OWNER(object); |
1892 | vm_object_ledger_tag_ledgers(object, | |
0a7de745 A |
1893 | &ledger_idx_volatile, |
1894 | &ledger_idx_nonvolatile, | |
1895 | &ledger_idx_volatile_compressed, | |
1896 | &ledger_idx_nonvolatile_compressed, | |
1897 | &do_footprint); | |
fe8ab488 A |
1898 | } |
1899 | if (owner && | |
1900 | (object->purgable == VM_PURGABLE_NONVOLATILE || | |
0a7de745 A |
1901 | object->purgable == VM_PURGABLE_DENY || |
1902 | VM_PAGE_WIRED(mem))) { | |
1903 | if (delayed_ledger_update) { | |
3e170ce0 | 1904 | *delayed_ledger_update += PAGE_SIZE; |
0a7de745 | 1905 | } else { |
3e170ce0 A |
1906 | /* more non-volatile bytes */ |
1907 | ledger_credit(owner->ledger, | |
0a7de745 A |
1908 | ledger_idx_nonvolatile, |
1909 | PAGE_SIZE); | |
d9a64523 A |
1910 | if (do_footprint) { |
1911 | /* more footprint */ | |
1912 | ledger_credit(owner->ledger, | |
0a7de745 A |
1913 | task_ledgers.phys_footprint, |
1914 | PAGE_SIZE); | |
d9a64523 | 1915 | } |
3e170ce0 | 1916 | } |
fe8ab488 | 1917 | } else if (owner && |
0a7de745 A |
1918 | (object->purgable == VM_PURGABLE_VOLATILE || |
1919 | object->purgable == VM_PURGABLE_EMPTY)) { | |
1920 | assert(!VM_PAGE_WIRED(mem)); | |
fe8ab488 A |
1921 | /* more volatile bytes */ |
1922 | ledger_credit(owner->ledger, | |
0a7de745 A |
1923 | ledger_idx_volatile, |
1924 | PAGE_SIZE); | |
fe8ab488 A |
1925 | } |
1926 | ||
b0d623f7 A |
1927 | if (object->purgable == VM_PURGABLE_VOLATILE) { |
1928 | if (VM_PAGE_WIRED(mem)) { | |
fe8ab488 | 1929 | OSAddAtomic(+1, &vm_page_purgeable_wired_count); |
b0d623f7 | 1930 | } else { |
fe8ab488 | 1931 | OSAddAtomic(+1, &vm_page_purgeable_count); |
b0d623f7 | 1932 | } |
593a1d5f | 1933 | } else if (object->purgable == VM_PURGABLE_EMPTY && |
0a7de745 | 1934 | mem->vmp_q_state == VM_PAGE_ON_THROTTLED_Q) { |
b0d623f7 A |
1935 | /* |
1936 | * This page belongs to a purged VM object but hasn't | |
1937 | * been purged (because it was "busy"). | |
1938 | * It's in the "throttled" queue and hence not | |
1939 | * visible to vm_pageout_scan(). Move it to a pageable | |
1940 | * queue, so that it can eventually be reclaimed, instead | |
1941 | * of lingering in the "empty" object. | |
1942 | */ | |
0a7de745 | 1943 | if (queues_lock_held == FALSE) { |
b0d623f7 | 1944 | vm_page_lockspin_queues(); |
0a7de745 | 1945 | } |
593a1d5f | 1946 | vm_page_deactivate(mem); |
0a7de745 | 1947 | if (queues_lock_held == FALSE) { |
2d21ac55 | 1948 | vm_page_unlock_queues(); |
0a7de745 | 1949 | } |
91447636 | 1950 | } |
fe8ab488 A |
1951 | |
1952 | #if VM_OBJECT_TRACKING_OP_MODIFIED | |
1953 | if (vm_object_tracking_inited && | |
1954 | object->internal && | |
1955 | object->resident_page_count == 0 && | |
1956 | object->pager == NULL && | |
1957 | object->shadow != NULL && | |
1958 | object->shadow->copy == object) { | |
1959 | void *bt[VM_OBJECT_TRACKING_BTDEPTH]; | |
1960 | int numsaved = 0; | |
1961 | ||
0a7de745 | 1962 | numsaved = OSBacktrace(bt, VM_OBJECT_TRACKING_BTDEPTH); |
fe8ab488 | 1963 | btlog_add_entry(vm_object_tracking_btlog, |
0a7de745 A |
1964 | object, |
1965 | VM_OBJECT_TRACKING_OP_MODIFIED, | |
1966 | bt, | |
1967 | numsaved); | |
fe8ab488 A |
1968 | } |
1969 | #endif /* VM_OBJECT_TRACKING_OP_MODIFIED */ | |
1c79356b A |
1970 | } |
1971 | ||
1972 | /* | |
1973 | * vm_page_replace: | |
1974 | * | |
1975 | * Exactly like vm_page_insert, except that we first | |
1976 | * remove any existing page at the given offset in object. | |
1977 | * | |
b0d623f7 | 1978 | * The object must be locked. |
1c79356b | 1979 | */ |
1c79356b A |
1980 | void |
1981 | vm_page_replace( | |
0a7de745 A |
1982 | vm_page_t mem, |
1983 | vm_object_t object, | |
1984 | vm_object_offset_t offset) | |
1c79356b | 1985 | { |
0c530ab8 | 1986 | vm_page_bucket_t *bucket; |
0a7de745 A |
1987 | vm_page_t found_m = VM_PAGE_NULL; |
1988 | lck_spin_t *bucket_lock; | |
1989 | int hash_id; | |
1c79356b | 1990 | |
316670eb A |
1991 | #if 0 |
1992 | /* | |
1993 | * we don't hold the page queue lock | |
1994 | * so this check isn't safe to make | |
1995 | */ | |
1c79356b | 1996 | VM_PAGE_CHECK(mem); |
316670eb | 1997 | #endif |
2d21ac55 | 1998 | vm_object_lock_assert_exclusive(object); |
f427ee49 | 1999 | #if DEBUG || VM_PAGE_BUCKETS_CHECK |
0a7de745 | 2000 | if (mem->vmp_tabled || mem->vmp_object) { |
91447636 | 2001 | panic("vm_page_replace: page %p for (obj=%p,off=0x%llx) " |
0a7de745 A |
2002 | "already in (obj=%p,off=0x%llx)", |
2003 | mem, object, offset, VM_PAGE_OBJECT(mem), mem->vmp_offset); | |
2004 | } | |
91447636 | 2005 | #endif |
39037602 A |
2006 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED); |
2007 | ||
2008 | assert(!VM_PAGE_PAGEABLE(mem)); | |
2009 | ||
1c79356b A |
2010 | /* |
2011 | * Record the object/offset pair in this page | |
2012 | */ | |
d9a64523 A |
2013 | mem->vmp_object = VM_PAGE_PACK_OBJECT(object); |
2014 | mem->vmp_offset = offset; | |
1c79356b A |
2015 | |
2016 | /* | |
2017 | * Insert it into the object_object/offset hash table, | |
2018 | * replacing any page that might have been there. | |
2019 | */ | |
2020 | ||
b0d623f7 A |
2021 | hash_id = vm_page_hash(object, offset); |
2022 | bucket = &vm_page_buckets[hash_id]; | |
2023 | bucket_lock = &vm_page_bucket_locks[hash_id / BUCKETS_PER_LOCK]; | |
2024 | ||
0a7de745 | 2025 | lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket); |
0c530ab8 | 2026 | |
fe8ab488 A |
2027 | if (bucket->page_list) { |
2028 | vm_page_packed_t *mp = &bucket->page_list; | |
39037602 | 2029 | vm_page_t m = (vm_page_t)(VM_PAGE_UNPACK_PTR(*mp)); |
0c530ab8 | 2030 | |
1c79356b | 2031 | do { |
39037602 A |
2032 | /* |
2033 | * compare packed object pointers | |
2034 | */ | |
d9a64523 | 2035 | if (m->vmp_object == mem->vmp_object && m->vmp_offset == offset) { |
1c79356b | 2036 | /* |
0c530ab8 | 2037 | * Remove old page from hash list |
1c79356b | 2038 | */ |
d9a64523 A |
2039 | *mp = m->vmp_next_m; |
2040 | m->vmp_hashed = FALSE; | |
2041 | m->vmp_next_m = VM_PAGE_PACK_PTR(NULL); | |
1c79356b | 2042 | |
0c530ab8 | 2043 | found_m = m; |
1c79356b A |
2044 | break; |
2045 | } | |
d9a64523 | 2046 | mp = &m->vmp_next_m; |
39037602 | 2047 | } while ((m = (vm_page_t)(VM_PAGE_UNPACK_PTR(*mp)))); |
0c530ab8 | 2048 | |
d9a64523 | 2049 | mem->vmp_next_m = bucket->page_list; |
1c79356b | 2050 | } else { |
d9a64523 | 2051 | mem->vmp_next_m = VM_PAGE_PACK_PTR(NULL); |
1c79356b | 2052 | } |
0c530ab8 A |
2053 | /* |
2054 | * insert new page at head of hash list | |
2055 | */ | |
fe8ab488 | 2056 | bucket->page_list = VM_PAGE_PACK_PTR(mem); |
d9a64523 | 2057 | mem->vmp_hashed = TRUE; |
0c530ab8 | 2058 | |
b0d623f7 | 2059 | lck_spin_unlock(bucket_lock); |
1c79356b | 2060 | |
0c530ab8 | 2061 | if (found_m) { |
0a7de745 | 2062 | /* |
0c530ab8 A |
2063 | * there was already a page at the specified |
2064 | * offset for this object... remove it from | |
2065 | * the object and free it back to the free list | |
2066 | */ | |
b0d623f7 | 2067 | vm_page_free_unlocked(found_m, FALSE); |
91447636 | 2068 | } |
3e170ce0 | 2069 | vm_page_insert_internal(mem, object, offset, VM_KERN_MEMORY_NONE, FALSE, FALSE, FALSE, FALSE, NULL); |
1c79356b A |
2070 | } |
2071 | ||
2072 | /* | |
2073 | * vm_page_remove: [ internal use only ] | |
2074 | * | |
2075 | * Removes the given mem entry from the object/offset-page | |
2076 | * table and the object page list. | |
2077 | * | |
b0d623f7 | 2078 | * The object must be locked. |
1c79356b A |
2079 | */ |
2080 | ||
2081 | void | |
2082 | vm_page_remove( | |
0a7de745 A |
2083 | vm_page_t mem, |
2084 | boolean_t remove_from_hash) | |
1c79356b | 2085 | { |
b0d623f7 | 2086 | vm_page_bucket_t *bucket; |
0a7de745 A |
2087 | vm_page_t this; |
2088 | lck_spin_t *bucket_lock; | |
2089 | int hash_id; | |
2090 | task_t owner; | |
2091 | vm_object_t m_object; | |
2092 | int ledger_idx_volatile; | |
2093 | int ledger_idx_nonvolatile; | |
2094 | int ledger_idx_volatile_compressed; | |
2095 | int ledger_idx_nonvolatile_compressed; | |
2096 | int do_footprint; | |
39037602 A |
2097 | |
2098 | m_object = VM_PAGE_OBJECT(mem); | |
1c79356b | 2099 | |
39037602 | 2100 | vm_object_lock_assert_exclusive(m_object); |
d9a64523 A |
2101 | assert(mem->vmp_tabled); |
2102 | assert(!mem->vmp_cleaning); | |
2103 | assert(!mem->vmp_laundry); | |
39037602 A |
2104 | |
2105 | if (VM_PAGE_PAGEABLE(mem)) { | |
2106 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
2107 | } | |
316670eb A |
2108 | #if 0 |
2109 | /* | |
2110 | * we don't hold the page queue lock | |
2111 | * so this check isn't safe to make | |
2112 | */ | |
1c79356b | 2113 | VM_PAGE_CHECK(mem); |
316670eb | 2114 | #endif |
b0d623f7 A |
2115 | if (remove_from_hash == TRUE) { |
2116 | /* | |
2117 | * Remove from the object_object/offset hash table | |
2118 | */ | |
d9a64523 | 2119 | hash_id = vm_page_hash(m_object, mem->vmp_offset); |
b0d623f7 A |
2120 | bucket = &vm_page_buckets[hash_id]; |
2121 | bucket_lock = &vm_page_bucket_locks[hash_id / BUCKETS_PER_LOCK]; | |
91447636 | 2122 | |
0a7de745 | 2123 | lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket); |
1c79356b | 2124 | |
39037602 | 2125 | if ((this = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list))) == mem) { |
b0d623f7 | 2126 | /* optimize for common case */ |
1c79356b | 2127 | |
d9a64523 | 2128 | bucket->page_list = mem->vmp_next_m; |
b0d623f7 | 2129 | } else { |
0a7de745 | 2130 | vm_page_packed_t *prev; |
1c79356b | 2131 | |
d9a64523 | 2132 | for (prev = &this->vmp_next_m; |
0a7de745 A |
2133 | (this = (vm_page_t)(VM_PAGE_UNPACK_PTR(*prev))) != mem; |
2134 | prev = &this->vmp_next_m) { | |
b0d623f7 | 2135 | continue; |
0a7de745 | 2136 | } |
d9a64523 | 2137 | *prev = this->vmp_next_m; |
b0d623f7 | 2138 | } |
1c79356b | 2139 | #if MACH_PAGE_HASH_STATS |
b0d623f7 | 2140 | bucket->cur_count--; |
1c79356b | 2141 | #endif /* MACH_PAGE_HASH_STATS */ |
d9a64523 A |
2142 | mem->vmp_hashed = FALSE; |
2143 | this->vmp_next_m = VM_PAGE_PACK_PTR(NULL); | |
b0d623f7 A |
2144 | lck_spin_unlock(bucket_lock); |
2145 | } | |
1c79356b A |
2146 | /* |
2147 | * Now remove from the object's list of backed pages. | |
2148 | */ | |
2149 | ||
3e170ce0 | 2150 | vm_page_remove_internal(mem); |
1c79356b A |
2151 | |
2152 | /* | |
2153 | * And show that the object has one fewer resident | |
2154 | * page. | |
2155 | */ | |
2156 | ||
39037602 A |
2157 | assert(m_object->resident_page_count > 0); |
2158 | m_object->resident_page_count--; | |
6d2010ae | 2159 | |
f427ee49 A |
2160 | #if DEVELOPMENT || DEBUG |
2161 | if (m_object->object_is_shared_cache && | |
2162 | m_object->pager != NULL && | |
2163 | m_object->pager->mo_pager_ops == &shared_region_pager_ops) { | |
2164 | assert(!m_object->internal); | |
2165 | OSAddAtomic(-1, &shared_region_pagers_resident_count); | |
2166 | } | |
2167 | #endif /* DEVELOPMENT || DEBUG */ | |
2168 | ||
39037602 | 2169 | if (m_object->internal) { |
fe8ab488 | 2170 | #if DEBUG |
39236c6e | 2171 | assert(vm_page_internal_count); |
fe8ab488 A |
2172 | #endif /* DEBUG */ |
2173 | ||
39236c6e A |
2174 | OSAddAtomic(-1, &vm_page_internal_count); |
2175 | } else { | |
2176 | assert(vm_page_external_count); | |
2177 | OSAddAtomic(-1, &vm_page_external_count); | |
fe8ab488 | 2178 | |
d9a64523 | 2179 | if (mem->vmp_xpmapped) { |
fe8ab488 A |
2180 | assert(vm_page_xpmapped_external_count); |
2181 | OSAddAtomic(-1, &vm_page_xpmapped_external_count); | |
2182 | } | |
39236c6e | 2183 | } |
d9a64523 A |
2184 | if (!m_object->internal && |
2185 | m_object->cached_list.next && | |
2186 | m_object->cached_list.prev) { | |
0a7de745 | 2187 | if (m_object->resident_page_count == 0) { |
39037602 | 2188 | vm_object_cache_remove(m_object); |
0a7de745 | 2189 | } |
6d2010ae A |
2190 | } |
2191 | ||
b0d623f7 | 2192 | if (VM_PAGE_WIRED(mem)) { |
d9a64523 | 2193 | assert(mem->vmp_wire_count > 0); |
5ba3f43e A |
2194 | VM_OBJECT_WIRED_PAGE_UPDATE_START(m_object); |
2195 | VM_OBJECT_WIRED_PAGE_REMOVE(m_object, mem); | |
2196 | VM_OBJECT_WIRED_PAGE_UPDATE_END(m_object, m_object->wire_tag); | |
b0d623f7 | 2197 | } |
39037602 | 2198 | assert(m_object->resident_page_count >= |
0a7de745 | 2199 | m_object->wired_page_count); |
d9a64523 | 2200 | if (mem->vmp_reusable) { |
39037602 A |
2201 | assert(m_object->reusable_page_count > 0); |
2202 | m_object->reusable_page_count--; | |
2203 | assert(m_object->reusable_page_count <= | |
0a7de745 | 2204 | m_object->resident_page_count); |
d9a64523 | 2205 | mem->vmp_reusable = FALSE; |
b0d623f7 A |
2206 | OSAddAtomic(-1, &vm_page_stats_reusable.reusable_count); |
2207 | vm_page_stats_reusable.reused_remove++; | |
39037602 | 2208 | } else if (m_object->all_reusable) { |
b0d623f7 A |
2209 | OSAddAtomic(-1, &vm_page_stats_reusable.reusable_count); |
2210 | vm_page_stats_reusable.reused_remove++; | |
2211 | } | |
1c79356b | 2212 | |
d9a64523 | 2213 | if (m_object->purgable == VM_PURGABLE_DENY && |
0a7de745 | 2214 | !m_object->vo_ledger_tag) { |
fe8ab488 A |
2215 | owner = TASK_NULL; |
2216 | } else { | |
d9a64523 A |
2217 | owner = VM_OBJECT_OWNER(m_object); |
2218 | vm_object_ledger_tag_ledgers(m_object, | |
0a7de745 A |
2219 | &ledger_idx_volatile, |
2220 | &ledger_idx_nonvolatile, | |
2221 | &ledger_idx_volatile_compressed, | |
2222 | &ledger_idx_nonvolatile_compressed, | |
2223 | &do_footprint); | |
fe8ab488 A |
2224 | } |
2225 | if (owner && | |
39037602 | 2226 | (m_object->purgable == VM_PURGABLE_NONVOLATILE || |
0a7de745 A |
2227 | m_object->purgable == VM_PURGABLE_DENY || |
2228 | VM_PAGE_WIRED(mem))) { | |
fe8ab488 A |
2229 | /* less non-volatile bytes */ |
2230 | ledger_debit(owner->ledger, | |
0a7de745 A |
2231 | ledger_idx_nonvolatile, |
2232 | PAGE_SIZE); | |
d9a64523 A |
2233 | if (do_footprint) { |
2234 | /* less footprint */ | |
2235 | ledger_debit(owner->ledger, | |
0a7de745 A |
2236 | task_ledgers.phys_footprint, |
2237 | PAGE_SIZE); | |
d9a64523 | 2238 | } |
fe8ab488 | 2239 | } else if (owner && |
0a7de745 A |
2240 | (m_object->purgable == VM_PURGABLE_VOLATILE || |
2241 | m_object->purgable == VM_PURGABLE_EMPTY)) { | |
2242 | assert(!VM_PAGE_WIRED(mem)); | |
fe8ab488 A |
2243 | /* less volatile bytes */ |
2244 | ledger_debit(owner->ledger, | |
0a7de745 A |
2245 | ledger_idx_volatile, |
2246 | PAGE_SIZE); | |
fe8ab488 | 2247 | } |
39037602 | 2248 | if (m_object->purgable == VM_PURGABLE_VOLATILE) { |
b0d623f7 A |
2249 | if (VM_PAGE_WIRED(mem)) { |
2250 | assert(vm_page_purgeable_wired_count > 0); | |
2251 | OSAddAtomic(-1, &vm_page_purgeable_wired_count); | |
2252 | } else { | |
2253 | assert(vm_page_purgeable_count > 0); | |
2254 | OSAddAtomic(-1, &vm_page_purgeable_count); | |
2255 | } | |
91447636 | 2256 | } |
5ba3f43e | 2257 | |
0a7de745 | 2258 | if (m_object->set_cache_attr == TRUE) { |
39037602 | 2259 | pmap_set_cache_attributes(VM_PAGE_GET_PHYS_PAGE(mem), 0); |
0a7de745 | 2260 | } |
6d2010ae | 2261 | |
d9a64523 A |
2262 | mem->vmp_tabled = FALSE; |
2263 | mem->vmp_object = 0; | |
2264 | mem->vmp_offset = (vm_object_offset_t) -1; | |
1c79356b A |
2265 | } |
2266 | ||
b0d623f7 | 2267 | |
1c79356b A |
2268 | /* |
2269 | * vm_page_lookup: | |
2270 | * | |
2271 | * Returns the page associated with the object/offset | |
2272 | * pair specified; if none is found, VM_PAGE_NULL is returned. | |
2273 | * | |
2274 | * The object must be locked. No side effects. | |
2275 | */ | |
2276 | ||
0a7de745 | 2277 | #define VM_PAGE_HASH_LOOKUP_THRESHOLD 10 |
3e170ce0 A |
2278 | |
2279 | #if DEBUG_VM_PAGE_LOOKUP | |
2d21ac55 | 2280 | |
3e170ce0 | 2281 | struct { |
0a7de745 A |
2282 | uint64_t vpl_total; |
2283 | uint64_t vpl_empty_obj; | |
2284 | uint64_t vpl_bucket_NULL; | |
2285 | uint64_t vpl_hit_hint; | |
2286 | uint64_t vpl_hit_hint_next; | |
2287 | uint64_t vpl_hit_hint_prev; | |
2288 | uint64_t vpl_fast; | |
2289 | uint64_t vpl_slow; | |
2290 | uint64_t vpl_hit; | |
2291 | uint64_t vpl_miss; | |
2292 | ||
2293 | uint64_t vpl_fast_elapsed; | |
2294 | uint64_t vpl_slow_elapsed; | |
3e170ce0 A |
2295 | } vm_page_lookup_stats __attribute__((aligned(8))); |
2296 | ||
2297 | #endif | |
2298 | ||
0a7de745 | 2299 | #define KDP_VM_PAGE_WALK_MAX 1000 |
3e170ce0 A |
2300 | |
2301 | vm_page_t | |
2302 | kdp_vm_page_lookup( | |
0a7de745 A |
2303 | vm_object_t object, |
2304 | vm_object_offset_t offset) | |
3e170ce0 A |
2305 | { |
2306 | vm_page_t cur_page; | |
2307 | int num_traversed = 0; | |
2308 | ||
2309 | if (not_in_kdp) { | |
2310 | panic("panic: kdp_vm_page_lookup done outside of kernel debugger"); | |
2311 | } | |
2312 | ||
0a7de745 | 2313 | vm_page_queue_iterate(&object->memq, cur_page, vmp_listq) { |
d9a64523 | 2314 | if (cur_page->vmp_offset == offset) { |
3e170ce0 A |
2315 | return cur_page; |
2316 | } | |
2317 | num_traversed++; | |
2318 | ||
2319 | if (num_traversed >= KDP_VM_PAGE_WALK_MAX) { | |
2320 | return VM_PAGE_NULL; | |
2321 | } | |
2322 | } | |
2323 | ||
2324 | return VM_PAGE_NULL; | |
2325 | } | |
91447636 | 2326 | |
1c79356b A |
2327 | vm_page_t |
2328 | vm_page_lookup( | |
0a7de745 A |
2329 | vm_object_t object, |
2330 | vm_object_offset_t offset) | |
1c79356b | 2331 | { |
0a7de745 | 2332 | vm_page_t mem; |
b0d623f7 | 2333 | vm_page_bucket_t *bucket; |
0a7de745 A |
2334 | vm_page_queue_entry_t qe; |
2335 | lck_spin_t *bucket_lock = NULL; | |
2336 | int hash_id; | |
3e170ce0 | 2337 | #if DEBUG_VM_PAGE_LOOKUP |
0a7de745 | 2338 | uint64_t start, elapsed; |
91447636 | 2339 | |
3e170ce0 A |
2340 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_total); |
2341 | #endif | |
2d21ac55 | 2342 | vm_object_lock_assert_held(object); |
f427ee49 | 2343 | assertf(page_aligned(offset), "offset 0x%llx\n", offset); |
3e170ce0 A |
2344 | |
2345 | if (object->resident_page_count == 0) { | |
2346 | #if DEBUG_VM_PAGE_LOOKUP | |
2347 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_empty_obj); | |
2348 | #endif | |
0a7de745 | 2349 | return VM_PAGE_NULL; |
3e170ce0 A |
2350 | } |
2351 | ||
91447636 | 2352 | mem = object->memq_hint; |
2d21ac55 | 2353 | |
91447636 | 2354 | if (mem != VM_PAGE_NULL) { |
39037602 | 2355 | assert(VM_PAGE_OBJECT(mem) == object); |
2d21ac55 | 2356 | |
d9a64523 | 2357 | if (mem->vmp_offset == offset) { |
3e170ce0 A |
2358 | #if DEBUG_VM_PAGE_LOOKUP |
2359 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_hit_hint); | |
2360 | #endif | |
0a7de745 | 2361 | return mem; |
91447636 | 2362 | } |
d9a64523 | 2363 | qe = (vm_page_queue_entry_t)vm_page_queue_next(&mem->vmp_listq); |
2d21ac55 | 2364 | |
0a7de745 A |
2365 | if (!vm_page_queue_end(&object->memq, qe)) { |
2366 | vm_page_t next_page; | |
91447636 | 2367 | |
39037602 A |
2368 | next_page = (vm_page_t)((uintptr_t)qe); |
2369 | assert(VM_PAGE_OBJECT(next_page) == object); | |
2d21ac55 | 2370 | |
d9a64523 | 2371 | if (next_page->vmp_offset == offset) { |
91447636 | 2372 | object->memq_hint = next_page; /* new hint */ |
3e170ce0 A |
2373 | #if DEBUG_VM_PAGE_LOOKUP |
2374 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_hit_hint_next); | |
2375 | #endif | |
0a7de745 | 2376 | return next_page; |
91447636 A |
2377 | } |
2378 | } | |
d9a64523 | 2379 | qe = (vm_page_queue_entry_t)vm_page_queue_prev(&mem->vmp_listq); |
2d21ac55 | 2380 | |
0a7de745 | 2381 | if (!vm_page_queue_end(&object->memq, qe)) { |
91447636 A |
2382 | vm_page_t prev_page; |
2383 | ||
39037602 A |
2384 | prev_page = (vm_page_t)((uintptr_t)qe); |
2385 | assert(VM_PAGE_OBJECT(prev_page) == object); | |
2d21ac55 | 2386 | |
d9a64523 | 2387 | if (prev_page->vmp_offset == offset) { |
91447636 | 2388 | object->memq_hint = prev_page; /* new hint */ |
3e170ce0 A |
2389 | #if DEBUG_VM_PAGE_LOOKUP |
2390 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_hit_hint_prev); | |
2391 | #endif | |
0a7de745 | 2392 | return prev_page; |
91447636 A |
2393 | } |
2394 | } | |
2395 | } | |
1c79356b | 2396 | /* |
2d21ac55 | 2397 | * Search the hash table for this object/offset pair |
1c79356b | 2398 | */ |
b0d623f7 A |
2399 | hash_id = vm_page_hash(object, offset); |
2400 | bucket = &vm_page_buckets[hash_id]; | |
1c79356b | 2401 | |
2d21ac55 A |
2402 | /* |
2403 | * since we hold the object lock, we are guaranteed that no | |
2404 | * new pages can be inserted into this object... this in turn | |
2405 | * guarantess that the page we're looking for can't exist | |
2406 | * if the bucket it hashes to is currently NULL even when looked | |
0a7de745 | 2407 | * at outside the scope of the hash bucket lock... this is a |
2d21ac55 A |
2408 | * really cheap optimiztion to avoid taking the lock |
2409 | */ | |
fe8ab488 | 2410 | if (!bucket->page_list) { |
3e170ce0 A |
2411 | #if DEBUG_VM_PAGE_LOOKUP |
2412 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_bucket_NULL); | |
2413 | #endif | |
0a7de745 | 2414 | return VM_PAGE_NULL; |
2d21ac55 | 2415 | } |
0c530ab8 | 2416 | |
3e170ce0 A |
2417 | #if DEBUG_VM_PAGE_LOOKUP |
2418 | start = mach_absolute_time(); | |
2419 | #endif | |
2420 | if (object->resident_page_count <= VM_PAGE_HASH_LOOKUP_THRESHOLD) { | |
316670eb | 2421 | /* |
3e170ce0 A |
2422 | * on average, it's roughly 3 times faster to run a short memq list |
2423 | * than to take the spin lock and go through the hash list | |
316670eb | 2424 | */ |
39037602 | 2425 | mem = (vm_page_t)vm_page_queue_first(&object->memq); |
3e170ce0 | 2426 | |
39037602 | 2427 | while (!vm_page_queue_end(&object->memq, (vm_page_queue_entry_t)mem)) { |
0a7de745 | 2428 | if (mem->vmp_offset == offset) { |
3e170ce0 | 2429 | break; |
0a7de745 | 2430 | } |
3e170ce0 | 2431 | |
d9a64523 | 2432 | mem = (vm_page_t)vm_page_queue_next(&mem->vmp_listq); |
3e170ce0 | 2433 | } |
0a7de745 | 2434 | if (vm_page_queue_end(&object->memq, (vm_page_queue_entry_t)mem)) { |
3e170ce0 | 2435 | mem = NULL; |
0a7de745 | 2436 | } |
3e170ce0 | 2437 | } else { |
0a7de745 | 2438 | vm_page_object_t packed_object; |
39037602 A |
2439 | |
2440 | packed_object = VM_PAGE_PACK_OBJECT(object); | |
3e170ce0 A |
2441 | |
2442 | bucket_lock = &vm_page_bucket_locks[hash_id / BUCKETS_PER_LOCK]; | |
2443 | ||
0a7de745 | 2444 | lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket); |
3e170ce0 | 2445 | |
39037602 | 2446 | for (mem = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list)); |
0a7de745 A |
2447 | mem != VM_PAGE_NULL; |
2448 | mem = (vm_page_t)(VM_PAGE_UNPACK_PTR(mem->vmp_next_m))) { | |
3e170ce0 A |
2449 | #if 0 |
2450 | /* | |
2451 | * we don't hold the page queue lock | |
2452 | * so this check isn't safe to make | |
2453 | */ | |
2454 | VM_PAGE_CHECK(mem); | |
316670eb | 2455 | #endif |
0a7de745 | 2456 | if ((mem->vmp_object == packed_object) && (mem->vmp_offset == offset)) { |
3e170ce0 | 2457 | break; |
0a7de745 | 2458 | } |
3e170ce0 A |
2459 | } |
2460 | lck_spin_unlock(bucket_lock); | |
1c79356b | 2461 | } |
55e303ae | 2462 | |
3e170ce0 A |
2463 | #if DEBUG_VM_PAGE_LOOKUP |
2464 | elapsed = mach_absolute_time() - start; | |
2465 | ||
2466 | if (bucket_lock) { | |
2467 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_slow); | |
2468 | OSAddAtomic64(elapsed, &vm_page_lookup_stats.vpl_slow_elapsed); | |
2469 | } else { | |
2470 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_fast); | |
2471 | OSAddAtomic64(elapsed, &vm_page_lookup_stats.vpl_fast_elapsed); | |
2472 | } | |
0a7de745 | 2473 | if (mem != VM_PAGE_NULL) { |
3e170ce0 | 2474 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_hit); |
0a7de745 A |
2475 | } else { |
2476 | OSAddAtomic64(1, &vm_page_lookup_stats.vpl_miss); | |
2477 | } | |
3e170ce0 | 2478 | #endif |
91447636 | 2479 | if (mem != VM_PAGE_NULL) { |
39037602 | 2480 | assert(VM_PAGE_OBJECT(mem) == object); |
91447636 | 2481 | |
3e170ce0 A |
2482 | object->memq_hint = mem; |
2483 | } | |
0a7de745 | 2484 | return mem; |
91447636 A |
2485 | } |
2486 | ||
2487 | ||
1c79356b A |
2488 | /* |
2489 | * vm_page_rename: | |
2490 | * | |
2491 | * Move the given memory entry from its | |
2492 | * current object to the specified target object/offset. | |
2493 | * | |
2494 | * The object must be locked. | |
2495 | */ | |
2496 | void | |
2497 | vm_page_rename( | |
0a7de745 A |
2498 | vm_page_t mem, |
2499 | vm_object_t new_object, | |
2500 | vm_object_offset_t new_offset) | |
1c79356b | 2501 | { |
0a7de745 A |
2502 | boolean_t internal_to_external, external_to_internal; |
2503 | vm_tag_t tag; | |
2504 | vm_object_t m_object; | |
39236c6e | 2505 | |
39037602 | 2506 | m_object = VM_PAGE_OBJECT(mem); |
2d21ac55 | 2507 | |
39037602 | 2508 | assert(m_object != new_object); |
0a7de745 | 2509 | assert(m_object); |
3e170ce0 | 2510 | |
1c79356b | 2511 | /* |
d9a64523 | 2512 | * Changes to mem->vmp_object require the page lock because |
1c79356b A |
2513 | * the pageout daemon uses that lock to get the object. |
2514 | */ | |
b0d623f7 | 2515 | vm_page_lockspin_queues(); |
1c79356b | 2516 | |
39236c6e A |
2517 | internal_to_external = FALSE; |
2518 | external_to_internal = FALSE; | |
2519 | ||
d9a64523 | 2520 | if (mem->vmp_q_state == VM_PAGE_ON_ACTIVE_LOCAL_Q) { |
39236c6e A |
2521 | /* |
2522 | * it's much easier to get the vm_page_pageable_xxx accounting correct | |
2523 | * if we first move the page to the active queue... it's going to end | |
2524 | * up there anyway, and we don't do vm_page_rename's frequently enough | |
2525 | * for this to matter. | |
2526 | */ | |
39037602 | 2527 | vm_page_queues_remove(mem, FALSE); |
39236c6e A |
2528 | vm_page_activate(mem); |
2529 | } | |
39037602 A |
2530 | if (VM_PAGE_PAGEABLE(mem)) { |
2531 | if (m_object->internal && !new_object->internal) { | |
39236c6e A |
2532 | internal_to_external = TRUE; |
2533 | } | |
39037602 | 2534 | if (!m_object->internal && new_object->internal) { |
39236c6e A |
2535 | external_to_internal = TRUE; |
2536 | } | |
2537 | } | |
2538 | ||
39037602 | 2539 | tag = m_object->wire_tag; |
0a7de745 | 2540 | vm_page_remove(mem, TRUE); |
3e170ce0 | 2541 | vm_page_insert_internal(mem, new_object, new_offset, tag, TRUE, TRUE, FALSE, FALSE, NULL); |
1c79356b | 2542 | |
39236c6e A |
2543 | if (internal_to_external) { |
2544 | vm_page_pageable_internal_count--; | |
2545 | vm_page_pageable_external_count++; | |
2546 | } else if (external_to_internal) { | |
2547 | vm_page_pageable_external_count--; | |
2548 | vm_page_pageable_internal_count++; | |
2549 | } | |
2550 | ||
1c79356b A |
2551 | vm_page_unlock_queues(); |
2552 | } | |
2553 | ||
2554 | /* | |
2555 | * vm_page_init: | |
2556 | * | |
2557 | * Initialize the fields in a new page. | |
2558 | * This takes a structure with random values and initializes it | |
2559 | * so that it can be given to vm_page_release or vm_page_insert. | |
2560 | */ | |
2561 | void | |
2562 | vm_page_init( | |
0a7de745 A |
2563 | vm_page_t mem, |
2564 | ppnum_t phys_page, | |
2565 | boolean_t lopage) | |
1c79356b | 2566 | { |
0a7de745 A |
2567 | uint_t i; |
2568 | uintptr_t *p; | |
2569 | ||
91447636 | 2570 | assert(phys_page); |
7ddcb079 | 2571 | |
0a7de745 | 2572 | #if DEBUG |
7ddcb079 A |
2573 | if ((phys_page != vm_page_fictitious_addr) && (phys_page != vm_page_guard_addr)) { |
2574 | if (!(pmap_valid_page(phys_page))) { | |
2575 | panic("vm_page_init: non-DRAM phys_page 0x%x\n", phys_page); | |
2576 | } | |
2577 | } | |
0a7de745 A |
2578 | #endif /* DEBUG */ |
2579 | ||
2580 | /* | |
2581 | * Initialize the fields of the vm_page. If adding any new fields to vm_page, | |
2582 | * try to use initial values which match 0. This minimizes the number of writes | |
2583 | * needed for boot-time initialization. | |
2584 | * | |
2585 | * Kernel bzero() isn't an inline yet, so do it by hand for performance. | |
2586 | */ | |
2587 | assert(VM_PAGE_NOT_ON_Q == 0); | |
2588 | assert(sizeof(*mem) % sizeof(uintptr_t) == 0); | |
2589 | for (p = (uintptr_t *)(void *)mem, i = sizeof(*mem) / sizeof(uintptr_t); i != 0; --i) { | |
2590 | *p++ = 0; | |
2591 | } | |
2592 | mem->vmp_offset = (vm_object_offset_t)-1; | |
2593 | mem->vmp_busy = TRUE; | |
2594 | mem->vmp_lopage = lopage; | |
39037602 A |
2595 | |
2596 | VM_PAGE_SET_PHYS_PAGE(mem, phys_page); | |
6d2010ae A |
2597 | #if 0 |
2598 | /* | |
2599 | * we're leaving this turned off for now... currently pages | |
2600 | * come off the free list and are either immediately dirtied/referenced | |
2601 | * due to zero-fill or COW faults, or are used to read or write files... | |
2602 | * in the file I/O case, the UPL mechanism takes care of clearing | |
2603 | * the state of the HW ref/mod bits in a somewhat fragile way. | |
2604 | * Since we may change the way this works in the future (to toughen it up), | |
2605 | * I'm leaving this as a reminder of where these bits could get cleared | |
2606 | */ | |
2607 | ||
2608 | /* | |
2609 | * make sure both the h/w referenced and modified bits are | |
0a7de745 | 2610 | * clear at this point... we are especially dependent on |
6d2010ae A |
2611 | * not finding a 'stale' h/w modified in a number of spots |
2612 | * once this page goes back into use | |
2613 | */ | |
2614 | pmap_clear_refmod(phys_page, VM_MEM_MODIFIED | VM_MEM_REFERENCED); | |
2615 | #endif | |
1c79356b A |
2616 | } |
2617 | ||
2618 | /* | |
2619 | * vm_page_grab_fictitious: | |
2620 | * | |
2621 | * Remove a fictitious page from the free list. | |
2622 | * Returns VM_PAGE_NULL if there are no free pages. | |
2623 | */ | |
0a7de745 A |
2624 | int c_vm_page_grab_fictitious = 0; |
2625 | int c_vm_page_grab_fictitious_failed = 0; | |
2626 | int c_vm_page_release_fictitious = 0; | |
2627 | int c_vm_page_more_fictitious = 0; | |
1c79356b A |
2628 | |
2629 | vm_page_t | |
2d21ac55 | 2630 | vm_page_grab_fictitious_common( |
b0d623f7 | 2631 | ppnum_t phys_addr) |
1c79356b | 2632 | { |
0a7de745 | 2633 | vm_page_t m; |
6d2010ae | 2634 | |
f427ee49 | 2635 | if ((m = (vm_page_t)zalloc_noblock(vm_page_zone))) { |
0b4c1975 | 2636 | vm_page_init(m, phys_addr, FALSE); |
d9a64523 | 2637 | m->vmp_fictitious = TRUE; |
1c79356b | 2638 | |
6d2010ae | 2639 | c_vm_page_grab_fictitious++; |
0a7de745 | 2640 | } else { |
6d2010ae | 2641 | c_vm_page_grab_fictitious_failed++; |
0a7de745 | 2642 | } |
6d2010ae | 2643 | |
1c79356b A |
2644 | return m; |
2645 | } | |
2646 | ||
2d21ac55 A |
2647 | vm_page_t |
2648 | vm_page_grab_fictitious(void) | |
2649 | { | |
2650 | return vm_page_grab_fictitious_common(vm_page_fictitious_addr); | |
2651 | } | |
2652 | ||
5ba3f43e A |
2653 | int vm_guard_count; |
2654 | ||
2655 | ||
2d21ac55 A |
2656 | vm_page_t |
2657 | vm_page_grab_guard(void) | |
2658 | { | |
5ba3f43e A |
2659 | vm_page_t page; |
2660 | page = vm_page_grab_fictitious_common(vm_page_guard_addr); | |
0a7de745 A |
2661 | if (page) { |
2662 | OSAddAtomic(1, &vm_guard_count); | |
2663 | } | |
5ba3f43e | 2664 | return page; |
2d21ac55 A |
2665 | } |
2666 | ||
6d2010ae | 2667 | |
1c79356b A |
2668 | /* |
2669 | * vm_page_release_fictitious: | |
2670 | * | |
6d2010ae | 2671 | * Release a fictitious page to the zone pool |
1c79356b | 2672 | */ |
1c79356b A |
2673 | void |
2674 | vm_page_release_fictitious( | |
6d2010ae | 2675 | vm_page_t m) |
1c79356b | 2676 | { |
d9a64523 A |
2677 | assert((m->vmp_q_state == VM_PAGE_NOT_ON_Q) || (m->vmp_q_state == VM_PAGE_IS_WIRED)); |
2678 | assert(m->vmp_fictitious); | |
39037602 | 2679 | assert(VM_PAGE_GET_PHYS_PAGE(m) == vm_page_fictitious_addr || |
0a7de745 | 2680 | VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr); |
1c79356b | 2681 | |
5ba3f43e | 2682 | |
0a7de745 A |
2683 | if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) { |
2684 | OSAddAtomic(-1, &vm_guard_count); | |
2685 | } | |
5ba3f43e | 2686 | |
1c79356b | 2687 | c_vm_page_release_fictitious++; |
6d2010ae | 2688 | |
91447636 | 2689 | zfree(vm_page_zone, m); |
1c79356b A |
2690 | } |
2691 | ||
2692 | /* | |
2693 | * vm_page_more_fictitious: | |
2694 | * | |
6d2010ae | 2695 | * Add more fictitious pages to the zone. |
1c79356b A |
2696 | * Allowed to block. This routine is way intimate |
2697 | * with the zones code, for several reasons: | |
2698 | * 1. we need to carve some page structures out of physical | |
2699 | * memory before zones work, so they _cannot_ come from | |
f427ee49 | 2700 | * the zone restricted submap. |
1c79356b A |
2701 | * 2. the zone needs to be collectable in order to prevent |
2702 | * growth without bound. These structures are used by | |
2703 | * the device pager (by the hundreds and thousands), as | |
2704 | * private pages for pageout, and as blocking pages for | |
2705 | * pagein. Temporary bursts in demand should not result in | |
2706 | * permanent allocation of a resource. | |
2707 | * 3. To smooth allocation humps, we allocate single pages | |
2708 | * with kernel_memory_allocate(), and cram them into the | |
6d2010ae | 2709 | * zone. |
1c79356b A |
2710 | */ |
2711 | ||
0a7de745 A |
2712 | void |
2713 | vm_page_more_fictitious(void) | |
1c79356b | 2714 | { |
0a7de745 A |
2715 | vm_offset_t addr; |
2716 | kern_return_t retval; | |
1c79356b A |
2717 | |
2718 | c_vm_page_more_fictitious++; | |
2719 | ||
1c79356b | 2720 | /* |
f427ee49 A |
2721 | * Allocate a single page from the zone restricted submap. Do not wait |
2722 | * if no physical pages are immediately available, and do not zero the | |
2723 | * space. We need our own blocking lock here to prevent having multiple, | |
2724 | * simultaneous requests from piling up on the zone restricted submap | |
2725 | * lock. | |
2726 | * Exactly one (of our) threads should be potentially waiting on the map | |
2727 | * lock. If winner is not vm-privileged, then the page allocation will | |
2728 | * fail, and it will temporarily block here in the vm_page_wait(). | |
1c79356b | 2729 | */ |
b0d623f7 | 2730 | lck_mtx_lock(&vm_page_alloc_lock); |
1c79356b A |
2731 | /* |
2732 | * If another thread allocated space, just bail out now. | |
2733 | */ | |
f427ee49 | 2734 | if (os_atomic_load(&vm_page_zone->countfree, relaxed) > 5) { |
1c79356b A |
2735 | /* |
2736 | * The number "5" is a small number that is larger than the | |
2737 | * number of fictitious pages that any single caller will | |
2738 | * attempt to allocate. Otherwise, a thread will attempt to | |
2739 | * acquire a fictitious page (vm_page_grab_fictitious), fail, | |
2740 | * release all of the resources and locks already acquired, | |
2741 | * and then call this routine. This routine finds the pages | |
2742 | * that the caller released, so fails to allocate new space. | |
2743 | * The process repeats infinitely. The largest known number | |
2744 | * of fictitious pages required in this manner is 2. 5 is | |
2745 | * simply a somewhat larger number. | |
2746 | */ | |
b0d623f7 | 2747 | lck_mtx_unlock(&vm_page_alloc_lock); |
1c79356b A |
2748 | return; |
2749 | } | |
2750 | ||
f427ee49 A |
2751 | retval = kernel_memory_allocate(zone_submap(vm_page_zone), |
2752 | &addr, PAGE_SIZE, 0, KMA_ZERO | KMA_KOBJECT | KMA_NOPAGEWAIT, | |
2753 | VM_KERN_MEMORY_ZONE); | |
2754 | ||
0a7de745 | 2755 | if (retval != KERN_SUCCESS) { |
1c79356b | 2756 | /* |
6d2010ae | 2757 | * No page was available. Drop the |
1c79356b A |
2758 | * lock to give another thread a chance at it, and |
2759 | * wait for the pageout daemon to make progress. | |
2760 | */ | |
b0d623f7 | 2761 | lck_mtx_unlock(&vm_page_alloc_lock); |
1c79356b A |
2762 | vm_page_wait(THREAD_UNINT); |
2763 | return; | |
2764 | } | |
39236c6e | 2765 | |
7ddcb079 | 2766 | zcram(vm_page_zone, addr, PAGE_SIZE); |
6d2010ae | 2767 | |
b0d623f7 | 2768 | lck_mtx_unlock(&vm_page_alloc_lock); |
1c79356b A |
2769 | } |
2770 | ||
1c79356b A |
2771 | |
2772 | /* | |
2773 | * vm_pool_low(): | |
2774 | * | |
2775 | * Return true if it is not likely that a non-vm_privileged thread | |
2776 | * can get memory without blocking. Advisory only, since the | |
2777 | * situation may change under us. | |
2778 | */ | |
2779 | int | |
2780 | vm_pool_low(void) | |
2781 | { | |
2782 | /* No locking, at worst we will fib. */ | |
0a7de745 | 2783 | return vm_page_free_count <= vm_page_free_reserved; |
1c79356b A |
2784 | } |
2785 | ||
d9a64523 A |
2786 | boolean_t vm_darkwake_mode = FALSE; |
2787 | ||
2788 | /* | |
2789 | * vm_update_darkwake_mode(): | |
2790 | * | |
2791 | * Tells the VM that the system is in / out of darkwake. | |
2792 | * | |
2793 | * Today, the VM only lowers/raises the background queue target | |
2794 | * so as to favor consuming more/less background pages when | |
2795 | * darwake is ON/OFF. | |
2796 | * | |
2797 | * We might need to do more things in the future. | |
2798 | */ | |
2799 | ||
2800 | void | |
2801 | vm_update_darkwake_mode(boolean_t darkwake_mode) | |
2802 | { | |
2803 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED); | |
2804 | ||
2805 | vm_page_lockspin_queues(); | |
2806 | ||
2807 | if (vm_darkwake_mode == darkwake_mode) { | |
2808 | /* | |
2809 | * No change. | |
2810 | */ | |
2811 | vm_page_unlock_queues(); | |
2812 | return; | |
2813 | } | |
2814 | ||
2815 | vm_darkwake_mode = darkwake_mode; | |
2816 | ||
2817 | if (vm_darkwake_mode == TRUE) { | |
2818 | #if CONFIG_BACKGROUND_QUEUE | |
2819 | ||
2820 | /* save background target to restore later */ | |
2821 | vm_page_background_target_snapshot = vm_page_background_target; | |
2822 | ||
2823 | /* target is set to 0...no protection for background pages */ | |
2824 | vm_page_background_target = 0; | |
2825 | ||
2826 | #endif /* CONFIG_BACKGROUND_QUEUE */ | |
d9a64523 A |
2827 | } else if (vm_darkwake_mode == FALSE) { |
2828 | #if CONFIG_BACKGROUND_QUEUE | |
2829 | ||
2830 | if (vm_page_background_target_snapshot) { | |
2831 | vm_page_background_target = vm_page_background_target_snapshot; | |
2832 | } | |
2833 | #endif /* CONFIG_BACKGROUND_QUEUE */ | |
2834 | } | |
2835 | vm_page_unlock_queues(); | |
2836 | } | |
0c530ab8 | 2837 | |
39037602 A |
2838 | #if CONFIG_BACKGROUND_QUEUE |
2839 | ||
2840 | void | |
2841 | vm_page_update_background_state(vm_page_t mem) | |
2842 | { | |
0a7de745 | 2843 | if (vm_page_background_mode == VM_PAGE_BG_DISABLED) { |
39037602 | 2844 | return; |
0a7de745 | 2845 | } |
39037602 | 2846 | |
0a7de745 | 2847 | if (mem->vmp_in_background == FALSE) { |
39037602 | 2848 | return; |
0a7de745 | 2849 | } |
39037602 | 2850 | |
0a7de745 | 2851 | task_t my_task = current_task(); |
d9a64523 A |
2852 | |
2853 | if (my_task) { | |
2854 | if (task_get_darkwake_mode(my_task)) { | |
2855 | return; | |
2856 | } | |
2857 | } | |
2858 | ||
39037602 | 2859 | #if BACKGROUNDQ_BASED_ON_QOS |
0a7de745 | 2860 | if (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_QOS) <= THREAD_QOS_LEGACY) { |
39037602 | 2861 | return; |
0a7de745 | 2862 | } |
39037602 | 2863 | #else |
39037602 | 2864 | if (my_task) { |
0a7de745 | 2865 | if (proc_get_effective_task_policy(my_task, TASK_POLICY_DARWIN_BG)) { |
39037602 | 2866 | return; |
0a7de745 | 2867 | } |
39037602 A |
2868 | } |
2869 | #endif | |
2870 | vm_page_lockspin_queues(); | |
2871 | ||
d9a64523 | 2872 | mem->vmp_in_background = FALSE; |
39037602 A |
2873 | vm_page_background_promoted_count++; |
2874 | ||
2875 | vm_page_remove_from_backgroundq(mem); | |
2876 | ||
2877 | vm_page_unlock_queues(); | |
2878 | } | |
2879 | ||
2880 | ||
2881 | void | |
2882 | vm_page_assign_background_state(vm_page_t mem) | |
2883 | { | |
0a7de745 | 2884 | if (vm_page_background_mode == VM_PAGE_BG_DISABLED) { |
39037602 | 2885 | return; |
0a7de745 | 2886 | } |
39037602 | 2887 | |
0a7de745 | 2888 | task_t my_task = current_task(); |
d9a64523 A |
2889 | |
2890 | if (my_task) { | |
2891 | if (task_get_darkwake_mode(my_task)) { | |
2892 | mem->vmp_in_background = TRUE; | |
2893 | return; | |
2894 | } | |
2895 | } | |
2896 | ||
39037602 | 2897 | #if BACKGROUNDQ_BASED_ON_QOS |
0a7de745 A |
2898 | if (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_QOS) <= THREAD_QOS_LEGACY) { |
2899 | mem->vmp_in_background = TRUE; | |
2900 | } else { | |
2901 | mem->vmp_in_background = FALSE; | |
2902 | } | |
39037602 | 2903 | #else |
0a7de745 | 2904 | if (my_task) { |
d9a64523 | 2905 | mem->vmp_in_background = proc_get_effective_task_policy(my_task, TASK_POLICY_DARWIN_BG); |
0a7de745 | 2906 | } |
39037602 A |
2907 | #endif |
2908 | } | |
2909 | ||
2910 | ||
2911 | void | |
2912 | vm_page_remove_from_backgroundq( | |
0a7de745 | 2913 | vm_page_t mem) |
39037602 | 2914 | { |
0a7de745 | 2915 | vm_object_t m_object; |
39037602 A |
2916 | |
2917 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
2918 | ||
d9a64523 | 2919 | if (mem->vmp_on_backgroundq) { |
0a7de745 | 2920 | vm_page_queue_remove(&vm_page_queue_background, mem, vmp_backgroundq); |
39037602 | 2921 | |
d9a64523 A |
2922 | mem->vmp_backgroundq.next = 0; |
2923 | mem->vmp_backgroundq.prev = 0; | |
2924 | mem->vmp_on_backgroundq = FALSE; | |
0a7de745 | 2925 | |
39037602 A |
2926 | vm_page_background_count--; |
2927 | ||
2928 | m_object = VM_PAGE_OBJECT(mem); | |
2929 | ||
0a7de745 | 2930 | if (m_object->internal) { |
39037602 | 2931 | vm_page_background_internal_count--; |
0a7de745 | 2932 | } else { |
39037602 | 2933 | vm_page_background_external_count--; |
0a7de745 | 2934 | } |
39037602 | 2935 | } else { |
d9a64523 | 2936 | assert(VM_PAGE_UNPACK_PTR(mem->vmp_backgroundq.next) == (uintptr_t)NULL && |
0a7de745 | 2937 | VM_PAGE_UNPACK_PTR(mem->vmp_backgroundq.prev) == (uintptr_t)NULL); |
39037602 A |
2938 | } |
2939 | } | |
2940 | ||
2941 | ||
2942 | void | |
2943 | vm_page_add_to_backgroundq( | |
0a7de745 A |
2944 | vm_page_t mem, |
2945 | boolean_t first) | |
2946 | { | |
2947 | vm_object_t m_object; | |
39037602 A |
2948 | |
2949 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
2950 | ||
0a7de745 | 2951 | if (vm_page_background_mode == VM_PAGE_BG_DISABLED) { |
39037602 | 2952 | return; |
0a7de745 | 2953 | } |
39037602 | 2954 | |
d9a64523 | 2955 | if (mem->vmp_on_backgroundq == FALSE) { |
39037602 A |
2956 | m_object = VM_PAGE_OBJECT(mem); |
2957 | ||
0a7de745 | 2958 | if (vm_page_background_exclude_external && !m_object->internal) { |
39037602 | 2959 | return; |
0a7de745 | 2960 | } |
39037602 | 2961 | |
0a7de745 A |
2962 | if (first == TRUE) { |
2963 | vm_page_queue_enter_first(&vm_page_queue_background, mem, vmp_backgroundq); | |
2964 | } else { | |
2965 | vm_page_queue_enter(&vm_page_queue_background, mem, vmp_backgroundq); | |
2966 | } | |
d9a64523 | 2967 | mem->vmp_on_backgroundq = TRUE; |
0a7de745 | 2968 | |
39037602 A |
2969 | vm_page_background_count++; |
2970 | ||
0a7de745 | 2971 | if (m_object->internal) { |
39037602 | 2972 | vm_page_background_internal_count++; |
0a7de745 | 2973 | } else { |
39037602 | 2974 | vm_page_background_external_count++; |
0a7de745 | 2975 | } |
39037602 A |
2976 | } |
2977 | } | |
2978 | ||
d9a64523 | 2979 | #endif /* CONFIG_BACKGROUND_QUEUE */ |
0c530ab8 A |
2980 | |
2981 | /* | |
0a7de745 A |
2982 | * This can be switched to FALSE to help debug drivers |
2983 | * that are having problems with memory > 4G. | |
0c530ab8 | 2984 | */ |
0a7de745 | 2985 | boolean_t vm_himemory_mode = TRUE; |
0c530ab8 A |
2986 | |
2987 | /* | |
2988 | * this interface exists to support hardware controllers | |
2989 | * incapable of generating DMAs with more than 32 bits | |
2990 | * of address on platforms with physical memory > 4G... | |
2991 | */ | |
0a7de745 A |
2992 | unsigned int vm_lopages_allocated_q = 0; |
2993 | unsigned int vm_lopages_allocated_cpm_success = 0; | |
2994 | unsigned int vm_lopages_allocated_cpm_failed = 0; | |
f427ee49 | 2995 | vm_page_queue_head_t vm_lopage_queue_free VM_PAGE_PACKED_ALIGNED; |
0c530ab8 A |
2996 | |
2997 | vm_page_t | |
2998 | vm_page_grablo(void) | |
2999 | { | |
0a7de745 | 3000 | vm_page_t mem; |
0c530ab8 | 3001 | |
0a7de745 A |
3002 | if (vm_lopage_needed == FALSE) { |
3003 | return vm_page_grab(); | |
3004 | } | |
0c530ab8 | 3005 | |
b0d623f7 | 3006 | lck_mtx_lock_spin(&vm_page_queue_free_lock); |
0c530ab8 | 3007 | |
0a7de745 A |
3008 | if (!vm_page_queue_empty(&vm_lopage_queue_free)) { |
3009 | vm_page_queue_remove_first(&vm_lopage_queue_free, mem, vmp_pageq); | |
0b4c1975 | 3010 | assert(vm_lopage_free_count); |
d9a64523 A |
3011 | assert(mem->vmp_q_state == VM_PAGE_ON_FREE_LOPAGE_Q); |
3012 | mem->vmp_q_state = VM_PAGE_NOT_ON_Q; | |
0c530ab8 | 3013 | |
0a7de745 | 3014 | vm_lopage_free_count--; |
0b4c1975 A |
3015 | vm_lopages_allocated_q++; |
3016 | ||
0a7de745 | 3017 | if (vm_lopage_free_count < vm_lopage_lowater) { |
0b4c1975 | 3018 | vm_lopage_refill = TRUE; |
0a7de745 | 3019 | } |
0c530ab8 | 3020 | |
0b4c1975 | 3021 | lck_mtx_unlock(&vm_page_queue_free_lock); |
39037602 A |
3022 | |
3023 | #if CONFIG_BACKGROUND_QUEUE | |
3024 | vm_page_assign_background_state(mem); | |
3025 | #endif | |
2d21ac55 | 3026 | } else { |
0b4c1975 A |
3027 | lck_mtx_unlock(&vm_page_queue_free_lock); |
3028 | ||
0a7de745 | 3029 | if (cpm_allocate(PAGE_SIZE, &mem, atop(PPNUM_MAX), 0, FALSE, KMA_LOMEM) != KERN_SUCCESS) { |
0b4c1975 A |
3030 | lck_mtx_lock_spin(&vm_page_queue_free_lock); |
3031 | vm_lopages_allocated_cpm_failed++; | |
3032 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3033 | ||
0a7de745 | 3034 | return VM_PAGE_NULL; |
0b4c1975 | 3035 | } |
d9a64523 | 3036 | assert(mem->vmp_q_state == VM_PAGE_NOT_ON_Q); |
39037602 | 3037 | |
d9a64523 | 3038 | mem->vmp_busy = TRUE; |
0b4c1975 A |
3039 | |
3040 | vm_page_lockspin_queues(); | |
0a7de745 | 3041 | |
d9a64523 | 3042 | mem->vmp_gobbled = FALSE; |
0b4c1975 A |
3043 | vm_page_gobble_count--; |
3044 | vm_page_wire_count--; | |
3045 | ||
3046 | vm_lopages_allocated_cpm_success++; | |
3047 | vm_page_unlock_queues(); | |
0c530ab8 | 3048 | } |
d9a64523 A |
3049 | assert(mem->vmp_busy); |
3050 | assert(!mem->vmp_pmapped); | |
3051 | assert(!mem->vmp_wpmapped); | |
39037602 | 3052 | assert(!pmap_is_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem))); |
0b4c1975 | 3053 | |
39037602 | 3054 | VM_PAGE_ZERO_PAGEQ_ENTRY(mem); |
0c530ab8 | 3055 | |
d9a64523 | 3056 | disable_preemption(); |
f427ee49 | 3057 | *PERCPU_GET(vm_page_grab_count) += 1; |
d9a64523 A |
3058 | VM_DEBUG_EVENT(vm_page_grab, VM_PAGE_GRAB, DBG_FUNC_NONE, 0, 1, 0, 0); |
3059 | enable_preemption(); | |
3060 | ||
0a7de745 | 3061 | return mem; |
0c530ab8 A |
3062 | } |
3063 | ||
6d2010ae | 3064 | |
1c79356b A |
3065 | /* |
3066 | * vm_page_grab: | |
3067 | * | |
2d21ac55 A |
3068 | * first try to grab a page from the per-cpu free list... |
3069 | * this must be done while pre-emption is disabled... if | |
0a7de745 | 3070 | * a page is available, we're done... |
2d21ac55 A |
3071 | * if no page is available, grab the vm_page_queue_free_lock |
3072 | * and see if current number of free pages would allow us | |
0a7de745 | 3073 | * to grab at least 1... if not, return VM_PAGE_NULL as before... |
2d21ac55 | 3074 | * if there are pages available, disable preemption and |
0a7de745 | 3075 | * recheck the state of the per-cpu free list... we could |
2d21ac55 | 3076 | * have been preempted and moved to a different cpu, or |
0a7de745 | 3077 | * some other thread could have re-filled it... if still |
2d21ac55 A |
3078 | * empty, figure out how many pages we can steal from the |
3079 | * global free queue and move to the per-cpu queue... | |
3080 | * return 1 of these pages when done... only wakeup the | |
0a7de745 | 3081 | * pageout_scan thread if we moved pages from the global |
2d21ac55 A |
3082 | * list... no need for the wakeup if we've satisfied the |
3083 | * request from the per-cpu queue. | |
1c79356b A |
3084 | */ |
3085 | ||
39037602 A |
3086 | #if CONFIG_SECLUDED_MEMORY |
3087 | vm_page_t vm_page_grab_secluded(void); | |
3088 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
1c79356b | 3089 | |
0a7de745 A |
3090 | static inline void |
3091 | vm_page_grab_diags(void); | |
3092 | ||
1c79356b | 3093 | vm_page_t |
39037602 | 3094 | vm_page_grab(void) |
1c79356b | 3095 | { |
0a7de745 | 3096 | return vm_page_grab_options(VM_PAGE_GRAB_OPTIONS_NONE); |
39037602 | 3097 | } |
2d21ac55 | 3098 | |
5ba3f43e A |
3099 | #if HIBERNATION |
3100 | boolean_t hibernate_rebuild_needed = FALSE; | |
3101 | #endif /* HIBERNATION */ | |
3102 | ||
39037602 A |
3103 | vm_page_t |
3104 | vm_page_grab_options( | |
3105 | int grab_options) | |
3106 | { | |
0a7de745 | 3107 | vm_page_t mem; |
2d21ac55 A |
3108 | |
3109 | disable_preemption(); | |
3110 | ||
f427ee49 | 3111 | if ((mem = *PERCPU_GET(free_pages))) { |
2d21ac55 | 3112 | return_page_from_cpu_list: |
d9a64523 | 3113 | assert(mem->vmp_q_state == VM_PAGE_ON_FREE_LOCAL_Q); |
39037602 | 3114 | |
5ba3f43e A |
3115 | #if HIBERNATION |
3116 | if (hibernate_rebuild_needed) { | |
3117 | panic("%s:%d should not modify cpu->free_pages while hibernating", __FUNCTION__, __LINE__); | |
3118 | } | |
3119 | #endif /* HIBERNATION */ | |
0a7de745 A |
3120 | |
3121 | vm_page_grab_diags(); | |
f427ee49 A |
3122 | |
3123 | vm_offset_t pcpu_base = current_percpu_base(); | |
3124 | *PERCPU_GET_WITH_BASE(pcpu_base, vm_page_grab_count) += 1; | |
3125 | *PERCPU_GET_WITH_BASE(pcpu_base, free_pages) = mem->vmp_snext; | |
d9a64523 | 3126 | VM_DEBUG_EVENT(vm_page_grab, VM_PAGE_GRAB, DBG_FUNC_NONE, grab_options, 0, 0, 0); |
2d21ac55 | 3127 | |
0a7de745 | 3128 | enable_preemption(); |
39037602 | 3129 | VM_PAGE_ZERO_PAGEQ_ENTRY(mem); |
d9a64523 A |
3130 | mem->vmp_q_state = VM_PAGE_NOT_ON_Q; |
3131 | ||
3132 | assert(mem->vmp_listq.next == 0 && mem->vmp_listq.prev == 0); | |
3133 | assert(mem->vmp_tabled == FALSE); | |
3134 | assert(mem->vmp_object == 0); | |
3135 | assert(!mem->vmp_laundry); | |
0a7de745 | 3136 | ASSERT_PMAP_FREE(mem); |
d9a64523 A |
3137 | assert(mem->vmp_busy); |
3138 | assert(!mem->vmp_pmapped); | |
3139 | assert(!mem->vmp_wpmapped); | |
39037602 | 3140 | assert(!pmap_is_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem))); |
2d21ac55 | 3141 | |
39037602 A |
3142 | #if CONFIG_BACKGROUND_QUEUE |
3143 | vm_page_assign_background_state(mem); | |
3144 | #endif | |
2d21ac55 A |
3145 | return mem; |
3146 | } | |
3147 | enable_preemption(); | |
3148 | ||
1c79356b | 3149 | |
1c79356b A |
3150 | /* |
3151 | * Optionally produce warnings if the wire or gobble | |
3152 | * counts exceed some threshold. | |
3153 | */ | |
fe8ab488 A |
3154 | #if VM_PAGE_WIRE_COUNT_WARNING |
3155 | if (vm_page_wire_count >= VM_PAGE_WIRE_COUNT_WARNING) { | |
1c79356b | 3156 | printf("mk: vm_page_grab(): high wired page count of %d\n", |
0a7de745 | 3157 | vm_page_wire_count); |
1c79356b | 3158 | } |
fe8ab488 A |
3159 | #endif |
3160 | #if VM_PAGE_GOBBLE_COUNT_WARNING | |
3161 | if (vm_page_gobble_count >= VM_PAGE_GOBBLE_COUNT_WARNING) { | |
1c79356b | 3162 | printf("mk: vm_page_grab(): high gobbled page count of %d\n", |
0a7de745 | 3163 | vm_page_gobble_count); |
1c79356b | 3164 | } |
fe8ab488 | 3165 | #endif |
39037602 | 3166 | |
0a7de745 A |
3167 | /* |
3168 | * If free count is low and we have delayed pages from early boot, | |
3169 | * get one of those instead. | |
3170 | */ | |
3171 | if (__improbable(vm_delayed_count > 0 && | |
3172 | vm_page_free_count <= vm_page_free_target && | |
3173 | (mem = vm_get_delayed_page(grab_options)) != NULL)) { | |
3174 | return mem; | |
3175 | } | |
3176 | ||
b0d623f7 A |
3177 | lck_mtx_lock_spin(&vm_page_queue_free_lock); |
3178 | ||
1c79356b A |
3179 | /* |
3180 | * Only let privileged threads (involved in pageout) | |
3181 | * dip into the reserved pool. | |
3182 | */ | |
1c79356b | 3183 | if ((vm_page_free_count < vm_page_free_reserved) && |
91447636 | 3184 | !(current_thread()->options & TH_OPT_VMPRIV)) { |
39037602 | 3185 | /* no page for us in the free queue... */ |
b0d623f7 | 3186 | lck_mtx_unlock(&vm_page_queue_free_lock); |
1c79356b | 3187 | mem = VM_PAGE_NULL; |
39037602 A |
3188 | |
3189 | #if CONFIG_SECLUDED_MEMORY | |
3190 | /* ... but can we try and grab from the secluded queue? */ | |
3191 | if (vm_page_secluded_count > 0 && | |
3192 | ((grab_options & VM_PAGE_GRAB_SECLUDED) || | |
0a7de745 | 3193 | task_can_use_secluded_mem(current_task(), TRUE))) { |
39037602 A |
3194 | mem = vm_page_grab_secluded(); |
3195 | if (grab_options & VM_PAGE_GRAB_SECLUDED) { | |
3196 | vm_page_secluded.grab_for_iokit++; | |
3197 | if (mem) { | |
3198 | vm_page_secluded.grab_for_iokit_success++; | |
3199 | } | |
3200 | } | |
3201 | if (mem) { | |
3202 | VM_CHECK_MEMORYSTATUS; | |
d9a64523 A |
3203 | |
3204 | disable_preemption(); | |
0a7de745 | 3205 | vm_page_grab_diags(); |
f427ee49 | 3206 | *PERCPU_GET(vm_page_grab_count) += 1; |
d9a64523 A |
3207 | VM_DEBUG_EVENT(vm_page_grab, VM_PAGE_GRAB, DBG_FUNC_NONE, grab_options, 0, 0, 0); |
3208 | enable_preemption(); | |
3209 | ||
39037602 A |
3210 | return mem; |
3211 | } | |
3212 | } | |
3213 | #else /* CONFIG_SECLUDED_MEMORY */ | |
3214 | (void) grab_options; | |
3215 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
0a7de745 A |
3216 | } else { |
3217 | vm_page_t head; | |
3218 | vm_page_t tail; | |
3219 | unsigned int pages_to_steal; | |
3220 | unsigned int color; | |
3221 | unsigned int clump_end, sub_count; | |
1c79356b | 3222 | |
0a7de745 | 3223 | while (vm_page_free_count == 0) { |
b0d623f7 | 3224 | lck_mtx_unlock(&vm_page_queue_free_lock); |
2d21ac55 A |
3225 | /* |
3226 | * must be a privileged thread to be | |
0a7de745 | 3227 | * in this state since a non-privileged |
2d21ac55 A |
3228 | * thread would have bailed if we were |
3229 | * under the vm_page_free_reserved mark | |
3230 | */ | |
3231 | VM_PAGE_WAIT(); | |
b0d623f7 | 3232 | lck_mtx_lock_spin(&vm_page_queue_free_lock); |
2d21ac55 A |
3233 | } |
3234 | ||
3235 | disable_preemption(); | |
3236 | ||
f427ee49 | 3237 | if ((mem = *PERCPU_GET(free_pages))) { |
b0d623f7 | 3238 | lck_mtx_unlock(&vm_page_queue_free_lock); |
2d21ac55 | 3239 | |
0a7de745 | 3240 | /* |
2d21ac55 A |
3241 | * we got preempted and moved to another processor |
3242 | * or we got preempted and someone else ran and filled the cache | |
3243 | */ | |
3244 | goto return_page_from_cpu_list; | |
3245 | } | |
0a7de745 A |
3246 | if (vm_page_free_count <= vm_page_free_reserved) { |
3247 | pages_to_steal = 1; | |
3248 | } else { | |
3249 | if (vm_free_magazine_refill_limit <= (vm_page_free_count - vm_page_free_reserved)) { | |
fe8ab488 | 3250 | pages_to_steal = vm_free_magazine_refill_limit; |
0a7de745 A |
3251 | } else { |
3252 | pages_to_steal = (vm_page_free_count - vm_page_free_reserved); | |
3253 | } | |
2d21ac55 | 3254 | } |
f427ee49 | 3255 | color = *PERCPU_GET(start_color); |
2d21ac55 A |
3256 | head = tail = NULL; |
3257 | ||
fe8ab488 | 3258 | vm_page_free_count -= pages_to_steal; |
5ba3f43e | 3259 | clump_end = sub_count = 0; |
fe8ab488 | 3260 | |
2d21ac55 | 3261 | while (pages_to_steal--) { |
0a7de745 A |
3262 | while (vm_page_queue_empty(&vm_page_queue_free[color].qhead)) { |
3263 | color = (color + 1) & vm_color_mask; | |
3264 | } | |
5ba3f43e A |
3265 | #if defined(__x86_64__) |
3266 | vm_page_queue_remove_first_with_clump(&vm_page_queue_free[color].qhead, | |
0a7de745 | 3267 | mem, clump_end); |
5ba3f43e | 3268 | #else |
39037602 | 3269 | vm_page_queue_remove_first(&vm_page_queue_free[color].qhead, |
0a7de745 | 3270 | mem, vmp_pageq); |
5ba3f43e A |
3271 | #endif |
3272 | ||
d9a64523 | 3273 | assert(mem->vmp_q_state == VM_PAGE_ON_FREE_Q); |
6d2010ae | 3274 | |
39037602 | 3275 | VM_PAGE_ZERO_PAGEQ_ENTRY(mem); |
5ba3f43e A |
3276 | |
3277 | #if defined(__arm__) || defined(__arm64__) | |
2d21ac55 | 3278 | color = (color + 1) & vm_color_mask; |
5ba3f43e A |
3279 | #else |
3280 | ||
3281 | #if DEVELOPMENT || DEBUG | |
3282 | ||
3283 | sub_count++; | |
3284 | if (clump_end) { | |
3285 | vm_clump_update_stats(sub_count); | |
3286 | sub_count = 0; | |
3287 | color = (color + 1) & vm_color_mask; | |
3288 | } | |
3289 | #else | |
0a7de745 A |
3290 | if (clump_end) { |
3291 | color = (color + 1) & vm_color_mask; | |
3292 | } | |
5ba3f43e A |
3293 | |
3294 | #endif /* if DEVELOPMENT || DEBUG */ | |
3295 | ||
3296 | #endif /* if defined(__arm__) || defined(__arm64__) */ | |
2d21ac55 | 3297 | |
0a7de745 | 3298 | if (head == NULL) { |
2d21ac55 | 3299 | head = mem; |
0a7de745 A |
3300 | } else { |
3301 | tail->vmp_snext = mem; | |
3302 | } | |
3303 | tail = mem; | |
2d21ac55 | 3304 | |
d9a64523 A |
3305 | assert(mem->vmp_listq.next == 0 && mem->vmp_listq.prev == 0); |
3306 | assert(mem->vmp_tabled == FALSE); | |
3307 | assert(mem->vmp_object == 0); | |
3308 | assert(!mem->vmp_laundry); | |
2d21ac55 | 3309 | |
d9a64523 | 3310 | mem->vmp_q_state = VM_PAGE_ON_FREE_LOCAL_Q; |
39037602 | 3311 | |
0a7de745 | 3312 | ASSERT_PMAP_FREE(mem); |
d9a64523 A |
3313 | assert(mem->vmp_busy); |
3314 | assert(!mem->vmp_pmapped); | |
3315 | assert(!mem->vmp_wpmapped); | |
39037602 | 3316 | assert(!pmap_is_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem))); |
2d21ac55 | 3317 | } |
5ba3f43e A |
3318 | #if defined (__x86_64__) && (DEVELOPMENT || DEBUG) |
3319 | vm_clump_update_stats(sub_count); | |
3320 | #endif | |
fe8ab488 A |
3321 | lck_mtx_unlock(&vm_page_queue_free_lock); |
3322 | ||
5ba3f43e A |
3323 | #if HIBERNATION |
3324 | if (hibernate_rebuild_needed) { | |
3325 | panic("%s:%d should not modify cpu->free_pages while hibernating", __FUNCTION__, __LINE__); | |
3326 | } | |
3327 | #endif /* HIBERNATION */ | |
f427ee49 A |
3328 | vm_offset_t pcpu_base = current_percpu_base(); |
3329 | *PERCPU_GET_WITH_BASE(pcpu_base, free_pages) = head->vmp_snext; | |
3330 | *PERCPU_GET_WITH_BASE(pcpu_base, start_color) = color; | |
2d21ac55 A |
3331 | |
3332 | /* | |
3333 | * satisfy this request | |
3334 | */ | |
0a7de745 | 3335 | vm_page_grab_diags(); |
f427ee49 | 3336 | *PERCPU_GET_WITH_BASE(pcpu_base, vm_page_grab_count) += 1; |
d9a64523 | 3337 | VM_DEBUG_EVENT(vm_page_grab, VM_PAGE_GRAB, DBG_FUNC_NONE, grab_options, 0, 0, 0); |
2d21ac55 | 3338 | mem = head; |
d9a64523 | 3339 | assert(mem->vmp_q_state == VM_PAGE_ON_FREE_LOCAL_Q); |
39037602 A |
3340 | |
3341 | VM_PAGE_ZERO_PAGEQ_ENTRY(mem); | |
d9a64523 | 3342 | mem->vmp_q_state = VM_PAGE_NOT_ON_Q; |
91447636 | 3343 | |
2d21ac55 A |
3344 | enable_preemption(); |
3345 | } | |
1c79356b A |
3346 | /* |
3347 | * Decide if we should poke the pageout daemon. | |
3348 | * We do this if the free count is less than the low | |
cb323159 A |
3349 | * water mark. VM Pageout Scan will keep running till |
3350 | * the free_count > free_target (& hence above free_min). | |
3351 | * This wakeup is to catch the possibility of the counts | |
3352 | * dropping between VM Pageout Scan parking and this check. | |
1c79356b A |
3353 | * |
3354 | * We don't have the counts locked ... if they change a little, | |
3355 | * it doesn't really matter. | |
3356 | */ | |
0a7de745 | 3357 | if (vm_page_free_count < vm_page_free_min) { |
cb323159 A |
3358 | lck_mtx_lock(&vm_page_queue_free_lock); |
3359 | if (vm_pageout_running == FALSE) { | |
3360 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3361 | thread_wakeup((event_t) &vm_page_free_wanted); | |
3362 | } else { | |
3363 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3364 | } | |
0a7de745 | 3365 | } |
2d21ac55 | 3366 | |
6d2010ae | 3367 | VM_CHECK_MEMORYSTATUS; |
39037602 A |
3368 | |
3369 | if (mem) { | |
3370 | // dbgLog(VM_PAGE_GET_PHYS_PAGE(mem), vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */ | |
3371 | ||
3372 | #if CONFIG_BACKGROUND_QUEUE | |
3373 | vm_page_assign_background_state(mem); | |
3374 | #endif | |
3375 | } | |
3376 | return mem; | |
3377 | } | |
3378 | ||
3379 | #if CONFIG_SECLUDED_MEMORY | |
3380 | vm_page_t | |
3381 | vm_page_grab_secluded(void) | |
3382 | { | |
0a7de745 A |
3383 | vm_page_t mem; |
3384 | vm_object_t object; | |
3385 | int refmod_state; | |
39037602 A |
3386 | |
3387 | if (vm_page_secluded_count == 0) { | |
3388 | /* no secluded pages to grab... */ | |
3389 | return VM_PAGE_NULL; | |
3390 | } | |
3391 | ||
3392 | /* secluded queue is protected by the VM page queue lock */ | |
3393 | vm_page_lock_queues(); | |
3394 | ||
3395 | if (vm_page_secluded_count == 0) { | |
3396 | /* no secluded pages to grab... */ | |
3397 | vm_page_unlock_queues(); | |
3398 | return VM_PAGE_NULL; | |
3399 | } | |
3400 | ||
3401 | #if 00 | |
3402 | /* can we grab from the secluded queue? */ | |
3403 | if (vm_page_secluded_count > vm_page_secluded_target || | |
3404 | (vm_page_secluded_count > 0 && | |
0a7de745 | 3405 | task_can_use_secluded_mem(current_task(), TRUE))) { |
39037602 A |
3406 | /* OK */ |
3407 | } else { | |
3408 | /* can't grab from secluded queue... */ | |
3409 | vm_page_unlock_queues(); | |
3410 | return VM_PAGE_NULL; | |
3411 | } | |
3412 | #endif | |
3413 | ||
3414 | /* we can grab a page from secluded queue! */ | |
3415 | assert((vm_page_secluded_count_free + | |
0a7de745 A |
3416 | vm_page_secluded_count_inuse) == |
3417 | vm_page_secluded_count); | |
39037602 A |
3418 | if (current_task()->task_can_use_secluded_mem) { |
3419 | assert(num_tasks_can_use_secluded_mem > 0); | |
3420 | } | |
3421 | assert(!vm_page_queue_empty(&vm_page_queue_secluded)); | |
3422 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
5ba3f43e | 3423 | mem = (vm_page_t)vm_page_queue_first(&vm_page_queue_secluded); |
d9a64523 | 3424 | assert(mem->vmp_q_state == VM_PAGE_ON_SECLUDED_Q); |
d190cdc3 | 3425 | vm_page_queues_remove(mem, TRUE); |
39037602 A |
3426 | |
3427 | object = VM_PAGE_OBJECT(mem); | |
3428 | ||
d9a64523 | 3429 | assert(!mem->vmp_fictitious); |
39037602 A |
3430 | assert(!VM_PAGE_WIRED(mem)); |
3431 | if (object == VM_OBJECT_NULL) { | |
3432 | /* free for grab! */ | |
39037602 A |
3433 | vm_page_unlock_queues(); |
3434 | vm_page_secluded.grab_success_free++; | |
d190cdc3 | 3435 | |
d9a64523 A |
3436 | assert(mem->vmp_busy); |
3437 | assert(mem->vmp_q_state == VM_PAGE_NOT_ON_Q); | |
d190cdc3 | 3438 | assert(VM_PAGE_OBJECT(mem) == VM_OBJECT_NULL); |
d9a64523 A |
3439 | assert(mem->vmp_pageq.next == 0); |
3440 | assert(mem->vmp_pageq.prev == 0); | |
3441 | assert(mem->vmp_listq.next == 0); | |
3442 | assert(mem->vmp_listq.prev == 0); | |
d190cdc3 | 3443 | #if CONFIG_BACKGROUND_QUEUE |
d9a64523 A |
3444 | assert(mem->vmp_on_backgroundq == 0); |
3445 | assert(mem->vmp_backgroundq.next == 0); | |
3446 | assert(mem->vmp_backgroundq.prev == 0); | |
d190cdc3 | 3447 | #endif /* CONFIG_BACKGROUND_QUEUE */ |
39037602 A |
3448 | return mem; |
3449 | } | |
3450 | ||
39037602 A |
3451 | assert(!object->internal); |
3452 | // vm_page_pageable_external_count--; | |
3453 | ||
3454 | if (!vm_object_lock_try(object)) { | |
3455 | // printf("SECLUDED: page %p: object %p locked\n", mem, object); | |
3456 | vm_page_secluded.grab_failure_locked++; | |
0a7de745 | 3457 | reactivate_secluded_page: |
39037602 A |
3458 | vm_page_activate(mem); |
3459 | vm_page_unlock_queues(); | |
3460 | return VM_PAGE_NULL; | |
3461 | } | |
d9a64523 A |
3462 | if (mem->vmp_busy || |
3463 | mem->vmp_cleaning || | |
3464 | mem->vmp_laundry) { | |
39037602 A |
3465 | /* can't steal page in this state... */ |
3466 | vm_object_unlock(object); | |
3467 | vm_page_secluded.grab_failure_state++; | |
3468 | goto reactivate_secluded_page; | |
3469 | } | |
3470 | ||
d9a64523 | 3471 | mem->vmp_busy = TRUE; |
39037602 A |
3472 | refmod_state = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(mem)); |
3473 | if (refmod_state & VM_MEM_REFERENCED) { | |
d9a64523 | 3474 | mem->vmp_reference = TRUE; |
39037602 A |
3475 | } |
3476 | if (refmod_state & VM_MEM_MODIFIED) { | |
3477 | SET_PAGE_DIRTY(mem, FALSE); | |
3478 | } | |
d9a64523 | 3479 | if (mem->vmp_dirty || mem->vmp_precious) { |
39037602 A |
3480 | /* can't grab a dirty page; re-activate */ |
3481 | // printf("SECLUDED: dirty page %p\n", mem); | |
743345f9 | 3482 | PAGE_WAKEUP_DONE(mem); |
39037602 A |
3483 | vm_page_secluded.grab_failure_dirty++; |
3484 | vm_object_unlock(object); | |
3485 | goto reactivate_secluded_page; | |
3486 | } | |
d9a64523 | 3487 | if (mem->vmp_reference) { |
39037602 A |
3488 | /* it's been used but we do need to grab a page... */ |
3489 | } | |
743345f9 | 3490 | |
39037602 A |
3491 | vm_page_unlock_queues(); |
3492 | ||
3493 | /* finish what vm_page_free() would have done... */ | |
3494 | vm_page_free_prepare_object(mem, TRUE); | |
3495 | vm_object_unlock(object); | |
3496 | object = VM_OBJECT_NULL; | |
3497 | if (vm_page_free_verify) { | |
0a7de745 | 3498 | ASSERT_PMAP_FREE(mem); |
39037602 A |
3499 | } |
3500 | pmap_clear_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem)); | |
39037602 | 3501 | vm_page_secluded.grab_success_other++; |
1c79356b | 3502 | |
d9a64523 A |
3503 | assert(mem->vmp_busy); |
3504 | assert(mem->vmp_q_state == VM_PAGE_NOT_ON_Q); | |
d190cdc3 | 3505 | assert(VM_PAGE_OBJECT(mem) == VM_OBJECT_NULL); |
d9a64523 A |
3506 | assert(mem->vmp_pageq.next == 0); |
3507 | assert(mem->vmp_pageq.prev == 0); | |
3508 | assert(mem->vmp_listq.next == 0); | |
3509 | assert(mem->vmp_listq.prev == 0); | |
d190cdc3 | 3510 | #if CONFIG_BACKGROUND_QUEUE |
d9a64523 A |
3511 | assert(mem->vmp_on_backgroundq == 0); |
3512 | assert(mem->vmp_backgroundq.next == 0); | |
3513 | assert(mem->vmp_backgroundq.prev == 0); | |
d190cdc3 A |
3514 | #endif /* CONFIG_BACKGROUND_QUEUE */ |
3515 | ||
1c79356b A |
3516 | return mem; |
3517 | } | |
cb323159 A |
3518 | |
3519 | uint64_t | |
3520 | vm_page_secluded_drain(void) | |
3521 | { | |
3522 | vm_page_t local_freeq; | |
3523 | int local_freed; | |
3524 | uint64_t num_reclaimed; | |
3525 | unsigned int saved_secluded_count, saved_secluded_target; | |
3526 | ||
3527 | num_reclaimed = 0; | |
3528 | local_freeq = NULL; | |
3529 | local_freed = 0; | |
3530 | ||
3531 | vm_page_lock_queues(); | |
3532 | ||
3533 | saved_secluded_count = vm_page_secluded_count; | |
3534 | saved_secluded_target = vm_page_secluded_target; | |
3535 | vm_page_secluded_target = 0; | |
3536 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); | |
3537 | while (vm_page_secluded_count) { | |
3538 | vm_page_t secluded_page; | |
3539 | ||
3540 | assert((vm_page_secluded_count_free + | |
3541 | vm_page_secluded_count_inuse) == | |
3542 | vm_page_secluded_count); | |
3543 | secluded_page = (vm_page_t)vm_page_queue_first(&vm_page_queue_secluded); | |
3544 | assert(secluded_page->vmp_q_state == VM_PAGE_ON_SECLUDED_Q); | |
3545 | ||
3546 | vm_page_queues_remove(secluded_page, FALSE); | |
3547 | assert(!secluded_page->vmp_fictitious); | |
3548 | assert(!VM_PAGE_WIRED(secluded_page)); | |
3549 | ||
3550 | if (secluded_page->vmp_object == 0) { | |
3551 | /* transfer to free queue */ | |
3552 | assert(secluded_page->vmp_busy); | |
3553 | secluded_page->vmp_snext = local_freeq; | |
3554 | local_freeq = secluded_page; | |
3555 | local_freed += 1; | |
3556 | } else { | |
3557 | /* transfer to head of active queue */ | |
3558 | vm_page_enqueue_active(secluded_page, FALSE); | |
3559 | secluded_page = VM_PAGE_NULL; | |
3560 | } | |
3561 | num_reclaimed++; | |
3562 | } | |
3563 | vm_page_secluded_target = saved_secluded_target; | |
3564 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); | |
3565 | ||
3566 | // printf("FBDP %s:%d secluded_count %d->%d, target %d, reclaimed %lld\n", __FUNCTION__, __LINE__, saved_secluded_count, vm_page_secluded_count, vm_page_secluded_target, num_reclaimed); | |
3567 | ||
3568 | vm_page_unlock_queues(); | |
3569 | ||
3570 | if (local_freed) { | |
3571 | vm_page_free_list(local_freeq, TRUE); | |
3572 | local_freeq = NULL; | |
3573 | local_freed = 0; | |
3574 | } | |
3575 | ||
3576 | return num_reclaimed; | |
3577 | } | |
39037602 | 3578 | #endif /* CONFIG_SECLUDED_MEMORY */ |
1c79356b | 3579 | |
0a7de745 A |
3580 | |
3581 | static inline void | |
3582 | vm_page_grab_diags() | |
3583 | { | |
3584 | #if DEVELOPMENT || DEBUG | |
3585 | task_t task = current_task(); | |
3586 | if (task == NULL) { | |
3587 | return; | |
3588 | } | |
3589 | ||
3590 | ledger_credit(task->ledger, task_ledgers.pages_grabbed, 1); | |
3591 | #endif /* DEVELOPMENT || DEBUG */ | |
3592 | } | |
3593 | ||
1c79356b A |
3594 | /* |
3595 | * vm_page_release: | |
3596 | * | |
3597 | * Return a page to the free list. | |
3598 | */ | |
3599 | ||
3600 | void | |
3601 | vm_page_release( | |
0a7de745 A |
3602 | vm_page_t mem, |
3603 | boolean_t page_queues_locked) | |
1c79356b | 3604 | { |
0a7de745 A |
3605 | unsigned int color; |
3606 | int need_wakeup = 0; | |
3607 | int need_priv_wakeup = 0; | |
39037602 | 3608 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 3609 | int need_secluded_wakeup = 0; |
39037602 | 3610 | #endif /* CONFIG_SECLUDED_MEMORY */ |
cb323159 | 3611 | event_t wakeup_event = NULL; |
55e303ae | 3612 | |
39037602 A |
3613 | if (page_queues_locked) { |
3614 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
3615 | } else { | |
3616 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED); | |
3617 | } | |
6d2010ae | 3618 | |
d9a64523 | 3619 | assert(!mem->vmp_private && !mem->vmp_fictitious); |
b0d623f7 | 3620 | if (vm_page_free_verify) { |
0a7de745 | 3621 | ASSERT_PMAP_FREE(mem); |
b0d623f7 | 3622 | } |
39037602 | 3623 | // dbgLog(VM_PAGE_GET_PHYS_PAGE(mem), vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */ |
1c79356b | 3624 | |
39037602 | 3625 | pmap_clear_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem)); |
7ddcb079 | 3626 | |
b0d623f7 | 3627 | lck_mtx_lock_spin(&vm_page_queue_free_lock); |
6d2010ae | 3628 | |
d9a64523 A |
3629 | assert(mem->vmp_q_state == VM_PAGE_NOT_ON_Q); |
3630 | assert(mem->vmp_busy); | |
3631 | assert(!mem->vmp_laundry); | |
3632 | assert(mem->vmp_object == 0); | |
3633 | assert(mem->vmp_pageq.next == 0 && mem->vmp_pageq.prev == 0); | |
3634 | assert(mem->vmp_listq.next == 0 && mem->vmp_listq.prev == 0); | |
39037602 | 3635 | #if CONFIG_BACKGROUND_QUEUE |
d9a64523 | 3636 | assert(mem->vmp_backgroundq.next == 0 && |
0a7de745 A |
3637 | mem->vmp_backgroundq.prev == 0 && |
3638 | mem->vmp_on_backgroundq == FALSE); | |
3639 | #endif | |
d9a64523 | 3640 | if ((mem->vmp_lopage == TRUE || vm_lopage_refill == TRUE) && |
0b4c1975 | 3641 | vm_lopage_free_count < vm_lopage_free_limit && |
39037602 | 3642 | VM_PAGE_GET_PHYS_PAGE(mem) < max_valid_low_ppnum) { |
0a7de745 | 3643 | /* |
0c530ab8 A |
3644 | * this exists to support hardware controllers |
3645 | * incapable of generating DMAs with more than 32 bits | |
3646 | * of address on platforms with physical memory > 4G... | |
3647 | */ | |
0a7de745 | 3648 | vm_page_queue_enter_first(&vm_lopage_queue_free, mem, vmp_pageq); |
0c530ab8 | 3649 | vm_lopage_free_count++; |
0b4c1975 | 3650 | |
0a7de745 | 3651 | if (vm_lopage_free_count >= vm_lopage_free_limit) { |
0b4c1975 | 3652 | vm_lopage_refill = FALSE; |
0a7de745 | 3653 | } |
0b4c1975 | 3654 | |
d9a64523 A |
3655 | mem->vmp_q_state = VM_PAGE_ON_FREE_LOPAGE_Q; |
3656 | mem->vmp_lopage = TRUE; | |
39037602 A |
3657 | #if CONFIG_SECLUDED_MEMORY |
3658 | } else if (vm_page_free_count > vm_page_free_reserved && | |
0a7de745 A |
3659 | vm_page_secluded_count < vm_page_secluded_target && |
3660 | num_tasks_can_use_secluded_mem == 0) { | |
39037602 A |
3661 | /* |
3662 | * XXX FBDP TODO: also avoid refilling secluded queue | |
3663 | * when some IOKit objects are already grabbing from it... | |
3664 | */ | |
3665 | if (!page_queues_locked) { | |
3666 | if (!vm_page_trylock_queues()) { | |
3667 | /* take locks in right order */ | |
3668 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3669 | vm_page_lock_queues(); | |
3670 | lck_mtx_lock_spin(&vm_page_queue_free_lock); | |
3671 | } | |
3672 | } | |
d9a64523 | 3673 | mem->vmp_lopage = FALSE; |
39037602 | 3674 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 3675 | vm_page_queue_enter_first(&vm_page_queue_secluded, mem, vmp_pageq); |
d9a64523 | 3676 | mem->vmp_q_state = VM_PAGE_ON_SECLUDED_Q; |
39037602 | 3677 | vm_page_secluded_count++; |
cb323159 | 3678 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); |
39037602 A |
3679 | vm_page_secluded_count_free++; |
3680 | if (!page_queues_locked) { | |
3681 | vm_page_unlock_queues(); | |
3682 | } | |
3683 | LCK_MTX_ASSERT(&vm_page_queue_free_lock, LCK_MTX_ASSERT_OWNED); | |
3684 | if (vm_page_free_wanted_secluded > 0) { | |
0a7de745 | 3685 | vm_page_free_wanted_secluded--; |
39037602 A |
3686 | need_secluded_wakeup = 1; |
3687 | } | |
3688 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
3689 | } else { | |
d9a64523 A |
3690 | mem->vmp_lopage = FALSE; |
3691 | mem->vmp_q_state = VM_PAGE_ON_FREE_Q; | |
0b4c1975 | 3692 | |
5ba3f43e A |
3693 | color = VM_PAGE_GET_COLOR(mem); |
3694 | #if defined(__x86_64__) | |
0a7de745 | 3695 | vm_page_queue_enter_clump(&vm_page_queue_free[color].qhead, mem); |
5ba3f43e | 3696 | #else |
0a7de745 | 3697 | vm_page_queue_enter(&vm_page_queue_free[color].qhead, mem, vmp_pageq); |
5ba3f43e | 3698 | #endif |
0c530ab8 A |
3699 | vm_page_free_count++; |
3700 | /* | |
3701 | * Check if we should wake up someone waiting for page. | |
3702 | * But don't bother waking them unless they can allocate. | |
3703 | * | |
3704 | * We wakeup only one thread, to prevent starvation. | |
3705 | * Because the scheduling system handles wait queues FIFO, | |
3706 | * if we wakeup all waiting threads, one greedy thread | |
3707 | * can starve multiple niceguy threads. When the threads | |
3708 | * all wakeup, the greedy threads runs first, grabs the page, | |
3709 | * and waits for another page. It will be the first to run | |
3710 | * when the next page is freed. | |
3711 | * | |
3712 | * However, there is a slight danger here. | |
3713 | * The thread we wake might not use the free page. | |
3714 | * Then the other threads could wait indefinitely | |
3715 | * while the page goes unused. To forestall this, | |
3716 | * the pageout daemon will keep making free pages | |
3717 | * as long as vm_page_free_wanted is non-zero. | |
3718 | */ | |
1c79356b | 3719 | |
b0d623f7 A |
3720 | assert(vm_page_free_count > 0); |
3721 | if (vm_page_free_wanted_privileged > 0) { | |
0a7de745 | 3722 | vm_page_free_wanted_privileged--; |
b0d623f7 | 3723 | need_priv_wakeup = 1; |
39037602 A |
3724 | #if CONFIG_SECLUDED_MEMORY |
3725 | } else if (vm_page_free_wanted_secluded > 0 && | |
0a7de745 | 3726 | vm_page_free_count > vm_page_free_reserved) { |
39037602 A |
3727 | vm_page_free_wanted_secluded--; |
3728 | need_secluded_wakeup = 1; | |
3729 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
b0d623f7 | 3730 | } else if (vm_page_free_wanted > 0 && |
0a7de745 A |
3731 | vm_page_free_count > vm_page_free_reserved) { |
3732 | vm_page_free_wanted--; | |
b0d623f7 | 3733 | need_wakeup = 1; |
0c530ab8 | 3734 | } |
1c79356b | 3735 | } |
d9a64523 A |
3736 | vm_pageout_vminfo.vm_page_pages_freed++; |
3737 | ||
3738 | VM_DEBUG_CONSTANT_EVENT(vm_page_release, VM_PAGE_RELEASE, DBG_FUNC_NONE, 1, 0, 0, 0); | |
3739 | ||
b0d623f7 A |
3740 | lck_mtx_unlock(&vm_page_queue_free_lock); |
3741 | ||
0a7de745 | 3742 | if (need_priv_wakeup) { |
cb323159 | 3743 | wakeup_event = &vm_page_free_wanted_privileged; |
0a7de745 | 3744 | } |
39037602 | 3745 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 3746 | else if (need_secluded_wakeup) { |
cb323159 | 3747 | wakeup_event = &vm_page_free_wanted_secluded; |
0a7de745 | 3748 | } |
39037602 | 3749 | #endif /* CONFIG_SECLUDED_MEMORY */ |
0a7de745 | 3750 | else if (need_wakeup) { |
cb323159 A |
3751 | wakeup_event = &vm_page_free_count; |
3752 | } | |
3753 | ||
3754 | if (wakeup_event) { | |
3755 | if (vps_dynamic_priority_enabled == TRUE) { | |
3756 | thread_t thread_woken = NULL; | |
3757 | wakeup_one_with_inheritor((event_t) wakeup_event, THREAD_AWAKENED, LCK_WAKE_DO_NOT_TRANSFER_PUSH, &thread_woken); | |
3758 | thread_deallocate(thread_woken); | |
3759 | } else { | |
3760 | thread_wakeup_one((event_t) wakeup_event); | |
3761 | } | |
0a7de745 | 3762 | } |
2d21ac55 | 3763 | |
6d2010ae | 3764 | VM_CHECK_MEMORYSTATUS; |
1c79356b A |
3765 | } |
3766 | ||
fe8ab488 A |
3767 | /* |
3768 | * This version of vm_page_release() is used only at startup | |
0a7de745 | 3769 | * when we are single-threaded and pages are being released |
fe8ab488 A |
3770 | * for the first time. Hence, no locking or unnecessary checks are made. |
3771 | * Note: VM_CHECK_MEMORYSTATUS invoked by the caller. | |
3772 | */ | |
3773 | void | |
3774 | vm_page_release_startup( | |
0a7de745 | 3775 | vm_page_t mem) |
fe8ab488 | 3776 | { |
0a7de745 | 3777 | vm_page_queue_t queue_free; |
fe8ab488 A |
3778 | |
3779 | if (vm_lopage_free_count < vm_lopage_free_limit && | |
39037602 | 3780 | VM_PAGE_GET_PHYS_PAGE(mem) < max_valid_low_ppnum) { |
d9a64523 A |
3781 | mem->vmp_lopage = TRUE; |
3782 | mem->vmp_q_state = VM_PAGE_ON_FREE_LOPAGE_Q; | |
fe8ab488 A |
3783 | vm_lopage_free_count++; |
3784 | queue_free = &vm_lopage_queue_free; | |
39037602 A |
3785 | #if CONFIG_SECLUDED_MEMORY |
3786 | } else if (vm_page_secluded_count < vm_page_secluded_target) { | |
d9a64523 A |
3787 | mem->vmp_lopage = FALSE; |
3788 | mem->vmp_q_state = VM_PAGE_ON_SECLUDED_Q; | |
39037602 | 3789 | vm_page_secluded_count++; |
cb323159 | 3790 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); |
39037602 A |
3791 | vm_page_secluded_count_free++; |
3792 | queue_free = &vm_page_queue_secluded; | |
3793 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
3794 | } else { | |
d9a64523 A |
3795 | mem->vmp_lopage = FALSE; |
3796 | mem->vmp_q_state = VM_PAGE_ON_FREE_Q; | |
fe8ab488 | 3797 | vm_page_free_count++; |
5ba3f43e | 3798 | queue_free = &vm_page_queue_free[VM_PAGE_GET_COLOR(mem)].qhead; |
fe8ab488 | 3799 | } |
d9a64523 | 3800 | if (mem->vmp_q_state == VM_PAGE_ON_FREE_Q) { |
5ba3f43e | 3801 | #if defined(__x86_64__) |
0a7de745 | 3802 | vm_page_queue_enter_clump(queue_free, mem); |
5ba3f43e | 3803 | #else |
0a7de745 | 3804 | vm_page_queue_enter(queue_free, mem, vmp_pageq); |
5ba3f43e | 3805 | #endif |
0a7de745 A |
3806 | } else { |
3807 | vm_page_queue_enter_first(queue_free, mem, vmp_pageq); | |
3808 | } | |
fe8ab488 A |
3809 | } |
3810 | ||
1c79356b A |
3811 | /* |
3812 | * vm_page_wait: | |
3813 | * | |
3814 | * Wait for a page to become available. | |
3815 | * If there are plenty of free pages, then we don't sleep. | |
3816 | * | |
3817 | * Returns: | |
3818 | * TRUE: There may be another page, try again | |
3819 | * FALSE: We were interrupted out of our wait, don't try again | |
3820 | */ | |
3821 | ||
3822 | boolean_t | |
3823 | vm_page_wait( | |
0a7de745 | 3824 | int interruptible ) |
1c79356b A |
3825 | { |
3826 | /* | |
3827 | * We can't use vm_page_free_reserved to make this | |
3828 | * determination. Consider: some thread might | |
3829 | * need to allocate two pages. The first allocation | |
3830 | * succeeds, the second fails. After the first page is freed, | |
3831 | * a call to vm_page_wait must really block. | |
3832 | */ | |
0a7de745 A |
3833 | kern_return_t wait_result; |
3834 | int need_wakeup = 0; | |
3835 | int is_privileged = current_thread()->options & TH_OPT_VMPRIV; | |
cb323159 | 3836 | event_t wait_event = NULL; |
1c79356b | 3837 | |
b0d623f7 | 3838 | lck_mtx_lock_spin(&vm_page_queue_free_lock); |
2d21ac55 A |
3839 | |
3840 | if (is_privileged && vm_page_free_count) { | |
b0d623f7 | 3841 | lck_mtx_unlock(&vm_page_queue_free_lock); |
2d21ac55 A |
3842 | return TRUE; |
3843 | } | |
2d21ac55 | 3844 | |
39037602 | 3845 | if (vm_page_free_count >= vm_page_free_target) { |
b0d623f7 | 3846 | lck_mtx_unlock(&vm_page_queue_free_lock); |
39037602 A |
3847 | return TRUE; |
3848 | } | |
9bccf70c | 3849 | |
39037602 | 3850 | if (is_privileged) { |
0a7de745 | 3851 | if (vm_page_free_wanted_privileged++ == 0) { |
39037602 | 3852 | need_wakeup = 1; |
0a7de745 | 3853 | } |
cb323159 | 3854 | wait_event = (event_t)&vm_page_free_wanted_privileged; |
39037602 A |
3855 | #if CONFIG_SECLUDED_MEMORY |
3856 | } else if (secluded_for_apps && | |
0a7de745 | 3857 | task_can_use_secluded_mem(current_task(), FALSE)) { |
39037602 A |
3858 | #if 00 |
3859 | /* XXX FBDP: need pageq lock for this... */ | |
3860 | /* XXX FBDP: might wait even if pages available, */ | |
3861 | /* XXX FBDP: hopefully not for too long... */ | |
3862 | if (vm_page_secluded_count > 0) { | |
3863 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3864 | return TRUE; | |
39236c6e | 3865 | } |
39037602 A |
3866 | #endif |
3867 | if (vm_page_free_wanted_secluded++ == 0) { | |
3868 | need_wakeup = 1; | |
3869 | } | |
cb323159 | 3870 | wait_event = (event_t)&vm_page_free_wanted_secluded; |
39037602 | 3871 | #endif /* CONFIG_SECLUDED_MEMORY */ |
1c79356b | 3872 | } else { |
0a7de745 | 3873 | if (vm_page_free_wanted++ == 0) { |
39037602 | 3874 | need_wakeup = 1; |
0a7de745 | 3875 | } |
cb323159 | 3876 | wait_event = (event_t)&vm_page_free_count; |
39037602 | 3877 | } |
39037602 | 3878 | |
cb323159 A |
3879 | /* |
3880 | * We don't do a vm_pageout_scan wakeup if we already have | |
3881 | * some waiters because vm_pageout_scan checks for waiters | |
3882 | * before it returns and does so behind the vm_page_queue_free_lock, | |
3883 | * which we own when we bump the waiter counts. | |
3884 | */ | |
3885 | ||
3886 | if (vps_dynamic_priority_enabled == TRUE) { | |
3887 | /* | |
3888 | * We are waking up vm_pageout_scan here. If it needs | |
3889 | * the vm_page_queue_free_lock before we unlock it | |
3890 | * we'll end up just blocking and incur an extra | |
3891 | * context switch. Could be a perf. issue. | |
3892 | */ | |
3893 | ||
3894 | counter(c_vm_page_wait_block++); | |
39037602 | 3895 | |
cb323159 A |
3896 | if (need_wakeup) { |
3897 | thread_wakeup((event_t)&vm_page_free_wanted); | |
3898 | } | |
3899 | ||
3900 | /* | |
3901 | * LD: This event is going to get recorded every time because | |
3902 | * we don't get back THREAD_WAITING from lck_mtx_sleep_with_inheritor. | |
3903 | * We just block in that routine. | |
3904 | */ | |
d9a64523 | 3905 | VM_DEBUG_CONSTANT_EVENT(vm_page_wait_block, VM_PAGE_WAIT_BLOCK, DBG_FUNC_START, |
0a7de745 A |
3906 | vm_page_free_wanted_privileged, |
3907 | vm_page_free_wanted, | |
39037602 | 3908 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 3909 | vm_page_free_wanted_secluded, |
39037602 | 3910 | #else /* CONFIG_SECLUDED_MEMORY */ |
0a7de745 | 3911 | 0, |
39037602 | 3912 | #endif /* CONFIG_SECLUDED_MEMORY */ |
0a7de745 | 3913 | 0); |
cb323159 A |
3914 | wait_result = lck_mtx_sleep_with_inheritor(&vm_page_queue_free_lock, |
3915 | LCK_SLEEP_UNLOCK, | |
3916 | wait_event, | |
3917 | vm_pageout_scan_thread, | |
3918 | interruptible, | |
3919 | 0); | |
3920 | } else { | |
3921 | wait_result = assert_wait(wait_event, interruptible); | |
3922 | ||
3923 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3924 | counter(c_vm_page_wait_block++); | |
3925 | ||
3926 | if (need_wakeup) { | |
3927 | thread_wakeup((event_t)&vm_page_free_wanted); | |
3928 | } | |
3929 | ||
3930 | if (wait_result == THREAD_WAITING) { | |
3931 | VM_DEBUG_CONSTANT_EVENT(vm_page_wait_block, VM_PAGE_WAIT_BLOCK, DBG_FUNC_START, | |
3932 | vm_page_free_wanted_privileged, | |
3933 | vm_page_free_wanted, | |
3934 | #if CONFIG_SECLUDED_MEMORY | |
3935 | vm_page_free_wanted_secluded, | |
3936 | #else /* CONFIG_SECLUDED_MEMORY */ | |
3937 | 0, | |
3938 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
3939 | 0); | |
3940 | wait_result = thread_block(THREAD_CONTINUE_NULL); | |
3941 | VM_DEBUG_CONSTANT_EVENT(vm_page_wait_block, | |
3942 | VM_PAGE_WAIT_BLOCK, DBG_FUNC_END, 0, 0, 0, 0); | |
3943 | } | |
1c79356b | 3944 | } |
39037602 | 3945 | |
cb323159 | 3946 | return (wait_result == THREAD_AWAKENED) || (wait_result == THREAD_NOT_WAITING); |
1c79356b A |
3947 | } |
3948 | ||
3949 | /* | |
3950 | * vm_page_alloc: | |
3951 | * | |
3952 | * Allocate and return a memory cell associated | |
3953 | * with this VM object/offset pair. | |
3954 | * | |
3955 | * Object must be locked. | |
3956 | */ | |
3957 | ||
3958 | vm_page_t | |
3959 | vm_page_alloc( | |
0a7de745 A |
3960 | vm_object_t object, |
3961 | vm_object_offset_t offset) | |
1c79356b | 3962 | { |
0a7de745 A |
3963 | vm_page_t mem; |
3964 | int grab_options; | |
1c79356b | 3965 | |
2d21ac55 | 3966 | vm_object_lock_assert_exclusive(object); |
39037602 A |
3967 | grab_options = 0; |
3968 | #if CONFIG_SECLUDED_MEMORY | |
3969 | if (object->can_grab_secluded) { | |
3970 | grab_options |= VM_PAGE_GRAB_SECLUDED; | |
3971 | } | |
3972 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
3973 | mem = vm_page_grab_options(grab_options); | |
0a7de745 | 3974 | if (mem == VM_PAGE_NULL) { |
1c79356b | 3975 | return VM_PAGE_NULL; |
0a7de745 | 3976 | } |
1c79356b A |
3977 | |
3978 | vm_page_insert(mem, object, offset); | |
3979 | ||
0a7de745 | 3980 | return mem; |
1c79356b A |
3981 | } |
3982 | ||
2d21ac55 A |
3983 | /* |
3984 | * vm_page_alloc_guard: | |
0a7de745 A |
3985 | * |
3986 | * Allocate a fictitious page which will be used | |
2d21ac55 A |
3987 | * as a guard page. The page will be inserted into |
3988 | * the object and returned to the caller. | |
3989 | */ | |
3990 | ||
3991 | vm_page_t | |
3992 | vm_page_alloc_guard( | |
0a7de745 A |
3993 | vm_object_t object, |
3994 | vm_object_offset_t offset) | |
2d21ac55 | 3995 | { |
0a7de745 | 3996 | vm_page_t mem; |
2d21ac55 A |
3997 | |
3998 | vm_object_lock_assert_exclusive(object); | |
3999 | mem = vm_page_grab_guard(); | |
0a7de745 | 4000 | if (mem == VM_PAGE_NULL) { |
2d21ac55 | 4001 | return VM_PAGE_NULL; |
0a7de745 | 4002 | } |
2d21ac55 A |
4003 | |
4004 | vm_page_insert(mem, object, offset); | |
4005 | ||
0a7de745 | 4006 | return mem; |
2d21ac55 A |
4007 | } |
4008 | ||
4009 | ||
0a7de745 | 4010 | counter(unsigned int c_laundry_pages_freed = 0; ) |
1c79356b | 4011 | |
1c79356b | 4012 | /* |
6d2010ae | 4013 | * vm_page_free_prepare: |
1c79356b | 4014 | * |
6d2010ae A |
4015 | * Removes page from any queue it may be on |
4016 | * and disassociates it from its VM object. | |
1c79356b A |
4017 | * |
4018 | * Object and page queues must be locked prior to entry. | |
4019 | */ | |
b0d623f7 | 4020 | static void |
2d21ac55 | 4021 | vm_page_free_prepare( |
0a7de745 | 4022 | vm_page_t mem) |
b0d623f7 A |
4023 | { |
4024 | vm_page_free_prepare_queues(mem); | |
4025 | vm_page_free_prepare_object(mem, TRUE); | |
4026 | } | |
4027 | ||
4028 | ||
4029 | void | |
4030 | vm_page_free_prepare_queues( | |
0a7de745 | 4031 | vm_page_t mem) |
1c79356b | 4032 | { |
0a7de745 | 4033 | vm_object_t m_object; |
39037602 | 4034 | |
2d21ac55 | 4035 | VM_PAGE_CHECK(mem); |
39037602 | 4036 | |
d9a64523 A |
4037 | assert(mem->vmp_q_state != VM_PAGE_ON_FREE_Q); |
4038 | assert(!mem->vmp_cleaning); | |
39037602 | 4039 | m_object = VM_PAGE_OBJECT(mem); |
fe8ab488 | 4040 | |
39037602 A |
4041 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); |
4042 | if (m_object) { | |
4043 | vm_object_lock_assert_exclusive(m_object); | |
b0d623f7 | 4044 | } |
d9a64523 | 4045 | if (mem->vmp_laundry) { |
2d21ac55 A |
4046 | /* |
4047 | * We may have to free a page while it's being laundered | |
4048 | * if we lost its pager (due to a forced unmount, for example). | |
316670eb A |
4049 | * We need to call vm_pageout_steal_laundry() before removing |
4050 | * the page from its VM object, so that we can remove it | |
4051 | * from its pageout queue and adjust the laundry accounting | |
2d21ac55 | 4052 | */ |
316670eb | 4053 | vm_pageout_steal_laundry(mem, TRUE); |
2d21ac55 A |
4054 | counter(++c_laundry_pages_freed); |
4055 | } | |
0a7de745 | 4056 | |
39037602 | 4057 | vm_page_queues_remove(mem, TRUE); |
b0d623f7 A |
4058 | |
4059 | if (VM_PAGE_WIRED(mem)) { | |
d9a64523 | 4060 | assert(mem->vmp_wire_count > 0); |
39037602 A |
4061 | |
4062 | if (m_object) { | |
5ba3f43e A |
4063 | VM_OBJECT_WIRED_PAGE_UPDATE_START(m_object); |
4064 | VM_OBJECT_WIRED_PAGE_REMOVE(m_object, mem); | |
4065 | VM_OBJECT_WIRED_PAGE_UPDATE_END(m_object, m_object->wire_tag); | |
3e170ce0 | 4066 | |
39037602 | 4067 | assert(m_object->resident_page_count >= |
0a7de745 | 4068 | m_object->wired_page_count); |
6d2010ae | 4069 | |
39037602 | 4070 | if (m_object->purgable == VM_PURGABLE_VOLATILE) { |
6d2010ae A |
4071 | OSAddAtomic(+1, &vm_page_purgeable_count); |
4072 | assert(vm_page_purgeable_wired_count > 0); | |
4073 | OSAddAtomic(-1, &vm_page_purgeable_wired_count); | |
4074 | } | |
39037602 | 4075 | if ((m_object->purgable == VM_PURGABLE_VOLATILE || |
0a7de745 | 4076 | m_object->purgable == VM_PURGABLE_EMPTY) && |
d9a64523 | 4077 | m_object->vo_owner != TASK_NULL) { |
0a7de745 A |
4078 | task_t owner; |
4079 | int ledger_idx_volatile; | |
4080 | int ledger_idx_nonvolatile; | |
4081 | int ledger_idx_volatile_compressed; | |
4082 | int ledger_idx_nonvolatile_compressed; | |
4083 | boolean_t do_footprint; | |
d9a64523 A |
4084 | |
4085 | owner = VM_OBJECT_OWNER(m_object); | |
4086 | vm_object_ledger_tag_ledgers( | |
4087 | m_object, | |
4088 | &ledger_idx_volatile, | |
4089 | &ledger_idx_nonvolatile, | |
4090 | &ledger_idx_volatile_compressed, | |
4091 | &ledger_idx_nonvolatile_compressed, | |
4092 | &do_footprint); | |
fe8ab488 A |
4093 | /* |
4094 | * While wired, this page was accounted | |
4095 | * as "non-volatile" but it should now | |
4096 | * be accounted as "volatile". | |
4097 | */ | |
4098 | /* one less "non-volatile"... */ | |
4099 | ledger_debit(owner->ledger, | |
0a7de745 A |
4100 | ledger_idx_nonvolatile, |
4101 | PAGE_SIZE); | |
d9a64523 A |
4102 | if (do_footprint) { |
4103 | /* ... and "phys_footprint" */ | |
4104 | ledger_debit(owner->ledger, | |
0a7de745 A |
4105 | task_ledgers.phys_footprint, |
4106 | PAGE_SIZE); | |
d9a64523 | 4107 | } |
fe8ab488 A |
4108 | /* one more "volatile" */ |
4109 | ledger_credit(owner->ledger, | |
0a7de745 A |
4110 | ledger_idx_volatile, |
4111 | PAGE_SIZE); | |
fe8ab488 | 4112 | } |
b0d623f7 | 4113 | } |
0a7de745 | 4114 | if (!mem->vmp_private && !mem->vmp_fictitious) { |
1c79356b | 4115 | vm_page_wire_count--; |
0a7de745 | 4116 | } |
39037602 | 4117 | |
d9a64523 A |
4118 | mem->vmp_q_state = VM_PAGE_NOT_ON_Q; |
4119 | mem->vmp_wire_count = 0; | |
4120 | assert(!mem->vmp_gobbled); | |
4121 | } else if (mem->vmp_gobbled) { | |
0a7de745 | 4122 | if (!mem->vmp_private && !mem->vmp_fictitious) { |
1c79356b | 4123 | vm_page_wire_count--; |
0a7de745 | 4124 | } |
1c79356b A |
4125 | vm_page_gobble_count--; |
4126 | } | |
b0d623f7 A |
4127 | } |
4128 | ||
4129 | ||
4130 | void | |
4131 | vm_page_free_prepare_object( | |
0a7de745 A |
4132 | vm_page_t mem, |
4133 | boolean_t remove_from_hash) | |
b0d623f7 | 4134 | { |
0a7de745 A |
4135 | if (mem->vmp_tabled) { |
4136 | vm_page_remove(mem, remove_from_hash); /* clears tabled, object, offset */ | |
4137 | } | |
4138 | PAGE_WAKEUP(mem); /* clears wanted */ | |
1c79356b | 4139 | |
d9a64523 A |
4140 | if (mem->vmp_private) { |
4141 | mem->vmp_private = FALSE; | |
4142 | mem->vmp_fictitious = TRUE; | |
39037602 | 4143 | VM_PAGE_SET_PHYS_PAGE(mem, vm_page_fictitious_addr); |
1c79356b | 4144 | } |
0a7de745 | 4145 | if (!mem->vmp_fictitious) { |
d9a64523 A |
4146 | assert(mem->vmp_pageq.next == 0); |
4147 | assert(mem->vmp_pageq.prev == 0); | |
4148 | assert(mem->vmp_listq.next == 0); | |
4149 | assert(mem->vmp_listq.prev == 0); | |
d190cdc3 | 4150 | #if CONFIG_BACKGROUND_QUEUE |
d9a64523 A |
4151 | assert(mem->vmp_backgroundq.next == 0); |
4152 | assert(mem->vmp_backgroundq.prev == 0); | |
d190cdc3 | 4153 | #endif /* CONFIG_BACKGROUND_QUEUE */ |
d9a64523 | 4154 | assert(mem->vmp_next_m == 0); |
0a7de745 | 4155 | ASSERT_PMAP_FREE(mem); |
d9a64523 | 4156 | vm_page_init(mem, VM_PAGE_GET_PHYS_PAGE(mem), mem->vmp_lopage); |
1c79356b A |
4157 | } |
4158 | } | |
4159 | ||
b0d623f7 | 4160 | |
6d2010ae A |
4161 | /* |
4162 | * vm_page_free: | |
4163 | * | |
4164 | * Returns the given page to the free list, | |
4165 | * disassociating it with any VM object. | |
4166 | * | |
4167 | * Object and page queues must be locked prior to entry. | |
4168 | */ | |
2d21ac55 A |
4169 | void |
4170 | vm_page_free( | |
0a7de745 | 4171 | vm_page_t mem) |
2d21ac55 | 4172 | { |
b0d623f7 | 4173 | vm_page_free_prepare(mem); |
6d2010ae | 4174 | |
d9a64523 | 4175 | if (mem->vmp_fictitious) { |
b0d623f7 A |
4176 | vm_page_release_fictitious(mem); |
4177 | } else { | |
39037602 | 4178 | vm_page_release(mem, |
0a7de745 | 4179 | TRUE); /* page queues are locked */ |
b0d623f7 A |
4180 | } |
4181 | } | |
4182 | ||
4183 | ||
4184 | void | |
4185 | vm_page_free_unlocked( | |
0a7de745 A |
4186 | vm_page_t mem, |
4187 | boolean_t remove_from_hash) | |
b0d623f7 A |
4188 | { |
4189 | vm_page_lockspin_queues(); | |
4190 | vm_page_free_prepare_queues(mem); | |
4191 | vm_page_unlock_queues(); | |
4192 | ||
4193 | vm_page_free_prepare_object(mem, remove_from_hash); | |
4194 | ||
d9a64523 | 4195 | if (mem->vmp_fictitious) { |
2d21ac55 A |
4196 | vm_page_release_fictitious(mem); |
4197 | } else { | |
39037602 | 4198 | vm_page_release(mem, FALSE); /* page queues are not locked */ |
2d21ac55 A |
4199 | } |
4200 | } | |
55e303ae | 4201 | |
316670eb | 4202 | |
2d21ac55 A |
4203 | /* |
4204 | * Free a list of pages. The list can be up to several hundred pages, | |
4205 | * as blocked up by vm_pageout_scan(). | |
b0d623f7 | 4206 | * The big win is not having to take the free list lock once |
316670eb | 4207 | * per page. |
d190cdc3 A |
4208 | * |
4209 | * The VM page queues lock (vm_page_queue_lock) should NOT be held. | |
4210 | * The VM page free queues lock (vm_page_queue_free_lock) should NOT be held. | |
2d21ac55 | 4211 | */ |
55e303ae A |
4212 | void |
4213 | vm_page_free_list( | |
0a7de745 A |
4214 | vm_page_t freeq, |
4215 | boolean_t prepare_object) | |
55e303ae | 4216 | { |
0a7de745 A |
4217 | vm_page_t mem; |
4218 | vm_page_t nxt; | |
4219 | vm_page_t local_freeq; | |
4220 | int pg_count; | |
2d21ac55 | 4221 | |
d190cdc3 A |
4222 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED); |
4223 | LCK_MTX_ASSERT(&vm_page_queue_free_lock, LCK_MTX_ASSERT_NOTOWNED); | |
4224 | ||
316670eb | 4225 | while (freeq) { |
316670eb A |
4226 | pg_count = 0; |
4227 | local_freeq = VM_PAGE_NULL; | |
4228 | mem = freeq; | |
b0d623f7 | 4229 | |
316670eb A |
4230 | /* |
4231 | * break up the processing into smaller chunks so | |
4232 | * that we can 'pipeline' the pages onto the | |
4233 | * free list w/o introducing too much | |
4234 | * contention on the global free queue lock | |
4235 | */ | |
4236 | while (mem && pg_count < 64) { | |
d9a64523 | 4237 | assert((mem->vmp_q_state == VM_PAGE_NOT_ON_Q) || |
0a7de745 | 4238 | (mem->vmp_q_state == VM_PAGE_IS_WIRED)); |
39037602 | 4239 | #if CONFIG_BACKGROUND_QUEUE |
d9a64523 | 4240 | assert(mem->vmp_backgroundq.next == 0 && |
0a7de745 A |
4241 | mem->vmp_backgroundq.prev == 0 && |
4242 | mem->vmp_on_backgroundq == FALSE); | |
39037602 | 4243 | #endif |
d9a64523 A |
4244 | nxt = mem->vmp_snext; |
4245 | mem->vmp_snext = NULL; | |
4246 | assert(mem->vmp_pageq.prev == 0); | |
316670eb | 4247 | |
d9a64523 | 4248 | if (vm_page_free_verify && !mem->vmp_fictitious && !mem->vmp_private) { |
0a7de745 | 4249 | ASSERT_PMAP_FREE(mem); |
316670eb | 4250 | } |
0a7de745 | 4251 | if (prepare_object == TRUE) { |
316670eb | 4252 | vm_page_free_prepare_object(mem, TRUE); |
0a7de745 | 4253 | } |
b0d623f7 | 4254 | |
d9a64523 A |
4255 | if (!mem->vmp_fictitious) { |
4256 | assert(mem->vmp_busy); | |
55e303ae | 4257 | |
d9a64523 | 4258 | if ((mem->vmp_lopage == TRUE || vm_lopage_refill == TRUE) && |
316670eb | 4259 | vm_lopage_free_count < vm_lopage_free_limit && |
39037602 A |
4260 | VM_PAGE_GET_PHYS_PAGE(mem) < max_valid_low_ppnum) { |
4261 | vm_page_release(mem, FALSE); /* page queues are not locked */ | |
4262 | #if CONFIG_SECLUDED_MEMORY | |
4263 | } else if (vm_page_secluded_count < vm_page_secluded_target && | |
0a7de745 | 4264 | num_tasks_can_use_secluded_mem == 0) { |
39037602 | 4265 | vm_page_release(mem, |
0a7de745 | 4266 | FALSE); /* page queues are not locked */ |
39037602 | 4267 | #endif /* CONFIG_SECLUDED_MEMORY */ |
316670eb A |
4268 | } else { |
4269 | /* | |
4270 | * IMPORTANT: we can't set the page "free" here | |
4271 | * because that would make the page eligible for | |
4272 | * a physically-contiguous allocation (see | |
4273 | * vm_page_find_contiguous()) right away (we don't | |
4274 | * hold the vm_page_queue_free lock). That would | |
4275 | * cause trouble because the page is not actually | |
4276 | * in the free queue yet... | |
4277 | */ | |
d9a64523 | 4278 | mem->vmp_snext = local_freeq; |
316670eb A |
4279 | local_freeq = mem; |
4280 | pg_count++; | |
935ed37a | 4281 | |
39037602 | 4282 | pmap_clear_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem)); |
935ed37a | 4283 | } |
316670eb | 4284 | } else { |
39037602 | 4285 | assert(VM_PAGE_GET_PHYS_PAGE(mem) == vm_page_fictitious_addr || |
0a7de745 | 4286 | VM_PAGE_GET_PHYS_PAGE(mem) == vm_page_guard_addr); |
316670eb | 4287 | vm_page_release_fictitious(mem); |
2d21ac55 | 4288 | } |
316670eb | 4289 | mem = nxt; |
55e303ae | 4290 | } |
316670eb A |
4291 | freeq = mem; |
4292 | ||
0a7de745 A |
4293 | if ((mem = local_freeq)) { |
4294 | unsigned int avail_free_count; | |
4295 | unsigned int need_wakeup = 0; | |
4296 | unsigned int need_priv_wakeup = 0; | |
39037602 | 4297 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 4298 | unsigned int need_wakeup_secluded = 0; |
39037602 | 4299 | #endif /* CONFIG_SECLUDED_MEMORY */ |
cb323159 A |
4300 | event_t priv_wakeup_event, secluded_wakeup_event, normal_wakeup_event; |
4301 | boolean_t priv_wakeup_all, secluded_wakeup_all, normal_wakeup_all; | |
0a7de745 | 4302 | |
316670eb | 4303 | lck_mtx_lock_spin(&vm_page_queue_free_lock); |
55e303ae | 4304 | |
316670eb | 4305 | while (mem) { |
0a7de745 | 4306 | int color; |
316670eb | 4307 | |
d9a64523 | 4308 | nxt = mem->vmp_snext; |
2d21ac55 | 4309 | |
d9a64523 A |
4310 | assert(mem->vmp_q_state == VM_PAGE_NOT_ON_Q); |
4311 | assert(mem->vmp_busy); | |
4312 | mem->vmp_lopage = FALSE; | |
4313 | mem->vmp_q_state = VM_PAGE_ON_FREE_Q; | |
39037602 | 4314 | |
5ba3f43e A |
4315 | color = VM_PAGE_GET_COLOR(mem); |
4316 | #if defined(__x86_64__) | |
0a7de745 | 4317 | vm_page_queue_enter_clump(&vm_page_queue_free[color].qhead, mem); |
5ba3f43e A |
4318 | #else |
4319 | vm_page_queue_enter(&vm_page_queue_free[color].qhead, | |
0a7de745 | 4320 | mem, vmp_pageq); |
5ba3f43e | 4321 | #endif |
316670eb | 4322 | mem = nxt; |
2d21ac55 | 4323 | } |
d9a64523 | 4324 | vm_pageout_vminfo.vm_page_pages_freed += pg_count; |
316670eb A |
4325 | vm_page_free_count += pg_count; |
4326 | avail_free_count = vm_page_free_count; | |
4327 | ||
d9a64523 A |
4328 | VM_DEBUG_CONSTANT_EVENT(vm_page_release, VM_PAGE_RELEASE, DBG_FUNC_NONE, pg_count, 0, 0, 0); |
4329 | ||
316670eb | 4330 | if (vm_page_free_wanted_privileged > 0 && avail_free_count > 0) { |
316670eb A |
4331 | if (avail_free_count < vm_page_free_wanted_privileged) { |
4332 | need_priv_wakeup = avail_free_count; | |
4333 | vm_page_free_wanted_privileged -= avail_free_count; | |
4334 | avail_free_count = 0; | |
4335 | } else { | |
4336 | need_priv_wakeup = vm_page_free_wanted_privileged; | |
316670eb | 4337 | avail_free_count -= vm_page_free_wanted_privileged; |
39037602 | 4338 | vm_page_free_wanted_privileged = 0; |
316670eb | 4339 | } |
b0d623f7 | 4340 | } |
39037602 A |
4341 | #if CONFIG_SECLUDED_MEMORY |
4342 | if (vm_page_free_wanted_secluded > 0 && | |
4343 | avail_free_count > vm_page_free_reserved) { | |
4344 | unsigned int available_pages; | |
4345 | available_pages = (avail_free_count - | |
0a7de745 | 4346 | vm_page_free_reserved); |
39037602 A |
4347 | if (available_pages < |
4348 | vm_page_free_wanted_secluded) { | |
4349 | need_wakeup_secluded = available_pages; | |
4350 | vm_page_free_wanted_secluded -= | |
0a7de745 | 4351 | available_pages; |
39037602 A |
4352 | avail_free_count -= available_pages; |
4353 | } else { | |
4354 | need_wakeup_secluded = | |
0a7de745 | 4355 | vm_page_free_wanted_secluded; |
39037602 | 4356 | avail_free_count -= |
0a7de745 | 4357 | vm_page_free_wanted_secluded; |
39037602 A |
4358 | vm_page_free_wanted_secluded = 0; |
4359 | } | |
4360 | } | |
4361 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
316670eb A |
4362 | if (vm_page_free_wanted > 0 && avail_free_count > vm_page_free_reserved) { |
4363 | unsigned int available_pages; | |
55e303ae | 4364 | |
316670eb | 4365 | available_pages = avail_free_count - vm_page_free_reserved; |
55e303ae | 4366 | |
316670eb A |
4367 | if (available_pages >= vm_page_free_wanted) { |
4368 | need_wakeup = vm_page_free_wanted; | |
4369 | vm_page_free_wanted = 0; | |
4370 | } else { | |
4371 | need_wakeup = available_pages; | |
4372 | vm_page_free_wanted -= available_pages; | |
4373 | } | |
4374 | } | |
4375 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
55e303ae | 4376 | |
cb323159 A |
4377 | priv_wakeup_event = NULL; |
4378 | secluded_wakeup_event = NULL; | |
4379 | normal_wakeup_event = NULL; | |
4380 | ||
4381 | priv_wakeup_all = FALSE; | |
4382 | secluded_wakeup_all = FALSE; | |
4383 | normal_wakeup_all = FALSE; | |
4384 | ||
4385 | ||
316670eb A |
4386 | if (need_priv_wakeup != 0) { |
4387 | /* | |
4388 | * There shouldn't be that many VM-privileged threads, | |
4389 | * so let's wake them all up, even if we don't quite | |
4390 | * have enough pages to satisfy them all. | |
4391 | */ | |
cb323159 A |
4392 | priv_wakeup_event = (event_t)&vm_page_free_wanted_privileged; |
4393 | priv_wakeup_all = TRUE; | |
316670eb | 4394 | } |
39037602 A |
4395 | #if CONFIG_SECLUDED_MEMORY |
4396 | if (need_wakeup_secluded != 0 && | |
4397 | vm_page_free_wanted_secluded == 0) { | |
cb323159 A |
4398 | secluded_wakeup_event = (event_t)&vm_page_free_wanted_secluded; |
4399 | secluded_wakeup_all = TRUE; | |
4400 | need_wakeup_secluded = 0; | |
39037602 | 4401 | } else { |
cb323159 | 4402 | secluded_wakeup_event = (event_t)&vm_page_free_wanted_secluded; |
39037602 A |
4403 | } |
4404 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
316670eb A |
4405 | if (need_wakeup != 0 && vm_page_free_wanted == 0) { |
4406 | /* | |
4407 | * We don't expect to have any more waiters | |
4408 | * after this, so let's wake them all up at | |
4409 | * once. | |
4410 | */ | |
cb323159 A |
4411 | normal_wakeup_event = (event_t) &vm_page_free_count; |
4412 | normal_wakeup_all = TRUE; | |
4413 | need_wakeup = 0; | |
0a7de745 | 4414 | } else { |
cb323159 A |
4415 | normal_wakeup_event = (event_t) &vm_page_free_count; |
4416 | } | |
4417 | ||
4418 | if (priv_wakeup_event || | |
4419 | #if CONFIG_SECLUDED_MEMORY | |
4420 | secluded_wakeup_event || | |
4421 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
4422 | normal_wakeup_event) { | |
4423 | if (vps_dynamic_priority_enabled == TRUE) { | |
4424 | thread_t thread_woken = NULL; | |
4425 | ||
4426 | if (priv_wakeup_all == TRUE) { | |
4427 | wakeup_all_with_inheritor(priv_wakeup_event, THREAD_AWAKENED); | |
4428 | } | |
4429 | ||
4430 | #if CONFIG_SECLUDED_MEMORY | |
4431 | if (secluded_wakeup_all == TRUE) { | |
4432 | wakeup_all_with_inheritor(secluded_wakeup_event, THREAD_AWAKENED); | |
4433 | } | |
4434 | ||
4435 | while (need_wakeup_secluded-- != 0) { | |
4436 | /* | |
4437 | * Wake up one waiter per page we just released. | |
4438 | */ | |
4439 | wakeup_one_with_inheritor(secluded_wakeup_event, THREAD_AWAKENED, LCK_WAKE_DO_NOT_TRANSFER_PUSH, &thread_woken); | |
4440 | thread_deallocate(thread_woken); | |
4441 | } | |
4442 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
4443 | ||
4444 | if (normal_wakeup_all == TRUE) { | |
4445 | wakeup_all_with_inheritor(normal_wakeup_event, THREAD_AWAKENED); | |
4446 | } | |
4447 | ||
4448 | while (need_wakeup-- != 0) { | |
4449 | /* | |
4450 | * Wake up one waiter per page we just released. | |
4451 | */ | |
4452 | wakeup_one_with_inheritor(normal_wakeup_event, THREAD_AWAKENED, LCK_WAKE_DO_NOT_TRANSFER_PUSH, &thread_woken); | |
4453 | thread_deallocate(thread_woken); | |
4454 | } | |
4455 | } else { | |
0a7de745 | 4456 | /* |
cb323159 | 4457 | * Non-priority-aware wakeups. |
0a7de745 | 4458 | */ |
cb323159 A |
4459 | |
4460 | if (priv_wakeup_all == TRUE) { | |
4461 | thread_wakeup(priv_wakeup_event); | |
4462 | } | |
4463 | ||
4464 | #if CONFIG_SECLUDED_MEMORY | |
4465 | if (secluded_wakeup_all == TRUE) { | |
4466 | thread_wakeup(secluded_wakeup_event); | |
4467 | } | |
4468 | ||
4469 | while (need_wakeup_secluded-- != 0) { | |
4470 | /* | |
4471 | * Wake up one waiter per page we just released. | |
4472 | */ | |
4473 | thread_wakeup_one(secluded_wakeup_event); | |
4474 | } | |
4475 | ||
4476 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
4477 | if (normal_wakeup_all == TRUE) { | |
4478 | thread_wakeup(normal_wakeup_event); | |
4479 | } | |
4480 | ||
4481 | while (need_wakeup-- != 0) { | |
4482 | /* | |
4483 | * Wake up one waiter per page we just released. | |
4484 | */ | |
4485 | thread_wakeup_one(normal_wakeup_event); | |
4486 | } | |
0a7de745 | 4487 | } |
55e303ae | 4488 | } |
2d21ac55 | 4489 | |
316670eb | 4490 | VM_CHECK_MEMORYSTATUS; |
b0d623f7 | 4491 | } |
55e303ae A |
4492 | } |
4493 | } | |
4494 | ||
4495 | ||
1c79356b A |
4496 | /* |
4497 | * vm_page_wire: | |
4498 | * | |
4499 | * Mark this page as wired down by yet | |
4500 | * another map, removing it from paging queues | |
4501 | * as necessary. | |
4502 | * | |
4503 | * The page's object and the page queues must be locked. | |
4504 | */ | |
3e170ce0 A |
4505 | |
4506 | ||
1c79356b A |
4507 | void |
4508 | vm_page_wire( | |
39037602 | 4509 | vm_page_t mem, |
3e170ce0 | 4510 | vm_tag_t tag, |
0a7de745 | 4511 | boolean_t check_memorystatus) |
1c79356b | 4512 | { |
0a7de745 | 4513 | vm_object_t m_object; |
39037602 A |
4514 | |
4515 | m_object = VM_PAGE_OBJECT(mem); | |
1c79356b | 4516 | |
d9a64523 | 4517 | // dbgLog(current_thread(), mem->vmp_offset, m_object, 1); /* (TEST/DEBUG) */ |
1c79356b A |
4518 | |
4519 | VM_PAGE_CHECK(mem); | |
39037602 A |
4520 | if (m_object) { |
4521 | vm_object_lock_assert_exclusive(m_object); | |
b0d623f7 A |
4522 | } else { |
4523 | /* | |
4524 | * In theory, the page should be in an object before it | |
4525 | * gets wired, since we need to hold the object lock | |
4526 | * to update some fields in the page structure. | |
4527 | * However, some code (i386 pmap, for example) might want | |
4528 | * to wire a page before it gets inserted into an object. | |
4529 | * That's somewhat OK, as long as nobody else can get to | |
4530 | * that page and update it at the same time. | |
4531 | */ | |
4532 | } | |
39037602 | 4533 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); |
0a7de745 A |
4534 | if (!VM_PAGE_WIRED(mem)) { |
4535 | if (mem->vmp_laundry) { | |
39037602 | 4536 | vm_pageout_steal_laundry(mem, TRUE); |
0a7de745 | 4537 | } |
39037602 A |
4538 | |
4539 | vm_page_queues_remove(mem, TRUE); | |
4540 | ||
d9a64523 A |
4541 | assert(mem->vmp_wire_count == 0); |
4542 | mem->vmp_q_state = VM_PAGE_IS_WIRED; | |
b0d623f7 | 4543 | |
39037602 | 4544 | if (m_object) { |
5ba3f43e A |
4545 | VM_OBJECT_WIRED_PAGE_UPDATE_START(m_object); |
4546 | VM_OBJECT_WIRED_PAGE_ADD(m_object, mem); | |
4547 | VM_OBJECT_WIRED_PAGE_UPDATE_END(m_object, tag); | |
3e170ce0 | 4548 | |
39037602 | 4549 | assert(m_object->resident_page_count >= |
0a7de745 | 4550 | m_object->wired_page_count); |
39037602 | 4551 | if (m_object->purgable == VM_PURGABLE_VOLATILE) { |
b0d623f7 A |
4552 | assert(vm_page_purgeable_count > 0); |
4553 | OSAddAtomic(-1, &vm_page_purgeable_count); | |
4554 | OSAddAtomic(1, &vm_page_purgeable_wired_count); | |
4555 | } | |
39037602 | 4556 | if ((m_object->purgable == VM_PURGABLE_VOLATILE || |
0a7de745 | 4557 | m_object->purgable == VM_PURGABLE_EMPTY) && |
d9a64523 | 4558 | m_object->vo_owner != TASK_NULL) { |
0a7de745 A |
4559 | task_t owner; |
4560 | int ledger_idx_volatile; | |
4561 | int ledger_idx_nonvolatile; | |
4562 | int ledger_idx_volatile_compressed; | |
4563 | int ledger_idx_nonvolatile_compressed; | |
4564 | boolean_t do_footprint; | |
d9a64523 A |
4565 | |
4566 | owner = VM_OBJECT_OWNER(m_object); | |
4567 | vm_object_ledger_tag_ledgers( | |
4568 | m_object, | |
4569 | &ledger_idx_volatile, | |
4570 | &ledger_idx_nonvolatile, | |
4571 | &ledger_idx_volatile_compressed, | |
4572 | &ledger_idx_nonvolatile_compressed, | |
4573 | &do_footprint); | |
fe8ab488 A |
4574 | /* less volatile bytes */ |
4575 | ledger_debit(owner->ledger, | |
0a7de745 A |
4576 | ledger_idx_volatile, |
4577 | PAGE_SIZE); | |
fe8ab488 A |
4578 | /* more not-quite-volatile bytes */ |
4579 | ledger_credit(owner->ledger, | |
0a7de745 A |
4580 | ledger_idx_nonvolatile, |
4581 | PAGE_SIZE); | |
d9a64523 A |
4582 | if (do_footprint) { |
4583 | /* more footprint */ | |
4584 | ledger_credit(owner->ledger, | |
0a7de745 A |
4585 | task_ledgers.phys_footprint, |
4586 | PAGE_SIZE); | |
d9a64523 | 4587 | } |
fe8ab488 | 4588 | } |
39037602 | 4589 | if (m_object->all_reusable) { |
b0d623f7 A |
4590 | /* |
4591 | * Wired pages are not counted as "re-usable" | |
4592 | * in "all_reusable" VM objects, so nothing | |
4593 | * to do here. | |
4594 | */ | |
d9a64523 | 4595 | } else if (mem->vmp_reusable) { |
b0d623f7 A |
4596 | /* |
4597 | * This page is not "re-usable" when it's | |
4598 | * wired, so adjust its state and the | |
4599 | * accounting. | |
4600 | */ | |
39037602 | 4601 | vm_object_reuse_pages(m_object, |
0a7de745 A |
4602 | mem->vmp_offset, |
4603 | mem->vmp_offset + PAGE_SIZE_64, | |
4604 | FALSE); | |
b0d623f7 A |
4605 | } |
4606 | } | |
d9a64523 | 4607 | assert(!mem->vmp_reusable); |
b0d623f7 | 4608 | |
0a7de745 | 4609 | if (!mem->vmp_private && !mem->vmp_fictitious && !mem->vmp_gobbled) { |
1c79356b | 4610 | vm_page_wire_count++; |
0a7de745 A |
4611 | } |
4612 | if (mem->vmp_gobbled) { | |
1c79356b | 4613 | vm_page_gobble_count--; |
0a7de745 | 4614 | } |
d9a64523 | 4615 | mem->vmp_gobbled = FALSE; |
593a1d5f | 4616 | |
3e170ce0 A |
4617 | if (check_memorystatus == TRUE) { |
4618 | VM_CHECK_MEMORYSTATUS; | |
4619 | } | |
1c79356b | 4620 | } |
d9a64523 A |
4621 | assert(!mem->vmp_gobbled); |
4622 | assert(mem->vmp_q_state == VM_PAGE_IS_WIRED); | |
4623 | mem->vmp_wire_count++; | |
4624 | if (__improbable(mem->vmp_wire_count == 0)) { | |
39037602 A |
4625 | panic("vm_page_wire(%p): wire_count overflow", mem); |
4626 | } | |
b0d623f7 | 4627 | VM_PAGE_CHECK(mem); |
1c79356b A |
4628 | } |
4629 | ||
1c79356b A |
4630 | /* |
4631 | * vm_page_unwire: | |
4632 | * | |
4633 | * Release one wiring of this page, potentially | |
4634 | * enabling it to be paged again. | |
4635 | * | |
4636 | * The page's object and the page queues must be locked. | |
4637 | */ | |
4638 | void | |
4639 | vm_page_unwire( | |
0a7de745 A |
4640 | vm_page_t mem, |
4641 | boolean_t queueit) | |
1c79356b | 4642 | { |
0a7de745 | 4643 | vm_object_t m_object; |
39037602 A |
4644 | |
4645 | m_object = VM_PAGE_OBJECT(mem); | |
1c79356b | 4646 | |
d9a64523 | 4647 | // dbgLog(current_thread(), mem->vmp_offset, m_object, 0); /* (TEST/DEBUG) */ |
1c79356b A |
4648 | |
4649 | VM_PAGE_CHECK(mem); | |
b0d623f7 | 4650 | assert(VM_PAGE_WIRED(mem)); |
d9a64523 A |
4651 | assert(mem->vmp_wire_count > 0); |
4652 | assert(!mem->vmp_gobbled); | |
39037602 A |
4653 | assert(m_object != VM_OBJECT_NULL); |
4654 | vm_object_lock_assert_exclusive(m_object); | |
4655 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 | 4656 | if (--mem->vmp_wire_count == 0) { |
d9a64523 | 4657 | mem->vmp_q_state = VM_PAGE_NOT_ON_Q; |
39037602 | 4658 | |
5ba3f43e A |
4659 | VM_OBJECT_WIRED_PAGE_UPDATE_START(m_object); |
4660 | VM_OBJECT_WIRED_PAGE_REMOVE(m_object, mem); | |
4661 | VM_OBJECT_WIRED_PAGE_UPDATE_END(m_object, m_object->wire_tag); | |
d9a64523 | 4662 | if (!mem->vmp_private && !mem->vmp_fictitious) { |
4bd07ac2 A |
4663 | vm_page_wire_count--; |
4664 | } | |
5ba3f43e | 4665 | |
39037602 | 4666 | assert(m_object->resident_page_count >= |
0a7de745 | 4667 | m_object->wired_page_count); |
39037602 | 4668 | if (m_object->purgable == VM_PURGABLE_VOLATILE) { |
b0d623f7 A |
4669 | OSAddAtomic(+1, &vm_page_purgeable_count); |
4670 | assert(vm_page_purgeable_wired_count > 0); | |
4671 | OSAddAtomic(-1, &vm_page_purgeable_wired_count); | |
4672 | } | |
39037602 | 4673 | if ((m_object->purgable == VM_PURGABLE_VOLATILE || |
0a7de745 | 4674 | m_object->purgable == VM_PURGABLE_EMPTY) && |
d9a64523 | 4675 | m_object->vo_owner != TASK_NULL) { |
0a7de745 A |
4676 | task_t owner; |
4677 | int ledger_idx_volatile; | |
4678 | int ledger_idx_nonvolatile; | |
4679 | int ledger_idx_volatile_compressed; | |
4680 | int ledger_idx_nonvolatile_compressed; | |
4681 | boolean_t do_footprint; | |
d9a64523 A |
4682 | |
4683 | owner = VM_OBJECT_OWNER(m_object); | |
4684 | vm_object_ledger_tag_ledgers( | |
4685 | m_object, | |
4686 | &ledger_idx_volatile, | |
4687 | &ledger_idx_nonvolatile, | |
4688 | &ledger_idx_volatile_compressed, | |
4689 | &ledger_idx_nonvolatile_compressed, | |
4690 | &do_footprint); | |
fe8ab488 A |
4691 | /* more volatile bytes */ |
4692 | ledger_credit(owner->ledger, | |
0a7de745 A |
4693 | ledger_idx_volatile, |
4694 | PAGE_SIZE); | |
fe8ab488 A |
4695 | /* less not-quite-volatile bytes */ |
4696 | ledger_debit(owner->ledger, | |
0a7de745 A |
4697 | ledger_idx_nonvolatile, |
4698 | PAGE_SIZE); | |
d9a64523 A |
4699 | if (do_footprint) { |
4700 | /* less footprint */ | |
4701 | ledger_debit(owner->ledger, | |
0a7de745 A |
4702 | task_ledgers.phys_footprint, |
4703 | PAGE_SIZE); | |
d9a64523 | 4704 | } |
fe8ab488 | 4705 | } |
39037602 | 4706 | assert(m_object != kernel_object); |
d9a64523 | 4707 | assert(mem->vmp_pageq.next == 0 && mem->vmp_pageq.prev == 0); |
0b4c1975 A |
4708 | |
4709 | if (queueit == TRUE) { | |
39037602 | 4710 | if (m_object->purgable == VM_PURGABLE_EMPTY) { |
0b4c1975 A |
4711 | vm_page_deactivate(mem); |
4712 | } else { | |
4713 | vm_page_activate(mem); | |
4714 | } | |
2d21ac55 | 4715 | } |
593a1d5f | 4716 | |
6d2010ae | 4717 | VM_CHECK_MEMORYSTATUS; |
1c79356b | 4718 | } |
b0d623f7 | 4719 | VM_PAGE_CHECK(mem); |
1c79356b A |
4720 | } |
4721 | ||
4722 | /* | |
4723 | * vm_page_deactivate: | |
4724 | * | |
4725 | * Returns the given page to the inactive list, | |
4726 | * indicating that no physical maps have access | |
4727 | * to this page. [Used by the physical mapping system.] | |
4728 | * | |
4729 | * The page queues must be locked. | |
4730 | */ | |
4731 | void | |
4732 | vm_page_deactivate( | |
0a7de745 | 4733 | vm_page_t m) |
b0d623f7 A |
4734 | { |
4735 | vm_page_deactivate_internal(m, TRUE); | |
4736 | } | |
4737 | ||
4738 | ||
4739 | void | |
4740 | vm_page_deactivate_internal( | |
0a7de745 A |
4741 | vm_page_t m, |
4742 | boolean_t clear_hw_reference) | |
1c79356b | 4743 | { |
0a7de745 | 4744 | vm_object_t m_object; |
39037602 A |
4745 | |
4746 | m_object = VM_PAGE_OBJECT(m); | |
2d21ac55 | 4747 | |
1c79356b | 4748 | VM_PAGE_CHECK(m); |
39037602 A |
4749 | assert(m_object != kernel_object); |
4750 | assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr); | |
1c79356b | 4751 | |
39037602 A |
4752 | // dbgLog(VM_PAGE_GET_PHYS_PAGE(m), vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */ |
4753 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
1c79356b A |
4754 | /* |
4755 | * This page is no longer very interesting. If it was | |
4756 | * interesting (active or inactive/referenced), then we | |
4757 | * clear the reference bit and (re)enter it in the | |
4758 | * inactive queue. Note wired pages should not have | |
4759 | * their reference bit cleared. | |
4760 | */ | |
0a7de745 | 4761 | assert( !(m->vmp_absent && !m->vmp_unusual)); |
0b4c1975 | 4762 | |
0a7de745 | 4763 | if (m->vmp_gobbled) { /* can this happen? */ |
b0d623f7 | 4764 | assert( !VM_PAGE_WIRED(m)); |
2d21ac55 | 4765 | |
0a7de745 | 4766 | if (!m->vmp_private && !m->vmp_fictitious) { |
1c79356b | 4767 | vm_page_wire_count--; |
0a7de745 | 4768 | } |
1c79356b | 4769 | vm_page_gobble_count--; |
d9a64523 | 4770 | m->vmp_gobbled = FALSE; |
1c79356b | 4771 | } |
316670eb A |
4772 | /* |
4773 | * if this page is currently on the pageout queue, we can't do the | |
3e170ce0 | 4774 | * vm_page_queues_remove (which doesn't handle the pageout queue case) |
316670eb A |
4775 | * and we can't remove it manually since we would need the object lock |
4776 | * (which is not required here) to decrement the activity_in_progress | |
4777 | * reference which is held on the object while the page is in the pageout queue... | |
4778 | * just let the normal laundry processing proceed | |
39037602 | 4779 | */ |
d9a64523 A |
4780 | if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious || |
4781 | (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || | |
4782 | (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) || | |
39037602 | 4783 | VM_PAGE_WIRED(m)) { |
0a7de745 | 4784 | return; |
39037602 | 4785 | } |
0a7de745 | 4786 | if (!m->vmp_absent && clear_hw_reference == TRUE) { |
39037602 | 4787 | pmap_clear_reference(VM_PAGE_GET_PHYS_PAGE(m)); |
0a7de745 | 4788 | } |
2d21ac55 | 4789 | |
d9a64523 A |
4790 | m->vmp_reference = FALSE; |
4791 | m->vmp_no_cache = FALSE; | |
2d21ac55 | 4792 | |
0a7de745 | 4793 | if (!VM_PAGE_INACTIVE(m)) { |
39037602 | 4794 | vm_page_queues_remove(m, FALSE); |
0b4e3aa0 | 4795 | |
39037602 | 4796 | if (!VM_DYNAMIC_PAGING_ENABLED() && |
d9a64523 | 4797 | m->vmp_dirty && m_object->internal && |
39037602 | 4798 | (m_object->purgable == VM_PURGABLE_DENY || |
0a7de745 A |
4799 | m_object->purgable == VM_PURGABLE_NONVOLATILE || |
4800 | m_object->purgable == VM_PURGABLE_VOLATILE)) { | |
3e170ce0 | 4801 | vm_page_check_pageable_safe(m); |
0a7de745 | 4802 | vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq); |
d9a64523 | 4803 | m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q; |
2d21ac55 | 4804 | vm_page_throttled_count++; |
9bccf70c | 4805 | } else { |
39037602 | 4806 | if (m_object->named && m_object->ref_count == 1) { |
0a7de745 | 4807 | vm_page_speculate(m, FALSE); |
b0d623f7 | 4808 | #if DEVELOPMENT || DEBUG |
2d21ac55 | 4809 | vm_page_speculative_recreated++; |
b0d623f7 | 4810 | #endif |
2d21ac55 | 4811 | } else { |
3e170ce0 | 4812 | vm_page_enqueue_inactive(m, FALSE); |
2d21ac55 | 4813 | } |
9bccf70c | 4814 | } |
1c79356b A |
4815 | } |
4816 | } | |
4817 | ||
316670eb A |
4818 | /* |
4819 | * vm_page_enqueue_cleaned | |
4820 | * | |
4821 | * Put the page on the cleaned queue, mark it cleaned, etc. | |
4822 | * Being on the cleaned queue (and having m->clean_queue set) | |
4823 | * does ** NOT ** guarantee that the page is clean! | |
4824 | * | |
4825 | * Call with the queues lock held. | |
4826 | */ | |
4827 | ||
0a7de745 A |
4828 | void |
4829 | vm_page_enqueue_cleaned(vm_page_t m) | |
316670eb | 4830 | { |
0a7de745 | 4831 | vm_object_t m_object; |
39037602 A |
4832 | |
4833 | m_object = VM_PAGE_OBJECT(m); | |
4834 | ||
4835 | assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr); | |
4836 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 | 4837 | assert( !(m->vmp_absent && !m->vmp_unusual)); |
5ba3f43e A |
4838 | |
4839 | if (VM_PAGE_WIRED(m)) { | |
4840 | return; | |
4841 | } | |
316670eb | 4842 | |
d9a64523 | 4843 | if (m->vmp_gobbled) { |
0a7de745 | 4844 | if (!m->vmp_private && !m->vmp_fictitious) { |
316670eb | 4845 | vm_page_wire_count--; |
0a7de745 | 4846 | } |
316670eb | 4847 | vm_page_gobble_count--; |
d9a64523 | 4848 | m->vmp_gobbled = FALSE; |
316670eb A |
4849 | } |
4850 | /* | |
4851 | * if this page is currently on the pageout queue, we can't do the | |
3e170ce0 | 4852 | * vm_page_queues_remove (which doesn't handle the pageout queue case) |
316670eb A |
4853 | * and we can't remove it manually since we would need the object lock |
4854 | * (which is not required here) to decrement the activity_in_progress | |
4855 | * reference which is held on the object while the page is in the pageout queue... | |
4856 | * just let the normal laundry processing proceed | |
4857 | */ | |
d9a64523 A |
4858 | if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious || |
4859 | (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) || | |
4860 | (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) { | |
0a7de745 | 4861 | return; |
39037602 A |
4862 | } |
4863 | vm_page_queues_remove(m, FALSE); | |
316670eb | 4864 | |
3e170ce0 | 4865 | vm_page_check_pageable_safe(m); |
0a7de745 | 4866 | vm_page_queue_enter(&vm_page_queue_cleaned, m, vmp_pageq); |
d9a64523 | 4867 | m->vmp_q_state = VM_PAGE_ON_INACTIVE_CLEANED_Q; |
316670eb A |
4868 | vm_page_cleaned_count++; |
4869 | ||
316670eb | 4870 | vm_page_inactive_count++; |
39037602 | 4871 | if (m_object->internal) { |
39236c6e A |
4872 | vm_page_pageable_internal_count++; |
4873 | } else { | |
4874 | vm_page_pageable_external_count++; | |
4875 | } | |
39037602 | 4876 | #if CONFIG_BACKGROUND_QUEUE |
0a7de745 | 4877 | if (m->vmp_in_background) { |
39037602 | 4878 | vm_page_add_to_backgroundq(m, TRUE); |
0a7de745 | 4879 | } |
39037602 | 4880 | #endif |
d9a64523 | 4881 | VM_PAGEOUT_DEBUG(vm_pageout_enqueued_cleaned, 1); |
316670eb A |
4882 | } |
4883 | ||
1c79356b A |
4884 | /* |
4885 | * vm_page_activate: | |
4886 | * | |
4887 | * Put the specified page on the active list (if appropriate). | |
4888 | * | |
4889 | * The page queues must be locked. | |
4890 | */ | |
4891 | ||
4892 | void | |
4893 | vm_page_activate( | |
0a7de745 | 4894 | vm_page_t m) |
1c79356b | 4895 | { |
0a7de745 | 4896 | vm_object_t m_object; |
39037602 A |
4897 | |
4898 | m_object = VM_PAGE_OBJECT(m); | |
4899 | ||
1c79356b | 4900 | VM_PAGE_CHECK(m); |
0a7de745 | 4901 | #ifdef FIXME_4778297 |
39037602 | 4902 | assert(m_object != kernel_object); |
91447636 | 4903 | #endif |
39037602 A |
4904 | assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr); |
4905 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 | 4906 | assert( !(m->vmp_absent && !m->vmp_unusual)); |
0b4c1975 | 4907 | |
d9a64523 | 4908 | if (m->vmp_gobbled) { |
b0d623f7 | 4909 | assert( !VM_PAGE_WIRED(m)); |
0a7de745 | 4910 | if (!m->vmp_private && !m->vmp_fictitious) { |
1c79356b | 4911 | vm_page_wire_count--; |
0a7de745 | 4912 | } |
1c79356b | 4913 | vm_page_gobble_count--; |
d9a64523 | 4914 | m->vmp_gobbled = FALSE; |
1c79356b | 4915 | } |
316670eb A |
4916 | /* |
4917 | * if this page is currently on the pageout queue, we can't do the | |
3e170ce0 | 4918 | * vm_page_queues_remove (which doesn't handle the pageout queue case) |
316670eb A |
4919 | * and we can't remove it manually since we would need the object lock |
4920 | * (which is not required here) to decrement the activity_in_progress | |
4921 | * reference which is held on the object while the page is in the pageout queue... | |
4922 | * just let the normal laundry processing proceed | |
4923 | */ | |
d9a64523 A |
4924 | if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious || |
4925 | (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || | |
0a7de745 | 4926 | (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) { |
1c79356b | 4927 | return; |
0a7de745 | 4928 | } |
1c79356b | 4929 | |
2d21ac55 | 4930 | #if DEBUG |
0a7de745 A |
4931 | if (m->vmp_q_state == VM_PAGE_ON_ACTIVE_Q) { |
4932 | panic("vm_page_activate: already active"); | |
4933 | } | |
2d21ac55 A |
4934 | #endif |
4935 | ||
d9a64523 | 4936 | if (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) { |
2d21ac55 A |
4937 | DTRACE_VM2(pgrec, int, 1, (uint64_t *), NULL); |
4938 | DTRACE_VM2(pgfrec, int, 1, (uint64_t *), NULL); | |
4939 | } | |
0a7de745 | 4940 | |
39037602 | 4941 | vm_page_queues_remove(m, FALSE); |
2d21ac55 | 4942 | |
0a7de745 | 4943 | if (!VM_PAGE_WIRED(m)) { |
3e170ce0 | 4944 | vm_page_check_pageable_safe(m); |
0a7de745 A |
4945 | if (!VM_DYNAMIC_PAGING_ENABLED() && |
4946 | m->vmp_dirty && m_object->internal && | |
39037602 | 4947 | (m_object->purgable == VM_PURGABLE_DENY || |
0a7de745 A |
4948 | m_object->purgable == VM_PURGABLE_NONVOLATILE || |
4949 | m_object->purgable == VM_PURGABLE_VOLATILE)) { | |
4950 | vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq); | |
d9a64523 | 4951 | m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q; |
2d21ac55 | 4952 | vm_page_throttled_count++; |
9bccf70c | 4953 | } else { |
39037602 A |
4954 | #if CONFIG_SECLUDED_MEMORY |
4955 | if (secluded_for_filecache && | |
4956 | vm_page_secluded_target != 0 && | |
4957 | num_tasks_can_use_secluded_mem == 0 && | |
5ba3f43e | 4958 | m_object->eligible_for_secluded) { |
0a7de745 | 4959 | vm_page_queue_enter(&vm_page_queue_secluded, m, vmp_pageq); |
d9a64523 | 4960 | m->vmp_q_state = VM_PAGE_ON_SECLUDED_Q; |
39037602 | 4961 | vm_page_secluded_count++; |
cb323159 | 4962 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); |
39037602 A |
4963 | vm_page_secluded_count_inuse++; |
4964 | assert(!m_object->internal); | |
4965 | // vm_page_pageable_external_count++; | |
4966 | } else | |
4967 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
4968 | vm_page_enqueue_active(m, FALSE); | |
9bccf70c | 4969 | } |
d9a64523 A |
4970 | m->vmp_reference = TRUE; |
4971 | m->vmp_no_cache = FALSE; | |
1c79356b | 4972 | } |
b0d623f7 | 4973 | VM_PAGE_CHECK(m); |
2d21ac55 A |
4974 | } |
4975 | ||
4976 | ||
4977 | /* | |
4978 | * vm_page_speculate: | |
4979 | * | |
4980 | * Put the specified page on the speculative list (if appropriate). | |
4981 | * | |
4982 | * The page queues must be locked. | |
4983 | */ | |
4984 | void | |
4985 | vm_page_speculate( | |
0a7de745 A |
4986 | vm_page_t m, |
4987 | boolean_t new) | |
2d21ac55 | 4988 | { |
0a7de745 A |
4989 | struct vm_speculative_age_q *aq; |
4990 | vm_object_t m_object; | |
39037602 A |
4991 | |
4992 | m_object = VM_PAGE_OBJECT(m); | |
2d21ac55 A |
4993 | |
4994 | VM_PAGE_CHECK(m); | |
3e170ce0 A |
4995 | vm_page_check_pageable_safe(m); |
4996 | ||
39037602 A |
4997 | assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr); |
4998 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 | 4999 | assert( !(m->vmp_absent && !m->vmp_unusual)); |
39037602 | 5000 | assert(m_object->internal == FALSE); |
b0d623f7 | 5001 | |
316670eb A |
5002 | /* |
5003 | * if this page is currently on the pageout queue, we can't do the | |
3e170ce0 | 5004 | * vm_page_queues_remove (which doesn't handle the pageout queue case) |
316670eb A |
5005 | * and we can't remove it manually since we would need the object lock |
5006 | * (which is not required here) to decrement the activity_in_progress | |
5007 | * reference which is held on the object while the page is in the pageout queue... | |
5008 | * just let the normal laundry processing proceed | |
5009 | */ | |
d9a64523 A |
5010 | if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious || |
5011 | (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || | |
0a7de745 | 5012 | (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) { |
6d2010ae | 5013 | return; |
0a7de745 | 5014 | } |
0b4c1975 | 5015 | |
39037602 | 5016 | vm_page_queues_remove(m, FALSE); |
b0d623f7 | 5017 | |
0a7de745 A |
5018 | if (!VM_PAGE_WIRED(m)) { |
5019 | mach_timespec_t ts; | |
b0d623f7 A |
5020 | clock_sec_t sec; |
5021 | clock_nsec_t nsec; | |
2d21ac55 | 5022 | |
0a7de745 | 5023 | clock_get_system_nanotime(&sec, &nsec); |
b0d623f7 A |
5024 | ts.tv_sec = (unsigned int) sec; |
5025 | ts.tv_nsec = nsec; | |
2d21ac55 A |
5026 | |
5027 | if (vm_page_speculative_count == 0) { | |
2d21ac55 A |
5028 | speculative_age_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q; |
5029 | speculative_steal_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q; | |
5030 | ||
5031 | aq = &vm_page_queue_speculative[speculative_age_index]; | |
5032 | ||
0a7de745 | 5033 | /* |
2d21ac55 A |
5034 | * set the timer to begin a new group |
5035 | */ | |
d9a64523 A |
5036 | aq->age_ts.tv_sec = vm_pageout_state.vm_page_speculative_q_age_ms / 1000; |
5037 | aq->age_ts.tv_nsec = (vm_pageout_state.vm_page_speculative_q_age_ms % 1000) * 1000 * NSEC_PER_USEC; | |
cb323159 | 5038 | |
2d21ac55 A |
5039 | ADD_MACH_TIMESPEC(&aq->age_ts, &ts); |
5040 | } else { | |
5041 | aq = &vm_page_queue_speculative[speculative_age_index]; | |
5042 | ||
5043 | if (CMP_MACH_TIMESPEC(&ts, &aq->age_ts) >= 0) { | |
0a7de745 | 5044 | speculative_age_index++; |
2d21ac55 | 5045 | |
0a7de745 A |
5046 | if (speculative_age_index > VM_PAGE_MAX_SPECULATIVE_AGE_Q) { |
5047 | speculative_age_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q; | |
5048 | } | |
2d21ac55 | 5049 | if (speculative_age_index == speculative_steal_index) { |
0a7de745 | 5050 | speculative_steal_index = speculative_age_index + 1; |
2d21ac55 | 5051 | |
0a7de745 A |
5052 | if (speculative_steal_index > VM_PAGE_MAX_SPECULATIVE_AGE_Q) { |
5053 | speculative_steal_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q; | |
5054 | } | |
2d21ac55 A |
5055 | } |
5056 | aq = &vm_page_queue_speculative[speculative_age_index]; | |
5057 | ||
0a7de745 A |
5058 | if (!vm_page_queue_empty(&aq->age_q)) { |
5059 | vm_page_speculate_ageit(aq); | |
5060 | } | |
2d21ac55 | 5061 | |
d9a64523 A |
5062 | aq->age_ts.tv_sec = vm_pageout_state.vm_page_speculative_q_age_ms / 1000; |
5063 | aq->age_ts.tv_nsec = (vm_pageout_state.vm_page_speculative_q_age_ms % 1000) * 1000 * NSEC_PER_USEC; | |
cb323159 | 5064 | |
2d21ac55 A |
5065 | ADD_MACH_TIMESPEC(&aq->age_ts, &ts); |
5066 | } | |
5067 | } | |
d9a64523 A |
5068 | vm_page_enqueue_tail(&aq->age_q, &m->vmp_pageq); |
5069 | m->vmp_q_state = VM_PAGE_ON_SPECULATIVE_Q; | |
2d21ac55 | 5070 | vm_page_speculative_count++; |
39037602 | 5071 | vm_page_pageable_external_count++; |
2d21ac55 A |
5072 | |
5073 | if (new == TRUE) { | |
39037602 | 5074 | vm_object_lock_assert_exclusive(m_object); |
6d2010ae | 5075 | |
0a7de745 | 5076 | m_object->pages_created++; |
b0d623f7 | 5077 | #if DEVELOPMENT || DEBUG |
2d21ac55 | 5078 | vm_page_speculative_created++; |
b0d623f7 | 5079 | #endif |
2d21ac55 A |
5080 | } |
5081 | } | |
b0d623f7 | 5082 | VM_PAGE_CHECK(m); |
2d21ac55 A |
5083 | } |
5084 | ||
5085 | ||
5086 | /* | |
5087 | * move pages from the specified aging bin to | |
5088 | * the speculative bin that pageout_scan claims from | |
5089 | * | |
5090 | * The page queues must be locked. | |
5091 | */ | |
5092 | void | |
5093 | vm_page_speculate_ageit(struct vm_speculative_age_q *aq) | |
5094 | { | |
0a7de745 A |
5095 | struct vm_speculative_age_q *sq; |
5096 | vm_page_t t; | |
2d21ac55 A |
5097 | |
5098 | sq = &vm_page_queue_speculative[VM_PAGE_SPECULATIVE_AGED_Q]; | |
5099 | ||
39037602 | 5100 | if (vm_page_queue_empty(&sq->age_q)) { |
0a7de745 | 5101 | sq->age_q.next = aq->age_q.next; |
2d21ac55 | 5102 | sq->age_q.prev = aq->age_q.prev; |
0a7de745 | 5103 | |
39037602 | 5104 | t = (vm_page_t)VM_PAGE_UNPACK_PTR(sq->age_q.next); |
d9a64523 | 5105 | t->vmp_pageq.prev = VM_PAGE_PACK_PTR(&sq->age_q); |
2d21ac55 | 5106 | |
39037602 | 5107 | t = (vm_page_t)VM_PAGE_UNPACK_PTR(sq->age_q.prev); |
d9a64523 | 5108 | t->vmp_pageq.next = VM_PAGE_PACK_PTR(&sq->age_q); |
2d21ac55 | 5109 | } else { |
0a7de745 | 5110 | t = (vm_page_t)VM_PAGE_UNPACK_PTR(sq->age_q.prev); |
d9a64523 | 5111 | t->vmp_pageq.next = aq->age_q.next; |
0a7de745 | 5112 | |
39037602 | 5113 | t = (vm_page_t)VM_PAGE_UNPACK_PTR(aq->age_q.next); |
d9a64523 | 5114 | t->vmp_pageq.prev = sq->age_q.prev; |
2d21ac55 | 5115 | |
39037602 | 5116 | t = (vm_page_t)VM_PAGE_UNPACK_PTR(aq->age_q.prev); |
d9a64523 | 5117 | t->vmp_pageq.next = VM_PAGE_PACK_PTR(&sq->age_q); |
2d21ac55 A |
5118 | |
5119 | sq->age_q.prev = aq->age_q.prev; | |
1c79356b | 5120 | } |
39037602 | 5121 | vm_page_queue_init(&aq->age_q); |
2d21ac55 A |
5122 | } |
5123 | ||
5124 | ||
5125 | void | |
5126 | vm_page_lru( | |
0a7de745 | 5127 | vm_page_t m) |
2d21ac55 A |
5128 | { |
5129 | VM_PAGE_CHECK(m); | |
39037602 A |
5130 | assert(VM_PAGE_OBJECT(m) != kernel_object); |
5131 | assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr); | |
2d21ac55 | 5132 | |
39037602 | 5133 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); |
d9a64523 A |
5134 | |
5135 | if (m->vmp_q_state == VM_PAGE_ON_INACTIVE_EXTERNAL_Q) { | |
0a7de745 A |
5136 | /* |
5137 | * we don't need to do all the other work that | |
d9a64523 A |
5138 | * vm_page_queues_remove and vm_page_enqueue_inactive |
5139 | * bring along for the ride | |
5140 | */ | |
0a7de745 | 5141 | assert(!m->vmp_laundry); |
d9a64523 | 5142 | assert(!m->vmp_private); |
0a7de745 | 5143 | |
d9a64523 A |
5144 | m->vmp_no_cache = FALSE; |
5145 | ||
0a7de745 A |
5146 | vm_page_queue_remove(&vm_page_queue_inactive, m, vmp_pageq); |
5147 | vm_page_queue_enter(&vm_page_queue_inactive, m, vmp_pageq); | |
d9a64523 A |
5148 | |
5149 | return; | |
5150 | } | |
316670eb A |
5151 | /* |
5152 | * if this page is currently on the pageout queue, we can't do the | |
3e170ce0 | 5153 | * vm_page_queues_remove (which doesn't handle the pageout queue case) |
316670eb A |
5154 | * and we can't remove it manually since we would need the object lock |
5155 | * (which is not required here) to decrement the activity_in_progress | |
5156 | * reference which is held on the object while the page is in the pageout queue... | |
5157 | * just let the normal laundry processing proceed | |
5158 | */ | |
d9a64523 A |
5159 | if (m->vmp_laundry || m->vmp_private || |
5160 | (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || | |
5161 | (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) || | |
0a7de745 | 5162 | VM_PAGE_WIRED(m)) { |
2d21ac55 | 5163 | return; |
0a7de745 | 5164 | } |
2d21ac55 | 5165 | |
d9a64523 | 5166 | m->vmp_no_cache = FALSE; |
2d21ac55 | 5167 | |
39037602 | 5168 | vm_page_queues_remove(m, FALSE); |
2d21ac55 | 5169 | |
3e170ce0 | 5170 | vm_page_enqueue_inactive(m, FALSE); |
1c79356b A |
5171 | } |
5172 | ||
2d21ac55 | 5173 | |
b0d623f7 A |
5174 | void |
5175 | vm_page_reactivate_all_throttled(void) | |
5176 | { | |
0a7de745 A |
5177 | vm_page_t first_throttled, last_throttled; |
5178 | vm_page_t first_active; | |
5179 | vm_page_t m; | |
5180 | int extra_active_count; | |
5181 | int extra_internal_count, extra_external_count; | |
5182 | vm_object_t m_object; | |
5183 | ||
5184 | if (!VM_DYNAMIC_PAGING_ENABLED()) { | |
6d2010ae | 5185 | return; |
0a7de745 | 5186 | } |
6d2010ae | 5187 | |
b0d623f7 | 5188 | extra_active_count = 0; |
39236c6e A |
5189 | extra_internal_count = 0; |
5190 | extra_external_count = 0; | |
b0d623f7 | 5191 | vm_page_lock_queues(); |
0a7de745 | 5192 | if (!vm_page_queue_empty(&vm_page_queue_throttled)) { |
b0d623f7 A |
5193 | /* |
5194 | * Switch "throttled" pages to "active". | |
5195 | */ | |
0a7de745 | 5196 | vm_page_queue_iterate(&vm_page_queue_throttled, m, vmp_pageq) { |
b0d623f7 | 5197 | VM_PAGE_CHECK(m); |
d9a64523 | 5198 | assert(m->vmp_q_state == VM_PAGE_ON_THROTTLED_Q); |
39037602 A |
5199 | |
5200 | m_object = VM_PAGE_OBJECT(m); | |
6d2010ae A |
5201 | |
5202 | extra_active_count++; | |
39037602 | 5203 | if (m_object->internal) { |
39236c6e A |
5204 | extra_internal_count++; |
5205 | } else { | |
5206 | extra_external_count++; | |
5207 | } | |
6d2010ae | 5208 | |
d9a64523 | 5209 | m->vmp_q_state = VM_PAGE_ON_ACTIVE_Q; |
b0d623f7 | 5210 | VM_PAGE_CHECK(m); |
39037602 | 5211 | #if CONFIG_BACKGROUND_QUEUE |
0a7de745 | 5212 | if (m->vmp_in_background) { |
39037602 | 5213 | vm_page_add_to_backgroundq(m, FALSE); |
0a7de745 | 5214 | } |
39037602 | 5215 | #endif |
b0d623f7 A |
5216 | } |
5217 | ||
5218 | /* | |
5219 | * Transfer the entire throttled queue to a regular LRU page queues. | |
5220 | * We insert it at the head of the active queue, so that these pages | |
5221 | * get re-evaluated by the LRU algorithm first, since they've been | |
5222 | * completely out of it until now. | |
5223 | */ | |
39037602 A |
5224 | first_throttled = (vm_page_t) vm_page_queue_first(&vm_page_queue_throttled); |
5225 | last_throttled = (vm_page_t) vm_page_queue_last(&vm_page_queue_throttled); | |
5226 | first_active = (vm_page_t) vm_page_queue_first(&vm_page_queue_active); | |
5227 | if (vm_page_queue_empty(&vm_page_queue_active)) { | |
5228 | vm_page_queue_active.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_throttled); | |
b0d623f7 | 5229 | } else { |
d9a64523 | 5230 | first_active->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_throttled); |
b0d623f7 | 5231 | } |
39037602 | 5232 | vm_page_queue_active.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_throttled); |
d9a64523 A |
5233 | first_throttled->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(&vm_page_queue_active); |
5234 | last_throttled->vmp_pageq.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_active); | |
b0d623f7 A |
5235 | |
5236 | #if DEBUG | |
5237 | printf("reactivated %d throttled pages\n", vm_page_throttled_count); | |
5238 | #endif | |
39037602 | 5239 | vm_page_queue_init(&vm_page_queue_throttled); |
b0d623f7 A |
5240 | /* |
5241 | * Adjust the global page counts. | |
5242 | */ | |
5243 | vm_page_active_count += extra_active_count; | |
39236c6e A |
5244 | vm_page_pageable_internal_count += extra_internal_count; |
5245 | vm_page_pageable_external_count += extra_external_count; | |
b0d623f7 A |
5246 | vm_page_throttled_count = 0; |
5247 | } | |
5248 | assert(vm_page_throttled_count == 0); | |
39037602 | 5249 | assert(vm_page_queue_empty(&vm_page_queue_throttled)); |
b0d623f7 A |
5250 | vm_page_unlock_queues(); |
5251 | } | |
5252 | ||
5253 | ||
5254 | /* | |
5255 | * move pages from the indicated local queue to the global active queue | |
5256 | * its ok to fail if we're below the hard limit and force == FALSE | |
5257 | * the nolocks == TRUE case is to allow this function to be run on | |
5258 | * the hibernate path | |
5259 | */ | |
5260 | ||
5261 | void | |
5262 | vm_page_reactivate_local(uint32_t lid, boolean_t force, boolean_t nolocks) | |
5263 | { | |
0a7de745 A |
5264 | struct vpl *lq; |
5265 | vm_page_t first_local, last_local; | |
5266 | vm_page_t first_active; | |
5267 | vm_page_t m; | |
5268 | uint32_t count = 0; | |
b0d623f7 | 5269 | |
0a7de745 | 5270 | if (vm_page_local_q == NULL) { |
b0d623f7 | 5271 | return; |
0a7de745 | 5272 | } |
b0d623f7 | 5273 | |
f427ee49 | 5274 | lq = zpercpu_get_cpu(vm_page_local_q, lid); |
b0d623f7 A |
5275 | |
5276 | if (nolocks == FALSE) { | |
5277 | if (lq->vpl_count < vm_page_local_q_hard_limit && force == FALSE) { | |
0a7de745 | 5278 | if (!vm_page_trylockspin_queues()) { |
b0d623f7 | 5279 | return; |
0a7de745 A |
5280 | } |
5281 | } else { | |
b0d623f7 | 5282 | vm_page_lockspin_queues(); |
0a7de745 | 5283 | } |
b0d623f7 A |
5284 | |
5285 | VPL_LOCK(&lq->vpl_lock); | |
5286 | } | |
5287 | if (lq->vpl_count) { | |
5288 | /* | |
5289 | * Switch "local" pages to "active". | |
5290 | */ | |
39037602 | 5291 | assert(!vm_page_queue_empty(&lq->vpl_queue)); |
b0d623f7 | 5292 | |
0a7de745 | 5293 | vm_page_queue_iterate(&lq->vpl_queue, m, vmp_pageq) { |
b0d623f7 | 5294 | VM_PAGE_CHECK(m); |
3e170ce0 | 5295 | vm_page_check_pageable_safe(m); |
d9a64523 A |
5296 | assert(m->vmp_q_state == VM_PAGE_ON_ACTIVE_LOCAL_Q); |
5297 | assert(!m->vmp_fictitious); | |
b0d623f7 | 5298 | |
0a7de745 | 5299 | if (m->vmp_local_id != lid) { |
b0d623f7 | 5300 | panic("vm_page_reactivate_local: found vm_page_t(%p) with wrong cpuid", m); |
0a7de745 A |
5301 | } |
5302 | ||
d9a64523 A |
5303 | m->vmp_local_id = 0; |
5304 | m->vmp_q_state = VM_PAGE_ON_ACTIVE_Q; | |
b0d623f7 | 5305 | VM_PAGE_CHECK(m); |
39037602 | 5306 | #if CONFIG_BACKGROUND_QUEUE |
0a7de745 | 5307 | if (m->vmp_in_background) { |
39037602 | 5308 | vm_page_add_to_backgroundq(m, FALSE); |
0a7de745 | 5309 | } |
39037602 | 5310 | #endif |
b0d623f7 A |
5311 | count++; |
5312 | } | |
0a7de745 | 5313 | if (count != lq->vpl_count) { |
b0d623f7 | 5314 | panic("vm_page_reactivate_local: count = %d, vm_page_local_count = %d\n", count, lq->vpl_count); |
0a7de745 | 5315 | } |
b0d623f7 A |
5316 | |
5317 | /* | |
5318 | * Transfer the entire local queue to a regular LRU page queues. | |
5319 | */ | |
39037602 A |
5320 | first_local = (vm_page_t) vm_page_queue_first(&lq->vpl_queue); |
5321 | last_local = (vm_page_t) vm_page_queue_last(&lq->vpl_queue); | |
5322 | first_active = (vm_page_t) vm_page_queue_first(&vm_page_queue_active); | |
b0d623f7 | 5323 | |
39037602 A |
5324 | if (vm_page_queue_empty(&vm_page_queue_active)) { |
5325 | vm_page_queue_active.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_local); | |
b0d623f7 | 5326 | } else { |
d9a64523 | 5327 | first_active->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_local); |
b0d623f7 | 5328 | } |
39037602 | 5329 | vm_page_queue_active.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_local); |
d9a64523 A |
5330 | first_local->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(&vm_page_queue_active); |
5331 | last_local->vmp_pageq.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_active); | |
b0d623f7 | 5332 | |
39037602 | 5333 | vm_page_queue_init(&lq->vpl_queue); |
b0d623f7 A |
5334 | /* |
5335 | * Adjust the global page counts. | |
5336 | */ | |
5337 | vm_page_active_count += lq->vpl_count; | |
39236c6e A |
5338 | vm_page_pageable_internal_count += lq->vpl_internal_count; |
5339 | vm_page_pageable_external_count += lq->vpl_external_count; | |
b0d623f7 | 5340 | lq->vpl_count = 0; |
39236c6e A |
5341 | lq->vpl_internal_count = 0; |
5342 | lq->vpl_external_count = 0; | |
b0d623f7 | 5343 | } |
39037602 | 5344 | assert(vm_page_queue_empty(&lq->vpl_queue)); |
b0d623f7 A |
5345 | |
5346 | if (nolocks == FALSE) { | |
5347 | VPL_UNLOCK(&lq->vpl_lock); | |
d9a64523 A |
5348 | |
5349 | vm_page_balance_inactive(count / 4); | |
b0d623f7 A |
5350 | vm_page_unlock_queues(); |
5351 | } | |
5352 | } | |
5353 | ||
1c79356b A |
5354 | /* |
5355 | * vm_page_part_zero_fill: | |
5356 | * | |
5357 | * Zero-fill a part of the page. | |
5358 | */ | |
39236c6e | 5359 | #define PMAP_ZERO_PART_PAGE_IMPLEMENTED |
1c79356b A |
5360 | void |
5361 | vm_page_part_zero_fill( | |
0a7de745 A |
5362 | vm_page_t m, |
5363 | vm_offset_t m_pa, | |
5364 | vm_size_t len) | |
1c79356b | 5365 | { |
316670eb A |
5366 | #if 0 |
5367 | /* | |
5368 | * we don't hold the page queue lock | |
5369 | * so this check isn't safe to make | |
5370 | */ | |
1c79356b | 5371 | VM_PAGE_CHECK(m); |
316670eb A |
5372 | #endif |
5373 | ||
1c79356b | 5374 | #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED |
39037602 | 5375 | pmap_zero_part_page(VM_PAGE_GET_PHYS_PAGE(m), m_pa, len); |
1c79356b | 5376 | #else |
0a7de745 | 5377 | vm_page_t tmp; |
1c79356b | 5378 | while (1) { |
0a7de745 | 5379 | tmp = vm_page_grab(); |
1c79356b A |
5380 | if (tmp == VM_PAGE_NULL) { |
5381 | vm_page_wait(THREAD_UNINT); | |
5382 | continue; | |
5383 | } | |
0a7de745 | 5384 | break; |
1c79356b A |
5385 | } |
5386 | vm_page_zero_fill(tmp); | |
0a7de745 | 5387 | if (m_pa != 0) { |
1c79356b A |
5388 | vm_page_part_copy(m, 0, tmp, 0, m_pa); |
5389 | } | |
0a7de745 A |
5390 | if ((m_pa + len) < PAGE_SIZE) { |
5391 | vm_page_part_copy(m, m_pa + len, tmp, | |
5392 | m_pa + len, PAGE_SIZE - (m_pa + len)); | |
1c79356b | 5393 | } |
0a7de745 A |
5394 | vm_page_copy(tmp, m); |
5395 | VM_PAGE_FREE(tmp); | |
1c79356b | 5396 | #endif |
1c79356b A |
5397 | } |
5398 | ||
5399 | /* | |
5400 | * vm_page_zero_fill: | |
5401 | * | |
5402 | * Zero-fill the specified page. | |
5403 | */ | |
5404 | void | |
5405 | vm_page_zero_fill( | |
0a7de745 | 5406 | vm_page_t m) |
1c79356b | 5407 | { |
316670eb A |
5408 | #if 0 |
5409 | /* | |
5410 | * we don't hold the page queue lock | |
5411 | * so this check isn't safe to make | |
5412 | */ | |
1c79356b | 5413 | VM_PAGE_CHECK(m); |
316670eb | 5414 | #endif |
1c79356b | 5415 | |
39037602 A |
5416 | // dbgTrace(0xAEAEAEAE, VM_PAGE_GET_PHYS_PAGE(m), 0); /* (BRINGUP) */ |
5417 | pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(m)); | |
1c79356b A |
5418 | } |
5419 | ||
5420 | /* | |
5421 | * vm_page_part_copy: | |
5422 | * | |
5423 | * copy part of one page to another | |
5424 | */ | |
5425 | ||
5426 | void | |
5427 | vm_page_part_copy( | |
0a7de745 A |
5428 | vm_page_t src_m, |
5429 | vm_offset_t src_pa, | |
5430 | vm_page_t dst_m, | |
5431 | vm_offset_t dst_pa, | |
5432 | vm_size_t len) | |
1c79356b | 5433 | { |
316670eb A |
5434 | #if 0 |
5435 | /* | |
5436 | * we don't hold the page queue lock | |
5437 | * so this check isn't safe to make | |
5438 | */ | |
1c79356b A |
5439 | VM_PAGE_CHECK(src_m); |
5440 | VM_PAGE_CHECK(dst_m); | |
316670eb | 5441 | #endif |
39037602 | 5442 | pmap_copy_part_page(VM_PAGE_GET_PHYS_PAGE(src_m), src_pa, |
0a7de745 | 5443 | VM_PAGE_GET_PHYS_PAGE(dst_m), dst_pa, len); |
1c79356b A |
5444 | } |
5445 | ||
5446 | /* | |
5447 | * vm_page_copy: | |
5448 | * | |
5449 | * Copy one page to another | |
5450 | */ | |
5451 | ||
2d21ac55 A |
5452 | int vm_page_copy_cs_validations = 0; |
5453 | int vm_page_copy_cs_tainted = 0; | |
5454 | ||
1c79356b A |
5455 | void |
5456 | vm_page_copy( | |
0a7de745 A |
5457 | vm_page_t src_m, |
5458 | vm_page_t dest_m) | |
1c79356b | 5459 | { |
0a7de745 | 5460 | vm_object_t src_m_object; |
39037602 A |
5461 | |
5462 | src_m_object = VM_PAGE_OBJECT(src_m); | |
5463 | ||
316670eb A |
5464 | #if 0 |
5465 | /* | |
5466 | * we don't hold the page queue lock | |
5467 | * so this check isn't safe to make | |
5468 | */ | |
1c79356b A |
5469 | VM_PAGE_CHECK(src_m); |
5470 | VM_PAGE_CHECK(dest_m); | |
316670eb | 5471 | #endif |
39037602 | 5472 | vm_object_lock_assert_held(src_m_object); |
1c79356b | 5473 | |
39037602 A |
5474 | if (src_m_object != VM_OBJECT_NULL && |
5475 | src_m_object->code_signed) { | |
2d21ac55 | 5476 | /* |
4a3eedf9 | 5477 | * We're copying a page from a code-signed object. |
2d21ac55 A |
5478 | * Whoever ends up mapping the copy page might care about |
5479 | * the original page's integrity, so let's validate the | |
5480 | * source page now. | |
5481 | */ | |
5482 | vm_page_copy_cs_validations++; | |
f427ee49 | 5483 | vm_page_validate_cs(src_m, PAGE_SIZE, 0); |
39037602 A |
5484 | #if DEVELOPMENT || DEBUG |
5485 | DTRACE_VM4(codesigned_copy, | |
0a7de745 A |
5486 | vm_object_t, src_m_object, |
5487 | vm_object_offset_t, src_m->vmp_offset, | |
5488 | int, src_m->vmp_cs_validated, | |
5489 | int, src_m->vmp_cs_tainted); | |
39037602 | 5490 | #endif /* DEVELOPMENT || DEBUG */ |
2d21ac55 | 5491 | } |
6d2010ae | 5492 | |
2d21ac55 | 5493 | /* |
b0d623f7 A |
5494 | * Propagate the cs_tainted bit to the copy page. Do not propagate |
5495 | * the cs_validated bit. | |
2d21ac55 | 5496 | */ |
d9a64523 | 5497 | dest_m->vmp_cs_tainted = src_m->vmp_cs_tainted; |
f427ee49 | 5498 | dest_m->vmp_cs_nx = src_m->vmp_cs_nx; |
d9a64523 | 5499 | if (dest_m->vmp_cs_tainted) { |
2d21ac55 A |
5500 | vm_page_copy_cs_tainted++; |
5501 | } | |
d9a64523 | 5502 | dest_m->vmp_error = src_m->vmp_error; /* sliding src_m might have failed... */ |
39037602 | 5503 | pmap_copy_page(VM_PAGE_GET_PHYS_PAGE(src_m), VM_PAGE_GET_PHYS_PAGE(dest_m)); |
1c79356b A |
5504 | } |
5505 | ||
2d21ac55 | 5506 | #if MACH_ASSERT |
b0d623f7 A |
5507 | static void |
5508 | _vm_page_print( | |
0a7de745 | 5509 | vm_page_t p) |
b0d623f7 A |
5510 | { |
5511 | printf("vm_page %p: \n", p); | |
39037602 | 5512 | printf(" pageq: next=%p prev=%p\n", |
0a7de745 A |
5513 | (vm_page_t)VM_PAGE_UNPACK_PTR(p->vmp_pageq.next), |
5514 | (vm_page_t)VM_PAGE_UNPACK_PTR(p->vmp_pageq.prev)); | |
39037602 | 5515 | printf(" listq: next=%p prev=%p\n", |
0a7de745 A |
5516 | (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_listq.next)), |
5517 | (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_listq.prev))); | |
d9a64523 | 5518 | printf(" next=%p\n", (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_next_m))); |
0a7de745 | 5519 | printf(" object=%p offset=0x%llx\n", VM_PAGE_OBJECT(p), p->vmp_offset); |
d9a64523 A |
5520 | printf(" wire_count=%u\n", p->vmp_wire_count); |
5521 | printf(" q_state=%u\n", p->vmp_q_state); | |
b0d623f7 | 5522 | |
39037602 | 5523 | printf(" %slaundry, %sref, %sgobbled, %sprivate\n", |
0a7de745 A |
5524 | (p->vmp_laundry ? "" : "!"), |
5525 | (p->vmp_reference ? "" : "!"), | |
5526 | (p->vmp_gobbled ? "" : "!"), | |
5527 | (p->vmp_private ? "" : "!")); | |
b0d623f7 | 5528 | printf(" %sbusy, %swanted, %stabled, %sfictitious, %spmapped, %swpmapped\n", |
0a7de745 A |
5529 | (p->vmp_busy ? "" : "!"), |
5530 | (p->vmp_wanted ? "" : "!"), | |
5531 | (p->vmp_tabled ? "" : "!"), | |
5532 | (p->vmp_fictitious ? "" : "!"), | |
5533 | (p->vmp_pmapped ? "" : "!"), | |
5534 | (p->vmp_wpmapped ? "" : "!")); | |
39037602 | 5535 | printf(" %sfree_when_done, %sabsent, %serror, %sdirty, %scleaning, %sprecious, %sclustered\n", |
0a7de745 A |
5536 | (p->vmp_free_when_done ? "" : "!"), |
5537 | (p->vmp_absent ? "" : "!"), | |
5538 | (p->vmp_error ? "" : "!"), | |
5539 | (p->vmp_dirty ? "" : "!"), | |
5540 | (p->vmp_cleaning ? "" : "!"), | |
5541 | (p->vmp_precious ? "" : "!"), | |
5542 | (p->vmp_clustered ? "" : "!")); | |
5ba3f43e | 5543 | printf(" %soverwriting, %srestart, %sunusual\n", |
0a7de745 A |
5544 | (p->vmp_overwriting ? "" : "!"), |
5545 | (p->vmp_restart ? "" : "!"), | |
5546 | (p->vmp_unusual ? "" : "!")); | |
f427ee49 A |
5547 | printf(" cs_validated=%d, cs_tainted=%d, cs_nx=%d, %sno_cache\n", |
5548 | p->vmp_cs_validated, | |
5549 | p->vmp_cs_tainted, | |
5550 | p->vmp_cs_nx, | |
0a7de745 | 5551 | (p->vmp_no_cache ? "" : "!")); |
b0d623f7 | 5552 | |
39037602 | 5553 | printf("phys_page=0x%x\n", VM_PAGE_GET_PHYS_PAGE(p)); |
b0d623f7 A |
5554 | } |
5555 | ||
1c79356b A |
5556 | /* |
5557 | * Check that the list of pages is ordered by | |
5558 | * ascending physical address and has no holes. | |
5559 | */ | |
2d21ac55 | 5560 | static int |
1c79356b | 5561 | vm_page_verify_contiguous( |
0a7de745 A |
5562 | vm_page_t pages, |
5563 | unsigned int npages) | |
1c79356b | 5564 | { |
0a7de745 A |
5565 | vm_page_t m; |
5566 | unsigned int page_count; | |
5567 | vm_offset_t prev_addr; | |
1c79356b | 5568 | |
39037602 | 5569 | prev_addr = VM_PAGE_GET_PHYS_PAGE(pages); |
1c79356b A |
5570 | page_count = 1; |
5571 | for (m = NEXT_PAGE(pages); m != VM_PAGE_NULL; m = NEXT_PAGE(m)) { | |
39037602 | 5572 | if (VM_PAGE_GET_PHYS_PAGE(m) != prev_addr + 1) { |
b0d623f7 | 5573 | printf("m %p prev_addr 0x%lx, current addr 0x%x\n", |
0a7de745 | 5574 | m, (long)prev_addr, VM_PAGE_GET_PHYS_PAGE(m)); |
6d2010ae | 5575 | printf("pages %p page_count %d npages %d\n", pages, page_count, npages); |
1c79356b A |
5576 | panic("vm_page_verify_contiguous: not contiguous!"); |
5577 | } | |
39037602 | 5578 | prev_addr = VM_PAGE_GET_PHYS_PAGE(m); |
1c79356b A |
5579 | ++page_count; |
5580 | } | |
5581 | if (page_count != npages) { | |
2d21ac55 | 5582 | printf("pages %p actual count 0x%x but requested 0x%x\n", |
0a7de745 | 5583 | pages, page_count, npages); |
1c79356b A |
5584 | panic("vm_page_verify_contiguous: count error"); |
5585 | } | |
5586 | return 1; | |
5587 | } | |
1c79356b A |
5588 | |
5589 | ||
2d21ac55 A |
5590 | /* |
5591 | * Check the free lists for proper length etc. | |
5592 | */ | |
fe8ab488 | 5593 | static boolean_t vm_page_verify_this_free_list_enabled = FALSE; |
b0d623f7 A |
5594 | static unsigned int |
5595 | vm_page_verify_free_list( | |
0a7de745 A |
5596 | vm_page_queue_head_t *vm_page_queue, |
5597 | unsigned int color, | |
5598 | vm_page_t look_for_page, | |
5599 | boolean_t expect_page) | |
b0d623f7 | 5600 | { |
0a7de745 A |
5601 | unsigned int npages; |
5602 | vm_page_t m; | |
5603 | vm_page_t prev_m; | |
5604 | boolean_t found_page; | |
b0d623f7 | 5605 | |
0a7de745 | 5606 | if (!vm_page_verify_this_free_list_enabled) { |
fe8ab488 | 5607 | return 0; |
0a7de745 | 5608 | } |
fe8ab488 | 5609 | |
b0d623f7 A |
5610 | found_page = FALSE; |
5611 | npages = 0; | |
39037602 A |
5612 | prev_m = (vm_page_t)((uintptr_t)vm_page_queue); |
5613 | ||
0a7de745 | 5614 | vm_page_queue_iterate(vm_page_queue, m, vmp_pageq) { |
b0d623f7 A |
5615 | if (m == look_for_page) { |
5616 | found_page = TRUE; | |
5617 | } | |
0a7de745 | 5618 | if ((vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.prev) != prev_m) { |
b0d623f7 | 5619 | panic("vm_page_verify_free_list(color=%u, npages=%u): page %p corrupted prev ptr %p instead of %p\n", |
0a7de745 A |
5620 | color, npages, m, (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.prev), prev_m); |
5621 | } | |
5622 | if (!m->vmp_busy) { | |
b0d623f7 | 5623 | panic("vm_page_verify_free_list(color=%u, npages=%u): page %p not busy\n", |
0a7de745 A |
5624 | color, npages, m); |
5625 | } | |
6d2010ae | 5626 | if (color != (unsigned int) -1) { |
0a7de745 | 5627 | if (VM_PAGE_GET_COLOR(m) != color) { |
6d2010ae | 5628 | panic("vm_page_verify_free_list(color=%u, npages=%u): page %p wrong color %u instead of %u\n", |
0a7de745 A |
5629 | color, npages, m, VM_PAGE_GET_COLOR(m), color); |
5630 | } | |
5631 | if (m->vmp_q_state != VM_PAGE_ON_FREE_Q) { | |
39037602 | 5632 | panic("vm_page_verify_free_list(color=%u, npages=%u): page %p - expecting q_state == VM_PAGE_ON_FREE_Q, found %d\n", |
0a7de745 A |
5633 | color, npages, m, m->vmp_q_state); |
5634 | } | |
39037602 | 5635 | } else { |
0a7de745 | 5636 | if (m->vmp_q_state != VM_PAGE_ON_FREE_LOCAL_Q) { |
39037602 | 5637 | panic("vm_page_verify_free_list(npages=%u): local page %p - expecting q_state == VM_PAGE_ON_FREE_LOCAL_Q, found %d\n", |
0a7de745 A |
5638 | npages, m, m->vmp_q_state); |
5639 | } | |
6d2010ae | 5640 | } |
b0d623f7 A |
5641 | ++npages; |
5642 | prev_m = m; | |
5643 | } | |
5644 | if (look_for_page != VM_PAGE_NULL) { | |
5645 | unsigned int other_color; | |
5646 | ||
5647 | if (expect_page && !found_page) { | |
5648 | printf("vm_page_verify_free_list(color=%u, npages=%u): page %p not found phys=%u\n", | |
0a7de745 | 5649 | color, npages, look_for_page, VM_PAGE_GET_PHYS_PAGE(look_for_page)); |
b0d623f7 A |
5650 | _vm_page_print(look_for_page); |
5651 | for (other_color = 0; | |
0a7de745 A |
5652 | other_color < vm_colors; |
5653 | other_color++) { | |
5654 | if (other_color == color) { | |
b0d623f7 | 5655 | continue; |
0a7de745 | 5656 | } |
39037602 | 5657 | vm_page_verify_free_list(&vm_page_queue_free[other_color].qhead, |
0a7de745 | 5658 | other_color, look_for_page, FALSE); |
b0d623f7 | 5659 | } |
6d2010ae | 5660 | if (color == (unsigned int) -1) { |
d1ecb069 | 5661 | vm_page_verify_free_list(&vm_lopage_queue_free, |
0a7de745 | 5662 | (unsigned int) -1, look_for_page, FALSE); |
d1ecb069 | 5663 | } |
b0d623f7 A |
5664 | panic("vm_page_verify_free_list(color=%u)\n", color); |
5665 | } | |
5666 | if (!expect_page && found_page) { | |
5667 | printf("vm_page_verify_free_list(color=%u, npages=%u): page %p found phys=%u\n", | |
0a7de745 | 5668 | color, npages, look_for_page, VM_PAGE_GET_PHYS_PAGE(look_for_page)); |
b0d623f7 A |
5669 | } |
5670 | } | |
5671 | return npages; | |
5672 | } | |
5673 | ||
fe8ab488 | 5674 | static boolean_t vm_page_verify_all_free_lists_enabled = FALSE; |
2d21ac55 A |
5675 | static void |
5676 | vm_page_verify_free_lists( void ) | |
5677 | { | |
0a7de745 A |
5678 | unsigned int color, npages, nlopages; |
5679 | boolean_t toggle = TRUE; | |
b0d623f7 | 5680 | |
0a7de745 | 5681 | if (!vm_page_verify_all_free_lists_enabled) { |
b0d623f7 | 5682 | return; |
0a7de745 | 5683 | } |
b0d623f7 | 5684 | |
2d21ac55 | 5685 | npages = 0; |
b0d623f7 A |
5686 | |
5687 | lck_mtx_lock(&vm_page_queue_free_lock); | |
0a7de745 | 5688 | |
fe8ab488 A |
5689 | if (vm_page_verify_this_free_list_enabled == TRUE) { |
5690 | /* | |
5691 | * This variable has been set globally for extra checking of | |
5692 | * each free list Q. Since we didn't set it, we don't own it | |
5693 | * and we shouldn't toggle it. | |
5694 | */ | |
5695 | toggle = FALSE; | |
5696 | } | |
5697 | ||
5698 | if (toggle == TRUE) { | |
5699 | vm_page_verify_this_free_list_enabled = TRUE; | |
5700 | } | |
2d21ac55 | 5701 | |
0a7de745 | 5702 | for (color = 0; color < vm_colors; color++) { |
39037602 | 5703 | npages += vm_page_verify_free_list(&vm_page_queue_free[color].qhead, |
0a7de745 | 5704 | color, VM_PAGE_NULL, FALSE); |
2d21ac55 | 5705 | } |
d1ecb069 | 5706 | nlopages = vm_page_verify_free_list(&vm_lopage_queue_free, |
0a7de745 A |
5707 | (unsigned int) -1, |
5708 | VM_PAGE_NULL, FALSE); | |
5709 | if (npages != vm_page_free_count || nlopages != vm_lopage_free_count) { | |
d1ecb069 | 5710 | panic("vm_page_verify_free_lists: " |
0a7de745 A |
5711 | "npages %u free_count %d nlopages %u lo_free_count %u", |
5712 | npages, vm_page_free_count, nlopages, vm_lopage_free_count); | |
5713 | } | |
6d2010ae | 5714 | |
fe8ab488 A |
5715 | if (toggle == TRUE) { |
5716 | vm_page_verify_this_free_list_enabled = FALSE; | |
5717 | } | |
5718 | ||
b0d623f7 | 5719 | lck_mtx_unlock(&vm_page_queue_free_lock); |
2d21ac55 | 5720 | } |
2d21ac55 | 5721 | |
0a7de745 | 5722 | #endif /* MACH_ASSERT */ |
2d21ac55 | 5723 | |
91447636 | 5724 | |
0a7de745 | 5725 | extern boolean_t(*volatile consider_buffer_cache_collect)(int); |
3e170ce0 | 5726 | |
1c79356b | 5727 | /* |
2d21ac55 | 5728 | * CONTIGUOUS PAGE ALLOCATION |
2d21ac55 A |
5729 | * |
5730 | * Find a region large enough to contain at least n pages | |
1c79356b A |
5731 | * of contiguous physical memory. |
5732 | * | |
2d21ac55 A |
5733 | * This is done by traversing the vm_page_t array in a linear fashion |
5734 | * we assume that the vm_page_t array has the avaiable physical pages in an | |
5735 | * ordered, ascending list... this is currently true of all our implementations | |
0a7de745 | 5736 | * and must remain so... there can be 'holes' in the array... we also can |
2d21ac55 | 5737 | * no longer tolerate the vm_page_t's in the list being 'freed' and reclaimed |
0a7de745 A |
5738 | * which use to happen via 'vm_page_convert'... that function was no longer |
5739 | * being called and was removed... | |
5740 | * | |
5741 | * The basic flow consists of stabilizing some of the interesting state of | |
2d21ac55 A |
5742 | * a vm_page_t behind the vm_page_queue and vm_page_free locks... we start our |
5743 | * sweep at the beginning of the array looking for pages that meet our criterea | |
5744 | * for a 'stealable' page... currently we are pretty conservative... if the page | |
5745 | * meets this criterea and is physically contiguous to the previous page in the 'run' | |
0a7de745 | 5746 | * we keep developing it. If we hit a page that doesn't fit, we reset our state |
2d21ac55 | 5747 | * and start to develop a new run... if at this point we've already considered |
0a7de745 A |
5748 | * at least MAX_CONSIDERED_BEFORE_YIELD pages, we'll drop the 2 locks we hold, |
5749 | * and mutex_pause (which will yield the processor), to keep the latency low w/r | |
2d21ac55 A |
5750 | * to other threads trying to acquire free pages (or move pages from q to q), |
5751 | * and then continue from the spot we left off... we only make 1 pass through the | |
5752 | * array. Once we have a 'run' that is long enough, we'll go into the loop which | |
0a7de745 | 5753 | * which steals the pages from the queues they're currently on... pages on the free |
2d21ac55 A |
5754 | * queue can be stolen directly... pages that are on any of the other queues |
5755 | * must be removed from the object they are tabled on... this requires taking the | |
0a7de745 | 5756 | * object lock... we do this as a 'try' to prevent deadlocks... if the 'try' fails |
2d21ac55 A |
5757 | * or if the state of the page behind the vm_object lock is no longer viable, we'll |
5758 | * dump the pages we've currently stolen back to the free list, and pick up our | |
5759 | * scan from the point where we aborted the 'current' run. | |
5760 | * | |
5761 | * | |
1c79356b | 5762 | * Requirements: |
2d21ac55 | 5763 | * - neither vm_page_queue nor vm_free_list lock can be held on entry |
1c79356b | 5764 | * |
2d21ac55 | 5765 | * Returns a pointer to a list of gobbled/wired pages or VM_PAGE_NULL. |
1c79356b | 5766 | * |
e5568f75 | 5767 | * Algorithm: |
1c79356b | 5768 | */ |
2d21ac55 | 5769 | |
0a7de745 | 5770 | #define MAX_CONSIDERED_BEFORE_YIELD 1000 |
2d21ac55 A |
5771 | |
5772 | ||
0a7de745 A |
5773 | #define RESET_STATE_OF_RUN() \ |
5774 | MACRO_BEGIN \ | |
5775 | prevcontaddr = -2; \ | |
5776 | start_pnum = -1; \ | |
5777 | free_considered = 0; \ | |
5778 | substitute_needed = 0; \ | |
5779 | npages = 0; \ | |
5780 | MACRO_END | |
2d21ac55 | 5781 | |
b0d623f7 A |
5782 | /* |
5783 | * Can we steal in-use (i.e. not free) pages when searching for | |
5784 | * physically-contiguous pages ? | |
5785 | */ | |
5786 | #define VM_PAGE_FIND_CONTIGUOUS_CAN_STEAL 1 | |
5787 | ||
0a7de745 | 5788 | static unsigned int vm_page_find_contiguous_last_idx = 0, vm_page_lomem_find_contiguous_last_idx = 0; |
b0d623f7 A |
5789 | #if DEBUG |
5790 | int vm_page_find_contig_debug = 0; | |
5791 | #endif | |
2d21ac55 | 5792 | |
1c79356b A |
5793 | static vm_page_t |
5794 | vm_page_find_contiguous( | |
0a7de745 A |
5795 | unsigned int contig_pages, |
5796 | ppnum_t max_pnum, | |
b0d623f7 | 5797 | ppnum_t pnum_mask, |
0a7de745 A |
5798 | boolean_t wire, |
5799 | int flags) | |
5800 | { | |
5801 | vm_page_t m = NULL; | |
5802 | ppnum_t prevcontaddr = 0; | |
5803 | ppnum_t start_pnum = 0; | |
5804 | unsigned int npages = 0, considered = 0, scanned = 0; | |
5805 | unsigned int page_idx = 0, start_idx = 0, last_idx = 0, orig_last_idx = 0; | |
5806 | unsigned int idx_last_contig_page_found = 0; | |
5807 | int free_considered = 0, free_available = 0; | |
5808 | int substitute_needed = 0; | |
5809 | boolean_t wrapped, zone_gc_called = FALSE; | |
5810 | kern_return_t kr; | |
593a1d5f | 5811 | #if DEBUG |
0a7de745 A |
5812 | clock_sec_t tv_start_sec = 0, tv_end_sec = 0; |
5813 | clock_usec_t tv_start_usec = 0, tv_end_usec = 0; | |
593a1d5f | 5814 | #endif |
3e170ce0 | 5815 | |
0a7de745 A |
5816 | int yielded = 0; |
5817 | int dumped_run = 0; | |
5818 | int stolen_pages = 0; | |
5819 | int compressed_pages = 0; | |
3e170ce0 | 5820 | |
1c79356b | 5821 | |
0a7de745 | 5822 | if (contig_pages == 0) { |
1c79356b | 5823 | return VM_PAGE_NULL; |
0a7de745 | 5824 | } |
1c79356b | 5825 | |
3e170ce0 A |
5826 | full_scan_again: |
5827 | ||
2d21ac55 A |
5828 | #if MACH_ASSERT |
5829 | vm_page_verify_free_lists(); | |
593a1d5f A |
5830 | #endif |
5831 | #if DEBUG | |
2d21ac55 A |
5832 | clock_get_system_microtime(&tv_start_sec, &tv_start_usec); |
5833 | #endif | |
39236c6e A |
5834 | PAGE_REPLACEMENT_ALLOWED(TRUE); |
5835 | ||
0a7de745 A |
5836 | /* |
5837 | * If there are still delayed pages, try to free up some that match. | |
5838 | */ | |
5839 | if (__improbable(vm_delayed_count != 0 && contig_pages != 0)) { | |
5840 | vm_free_delayed_pages_contig(contig_pages, max_pnum, pnum_mask); | |
5ba3f43e | 5841 | } |
3e170ce0 | 5842 | |
0a7de745 | 5843 | vm_page_lock_queues(); |
b0d623f7 | 5844 | lck_mtx_lock(&vm_page_queue_free_lock); |
2d21ac55 A |
5845 | |
5846 | RESET_STATE_OF_RUN(); | |
1c79356b | 5847 | |
b0d623f7 | 5848 | scanned = 0; |
2d21ac55 A |
5849 | considered = 0; |
5850 | free_available = vm_page_free_count - vm_page_free_reserved; | |
e5568f75 | 5851 | |
b0d623f7 | 5852 | wrapped = FALSE; |
0a7de745 A |
5853 | |
5854 | if (flags & KMA_LOMEM) { | |
b0d623f7 | 5855 | idx_last_contig_page_found = vm_page_lomem_find_contiguous_last_idx; |
0a7de745 | 5856 | } else { |
b0d623f7 | 5857 | idx_last_contig_page_found = vm_page_find_contiguous_last_idx; |
0a7de745 | 5858 | } |
b0d623f7 A |
5859 | |
5860 | orig_last_idx = idx_last_contig_page_found; | |
5861 | last_idx = orig_last_idx; | |
5862 | ||
5863 | for (page_idx = last_idx, start_idx = last_idx; | |
0a7de745 A |
5864 | npages < contig_pages && page_idx < vm_pages_count; |
5865 | page_idx++) { | |
b0d623f7 A |
5866 | retry: |
5867 | if (wrapped && | |
5868 | npages == 0 && | |
5869 | page_idx >= orig_last_idx) { | |
5870 | /* | |
5871 | * We're back where we started and we haven't | |
5872 | * found any suitable contiguous range. Let's | |
5873 | * give up. | |
5874 | */ | |
5875 | break; | |
5876 | } | |
5877 | scanned++; | |
2d21ac55 | 5878 | m = &vm_pages[page_idx]; |
e5568f75 | 5879 | |
d9a64523 A |
5880 | assert(!m->vmp_fictitious); |
5881 | assert(!m->vmp_private); | |
b0d623f7 | 5882 | |
39037602 | 5883 | if (max_pnum && VM_PAGE_GET_PHYS_PAGE(m) > max_pnum) { |
2d21ac55 A |
5884 | /* no more low pages... */ |
5885 | break; | |
e5568f75 | 5886 | } |
39037602 | 5887 | if (!npages & ((VM_PAGE_GET_PHYS_PAGE(m) & pnum_mask) != 0)) { |
b0d623f7 A |
5888 | /* |
5889 | * not aligned | |
5890 | */ | |
5891 | RESET_STATE_OF_RUN(); | |
d9a64523 | 5892 | } else if (VM_PAGE_WIRED(m) || m->vmp_gobbled || |
0a7de745 A |
5893 | m->vmp_laundry || m->vmp_wanted || |
5894 | m->vmp_cleaning || m->vmp_overwriting || m->vmp_free_when_done) { | |
2d21ac55 A |
5895 | /* |
5896 | * page is in a transient state | |
5897 | * or a state we don't want to deal | |
5898 | * with, so don't consider it which | |
5899 | * means starting a new run | |
5900 | */ | |
5901 | RESET_STATE_OF_RUN(); | |
d9a64523 | 5902 | } else if ((m->vmp_q_state == VM_PAGE_NOT_ON_Q) || |
0a7de745 A |
5903 | (m->vmp_q_state == VM_PAGE_ON_FREE_LOCAL_Q) || |
5904 | (m->vmp_q_state == VM_PAGE_ON_FREE_LOPAGE_Q) || | |
5905 | (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) { | |
2d21ac55 | 5906 | /* |
39037602 A |
5907 | * page needs to be on one of our queues (other then the pageout or special free queues) |
5908 | * or it needs to belong to the compressor pool (which is now indicated | |
d9a64523 | 5909 | * by vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR and falls out |
39037602 | 5910 | * from the check for VM_PAGE_NOT_ON_Q) |
2d21ac55 A |
5911 | * in order for it to be stable behind the |
5912 | * locks we hold at this point... | |
5913 | * if not, don't consider it which | |
5914 | * means starting a new run | |
5915 | */ | |
5916 | RESET_STATE_OF_RUN(); | |
d9a64523 | 5917 | } else if ((m->vmp_q_state != VM_PAGE_ON_FREE_Q) && (!m->vmp_tabled || m->vmp_busy)) { |
2d21ac55 A |
5918 | /* |
5919 | * pages on the free list are always 'busy' | |
5920 | * so we couldn't test for 'busy' in the check | |
5921 | * for the transient states... pages that are | |
5922 | * 'free' are never 'tabled', so we also couldn't | |
5923 | * test for 'tabled'. So we check here to make | |
5924 | * sure that a non-free page is not busy and is | |
0a7de745 | 5925 | * tabled on an object... |
2d21ac55 A |
5926 | * if not, don't consider it which |
5927 | * means starting a new run | |
5928 | */ | |
5929 | RESET_STATE_OF_RUN(); | |
2d21ac55 | 5930 | } else { |
39037602 A |
5931 | if (VM_PAGE_GET_PHYS_PAGE(m) != prevcontaddr + 1) { |
5932 | if ((VM_PAGE_GET_PHYS_PAGE(m) & pnum_mask) != 0) { | |
b0d623f7 A |
5933 | RESET_STATE_OF_RUN(); |
5934 | goto did_consider; | |
5935 | } else { | |
5936 | npages = 1; | |
5937 | start_idx = page_idx; | |
39037602 | 5938 | start_pnum = VM_PAGE_GET_PHYS_PAGE(m); |
b0d623f7 | 5939 | } |
2d21ac55 A |
5940 | } else { |
5941 | npages++; | |
e5568f75 | 5942 | } |
39037602 | 5943 | prevcontaddr = VM_PAGE_GET_PHYS_PAGE(m); |
0a7de745 | 5944 | |
b0d623f7 | 5945 | VM_PAGE_CHECK(m); |
d9a64523 | 5946 | if (m->vmp_q_state == VM_PAGE_ON_FREE_Q) { |
2d21ac55 | 5947 | free_considered++; |
b0d623f7 A |
5948 | } else { |
5949 | /* | |
5950 | * This page is not free. | |
5951 | * If we can't steal used pages, | |
5952 | * we have to give up this run | |
5953 | * and keep looking. | |
5954 | * Otherwise, we might need to | |
5955 | * move the contents of this page | |
5956 | * into a substitute page. | |
5957 | */ | |
5958 | #if VM_PAGE_FIND_CONTIGUOUS_CAN_STEAL | |
d9a64523 | 5959 | if (m->vmp_pmapped || m->vmp_dirty || m->vmp_precious) { |
b0d623f7 A |
5960 | substitute_needed++; |
5961 | } | |
5962 | #else | |
5963 | RESET_STATE_OF_RUN(); | |
5964 | #endif | |
2d21ac55 | 5965 | } |
b0d623f7 | 5966 | |
0a7de745 | 5967 | if ((free_considered + substitute_needed) > free_available) { |
2d21ac55 A |
5968 | /* |
5969 | * if we let this run continue | |
5970 | * we will end up dropping the vm_page_free_count | |
5971 | * below the reserve limit... we need to abort | |
5972 | * this run, but we can at least re-consider this | |
5973 | * page... thus the jump back to 'retry' | |
5974 | */ | |
5975 | RESET_STATE_OF_RUN(); | |
5976 | ||
5977 | if (free_available && considered <= MAX_CONSIDERED_BEFORE_YIELD) { | |
5978 | considered++; | |
5979 | goto retry; | |
e5568f75 | 5980 | } |
2d21ac55 A |
5981 | /* |
5982 | * free_available == 0 | |
5983 | * so can't consider any free pages... if | |
5984 | * we went to retry in this case, we'd | |
5985 | * get stuck looking at the same page | |
5986 | * w/o making any forward progress | |
5987 | * we also want to take this path if we've already | |
5988 | * reached our limit that controls the lock latency | |
5989 | */ | |
e5568f75 | 5990 | } |
2d21ac55 | 5991 | } |
b0d623f7 | 5992 | did_consider: |
2d21ac55 | 5993 | if (considered > MAX_CONSIDERED_BEFORE_YIELD && npages <= 1) { |
39236c6e A |
5994 | PAGE_REPLACEMENT_ALLOWED(FALSE); |
5995 | ||
b0d623f7 | 5996 | lck_mtx_unlock(&vm_page_queue_free_lock); |
2d21ac55 | 5997 | vm_page_unlock_queues(); |
e5568f75 | 5998 | |
2d21ac55 A |
5999 | mutex_pause(0); |
6000 | ||
39236c6e A |
6001 | PAGE_REPLACEMENT_ALLOWED(TRUE); |
6002 | ||
2d21ac55 | 6003 | vm_page_lock_queues(); |
b0d623f7 | 6004 | lck_mtx_lock(&vm_page_queue_free_lock); |
2d21ac55 A |
6005 | |
6006 | RESET_STATE_OF_RUN(); | |
1c79356b | 6007 | /* |
2d21ac55 A |
6008 | * reset our free page limit since we |
6009 | * dropped the lock protecting the vm_page_free_queue | |
1c79356b | 6010 | */ |
2d21ac55 A |
6011 | free_available = vm_page_free_count - vm_page_free_reserved; |
6012 | considered = 0; | |
3e170ce0 | 6013 | |
2d21ac55 | 6014 | yielded++; |
3e170ce0 | 6015 | |
2d21ac55 A |
6016 | goto retry; |
6017 | } | |
6018 | considered++; | |
6019 | } | |
6020 | m = VM_PAGE_NULL; | |
6021 | ||
b0d623f7 A |
6022 | if (npages != contig_pages) { |
6023 | if (!wrapped) { | |
6024 | /* | |
6025 | * We didn't find a contiguous range but we didn't | |
6026 | * start from the very first page. | |
6027 | * Start again from the very first page. | |
6028 | */ | |
6029 | RESET_STATE_OF_RUN(); | |
0a7de745 | 6030 | if (flags & KMA_LOMEM) { |
b0d623f7 | 6031 | idx_last_contig_page_found = vm_page_lomem_find_contiguous_last_idx = 0; |
0a7de745 | 6032 | } else { |
b0d623f7 | 6033 | idx_last_contig_page_found = vm_page_find_contiguous_last_idx = 0; |
0a7de745 | 6034 | } |
b0d623f7 A |
6035 | last_idx = 0; |
6036 | page_idx = last_idx; | |
6037 | wrapped = TRUE; | |
6038 | goto retry; | |
6039 | } | |
6040 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
6041 | } else { | |
0a7de745 A |
6042 | vm_page_t m1; |
6043 | vm_page_t m2; | |
6044 | unsigned int cur_idx; | |
6045 | unsigned int tmp_start_idx; | |
6046 | vm_object_t locked_object = VM_OBJECT_NULL; | |
6047 | boolean_t abort_run = FALSE; | |
6048 | ||
b0d623f7 A |
6049 | assert(page_idx - start_idx == contig_pages); |
6050 | ||
2d21ac55 A |
6051 | tmp_start_idx = start_idx; |
6052 | ||
6053 | /* | |
6054 | * first pass through to pull the free pages | |
6055 | * off of the free queue so that in case we | |
0a7de745 | 6056 | * need substitute pages, we won't grab any |
2d21ac55 A |
6057 | * of the free pages in the run... we'll clear |
6058 | * the 'free' bit in the 2nd pass, and even in | |
6059 | * an abort_run case, we'll collect all of the | |
6060 | * free pages in this run and return them to the free list | |
6061 | */ | |
6062 | while (start_idx < page_idx) { | |
2d21ac55 A |
6063 | m1 = &vm_pages[start_idx++]; |
6064 | ||
b0d623f7 | 6065 | #if !VM_PAGE_FIND_CONTIGUOUS_CAN_STEAL |
d9a64523 | 6066 | assert(m1->vmp_q_state == VM_PAGE_ON_FREE_Q); |
b0d623f7 A |
6067 | #endif |
6068 | ||
d9a64523 | 6069 | if (m1->vmp_q_state == VM_PAGE_ON_FREE_Q) { |
0b4c1975 | 6070 | unsigned int color; |
2d21ac55 | 6071 | |
5ba3f43e | 6072 | color = VM_PAGE_GET_COLOR(m1); |
b0d623f7 | 6073 | #if MACH_ASSERT |
39037602 | 6074 | vm_page_verify_free_list(&vm_page_queue_free[color].qhead, color, m1, TRUE); |
b0d623f7 | 6075 | #endif |
0a7de745 | 6076 | vm_page_queue_remove(&vm_page_queue_free[color].qhead, m1, vmp_pageq); |
39037602 A |
6077 | |
6078 | VM_PAGE_ZERO_PAGEQ_ENTRY(m1); | |
0b4c1975 | 6079 | #if MACH_ASSERT |
39037602 | 6080 | vm_page_verify_free_list(&vm_page_queue_free[color].qhead, color, VM_PAGE_NULL, FALSE); |
0b4c1975 | 6081 | #endif |
b0d623f7 A |
6082 | /* |
6083 | * Clear the "free" bit so that this page | |
6084 | * does not get considered for another | |
6085 | * concurrent physically-contiguous allocation. | |
6086 | */ | |
d9a64523 A |
6087 | m1->vmp_q_state = VM_PAGE_NOT_ON_Q; |
6088 | assert(m1->vmp_busy); | |
0b4c1975 A |
6089 | |
6090 | vm_page_free_count--; | |
2d21ac55 A |
6091 | } |
6092 | } | |
0a7de745 | 6093 | if (flags & KMA_LOMEM) { |
b0d623f7 | 6094 | vm_page_lomem_find_contiguous_last_idx = page_idx; |
0a7de745 | 6095 | } else { |
b0d623f7 | 6096 | vm_page_find_contiguous_last_idx = page_idx; |
0a7de745 A |
6097 | } |
6098 | ||
2d21ac55 A |
6099 | /* |
6100 | * we can drop the free queue lock at this point since | |
6101 | * we've pulled any 'free' candidates off of the list | |
6102 | * we need it dropped so that we can do a vm_page_grab | |
6103 | * when substituing for pmapped/dirty pages | |
6104 | */ | |
b0d623f7 | 6105 | lck_mtx_unlock(&vm_page_queue_free_lock); |
2d21ac55 A |
6106 | |
6107 | start_idx = tmp_start_idx; | |
6108 | cur_idx = page_idx - 1; | |
6109 | ||
6110 | while (start_idx++ < page_idx) { | |
6111 | /* | |
6112 | * must go through the list from back to front | |
6113 | * so that the page list is created in the | |
6114 | * correct order - low -> high phys addresses | |
6115 | */ | |
6116 | m1 = &vm_pages[cur_idx--]; | |
6117 | ||
d9a64523 | 6118 | if (m1->vmp_object == 0) { |
2d21ac55 | 6119 | /* |
b0d623f7 | 6120 | * page has already been removed from |
2d21ac55 A |
6121 | * the free list in the 1st pass |
6122 | */ | |
d9a64523 A |
6123 | assert(m1->vmp_q_state == VM_PAGE_NOT_ON_Q); |
6124 | assert(m1->vmp_offset == (vm_object_offset_t) -1); | |
6125 | assert(m1->vmp_busy); | |
6126 | assert(!m1->vmp_wanted); | |
6127 | assert(!m1->vmp_laundry); | |
e5568f75 | 6128 | } else { |
2d21ac55 | 6129 | vm_object_t object; |
39236c6e A |
6130 | int refmod; |
6131 | boolean_t disconnected, reusable; | |
2d21ac55 | 6132 | |
0a7de745 | 6133 | if (abort_run == TRUE) { |
2d21ac55 | 6134 | continue; |
0a7de745 | 6135 | } |
2d21ac55 | 6136 | |
d9a64523 | 6137 | assert(m1->vmp_q_state != VM_PAGE_NOT_ON_Q); |
39037602 A |
6138 | |
6139 | object = VM_PAGE_OBJECT(m1); | |
2d21ac55 A |
6140 | |
6141 | if (object != locked_object) { | |
6142 | if (locked_object) { | |
6143 | vm_object_unlock(locked_object); | |
6144 | locked_object = VM_OBJECT_NULL; | |
6145 | } | |
0a7de745 | 6146 | if (vm_object_lock_try(object)) { |
2d21ac55 | 6147 | locked_object = object; |
0a7de745 | 6148 | } |
2d21ac55 | 6149 | } |
0a7de745 | 6150 | if (locked_object == VM_OBJECT_NULL || |
d9a64523 | 6151 | (VM_PAGE_WIRED(m1) || m1->vmp_gobbled || |
0a7de745 A |
6152 | m1->vmp_laundry || m1->vmp_wanted || |
6153 | m1->vmp_cleaning || m1->vmp_overwriting || m1->vmp_free_when_done || m1->vmp_busy) || | |
d9a64523 | 6154 | (m1->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) { |
2d21ac55 A |
6155 | if (locked_object) { |
6156 | vm_object_unlock(locked_object); | |
6157 | locked_object = VM_OBJECT_NULL; | |
6158 | } | |
6159 | tmp_start_idx = cur_idx; | |
6160 | abort_run = TRUE; | |
6161 | continue; | |
6162 | } | |
39236c6e A |
6163 | |
6164 | disconnected = FALSE; | |
6165 | reusable = FALSE; | |
6166 | ||
d9a64523 | 6167 | if ((m1->vmp_reusable || |
0a7de745 | 6168 | object->all_reusable) && |
d9a64523 A |
6169 | (m1->vmp_q_state == VM_PAGE_ON_INACTIVE_INTERNAL_Q) && |
6170 | !m1->vmp_dirty && | |
6171 | !m1->vmp_reference) { | |
39236c6e | 6172 | /* reusable page... */ |
39037602 | 6173 | refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m1)); |
39236c6e A |
6174 | disconnected = TRUE; |
6175 | if (refmod == 0) { | |
6176 | /* | |
6177 | * ... not reused: can steal | |
6178 | * without relocating contents. | |
6179 | */ | |
6180 | reusable = TRUE; | |
6181 | } | |
6182 | } | |
6183 | ||
d9a64523 | 6184 | if ((m1->vmp_pmapped && |
0a7de745 | 6185 | !reusable) || |
d9a64523 A |
6186 | m1->vmp_dirty || |
6187 | m1->vmp_precious) { | |
2d21ac55 A |
6188 | vm_object_offset_t offset; |
6189 | ||
0a7de745 | 6190 | m2 = vm_page_grab_options(VM_PAGE_GRAB_Q_LOCK_HELD); |
2d21ac55 A |
6191 | |
6192 | if (m2 == VM_PAGE_NULL) { | |
6193 | if (locked_object) { | |
6194 | vm_object_unlock(locked_object); | |
6195 | locked_object = VM_OBJECT_NULL; | |
6196 | } | |
6197 | tmp_start_idx = cur_idx; | |
6198 | abort_run = TRUE; | |
6199 | continue; | |
6200 | } | |
0a7de745 A |
6201 | if (!disconnected) { |
6202 | if (m1->vmp_pmapped) { | |
39037602 | 6203 | refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m1)); |
0a7de745 | 6204 | } else { |
39236c6e | 6205 | refmod = 0; |
0a7de745 | 6206 | } |
39236c6e A |
6207 | } |
6208 | ||
6209 | /* copy the page's contents */ | |
39037602 | 6210 | pmap_copy_page(VM_PAGE_GET_PHYS_PAGE(m1), VM_PAGE_GET_PHYS_PAGE(m2)); |
39236c6e A |
6211 | /* copy the page's state */ |
6212 | assert(!VM_PAGE_WIRED(m1)); | |
d9a64523 A |
6213 | assert(m1->vmp_q_state != VM_PAGE_ON_FREE_Q); |
6214 | assert(m1->vmp_q_state != VM_PAGE_ON_PAGEOUT_Q); | |
6215 | assert(!m1->vmp_laundry); | |
0a7de745 | 6216 | m2->vmp_reference = m1->vmp_reference; |
d9a64523 A |
6217 | assert(!m1->vmp_gobbled); |
6218 | assert(!m1->vmp_private); | |
0a7de745 A |
6219 | m2->vmp_no_cache = m1->vmp_no_cache; |
6220 | m2->vmp_xpmapped = 0; | |
d9a64523 A |
6221 | assert(!m1->vmp_busy); |
6222 | assert(!m1->vmp_wanted); | |
6223 | assert(!m1->vmp_fictitious); | |
0a7de745 A |
6224 | m2->vmp_pmapped = m1->vmp_pmapped; /* should flush cache ? */ |
6225 | m2->vmp_wpmapped = m1->vmp_wpmapped; | |
d9a64523 | 6226 | assert(!m1->vmp_free_when_done); |
0a7de745 A |
6227 | m2->vmp_absent = m1->vmp_absent; |
6228 | m2->vmp_error = m1->vmp_error; | |
6229 | m2->vmp_dirty = m1->vmp_dirty; | |
d9a64523 | 6230 | assert(!m1->vmp_cleaning); |
0a7de745 A |
6231 | m2->vmp_precious = m1->vmp_precious; |
6232 | m2->vmp_clustered = m1->vmp_clustered; | |
d9a64523 | 6233 | assert(!m1->vmp_overwriting); |
0a7de745 A |
6234 | m2->vmp_restart = m1->vmp_restart; |
6235 | m2->vmp_unusual = m1->vmp_unusual; | |
d9a64523 | 6236 | m2->vmp_cs_validated = m1->vmp_cs_validated; |
0a7de745 A |
6237 | m2->vmp_cs_tainted = m1->vmp_cs_tainted; |
6238 | m2->vmp_cs_nx = m1->vmp_cs_nx; | |
39236c6e A |
6239 | |
6240 | /* | |
6241 | * If m1 had really been reusable, | |
6242 | * we would have just stolen it, so | |
6243 | * let's not propagate it's "reusable" | |
6244 | * bit and assert that m2 is not | |
6245 | * marked as "reusable". | |
6246 | */ | |
d9a64523 A |
6247 | // m2->vmp_reusable = m1->vmp_reusable; |
6248 | assert(!m2->vmp_reusable); | |
39236c6e | 6249 | |
d9a64523 | 6250 | // assert(!m1->vmp_lopage); |
39037602 | 6251 | |
0a7de745 | 6252 | if (m1->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) { |
d9a64523 | 6253 | m2->vmp_q_state = VM_PAGE_USED_BY_COMPRESSOR; |
0a7de745 | 6254 | } |
39236c6e | 6255 | |
15129b1c A |
6256 | /* |
6257 | * page may need to be flushed if | |
6258 | * it is marshalled into a UPL | |
6259 | * that is going to be used by a device | |
6260 | * that doesn't support coherency | |
6261 | */ | |
d9a64523 | 6262 | m2->vmp_written_by_kernel = TRUE; |
15129b1c | 6263 | |
39236c6e A |
6264 | /* |
6265 | * make sure we clear the ref/mod state | |
6266 | * from the pmap layer... else we risk | |
6267 | * inheriting state from the last time | |
6268 | * this page was used... | |
6269 | */ | |
39037602 | 6270 | pmap_clear_refmod(VM_PAGE_GET_PHYS_PAGE(m2), VM_MEM_MODIFIED | VM_MEM_REFERENCED); |
2d21ac55 | 6271 | |
0a7de745 | 6272 | if (refmod & VM_MEM_REFERENCED) { |
d9a64523 | 6273 | m2->vmp_reference = TRUE; |
0a7de745 | 6274 | } |
316670eb A |
6275 | if (refmod & VM_MEM_MODIFIED) { |
6276 | SET_PAGE_DIRTY(m2, TRUE); | |
6277 | } | |
d9a64523 | 6278 | offset = m1->vmp_offset; |
2d21ac55 A |
6279 | |
6280 | /* | |
6281 | * completely cleans up the state | |
6282 | * of the page so that it is ready | |
6283 | * to be put onto the free list, or | |
6284 | * for this purpose it looks like it | |
6285 | * just came off of the free list | |
6286 | */ | |
6287 | vm_page_free_prepare(m1); | |
6288 | ||
6289 | /* | |
39236c6e A |
6290 | * now put the substitute page |
6291 | * on the object | |
2d21ac55 | 6292 | */ |
3e170ce0 | 6293 | vm_page_insert_internal(m2, locked_object, offset, VM_KERN_MEMORY_NONE, TRUE, TRUE, FALSE, FALSE, NULL); |
2d21ac55 | 6294 | |
d9a64523 A |
6295 | if (m2->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) { |
6296 | m2->vmp_pmapped = TRUE; | |
6297 | m2->vmp_wpmapped = TRUE; | |
2d21ac55 | 6298 | |
f427ee49 | 6299 | PMAP_ENTER(kernel_pmap, (vm_map_offset_t)m2->vmp_offset, m2, |
0a7de745 | 6300 | VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, 0, TRUE, kr); |
5ba3f43e A |
6301 | |
6302 | assert(kr == KERN_SUCCESS); | |
3e170ce0 | 6303 | |
39236c6e | 6304 | compressed_pages++; |
39236c6e | 6305 | } else { |
0a7de745 | 6306 | if (m2->vmp_reference) { |
39236c6e | 6307 | vm_page_activate(m2); |
0a7de745 | 6308 | } else { |
39236c6e | 6309 | vm_page_deactivate(m2); |
0a7de745 | 6310 | } |
39236c6e | 6311 | } |
2d21ac55 | 6312 | PAGE_WAKEUP_DONE(m2); |
2d21ac55 | 6313 | } else { |
d9a64523 | 6314 | assert(m1->vmp_q_state != VM_PAGE_USED_BY_COMPRESSOR); |
39236c6e | 6315 | |
2d21ac55 A |
6316 | /* |
6317 | * completely cleans up the state | |
6318 | * of the page so that it is ready | |
6319 | * to be put onto the free list, or | |
6320 | * for this purpose it looks like it | |
6321 | * just came off of the free list | |
6322 | */ | |
6323 | vm_page_free_prepare(m1); | |
6324 | } | |
3e170ce0 | 6325 | |
2d21ac55 | 6326 | stolen_pages++; |
1c79356b | 6327 | } |
39037602 A |
6328 | #if CONFIG_BACKGROUND_QUEUE |
6329 | vm_page_assign_background_state(m1); | |
6330 | #endif | |
6331 | VM_PAGE_ZERO_PAGEQ_ENTRY(m1); | |
d9a64523 | 6332 | m1->vmp_snext = m; |
2d21ac55 | 6333 | m = m1; |
e5568f75 | 6334 | } |
2d21ac55 A |
6335 | if (locked_object) { |
6336 | vm_object_unlock(locked_object); | |
6337 | locked_object = VM_OBJECT_NULL; | |
1c79356b A |
6338 | } |
6339 | ||
2d21ac55 | 6340 | if (abort_run == TRUE) { |
2d21ac55 A |
6341 | /* |
6342 | * want the index of the last | |
6343 | * page in this run that was | |
6344 | * successfully 'stolen', so back | |
6345 | * it up 1 for the auto-decrement on use | |
6346 | * and 1 more to bump back over this page | |
6347 | */ | |
6348 | page_idx = tmp_start_idx + 2; | |
b0d623f7 | 6349 | if (page_idx >= vm_pages_count) { |
d190cdc3 A |
6350 | if (wrapped) { |
6351 | if (m != VM_PAGE_NULL) { | |
6352 | vm_page_unlock_queues(); | |
6353 | vm_page_free_list(m, FALSE); | |
6354 | vm_page_lock_queues(); | |
6355 | m = VM_PAGE_NULL; | |
6356 | } | |
6357 | dumped_run++; | |
b0d623f7 | 6358 | goto done_scanning; |
d190cdc3 | 6359 | } |
b0d623f7 A |
6360 | page_idx = last_idx = 0; |
6361 | wrapped = TRUE; | |
6362 | } | |
6363 | abort_run = FALSE; | |
0a7de745 | 6364 | |
2d21ac55 | 6365 | /* |
b0d623f7 A |
6366 | * We didn't find a contiguous range but we didn't |
6367 | * start from the very first page. | |
6368 | * Start again from the very first page. | |
2d21ac55 | 6369 | */ |
b0d623f7 | 6370 | RESET_STATE_OF_RUN(); |
0a7de745 A |
6371 | |
6372 | if (flags & KMA_LOMEM) { | |
b0d623f7 | 6373 | idx_last_contig_page_found = vm_page_lomem_find_contiguous_last_idx = page_idx; |
0a7de745 | 6374 | } else { |
b0d623f7 | 6375 | idx_last_contig_page_found = vm_page_find_contiguous_last_idx = page_idx; |
0a7de745 A |
6376 | } |
6377 | ||
b0d623f7 | 6378 | last_idx = page_idx; |
0a7de745 | 6379 | |
d190cdc3 A |
6380 | if (m != VM_PAGE_NULL) { |
6381 | vm_page_unlock_queues(); | |
6382 | vm_page_free_list(m, FALSE); | |
6383 | vm_page_lock_queues(); | |
6384 | m = VM_PAGE_NULL; | |
6385 | } | |
6386 | dumped_run++; | |
6387 | ||
b0d623f7 A |
6388 | lck_mtx_lock(&vm_page_queue_free_lock); |
6389 | /* | |
0a7de745 A |
6390 | * reset our free page limit since we |
6391 | * dropped the lock protecting the vm_page_free_queue | |
6392 | */ | |
b0d623f7 | 6393 | free_available = vm_page_free_count - vm_page_free_reserved; |
2d21ac55 A |
6394 | goto retry; |
6395 | } | |
e5568f75 | 6396 | |
e5568f75 | 6397 | for (m1 = m; m1 != VM_PAGE_NULL; m1 = NEXT_PAGE(m1)) { |
d9a64523 A |
6398 | assert(m1->vmp_q_state == VM_PAGE_NOT_ON_Q); |
6399 | assert(m1->vmp_wire_count == 0); | |
39037602 A |
6400 | |
6401 | if (wire == TRUE) { | |
d9a64523 A |
6402 | m1->vmp_wire_count++; |
6403 | m1->vmp_q_state = VM_PAGE_IS_WIRED; | |
0a7de745 | 6404 | } else { |
d9a64523 | 6405 | m1->vmp_gobbled = TRUE; |
0a7de745 | 6406 | } |
e5568f75 | 6407 | } |
0a7de745 | 6408 | if (wire == FALSE) { |
2d21ac55 | 6409 | vm_page_gobble_count += npages; |
0a7de745 | 6410 | } |
2d21ac55 A |
6411 | |
6412 | /* | |
6413 | * gobbled pages are also counted as wired pages | |
6414 | */ | |
e5568f75 | 6415 | vm_page_wire_count += npages; |
e5568f75 | 6416 | |
0a7de745 | 6417 | assert(vm_page_verify_contiguous(m, npages)); |
2d21ac55 A |
6418 | } |
6419 | done_scanning: | |
39236c6e A |
6420 | PAGE_REPLACEMENT_ALLOWED(FALSE); |
6421 | ||
2d21ac55 A |
6422 | vm_page_unlock_queues(); |
6423 | ||
593a1d5f | 6424 | #if DEBUG |
2d21ac55 A |
6425 | clock_get_system_microtime(&tv_end_sec, &tv_end_usec); |
6426 | ||
6427 | tv_end_sec -= tv_start_sec; | |
6428 | if (tv_end_usec < tv_start_usec) { | |
6429 | tv_end_sec--; | |
6430 | tv_end_usec += 1000000; | |
1c79356b | 6431 | } |
2d21ac55 A |
6432 | tv_end_usec -= tv_start_usec; |
6433 | if (tv_end_usec >= 1000000) { | |
6434 | tv_end_sec++; | |
6435 | tv_end_sec -= 1000000; | |
6436 | } | |
b0d623f7 | 6437 | if (vm_page_find_contig_debug) { |
39236c6e | 6438 | printf("%s(num=%d,low=%d): found %d pages at 0x%llx in %ld.%06ds... started at %d... scanned %d pages... yielded %d times... dumped run %d times... stole %d pages... stole %d compressed pages\n", |
0a7de745 A |
6439 | __func__, contig_pages, max_pnum, npages, (vm_object_offset_t)start_pnum << PAGE_SHIFT, |
6440 | (long)tv_end_sec, tv_end_usec, orig_last_idx, | |
6441 | scanned, yielded, dumped_run, stolen_pages, compressed_pages); | |
b0d623f7 | 6442 | } |
e5568f75 | 6443 | |
593a1d5f A |
6444 | #endif |
6445 | #if MACH_ASSERT | |
2d21ac55 A |
6446 | vm_page_verify_free_lists(); |
6447 | #endif | |
3e170ce0 A |
6448 | if (m == NULL && zone_gc_called == FALSE) { |
6449 | printf("%s(num=%d,low=%d): found %d pages at 0x%llx...scanned %d pages... yielded %d times... dumped run %d times... stole %d pages... stole %d compressed pages... wired count is %d\n", | |
0a7de745 A |
6450 | __func__, contig_pages, max_pnum, npages, (vm_object_offset_t)start_pnum << PAGE_SHIFT, |
6451 | scanned, yielded, dumped_run, stolen_pages, compressed_pages, vm_page_wire_count); | |
3e170ce0 A |
6452 | |
6453 | if (consider_buffer_cache_collect != NULL) { | |
6454 | (void)(*consider_buffer_cache_collect)(1); | |
6455 | } | |
6456 | ||
5ba3f43e | 6457 | consider_zone_gc(FALSE); |
3e170ce0 A |
6458 | |
6459 | zone_gc_called = TRUE; | |
6460 | ||
6461 | printf("vm_page_find_contiguous: zone_gc called... wired count is %d\n", vm_page_wire_count); | |
6462 | goto full_scan_again; | |
6463 | } | |
6464 | ||
e5568f75 | 6465 | return m; |
1c79356b A |
6466 | } |
6467 | ||
6468 | /* | |
6469 | * Allocate a list of contiguous, wired pages. | |
6470 | */ | |
6471 | kern_return_t | |
6472 | cpm_allocate( | |
0a7de745 A |
6473 | vm_size_t size, |
6474 | vm_page_t *list, | |
6475 | ppnum_t max_pnum, | |
6476 | ppnum_t pnum_mask, | |
6477 | boolean_t wire, | |
6478 | int flags) | |
1c79356b | 6479 | { |
0a7de745 A |
6480 | vm_page_t pages; |
6481 | unsigned int npages; | |
1c79356b | 6482 | |
0a7de745 | 6483 | if (size % PAGE_SIZE != 0) { |
1c79356b | 6484 | return KERN_INVALID_ARGUMENT; |
0a7de745 | 6485 | } |
1c79356b | 6486 | |
b0d623f7 A |
6487 | npages = (unsigned int) (size / PAGE_SIZE); |
6488 | if (npages != size / PAGE_SIZE) { | |
6489 | /* 32-bit overflow */ | |
6490 | return KERN_INVALID_ARGUMENT; | |
6491 | } | |
1c79356b | 6492 | |
1c79356b A |
6493 | /* |
6494 | * Obtain a pointer to a subset of the free | |
6495 | * list large enough to satisfy the request; | |
6496 | * the region will be physically contiguous. | |
6497 | */ | |
b0d623f7 | 6498 | pages = vm_page_find_contiguous(npages, max_pnum, pnum_mask, wire, flags); |
e5568f75 | 6499 | |
0a7de745 | 6500 | if (pages == VM_PAGE_NULL) { |
1c79356b | 6501 | return KERN_NO_SPACE; |
0a7de745 | 6502 | } |
1c79356b | 6503 | /* |
2d21ac55 | 6504 | * determine need for wakeups |
1c79356b | 6505 | */ |
0a7de745 | 6506 | if (vm_page_free_count < vm_page_free_min) { |
cb323159 A |
6507 | lck_mtx_lock(&vm_page_queue_free_lock); |
6508 | if (vm_pageout_running == FALSE) { | |
6509 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
6510 | thread_wakeup((event_t) &vm_page_free_wanted); | |
6511 | } else { | |
6512 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
6513 | } | |
0a7de745 A |
6514 | } |
6515 | ||
6d2010ae | 6516 | VM_CHECK_MEMORYSTATUS; |
0a7de745 | 6517 | |
1c79356b A |
6518 | /* |
6519 | * The CPM pages should now be available and | |
6520 | * ordered by ascending physical address. | |
6521 | */ | |
6522 | assert(vm_page_verify_contiguous(pages, npages)); | |
6523 | ||
6524 | *list = pages; | |
6525 | return KERN_SUCCESS; | |
6526 | } | |
6d2010ae A |
6527 | |
6528 | ||
6529 | unsigned int vm_max_delayed_work_limit = DEFAULT_DELAYED_WORK_LIMIT; | |
6530 | ||
6531 | /* | |
0a7de745 | 6532 | * when working on a 'run' of pages, it is necessary to hold |
6d2010ae A |
6533 | * the vm_page_queue_lock (a hot global lock) for certain operations |
6534 | * on the page... however, the majority of the work can be done | |
6535 | * while merely holding the object lock... in fact there are certain | |
6536 | * collections of pages that don't require any work brokered by the | |
6537 | * vm_page_queue_lock... to mitigate the time spent behind the global | |
6538 | * lock, go to a 2 pass algorithm... collect pages up to DELAYED_WORK_LIMIT | |
6539 | * while doing all of the work that doesn't require the vm_page_queue_lock... | |
6540 | * then call vm_page_do_delayed_work to acquire the vm_page_queue_lock and do the | |
6541 | * necessary work for each page... we will grab the busy bit on the page | |
6542 | * if it's not already held so that vm_page_do_delayed_work can drop the object lock | |
6543 | * if it can't immediately take the vm_page_queue_lock in order to compete | |
6544 | * for the locks in the same order that vm_pageout_scan takes them. | |
6545 | * the operation names are modeled after the names of the routines that | |
6546 | * need to be called in order to make the changes very obvious in the | |
6547 | * original loop | |
6548 | */ | |
6549 | ||
6550 | void | |
6551 | vm_page_do_delayed_work( | |
0a7de745 | 6552 | vm_object_t object, |
3e170ce0 | 6553 | vm_tag_t tag, |
6d2010ae | 6554 | struct vm_page_delayed_work *dwp, |
0a7de745 | 6555 | int dw_count) |
6d2010ae | 6556 | { |
0a7de745 A |
6557 | int j; |
6558 | vm_page_t m; | |
6559 | vm_page_t local_free_q = VM_PAGE_NULL; | |
6d2010ae A |
6560 | |
6561 | /* | |
6562 | * pageout_scan takes the vm_page_lock_queues first | |
6563 | * then tries for the object lock... to avoid what | |
6564 | * is effectively a lock inversion, we'll go to the | |
6565 | * trouble of taking them in that same order... otherwise | |
6566 | * if this object contains the majority of the pages resident | |
6567 | * in the UBC (or a small set of large objects actively being | |
6568 | * worked on contain the majority of the pages), we could | |
6569 | * cause the pageout_scan thread to 'starve' in its attempt | |
6570 | * to find pages to move to the free queue, since it has to | |
6571 | * successfully acquire the object lock of any candidate page | |
6572 | * before it can steal/clean it. | |
6573 | */ | |
6574 | if (!vm_page_trylockspin_queues()) { | |
6575 | vm_object_unlock(object); | |
6576 | ||
cb323159 A |
6577 | /* |
6578 | * "Turnstile enabled vm_pageout_scan" can be runnable | |
6579 | * for a very long time without getting on a core. | |
6580 | * If this is a higher priority thread it could be | |
6581 | * waiting here for a very long time respecting the fact | |
6582 | * that pageout_scan would like its object after VPS does | |
6583 | * a mutex_pause(0). | |
6584 | * So we cap the number of yields in the vm_object_lock_avoid() | |
6585 | * case to a single mutex_pause(0) which will give vm_pageout_scan | |
6586 | * 10us to run and grab the object if needed. | |
6587 | */ | |
6d2010ae A |
6588 | vm_page_lockspin_queues(); |
6589 | ||
0a7de745 | 6590 | for (j = 0;; j++) { |
cb323159 A |
6591 | if ((!vm_object_lock_avoid(object) || |
6592 | (vps_dynamic_priority_enabled && (j > 0))) && | |
0a7de745 | 6593 | _vm_object_lock_try(object)) { |
6d2010ae | 6594 | break; |
0a7de745 | 6595 | } |
6d2010ae A |
6596 | vm_page_unlock_queues(); |
6597 | mutex_pause(j); | |
6598 | vm_page_lockspin_queues(); | |
6599 | } | |
6d2010ae A |
6600 | } |
6601 | for (j = 0; j < dw_count; j++, dwp++) { | |
6d2010ae A |
6602 | m = dwp->dw_m; |
6603 | ||
0a7de745 | 6604 | if (dwp->dw_mask & DW_vm_pageout_throttle_up) { |
6d2010ae | 6605 | vm_pageout_throttle_up(m); |
0a7de745 | 6606 | } |
fe8ab488 | 6607 | #if CONFIG_PHANTOM_CACHE |
0a7de745 | 6608 | if (dwp->dw_mask & DW_vm_phantom_cache_update) { |
fe8ab488 | 6609 | vm_phantom_cache_update(m); |
0a7de745 | 6610 | } |
fe8ab488 | 6611 | #endif |
0a7de745 | 6612 | if (dwp->dw_mask & DW_vm_page_wire) { |
3e170ce0 | 6613 | vm_page_wire(m, tag, FALSE); |
0a7de745 A |
6614 | } else if (dwp->dw_mask & DW_vm_page_unwire) { |
6615 | boolean_t queueit; | |
6d2010ae | 6616 | |
fe8ab488 | 6617 | queueit = (dwp->dw_mask & (DW_vm_page_free | DW_vm_page_deactivate_internal)) ? FALSE : TRUE; |
6d2010ae A |
6618 | |
6619 | vm_page_unwire(m, queueit); | |
6620 | } | |
6621 | if (dwp->dw_mask & DW_vm_page_free) { | |
6622 | vm_page_free_prepare_queues(m); | |
6623 | ||
d9a64523 | 6624 | assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0); |
6d2010ae A |
6625 | /* |
6626 | * Add this page to our list of reclaimed pages, | |
6627 | * to be freed later. | |
6628 | */ | |
d9a64523 | 6629 | m->vmp_snext = local_free_q; |
6d2010ae A |
6630 | local_free_q = m; |
6631 | } else { | |
0a7de745 | 6632 | if (dwp->dw_mask & DW_vm_page_deactivate_internal) { |
6d2010ae | 6633 | vm_page_deactivate_internal(m, FALSE); |
0a7de745 | 6634 | } else if (dwp->dw_mask & DW_vm_page_activate) { |
d9a64523 | 6635 | if (m->vmp_q_state != VM_PAGE_ON_ACTIVE_Q) { |
6d2010ae A |
6636 | vm_page_activate(m); |
6637 | } | |
0a7de745 | 6638 | } else if (dwp->dw_mask & DW_vm_page_speculate) { |
6d2010ae | 6639 | vm_page_speculate(m, TRUE); |
0a7de745 | 6640 | } else if (dwp->dw_mask & DW_enqueue_cleaned) { |
316670eb A |
6641 | /* |
6642 | * if we didn't hold the object lock and did this, | |
6643 | * we might disconnect the page, then someone might | |
6644 | * soft fault it back in, then we would put it on the | |
6645 | * cleaned queue, and so we would have a referenced (maybe even dirty) | |
6646 | * page on that queue, which we don't want | |
6647 | */ | |
39037602 | 6648 | int refmod_state = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); |
316670eb A |
6649 | |
6650 | if ((refmod_state & VM_MEM_REFERENCED)) { | |
6651 | /* | |
6652 | * this page has been touched since it got cleaned; let's activate it | |
6653 | * if it hasn't already been | |
6654 | */ | |
0a7de745 | 6655 | VM_PAGEOUT_DEBUG(vm_pageout_enqueued_cleaned, 1); |
d9a64523 | 6656 | VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated, 1); |
316670eb | 6657 | |
0a7de745 | 6658 | if (m->vmp_q_state != VM_PAGE_ON_ACTIVE_Q) { |
316670eb | 6659 | vm_page_activate(m); |
0a7de745 | 6660 | } |
316670eb | 6661 | } else { |
d9a64523 | 6662 | m->vmp_reference = FALSE; |
316670eb A |
6663 | vm_page_enqueue_cleaned(m); |
6664 | } | |
0a7de745 | 6665 | } else if (dwp->dw_mask & DW_vm_page_lru) { |
6d2010ae | 6666 | vm_page_lru(m); |
0a7de745 A |
6667 | } else if (dwp->dw_mask & DW_VM_PAGE_QUEUES_REMOVE) { |
6668 | if (m->vmp_q_state != VM_PAGE_ON_PAGEOUT_Q) { | |
39037602 | 6669 | vm_page_queues_remove(m, TRUE); |
0a7de745 | 6670 | } |
316670eb | 6671 | } |
0a7de745 | 6672 | if (dwp->dw_mask & DW_set_reference) { |
d9a64523 | 6673 | m->vmp_reference = TRUE; |
0a7de745 | 6674 | } else if (dwp->dw_mask & DW_clear_reference) { |
d9a64523 | 6675 | m->vmp_reference = FALSE; |
0a7de745 | 6676 | } |
6d2010ae A |
6677 | |
6678 | if (dwp->dw_mask & DW_move_page) { | |
d9a64523 | 6679 | if (m->vmp_q_state != VM_PAGE_ON_PAGEOUT_Q) { |
39037602 | 6680 | vm_page_queues_remove(m, FALSE); |
6d2010ae | 6681 | |
39037602 | 6682 | assert(VM_PAGE_OBJECT(m) != kernel_object); |
6d2010ae | 6683 | |
3e170ce0 | 6684 | vm_page_enqueue_inactive(m, FALSE); |
316670eb | 6685 | } |
6d2010ae | 6686 | } |
0a7de745 | 6687 | if (dwp->dw_mask & DW_clear_busy) { |
d9a64523 | 6688 | m->vmp_busy = FALSE; |
0a7de745 | 6689 | } |
6d2010ae | 6690 | |
0a7de745 | 6691 | if (dwp->dw_mask & DW_PAGE_WAKEUP) { |
6d2010ae | 6692 | PAGE_WAKEUP(m); |
0a7de745 | 6693 | } |
6d2010ae A |
6694 | } |
6695 | } | |
6696 | vm_page_unlock_queues(); | |
6697 | ||
0a7de745 | 6698 | if (local_free_q) { |
6d2010ae | 6699 | vm_page_free_list(local_free_q, TRUE); |
0a7de745 | 6700 | } |
6d2010ae | 6701 | |
0a7de745 | 6702 | VM_CHECK_MEMORYSTATUS; |
6d2010ae A |
6703 | } |
6704 | ||
0b4c1975 A |
6705 | kern_return_t |
6706 | vm_page_alloc_list( | |
0a7de745 A |
6707 | int page_count, |
6708 | int flags, | |
0b4c1975 A |
6709 | vm_page_t *list) |
6710 | { | |
0a7de745 A |
6711 | vm_page_t lo_page_list = VM_PAGE_NULL; |
6712 | vm_page_t mem; | |
6713 | int i; | |
0b4c1975 | 6714 | |
0a7de745 | 6715 | if (!(flags & KMA_LOMEM)) { |
0b4c1975 | 6716 | panic("vm_page_alloc_list: called w/o KMA_LOMEM"); |
0a7de745 | 6717 | } |
0b4c1975 A |
6718 | |
6719 | for (i = 0; i < page_count; i++) { | |
0b4c1975 A |
6720 | mem = vm_page_grablo(); |
6721 | ||
6722 | if (mem == VM_PAGE_NULL) { | |
0a7de745 | 6723 | if (lo_page_list) { |
0b4c1975 | 6724 | vm_page_free_list(lo_page_list, FALSE); |
0a7de745 | 6725 | } |
0b4c1975 A |
6726 | |
6727 | *list = VM_PAGE_NULL; | |
6728 | ||
0a7de745 | 6729 | return KERN_RESOURCE_SHORTAGE; |
0b4c1975 | 6730 | } |
d9a64523 | 6731 | mem->vmp_snext = lo_page_list; |
0b4c1975 A |
6732 | lo_page_list = mem; |
6733 | } | |
6734 | *list = lo_page_list; | |
6735 | ||
0a7de745 | 6736 | return KERN_SUCCESS; |
0b4c1975 A |
6737 | } |
6738 | ||
6739 | void | |
6740 | vm_page_set_offset(vm_page_t page, vm_object_offset_t offset) | |
6741 | { | |
d9a64523 | 6742 | page->vmp_offset = offset; |
0b4c1975 A |
6743 | } |
6744 | ||
6745 | vm_page_t | |
6746 | vm_page_get_next(vm_page_t page) | |
6747 | { | |
0a7de745 | 6748 | return page->vmp_snext; |
0b4c1975 A |
6749 | } |
6750 | ||
6751 | vm_object_offset_t | |
6752 | vm_page_get_offset(vm_page_t page) | |
6753 | { | |
0a7de745 | 6754 | return page->vmp_offset; |
0b4c1975 A |
6755 | } |
6756 | ||
6757 | ppnum_t | |
6758 | vm_page_get_phys_page(vm_page_t page) | |
6759 | { | |
0a7de745 | 6760 | return VM_PAGE_GET_PHYS_PAGE(page); |
0b4c1975 | 6761 | } |
0a7de745 A |
6762 | |
6763 | ||
b0d623f7 A |
6764 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
6765 | ||
d1ecb069 A |
6766 | #if HIBERNATION |
6767 | ||
b0d623f7 A |
6768 | static vm_page_t hibernate_gobble_queue; |
6769 | ||
0b4c1975 | 6770 | static int hibernate_drain_pageout_queue(struct vm_pageout_queue *); |
39236c6e | 6771 | static int hibernate_flush_dirty_pages(int); |
39037602 | 6772 | static int hibernate_flush_queue(vm_page_queue_head_t *, int); |
0b4c1975 A |
6773 | |
6774 | void hibernate_flush_wait(void); | |
6775 | void hibernate_mark_in_progress(void); | |
6776 | void hibernate_clear_in_progress(void); | |
6777 | ||
0a7de745 A |
6778 | void hibernate_free_range(int, int); |
6779 | void hibernate_hash_insert_page(vm_page_t); | |
6780 | uint32_t hibernate_mark_as_unneeded(addr64_t, addr64_t, hibernate_page_list_t *, hibernate_page_list_t *); | |
0a7de745 A |
6781 | uint32_t hibernate_teardown_vm_structs(hibernate_page_list_t *, hibernate_page_list_t *); |
6782 | ppnum_t hibernate_lookup_paddr(unsigned int); | |
0b4c1975 A |
6783 | |
6784 | struct hibernate_statistics { | |
6785 | int hibernate_considered; | |
6786 | int hibernate_reentered_on_q; | |
6787 | int hibernate_found_dirty; | |
6788 | int hibernate_skipped_cleaning; | |
6789 | int hibernate_skipped_transient; | |
6790 | int hibernate_skipped_precious; | |
39236c6e | 6791 | int hibernate_skipped_external; |
0b4c1975 A |
6792 | int hibernate_queue_nolock; |
6793 | int hibernate_queue_paused; | |
6794 | int hibernate_throttled; | |
6795 | int hibernate_throttle_timeout; | |
6796 | int hibernate_drained; | |
6797 | int hibernate_drain_timeout; | |
6798 | int cd_lock_failed; | |
6799 | int cd_found_precious; | |
6800 | int cd_found_wired; | |
6801 | int cd_found_busy; | |
6802 | int cd_found_unusual; | |
6803 | int cd_found_cleaning; | |
6804 | int cd_found_laundry; | |
6805 | int cd_found_dirty; | |
39236c6e | 6806 | int cd_found_xpmapped; |
8a3053a0 | 6807 | int cd_skipped_xpmapped; |
0b4c1975 A |
6808 | int cd_local_free; |
6809 | int cd_total_free; | |
6810 | int cd_vm_page_wire_count; | |
39236c6e | 6811 | int cd_vm_struct_pages_unneeded; |
0b4c1975 A |
6812 | int cd_pages; |
6813 | int cd_discarded; | |
6814 | int cd_count_wire; | |
6815 | } hibernate_stats; | |
6816 | ||
6817 | ||
8a3053a0 A |
6818 | /* |
6819 | * clamp the number of 'xpmapped' pages we'll sweep into the hibernation image | |
6820 | * so that we don't overrun the estimated image size, which would | |
6821 | * result in a hibernation failure. | |
6822 | */ | |
0a7de745 | 6823 | #define HIBERNATE_XPMAPPED_LIMIT 40000 |
8a3053a0 | 6824 | |
0b4c1975 A |
6825 | |
6826 | static int | |
6827 | hibernate_drain_pageout_queue(struct vm_pageout_queue *q) | |
6828 | { | |
0a7de745 | 6829 | wait_result_t wait_result; |
0b4c1975 A |
6830 | |
6831 | vm_page_lock_queues(); | |
6832 | ||
0a7de745 | 6833 | while (!vm_page_queue_empty(&q->pgo_pending)) { |
0b4c1975 A |
6834 | q->pgo_draining = TRUE; |
6835 | ||
0a7de745 | 6836 | assert_wait_timeout((event_t) (&q->pgo_laundry + 1), THREAD_INTERRUPTIBLE, 5000, 1000 * NSEC_PER_USEC); |
0b4c1975 A |
6837 | |
6838 | vm_page_unlock_queues(); | |
6839 | ||
6840 | wait_result = thread_block(THREAD_CONTINUE_NULL); | |
6841 | ||
39037602 | 6842 | if (wait_result == THREAD_TIMED_OUT && !vm_page_queue_empty(&q->pgo_pending)) { |
0b4c1975 | 6843 | hibernate_stats.hibernate_drain_timeout++; |
0a7de745 A |
6844 | |
6845 | if (q == &vm_pageout_queue_external) { | |
6846 | return 0; | |
6847 | } | |
6848 | ||
6849 | return 1; | |
0b4c1975 A |
6850 | } |
6851 | vm_page_lock_queues(); | |
6852 | ||
6853 | hibernate_stats.hibernate_drained++; | |
6854 | } | |
6855 | vm_page_unlock_queues(); | |
6856 | ||
0a7de745 | 6857 | return 0; |
0b4c1975 A |
6858 | } |
6859 | ||
0b4c1975 | 6860 | |
39236c6e A |
6861 | boolean_t hibernate_skip_external = FALSE; |
6862 | ||
0b4c1975 | 6863 | static int |
39037602 | 6864 | hibernate_flush_queue(vm_page_queue_head_t *q, int qcount) |
0b4c1975 | 6865 | { |
0a7de745 A |
6866 | vm_page_t m; |
6867 | vm_object_t l_object = NULL; | |
6868 | vm_object_t m_object = NULL; | |
6869 | int refmod_state = 0; | |
6870 | int try_failed_count = 0; | |
6871 | int retval = 0; | |
6872 | int current_run = 0; | |
6873 | struct vm_pageout_queue *iq; | |
6874 | struct vm_pageout_queue *eq; | |
6875 | struct vm_pageout_queue *tq; | |
0b4c1975 | 6876 | |
5ba3f43e | 6877 | KDBG(IOKDBG_CODE(DBG_HIBERNATE, 4) | DBG_FUNC_START, |
0a7de745 | 6878 | VM_KERNEL_UNSLIDE_OR_PERM(q), qcount); |
0b4c1975 | 6879 | |
0b4c1975 A |
6880 | iq = &vm_pageout_queue_internal; |
6881 | eq = &vm_pageout_queue_external; | |
6882 | ||
6883 | vm_page_lock_queues(); | |
6884 | ||
39037602 | 6885 | while (qcount && !vm_page_queue_empty(q)) { |
0b4c1975 A |
6886 | if (current_run++ == 1000) { |
6887 | if (hibernate_should_abort()) { | |
6888 | retval = 1; | |
6889 | break; | |
6890 | } | |
6891 | current_run = 0; | |
6892 | } | |
6893 | ||
39037602 A |
6894 | m = (vm_page_t) vm_page_queue_first(q); |
6895 | m_object = VM_PAGE_OBJECT(m); | |
0b4c1975 A |
6896 | |
6897 | /* | |
6898 | * check to see if we currently are working | |
6899 | * with the same object... if so, we've | |
6900 | * already got the lock | |
6901 | */ | |
6902 | if (m_object != l_object) { | |
0a7de745 A |
6903 | /* |
6904 | * the object associated with candidate page is | |
0b4c1975 A |
6905 | * different from the one we were just working |
6906 | * with... dump the lock if we still own it | |
6907 | */ | |
0a7de745 A |
6908 | if (l_object != NULL) { |
6909 | vm_object_unlock(l_object); | |
0b4c1975 A |
6910 | l_object = NULL; |
6911 | } | |
6912 | /* | |
6913 | * Try to lock object; since we've alread got the | |
6914 | * page queues lock, we can only 'try' for this one. | |
6915 | * if the 'try' fails, we need to do a mutex_pause | |
6916 | * to allow the owner of the object lock a chance to | |
0a7de745 | 6917 | * run... |
0b4c1975 | 6918 | */ |
0a7de745 | 6919 | if (!vm_object_lock_try_scan(m_object)) { |
0b4c1975 A |
6920 | if (try_failed_count > 20) { |
6921 | hibernate_stats.hibernate_queue_nolock++; | |
6922 | ||
6923 | goto reenter_pg_on_q; | |
6924 | } | |
0b4c1975 A |
6925 | |
6926 | vm_page_unlock_queues(); | |
6927 | mutex_pause(try_failed_count++); | |
6928 | vm_page_lock_queues(); | |
6929 | ||
6930 | hibernate_stats.hibernate_queue_paused++; | |
6931 | continue; | |
6932 | } else { | |
6933 | l_object = m_object; | |
0b4c1975 A |
6934 | } |
6935 | } | |
0a7de745 | 6936 | if (!m_object->alive || m->vmp_cleaning || m->vmp_laundry || m->vmp_busy || m->vmp_absent || m->vmp_error) { |
0b4c1975 A |
6937 | /* |
6938 | * page is not to be cleaned | |
6939 | * put it back on the head of its queue | |
6940 | */ | |
0a7de745 | 6941 | if (m->vmp_cleaning) { |
0b4c1975 | 6942 | hibernate_stats.hibernate_skipped_cleaning++; |
0a7de745 | 6943 | } else { |
0b4c1975 | 6944 | hibernate_stats.hibernate_skipped_transient++; |
0a7de745 | 6945 | } |
0b4c1975 A |
6946 | |
6947 | goto reenter_pg_on_q; | |
6948 | } | |
0b4c1975 A |
6949 | if (m_object->copy == VM_OBJECT_NULL) { |
6950 | if (m_object->purgable == VM_PURGABLE_VOLATILE || m_object->purgable == VM_PURGABLE_EMPTY) { | |
6951 | /* | |
6952 | * let the normal hibernate image path | |
6953 | * deal with these | |
6954 | */ | |
6955 | goto reenter_pg_on_q; | |
6956 | } | |
6957 | } | |
0a7de745 A |
6958 | if (!m->vmp_dirty && m->vmp_pmapped) { |
6959 | refmod_state = pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(m)); | |
0b4c1975 | 6960 | |
316670eb A |
6961 | if ((refmod_state & VM_MEM_MODIFIED)) { |
6962 | SET_PAGE_DIRTY(m, FALSE); | |
6963 | } | |
0a7de745 | 6964 | } else { |
0b4c1975 | 6965 | refmod_state = 0; |
0a7de745 | 6966 | } |
0b4c1975 | 6967 | |
0a7de745 | 6968 | if (!m->vmp_dirty) { |
0b4c1975 A |
6969 | /* |
6970 | * page is not to be cleaned | |
6971 | * put it back on the head of its queue | |
6972 | */ | |
0a7de745 | 6973 | if (m->vmp_precious) { |
0b4c1975 | 6974 | hibernate_stats.hibernate_skipped_precious++; |
0a7de745 | 6975 | } |
0b4c1975 A |
6976 | |
6977 | goto reenter_pg_on_q; | |
6978 | } | |
39236c6e A |
6979 | |
6980 | if (hibernate_skip_external == TRUE && !m_object->internal) { | |
39236c6e | 6981 | hibernate_stats.hibernate_skipped_external++; |
0a7de745 | 6982 | |
39236c6e A |
6983 | goto reenter_pg_on_q; |
6984 | } | |
0b4c1975 A |
6985 | tq = NULL; |
6986 | ||
6987 | if (m_object->internal) { | |
0a7de745 | 6988 | if (VM_PAGE_Q_THROTTLED(iq)) { |
0b4c1975 | 6989 | tq = iq; |
0a7de745 A |
6990 | } |
6991 | } else if (VM_PAGE_Q_THROTTLED(eq)) { | |
0b4c1975 | 6992 | tq = eq; |
0a7de745 | 6993 | } |
0b4c1975 A |
6994 | |
6995 | if (tq != NULL) { | |
0a7de745 A |
6996 | wait_result_t wait_result; |
6997 | int wait_count = 5; | |
0b4c1975 | 6998 | |
0a7de745 A |
6999 | if (l_object != NULL) { |
7000 | vm_object_unlock(l_object); | |
0b4c1975 A |
7001 | l_object = NULL; |
7002 | } | |
0b4c1975 | 7003 | |
0b4c1975 | 7004 | while (retval == 0) { |
39236c6e A |
7005 | tq->pgo_throttled = TRUE; |
7006 | ||
0a7de745 | 7007 | assert_wait_timeout((event_t) &tq->pgo_laundry, THREAD_INTERRUPTIBLE, 1000, 1000 * NSEC_PER_USEC); |
0b4c1975 | 7008 | |
316670eb | 7009 | vm_page_unlock_queues(); |
0b4c1975 | 7010 | |
316670eb | 7011 | wait_result = thread_block(THREAD_CONTINUE_NULL); |
0b4c1975 A |
7012 | |
7013 | vm_page_lock_queues(); | |
7014 | ||
0a7de745 A |
7015 | if (wait_result != THREAD_TIMED_OUT) { |
7016 | break; | |
7017 | } | |
7018 | if (!VM_PAGE_Q_THROTTLED(tq)) { | |
39236c6e | 7019 | break; |
0a7de745 | 7020 | } |
39236c6e | 7021 | |
0a7de745 | 7022 | if (hibernate_should_abort()) { |
0b4c1975 | 7023 | retval = 1; |
0a7de745 | 7024 | } |
0b4c1975 | 7025 | |
0b4c1975 | 7026 | if (--wait_count == 0) { |
316670eb | 7027 | hibernate_stats.hibernate_throttle_timeout++; |
39236c6e A |
7028 | |
7029 | if (tq == eq) { | |
7030 | hibernate_skip_external = TRUE; | |
7031 | break; | |
7032 | } | |
316670eb A |
7033 | retval = 1; |
7034 | } | |
0b4c1975 | 7035 | } |
0a7de745 | 7036 | if (retval) { |
0b4c1975 | 7037 | break; |
0a7de745 | 7038 | } |
0b4c1975 A |
7039 | |
7040 | hibernate_stats.hibernate_throttled++; | |
7041 | ||
7042 | continue; | |
7043 | } | |
316670eb A |
7044 | /* |
7045 | * we've already factored out pages in the laundry which | |
7046 | * means this page can't be on the pageout queue so it's | |
3e170ce0 | 7047 | * safe to do the vm_page_queues_remove |
316670eb | 7048 | */ |
39037602 | 7049 | vm_page_queues_remove(m, TRUE); |
0b4c1975 | 7050 | |
0a7de745 | 7051 | if (m_object->internal == TRUE) { |
39037602 | 7052 | pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(m), PMAP_OPTIONS_COMPRESSOR, NULL); |
0a7de745 | 7053 | } |
39236c6e | 7054 | |
5ba3f43e | 7055 | vm_pageout_cluster(m); |
0b4c1975 A |
7056 | |
7057 | hibernate_stats.hibernate_found_dirty++; | |
7058 | ||
7059 | goto next_pg; | |
7060 | ||
7061 | reenter_pg_on_q: | |
0a7de745 A |
7062 | vm_page_queue_remove(q, m, vmp_pageq); |
7063 | vm_page_queue_enter(q, m, vmp_pageq); | |
0b4c1975 A |
7064 | |
7065 | hibernate_stats.hibernate_reentered_on_q++; | |
7066 | next_pg: | |
7067 | hibernate_stats.hibernate_considered++; | |
7068 | ||
7069 | qcount--; | |
7070 | try_failed_count = 0; | |
7071 | } | |
7072 | if (l_object != NULL) { | |
7073 | vm_object_unlock(l_object); | |
7074 | l_object = NULL; | |
7075 | } | |
0b4c1975 A |
7076 | |
7077 | vm_page_unlock_queues(); | |
7078 | ||
7079 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 4) | DBG_FUNC_END, hibernate_stats.hibernate_found_dirty, retval, 0, 0, 0); | |
7080 | ||
0a7de745 | 7081 | return retval; |
0b4c1975 A |
7082 | } |
7083 | ||
7084 | ||
7085 | static int | |
39236c6e | 7086 | hibernate_flush_dirty_pages(int pass) |
0b4c1975 | 7087 | { |
0a7de745 A |
7088 | struct vm_speculative_age_q *aq; |
7089 | uint32_t i; | |
0b4c1975 | 7090 | |
0b4c1975 | 7091 | if (vm_page_local_q) { |
f427ee49 A |
7092 | zpercpu_foreach_cpu(lid) { |
7093 | vm_page_reactivate_local(lid, TRUE, FALSE); | |
0a7de745 | 7094 | } |
0b4c1975 A |
7095 | } |
7096 | ||
7097 | for (i = 0; i <= VM_PAGE_MAX_SPECULATIVE_AGE_Q; i++) { | |
0a7de745 A |
7098 | int qcount; |
7099 | vm_page_t m; | |
0b4c1975 A |
7100 | |
7101 | aq = &vm_page_queue_speculative[i]; | |
7102 | ||
0a7de745 | 7103 | if (vm_page_queue_empty(&aq->age_q)) { |
0b4c1975 | 7104 | continue; |
0a7de745 | 7105 | } |
0b4c1975 A |
7106 | qcount = 0; |
7107 | ||
7108 | vm_page_lockspin_queues(); | |
7109 | ||
0a7de745 | 7110 | vm_page_queue_iterate(&aq->age_q, m, vmp_pageq) { |
0b4c1975 A |
7111 | qcount++; |
7112 | } | |
7113 | vm_page_unlock_queues(); | |
7114 | ||
7115 | if (qcount) { | |
0a7de745 A |
7116 | if (hibernate_flush_queue(&aq->age_q, qcount)) { |
7117 | return 1; | |
7118 | } | |
0b4c1975 A |
7119 | } |
7120 | } | |
0a7de745 A |
7121 | if (hibernate_flush_queue(&vm_page_queue_inactive, vm_page_inactive_count - vm_page_anonymous_count - vm_page_cleaned_count)) { |
7122 | return 1; | |
7123 | } | |
39037602 | 7124 | /* XXX FBDP TODO: flush secluded queue */ |
0a7de745 A |
7125 | if (hibernate_flush_queue(&vm_page_queue_anonymous, vm_page_anonymous_count)) { |
7126 | return 1; | |
7127 | } | |
7128 | if (hibernate_flush_queue(&vm_page_queue_cleaned, vm_page_cleaned_count)) { | |
7129 | return 1; | |
7130 | } | |
7131 | if (hibernate_drain_pageout_queue(&vm_pageout_queue_internal)) { | |
7132 | return 1; | |
7133 | } | |
7134 | ||
7135 | if (pass == 1) { | |
39236c6e | 7136 | vm_compressor_record_warmup_start(); |
0a7de745 | 7137 | } |
39236c6e A |
7138 | |
7139 | if (hibernate_flush_queue(&vm_page_queue_active, vm_page_active_count)) { | |
0a7de745 | 7140 | if (pass == 1) { |
39236c6e | 7141 | vm_compressor_record_warmup_end(); |
0a7de745 A |
7142 | } |
7143 | return 1; | |
39236c6e A |
7144 | } |
7145 | if (hibernate_drain_pageout_queue(&vm_pageout_queue_internal)) { | |
0a7de745 | 7146 | if (pass == 1) { |
39236c6e | 7147 | vm_compressor_record_warmup_end(); |
0a7de745 A |
7148 | } |
7149 | return 1; | |
39236c6e | 7150 | } |
0a7de745 | 7151 | if (pass == 1) { |
39236c6e | 7152 | vm_compressor_record_warmup_end(); |
0a7de745 | 7153 | } |
39236c6e | 7154 | |
0a7de745 A |
7155 | if (hibernate_skip_external == FALSE && hibernate_drain_pageout_queue(&vm_pageout_queue_external)) { |
7156 | return 1; | |
7157 | } | |
39236c6e | 7158 | |
0a7de745 | 7159 | return 0; |
39236c6e | 7160 | } |
0b4c1975 | 7161 | |
0b4c1975 | 7162 | |
fe8ab488 A |
7163 | void |
7164 | hibernate_reset_stats() | |
7165 | { | |
7166 | bzero(&hibernate_stats, sizeof(struct hibernate_statistics)); | |
7167 | } | |
7168 | ||
7169 | ||
0b4c1975 A |
7170 | int |
7171 | hibernate_flush_memory() | |
7172 | { | |
0a7de745 | 7173 | int retval; |
0b4c1975 | 7174 | |
39037602 A |
7175 | assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); |
7176 | ||
0b4c1975 A |
7177 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 3) | DBG_FUNC_START, vm_page_free_count, 0, 0, 0, 0); |
7178 | ||
39236c6e A |
7179 | hibernate_cleaning_in_progress = TRUE; |
7180 | hibernate_skip_external = FALSE; | |
7181 | ||
7182 | if ((retval = hibernate_flush_dirty_pages(1)) == 0) { | |
39037602 | 7183 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 10) | DBG_FUNC_START, VM_PAGE_COMPRESSOR_COUNT, 0, 0, 0, 0); |
0b4c1975 | 7184 | |
39037602 | 7185 | vm_compressor_flush(); |
0b4c1975 | 7186 | |
39037602 | 7187 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 10) | DBG_FUNC_END, VM_PAGE_COMPRESSOR_COUNT, 0, 0, 0, 0); |
39236c6e | 7188 | |
fe8ab488 | 7189 | if (consider_buffer_cache_collect != NULL) { |
39236c6e A |
7190 | unsigned int orig_wire_count; |
7191 | ||
7192 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 7) | DBG_FUNC_START, 0, 0, 0, 0, 0); | |
7193 | orig_wire_count = vm_page_wire_count; | |
0a7de745 | 7194 | |
0b4c1975 | 7195 | (void)(*consider_buffer_cache_collect)(1); |
5ba3f43e | 7196 | consider_zone_gc(FALSE); |
0b4c1975 | 7197 | |
39236c6e A |
7198 | HIBLOG("hibernate_flush_memory: buffer_cache_gc freed up %d wired pages\n", orig_wire_count - vm_page_wire_count); |
7199 | ||
7200 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 7) | DBG_FUNC_END, orig_wire_count - vm_page_wire_count, 0, 0, 0, 0); | |
0b4c1975 A |
7201 | } |
7202 | } | |
39236c6e A |
7203 | hibernate_cleaning_in_progress = FALSE; |
7204 | ||
0b4c1975 A |
7205 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 3) | DBG_FUNC_END, vm_page_free_count, hibernate_stats.hibernate_found_dirty, retval, 0, 0); |
7206 | ||
0a7de745 | 7207 | if (retval) { |
39236c6e | 7208 | HIBLOG("hibernate_flush_memory() failed to finish - vm_page_compressor_count(%d)\n", VM_PAGE_COMPRESSOR_COUNT); |
0a7de745 | 7209 | } |
39236c6e A |
7210 | |
7211 | ||
0a7de745 A |
7212 | HIBPRINT("hibernate_flush_memory() considered(%d) reentered_on_q(%d) found_dirty(%d)\n", |
7213 | hibernate_stats.hibernate_considered, | |
7214 | hibernate_stats.hibernate_reentered_on_q, | |
7215 | hibernate_stats.hibernate_found_dirty); | |
7216 | HIBPRINT(" skipped_cleaning(%d) skipped_transient(%d) skipped_precious(%d) skipped_external(%d) queue_nolock(%d)\n", | |
7217 | hibernate_stats.hibernate_skipped_cleaning, | |
7218 | hibernate_stats.hibernate_skipped_transient, | |
7219 | hibernate_stats.hibernate_skipped_precious, | |
7220 | hibernate_stats.hibernate_skipped_external, | |
7221 | hibernate_stats.hibernate_queue_nolock); | |
7222 | HIBPRINT(" queue_paused(%d) throttled(%d) throttle_timeout(%d) drained(%d) drain_timeout(%d)\n", | |
7223 | hibernate_stats.hibernate_queue_paused, | |
7224 | hibernate_stats.hibernate_throttled, | |
7225 | hibernate_stats.hibernate_throttle_timeout, | |
7226 | hibernate_stats.hibernate_drained, | |
7227 | hibernate_stats.hibernate_drain_timeout); | |
0b4c1975 | 7228 | |
0a7de745 | 7229 | return retval; |
0b4c1975 A |
7230 | } |
7231 | ||
6d2010ae | 7232 | |
b0d623f7 A |
7233 | static void |
7234 | hibernate_page_list_zero(hibernate_page_list_t *list) | |
7235 | { | |
0a7de745 A |
7236 | uint32_t bank; |
7237 | hibernate_bitmap_t * bitmap; | |
7238 | ||
7239 | bitmap = &list->bank_bitmap[0]; | |
7240 | for (bank = 0; bank < list->bank_count; bank++) { | |
7241 | uint32_t last_bit; | |
7242 | ||
7243 | bzero((void *) &bitmap->bitmap[0], bitmap->bitmapwords << 2); | |
7244 | // set out-of-bound bits at end of bitmap. | |
7245 | last_bit = ((bitmap->last_page - bitmap->first_page + 1) & 31); | |
7246 | if (last_bit) { | |
7247 | bitmap->bitmap[bitmap->bitmapwords - 1] = (0xFFFFFFFF >> last_bit); | |
7248 | } | |
b0d623f7 | 7249 | |
0a7de745 A |
7250 | bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords]; |
7251 | } | |
b0d623f7 A |
7252 | } |
7253 | ||
b0d623f7 A |
7254 | void |
7255 | hibernate_free_gobble_pages(void) | |
7256 | { | |
0a7de745 A |
7257 | vm_page_t m, next; |
7258 | uint32_t count = 0; | |
7259 | ||
7260 | m = (vm_page_t) hibernate_gobble_queue; | |
7261 | while (m) { | |
7262 | next = m->vmp_snext; | |
7263 | vm_page_free(m); | |
7264 | count++; | |
7265 | m = next; | |
7266 | } | |
7267 | hibernate_gobble_queue = VM_PAGE_NULL; | |
b0d623f7 | 7268 | |
0a7de745 A |
7269 | if (count) { |
7270 | HIBLOG("Freed %d pages\n", count); | |
7271 | } | |
b0d623f7 A |
7272 | } |
7273 | ||
0a7de745 | 7274 | static boolean_t |
db609669 | 7275 | hibernate_consider_discard(vm_page_t m, boolean_t preflight) |
b0d623f7 | 7276 | { |
0a7de745 A |
7277 | vm_object_t object = NULL; |
7278 | int refmod_state; | |
7279 | boolean_t discard = FALSE; | |
b0d623f7 | 7280 | |
0a7de745 A |
7281 | do{ |
7282 | if (m->vmp_private) { | |
7283 | panic("hibernate_consider_discard: private"); | |
7284 | } | |
b0d623f7 | 7285 | |
0a7de745 | 7286 | object = VM_PAGE_OBJECT(m); |
39037602 | 7287 | |
0a7de745 A |
7288 | if (!vm_object_lock_try(object)) { |
7289 | object = NULL; | |
7290 | if (!preflight) { | |
7291 | hibernate_stats.cd_lock_failed++; | |
7292 | } | |
7293 | break; | |
7294 | } | |
7295 | if (VM_PAGE_WIRED(m)) { | |
7296 | if (!preflight) { | |
7297 | hibernate_stats.cd_found_wired++; | |
7298 | } | |
7299 | break; | |
7300 | } | |
7301 | if (m->vmp_precious) { | |
7302 | if (!preflight) { | |
7303 | hibernate_stats.cd_found_precious++; | |
7304 | } | |
7305 | break; | |
7306 | } | |
7307 | if (m->vmp_busy || !object->alive) { | |
7308 | /* | |
7309 | * Somebody is playing with this page. | |
7310 | */ | |
7311 | if (!preflight) { | |
7312 | hibernate_stats.cd_found_busy++; | |
7313 | } | |
7314 | break; | |
7315 | } | |
7316 | if (m->vmp_absent || m->vmp_unusual || m->vmp_error) { | |
7317 | /* | |
7318 | * If it's unusual in anyway, ignore it | |
7319 | */ | |
7320 | if (!preflight) { | |
7321 | hibernate_stats.cd_found_unusual++; | |
7322 | } | |
7323 | break; | |
7324 | } | |
7325 | if (m->vmp_cleaning) { | |
7326 | if (!preflight) { | |
7327 | hibernate_stats.cd_found_cleaning++; | |
7328 | } | |
7329 | break; | |
7330 | } | |
7331 | if (m->vmp_laundry) { | |
7332 | if (!preflight) { | |
7333 | hibernate_stats.cd_found_laundry++; | |
7334 | } | |
7335 | break; | |
7336 | } | |
7337 | if (!m->vmp_dirty) { | |
7338 | refmod_state = pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(m)); | |
7339 | ||
7340 | if (refmod_state & VM_MEM_REFERENCED) { | |
7341 | m->vmp_reference = TRUE; | |
7342 | } | |
7343 | if (refmod_state & VM_MEM_MODIFIED) { | |
7344 | SET_PAGE_DIRTY(m, FALSE); | |
7345 | } | |
7346 | } | |
7347 | ||
7348 | /* | |
7349 | * If it's clean or purgeable we can discard the page on wakeup. | |
7350 | */ | |
7351 | discard = (!m->vmp_dirty) | |
b0d623f7 | 7352 | || (VM_PURGABLE_VOLATILE == object->purgable) |
0a7de745 | 7353 | || (VM_PURGABLE_EMPTY == object->purgable); |
0b4c1975 | 7354 | |
39236c6e | 7355 | |
0a7de745 A |
7356 | if (discard == FALSE) { |
7357 | if (!preflight) { | |
7358 | hibernate_stats.cd_found_dirty++; | |
7359 | } | |
7360 | } else if (m->vmp_xpmapped && m->vmp_reference && !object->internal) { | |
7361 | if (hibernate_stats.cd_found_xpmapped < HIBERNATE_XPMAPPED_LIMIT) { | |
7362 | if (!preflight) { | |
7363 | hibernate_stats.cd_found_xpmapped++; | |
7364 | } | |
7365 | discard = FALSE; | |
7366 | } else { | |
7367 | if (!preflight) { | |
7368 | hibernate_stats.cd_skipped_xpmapped++; | |
7369 | } | |
7370 | } | |
8a3053a0 | 7371 | } |
0a7de745 | 7372 | }while (FALSE); |
b0d623f7 | 7373 | |
0a7de745 A |
7374 | if (object) { |
7375 | vm_object_unlock(object); | |
7376 | } | |
b0d623f7 | 7377 | |
0a7de745 | 7378 | return discard; |
b0d623f7 A |
7379 | } |
7380 | ||
7381 | ||
7382 | static void | |
7383 | hibernate_discard_page(vm_page_t m) | |
7384 | { | |
0a7de745 | 7385 | vm_object_t m_object; |
39037602 | 7386 | |
0a7de745 A |
7387 | if (m->vmp_absent || m->vmp_unusual || m->vmp_error) { |
7388 | /* | |
7389 | * If it's unusual in anyway, ignore | |
7390 | */ | |
7391 | return; | |
7392 | } | |
b0d623f7 | 7393 | |
0a7de745 | 7394 | m_object = VM_PAGE_OBJECT(m); |
39037602 | 7395 | |
fe8ab488 | 7396 | #if MACH_ASSERT || DEBUG |
0a7de745 A |
7397 | if (!vm_object_lock_try(m_object)) { |
7398 | panic("hibernate_discard_page(%p) !vm_object_lock_try", m); | |
7399 | } | |
316670eb | 7400 | #else |
0a7de745 A |
7401 | /* No need to lock page queue for token delete, hibernate_vm_unlock() |
7402 | * makes sure these locks are uncontended before sleep */ | |
fe8ab488 | 7403 | #endif /* MACH_ASSERT || DEBUG */ |
316670eb | 7404 | |
0a7de745 A |
7405 | if (m->vmp_pmapped == TRUE) { |
7406 | __unused int refmod_state = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); | |
39236c6e | 7407 | } |
fe8ab488 | 7408 | |
0a7de745 A |
7409 | if (m->vmp_laundry) { |
7410 | panic("hibernate_discard_page(%p) laundry", m); | |
7411 | } | |
7412 | if (m->vmp_private) { | |
7413 | panic("hibernate_discard_page(%p) private", m); | |
7414 | } | |
7415 | if (m->vmp_fictitious) { | |
7416 | panic("hibernate_discard_page(%p) fictitious", m); | |
7417 | } | |
7418 | ||
7419 | if (VM_PURGABLE_VOLATILE == m_object->purgable) { | |
7420 | /* object should be on a queue */ | |
7421 | assert((m_object->objq.next != NULL) && (m_object->objq.prev != NULL)); | |
7422 | purgeable_q_t old_queue = vm_purgeable_object_remove(m_object); | |
7423 | assert(old_queue); | |
7424 | if (m_object->purgeable_when_ripe) { | |
7425 | vm_purgeable_token_delete_first(old_queue); | |
7426 | } | |
7427 | vm_object_lock_assert_exclusive(m_object); | |
7428 | m_object->purgable = VM_PURGABLE_EMPTY; | |
7429 | ||
7430 | /* | |
7431 | * Purgeable ledgers: pages of VOLATILE and EMPTY objects are | |
7432 | * accounted in the "volatile" ledger, so no change here. | |
7433 | * We have to update vm_page_purgeable_count, though, since we're | |
7434 | * effectively purging this object. | |
7435 | */ | |
7436 | unsigned int delta; | |
7437 | assert(m_object->resident_page_count >= m_object->wired_page_count); | |
7438 | delta = (m_object->resident_page_count - m_object->wired_page_count); | |
7439 | assert(vm_page_purgeable_count >= delta); | |
7440 | assert(delta > 0); | |
7441 | OSAddAtomic(-delta, (SInt32 *)&vm_page_purgeable_count); | |
7442 | } | |
7443 | ||
7444 | vm_page_free(m); | |
316670eb | 7445 | |
fe8ab488 | 7446 | #if MACH_ASSERT || DEBUG |
0a7de745 A |
7447 | vm_object_unlock(m_object); |
7448 | #endif /* MACH_ASSERT || DEBUG */ | |
b0d623f7 A |
7449 | } |
7450 | ||
db609669 | 7451 | /* |
0a7de745 A |
7452 | * Grab locks for hibernate_page_list_setall() |
7453 | */ | |
db609669 A |
7454 | void |
7455 | hibernate_vm_lock_queues(void) | |
7456 | { | |
0a7de745 A |
7457 | vm_object_lock(compressor_object); |
7458 | vm_page_lock_queues(); | |
7459 | lck_mtx_lock(&vm_page_queue_free_lock); | |
7460 | lck_mtx_lock(&vm_purgeable_queue_lock); | |
db609669 | 7461 | |
0a7de745 | 7462 | if (vm_page_local_q) { |
f427ee49 | 7463 | zpercpu_foreach(lq, vm_page_local_q) { |
0a7de745 A |
7464 | VPL_LOCK(&lq->vpl_lock); |
7465 | } | |
db609669 | 7466 | } |
db609669 A |
7467 | } |
7468 | ||
7469 | void | |
7470 | hibernate_vm_unlock_queues(void) | |
7471 | { | |
0a7de745 | 7472 | if (vm_page_local_q) { |
f427ee49 | 7473 | zpercpu_foreach(lq, vm_page_local_q) { |
0a7de745 A |
7474 | VPL_UNLOCK(&lq->vpl_lock); |
7475 | } | |
db609669 | 7476 | } |
0a7de745 A |
7477 | lck_mtx_unlock(&vm_purgeable_queue_lock); |
7478 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
7479 | vm_page_unlock_queues(); | |
7480 | vm_object_unlock(compressor_object); | |
db609669 A |
7481 | } |
7482 | ||
b0d623f7 | 7483 | /* |
0a7de745 A |
7484 | * Bits zero in the bitmaps => page needs to be saved. All pages default to be saved, |
7485 | * pages known to VM to not need saving are subtracted. | |
7486 | * Wired pages to be saved are present in page_list_wired, pageable in page_list. | |
7487 | */ | |
b0d623f7 A |
7488 | |
7489 | void | |
7490 | hibernate_page_list_setall(hibernate_page_list_t * page_list, | |
0a7de745 A |
7491 | hibernate_page_list_t * page_list_wired, |
7492 | hibernate_page_list_t * page_list_pal, | |
7493 | boolean_t preflight, | |
7494 | boolean_t will_discard, | |
7495 | uint32_t * pagesOut) | |
7496 | { | |
7497 | uint64_t start, end, nsec; | |
7498 | vm_page_t m; | |
7499 | vm_page_t next; | |
7500 | uint32_t pages = page_list->page_count; | |
7501 | uint32_t count_anonymous = 0, count_throttled = 0, count_compressor = 0; | |
7502 | uint32_t count_inactive = 0, count_active = 0, count_speculative = 0, count_cleaned = 0; | |
7503 | uint32_t count_wire = pages; | |
7504 | uint32_t count_discard_active = 0; | |
7505 | uint32_t count_discard_inactive = 0; | |
7506 | uint32_t count_discard_cleaned = 0; | |
7507 | uint32_t count_discard_purgeable = 0; | |
7508 | uint32_t count_discard_speculative = 0; | |
7509 | uint32_t count_discard_vm_struct_pages = 0; | |
7510 | uint32_t i; | |
7511 | uint32_t bank; | |
7512 | hibernate_bitmap_t * bitmap; | |
7513 | hibernate_bitmap_t * bitmap_wired; | |
7514 | boolean_t discard_all; | |
7515 | boolean_t discard; | |
7516 | ||
7517 | HIBLOG("hibernate_page_list_setall(preflight %d) start\n", preflight); | |
7518 | ||
7519 | if (preflight) { | |
7520 | page_list = NULL; | |
7521 | page_list_wired = NULL; | |
7522 | page_list_pal = NULL; | |
39236c6e | 7523 | discard_all = FALSE; |
0a7de745 | 7524 | } else { |
39236c6e | 7525 | discard_all = will_discard; |
0a7de745 | 7526 | } |
0b4c1975 | 7527 | |
fe8ab488 | 7528 | #if MACH_ASSERT || DEBUG |
0a7de745 A |
7529 | if (!preflight) { |
7530 | assert(hibernate_vm_locks_are_safe()); | |
7531 | vm_page_lock_queues(); | |
7532 | if (vm_page_local_q) { | |
f427ee49 | 7533 | zpercpu_foreach(lq, vm_page_local_q) { |
0a7de745 A |
7534 | VPL_LOCK(&lq->vpl_lock); |
7535 | } | |
7536 | } | |
7537 | } | |
7538 | #endif /* MACH_ASSERT || DEBUG */ | |
7539 | ||
7540 | ||
7541 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 8) | DBG_FUNC_START, count_wire, 0, 0, 0, 0); | |
7542 | ||
7543 | clock_get_uptime(&start); | |
7544 | ||
7545 | if (!preflight) { | |
7546 | hibernate_page_list_zero(page_list); | |
7547 | hibernate_page_list_zero(page_list_wired); | |
7548 | hibernate_page_list_zero(page_list_pal); | |
7549 | ||
7550 | hibernate_stats.cd_vm_page_wire_count = vm_page_wire_count; | |
7551 | hibernate_stats.cd_pages = pages; | |
7552 | } | |
7553 | ||
316670eb | 7554 | if (vm_page_local_q) { |
f427ee49 A |
7555 | zpercpu_foreach_cpu(lid) { |
7556 | vm_page_reactivate_local(lid, TRUE, !preflight); | |
0a7de745 A |
7557 | } |
7558 | } | |
7559 | ||
7560 | if (preflight) { | |
7561 | vm_object_lock(compressor_object); | |
7562 | vm_page_lock_queues(); | |
7563 | lck_mtx_lock(&vm_page_queue_free_lock); | |
7564 | } | |
7565 | ||
7566 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
7567 | ||
7568 | hibernation_vmqueues_inspection = TRUE; | |
7569 | ||
7570 | m = (vm_page_t) hibernate_gobble_queue; | |
7571 | while (m) { | |
7572 | pages--; | |
7573 | count_wire--; | |
7574 | if (!preflight) { | |
7575 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7576 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7577 | } | |
7578 | m = m->vmp_snext; | |
7579 | } | |
7580 | ||
7581 | if (!preflight) { | |
f427ee49 A |
7582 | percpu_foreach(free_pages_head, free_pages) { |
7583 | for (m = *free_pages_head; m; m = m->vmp_snext) { | |
7584 | assert(m->vmp_q_state == VM_PAGE_ON_FREE_LOCAL_Q); | |
0a7de745 | 7585 | |
f427ee49 A |
7586 | pages--; |
7587 | count_wire--; | |
7588 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7589 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
0a7de745 | 7590 | |
f427ee49 A |
7591 | hibernate_stats.cd_local_free++; |
7592 | hibernate_stats.cd_total_free++; | |
0a7de745 A |
7593 | } |
7594 | } | |
7595 | } | |
7596 | ||
7597 | for (i = 0; i < vm_colors; i++) { | |
7598 | vm_page_queue_iterate(&vm_page_queue_free[i].qhead, m, vmp_pageq) { | |
7599 | assert(m->vmp_q_state == VM_PAGE_ON_FREE_Q); | |
7600 | ||
7601 | pages--; | |
7602 | count_wire--; | |
7603 | if (!preflight) { | |
7604 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7605 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7606 | ||
7607 | hibernate_stats.cd_total_free++; | |
7608 | } | |
7609 | } | |
7610 | } | |
7611 | ||
7612 | vm_page_queue_iterate(&vm_lopage_queue_free, m, vmp_pageq) { | |
7613 | assert(m->vmp_q_state == VM_PAGE_ON_FREE_LOPAGE_Q); | |
7614 | ||
7615 | pages--; | |
7616 | count_wire--; | |
7617 | if (!preflight) { | |
7618 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7619 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7620 | ||
7621 | hibernate_stats.cd_total_free++; | |
7622 | } | |
7623 | } | |
7624 | ||
7625 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_throttled); | |
7626 | while (m && !vm_page_queue_end(&vm_page_queue_throttled, (vm_page_queue_entry_t)m)) { | |
7627 | assert(m->vmp_q_state == VM_PAGE_ON_THROTTLED_Q); | |
7628 | ||
7629 | next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7630 | discard = FALSE; | |
7631 | if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) | |
7632 | && hibernate_consider_discard(m, preflight)) { | |
7633 | if (!preflight) { | |
7634 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7635 | } | |
7636 | count_discard_inactive++; | |
7637 | discard = discard_all; | |
7638 | } else { | |
7639 | count_throttled++; | |
7640 | } | |
7641 | count_wire--; | |
7642 | if (!preflight) { | |
7643 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7644 | } | |
7645 | ||
7646 | if (discard) { | |
7647 | hibernate_discard_page(m); | |
7648 | } | |
7649 | m = next; | |
7650 | } | |
7651 | ||
7652 | m = (vm_page_t)vm_page_queue_first(&vm_page_queue_anonymous); | |
7653 | while (m && !vm_page_queue_end(&vm_page_queue_anonymous, (vm_page_queue_entry_t)m)) { | |
7654 | assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_INTERNAL_Q); | |
7655 | ||
7656 | next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7657 | discard = FALSE; | |
7658 | if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) && | |
7659 | hibernate_consider_discard(m, preflight)) { | |
7660 | if (!preflight) { | |
7661 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7662 | } | |
7663 | if (m->vmp_dirty) { | |
7664 | count_discard_purgeable++; | |
7665 | } else { | |
7666 | count_discard_inactive++; | |
7667 | } | |
7668 | discard = discard_all; | |
7669 | } else { | |
7670 | count_anonymous++; | |
7671 | } | |
7672 | count_wire--; | |
7673 | if (!preflight) { | |
7674 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7675 | } | |
7676 | if (discard) { | |
7677 | hibernate_discard_page(m); | |
7678 | } | |
7679 | m = next; | |
7680 | } | |
7681 | ||
7682 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_cleaned); | |
7683 | while (m && !vm_page_queue_end(&vm_page_queue_cleaned, (vm_page_queue_entry_t)m)) { | |
7684 | assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q); | |
7685 | ||
7686 | next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7687 | discard = FALSE; | |
7688 | if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) && | |
7689 | hibernate_consider_discard(m, preflight)) { | |
7690 | if (!preflight) { | |
7691 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7692 | } | |
7693 | if (m->vmp_dirty) { | |
7694 | count_discard_purgeable++; | |
7695 | } else { | |
7696 | count_discard_cleaned++; | |
7697 | } | |
7698 | discard = discard_all; | |
7699 | } else { | |
7700 | count_cleaned++; | |
7701 | } | |
7702 | count_wire--; | |
7703 | if (!preflight) { | |
7704 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7705 | } | |
7706 | if (discard) { | |
7707 | hibernate_discard_page(m); | |
7708 | } | |
7709 | m = next; | |
7710 | } | |
7711 | ||
7712 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_active); | |
7713 | while (m && !vm_page_queue_end(&vm_page_queue_active, (vm_page_queue_entry_t)m)) { | |
7714 | assert(m->vmp_q_state == VM_PAGE_ON_ACTIVE_Q); | |
7715 | ||
7716 | next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7717 | discard = FALSE; | |
7718 | if ((kIOHibernateModeDiscardCleanActive & gIOHibernateMode) && | |
7719 | hibernate_consider_discard(m, preflight)) { | |
7720 | if (!preflight) { | |
7721 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7722 | } | |
7723 | if (m->vmp_dirty) { | |
7724 | count_discard_purgeable++; | |
7725 | } else { | |
7726 | count_discard_active++; | |
7727 | } | |
7728 | discard = discard_all; | |
7729 | } else { | |
7730 | count_active++; | |
7731 | } | |
7732 | count_wire--; | |
7733 | if (!preflight) { | |
7734 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7735 | } | |
7736 | if (discard) { | |
7737 | hibernate_discard_page(m); | |
7738 | } | |
7739 | m = next; | |
7740 | } | |
7741 | ||
7742 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_inactive); | |
7743 | while (m && !vm_page_queue_end(&vm_page_queue_inactive, (vm_page_queue_entry_t)m)) { | |
7744 | assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_EXTERNAL_Q); | |
7745 | ||
7746 | next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7747 | discard = FALSE; | |
7748 | if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) && | |
7749 | hibernate_consider_discard(m, preflight)) { | |
7750 | if (!preflight) { | |
7751 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7752 | } | |
7753 | if (m->vmp_dirty) { | |
7754 | count_discard_purgeable++; | |
7755 | } else { | |
7756 | count_discard_inactive++; | |
7757 | } | |
7758 | discard = discard_all; | |
7759 | } else { | |
7760 | count_inactive++; | |
7761 | } | |
7762 | count_wire--; | |
7763 | if (!preflight) { | |
7764 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7765 | } | |
7766 | if (discard) { | |
7767 | hibernate_discard_page(m); | |
7768 | } | |
7769 | m = next; | |
7770 | } | |
7771 | /* XXX FBDP TODO: secluded queue */ | |
7772 | ||
7773 | for (i = 0; i <= VM_PAGE_MAX_SPECULATIVE_AGE_Q; i++) { | |
7774 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_speculative[i].age_q); | |
7775 | while (m && !vm_page_queue_end(&vm_page_queue_speculative[i].age_q, (vm_page_queue_entry_t)m)) { | |
7776 | assertf(m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q, | |
7777 | "Bad page: %p (0x%x:0x%x) on queue %d has state: %d (Discard: %d, Preflight: %d)", | |
7778 | m, m->vmp_pageq.next, m->vmp_pageq.prev, i, m->vmp_q_state, discard, preflight); | |
7779 | ||
7780 | next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7781 | discard = FALSE; | |
7782 | if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) && | |
7783 | hibernate_consider_discard(m, preflight)) { | |
7784 | if (!preflight) { | |
7785 | hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7786 | } | |
7787 | count_discard_speculative++; | |
7788 | discard = discard_all; | |
7789 | } else { | |
7790 | count_speculative++; | |
7791 | } | |
7792 | count_wire--; | |
7793 | if (!preflight) { | |
7794 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7795 | } | |
7796 | if (discard) { | |
7797 | hibernate_discard_page(m); | |
7798 | } | |
7799 | m = next; | |
7800 | } | |
316670eb | 7801 | } |
316670eb | 7802 | |
0a7de745 A |
7803 | vm_page_queue_iterate(&compressor_object->memq, m, vmp_listq) { |
7804 | assert(m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR); | |
316670eb | 7805 | |
0a7de745 A |
7806 | count_compressor++; |
7807 | count_wire--; | |
7808 | if (!preflight) { | |
7809 | hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m)); | |
7810 | } | |
7811 | } | |
b0d623f7 | 7812 | |
0a7de745 A |
7813 | if (preflight == FALSE && discard_all == TRUE) { |
7814 | KDBG(IOKDBG_CODE(DBG_HIBERNATE, 12) | DBG_FUNC_START); | |
0b4c1975 | 7815 | |
0a7de745 A |
7816 | HIBLOG("hibernate_teardown started\n"); |
7817 | count_discard_vm_struct_pages = hibernate_teardown_vm_structs(page_list, page_list_wired); | |
7818 | HIBLOG("hibernate_teardown completed - discarded %d\n", count_discard_vm_struct_pages); | |
db609669 | 7819 | |
0a7de745 A |
7820 | pages -= count_discard_vm_struct_pages; |
7821 | count_wire -= count_discard_vm_struct_pages; | |
b0d623f7 | 7822 | |
0a7de745 | 7823 | hibernate_stats.cd_vm_struct_pages_unneeded = count_discard_vm_struct_pages; |
e8c3f781 | 7824 | |
0a7de745 A |
7825 | KDBG(IOKDBG_CODE(DBG_HIBERNATE, 12) | DBG_FUNC_END); |
7826 | } | |
e8c3f781 | 7827 | |
db609669 | 7828 | if (!preflight) { |
0a7de745 A |
7829 | // pull wired from hibernate_bitmap |
7830 | bitmap = &page_list->bank_bitmap[0]; | |
7831 | bitmap_wired = &page_list_wired->bank_bitmap[0]; | |
7832 | for (bank = 0; bank < page_list->bank_count; bank++) { | |
7833 | for (i = 0; i < bitmap->bitmapwords; i++) { | |
7834 | bitmap->bitmap[i] = bitmap->bitmap[i] | ~bitmap_wired->bitmap[i]; | |
7835 | } | |
7836 | bitmap = (hibernate_bitmap_t *)&bitmap->bitmap[bitmap->bitmapwords]; | |
7837 | bitmap_wired = (hibernate_bitmap_t *) &bitmap_wired->bitmap[bitmap_wired->bitmapwords]; | |
7838 | } | |
db609669 | 7839 | } |
39037602 | 7840 | |
0a7de745 A |
7841 | // machine dependent adjustments |
7842 | hibernate_page_list_setall_machine(page_list, page_list_wired, preflight, &pages); | |
6d2010ae | 7843 | |
db609669 | 7844 | if (!preflight) { |
0a7de745 A |
7845 | hibernate_stats.cd_count_wire = count_wire; |
7846 | hibernate_stats.cd_discarded = count_discard_active + count_discard_inactive + count_discard_purgeable + | |
7847 | count_discard_speculative + count_discard_cleaned + count_discard_vm_struct_pages; | |
db609669 | 7848 | } |
b0d623f7 | 7849 | |
0a7de745 A |
7850 | clock_get_uptime(&end); |
7851 | absolutetime_to_nanoseconds(end - start, &nsec); | |
7852 | HIBLOG("hibernate_page_list_setall time: %qd ms\n", nsec / 1000000ULL); | |
0b4c1975 | 7853 | |
0a7de745 A |
7854 | HIBLOG("pages %d, wire %d, act %d, inact %d, cleaned %d spec %d, zf %d, throt %d, compr %d, xpmapped %d\n %s discard act %d inact %d purgeable %d spec %d cleaned %d\n", |
7855 | pages, count_wire, count_active, count_inactive, count_cleaned, count_speculative, count_anonymous, count_throttled, count_compressor, hibernate_stats.cd_found_xpmapped, | |
7856 | discard_all ? "did" : "could", | |
7857 | count_discard_active, count_discard_inactive, count_discard_purgeable, count_discard_speculative, count_discard_cleaned); | |
b0d623f7 | 7858 | |
0a7de745 A |
7859 | if (hibernate_stats.cd_skipped_xpmapped) { |
7860 | HIBLOG("WARNING: hibernate_page_list_setall skipped %d xpmapped pages\n", hibernate_stats.cd_skipped_xpmapped); | |
7861 | } | |
8a3053a0 | 7862 | |
0a7de745 | 7863 | *pagesOut = pages - count_discard_active - count_discard_inactive - count_discard_purgeable - count_discard_speculative - count_discard_cleaned; |
316670eb | 7864 | |
0a7de745 A |
7865 | if (preflight && will_discard) { |
7866 | *pagesOut -= count_compressor + count_throttled + count_anonymous + count_inactive + count_cleaned + count_speculative + count_active; | |
7867 | } | |
39236c6e | 7868 | |
0a7de745 | 7869 | hibernation_vmqueues_inspection = FALSE; |
e8c3f781 | 7870 | |
fe8ab488 | 7871 | #if MACH_ASSERT || DEBUG |
0a7de745 A |
7872 | if (!preflight) { |
7873 | if (vm_page_local_q) { | |
f427ee49 | 7874 | zpercpu_foreach(lq, vm_page_local_q) { |
0a7de745 A |
7875 | VPL_UNLOCK(&lq->vpl_lock); |
7876 | } | |
7877 | } | |
7878 | vm_page_unlock_queues(); | |
316670eb | 7879 | } |
fe8ab488 | 7880 | #endif /* MACH_ASSERT || DEBUG */ |
0b4c1975 | 7881 | |
0a7de745 A |
7882 | if (preflight) { |
7883 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
7884 | vm_page_unlock_queues(); | |
7885 | vm_object_unlock(compressor_object); | |
7886 | } | |
db609669 | 7887 | |
0a7de745 | 7888 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 8) | DBG_FUNC_END, count_wire, *pagesOut, 0, 0, 0); |
b0d623f7 A |
7889 | } |
7890 | ||
7891 | void | |
7892 | hibernate_page_list_discard(hibernate_page_list_t * page_list) | |
7893 | { | |
0a7de745 A |
7894 | uint64_t start, end, nsec; |
7895 | vm_page_t m; | |
7896 | vm_page_t next; | |
7897 | uint32_t i; | |
7898 | uint32_t count_discard_active = 0; | |
7899 | uint32_t count_discard_inactive = 0; | |
7900 | uint32_t count_discard_purgeable = 0; | |
7901 | uint32_t count_discard_cleaned = 0; | |
7902 | uint32_t count_discard_speculative = 0; | |
b0d623f7 | 7903 | |
39236c6e | 7904 | |
fe8ab488 | 7905 | #if MACH_ASSERT || DEBUG |
0a7de745 | 7906 | vm_page_lock_queues(); |
316670eb | 7907 | if (vm_page_local_q) { |
f427ee49 | 7908 | zpercpu_foreach(lq, vm_page_local_q) { |
0a7de745 A |
7909 | VPL_LOCK(&lq->vpl_lock); |
7910 | } | |
316670eb | 7911 | } |
fe8ab488 | 7912 | #endif /* MACH_ASSERT || DEBUG */ |
316670eb | 7913 | |
0a7de745 A |
7914 | clock_get_uptime(&start); |
7915 | ||
7916 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_anonymous); | |
7917 | while (m && !vm_page_queue_end(&vm_page_queue_anonymous, (vm_page_queue_entry_t)m)) { | |
7918 | assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_INTERNAL_Q); | |
7919 | ||
7920 | next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7921 | if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) { | |
7922 | if (m->vmp_dirty) { | |
7923 | count_discard_purgeable++; | |
7924 | } else { | |
7925 | count_discard_inactive++; | |
7926 | } | |
7927 | hibernate_discard_page(m); | |
7928 | } | |
7929 | m = next; | |
7930 | } | |
7931 | ||
7932 | for (i = 0; i <= VM_PAGE_MAX_SPECULATIVE_AGE_Q; i++) { | |
7933 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_speculative[i].age_q); | |
7934 | while (m && !vm_page_queue_end(&vm_page_queue_speculative[i].age_q, (vm_page_queue_entry_t)m)) { | |
7935 | assert(m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q); | |
7936 | ||
7937 | next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7938 | if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) { | |
7939 | count_discard_speculative++; | |
7940 | hibernate_discard_page(m); | |
7941 | } | |
7942 | m = next; | |
7943 | } | |
7944 | } | |
7945 | ||
7946 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_inactive); | |
7947 | while (m && !vm_page_queue_end(&vm_page_queue_inactive, (vm_page_queue_entry_t)m)) { | |
7948 | assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_EXTERNAL_Q); | |
7949 | ||
7950 | next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7951 | if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) { | |
7952 | if (m->vmp_dirty) { | |
7953 | count_discard_purgeable++; | |
7954 | } else { | |
7955 | count_discard_inactive++; | |
7956 | } | |
7957 | hibernate_discard_page(m); | |
7958 | } | |
7959 | m = next; | |
7960 | } | |
7961 | /* XXX FBDP TODO: secluded queue */ | |
7962 | ||
7963 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_active); | |
7964 | while (m && !vm_page_queue_end(&vm_page_queue_active, (vm_page_queue_entry_t)m)) { | |
7965 | assert(m->vmp_q_state == VM_PAGE_ON_ACTIVE_Q); | |
7966 | ||
7967 | next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7968 | if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) { | |
7969 | if (m->vmp_dirty) { | |
7970 | count_discard_purgeable++; | |
7971 | } else { | |
7972 | count_discard_active++; | |
7973 | } | |
7974 | hibernate_discard_page(m); | |
7975 | } | |
7976 | m = next; | |
7977 | } | |
7978 | ||
7979 | m = (vm_page_t) vm_page_queue_first(&vm_page_queue_cleaned); | |
7980 | while (m && !vm_page_queue_end(&vm_page_queue_cleaned, (vm_page_queue_entry_t)m)) { | |
7981 | assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q); | |
7982 | ||
7983 | next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next); | |
7984 | if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) { | |
7985 | if (m->vmp_dirty) { | |
7986 | count_discard_purgeable++; | |
7987 | } else { | |
7988 | count_discard_cleaned++; | |
7989 | } | |
7990 | hibernate_discard_page(m); | |
7991 | } | |
7992 | m = next; | |
7993 | } | |
316670eb | 7994 | |
fe8ab488 | 7995 | #if MACH_ASSERT || DEBUG |
316670eb | 7996 | if (vm_page_local_q) { |
f427ee49 | 7997 | zpercpu_foreach(lq, vm_page_local_q) { |
0a7de745 A |
7998 | VPL_UNLOCK(&lq->vpl_lock); |
7999 | } | |
316670eb | 8000 | } |
0a7de745 | 8001 | vm_page_unlock_queues(); |
fe8ab488 | 8002 | #endif /* MACH_ASSERT || DEBUG */ |
316670eb | 8003 | |
0a7de745 A |
8004 | clock_get_uptime(&end); |
8005 | absolutetime_to_nanoseconds(end - start, &nsec); | |
8006 | HIBLOG("hibernate_page_list_discard time: %qd ms, discarded act %d inact %d purgeable %d spec %d cleaned %d\n", | |
8007 | nsec / 1000000ULL, | |
8008 | count_discard_active, count_discard_inactive, count_discard_purgeable, count_discard_speculative, count_discard_cleaned); | |
b0d623f7 A |
8009 | } |
8010 | ||
39236c6e | 8011 | boolean_t hibernate_paddr_map_inited = FALSE; |
0a7de745 A |
8012 | unsigned int hibernate_teardown_last_valid_compact_indx = -1; |
8013 | vm_page_t hibernate_rebuild_hash_list = NULL; | |
39236c6e | 8014 | |
0a7de745 A |
8015 | unsigned int hibernate_teardown_found_tabled_pages = 0; |
8016 | unsigned int hibernate_teardown_found_created_pages = 0; | |
8017 | unsigned int hibernate_teardown_found_free_pages = 0; | |
8018 | unsigned int hibernate_teardown_vm_page_free_count; | |
39236c6e A |
8019 | |
8020 | ||
8021 | struct ppnum_mapping { | |
0a7de745 A |
8022 | struct ppnum_mapping *ppnm_next; |
8023 | ppnum_t ppnm_base_paddr; | |
8024 | unsigned int ppnm_sindx; | |
8025 | unsigned int ppnm_eindx; | |
39236c6e A |
8026 | }; |
8027 | ||
0a7de745 A |
8028 | struct ppnum_mapping *ppnm_head; |
8029 | struct ppnum_mapping *ppnm_last_found = NULL; | |
39236c6e A |
8030 | |
8031 | ||
8032 | void | |
f427ee49 | 8033 | hibernate_create_paddr_map(void) |
39236c6e | 8034 | { |
0a7de745 A |
8035 | unsigned int i; |
8036 | ppnum_t next_ppnum_in_run = 0; | |
39236c6e A |
8037 | struct ppnum_mapping *ppnm = NULL; |
8038 | ||
8039 | if (hibernate_paddr_map_inited == FALSE) { | |
39236c6e | 8040 | for (i = 0; i < vm_pages_count; i++) { |
0a7de745 | 8041 | if (ppnm) { |
39236c6e | 8042 | ppnm->ppnm_eindx = i; |
0a7de745 | 8043 | } |
39236c6e | 8044 | |
39037602 | 8045 | if (ppnm == NULL || VM_PAGE_GET_PHYS_PAGE(&vm_pages[i]) != next_ppnum_in_run) { |
f427ee49 | 8046 | ppnm = zalloc_permanent_type(struct ppnum_mapping); |
39236c6e A |
8047 | |
8048 | ppnm->ppnm_next = ppnm_head; | |
8049 | ppnm_head = ppnm; | |
8050 | ||
8051 | ppnm->ppnm_sindx = i; | |
39037602 | 8052 | ppnm->ppnm_base_paddr = VM_PAGE_GET_PHYS_PAGE(&vm_pages[i]); |
39236c6e | 8053 | } |
39037602 | 8054 | next_ppnum_in_run = VM_PAGE_GET_PHYS_PAGE(&vm_pages[i]) + 1; |
39236c6e A |
8055 | } |
8056 | ppnm->ppnm_eindx++; | |
8057 | ||
8058 | hibernate_paddr_map_inited = TRUE; | |
8059 | } | |
8060 | } | |
8061 | ||
8062 | ppnum_t | |
8063 | hibernate_lookup_paddr(unsigned int indx) | |
8064 | { | |
8065 | struct ppnum_mapping *ppnm = NULL; | |
0a7de745 | 8066 | |
39236c6e A |
8067 | ppnm = ppnm_last_found; |
8068 | ||
8069 | if (ppnm) { | |
0a7de745 | 8070 | if (indx >= ppnm->ppnm_sindx && indx < ppnm->ppnm_eindx) { |
39236c6e | 8071 | goto done; |
0a7de745 | 8072 | } |
39236c6e A |
8073 | } |
8074 | for (ppnm = ppnm_head; ppnm; ppnm = ppnm->ppnm_next) { | |
39236c6e A |
8075 | if (indx >= ppnm->ppnm_sindx && indx < ppnm->ppnm_eindx) { |
8076 | ppnm_last_found = ppnm; | |
8077 | break; | |
8078 | } | |
8079 | } | |
0a7de745 | 8080 | if (ppnm == NULL) { |
39236c6e | 8081 | panic("hibernate_lookup_paddr of %d failed\n", indx); |
0a7de745 | 8082 | } |
39236c6e | 8083 | done: |
0a7de745 | 8084 | return ppnm->ppnm_base_paddr + (indx - ppnm->ppnm_sindx); |
39236c6e A |
8085 | } |
8086 | ||
8087 | ||
8088 | uint32_t | |
8089 | hibernate_mark_as_unneeded(addr64_t saddr, addr64_t eaddr, hibernate_page_list_t *page_list, hibernate_page_list_t *page_list_wired) | |
8090 | { | |
0a7de745 A |
8091 | addr64_t saddr_aligned; |
8092 | addr64_t eaddr_aligned; | |
8093 | addr64_t addr; | |
8094 | ppnum_t paddr; | |
8095 | unsigned int mark_as_unneeded_pages = 0; | |
39236c6e A |
8096 | |
8097 | saddr_aligned = (saddr + PAGE_MASK_64) & ~PAGE_MASK_64; | |
8098 | eaddr_aligned = eaddr & ~PAGE_MASK_64; | |
8099 | ||
8100 | for (addr = saddr_aligned; addr < eaddr_aligned; addr += PAGE_SIZE_64) { | |
39236c6e A |
8101 | paddr = pmap_find_phys(kernel_pmap, addr); |
8102 | ||
8103 | assert(paddr); | |
8104 | ||
0a7de745 | 8105 | hibernate_page_bitset(page_list, TRUE, paddr); |
39236c6e A |
8106 | hibernate_page_bitset(page_list_wired, TRUE, paddr); |
8107 | ||
8108 | mark_as_unneeded_pages++; | |
8109 | } | |
0a7de745 | 8110 | return mark_as_unneeded_pages; |
39236c6e A |
8111 | } |
8112 | ||
8113 | ||
8114 | void | |
8115 | hibernate_hash_insert_page(vm_page_t mem) | |
8116 | { | |
8117 | vm_page_bucket_t *bucket; | |
0a7de745 A |
8118 | int hash_id; |
8119 | vm_object_t m_object; | |
39037602 A |
8120 | |
8121 | m_object = VM_PAGE_OBJECT(mem); | |
39236c6e | 8122 | |
d9a64523 | 8123 | assert(mem->vmp_hashed); |
39037602 | 8124 | assert(m_object); |
d9a64523 | 8125 | assert(mem->vmp_offset != (vm_object_offset_t) -1); |
39236c6e A |
8126 | |
8127 | /* | |
8128 | * Insert it into the object_object/offset hash table | |
8129 | */ | |
d9a64523 | 8130 | hash_id = vm_page_hash(m_object, mem->vmp_offset); |
39236c6e A |
8131 | bucket = &vm_page_buckets[hash_id]; |
8132 | ||
d9a64523 | 8133 | mem->vmp_next_m = bucket->page_list; |
fe8ab488 | 8134 | bucket->page_list = VM_PAGE_PACK_PTR(mem); |
39236c6e A |
8135 | } |
8136 | ||
8137 | ||
8138 | void | |
8139 | hibernate_free_range(int sindx, int eindx) | |
8140 | { | |
0a7de745 A |
8141 | vm_page_t mem; |
8142 | unsigned int color; | |
39236c6e A |
8143 | |
8144 | while (sindx < eindx) { | |
8145 | mem = &vm_pages[sindx]; | |
8146 | ||
8147 | vm_page_init(mem, hibernate_lookup_paddr(sindx), FALSE); | |
8148 | ||
d9a64523 A |
8149 | mem->vmp_lopage = FALSE; |
8150 | mem->vmp_q_state = VM_PAGE_ON_FREE_Q; | |
39236c6e | 8151 | |
5ba3f43e A |
8152 | color = VM_PAGE_GET_COLOR(mem); |
8153 | #if defined(__x86_64__) | |
0a7de745 | 8154 | vm_page_queue_enter_clump(&vm_page_queue_free[color].qhead, mem); |
5ba3f43e | 8155 | #else |
0a7de745 | 8156 | vm_page_queue_enter(&vm_page_queue_free[color].qhead, mem, vmp_pageq); |
5ba3f43e | 8157 | #endif |
39236c6e A |
8158 | vm_page_free_count++; |
8159 | ||
8160 | sindx++; | |
8161 | } | |
8162 | } | |
8163 | ||
39236c6e A |
8164 | void |
8165 | hibernate_rebuild_vm_structs(void) | |
8166 | { | |
0a7de745 A |
8167 | int i, cindx, sindx, eindx; |
8168 | vm_page_t mem, tmem, mem_next; | |
8169 | AbsoluteTime startTime, endTime; | |
8170 | uint64_t nsec; | |
39236c6e | 8171 | |
0a7de745 | 8172 | if (hibernate_rebuild_needed == FALSE) { |
39236c6e | 8173 | return; |
0a7de745 | 8174 | } |
39236c6e | 8175 | |
5ba3f43e | 8176 | KDBG(IOKDBG_CODE(DBG_HIBERNATE, 13) | DBG_FUNC_START); |
39236c6e A |
8177 | HIBLOG("hibernate_rebuild started\n"); |
8178 | ||
8179 | clock_get_uptime(&startTime); | |
8180 | ||
f427ee49 | 8181 | pal_hib_rebuild_pmap_structs(); |
39236c6e A |
8182 | |
8183 | bzero(&vm_page_buckets[0], vm_page_bucket_count * sizeof(vm_page_bucket_t)); | |
8184 | eindx = vm_pages_count; | |
8185 | ||
5ba3f43e | 8186 | /* |
0a7de745 | 8187 | * Mark all the vm_pages[] that have not been initialized yet as being |
5ba3f43e | 8188 | * transient. This is needed to ensure that buddy page search is corrrect. |
0a7de745 | 8189 | * Without this random data in these vm_pages[] can trip the buddy search |
5ba3f43e | 8190 | */ |
0a7de745 | 8191 | for (i = hibernate_teardown_last_valid_compact_indx + 1; i < eindx; ++i) { |
d9a64523 | 8192 | vm_pages[i].vmp_q_state = VM_PAGE_NOT_ON_Q; |
0a7de745 | 8193 | } |
5ba3f43e | 8194 | |
39236c6e | 8195 | for (cindx = hibernate_teardown_last_valid_compact_indx; cindx >= 0; cindx--) { |
39236c6e | 8196 | mem = &vm_pages[cindx]; |
d9a64523 | 8197 | assert(mem->vmp_q_state != VM_PAGE_ON_FREE_Q); |
39236c6e A |
8198 | /* |
8199 | * hibernate_teardown_vm_structs leaves the location where | |
8200 | * this vm_page_t must be located in "next". | |
8201 | */ | |
d9a64523 A |
8202 | tmem = (vm_page_t)(VM_PAGE_UNPACK_PTR(mem->vmp_next_m)); |
8203 | mem->vmp_next_m = VM_PAGE_PACK_PTR(NULL); | |
39236c6e A |
8204 | |
8205 | sindx = (int)(tmem - &vm_pages[0]); | |
8206 | ||
8207 | if (mem != tmem) { | |
8208 | /* | |
8209 | * this vm_page_t was moved by hibernate_teardown_vm_structs, | |
8210 | * so move it back to its real location | |
8211 | */ | |
8212 | *tmem = *mem; | |
8213 | mem = tmem; | |
8214 | } | |
0a7de745 | 8215 | if (mem->vmp_hashed) { |
39236c6e | 8216 | hibernate_hash_insert_page(mem); |
0a7de745 | 8217 | } |
39236c6e A |
8218 | /* |
8219 | * the 'hole' between this vm_page_t and the previous | |
0a7de745 | 8220 | * vm_page_t we moved needs to be initialized as |
39236c6e A |
8221 | * a range of free vm_page_t's |
8222 | */ | |
8223 | hibernate_free_range(sindx + 1, eindx); | |
8224 | ||
8225 | eindx = sindx; | |
8226 | } | |
0a7de745 | 8227 | if (sindx) { |
39236c6e | 8228 | hibernate_free_range(0, sindx); |
0a7de745 | 8229 | } |
39236c6e A |
8230 | |
8231 | assert(vm_page_free_count == hibernate_teardown_vm_page_free_count); | |
8232 | ||
8233 | /* | |
15129b1c | 8234 | * process the list of vm_page_t's that were entered in the hash, |
0a7de745 | 8235 | * but were not located in the vm_pages arrary... these are |
39236c6e A |
8236 | * vm_page_t's that were created on the fly (i.e. fictitious) |
8237 | */ | |
8238 | for (mem = hibernate_rebuild_hash_list; mem; mem = mem_next) { | |
d9a64523 | 8239 | mem_next = (vm_page_t)(VM_PAGE_UNPACK_PTR(mem->vmp_next_m)); |
39236c6e | 8240 | |
d9a64523 | 8241 | mem->vmp_next_m = 0; |
39236c6e A |
8242 | hibernate_hash_insert_page(mem); |
8243 | } | |
8244 | hibernate_rebuild_hash_list = NULL; | |
8245 | ||
0a7de745 A |
8246 | clock_get_uptime(&endTime); |
8247 | SUB_ABSOLUTETIME(&endTime, &startTime); | |
8248 | absolutetime_to_nanoseconds(endTime, &nsec); | |
39236c6e A |
8249 | |
8250 | HIBLOG("hibernate_rebuild completed - took %qd msecs\n", nsec / 1000000ULL); | |
8251 | ||
8252 | hibernate_rebuild_needed = FALSE; | |
8253 | ||
5ba3f43e | 8254 | KDBG(IOKDBG_CODE(DBG_HIBERNATE, 13) | DBG_FUNC_END); |
39236c6e A |
8255 | } |
8256 | ||
39236c6e A |
8257 | uint32_t |
8258 | hibernate_teardown_vm_structs(hibernate_page_list_t *page_list, hibernate_page_list_t *page_list_wired) | |
8259 | { | |
0a7de745 A |
8260 | unsigned int i; |
8261 | unsigned int compact_target_indx; | |
8262 | vm_page_t mem, mem_next; | |
39236c6e | 8263 | vm_page_bucket_t *bucket; |
0a7de745 A |
8264 | unsigned int mark_as_unneeded_pages = 0; |
8265 | unsigned int unneeded_vm_page_bucket_pages = 0; | |
8266 | unsigned int unneeded_vm_pages_pages = 0; | |
8267 | unsigned int unneeded_pmap_pages = 0; | |
8268 | addr64_t start_of_unneeded = 0; | |
8269 | addr64_t end_of_unneeded = 0; | |
8270 | ||
39236c6e | 8271 | |
0a7de745 A |
8272 | if (hibernate_should_abort()) { |
8273 | return 0; | |
8274 | } | |
39236c6e | 8275 | |
5ba3f43e A |
8276 | hibernate_rebuild_needed = TRUE; |
8277 | ||
39236c6e | 8278 | HIBLOG("hibernate_teardown: wired_pages %d, free_pages %d, active_pages %d, inactive_pages %d, speculative_pages %d, cleaned_pages %d, compressor_pages %d\n", |
0a7de745 A |
8279 | vm_page_wire_count, vm_page_free_count, vm_page_active_count, vm_page_inactive_count, vm_page_speculative_count, |
8280 | vm_page_cleaned_count, compressor_object->resident_page_count); | |
39236c6e A |
8281 | |
8282 | for (i = 0; i < vm_page_bucket_count; i++) { | |
39236c6e A |
8283 | bucket = &vm_page_buckets[i]; |
8284 | ||
39037602 | 8285 | for (mem = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list)); mem != VM_PAGE_NULL; mem = mem_next) { |
d9a64523 | 8286 | assert(mem->vmp_hashed); |
39236c6e | 8287 | |
d9a64523 | 8288 | mem_next = (vm_page_t)(VM_PAGE_UNPACK_PTR(mem->vmp_next_m)); |
39236c6e A |
8289 | |
8290 | if (mem < &vm_pages[0] || mem >= &vm_pages[vm_pages_count]) { | |
d9a64523 | 8291 | mem->vmp_next_m = VM_PAGE_PACK_PTR(hibernate_rebuild_hash_list); |
39236c6e A |
8292 | hibernate_rebuild_hash_list = mem; |
8293 | } | |
8294 | } | |
8295 | } | |
8296 | unneeded_vm_page_bucket_pages = hibernate_mark_as_unneeded((addr64_t)&vm_page_buckets[0], (addr64_t)&vm_page_buckets[vm_page_bucket_count], page_list, page_list_wired); | |
8297 | mark_as_unneeded_pages += unneeded_vm_page_bucket_pages; | |
8298 | ||
8299 | hibernate_teardown_vm_page_free_count = vm_page_free_count; | |
8300 | ||
8301 | compact_target_indx = 0; | |
8302 | ||
8303 | for (i = 0; i < vm_pages_count; i++) { | |
39236c6e A |
8304 | mem = &vm_pages[i]; |
8305 | ||
d9a64523 | 8306 | if (mem->vmp_q_state == VM_PAGE_ON_FREE_Q) { |
39236c6e A |
8307 | unsigned int color; |
8308 | ||
d9a64523 A |
8309 | assert(mem->vmp_busy); |
8310 | assert(!mem->vmp_lopage); | |
39236c6e | 8311 | |
5ba3f43e | 8312 | color = VM_PAGE_GET_COLOR(mem); |
39037602 | 8313 | |
0a7de745 | 8314 | vm_page_queue_remove(&vm_page_queue_free[color].qhead, mem, vmp_pageq); |
39236c6e | 8315 | |
39037602 | 8316 | VM_PAGE_ZERO_PAGEQ_ENTRY(mem); |
39236c6e A |
8317 | |
8318 | vm_page_free_count--; | |
8319 | ||
8320 | hibernate_teardown_found_free_pages++; | |
8321 | ||
0a7de745 | 8322 | if (vm_pages[compact_target_indx].vmp_q_state != VM_PAGE_ON_FREE_Q) { |
39236c6e | 8323 | compact_target_indx = i; |
0a7de745 | 8324 | } |
39236c6e A |
8325 | } else { |
8326 | /* | |
8327 | * record this vm_page_t's original location | |
8328 | * we need this even if it doesn't get moved | |
8329 | * as an indicator to the rebuild function that | |
8330 | * we don't have to move it | |
8331 | */ | |
d9a64523 | 8332 | mem->vmp_next_m = VM_PAGE_PACK_PTR(mem); |
39236c6e | 8333 | |
d9a64523 | 8334 | if (vm_pages[compact_target_indx].vmp_q_state == VM_PAGE_ON_FREE_Q) { |
39236c6e A |
8335 | /* |
8336 | * we've got a hole to fill, so | |
8337 | * move this vm_page_t to it's new home | |
8338 | */ | |
8339 | vm_pages[compact_target_indx] = *mem; | |
d9a64523 | 8340 | mem->vmp_q_state = VM_PAGE_ON_FREE_Q; |
39236c6e A |
8341 | |
8342 | hibernate_teardown_last_valid_compact_indx = compact_target_indx; | |
8343 | compact_target_indx++; | |
0a7de745 | 8344 | } else { |
39236c6e | 8345 | hibernate_teardown_last_valid_compact_indx = i; |
0a7de745 | 8346 | } |
39236c6e A |
8347 | } |
8348 | } | |
0a7de745 A |
8349 | unneeded_vm_pages_pages = hibernate_mark_as_unneeded((addr64_t)&vm_pages[hibernate_teardown_last_valid_compact_indx + 1], |
8350 | (addr64_t)&vm_pages[vm_pages_count - 1], page_list, page_list_wired); | |
39236c6e A |
8351 | mark_as_unneeded_pages += unneeded_vm_pages_pages; |
8352 | ||
f427ee49 | 8353 | pal_hib_teardown_pmap_structs(&start_of_unneeded, &end_of_unneeded); |
39236c6e A |
8354 | |
8355 | if (start_of_unneeded) { | |
8356 | unneeded_pmap_pages = hibernate_mark_as_unneeded(start_of_unneeded, end_of_unneeded, page_list, page_list_wired); | |
8357 | mark_as_unneeded_pages += unneeded_pmap_pages; | |
8358 | } | |
8359 | HIBLOG("hibernate_teardown: mark_as_unneeded_pages %d, %d, %d\n", unneeded_vm_page_bucket_pages, unneeded_vm_pages_pages, unneeded_pmap_pages); | |
8360 | ||
0a7de745 | 8361 | return mark_as_unneeded_pages; |
39236c6e A |
8362 | } |
8363 | ||
8364 | ||
d1ecb069 A |
8365 | #endif /* HIBERNATION */ |
8366 | ||
b0d623f7 | 8367 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
1c79356b A |
8368 | |
8369 | #include <mach_vm_debug.h> | |
0a7de745 | 8370 | #if MACH_VM_DEBUG |
1c79356b A |
8371 | |
8372 | #include <mach_debug/hash_info.h> | |
8373 | #include <vm/vm_debug.h> | |
8374 | ||
8375 | /* | |
8376 | * Routine: vm_page_info | |
8377 | * Purpose: | |
8378 | * Return information about the global VP table. | |
8379 | * Fills the buffer with as much information as possible | |
8380 | * and returns the desired size of the buffer. | |
8381 | * Conditions: | |
8382 | * Nothing locked. The caller should provide | |
8383 | * possibly-pageable memory. | |
8384 | */ | |
8385 | ||
8386 | unsigned int | |
8387 | vm_page_info( | |
8388 | hash_info_bucket_t *info, | |
8389 | unsigned int count) | |
8390 | { | |
91447636 | 8391 | unsigned int i; |
0a7de745 | 8392 | lck_spin_t *bucket_lock; |
1c79356b | 8393 | |
0a7de745 | 8394 | if (vm_page_bucket_count < count) { |
1c79356b | 8395 | count = vm_page_bucket_count; |
0a7de745 | 8396 | } |
1c79356b A |
8397 | |
8398 | for (i = 0; i < count; i++) { | |
8399 | vm_page_bucket_t *bucket = &vm_page_buckets[i]; | |
8400 | unsigned int bucket_count = 0; | |
8401 | vm_page_t m; | |
8402 | ||
b0d623f7 | 8403 | bucket_lock = &vm_page_bucket_locks[i / BUCKETS_PER_LOCK]; |
0a7de745 | 8404 | lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket); |
b0d623f7 | 8405 | |
39037602 | 8406 | for (m = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list)); |
0a7de745 A |
8407 | m != VM_PAGE_NULL; |
8408 | m = (vm_page_t)(VM_PAGE_UNPACK_PTR(m->vmp_next_m))) { | |
1c79356b | 8409 | bucket_count++; |
0a7de745 | 8410 | } |
b0d623f7 A |
8411 | |
8412 | lck_spin_unlock(bucket_lock); | |
1c79356b A |
8413 | |
8414 | /* don't touch pageable memory while holding locks */ | |
8415 | info[i].hib_count = bucket_count; | |
8416 | } | |
8417 | ||
8418 | return vm_page_bucket_count; | |
8419 | } | |
0a7de745 | 8420 | #endif /* MACH_VM_DEBUG */ |
15129b1c A |
8421 | |
8422 | #if VM_PAGE_BUCKETS_CHECK | |
8423 | void | |
8424 | vm_page_buckets_check(void) | |
8425 | { | |
8426 | unsigned int i; | |
8427 | vm_page_t p; | |
8428 | unsigned int p_hash; | |
8429 | vm_page_bucket_t *bucket; | |
0a7de745 | 8430 | lck_spin_t *bucket_lock; |
15129b1c A |
8431 | |
8432 | if (!vm_page_buckets_check_ready) { | |
8433 | return; | |
8434 | } | |
8435 | ||
8436 | #if HIBERNATION | |
8437 | if (hibernate_rebuild_needed || | |
8438 | hibernate_rebuild_hash_list) { | |
8439 | panic("BUCKET_CHECK: hibernation in progress: " | |
0a7de745 A |
8440 | "rebuild_needed=%d rebuild_hash_list=%p\n", |
8441 | hibernate_rebuild_needed, | |
8442 | hibernate_rebuild_hash_list); | |
15129b1c A |
8443 | } |
8444 | #endif /* HIBERNATION */ | |
8445 | ||
8446 | #if VM_PAGE_FAKE_BUCKETS | |
8447 | char *cp; | |
8448 | for (cp = (char *) vm_page_fake_buckets_start; | |
0a7de745 A |
8449 | cp < (char *) vm_page_fake_buckets_end; |
8450 | cp++) { | |
15129b1c A |
8451 | if (*cp != 0x5a) { |
8452 | panic("BUCKET_CHECK: corruption at %p in fake buckets " | |
0a7de745 A |
8453 | "[0x%llx:0x%llx]\n", |
8454 | cp, | |
8455 | (uint64_t) vm_page_fake_buckets_start, | |
8456 | (uint64_t) vm_page_fake_buckets_end); | |
15129b1c A |
8457 | } |
8458 | } | |
8459 | #endif /* VM_PAGE_FAKE_BUCKETS */ | |
8460 | ||
8461 | for (i = 0; i < vm_page_bucket_count; i++) { | |
0a7de745 | 8462 | vm_object_t p_object; |
39037602 | 8463 | |
15129b1c | 8464 | bucket = &vm_page_buckets[i]; |
fe8ab488 | 8465 | if (!bucket->page_list) { |
15129b1c A |
8466 | continue; |
8467 | } | |
8468 | ||
8469 | bucket_lock = &vm_page_bucket_locks[i / BUCKETS_PER_LOCK]; | |
0a7de745 | 8470 | lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket); |
39037602 A |
8471 | p = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list)); |
8472 | ||
15129b1c | 8473 | while (p != VM_PAGE_NULL) { |
39037602 A |
8474 | p_object = VM_PAGE_OBJECT(p); |
8475 | ||
d9a64523 | 8476 | if (!p->vmp_hashed) { |
15129b1c | 8477 | panic("BUCKET_CHECK: page %p (%p,0x%llx) " |
0a7de745 A |
8478 | "hash %d in bucket %d at %p " |
8479 | "is not hashed\n", | |
8480 | p, p_object, p->vmp_offset, | |
8481 | p_hash, i, bucket); | |
15129b1c | 8482 | } |
d9a64523 | 8483 | p_hash = vm_page_hash(p_object, p->vmp_offset); |
15129b1c A |
8484 | if (p_hash != i) { |
8485 | panic("BUCKET_CHECK: corruption in bucket %d " | |
0a7de745 A |
8486 | "at %p: page %p object %p offset 0x%llx " |
8487 | "hash %d\n", | |
8488 | i, bucket, p, p_object, p->vmp_offset, | |
8489 | p_hash); | |
15129b1c | 8490 | } |
d9a64523 | 8491 | p = (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_next_m)); |
15129b1c A |
8492 | } |
8493 | lck_spin_unlock(bucket_lock); | |
8494 | } | |
8495 | ||
8496 | // printf("BUCKET_CHECK: checked buckets\n"); | |
8497 | } | |
8498 | #endif /* VM_PAGE_BUCKETS_CHECK */ | |
3e170ce0 A |
8499 | |
8500 | /* | |
8501 | * 'vm_fault_enter' will place newly created pages (zero-fill and COW) onto the | |
8502 | * local queues if they exist... its the only spot in the system where we add pages | |
8503 | * to those queues... once on those queues, those pages can only move to one of the | |
8504 | * global page queues or the free queues... they NEVER move from local q to local q. | |
8505 | * the 'local' state is stable when vm_page_queues_remove is called since we're behind | |
8506 | * the global vm_page_queue_lock at this point... we still need to take the local lock | |
8507 | * in case this operation is being run on a different CPU then the local queue's identity, | |
8508 | * but we don't have to worry about the page moving to a global queue or becoming wired | |
8509 | * while we're grabbing the local lock since those operations would require the global | |
8510 | * vm_page_queue_lock to be held, and we already own it. | |
8511 | * | |
8512 | * this is why its safe to utilze the wire_count field in the vm_page_t as the local_id... | |
8513 | * 'wired' and local are ALWAYS mutually exclusive conditions. | |
8514 | */ | |
39037602 A |
8515 | |
8516 | #if CONFIG_BACKGROUND_QUEUE | |
8517 | void | |
8518 | vm_page_queues_remove(vm_page_t mem, boolean_t remove_from_backgroundq) | |
8519 | #else | |
3e170ce0 | 8520 | void |
39037602 A |
8521 | vm_page_queues_remove(vm_page_t mem, boolean_t __unused remove_from_backgroundq) |
8522 | #endif | |
3e170ce0 | 8523 | { |
0a7de745 A |
8524 | boolean_t was_pageable = TRUE; |
8525 | vm_object_t m_object; | |
3e170ce0 | 8526 | |
39037602 A |
8527 | m_object = VM_PAGE_OBJECT(mem); |
8528 | ||
8529 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
8530 | ||
0a7de745 | 8531 | if (mem->vmp_q_state == VM_PAGE_NOT_ON_Q) { |
d9a64523 | 8532 | assert(mem->vmp_pageq.next == 0 && mem->vmp_pageq.prev == 0); |
39037602 | 8533 | #if CONFIG_BACKGROUND_QUEUE |
743345f9 A |
8534 | if (remove_from_backgroundq == TRUE) { |
8535 | vm_page_remove_from_backgroundq(mem); | |
39037602 | 8536 | } |
d9a64523 A |
8537 | if (mem->vmp_on_backgroundq) { |
8538 | assert(mem->vmp_backgroundq.next != 0); | |
8539 | assert(mem->vmp_backgroundq.prev != 0); | |
743345f9 | 8540 | } else { |
d9a64523 A |
8541 | assert(mem->vmp_backgroundq.next == 0); |
8542 | assert(mem->vmp_backgroundq.prev == 0); | |
743345f9 A |
8543 | } |
8544 | #endif /* CONFIG_BACKGROUND_QUEUE */ | |
39037602 A |
8545 | return; |
8546 | } | |
d190cdc3 | 8547 | |
0a7de745 | 8548 | if (mem->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) { |
d9a64523 | 8549 | assert(mem->vmp_pageq.next == 0 && mem->vmp_pageq.prev == 0); |
39037602 | 8550 | #if CONFIG_BACKGROUND_QUEUE |
d9a64523 | 8551 | assert(mem->vmp_backgroundq.next == 0 && |
0a7de745 A |
8552 | mem->vmp_backgroundq.prev == 0 && |
8553 | mem->vmp_on_backgroundq == FALSE); | |
39037602 A |
8554 | #endif |
8555 | return; | |
8556 | } | |
d9a64523 | 8557 | if (mem->vmp_q_state == VM_PAGE_IS_WIRED) { |
39037602 A |
8558 | /* |
8559 | * might put these guys on a list for debugging purposes | |
8560 | * if we do, we'll need to remove this assert | |
8561 | */ | |
d9a64523 | 8562 | assert(mem->vmp_pageq.next == 0 && mem->vmp_pageq.prev == 0); |
39037602 | 8563 | #if CONFIG_BACKGROUND_QUEUE |
d9a64523 | 8564 | assert(mem->vmp_backgroundq.next == 0 && |
0a7de745 A |
8565 | mem->vmp_backgroundq.prev == 0 && |
8566 | mem->vmp_on_backgroundq == FALSE); | |
39037602 A |
8567 | #endif |
8568 | return; | |
8569 | } | |
8570 | ||
8571 | assert(m_object != compressor_object); | |
8572 | assert(m_object != kernel_object); | |
8573 | assert(m_object != vm_submap_object); | |
d9a64523 | 8574 | assert(!mem->vmp_fictitious); |
39037602 | 8575 | |
0a7de745 | 8576 | switch (mem->vmp_q_state) { |
39037602 A |
8577 | case VM_PAGE_ON_ACTIVE_LOCAL_Q: |
8578 | { | |
0a7de745 | 8579 | struct vpl *lq; |
39037602 | 8580 | |
f427ee49 | 8581 | lq = zpercpu_get_cpu(vm_page_local_q, mem->vmp_local_id); |
3e170ce0 | 8582 | VPL_LOCK(&lq->vpl_lock); |
0a7de745 | 8583 | vm_page_queue_remove(&lq->vpl_queue, mem, vmp_pageq); |
d9a64523 | 8584 | mem->vmp_local_id = 0; |
3e170ce0 | 8585 | lq->vpl_count--; |
39037602 | 8586 | if (m_object->internal) { |
3e170ce0 A |
8587 | lq->vpl_internal_count--; |
8588 | } else { | |
8589 | lq->vpl_external_count--; | |
8590 | } | |
8591 | VPL_UNLOCK(&lq->vpl_lock); | |
8592 | was_pageable = FALSE; | |
39037602 | 8593 | break; |
3e170ce0 | 8594 | } |
39037602 A |
8595 | case VM_PAGE_ON_ACTIVE_Q: |
8596 | { | |
0a7de745 | 8597 | vm_page_queue_remove(&vm_page_queue_active, mem, vmp_pageq); |
3e170ce0 | 8598 | vm_page_active_count--; |
39037602 | 8599 | break; |
3e170ce0 A |
8600 | } |
8601 | ||
39037602 A |
8602 | case VM_PAGE_ON_INACTIVE_INTERNAL_Q: |
8603 | { | |
8604 | assert(m_object->internal == TRUE); | |
8605 | ||
3e170ce0 | 8606 | vm_page_inactive_count--; |
0a7de745 | 8607 | vm_page_queue_remove(&vm_page_queue_anonymous, mem, vmp_pageq); |
39037602 | 8608 | vm_page_anonymous_count--; |
d9a64523 | 8609 | |
39037602 | 8610 | vm_purgeable_q_advance_all(); |
d9a64523 | 8611 | vm_page_balance_inactive(3); |
39037602 A |
8612 | break; |
8613 | } | |
8614 | ||
8615 | case VM_PAGE_ON_INACTIVE_EXTERNAL_Q: | |
8616 | { | |
8617 | assert(m_object->internal == FALSE); | |
8618 | ||
8619 | vm_page_inactive_count--; | |
0a7de745 | 8620 | vm_page_queue_remove(&vm_page_queue_inactive, mem, vmp_pageq); |
39037602 | 8621 | vm_purgeable_q_advance_all(); |
d9a64523 | 8622 | vm_page_balance_inactive(3); |
39037602 A |
8623 | break; |
8624 | } | |
8625 | ||
8626 | case VM_PAGE_ON_INACTIVE_CLEANED_Q: | |
8627 | { | |
8628 | assert(m_object->internal == FALSE); | |
8629 | ||
8630 | vm_page_inactive_count--; | |
0a7de745 | 8631 | vm_page_queue_remove(&vm_page_queue_cleaned, mem, vmp_pageq); |
39037602 | 8632 | vm_page_cleaned_count--; |
d9a64523 | 8633 | vm_page_balance_inactive(3); |
39037602 A |
8634 | break; |
8635 | } | |
8636 | ||
8637 | case VM_PAGE_ON_THROTTLED_Q: | |
8638 | { | |
8639 | assert(m_object->internal == TRUE); | |
8640 | ||
0a7de745 | 8641 | vm_page_queue_remove(&vm_page_queue_throttled, mem, vmp_pageq); |
3e170ce0 A |
8642 | vm_page_throttled_count--; |
8643 | was_pageable = FALSE; | |
39037602 | 8644 | break; |
3e170ce0 A |
8645 | } |
8646 | ||
39037602 A |
8647 | case VM_PAGE_ON_SPECULATIVE_Q: |
8648 | { | |
8649 | assert(m_object->internal == FALSE); | |
8650 | ||
0a7de745 | 8651 | vm_page_remque(&mem->vmp_pageq); |
3e170ce0 | 8652 | vm_page_speculative_count--; |
d9a64523 | 8653 | vm_page_balance_inactive(3); |
39037602 A |
8654 | break; |
8655 | } | |
8656 | ||
8657 | #if CONFIG_SECLUDED_MEMORY | |
8658 | case VM_PAGE_ON_SECLUDED_Q: | |
8659 | { | |
0a7de745 | 8660 | vm_page_queue_remove(&vm_page_queue_secluded, mem, vmp_pageq); |
39037602 | 8661 | vm_page_secluded_count--; |
cb323159 | 8662 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); |
39037602 A |
8663 | if (m_object == VM_OBJECT_NULL) { |
8664 | vm_page_secluded_count_free--; | |
8665 | was_pageable = FALSE; | |
8666 | } else { | |
8667 | assert(!m_object->internal); | |
8668 | vm_page_secluded_count_inuse--; | |
8669 | was_pageable = FALSE; | |
8670 | // was_pageable = TRUE; | |
8671 | } | |
8672 | break; | |
8673 | } | |
8674 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
8675 | ||
8676 | default: | |
8677 | { | |
8678 | /* | |
d9a64523 | 8679 | * if (mem->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) |
0a7de745 A |
8680 | * NOTE: vm_page_queues_remove does not deal with removing pages from the pageout queue... |
8681 | * the caller is responsible for determing if the page is on that queue, and if so, must | |
8682 | * either first remove it (it needs both the page queues lock and the object lock to do | |
8683 | * this via vm_pageout_steal_laundry), or avoid the call to vm_page_queues_remove | |
39037602 A |
8684 | * |
8685 | * we also don't expect to encounter VM_PAGE_ON_FREE_Q, VM_PAGE_ON_FREE_LOCAL_Q, VM_PAGE_ON_FREE_LOPAGE_Q | |
8686 | * or any of the undefined states | |
8687 | */ | |
d9a64523 | 8688 | panic("vm_page_queues_remove - bad page q_state (%p, %d)\n", mem, mem->vmp_q_state); |
39037602 | 8689 | break; |
3e170ce0 | 8690 | } |
3e170ce0 | 8691 | } |
39037602 | 8692 | VM_PAGE_ZERO_PAGEQ_ENTRY(mem); |
d9a64523 | 8693 | mem->vmp_q_state = VM_PAGE_NOT_ON_Q; |
3e170ce0 | 8694 | |
39037602 | 8695 | #if CONFIG_BACKGROUND_QUEUE |
0a7de745 | 8696 | if (remove_from_backgroundq == TRUE) { |
39037602 | 8697 | vm_page_remove_from_backgroundq(mem); |
0a7de745 | 8698 | } |
39037602 | 8699 | #endif |
3e170ce0 | 8700 | if (was_pageable) { |
39037602 | 8701 | if (m_object->internal) { |
3e170ce0 A |
8702 | vm_page_pageable_internal_count--; |
8703 | } else { | |
8704 | vm_page_pageable_external_count--; | |
8705 | } | |
8706 | } | |
8707 | } | |
8708 | ||
8709 | void | |
8710 | vm_page_remove_internal(vm_page_t page) | |
8711 | { | |
39037602 | 8712 | vm_object_t __object = VM_PAGE_OBJECT(page); |
3e170ce0 | 8713 | if (page == __object->memq_hint) { |
0a7de745 A |
8714 | vm_page_t __new_hint; |
8715 | vm_page_queue_entry_t __qe; | |
d9a64523 | 8716 | __qe = (vm_page_queue_entry_t)vm_page_queue_next(&page->vmp_listq); |
39037602 | 8717 | if (vm_page_queue_end(&__object->memq, __qe)) { |
d9a64523 | 8718 | __qe = (vm_page_queue_entry_t)vm_page_queue_prev(&page->vmp_listq); |
39037602 | 8719 | if (vm_page_queue_end(&__object->memq, __qe)) { |
3e170ce0 A |
8720 | __qe = NULL; |
8721 | } | |
8722 | } | |
39037602 | 8723 | __new_hint = (vm_page_t)((uintptr_t) __qe); |
3e170ce0 A |
8724 | __object->memq_hint = __new_hint; |
8725 | } | |
0a7de745 | 8726 | vm_page_queue_remove(&__object->memq, page, vmp_listq); |
39037602 A |
8727 | #if CONFIG_SECLUDED_MEMORY |
8728 | if (__object->eligible_for_secluded) { | |
8729 | vm_page_secluded.eligible_for_secluded--; | |
8730 | } | |
8731 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
3e170ce0 A |
8732 | } |
8733 | ||
8734 | void | |
8735 | vm_page_enqueue_inactive(vm_page_t mem, boolean_t first) | |
8736 | { | |
0a7de745 | 8737 | vm_object_t m_object; |
39037602 A |
8738 | |
8739 | m_object = VM_PAGE_OBJECT(mem); | |
8740 | ||
8741 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 A |
8742 | assert(!mem->vmp_fictitious); |
8743 | assert(!mem->vmp_laundry); | |
8744 | assert(mem->vmp_q_state == VM_PAGE_NOT_ON_Q); | |
3e170ce0 | 8745 | vm_page_check_pageable_safe(mem); |
39037602 | 8746 | |
39037602 | 8747 | if (m_object->internal) { |
d9a64523 | 8748 | mem->vmp_q_state = VM_PAGE_ON_INACTIVE_INTERNAL_Q; |
39037602 | 8749 | |
0a7de745 A |
8750 | if (first == TRUE) { |
8751 | vm_page_queue_enter_first(&vm_page_queue_anonymous, mem, vmp_pageq); | |
8752 | } else { | |
8753 | vm_page_queue_enter(&vm_page_queue_anonymous, mem, vmp_pageq); | |
8754 | } | |
39037602 | 8755 | |
3e170ce0 A |
8756 | vm_page_anonymous_count++; |
8757 | vm_page_pageable_internal_count++; | |
8758 | } else { | |
d9a64523 | 8759 | mem->vmp_q_state = VM_PAGE_ON_INACTIVE_EXTERNAL_Q; |
39037602 | 8760 | |
0a7de745 A |
8761 | if (first == TRUE) { |
8762 | vm_page_queue_enter_first(&vm_page_queue_inactive, mem, vmp_pageq); | |
8763 | } else { | |
8764 | vm_page_queue_enter(&vm_page_queue_inactive, mem, vmp_pageq); | |
8765 | } | |
39037602 | 8766 | |
3e170ce0 A |
8767 | vm_page_pageable_external_count++; |
8768 | } | |
3e170ce0 A |
8769 | vm_page_inactive_count++; |
8770 | token_new_pagecount++; | |
39037602 A |
8771 | |
8772 | #if CONFIG_BACKGROUND_QUEUE | |
0a7de745 | 8773 | if (mem->vmp_in_background) { |
39037602 | 8774 | vm_page_add_to_backgroundq(mem, FALSE); |
0a7de745 | 8775 | } |
39037602 A |
8776 | #endif |
8777 | } | |
8778 | ||
8779 | void | |
8780 | vm_page_enqueue_active(vm_page_t mem, boolean_t first) | |
8781 | { | |
0a7de745 | 8782 | vm_object_t m_object; |
39037602 A |
8783 | |
8784 | m_object = VM_PAGE_OBJECT(mem); | |
8785 | ||
8786 | LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 A |
8787 | assert(!mem->vmp_fictitious); |
8788 | assert(!mem->vmp_laundry); | |
8789 | assert(mem->vmp_q_state == VM_PAGE_NOT_ON_Q); | |
39037602 A |
8790 | vm_page_check_pageable_safe(mem); |
8791 | ||
d9a64523 | 8792 | mem->vmp_q_state = VM_PAGE_ON_ACTIVE_Q; |
0a7de745 A |
8793 | if (first == TRUE) { |
8794 | vm_page_queue_enter_first(&vm_page_queue_active, mem, vmp_pageq); | |
8795 | } else { | |
8796 | vm_page_queue_enter(&vm_page_queue_active, mem, vmp_pageq); | |
8797 | } | |
39037602 A |
8798 | vm_page_active_count++; |
8799 | ||
8800 | if (m_object->internal) { | |
8801 | vm_page_pageable_internal_count++; | |
8802 | } else { | |
8803 | vm_page_pageable_external_count++; | |
8804 | } | |
8805 | ||
8806 | #if CONFIG_BACKGROUND_QUEUE | |
0a7de745 | 8807 | if (mem->vmp_in_background) { |
39037602 | 8808 | vm_page_add_to_backgroundq(mem, FALSE); |
0a7de745 | 8809 | } |
39037602 | 8810 | #endif |
d9a64523 | 8811 | vm_page_balance_inactive(3); |
3e170ce0 A |
8812 | } |
8813 | ||
8814 | /* | |
8815 | * Pages from special kernel objects shouldn't | |
8816 | * be placed on pageable queues. | |
8817 | */ | |
8818 | void | |
8819 | vm_page_check_pageable_safe(vm_page_t page) | |
8820 | { | |
0a7de745 | 8821 | vm_object_t page_object; |
39037602 A |
8822 | |
8823 | page_object = VM_PAGE_OBJECT(page); | |
8824 | ||
8825 | if (page_object == kernel_object) { | |
3e170ce0 | 8826 | panic("vm_page_check_pageable_safe: trying to add page" \ |
0a7de745 | 8827 | "from kernel object (%p) to pageable queue", kernel_object); |
3e170ce0 A |
8828 | } |
8829 | ||
39037602 | 8830 | if (page_object == compressor_object) { |
3e170ce0 | 8831 | panic("vm_page_check_pageable_safe: trying to add page" \ |
0a7de745 | 8832 | "from compressor object (%p) to pageable queue", compressor_object); |
3e170ce0 A |
8833 | } |
8834 | ||
39037602 | 8835 | if (page_object == vm_submap_object) { |
3e170ce0 | 8836 | panic("vm_page_check_pageable_safe: trying to add page" \ |
0a7de745 | 8837 | "from submap object (%p) to pageable queue", vm_submap_object); |
3e170ce0 A |
8838 | } |
8839 | } | |
8840 | ||
8841 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
0a7de745 A |
8842 | * wired page diagnose |
8843 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
3e170ce0 A |
8844 | |
8845 | #include <libkern/OSKextLibPrivate.h> | |
8846 | ||
0a7de745 | 8847 | #define KA_SIZE(namelen, subtotalscount) \ |
5ba3f43e A |
8848 | (sizeof(struct vm_allocation_site) + (namelen) + 1 + ((subtotalscount) * sizeof(struct vm_allocation_total))) |
8849 | ||
0a7de745 | 8850 | #define KA_NAME(alloc) \ |
5ba3f43e A |
8851 | ((char *)(&(alloc)->subtotals[(alloc->subtotalscount)])) |
8852 | ||
0a7de745 | 8853 | #define KA_NAME_LEN(alloc) \ |
5ba3f43e | 8854 | (VM_TAG_NAME_LEN_MAX & (alloc->flags >> VM_TAG_NAME_LEN_SHIFT)) |
3e170ce0 | 8855 | |
0a7de745 | 8856 | vm_tag_t |
3e170ce0 A |
8857 | vm_tag_bt(void) |
8858 | { | |
0a7de745 A |
8859 | uintptr_t* frameptr; |
8860 | uintptr_t* frameptr_next; | |
8861 | uintptr_t retaddr; | |
8862 | uintptr_t kstackb, kstackt; | |
8863 | const vm_allocation_site_t * site; | |
8864 | thread_t cthread; | |
8865 | kern_allocation_name_t name; | |
8866 | ||
8867 | cthread = current_thread(); | |
8868 | if (__improbable(cthread == NULL)) { | |
8869 | return VM_KERN_MEMORY_OSFMK; | |
8870 | } | |
8871 | ||
8872 | if ((name = thread_get_kernel_state(cthread)->allocation_name)) { | |
8873 | if (!name->tag) { | |
8874 | vm_tag_alloc(name); | |
8875 | } | |
8876 | return name->tag; | |
8877 | } | |
8878 | ||
8879 | kstackb = cthread->kernel_stack; | |
8880 | kstackt = kstackb + kernel_stack_size; | |
8881 | ||
8882 | /* Load stack frame pointer (EBP on x86) into frameptr */ | |
8883 | frameptr = __builtin_frame_address(0); | |
8884 | site = NULL; | |
8885 | while (frameptr != NULL) { | |
8886 | /* Verify thread stack bounds */ | |
8887 | if (((uintptr_t)(frameptr + 2) > kstackt) || ((uintptr_t)frameptr < kstackb)) { | |
8888 | break; | |
8889 | } | |
8890 | ||
8891 | /* Next frame pointer is pointed to by the previous one */ | |
8892 | frameptr_next = (uintptr_t*) *frameptr; | |
8893 | ||
8894 | /* Pull return address from one spot above the frame pointer */ | |
8895 | retaddr = *(frameptr + 1); | |
8896 | ||
cb323159 A |
8897 | #if defined(HAS_APPLE_PAC) |
8898 | retaddr = (uintptr_t) ptrauth_strip((void *)retaddr, ptrauth_key_return_address); | |
8899 | #endif | |
0a7de745 A |
8900 | |
8901 | if (((retaddr < vm_kernel_builtinkmod_text_end) && (retaddr >= vm_kernel_builtinkmod_text)) | |
8902 | || (retaddr < vm_kernel_stext) || (retaddr > vm_kernel_top)) { | |
8903 | site = OSKextGetAllocationSiteForCaller(retaddr); | |
8904 | break; | |
8905 | } | |
8906 | frameptr = frameptr_next; | |
3e170ce0 | 8907 | } |
5ba3f43e | 8908 | |
0a7de745 | 8909 | return site ? site->tag : VM_KERN_MEMORY_NONE; |
3e170ce0 A |
8910 | } |
8911 | ||
0a7de745 | 8912 | static uint64_t free_tag_bits[VM_MAX_TAG_VALUE / 64]; |
3e170ce0 A |
8913 | |
8914 | void | |
5ba3f43e | 8915 | vm_tag_alloc_locked(vm_allocation_site_t * site, vm_allocation_site_t ** releasesiteP) |
3e170ce0 | 8916 | { |
0a7de745 A |
8917 | vm_tag_t tag; |
8918 | uint64_t avail; | |
8919 | uint32_t idx; | |
8920 | vm_allocation_site_t * prev; | |
3e170ce0 | 8921 | |
0a7de745 A |
8922 | if (site->tag) { |
8923 | return; | |
8924 | } | |
3e170ce0 | 8925 | |
0a7de745 A |
8926 | idx = 0; |
8927 | while (TRUE) { | |
5ba3f43e | 8928 | avail = free_tag_bits[idx]; |
0a7de745 | 8929 | if (avail) { |
f427ee49 | 8930 | tag = (vm_tag_t)__builtin_clzll(avail); |
0a7de745 A |
8931 | avail &= ~(1ULL << (63 - tag)); |
8932 | free_tag_bits[idx] = avail; | |
8933 | tag += (idx << 6); | |
8934 | break; | |
5ba3f43e A |
8935 | } |
8936 | idx++; | |
0a7de745 A |
8937 | if (idx >= ARRAY_COUNT(free_tag_bits)) { |
8938 | for (idx = 0; idx < ARRAY_COUNT(vm_allocation_sites); idx++) { | |
5ba3f43e | 8939 | prev = vm_allocation_sites[idx]; |
0a7de745 A |
8940 | if (!prev) { |
8941 | continue; | |
8942 | } | |
8943 | if (!KA_NAME_LEN(prev)) { | |
8944 | continue; | |
8945 | } | |
8946 | if (!prev->tag) { | |
8947 | continue; | |
8948 | } | |
8949 | if (prev->total) { | |
8950 | continue; | |
8951 | } | |
8952 | if (1 != prev->refcount) { | |
8953 | continue; | |
8954 | } | |
5ba3f43e A |
8955 | |
8956 | assert(idx == prev->tag); | |
f427ee49 | 8957 | tag = (vm_tag_t)idx; |
5ba3f43e A |
8958 | prev->tag = VM_KERN_MEMORY_NONE; |
8959 | *releasesiteP = prev; | |
8960 | break; | |
0a7de745 A |
8961 | } |
8962 | if (idx >= ARRAY_COUNT(vm_allocation_sites)) { | |
5ba3f43e A |
8963 | tag = VM_KERN_MEMORY_ANY; |
8964 | } | |
0a7de745 | 8965 | break; |
5ba3f43e | 8966 | } |
0a7de745 A |
8967 | } |
8968 | site->tag = tag; | |
5ba3f43e | 8969 | |
0a7de745 | 8970 | OSAddAtomic16(1, &site->refcount); |
5ba3f43e | 8971 | |
0a7de745 A |
8972 | if (VM_KERN_MEMORY_ANY != tag) { |
8973 | vm_allocation_sites[tag] = site; | |
8974 | } | |
5ba3f43e | 8975 | |
0a7de745 A |
8976 | if (tag > vm_allocation_tag_highest) { |
8977 | vm_allocation_tag_highest = tag; | |
8978 | } | |
3e170ce0 A |
8979 | } |
8980 | ||
8981 | static void | |
8982 | vm_tag_free_locked(vm_tag_t tag) | |
8983 | { | |
0a7de745 A |
8984 | uint64_t avail; |
8985 | uint32_t idx; | |
8986 | uint64_t bit; | |
3e170ce0 | 8987 | |
0a7de745 A |
8988 | if (VM_KERN_MEMORY_ANY == tag) { |
8989 | return; | |
8990 | } | |
3e170ce0 | 8991 | |
0a7de745 A |
8992 | idx = (tag >> 6); |
8993 | avail = free_tag_bits[idx]; | |
8994 | tag &= 63; | |
8995 | bit = (1ULL << (63 - tag)); | |
8996 | assert(!(avail & bit)); | |
8997 | free_tag_bits[idx] = (avail | bit); | |
3e170ce0 A |
8998 | } |
8999 | ||
9000 | static void | |
9001 | vm_tag_init(void) | |
9002 | { | |
0a7de745 A |
9003 | vm_tag_t tag; |
9004 | for (tag = VM_KERN_MEMORY_FIRST_DYNAMIC; tag < VM_KERN_MEMORY_ANY; tag++) { | |
9005 | vm_tag_free_locked(tag); | |
9006 | } | |
5ba3f43e | 9007 | |
0a7de745 A |
9008 | for (tag = VM_KERN_MEMORY_ANY + 1; tag < VM_MAX_TAG_VALUE; tag++) { |
9009 | vm_tag_free_locked(tag); | |
9010 | } | |
3e170ce0 A |
9011 | } |
9012 | ||
9013 | vm_tag_t | |
9014 | vm_tag_alloc(vm_allocation_site_t * site) | |
9015 | { | |
0a7de745 A |
9016 | vm_tag_t tag; |
9017 | vm_allocation_site_t * releasesite; | |
3e170ce0 | 9018 | |
0a7de745 | 9019 | if (VM_TAG_BT & site->flags) { |
5ba3f43e | 9020 | tag = vm_tag_bt(); |
0a7de745 A |
9021 | if (VM_KERN_MEMORY_NONE != tag) { |
9022 | return tag; | |
9023 | } | |
9024 | } | |
3e170ce0 | 9025 | |
0a7de745 | 9026 | if (!site->tag) { |
5ba3f43e A |
9027 | releasesite = NULL; |
9028 | lck_spin_lock(&vm_allocation_sites_lock); | |
9029 | vm_tag_alloc_locked(site, &releasesite); | |
9030 | lck_spin_unlock(&vm_allocation_sites_lock); | |
0a7de745 A |
9031 | if (releasesite) { |
9032 | kern_allocation_name_release(releasesite); | |
9033 | } | |
9034 | } | |
3e170ce0 | 9035 | |
0a7de745 | 9036 | return site->tag; |
3e170ce0 A |
9037 | } |
9038 | ||
5ba3f43e A |
9039 | void |
9040 | vm_tag_update_size(vm_tag_t tag, int64_t delta) | |
9041 | { | |
0a7de745 A |
9042 | vm_allocation_site_t * allocation; |
9043 | uint64_t prior; | |
5ba3f43e | 9044 | |
0a7de745 A |
9045 | assert(VM_KERN_MEMORY_NONE != tag); |
9046 | assert(tag < VM_MAX_TAG_VALUE); | |
5ba3f43e | 9047 | |
0a7de745 A |
9048 | allocation = vm_allocation_sites[tag]; |
9049 | assert(allocation); | |
5ba3f43e | 9050 | |
0a7de745 | 9051 | if (delta < 0) { |
5ba3f43e | 9052 | assertf(allocation->total >= ((uint64_t)-delta), "tag %d, site %p", tag, allocation); |
0a7de745 A |
9053 | } |
9054 | prior = OSAddAtomic64(delta, &allocation->total); | |
5ba3f43e A |
9055 | |
9056 | #if DEBUG || DEVELOPMENT | |
9057 | ||
0a7de745 | 9058 | uint64_t new, peak; |
5ba3f43e | 9059 | new = prior + delta; |
0a7de745 A |
9060 | do{ |
9061 | peak = allocation->peak; | |
9062 | if (new <= peak) { | |
9063 | break; | |
9064 | } | |
9065 | }while (!OSCompareAndSwap64(peak, new, &allocation->peak)); | |
5ba3f43e A |
9066 | |
9067 | #endif /* DEBUG || DEVELOPMENT */ | |
9068 | ||
0a7de745 A |
9069 | if (tag < VM_KERN_MEMORY_FIRST_DYNAMIC) { |
9070 | return; | |
9071 | } | |
5ba3f43e | 9072 | |
0a7de745 A |
9073 | if (!prior && !allocation->tag) { |
9074 | vm_tag_alloc(allocation); | |
9075 | } | |
5ba3f43e A |
9076 | } |
9077 | ||
9078 | void | |
9079 | kern_allocation_update_size(kern_allocation_name_t allocation, int64_t delta) | |
9080 | { | |
0a7de745 | 9081 | uint64_t prior; |
5ba3f43e | 9082 | |
0a7de745 | 9083 | if (delta < 0) { |
5ba3f43e | 9084 | assertf(allocation->total >= ((uint64_t)-delta), "name %p", allocation); |
0a7de745 A |
9085 | } |
9086 | prior = OSAddAtomic64(delta, &allocation->total); | |
5ba3f43e A |
9087 | |
9088 | #if DEBUG || DEVELOPMENT | |
9089 | ||
0a7de745 | 9090 | uint64_t new, peak; |
5ba3f43e | 9091 | new = prior + delta; |
0a7de745 A |
9092 | do{ |
9093 | peak = allocation->peak; | |
9094 | if (new <= peak) { | |
9095 | break; | |
9096 | } | |
9097 | }while (!OSCompareAndSwap64(peak, new, &allocation->peak)); | |
5ba3f43e A |
9098 | |
9099 | #endif /* DEBUG || DEVELOPMENT */ | |
9100 | ||
0a7de745 A |
9101 | if (!prior && !allocation->tag) { |
9102 | vm_tag_alloc(allocation); | |
9103 | } | |
5ba3f43e A |
9104 | } |
9105 | ||
9106 | #if VM_MAX_TAG_ZONES | |
9107 | ||
9108 | void | |
9109 | vm_allocation_zones_init(void) | |
9110 | { | |
0a7de745 A |
9111 | kern_return_t ret; |
9112 | vm_offset_t addr; | |
5ba3f43e A |
9113 | vm_size_t size; |
9114 | ||
0a7de745 A |
9115 | size = VM_MAX_TAG_VALUE * sizeof(vm_allocation_zone_total_t * *) |
9116 | + 2 * VM_MAX_TAG_ZONES * sizeof(vm_allocation_zone_total_t); | |
5ba3f43e A |
9117 | |
9118 | ret = kernel_memory_allocate(kernel_map, | |
0a7de745 A |
9119 | &addr, round_page(size), 0, |
9120 | KMA_ZERO, VM_KERN_MEMORY_DIAG); | |
9121 | assert(KERN_SUCCESS == ret); | |
5ba3f43e | 9122 | |
0a7de745 A |
9123 | vm_allocation_zone_totals = (vm_allocation_zone_total_t **) addr; |
9124 | addr += VM_MAX_TAG_VALUE * sizeof(vm_allocation_zone_total_t * *); | |
5ba3f43e | 9125 | |
0a7de745 A |
9126 | // prepopulate VM_KERN_MEMORY_DIAG & VM_KERN_MEMORY_KALLOC so allocations |
9127 | // in vm_tag_update_zone_size() won't recurse | |
9128 | vm_allocation_zone_totals[VM_KERN_MEMORY_DIAG] = (vm_allocation_zone_total_t *) addr; | |
9129 | addr += VM_MAX_TAG_ZONES * sizeof(vm_allocation_zone_total_t); | |
9130 | vm_allocation_zone_totals[VM_KERN_MEMORY_KALLOC] = (vm_allocation_zone_total_t *) addr; | |
5ba3f43e A |
9131 | } |
9132 | ||
9133 | void | |
9134 | vm_tag_will_update_zone(vm_tag_t tag, uint32_t zidx) | |
9135 | { | |
0a7de745 | 9136 | vm_allocation_zone_total_t * zone; |
5ba3f43e | 9137 | |
0a7de745 A |
9138 | assert(VM_KERN_MEMORY_NONE != tag); |
9139 | assert(tag < VM_MAX_TAG_VALUE); | |
5ba3f43e | 9140 | |
0a7de745 A |
9141 | if (zidx >= VM_MAX_TAG_ZONES) { |
9142 | return; | |
9143 | } | |
5ba3f43e A |
9144 | |
9145 | zone = vm_allocation_zone_totals[tag]; | |
0a7de745 A |
9146 | if (!zone) { |
9147 | zone = kalloc_tag(VM_MAX_TAG_ZONES * sizeof(*zone), VM_KERN_MEMORY_DIAG); | |
9148 | if (!zone) { | |
9149 | return; | |
9150 | } | |
9151 | bzero(zone, VM_MAX_TAG_ZONES * sizeof(*zone)); | |
9152 | if (!OSCompareAndSwapPtr(NULL, zone, &vm_allocation_zone_totals[tag])) { | |
5ba3f43e A |
9153 | kfree(zone, VM_MAX_TAG_ZONES * sizeof(*zone)); |
9154 | } | |
0a7de745 | 9155 | } |
5ba3f43e A |
9156 | } |
9157 | ||
9158 | void | |
9159 | vm_tag_update_zone_size(vm_tag_t tag, uint32_t zidx, int64_t delta, int64_t dwaste) | |
9160 | { | |
0a7de745 A |
9161 | vm_allocation_zone_total_t * zone; |
9162 | uint32_t new; | |
5ba3f43e | 9163 | |
0a7de745 A |
9164 | assert(VM_KERN_MEMORY_NONE != tag); |
9165 | assert(tag < VM_MAX_TAG_VALUE); | |
5ba3f43e | 9166 | |
0a7de745 A |
9167 | if (zidx >= VM_MAX_TAG_ZONES) { |
9168 | return; | |
9169 | } | |
5ba3f43e A |
9170 | |
9171 | zone = vm_allocation_zone_totals[tag]; | |
0a7de745 A |
9172 | assert(zone); |
9173 | zone += zidx; | |
5ba3f43e | 9174 | |
0a7de745 A |
9175 | /* the zone is locked */ |
9176 | if (delta < 0) { | |
5ba3f43e | 9177 | assertf(zone->total >= ((uint64_t)-delta), "zidx %d, tag %d, %p", zidx, tag, zone); |
5ba3f43e | 9178 | zone->total += delta; |
0a7de745 A |
9179 | } else { |
9180 | zone->total += delta; | |
9181 | if (zone->total > zone->peak) { | |
9182 | zone->peak = zone->total; | |
9183 | } | |
9184 | if (dwaste) { | |
5ba3f43e | 9185 | new = zone->waste; |
0a7de745 A |
9186 | if (zone->wastediv < 65536) { |
9187 | zone->wastediv++; | |
9188 | } else { | |
9189 | new -= (new >> 16); | |
9190 | } | |
5ba3f43e A |
9191 | __assert_only bool ov = os_add_overflow(new, dwaste, &new); |
9192 | assert(!ov); | |
9193 | zone->waste = new; | |
0a7de745 A |
9194 | } |
9195 | } | |
5ba3f43e A |
9196 | } |
9197 | ||
9198 | #endif /* VM_MAX_TAG_ZONES */ | |
9199 | ||
9200 | void | |
9201 | kern_allocation_update_subtotal(kern_allocation_name_t allocation, uint32_t subtag, int64_t delta) | |
9202 | { | |
0a7de745 | 9203 | kern_allocation_name_t other; |
5ba3f43e | 9204 | struct vm_allocation_total * total; |
0a7de745 | 9205 | uint32_t subidx; |
5ba3f43e | 9206 | |
0a7de745 A |
9207 | subidx = 0; |
9208 | assert(VM_KERN_MEMORY_NONE != subtag); | |
cb323159 | 9209 | lck_spin_lock(&vm_allocation_sites_lock); |
0a7de745 A |
9210 | for (; subidx < allocation->subtotalscount; subidx++) { |
9211 | if (VM_KERN_MEMORY_NONE == allocation->subtotals[subidx].tag) { | |
f427ee49 | 9212 | allocation->subtotals[subidx].tag = (vm_tag_t)subtag; |
5ba3f43e A |
9213 | break; |
9214 | } | |
0a7de745 A |
9215 | if (subtag == allocation->subtotals[subidx].tag) { |
9216 | break; | |
9217 | } | |
9218 | } | |
cb323159 | 9219 | lck_spin_unlock(&vm_allocation_sites_lock); |
0a7de745 A |
9220 | assert(subidx < allocation->subtotalscount); |
9221 | if (subidx >= allocation->subtotalscount) { | |
9222 | return; | |
5ba3f43e | 9223 | } |
5ba3f43e | 9224 | |
0a7de745 A |
9225 | total = &allocation->subtotals[subidx]; |
9226 | other = vm_allocation_sites[subtag]; | |
9227 | assert(other); | |
5ba3f43e | 9228 | |
0a7de745 | 9229 | if (delta < 0) { |
5ba3f43e | 9230 | assertf(total->total >= ((uint64_t)-delta), "name %p", allocation); |
5ba3f43e | 9231 | assertf(other->mapped >= ((uint64_t)-delta), "other %p", other); |
0a7de745 | 9232 | } |
cb323159 A |
9233 | OSAddAtomic64(delta, &other->mapped); |
9234 | OSAddAtomic64(delta, &total->total); | |
5ba3f43e A |
9235 | } |
9236 | ||
9237 | const char * | |
9238 | kern_allocation_get_name(kern_allocation_name_t allocation) | |
9239 | { | |
0a7de745 | 9240 | return KA_NAME(allocation); |
5ba3f43e A |
9241 | } |
9242 | ||
9243 | kern_allocation_name_t | |
f427ee49 | 9244 | kern_allocation_name_allocate(const char * name, uint16_t subtotalscount) |
5ba3f43e | 9245 | { |
f427ee49 | 9246 | uint16_t namelen; |
5ba3f43e | 9247 | |
f427ee49 | 9248 | namelen = (uint16_t)strnlen(name, MACH_MEMORY_INFO_NAME_MAX_LEN - 1); |
5ba3f43e | 9249 | |
0a7de745 | 9250 | kern_allocation_name_t allocation; |
f427ee49 A |
9251 | allocation = kheap_alloc(KHEAP_DATA_BUFFERS, |
9252 | KA_SIZE(namelen, subtotalscount), Z_WAITOK); | |
0a7de745 | 9253 | bzero(allocation, KA_SIZE(namelen, subtotalscount)); |
5ba3f43e | 9254 | |
0a7de745 A |
9255 | allocation->refcount = 1; |
9256 | allocation->subtotalscount = subtotalscount; | |
f427ee49 | 9257 | allocation->flags = (uint16_t)(namelen << VM_TAG_NAME_LEN_SHIFT); |
0a7de745 | 9258 | strlcpy(KA_NAME(allocation), name, namelen + 1); |
5ba3f43e | 9259 | |
0a7de745 | 9260 | return allocation; |
5ba3f43e A |
9261 | } |
9262 | ||
9263 | void | |
9264 | kern_allocation_name_release(kern_allocation_name_t allocation) | |
9265 | { | |
0a7de745 A |
9266 | assert(allocation->refcount > 0); |
9267 | if (1 == OSAddAtomic16(-1, &allocation->refcount)) { | |
f427ee49 A |
9268 | kheap_free(KHEAP_DATA_BUFFERS, allocation, |
9269 | KA_SIZE(KA_NAME_LEN(allocation), allocation->subtotalscount)); | |
0a7de745 | 9270 | } |
5ba3f43e A |
9271 | } |
9272 | ||
9273 | vm_tag_t | |
9274 | kern_allocation_name_get_vm_tag(kern_allocation_name_t allocation) | |
9275 | { | |
0a7de745 | 9276 | return vm_tag_alloc(allocation); |
5ba3f43e A |
9277 | } |
9278 | ||
0a7de745 A |
9279 | #if !VM_TAG_ACTIVE_UPDATE |
9280 | static void | |
5ba3f43e | 9281 | vm_page_count_object(mach_memory_info_t * info, unsigned int __unused num_info, vm_object_t object) |
3e170ce0 | 9282 | { |
0a7de745 A |
9283 | if (!object->wired_page_count) { |
9284 | return; | |
9285 | } | |
9286 | if (object != kernel_object) { | |
5ba3f43e A |
9287 | assert(object->wire_tag < num_info); |
9288 | info[object->wire_tag].size += ptoa_64(object->wired_page_count); | |
0a7de745 | 9289 | } |
3e170ce0 A |
9290 | } |
9291 | ||
5ba3f43e | 9292 | typedef void (*vm_page_iterate_proc)(mach_memory_info_t * info, |
0a7de745 | 9293 | unsigned int num_info, vm_object_t object); |
3e170ce0 | 9294 | |
0a7de745 | 9295 | static void |
5ba3f43e | 9296 | vm_page_iterate_purgeable_objects(mach_memory_info_t * info, unsigned int num_info, |
0a7de745 A |
9297 | vm_page_iterate_proc proc, purgeable_q_t queue, |
9298 | int group) | |
3e170ce0 | 9299 | { |
0a7de745 | 9300 | vm_object_t object; |
3e170ce0 | 9301 | |
0a7de745 A |
9302 | for (object = (vm_object_t) queue_first(&queue->objq[group]); |
9303 | !queue_end(&queue->objq[group], (queue_entry_t) object); | |
9304 | object = (vm_object_t) queue_next(&object->objq)) { | |
5ba3f43e | 9305 | proc(info, num_info, object); |
0a7de745 | 9306 | } |
3e170ce0 A |
9307 | } |
9308 | ||
0a7de745 | 9309 | static void |
5ba3f43e | 9310 | vm_page_iterate_objects(mach_memory_info_t * info, unsigned int num_info, |
0a7de745 | 9311 | vm_page_iterate_proc proc) |
3e170ce0 | 9312 | { |
0a7de745 | 9313 | vm_object_t object; |
3e170ce0 | 9314 | |
0a7de745 A |
9315 | lck_spin_lock_grp(&vm_objects_wired_lock, &vm_page_lck_grp_bucket); |
9316 | queue_iterate(&vm_objects_wired, | |
9317 | object, | |
9318 | vm_object_t, | |
9319 | wired_objq) | |
9320 | { | |
5ba3f43e | 9321 | proc(info, num_info, object); |
0a7de745 A |
9322 | } |
9323 | lck_spin_unlock(&vm_objects_wired_lock); | |
3e170ce0 | 9324 | } |
d9a64523 | 9325 | #endif /* ! VM_TAG_ACTIVE_UPDATE */ |
3e170ce0 A |
9326 | |
9327 | static uint64_t | |
f427ee49 A |
9328 | process_account(mach_memory_info_t * info, unsigned int num_info, |
9329 | uint64_t zones_collectable_bytes, boolean_t iterated) | |
3e170ce0 | 9330 | { |
0a7de745 A |
9331 | size_t namelen; |
9332 | unsigned int idx, count, nextinfo; | |
9333 | vm_allocation_site_t * site; | |
5ba3f43e | 9334 | lck_spin_lock(&vm_allocation_sites_lock); |
3e170ce0 | 9335 | |
0a7de745 | 9336 | for (idx = 0; idx <= vm_allocation_tag_highest; idx++) { |
5ba3f43e | 9337 | site = vm_allocation_sites[idx]; |
0a7de745 A |
9338 | if (!site) { |
9339 | continue; | |
9340 | } | |
5ba3f43e A |
9341 | info[idx].mapped = site->mapped; |
9342 | info[idx].tag = site->tag; | |
0a7de745 | 9343 | if (!iterated) { |
5ba3f43e A |
9344 | info[idx].size = site->total; |
9345 | #if DEBUG || DEVELOPMENT | |
9346 | info[idx].peak = site->peak; | |
9347 | #endif /* DEBUG || DEVELOPMENT */ | |
0a7de745 A |
9348 | } else { |
9349 | if (!site->subtotalscount && (site->total != info[idx].size)) { | |
9350 | printf("tag mismatch[%d] 0x%qx, iter 0x%qx\n", idx, site->total, info[idx].size); | |
9351 | info[idx].size = site->total; | |
9352 | } | |
9353 | } | |
5ba3f43e | 9354 | info[idx].flags |= VM_KERN_SITE_WIRED; |
0a7de745 A |
9355 | if (idx < VM_KERN_MEMORY_FIRST_DYNAMIC) { |
9356 | info[idx].site = idx; | |
9357 | info[idx].flags |= VM_KERN_SITE_TAG; | |
9358 | if (VM_KERN_MEMORY_ZONE == idx) { | |
5ba3f43e A |
9359 | info[idx].flags |= VM_KERN_SITE_HIDE; |
9360 | info[idx].flags &= ~VM_KERN_SITE_WIRED; | |
9361 | info[idx].collectable_bytes = zones_collectable_bytes; | |
9362 | } | |
0a7de745 A |
9363 | } else if ((namelen = (VM_TAG_NAME_LEN_MAX & (site->flags >> VM_TAG_NAME_LEN_SHIFT)))) { |
9364 | info[idx].site = 0; | |
9365 | info[idx].flags |= VM_KERN_SITE_NAMED; | |
9366 | if (namelen > sizeof(info[idx].name)) { | |
9367 | namelen = sizeof(info[idx].name); | |
9368 | } | |
9369 | strncpy(&info[idx].name[0], KA_NAME(site), namelen); | |
9370 | } else if (VM_TAG_KMOD & site->flags) { | |
9371 | info[idx].site = OSKextGetKmodIDForSite(site, NULL, 0); | |
9372 | info[idx].flags |= VM_KERN_SITE_KMOD; | |
9373 | } else { | |
9374 | info[idx].site = VM_KERNEL_UNSLIDE(site); | |
9375 | info[idx].flags |= VM_KERN_SITE_KERNEL; | |
5ba3f43e | 9376 | } |
0a7de745 A |
9377 | } |
9378 | ||
9379 | nextinfo = (vm_allocation_tag_highest + 1); | |
9380 | count = nextinfo; | |
9381 | if (count >= num_info) { | |
9382 | count = num_info; | |
9383 | } | |
9384 | ||
9385 | for (idx = 0; idx < count; idx++) { | |
9386 | site = vm_allocation_sites[idx]; | |
9387 | if (!site) { | |
9388 | continue; | |
3e170ce0 | 9389 | } |
5ba3f43e A |
9390 | #if VM_MAX_TAG_ZONES |
9391 | vm_allocation_zone_total_t * zone; | |
9392 | unsigned int zidx; | |
9393 | vm_size_t elem_size; | |
9394 | ||
0a7de745 A |
9395 | if (vm_allocation_zone_totals |
9396 | && (zone = vm_allocation_zone_totals[idx]) | |
9397 | && (nextinfo < num_info)) { | |
9398 | for (zidx = 0; zidx < VM_MAX_TAG_ZONES; zidx++) { | |
9399 | if (!zone[zidx].peak) { | |
9400 | continue; | |
9401 | } | |
f427ee49 A |
9402 | info[nextinfo] = info[idx]; |
9403 | info[nextinfo].zone = (uint16_t)zone_index_from_tag_index(zidx, &elem_size); | |
9404 | info[nextinfo].flags &= ~VM_KERN_SITE_WIRED; | |
9405 | info[nextinfo].flags |= VM_KERN_SITE_ZONE; | |
9406 | info[nextinfo].size = zone[zidx].total; | |
9407 | info[nextinfo].peak = zone[zidx].peak; | |
9408 | info[nextinfo].mapped = 0; | |
0a7de745 | 9409 | if (zone[zidx].wastediv) { |
5ba3f43e A |
9410 | info[nextinfo].collectable_bytes = ((zone[zidx].waste * zone[zidx].total / elem_size) / zone[zidx].wastediv); |
9411 | } | |
0a7de745 A |
9412 | nextinfo++; |
9413 | } | |
9414 | } | |
5ba3f43e | 9415 | #endif /* VM_MAX_TAG_ZONES */ |
0a7de745 | 9416 | if (site->subtotalscount) { |
5ba3f43e A |
9417 | uint64_t mapped, mapcost, take; |
9418 | uint32_t sub; | |
9419 | vm_tag_t alloctag; | |
9420 | ||
0a7de745 A |
9421 | info[idx].size = site->total; |
9422 | mapped = info[idx].size; | |
9423 | info[idx].mapped = mapped; | |
9424 | mapcost = 0; | |
9425 | for (sub = 0; sub < site->subtotalscount; sub++) { | |
5ba3f43e A |
9426 | alloctag = site->subtotals[sub].tag; |
9427 | assert(alloctag < num_info); | |
0a7de745 A |
9428 | if (info[alloctag].name[0]) { |
9429 | continue; | |
9430 | } | |
9431 | take = site->subtotals[sub].total; | |
9432 | if (take > info[alloctag].size) { | |
9433 | take = info[alloctag].size; | |
9434 | } | |
9435 | if (take > mapped) { | |
9436 | take = mapped; | |
9437 | } | |
5ba3f43e A |
9438 | info[alloctag].mapped -= take; |
9439 | info[alloctag].size -= take; | |
9440 | mapped -= take; | |
9441 | mapcost += take; | |
0a7de745 A |
9442 | } |
9443 | info[idx].size = mapcost; | |
9444 | } | |
3e170ce0 A |
9445 | } |
9446 | lck_spin_unlock(&vm_allocation_sites_lock); | |
5ba3f43e | 9447 | |
0a7de745 | 9448 | return 0; |
5ba3f43e A |
9449 | } |
9450 | ||
9451 | uint32_t | |
9452 | vm_page_diagnose_estimate(void) | |
9453 | { | |
0a7de745 | 9454 | vm_allocation_site_t * site; |
f427ee49 | 9455 | uint32_t count = zone_view_count; |
0a7de745 | 9456 | uint32_t idx; |
5ba3f43e A |
9457 | |
9458 | lck_spin_lock(&vm_allocation_sites_lock); | |
f427ee49 | 9459 | for (idx = 0; idx < VM_MAX_TAG_VALUE; idx++) { |
5ba3f43e | 9460 | site = vm_allocation_sites[idx]; |
0a7de745 A |
9461 | if (!site) { |
9462 | continue; | |
9463 | } | |
5ba3f43e A |
9464 | count++; |
9465 | #if VM_MAX_TAG_ZONES | |
0a7de745 | 9466 | if (vm_allocation_zone_totals) { |
5ba3f43e A |
9467 | vm_allocation_zone_total_t * zone; |
9468 | zone = vm_allocation_zone_totals[idx]; | |
0a7de745 A |
9469 | if (!zone) { |
9470 | continue; | |
9471 | } | |
9472 | for (uint32_t zidx = 0; zidx < VM_MAX_TAG_ZONES; zidx++) { | |
9473 | if (zone[zidx].peak) { | |
9474 | count++; | |
9475 | } | |
9476 | } | |
5ba3f43e A |
9477 | } |
9478 | #endif | |
0a7de745 | 9479 | } |
5ba3f43e | 9480 | lck_spin_unlock(&vm_allocation_sites_lock); |
39037602 | 9481 | |
0a7de745 A |
9482 | /* some slop for new tags created */ |
9483 | count += 8; | |
9484 | count += VM_KERN_COUNTER_COUNT; | |
5ba3f43e | 9485 | |
0a7de745 | 9486 | return count; |
3e170ce0 A |
9487 | } |
9488 | ||
f427ee49 A |
9489 | static void |
9490 | vm_page_diagnose_zone_stats(mach_memory_info_t *info, zone_stats_t zstats, | |
9491 | bool percpu) | |
9492 | { | |
9493 | zpercpu_foreach(zs, zstats) { | |
9494 | info->size += zs->zs_mem_allocated - zs->zs_mem_freed; | |
9495 | } | |
9496 | if (percpu) { | |
9497 | info->size *= zpercpu_count(); | |
9498 | } | |
9499 | info->flags |= VM_KERN_SITE_NAMED | VM_KERN_SITE_ZONE_VIEW; | |
9500 | } | |
9501 | ||
9502 | static void | |
9503 | vm_page_diagnose_zone(mach_memory_info_t *info, zone_t z) | |
9504 | { | |
9505 | vm_page_diagnose_zone_stats(info, z->z_stats, z->percpu); | |
9506 | snprintf(info->name, sizeof(info->name), | |
9507 | "%s%s[raw]", zone_heap_name(z), z->z_name); | |
9508 | } | |
9509 | ||
9510 | static int | |
9511 | vm_page_diagnose_heap(mach_memory_info_t *info, kalloc_heap_t kheap) | |
9512 | { | |
9513 | struct kheap_zones *zones = kheap->kh_zones; | |
9514 | int i = 0; | |
9515 | ||
9516 | for (; i < zones->max_k_zone; i++) { | |
9517 | vm_page_diagnose_zone(info + i, zones->k_zone[i]); | |
9518 | } | |
9519 | ||
9520 | for (kalloc_heap_t kh = zones->views; kh; kh = kh->kh_next, i++) { | |
9521 | vm_page_diagnose_zone_stats(info + i, kh->kh_stats, false); | |
9522 | snprintf(info[i].name, sizeof(info[i].name), | |
9523 | "%skalloc[%s]", kheap->kh_name, kh->kh_name); | |
9524 | } | |
9525 | ||
9526 | return i; | |
9527 | } | |
9528 | ||
0a7de745 | 9529 | kern_return_t |
5ba3f43e | 9530 | vm_page_diagnose(mach_memory_info_t * info, unsigned int num_info, uint64_t zones_collectable_bytes) |
3e170ce0 | 9531 | { |
0a7de745 A |
9532 | uint64_t wired_size; |
9533 | uint64_t wired_managed_size; | |
9534 | uint64_t wired_reserved_size; | |
0a7de745 A |
9535 | boolean_t iterate; |
9536 | mach_memory_info_t * counts; | |
f427ee49 | 9537 | uint32_t i; |
3e170ce0 | 9538 | |
0a7de745 | 9539 | bzero(info, num_info * sizeof(mach_memory_info_t)); |
3e170ce0 | 9540 | |
0a7de745 A |
9541 | if (!vm_page_wire_count_initial) { |
9542 | return KERN_ABORTED; | |
9543 | } | |
39037602 | 9544 | |
5ba3f43e | 9545 | #if CONFIG_EMBEDDED |
0a7de745 A |
9546 | wired_size = ptoa_64(vm_page_wire_count); |
9547 | wired_reserved_size = ptoa_64(vm_page_wire_count_initial - vm_page_stolen_count); | |
5ba3f43e | 9548 | #else |
0a7de745 A |
9549 | wired_size = ptoa_64(vm_page_wire_count + vm_lopage_free_count + vm_page_throttled_count); |
9550 | wired_reserved_size = ptoa_64(vm_page_wire_count_initial - vm_page_stolen_count + vm_page_throttled_count); | |
5ba3f43e | 9551 | #endif |
0a7de745 | 9552 | wired_managed_size = ptoa_64(vm_page_wire_count - vm_page_wire_count_initial); |
3e170ce0 | 9553 | |
0a7de745 | 9554 | wired_size += booter_size; |
5ba3f43e | 9555 | |
0a7de745 A |
9556 | assert(num_info >= VM_KERN_COUNTER_COUNT); |
9557 | num_info -= VM_KERN_COUNTER_COUNT; | |
9558 | counts = &info[num_info]; | |
3e170ce0 | 9559 | |
0a7de745 | 9560 | #define SET_COUNT(xcount, xsize, xflags) \ |
5ba3f43e | 9561 | counts[xcount].tag = VM_MAX_TAG_VALUE + xcount; \ |
0a7de745 A |
9562 | counts[xcount].site = (xcount); \ |
9563 | counts[xcount].size = (xsize); \ | |
9564 | counts[xcount].mapped = (xsize); \ | |
3e170ce0 A |
9565 | counts[xcount].flags = VM_KERN_SITE_COUNTER | xflags; |
9566 | ||
0a7de745 A |
9567 | SET_COUNT(VM_KERN_COUNT_MANAGED, ptoa_64(vm_page_pages), 0); |
9568 | SET_COUNT(VM_KERN_COUNT_WIRED, wired_size, 0); | |
9569 | SET_COUNT(VM_KERN_COUNT_WIRED_MANAGED, wired_managed_size, 0); | |
9570 | SET_COUNT(VM_KERN_COUNT_RESERVED, wired_reserved_size, VM_KERN_SITE_WIRED); | |
9571 | SET_COUNT(VM_KERN_COUNT_STOLEN, ptoa_64(vm_page_stolen_count), VM_KERN_SITE_WIRED); | |
9572 | SET_COUNT(VM_KERN_COUNT_LOPAGE, ptoa_64(vm_lopage_free_count), VM_KERN_SITE_WIRED); | |
9573 | SET_COUNT(VM_KERN_COUNT_WIRED_BOOT, ptoa_64(vm_page_wire_count_on_boot), 0); | |
9574 | SET_COUNT(VM_KERN_COUNT_BOOT_STOLEN, booter_size, VM_KERN_SITE_WIRED); | |
f427ee49 | 9575 | SET_COUNT(VM_KERN_COUNT_WIRED_STATIC_KERNELCACHE, ptoa_64(vm_page_kernelcache_count), 0); |
0a7de745 A |
9576 | |
9577 | #define SET_MAP(xcount, xsize, xfree, xlargest) \ | |
9578 | counts[xcount].site = (xcount); \ | |
9579 | counts[xcount].size = (xsize); \ | |
9580 | counts[xcount].mapped = (xsize); \ | |
9581 | counts[xcount].free = (xfree); \ | |
9582 | counts[xcount].largest = (xlargest); \ | |
3e170ce0 A |
9583 | counts[xcount].flags = VM_KERN_SITE_COUNTER; |
9584 | ||
0a7de745 | 9585 | vm_map_size_t map_size, map_free, map_largest; |
3e170ce0 | 9586 | |
0a7de745 A |
9587 | vm_map_sizes(kernel_map, &map_size, &map_free, &map_largest); |
9588 | SET_MAP(VM_KERN_COUNT_MAP_KERNEL, map_size, map_free, map_largest); | |
3e170ce0 | 9589 | |
f427ee49 | 9590 | zone_map_sizes(&map_size, &map_free, &map_largest); |
0a7de745 | 9591 | SET_MAP(VM_KERN_COUNT_MAP_ZONE, map_size, map_free, map_largest); |
3e170ce0 | 9592 | |
0a7de745 A |
9593 | vm_map_sizes(kalloc_map, &map_size, &map_free, &map_largest); |
9594 | SET_MAP(VM_KERN_COUNT_MAP_KALLOC, map_size, map_free, map_largest); | |
3e170ce0 | 9595 | |
f427ee49 A |
9596 | assert(num_info >= zone_view_count); |
9597 | num_info -= zone_view_count; | |
9598 | counts = &info[num_info]; | |
9599 | i = 0; | |
9600 | ||
9601 | i += vm_page_diagnose_heap(counts + i, KHEAP_DEFAULT); | |
9602 | if (KHEAP_DATA_BUFFERS->kh_heap_id == KHEAP_ID_DATA_BUFFERS) { | |
9603 | i += vm_page_diagnose_heap(counts + i, KHEAP_DATA_BUFFERS); | |
9604 | } | |
9605 | if (KHEAP_KEXT->kh_heap_id == KHEAP_ID_KEXT) { | |
9606 | i += vm_page_diagnose_heap(counts + i, KHEAP_KEXT); | |
9607 | } | |
9608 | assert(i <= zone_view_count); | |
9609 | ||
9610 | zone_index_foreach(zidx) { | |
9611 | zone_t z = &zone_array[zidx]; | |
9612 | zone_view_t zv = z->z_views; | |
9613 | ||
9614 | if (zv == NULL) { | |
9615 | continue; | |
9616 | } | |
9617 | ||
9618 | if (z->kalloc_heap == KHEAP_ID_NONE) { | |
9619 | vm_page_diagnose_zone(counts + i, z); | |
9620 | i++; | |
9621 | assert(i <= zone_view_count); | |
9622 | } | |
9623 | ||
9624 | for (; zv; zv = zv->zv_next) { | |
9625 | vm_page_diagnose_zone_stats(counts + i, zv->zv_stats, | |
9626 | z->percpu); | |
9627 | snprintf(counts[i].name, sizeof(counts[i].name), "%s%s[%s]", | |
9628 | zone_heap_name(z), z->z_name, zv->zv_name); | |
9629 | i++; | |
9630 | assert(i <= zone_view_count); | |
9631 | } | |
9632 | } | |
9633 | ||
0a7de745 A |
9634 | iterate = !VM_TAG_ACTIVE_UPDATE; |
9635 | if (iterate) { | |
9636 | enum { kMaxKernelDepth = 1 }; | |
9637 | vm_map_t maps[kMaxKernelDepth]; | |
9638 | vm_map_entry_t entries[kMaxKernelDepth]; | |
9639 | vm_map_t map; | |
9640 | vm_map_entry_t entry; | |
9641 | vm_object_offset_t offset; | |
9642 | vm_page_t page; | |
9643 | int stackIdx, count; | |
9644 | ||
9645 | #if !VM_TAG_ACTIVE_UPDATE | |
9646 | vm_page_iterate_objects(info, num_info, &vm_page_count_object); | |
d9a64523 | 9647 | #endif /* ! VM_TAG_ACTIVE_UPDATE */ |
5ba3f43e | 9648 | |
0a7de745 A |
9649 | map = kernel_map; |
9650 | stackIdx = 0; | |
9651 | while (map) { | |
5ba3f43e | 9652 | vm_map_lock(map); |
0a7de745 A |
9653 | for (entry = map->hdr.links.next; map; entry = entry->links.next) { |
9654 | if (entry->is_sub_map) { | |
5ba3f43e A |
9655 | assert(stackIdx < kMaxKernelDepth); |
9656 | maps[stackIdx] = map; | |
9657 | entries[stackIdx] = entry; | |
9658 | stackIdx++; | |
9659 | map = VME_SUBMAP(entry); | |
9660 | entry = NULL; | |
9661 | break; | |
0a7de745 A |
9662 | } |
9663 | if (VME_OBJECT(entry) == kernel_object) { | |
5ba3f43e A |
9664 | count = 0; |
9665 | vm_object_lock(VME_OBJECT(entry)); | |
0a7de745 | 9666 | for (offset = entry->links.start; offset < entry->links.end; offset += page_size) { |
5ba3f43e | 9667 | page = vm_page_lookup(VME_OBJECT(entry), offset); |
0a7de745 A |
9668 | if (page && VM_PAGE_WIRED(page)) { |
9669 | count++; | |
9670 | } | |
5ba3f43e A |
9671 | } |
9672 | vm_object_unlock(VME_OBJECT(entry)); | |
3e170ce0 | 9673 | |
0a7de745 A |
9674 | if (count) { |
9675 | assert(VME_ALIAS(entry) != VM_KERN_MEMORY_NONE); | |
9676 | assert(VME_ALIAS(entry) < num_info); | |
9677 | info[VME_ALIAS(entry)].size += ptoa_64(count); | |
5ba3f43e | 9678 | } |
0a7de745 A |
9679 | } |
9680 | while (map && (entry == vm_map_last_entry(map))) { | |
5ba3f43e | 9681 | vm_map_unlock(map); |
0a7de745 A |
9682 | if (!stackIdx) { |
9683 | map = NULL; | |
9684 | } else { | |
9685 | --stackIdx; | |
9686 | map = maps[stackIdx]; | |
9687 | entry = entries[stackIdx]; | |
5ba3f43e | 9688 | } |
0a7de745 | 9689 | } |
5ba3f43e | 9690 | } |
0a7de745 A |
9691 | } |
9692 | } | |
9693 | ||
9694 | process_account(info, num_info, zones_collectable_bytes, iterate); | |
3e170ce0 | 9695 | |
0a7de745 | 9696 | return KERN_SUCCESS; |
3e170ce0 | 9697 | } |
39037602 | 9698 | |
39037602 A |
9699 | #if DEBUG || DEVELOPMENT |
9700 | ||
39037602 | 9701 | kern_return_t |
5ba3f43e | 9702 | vm_kern_allocation_info(uintptr_t addr, vm_size_t * size, vm_tag_t * tag, vm_size_t * zone_size) |
39037602 | 9703 | { |
0a7de745 A |
9704 | kern_return_t ret; |
9705 | vm_size_t zsize; | |
9706 | vm_map_t map; | |
9707 | vm_map_entry_t entry; | |
39037602 | 9708 | |
0a7de745 A |
9709 | zsize = zone_element_info((void *) addr, tag); |
9710 | if (zsize) { | |
5ba3f43e | 9711 | *zone_size = *size = zsize; |
0a7de745 A |
9712 | return KERN_SUCCESS; |
9713 | } | |
39037602 | 9714 | |
5ba3f43e | 9715 | *zone_size = 0; |
0a7de745 A |
9716 | ret = KERN_INVALID_ADDRESS; |
9717 | for (map = kernel_map; map;) { | |
5ba3f43e | 9718 | vm_map_lock(map); |
0a7de745 A |
9719 | if (!vm_map_lookup_entry(map, addr, &entry)) { |
9720 | break; | |
9721 | } | |
9722 | if (entry->is_sub_map) { | |
9723 | if (map != kernel_map) { | |
9724 | break; | |
9725 | } | |
5ba3f43e A |
9726 | map = VME_SUBMAP(entry); |
9727 | continue; | |
0a7de745 A |
9728 | } |
9729 | if (entry->vme_start != addr) { | |
9730 | break; | |
9731 | } | |
f427ee49 | 9732 | *tag = (vm_tag_t)VME_ALIAS(entry); |
5ba3f43e A |
9733 | *size = (entry->vme_end - addr); |
9734 | ret = KERN_SUCCESS; | |
9735 | break; | |
9736 | } | |
0a7de745 A |
9737 | if (map != kernel_map) { |
9738 | vm_map_unlock(map); | |
9739 | } | |
5ba3f43e | 9740 | vm_map_unlock(kernel_map); |
39037602 | 9741 | |
0a7de745 | 9742 | return ret; |
39037602 A |
9743 | } |
9744 | ||
5ba3f43e | 9745 | #endif /* DEBUG || DEVELOPMENT */ |
39037602 | 9746 | |
5ba3f43e A |
9747 | uint32_t |
9748 | vm_tag_get_kext(vm_tag_t tag, char * name, vm_size_t namelen) | |
9749 | { | |
0a7de745 A |
9750 | vm_allocation_site_t * site; |
9751 | uint32_t kmodId; | |
39037602 | 9752 | |
0a7de745 A |
9753 | kmodId = 0; |
9754 | lck_spin_lock(&vm_allocation_sites_lock); | |
9755 | if ((site = vm_allocation_sites[tag])) { | |
9756 | if (VM_TAG_KMOD & site->flags) { | |
9757 | kmodId = OSKextGetKmodIDForSite(site, name, namelen); | |
9758 | } | |
9759 | } | |
9760 | lck_spin_unlock(&vm_allocation_sites_lock); | |
39037602 | 9761 | |
0a7de745 | 9762 | return kmodId; |
39037602 | 9763 | } |
d9a64523 A |
9764 | |
9765 | ||
9766 | #if CONFIG_SECLUDED_MEMORY | |
9767 | /* | |
9768 | * Note that there's no locking around other accesses to vm_page_secluded_target. | |
9769 | * That should be OK, since these are the only place where it can be changed after | |
9770 | * initialization. Other users (like vm_pageout) may see the wrong value briefly, | |
9771 | * but will eventually get the correct value. This brief mismatch is OK as pageout | |
9772 | * and page freeing will auto-adjust the vm_page_secluded_count to match the target | |
9773 | * over time. | |
9774 | */ | |
9775 | unsigned int vm_page_secluded_suppress_cnt = 0; | |
9776 | unsigned int vm_page_secluded_save_target; | |
9777 | ||
f427ee49 A |
9778 | LCK_GRP_DECLARE(secluded_suppress_slock_grp, "secluded_suppress_slock"); |
9779 | LCK_SPIN_DECLARE(secluded_suppress_slock, &secluded_suppress_slock_grp); | |
d9a64523 A |
9780 | |
9781 | void | |
9782 | start_secluded_suppression(task_t task) | |
9783 | { | |
0a7de745 | 9784 | if (task->task_suppressed_secluded) { |
d9a64523 | 9785 | return; |
0a7de745 | 9786 | } |
d9a64523 A |
9787 | lck_spin_lock(&secluded_suppress_slock); |
9788 | if (!task->task_suppressed_secluded && vm_page_secluded_suppress_cnt++ == 0) { | |
9789 | task->task_suppressed_secluded = TRUE; | |
9790 | vm_page_secluded_save_target = vm_page_secluded_target; | |
9791 | vm_page_secluded_target = 0; | |
cb323159 | 9792 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); |
d9a64523 A |
9793 | } |
9794 | lck_spin_unlock(&secluded_suppress_slock); | |
9795 | } | |
9796 | ||
9797 | void | |
9798 | stop_secluded_suppression(task_t task) | |
9799 | { | |
9800 | lck_spin_lock(&secluded_suppress_slock); | |
9801 | if (task->task_suppressed_secluded && --vm_page_secluded_suppress_cnt == 0) { | |
9802 | task->task_suppressed_secluded = FALSE; | |
9803 | vm_page_secluded_target = vm_page_secluded_save_target; | |
cb323159 | 9804 | VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE(); |
d9a64523 A |
9805 | } |
9806 | lck_spin_unlock(&secluded_suppress_slock); | |
9807 | } | |
9808 | ||
9809 | #endif /* CONFIG_SECLUDED_MEMORY */ |