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