2 * Copyright (c) 2018-2020 Apple 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_protos.h>
58 #include <vm/vm_shared_region.h>
60 #if __has_feature(ptrauth_calls)
62 extern boolean_t diversify_user_jop
;
63 #endif /* __has_feature(ptrauth_calls) */
66 * SHARED REGION MEMORY PAGER
68 * This external memory manager (EMM) handles mappings of a dyld shared cache
69 * in shared regions, applying any necessary modifications (sliding,
70 * pointer signing, ...).
72 * It mostly handles page-in requests (from memory_object_data_request()) by
73 * getting the original data from its backing VM object, itself backed by
74 * the dyld shared cache file, modifying it if needed and providing it to VM.
76 * The modified pages will never be dirtied, so the memory manager doesn't
77 * need to handle page-out requests (from memory_object_data_return()). The
78 * pages need to be mapped copy-on-write, so that the originals stay clean.
80 * We don't expect to have to handle a large number of shared cache files,
81 * so the data structures are very simple (simple linked list) for now.
84 /* forward declarations */
85 void shared_region_pager_reference(memory_object_t mem_obj
);
86 void shared_region_pager_deallocate(memory_object_t mem_obj
);
87 kern_return_t
shared_region_pager_init(memory_object_t mem_obj
,
88 memory_object_control_t control
,
89 memory_object_cluster_size_t pg_size
);
90 kern_return_t
shared_region_pager_terminate(memory_object_t mem_obj
);
91 kern_return_t
shared_region_pager_data_request(memory_object_t mem_obj
,
92 memory_object_offset_t offset
,
93 memory_object_cluster_size_t length
,
94 vm_prot_t protection_required
,
95 memory_object_fault_info_t fault_info
);
96 kern_return_t
shared_region_pager_data_return(memory_object_t mem_obj
,
97 memory_object_offset_t offset
,
98 memory_object_cluster_size_t data_cnt
,
99 memory_object_offset_t
*resid_offset
,
102 boolean_t kernel_copy
,
104 kern_return_t
shared_region_pager_data_initialize(memory_object_t mem_obj
,
105 memory_object_offset_t offset
,
106 memory_object_cluster_size_t data_cnt
);
107 kern_return_t
shared_region_pager_data_unlock(memory_object_t mem_obj
,
108 memory_object_offset_t offset
,
109 memory_object_size_t size
,
110 vm_prot_t desired_access
);
111 kern_return_t
shared_region_pager_synchronize(memory_object_t mem_obj
,
112 memory_object_offset_t offset
,
113 memory_object_size_t length
,
114 vm_sync_t sync_flags
);
115 kern_return_t
shared_region_pager_map(memory_object_t mem_obj
,
117 kern_return_t
shared_region_pager_last_unmap(memory_object_t mem_obj
);
120 * Vector of VM operations for this EMM.
121 * These routines are invoked by VM via the memory_object_*() interfaces.
123 const struct memory_object_pager_ops shared_region_pager_ops
= {
124 .memory_object_reference
= shared_region_pager_reference
,
125 .memory_object_deallocate
= shared_region_pager_deallocate
,
126 .memory_object_init
= shared_region_pager_init
,
127 .memory_object_terminate
= shared_region_pager_terminate
,
128 .memory_object_data_request
= shared_region_pager_data_request
,
129 .memory_object_data_return
= shared_region_pager_data_return
,
130 .memory_object_data_initialize
= shared_region_pager_data_initialize
,
131 .memory_object_data_unlock
= shared_region_pager_data_unlock
,
132 .memory_object_synchronize
= shared_region_pager_synchronize
,
133 .memory_object_map
= shared_region_pager_map
,
134 .memory_object_last_unmap
= shared_region_pager_last_unmap
,
135 .memory_object_data_reclaim
= NULL
,
136 .memory_object_pager_name
= "shared_region"
139 #if __has_feature(ptrauth_calls)
141 * Track mappings between shared_region_id and the key used to sign
142 * authenticated pointers.
144 typedef struct shared_region_jop_key_map
{
145 queue_chain_t srk_queue
;
146 char *srk_shared_region_id
;
147 uint64_t srk_jop_key
;
148 os_refcnt_t srk_ref_count
; /* count of tasks active with this shared_region_id */
149 } *shared_region_jop_key_map_t
;
151 os_refgrp_decl(static, srk_refgrp
, "shared region key ref cnts", NULL
);
154 * The list is protected by the "shared_region_key_map" lock.
156 int shared_region_key_count
= 0; /* number of active shared_region_id keys */
157 queue_head_t shared_region_jop_key_queue
= QUEUE_HEAD_INITIALIZER(shared_region_jop_key_queue
);
158 LCK_GRP_DECLARE(shared_region_jop_key_lck_grp
, "shared_region_jop_key");
159 LCK_MTX_DECLARE(shared_region_jop_key_lock
, &shared_region_jop_key_lck_grp
);
162 * Find the pointer signing key for the give shared_region_id.
165 shared_region_find_key(char *shared_region_id
)
167 shared_region_jop_key_map_t region
;
170 lck_mtx_lock(&shared_region_jop_key_lock
);
171 queue_iterate(&shared_region_jop_key_queue
, region
, shared_region_jop_key_map_t
, srk_queue
) {
172 if (strcmp(region
->srk_shared_region_id
, shared_region_id
) == 0) {
176 panic("shared_region_find_key() no key for region '%s'", shared_region_id
);
179 key
= region
->srk_jop_key
;
180 lck_mtx_unlock(&shared_region_jop_key_lock
);
185 * Return a authentication key to use for the given shared_region_id.
186 * If inherit is TRUE, then the key must match inherited_key.
187 * Creates an additional reference when successful.
190 shared_region_key_alloc(char *shared_region_id
, bool inherit
, uint64_t inherited_key
)
192 shared_region_jop_key_map_t region
;
193 shared_region_jop_key_map_t
new = NULL
;
195 assert(shared_region_id
!= NULL
);
197 lck_mtx_lock(&shared_region_jop_key_lock
);
198 queue_iterate(&shared_region_jop_key_queue
, region
, shared_region_jop_key_map_t
, srk_queue
) {
199 if (strcmp(region
->srk_shared_region_id
, shared_region_id
) == 0) {
200 os_ref_retain_locked(®ion
->srk_ref_count
);
206 * ID was not found, if first time, allocate a new one and redo the lookup.
209 lck_mtx_unlock(&shared_region_jop_key_lock
);
210 new = kalloc(sizeof *new);
211 uint_t len
= strlen(shared_region_id
) + 1;
212 new->srk_shared_region_id
= kheap_alloc(KHEAP_DATA_BUFFERS
, len
, Z_WAITOK
);
213 strlcpy(new->srk_shared_region_id
, shared_region_id
, len
);
214 os_ref_init(&new->srk_ref_count
, &srk_refgrp
);
216 if (diversify_user_jop
&& inherit
) {
217 new->srk_jop_key
= inherited_key
;
218 } else if (diversify_user_jop
&& strlen(shared_region_id
) > 0) {
219 new->srk_jop_key
= generate_jop_key();
221 new->srk_jop_key
= ml_default_jop_pid();
228 * Use the newly allocated entry
230 ++shared_region_key_count
;
231 queue_enter_first(&shared_region_jop_key_queue
, new, shared_region_jop_key_map_t
, srk_queue
);
236 if (inherit
&& inherited_key
!= region
->srk_jop_key
) {
237 panic("shared_region_key_alloc() inherited key mismatch");
239 lck_mtx_unlock(&shared_region_jop_key_lock
);
242 * free any unused new entry
245 kheap_free(KHEAP_DATA_BUFFERS
, new->srk_shared_region_id
, strlen(new->srk_shared_region_id
) + 1);
246 kfree(new, sizeof *new);
251 * Mark the end of using a shared_region_id's key
254 shared_region_key_dealloc(char *shared_region_id
)
256 shared_region_jop_key_map_t region
;
258 assert(shared_region_id
!= NULL
);
259 lck_mtx_lock(&shared_region_jop_key_lock
);
260 queue_iterate(&shared_region_jop_key_queue
, region
, shared_region_jop_key_map_t
, srk_queue
) {
261 if (strcmp(region
->srk_shared_region_id
, shared_region_id
) == 0) {
265 panic("shared_region_key_dealloc() Shared region ID '%s' not found", shared_region_id
);
268 if (os_ref_release_locked(®ion
->srk_ref_count
) == 0) {
269 queue_remove(&shared_region_jop_key_queue
, region
, shared_region_jop_key_map_t
, srk_queue
);
270 --shared_region_key_count
;
274 lck_mtx_unlock(&shared_region_jop_key_lock
);
276 if (region
!= NULL
) {
277 kheap_free(KHEAP_DATA_BUFFERS
, region
->srk_shared_region_id
, strlen(region
->srk_shared_region_id
) + 1);
278 kfree(region
, sizeof *region
);
281 #endif /* __has_feature(ptrauth_calls) */
284 * The "shared_region_pager" describes a memory object backed by
285 * the "shared_region" EMM.
287 typedef struct shared_region_pager
{
288 struct memory_object srp_header
; /* mandatory generic header */
290 /* pager-specific data */
291 queue_chain_t srp_queue
; /* next & prev pagers */
292 uint32_t srp_ref_count
; /* active uses */
293 bool srp_is_mapped
; /* has active mappings */
294 bool srp_is_ready
; /* is this pager ready? */
295 vm_object_t srp_backing_object
; /* VM object for shared cache */
296 vm_object_offset_t srp_backing_offset
;
297 vm_shared_region_slide_info_t srp_slide_info
;
298 #if __has_feature(ptrauth_calls)
299 uint64_t srp_jop_key
; /* zero if used for arm64 */
300 #endif /* __has_feature(ptrauth_calls) */
301 } *shared_region_pager_t
;
302 #define SHARED_REGION_PAGER_NULL ((shared_region_pager_t) NULL)
305 * List of memory objects managed by this EMM.
306 * The list is protected by the "shared_region_pager_lock" lock.
308 int shared_region_pager_count
= 0; /* number of pagers */
309 int shared_region_pager_count_mapped
= 0; /* number of unmapped pagers */
310 queue_head_t shared_region_pager_queue
= QUEUE_HEAD_INITIALIZER(shared_region_pager_queue
);
311 LCK_GRP_DECLARE(shared_region_pager_lck_grp
, "shared_region_pager");
312 LCK_MTX_DECLARE(shared_region_pager_lock
, &shared_region_pager_lck_grp
);
315 * Maximum number of unmapped pagers we're willing to keep around.
317 int shared_region_pager_cache_limit
= 0;
320 * Statistics & counters.
322 int shared_region_pager_count_max
= 0;
323 int shared_region_pager_count_unmapped_max
= 0;
324 int shared_region_pager_num_trim_max
= 0;
325 int shared_region_pager_num_trim_total
= 0;
327 uint64_t shared_region_pager_copied
= 0;
328 uint64_t shared_region_pager_slid
= 0;
329 uint64_t shared_region_pager_slid_error
= 0;
330 uint64_t shared_region_pager_reclaimed
= 0;
332 /* internal prototypes */
333 shared_region_pager_t
shared_region_pager_lookup(memory_object_t mem_obj
);
334 void shared_region_pager_dequeue(shared_region_pager_t pager
);
335 void shared_region_pager_deallocate_internal(shared_region_pager_t pager
,
337 void shared_region_pager_terminate_internal(shared_region_pager_t pager
);
338 void shared_region_pager_trim(void);
342 int shared_region_pagerdebug
= 0;
343 #define PAGER_ALL 0xffffffff
344 #define PAGER_INIT 0x00000001
345 #define PAGER_PAGEIN 0x00000002
347 #define PAGER_DEBUG(LEVEL, A) \
349 if ((shared_region_pagerdebug & (LEVEL)) == (LEVEL)) { \
354 #define PAGER_DEBUG(LEVEL, A)
358 * shared_region_pager_init()
360 * Initialize the memory object and makes it ready to be used and mapped.
363 shared_region_pager_init(
364 memory_object_t mem_obj
,
365 memory_object_control_t control
,
369 memory_object_cluster_size_t pg_size
)
371 shared_region_pager_t pager
;
373 memory_object_attr_info_data_t attributes
;
375 PAGER_DEBUG(PAGER_ALL
,
376 ("shared_region_pager_init: %p, %p, %x\n",
377 mem_obj
, control
, pg_size
));
379 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
380 return KERN_INVALID_ARGUMENT
;
383 pager
= shared_region_pager_lookup(mem_obj
);
385 memory_object_control_reference(control
);
387 pager
->srp_header
.mo_control
= control
;
389 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
390 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
391 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
392 attributes
.may_cache_object
= FALSE
;
393 attributes
.temporary
= TRUE
;
395 kr
= memory_object_change_attributes(
397 MEMORY_OBJECT_ATTRIBUTE_INFO
,
398 (memory_object_info_t
) &attributes
,
399 MEMORY_OBJECT_ATTR_INFO_COUNT
);
400 if (kr
!= KERN_SUCCESS
) {
401 panic("shared_region_pager_init: "
402 "memory_object_change_attributes() failed");
405 #if CONFIG_SECLUDED_MEMORY
406 if (secluded_for_filecache
) {
409 * XXX FBDP do we want this in the secluded pool?
410 * Ideally, we'd want the shared region used by Camera to
411 * NOT be in the secluded pool, but all other shared regions
412 * in the secluded pool...
414 memory_object_mark_eligible_for_secluded(control
, TRUE
);
417 #endif /* CONFIG_SECLUDED_MEMORY */
423 * shared_region_data_return()
425 * Handles page-out requests from VM. This should never happen since
426 * the pages provided by this EMM are not supposed to be dirty or dirtied
427 * and VM should simply discard the contents and reclaim the pages if it
431 shared_region_pager_data_return(
432 __unused memory_object_t mem_obj
,
433 __unused memory_object_offset_t offset
,
434 __unused memory_object_cluster_size_t data_cnt
,
435 __unused memory_object_offset_t
*resid_offset
,
436 __unused
int *io_error
,
437 __unused boolean_t dirty
,
438 __unused boolean_t kernel_copy
,
439 __unused
int upl_flags
)
441 panic("shared_region_pager_data_return: should never get called");
446 shared_region_pager_data_initialize(
447 __unused memory_object_t mem_obj
,
448 __unused memory_object_offset_t offset
,
449 __unused memory_object_cluster_size_t data_cnt
)
451 panic("shared_region_pager_data_initialize: should never get called");
456 shared_region_pager_data_unlock(
457 __unused memory_object_t mem_obj
,
458 __unused memory_object_offset_t offset
,
459 __unused memory_object_size_t size
,
460 __unused vm_prot_t desired_access
)
466 * shared_region_pager_data_request()
468 * Handles page-in requests from VM.
470 int shared_region_pager_data_request_debug
= 0;
472 shared_region_pager_data_request(
473 memory_object_t mem_obj
,
474 memory_object_offset_t offset
,
475 memory_object_cluster_size_t length
,
479 vm_prot_t protection_required
,
480 memory_object_fault_info_t mo_fault_info
)
482 shared_region_pager_t pager
;
483 memory_object_control_t mo_control
;
487 upl_page_info_t
*upl_pl
;
488 unsigned int pl_count
;
489 vm_object_t src_top_object
, src_page_object
, dst_object
;
490 kern_return_t kr
, retval
;
491 vm_offset_t src_vaddr
, dst_vaddr
;
492 vm_offset_t cur_offset
;
493 vm_offset_t offset_in_page
;
494 kern_return_t error_code
;
496 vm_page_t src_page
, top_page
;
498 struct vm_object_fault_info fault_info
;
499 mach_vm_offset_t slide_start_address
;
501 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_data_request: %p, %llx, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
503 retval
= KERN_SUCCESS
;
504 src_top_object
= VM_OBJECT_NULL
;
505 src_page_object
= VM_OBJECT_NULL
;
508 fault_info
= *((struct vm_object_fault_info
*)(uintptr_t)mo_fault_info
);
509 fault_info
.stealth
= TRUE
;
510 fault_info
.io_sync
= FALSE
;
511 fault_info
.mark_zf_absent
= FALSE
;
512 fault_info
.batch_pmap_op
= FALSE
;
513 interruptible
= fault_info
.interruptible
;
515 pager
= shared_region_pager_lookup(mem_obj
);
516 assert(pager
->srp_is_ready
);
517 assert(pager
->srp_ref_count
> 1); /* pager is alive */
518 assert(pager
->srp_is_mapped
); /* pager is mapped */
520 PAGER_DEBUG(PAGER_PAGEIN
, ("shared_region_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
523 * Gather in a UPL all the VM pages requested by VM.
525 mo_control
= pager
->srp_header
.mo_control
;
529 UPL_RET_ONLY_ABSENT
|
532 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
535 kr
= memory_object_upl_request(mo_control
,
537 &upl
, NULL
, NULL
, upl_flags
, VM_KERN_MEMORY_SECURITY
);
538 if (kr
!= KERN_SUCCESS
) {
542 dst_object
= mo_control
->moc_object
;
543 assert(dst_object
!= VM_OBJECT_NULL
);
546 * We'll map the original data in the kernel address space from the
547 * backing VM object (itself backed by the shared cache file via
550 src_top_object
= pager
->srp_backing_object
;
551 assert(src_top_object
!= VM_OBJECT_NULL
);
552 vm_object_reference(src_top_object
); /* keep the source object alive */
554 slide_start_address
= pager
->srp_slide_info
->si_slid_address
;
556 fault_info
.lo_offset
+= pager
->srp_backing_offset
;
557 fault_info
.hi_offset
+= pager
->srp_backing_offset
;
560 * Fill in the contents of the pages requested by VM.
562 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
563 pl_count
= length
/ PAGE_SIZE
;
565 retval
== KERN_SUCCESS
&& cur_offset
< length
;
566 cur_offset
+= PAGE_SIZE
) {
569 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
570 /* this page is not in the UPL: skip it */
575 * Map the source (dyld shared cache) page in the kernel's
576 * virtual address space.
577 * We already hold a reference on the src_top_object.
580 vm_object_lock(src_top_object
);
581 vm_object_paging_begin(src_top_object
);
584 src_page
= VM_PAGE_NULL
;
585 kr
= vm_fault_page(src_top_object
,
586 pager
->srp_backing_offset
+ offset
+ cur_offset
,
589 FALSE
, /* src_page not looked up */
599 case VM_FAULT_SUCCESS
:
602 goto retry_src_fault
;
603 case VM_FAULT_MEMORY_SHORTAGE
:
604 if (vm_page_wait(interruptible
)) {
605 goto retry_src_fault
;
608 case VM_FAULT_INTERRUPTED
:
609 retval
= MACH_SEND_INTERRUPTED
;
611 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
612 /* success but no VM page: fail */
613 vm_object_paging_end(src_top_object
);
614 vm_object_unlock(src_top_object
);
616 case VM_FAULT_MEMORY_ERROR
:
617 /* the page is not there ! */
621 retval
= KERN_MEMORY_ERROR
;
625 panic("shared_region_pager_data_request: "
626 "vm_fault_page() unexpected error 0x%x\n",
629 assert(src_page
!= VM_PAGE_NULL
);
630 assert(src_page
->vmp_busy
);
632 if (src_page
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
633 vm_page_lockspin_queues();
634 if (src_page
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
635 vm_page_speculate(src_page
, FALSE
);
637 vm_page_unlock_queues();
641 * Establish pointers to the source
642 * and destination physical pages.
645 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
646 assert(dst_pnum
!= 0);
648 src_vaddr
= (vm_map_offset_t
)
649 phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
651 dst_vaddr
= (vm_map_offset_t
)
652 phystokv((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
653 src_page_object
= VM_PAGE_OBJECT(src_page
);
656 * Validate the original page...
658 if (src_page_object
->code_signed
) {
659 vm_page_validate_cs_mapped(
660 src_page
, PAGE_SIZE
, 0,
661 (const void *) src_vaddr
);
664 * ... and transfer the results to the destination page.
666 UPL_SET_CS_VALIDATED(upl_pl
, cur_offset
/ PAGE_SIZE
,
667 src_page
->vmp_cs_validated
);
668 UPL_SET_CS_TAINTED(upl_pl
, cur_offset
/ PAGE_SIZE
,
669 src_page
->vmp_cs_tainted
);
670 UPL_SET_CS_NX(upl_pl
, cur_offset
/ PAGE_SIZE
,
671 src_page
->vmp_cs_nx
);
674 * The page provider might access a mapped file, so let's
675 * release the object lock for the source page to avoid a
676 * potential deadlock.
677 * The source page is kept busy and we have a
678 * "paging_in_progress" reference on its object, so it's safe
679 * to unlock the object here.
681 assert(src_page
->vmp_busy
);
682 assert(src_page_object
->paging_in_progress
> 0);
683 vm_object_unlock(src_page_object
);
686 * Process the original contents of the source page
687 * into the destination page.
689 for (offset_in_page
= 0;
690 offset_in_page
< PAGE_SIZE
;
691 offset_in_page
+= PAGE_SIZE_FOR_SR_SLIDE
) {
692 vm_object_offset_t chunk_offset
;
693 vm_object_offset_t offset_in_backing_object
;
694 vm_object_offset_t offset_in_sliding_range
;
696 chunk_offset
= offset
+ cur_offset
+ offset_in_page
;
698 bcopy((const char *)(src_vaddr
+
700 (char *)(dst_vaddr
+ offset_in_page
),
701 PAGE_SIZE_FOR_SR_SLIDE
);
703 offset_in_backing_object
= (chunk_offset
+
704 pager
->srp_backing_offset
);
705 if ((offset_in_backing_object
< pager
->srp_slide_info
->si_start
) ||
706 (offset_in_backing_object
>= pager
->srp_slide_info
->si_end
)) {
707 /* chunk is outside of sliding range: done */
708 shared_region_pager_copied
++;
712 offset_in_sliding_range
= offset_in_backing_object
- pager
->srp_slide_info
->si_start
;
713 kr
= vm_shared_region_slide_page(pager
->srp_slide_info
,
714 dst_vaddr
+ offset_in_page
,
715 (mach_vm_offset_t
) (offset_in_sliding_range
+ slide_start_address
),
716 (uint32_t) (offset_in_sliding_range
/ PAGE_SIZE_FOR_SR_SLIDE
),
717 #if __has_feature(ptrauth_calls)
718 pager
->srp_slide_info
->si_ptrauth
? pager
->srp_jop_key
: 0
719 #else /* __has_feature(ptrauth_calls) */
721 #endif /* __has_feature(ptrauth_calls) */
723 if (shared_region_pager_data_request_debug
) {
724 printf("shared_region_data_request"
725 "(%p,0x%llx+0x%llx+0x%04llx): 0x%llx "
726 "in sliding range [0x%llx:0x%llx]: "
727 "SLIDE offset 0x%llx="
728 "(0x%llx+0x%llx+0x%llx+0x%04llx)"
729 "[0x%016llx 0x%016llx] "
737 (uint64_t) cur_offset
,
738 (uint64_t) offset_in_page
,
740 pager
->srp_slide_info
->si_start
,
741 pager
->srp_slide_info
->si_end
,
742 (pager
->srp_backing_offset
+
746 pager
->srp_backing_offset
,
748 (uint64_t) cur_offset
,
749 (uint64_t) offset_in_page
,
750 *(uint64_t *)(dst_vaddr
+ offset_in_page
),
751 *(uint64_t *)(dst_vaddr
+ offset_in_page
+ 8),
752 src_page_object
->code_signed
,
753 src_page
->vmp_cs_validated
,
754 src_page
->vmp_cs_tainted
,
758 if (kr
!= KERN_SUCCESS
) {
759 shared_region_pager_slid_error
++;
762 shared_region_pager_slid
++;
765 assert(VM_PAGE_OBJECT(src_page
) == src_page_object
);
766 assert(src_page
->vmp_busy
);
767 assert(src_page_object
->paging_in_progress
> 0);
768 vm_object_lock(src_page_object
);
771 * Cleanup the result of vm_fault_page() of the source page.
773 PAGE_WAKEUP_DONE(src_page
);
774 src_page
= VM_PAGE_NULL
;
775 vm_object_paging_end(src_page_object
);
776 vm_object_unlock(src_page_object
);
778 if (top_page
!= VM_PAGE_NULL
) {
779 assert(VM_PAGE_OBJECT(top_page
) == src_top_object
);
780 vm_object_lock(src_top_object
);
781 VM_PAGE_FREE(top_page
);
782 vm_object_paging_end(src_top_object
);
783 vm_object_unlock(src_top_object
);
789 /* clean up the UPL */
792 * The pages are currently dirty because we've just been
793 * writing on them, but as far as we're concerned, they're
794 * clean since they contain their "original" contents as
795 * provided by us, the pager.
796 * Tell the UPL to mark them "clean".
798 upl_clear_dirty(upl
, TRUE
);
800 /* abort or commit the UPL */
801 if (retval
!= KERN_SUCCESS
) {
805 assertf(page_aligned(upl
->u_offset
) && page_aligned(upl
->u_size
),
806 "upl %p offset 0x%llx size 0x%x\n",
807 upl
, upl
->u_offset
, upl
->u_size
);
808 upl_commit_range(upl
, 0, upl
->u_size
,
809 UPL_COMMIT_CS_VALIDATED
| UPL_COMMIT_WRITTEN_BY_KERNEL
,
810 upl_pl
, pl_count
, &empty
);
813 /* and deallocate the UPL */
817 if (src_top_object
!= VM_OBJECT_NULL
) {
818 vm_object_deallocate(src_top_object
);
824 * shared_region_pager_reference()
826 * Get a reference on this memory object.
827 * For external usage only. Assumes that the initial reference count is not 0,
828 * i.e one should not "revive" a dead pager this way.
831 shared_region_pager_reference(
832 memory_object_t mem_obj
)
834 shared_region_pager_t pager
;
836 pager
= shared_region_pager_lookup(mem_obj
);
838 lck_mtx_lock(&shared_region_pager_lock
);
839 assert(pager
->srp_ref_count
> 0);
840 pager
->srp_ref_count
++;
841 lck_mtx_unlock(&shared_region_pager_lock
);
846 * shared_region_pager_dequeue:
848 * Removes a pager from the list of pagers.
850 * The caller must hold "shared_region_pager_lock".
853 shared_region_pager_dequeue(
854 shared_region_pager_t pager
)
856 assert(!pager
->srp_is_mapped
);
858 queue_remove(&shared_region_pager_queue
,
860 shared_region_pager_t
,
862 pager
->srp_queue
.next
= NULL
;
863 pager
->srp_queue
.prev
= NULL
;
865 shared_region_pager_count
--;
869 * shared_region_pager_terminate_internal:
871 * Trigger the asynchronous termination of the memory object associated
873 * When the memory object is terminated, there will be one more call
874 * to memory_object_deallocate() (i.e. shared_region_pager_deallocate())
875 * to finish the clean up.
877 * "shared_region_pager_lock" should not be held by the caller.
878 * We don't need the lock because the pager has already been removed from
879 * the pagers' list and is now ours exclusively.
882 shared_region_pager_terminate_internal(
883 shared_region_pager_t pager
)
885 assert(pager
->srp_is_ready
);
886 assert(!pager
->srp_is_mapped
);
887 assert(pager
->srp_ref_count
== 1);
889 if (pager
->srp_backing_object
!= VM_OBJECT_NULL
) {
890 vm_object_deallocate(pager
->srp_backing_object
);
891 pager
->srp_backing_object
= VM_OBJECT_NULL
;
893 /* trigger the destruction of the memory object */
894 memory_object_destroy(pager
->srp_header
.mo_control
, 0);
898 * shared_region_pager_deallocate_internal()
900 * Release a reference on this pager and free it when the last reference goes away.
901 * Can be called with shared_region_pager_lock held or not, but always returns
905 shared_region_pager_deallocate_internal(
906 shared_region_pager_t pager
,
909 boolean_t needs_trimming
;
913 lck_mtx_lock(&shared_region_pager_lock
);
916 /* if we have too many unmapped pagers, trim some */
917 count_unmapped
= shared_region_pager_count
- shared_region_pager_count_mapped
;
918 needs_trimming
= (count_unmapped
> shared_region_pager_cache_limit
);
920 /* drop a reference on this pager */
921 assert(pager
->srp_ref_count
> 0);
922 pager
->srp_ref_count
--;
924 if (pager
->srp_ref_count
== 1) {
926 * Only the "named" reference is left, which means that
927 * no one is really holding on to this pager anymore.
930 shared_region_pager_dequeue(pager
);
931 /* the pager is all ours: no need for the lock now */
932 lck_mtx_unlock(&shared_region_pager_lock
);
933 shared_region_pager_terminate_internal(pager
);
934 } else if (pager
->srp_ref_count
== 0) {
936 * Dropped the existence reference; the memory object has
937 * been terminated. Do some final cleanup and release the
940 lck_mtx_unlock(&shared_region_pager_lock
);
942 vm_shared_region_slide_info_t si
= pager
->srp_slide_info
;
943 #if __has_feature(ptrauth_calls)
945 * The slide_info for auth sections lives in the shared region.
946 * Just deallocate() on the shared region and clear the field.
949 if (si
->si_shared_region
!= NULL
) {
950 assert(si
->si_ptrauth
);
951 vm_shared_region_deallocate(si
->si_shared_region
);
952 pager
->srp_slide_info
= NULL
;
956 #endif /* __has_feature(ptrauth_calls) */
958 vm_object_deallocate(si
->si_slide_object
);
959 /* free the slide_info_entry */
960 kheap_free(KHEAP_DATA_BUFFERS
, si
->si_slide_info_entry
, si
->si_slide_info_size
);
961 kfree(si
, sizeof *si
);
962 pager
->srp_slide_info
= NULL
;
965 if (pager
->srp_header
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
966 memory_object_control_deallocate(pager
->srp_header
.mo_control
);
967 pager
->srp_header
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
969 kfree(pager
, sizeof(*pager
));
970 pager
= SHARED_REGION_PAGER_NULL
;
972 /* there are still plenty of references: keep going... */
973 lck_mtx_unlock(&shared_region_pager_lock
);
976 if (needs_trimming
) {
977 shared_region_pager_trim();
979 /* caution: lock is not held on return... */
983 * shared_region_pager_deallocate()
985 * Release a reference on this pager and free it when the last
986 * reference goes away.
989 shared_region_pager_deallocate(
990 memory_object_t mem_obj
)
992 shared_region_pager_t pager
;
994 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_deallocate: %p\n", mem_obj
));
995 pager
= shared_region_pager_lookup(mem_obj
);
996 shared_region_pager_deallocate_internal(pager
, FALSE
);
1003 shared_region_pager_terminate(
1007 memory_object_t mem_obj
)
1009 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_terminate: %p\n", mem_obj
));
1011 return KERN_SUCCESS
;
1018 shared_region_pager_synchronize(
1019 __unused memory_object_t mem_obj
,
1020 __unused memory_object_offset_t offset
,
1021 __unused memory_object_size_t length
,
1022 __unused vm_sync_t sync_flags
)
1024 panic("shared_region_pager_synchronize: memory_object_synchronize no longer supported\n");
1025 return KERN_FAILURE
;
1029 * shared_region_pager_map()
1031 * This allows VM to let us, the EMM, know that this memory object
1032 * is currently mapped one or more times. This is called by VM each time
1033 * the memory object gets mapped, but we only take one extra reference the
1034 * first time it is called.
1037 shared_region_pager_map(
1038 memory_object_t mem_obj
,
1039 __unused vm_prot_t prot
)
1041 shared_region_pager_t pager
;
1043 PAGER_DEBUG(PAGER_ALL
, ("shared_region_pager_map: %p\n", mem_obj
));
1045 pager
= shared_region_pager_lookup(mem_obj
);
1047 lck_mtx_lock(&shared_region_pager_lock
);
1048 assert(pager
->srp_is_ready
);
1049 assert(pager
->srp_ref_count
> 0); /* pager is alive */
1050 if (!pager
->srp_is_mapped
) {
1051 pager
->srp_is_mapped
= TRUE
;
1052 pager
->srp_ref_count
++;
1053 shared_region_pager_count_mapped
++;
1055 lck_mtx_unlock(&shared_region_pager_lock
);
1057 return KERN_SUCCESS
;
1061 * shared_region_pager_last_unmap()
1063 * This is called by VM when this memory object is no longer mapped anywhere.
1066 shared_region_pager_last_unmap(
1067 memory_object_t mem_obj
)
1069 shared_region_pager_t pager
;
1072 PAGER_DEBUG(PAGER_ALL
,
1073 ("shared_region_pager_last_unmap: %p\n", mem_obj
));
1075 pager
= shared_region_pager_lookup(mem_obj
);
1077 lck_mtx_lock(&shared_region_pager_lock
);
1078 if (pager
->srp_is_mapped
) {
1080 * All the mappings are gone, so let go of the one extra
1081 * reference that represents all the mappings of this pager.
1083 shared_region_pager_count_mapped
--;
1084 count_unmapped
= (shared_region_pager_count
- shared_region_pager_count_mapped
);
1085 if (count_unmapped
> shared_region_pager_count_unmapped_max
) {
1086 shared_region_pager_count_unmapped_max
= count_unmapped
;
1088 pager
->srp_is_mapped
= FALSE
;
1089 shared_region_pager_deallocate_internal(pager
, TRUE
);
1090 /* caution: deallocate_internal() released the lock ! */
1092 lck_mtx_unlock(&shared_region_pager_lock
);
1095 return KERN_SUCCESS
;
1102 shared_region_pager_t
1103 shared_region_pager_lookup(
1104 memory_object_t mem_obj
)
1106 shared_region_pager_t pager
;
1108 assert(mem_obj
->mo_pager_ops
== &shared_region_pager_ops
);
1109 pager
= (shared_region_pager_t
)(uintptr_t) mem_obj
;
1110 assert(pager
->srp_ref_count
> 0);
1115 * Create and return a pager for the given object with the
1116 * given slide information.
1118 static shared_region_pager_t
1119 shared_region_pager_create(
1120 vm_object_t backing_object
,
1121 vm_object_offset_t backing_offset
,
1122 struct vm_shared_region_slide_info
*slide_info
,
1123 #if !__has_feature(ptrauth_calls)
1125 #endif /* !__has_feature(ptrauth_calls) */
1128 shared_region_pager_t pager
;
1129 memory_object_control_t control
;
1133 pager
= (shared_region_pager_t
) kalloc(sizeof(*pager
));
1134 if (pager
== SHARED_REGION_PAGER_NULL
) {
1135 return SHARED_REGION_PAGER_NULL
;
1139 * The vm_map call takes both named entry ports and raw memory
1140 * objects in the same parameter. We need to make sure that
1141 * vm_map does not see this object as a named entry port. So,
1142 * we reserve the first word in the object for a fake ip_kotype
1143 * setting - that will tell vm_map to use it as a memory object.
1145 pager
->srp_header
.mo_ikot
= IKOT_MEMORY_OBJECT
;
1146 pager
->srp_header
.mo_pager_ops
= &shared_region_pager_ops
;
1147 pager
->srp_header
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
1149 pager
->srp_is_ready
= FALSE
;/* not ready until it has a "name" */
1150 pager
->srp_ref_count
= 1; /* existence reference (for the cache) */
1151 pager
->srp_ref_count
++; /* for the caller */
1152 pager
->srp_is_mapped
= FALSE
;
1153 pager
->srp_backing_object
= backing_object
;
1154 pager
->srp_backing_offset
= backing_offset
;
1155 pager
->srp_slide_info
= slide_info
;
1156 #if __has_feature(ptrauth_calls)
1157 pager
->srp_jop_key
= jop_key
;
1159 * If we're getting slide_info from the shared_region,
1160 * take a reference, so it can't disappear from under us.
1162 if (slide_info
->si_shared_region
) {
1163 assert(slide_info
->si_ptrauth
);
1164 vm_shared_region_reference(slide_info
->si_shared_region
);
1166 #endif /* __has_feature(ptrauth_calls) */
1168 vm_object_reference(backing_object
);
1170 lck_mtx_lock(&shared_region_pager_lock
);
1171 /* enter new pager at the head of our list of pagers */
1172 queue_enter_first(&shared_region_pager_queue
,
1174 shared_region_pager_t
,
1176 shared_region_pager_count
++;
1177 if (shared_region_pager_count
> shared_region_pager_count_max
) {
1178 shared_region_pager_count_max
= shared_region_pager_count
;
1180 lck_mtx_unlock(&shared_region_pager_lock
);
1182 kr
= memory_object_create_named((memory_object_t
) pager
,
1185 assert(kr
== KERN_SUCCESS
);
1187 memory_object_mark_trusted(control
);
1189 lck_mtx_lock(&shared_region_pager_lock
);
1190 /* the new pager is now ready to be used */
1191 pager
->srp_is_ready
= TRUE
;
1192 object
= memory_object_to_vm_object((memory_object_t
) pager
);
1195 * No one knows about this object and so we get away without the object lock.
1196 * This object is _eventually_ backed by the dyld shared cache and so we want
1197 * to benefit from the lock priority boosting.
1199 object
->object_is_shared_cache
= TRUE
;
1200 lck_mtx_unlock(&shared_region_pager_lock
);
1202 /* wakeup anyone waiting for this pager to be ready */
1203 thread_wakeup(&pager
->srp_is_ready
);
1209 * shared_region_pager_setup()
1211 * Provide the caller with a memory object backed by the provided
1212 * "backing_object" VM object.
1215 shared_region_pager_setup(
1216 vm_object_t backing_object
,
1217 vm_object_offset_t backing_offset
,
1218 struct vm_shared_region_slide_info
*slide_info
,
1221 shared_region_pager_t pager
;
1223 /* create new pager */
1224 pager
= shared_region_pager_create(backing_object
,
1225 backing_offset
, slide_info
, jop_key
);
1226 if (pager
== SHARED_REGION_PAGER_NULL
) {
1227 /* could not create a new pager */
1228 return MEMORY_OBJECT_NULL
;
1231 lck_mtx_lock(&shared_region_pager_lock
);
1232 while (!pager
->srp_is_ready
) {
1233 lck_mtx_sleep(&shared_region_pager_lock
,
1235 &pager
->srp_is_ready
,
1238 lck_mtx_unlock(&shared_region_pager_lock
);
1240 return (memory_object_t
) pager
;
1243 #if __has_feature(ptrauth_calls)
1245 * shared_region_pager_match()
1247 * Provide the caller with a memory object backed by the provided
1248 * "backing_object" VM object.
1251 shared_region_pager_match(
1252 vm_object_t backing_object
,
1253 vm_object_offset_t backing_offset
,
1254 vm_shared_region_slide_info_t slide_info
,
1257 shared_region_pager_t pager
;
1258 vm_shared_region_slide_info_t si
;
1260 lck_mtx_lock(&shared_region_pager_lock
);
1261 queue_iterate(&shared_region_pager_queue
, pager
, shared_region_pager_t
, srp_queue
) {
1262 if (pager
->srp_backing_object
!= backing_object
->copy
) {
1265 if (pager
->srp_backing_offset
!= backing_offset
) {
1268 si
= pager
->srp_slide_info
;
1270 /* If there's no AUTH section then it can't match (slide_info is always !NULL) */
1271 if (!si
->si_ptrauth
) {
1274 if (pager
->srp_jop_key
!= jop_key
) {
1277 if (si
->si_slide
!= slide_info
->si_slide
) {
1280 if (si
->si_start
!= slide_info
->si_start
) {
1283 if (si
->si_end
!= slide_info
->si_end
) {
1286 if (si
->si_slide_object
!= slide_info
->si_slide_object
) {
1289 if (si
->si_slide_info_size
!= slide_info
->si_slide_info_size
) {
1292 if (memcmp(si
->si_slide_info_entry
, slide_info
->si_slide_info_entry
, si
->si_slide_info_size
) != 0) {
1295 ++pager
->srp_ref_count
; /* the caller expects a reference on this */
1296 lck_mtx_unlock(&shared_region_pager_lock
);
1297 return (memory_object_t
)pager
;
1301 * We didn't find a pre-existing pager, so create one.
1303 * Note slight race condition here since we drop the lock. This could lead to more than one
1304 * thread calling setup with the same arguments here. That shouldn't break anything, just
1305 * waste a little memory.
1307 lck_mtx_unlock(&shared_region_pager_lock
);
1308 return shared_region_pager_setup(backing_object
->copy
, backing_offset
, slide_info
, jop_key
);
1312 shared_region_pager_match_task_key(memory_object_t memobj
, __unused task_t task
)
1314 __unused shared_region_pager_t pager
= (shared_region_pager_t
)memobj
;
1316 assert(pager
->srp_jop_key
== task
->jop_pid
);
1318 #endif /* __has_feature(ptrauth_calls) */
1321 shared_region_pager_trim(void)
1323 shared_region_pager_t pager
, prev_pager
;
1324 queue_head_t trim_queue
;
1328 lck_mtx_lock(&shared_region_pager_lock
);
1331 * We have too many pagers, try and trim some unused ones,
1332 * starting with the oldest pager at the end of the queue.
1334 queue_init(&trim_queue
);
1337 for (pager
= (shared_region_pager_t
)queue_last(&shared_region_pager_queue
);
1338 !queue_end(&shared_region_pager_queue
, (queue_entry_t
) pager
);
1339 pager
= prev_pager
) {
1340 /* get prev elt before we dequeue */
1341 prev_pager
= (shared_region_pager_t
)queue_prev(&pager
->srp_queue
);
1343 if (pager
->srp_ref_count
== 2 &&
1344 pager
->srp_is_ready
&&
1345 !pager
->srp_is_mapped
) {
1346 /* this pager can be trimmed */
1348 /* remove this pager from the main list ... */
1349 shared_region_pager_dequeue(pager
);
1350 /* ... and add it to our trim queue */
1351 queue_enter_first(&trim_queue
,
1353 shared_region_pager_t
,
1356 /* do we have enough pagers to trim? */
1357 count_unmapped
= (shared_region_pager_count
- shared_region_pager_count_mapped
);
1358 if (count_unmapped
<= shared_region_pager_cache_limit
) {
1363 if (num_trim
> shared_region_pager_num_trim_max
) {
1364 shared_region_pager_num_trim_max
= num_trim
;
1366 shared_region_pager_num_trim_total
+= num_trim
;
1368 lck_mtx_unlock(&shared_region_pager_lock
);
1370 /* terminate the trimmed pagers */
1371 while (!queue_empty(&trim_queue
)) {
1372 queue_remove_first(&trim_queue
,
1374 shared_region_pager_t
,
1376 pager
->srp_queue
.next
= NULL
;
1377 pager
->srp_queue
.prev
= NULL
;
1378 assert(pager
->srp_ref_count
== 2);
1380 * We can't call deallocate_internal() because the pager
1381 * has already been dequeued, but we still need to remove
1384 pager
->srp_ref_count
--;
1385 shared_region_pager_terminate_internal(pager
);