]>
Commit | Line | Data |
---|---|---|
0c530ab8 A |
1 | /* |
2 | * Copyright (c) 2006 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0c530ab8 | 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. | |
0c530ab8 | 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. | |
17 | * | |
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 | |
0c530ab8 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. | |
0c530ab8 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
0c530ab8 A |
27 | */ |
28 | ||
29 | #include <sys/errno.h> | |
30 | ||
31 | #include <mach/mach_types.h> | |
32 | #include <mach/mach_traps.h> | |
33 | #include <mach/host_priv.h> | |
34 | #include <mach/kern_return.h> | |
35 | #include <mach/memory_object_control.h> | |
36 | #include <mach/memory_object_types.h> | |
37 | #include <mach/port.h> | |
38 | #include <mach/policy.h> | |
39 | #include <mach/upl.h> | |
40 | #include <mach/thread_act.h> | |
41 | #include <mach/mach_vm.h> | |
42 | ||
43 | #include <kern/host.h> | |
44 | #include <kern/kalloc.h> | |
45 | #include <kern/page_decrypt.h> | |
46 | #include <kern/queue.h> | |
47 | #include <kern/thread.h> | |
39037602 | 48 | #include <kern/ipc_kobject.h> |
0c530ab8 A |
49 | |
50 | #include <ipc/ipc_port.h> | |
51 | #include <ipc/ipc_space.h> | |
52 | ||
2d21ac55 | 53 | #include <vm/vm_fault.h> |
0c530ab8 A |
54 | #include <vm/vm_map.h> |
55 | #include <vm/vm_pageout.h> | |
56 | #include <vm/memory_object.h> | |
57 | #include <vm/vm_pageout.h> | |
58 | #include <vm/vm_protos.h> | |
39037602 | 59 | #include <vm/vm_kern.h> |
0c530ab8 A |
60 | |
61 | ||
62 | /* | |
63 | * APPLE PROTECT MEMORY PAGER | |
64 | * | |
65 | * This external memory manager (EMM) handles memory from the encrypted | |
66 | * sections of some executables protected by the DSMOS kernel extension. | |
67 | * | |
68 | * It mostly handles page-in requests (from memory_object_data_request()) by | |
69 | * getting the encrypted data from its backing VM object, itself backed by | |
70 | * the encrypted file, decrypting it and providing it to VM. | |
71 | * | |
72 | * The decrypted pages will never be dirtied, so the memory manager doesn't | |
73 | * need to handle page-out requests (from memory_object_data_return()). The | |
74 | * pages need to be mapped copy-on-write, so that the originals stay clean. | |
75 | * | |
76 | * We don't expect to have to handle a large number of apple-protected | |
77 | * binaries, so the data structures are very simple (simple linked list) | |
78 | * for now. | |
79 | */ | |
80 | ||
81 | /* forward declarations */ | |
82 | void apple_protect_pager_reference(memory_object_t mem_obj); | |
83 | void apple_protect_pager_deallocate(memory_object_t mem_obj); | |
84 | kern_return_t apple_protect_pager_init(memory_object_t mem_obj, | |
85 | memory_object_control_t control, | |
b0d623f7 | 86 | memory_object_cluster_size_t pg_size); |
0c530ab8 A |
87 | kern_return_t apple_protect_pager_terminate(memory_object_t mem_obj); |
88 | kern_return_t apple_protect_pager_data_request(memory_object_t mem_obj, | |
89 | memory_object_offset_t offset, | |
b0d623f7 | 90 | memory_object_cluster_size_t length, |
2d21ac55 A |
91 | vm_prot_t protection_required, |
92 | memory_object_fault_info_t fault_info); | |
0c530ab8 A |
93 | kern_return_t apple_protect_pager_data_return(memory_object_t mem_obj, |
94 | memory_object_offset_t offset, | |
b0d623f7 | 95 | memory_object_cluster_size_t data_cnt, |
0c530ab8 A |
96 | memory_object_offset_t *resid_offset, |
97 | int *io_error, | |
98 | boolean_t dirty, | |
99 | boolean_t kernel_copy, | |
100 | int upl_flags); | |
101 | kern_return_t apple_protect_pager_data_initialize(memory_object_t mem_obj, | |
102 | memory_object_offset_t offset, | |
b0d623f7 | 103 | memory_object_cluster_size_t data_cnt); |
0c530ab8 A |
104 | kern_return_t apple_protect_pager_data_unlock(memory_object_t mem_obj, |
105 | memory_object_offset_t offset, | |
b0d623f7 | 106 | memory_object_size_t size, |
0c530ab8 A |
107 | vm_prot_t desired_access); |
108 | kern_return_t apple_protect_pager_synchronize(memory_object_t mem_obj, | |
109 | memory_object_offset_t offset, | |
b0d623f7 | 110 | memory_object_size_t length, |
0c530ab8 | 111 | vm_sync_t sync_flags); |
593a1d5f A |
112 | kern_return_t apple_protect_pager_map(memory_object_t mem_obj, |
113 | vm_prot_t prot); | |
114 | kern_return_t apple_protect_pager_last_unmap(memory_object_t mem_obj); | |
0c530ab8 | 115 | |
3e170ce0 A |
116 | #define CRYPT_INFO_DEBUG 0 |
117 | void crypt_info_reference(struct pager_crypt_info *crypt_info); | |
118 | void crypt_info_deallocate(struct pager_crypt_info *crypt_info); | |
119 | ||
0c530ab8 A |
120 | /* |
121 | * Vector of VM operations for this EMM. | |
122 | * These routines are invoked by VM via the memory_object_*() interfaces. | |
123 | */ | |
124 | const struct memory_object_pager_ops apple_protect_pager_ops = { | |
125 | apple_protect_pager_reference, | |
126 | apple_protect_pager_deallocate, | |
127 | apple_protect_pager_init, | |
128 | apple_protect_pager_terminate, | |
129 | apple_protect_pager_data_request, | |
130 | apple_protect_pager_data_return, | |
131 | apple_protect_pager_data_initialize, | |
132 | apple_protect_pager_data_unlock, | |
133 | apple_protect_pager_synchronize, | |
593a1d5f A |
134 | apple_protect_pager_map, |
135 | apple_protect_pager_last_unmap, | |
6d2010ae | 136 | NULL, /* data_reclaim */ |
3e170ce0 | 137 | "apple_protect" |
0c530ab8 A |
138 | }; |
139 | ||
140 | /* | |
141 | * The "apple_protect_pager" describes a memory object backed by | |
142 | * the "apple protect" EMM. | |
143 | */ | |
144 | typedef struct apple_protect_pager { | |
3e170ce0 | 145 | struct ipc_object_header pager_header; /* fake ip_kotype() */ |
0c530ab8 | 146 | memory_object_pager_ops_t pager_ops; /* == &apple_protect_pager_ops */ |
0c530ab8 A |
147 | queue_chain_t pager_queue; /* next & prev pagers */ |
148 | unsigned int ref_count; /* reference count */ | |
149 | boolean_t is_ready; /* is this pager ready ? */ | |
150 | boolean_t is_mapped; /* is this mem_obj mapped ? */ | |
151 | memory_object_control_t pager_control; /* mem object control handle */ | |
152 | vm_object_t backing_object; /* VM obj w/ encrypted data */ | |
3e170ce0 A |
153 | vm_object_offset_t backing_offset; |
154 | vm_object_offset_t crypto_backing_offset; /* for key... */ | |
155 | vm_object_offset_t crypto_start; | |
156 | vm_object_offset_t crypto_end; | |
157 | struct pager_crypt_info *crypt_info; | |
0c530ab8 A |
158 | } *apple_protect_pager_t; |
159 | #define APPLE_PROTECT_PAGER_NULL ((apple_protect_pager_t) NULL) | |
b0d623f7 | 160 | #define pager_ikot pager_header.io_bits |
0c530ab8 A |
161 | |
162 | /* | |
163 | * List of memory objects managed by this EMM. | |
164 | * The list is protected by the "apple_protect_pager_lock" lock. | |
165 | */ | |
166 | int apple_protect_pager_count = 0; /* number of pagers */ | |
167 | int apple_protect_pager_count_mapped = 0; /* number of unmapped pagers */ | |
168 | queue_head_t apple_protect_pager_queue; | |
b0d623f7 | 169 | decl_lck_mtx_data(,apple_protect_pager_lock) |
0c530ab8 A |
170 | |
171 | /* | |
172 | * Maximum number of unmapped pagers we're willing to keep around. | |
173 | */ | |
490019cf | 174 | int apple_protect_pager_cache_limit = 20; |
0c530ab8 A |
175 | |
176 | /* | |
177 | * Statistics & counters. | |
178 | */ | |
179 | int apple_protect_pager_count_max = 0; | |
180 | int apple_protect_pager_count_unmapped_max = 0; | |
181 | int apple_protect_pager_num_trim_max = 0; | |
182 | int apple_protect_pager_num_trim_total = 0; | |
183 | ||
b0d623f7 A |
184 | |
185 | lck_grp_t apple_protect_pager_lck_grp; | |
186 | lck_grp_attr_t apple_protect_pager_lck_grp_attr; | |
187 | lck_attr_t apple_protect_pager_lck_attr; | |
188 | ||
189 | ||
0c530ab8 | 190 | /* internal prototypes */ |
3e170ce0 A |
191 | apple_protect_pager_t apple_protect_pager_create( |
192 | vm_object_t backing_object, | |
193 | vm_object_offset_t backing_offset, | |
194 | vm_object_offset_t crypto_backing_offset, | |
195 | struct pager_crypt_info *crypt_info, | |
196 | vm_object_offset_t crypto_start, | |
197 | vm_object_offset_t crypto_end); | |
0c530ab8 A |
198 | apple_protect_pager_t apple_protect_pager_lookup(memory_object_t mem_obj); |
199 | void apple_protect_pager_dequeue(apple_protect_pager_t pager); | |
200 | void apple_protect_pager_deallocate_internal(apple_protect_pager_t pager, | |
201 | boolean_t locked); | |
202 | void apple_protect_pager_terminate_internal(apple_protect_pager_t pager); | |
203 | void apple_protect_pager_trim(void); | |
204 | ||
205 | ||
206 | #if DEBUG | |
207 | int apple_protect_pagerdebug = 0; | |
208 | #define PAGER_ALL 0xffffffff | |
209 | #define PAGER_INIT 0x00000001 | |
210 | #define PAGER_PAGEIN 0x00000002 | |
211 | ||
212 | #define PAGER_DEBUG(LEVEL, A) \ | |
213 | MACRO_BEGIN \ | |
214 | if ((apple_protect_pagerdebug & LEVEL)==LEVEL) { \ | |
215 | printf A; \ | |
216 | } \ | |
217 | MACRO_END | |
218 | #else | |
219 | #define PAGER_DEBUG(LEVEL, A) | |
220 | #endif | |
221 | ||
222 | ||
223 | void | |
224 | apple_protect_pager_bootstrap(void) | |
225 | { | |
b0d623f7 A |
226 | lck_grp_attr_setdefault(&apple_protect_pager_lck_grp_attr); |
227 | lck_grp_init(&apple_protect_pager_lck_grp, "apple_protect", &apple_protect_pager_lck_grp_attr); | |
228 | lck_attr_setdefault(&apple_protect_pager_lck_attr); | |
229 | lck_mtx_init(&apple_protect_pager_lock, &apple_protect_pager_lck_grp, &apple_protect_pager_lck_attr); | |
0c530ab8 A |
230 | queue_init(&apple_protect_pager_queue); |
231 | } | |
232 | ||
233 | /* | |
234 | * apple_protect_pager_init() | |
235 | * | |
236 | * Initialize the memory object and makes it ready to be used and mapped. | |
237 | */ | |
238 | kern_return_t | |
239 | apple_protect_pager_init( | |
240 | memory_object_t mem_obj, | |
241 | memory_object_control_t control, | |
242 | #if !DEBUG | |
243 | __unused | |
244 | #endif | |
b0d623f7 | 245 | memory_object_cluster_size_t pg_size) |
0c530ab8 A |
246 | { |
247 | apple_protect_pager_t pager; | |
248 | kern_return_t kr; | |
249 | memory_object_attr_info_data_t attributes; | |
250 | ||
251 | PAGER_DEBUG(PAGER_ALL, | |
252 | ("apple_protect_pager_init: %p, %p, %x\n", | |
253 | mem_obj, control, pg_size)); | |
254 | ||
255 | if (control == MEMORY_OBJECT_CONTROL_NULL) | |
256 | return KERN_INVALID_ARGUMENT; | |
257 | ||
258 | pager = apple_protect_pager_lookup(mem_obj); | |
259 | ||
260 | memory_object_control_reference(control); | |
261 | ||
262 | pager->pager_control = control; | |
263 | ||
264 | attributes.copy_strategy = MEMORY_OBJECT_COPY_DELAY; | |
265 | /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/ | |
266 | attributes.cluster_size = (1 << (PAGE_SHIFT)); | |
267 | attributes.may_cache_object = FALSE; | |
268 | attributes.temporary = TRUE; | |
269 | ||
270 | kr = memory_object_change_attributes( | |
271 | control, | |
272 | MEMORY_OBJECT_ATTRIBUTE_INFO, | |
273 | (memory_object_info_t) &attributes, | |
274 | MEMORY_OBJECT_ATTR_INFO_COUNT); | |
275 | if (kr != KERN_SUCCESS) | |
276 | panic("apple_protect_pager_init: " | |
277 | "memory_object_change_attributes() failed"); | |
278 | ||
39037602 A |
279 | #if CONFIG_SECLUDED_MEMORY |
280 | if (secluded_for_filecache) { | |
281 | memory_object_mark_eligible_for_secluded(control, TRUE); | |
282 | } | |
283 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
284 | ||
0c530ab8 A |
285 | return KERN_SUCCESS; |
286 | } | |
287 | ||
288 | /* | |
289 | * apple_protect_data_return() | |
290 | * | |
291 | * Handles page-out requests from VM. This should never happen since | |
292 | * the pages provided by this EMM are not supposed to be dirty or dirtied | |
293 | * and VM should simply discard the contents and reclaim the pages if it | |
294 | * needs to. | |
295 | */ | |
296 | kern_return_t | |
297 | apple_protect_pager_data_return( | |
298 | __unused memory_object_t mem_obj, | |
299 | __unused memory_object_offset_t offset, | |
b0d623f7 | 300 | __unused memory_object_cluster_size_t data_cnt, |
0c530ab8 A |
301 | __unused memory_object_offset_t *resid_offset, |
302 | __unused int *io_error, | |
303 | __unused boolean_t dirty, | |
304 | __unused boolean_t kernel_copy, | |
305 | __unused int upl_flags) | |
306 | { | |
307 | panic("apple_protect_pager_data_return: should never get called"); | |
308 | return KERN_FAILURE; | |
309 | } | |
310 | ||
311 | kern_return_t | |
312 | apple_protect_pager_data_initialize( | |
313 | __unused memory_object_t mem_obj, | |
314 | __unused memory_object_offset_t offset, | |
b0d623f7 | 315 | __unused memory_object_cluster_size_t data_cnt) |
0c530ab8 A |
316 | { |
317 | panic("apple_protect_pager_data_initialize: should never get called"); | |
318 | return KERN_FAILURE; | |
319 | } | |
320 | ||
321 | kern_return_t | |
322 | apple_protect_pager_data_unlock( | |
323 | __unused memory_object_t mem_obj, | |
324 | __unused memory_object_offset_t offset, | |
b0d623f7 | 325 | __unused memory_object_size_t size, |
0c530ab8 A |
326 | __unused vm_prot_t desired_access) |
327 | { | |
328 | return KERN_FAILURE; | |
329 | } | |
330 | ||
331 | /* | |
332 | * apple_protect_pager_data_request() | |
333 | * | |
334 | * Handles page-in requests from VM. | |
335 | */ | |
3e170ce0 | 336 | int apple_protect_pager_data_request_debug = 0; |
0c530ab8 A |
337 | kern_return_t |
338 | apple_protect_pager_data_request( | |
339 | memory_object_t mem_obj, | |
340 | memory_object_offset_t offset, | |
b0d623f7 | 341 | memory_object_cluster_size_t length, |
0c530ab8 A |
342 | #if !DEBUG |
343 | __unused | |
344 | #endif | |
2d21ac55 A |
345 | vm_prot_t protection_required, |
346 | memory_object_fault_info_t mo_fault_info) | |
0c530ab8 A |
347 | { |
348 | apple_protect_pager_t pager; | |
349 | memory_object_control_t mo_control; | |
2d21ac55 | 350 | upl_t upl; |
0c530ab8 A |
351 | int upl_flags; |
352 | upl_size_t upl_size; | |
b0d623f7 | 353 | upl_page_info_t *upl_pl; |
593a1d5f | 354 | unsigned int pl_count; |
39037602 | 355 | vm_object_t src_top_object, src_page_object, dst_object; |
0c530ab8 | 356 | kern_return_t kr, retval; |
2d21ac55 | 357 | vm_map_offset_t kernel_mapping; |
0c530ab8 A |
358 | vm_offset_t src_vaddr, dst_vaddr; |
359 | vm_offset_t cur_offset; | |
fe8ab488 | 360 | vm_offset_t offset_in_page; |
2d21ac55 A |
361 | kern_return_t error_code; |
362 | vm_prot_t prot; | |
363 | vm_page_t src_page, top_page; | |
364 | int interruptible; | |
b0d623f7 A |
365 | struct vm_object_fault_info fault_info; |
366 | int ret; | |
2d21ac55 A |
367 | |
368 | PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_data_request: %p, %llx, %x, %x\n", mem_obj, offset, length, protection_required)); | |
0c530ab8 | 369 | |
b0d623f7 | 370 | retval = KERN_SUCCESS; |
39037602 A |
371 | src_top_object = VM_OBJECT_NULL; |
372 | src_page_object = VM_OBJECT_NULL; | |
2d21ac55 A |
373 | kernel_mapping = 0; |
374 | upl = NULL; | |
593a1d5f | 375 | upl_pl = NULL; |
b0d623f7 A |
376 | fault_info = *((struct vm_object_fault_info *) mo_fault_info); |
377 | fault_info.stealth = TRUE; | |
6d2010ae | 378 | fault_info.io_sync = FALSE; |
0b4c1975 | 379 | fault_info.mark_zf_absent = FALSE; |
316670eb | 380 | fault_info.batch_pmap_op = FALSE; |
b0d623f7 | 381 | interruptible = fault_info.interruptible; |
0c530ab8 A |
382 | |
383 | pager = apple_protect_pager_lookup(mem_obj); | |
384 | assert(pager->is_ready); | |
385 | assert(pager->ref_count > 1); /* pager is alive and mapped */ | |
386 | ||
2d21ac55 | 387 | PAGER_DEBUG(PAGER_PAGEIN, ("apple_protect_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj, offset, length, protection_required, pager)); |
0c530ab8 A |
388 | |
389 | /* | |
390 | * Gather in a UPL all the VM pages requested by VM. | |
391 | */ | |
392 | mo_control = pager->pager_control; | |
393 | ||
394 | upl_size = length; | |
395 | upl_flags = | |
396 | UPL_RET_ONLY_ABSENT | | |
397 | UPL_SET_LITE | | |
398 | UPL_NO_SYNC | | |
399 | UPL_CLEAN_IN_PLACE | /* triggers UPL_CLEAR_DIRTY */ | |
400 | UPL_SET_INTERNAL; | |
593a1d5f | 401 | pl_count = 0; |
0c530ab8 A |
402 | kr = memory_object_upl_request(mo_control, |
403 | offset, upl_size, | |
404 | &upl, NULL, NULL, upl_flags); | |
405 | if (kr != KERN_SUCCESS) { | |
406 | retval = kr; | |
407 | goto done; | |
408 | } | |
2d21ac55 A |
409 | dst_object = mo_control->moc_object; |
410 | assert(dst_object != VM_OBJECT_NULL); | |
411 | ||
0c530ab8 | 412 | |
3e170ce0 A |
413 | #if __x86_64__ || __arm__ || __arm64__ |
414 | /* we'll use the 1-to-1 mapping of physical memory */ | |
415 | src_vaddr = 0; | |
416 | dst_vaddr = 0; | |
417 | #else /* __x86_64__ || __arm__ || __arm64__ */ | |
0c530ab8 | 418 | /* |
2d21ac55 A |
419 | * Reserve 2 virtual pages in the kernel address space to map each |
420 | * source and destination physical pages when it's their turn to | |
421 | * be processed. | |
0c530ab8 | 422 | */ |
3e170ce0 A |
423 | vm_map_entry_t map_entry; |
424 | ||
0c530ab8 A |
425 | vm_object_reference(kernel_object); /* ref. for mapping */ |
426 | kr = vm_map_find_space(kernel_map, | |
2d21ac55 A |
427 | &kernel_mapping, |
428 | 2 * PAGE_SIZE_64, | |
0c530ab8 A |
429 | 0, |
430 | 0, | |
431 | &map_entry); | |
432 | if (kr != KERN_SUCCESS) { | |
433 | vm_object_deallocate(kernel_object); | |
434 | retval = kr; | |
435 | goto done; | |
436 | } | |
437 | map_entry->object.vm_object = kernel_object; | |
b0d623f7 | 438 | map_entry->offset = kernel_mapping; |
0c530ab8 | 439 | vm_map_unlock(kernel_map); |
2d21ac55 A |
440 | src_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping); |
441 | dst_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping + PAGE_SIZE_64); | |
3e170ce0 | 442 | #endif /* __x86_64__ || __arm__ || __arm64__ */ |
2d21ac55 A |
443 | |
444 | /* | |
445 | * We'll map the encrypted data in the kernel address space from the | |
446 | * backing VM object (itself backed by the encrypted file via | |
447 | * the vnode pager). | |
448 | */ | |
39037602 A |
449 | src_top_object = pager->backing_object; |
450 | assert(src_top_object != VM_OBJECT_NULL); | |
451 | vm_object_reference(src_top_object); /* keep the source object alive */ | |
0c530ab8 A |
452 | |
453 | /* | |
454 | * Fill in the contents of the pages requested by VM. | |
455 | */ | |
456 | upl_pl = UPL_GET_INTERNAL_PAGE_LIST(upl); | |
593a1d5f | 457 | pl_count = length / PAGE_SIZE; |
b0d623f7 A |
458 | for (cur_offset = 0; |
459 | retval == KERN_SUCCESS && cur_offset < length; | |
460 | cur_offset += PAGE_SIZE) { | |
0c530ab8 A |
461 | ppnum_t dst_pnum; |
462 | ||
b0d623f7 | 463 | if (!upl_page_present(upl_pl, (int)(cur_offset / PAGE_SIZE))) { |
0c530ab8 A |
464 | /* this page is not in the UPL: skip it */ |
465 | continue; | |
466 | } | |
467 | ||
468 | /* | |
469 | * Map the source (encrypted) page in the kernel's | |
470 | * virtual address space. | |
39037602 | 471 | * We already hold a reference on the src_top_object. |
0c530ab8 | 472 | */ |
2d21ac55 | 473 | retry_src_fault: |
39037602 A |
474 | vm_object_lock(src_top_object); |
475 | vm_object_paging_begin(src_top_object); | |
2d21ac55 A |
476 | error_code = 0; |
477 | prot = VM_PROT_READ; | |
39236c6e | 478 | src_page = VM_PAGE_NULL; |
39037602 | 479 | kr = vm_fault_page(src_top_object, |
3e170ce0 | 480 | pager->backing_offset + offset + cur_offset, |
2d21ac55 A |
481 | VM_PROT_READ, |
482 | FALSE, | |
39236c6e | 483 | FALSE, /* src_page not looked up */ |
2d21ac55 A |
484 | &prot, |
485 | &src_page, | |
486 | &top_page, | |
b0d623f7 | 487 | NULL, |
2d21ac55 A |
488 | &error_code, |
489 | FALSE, | |
490 | FALSE, | |
b0d623f7 | 491 | &fault_info); |
2d21ac55 A |
492 | switch (kr) { |
493 | case VM_FAULT_SUCCESS: | |
494 | break; | |
495 | case VM_FAULT_RETRY: | |
496 | goto retry_src_fault; | |
497 | case VM_FAULT_MEMORY_SHORTAGE: | |
498 | if (vm_page_wait(interruptible)) { | |
499 | goto retry_src_fault; | |
0c530ab8 | 500 | } |
2d21ac55 A |
501 | /* fall thru */ |
502 | case VM_FAULT_INTERRUPTED: | |
503 | retval = MACH_SEND_INTERRUPTED; | |
504 | goto done; | |
b0d623f7 A |
505 | case VM_FAULT_SUCCESS_NO_VM_PAGE: |
506 | /* success but no VM page: fail */ | |
39037602 A |
507 | vm_object_paging_end(src_top_object); |
508 | vm_object_unlock(src_top_object); | |
b0d623f7 | 509 | /*FALLTHROUGH*/ |
2d21ac55 A |
510 | case VM_FAULT_MEMORY_ERROR: |
511 | /* the page is not there ! */ | |
512 | if (error_code) { | |
513 | retval = error_code; | |
514 | } else { | |
515 | retval = KERN_MEMORY_ERROR; | |
0c530ab8 | 516 | } |
2d21ac55 A |
517 | goto done; |
518 | default: | |
b0d623f7 A |
519 | panic("apple_protect_pager_data_request: " |
520 | "vm_fault_page() unexpected error 0x%x\n", | |
521 | kr); | |
0c530ab8 | 522 | } |
2d21ac55 A |
523 | assert(src_page != VM_PAGE_NULL); |
524 | assert(src_page->busy); | |
b0d623f7 | 525 | |
39037602 A |
526 | if (( !VM_PAGE_NON_SPECULATIVE_PAGEABLE(src_page))) { |
527 | ||
b0d623f7 | 528 | vm_page_lockspin_queues(); |
39037602 A |
529 | |
530 | if (( !VM_PAGE_NON_SPECULATIVE_PAGEABLE(src_page))) { | |
b0d623f7 A |
531 | vm_page_deactivate(src_page); |
532 | } | |
533 | vm_page_unlock_queues(); | |
534 | } | |
3e170ce0 | 535 | |
2d21ac55 A |
536 | /* |
537 | * Establish an explicit mapping of the source | |
538 | * physical page. | |
539 | */ | |
3e170ce0 A |
540 | #if __x86_64__ |
541 | src_vaddr = (vm_map_offset_t) | |
39037602 | 542 | PHYSMAP_PTOV((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(src_page) |
3e170ce0 A |
543 | << PAGE_SHIFT); |
544 | #else | |
2d21ac55 | 545 | pmap_enter(kernel_pmap, |
3e170ce0 | 546 | src_vaddr, |
39037602 | 547 | VM_PAGE_GET_PHYS_PAGE(src_page), |
2d21ac55 | 548 | VM_PROT_READ, |
316670eb | 549 | VM_PROT_NONE, |
6d2010ae | 550 | 0, |
2d21ac55 | 551 | TRUE); |
3e170ce0 | 552 | #endif |
0c530ab8 A |
553 | /* |
554 | * Establish an explicit pmap mapping of the destination | |
555 | * physical page. | |
556 | * We can't do a regular VM mapping because the VM page | |
557 | * is "busy". | |
558 | */ | |
b0d623f7 A |
559 | dst_pnum = (ppnum_t) |
560 | upl_phys_page(upl_pl, (int)(cur_offset / PAGE_SIZE)); | |
0c530ab8 | 561 | assert(dst_pnum != 0); |
3e170ce0 A |
562 | #if __x86_64__ |
563 | dst_vaddr = (vm_map_offset_t) | |
564 | PHYSMAP_PTOV((pmap_paddr_t)dst_pnum << PAGE_SHIFT); | |
565 | #else | |
2d21ac55 | 566 | pmap_enter(kernel_pmap, |
3e170ce0 | 567 | dst_vaddr, |
2d21ac55 | 568 | dst_pnum, |
0c530ab8 | 569 | VM_PROT_READ | VM_PROT_WRITE, |
316670eb | 570 | VM_PROT_NONE, |
6d2010ae | 571 | 0, |
2d21ac55 | 572 | TRUE); |
3e170ce0 | 573 | #endif |
39037602 | 574 | src_page_object = VM_PAGE_OBJECT(src_page); |
3e170ce0 A |
575 | |
576 | /* | |
577 | * Validate the original page... | |
578 | */ | |
39037602 | 579 | if (src_page_object->code_signed) { |
3e170ce0 A |
580 | vm_page_validate_cs_mapped( |
581 | src_page, | |
582 | (const void *) src_vaddr); | |
583 | } | |
584 | /* | |
585 | * ... and transfer the results to the destination page. | |
586 | */ | |
587 | UPL_SET_CS_VALIDATED(upl_pl, cur_offset / PAGE_SIZE, | |
588 | src_page->cs_validated); | |
589 | UPL_SET_CS_TAINTED(upl_pl, cur_offset / PAGE_SIZE, | |
590 | src_page->cs_tainted); | |
591 | UPL_SET_CS_NX(upl_pl, cur_offset / PAGE_SIZE, | |
592 | src_page->cs_nx); | |
593 | ||
594 | /* | |
595 | * page_decrypt() might access a mapped file, so let's release | |
596 | * the object lock for the source page to avoid a potential | |
597 | * deadlock. The source page is kept busy and we have a | |
598 | * "paging_in_progress" reference on its object, so it's safe | |
599 | * to unlock the object here. | |
600 | */ | |
601 | assert(src_page->busy); | |
39037602 A |
602 | assert(src_page_object->paging_in_progress > 0); |
603 | vm_object_unlock(src_page_object); | |
0c530ab8 A |
604 | |
605 | /* | |
606 | * Decrypt the encrypted contents of the source page | |
607 | * into the destination page. | |
608 | */ | |
fe8ab488 A |
609 | for (offset_in_page = 0; |
610 | offset_in_page < PAGE_SIZE; | |
611 | offset_in_page += 4096) { | |
3e170ce0 A |
612 | if (offset + cur_offset + offset_in_page < |
613 | pager->crypto_start || | |
614 | offset + cur_offset + offset_in_page >= | |
615 | pager->crypto_end) { | |
616 | /* not encrypted: just copy */ | |
617 | bcopy((const char *)(src_vaddr + | |
618 | offset_in_page), | |
619 | (char *)(dst_vaddr + offset_in_page), | |
620 | 4096); | |
621 | if (apple_protect_pager_data_request_debug) { | |
622 | printf("apple_protect_data_request" | |
623 | "(%p,0x%llx+0x%llx+0x%04llx): " | |
624 | "out of crypto range " | |
625 | "[0x%llx:0x%llx]: " | |
626 | "COPY [0x%016llx 0x%016llx] " | |
627 | "code_signed=%d " | |
628 | "cs_validated=%d " | |
629 | "cs_tainted=%d " | |
630 | "cs_nx=%d\n", | |
631 | pager, | |
632 | offset, | |
633 | (uint64_t) cur_offset, | |
634 | (uint64_t) offset_in_page, | |
635 | pager->crypto_start, | |
636 | pager->crypto_end, | |
637 | *(uint64_t *)(dst_vaddr+ | |
638 | offset_in_page), | |
639 | *(uint64_t *)(dst_vaddr+ | |
640 | offset_in_page+8), | |
39037602 | 641 | src_page_object->code_signed, |
3e170ce0 A |
642 | src_page->cs_validated, |
643 | src_page->cs_tainted, | |
644 | src_page->cs_nx); | |
645 | } | |
646 | ret = 0; | |
647 | continue; | |
648 | } | |
649 | ret = pager->crypt_info->page_decrypt( | |
650 | (const void *)(src_vaddr + offset_in_page), | |
651 | (void *)(dst_vaddr + offset_in_page), | |
652 | ((pager->crypto_backing_offset - | |
653 | pager->crypto_start) + /* XXX ? */ | |
654 | offset + | |
655 | cur_offset + | |
656 | offset_in_page), | |
657 | pager->crypt_info->crypt_ops); | |
658 | if (apple_protect_pager_data_request_debug) { | |
659 | printf("apple_protect_data_request" | |
660 | "(%p,0x%llx+0x%llx+0x%04llx): " | |
661 | "in crypto range [0x%llx:0x%llx]: " | |
662 | "DECRYPT offset 0x%llx=" | |
663 | "(0x%llx-0x%llx+0x%llx+0x%llx+0x%04llx)" | |
664 | "[0x%016llx 0x%016llx] " | |
665 | "code_signed=%d " | |
666 | "cs_validated=%d " | |
667 | "cs_tainted=%d " | |
668 | "cs_nx=%d " | |
669 | "ret=0x%x\n", | |
670 | pager, | |
671 | offset, | |
672 | (uint64_t) cur_offset, | |
673 | (uint64_t) offset_in_page, | |
674 | pager->crypto_start, pager->crypto_end, | |
675 | ((pager->crypto_backing_offset - | |
676 | pager->crypto_start) + | |
677 | offset + | |
678 | cur_offset + | |
679 | offset_in_page), | |
680 | pager->crypto_backing_offset, | |
681 | pager->crypto_start, | |
682 | offset, | |
683 | (uint64_t) cur_offset, | |
684 | (uint64_t) offset_in_page, | |
685 | *(uint64_t *)(dst_vaddr+offset_in_page), | |
686 | *(uint64_t *)(dst_vaddr+offset_in_page+8), | |
39037602 | 687 | src_page_object->code_signed, |
3e170ce0 A |
688 | src_page->cs_validated, |
689 | src_page->cs_tainted, | |
690 | src_page->cs_nx, | |
691 | ret); | |
692 | } | |
fe8ab488 A |
693 | if (ret) { |
694 | break; | |
695 | } | |
696 | } | |
b0d623f7 A |
697 | if (ret) { |
698 | /* | |
699 | * Decryption failed. Abort the fault. | |
700 | */ | |
701 | retval = KERN_ABORTED; | |
b0d623f7 | 702 | } |
3e170ce0 | 703 | |
39037602 | 704 | assert(VM_PAGE_OBJECT(src_page) == src_page_object); |
3e170ce0 | 705 | assert(src_page->busy); |
39037602 A |
706 | assert(src_page_object->paging_in_progress > 0); |
707 | vm_object_lock(src_page_object); | |
3e170ce0 A |
708 | |
709 | #if __x86_64__ || __arm__ || __arm64__ | |
710 | /* we used the 1-to-1 mapping of physical memory */ | |
711 | src_vaddr = 0; | |
712 | dst_vaddr = 0; | |
713 | #else /* __x86_64__ || __arm__ || __arm64__ */ | |
0c530ab8 | 714 | /* |
2d21ac55 | 715 | * Remove the pmap mapping of the source and destination pages |
0c530ab8 A |
716 | * in the kernel. |
717 | */ | |
718 | pmap_remove(kernel_pmap, | |
2d21ac55 A |
719 | (addr64_t) kernel_mapping, |
720 | (addr64_t) (kernel_mapping + (2 * PAGE_SIZE_64))); | |
3e170ce0 | 721 | #endif /* __x86_64__ || __arm__ || __arm64__ */ |
2d21ac55 A |
722 | |
723 | /* | |
724 | * Cleanup the result of vm_fault_page() of the source page. | |
725 | */ | |
39037602 A |
726 | if (retval == KERN_SUCCESS && |
727 | src_page->busy && | |
728 | !VM_PAGE_WIRED(src_page) && | |
729 | !src_page->dirty && | |
730 | !src_page->precious && | |
731 | !src_page->laundry && | |
732 | !src_page->cleaning) { | |
733 | int refmod_state; | |
734 | ||
735 | refmod_state = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(src_page)); | |
736 | ||
737 | if (refmod_state & VM_MEM_MODIFIED) { | |
738 | SET_PAGE_DIRTY(src_page, FALSE); | |
739 | } | |
740 | if (!src_page->dirty) { | |
741 | vm_page_free_unlocked(src_page, TRUE); | |
742 | src_page = VM_PAGE_NULL; | |
743 | } else { | |
744 | PAGE_WAKEUP_DONE(src_page); | |
745 | } | |
746 | } else { | |
747 | PAGE_WAKEUP_DONE(src_page); | |
748 | } | |
749 | src_page = VM_PAGE_NULL; | |
750 | vm_object_paging_end(src_page_object); | |
751 | vm_object_unlock(src_page_object); | |
2d21ac55 | 752 | if (top_page != VM_PAGE_NULL) { |
39037602 A |
753 | assert(VM_PAGE_OBJECT(top_page) == src_top_object); |
754 | vm_object_lock(src_top_object); | |
2d21ac55 | 755 | VM_PAGE_FREE(top_page); |
39037602 A |
756 | vm_object_paging_end(src_top_object); |
757 | vm_object_unlock(src_top_object); | |
0c530ab8 A |
758 | } |
759 | } | |
760 | ||
0c530ab8 | 761 | done: |
0c530ab8 A |
762 | if (upl != NULL) { |
763 | /* clean up the UPL */ | |
764 | ||
765 | /* | |
766 | * The pages are currently dirty because we've just been | |
767 | * writing on them, but as far as we're concerned, they're | |
768 | * clean since they contain their "original" contents as | |
769 | * provided by us, the pager. | |
770 | * Tell the UPL to mark them "clean". | |
771 | */ | |
772 | upl_clear_dirty(upl, TRUE); | |
773 | ||
774 | /* abort or commit the UPL */ | |
775 | if (retval != KERN_SUCCESS) { | |
776 | upl_abort(upl, 0); | |
b0d623f7 A |
777 | if (retval == KERN_ABORTED) { |
778 | wait_result_t wait_result; | |
779 | ||
780 | /* | |
781 | * We aborted the fault and did not provide | |
782 | * any contents for the requested pages but | |
783 | * the pages themselves are not invalid, so | |
784 | * let's return success and let the caller | |
785 | * retry the fault, in case it might succeed | |
786 | * later (when the decryption code is up and | |
787 | * running in the kernel, for example). | |
788 | */ | |
789 | retval = KERN_SUCCESS; | |
790 | /* | |
791 | * Wait a little bit first to avoid using | |
792 | * too much CPU time retrying and failing | |
793 | * the same fault over and over again. | |
794 | */ | |
795 | wait_result = assert_wait_timeout( | |
796 | (event_t) apple_protect_pager_data_request, | |
797 | THREAD_UNINT, | |
798 | 10000, /* 10ms */ | |
799 | NSEC_PER_USEC); | |
800 | assert(wait_result == THREAD_WAITING); | |
801 | wait_result = thread_block(THREAD_CONTINUE_NULL); | |
802 | assert(wait_result == THREAD_TIMED_OUT); | |
803 | } | |
0c530ab8 | 804 | } else { |
593a1d5f A |
805 | boolean_t empty; |
806 | upl_commit_range(upl, 0, upl->size, | |
15129b1c | 807 | UPL_COMMIT_CS_VALIDATED | UPL_COMMIT_WRITTEN_BY_KERNEL, |
593a1d5f | 808 | upl_pl, pl_count, &empty); |
0c530ab8 A |
809 | } |
810 | ||
811 | /* and deallocate the UPL */ | |
812 | upl_deallocate(upl); | |
813 | upl = NULL; | |
814 | } | |
2d21ac55 A |
815 | if (kernel_mapping != 0) { |
816 | /* clean up the mapping of the source and destination pages */ | |
0c530ab8 | 817 | kr = vm_map_remove(kernel_map, |
2d21ac55 A |
818 | kernel_mapping, |
819 | kernel_mapping + (2 * PAGE_SIZE_64), | |
0c530ab8 A |
820 | VM_MAP_NO_FLAGS); |
821 | assert(kr == KERN_SUCCESS); | |
2d21ac55 A |
822 | kernel_mapping = 0; |
823 | src_vaddr = 0; | |
0c530ab8 A |
824 | dst_vaddr = 0; |
825 | } | |
39037602 A |
826 | if (src_top_object != VM_OBJECT_NULL) { |
827 | vm_object_deallocate(src_top_object); | |
2d21ac55 | 828 | } |
0c530ab8 A |
829 | |
830 | return retval; | |
831 | } | |
832 | ||
833 | /* | |
834 | * apple_protect_pager_reference() | |
835 | * | |
836 | * Get a reference on this memory object. | |
837 | * For external usage only. Assumes that the initial reference count is not 0, | |
838 | * i.e one should not "revive" a dead pager this way. | |
839 | */ | |
840 | void | |
841 | apple_protect_pager_reference( | |
842 | memory_object_t mem_obj) | |
843 | { | |
844 | apple_protect_pager_t pager; | |
845 | ||
846 | pager = apple_protect_pager_lookup(mem_obj); | |
847 | ||
b0d623f7 | 848 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
849 | assert(pager->ref_count > 0); |
850 | pager->ref_count++; | |
b0d623f7 | 851 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
852 | } |
853 | ||
854 | ||
855 | /* | |
856 | * apple_protect_pager_dequeue: | |
857 | * | |
858 | * Removes a pager from the list of pagers. | |
859 | * | |
860 | * The caller must hold "apple_protect_pager_lock". | |
861 | */ | |
862 | void | |
863 | apple_protect_pager_dequeue( | |
864 | apple_protect_pager_t pager) | |
865 | { | |
866 | assert(!pager->is_mapped); | |
867 | ||
868 | queue_remove(&apple_protect_pager_queue, | |
869 | pager, | |
870 | apple_protect_pager_t, | |
871 | pager_queue); | |
872 | pager->pager_queue.next = NULL; | |
873 | pager->pager_queue.prev = NULL; | |
874 | ||
875 | apple_protect_pager_count--; | |
876 | } | |
877 | ||
878 | /* | |
879 | * apple_protect_pager_terminate_internal: | |
880 | * | |
881 | * Trigger the asynchronous termination of the memory object associated | |
882 | * with this pager. | |
883 | * When the memory object is terminated, there will be one more call | |
884 | * to memory_object_deallocate() (i.e. apple_protect_pager_deallocate()) | |
885 | * to finish the clean up. | |
886 | * | |
887 | * "apple_protect_pager_lock" should not be held by the caller. | |
888 | * We don't need the lock because the pager has already been removed from | |
889 | * the pagers' list and is now ours exclusively. | |
890 | */ | |
891 | void | |
892 | apple_protect_pager_terminate_internal( | |
893 | apple_protect_pager_t pager) | |
894 | { | |
895 | assert(pager->is_ready); | |
896 | assert(!pager->is_mapped); | |
897 | ||
898 | if (pager->backing_object != VM_OBJECT_NULL) { | |
899 | vm_object_deallocate(pager->backing_object); | |
900 | pager->backing_object = VM_OBJECT_NULL; | |
901 | } | |
3e170ce0 A |
902 | |
903 | /* one less pager using this "pager_crypt_info" */ | |
904 | #if CRYPT_INFO_DEBUG | |
905 | printf("CRYPT_INFO %s: deallocate %p ref %d\n", | |
906 | __FUNCTION__, | |
907 | pager->crypt_info, | |
908 | pager->crypt_info->crypt_refcnt); | |
909 | #endif /* CRYPT_INFO_DEBUG */ | |
910 | crypt_info_deallocate(pager->crypt_info); | |
911 | pager->crypt_info = NULL; | |
6d2010ae A |
912 | |
913 | /* trigger the destruction of the memory object */ | |
914 | memory_object_destroy(pager->pager_control, 0); | |
0c530ab8 A |
915 | } |
916 | ||
917 | /* | |
918 | * apple_protect_pager_deallocate_internal() | |
919 | * | |
920 | * Release a reference on this pager and free it when the last | |
921 | * reference goes away. | |
922 | * Can be called with apple_protect_pager_lock held or not but always returns | |
923 | * with it unlocked. | |
924 | */ | |
925 | void | |
926 | apple_protect_pager_deallocate_internal( | |
927 | apple_protect_pager_t pager, | |
928 | boolean_t locked) | |
929 | { | |
930 | boolean_t needs_trimming; | |
931 | int count_unmapped; | |
932 | ||
933 | if (! locked) { | |
b0d623f7 | 934 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
935 | } |
936 | ||
937 | count_unmapped = (apple_protect_pager_count - | |
938 | apple_protect_pager_count_mapped); | |
939 | if (count_unmapped > apple_protect_pager_cache_limit) { | |
940 | /* we have too many unmapped pagers: trim some */ | |
941 | needs_trimming = TRUE; | |
942 | } else { | |
943 | needs_trimming = FALSE; | |
944 | } | |
945 | ||
946 | /* drop a reference on this pager */ | |
947 | pager->ref_count--; | |
948 | ||
949 | if (pager->ref_count == 1) { | |
950 | /* | |
951 | * Only the "named" reference is left, which means that | |
2d21ac55 | 952 | * no one is really holding on to this pager anymore. |
0c530ab8 A |
953 | * Terminate it. |
954 | */ | |
955 | apple_protect_pager_dequeue(pager); | |
956 | /* the pager is all ours: no need for the lock now */ | |
b0d623f7 | 957 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
958 | apple_protect_pager_terminate_internal(pager); |
959 | } else if (pager->ref_count == 0) { | |
960 | /* | |
961 | * Dropped the existence reference; the memory object has | |
962 | * been terminated. Do some final cleanup and release the | |
963 | * pager structure. | |
964 | */ | |
b0d623f7 | 965 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
966 | if (pager->pager_control != MEMORY_OBJECT_CONTROL_NULL) { |
967 | memory_object_control_deallocate(pager->pager_control); | |
968 | pager->pager_control = MEMORY_OBJECT_CONTROL_NULL; | |
969 | } | |
970 | kfree(pager, sizeof (*pager)); | |
971 | pager = APPLE_PROTECT_PAGER_NULL; | |
972 | } else { | |
973 | /* there are still plenty of references: keep going... */ | |
b0d623f7 | 974 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
975 | } |
976 | ||
977 | if (needs_trimming) { | |
978 | apple_protect_pager_trim(); | |
979 | } | |
980 | /* caution: lock is not held on return... */ | |
981 | } | |
982 | ||
983 | /* | |
984 | * apple_protect_pager_deallocate() | |
985 | * | |
986 | * Release a reference on this pager and free it when the last | |
987 | * reference goes away. | |
988 | */ | |
989 | void | |
990 | apple_protect_pager_deallocate( | |
991 | memory_object_t mem_obj) | |
992 | { | |
993 | apple_protect_pager_t pager; | |
994 | ||
2d21ac55 | 995 | PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_deallocate: %p\n", mem_obj)); |
0c530ab8 A |
996 | pager = apple_protect_pager_lookup(mem_obj); |
997 | apple_protect_pager_deallocate_internal(pager, FALSE); | |
998 | } | |
999 | ||
1000 | /* | |
1001 | * | |
1002 | */ | |
1003 | kern_return_t | |
1004 | apple_protect_pager_terminate( | |
1005 | #if !DEBUG | |
1006 | __unused | |
1007 | #endif | |
1008 | memory_object_t mem_obj) | |
1009 | { | |
2d21ac55 | 1010 | PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_terminate: %p\n", mem_obj)); |
0c530ab8 A |
1011 | |
1012 | return KERN_SUCCESS; | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * | |
1017 | */ | |
1018 | kern_return_t | |
1019 | apple_protect_pager_synchronize( | |
1020 | memory_object_t mem_obj, | |
1021 | memory_object_offset_t offset, | |
b0d623f7 | 1022 | memory_object_size_t length, |
0c530ab8 A |
1023 | __unused vm_sync_t sync_flags) |
1024 | { | |
1025 | apple_protect_pager_t pager; | |
1026 | ||
2d21ac55 | 1027 | PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_synchronize: %p\n", mem_obj)); |
0c530ab8 A |
1028 | |
1029 | pager = apple_protect_pager_lookup(mem_obj); | |
1030 | ||
1031 | memory_object_synchronize_completed(pager->pager_control, | |
1032 | offset, length); | |
1033 | ||
1034 | return KERN_SUCCESS; | |
1035 | } | |
1036 | ||
1037 | /* | |
1038 | * apple_protect_pager_map() | |
1039 | * | |
1040 | * This allows VM to let us, the EMM, know that this memory object | |
b0d623f7 A |
1041 | * is currently mapped one or more times. This is called by VM each time |
1042 | * the memory object gets mapped and we take one extra reference on the | |
0c530ab8 A |
1043 | * memory object to account for all its mappings. |
1044 | */ | |
593a1d5f | 1045 | kern_return_t |
0c530ab8 | 1046 | apple_protect_pager_map( |
593a1d5f A |
1047 | memory_object_t mem_obj, |
1048 | __unused vm_prot_t prot) | |
0c530ab8 A |
1049 | { |
1050 | apple_protect_pager_t pager; | |
1051 | ||
2d21ac55 | 1052 | PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_map: %p\n", mem_obj)); |
0c530ab8 A |
1053 | |
1054 | pager = apple_protect_pager_lookup(mem_obj); | |
1055 | ||
b0d623f7 | 1056 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
1057 | assert(pager->is_ready); |
1058 | assert(pager->ref_count > 0); /* pager is alive */ | |
1059 | if (pager->is_mapped == FALSE) { | |
1060 | /* | |
1061 | * First mapping of this pager: take an extra reference | |
1062 | * that will remain until all the mappings of this pager | |
1063 | * are removed. | |
1064 | */ | |
1065 | pager->is_mapped = TRUE; | |
1066 | pager->ref_count++; | |
1067 | apple_protect_pager_count_mapped++; | |
1068 | } | |
b0d623f7 | 1069 | lck_mtx_unlock(&apple_protect_pager_lock); |
593a1d5f A |
1070 | |
1071 | return KERN_SUCCESS; | |
0c530ab8 A |
1072 | } |
1073 | ||
1074 | /* | |
593a1d5f | 1075 | * apple_protect_pager_last_unmap() |
0c530ab8 A |
1076 | * |
1077 | * This is called by VM when this memory object is no longer mapped anywhere. | |
1078 | */ | |
1079 | kern_return_t | |
593a1d5f | 1080 | apple_protect_pager_last_unmap( |
0c530ab8 A |
1081 | memory_object_t mem_obj) |
1082 | { | |
1083 | apple_protect_pager_t pager; | |
1084 | int count_unmapped; | |
1085 | ||
593a1d5f A |
1086 | PAGER_DEBUG(PAGER_ALL, |
1087 | ("apple_protect_pager_last_unmap: %p\n", mem_obj)); | |
0c530ab8 A |
1088 | |
1089 | pager = apple_protect_pager_lookup(mem_obj); | |
1090 | ||
b0d623f7 | 1091 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
1092 | if (pager->is_mapped) { |
1093 | /* | |
1094 | * All the mappings are gone, so let go of the one extra | |
1095 | * reference that represents all the mappings of this pager. | |
1096 | */ | |
1097 | apple_protect_pager_count_mapped--; | |
1098 | count_unmapped = (apple_protect_pager_count - | |
1099 | apple_protect_pager_count_mapped); | |
1100 | if (count_unmapped > apple_protect_pager_count_unmapped_max) { | |
1101 | apple_protect_pager_count_unmapped_max = count_unmapped; | |
1102 | } | |
1103 | pager->is_mapped = FALSE; | |
1104 | apple_protect_pager_deallocate_internal(pager, TRUE); | |
1105 | /* caution: deallocate_internal() released the lock ! */ | |
1106 | } else { | |
b0d623f7 | 1107 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
1108 | } |
1109 | ||
1110 | return KERN_SUCCESS; | |
1111 | } | |
1112 | ||
1113 | ||
1114 | /* | |
1115 | * | |
1116 | */ | |
1117 | apple_protect_pager_t | |
1118 | apple_protect_pager_lookup( | |
1119 | memory_object_t mem_obj) | |
1120 | { | |
1121 | apple_protect_pager_t pager; | |
1122 | ||
1123 | pager = (apple_protect_pager_t) mem_obj; | |
1124 | assert(pager->pager_ops == &apple_protect_pager_ops); | |
1125 | assert(pager->ref_count > 0); | |
1126 | return pager; | |
1127 | } | |
1128 | ||
1129 | apple_protect_pager_t | |
1130 | apple_protect_pager_create( | |
3e170ce0 A |
1131 | vm_object_t backing_object, |
1132 | vm_object_offset_t backing_offset, | |
1133 | vm_object_offset_t crypto_backing_offset, | |
1134 | struct pager_crypt_info *crypt_info, | |
1135 | vm_object_offset_t crypto_start, | |
1136 | vm_object_offset_t crypto_end) | |
0c530ab8 A |
1137 | { |
1138 | apple_protect_pager_t pager, pager2; | |
1139 | memory_object_control_t control; | |
1140 | kern_return_t kr; | |
3e170ce0 | 1141 | struct pager_crypt_info *old_crypt_info; |
0c530ab8 A |
1142 | |
1143 | pager = (apple_protect_pager_t) kalloc(sizeof (*pager)); | |
1144 | if (pager == APPLE_PROTECT_PAGER_NULL) { | |
1145 | return APPLE_PROTECT_PAGER_NULL; | |
1146 | } | |
1147 | ||
1148 | /* | |
1149 | * The vm_map call takes both named entry ports and raw memory | |
1150 | * objects in the same parameter. We need to make sure that | |
1151 | * vm_map does not see this object as a named entry port. So, | |
b0d623f7 | 1152 | * we reserve the first word in the object for a fake ip_kotype |
0c530ab8 A |
1153 | * setting - that will tell vm_map to use it as a memory object. |
1154 | */ | |
1155 | pager->pager_ops = &apple_protect_pager_ops; | |
1156 | pager->pager_ikot = IKOT_MEMORY_OBJECT; | |
1157 | pager->is_ready = FALSE;/* not ready until it has a "name" */ | |
3e170ce0 A |
1158 | pager->ref_count = 1; /* existence reference (for the cache) */ |
1159 | pager->ref_count++; /* for the caller */ | |
0c530ab8 A |
1160 | pager->is_mapped = FALSE; |
1161 | pager->pager_control = MEMORY_OBJECT_CONTROL_NULL; | |
1162 | pager->backing_object = backing_object; | |
3e170ce0 A |
1163 | pager->backing_offset = backing_offset; |
1164 | pager->crypto_backing_offset = crypto_backing_offset; | |
1165 | pager->crypto_start = crypto_start; | |
1166 | pager->crypto_end = crypto_end; | |
1167 | pager->crypt_info = crypt_info; /* allocated by caller */ | |
1168 | ||
1169 | #if CRYPT_INFO_DEBUG | |
1170 | printf("CRYPT_INFO %s: crypt_info %p [%p,%p,%p,%d]\n", | |
1171 | __FUNCTION__, | |
1172 | crypt_info, | |
1173 | crypt_info->page_decrypt, | |
1174 | crypt_info->crypt_end, | |
1175 | crypt_info->crypt_ops, | |
1176 | crypt_info->crypt_refcnt); | |
1177 | #endif /* CRYPT_INFO_DEBUG */ | |
593a1d5f | 1178 | |
0c530ab8 A |
1179 | vm_object_reference(backing_object); |
1180 | ||
3e170ce0 A |
1181 | old_crypt_info = NULL; |
1182 | ||
b0d623f7 | 1183 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
1184 | /* see if anyone raced us to create a pager for the same object */ |
1185 | queue_iterate(&apple_protect_pager_queue, | |
1186 | pager2, | |
1187 | apple_protect_pager_t, | |
1188 | pager_queue) { | |
3e170ce0 A |
1189 | if ((pager2->crypt_info->page_decrypt != |
1190 | crypt_info->page_decrypt) || | |
1191 | (pager2->crypt_info->crypt_end != | |
1192 | crypt_info->crypt_end) || | |
1193 | (pager2->crypt_info->crypt_ops != | |
1194 | crypt_info->crypt_ops)) { | |
1195 | /* crypt_info contents do not match: next pager */ | |
1196 | continue; | |
1197 | } | |
1198 | ||
1199 | /* found a match for crypt_info ... */ | |
1200 | if (old_crypt_info) { | |
1201 | /* ... already switched to that crypt_info */ | |
1202 | assert(old_crypt_info == pager2->crypt_info); | |
1203 | } else if (pager2->crypt_info != crypt_info) { | |
1204 | /* ... switch to that pager's crypt_info */ | |
1205 | #if CRYPT_INFO_DEBUG | |
1206 | printf("CRYPT_INFO %s: reference %p ref %d " | |
1207 | "(create match)\n", | |
1208 | __FUNCTION__, | |
1209 | pager2->crypt_info, | |
1210 | pager2->crypt_info->crypt_refcnt); | |
1211 | #endif /* CRYPT_INFO_DEBUG */ | |
1212 | old_crypt_info = pager2->crypt_info; | |
1213 | crypt_info_reference(old_crypt_info); | |
1214 | pager->crypt_info = old_crypt_info; | |
1215 | } | |
1216 | ||
1217 | if (pager2->backing_object == backing_object && | |
1218 | pager2->backing_offset == backing_offset && | |
1219 | pager2->crypto_backing_offset == crypto_backing_offset && | |
1220 | pager2->crypto_start == crypto_start && | |
1221 | pager2->crypto_end == crypto_end) { | |
1222 | /* full match: use that pager */ | |
0c530ab8 A |
1223 | break; |
1224 | } | |
1225 | } | |
1226 | if (! queue_end(&apple_protect_pager_queue, | |
1227 | (queue_entry_t) pager2)) { | |
0c530ab8 | 1228 | /* we lost the race, down with the loser... */ |
b0d623f7 | 1229 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
1230 | vm_object_deallocate(pager->backing_object); |
1231 | pager->backing_object = VM_OBJECT_NULL; | |
3e170ce0 A |
1232 | #if CRYPT_INFO_DEBUG |
1233 | printf("CRYPT_INFO %s: %p ref %d (create pager match)\n", | |
1234 | __FUNCTION__, | |
1235 | pager->crypt_info, | |
1236 | pager->crypt_info->crypt_refcnt); | |
1237 | #endif /* CRYPT_INFO_DEBUG */ | |
1238 | crypt_info_deallocate(pager->crypt_info); | |
1239 | pager->crypt_info = NULL; | |
0c530ab8 A |
1240 | kfree(pager, sizeof (*pager)); |
1241 | /* ... and go with the winner */ | |
1242 | pager = pager2; | |
1243 | /* let the winner make sure the pager gets ready */ | |
1244 | return pager; | |
1245 | } | |
1246 | ||
1247 | /* enter new pager at the head of our list of pagers */ | |
1248 | queue_enter_first(&apple_protect_pager_queue, | |
1249 | pager, | |
1250 | apple_protect_pager_t, | |
1251 | pager_queue); | |
1252 | apple_protect_pager_count++; | |
1253 | if (apple_protect_pager_count > apple_protect_pager_count_max) { | |
1254 | apple_protect_pager_count_max = apple_protect_pager_count; | |
1255 | } | |
b0d623f7 | 1256 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
1257 | |
1258 | kr = memory_object_create_named((memory_object_t) pager, | |
1259 | 0, | |
1260 | &control); | |
1261 | assert(kr == KERN_SUCCESS); | |
1262 | ||
b0d623f7 | 1263 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
1264 | /* the new pager is now ready to be used */ |
1265 | pager->is_ready = TRUE; | |
b0d623f7 | 1266 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
1267 | |
1268 | /* wakeup anyone waiting for this pager to be ready */ | |
1269 | thread_wakeup(&pager->is_ready); | |
1270 | ||
3e170ce0 A |
1271 | if (old_crypt_info != NULL && |
1272 | old_crypt_info != crypt_info) { | |
1273 | /* we re-used an old crypt_info instead of using our new one */ | |
1274 | #if CRYPT_INFO_DEBUG | |
1275 | printf("CRYPT_INFO %s: deallocate %p ref %d " | |
1276 | "(create used old)\n", | |
1277 | __FUNCTION__, | |
1278 | crypt_info, | |
1279 | crypt_info->crypt_refcnt); | |
1280 | #endif /* CRYPT_INFO_DEBUG */ | |
1281 | crypt_info_deallocate(crypt_info); | |
1282 | crypt_info = NULL; | |
1283 | } | |
1284 | ||
0c530ab8 A |
1285 | return pager; |
1286 | } | |
1287 | ||
1288 | /* | |
1289 | * apple_protect_pager_setup() | |
1290 | * | |
1291 | * Provide the caller with a memory object backed by the provided | |
1292 | * "backing_object" VM object. If such a memory object already exists, | |
1293 | * re-use it, otherwise create a new memory object. | |
1294 | */ | |
1295 | memory_object_t | |
1296 | apple_protect_pager_setup( | |
3e170ce0 A |
1297 | vm_object_t backing_object, |
1298 | vm_object_offset_t backing_offset, | |
1299 | vm_object_offset_t crypto_backing_offset, | |
1300 | struct pager_crypt_info *crypt_info, | |
1301 | vm_object_offset_t crypto_start, | |
1302 | vm_object_offset_t crypto_end) | |
0c530ab8 A |
1303 | { |
1304 | apple_protect_pager_t pager; | |
3e170ce0 A |
1305 | struct pager_crypt_info *old_crypt_info, *new_crypt_info; |
1306 | ||
1307 | #if CRYPT_INFO_DEBUG | |
1308 | printf("CRYPT_INFO %s: crypt_info=%p [%p,%p,%p,%d]\n", | |
1309 | __FUNCTION__, | |
1310 | crypt_info, | |
1311 | crypt_info->page_decrypt, | |
1312 | crypt_info->crypt_end, | |
1313 | crypt_info->crypt_ops, | |
1314 | crypt_info->crypt_refcnt); | |
1315 | #endif /* CRYPT_INFO_DEBUG */ | |
1316 | ||
1317 | old_crypt_info = NULL; | |
0c530ab8 | 1318 | |
b0d623f7 | 1319 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
1320 | |
1321 | queue_iterate(&apple_protect_pager_queue, | |
1322 | pager, | |
1323 | apple_protect_pager_t, | |
1324 | pager_queue) { | |
3e170ce0 A |
1325 | if ((pager->crypt_info->page_decrypt != |
1326 | crypt_info->page_decrypt) || | |
1327 | (pager->crypt_info->crypt_end != | |
1328 | crypt_info->crypt_end) || | |
1329 | (pager->crypt_info->crypt_ops != | |
1330 | crypt_info->crypt_ops)) { | |
1331 | /* no match for "crypt_info": next pager */ | |
1332 | continue; | |
1333 | } | |
1334 | /* found a match for crypt_info ... */ | |
1335 | if (old_crypt_info) { | |
1336 | /* ... already switched to that crypt_info */ | |
1337 | assert(old_crypt_info == pager->crypt_info); | |
1338 | } else { | |
1339 | /* ... switch to that pager's crypt_info */ | |
1340 | old_crypt_info = pager->crypt_info; | |
1341 | #if CRYPT_INFO_DEBUG | |
1342 | printf("CRYPT_INFO %s: " | |
1343 | "switching crypt_info from %p [%p,%p,%p,%d] " | |
1344 | "to %p [%p,%p,%p,%d] from pager %p\n", | |
1345 | __FUNCTION__, | |
1346 | crypt_info, | |
1347 | crypt_info->page_decrypt, | |
1348 | crypt_info->crypt_end, | |
1349 | crypt_info->crypt_ops, | |
1350 | crypt_info->crypt_refcnt, | |
1351 | old_crypt_info, | |
1352 | old_crypt_info->page_decrypt, | |
1353 | old_crypt_info->crypt_end, | |
1354 | old_crypt_info->crypt_ops, | |
1355 | old_crypt_info->crypt_refcnt, | |
1356 | pager); | |
1357 | printf("CRYPT_INFO %s: %p ref %d (setup match)\n", | |
1358 | __FUNCTION__, | |
1359 | pager->crypt_info, | |
1360 | pager->crypt_info->crypt_refcnt); | |
1361 | #endif /* CRYPT_INFO_DEBUG */ | |
1362 | crypt_info_reference(pager->crypt_info); | |
1363 | } | |
1364 | ||
1365 | if (pager->backing_object == backing_object && | |
1366 | pager->backing_offset == backing_offset && | |
1367 | pager->crypto_backing_offset == crypto_backing_offset && | |
1368 | pager->crypto_start == crypto_start && | |
1369 | pager->crypto_end == crypto_end) { | |
1370 | /* full match: use that pager! */ | |
1371 | assert(old_crypt_info == pager->crypt_info); | |
1372 | assert(old_crypt_info->crypt_refcnt > 1); | |
1373 | #if CRYPT_INFO_DEBUG | |
1374 | printf("CRYPT_INFO %s: " | |
1375 | "pager match with %p crypt_info %p\n", | |
1376 | __FUNCTION__, | |
1377 | pager, | |
1378 | pager->crypt_info); | |
1379 | printf("CRYPT_INFO %s: deallocate %p ref %d " | |
1380 | "(pager match)\n", | |
1381 | __FUNCTION__, | |
1382 | old_crypt_info, | |
1383 | old_crypt_info->crypt_refcnt); | |
1384 | #endif /* CRYPT_INFO_DEBUG */ | |
1385 | /* release the extra ref on crypt_info we got above */ | |
1386 | crypt_info_deallocate(old_crypt_info); | |
1387 | assert(old_crypt_info->crypt_refcnt > 0); | |
1388 | /* give extra reference on pager to the caller */ | |
1389 | assert(pager->ref_count > 0); | |
1390 | pager->ref_count++; | |
0c530ab8 A |
1391 | break; |
1392 | } | |
1393 | } | |
1394 | if (queue_end(&apple_protect_pager_queue, | |
1395 | (queue_entry_t) pager)) { | |
3e170ce0 | 1396 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
1397 | /* no existing pager for this backing object */ |
1398 | pager = APPLE_PROTECT_PAGER_NULL; | |
3e170ce0 A |
1399 | if (old_crypt_info) { |
1400 | /* use this old crypt_info for new pager */ | |
1401 | new_crypt_info = old_crypt_info; | |
1402 | #if CRYPT_INFO_DEBUG | |
1403 | printf("CRYPT_INFO %s: " | |
1404 | "will use old_crypt_info %p for new pager\n", | |
1405 | __FUNCTION__, | |
1406 | old_crypt_info); | |
1407 | #endif /* CRYPT_INFO_DEBUG */ | |
1408 | } else { | |
1409 | /* allocate a new crypt_info for new pager */ | |
1410 | new_crypt_info = kalloc(sizeof (*new_crypt_info)); | |
1411 | *new_crypt_info = *crypt_info; | |
1412 | new_crypt_info->crypt_refcnt = 1; | |
1413 | #if CRYPT_INFO_DEBUG | |
1414 | printf("CRYPT_INFO %s: " | |
1415 | "will use new_crypt_info %p for new pager\n", | |
1416 | __FUNCTION__, | |
1417 | new_crypt_info); | |
1418 | #endif /* CRYPT_INFO_DEBUG */ | |
1419 | } | |
1420 | if (new_crypt_info == NULL) { | |
1421 | /* can't create new pager without a crypt_info */ | |
1422 | } else { | |
1423 | /* create new pager */ | |
1424 | pager = apple_protect_pager_create( | |
1425 | backing_object, | |
1426 | backing_offset, | |
1427 | crypto_backing_offset, | |
1428 | new_crypt_info, | |
1429 | crypto_start, | |
1430 | crypto_end); | |
1431 | } | |
0c530ab8 | 1432 | if (pager == APPLE_PROTECT_PAGER_NULL) { |
3e170ce0 A |
1433 | /* could not create a new pager */ |
1434 | if (new_crypt_info == old_crypt_info) { | |
1435 | /* release extra reference on old_crypt_info */ | |
1436 | #if CRYPT_INFO_DEBUG | |
1437 | printf("CRYPT_INFO %s: deallocate %p ref %d " | |
1438 | "(create fail old_crypt_info)\n", | |
1439 | __FUNCTION__, | |
1440 | old_crypt_info, | |
1441 | old_crypt_info->crypt_refcnt); | |
1442 | #endif /* CRYPT_INFO_DEBUG */ | |
1443 | crypt_info_deallocate(old_crypt_info); | |
1444 | old_crypt_info = NULL; | |
1445 | } else { | |
1446 | /* release unused new_crypt_info */ | |
1447 | assert(new_crypt_info->crypt_refcnt == 1); | |
1448 | #if CRYPT_INFO_DEBUG | |
1449 | printf("CRYPT_INFO %s: deallocate %p ref %d " | |
1450 | "(create fail new_crypt_info)\n", | |
1451 | __FUNCTION__, | |
1452 | new_crypt_info, | |
1453 | new_crypt_info->crypt_refcnt); | |
1454 | #endif /* CRYPT_INFO_DEBUG */ | |
1455 | crypt_info_deallocate(new_crypt_info); | |
1456 | new_crypt_info = NULL; | |
1457 | } | |
0c530ab8 A |
1458 | return MEMORY_OBJECT_NULL; |
1459 | } | |
3e170ce0 A |
1460 | lck_mtx_lock(&apple_protect_pager_lock); |
1461 | } else { | |
1462 | assert(old_crypt_info == pager->crypt_info); | |
0c530ab8 A |
1463 | } |
1464 | ||
0c530ab8 | 1465 | while (!pager->is_ready) { |
b0d623f7 A |
1466 | lck_mtx_sleep(&apple_protect_pager_lock, |
1467 | LCK_SLEEP_DEFAULT, | |
1468 | &pager->is_ready, | |
1469 | THREAD_UNINT); | |
0c530ab8 | 1470 | } |
b0d623f7 | 1471 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
1472 | |
1473 | return (memory_object_t) pager; | |
1474 | } | |
1475 | ||
1476 | void | |
1477 | apple_protect_pager_trim(void) | |
1478 | { | |
1479 | apple_protect_pager_t pager, prev_pager; | |
1480 | queue_head_t trim_queue; | |
1481 | int num_trim; | |
1482 | int count_unmapped; | |
1483 | ||
b0d623f7 | 1484 | lck_mtx_lock(&apple_protect_pager_lock); |
0c530ab8 A |
1485 | |
1486 | /* | |
1487 | * We have too many pagers, try and trim some unused ones, | |
1488 | * starting with the oldest pager at the end of the queue. | |
1489 | */ | |
1490 | queue_init(&trim_queue); | |
1491 | num_trim = 0; | |
1492 | ||
1493 | for (pager = (apple_protect_pager_t) | |
1494 | queue_last(&apple_protect_pager_queue); | |
1495 | !queue_end(&apple_protect_pager_queue, | |
1496 | (queue_entry_t) pager); | |
1497 | pager = prev_pager) { | |
1498 | /* get prev elt before we dequeue */ | |
1499 | prev_pager = (apple_protect_pager_t) | |
1500 | queue_prev(&pager->pager_queue); | |
1501 | ||
1502 | if (pager->ref_count == 2 && | |
1503 | pager->is_ready && | |
1504 | !pager->is_mapped) { | |
1505 | /* this pager can be trimmed */ | |
1506 | num_trim++; | |
1507 | /* remove this pager from the main list ... */ | |
1508 | apple_protect_pager_dequeue(pager); | |
1509 | /* ... and add it to our trim queue */ | |
1510 | queue_enter_first(&trim_queue, | |
1511 | pager, | |
1512 | apple_protect_pager_t, | |
1513 | pager_queue); | |
1514 | ||
1515 | count_unmapped = (apple_protect_pager_count - | |
1516 | apple_protect_pager_count_mapped); | |
1517 | if (count_unmapped <= apple_protect_pager_cache_limit) { | |
1518 | /* we have enough pagers to trim */ | |
1519 | break; | |
1520 | } | |
1521 | } | |
1522 | } | |
1523 | if (num_trim > apple_protect_pager_num_trim_max) { | |
1524 | apple_protect_pager_num_trim_max = num_trim; | |
1525 | } | |
1526 | apple_protect_pager_num_trim_total += num_trim; | |
1527 | ||
b0d623f7 | 1528 | lck_mtx_unlock(&apple_protect_pager_lock); |
0c530ab8 A |
1529 | |
1530 | /* terminate the trimmed pagers */ | |
1531 | while (!queue_empty(&trim_queue)) { | |
1532 | queue_remove_first(&trim_queue, | |
1533 | pager, | |
1534 | apple_protect_pager_t, | |
1535 | pager_queue); | |
1536 | pager->pager_queue.next = NULL; | |
1537 | pager->pager_queue.prev = NULL; | |
1538 | assert(pager->ref_count == 2); | |
1539 | /* | |
1540 | * We can't call deallocate_internal() because the pager | |
1541 | * has already been dequeued, but we still need to remove | |
1542 | * a reference. | |
1543 | */ | |
1544 | pager->ref_count--; | |
1545 | apple_protect_pager_terminate_internal(pager); | |
1546 | } | |
1547 | } | |
3e170ce0 A |
1548 | |
1549 | ||
1550 | void | |
1551 | crypt_info_reference( | |
1552 | struct pager_crypt_info *crypt_info) | |
1553 | { | |
1554 | assert(crypt_info->crypt_refcnt != 0); | |
1555 | #if CRYPT_INFO_DEBUG | |
1556 | printf("CRYPT_INFO %s: %p ref %d -> %d\n", | |
1557 | __FUNCTION__, | |
1558 | crypt_info, | |
1559 | crypt_info->crypt_refcnt, | |
1560 | crypt_info->crypt_refcnt + 1); | |
1561 | #endif /* CRYPT_INFO_DEBUG */ | |
1562 | OSAddAtomic(+1, &crypt_info->crypt_refcnt); | |
1563 | } | |
1564 | ||
1565 | void | |
1566 | crypt_info_deallocate( | |
1567 | struct pager_crypt_info *crypt_info) | |
1568 | { | |
1569 | #if CRYPT_INFO_DEBUG | |
1570 | printf("CRYPT_INFO %s: %p ref %d -> %d\n", | |
1571 | __FUNCTION__, | |
1572 | crypt_info, | |
1573 | crypt_info->crypt_refcnt, | |
1574 | crypt_info->crypt_refcnt - 1); | |
1575 | #endif /* CRYPT_INFO_DEBUG */ | |
1576 | OSAddAtomic(-1, &crypt_info->crypt_refcnt); | |
1577 | if (crypt_info->crypt_refcnt == 0) { | |
1578 | /* deallocate any crypt module data */ | |
1579 | if (crypt_info->crypt_end) { | |
1580 | crypt_info->crypt_end(crypt_info->crypt_ops); | |
1581 | crypt_info->crypt_end = NULL; | |
1582 | } | |
1583 | #if CRYPT_INFO_DEBUG | |
1584 | printf("CRYPT_INFO %s: freeing %p\n", | |
1585 | __FUNCTION__, | |
1586 | crypt_info); | |
1587 | #endif /* CRYPT_INFO_DEBUG */ | |
1588 | kfree(crypt_info, sizeof (*crypt_info)); | |
1589 | crypt_info = NULL; | |
1590 | } | |
1591 | } |