2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <sys/errno.h>
33 #include <mach/mach_types.h>
34 #include <mach/mach_traps.h>
35 #include <mach/host_priv.h>
36 #include <mach/kern_return.h>
37 #include <mach/memory_object_control.h>
38 #include <mach/memory_object_types.h>
39 #include <mach/port.h>
40 #include <mach/policy.h>
42 #include <mach/thread_act.h>
43 #include <mach/mach_vm.h>
45 #include <kern/host.h>
46 #include <kern/kalloc.h>
47 #include <kern/page_decrypt.h>
48 #include <kern/queue.h>
49 #include <kern/thread.h>
51 #include <ipc/ipc_port.h>
52 #include <ipc/ipc_space.h>
54 #include <default_pager/default_pager_types.h>
55 #include <default_pager/default_pager_object_server.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_pageout.h>
59 #include <vm/memory_object.h>
60 #include <vm/vm_pageout.h>
61 #include <vm/vm_protos.h>
65 * APPLE PROTECT MEMORY PAGER
67 * This external memory manager (EMM) handles memory from the encrypted
68 * sections of some executables protected by the DSMOS kernel extension.
70 * It mostly handles page-in requests (from memory_object_data_request()) by
71 * getting the encrypted data from its backing VM object, itself backed by
72 * the encrypted file, decrypting it and providing it to VM.
74 * The decrypted pages will never be dirtied, so the memory manager doesn't
75 * need to handle page-out requests (from memory_object_data_return()). The
76 * pages need to be mapped copy-on-write, so that the originals stay clean.
78 * We don't expect to have to handle a large number of apple-protected
79 * binaries, so the data structures are very simple (simple linked list)
83 /* forward declarations */
84 void apple_protect_pager_reference(memory_object_t mem_obj
);
85 void apple_protect_pager_deallocate(memory_object_t mem_obj
);
86 kern_return_t
apple_protect_pager_init(memory_object_t mem_obj
,
87 memory_object_control_t control
,
89 kern_return_t
apple_protect_pager_terminate(memory_object_t mem_obj
);
90 kern_return_t
apple_protect_pager_data_request(memory_object_t mem_obj
,
91 memory_object_offset_t offset
,
93 vm_prot_t protection_required
);
94 kern_return_t
apple_protect_pager_data_return(memory_object_t mem_obj
,
95 memory_object_offset_t offset
,
97 memory_object_offset_t
*resid_offset
,
100 boolean_t kernel_copy
,
102 kern_return_t
apple_protect_pager_data_initialize(memory_object_t mem_obj
,
103 memory_object_offset_t offset
,
105 kern_return_t
apple_protect_pager_data_unlock(memory_object_t mem_obj
,
106 memory_object_offset_t offset
,
108 vm_prot_t desired_access
);
109 kern_return_t
apple_protect_pager_synchronize(memory_object_t mem_obj
,
110 memory_object_offset_t offset
,
112 vm_sync_t sync_flags
);
113 kern_return_t
apple_protect_pager_unmap(memory_object_t mem_obj
);
116 * Vector of VM operations for this EMM.
117 * These routines are invoked by VM via the memory_object_*() interfaces.
119 const struct memory_object_pager_ops apple_protect_pager_ops
= {
120 apple_protect_pager_reference
,
121 apple_protect_pager_deallocate
,
122 apple_protect_pager_init
,
123 apple_protect_pager_terminate
,
124 apple_protect_pager_data_request
,
125 apple_protect_pager_data_return
,
126 apple_protect_pager_data_initialize
,
127 apple_protect_pager_data_unlock
,
128 apple_protect_pager_synchronize
,
129 apple_protect_pager_unmap
,
130 "apple protect pager"
134 * The "apple_protect_pager" describes a memory object backed by
135 * the "apple protect" EMM.
137 typedef struct apple_protect_pager
{
138 memory_object_pager_ops_t pager_ops
; /* == &apple_protect_pager_ops */
139 unsigned int pager_ikot
; /* JMM: fake ip_kotype() */
140 queue_chain_t pager_queue
; /* next & prev pagers */
141 unsigned int ref_count
; /* reference count */
142 boolean_t is_ready
; /* is this pager ready ? */
143 boolean_t is_mapped
; /* is this mem_obj mapped ? */
144 memory_object_control_t pager_control
; /* mem object control handle */
145 vm_object_t backing_object
; /* VM obj w/ encrypted data */
146 } *apple_protect_pager_t
;
147 #define APPLE_PROTECT_PAGER_NULL ((apple_protect_pager_t) NULL)
150 * List of memory objects managed by this EMM.
151 * The list is protected by the "apple_protect_pager_lock" lock.
153 int apple_protect_pager_count
= 0; /* number of pagers */
154 int apple_protect_pager_count_mapped
= 0; /* number of unmapped pagers */
155 queue_head_t apple_protect_pager_queue
;
156 decl_mutex_data(,apple_protect_pager_lock
)
159 * Maximum number of unmapped pagers we're willing to keep around.
161 int apple_protect_pager_cache_limit
= 10;
164 * Statistics & counters.
166 int apple_protect_pager_count_max
= 0;
167 int apple_protect_pager_count_unmapped_max
= 0;
168 int apple_protect_pager_num_trim_max
= 0;
169 int apple_protect_pager_num_trim_total
= 0;
171 /* internal prototypes */
172 apple_protect_pager_t
apple_protect_pager_create(vm_object_t backing_object
);
173 apple_protect_pager_t
apple_protect_pager_lookup(memory_object_t mem_obj
);
174 void apple_protect_pager_dequeue(apple_protect_pager_t pager
);
175 void apple_protect_pager_deallocate_internal(apple_protect_pager_t pager
,
177 void apple_protect_pager_terminate_internal(apple_protect_pager_t pager
);
178 void apple_protect_pager_trim(void);
182 int apple_protect_pagerdebug
= 0;
183 #define PAGER_ALL 0xffffffff
184 #define PAGER_INIT 0x00000001
185 #define PAGER_PAGEIN 0x00000002
187 #define PAGER_DEBUG(LEVEL, A) \
189 if ((apple_protect_pagerdebug & LEVEL)==LEVEL) { \
194 #define PAGER_DEBUG(LEVEL, A)
199 apple_protect_pager_bootstrap(void)
201 mutex_init(&apple_protect_pager_lock
, 0);
202 queue_init(&apple_protect_pager_queue
);
206 * apple_protect_pager_init()
208 * Initialize the memory object and makes it ready to be used and mapped.
211 apple_protect_pager_init(
212 memory_object_t mem_obj
,
213 memory_object_control_t control
,
219 apple_protect_pager_t pager
;
221 memory_object_attr_info_data_t attributes
;
223 PAGER_DEBUG(PAGER_ALL
,
224 ("apple_protect_pager_init: %p, %p, %x\n",
225 mem_obj
, control
, pg_size
));
227 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
228 return KERN_INVALID_ARGUMENT
;
230 pager
= apple_protect_pager_lookup(mem_obj
);
232 memory_object_control_reference(control
);
234 pager
->pager_control
= control
;
236 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
237 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
238 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
239 attributes
.may_cache_object
= FALSE
;
240 attributes
.temporary
= TRUE
;
242 kr
= memory_object_change_attributes(
244 MEMORY_OBJECT_ATTRIBUTE_INFO
,
245 (memory_object_info_t
) &attributes
,
246 MEMORY_OBJECT_ATTR_INFO_COUNT
);
247 if (kr
!= KERN_SUCCESS
)
248 panic("apple_protect_pager_init: "
249 "memory_object_change_attributes() failed");
255 * apple_protect_data_return()
257 * Handles page-out requests from VM. This should never happen since
258 * the pages provided by this EMM are not supposed to be dirty or dirtied
259 * and VM should simply discard the contents and reclaim the pages if it
263 apple_protect_pager_data_return(
264 __unused memory_object_t mem_obj
,
265 __unused memory_object_offset_t offset
,
266 __unused vm_size_t data_cnt
,
267 __unused memory_object_offset_t
*resid_offset
,
268 __unused
int *io_error
,
269 __unused boolean_t dirty
,
270 __unused boolean_t kernel_copy
,
271 __unused
int upl_flags
)
273 panic("apple_protect_pager_data_return: should never get called");
278 apple_protect_pager_data_initialize(
279 __unused memory_object_t mem_obj
,
280 __unused memory_object_offset_t offset
,
281 __unused vm_size_t data_cnt
)
283 panic("apple_protect_pager_data_initialize: should never get called");
288 apple_protect_pager_data_unlock(
289 __unused memory_object_t mem_obj
,
290 __unused memory_object_offset_t offset
,
291 __unused vm_size_t size
,
292 __unused vm_prot_t desired_access
)
298 * apple_protect_pager_data_request()
300 * Handles page-in requests from VM.
303 apple_protect_pager_data_request(
304 memory_object_t mem_obj
,
305 memory_object_offset_t offset
,
310 vm_prot_t protection_required
)
312 apple_protect_pager_t pager
;
313 memory_object_control_t mo_control
;
317 upl_page_info_t
*upl_pl
;
318 vm_object_t src_object
, dst_object
;
319 kern_return_t kr
, retval
;
320 vm_map_offset_t src_mapping
= 0, dst_mapping
= 0;
321 vm_offset_t src_vaddr
, dst_vaddr
;
322 vm_offset_t cur_offset
;
323 boolean_t src_map_page_by_page
;
324 vm_map_entry_t map_entry
;
326 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_data_request: %x, %llx, %llxx, %x\n", mem_obj
, offset
, length
, protection_required
));
328 pager
= apple_protect_pager_lookup(mem_obj
);
329 assert(pager
->is_ready
);
330 assert(pager
->ref_count
> 1); /* pager is alive and mapped */
332 PAGER_DEBUG(PAGER_PAGEIN
, ("apple_protect_pager_data_request: %x, %llx, %llx, %x, pager %x\n", mem_obj
, offset
, length
, protection_required
, pager
));
335 * Map the encrypted data in the kernel address space from the
336 * backing VM object (itself backed by the encrypted file via
339 src_object
= pager
->backing_object
;
340 assert(src_object
!= VM_OBJECT_NULL
);
341 vm_object_reference(src_object
); /* ref. for the mapping */
343 kr
= vm_map_enter(kernel_map
,
356 /* wire the memory to make sure it is available */
357 kr
= vm_map_wire(kernel_map
,
359 src_mapping
+ length
,
362 if (kr
!= KERN_SUCCESS
) {
364 * Wiring failed, so unmap source and fall back
365 * to page by page mapping of the source.
367 kr
= vm_map_remove(kernel_map
,
369 src_mapping
+ length
,
371 assert(kr
== KERN_SUCCESS
);
374 src_map_page_by_page
= TRUE
;
377 /* source region is now fully mapped and wired */
378 src_map_page_by_page
= FALSE
;
379 src_vaddr
= CAST_DOWN(vm_offset_t
, src_mapping
);
382 /* we couldn't map the entire source, so map it page by page */
383 src_map_page_by_page
= TRUE
;
384 /* release the reference for the failed mapping */
385 vm_object_deallocate(src_object
);
388 vm_object_deallocate(src_object
);
395 * Gather in a UPL all the VM pages requested by VM.
397 mo_control
= pager
->pager_control
;
401 UPL_RET_ONLY_ABSENT
|
404 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
406 kr
= memory_object_upl_request(mo_control
,
408 &upl
, NULL
, NULL
, upl_flags
);
409 if (kr
!= KERN_SUCCESS
) {
415 * Reserve a virtual page in the kernel address space to map each
416 * destination physical page when it's its turn to be filled.
418 dst_object
= mo_control
->moc_object
;
419 assert(dst_object
!= VM_OBJECT_NULL
);
421 vm_object_reference(kernel_object
); /* ref. for mapping */
422 kr
= vm_map_find_space(kernel_map
,
428 if (kr
!= KERN_SUCCESS
) {
429 vm_object_deallocate(kernel_object
);
433 map_entry
->object
.vm_object
= kernel_object
;
434 map_entry
->offset
= dst_mapping
- VM_MIN_KERNEL_ADDRESS
;
435 vm_map_unlock(kernel_map
);
436 dst_vaddr
= CAST_DOWN(vm_offset_t
, dst_mapping
);
439 * Fill in the contents of the pages requested by VM.
441 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
442 for (cur_offset
= 0; cur_offset
< length
; cur_offset
+= PAGE_SIZE
) {
445 if (!upl_page_present(upl_pl
, cur_offset
/ PAGE_SIZE
)) {
446 /* this page is not in the UPL: skip it */
451 * Map the source (encrypted) page in the kernel's
452 * virtual address space.
454 if (src_map_page_by_page
) {
455 vm_object_reference(src_object
); /* ref. for mapping */
456 kr
= vm_map_enter(kernel_map
,
467 if (kr
!= KERN_SUCCESS
) {
468 vm_object_deallocate(src_object
);
472 kr
= vm_map_wire(kernel_map
,
474 src_mapping
+ PAGE_SIZE_64
,
477 if (kr
!= KERN_SUCCESS
) {
479 kr
= vm_map_remove(kernel_map
,
481 src_mapping
+ PAGE_SIZE_64
,
483 assert(kr
== KERN_SUCCESS
);
486 printf("apple_protect_pager_data_request: "
487 "failed to resolve page fault for src "
488 "object %p offset 0x%llx "
489 "preempt %d error 0x%x\n",
490 src_object
, offset
+ cur_offset
,
491 get_preemption_level(), retval
);
494 src_vaddr
= CAST_DOWN(vm_offset_t
, src_mapping
);
496 src_vaddr
= src_mapping
+ cur_offset
;
500 * Establish an explicit pmap mapping of the destination
502 * We can't do a regular VM mapping because the VM page
505 dst_pnum
= (addr64_t
)
506 upl_phys_page(upl_pl
, cur_offset
/ PAGE_SIZE
);
507 assert(dst_pnum
!= 0);
508 pmap_enter(kernel_pmap
, dst_mapping
, dst_pnum
,
509 VM_PROT_READ
| VM_PROT_WRITE
,
510 dst_object
->wimg_bits
& VM_WIMG_MASK
,
514 * Decrypt the encrypted contents of the source page
515 * into the destination page.
517 dsmos_page_transform((const void *) src_vaddr
,
521 * Remove the pmap mapping of the destination page
524 pmap_remove(kernel_pmap
,
525 (addr64_t
) dst_mapping
,
526 (addr64_t
) (dst_mapping
+ PAGE_SIZE_64
));
528 if (src_map_page_by_page
) {
530 * Remove the wired kernel mapping of the source page.
531 * This releases the extra reference we took on
534 kr
= vm_map_remove(kernel_map
,
536 src_mapping
+ PAGE_SIZE_64
,
537 VM_MAP_REMOVE_KUNWIRE
);
538 assert(kr
== KERN_SUCCESS
);
544 retval
= KERN_SUCCESS
;
546 if (src_mapping
!= 0) {
547 /* remove the wired mapping of the source pages */
548 kr
= vm_map_remove(kernel_map
,
550 src_mapping
+ length
,
551 VM_MAP_REMOVE_KUNWIRE
);
552 assert(kr
== KERN_SUCCESS
);
557 /* clean up the UPL */
560 * The pages are currently dirty because we've just been
561 * writing on them, but as far as we're concerned, they're
562 * clean since they contain their "original" contents as
563 * provided by us, the pager.
564 * Tell the UPL to mark them "clean".
566 upl_clear_dirty(upl
, TRUE
);
568 /* abort or commit the UPL */
569 if (retval
!= KERN_SUCCESS
) {
572 upl_commit(upl
, NULL
, 0);
575 /* and deallocate the UPL */
579 if (dst_mapping
!= 0) {
580 /* clean up the mapping of the destination pages */
581 kr
= vm_map_remove(kernel_map
,
583 dst_mapping
+ PAGE_SIZE_64
,
585 assert(kr
== KERN_SUCCESS
);
594 * apple_protect_pager_reference()
596 * Get a reference on this memory object.
597 * For external usage only. Assumes that the initial reference count is not 0,
598 * i.e one should not "revive" a dead pager this way.
601 apple_protect_pager_reference(
602 memory_object_t mem_obj
)
604 apple_protect_pager_t pager
;
606 pager
= apple_protect_pager_lookup(mem_obj
);
608 mutex_lock(&apple_protect_pager_lock
);
609 assert(pager
->ref_count
> 0);
611 mutex_unlock(&apple_protect_pager_lock
);
616 * apple_protect_pager_dequeue:
618 * Removes a pager from the list of pagers.
620 * The caller must hold "apple_protect_pager_lock".
623 apple_protect_pager_dequeue(
624 apple_protect_pager_t pager
)
626 assert(!pager
->is_mapped
);
628 queue_remove(&apple_protect_pager_queue
,
630 apple_protect_pager_t
,
632 pager
->pager_queue
.next
= NULL
;
633 pager
->pager_queue
.prev
= NULL
;
635 apple_protect_pager_count
--;
639 * apple_protect_pager_terminate_internal:
641 * Trigger the asynchronous termination of the memory object associated
643 * When the memory object is terminated, there will be one more call
644 * to memory_object_deallocate() (i.e. apple_protect_pager_deallocate())
645 * to finish the clean up.
647 * "apple_protect_pager_lock" should not be held by the caller.
648 * We don't need the lock because the pager has already been removed from
649 * the pagers' list and is now ours exclusively.
652 apple_protect_pager_terminate_internal(
653 apple_protect_pager_t pager
)
655 assert(pager
->is_ready
);
656 assert(!pager
->is_mapped
);
658 if (pager
->backing_object
!= VM_OBJECT_NULL
) {
659 vm_object_deallocate(pager
->backing_object
);
660 pager
->backing_object
= VM_OBJECT_NULL
;
663 /* trigger the destruction of the memory object */
664 memory_object_destroy(pager
->pager_control
, 0);
668 * apple_protect_pager_deallocate_internal()
670 * Release a reference on this pager and free it when the last
671 * reference goes away.
672 * Can be called with apple_protect_pager_lock held or not but always returns
676 apple_protect_pager_deallocate_internal(
677 apple_protect_pager_t pager
,
680 boolean_t needs_trimming
;
684 mutex_lock(&apple_protect_pager_lock
);
687 count_unmapped
= (apple_protect_pager_count
-
688 apple_protect_pager_count_mapped
);
689 if (count_unmapped
> apple_protect_pager_cache_limit
) {
690 /* we have too many unmapped pagers: trim some */
691 needs_trimming
= TRUE
;
693 needs_trimming
= FALSE
;
696 /* drop a reference on this pager */
699 if (pager
->ref_count
== 1) {
701 * Only the "named" reference is left, which means that
702 * no one is realy holding on to this pager anymore.
705 apple_protect_pager_dequeue(pager
);
706 /* the pager is all ours: no need for the lock now */
707 mutex_unlock(&apple_protect_pager_lock
);
708 apple_protect_pager_terminate_internal(pager
);
709 } else if (pager
->ref_count
== 0) {
711 * Dropped the existence reference; the memory object has
712 * been terminated. Do some final cleanup and release the
715 mutex_unlock(&apple_protect_pager_lock
);
716 if (pager
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
717 memory_object_control_deallocate(pager
->pager_control
);
718 pager
->pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
720 kfree(pager
, sizeof (*pager
));
721 pager
= APPLE_PROTECT_PAGER_NULL
;
723 /* there are still plenty of references: keep going... */
724 mutex_unlock(&apple_protect_pager_lock
);
727 if (needs_trimming
) {
728 apple_protect_pager_trim();
730 /* caution: lock is not held on return... */
734 * apple_protect_pager_deallocate()
736 * Release a reference on this pager and free it when the last
737 * reference goes away.
740 apple_protect_pager_deallocate(
741 memory_object_t mem_obj
)
743 apple_protect_pager_t pager
;
745 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_deallocate: %x\n", mem_obj
));
746 pager
= apple_protect_pager_lookup(mem_obj
);
747 apple_protect_pager_deallocate_internal(pager
, FALSE
);
754 apple_protect_pager_terminate(
758 memory_object_t mem_obj
)
760 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_terminate: %x\n", mem_obj
));
769 apple_protect_pager_synchronize(
770 memory_object_t mem_obj
,
771 memory_object_offset_t offset
,
773 __unused vm_sync_t sync_flags
)
775 apple_protect_pager_t pager
;
777 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_synchronize: %x\n", mem_obj
));
779 pager
= apple_protect_pager_lookup(mem_obj
);
781 memory_object_synchronize_completed(pager
->pager_control
,
788 * apple_protect_pager_map()
790 * This allows VM to let us, the EMM, know that this memory object
791 * is currently mapped one or more times. This is called by VM only the first
792 * time the memory object gets mapped and we take one extra reference on the
793 * memory object to account for all its mappings.
796 apple_protect_pager_map(
797 memory_object_t mem_obj
)
799 apple_protect_pager_t pager
;
801 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_map: %x\n", mem_obj
));
803 pager
= apple_protect_pager_lookup(mem_obj
);
805 mutex_lock(&apple_protect_pager_lock
);
806 assert(pager
->is_ready
);
807 assert(pager
->ref_count
> 0); /* pager is alive */
808 if (pager
->is_mapped
== FALSE
) {
810 * First mapping of this pager: take an extra reference
811 * that will remain until all the mappings of this pager
814 pager
->is_mapped
= TRUE
;
816 apple_protect_pager_count_mapped
++;
818 mutex_unlock(&apple_protect_pager_lock
);
822 * apple_protect_pager_unmap()
824 * This is called by VM when this memory object is no longer mapped anywhere.
827 apple_protect_pager_unmap(
828 memory_object_t mem_obj
)
830 apple_protect_pager_t pager
;
833 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_unmap: %x\n", mem_obj
));
835 pager
= apple_protect_pager_lookup(mem_obj
);
837 mutex_lock(&apple_protect_pager_lock
);
838 if (pager
->is_mapped
) {
840 * All the mappings are gone, so let go of the one extra
841 * reference that represents all the mappings of this pager.
843 apple_protect_pager_count_mapped
--;
844 count_unmapped
= (apple_protect_pager_count
-
845 apple_protect_pager_count_mapped
);
846 if (count_unmapped
> apple_protect_pager_count_unmapped_max
) {
847 apple_protect_pager_count_unmapped_max
= count_unmapped
;
849 pager
->is_mapped
= FALSE
;
850 apple_protect_pager_deallocate_internal(pager
, TRUE
);
851 /* caution: deallocate_internal() released the lock ! */
853 mutex_unlock(&apple_protect_pager_lock
);
863 apple_protect_pager_t
864 apple_protect_pager_lookup(
865 memory_object_t mem_obj
)
867 apple_protect_pager_t pager
;
869 pager
= (apple_protect_pager_t
) mem_obj
;
870 assert(pager
->pager_ops
== &apple_protect_pager_ops
);
871 assert(pager
->ref_count
> 0);
875 apple_protect_pager_t
876 apple_protect_pager_create(
877 vm_object_t backing_object
)
879 apple_protect_pager_t pager
, pager2
;
880 memory_object_control_t control
;
883 pager
= (apple_protect_pager_t
) kalloc(sizeof (*pager
));
884 if (pager
== APPLE_PROTECT_PAGER_NULL
) {
885 return APPLE_PROTECT_PAGER_NULL
;
889 * The vm_map call takes both named entry ports and raw memory
890 * objects in the same parameter. We need to make sure that
891 * vm_map does not see this object as a named entry port. So,
892 * we reserve the second word in the object for a fake ip_kotype
893 * setting - that will tell vm_map to use it as a memory object.
895 pager
->pager_ops
= &apple_protect_pager_ops
;
896 pager
->pager_ikot
= IKOT_MEMORY_OBJECT
;
897 pager
->is_ready
= FALSE
;/* not ready until it has a "name" */
898 pager
->ref_count
= 2; /* existence + setup reference */
899 pager
->is_mapped
= FALSE
;
900 pager
->pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
901 pager
->backing_object
= backing_object
;
902 vm_object_reference(backing_object
);
904 mutex_lock(&apple_protect_pager_lock
);
905 /* see if anyone raced us to create a pager for the same object */
906 queue_iterate(&apple_protect_pager_queue
,
908 apple_protect_pager_t
,
910 if (pager2
->backing_object
== backing_object
) {
914 if (! queue_end(&apple_protect_pager_queue
,
915 (queue_entry_t
) pager2
)) {
916 /* while we hold the lock, transfer our setup ref to winner */
918 /* we lost the race, down with the loser... */
919 mutex_unlock(&apple_protect_pager_lock
);
920 vm_object_deallocate(pager
->backing_object
);
921 pager
->backing_object
= VM_OBJECT_NULL
;
922 kfree(pager
, sizeof (*pager
));
923 /* ... and go with the winner */
925 /* let the winner make sure the pager gets ready */
929 /* enter new pager at the head of our list of pagers */
930 queue_enter_first(&apple_protect_pager_queue
,
932 apple_protect_pager_t
,
934 apple_protect_pager_count
++;
935 if (apple_protect_pager_count
> apple_protect_pager_count_max
) {
936 apple_protect_pager_count_max
= apple_protect_pager_count
;
938 mutex_unlock(&apple_protect_pager_lock
);
940 kr
= memory_object_create_named((memory_object_t
) pager
,
943 assert(kr
== KERN_SUCCESS
);
945 mutex_lock(&apple_protect_pager_lock
);
946 /* the new pager is now ready to be used */
947 pager
->is_ready
= TRUE
;
948 mutex_unlock(&apple_protect_pager_lock
);
950 /* wakeup anyone waiting for this pager to be ready */
951 thread_wakeup(&pager
->is_ready
);
957 * apple_protect_pager_setup()
959 * Provide the caller with a memory object backed by the provided
960 * "backing_object" VM object. If such a memory object already exists,
961 * re-use it, otherwise create a new memory object.
964 apple_protect_pager_setup(
965 vm_object_t backing_object
)
967 apple_protect_pager_t pager
;
969 mutex_lock(&apple_protect_pager_lock
);
971 queue_iterate(&apple_protect_pager_queue
,
973 apple_protect_pager_t
,
975 if (pager
->backing_object
== backing_object
) {
979 if (queue_end(&apple_protect_pager_queue
,
980 (queue_entry_t
) pager
)) {
981 /* no existing pager for this backing object */
982 pager
= APPLE_PROTECT_PAGER_NULL
;
984 /* make sure pager doesn't disappear */
988 mutex_unlock(&apple_protect_pager_lock
);
990 if (pager
== APPLE_PROTECT_PAGER_NULL
) {
991 pager
= apple_protect_pager_create(backing_object
);
992 if (pager
== APPLE_PROTECT_PAGER_NULL
) {
993 return MEMORY_OBJECT_NULL
;
997 mutex_lock(&apple_protect_pager_lock
);
998 while (!pager
->is_ready
) {
999 thread_sleep_mutex(&pager
->is_ready
,
1000 &apple_protect_pager_lock
,
1003 mutex_unlock(&apple_protect_pager_lock
);
1005 return (memory_object_t
) pager
;
1009 apple_protect_pager_trim(void)
1011 apple_protect_pager_t pager
, prev_pager
;
1012 queue_head_t trim_queue
;
1016 mutex_lock(&apple_protect_pager_lock
);
1019 * We have too many pagers, try and trim some unused ones,
1020 * starting with the oldest pager at the end of the queue.
1022 queue_init(&trim_queue
);
1025 for (pager
= (apple_protect_pager_t
)
1026 queue_last(&apple_protect_pager_queue
);
1027 !queue_end(&apple_protect_pager_queue
,
1028 (queue_entry_t
) pager
);
1029 pager
= prev_pager
) {
1030 /* get prev elt before we dequeue */
1031 prev_pager
= (apple_protect_pager_t
)
1032 queue_prev(&pager
->pager_queue
);
1034 if (pager
->ref_count
== 2 &&
1036 !pager
->is_mapped
) {
1037 /* this pager can be trimmed */
1039 /* remove this pager from the main list ... */
1040 apple_protect_pager_dequeue(pager
);
1041 /* ... and add it to our trim queue */
1042 queue_enter_first(&trim_queue
,
1044 apple_protect_pager_t
,
1047 count_unmapped
= (apple_protect_pager_count
-
1048 apple_protect_pager_count_mapped
);
1049 if (count_unmapped
<= apple_protect_pager_cache_limit
) {
1050 /* we have enough pagers to trim */
1055 if (num_trim
> apple_protect_pager_num_trim_max
) {
1056 apple_protect_pager_num_trim_max
= num_trim
;
1058 apple_protect_pager_num_trim_total
+= num_trim
;
1060 mutex_unlock(&apple_protect_pager_lock
);
1062 /* terminate the trimmed pagers */
1063 while (!queue_empty(&trim_queue
)) {
1064 queue_remove_first(&trim_queue
,
1066 apple_protect_pager_t
,
1068 pager
->pager_queue
.next
= NULL
;
1069 pager
->pager_queue
.prev
= NULL
;
1070 assert(pager
->ref_count
== 2);
1072 * We can't call deallocate_internal() because the pager
1073 * has already been dequeued, but we still need to remove
1077 apple_protect_pager_terminate_internal(pager
);