2 * Copyright (c) 2018 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/errno.h>
31 #include <mach/mach_types.h>
32 #include <mach/mach_traps.h>
33 #include <mach/host_priv.h>
34 #include <mach/kern_return.h>
35 #include <mach/memory_object_control.h>
36 #include <mach/memory_object_types.h>
37 #include <mach/port.h>
38 #include <mach/policy.h>
40 #include <mach/thread_act.h>
41 #include <mach/mach_vm.h>
43 #include <kern/host.h>
44 #include <kern/kalloc.h>
45 #include <kern/queue.h>
46 #include <kern/thread.h>
47 #include <kern/ipc_kobject.h>
49 #include <ipc/ipc_port.h>
50 #include <ipc/ipc_space.h>
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>
63 * SHARED REGION MEMORY PAGER
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, ...).
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.
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.
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.
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
,
99 boolean_t kernel_copy
,
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
,
114 kern_return_t
shared_region_pager_last_unmap(memory_object_t mem_obj
);
117 * Vector of VM operations for this EMM.
118 * These routines are invoked by VM via the memory_object_*() interfaces.
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 */
137 * The "shared_region_pager" describes a memory object backed by
138 * the "shared_region" EMM.
140 typedef struct shared_region_pager
{
141 /* mandatory generic header */
142 struct memory_object sc_pgr_hdr
;
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)
156 * List of memory objects managed by this EMM.
157 * The list is protected by the "shared_region_pager_lock" lock.
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
)
165 * Maximum number of unmapped pagers we're willing to keep around.
167 int shared_region_pager_cache_limit
= 0;
170 * Statistics & counters.
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;
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
;
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;
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
,
196 void shared_region_pager_terminate_internal(shared_region_pager_t pager
);
197 void shared_region_pager_trim(void);
201 int shared_region_pagerdebug
= 0;
202 #define PAGER_ALL 0xffffffff
203 #define PAGER_INIT 0x00000001
204 #define PAGER_PAGEIN 0x00000002
206 #define PAGER_DEBUG(LEVEL, A) \
208 if ((shared_region_pagerdebug & (LEVEL)) == (LEVEL)) { \
213 #define PAGER_DEBUG(LEVEL, A)
218 shared_region_pager_bootstrap(void)
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
);
228 * shared_region_pager_init()
230 * Initialize the memory object and makes it ready to be used and mapped.
233 shared_region_pager_init(
234 memory_object_t mem_obj
,
235 memory_object_control_t control
,
239 memory_object_cluster_size_t pg_size
)
241 shared_region_pager_t pager
;
243 memory_object_attr_info_data_t attributes
;
245 PAGER_DEBUG(PAGER_ALL
,
246 ("shared_region_pager_init: %p, %p, %x\n",
247 mem_obj
, control
, pg_size
));
249 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
250 return KERN_INVALID_ARGUMENT
;
253 pager
= shared_region_pager_lookup(mem_obj
);
255 memory_object_control_reference(control
);
257 pager
->sc_pgr_hdr
.mo_control
= control
;
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
;
265 kr
= memory_object_change_attributes(
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");
275 #if CONFIG_SECLUDED_MEMORY
276 if (secluded_for_filecache
) {
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...
284 memory_object_mark_eligible_for_secluded(control
, TRUE
);
287 #endif /* CONFIG_SECLUDED_MEMORY */
293 * shared_region_data_return()
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
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
)
311 panic("shared_region_pager_data_return: should never get called");
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
)
321 panic("shared_region_pager_data_initialize: should never get called");
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
)
336 * shared_region_pager_data_request()
338 * Handles page-in requests from VM.
340 int shared_region_pager_data_request_debug
= 0;
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
,
349 vm_prot_t protection_required
,
350 memory_object_fault_info_t mo_fault_info
)
352 shared_region_pager_t pager
;
353 memory_object_control_t mo_control
;
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
;
366 vm_page_t src_page
, top_page
;
368 struct vm_object_fault_info fault_info
;
369 mach_vm_offset_t slide_start_address
;
371 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_data_request: %p, %llx, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
373 retval
= KERN_SUCCESS
;
374 src_top_object
= VM_OBJECT_NULL
;
375 src_page_object
= VM_OBJECT_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
;
385 pager
= shared_region_pager_lookup(mem_obj
);
386 assert(pager
->is_ready
);
387 assert(pager
->ref_count
> 1); /* pager is alive and mapped */
389 PAGER_DEBUG(PAGER_PAGEIN
, ("shared_region_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
392 * Gather in a UPL all the VM pages requested by VM.
394 mo_control
= pager
->sc_pgr_hdr
.mo_control
;
398 UPL_RET_ONLY_ABSENT
|
401 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
404 kr
= memory_object_upl_request(mo_control
,
406 &upl
, NULL
, NULL
, upl_flags
, VM_KERN_MEMORY_SECURITY
);
407 if (kr
!= KERN_SUCCESS
) {
411 dst_object
= mo_control
->moc_object
;
412 assert(dst_object
!= VM_OBJECT_NULL
);
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
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 */
423 slide_start_address
= pager
->scp_slide_info
->slid_address
;
425 fault_info
.lo_offset
+= pager
->backing_offset
;
426 fault_info
.hi_offset
+= pager
->backing_offset
;
429 * Fill in the contents of the pages requested by VM.
431 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
432 pl_count
= length
/ PAGE_SIZE
;
434 retval
== KERN_SUCCESS
&& cur_offset
< length
;
435 cur_offset
+= PAGE_SIZE
) {
438 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
439 /* this page is not in the UPL: skip it */
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.
449 vm_object_lock(src_top_object
);
450 vm_object_paging_begin(src_top_object
);
453 src_page
= VM_PAGE_NULL
;
454 kr
= vm_fault_page(src_top_object
,
455 pager
->backing_offset
+ offset
+ cur_offset
,
458 FALSE
, /* src_page not looked up */
468 case VM_FAULT_SUCCESS
:
471 goto retry_src_fault
;
472 case VM_FAULT_MEMORY_SHORTAGE
:
473 if (vm_page_wait(interruptible
)) {
474 goto retry_src_fault
;
477 case VM_FAULT_INTERRUPTED
:
478 retval
= MACH_SEND_INTERRUPTED
;
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
);
485 case VM_FAULT_MEMORY_ERROR
:
486 /* the page is not there ! */
490 retval
= KERN_MEMORY_ERROR
;
494 panic("shared_region_pager_data_request: "
495 "vm_fault_page() unexpected error 0x%x\n",
498 assert(src_page
!= VM_PAGE_NULL
);
499 assert(src_page
->vmp_busy
);
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
);
506 vm_page_unlock_queues();
510 * Establish pointers to the source
511 * and destination physical pages.
514 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
515 assert(dst_pnum
!= 0);
517 src_vaddr
= (vm_map_offset_t
)
518 PHYSMAP_PTOV((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
520 dst_vaddr
= (vm_map_offset_t
)
521 PHYSMAP_PTOV((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
523 #elif __arm__ || __arm64__
524 src_vaddr
= (vm_map_offset_t
)
525 phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
527 dst_vaddr
= (vm_map_offset_t
)
528 phystokv((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
530 #error "vm_paging_map_object: no 1-to-1 kernel mapping of physical memory..."
534 src_page_object
= VM_PAGE_OBJECT(src_page
);
537 * Validate the original page...
539 if (src_page_object
->code_signed
) {
540 vm_page_validate_cs_mapped(
542 (const void *) src_vaddr
);
545 * ... and transfer the results to the destination page.
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
);
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.
562 assert(src_page
->vmp_busy
);
563 assert(src_page_object
->paging_in_progress
> 0);
564 vm_object_unlock(src_page_object
);
567 * Process the original contents of the source page
568 * into the destination page.
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
;
577 chunk_offset
= offset
+ cur_offset
+ offset_in_page
;
579 bcopy((const char *)(src_vaddr
+
581 (char *)(dst_vaddr
+ offset_in_page
),
582 PAGE_SIZE_FOR_SR_SLIDE
);
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
++;
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] "
617 (uint64_t) cur_offset
,
618 (uint64_t) offset_in_page
,
620 pager
->scp_slide_info
->start
,
621 pager
->scp_slide_info
->end
,
622 (pager
->backing_offset
+
626 pager
->backing_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
,
638 if (kr
!= KERN_SUCCESS
) {
639 shared_region_pager_slid_error
++;
642 shared_region_pager_slid
++;
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
);
651 * Cleanup the result of vm_fault_page() of the source page.
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
);
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
);
669 /* clean up the UPL */
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".
678 upl_clear_dirty(upl
, TRUE
);
680 /* abort or commit the UPL */
681 if (retval
!= KERN_SUCCESS
) {
685 upl_commit_range(upl
, 0, upl
->size
,
686 UPL_COMMIT_CS_VALIDATED
| UPL_COMMIT_WRITTEN_BY_KERNEL
,
687 upl_pl
, pl_count
, &empty
);
690 /* and deallocate the UPL */
694 if (src_top_object
!= VM_OBJECT_NULL
) {
695 vm_object_deallocate(src_top_object
);
701 * shared_region_pager_reference()
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.
708 shared_region_pager_reference(
709 memory_object_t mem_obj
)
711 shared_region_pager_t pager
;
713 pager
= shared_region_pager_lookup(mem_obj
);
715 lck_mtx_lock(&shared_region_pager_lock
);
716 assert(pager
->ref_count
> 0);
718 lck_mtx_unlock(&shared_region_pager_lock
);
723 * shared_region_pager_dequeue:
725 * Removes a pager from the list of pagers.
727 * The caller must hold "shared_region_pager_lock".
730 shared_region_pager_dequeue(
731 shared_region_pager_t pager
)
733 assert(!pager
->is_mapped
);
735 queue_remove(&shared_region_pager_queue
,
737 shared_region_pager_t
,
739 pager
->pager_queue
.next
= NULL
;
740 pager
->pager_queue
.prev
= NULL
;
742 shared_region_pager_count
--;
746 * shared_region_pager_terminate_internal:
748 * Trigger the asynchronous termination of the memory object associated
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.
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.
759 shared_region_pager_terminate_internal(
760 shared_region_pager_t pager
)
762 assert(pager
->is_ready
);
763 assert(!pager
->is_mapped
);
765 if (pager
->backing_object
!= VM_OBJECT_NULL
) {
766 vm_object_deallocate(pager
->backing_object
);
767 pager
->backing_object
= VM_OBJECT_NULL
;
769 /* trigger the destruction of the memory object */
770 memory_object_destroy(pager
->sc_pgr_hdr
.mo_control
, 0);
774 * shared_region_pager_deallocate_internal()
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
782 shared_region_pager_deallocate_internal(
783 shared_region_pager_t pager
,
786 boolean_t needs_trimming
;
790 lck_mtx_lock(&shared_region_pager_lock
);
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
;
799 needs_trimming
= FALSE
;
802 /* drop a reference on this pager */
805 if (pager
->ref_count
== 1) {
807 * Only the "named" reference is left, which means that
808 * no one is really holding on to this pager anymore.
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) {
817 * Dropped the existence reference; the memory object has
818 * been terminated. Do some final cleanup and release the
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
;
826 kfree(pager
, sizeof(*pager
));
827 pager
= SHARED_REGION_PAGER_NULL
;
829 /* there are still plenty of references: keep going... */
830 lck_mtx_unlock(&shared_region_pager_lock
);
833 if (needs_trimming
) {
834 shared_region_pager_trim();
836 /* caution: lock is not held on return... */
840 * shared_region_pager_deallocate()
842 * Release a reference on this pager and free it when the last
843 * reference goes away.
846 shared_region_pager_deallocate(
847 memory_object_t mem_obj
)
849 shared_region_pager_t pager
;
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
);
860 shared_region_pager_terminate(
864 memory_object_t mem_obj
)
866 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_terminate: %p\n", mem_obj
));
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
)
881 panic("shared_region_pager_synchronize: memory_object_synchronize no longer supported\n");
886 * shared_region_pager_map()
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.
894 shared_region_pager_map(
895 memory_object_t mem_obj
,
896 __unused vm_prot_t prot
)
898 shared_region_pager_t pager
;
900 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_map: %p\n", mem_obj
));
902 pager
= shared_region_pager_lookup(mem_obj
);
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
) {
909 * First mapping of this pager: take an extra reference
910 * that will remain until all the mappings of this pager
913 pager
->is_mapped
= TRUE
;
915 shared_region_pager_count_mapped
++;
917 lck_mtx_unlock(&shared_region_pager_lock
);
923 * shared_region_pager_last_unmap()
925 * This is called by VM when this memory object is no longer mapped anywhere.
928 shared_region_pager_last_unmap(
929 memory_object_t mem_obj
)
931 shared_region_pager_t pager
;
934 PAGER_DEBUG(PAGER_ALL
,
935 ("shared_region_pager_last_unmap: %p\n", mem_obj
));
937 pager
= shared_region_pager_lookup(mem_obj
);
939 lck_mtx_lock(&shared_region_pager_lock
);
940 if (pager
->is_mapped
) {
942 * All the mappings are gone, so let go of the one extra
943 * reference that represents all the mappings of this pager.
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
;
951 pager
->is_mapped
= FALSE
;
952 shared_region_pager_deallocate_internal(pager
, TRUE
);
953 /* caution: deallocate_internal() released the lock ! */
955 lck_mtx_unlock(&shared_region_pager_lock
);
965 shared_region_pager_t
966 shared_region_pager_lookup(
967 memory_object_t mem_obj
)
969 shared_region_pager_t pager
;
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);
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
)
983 shared_region_pager_t pager
;
984 memory_object_control_t control
;
987 pager
= (shared_region_pager_t
) kalloc(sizeof(*pager
));
988 if (pager
== SHARED_REGION_PAGER_NULL
) {
989 return SHARED_REGION_PAGER_NULL
;
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.
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
;
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
;
1011 vm_object_reference(backing_object
);
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
,
1017 shared_region_pager_t
,
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
;
1023 lck_mtx_unlock(&shared_region_pager_lock
);
1025 kr
= memory_object_create_named((memory_object_t
) pager
,
1028 assert(kr
== KERN_SUCCESS
);
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
);
1035 /* wakeup anyone waiting for this pager to be ready */
1036 thread_wakeup(&pager
->is_ready
);
1042 * shared_region_pager_setup()
1044 * Provide the caller with a memory object backed by the provided
1045 * "backing_object" VM object.
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
)
1053 shared_region_pager_t pager
;
1055 /* create new pager */
1056 pager
= shared_region_pager_create(
1060 if (pager
== SHARED_REGION_PAGER_NULL
) {
1061 /* could not create a new pager */
1062 return MEMORY_OBJECT_NULL
;
1065 lck_mtx_lock(&shared_region_pager_lock
);
1066 while (!pager
->is_ready
) {
1067 lck_mtx_sleep(&shared_region_pager_lock
,
1072 lck_mtx_unlock(&shared_region_pager_lock
);
1074 return (memory_object_t
) pager
;
1078 shared_region_pager_trim(void)
1080 shared_region_pager_t pager
, prev_pager
;
1081 queue_head_t trim_queue
;
1085 lck_mtx_lock(&shared_region_pager_lock
);
1088 * We have too many pagers, try and trim some unused ones,
1089 * starting with the oldest pager at the end of the queue.
1091 queue_init(&trim_queue
);
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
);
1103 if (pager
->ref_count
== 2 &&
1105 !pager
->is_mapped
) {
1106 /* this pager can be trimmed */
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
,
1113 shared_region_pager_t
,
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 */
1124 if (num_trim
> shared_region_pager_num_trim_max
) {
1125 shared_region_pager_num_trim_max
= num_trim
;
1127 shared_region_pager_num_trim_total
+= num_trim
;
1129 lck_mtx_unlock(&shared_region_pager_lock
);
1131 /* terminate the trimmed pagers */
1132 while (!queue_empty(&trim_queue
)) {
1133 queue_remove_first(&trim_queue
,
1135 shared_region_pager_t
,
1137 pager
->pager_queue
.next
= NULL
;
1138 pager
->pager_queue
.prev
= NULL
;
1139 assert(pager
->ref_count
== 2);
1141 * We can't call deallocate_internal() because the pager
1142 * has already been dequeued, but we still need to remove
1146 shared_region_pager_terminate_internal(pager
);