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