2 * Copyright (c) 2008-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 <mach/kern_return.h>
30 #include <mach/memory_object_control.h>
33 #include <kern/ipc_kobject.h>
34 #include <kern/kalloc.h>
35 #include <kern/queue.h>
37 #include <vm/memory_object.h>
38 #include <vm/vm_kern.h>
39 #include <vm/vm_map.h>
40 #include <vm/vm_pageout.h>
41 #include <vm/vm_protos.h>
45 * APPLE SWAPFILE MEMORY PAGER
47 * This external memory manager (EMM) handles mappings of the swap files.
48 * Swap files are not regular files and are used solely to store contents of
49 * anonymous memory mappings while not resident in memory.
50 * There's no valid reason to map a swap file. This just puts extra burden
51 * on the system, is potentially a security issue and is not reliable since
52 * the contents can change at any time with pageout operations.
53 * Here are some of the issues with mapping a swap file.
55 * Each page in the swap file belong to an anonymous memory object. Mapping
56 * the swap file makes those pages also accessible via a vnode memory
57 * object and each page can now be resident twice.
59 * Mapping a swap file allows access to other processes' memory. Swap files
60 * are only accessible by the "root" super-user, who can already access any
61 * process's memory, so this is not a real issue but if permissions on the
62 * swap file got changed, it could become one.
63 * Swap files are not "zero-filled" on creation, so until their contents are
64 * overwritten with pageout operations, they still contain whatever was on
65 * the disk blocks they were allocated. The "super-user" could see the
66 * contents of free blocks anyway, so this is not a new security issue but
67 * it may be perceive as one.
69 * We can't legitimately prevent a user process with appropriate privileges
70 * from mapping a swap file, but we can prevent it from accessing its actual
72 * This pager mostly handles page-in request (from memory_object_data_request())
73 * for swap file mappings and just returns bogus data.
74 * Pageouts are not handled, so mmap() has to make sure it does not allow
75 * writable (i.e. MAP_SHARED and PROT_WRITE) mappings of swap files.
78 /* forward declarations */
79 void swapfile_pager_reference(memory_object_t mem_obj
);
80 void swapfile_pager_deallocate(memory_object_t mem_obj
);
81 kern_return_t
swapfile_pager_init(memory_object_t mem_obj
,
82 memory_object_control_t control
,
83 memory_object_cluster_size_t pg_size
);
84 kern_return_t
swapfile_pager_terminate(memory_object_t mem_obj
);
85 kern_return_t
swapfile_pager_data_request(memory_object_t mem_obj
,
86 memory_object_offset_t offset
,
87 memory_object_cluster_size_t length
,
88 vm_prot_t protection_required
,
89 memory_object_fault_info_t fault_info
);
90 kern_return_t
swapfile_pager_data_return(memory_object_t mem_obj
,
91 memory_object_offset_t offset
,
92 memory_object_cluster_size_t data_cnt
,
93 memory_object_offset_t
*resid_offset
,
96 boolean_t kernel_copy
,
98 kern_return_t
swapfile_pager_data_initialize(memory_object_t mem_obj
,
99 memory_object_offset_t offset
,
100 memory_object_cluster_size_t data_cnt
);
101 kern_return_t
swapfile_pager_data_unlock(memory_object_t mem_obj
,
102 memory_object_offset_t offset
,
103 memory_object_size_t size
,
104 vm_prot_t desired_access
);
105 kern_return_t
swapfile_pager_synchronize(memory_object_t mem_obj
,
106 memory_object_offset_t offset
,
107 memory_object_size_t length
,
108 vm_sync_t sync_flags
);
109 kern_return_t
swapfile_pager_map(memory_object_t mem_obj
,
111 kern_return_t
swapfile_pager_last_unmap(memory_object_t mem_obj
);
114 * Vector of VM operations for this EMM.
115 * These routines are invoked by VM via the memory_object_*() interfaces.
117 const struct memory_object_pager_ops swapfile_pager_ops
= {
118 .memory_object_reference
= swapfile_pager_reference
,
119 .memory_object_deallocate
= swapfile_pager_deallocate
,
120 .memory_object_init
= swapfile_pager_init
,
121 .memory_object_terminate
= swapfile_pager_terminate
,
122 .memory_object_data_request
= swapfile_pager_data_request
,
123 .memory_object_data_return
= swapfile_pager_data_return
,
124 .memory_object_data_initialize
= swapfile_pager_data_initialize
,
125 .memory_object_data_unlock
= swapfile_pager_data_unlock
,
126 .memory_object_synchronize
= swapfile_pager_synchronize
,
127 .memory_object_map
= swapfile_pager_map
,
128 .memory_object_last_unmap
= swapfile_pager_last_unmap
,
129 .memory_object_data_reclaim
= NULL
,
130 .memory_object_backing_object
= NULL
,
131 .memory_object_pager_name
= "swapfile pager"
135 * The "swapfile_pager" describes a memory object backed by
136 * the "swapfile" EMM.
138 typedef struct swapfile_pager
{
139 /* mandatory generic header */
140 struct memory_object swp_pgr_hdr
;
142 /* pager-specific data */
143 queue_chain_t pager_queue
; /* next & prev pagers */
144 unsigned int ref_count
; /* reference count */
145 boolean_t is_ready
; /* is this pager ready ? */
146 boolean_t is_mapped
; /* is this pager mapped ? */
147 struct vnode
*swapfile_vnode
;/* the swapfile's vnode */
149 #define SWAPFILE_PAGER_NULL ((swapfile_pager_t) NULL)
152 * List of memory objects managed by this EMM.
153 * The list is protected by the "swapfile_pager_lock" lock.
155 int swapfile_pager_count
= 0; /* number of pagers */
156 queue_head_t swapfile_pager_queue
= QUEUE_HEAD_INITIALIZER(swapfile_pager_queue
);
157 LCK_GRP_DECLARE(swapfile_pager_lck_grp
, "swapfile pager");
158 LCK_MTX_DECLARE(swapfile_pager_lock
, &swapfile_pager_lck_grp
);
161 * Statistics & counters.
163 int swapfile_pager_count_max
= 0;
165 /* internal prototypes */
166 swapfile_pager_t
swapfile_pager_create(struct vnode
*vp
);
167 swapfile_pager_t
swapfile_pager_lookup(memory_object_t mem_obj
);
168 void swapfile_pager_dequeue(swapfile_pager_t pager
);
169 void swapfile_pager_deallocate_internal(swapfile_pager_t pager
,
171 void swapfile_pager_terminate_internal(swapfile_pager_t pager
);
175 int swapfile_pagerdebug
= 0;
176 #define PAGER_ALL 0xffffffff
177 #define PAGER_INIT 0x00000001
178 #define PAGER_PAGEIN 0x00000002
180 #define PAGER_DEBUG(LEVEL, A) \
182 if ((swapfile_pagerdebug & LEVEL)==LEVEL) { \
187 #define PAGER_DEBUG(LEVEL, A)
192 * swapfile_pager_init()
194 * Initialize the memory object and makes it ready to be used and mapped.
198 memory_object_t mem_obj
,
199 memory_object_control_t control
,
203 memory_object_cluster_size_t pg_size
)
205 swapfile_pager_t pager
;
207 memory_object_attr_info_data_t attributes
;
209 PAGER_DEBUG(PAGER_ALL
,
210 ("swapfile_pager_init: %p, %p, %x\n",
211 mem_obj
, control
, pg_size
));
213 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
214 return KERN_INVALID_ARGUMENT
;
217 pager
= swapfile_pager_lookup(mem_obj
);
219 memory_object_control_reference(control
);
221 pager
->swp_pgr_hdr
.mo_control
= control
;
223 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
224 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
225 attributes
.may_cache_object
= FALSE
;
226 attributes
.temporary
= TRUE
;
228 kr
= memory_object_change_attributes(
230 MEMORY_OBJECT_ATTRIBUTE_INFO
,
231 (memory_object_info_t
) &attributes
,
232 MEMORY_OBJECT_ATTR_INFO_COUNT
);
233 if (kr
!= KERN_SUCCESS
) {
234 panic("swapfile_pager_init: "
235 "memory_object_change_attributes() failed");
242 * swapfile_data_return()
244 * Handles page-out requests from VM. This should never happen since
245 * the pages provided by this EMM are not supposed to be dirty or dirtied
246 * and VM should simply discard the contents and reclaim the pages if it
250 swapfile_pager_data_return(
251 __unused memory_object_t mem_obj
,
252 __unused memory_object_offset_t offset
,
253 __unused memory_object_cluster_size_t data_cnt
,
254 __unused memory_object_offset_t
*resid_offset
,
255 __unused
int *io_error
,
256 __unused boolean_t dirty
,
257 __unused boolean_t kernel_copy
,
258 __unused
int upl_flags
)
260 panic("swapfile_pager_data_return: should never get called");
265 swapfile_pager_data_initialize(
266 __unused memory_object_t mem_obj
,
267 __unused memory_object_offset_t offset
,
268 __unused memory_object_cluster_size_t data_cnt
)
270 panic("swapfile_pager_data_initialize: should never get called");
275 swapfile_pager_data_unlock(
276 __unused memory_object_t mem_obj
,
277 __unused memory_object_offset_t offset
,
278 __unused memory_object_size_t size
,
279 __unused vm_prot_t desired_access
)
285 * swapfile_pager_data_request()
287 * Handles page-in requests from VM.
290 swapfile_pager_data_request(
291 memory_object_t mem_obj
,
292 memory_object_offset_t offset
,
293 memory_object_cluster_size_t length
,
297 vm_prot_t protection_required
,
298 __unused memory_object_fault_info_t mo_fault_info
)
300 swapfile_pager_t pager
;
301 memory_object_control_t mo_control
;
305 upl_page_info_t
*upl_pl
= NULL
;
306 unsigned int pl_count
;
307 vm_object_t dst_object
;
308 kern_return_t kr
, retval
;
309 vm_map_offset_t kernel_mapping
;
310 vm_offset_t dst_vaddr
;
312 vm_offset_t cur_offset
;
313 vm_map_entry_t map_entry
;
315 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_data_request: %p, %llx, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
321 pager
= swapfile_pager_lookup(mem_obj
);
322 assert(pager
->is_ready
);
323 assert(pager
->ref_count
> 1); /* pager is alive and mapped */
325 PAGER_DEBUG(PAGER_PAGEIN
, ("swapfile_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
328 * Gather in a UPL all the VM pages requested by VM.
330 mo_control
= pager
->swp_pgr_hdr
.mo_control
;
334 UPL_RET_ONLY_ABSENT
|
337 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
340 kr
= memory_object_upl_request(mo_control
,
342 &upl
, NULL
, NULL
, upl_flags
, VM_KERN_MEMORY_OSFMK
);
343 if (kr
!= KERN_SUCCESS
) {
347 dst_object
= mo_control
->moc_object
;
348 assert(dst_object
!= VM_OBJECT_NULL
);
352 * Reserve a virtual page in the kernel address space to map each
353 * destination physical page when it's its turn to be processed.
355 vm_object_reference(kernel_object
); /* ref. for mapping */
356 kr
= vm_map_find_space(kernel_map
,
361 VM_MAP_KERNEL_FLAGS_NONE
,
364 if (kr
!= KERN_SUCCESS
) {
365 vm_object_deallocate(kernel_object
);
369 VME_OBJECT_SET(map_entry
, kernel_object
);
370 VME_OFFSET_SET(map_entry
, kernel_mapping
- VM_MIN_KERNEL_ADDRESS
);
371 vm_map_unlock(kernel_map
);
372 dst_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping
);
373 dst_ptr
= (char *) dst_vaddr
;
376 * Fill in the contents of the pages requested by VM.
378 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
379 pl_count
= length
/ PAGE_SIZE
;
380 for (cur_offset
= 0; cur_offset
< length
; cur_offset
+= PAGE_SIZE
) {
383 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
384 /* this page is not in the UPL: skip it */
389 * Establish an explicit pmap mapping of the destination
391 * We can't do a regular VM mapping because the VM page
395 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
396 assert(dst_pnum
!= 0);
397 retval
= pmap_enter(kernel_pmap
,
400 VM_PROT_READ
| VM_PROT_WRITE
,
405 assert(retval
== KERN_SUCCESS
);
407 if (retval
!= KERN_SUCCESS
) {
411 memset(dst_ptr
, '\0', PAGE_SIZE
);
412 /* add an end-of-line to keep line counters happy */
413 dst_ptr
[PAGE_SIZE
- 1] = '\n';
416 * Remove the pmap mapping of the destination page
419 pmap_remove(kernel_pmap
,
420 (addr64_t
) kernel_mapping
,
421 (addr64_t
) (kernel_mapping
+ PAGE_SIZE_64
));
424 retval
= KERN_SUCCESS
;
427 /* clean up the UPL */
430 * The pages are currently dirty because we've just been
431 * writing on them, but as far as we're concerned, they're
432 * clean since they contain their "original" contents as
433 * provided by us, the pager.
434 * Tell the UPL to mark them "clean".
436 upl_clear_dirty(upl
, TRUE
);
438 /* abort or commit the UPL */
439 if (retval
!= KERN_SUCCESS
) {
443 assertf(page_aligned(upl
->u_offset
) && page_aligned(upl
->u_size
),
444 "upl %p offset 0x%llx size 0x%x",
445 upl
, upl
->u_offset
, upl
->u_size
);
446 upl_commit_range(upl
, 0, upl
->u_size
,
447 UPL_COMMIT_CS_VALIDATED
,
448 upl_pl
, pl_count
, &empty
);
451 /* and deallocate the UPL */
455 if (kernel_mapping
!= 0) {
456 /* clean up the mapping of the source and destination pages */
457 kr
= vm_map_remove(kernel_map
,
459 kernel_mapping
+ PAGE_SIZE_64
,
460 VM_MAP_REMOVE_NO_FLAGS
);
461 assert(kr
== KERN_SUCCESS
);
470 * swapfile_pager_reference()
472 * Get a reference on this memory object.
473 * For external usage only. Assumes that the initial reference count is not 0,
474 * i.e one should not "revive" a dead pager this way.
477 swapfile_pager_reference(
478 memory_object_t mem_obj
)
480 swapfile_pager_t pager
;
482 pager
= swapfile_pager_lookup(mem_obj
);
484 lck_mtx_lock(&swapfile_pager_lock
);
485 assert(pager
->ref_count
> 0);
487 lck_mtx_unlock(&swapfile_pager_lock
);
492 * swapfile_pager_dequeue:
494 * Removes a pager from the list of pagers.
496 * The caller must hold "swapfile_pager_lock".
499 swapfile_pager_dequeue(
500 swapfile_pager_t pager
)
502 assert(!pager
->is_mapped
);
504 queue_remove(&swapfile_pager_queue
,
508 pager
->pager_queue
.next
= NULL
;
509 pager
->pager_queue
.prev
= NULL
;
511 swapfile_pager_count
--;
515 * swapfile_pager_terminate_internal:
517 * Trigger the asynchronous termination of the memory object associated
519 * When the memory object is terminated, there will be one more call
520 * to memory_object_deallocate() (i.e. swapfile_pager_deallocate())
521 * to finish the clean up.
523 * "swapfile_pager_lock" should not be held by the caller.
524 * We don't need the lock because the pager has already been removed from
525 * the pagers' list and is now ours exclusively.
528 swapfile_pager_terminate_internal(
529 swapfile_pager_t pager
)
531 assert(pager
->is_ready
);
532 assert(!pager
->is_mapped
);
534 if (pager
->swapfile_vnode
!= NULL
) {
535 pager
->swapfile_vnode
= NULL
;
538 /* trigger the destruction of the memory object */
539 memory_object_destroy(pager
->swp_pgr_hdr
.mo_control
, 0);
543 * swapfile_pager_deallocate_internal()
545 * Release a reference on this pager and free it when the last
546 * reference goes away.
547 * Can be called with swapfile_pager_lock held or not but always returns
551 swapfile_pager_deallocate_internal(
552 swapfile_pager_t pager
,
556 lck_mtx_lock(&swapfile_pager_lock
);
559 /* drop a reference on this pager */
562 if (pager
->ref_count
== 1) {
564 * Only the "named" reference is left, which means that
565 * no one is really holding on to this pager anymore.
568 swapfile_pager_dequeue(pager
);
569 /* the pager is all ours: no need for the lock now */
570 lck_mtx_unlock(&swapfile_pager_lock
);
571 swapfile_pager_terminate_internal(pager
);
572 } else if (pager
->ref_count
== 0) {
574 * Dropped the existence reference; the memory object has
575 * been terminated. Do some final cleanup and release the
578 lck_mtx_unlock(&swapfile_pager_lock
);
579 if (pager
->swp_pgr_hdr
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
580 memory_object_control_deallocate(pager
->swp_pgr_hdr
.mo_control
);
581 pager
->swp_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
583 kfree(pager
, sizeof(*pager
));
584 pager
= SWAPFILE_PAGER_NULL
;
586 /* there are still plenty of references: keep going... */
587 lck_mtx_unlock(&swapfile_pager_lock
);
590 /* caution: lock is not held on return... */
594 * swapfile_pager_deallocate()
596 * Release a reference on this pager and free it when the last
597 * reference goes away.
600 swapfile_pager_deallocate(
601 memory_object_t mem_obj
)
603 swapfile_pager_t pager
;
605 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_deallocate: %p\n", mem_obj
));
606 pager
= swapfile_pager_lookup(mem_obj
);
607 swapfile_pager_deallocate_internal(pager
, FALSE
);
614 swapfile_pager_terminate(
618 memory_object_t mem_obj
)
620 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_terminate: %p\n", mem_obj
));
629 swapfile_pager_synchronize(
630 __unused memory_object_t mem_obbj
,
631 __unused memory_object_offset_t offset
,
632 __unused memory_object_size_t length
,
633 __unused vm_sync_t sync_flags
)
635 panic("swapfile_pager_synchronize: memory_object_synchronize no longer supported\n");
640 * swapfile_pager_map()
642 * This allows VM to let us, the EMM, know that this memory object
643 * is currently mapped one or more times. This is called by VM each time
644 * the memory object gets mapped and we take one extra reference on the
645 * memory object to account for all its mappings.
649 memory_object_t mem_obj
,
650 __unused vm_prot_t prot
)
652 swapfile_pager_t pager
;
654 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_map: %p\n", mem_obj
));
656 pager
= swapfile_pager_lookup(mem_obj
);
658 lck_mtx_lock(&swapfile_pager_lock
);
659 assert(pager
->is_ready
);
660 assert(pager
->ref_count
> 0); /* pager is alive */
661 if (pager
->is_mapped
== FALSE
) {
663 * First mapping of this pager: take an extra reference
664 * that will remain until all the mappings of this pager
667 pager
->is_mapped
= TRUE
;
670 lck_mtx_unlock(&swapfile_pager_lock
);
676 * swapfile_pager_last_unmap()
678 * This is called by VM when this memory object is no longer mapped anywhere.
681 swapfile_pager_last_unmap(
682 memory_object_t mem_obj
)
684 swapfile_pager_t pager
;
686 PAGER_DEBUG(PAGER_ALL
,
687 ("swapfile_pager_last_unmap: %p\n", mem_obj
));
689 pager
= swapfile_pager_lookup(mem_obj
);
691 lck_mtx_lock(&swapfile_pager_lock
);
692 if (pager
->is_mapped
) {
694 * All the mappings are gone, so let go of the one extra
695 * reference that represents all the mappings of this pager.
697 pager
->is_mapped
= FALSE
;
698 swapfile_pager_deallocate_internal(pager
, TRUE
);
699 /* caution: deallocate_internal() released the lock ! */
701 lck_mtx_unlock(&swapfile_pager_lock
);
712 swapfile_pager_lookup(
713 memory_object_t mem_obj
)
715 swapfile_pager_t pager
;
717 assert(mem_obj
->mo_pager_ops
== &swapfile_pager_ops
);
718 __IGNORE_WCASTALIGN(pager
= (swapfile_pager_t
) mem_obj
);
719 assert(pager
->ref_count
> 0);
724 swapfile_pager_create(
727 swapfile_pager_t pager
, pager2
;
728 memory_object_control_t control
;
731 pager
= (swapfile_pager_t
) kalloc(sizeof(*pager
));
732 if (pager
== SWAPFILE_PAGER_NULL
) {
733 return SWAPFILE_PAGER_NULL
;
737 * The vm_map call takes both named entry ports and raw memory
738 * objects in the same parameter. We need to make sure that
739 * vm_map does not see this object as a named entry port. So,
740 * we reserve the second word in the object for a fake ip_kotype
741 * setting - that will tell vm_map to use it as a memory object.
743 pager
->swp_pgr_hdr
.mo_ikot
= IKOT_MEMORY_OBJECT
;
744 pager
->swp_pgr_hdr
.mo_pager_ops
= &swapfile_pager_ops
;
745 pager
->swp_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
747 pager
->is_ready
= FALSE
;/* not ready until it has a "name" */
748 pager
->ref_count
= 1; /* setup reference */
749 pager
->is_mapped
= FALSE
;
750 pager
->swapfile_vnode
= vp
;
752 lck_mtx_lock(&swapfile_pager_lock
);
753 /* see if anyone raced us to create a pager for the same object */
754 queue_iterate(&swapfile_pager_queue
,
758 if (pager2
->swapfile_vnode
== vp
) {
762 if (!queue_end(&swapfile_pager_queue
,
763 (queue_entry_t
) pager2
)) {
764 /* while we hold the lock, transfer our setup ref to winner */
766 /* we lost the race, down with the loser... */
767 lck_mtx_unlock(&swapfile_pager_lock
);
768 pager
->swapfile_vnode
= NULL
;
769 kfree(pager
, sizeof(*pager
));
770 /* ... and go with the winner */
772 /* let the winner make sure the pager gets ready */
776 /* enter new pager at the head of our list of pagers */
777 queue_enter_first(&swapfile_pager_queue
,
781 swapfile_pager_count
++;
782 if (swapfile_pager_count
> swapfile_pager_count_max
) {
783 swapfile_pager_count_max
= swapfile_pager_count
;
785 lck_mtx_unlock(&swapfile_pager_lock
);
787 kr
= memory_object_create_named((memory_object_t
) pager
,
790 assert(kr
== KERN_SUCCESS
);
792 memory_object_mark_trusted(control
);
794 lck_mtx_lock(&swapfile_pager_lock
);
795 /* the new pager is now ready to be used */
796 pager
->is_ready
= TRUE
;
797 lck_mtx_unlock(&swapfile_pager_lock
);
799 /* wakeup anyone waiting for this pager to be ready */
800 thread_wakeup(&pager
->is_ready
);
806 * swapfile_pager_setup()
808 * Provide the caller with a memory object backed by the provided
809 * "backing_object" VM object. If such a memory object already exists,
810 * re-use it, otherwise create a new memory object.
813 swapfile_pager_setup(
816 swapfile_pager_t pager
;
818 lck_mtx_lock(&swapfile_pager_lock
);
820 queue_iterate(&swapfile_pager_queue
,
824 if (pager
->swapfile_vnode
== vp
) {
828 if (queue_end(&swapfile_pager_queue
,
829 (queue_entry_t
) pager
)) {
830 /* no existing pager for this backing object */
831 pager
= SWAPFILE_PAGER_NULL
;
833 /* make sure pager doesn't disappear */
837 lck_mtx_unlock(&swapfile_pager_lock
);
839 if (pager
== SWAPFILE_PAGER_NULL
) {
840 pager
= swapfile_pager_create(vp
);
841 if (pager
== SWAPFILE_PAGER_NULL
) {
842 return MEMORY_OBJECT_NULL
;
846 lck_mtx_lock(&swapfile_pager_lock
);
847 while (!pager
->is_ready
) {
848 lck_mtx_sleep(&swapfile_pager_lock
,
853 lck_mtx_unlock(&swapfile_pager_lock
);
855 return (memory_object_t
) pager
;
858 memory_object_control_t
859 swapfile_pager_control(
860 memory_object_t mem_obj
)
862 swapfile_pager_t pager
;
864 if (mem_obj
== MEMORY_OBJECT_NULL
||
865 mem_obj
->mo_pager_ops
!= &swapfile_pager_ops
) {
866 return MEMORY_OBJECT_CONTROL_NULL
;
868 pager
= swapfile_pager_lookup(mem_obj
);
869 return pager
->swp_pgr_hdr
.mo_control
;