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
;
252 pager
= shared_region_pager_lookup(mem_obj
);
254 memory_object_control_reference(control
);
256 pager
->sc_pgr_hdr
.mo_control
= control
;
258 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
259 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
260 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
261 attributes
.may_cache_object
= FALSE
;
262 attributes
.temporary
= TRUE
;
264 kr
= memory_object_change_attributes(
266 MEMORY_OBJECT_ATTRIBUTE_INFO
,
267 (memory_object_info_t
) &attributes
,
268 MEMORY_OBJECT_ATTR_INFO_COUNT
);
269 if (kr
!= KERN_SUCCESS
)
270 panic("shared_region_pager_init: "
271 "memory_object_change_attributes() failed");
273 #if CONFIG_SECLUDED_MEMORY
274 if (secluded_for_filecache
) {
277 * XXX FBDP do we want this in the secluded pool?
278 * Ideally, we'd want the shared region used by Camera to
279 * NOT be in the secluded pool, but all other shared regions
280 * in the secluded pool...
282 memory_object_mark_eligible_for_secluded(control
, TRUE
);
285 #endif /* CONFIG_SECLUDED_MEMORY */
291 * shared_region_data_return()
293 * Handles page-out requests from VM. This should never happen since
294 * the pages provided by this EMM are not supposed to be dirty or dirtied
295 * and VM should simply discard the contents and reclaim the pages if it
299 shared_region_pager_data_return(
300 __unused memory_object_t mem_obj
,
301 __unused memory_object_offset_t offset
,
302 __unused memory_object_cluster_size_t data_cnt
,
303 __unused memory_object_offset_t
*resid_offset
,
304 __unused
int *io_error
,
305 __unused boolean_t dirty
,
306 __unused boolean_t kernel_copy
,
307 __unused
int upl_flags
)
309 panic("shared_region_pager_data_return: should never get called");
314 shared_region_pager_data_initialize(
315 __unused memory_object_t mem_obj
,
316 __unused memory_object_offset_t offset
,
317 __unused memory_object_cluster_size_t data_cnt
)
319 panic("shared_region_pager_data_initialize: should never get called");
324 shared_region_pager_data_unlock(
325 __unused memory_object_t mem_obj
,
326 __unused memory_object_offset_t offset
,
327 __unused memory_object_size_t size
,
328 __unused vm_prot_t desired_access
)
334 * shared_region_pager_data_request()
336 * Handles page-in requests from VM.
338 int shared_region_pager_data_request_debug
= 0;
340 shared_region_pager_data_request(
341 memory_object_t mem_obj
,
342 memory_object_offset_t offset
,
343 memory_object_cluster_size_t length
,
347 vm_prot_t protection_required
,
348 memory_object_fault_info_t mo_fault_info
)
350 shared_region_pager_t pager
;
351 memory_object_control_t mo_control
;
355 upl_page_info_t
*upl_pl
;
356 unsigned int pl_count
;
357 vm_object_t src_top_object
, src_page_object
, dst_object
;
358 kern_return_t kr
, retval
;
359 vm_offset_t src_vaddr
, dst_vaddr
;
360 vm_offset_t cur_offset
;
361 vm_offset_t offset_in_page
;
362 kern_return_t error_code
;
364 vm_page_t src_page
, top_page
;
366 struct vm_object_fault_info fault_info
;
367 mach_vm_offset_t slide_start_address
;
369 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_data_request: %p, %llx, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
371 retval
= KERN_SUCCESS
;
372 src_top_object
= VM_OBJECT_NULL
;
373 src_page_object
= VM_OBJECT_NULL
;
376 fault_info
= *((struct vm_object_fault_info
*)(uintptr_t)mo_fault_info
);
377 fault_info
.stealth
= TRUE
;
378 fault_info
.io_sync
= FALSE
;
379 fault_info
.mark_zf_absent
= FALSE
;
380 fault_info
.batch_pmap_op
= FALSE
;
381 interruptible
= fault_info
.interruptible
;
383 pager
= shared_region_pager_lookup(mem_obj
);
384 assert(pager
->is_ready
);
385 assert(pager
->ref_count
> 1); /* pager is alive and mapped */
387 PAGER_DEBUG(PAGER_PAGEIN
, ("shared_region_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
390 * Gather in a UPL all the VM pages requested by VM.
392 mo_control
= pager
->sc_pgr_hdr
.mo_control
;
396 UPL_RET_ONLY_ABSENT
|
399 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
402 kr
= memory_object_upl_request(mo_control
,
404 &upl
, NULL
, NULL
, upl_flags
, VM_KERN_MEMORY_SECURITY
);
405 if (kr
!= KERN_SUCCESS
) {
409 dst_object
= mo_control
->moc_object
;
410 assert(dst_object
!= VM_OBJECT_NULL
);
413 * We'll map the original data in the kernel address space from the
414 * backing VM object (itself backed by the shared cache file via
417 src_top_object
= pager
->backing_object
;
418 assert(src_top_object
!= VM_OBJECT_NULL
);
419 vm_object_reference(src_top_object
); /* keep the source object alive */
421 slide_start_address
= pager
->scp_slide_info
->slid_address
;
423 fault_info
.lo_offset
+= pager
->backing_offset
;
424 fault_info
.hi_offset
+= pager
->backing_offset
;
427 * Fill in the contents of the pages requested by VM.
429 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
430 pl_count
= length
/ PAGE_SIZE
;
432 retval
== KERN_SUCCESS
&& cur_offset
< length
;
433 cur_offset
+= PAGE_SIZE
) {
436 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
437 /* this page is not in the UPL: skip it */
442 * Map the source (dyld shared cache) page in the kernel's
443 * virtual address space.
444 * We already hold a reference on the src_top_object.
447 vm_object_lock(src_top_object
);
448 vm_object_paging_begin(src_top_object
);
451 src_page
= VM_PAGE_NULL
;
452 kr
= vm_fault_page(src_top_object
,
453 pager
->backing_offset
+ offset
+ cur_offset
,
456 FALSE
, /* src_page not looked up */
466 case VM_FAULT_SUCCESS
:
469 goto retry_src_fault
;
470 case VM_FAULT_MEMORY_SHORTAGE
:
471 if (vm_page_wait(interruptible
)) {
472 goto retry_src_fault
;
475 case VM_FAULT_INTERRUPTED
:
476 retval
= MACH_SEND_INTERRUPTED
;
478 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
479 /* success but no VM page: fail */
480 vm_object_paging_end(src_top_object
);
481 vm_object_unlock(src_top_object
);
483 case VM_FAULT_MEMORY_ERROR
:
484 /* the page is not there ! */
488 retval
= KERN_MEMORY_ERROR
;
492 panic("shared_region_pager_data_request: "
493 "vm_fault_page() unexpected error 0x%x\n",
496 assert(src_page
!= VM_PAGE_NULL
);
497 assert(src_page
->vmp_busy
);
499 if (src_page
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
500 vm_page_lockspin_queues();
501 if (src_page
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
502 vm_page_speculate(src_page
, FALSE
);
504 vm_page_unlock_queues();
508 * Establish pointers to the source
509 * and destination physical pages.
512 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
513 assert(dst_pnum
!= 0);
515 src_vaddr
= (vm_map_offset_t
)
516 PHYSMAP_PTOV((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
518 dst_vaddr
= (vm_map_offset_t
)
519 PHYSMAP_PTOV((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
521 #elif __arm__ || __arm64__
522 src_vaddr
= (vm_map_offset_t
)
523 phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
525 dst_vaddr
= (vm_map_offset_t
)
526 phystokv((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
528 #error "vm_paging_map_object: no 1-to-1 kernel mapping of physical memory..."
532 src_page_object
= VM_PAGE_OBJECT(src_page
);
535 * Validate the original page...
537 if (src_page_object
->code_signed
) {
538 vm_page_validate_cs_mapped(
540 (const void *) src_vaddr
);
543 * ... and transfer the results to the destination page.
545 UPL_SET_CS_VALIDATED(upl_pl
, cur_offset
/ PAGE_SIZE
,
546 src_page
->vmp_cs_validated
);
547 UPL_SET_CS_TAINTED(upl_pl
, cur_offset
/ PAGE_SIZE
,
548 src_page
->vmp_cs_tainted
);
549 UPL_SET_CS_NX(upl_pl
, cur_offset
/ PAGE_SIZE
,
550 src_page
->vmp_cs_nx
);
553 * The page provider might access a mapped file, so let's
554 * release the object lock for the source page to avoid a
555 * potential deadlock.
556 * The source page is kept busy and we have a
557 * "paging_in_progress" reference on its object, so it's safe
558 * to unlock the object here.
560 assert(src_page
->vmp_busy
);
561 assert(src_page_object
->paging_in_progress
> 0);
562 vm_object_unlock(src_page_object
);
565 * Process the original contents of the source page
566 * into the destination page.
568 for (offset_in_page
= 0;
569 offset_in_page
< PAGE_SIZE
;
570 offset_in_page
+= PAGE_SIZE_FOR_SR_SLIDE
) {
571 vm_object_offset_t chunk_offset
;
572 vm_object_offset_t offset_in_backing_object
;
573 vm_object_offset_t offset_in_sliding_range
;
575 chunk_offset
= offset
+ cur_offset
+ offset_in_page
;
577 bcopy((const char *)(src_vaddr
+
579 (char *)(dst_vaddr
+ offset_in_page
),
580 PAGE_SIZE_FOR_SR_SLIDE
);
582 offset_in_backing_object
= (chunk_offset
+
583 pager
->backing_offset
);
584 if ((offset_in_backing_object
< pager
->scp_slide_info
->start
) ||
585 (offset_in_backing_object
>= pager
->scp_slide_info
->end
)) {
586 /* chunk is outside of sliding range: done */
587 shared_region_pager_copied
++;
591 offset_in_sliding_range
=
592 (offset_in_backing_object
-
593 pager
->scp_slide_info
->start
);
594 kr
= vm_shared_region_slide_page(
595 pager
->scp_slide_info
,
596 dst_vaddr
+ offset_in_page
,
597 (mach_vm_offset_t
) (offset_in_sliding_range
+
598 slide_start_address
),
599 (uint32_t) (offset_in_sliding_range
/
600 PAGE_SIZE_FOR_SR_SLIDE
));
601 if (shared_region_pager_data_request_debug
) {
602 printf("shared_region_data_request"
603 "(%p,0x%llx+0x%llx+0x%04llx): 0x%llx "
604 "in sliding range [0x%llx:0x%llx]: "
605 "SLIDE offset 0x%llx="
606 "(0x%llx+0x%llx+0x%llx+0x%04llx)"
607 "[0x%016llx 0x%016llx] "
615 (uint64_t) cur_offset
,
616 (uint64_t) offset_in_page
,
618 pager
->scp_slide_info
->start
,
619 pager
->scp_slide_info
->end
,
620 (pager
->backing_offset
+
624 pager
->backing_offset
,
626 (uint64_t) cur_offset
,
627 (uint64_t) offset_in_page
,
628 *(uint64_t *)(dst_vaddr
+offset_in_page
),
629 *(uint64_t *)(dst_vaddr
+offset_in_page
+8),
630 src_page_object
->code_signed
,
631 src_page
->vmp_cs_validated
,
632 src_page
->vmp_cs_tainted
,
636 if (kr
!= KERN_SUCCESS
) {
637 shared_region_pager_slid_error
++;
640 shared_region_pager_slid
++;
643 assert(VM_PAGE_OBJECT(src_page
) == src_page_object
);
644 assert(src_page
->vmp_busy
);
645 assert(src_page_object
->paging_in_progress
> 0);
646 vm_object_lock(src_page_object
);
649 * Cleanup the result of vm_fault_page() of the source page.
651 PAGE_WAKEUP_DONE(src_page
);
652 src_page
= VM_PAGE_NULL
;
653 vm_object_paging_end(src_page_object
);
654 vm_object_unlock(src_page_object
);
656 if (top_page
!= VM_PAGE_NULL
) {
657 assert(VM_PAGE_OBJECT(top_page
) == src_top_object
);
658 vm_object_lock(src_top_object
);
659 VM_PAGE_FREE(top_page
);
660 vm_object_paging_end(src_top_object
);
661 vm_object_unlock(src_top_object
);
667 /* clean up the UPL */
670 * The pages are currently dirty because we've just been
671 * writing on them, but as far as we're concerned, they're
672 * clean since they contain their "original" contents as
673 * provided by us, the pager.
674 * Tell the UPL to mark them "clean".
676 upl_clear_dirty(upl
, TRUE
);
678 /* abort or commit the UPL */
679 if (retval
!= KERN_SUCCESS
) {
683 upl_commit_range(upl
, 0, upl
->size
,
684 UPL_COMMIT_CS_VALIDATED
| UPL_COMMIT_WRITTEN_BY_KERNEL
,
685 upl_pl
, pl_count
, &empty
);
688 /* and deallocate the UPL */
692 if (src_top_object
!= VM_OBJECT_NULL
) {
693 vm_object_deallocate(src_top_object
);
699 * shared_region_pager_reference()
701 * Get a reference on this memory object.
702 * For external usage only. Assumes that the initial reference count is not 0,
703 * i.e one should not "revive" a dead pager this way.
706 shared_region_pager_reference(
707 memory_object_t mem_obj
)
709 shared_region_pager_t pager
;
711 pager
= shared_region_pager_lookup(mem_obj
);
713 lck_mtx_lock(&shared_region_pager_lock
);
714 assert(pager
->ref_count
> 0);
716 lck_mtx_unlock(&shared_region_pager_lock
);
721 * shared_region_pager_dequeue:
723 * Removes a pager from the list of pagers.
725 * The caller must hold "shared_region_pager_lock".
728 shared_region_pager_dequeue(
729 shared_region_pager_t pager
)
731 assert(!pager
->is_mapped
);
733 queue_remove(&shared_region_pager_queue
,
735 shared_region_pager_t
,
737 pager
->pager_queue
.next
= NULL
;
738 pager
->pager_queue
.prev
= NULL
;
740 shared_region_pager_count
--;
744 * shared_region_pager_terminate_internal:
746 * Trigger the asynchronous termination of the memory object associated
748 * When the memory object is terminated, there will be one more call
749 * to memory_object_deallocate() (i.e. shared_region_pager_deallocate())
750 * to finish the clean up.
752 * "shared_region_pager_lock" should not be held by the caller.
753 * We don't need the lock because the pager has already been removed from
754 * the pagers' list and is now ours exclusively.
757 shared_region_pager_terminate_internal(
758 shared_region_pager_t pager
)
760 assert(pager
->is_ready
);
761 assert(!pager
->is_mapped
);
763 if (pager
->backing_object
!= VM_OBJECT_NULL
) {
764 vm_object_deallocate(pager
->backing_object
);
765 pager
->backing_object
= VM_OBJECT_NULL
;
767 /* trigger the destruction of the memory object */
768 memory_object_destroy(pager
->sc_pgr_hdr
.mo_control
, 0);
772 * shared_region_pager_deallocate_internal()
774 * Release a reference on this pager and free it when the last
775 * reference goes away.
776 * Can be called with shared_region_pager_lock held or not but always returns
780 shared_region_pager_deallocate_internal(
781 shared_region_pager_t pager
,
784 boolean_t needs_trimming
;
788 lck_mtx_lock(&shared_region_pager_lock
);
791 count_unmapped
= (shared_region_pager_count
-
792 shared_region_pager_count_mapped
);
793 if (count_unmapped
> shared_region_pager_cache_limit
) {
794 /* we have too many unmapped pagers: trim some */
795 needs_trimming
= TRUE
;
797 needs_trimming
= FALSE
;
800 /* drop a reference on this pager */
803 if (pager
->ref_count
== 1) {
805 * Only the "named" reference is left, which means that
806 * no one is really holding on to this pager anymore.
809 shared_region_pager_dequeue(pager
);
810 /* the pager is all ours: no need for the lock now */
811 lck_mtx_unlock(&shared_region_pager_lock
);
812 shared_region_pager_terminate_internal(pager
);
813 } else if (pager
->ref_count
== 0) {
815 * Dropped the existence reference; the memory object has
816 * been terminated. Do some final cleanup and release the
819 lck_mtx_unlock(&shared_region_pager_lock
);
820 if (pager
->sc_pgr_hdr
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
821 memory_object_control_deallocate(pager
->sc_pgr_hdr
.mo_control
);
822 pager
->sc_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
824 kfree(pager
, sizeof (*pager
));
825 pager
= SHARED_REGION_PAGER_NULL
;
827 /* there are still plenty of references: keep going... */
828 lck_mtx_unlock(&shared_region_pager_lock
);
831 if (needs_trimming
) {
832 shared_region_pager_trim();
834 /* caution: lock is not held on return... */
838 * shared_region_pager_deallocate()
840 * Release a reference on this pager and free it when the last
841 * reference goes away.
844 shared_region_pager_deallocate(
845 memory_object_t mem_obj
)
847 shared_region_pager_t pager
;
849 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_deallocate: %p\n", mem_obj
));
850 pager
= shared_region_pager_lookup(mem_obj
);
851 shared_region_pager_deallocate_internal(pager
, FALSE
);
858 shared_region_pager_terminate(
862 memory_object_t mem_obj
)
864 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_terminate: %p\n", mem_obj
));
873 shared_region_pager_synchronize(
874 __unused memory_object_t mem_obj
,
875 __unused memory_object_offset_t offset
,
876 __unused memory_object_size_t length
,
877 __unused vm_sync_t sync_flags
)
879 panic("shared_region_pager_synchronize: memory_object_synchronize no longer supported\n");
884 * shared_region_pager_map()
886 * This allows VM to let us, the EMM, know that this memory object
887 * is currently mapped one or more times. This is called by VM each time
888 * the memory object gets mapped and we take one extra reference on the
889 * memory object to account for all its mappings.
892 shared_region_pager_map(
893 memory_object_t mem_obj
,
894 __unused vm_prot_t prot
)
896 shared_region_pager_t pager
;
898 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_map: %p\n", mem_obj
));
900 pager
= shared_region_pager_lookup(mem_obj
);
902 lck_mtx_lock(&shared_region_pager_lock
);
903 assert(pager
->is_ready
);
904 assert(pager
->ref_count
> 0); /* pager is alive */
905 if (pager
->is_mapped
== FALSE
) {
907 * First mapping of this pager: take an extra reference
908 * that will remain until all the mappings of this pager
911 pager
->is_mapped
= TRUE
;
913 shared_region_pager_count_mapped
++;
915 lck_mtx_unlock(&shared_region_pager_lock
);
921 * shared_region_pager_last_unmap()
923 * This is called by VM when this memory object is no longer mapped anywhere.
926 shared_region_pager_last_unmap(
927 memory_object_t mem_obj
)
929 shared_region_pager_t pager
;
932 PAGER_DEBUG(PAGER_ALL
,
933 ("shared_region_pager_last_unmap: %p\n", mem_obj
));
935 pager
= shared_region_pager_lookup(mem_obj
);
937 lck_mtx_lock(&shared_region_pager_lock
);
938 if (pager
->is_mapped
) {
940 * All the mappings are gone, so let go of the one extra
941 * reference that represents all the mappings of this pager.
943 shared_region_pager_count_mapped
--;
944 count_unmapped
= (shared_region_pager_count
-
945 shared_region_pager_count_mapped
);
946 if (count_unmapped
> shared_region_pager_count_unmapped_max
) {
947 shared_region_pager_count_unmapped_max
= count_unmapped
;
949 pager
->is_mapped
= FALSE
;
950 shared_region_pager_deallocate_internal(pager
, TRUE
);
951 /* caution: deallocate_internal() released the lock ! */
953 lck_mtx_unlock(&shared_region_pager_lock
);
963 shared_region_pager_t
964 shared_region_pager_lookup(
965 memory_object_t mem_obj
)
967 shared_region_pager_t pager
;
969 assert(mem_obj
->mo_pager_ops
== &shared_region_pager_ops
);
970 pager
= (shared_region_pager_t
)(uintptr_t) mem_obj
;
971 assert(pager
->ref_count
> 0);
975 shared_region_pager_t
976 shared_region_pager_create(
977 vm_object_t backing_object
,
978 vm_object_offset_t backing_offset
,
979 struct vm_shared_region_slide_info
*slide_info
)
981 shared_region_pager_t pager
;
982 memory_object_control_t control
;
985 pager
= (shared_region_pager_t
) kalloc(sizeof (*pager
));
986 if (pager
== SHARED_REGION_PAGER_NULL
) {
987 return SHARED_REGION_PAGER_NULL
;
991 * The vm_map call takes both named entry ports and raw memory
992 * objects in the same parameter. We need to make sure that
993 * vm_map does not see this object as a named entry port. So,
994 * we reserve the first word in the object for a fake ip_kotype
995 * setting - that will tell vm_map to use it as a memory object.
997 pager
->sc_pgr_hdr
.mo_ikot
= IKOT_MEMORY_OBJECT
;
998 pager
->sc_pgr_hdr
.mo_pager_ops
= &shared_region_pager_ops
;
999 pager
->sc_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
1001 pager
->is_ready
= FALSE
;/* not ready until it has a "name" */
1002 pager
->ref_count
= 1; /* existence reference (for the cache) */
1003 pager
->ref_count
++; /* for the caller */
1004 pager
->is_mapped
= FALSE
;
1005 pager
->backing_object
= backing_object
;
1006 pager
->backing_offset
= backing_offset
;
1007 pager
->scp_slide_info
= slide_info
;
1009 vm_object_reference(backing_object
);
1011 lck_mtx_lock(&shared_region_pager_lock
);
1012 /* enter new pager at the head of our list of pagers */
1013 queue_enter_first(&shared_region_pager_queue
,
1015 shared_region_pager_t
,
1017 shared_region_pager_count
++;
1018 if (shared_region_pager_count
> shared_region_pager_count_max
) {
1019 shared_region_pager_count_max
= shared_region_pager_count
;
1021 lck_mtx_unlock(&shared_region_pager_lock
);
1023 kr
= memory_object_create_named((memory_object_t
) pager
,
1026 assert(kr
== KERN_SUCCESS
);
1028 lck_mtx_lock(&shared_region_pager_lock
);
1029 /* the new pager is now ready to be used */
1030 pager
->is_ready
= TRUE
;
1031 lck_mtx_unlock(&shared_region_pager_lock
);
1033 /* wakeup anyone waiting for this pager to be ready */
1034 thread_wakeup(&pager
->is_ready
);
1040 * shared_region_pager_setup()
1042 * Provide the caller with a memory object backed by the provided
1043 * "backing_object" VM object.
1046 shared_region_pager_setup(
1047 vm_object_t backing_object
,
1048 vm_object_offset_t backing_offset
,
1049 struct vm_shared_region_slide_info
*slide_info
)
1051 shared_region_pager_t pager
;
1053 /* create new pager */
1054 pager
= shared_region_pager_create(
1058 if (pager
== SHARED_REGION_PAGER_NULL
) {
1059 /* could not create a new pager */
1060 return MEMORY_OBJECT_NULL
;
1063 lck_mtx_lock(&shared_region_pager_lock
);
1064 while (!pager
->is_ready
) {
1065 lck_mtx_sleep(&shared_region_pager_lock
,
1070 lck_mtx_unlock(&shared_region_pager_lock
);
1072 return (memory_object_t
) pager
;
1076 shared_region_pager_trim(void)
1078 shared_region_pager_t pager
, prev_pager
;
1079 queue_head_t trim_queue
;
1083 lck_mtx_lock(&shared_region_pager_lock
);
1086 * We have too many pagers, try and trim some unused ones,
1087 * starting with the oldest pager at the end of the queue.
1089 queue_init(&trim_queue
);
1092 for (pager
= (shared_region_pager_t
)
1093 queue_last(&shared_region_pager_queue
);
1094 !queue_end(&shared_region_pager_queue
,
1095 (queue_entry_t
) pager
);
1096 pager
= prev_pager
) {
1097 /* get prev elt before we dequeue */
1098 prev_pager
= (shared_region_pager_t
)
1099 queue_prev(&pager
->pager_queue
);
1101 if (pager
->ref_count
== 2 &&
1103 !pager
->is_mapped
) {
1104 /* this pager can be trimmed */
1106 /* remove this pager from the main list ... */
1107 shared_region_pager_dequeue(pager
);
1108 /* ... and add it to our trim queue */
1109 queue_enter_first(&trim_queue
,
1111 shared_region_pager_t
,
1114 count_unmapped
= (shared_region_pager_count
-
1115 shared_region_pager_count_mapped
);
1116 if (count_unmapped
<= shared_region_pager_cache_limit
) {
1117 /* we have enough pagers to trim */
1122 if (num_trim
> shared_region_pager_num_trim_max
) {
1123 shared_region_pager_num_trim_max
= num_trim
;
1125 shared_region_pager_num_trim_total
+= num_trim
;
1127 lck_mtx_unlock(&shared_region_pager_lock
);
1129 /* terminate the trimmed pagers */
1130 while (!queue_empty(&trim_queue
)) {
1131 queue_remove_first(&trim_queue
,
1133 shared_region_pager_t
,
1135 pager
->pager_queue
.next
= NULL
;
1136 pager
->pager_queue
.prev
= NULL
;
1137 assert(pager
->ref_count
== 2);
1139 * We can't call deallocate_internal() because the pager
1140 * has already been dequeued, but we still need to remove
1144 shared_region_pager_terminate_internal(pager
);