]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_apple_protect.c
xnu-1699.26.8.tar.gz
[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
49 #include <ipc/ipc_port.h>
50 #include <ipc/ipc_space.h>
51
52 #include <default_pager/default_pager_types.h>
53 #include <default_pager/default_pager_object_server.h>
54
55 #include <vm/vm_fault.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_pageout.h>
58 #include <vm/memory_object.h>
59 #include <vm/vm_pageout.h>
60 #include <vm/vm_protos.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 /*
118 * Vector of VM operations for this EMM.
119 * These routines are invoked by VM via the memory_object_*() interfaces.
120 */
121 const struct memory_object_pager_ops apple_protect_pager_ops = {
122 apple_protect_pager_reference,
123 apple_protect_pager_deallocate,
124 apple_protect_pager_init,
125 apple_protect_pager_terminate,
126 apple_protect_pager_data_request,
127 apple_protect_pager_data_return,
128 apple_protect_pager_data_initialize,
129 apple_protect_pager_data_unlock,
130 apple_protect_pager_synchronize,
131 apple_protect_pager_map,
132 apple_protect_pager_last_unmap,
133 NULL, /* data_reclaim */
134 "apple protect pager"
135 };
136
137 /*
138 * The "apple_protect_pager" describes a memory object backed by
139 * the "apple protect" EMM.
140 */
141 typedef struct apple_protect_pager {
142 struct ipc_object_header pager_header; /* fake ip_kotype() */
143 memory_object_pager_ops_t pager_ops; /* == &apple_protect_pager_ops */
144 queue_chain_t pager_queue; /* next & prev pagers */
145 unsigned int ref_count; /* reference count */
146 boolean_t is_ready; /* is this pager ready ? */
147 boolean_t is_mapped; /* is this mem_obj mapped ? */
148 memory_object_control_t pager_control; /* mem object control handle */
149 vm_object_t backing_object; /* VM obj w/ encrypted data */
150 struct pager_crypt_info crypt;
151 } *apple_protect_pager_t;
152 #define APPLE_PROTECT_PAGER_NULL ((apple_protect_pager_t) NULL)
153 #define pager_ikot pager_header.io_bits
154
155 /*
156 * List of memory objects managed by this EMM.
157 * The list is protected by the "apple_protect_pager_lock" lock.
158 */
159 int apple_protect_pager_count = 0; /* number of pagers */
160 int apple_protect_pager_count_mapped = 0; /* number of unmapped pagers */
161 queue_head_t apple_protect_pager_queue;
162 decl_lck_mtx_data(,apple_protect_pager_lock)
163
164 /*
165 * Maximum number of unmapped pagers we're willing to keep around.
166 */
167 int apple_protect_pager_cache_limit = 10;
168
169 /*
170 * Statistics & counters.
171 */
172 int apple_protect_pager_count_max = 0;
173 int apple_protect_pager_count_unmapped_max = 0;
174 int apple_protect_pager_num_trim_max = 0;
175 int apple_protect_pager_num_trim_total = 0;
176
177
178 lck_grp_t apple_protect_pager_lck_grp;
179 lck_grp_attr_t apple_protect_pager_lck_grp_attr;
180 lck_attr_t apple_protect_pager_lck_attr;
181
182
183 /* internal prototypes */
184 apple_protect_pager_t apple_protect_pager_create(vm_object_t backing_object,
185 struct pager_crypt_info *crypt_info);
186 apple_protect_pager_t apple_protect_pager_lookup(memory_object_t mem_obj);
187 void apple_protect_pager_dequeue(apple_protect_pager_t pager);
188 void apple_protect_pager_deallocate_internal(apple_protect_pager_t pager,
189 boolean_t locked);
190 void apple_protect_pager_terminate_internal(apple_protect_pager_t pager);
191 void apple_protect_pager_trim(void);
192
193
194 #if DEBUG
195 int apple_protect_pagerdebug = 0;
196 #define PAGER_ALL 0xffffffff
197 #define PAGER_INIT 0x00000001
198 #define PAGER_PAGEIN 0x00000002
199
200 #define PAGER_DEBUG(LEVEL, A) \
201 MACRO_BEGIN \
202 if ((apple_protect_pagerdebug & LEVEL)==LEVEL) { \
203 printf A; \
204 } \
205 MACRO_END
206 #else
207 #define PAGER_DEBUG(LEVEL, A)
208 #endif
209
210
211 void
212 apple_protect_pager_bootstrap(void)
213 {
214 lck_grp_attr_setdefault(&apple_protect_pager_lck_grp_attr);
215 lck_grp_init(&apple_protect_pager_lck_grp, "apple_protect", &apple_protect_pager_lck_grp_attr);
216 lck_attr_setdefault(&apple_protect_pager_lck_attr);
217 lck_mtx_init(&apple_protect_pager_lock, &apple_protect_pager_lck_grp, &apple_protect_pager_lck_attr);
218 queue_init(&apple_protect_pager_queue);
219 }
220
221 /*
222 * apple_protect_pager_init()
223 *
224 * Initialize the memory object and makes it ready to be used and mapped.
225 */
226 kern_return_t
227 apple_protect_pager_init(
228 memory_object_t mem_obj,
229 memory_object_control_t control,
230 #if !DEBUG
231 __unused
232 #endif
233 memory_object_cluster_size_t pg_size)
234 {
235 apple_protect_pager_t pager;
236 kern_return_t kr;
237 memory_object_attr_info_data_t attributes;
238
239 PAGER_DEBUG(PAGER_ALL,
240 ("apple_protect_pager_init: %p, %p, %x\n",
241 mem_obj, control, pg_size));
242
243 if (control == MEMORY_OBJECT_CONTROL_NULL)
244 return KERN_INVALID_ARGUMENT;
245
246 pager = apple_protect_pager_lookup(mem_obj);
247
248 memory_object_control_reference(control);
249
250 pager->pager_control = control;
251
252 attributes.copy_strategy = MEMORY_OBJECT_COPY_DELAY;
253 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
254 attributes.cluster_size = (1 << (PAGE_SHIFT));
255 attributes.may_cache_object = FALSE;
256 attributes.temporary = TRUE;
257
258 kr = memory_object_change_attributes(
259 control,
260 MEMORY_OBJECT_ATTRIBUTE_INFO,
261 (memory_object_info_t) &attributes,
262 MEMORY_OBJECT_ATTR_INFO_COUNT);
263 if (kr != KERN_SUCCESS)
264 panic("apple_protect_pager_init: "
265 "memory_object_change_attributes() failed");
266
267 return KERN_SUCCESS;
268 }
269
270 /*
271 * apple_protect_data_return()
272 *
273 * Handles page-out requests from VM. This should never happen since
274 * the pages provided by this EMM are not supposed to be dirty or dirtied
275 * and VM should simply discard the contents and reclaim the pages if it
276 * needs to.
277 */
278 kern_return_t
279 apple_protect_pager_data_return(
280 __unused memory_object_t mem_obj,
281 __unused memory_object_offset_t offset,
282 __unused memory_object_cluster_size_t data_cnt,
283 __unused memory_object_offset_t *resid_offset,
284 __unused int *io_error,
285 __unused boolean_t dirty,
286 __unused boolean_t kernel_copy,
287 __unused int upl_flags)
288 {
289 panic("apple_protect_pager_data_return: should never get called");
290 return KERN_FAILURE;
291 }
292
293 kern_return_t
294 apple_protect_pager_data_initialize(
295 __unused memory_object_t mem_obj,
296 __unused memory_object_offset_t offset,
297 __unused memory_object_cluster_size_t data_cnt)
298 {
299 panic("apple_protect_pager_data_initialize: should never get called");
300 return KERN_FAILURE;
301 }
302
303 kern_return_t
304 apple_protect_pager_data_unlock(
305 __unused memory_object_t mem_obj,
306 __unused memory_object_offset_t offset,
307 __unused memory_object_size_t size,
308 __unused vm_prot_t desired_access)
309 {
310 return KERN_FAILURE;
311 }
312
313 /*
314 * apple_protect_pager_data_request()
315 *
316 * Handles page-in requests from VM.
317 */
318 kern_return_t
319 apple_protect_pager_data_request(
320 memory_object_t mem_obj,
321 memory_object_offset_t offset,
322 memory_object_cluster_size_t length,
323 #if !DEBUG
324 __unused
325 #endif
326 vm_prot_t protection_required,
327 memory_object_fault_info_t mo_fault_info)
328 {
329 apple_protect_pager_t pager;
330 memory_object_control_t mo_control;
331 upl_t upl;
332 int upl_flags;
333 upl_size_t upl_size;
334 upl_page_info_t *upl_pl;
335 unsigned int pl_count;
336 vm_object_t src_object, dst_object;
337 kern_return_t kr, retval;
338 vm_map_offset_t kernel_mapping;
339 vm_offset_t src_vaddr, dst_vaddr;
340 vm_offset_t cur_offset;
341 vm_map_entry_t map_entry;
342 kern_return_t error_code;
343 vm_prot_t prot;
344 vm_page_t src_page, top_page;
345 int interruptible;
346 struct vm_object_fault_info fault_info;
347 int ret;
348
349 PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_data_request: %p, %llx, %x, %x\n", mem_obj, offset, length, protection_required));
350
351 retval = KERN_SUCCESS;
352 src_object = VM_OBJECT_NULL;
353 kernel_mapping = 0;
354 upl = NULL;
355 upl_pl = NULL;
356 fault_info = *((struct vm_object_fault_info *) mo_fault_info);
357 fault_info.stealth = TRUE;
358 fault_info.io_sync = FALSE;
359 fault_info.mark_zf_absent = FALSE;
360 interruptible = fault_info.interruptible;
361
362 pager = apple_protect_pager_lookup(mem_obj);
363 assert(pager->is_ready);
364 assert(pager->ref_count > 1); /* pager is alive and mapped */
365
366 PAGER_DEBUG(PAGER_PAGEIN, ("apple_protect_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj, offset, length, protection_required, pager));
367
368 /*
369 * Gather in a UPL all the VM pages requested by VM.
370 */
371 mo_control = pager->pager_control;
372
373 upl_size = length;
374 upl_flags =
375 UPL_RET_ONLY_ABSENT |
376 UPL_SET_LITE |
377 UPL_NO_SYNC |
378 UPL_CLEAN_IN_PLACE | /* triggers UPL_CLEAR_DIRTY */
379 UPL_SET_INTERNAL;
380 pl_count = 0;
381 kr = memory_object_upl_request(mo_control,
382 offset, upl_size,
383 &upl, NULL, NULL, upl_flags);
384 if (kr != KERN_SUCCESS) {
385 retval = kr;
386 goto done;
387 }
388 dst_object = mo_control->moc_object;
389 assert(dst_object != VM_OBJECT_NULL);
390
391
392 /*
393 * Reserve 2 virtual pages in the kernel address space to map each
394 * source and destination physical pages when it's their turn to
395 * be processed.
396 */
397 vm_object_reference(kernel_object); /* ref. for mapping */
398 kr = vm_map_find_space(kernel_map,
399 &kernel_mapping,
400 2 * PAGE_SIZE_64,
401 0,
402 0,
403 &map_entry);
404 if (kr != KERN_SUCCESS) {
405 vm_object_deallocate(kernel_object);
406 retval = kr;
407 goto done;
408 }
409 map_entry->object.vm_object = kernel_object;
410 map_entry->offset = kernel_mapping;
411 vm_map_unlock(kernel_map);
412 src_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping);
413 dst_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping + PAGE_SIZE_64);
414
415 /*
416 * We'll map the encrypted data in the kernel address space from the
417 * backing VM object (itself backed by the encrypted file via
418 * the vnode pager).
419 */
420 src_object = pager->backing_object;
421 assert(src_object != VM_OBJECT_NULL);
422 vm_object_reference(src_object); /* to keep the source object alive */
423
424 /*
425 * Fill in the contents of the pages requested by VM.
426 */
427 upl_pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
428 pl_count = length / PAGE_SIZE;
429 for (cur_offset = 0;
430 retval == KERN_SUCCESS && cur_offset < length;
431 cur_offset += PAGE_SIZE) {
432 ppnum_t dst_pnum;
433
434 if (!upl_page_present(upl_pl, (int)(cur_offset / PAGE_SIZE))) {
435 /* this page is not in the UPL: skip it */
436 continue;
437 }
438
439 /*
440 * Map the source (encrypted) page in the kernel's
441 * virtual address space.
442 * We already hold a reference on the src_object.
443 */
444 retry_src_fault:
445 vm_object_lock(src_object);
446 vm_object_paging_begin(src_object);
447 error_code = 0;
448 prot = VM_PROT_READ;
449 kr = vm_fault_page(src_object,
450 offset + cur_offset,
451 VM_PROT_READ,
452 FALSE,
453 &prot,
454 &src_page,
455 &top_page,
456 NULL,
457 &error_code,
458 FALSE,
459 FALSE,
460 &fault_info);
461 switch (kr) {
462 case VM_FAULT_SUCCESS:
463 break;
464 case VM_FAULT_RETRY:
465 goto retry_src_fault;
466 case VM_FAULT_MEMORY_SHORTAGE:
467 if (vm_page_wait(interruptible)) {
468 goto retry_src_fault;
469 }
470 /* fall thru */
471 case VM_FAULT_INTERRUPTED:
472 retval = MACH_SEND_INTERRUPTED;
473 goto done;
474 case VM_FAULT_SUCCESS_NO_VM_PAGE:
475 /* success but no VM page: fail */
476 vm_object_paging_end(src_object);
477 vm_object_unlock(src_object);
478 /*FALLTHROUGH*/
479 case VM_FAULT_MEMORY_ERROR:
480 /* the page is not there ! */
481 if (error_code) {
482 retval = error_code;
483 } else {
484 retval = KERN_MEMORY_ERROR;
485 }
486 goto done;
487 default:
488 panic("apple_protect_pager_data_request: "
489 "vm_fault_page() unexpected error 0x%x\n",
490 kr);
491 }
492 assert(src_page != VM_PAGE_NULL);
493 assert(src_page->busy);
494
495 if (!src_page->active &&
496 !src_page->inactive &&
497 !src_page->throttled) {
498 vm_page_lockspin_queues();
499 if (!src_page->active &&
500 !src_page->inactive &&
501 !src_page->throttled) {
502 vm_page_deactivate(src_page);
503 }
504 vm_page_unlock_queues();
505 }
506
507 /*
508 * Establish an explicit mapping of the source
509 * physical page.
510 */
511 pmap_enter(kernel_pmap,
512 kernel_mapping,
513 src_page->phys_page,
514 VM_PROT_READ,
515 0,
516 TRUE);
517 /*
518 * Establish an explicit pmap mapping of the destination
519 * physical page.
520 * We can't do a regular VM mapping because the VM page
521 * is "busy".
522 */
523 dst_pnum = (ppnum_t)
524 upl_phys_page(upl_pl, (int)(cur_offset / PAGE_SIZE));
525 assert(dst_pnum != 0);
526 pmap_enter(kernel_pmap,
527 kernel_mapping + PAGE_SIZE_64,
528 dst_pnum,
529 VM_PROT_READ | VM_PROT_WRITE,
530 0,
531 TRUE);
532
533 /*
534 * Decrypt the encrypted contents of the source page
535 * into the destination page.
536 */
537 ret = pager->crypt.page_decrypt((const void *) src_vaddr,
538 (void *) dst_vaddr,
539 offset+cur_offset,
540 pager->crypt.crypt_ops);
541 if (ret) {
542 /*
543 * Decryption failed. Abort the fault.
544 */
545 retval = KERN_ABORTED;
546 } else {
547 /*
548 * Validate the original page...
549 */
550 if (src_page->object->code_signed) {
551 vm_page_validate_cs_mapped(
552 src_page,
553 (const void *) src_vaddr);
554 }
555 /*
556 * ... and transfer the results to the destination page.
557 */
558 UPL_SET_CS_VALIDATED(upl_pl, cur_offset / PAGE_SIZE,
559 src_page->cs_validated);
560 UPL_SET_CS_TAINTED(upl_pl, cur_offset / PAGE_SIZE,
561 src_page->cs_tainted);
562 }
563
564 /*
565 * Remove the pmap mapping of the source and destination pages
566 * in the kernel.
567 */
568 pmap_remove(kernel_pmap,
569 (addr64_t) kernel_mapping,
570 (addr64_t) (kernel_mapping + (2 * PAGE_SIZE_64)));
571
572 /*
573 * Cleanup the result of vm_fault_page() of the source page.
574 */
575 PAGE_WAKEUP_DONE(src_page);
576 vm_object_paging_end(src_page->object);
577 vm_object_unlock(src_page->object);
578 if (top_page != VM_PAGE_NULL) {
579 vm_object_t top_object;
580
581 top_object = top_page->object;
582 vm_object_lock(top_object);
583 VM_PAGE_FREE(top_page);
584 vm_object_paging_end(top_object);
585 vm_object_unlock(top_object);
586 }
587 }
588
589 done:
590 if (upl != NULL) {
591 /* clean up the UPL */
592
593 /*
594 * The pages are currently dirty because we've just been
595 * writing on them, but as far as we're concerned, they're
596 * clean since they contain their "original" contents as
597 * provided by us, the pager.
598 * Tell the UPL to mark them "clean".
599 */
600 upl_clear_dirty(upl, TRUE);
601
602 /* abort or commit the UPL */
603 if (retval != KERN_SUCCESS) {
604 upl_abort(upl, 0);
605 if (retval == KERN_ABORTED) {
606 wait_result_t wait_result;
607
608 /*
609 * We aborted the fault and did not provide
610 * any contents for the requested pages but
611 * the pages themselves are not invalid, so
612 * let's return success and let the caller
613 * retry the fault, in case it might succeed
614 * later (when the decryption code is up and
615 * running in the kernel, for example).
616 */
617 retval = KERN_SUCCESS;
618 /*
619 * Wait a little bit first to avoid using
620 * too much CPU time retrying and failing
621 * the same fault over and over again.
622 */
623 wait_result = assert_wait_timeout(
624 (event_t) apple_protect_pager_data_request,
625 THREAD_UNINT,
626 10000, /* 10ms */
627 NSEC_PER_USEC);
628 assert(wait_result == THREAD_WAITING);
629 wait_result = thread_block(THREAD_CONTINUE_NULL);
630 assert(wait_result == THREAD_TIMED_OUT);
631 }
632 } else {
633 boolean_t empty;
634 upl_commit_range(upl, 0, upl->size,
635 UPL_COMMIT_CS_VALIDATED,
636 upl_pl, pl_count, &empty);
637 }
638
639 /* and deallocate the UPL */
640 upl_deallocate(upl);
641 upl = NULL;
642 }
643 if (kernel_mapping != 0) {
644 /* clean up the mapping of the source and destination pages */
645 kr = vm_map_remove(kernel_map,
646 kernel_mapping,
647 kernel_mapping + (2 * PAGE_SIZE_64),
648 VM_MAP_NO_FLAGS);
649 assert(kr == KERN_SUCCESS);
650 kernel_mapping = 0;
651 src_vaddr = 0;
652 dst_vaddr = 0;
653 }
654 if (src_object != VM_OBJECT_NULL) {
655 vm_object_deallocate(src_object);
656 }
657
658 return retval;
659 }
660
661 /*
662 * apple_protect_pager_reference()
663 *
664 * Get a reference on this memory object.
665 * For external usage only. Assumes that the initial reference count is not 0,
666 * i.e one should not "revive" a dead pager this way.
667 */
668 void
669 apple_protect_pager_reference(
670 memory_object_t mem_obj)
671 {
672 apple_protect_pager_t pager;
673
674 pager = apple_protect_pager_lookup(mem_obj);
675
676 lck_mtx_lock(&apple_protect_pager_lock);
677 assert(pager->ref_count > 0);
678 pager->ref_count++;
679 lck_mtx_unlock(&apple_protect_pager_lock);
680 }
681
682
683 /*
684 * apple_protect_pager_dequeue:
685 *
686 * Removes a pager from the list of pagers.
687 *
688 * The caller must hold "apple_protect_pager_lock".
689 */
690 void
691 apple_protect_pager_dequeue(
692 apple_protect_pager_t pager)
693 {
694 assert(!pager->is_mapped);
695
696 queue_remove(&apple_protect_pager_queue,
697 pager,
698 apple_protect_pager_t,
699 pager_queue);
700 pager->pager_queue.next = NULL;
701 pager->pager_queue.prev = NULL;
702
703 apple_protect_pager_count--;
704 }
705
706 /*
707 * apple_protect_pager_terminate_internal:
708 *
709 * Trigger the asynchronous termination of the memory object associated
710 * with this pager.
711 * When the memory object is terminated, there will be one more call
712 * to memory_object_deallocate() (i.e. apple_protect_pager_deallocate())
713 * to finish the clean up.
714 *
715 * "apple_protect_pager_lock" should not be held by the caller.
716 * We don't need the lock because the pager has already been removed from
717 * the pagers' list and is now ours exclusively.
718 */
719 void
720 apple_protect_pager_terminate_internal(
721 apple_protect_pager_t pager)
722 {
723 assert(pager->is_ready);
724 assert(!pager->is_mapped);
725
726 if (pager->backing_object != VM_OBJECT_NULL) {
727 vm_object_deallocate(pager->backing_object);
728 pager->backing_object = VM_OBJECT_NULL;
729 }
730
731 /* deallocate any crypt module data */
732 if(pager->crypt.crypt_end)
733 pager->crypt.crypt_end(pager->crypt.crypt_ops);
734
735 /* trigger the destruction of the memory object */
736 memory_object_destroy(pager->pager_control, 0);
737 }
738
739 /*
740 * apple_protect_pager_deallocate_internal()
741 *
742 * Release a reference on this pager and free it when the last
743 * reference goes away.
744 * Can be called with apple_protect_pager_lock held or not but always returns
745 * with it unlocked.
746 */
747 void
748 apple_protect_pager_deallocate_internal(
749 apple_protect_pager_t pager,
750 boolean_t locked)
751 {
752 boolean_t needs_trimming;
753 int count_unmapped;
754
755 if (! locked) {
756 lck_mtx_lock(&apple_protect_pager_lock);
757 }
758
759 count_unmapped = (apple_protect_pager_count -
760 apple_protect_pager_count_mapped);
761 if (count_unmapped > apple_protect_pager_cache_limit) {
762 /* we have too many unmapped pagers: trim some */
763 needs_trimming = TRUE;
764 } else {
765 needs_trimming = FALSE;
766 }
767
768 /* drop a reference on this pager */
769 pager->ref_count--;
770
771 if (pager->ref_count == 1) {
772 /*
773 * Only the "named" reference is left, which means that
774 * no one is really holding on to this pager anymore.
775 * Terminate it.
776 */
777 apple_protect_pager_dequeue(pager);
778 /* the pager is all ours: no need for the lock now */
779 lck_mtx_unlock(&apple_protect_pager_lock);
780 apple_protect_pager_terminate_internal(pager);
781 } else if (pager->ref_count == 0) {
782 /*
783 * Dropped the existence reference; the memory object has
784 * been terminated. Do some final cleanup and release the
785 * pager structure.
786 */
787 lck_mtx_unlock(&apple_protect_pager_lock);
788 if (pager->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
789 memory_object_control_deallocate(pager->pager_control);
790 pager->pager_control = MEMORY_OBJECT_CONTROL_NULL;
791 }
792 kfree(pager, sizeof (*pager));
793 pager = APPLE_PROTECT_PAGER_NULL;
794 } else {
795 /* there are still plenty of references: keep going... */
796 lck_mtx_unlock(&apple_protect_pager_lock);
797 }
798
799 if (needs_trimming) {
800 apple_protect_pager_trim();
801 }
802 /* caution: lock is not held on return... */
803 }
804
805 /*
806 * apple_protect_pager_deallocate()
807 *
808 * Release a reference on this pager and free it when the last
809 * reference goes away.
810 */
811 void
812 apple_protect_pager_deallocate(
813 memory_object_t mem_obj)
814 {
815 apple_protect_pager_t pager;
816
817 PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_deallocate: %p\n", mem_obj));
818 pager = apple_protect_pager_lookup(mem_obj);
819 apple_protect_pager_deallocate_internal(pager, FALSE);
820 }
821
822 /*
823 *
824 */
825 kern_return_t
826 apple_protect_pager_terminate(
827 #if !DEBUG
828 __unused
829 #endif
830 memory_object_t mem_obj)
831 {
832 PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_terminate: %p\n", mem_obj));
833
834 return KERN_SUCCESS;
835 }
836
837 /*
838 *
839 */
840 kern_return_t
841 apple_protect_pager_synchronize(
842 memory_object_t mem_obj,
843 memory_object_offset_t offset,
844 memory_object_size_t length,
845 __unused vm_sync_t sync_flags)
846 {
847 apple_protect_pager_t pager;
848
849 PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_synchronize: %p\n", mem_obj));
850
851 pager = apple_protect_pager_lookup(mem_obj);
852
853 memory_object_synchronize_completed(pager->pager_control,
854 offset, length);
855
856 return KERN_SUCCESS;
857 }
858
859 /*
860 * apple_protect_pager_map()
861 *
862 * This allows VM to let us, the EMM, know that this memory object
863 * is currently mapped one or more times. This is called by VM each time
864 * the memory object gets mapped and we take one extra reference on the
865 * memory object to account for all its mappings.
866 */
867 kern_return_t
868 apple_protect_pager_map(
869 memory_object_t mem_obj,
870 __unused vm_prot_t prot)
871 {
872 apple_protect_pager_t pager;
873
874 PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_map: %p\n", mem_obj));
875
876 pager = apple_protect_pager_lookup(mem_obj);
877
878 lck_mtx_lock(&apple_protect_pager_lock);
879 assert(pager->is_ready);
880 assert(pager->ref_count > 0); /* pager is alive */
881 if (pager->is_mapped == FALSE) {
882 /*
883 * First mapping of this pager: take an extra reference
884 * that will remain until all the mappings of this pager
885 * are removed.
886 */
887 pager->is_mapped = TRUE;
888 pager->ref_count++;
889 apple_protect_pager_count_mapped++;
890 }
891 lck_mtx_unlock(&apple_protect_pager_lock);
892
893 return KERN_SUCCESS;
894 }
895
896 /*
897 * apple_protect_pager_last_unmap()
898 *
899 * This is called by VM when this memory object is no longer mapped anywhere.
900 */
901 kern_return_t
902 apple_protect_pager_last_unmap(
903 memory_object_t mem_obj)
904 {
905 apple_protect_pager_t pager;
906 int count_unmapped;
907
908 PAGER_DEBUG(PAGER_ALL,
909 ("apple_protect_pager_last_unmap: %p\n", mem_obj));
910
911 pager = apple_protect_pager_lookup(mem_obj);
912
913 lck_mtx_lock(&apple_protect_pager_lock);
914 if (pager->is_mapped) {
915 /*
916 * All the mappings are gone, so let go of the one extra
917 * reference that represents all the mappings of this pager.
918 */
919 apple_protect_pager_count_mapped--;
920 count_unmapped = (apple_protect_pager_count -
921 apple_protect_pager_count_mapped);
922 if (count_unmapped > apple_protect_pager_count_unmapped_max) {
923 apple_protect_pager_count_unmapped_max = count_unmapped;
924 }
925 pager->is_mapped = FALSE;
926 apple_protect_pager_deallocate_internal(pager, TRUE);
927 /* caution: deallocate_internal() released the lock ! */
928 } else {
929 lck_mtx_unlock(&apple_protect_pager_lock);
930 }
931
932 return KERN_SUCCESS;
933 }
934
935
936 /*
937 *
938 */
939 apple_protect_pager_t
940 apple_protect_pager_lookup(
941 memory_object_t mem_obj)
942 {
943 apple_protect_pager_t pager;
944
945 pager = (apple_protect_pager_t) mem_obj;
946 assert(pager->pager_ops == &apple_protect_pager_ops);
947 assert(pager->ref_count > 0);
948 return pager;
949 }
950
951 apple_protect_pager_t
952 apple_protect_pager_create(
953 vm_object_t backing_object,
954 struct pager_crypt_info *crypt_info)
955 {
956 apple_protect_pager_t pager, pager2;
957 memory_object_control_t control;
958 kern_return_t kr;
959
960 pager = (apple_protect_pager_t) kalloc(sizeof (*pager));
961 if (pager == APPLE_PROTECT_PAGER_NULL) {
962 return APPLE_PROTECT_PAGER_NULL;
963 }
964
965 /*
966 * The vm_map call takes both named entry ports and raw memory
967 * objects in the same parameter. We need to make sure that
968 * vm_map does not see this object as a named entry port. So,
969 * we reserve the first word in the object for a fake ip_kotype
970 * setting - that will tell vm_map to use it as a memory object.
971 */
972 pager->pager_ops = &apple_protect_pager_ops;
973 pager->pager_ikot = IKOT_MEMORY_OBJECT;
974 pager->is_ready = FALSE;/* not ready until it has a "name" */
975 pager->ref_count = 2; /* existence + setup reference */
976 pager->is_mapped = FALSE;
977 pager->pager_control = MEMORY_OBJECT_CONTROL_NULL;
978 pager->backing_object = backing_object;
979 pager->crypt = *crypt_info;
980
981 vm_object_reference(backing_object);
982
983 lck_mtx_lock(&apple_protect_pager_lock);
984 /* see if anyone raced us to create a pager for the same object */
985 queue_iterate(&apple_protect_pager_queue,
986 pager2,
987 apple_protect_pager_t,
988 pager_queue) {
989 if (pager2->backing_object == backing_object) {
990 break;
991 }
992 }
993 if (! queue_end(&apple_protect_pager_queue,
994 (queue_entry_t) pager2)) {
995 /* while we hold the lock, transfer our setup ref to winner */
996 pager2->ref_count++;
997 /* we lost the race, down with the loser... */
998 lck_mtx_unlock(&apple_protect_pager_lock);
999 vm_object_deallocate(pager->backing_object);
1000 pager->backing_object = VM_OBJECT_NULL;
1001 kfree(pager, sizeof (*pager));
1002 /* ... and go with the winner */
1003 pager = pager2;
1004 /* let the winner make sure the pager gets ready */
1005 return pager;
1006 }
1007
1008 /* enter new pager at the head of our list of pagers */
1009 queue_enter_first(&apple_protect_pager_queue,
1010 pager,
1011 apple_protect_pager_t,
1012 pager_queue);
1013 apple_protect_pager_count++;
1014 if (apple_protect_pager_count > apple_protect_pager_count_max) {
1015 apple_protect_pager_count_max = apple_protect_pager_count;
1016 }
1017 lck_mtx_unlock(&apple_protect_pager_lock);
1018
1019 kr = memory_object_create_named((memory_object_t) pager,
1020 0,
1021 &control);
1022 assert(kr == KERN_SUCCESS);
1023
1024 lck_mtx_lock(&apple_protect_pager_lock);
1025 /* the new pager is now ready to be used */
1026 pager->is_ready = TRUE;
1027 lck_mtx_unlock(&apple_protect_pager_lock);
1028
1029 /* wakeup anyone waiting for this pager to be ready */
1030 thread_wakeup(&pager->is_ready);
1031
1032 return pager;
1033 }
1034
1035 /*
1036 * apple_protect_pager_setup()
1037 *
1038 * Provide the caller with a memory object backed by the provided
1039 * "backing_object" VM object. If such a memory object already exists,
1040 * re-use it, otherwise create a new memory object.
1041 */
1042 memory_object_t
1043 apple_protect_pager_setup(
1044 vm_object_t backing_object,
1045 struct pager_crypt_info *crypt_info)
1046 {
1047 apple_protect_pager_t pager;
1048
1049 lck_mtx_lock(&apple_protect_pager_lock);
1050
1051 queue_iterate(&apple_protect_pager_queue,
1052 pager,
1053 apple_protect_pager_t,
1054 pager_queue) {
1055 if (pager->backing_object == backing_object) {
1056 /* For the same object we must always use the same protection options */
1057 if (!((pager->crypt.page_decrypt == crypt_info->page_decrypt) &&
1058 (pager->crypt.crypt_ops == crypt_info->crypt_ops) )) {
1059 lck_mtx_unlock(&apple_protect_pager_lock);
1060 return MEMORY_OBJECT_NULL;
1061 }
1062 break;
1063 }
1064 }
1065 if (queue_end(&apple_protect_pager_queue,
1066 (queue_entry_t) pager)) {
1067 /* no existing pager for this backing object */
1068 pager = APPLE_PROTECT_PAGER_NULL;
1069 } else {
1070 /* make sure pager doesn't disappear */
1071 pager->ref_count++;
1072 }
1073
1074 lck_mtx_unlock(&apple_protect_pager_lock);
1075
1076 if (pager == APPLE_PROTECT_PAGER_NULL) {
1077 pager = apple_protect_pager_create(backing_object, crypt_info);
1078 if (pager == APPLE_PROTECT_PAGER_NULL) {
1079 return MEMORY_OBJECT_NULL;
1080 }
1081 }
1082
1083 lck_mtx_lock(&apple_protect_pager_lock);
1084 while (!pager->is_ready) {
1085 lck_mtx_sleep(&apple_protect_pager_lock,
1086 LCK_SLEEP_DEFAULT,
1087 &pager->is_ready,
1088 THREAD_UNINT);
1089 }
1090 lck_mtx_unlock(&apple_protect_pager_lock);
1091
1092 return (memory_object_t) pager;
1093 }
1094
1095 void
1096 apple_protect_pager_trim(void)
1097 {
1098 apple_protect_pager_t pager, prev_pager;
1099 queue_head_t trim_queue;
1100 int num_trim;
1101 int count_unmapped;
1102
1103 lck_mtx_lock(&apple_protect_pager_lock);
1104
1105 /*
1106 * We have too many pagers, try and trim some unused ones,
1107 * starting with the oldest pager at the end of the queue.
1108 */
1109 queue_init(&trim_queue);
1110 num_trim = 0;
1111
1112 for (pager = (apple_protect_pager_t)
1113 queue_last(&apple_protect_pager_queue);
1114 !queue_end(&apple_protect_pager_queue,
1115 (queue_entry_t) pager);
1116 pager = prev_pager) {
1117 /* get prev elt before we dequeue */
1118 prev_pager = (apple_protect_pager_t)
1119 queue_prev(&pager->pager_queue);
1120
1121 if (pager->ref_count == 2 &&
1122 pager->is_ready &&
1123 !pager->is_mapped) {
1124 /* this pager can be trimmed */
1125 num_trim++;
1126 /* remove this pager from the main list ... */
1127 apple_protect_pager_dequeue(pager);
1128 /* ... and add it to our trim queue */
1129 queue_enter_first(&trim_queue,
1130 pager,
1131 apple_protect_pager_t,
1132 pager_queue);
1133
1134 count_unmapped = (apple_protect_pager_count -
1135 apple_protect_pager_count_mapped);
1136 if (count_unmapped <= apple_protect_pager_cache_limit) {
1137 /* we have enough pagers to trim */
1138 break;
1139 }
1140 }
1141 }
1142 if (num_trim > apple_protect_pager_num_trim_max) {
1143 apple_protect_pager_num_trim_max = num_trim;
1144 }
1145 apple_protect_pager_num_trim_total += num_trim;
1146
1147 lck_mtx_unlock(&apple_protect_pager_lock);
1148
1149 /* terminate the trimmed pagers */
1150 while (!queue_empty(&trim_queue)) {
1151 queue_remove_first(&trim_queue,
1152 pager,
1153 apple_protect_pager_t,
1154 pager_queue);
1155 pager->pager_queue.next = NULL;
1156 pager->pager_queue.prev = NULL;
1157 assert(pager->ref_count == 2);
1158 /*
1159 * We can't call deallocate_internal() because the pager
1160 * has already been dequeued, but we still need to remove
1161 * a reference.
1162 */
1163 pager->ref_count--;
1164 apple_protect_pager_terminate_internal(pager);
1165 }
1166 }