2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/errno.h>
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>
40 #include <mach/thread_act.h>
41 #include <mach/mach_vm.h>
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>
48 #include <kern/ipc_kobject.h>
50 #include <ipc/ipc_port.h>
51 #include <ipc/ipc_space.h>
53 #include <vm/vm_fault.h>
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>
59 #include <vm/vm_kern.h>
63 * APPLE PROTECT MEMORY PAGER
65 * This external memory manager (EMM) handles memory from the encrypted
66 * sections of some executables protected by the DSMOS kernel extension.
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.
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.
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)
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
,
86 memory_object_cluster_size_t pg_size
);
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
,
90 memory_object_cluster_size_t length
,
91 vm_prot_t protection_required
,
92 memory_object_fault_info_t fault_info
);
93 kern_return_t
apple_protect_pager_data_return(memory_object_t mem_obj
,
94 memory_object_offset_t offset
,
95 memory_object_cluster_size_t data_cnt
,
96 memory_object_offset_t
*resid_offset
,
99 boolean_t kernel_copy
,
101 kern_return_t
apple_protect_pager_data_initialize(memory_object_t mem_obj
,
102 memory_object_offset_t offset
,
103 memory_object_cluster_size_t data_cnt
);
104 kern_return_t
apple_protect_pager_data_unlock(memory_object_t mem_obj
,
105 memory_object_offset_t offset
,
106 memory_object_size_t size
,
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
,
110 memory_object_size_t length
,
111 vm_sync_t sync_flags
);
112 kern_return_t
apple_protect_pager_map(memory_object_t mem_obj
,
114 kern_return_t
apple_protect_pager_last_unmap(memory_object_t mem_obj
);
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
);
121 * Vector of VM operations for this EMM.
122 * These routines are invoked by VM via the memory_object_*() interfaces.
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
,
134 apple_protect_pager_map
,
135 apple_protect_pager_last_unmap
,
136 NULL
, /* data_reclaim */
141 * The "apple_protect_pager" describes a memory object backed by
142 * the "apple protect" EMM.
144 typedef struct apple_protect_pager
{
145 struct ipc_object_header pager_header
; /* fake ip_kotype() */
146 memory_object_pager_ops_t pager_ops
; /* == &apple_protect_pager_ops */
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 */
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
;
158 } *apple_protect_pager_t
;
159 #define APPLE_PROTECT_PAGER_NULL ((apple_protect_pager_t) NULL)
160 #define pager_ikot pager_header.io_bits
163 * List of memory objects managed by this EMM.
164 * The list is protected by the "apple_protect_pager_lock" lock.
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
;
169 decl_lck_mtx_data(,apple_protect_pager_lock
)
172 * Maximum number of unmapped pagers we're willing to keep around.
174 int apple_protect_pager_cache_limit
= 20;
177 * Statistics & counters.
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;
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
;
190 /* internal prototypes */
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
);
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
,
202 void apple_protect_pager_terminate_internal(apple_protect_pager_t pager
);
203 void apple_protect_pager_trim(void);
207 int apple_protect_pagerdebug
= 0;
208 #define PAGER_ALL 0xffffffff
209 #define PAGER_INIT 0x00000001
210 #define PAGER_PAGEIN 0x00000002
212 #define PAGER_DEBUG(LEVEL, A) \
214 if ((apple_protect_pagerdebug & LEVEL)==LEVEL) { \
219 #define PAGER_DEBUG(LEVEL, A)
224 apple_protect_pager_bootstrap(void)
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
);
230 queue_init(&apple_protect_pager_queue
);
234 * apple_protect_pager_init()
236 * Initialize the memory object and makes it ready to be used and mapped.
239 apple_protect_pager_init(
240 memory_object_t mem_obj
,
241 memory_object_control_t control
,
245 memory_object_cluster_size_t pg_size
)
247 apple_protect_pager_t pager
;
249 memory_object_attr_info_data_t attributes
;
251 PAGER_DEBUG(PAGER_ALL
,
252 ("apple_protect_pager_init: %p, %p, %x\n",
253 mem_obj
, control
, pg_size
));
255 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
256 return KERN_INVALID_ARGUMENT
;
258 pager
= apple_protect_pager_lookup(mem_obj
);
260 memory_object_control_reference(control
);
262 pager
->pager_control
= control
;
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
;
270 kr
= memory_object_change_attributes(
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");
279 #if CONFIG_SECLUDED_MEMORY
280 if (secluded_for_filecache
) {
281 memory_object_mark_eligible_for_secluded(control
, TRUE
);
283 #endif /* CONFIG_SECLUDED_MEMORY */
289 * apple_protect_data_return()
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
297 apple_protect_pager_data_return(
298 __unused memory_object_t mem_obj
,
299 __unused memory_object_offset_t offset
,
300 __unused memory_object_cluster_size_t data_cnt
,
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
)
307 panic("apple_protect_pager_data_return: should never get called");
312 apple_protect_pager_data_initialize(
313 __unused memory_object_t mem_obj
,
314 __unused memory_object_offset_t offset
,
315 __unused memory_object_cluster_size_t data_cnt
)
317 panic("apple_protect_pager_data_initialize: should never get called");
322 apple_protect_pager_data_unlock(
323 __unused memory_object_t mem_obj
,
324 __unused memory_object_offset_t offset
,
325 __unused memory_object_size_t size
,
326 __unused vm_prot_t desired_access
)
332 * apple_protect_pager_data_request()
334 * Handles page-in requests from VM.
336 int apple_protect_pager_data_request_debug
= 0;
338 apple_protect_pager_data_request(
339 memory_object_t mem_obj
,
340 memory_object_offset_t offset
,
341 memory_object_cluster_size_t length
,
345 vm_prot_t protection_required
,
346 memory_object_fault_info_t mo_fault_info
)
348 apple_protect_pager_t pager
;
349 memory_object_control_t mo_control
;
353 upl_page_info_t
*upl_pl
;
354 unsigned int pl_count
;
355 vm_object_t src_top_object
, src_page_object
, dst_object
;
356 kern_return_t kr
, retval
;
357 vm_map_offset_t kernel_mapping
;
358 vm_offset_t src_vaddr
, dst_vaddr
;
359 vm_offset_t cur_offset
;
360 vm_offset_t offset_in_page
;
361 kern_return_t error_code
;
363 vm_page_t src_page
, top_page
;
365 struct vm_object_fault_info fault_info
;
368 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_data_request: %p, %llx, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
370 retval
= KERN_SUCCESS
;
371 src_top_object
= VM_OBJECT_NULL
;
372 src_page_object
= VM_OBJECT_NULL
;
376 fault_info
= *((struct vm_object_fault_info
*) mo_fault_info
);
377 fault_info
.stealth
= TRUE
;
378 fault_info
.io_sync
= FALSE
;
379 fault_info
.mark_zf_absent
= FALSE
;
380 fault_info
.batch_pmap_op
= FALSE
;
381 interruptible
= fault_info
.interruptible
;
383 pager
= apple_protect_pager_lookup(mem_obj
);
384 assert(pager
->is_ready
);
385 assert(pager
->ref_count
> 1); /* pager is alive and mapped */
387 PAGER_DEBUG(PAGER_PAGEIN
, ("apple_protect_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
390 * Gather in a UPL all the VM pages requested by VM.
392 mo_control
= pager
->pager_control
;
396 UPL_RET_ONLY_ABSENT
|
399 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
402 kr
= memory_object_upl_request(mo_control
,
404 &upl
, NULL
, NULL
, upl_flags
);
405 if (kr
!= KERN_SUCCESS
) {
409 dst_object
= mo_control
->moc_object
;
410 assert(dst_object
!= VM_OBJECT_NULL
);
413 #if __x86_64__ || __arm__ || __arm64__
414 /* we'll use the 1-to-1 mapping of physical memory */
417 #else /* __x86_64__ || __arm__ || __arm64__ */
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
423 vm_map_entry_t map_entry
;
425 vm_object_reference(kernel_object
); /* ref. for mapping */
426 kr
= vm_map_find_space(kernel_map
,
432 if (kr
!= KERN_SUCCESS
) {
433 vm_object_deallocate(kernel_object
);
437 map_entry
->object
.vm_object
= kernel_object
;
438 map_entry
->offset
= kernel_mapping
;
439 vm_map_unlock(kernel_map
);
440 src_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping
);
441 dst_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping
+ PAGE_SIZE_64
);
442 #endif /* __x86_64__ || __arm__ || __arm64__ */
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
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 */
454 * Fill in the contents of the pages requested by VM.
456 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
457 pl_count
= length
/ PAGE_SIZE
;
459 retval
== KERN_SUCCESS
&& cur_offset
< length
;
460 cur_offset
+= PAGE_SIZE
) {
463 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
464 /* this page is not in the UPL: skip it */
469 * Map the source (encrypted) page in the kernel's
470 * virtual address space.
471 * We already hold a reference on the src_top_object.
474 vm_object_lock(src_top_object
);
475 vm_object_paging_begin(src_top_object
);
478 src_page
= VM_PAGE_NULL
;
479 kr
= vm_fault_page(src_top_object
,
480 pager
->backing_offset
+ offset
+ cur_offset
,
483 FALSE
, /* src_page not looked up */
493 case VM_FAULT_SUCCESS
:
496 goto retry_src_fault
;
497 case VM_FAULT_MEMORY_SHORTAGE
:
498 if (vm_page_wait(interruptible
)) {
499 goto retry_src_fault
;
502 case VM_FAULT_INTERRUPTED
:
503 retval
= MACH_SEND_INTERRUPTED
;
505 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
506 /* success but no VM page: fail */
507 vm_object_paging_end(src_top_object
);
508 vm_object_unlock(src_top_object
);
510 case VM_FAULT_MEMORY_ERROR
:
511 /* the page is not there ! */
515 retval
= KERN_MEMORY_ERROR
;
519 panic("apple_protect_pager_data_request: "
520 "vm_fault_page() unexpected error 0x%x\n",
523 assert(src_page
!= VM_PAGE_NULL
);
524 assert(src_page
->busy
);
526 if (( !VM_PAGE_NON_SPECULATIVE_PAGEABLE(src_page
))) {
528 vm_page_lockspin_queues();
530 if (( !VM_PAGE_NON_SPECULATIVE_PAGEABLE(src_page
))) {
531 vm_page_deactivate(src_page
);
533 vm_page_unlock_queues();
537 * Establish an explicit mapping of the source
541 src_vaddr
= (vm_map_offset_t
)
542 PHYSMAP_PTOV((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
545 pmap_enter(kernel_pmap
,
547 VM_PAGE_GET_PHYS_PAGE(src_page
),
554 * Establish an explicit pmap mapping of the destination
556 * We can't do a regular VM mapping because the VM page
560 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
561 assert(dst_pnum
!= 0);
563 dst_vaddr
= (vm_map_offset_t
)
564 PHYSMAP_PTOV((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
566 pmap_enter(kernel_pmap
,
569 VM_PROT_READ
| VM_PROT_WRITE
,
574 src_page_object
= VM_PAGE_OBJECT(src_page
);
577 * Validate the original page...
579 if (src_page_object
->code_signed
) {
580 vm_page_validate_cs_mapped(
582 (const void *) src_vaddr
);
585 * ... and transfer the results to the destination page.
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
,
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.
601 assert(src_page
->busy
);
602 assert(src_page_object
->paging_in_progress
> 0);
603 vm_object_unlock(src_page_object
);
606 * Decrypt the encrypted contents of the source page
607 * into the destination page.
609 for (offset_in_page
= 0;
610 offset_in_page
< PAGE_SIZE
;
611 offset_in_page
+= 4096) {
612 if (offset
+ cur_offset
+ offset_in_page
<
613 pager
->crypto_start
||
614 offset
+ cur_offset
+ offset_in_page
>=
616 /* not encrypted: just copy */
617 bcopy((const char *)(src_vaddr
+
619 (char *)(dst_vaddr
+ offset_in_page
),
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 "
626 "COPY [0x%016llx 0x%016llx] "
633 (uint64_t) cur_offset
,
634 (uint64_t) offset_in_page
,
637 *(uint64_t *)(dst_vaddr
+
639 *(uint64_t *)(dst_vaddr
+
641 src_page_object
->code_signed
,
642 src_page
->cs_validated
,
643 src_page
->cs_tainted
,
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 ? */
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] "
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
) +
680 pager
->crypto_backing_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),
687 src_page_object
->code_signed
,
688 src_page
->cs_validated
,
689 src_page
->cs_tainted
,
699 * Decryption failed. Abort the fault.
701 retval
= KERN_ABORTED
;
704 assert(VM_PAGE_OBJECT(src_page
) == src_page_object
);
705 assert(src_page
->busy
);
706 assert(src_page_object
->paging_in_progress
> 0);
707 vm_object_lock(src_page_object
);
709 #if __x86_64__ || __arm__ || __arm64__
710 /* we used the 1-to-1 mapping of physical memory */
713 #else /* __x86_64__ || __arm__ || __arm64__ */
715 * Remove the pmap mapping of the source and destination pages
718 pmap_remove(kernel_pmap
,
719 (addr64_t
) kernel_mapping
,
720 (addr64_t
) (kernel_mapping
+ (2 * PAGE_SIZE_64
)));
721 #endif /* __x86_64__ || __arm__ || __arm64__ */
724 * Cleanup the result of vm_fault_page() of the source page.
726 if (retval
== KERN_SUCCESS
&&
728 !VM_PAGE_WIRED(src_page
) &&
730 !src_page
->precious
&&
731 !src_page
->laundry
&&
732 !src_page
->cleaning
) {
735 refmod_state
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(src_page
));
737 if (refmod_state
& VM_MEM_MODIFIED
) {
738 SET_PAGE_DIRTY(src_page
, FALSE
);
740 if (!src_page
->dirty
) {
741 vm_page_free_unlocked(src_page
, TRUE
);
742 src_page
= VM_PAGE_NULL
;
744 PAGE_WAKEUP_DONE(src_page
);
747 PAGE_WAKEUP_DONE(src_page
);
749 src_page
= VM_PAGE_NULL
;
750 vm_object_paging_end(src_page_object
);
751 vm_object_unlock(src_page_object
);
752 if (top_page
!= VM_PAGE_NULL
) {
753 assert(VM_PAGE_OBJECT(top_page
) == src_top_object
);
754 vm_object_lock(src_top_object
);
755 VM_PAGE_FREE(top_page
);
756 vm_object_paging_end(src_top_object
);
757 vm_object_unlock(src_top_object
);
763 /* clean up the UPL */
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".
772 upl_clear_dirty(upl
, TRUE
);
774 /* abort or commit the UPL */
775 if (retval
!= KERN_SUCCESS
) {
777 if (retval
== KERN_ABORTED
) {
778 wait_result_t wait_result
;
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).
789 retval
= KERN_SUCCESS
;
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.
795 wait_result
= assert_wait_timeout(
796 (event_t
) apple_protect_pager_data_request
,
800 assert(wait_result
== THREAD_WAITING
);
801 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
802 assert(wait_result
== THREAD_TIMED_OUT
);
806 upl_commit_range(upl
, 0, upl
->size
,
807 UPL_COMMIT_CS_VALIDATED
| UPL_COMMIT_WRITTEN_BY_KERNEL
,
808 upl_pl
, pl_count
, &empty
);
811 /* and deallocate the UPL */
815 if (kernel_mapping
!= 0) {
816 /* clean up the mapping of the source and destination pages */
817 kr
= vm_map_remove(kernel_map
,
819 kernel_mapping
+ (2 * PAGE_SIZE_64
),
821 assert(kr
== KERN_SUCCESS
);
826 if (src_top_object
!= VM_OBJECT_NULL
) {
827 vm_object_deallocate(src_top_object
);
834 * apple_protect_pager_reference()
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.
841 apple_protect_pager_reference(
842 memory_object_t mem_obj
)
844 apple_protect_pager_t pager
;
846 pager
= apple_protect_pager_lookup(mem_obj
);
848 lck_mtx_lock(&apple_protect_pager_lock
);
849 assert(pager
->ref_count
> 0);
851 lck_mtx_unlock(&apple_protect_pager_lock
);
856 * apple_protect_pager_dequeue:
858 * Removes a pager from the list of pagers.
860 * The caller must hold "apple_protect_pager_lock".
863 apple_protect_pager_dequeue(
864 apple_protect_pager_t pager
)
866 assert(!pager
->is_mapped
);
868 queue_remove(&apple_protect_pager_queue
,
870 apple_protect_pager_t
,
872 pager
->pager_queue
.next
= NULL
;
873 pager
->pager_queue
.prev
= NULL
;
875 apple_protect_pager_count
--;
879 * apple_protect_pager_terminate_internal:
881 * Trigger the asynchronous termination of the memory object associated
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.
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.
892 apple_protect_pager_terminate_internal(
893 apple_protect_pager_t pager
)
895 assert(pager
->is_ready
);
896 assert(!pager
->is_mapped
);
898 if (pager
->backing_object
!= VM_OBJECT_NULL
) {
899 vm_object_deallocate(pager
->backing_object
);
900 pager
->backing_object
= VM_OBJECT_NULL
;
903 /* one less pager using this "pager_crypt_info" */
905 printf("CRYPT_INFO %s: deallocate %p ref %d\n",
908 pager
->crypt_info
->crypt_refcnt
);
909 #endif /* CRYPT_INFO_DEBUG */
910 crypt_info_deallocate(pager
->crypt_info
);
911 pager
->crypt_info
= NULL
;
913 /* trigger the destruction of the memory object */
914 memory_object_destroy(pager
->pager_control
, 0);
918 * apple_protect_pager_deallocate_internal()
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
926 apple_protect_pager_deallocate_internal(
927 apple_protect_pager_t pager
,
930 boolean_t needs_trimming
;
934 lck_mtx_lock(&apple_protect_pager_lock
);
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
;
943 needs_trimming
= FALSE
;
946 /* drop a reference on this pager */
949 if (pager
->ref_count
== 1) {
951 * Only the "named" reference is left, which means that
952 * no one is really holding on to this pager anymore.
955 apple_protect_pager_dequeue(pager
);
956 /* the pager is all ours: no need for the lock now */
957 lck_mtx_unlock(&apple_protect_pager_lock
);
958 apple_protect_pager_terminate_internal(pager
);
959 } else if (pager
->ref_count
== 0) {
961 * Dropped the existence reference; the memory object has
962 * been terminated. Do some final cleanup and release the
965 lck_mtx_unlock(&apple_protect_pager_lock
);
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
;
970 kfree(pager
, sizeof (*pager
));
971 pager
= APPLE_PROTECT_PAGER_NULL
;
973 /* there are still plenty of references: keep going... */
974 lck_mtx_unlock(&apple_protect_pager_lock
);
977 if (needs_trimming
) {
978 apple_protect_pager_trim();
980 /* caution: lock is not held on return... */
984 * apple_protect_pager_deallocate()
986 * Release a reference on this pager and free it when the last
987 * reference goes away.
990 apple_protect_pager_deallocate(
991 memory_object_t mem_obj
)
993 apple_protect_pager_t pager
;
995 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_deallocate: %p\n", mem_obj
));
996 pager
= apple_protect_pager_lookup(mem_obj
);
997 apple_protect_pager_deallocate_internal(pager
, FALSE
);
1004 apple_protect_pager_terminate(
1008 memory_object_t mem_obj
)
1010 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_terminate: %p\n", mem_obj
));
1012 return KERN_SUCCESS
;
1019 apple_protect_pager_synchronize(
1020 memory_object_t mem_obj
,
1021 memory_object_offset_t offset
,
1022 memory_object_size_t length
,
1023 __unused vm_sync_t sync_flags
)
1025 apple_protect_pager_t pager
;
1027 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_synchronize: %p\n", mem_obj
));
1029 pager
= apple_protect_pager_lookup(mem_obj
);
1031 memory_object_synchronize_completed(pager
->pager_control
,
1034 return KERN_SUCCESS
;
1038 * apple_protect_pager_map()
1040 * This allows VM to let us, the EMM, know that this memory object
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
1043 * memory object to account for all its mappings.
1046 apple_protect_pager_map(
1047 memory_object_t mem_obj
,
1048 __unused vm_prot_t prot
)
1050 apple_protect_pager_t pager
;
1052 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_map: %p\n", mem_obj
));
1054 pager
= apple_protect_pager_lookup(mem_obj
);
1056 lck_mtx_lock(&apple_protect_pager_lock
);
1057 assert(pager
->is_ready
);
1058 assert(pager
->ref_count
> 0); /* pager is alive */
1059 if (pager
->is_mapped
== FALSE
) {
1061 * First mapping of this pager: take an extra reference
1062 * that will remain until all the mappings of this pager
1065 pager
->is_mapped
= TRUE
;
1067 apple_protect_pager_count_mapped
++;
1069 lck_mtx_unlock(&apple_protect_pager_lock
);
1071 return KERN_SUCCESS
;
1075 * apple_protect_pager_last_unmap()
1077 * This is called by VM when this memory object is no longer mapped anywhere.
1080 apple_protect_pager_last_unmap(
1081 memory_object_t mem_obj
)
1083 apple_protect_pager_t pager
;
1086 PAGER_DEBUG(PAGER_ALL
,
1087 ("apple_protect_pager_last_unmap: %p\n", mem_obj
));
1089 pager
= apple_protect_pager_lookup(mem_obj
);
1091 lck_mtx_lock(&apple_protect_pager_lock
);
1092 if (pager
->is_mapped
) {
1094 * All the mappings are gone, so let go of the one extra
1095 * reference that represents all the mappings of this pager.
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
;
1103 pager
->is_mapped
= FALSE
;
1104 apple_protect_pager_deallocate_internal(pager
, TRUE
);
1105 /* caution: deallocate_internal() released the lock ! */
1107 lck_mtx_unlock(&apple_protect_pager_lock
);
1110 return KERN_SUCCESS
;
1117 apple_protect_pager_t
1118 apple_protect_pager_lookup(
1119 memory_object_t mem_obj
)
1121 apple_protect_pager_t pager
;
1123 pager
= (apple_protect_pager_t
) mem_obj
;
1124 assert(pager
->pager_ops
== &apple_protect_pager_ops
);
1125 assert(pager
->ref_count
> 0);
1129 apple_protect_pager_t
1130 apple_protect_pager_create(
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
)
1138 apple_protect_pager_t pager
, pager2
;
1139 memory_object_control_t control
;
1141 struct pager_crypt_info
*old_crypt_info
;
1143 pager
= (apple_protect_pager_t
) kalloc(sizeof (*pager
));
1144 if (pager
== APPLE_PROTECT_PAGER_NULL
) {
1145 return APPLE_PROTECT_PAGER_NULL
;
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,
1152 * we reserve the first word in the object for a fake ip_kotype
1153 * setting - that will tell vm_map to use it as a memory object.
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" */
1158 pager
->ref_count
= 1; /* existence reference (for the cache) */
1159 pager
->ref_count
++; /* for the caller */
1160 pager
->is_mapped
= FALSE
;
1161 pager
->pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
1162 pager
->backing_object
= backing_object
;
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 */
1169 #if CRYPT_INFO_DEBUG
1170 printf("CRYPT_INFO %s: crypt_info %p [%p,%p,%p,%d]\n",
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 */
1179 vm_object_reference(backing_object
);
1181 old_crypt_info
= NULL
;
1183 lck_mtx_lock(&apple_protect_pager_lock
);
1184 /* see if anyone raced us to create a pager for the same object */
1185 queue_iterate(&apple_protect_pager_queue
,
1187 apple_protect_pager_t
,
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 */
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 "
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
;
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 */
1226 if (! queue_end(&apple_protect_pager_queue
,
1227 (queue_entry_t
) pager2
)) {
1228 /* we lost the race, down with the loser... */
1229 lck_mtx_unlock(&apple_protect_pager_lock
);
1230 vm_object_deallocate(pager
->backing_object
);
1231 pager
->backing_object
= VM_OBJECT_NULL
;
1232 #if CRYPT_INFO_DEBUG
1233 printf("CRYPT_INFO %s: %p ref %d (create pager match)\n",
1236 pager
->crypt_info
->crypt_refcnt
);
1237 #endif /* CRYPT_INFO_DEBUG */
1238 crypt_info_deallocate(pager
->crypt_info
);
1239 pager
->crypt_info
= NULL
;
1240 kfree(pager
, sizeof (*pager
));
1241 /* ... and go with the winner */
1243 /* let the winner make sure the pager gets ready */
1247 /* enter new pager at the head of our list of pagers */
1248 queue_enter_first(&apple_protect_pager_queue
,
1250 apple_protect_pager_t
,
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
;
1256 lck_mtx_unlock(&apple_protect_pager_lock
);
1258 kr
= memory_object_create_named((memory_object_t
) pager
,
1261 assert(kr
== KERN_SUCCESS
);
1263 lck_mtx_lock(&apple_protect_pager_lock
);
1264 /* the new pager is now ready to be used */
1265 pager
->is_ready
= TRUE
;
1266 lck_mtx_unlock(&apple_protect_pager_lock
);
1268 /* wakeup anyone waiting for this pager to be ready */
1269 thread_wakeup(&pager
->is_ready
);
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",
1279 crypt_info
->crypt_refcnt
);
1280 #endif /* CRYPT_INFO_DEBUG */
1281 crypt_info_deallocate(crypt_info
);
1289 * apple_protect_pager_setup()
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.
1296 apple_protect_pager_setup(
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
)
1304 apple_protect_pager_t pager
;
1305 struct pager_crypt_info
*old_crypt_info
, *new_crypt_info
;
1307 #if CRYPT_INFO_DEBUG
1308 printf("CRYPT_INFO %s: crypt_info=%p [%p,%p,%p,%d]\n",
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 */
1317 old_crypt_info
= NULL
;
1319 lck_mtx_lock(&apple_protect_pager_lock
);
1321 queue_iterate(&apple_protect_pager_queue
,
1323 apple_protect_pager_t
,
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 */
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
);
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",
1347 crypt_info
->page_decrypt
,
1348 crypt_info
->crypt_end
,
1349 crypt_info
->crypt_ops
,
1350 crypt_info
->crypt_refcnt
,
1352 old_crypt_info
->page_decrypt
,
1353 old_crypt_info
->crypt_end
,
1354 old_crypt_info
->crypt_ops
,
1355 old_crypt_info
->crypt_refcnt
,
1357 printf("CRYPT_INFO %s: %p ref %d (setup match)\n",
1360 pager
->crypt_info
->crypt_refcnt
);
1361 #endif /* CRYPT_INFO_DEBUG */
1362 crypt_info_reference(pager
->crypt_info
);
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",
1379 printf("CRYPT_INFO %s: deallocate %p ref %d "
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);
1394 if (queue_end(&apple_protect_pager_queue
,
1395 (queue_entry_t
) pager
)) {
1396 lck_mtx_unlock(&apple_protect_pager_lock
);
1397 /* no existing pager for this backing object */
1398 pager
= APPLE_PROTECT_PAGER_NULL
;
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",
1407 #endif /* CRYPT_INFO_DEBUG */
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",
1418 #endif /* CRYPT_INFO_DEBUG */
1420 if (new_crypt_info
== NULL
) {
1421 /* can't create new pager without a crypt_info */
1423 /* create new pager */
1424 pager
= apple_protect_pager_create(
1427 crypto_backing_offset
,
1432 if (pager
== APPLE_PROTECT_PAGER_NULL
) {
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",
1441 old_crypt_info
->crypt_refcnt
);
1442 #endif /* CRYPT_INFO_DEBUG */
1443 crypt_info_deallocate(old_crypt_info
);
1444 old_crypt_info
= NULL
;
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",
1453 new_crypt_info
->crypt_refcnt
);
1454 #endif /* CRYPT_INFO_DEBUG */
1455 crypt_info_deallocate(new_crypt_info
);
1456 new_crypt_info
= NULL
;
1458 return MEMORY_OBJECT_NULL
;
1460 lck_mtx_lock(&apple_protect_pager_lock
);
1462 assert(old_crypt_info
== pager
->crypt_info
);
1465 while (!pager
->is_ready
) {
1466 lck_mtx_sleep(&apple_protect_pager_lock
,
1471 lck_mtx_unlock(&apple_protect_pager_lock
);
1473 return (memory_object_t
) pager
;
1477 apple_protect_pager_trim(void)
1479 apple_protect_pager_t pager
, prev_pager
;
1480 queue_head_t trim_queue
;
1484 lck_mtx_lock(&apple_protect_pager_lock
);
1487 * We have too many pagers, try and trim some unused ones,
1488 * starting with the oldest pager at the end of the queue.
1490 queue_init(&trim_queue
);
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
);
1502 if (pager
->ref_count
== 2 &&
1504 !pager
->is_mapped
) {
1505 /* this pager can be trimmed */
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
,
1512 apple_protect_pager_t
,
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 */
1523 if (num_trim
> apple_protect_pager_num_trim_max
) {
1524 apple_protect_pager_num_trim_max
= num_trim
;
1526 apple_protect_pager_num_trim_total
+= num_trim
;
1528 lck_mtx_unlock(&apple_protect_pager_lock
);
1530 /* terminate the trimmed pagers */
1531 while (!queue_empty(&trim_queue
)) {
1532 queue_remove_first(&trim_queue
,
1534 apple_protect_pager_t
,
1536 pager
->pager_queue
.next
= NULL
;
1537 pager
->pager_queue
.prev
= NULL
;
1538 assert(pager
->ref_count
== 2);
1540 * We can't call deallocate_internal() because the pager
1541 * has already been dequeued, but we still need to remove
1545 apple_protect_pager_terminate_internal(pager
);
1551 crypt_info_reference(
1552 struct pager_crypt_info
*crypt_info
)
1554 assert(crypt_info
->crypt_refcnt
!= 0);
1555 #if CRYPT_INFO_DEBUG
1556 printf("CRYPT_INFO %s: %p ref %d -> %d\n",
1559 crypt_info
->crypt_refcnt
,
1560 crypt_info
->crypt_refcnt
+ 1);
1561 #endif /* CRYPT_INFO_DEBUG */
1562 OSAddAtomic(+1, &crypt_info
->crypt_refcnt
);
1566 crypt_info_deallocate(
1567 struct pager_crypt_info
*crypt_info
)
1569 #if CRYPT_INFO_DEBUG
1570 printf("CRYPT_INFO %s: %p ref %d -> %d\n",
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
;
1583 #if CRYPT_INFO_DEBUG
1584 printf("CRYPT_INFO %s: freeing %p\n",
1587 #endif /* CRYPT_INFO_DEBUG */
1588 kfree(crypt_info
, sizeof (*crypt_info
));