2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * Memory Object Management.
64 #include "default_pager_internal.h"
65 #include <default_pager/default_pager_object_server.h>
66 #include <mach/memory_object_default_server.h>
67 #include <mach/memory_object_control.h>
68 #include <mach/memory_object_types.h>
69 #include <mach/memory_object_server.h>
71 #include <mach/vm_map.h>
72 #include <vm/memory_object.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_protos.h>
77 /* forward declaration */
78 vstruct_t
vs_object_create(vm_size_t size
);
81 * List of all vstructs. A specific vstruct is
82 * found directly via its port, this list is
83 * only used for monitoring purposes by the
84 * default_pager_object* calls and by ps_delete
85 * when abstract memory objects must be scanned
86 * to remove any live storage on a segment which
89 struct vstruct_list_head vstruct_list
;
91 __private_extern__
void
96 queue_enter(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
97 vstruct_list
.vsl_count
++;
102 __private_extern__
void
106 queue_remove(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
107 vstruct_list
.vsl_count
--;
111 * We use the sequence numbers on requests to regulate
112 * our parallelism. In general, we allow multiple reads and writes
113 * to proceed in parallel, with the exception that reads must
114 * wait for previous writes to finish. (Because the kernel might
115 * generate a data-request for a page on the heels of a data-write
116 * for the same page, and we must avoid returning stale data.)
117 * terminate requests wait for proceeding reads and writes to finish.
120 static unsigned int default_pager_total
= 0; /* debugging */
121 static unsigned int default_pager_wait_seqno
= 0; /* debugging */
122 static unsigned int default_pager_wait_read
= 0; /* debugging */
123 static unsigned int default_pager_wait_write
= 0; /* debugging */
125 __private_extern__
void
130 ASSERT(vs
->vs_async_pending
>= 0);
131 while (vs
->vs_async_pending
> 0) {
132 vs
->vs_waiting_async
= TRUE
;
133 assert_wait(&vs
->vs_async_pending
, THREAD_UNINT
);
135 thread_block(THREAD_CONTINUE_NULL
);
138 ASSERT(vs
->vs_async_pending
== 0);
144 * Waits for correct sequence number. Leaves pager locked.
146 * JMM - Sequence numbers guarantee ordering of requests generated
147 * by a single thread if the receiver is multithreaded and
148 * the interfaces are asynchronous (i.e. sender can generate
149 * more than one request before the first is received in the
150 * pager). Normally, IPC would generate these number in that
151 * case. But we are trying to avoid using IPC for the in-kernel
152 * scenario. Since these are actually invoked synchronously
153 * anyway (in-kernel), we can just fake the sequence number
154 * generation here (thus avoiding the dependence on IPC).
156 __private_extern__
void
160 mach_port_seqno_t seqno
;
162 default_pager_total
++;
165 seqno
= vs
->vs_next_seqno
++;
167 while (vs
->vs_seqno
!= seqno
) {
168 default_pager_wait_seqno
++;
169 vs
->vs_waiting_seqno
= TRUE
;
170 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
172 thread_block(THREAD_CONTINUE_NULL
);
178 * Increments sequence number and unlocks pager.
180 __private_extern__
void
181 vs_unlock(vstruct_t vs
)
184 if (vs
->vs_waiting_seqno
) {
185 vs
->vs_waiting_seqno
= FALSE
;
187 thread_wakeup(&vs
->vs_seqno
);
194 * Start a read - one more reader. Pager must be locked.
196 __private_extern__
void
204 * Wait for readers. Unlocks and relocks pager if wait needed.
206 __private_extern__
void
210 while (vs
->vs_readers
!= 0) {
211 default_pager_wait_read
++;
212 vs
->vs_waiting_read
= TRUE
;
213 assert_wait(&vs
->vs_readers
, THREAD_UNINT
);
215 thread_block(THREAD_CONTINUE_NULL
);
221 * Finish a read. Pager is unlocked and returns unlocked.
223 __private_extern__
void
228 if (--vs
->vs_readers
== 0 && vs
->vs_waiting_read
) {
229 vs
->vs_waiting_read
= FALSE
;
231 thread_wakeup(&vs
->vs_readers
);
238 * Start a write - one more writer. Pager must be locked.
240 __private_extern__
void
248 * Wait for writers. Unlocks and relocks pager if wait needed.
250 __private_extern__
void
254 while (vs
->vs_writers
!= 0) {
255 default_pager_wait_write
++;
256 vs
->vs_waiting_write
= TRUE
;
257 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
259 thread_block(THREAD_CONTINUE_NULL
);
265 /* This is to be used for the transfer from segment code ONLY */
266 /* The transfer code holds off vs destruction by keeping the */
267 /* vs_async_wait count non-zero. It will not ocnflict with */
268 /* other writers on an async basis because it only writes on */
269 /* a cluster basis into fresh (as of sync time) cluster locations */
271 __private_extern__
void
272 vs_wait_for_sync_writers(
275 while (vs
->vs_writers
!= 0) {
276 default_pager_wait_write
++;
277 vs
->vs_waiting_write
= TRUE
;
278 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
280 thread_block(THREAD_CONTINUE_NULL
);
287 * Finish a write. Pager is unlocked and returns unlocked.
289 __private_extern__
void
294 if (--vs
->vs_writers
== 0 && vs
->vs_waiting_write
) {
295 vs
->vs_waiting_write
= FALSE
;
297 thread_wakeup(&vs
->vs_writers
);
302 #endif /* PARALLEL */
311 * Allocate a vstruct. If there are any problems, then report them
314 vs
= ps_vstruct_create(size
);
315 if (vs
== VSTRUCT_NULL
) {
316 dprintf(("vs_object_create: unable to allocate %s\n",
317 "-- either run swapon command or reboot"));
325 void default_pager_add(vstruct_t
, boolean_t
); /* forward */
332 memory_object_t mem_obj
= vs
->vs_mem_obj
;
334 mach_port_mscount_t sync
;
335 mach_port_t previous
;
337 static char here
[] = "default_pager_add";
340 * The port currently has a make-send count of zero,
341 * because either we just created the port or we just
342 * received the port in a memory_object_create request.
346 /* possibly generate an immediate no-senders notification */
348 pset
= default_pager_internal_set
;
350 /* delay notification till send right is created */
352 pset
= default_pager_external_set
;
355 ipc_port_make_sonce(mem_obj
);
356 ip_lock(mem_obj
); /* unlocked in nsrequest below */
357 ipc_port_nsrequest(mem_obj
, sync
, mem_obj
, &previous
);
362 const struct memory_object_pager_ops default_pager_ops
= {
363 dp_memory_object_reference
,
364 dp_memory_object_deallocate
,
365 dp_memory_object_init
,
366 dp_memory_object_terminate
,
367 dp_memory_object_data_request
,
368 dp_memory_object_data_return
,
369 dp_memory_object_data_initialize
,
370 dp_memory_object_data_unlock
,
371 dp_memory_object_synchronize
,
372 dp_memory_object_unmap
,
377 dp_memory_object_init(
378 memory_object_t mem_obj
,
379 memory_object_control_t control
,
380 __unused vm_size_t pager_page_size
)
384 assert(pager_page_size
== vm_page_size
);
386 memory_object_control_reference(control
);
388 vs_lookup(mem_obj
, vs
);
391 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
392 Panic("bad request");
394 vs
->vs_control
= control
;
401 dp_memory_object_synchronize(
402 memory_object_t mem_obj
,
403 memory_object_offset_t offset
,
405 __unused vm_sync_t flags
)
409 vs_lookup(mem_obj
, vs
);
413 memory_object_synchronize_completed(vs
->vs_control
, offset
, length
);
419 dp_memory_object_unmap(
420 __unused memory_object_t mem_obj
)
422 panic("dp_memory_object_unmap");
428 dp_memory_object_terminate(
429 memory_object_t mem_obj
)
431 memory_object_control_t control
;
435 * control port is a receive right, not a send right.
438 vs_lookup(mem_obj
, vs
);
442 * Wait for read and write requests to terminate.
445 vs_wait_for_readers(vs
);
446 vs_wait_for_writers(vs
);
449 * After memory_object_terminate both memory_object_init
450 * and a no-senders notification are possible, so we need
451 * to clean up our reference to the memory_object_control
452 * to prepare for a new init.
455 control
= vs
->vs_control
;
456 vs
->vs_control
= MEMORY_OBJECT_CONTROL_NULL
;
458 /* a bit of special case ugliness here. Wakeup any waiting reads */
459 /* these data requests had to be removed from the seqno traffic */
460 /* based on a performance bottleneck with large memory objects */
461 /* the problem will right itself with the new component based */
462 /* synchronous interface. The new async will be able to return */
463 /* failure during its sync phase. In the mean time ... */
465 thread_wakeup(&vs
->vs_writers
);
466 thread_wakeup(&vs
->vs_async_pending
);
471 * Now we deallocate our reference on the control.
473 memory_object_control_deallocate(control
);
478 dp_memory_object_reference(
479 memory_object_t mem_obj
)
483 vs_lookup_safe(mem_obj
, vs
);
484 if (vs
== VSTRUCT_NULL
)
488 assert(vs
->vs_references
> 0);
494 dp_memory_object_deallocate(
495 memory_object_t mem_obj
)
498 mach_port_seqno_t seqno
;
501 * Because we don't give out multiple first references
502 * for a memory object, there can't be a race
503 * between getting a deallocate call and creating
504 * a new reference for the object.
507 vs_lookup_safe(mem_obj
, vs
);
508 if (vs
== VSTRUCT_NULL
)
512 if (--vs
->vs_references
> 0) {
517 seqno
= vs
->vs_next_seqno
++;
518 while (vs
->vs_seqno
!= seqno
) {
519 default_pager_wait_seqno
++;
520 vs
->vs_waiting_seqno
= TRUE
;
521 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
523 thread_block(THREAD_CONTINUE_NULL
);
527 vs_async_wait(vs
); /* wait for pending async IO */
529 /* do not delete the vs structure until the referencing pointers */
530 /* in the vstruct list have been expunged */
532 /* get VSL_LOCK out of order by using TRY mechanism */
533 while(!VSL_LOCK_TRY()) {
538 vs_async_wait(vs
); /* wait for pending async IO */
543 * We shouldn't get a deallocation call
544 * when the kernel has the object cached.
546 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
547 Panic("bad request");
550 * Unlock the pager (though there should be no one
555 /* Lock out paging segment removal for the duration of this */
556 /* call. We are vulnerable to losing a paging segment we rely */
557 /* on as soon as we remove ourselves from the VSL and unlock */
559 /* Keep our thread from blocking on attempt to trigger backing */
561 backing_store_release_trigger_disable
+= 1;
564 * Remove the memory object port association, and then
565 * the destroy the port itself. We must remove the object
566 * from the port list before deallocating the pager,
567 * because of default_pager_objects.
569 vstruct_list_delete(vs
);
572 ps_vstruct_dealloc(vs
);
575 backing_store_release_trigger_disable
-= 1;
576 if(backing_store_release_trigger_disable
== 0) {
577 thread_wakeup((event_t
)&backing_store_release_trigger_disable
);
583 dp_memory_object_data_request(
584 memory_object_t mem_obj
,
585 memory_object_offset_t offset
,
587 __unused vm_prot_t protection_required
)
591 GSTAT(global_stats
.gs_pagein_calls
++);
594 /* CDY at this moment vs_lookup panics when presented with the wrong */
595 /* port. As we are expanding this pager to support user interfaces */
596 /* this should be changed to return kern_failure */
597 vs_lookup(mem_obj
, vs
);
600 /* We are going to relax the strict sequencing here for performance */
601 /* reasons. We can do this because we know that the read and */
602 /* write threads are different and we rely on synchronization */
603 /* of read and write requests at the cache memory_object level */
604 /* break out wait_for_writers, all of this goes away when */
605 /* we get real control of seqno with the new component interface */
607 if (vs
->vs_writers
!= 0) {
608 /* you can't hold on to the seqno and go */
609 /* to sleep like that */
610 vs_unlock(vs
); /* bump internal count of seqno */
612 while (vs
->vs_writers
!= 0) {
613 default_pager_wait_write
++;
614 vs
->vs_waiting_write
= TRUE
;
615 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
617 thread_block(THREAD_CONTINUE_NULL
);
621 if(vs
->vs_control
== MEMORY_OBJECT_CONTROL_NULL
) {
633 * Request must be on a page boundary and a multiple of pages.
635 if ((offset
& vm_page_mask
) != 0 || (length
& vm_page_mask
) != 0)
636 Panic("bad alignment");
638 pvs_cluster_read(vs
, (vm_offset_t
)offset
, length
);
646 * memory_object_data_initialize: check whether we already have each page, and
647 * write it if we do not. The implementation is far from optimized, and
648 * also assumes that the default_pager is single-threaded.
650 /* It is questionable whether or not a pager should decide what is relevant */
651 /* and what is not in data sent from the kernel. Data initialize has been */
652 /* changed to copy back all data sent to it in preparation for its eventual */
653 /* merge with data return. It is the kernel that should decide what pages */
654 /* to write back. As of the writing of this note, this is indeed the case */
655 /* the kernel writes back one page at a time through this interface */
658 dp_memory_object_data_initialize(
659 memory_object_t mem_obj
,
660 memory_object_offset_t offset
,
665 DP_DEBUG(DEBUG_MO_EXTERNAL
,
666 ("mem_obj=0x%x,offset=0x%x,cnt=0x%x\n",
667 (int)mem_obj
, (int)offset
, (int)size
));
668 GSTAT(global_stats
.gs_pages_init
+= atop_32(size
));
670 vs_lookup(mem_obj
, vs
);
676 * Write the data via clustered writes. vs_cluster_write will
677 * loop if the address range specified crosses cluster
680 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
688 dp_memory_object_data_unlock(
689 __unused memory_object_t mem_obj
,
690 __unused memory_object_offset_t offset
,
691 __unused vm_size_t size
,
692 __unused vm_prot_t desired_access
)
694 Panic("dp_memory_object_data_unlock: illegal");
701 dp_memory_object_data_return(
702 memory_object_t mem_obj
,
703 memory_object_offset_t offset
,
705 __unused memory_object_offset_t
*resid_offset
,
706 __unused
int *io_error
,
707 __unused boolean_t dirty
,
708 __unused boolean_t kernel_copy
,
709 __unused
int upl_flags
)
713 DP_DEBUG(DEBUG_MO_EXTERNAL
,
714 ("mem_obj=0x%x,offset=0x%x,size=0x%x\n",
715 (int)mem_obj
, (int)offset
, (int)size
));
716 GSTAT(global_stats
.gs_pageout_calls
++);
718 /* This routine is called by the pageout thread. The pageout thread */
719 /* cannot be blocked by read activities unless the read activities */
720 /* Therefore the grant of vs lock must be done on a try versus a */
721 /* blocking basis. The code below relies on the fact that the */
722 /* interface is synchronous. Should this interface be again async */
723 /* for some type of pager in the future the pages will have to be */
724 /* returned through a separate, asynchronous path. */
726 vs_lookup(mem_obj
, vs
);
728 default_pager_total
++;
729 if(!VS_TRY_LOCK(vs
)) {
730 /* the call below will not be done by caller when we have */
731 /* a synchronous interface */
732 /* return KERN_LOCK_OWNED; */
734 unsigned int page_list_count
= 0;
735 memory_object_super_upl_request(vs
->vs_control
,
736 (memory_object_offset_t
)offset
,
738 &upl
, NULL
, &page_list_count
,
739 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
740 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
746 if ((vs
->vs_seqno
!= vs
->vs_next_seqno
++)
748 || (vs
->vs_xfer_pending
)) {
750 unsigned int page_list_count
= 0;
755 /* the call below will not be done by caller when we have */
756 /* a synchronous interface */
757 /* return KERN_LOCK_OWNED; */
758 memory_object_super_upl_request(vs
->vs_control
,
759 (memory_object_offset_t
)offset
,
761 &upl
, NULL
, &page_list_count
,
762 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
763 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
769 if ((size
% vm_page_size
) != 0)
770 Panic("bad alignment");
775 vs
->vs_async_pending
+= 1; /* protect from backing store contraction */
779 * Write the data via clustered writes. vs_cluster_write will
780 * loop if the address range specified crosses cluster
783 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
787 /* temporary, need a finer lock based on cluster */
790 vs
->vs_async_pending
-= 1; /* release vs_async_wait */
791 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
792 vs
->vs_waiting_async
= FALSE
;
794 thread_wakeup(&vs
->vs_async_pending
);
804 * Routine: default_pager_memory_object_create
806 * Handle requests for memory objects from the
809 * Because we only give out the default memory
810 * manager port to the kernel, we don't have to
811 * be so paranoid about the contents.
814 default_pager_memory_object_create(
815 __unused memory_object_default_t dmm
,
817 memory_object_t
*new_mem_obj
)
821 assert(dmm
== default_pager_object
);
823 vs
= vs_object_create(new_size
);
824 if (vs
== VSTRUCT_NULL
)
825 return KERN_RESOURCE_SHORTAGE
;
827 vs
->vs_next_seqno
= 0;
830 * Set up associations between this memory object
831 * and this default_pager structure
834 vs
->vs_pager_ops
= &default_pager_ops
;
835 vs
->vs_mem_obj_ikot
= IKOT_MEMORY_OBJECT
;
838 * After this, other threads might receive requests
839 * for this memory object or find it in the port list.
842 vstruct_list_insert(vs
);
843 *new_mem_obj
= vs_to_mem_obj(vs
);
848 * Create an external object.
851 default_pager_object_create(
852 default_pager_t default_pager
,
854 memory_object_t
*mem_objp
)
858 if (default_pager
!= default_pager_object
)
859 return KERN_INVALID_ARGUMENT
;
861 vs
= vs_object_create(size
);
862 if (vs
== VSTRUCT_NULL
)
863 return KERN_RESOURCE_SHORTAGE
;
866 * Set up associations between the default pager
867 * and this vstruct structure
869 vs
->vs_pager_ops
= &default_pager_ops
;
870 vstruct_list_insert(vs
);
871 *mem_objp
= vs_to_mem_obj(vs
);
876 default_pager_objects(
877 default_pager_t default_pager
,
878 default_pager_object_array_t
*objectsp
,
879 mach_msg_type_number_t
*ocountp
,
880 mach_port_array_t
*portsp
,
881 mach_msg_type_number_t
*pcountp
)
883 vm_offset_t oaddr
= 0; /* memory for objects */
884 vm_size_t osize
= 0; /* current size */
885 default_pager_object_t
* objects
;
886 unsigned int opotential
= 0;
888 vm_map_copy_t pcopy
= 0; /* copy handle for pagers */
889 vm_size_t psize
= 0; /* current size */
890 memory_object_t
* pagers
;
891 unsigned int ppotential
= 0;
894 unsigned int num_objects
;
898 if (default_pager
!= default_pager_object
)
899 return KERN_INVALID_ARGUMENT
;
902 * We will send no more than this many
904 actual
= vstruct_list
.vsl_count
;
907 * Out out-of-line port arrays are simply kalloc'ed.
909 psize
= round_page(actual
* sizeof * pagers
);
910 ppotential
= psize
/ sizeof * pagers
;
911 pagers
= (memory_object_t
*)kalloc(psize
);
913 return KERN_RESOURCE_SHORTAGE
;
916 * returned out of line data must be allocated out
917 * the ipc_kernel_map, wired down, filled in, and
918 * then "copied in" as if it had been sent by a
921 osize
= round_page(actual
* sizeof * objects
);
922 opotential
= osize
/ sizeof * objects
;
923 kr
= kmem_alloc(ipc_kernel_map
, &oaddr
, osize
);
924 if (KERN_SUCCESS
!= kr
) {
925 kfree(pagers
, psize
);
926 return KERN_RESOURCE_SHORTAGE
;
928 objects
= (default_pager_object_t
*)oaddr
;
938 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
, vs_links
) {
940 memory_object_t pager
;
943 if ((num_objects
>= opotential
) ||
944 (num_objects
>= ppotential
)) {
947 * This should be rare. In any case,
948 * we will only miss recent objects,
949 * because they are added at the end.
955 * Avoid interfering with normal operations
957 if (!VS_MAP_TRY_LOCK(entry
))
959 size
= ps_vstruct_allocated_size(entry
);
960 VS_MAP_UNLOCK(entry
);
965 * We need a reference for our caller. Adding this
966 * reference through the linked list could race with
967 * destruction of the object. If we find the object
968 * has no references, just give up on it.
971 if (entry
->vs_references
== 0) {
975 pager
= vs_to_mem_obj(entry
);
976 dp_memory_object_reference(pager
);
979 /* the arrays are wired, so no deadlock worries */
981 objects
[num_objects
].dpo_object
= (vm_offset_t
) entry
;
982 objects
[num_objects
].dpo_size
= size
;
983 pagers
[num_objects
++] = pager
;
988 * Do not return garbage
990 objects
[num_objects
].dpo_object
= (vm_offset_t
) 0;
991 objects
[num_objects
].dpo_size
= 0;
992 pagers
[num_objects
++] = MEMORY_OBJECT_NULL
;
998 /* clear out any excess allocation */
999 while (num_objects
< opotential
) {
1000 objects
[--opotential
].dpo_object
= (vm_offset_t
) 0;
1001 objects
[opotential
].dpo_size
= 0;
1003 while (num_objects
< ppotential
) {
1004 pagers
[--ppotential
] = MEMORY_OBJECT_NULL
;
1007 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(oaddr
),
1008 vm_map_round_page(oaddr
+ osize
), FALSE
);
1009 assert(KERN_SUCCESS
== kr
);
1010 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)oaddr
,
1011 (vm_map_size_t
)osize
, TRUE
, &pcopy
);
1012 assert(KERN_SUCCESS
== kr
);
1014 *objectsp
= (default_pager_object_array_t
)objects
;
1015 *ocountp
= num_objects
;
1016 *portsp
= (mach_port_array_t
)pcopy
;
1017 *pcountp
= num_objects
;
1019 return KERN_SUCCESS
;
1023 default_pager_object_pages(
1024 default_pager_t default_pager
,
1025 mach_port_t memory_object
,
1026 default_pager_page_array_t
*pagesp
,
1027 mach_msg_type_number_t
*countp
)
1029 vm_offset_t addr
= 0; /* memory for page offsets */
1030 vm_size_t size
= 0; /* current memory size */
1032 default_pager_page_t
* pages
= 0;
1033 unsigned int potential
;
1034 unsigned int actual
;
1036 memory_object_t object
;
1038 if (default_pager
!= default_pager_object
)
1039 return KERN_INVALID_ARGUMENT
;
1041 object
= (memory_object_t
) memory_object
;
1048 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
,
1051 if (vs_to_mem_obj(entry
) == object
) {
1059 /* did not find the object */
1061 kmem_free(ipc_kernel_map
, addr
, size
);
1063 return KERN_INVALID_ARGUMENT
;
1067 if (!VS_MAP_TRY_LOCK(entry
)) {
1068 /* oh well bad luck */
1073 assert_wait_timeout((event_t
)assert_wait_timeout
, THREAD_UNINT
, 1, 1000*NSEC_PER_USEC
);
1074 wresult
= thread_block(THREAD_CONTINUE_NULL
);
1075 assert(wresult
== THREAD_TIMED_OUT
);
1079 actual
= ps_vstruct_allocated_pages(entry
, pages
, potential
);
1080 VS_MAP_UNLOCK(entry
);
1083 if (actual
<= potential
)
1086 /* allocate more memory */
1088 kmem_free(ipc_kernel_map
, addr
, size
);
1090 size
= round_page(actual
* sizeof * pages
);
1091 kr
= kmem_alloc(ipc_kernel_map
, &addr
, size
);
1092 if (KERN_SUCCESS
!= kr
)
1093 return KERN_RESOURCE_SHORTAGE
;
1095 pages
= (default_pager_page_t
*)addr
;
1096 potential
= size
/ sizeof * pages
;
1100 * Clear unused memory.
1102 while (actual
< potential
)
1103 pages
[--potential
].dpp_offset
= 0;
1105 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
1106 vm_map_round_page(addr
+ size
), FALSE
);
1107 assert(KERN_SUCCESS
== kr
);
1108 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
1109 (vm_map_size_t
)size
, TRUE
, ©
);
1110 assert(KERN_SUCCESS
== kr
);
1113 *pagesp
= (default_pager_page_array_t
)copy
;
1115 return KERN_SUCCESS
;