2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <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>
36 #include <os/refcnt.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 swapfile_pager_reference
,
119 swapfile_pager_deallocate
,
121 swapfile_pager_terminate
,
122 swapfile_pager_data_request
,
123 swapfile_pager_data_return
,
124 swapfile_pager_data_initialize
,
125 swapfile_pager_data_unlock
,
126 swapfile_pager_synchronize
,
128 swapfile_pager_last_unmap
,
129 NULL
, /* data_reclaim */
134 * The "swapfile_pager" describes a memory object backed by
135 * the "swapfile" EMM.
137 typedef struct swapfile_pager
{
138 /* mandatory generic header */
139 struct memory_object swp_pgr_hdr
;
141 /* pager-specific data */
142 queue_chain_t pager_queue
; /* next & prev pagers */
143 struct os_refcnt ref_count
; /* reference count */
144 boolean_t is_ready
; /* is this pager ready ? */
145 boolean_t is_mapped
; /* is this pager mapped ? */
146 struct vnode
*swapfile_vnode
;/* the swapfile's vnode */
148 #define SWAPFILE_PAGER_NULL ((swapfile_pager_t) NULL)
151 * List of memory objects managed by this EMM.
152 * The list is protected by the "swapfile_pager_lock" lock.
154 int swapfile_pager_count
= 0; /* number of pagers */
155 queue_head_t swapfile_pager_queue
;
156 decl_lck_mtx_data(, swapfile_pager_lock
)
159 * Statistics & counters.
161 int swapfile_pager_count_max
= 0;
164 lck_grp_t swapfile_pager_lck_grp
;
165 lck_grp_attr_t swapfile_pager_lck_grp_attr
;
166 lck_attr_t swapfile_pager_lck_attr
;
169 /* internal prototypes */
170 swapfile_pager_t
swapfile_pager_create(struct vnode
*vp
);
171 swapfile_pager_t
swapfile_pager_lookup(memory_object_t mem_obj
);
172 void swapfile_pager_dequeue(swapfile_pager_t pager
);
173 void swapfile_pager_deallocate_internal(swapfile_pager_t pager
,
175 void swapfile_pager_terminate_internal(swapfile_pager_t pager
);
179 int swapfile_pagerdebug
= 0;
180 #define PAGER_ALL 0xffffffff
181 #define PAGER_INIT 0x00000001
182 #define PAGER_PAGEIN 0x00000002
184 #define PAGER_DEBUG(LEVEL, A) \
186 if ((swapfile_pagerdebug & LEVEL)==LEVEL) { \
191 #define PAGER_DEBUG(LEVEL, A)
196 swapfile_pager_bootstrap(void)
198 lck_grp_attr_setdefault(&swapfile_pager_lck_grp_attr
);
199 lck_grp_init(&swapfile_pager_lck_grp
, "swapfile pager", &swapfile_pager_lck_grp_attr
);
200 lck_attr_setdefault(&swapfile_pager_lck_attr
);
201 lck_mtx_init(&swapfile_pager_lock
, &swapfile_pager_lck_grp
, &swapfile_pager_lck_attr
);
202 queue_init(&swapfile_pager_queue
);
206 * swapfile_pager_init()
208 * Initialize the memory object and makes it ready to be used and mapped.
212 memory_object_t mem_obj
,
213 memory_object_control_t control
,
217 memory_object_cluster_size_t pg_size
)
219 swapfile_pager_t pager
;
221 memory_object_attr_info_data_t attributes
;
223 PAGER_DEBUG(PAGER_ALL
,
224 ("swapfile_pager_init: %p, %p, %x\n",
225 mem_obj
, control
, pg_size
));
227 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
228 return KERN_INVALID_ARGUMENT
;
231 pager
= swapfile_pager_lookup(mem_obj
);
233 memory_object_control_reference(control
);
235 pager
->swp_pgr_hdr
.mo_control
= control
;
237 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
238 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
239 attributes
.may_cache_object
= FALSE
;
240 attributes
.temporary
= TRUE
;
242 kr
= memory_object_change_attributes(
244 MEMORY_OBJECT_ATTRIBUTE_INFO
,
245 (memory_object_info_t
) &attributes
,
246 MEMORY_OBJECT_ATTR_INFO_COUNT
);
247 if (kr
!= KERN_SUCCESS
) {
248 panic("swapfile_pager_init: "
249 "memory_object_change_attributes() failed");
256 * swapfile_data_return()
258 * Handles page-out requests from VM. This should never happen since
259 * the pages provided by this EMM are not supposed to be dirty or dirtied
260 * and VM should simply discard the contents and reclaim the pages if it
264 swapfile_pager_data_return(
265 __unused memory_object_t mem_obj
,
266 __unused memory_object_offset_t offset
,
267 __unused memory_object_cluster_size_t data_cnt
,
268 __unused memory_object_offset_t
*resid_offset
,
269 __unused
int *io_error
,
270 __unused boolean_t dirty
,
271 __unused boolean_t kernel_copy
,
272 __unused
int upl_flags
)
274 panic("swapfile_pager_data_return: should never get called");
279 swapfile_pager_data_initialize(
280 __unused memory_object_t mem_obj
,
281 __unused memory_object_offset_t offset
,
282 __unused memory_object_cluster_size_t data_cnt
)
284 panic("swapfile_pager_data_initialize: should never get called");
289 swapfile_pager_data_unlock(
290 __unused memory_object_t mem_obj
,
291 __unused memory_object_offset_t offset
,
292 __unused memory_object_size_t size
,
293 __unused vm_prot_t desired_access
)
299 * swapfile_pager_data_request()
301 * Handles page-in requests from VM.
304 swapfile_pager_data_request(
305 memory_object_t mem_obj
,
306 memory_object_offset_t offset
,
307 memory_object_cluster_size_t length
,
311 vm_prot_t protection_required
,
312 __unused memory_object_fault_info_t mo_fault_info
)
314 swapfile_pager_t pager
;
315 memory_object_control_t mo_control
;
319 upl_page_info_t
*upl_pl
= NULL
;
320 unsigned int pl_count
;
321 vm_object_t dst_object
;
322 kern_return_t kr
, retval
;
323 vm_map_offset_t kernel_mapping
;
324 vm_offset_t dst_vaddr
;
326 vm_offset_t cur_offset
;
327 vm_map_entry_t map_entry
;
329 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_data_request: %p, %llx, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
335 pager
= swapfile_pager_lookup(mem_obj
);
336 assert(pager
->is_ready
);
337 assert(os_ref_get_count(&pager
->ref_count
) > 1); /* pager is alive and mapped */
339 PAGER_DEBUG(PAGER_PAGEIN
, ("swapfile_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
342 * Gather in a UPL all the VM pages requested by VM.
344 mo_control
= pager
->swp_pgr_hdr
.mo_control
;
348 UPL_RET_ONLY_ABSENT
|
351 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
354 kr
= memory_object_upl_request(mo_control
,
356 &upl
, NULL
, NULL
, upl_flags
, VM_KERN_MEMORY_OSFMK
);
357 if (kr
!= KERN_SUCCESS
) {
361 dst_object
= mo_control
->moc_object
;
362 assert(dst_object
!= VM_OBJECT_NULL
);
366 * Reserve a virtual page in the kernel address space to map each
367 * destination physical page when it's its turn to be processed.
369 vm_object_reference(kernel_object
); /* ref. for mapping */
370 kr
= vm_map_find_space(kernel_map
,
375 VM_MAP_KERNEL_FLAGS_NONE
,
378 if (kr
!= KERN_SUCCESS
) {
379 vm_object_deallocate(kernel_object
);
383 VME_OBJECT_SET(map_entry
, kernel_object
);
384 VME_OFFSET_SET(map_entry
, kernel_mapping
- VM_MIN_KERNEL_ADDRESS
);
385 vm_map_unlock(kernel_map
);
386 dst_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping
);
387 dst_ptr
= (char *) dst_vaddr
;
390 * Fill in the contents of the pages requested by VM.
392 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
393 pl_count
= length
/ PAGE_SIZE
;
394 for (cur_offset
= 0; cur_offset
< length
; cur_offset
+= PAGE_SIZE
) {
397 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
398 /* this page is not in the UPL: skip it */
403 * Establish an explicit pmap mapping of the destination
405 * We can't do a regular VM mapping because the VM page
409 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
410 assert(dst_pnum
!= 0);
411 retval
= pmap_enter(kernel_pmap
,
414 VM_PROT_READ
| VM_PROT_WRITE
,
419 assert(retval
== KERN_SUCCESS
);
421 if (retval
!= KERN_SUCCESS
) {
425 memset(dst_ptr
, '\0', PAGE_SIZE
);
426 /* add an end-of-line to keep line counters happy */
427 dst_ptr
[PAGE_SIZE
- 1] = '\n';
430 * Remove the pmap mapping of the destination page
433 pmap_remove(kernel_pmap
,
434 (addr64_t
) kernel_mapping
,
435 (addr64_t
) (kernel_mapping
+ PAGE_SIZE_64
));
438 retval
= KERN_SUCCESS
;
441 /* clean up the UPL */
444 * The pages are currently dirty because we've just been
445 * writing on them, but as far as we're concerned, they're
446 * clean since they contain their "original" contents as
447 * provided by us, the pager.
448 * Tell the UPL to mark them "clean".
450 upl_clear_dirty(upl
, TRUE
);
452 /* abort or commit the UPL */
453 if (retval
!= KERN_SUCCESS
) {
457 upl_commit_range(upl
, 0, upl
->size
,
458 UPL_COMMIT_CS_VALIDATED
,
459 upl_pl
, pl_count
, &empty
);
462 /* and deallocate the UPL */
466 if (kernel_mapping
!= 0) {
467 /* clean up the mapping of the source and destination pages */
468 kr
= vm_map_remove(kernel_map
,
470 kernel_mapping
+ PAGE_SIZE_64
,
471 VM_MAP_REMOVE_NO_FLAGS
);
472 assert(kr
== KERN_SUCCESS
);
481 * swapfile_pager_reference()
483 * Get a reference on this memory object.
484 * For external usage only. Assumes that the initial reference count is not 0,
485 * i.e one should not "revive" a dead pager this way.
488 swapfile_pager_reference(
489 memory_object_t mem_obj
)
491 swapfile_pager_t pager
;
493 pager
= swapfile_pager_lookup(mem_obj
);
495 lck_mtx_lock(&swapfile_pager_lock
);
496 os_ref_retain_locked(&pager
->ref_count
);
497 lck_mtx_unlock(&swapfile_pager_lock
);
502 * swapfile_pager_dequeue:
504 * Removes a pager from the list of pagers.
506 * The caller must hold "swapfile_pager_lock".
509 swapfile_pager_dequeue(
510 swapfile_pager_t pager
)
512 assert(!pager
->is_mapped
);
514 queue_remove(&swapfile_pager_queue
,
518 pager
->pager_queue
.next
= NULL
;
519 pager
->pager_queue
.prev
= NULL
;
521 swapfile_pager_count
--;
525 * swapfile_pager_terminate_internal:
527 * Trigger the asynchronous termination of the memory object associated
529 * When the memory object is terminated, there will be one more call
530 * to memory_object_deallocate() (i.e. swapfile_pager_deallocate())
531 * to finish the clean up.
533 * "swapfile_pager_lock" should not be held by the caller.
534 * We don't need the lock because the pager has already been removed from
535 * the pagers' list and is now ours exclusively.
538 swapfile_pager_terminate_internal(
539 swapfile_pager_t pager
)
541 assert(pager
->is_ready
);
542 assert(!pager
->is_mapped
);
544 if (pager
->swapfile_vnode
!= NULL
) {
545 pager
->swapfile_vnode
= NULL
;
548 /* trigger the destruction of the memory object */
549 memory_object_destroy(pager
->swp_pgr_hdr
.mo_control
, 0);
553 * swapfile_pager_deallocate_internal()
555 * Release a reference on this pager and free it when the last
556 * reference goes away.
557 * Can be called with swapfile_pager_lock held or not but always returns
561 swapfile_pager_deallocate_internal(
562 swapfile_pager_t pager
,
566 lck_mtx_lock(&swapfile_pager_lock
);
569 /* drop a reference on this pager */
570 os_ref_count_t refcount
= os_ref_release_locked(&pager
->ref_count
);
574 * Only the "named" reference is left, which means that
575 * no one is really holding on to this pager anymore.
578 swapfile_pager_dequeue(pager
);
579 /* the pager is all ours: no need for the lock now */
580 lck_mtx_unlock(&swapfile_pager_lock
);
581 swapfile_pager_terminate_internal(pager
);
582 } else if (refcount
== 0) {
584 * Dropped the existence reference; the memory object has
585 * been terminated. Do some final cleanup and release the
588 lck_mtx_unlock(&swapfile_pager_lock
);
589 if (pager
->swp_pgr_hdr
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
590 memory_object_control_deallocate(pager
->swp_pgr_hdr
.mo_control
);
591 pager
->swp_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
593 kfree(pager
, sizeof(*pager
));
594 pager
= SWAPFILE_PAGER_NULL
;
596 /* there are still plenty of references: keep going... */
597 lck_mtx_unlock(&swapfile_pager_lock
);
600 /* caution: lock is not held on return... */
604 * swapfile_pager_deallocate()
606 * Release a reference on this pager and free it when the last
607 * reference goes away.
610 swapfile_pager_deallocate(
611 memory_object_t mem_obj
)
613 swapfile_pager_t pager
;
615 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_deallocate: %p\n", mem_obj
));
616 pager
= swapfile_pager_lookup(mem_obj
);
617 swapfile_pager_deallocate_internal(pager
, FALSE
);
624 swapfile_pager_terminate(
628 memory_object_t mem_obj
)
630 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_terminate: %p\n", mem_obj
));
639 swapfile_pager_synchronize(
640 __unused memory_object_t mem_obbj
,
641 __unused memory_object_offset_t offset
,
642 __unused memory_object_size_t length
,
643 __unused vm_sync_t sync_flags
)
645 panic("swapfile_pager_synchronize: memory_object_synchronize no longer supported\n");
650 * swapfile_pager_map()
652 * This allows VM to let us, the EMM, know that this memory object
653 * is currently mapped one or more times. This is called by VM each time
654 * the memory object gets mapped and we take one extra reference on the
655 * memory object to account for all its mappings.
659 memory_object_t mem_obj
,
660 __unused vm_prot_t prot
)
662 swapfile_pager_t pager
;
664 PAGER_DEBUG(PAGER_ALL
, ("swapfile_pager_map: %p\n", mem_obj
));
666 pager
= swapfile_pager_lookup(mem_obj
);
668 lck_mtx_lock(&swapfile_pager_lock
);
669 assert(pager
->is_ready
);
670 assert(os_ref_get_count(&pager
->ref_count
) > 0); /* pager is alive */
671 if (pager
->is_mapped
== FALSE
) {
673 * First mapping of this pager: take an extra reference
674 * that will remain until all the mappings of this pager
677 pager
->is_mapped
= TRUE
;
678 os_ref_retain_locked(&pager
->ref_count
);
680 lck_mtx_unlock(&swapfile_pager_lock
);
686 * swapfile_pager_last_unmap()
688 * This is called by VM when this memory object is no longer mapped anywhere.
691 swapfile_pager_last_unmap(
692 memory_object_t mem_obj
)
694 swapfile_pager_t pager
;
696 PAGER_DEBUG(PAGER_ALL
,
697 ("swapfile_pager_last_unmap: %p\n", mem_obj
));
699 pager
= swapfile_pager_lookup(mem_obj
);
701 lck_mtx_lock(&swapfile_pager_lock
);
702 if (pager
->is_mapped
) {
704 * All the mappings are gone, so let go of the one extra
705 * reference that represents all the mappings of this pager.
707 pager
->is_mapped
= FALSE
;
708 swapfile_pager_deallocate_internal(pager
, TRUE
);
709 /* caution: deallocate_internal() released the lock ! */
711 lck_mtx_unlock(&swapfile_pager_lock
);
722 swapfile_pager_lookup(
723 memory_object_t mem_obj
)
725 swapfile_pager_t pager
;
727 assert(mem_obj
->mo_pager_ops
== &swapfile_pager_ops
);
728 __IGNORE_WCASTALIGN(pager
= (swapfile_pager_t
) mem_obj
);
729 assert(os_ref_get_count(&pager
->ref_count
) > 0);
734 swapfile_pager_create(
737 swapfile_pager_t pager
, pager2
;
738 memory_object_control_t control
;
741 pager
= (swapfile_pager_t
) kalloc(sizeof(*pager
));
742 if (pager
== SWAPFILE_PAGER_NULL
) {
743 return SWAPFILE_PAGER_NULL
;
747 * The vm_map call takes both named entry ports and raw memory
748 * objects in the same parameter. We need to make sure that
749 * vm_map does not see this object as a named entry port. So,
750 * we reserve the second word in the object for a fake ip_kotype
751 * setting - that will tell vm_map to use it as a memory object.
753 pager
->swp_pgr_hdr
.mo_ikot
= IKOT_MEMORY_OBJECT
;
754 pager
->swp_pgr_hdr
.mo_pager_ops
= &swapfile_pager_ops
;
755 pager
->swp_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
757 pager
->is_ready
= FALSE
;/* not ready until it has a "name" */
758 os_ref_init(&pager
->ref_count
, NULL
); /* setup reference */
759 pager
->is_mapped
= FALSE
;
760 pager
->swapfile_vnode
= vp
;
762 lck_mtx_lock(&swapfile_pager_lock
);
763 /* see if anyone raced us to create a pager for the same object */
764 queue_iterate(&swapfile_pager_queue
,
768 if (pager2
->swapfile_vnode
== vp
) {
772 if (!queue_end(&swapfile_pager_queue
,
773 (queue_entry_t
) pager2
)) {
774 /* while we hold the lock, transfer our setup ref to winner */
775 os_ref_retain_locked(&pager2
->ref_count
);
776 /* we lost the race, down with the loser... */
777 lck_mtx_unlock(&swapfile_pager_lock
);
778 pager
->swapfile_vnode
= NULL
;
779 kfree(pager
, sizeof(*pager
));
780 /* ... and go with the winner */
782 /* let the winner make sure the pager gets ready */
786 /* enter new pager at the head of our list of pagers */
787 queue_enter_first(&swapfile_pager_queue
,
791 swapfile_pager_count
++;
792 if (swapfile_pager_count
> swapfile_pager_count_max
) {
793 swapfile_pager_count_max
= swapfile_pager_count
;
795 lck_mtx_unlock(&swapfile_pager_lock
);
797 kr
= memory_object_create_named((memory_object_t
) pager
,
800 assert(kr
== KERN_SUCCESS
);
802 lck_mtx_lock(&swapfile_pager_lock
);
803 /* the new pager is now ready to be used */
804 pager
->is_ready
= TRUE
;
805 lck_mtx_unlock(&swapfile_pager_lock
);
807 /* wakeup anyone waiting for this pager to be ready */
808 thread_wakeup(&pager
->is_ready
);
814 * swapfile_pager_setup()
816 * Provide the caller with a memory object backed by the provided
817 * "backing_object" VM object. If such a memory object already exists,
818 * re-use it, otherwise create a new memory object.
821 swapfile_pager_setup(
824 swapfile_pager_t pager
;
826 lck_mtx_lock(&swapfile_pager_lock
);
828 queue_iterate(&swapfile_pager_queue
,
832 if (pager
->swapfile_vnode
== vp
) {
836 if (queue_end(&swapfile_pager_queue
,
837 (queue_entry_t
) pager
)) {
838 /* no existing pager for this backing object */
839 pager
= SWAPFILE_PAGER_NULL
;
841 /* make sure pager doesn't disappear */
842 os_ref_retain_locked(&pager
->ref_count
);
845 lck_mtx_unlock(&swapfile_pager_lock
);
847 if (pager
== SWAPFILE_PAGER_NULL
) {
848 pager
= swapfile_pager_create(vp
);
849 if (pager
== SWAPFILE_PAGER_NULL
) {
850 return MEMORY_OBJECT_NULL
;
854 lck_mtx_lock(&swapfile_pager_lock
);
855 while (!pager
->is_ready
) {
856 lck_mtx_sleep(&swapfile_pager_lock
,
861 lck_mtx_unlock(&swapfile_pager_lock
);
863 return (memory_object_t
) pager
;
866 memory_object_control_t
867 swapfile_pager_control(
868 memory_object_t mem_obj
)
870 swapfile_pager_t pager
;
872 if (mem_obj
== MEMORY_OBJECT_NULL
||
873 mem_obj
->mo_pager_ops
!= &swapfile_pager_ops
) {
874 return MEMORY_OBJECT_CONTROL_NULL
;
876 pager
= swapfile_pager_lookup(mem_obj
);
877 return pager
->swp_pgr_hdr
.mo_control
;