2 * Copyright (c) 2014-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/page_decrypt.h>
46 #include <kern/queue.h>
47 #include <kern/thread.h>
48 #include <kern/ipc_kobject.h>
50 #include <ipc/ipc_port.h>
51 #include <ipc/ipc_space.h>
53 #include <vm/vm_fault.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_pageout.h>
56 #include <vm/memory_object.h>
57 #include <vm/vm_pageout.h>
58 #include <vm/vm_protos.h>
59 #include <vm/vm_kern.h>
65 * This external memory manager (EMM) handles memory mappings that are
66 * 4K-aligned but not page-aligned and can therefore not be mapped directly.
68 * It mostly handles page-in requests (from memory_object_data_request()) by
69 * getting the data needed to fill in each 4K-chunk. That can require
70 * getting data from one or two pages from its backing VM object
71 * (a file or a "apple-protected" pager backed by an encrypted file), and
72 * copies the data to another page so that it is aligned as expected by
75 * Returned pages can never be dirtied and must always be mapped copy-on-write,
76 * so the memory manager does not need to handle page-out requests (from
77 * memory_object_data_return()).
81 /* forward declarations */
82 void fourk_pager_reference(memory_object_t mem_obj
);
83 void fourk_pager_deallocate(memory_object_t mem_obj
);
84 kern_return_t
fourk_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
fourk_pager_terminate(memory_object_t mem_obj
);
88 kern_return_t
fourk_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
fourk_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
fourk_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
fourk_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
fourk_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
fourk_pager_map(memory_object_t mem_obj
,
114 kern_return_t
fourk_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 fourk_pager_ops
= {
121 .memory_object_reference
= fourk_pager_reference
,
122 .memory_object_deallocate
= fourk_pager_deallocate
,
123 .memory_object_init
= fourk_pager_init
,
124 .memory_object_terminate
= fourk_pager_terminate
,
125 .memory_object_data_request
= fourk_pager_data_request
,
126 .memory_object_data_return
= fourk_pager_data_return
,
127 .memory_object_data_initialize
= fourk_pager_data_initialize
,
128 .memory_object_data_unlock
= fourk_pager_data_unlock
,
129 .memory_object_synchronize
= fourk_pager_synchronize
,
130 .memory_object_map
= fourk_pager_map
,
131 .memory_object_last_unmap
= fourk_pager_last_unmap
,
132 .memory_object_data_reclaim
= NULL
,
133 .memory_object_pager_name
= "fourk_pager"
137 * The "fourk_pager" describes a memory object backed by
140 #define FOURK_PAGER_SLOTS 4 /* 16K / 4K */
141 typedef struct fourk_pager_backing
{
142 vm_object_t backing_object
;
143 vm_object_offset_t backing_offset
;
144 } *fourk_pager_backing_t
;
145 typedef struct fourk_pager
{
146 /* mandatory generic header */
147 struct memory_object fourk_pgr_hdr
;
149 /* pager-specific data */
150 queue_chain_t pager_queue
; /* next & prev pagers */
151 unsigned int ref_count
; /* reference count */
152 int is_ready
; /* is this pager ready ? */
153 int is_mapped
; /* is this mem_obj mapped ? */
154 struct fourk_pager_backing slots
[FOURK_PAGER_SLOTS
]; /* backing for each
157 #define FOURK_PAGER_NULL ((fourk_pager_t) NULL)
160 * List of memory objects managed by this EMM.
161 * The list is protected by the "fourk_pager_lock" lock.
163 int fourk_pager_count
= 0; /* number of pagers */
164 int fourk_pager_count_mapped
= 0; /* number of unmapped pagers */
165 queue_head_t fourk_pager_queue
= QUEUE_HEAD_INITIALIZER(fourk_pager_queue
);
166 LCK_GRP_DECLARE(fourk_pager_lck_grp
, "4K-pager");
167 LCK_MTX_DECLARE(fourk_pager_lock
, &fourk_pager_lck_grp
);
170 * Maximum number of unmapped pagers we're willing to keep around.
172 int fourk_pager_cache_limit
= 0;
175 * Statistics & counters.
177 int fourk_pager_count_max
= 0;
178 int fourk_pager_count_unmapped_max
= 0;
179 int fourk_pager_num_trim_max
= 0;
180 int fourk_pager_num_trim_total
= 0;
182 /* internal prototypes */
183 fourk_pager_t
fourk_pager_lookup(memory_object_t mem_obj
);
184 void fourk_pager_dequeue(fourk_pager_t pager
);
185 void fourk_pager_deallocate_internal(fourk_pager_t pager
,
187 void fourk_pager_terminate_internal(fourk_pager_t pager
);
188 void fourk_pager_trim(void);
192 int fourk_pagerdebug
= 0;
193 #define PAGER_ALL 0xffffffff
194 #define PAGER_INIT 0x00000001
195 #define PAGER_PAGEIN 0x00000002
197 #define PAGER_DEBUG(LEVEL, A) \
199 if ((fourk_pagerdebug & LEVEL)==LEVEL) { \
204 #define PAGER_DEBUG(LEVEL, A)
211 * Initialize the memory object and makes it ready to be used and mapped.
215 memory_object_t mem_obj
,
216 memory_object_control_t control
,
220 memory_object_cluster_size_t pg_size
)
224 memory_object_attr_info_data_t attributes
;
226 PAGER_DEBUG(PAGER_ALL
,
227 ("fourk_pager_init: %p, %p, %x\n",
228 mem_obj
, control
, pg_size
));
230 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
231 return KERN_INVALID_ARGUMENT
;
234 pager
= fourk_pager_lookup(mem_obj
);
236 memory_object_control_reference(control
);
238 pager
->fourk_pgr_hdr
.mo_control
= control
;
240 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
241 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
242 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
243 attributes
.may_cache_object
= FALSE
;
244 attributes
.temporary
= TRUE
;
246 kr
= memory_object_change_attributes(
248 MEMORY_OBJECT_ATTRIBUTE_INFO
,
249 (memory_object_info_t
) &attributes
,
250 MEMORY_OBJECT_ATTR_INFO_COUNT
);
251 if (kr
!= KERN_SUCCESS
) {
252 panic("fourk_pager_init: "
253 "memory_object_change_attributes() failed");
256 #if CONFIG_SECLUDED_MEMORY
257 if (secluded_for_filecache
) {
258 memory_object_mark_eligible_for_secluded(control
, TRUE
);
260 #endif /* CONFIG_SECLUDED_MEMORY */
266 * fourk_pager_data_return()
268 * Handles page-out requests from VM. This should never happen since
269 * the pages provided by this EMM are not supposed to be dirty or dirtied
270 * and VM should simply discard the contents and reclaim the pages if it
274 fourk_pager_data_return(
275 __unused memory_object_t mem_obj
,
276 __unused memory_object_offset_t offset
,
277 __unused memory_object_cluster_size_t data_cnt
,
278 __unused memory_object_offset_t
*resid_offset
,
279 __unused
int *io_error
,
280 __unused boolean_t dirty
,
281 __unused boolean_t kernel_copy
,
282 __unused
int upl_flags
)
284 panic("fourk_pager_data_return: should never get called");
289 fourk_pager_data_initialize(
290 __unused memory_object_t mem_obj
,
291 __unused memory_object_offset_t offset
,
292 __unused memory_object_cluster_size_t data_cnt
)
294 panic("fourk_pager_data_initialize: should never get called");
299 fourk_pager_data_unlock(
300 __unused memory_object_t mem_obj
,
301 __unused memory_object_offset_t offset
,
302 __unused memory_object_size_t size
,
303 __unused vm_prot_t desired_access
)
309 * fourk_pager_reference()
311 * Get a reference on this memory object.
312 * For external usage only. Assumes that the initial reference count is not 0,
313 * i.e one should not "revive" a dead pager this way.
316 fourk_pager_reference(
317 memory_object_t mem_obj
)
321 pager
= fourk_pager_lookup(mem_obj
);
323 lck_mtx_lock(&fourk_pager_lock
);
324 assert(pager
->ref_count
> 0);
326 lck_mtx_unlock(&fourk_pager_lock
);
331 * fourk_pager_dequeue:
333 * Removes a pager from the list of pagers.
335 * The caller must hold "fourk_pager_lock".
341 assert(!pager
->is_mapped
);
343 queue_remove(&fourk_pager_queue
,
347 pager
->pager_queue
.next
= NULL
;
348 pager
->pager_queue
.prev
= NULL
;
354 * fourk_pager_terminate_internal:
356 * Trigger the asynchronous termination of the memory object associated
358 * When the memory object is terminated, there will be one more call
359 * to memory_object_deallocate() (i.e. fourk_pager_deallocate())
360 * to finish the clean up.
362 * "fourk_pager_lock" should not be held by the caller.
363 * We don't need the lock because the pager has already been removed from
364 * the pagers' list and is now ours exclusively.
367 fourk_pager_terminate_internal(
372 assert(pager
->is_ready
);
373 assert(!pager
->is_mapped
);
375 for (i
= 0; i
< FOURK_PAGER_SLOTS
; i
++) {
376 if (pager
->slots
[i
].backing_object
!= VM_OBJECT_NULL
&&
377 pager
->slots
[i
].backing_object
!= (vm_object_t
) -1) {
378 vm_object_deallocate(pager
->slots
[i
].backing_object
);
379 pager
->slots
[i
].backing_object
= (vm_object_t
) -1;
380 pager
->slots
[i
].backing_offset
= (vm_object_offset_t
) -1;
384 /* trigger the destruction of the memory object */
385 memory_object_destroy(pager
->fourk_pgr_hdr
.mo_control
, 0);
389 * fourk_pager_deallocate_internal()
391 * Release a reference on this pager and free it when the last
392 * reference goes away.
393 * Can be called with fourk_pager_lock held or not but always returns
397 fourk_pager_deallocate_internal(
401 boolean_t needs_trimming
;
405 lck_mtx_lock(&fourk_pager_lock
);
408 count_unmapped
= (fourk_pager_count
-
409 fourk_pager_count_mapped
);
410 if (count_unmapped
> fourk_pager_cache_limit
) {
411 /* we have too many unmapped pagers: trim some */
412 needs_trimming
= TRUE
;
414 needs_trimming
= FALSE
;
417 /* drop a reference on this pager */
420 if (pager
->ref_count
== 1) {
422 * Only the "named" reference is left, which means that
423 * no one is really holding on to this pager anymore.
426 fourk_pager_dequeue(pager
);
427 /* the pager is all ours: no need for the lock now */
428 lck_mtx_unlock(&fourk_pager_lock
);
429 fourk_pager_terminate_internal(pager
);
430 } else if (pager
->ref_count
== 0) {
432 * Dropped the existence reference; the memory object has
433 * been terminated. Do some final cleanup and release the
436 lck_mtx_unlock(&fourk_pager_lock
);
437 if (pager
->fourk_pgr_hdr
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
438 memory_object_control_deallocate(pager
->fourk_pgr_hdr
.mo_control
);
439 pager
->fourk_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
441 kfree(pager
, sizeof(*pager
));
442 pager
= FOURK_PAGER_NULL
;
444 /* there are still plenty of references: keep going... */
445 lck_mtx_unlock(&fourk_pager_lock
);
448 if (needs_trimming
) {
451 /* caution: lock is not held on return... */
455 * fourk_pager_deallocate()
457 * Release a reference on this pager and free it when the last
458 * reference goes away.
461 fourk_pager_deallocate(
462 memory_object_t mem_obj
)
466 PAGER_DEBUG(PAGER_ALL
, ("fourk_pager_deallocate: %p\n", mem_obj
));
467 pager
= fourk_pager_lookup(mem_obj
);
468 fourk_pager_deallocate_internal(pager
, FALSE
);
475 fourk_pager_terminate(
479 memory_object_t mem_obj
)
481 PAGER_DEBUG(PAGER_ALL
, ("fourk_pager_terminate: %p\n", mem_obj
));
490 fourk_pager_synchronize(
491 __unused memory_object_t mem_obj
,
492 __unused memory_object_offset_t offset
,
493 __unused memory_object_size_t length
,
494 __unused vm_sync_t sync_flags
)
496 panic("fourk_pager_synchronize: memory_object_synchronize no longer supported\n");
503 * This allows VM to let us, the EMM, know that this memory object
504 * is currently mapped one or more times. This is called by VM each time
505 * the memory object gets mapped and we take one extra reference on the
506 * memory object to account for all its mappings.
510 memory_object_t mem_obj
,
511 __unused vm_prot_t prot
)
515 PAGER_DEBUG(PAGER_ALL
, ("fourk_pager_map: %p\n", mem_obj
));
517 pager
= fourk_pager_lookup(mem_obj
);
519 lck_mtx_lock(&fourk_pager_lock
);
520 assert(pager
->is_ready
);
521 assert(pager
->ref_count
> 0); /* pager is alive */
522 if (pager
->is_mapped
== FALSE
) {
524 * First mapping of this pager: take an extra reference
525 * that will remain until all the mappings of this pager
528 pager
->is_mapped
= TRUE
;
530 fourk_pager_count_mapped
++;
532 lck_mtx_unlock(&fourk_pager_lock
);
538 * fourk_pager_last_unmap()
540 * This is called by VM when this memory object is no longer mapped anywhere.
543 fourk_pager_last_unmap(
544 memory_object_t mem_obj
)
549 PAGER_DEBUG(PAGER_ALL
,
550 ("fourk_pager_last_unmap: %p\n", mem_obj
));
552 pager
= fourk_pager_lookup(mem_obj
);
554 lck_mtx_lock(&fourk_pager_lock
);
555 if (pager
->is_mapped
) {
557 * All the mappings are gone, so let go of the one extra
558 * reference that represents all the mappings of this pager.
560 fourk_pager_count_mapped
--;
561 count_unmapped
= (fourk_pager_count
-
562 fourk_pager_count_mapped
);
563 if (count_unmapped
> fourk_pager_count_unmapped_max
) {
564 fourk_pager_count_unmapped_max
= count_unmapped
;
566 pager
->is_mapped
= FALSE
;
567 fourk_pager_deallocate_internal(pager
, TRUE
);
568 /* caution: deallocate_internal() released the lock ! */
570 lck_mtx_unlock(&fourk_pager_lock
);
582 memory_object_t mem_obj
)
586 assert(mem_obj
->mo_pager_ops
== &fourk_pager_ops
);
587 pager
= (fourk_pager_t
) mem_obj
;
588 assert(pager
->ref_count
> 0);
593 fourk_pager_trim(void)
595 fourk_pager_t pager
, prev_pager
;
596 queue_head_t trim_queue
;
600 lck_mtx_lock(&fourk_pager_lock
);
603 * We have too many pagers, try and trim some unused ones,
604 * starting with the oldest pager at the end of the queue.
606 queue_init(&trim_queue
);
609 for (pager
= (fourk_pager_t
)
610 queue_last(&fourk_pager_queue
);
611 !queue_end(&fourk_pager_queue
,
612 (queue_entry_t
) pager
);
613 pager
= prev_pager
) {
614 /* get prev elt before we dequeue */
615 prev_pager
= (fourk_pager_t
)
616 queue_prev(&pager
->pager_queue
);
618 if (pager
->ref_count
== 2 &&
621 /* this pager can be trimmed */
623 /* remove this pager from the main list ... */
624 fourk_pager_dequeue(pager
);
625 /* ... and add it to our trim queue */
626 queue_enter_first(&trim_queue
,
631 count_unmapped
= (fourk_pager_count
-
632 fourk_pager_count_mapped
);
633 if (count_unmapped
<= fourk_pager_cache_limit
) {
634 /* we have enough pagers to trim */
639 if (num_trim
> fourk_pager_num_trim_max
) {
640 fourk_pager_num_trim_max
= num_trim
;
642 fourk_pager_num_trim_total
+= num_trim
;
644 lck_mtx_unlock(&fourk_pager_lock
);
646 /* terminate the trimmed pagers */
647 while (!queue_empty(&trim_queue
)) {
648 queue_remove_first(&trim_queue
,
652 pager
->pager_queue
.next
= NULL
;
653 pager
->pager_queue
.prev
= NULL
;
654 assert(pager
->ref_count
== 2);
656 * We can't call deallocate_internal() because the pager
657 * has already been dequeued, but we still need to remove
661 fourk_pager_terminate_internal(pager
);
671 fourk_pager_to_vm_object(
672 memory_object_t mem_obj
)
677 pager
= fourk_pager_lookup(mem_obj
);
679 return VM_OBJECT_NULL
;
682 assert(pager
->ref_count
> 0);
683 assert(pager
->fourk_pgr_hdr
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
);
684 object
= memory_object_control_to_vm_object(pager
->fourk_pgr_hdr
.mo_control
);
685 assert(object
!= VM_OBJECT_NULL
);
690 fourk_pager_create(void)
693 memory_object_control_t control
;
698 if (PAGE_SIZE_64
== FOURK_PAGE_SIZE
) {
699 panic("fourk_pager_create: page size is 4K !?");
703 pager
= (fourk_pager_t
) kalloc(sizeof(*pager
));
704 if (pager
== FOURK_PAGER_NULL
) {
705 return MEMORY_OBJECT_NULL
;
707 bzero(pager
, sizeof(*pager
));
710 * The vm_map call takes both named entry ports and raw memory
711 * objects in the same parameter. We need to make sure that
712 * vm_map does not see this object as a named entry port. So,
713 * we reserve the first word in the object for a fake ip_kotype
714 * setting - that will tell vm_map to use it as a memory object.
716 pager
->fourk_pgr_hdr
.mo_ikot
= IKOT_MEMORY_OBJECT
;
717 pager
->fourk_pgr_hdr
.mo_pager_ops
= &fourk_pager_ops
;
718 pager
->fourk_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
720 pager
->ref_count
= 2; /* existence + setup reference */
721 pager
->is_ready
= FALSE
;/* not ready until it has a "name" */
722 pager
->is_mapped
= FALSE
;
724 for (i
= 0; i
< FOURK_PAGER_SLOTS
; i
++) {
725 pager
->slots
[i
].backing_object
= (vm_object_t
) -1;
726 pager
->slots
[i
].backing_offset
= (vm_object_offset_t
) -1;
729 lck_mtx_lock(&fourk_pager_lock
);
731 /* enter new pager at the head of our list of pagers */
732 queue_enter_first(&fourk_pager_queue
,
737 if (fourk_pager_count
> fourk_pager_count_max
) {
738 fourk_pager_count_max
= fourk_pager_count
;
740 lck_mtx_unlock(&fourk_pager_lock
);
742 kr
= memory_object_create_named((memory_object_t
) pager
,
745 assert(kr
== KERN_SUCCESS
);
747 memory_object_mark_trusted(control
);
749 lck_mtx_lock(&fourk_pager_lock
);
750 /* the new pager is now ready to be used */
751 pager
->is_ready
= TRUE
;
752 lck_mtx_unlock(&fourk_pager_lock
);
754 /* wakeup anyone waiting for this pager to be ready */
755 thread_wakeup(&pager
->is_ready
);
757 return (memory_object_t
) pager
;
761 * fourk_pager_data_request()
763 * Handles page-in requests from VM.
765 int fourk_pager_data_request_debug
= 0;
767 fourk_pager_data_request(
768 memory_object_t mem_obj
,
769 memory_object_offset_t offset
,
770 memory_object_cluster_size_t length
,
774 vm_prot_t protection_required
,
775 memory_object_fault_info_t mo_fault_info
)
778 memory_object_control_t mo_control
;
782 upl_page_info_t
*upl_pl
;
783 unsigned int pl_count
;
784 vm_object_t dst_object
;
785 kern_return_t kr
, retval
;
786 vm_map_offset_t kernel_mapping
;
787 vm_offset_t src_vaddr
, dst_vaddr
;
788 vm_offset_t cur_offset
;
790 int sub_page_idx
, sub_page_cnt
;
792 pager
= fourk_pager_lookup(mem_obj
);
793 assert(pager
->is_ready
);
794 assert(pager
->ref_count
> 1); /* pager is alive and mapped */
796 PAGER_DEBUG(PAGER_PAGEIN
, ("fourk_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
798 retval
= KERN_SUCCESS
;
801 offset
= memory_object_trunc_page(offset
);
804 * Gather in a UPL all the VM pages requested by VM.
806 mo_control
= pager
->fourk_pgr_hdr
.mo_control
;
810 UPL_RET_ONLY_ABSENT
|
813 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
816 kr
= memory_object_upl_request(mo_control
,
818 &upl
, NULL
, NULL
, upl_flags
, VM_KERN_MEMORY_NONE
);
819 if (kr
!= KERN_SUCCESS
) {
823 dst_object
= mo_control
->moc_object
;
824 assert(dst_object
!= VM_OBJECT_NULL
);
826 #if __x86_64__ || __arm__ || __arm64__
827 /* use the 1-to-1 mapping of physical memory */
828 #else /* __x86_64__ || __arm__ || __arm64__ */
830 * Reserve 2 virtual pages in the kernel address space to map the
831 * source and destination physical pages when it's their turn to
834 vm_map_entry_t map_entry
;
836 vm_object_reference(kernel_object
); /* ref. for mapping */
837 kr
= vm_map_find_space(kernel_map
,
842 VM_MAP_KERNEL_FLAGS_NONE
,
844 if (kr
!= KERN_SUCCESS
) {
845 vm_object_deallocate(kernel_object
);
849 map_entry
->object
.vm_object
= kernel_object
;
850 map_entry
->offset
= kernel_mapping
;
851 vm_map_unlock(kernel_map
);
852 src_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping
);
853 dst_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping
+ PAGE_SIZE_64
);
854 #endif /* __x86_64__ || __arm__ || __arm64__ */
857 * Fill in the contents of the pages requested by VM.
859 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
860 pl_count
= length
/ PAGE_SIZE
;
862 retval
== KERN_SUCCESS
&& cur_offset
< length
;
863 cur_offset
+= PAGE_SIZE
) {
865 int num_subpg_signed
, num_subpg_validated
;
866 int num_subpg_tainted
, num_subpg_nx
;
868 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
869 /* this page is not in the UPL: skip it */
874 * Establish an explicit pmap mapping of the destination
876 * We can't do a regular VM mapping because the VM page
880 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
881 assert(dst_pnum
!= 0);
882 dst_vaddr
= (vm_map_offset_t
)
883 phystokv((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
885 /* retrieve appropriate data for each 4K-page in this page */
886 if (PAGE_SHIFT
== FOURK_PAGE_SHIFT
&&
887 page_shift_user32
== SIXTEENK_PAGE_SHIFT
) {
889 * Find the slot for the requested 4KB page in
892 assert(PAGE_SHIFT
== FOURK_PAGE_SHIFT
);
893 assert(page_shift_user32
== SIXTEENK_PAGE_SHIFT
);
894 sub_page_idx
= ((offset
& SIXTEENK_PAGE_MASK
) /
897 * ... and provide only that one 4KB page.
902 * Iterate over all slots, i.e. retrieve all four 4KB
903 * pages in the requested 16KB page.
905 assert(PAGE_SHIFT
== SIXTEENK_PAGE_SHIFT
);
907 sub_page_cnt
= FOURK_PAGER_SLOTS
;
910 num_subpg_signed
= 0;
911 num_subpg_validated
= 0;
912 num_subpg_tainted
= 0;
915 /* retrieve appropriate data for each 4K-page in this page */
916 for (sub_page
= sub_page_idx
;
917 sub_page
< sub_page_idx
+ sub_page_cnt
;
919 vm_object_t src_object
;
920 memory_object_offset_t src_offset
;
921 vm_offset_t offset_in_src_page
;
922 kern_return_t error_code
;
923 vm_object_t src_page_object
;
928 struct vm_object_fault_info fault_info
;
929 boolean_t subpg_validated
;
930 unsigned subpg_tainted
;
933 if (offset
< SIXTEENK_PAGE_SIZE
) {
935 * The 1st 16K-page can cover multiple
936 * sub-mappings, as described in the
937 * pager->slots[] array.
940 pager
->slots
[sub_page
].backing_object
;
942 pager
->slots
[sub_page
].backing_offset
;
944 fourk_pager_backing_t slot
;
947 * Beyond the 1st 16K-page in the pager is
948 * an extension of the last "sub page" in
949 * the pager->slots[] array.
951 slot
= &pager
->slots
[FOURK_PAGER_SLOTS
- 1];
952 src_object
= slot
->backing_object
;
953 src_offset
= slot
->backing_offset
;
954 src_offset
+= FOURK_PAGE_SIZE
;
956 (vm_map_trunc_page(offset
,
958 - SIXTEENK_PAGE_SIZE
);
959 src_offset
+= sub_page
* FOURK_PAGE_SIZE
;
961 offset_in_src_page
= src_offset
& PAGE_MASK_64
;
962 src_offset
= vm_object_trunc_page(src_offset
);
964 if (src_object
== VM_OBJECT_NULL
||
965 src_object
== (vm_object_t
) -1) {
967 bzero((char *)(dst_vaddr
+
968 ((sub_page
- sub_page_idx
)
971 if (fourk_pager_data_request_debug
) {
972 printf("fourk_pager_data_request"
973 "(%p,0x%llx+0x%lx+0x%04x): "
978 ((sub_page
- sub_page_idx
)
984 /* fault in the source page from src_object */
986 src_page
= VM_PAGE_NULL
;
987 top_page
= VM_PAGE_NULL
;
988 fault_info
= *((struct vm_object_fault_info
*)
989 (uintptr_t)mo_fault_info
);
990 fault_info
.stealth
= TRUE
;
991 fault_info
.io_sync
= FALSE
;
992 fault_info
.mark_zf_absent
= FALSE
;
993 fault_info
.batch_pmap_op
= FALSE
;
994 interruptible
= fault_info
.interruptible
;
998 vm_object_lock(src_object
);
999 vm_object_paging_begin(src_object
);
1000 kr
= vm_fault_page(src_object
,
1004 FALSE
, /* src_page not looked up */
1014 case VM_FAULT_SUCCESS
:
1016 case VM_FAULT_RETRY
:
1017 goto retry_src_fault
;
1018 case VM_FAULT_MEMORY_SHORTAGE
:
1019 if (vm_page_wait(interruptible
)) {
1020 goto retry_src_fault
;
1023 case VM_FAULT_INTERRUPTED
:
1024 retval
= MACH_SEND_INTERRUPTED
;
1025 goto src_fault_done
;
1026 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
1027 /* success but no VM page: fail */
1028 vm_object_paging_end(src_object
);
1029 vm_object_unlock(src_object
);
1031 case VM_FAULT_MEMORY_ERROR
:
1032 /* the page is not there! */
1034 retval
= error_code
;
1036 retval
= KERN_MEMORY_ERROR
;
1038 goto src_fault_done
;
1040 panic("fourk_pager_data_request: "
1041 "vm_fault_page() unexpected error 0x%x\n",
1044 assert(src_page
!= VM_PAGE_NULL
);
1045 assert(src_page
->vmp_busy
);
1047 src_page_object
= VM_PAGE_OBJECT(src_page
);
1049 if ((!VM_PAGE_PAGEABLE(src_page
)) &&
1050 !VM_PAGE_WIRED(src_page
)) {
1051 vm_page_lockspin_queues();
1052 if ((!VM_PAGE_PAGEABLE(src_page
)) &&
1053 !VM_PAGE_WIRED(src_page
)) {
1054 vm_page_deactivate(src_page
);
1056 vm_page_unlock_queues();
1059 src_vaddr
= (vm_map_offset_t
)
1060 phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
1064 * Validate the 4K page we want from
1065 * this source page...
1067 subpg_validated
= FALSE
;
1069 if (src_page_object
->code_signed
) {
1070 vm_page_validate_cs_mapped_chunk(
1072 (const void *) src_vaddr
,
1078 if (subpg_validated
) {
1079 num_subpg_validated
++;
1081 if (subpg_tainted
& CS_VALIDATE_TAINTED
) {
1082 num_subpg_tainted
++;
1084 if (subpg_tainted
& CS_VALIDATE_NX
) {
1085 /* subpg should not be executable */
1086 if (sub_page_cnt
> 1) {
1088 * The destination page has
1089 * more than 1 subpage and its
1090 * other subpages might need
1091 * EXEC, so we do not propagate
1092 * CS_VALIDATE_NX to the
1093 * destination page...
1102 * Copy the relevant portion of the source page
1103 * into the appropriate part of the destination page.
1105 bcopy((const char *)(src_vaddr
+ offset_in_src_page
),
1106 (char *)(dst_vaddr
+
1107 ((sub_page
- sub_page_idx
) *
1110 if (fourk_pager_data_request_debug
) {
1111 printf("fourk_data_request"
1112 "(%p,0x%llx+0x%lx+0x%04x): "
1113 "backed by [%p:0x%llx]: "
1114 "[0x%016llx 0x%016llx] "
1116 "cs_valid=%d cs_tainted=%d cs_nx=%d\n",
1119 (sub_page
- sub_page_idx
) * FOURK_PAGE_SIZE
,
1121 src_page
->vmp_offset
+ offset_in_src_page
,
1122 *(uint64_t *)(dst_vaddr
+
1123 ((sub_page
- sub_page_idx
) *
1125 *(uint64_t *)(dst_vaddr
+
1126 ((sub_page
- sub_page_idx
) *
1129 src_page_object
->code_signed
,
1131 !!(subpg_tainted
& CS_VALIDATE_TAINTED
),
1132 !!(subpg_tainted
& CS_VALIDATE_NX
));
1135 #if __x86_64__ || __arm__ || __arm64__
1136 /* we used the 1-to-1 mapping of physical memory */
1138 #else /* __x86_64__ || __arm__ || __arm64__ */
1140 * Remove the pmap mapping of the source page
1143 pmap_remove(kernel_pmap
,
1144 (addr64_t
) src_vaddr
,
1145 (addr64_t
) src_vaddr
+ PAGE_SIZE_64
);
1146 #endif /* __x86_64__ || __arm__ || __arm64__ */
1150 * Cleanup the result of vm_fault_page().
1153 assert(VM_PAGE_OBJECT(src_page
) == src_page_object
);
1155 PAGE_WAKEUP_DONE(src_page
);
1156 src_page
= VM_PAGE_NULL
;
1157 vm_object_paging_end(src_page_object
);
1158 vm_object_unlock(src_page_object
);
1160 vm_object_t top_object
;
1162 top_object
= VM_PAGE_OBJECT(top_page
);
1163 vm_object_lock(top_object
);
1164 VM_PAGE_FREE(top_page
);
1165 top_page
= VM_PAGE_NULL
;
1166 vm_object_paging_end(top_object
);
1167 vm_object_unlock(top_object
);
1171 if (num_subpg_signed
> 0) {
1172 /* some code-signing involved with this 16K page */
1173 if (num_subpg_tainted
> 0) {
1174 /* a tainted subpage taints entire 16K page */
1175 UPL_SET_CS_TAINTED(upl_pl
,
1176 cur_offset
/ PAGE_SIZE
,
1178 /* also mark as "validated" for consisteny */
1179 UPL_SET_CS_VALIDATED(upl_pl
,
1180 cur_offset
/ PAGE_SIZE
,
1182 } else if (num_subpg_validated
== num_subpg_signed
) {
1184 * All the code-signed 4K subpages of this
1185 * 16K page are validated: our 16K page is
1186 * considered validated.
1188 UPL_SET_CS_VALIDATED(upl_pl
,
1189 cur_offset
/ PAGE_SIZE
,
1192 if (num_subpg_nx
> 0) {
1193 UPL_SET_CS_NX(upl_pl
,
1194 cur_offset
/ PAGE_SIZE
,
1202 /* clean up the UPL */
1205 * The pages are currently dirty because we've just been
1206 * writing on them, but as far as we're concerned, they're
1207 * clean since they contain their "original" contents as
1208 * provided by us, the pager.
1209 * Tell the UPL to mark them "clean".
1211 upl_clear_dirty(upl
, TRUE
);
1213 /* abort or commit the UPL */
1214 if (retval
!= KERN_SUCCESS
) {
1216 if (retval
== KERN_ABORTED
) {
1217 wait_result_t wait_result
;
1220 * We aborted the fault and did not provide
1221 * any contents for the requested pages but
1222 * the pages themselves are not invalid, so
1223 * let's return success and let the caller
1224 * retry the fault, in case it might succeed
1225 * later (when the decryption code is up and
1226 * running in the kernel, for example).
1228 retval
= KERN_SUCCESS
;
1230 * Wait a little bit first to avoid using
1231 * too much CPU time retrying and failing
1232 * the same fault over and over again.
1234 wait_result
= assert_wait_timeout(
1235 (event_t
) fourk_pager_data_request
,
1239 assert(wait_result
== THREAD_WAITING
);
1240 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1241 assert(wait_result
== THREAD_TIMED_OUT
);
1245 assertf(page_aligned(upl
->u_offset
) && page_aligned(upl
->u_size
),
1246 "upl %p offset 0x%llx size 0x%x",
1247 upl
, upl
->u_offset
, upl
->u_size
);
1248 upl_commit_range(upl
, 0, upl
->u_size
,
1249 UPL_COMMIT_CS_VALIDATED
| UPL_COMMIT_WRITTEN_BY_KERNEL
,
1250 upl_pl
, pl_count
, &empty
);
1253 /* and deallocate the UPL */
1254 upl_deallocate(upl
);
1257 if (kernel_mapping
!= 0) {
1258 /* clean up the mapping of the source and destination pages */
1259 kr
= vm_map_remove(kernel_map
,
1261 kernel_mapping
+ (2 * PAGE_SIZE_64
),
1262 VM_MAP_REMOVE_NO_FLAGS
);
1263 assert(kr
== KERN_SUCCESS
);
1275 fourk_pager_populate(
1276 memory_object_t mem_obj
,
1277 boolean_t overwrite
,
1279 vm_object_t new_backing_object
,
1280 vm_object_offset_t new_backing_offset
,
1281 vm_object_t
*old_backing_object
,
1282 vm_object_offset_t
*old_backing_offset
)
1284 fourk_pager_t pager
;
1286 pager
= fourk_pager_lookup(mem_obj
);
1287 if (pager
== NULL
) {
1288 return KERN_INVALID_ARGUMENT
;
1291 assert(pager
->ref_count
> 0);
1292 assert(pager
->fourk_pgr_hdr
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
);
1294 if (index
< 0 || index
> FOURK_PAGER_SLOTS
) {
1295 return KERN_INVALID_ARGUMENT
;
1299 (pager
->slots
[index
].backing_object
!= (vm_object_t
) -1 ||
1300 pager
->slots
[index
].backing_offset
!= (vm_object_offset_t
) -1)) {
1301 return KERN_INVALID_ADDRESS
;
1304 *old_backing_object
= pager
->slots
[index
].backing_object
;
1305 *old_backing_offset
= pager
->slots
[index
].backing_offset
;
1307 pager
->slots
[index
].backing_object
= new_backing_object
;
1308 pager
->slots
[index
].backing_offset
= new_backing_offset
;
1310 return KERN_SUCCESS
;