2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * Memory Object Management.
56 #include "default_pager_internal.h"
57 #include <default_pager/default_pager_object_server.h>
58 #include <mach/memory_object_default_server.h>
59 #include <mach/memory_object_control.h>
60 #include <mach/memory_object_types.h>
61 #include <mach/memory_object_server.h>
63 #include <mach/vm_map.h>
64 #include <vm/memory_object.h>
65 #include <vm/vm_pageout.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_protos.h>
69 /* forward declaration */
70 vstruct_t
vs_object_create(vm_size_t size
);
73 * List of all vstructs. A specific vstruct is
74 * found directly via its port, this list is
75 * only used for monitoring purposes by the
76 * default_pager_object* calls and by ps_delete
77 * when abstract memory objects must be scanned
78 * to remove any live storage on a segment which
81 struct vstruct_list_head vstruct_list
;
83 __private_extern__
void
88 queue_enter(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
89 vstruct_list
.vsl_count
++;
94 __private_extern__
void
98 queue_remove(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
99 vstruct_list
.vsl_count
--;
103 * We use the sequence numbers on requests to regulate
104 * our parallelism. In general, we allow multiple reads and writes
105 * to proceed in parallel, with the exception that reads must
106 * wait for previous writes to finish. (Because the kernel might
107 * generate a data-request for a page on the heels of a data-write
108 * for the same page, and we must avoid returning stale data.)
109 * terminate requests wait for proceeding reads and writes to finish.
112 static unsigned int default_pager_total
= 0; /* debugging */
113 static unsigned int default_pager_wait_seqno
= 0; /* debugging */
114 static unsigned int default_pager_wait_read
= 0; /* debugging */
115 static unsigned int default_pager_wait_write
= 0; /* debugging */
117 __private_extern__
void
122 ASSERT(vs
->vs_async_pending
>= 0);
123 while (vs
->vs_async_pending
> 0) {
124 vs
->vs_waiting_async
= TRUE
;
125 assert_wait(&vs
->vs_async_pending
, THREAD_UNINT
);
127 thread_block(THREAD_CONTINUE_NULL
);
130 ASSERT(vs
->vs_async_pending
== 0);
136 * Waits for correct sequence number. Leaves pager locked.
138 * JMM - Sequence numbers guarantee ordering of requests generated
139 * by a single thread if the receiver is multithreaded and
140 * the interfaces are asynchronous (i.e. sender can generate
141 * more than one request before the first is received in the
142 * pager). Normally, IPC would generate these number in that
143 * case. But we are trying to avoid using IPC for the in-kernel
144 * scenario. Since these are actually invoked synchronously
145 * anyway (in-kernel), we can just fake the sequence number
146 * generation here (thus avoiding the dependence on IPC).
148 __private_extern__
void
152 mach_port_seqno_t seqno
;
154 default_pager_total
++;
157 seqno
= vs
->vs_next_seqno
++;
159 while (vs
->vs_seqno
!= seqno
) {
160 default_pager_wait_seqno
++;
161 vs
->vs_waiting_seqno
= TRUE
;
162 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
164 thread_block(THREAD_CONTINUE_NULL
);
170 * Increments sequence number and unlocks pager.
172 __private_extern__
void
173 vs_unlock(vstruct_t vs
)
176 if (vs
->vs_waiting_seqno
) {
177 vs
->vs_waiting_seqno
= FALSE
;
179 thread_wakeup(&vs
->vs_seqno
);
186 * Start a read - one more reader. Pager must be locked.
188 __private_extern__
void
196 * Wait for readers. Unlocks and relocks pager if wait needed.
198 __private_extern__
void
202 while (vs
->vs_readers
!= 0) {
203 default_pager_wait_read
++;
204 vs
->vs_waiting_read
= TRUE
;
205 assert_wait(&vs
->vs_readers
, THREAD_UNINT
);
207 thread_block(THREAD_CONTINUE_NULL
);
213 * Finish a read. Pager is unlocked and returns unlocked.
215 __private_extern__
void
220 if (--vs
->vs_readers
== 0 && vs
->vs_waiting_read
) {
221 vs
->vs_waiting_read
= FALSE
;
223 thread_wakeup(&vs
->vs_readers
);
230 * Start a write - one more writer. Pager must be locked.
232 __private_extern__
void
240 * Wait for writers. Unlocks and relocks pager if wait needed.
242 __private_extern__
void
246 while (vs
->vs_writers
!= 0) {
247 default_pager_wait_write
++;
248 vs
->vs_waiting_write
= TRUE
;
249 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
251 thread_block(THREAD_CONTINUE_NULL
);
257 /* This is to be used for the transfer from segment code ONLY */
258 /* The transfer code holds off vs destruction by keeping the */
259 /* vs_async_wait count non-zero. It will not ocnflict with */
260 /* other writers on an async basis because it only writes on */
261 /* a cluster basis into fresh (as of sync time) cluster locations */
263 __private_extern__
void
264 vs_wait_for_sync_writers(
267 while (vs
->vs_writers
!= 0) {
268 default_pager_wait_write
++;
269 vs
->vs_waiting_write
= TRUE
;
270 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
272 thread_block(THREAD_CONTINUE_NULL
);
279 * Finish a write. Pager is unlocked and returns unlocked.
281 __private_extern__
void
286 if (--vs
->vs_writers
== 0 && vs
->vs_waiting_write
) {
287 vs
->vs_waiting_write
= FALSE
;
289 thread_wakeup(&vs
->vs_writers
);
294 #endif /* PARALLEL */
303 * Allocate a vstruct. If there are any problems, then report them
306 vs
= ps_vstruct_create(size
);
307 if (vs
== VSTRUCT_NULL
) {
308 dprintf(("vs_object_create: unable to allocate %s\n",
309 "-- either run swapon command or reboot"));
317 void default_pager_add(vstruct_t
, boolean_t
); /* forward */
324 memory_object_t mem_obj
= vs
->vs_mem_obj
;
326 mach_port_mscount_t sync
;
327 mach_port_t previous
;
329 static char here
[] = "default_pager_add";
332 * The port currently has a make-send count of zero,
333 * because either we just created the port or we just
334 * received the port in a memory_object_create request.
338 /* possibly generate an immediate no-senders notification */
340 pset
= default_pager_internal_set
;
342 /* delay notification till send right is created */
344 pset
= default_pager_external_set
;
347 ipc_port_make_sonce(mem_obj
);
348 ip_lock(mem_obj
); /* unlocked in nsrequest below */
349 ipc_port_nsrequest(mem_obj
, sync
, mem_obj
, &previous
);
354 const struct memory_object_pager_ops default_pager_ops
= {
355 dp_memory_object_reference
,
356 dp_memory_object_deallocate
,
357 dp_memory_object_init
,
358 dp_memory_object_terminate
,
359 dp_memory_object_data_request
,
360 dp_memory_object_data_return
,
361 dp_memory_object_data_initialize
,
362 dp_memory_object_data_unlock
,
363 dp_memory_object_synchronize
,
364 dp_memory_object_unmap
,
369 dp_memory_object_init(
370 memory_object_t mem_obj
,
371 memory_object_control_t control
,
372 __unused vm_size_t pager_page_size
)
376 assert(pager_page_size
== vm_page_size
);
378 memory_object_control_reference(control
);
380 vs_lookup(mem_obj
, vs
);
383 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
384 Panic("bad request");
386 vs
->vs_control
= control
;
393 dp_memory_object_synchronize(
394 memory_object_t mem_obj
,
395 memory_object_offset_t offset
,
397 __unused vm_sync_t flags
)
401 vs_lookup(mem_obj
, vs
);
405 memory_object_synchronize_completed(vs
->vs_control
, offset
, length
);
411 dp_memory_object_unmap(
412 __unused memory_object_t mem_obj
)
414 panic("dp_memory_object_unmap");
420 dp_memory_object_terminate(
421 memory_object_t mem_obj
)
423 memory_object_control_t control
;
427 * control port is a receive right, not a send right.
430 vs_lookup(mem_obj
, vs
);
434 * Wait for read and write requests to terminate.
437 vs_wait_for_readers(vs
);
438 vs_wait_for_writers(vs
);
441 * After memory_object_terminate both memory_object_init
442 * and a no-senders notification are possible, so we need
443 * to clean up our reference to the memory_object_control
444 * to prepare for a new init.
447 control
= vs
->vs_control
;
448 vs
->vs_control
= MEMORY_OBJECT_CONTROL_NULL
;
450 /* a bit of special case ugliness here. Wakeup any waiting reads */
451 /* these data requests had to be removed from the seqno traffic */
452 /* based on a performance bottleneck with large memory objects */
453 /* the problem will right itself with the new component based */
454 /* synchronous interface. The new async will be able to return */
455 /* failure during its sync phase. In the mean time ... */
457 thread_wakeup(&vs
->vs_writers
);
458 thread_wakeup(&vs
->vs_async_pending
);
463 * Now we deallocate our reference on the control.
465 memory_object_control_deallocate(control
);
470 dp_memory_object_reference(
471 memory_object_t mem_obj
)
475 vs_lookup_safe(mem_obj
, vs
);
476 if (vs
== VSTRUCT_NULL
)
480 assert(vs
->vs_references
> 0);
486 dp_memory_object_deallocate(
487 memory_object_t mem_obj
)
490 mach_port_seqno_t seqno
;
493 * Because we don't give out multiple first references
494 * for a memory object, there can't be a race
495 * between getting a deallocate call and creating
496 * a new reference for the object.
499 vs_lookup_safe(mem_obj
, vs
);
500 if (vs
== VSTRUCT_NULL
)
504 if (--vs
->vs_references
> 0) {
509 seqno
= vs
->vs_next_seqno
++;
510 while (vs
->vs_seqno
!= seqno
) {
511 default_pager_wait_seqno
++;
512 vs
->vs_waiting_seqno
= TRUE
;
513 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
515 thread_block(THREAD_CONTINUE_NULL
);
519 vs_async_wait(vs
); /* wait for pending async IO */
521 /* do not delete the vs structure until the referencing pointers */
522 /* in the vstruct list have been expunged */
524 /* get VSL_LOCK out of order by using TRY mechanism */
525 while(!VSL_LOCK_TRY()) {
530 vs_async_wait(vs
); /* wait for pending async IO */
535 * We shouldn't get a deallocation call
536 * when the kernel has the object cached.
538 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
539 Panic("bad request");
542 * Unlock the pager (though there should be no one
547 /* Lock out paging segment removal for the duration of this */
548 /* call. We are vulnerable to losing a paging segment we rely */
549 /* on as soon as we remove ourselves from the VSL and unlock */
551 /* Keep our thread from blocking on attempt to trigger backing */
553 backing_store_release_trigger_disable
+= 1;
556 * Remove the memory object port association, and then
557 * the destroy the port itself. We must remove the object
558 * from the port list before deallocating the pager,
559 * because of default_pager_objects.
561 vstruct_list_delete(vs
);
564 ps_vstruct_dealloc(vs
);
567 backing_store_release_trigger_disable
-= 1;
568 if(backing_store_release_trigger_disable
== 0) {
569 thread_wakeup((event_t
)&backing_store_release_trigger_disable
);
575 dp_memory_object_data_request(
576 memory_object_t mem_obj
,
577 memory_object_offset_t offset
,
579 __unused vm_prot_t protection_required
)
583 GSTAT(global_stats
.gs_pagein_calls
++);
586 /* CDY at this moment vs_lookup panics when presented with the wrong */
587 /* port. As we are expanding this pager to support user interfaces */
588 /* this should be changed to return kern_failure */
589 vs_lookup(mem_obj
, vs
);
592 /* We are going to relax the strict sequencing here for performance */
593 /* reasons. We can do this because we know that the read and */
594 /* write threads are different and we rely on synchronization */
595 /* of read and write requests at the cache memory_object level */
596 /* break out wait_for_writers, all of this goes away when */
597 /* we get real control of seqno with the new component interface */
599 if (vs
->vs_writers
!= 0) {
600 /* you can't hold on to the seqno and go */
601 /* to sleep like that */
602 vs_unlock(vs
); /* bump internal count of seqno */
604 while (vs
->vs_writers
!= 0) {
605 default_pager_wait_write
++;
606 vs
->vs_waiting_write
= TRUE
;
607 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
609 thread_block(THREAD_CONTINUE_NULL
);
613 if(vs
->vs_control
== MEMORY_OBJECT_CONTROL_NULL
) {
625 * Request must be on a page boundary and a multiple of pages.
627 if ((offset
& vm_page_mask
) != 0 || (length
& vm_page_mask
) != 0)
628 Panic("bad alignment");
630 pvs_cluster_read(vs
, (vm_offset_t
)offset
, length
);
638 * memory_object_data_initialize: check whether we already have each page, and
639 * write it if we do not. The implementation is far from optimized, and
640 * also assumes that the default_pager is single-threaded.
642 /* It is questionable whether or not a pager should decide what is relevant */
643 /* and what is not in data sent from the kernel. Data initialize has been */
644 /* changed to copy back all data sent to it in preparation for its eventual */
645 /* merge with data return. It is the kernel that should decide what pages */
646 /* to write back. As of the writing of this note, this is indeed the case */
647 /* the kernel writes back one page at a time through this interface */
650 dp_memory_object_data_initialize(
651 memory_object_t mem_obj
,
652 memory_object_offset_t offset
,
657 DP_DEBUG(DEBUG_MO_EXTERNAL
,
658 ("mem_obj=0x%x,offset=0x%x,cnt=0x%x\n",
659 (int)mem_obj
, (int)offset
, (int)size
));
660 GSTAT(global_stats
.gs_pages_init
+= atop_32(size
));
662 vs_lookup(mem_obj
, vs
);
668 * Write the data via clustered writes. vs_cluster_write will
669 * loop if the address range specified crosses cluster
672 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
680 dp_memory_object_data_unlock(
681 __unused memory_object_t mem_obj
,
682 __unused memory_object_offset_t offset
,
683 __unused vm_size_t size
,
684 __unused vm_prot_t desired_access
)
686 Panic("dp_memory_object_data_unlock: illegal");
693 dp_memory_object_data_return(
694 memory_object_t mem_obj
,
695 memory_object_offset_t offset
,
697 __unused memory_object_offset_t
*resid_offset
,
698 __unused
int *io_error
,
699 __unused boolean_t dirty
,
700 __unused boolean_t kernel_copy
,
701 __unused
int upl_flags
)
705 DP_DEBUG(DEBUG_MO_EXTERNAL
,
706 ("mem_obj=0x%x,offset=0x%x,size=0x%x\n",
707 (int)mem_obj
, (int)offset
, (int)size
));
708 GSTAT(global_stats
.gs_pageout_calls
++);
710 /* This routine is called by the pageout thread. The pageout thread */
711 /* cannot be blocked by read activities unless the read activities */
712 /* Therefore the grant of vs lock must be done on a try versus a */
713 /* blocking basis. The code below relies on the fact that the */
714 /* interface is synchronous. Should this interface be again async */
715 /* for some type of pager in the future the pages will have to be */
716 /* returned through a separate, asynchronous path. */
718 vs_lookup(mem_obj
, vs
);
720 default_pager_total
++;
721 if(!VS_TRY_LOCK(vs
)) {
722 /* the call below will not be done by caller when we have */
723 /* a synchronous interface */
724 /* return KERN_LOCK_OWNED; */
726 unsigned int page_list_count
= 0;
727 memory_object_super_upl_request(vs
->vs_control
,
728 (memory_object_offset_t
)offset
,
730 &upl
, NULL
, &page_list_count
,
731 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
732 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
738 if ((vs
->vs_seqno
!= vs
->vs_next_seqno
++)
740 || (vs
->vs_xfer_pending
)) {
742 unsigned int page_list_count
= 0;
747 /* the call below will not be done by caller when we have */
748 /* a synchronous interface */
749 /* return KERN_LOCK_OWNED; */
750 memory_object_super_upl_request(vs
->vs_control
,
751 (memory_object_offset_t
)offset
,
753 &upl
, NULL
, &page_list_count
,
754 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
755 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
761 if ((size
% vm_page_size
) != 0)
762 Panic("bad alignment");
767 vs
->vs_async_pending
+= 1; /* protect from backing store contraction */
771 * Write the data via clustered writes. vs_cluster_write will
772 * loop if the address range specified crosses cluster
775 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
779 /* temporary, need a finer lock based on cluster */
782 vs
->vs_async_pending
-= 1; /* release vs_async_wait */
783 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
784 vs
->vs_waiting_async
= FALSE
;
786 thread_wakeup(&vs
->vs_async_pending
);
796 * Routine: default_pager_memory_object_create
798 * Handle requests for memory objects from the
801 * Because we only give out the default memory
802 * manager port to the kernel, we don't have to
803 * be so paranoid about the contents.
806 default_pager_memory_object_create(
807 __unused memory_object_default_t dmm
,
809 memory_object_t
*new_mem_obj
)
813 assert(dmm
== default_pager_object
);
815 vs
= vs_object_create(new_size
);
816 if (vs
== VSTRUCT_NULL
)
817 return KERN_RESOURCE_SHORTAGE
;
819 vs
->vs_next_seqno
= 0;
822 * Set up associations between this memory object
823 * and this default_pager structure
826 vs
->vs_pager_ops
= &default_pager_ops
;
827 vs
->vs_mem_obj_ikot
= IKOT_MEMORY_OBJECT
;
830 * After this, other threads might receive requests
831 * for this memory object or find it in the port list.
834 vstruct_list_insert(vs
);
835 *new_mem_obj
= vs_to_mem_obj(vs
);
840 * Create an external object.
843 default_pager_object_create(
844 default_pager_t default_pager
,
846 memory_object_t
*mem_objp
)
850 if (default_pager
!= default_pager_object
)
851 return KERN_INVALID_ARGUMENT
;
853 vs
= vs_object_create(size
);
854 if (vs
== VSTRUCT_NULL
)
855 return KERN_RESOURCE_SHORTAGE
;
858 * Set up associations between the default pager
859 * and this vstruct structure
861 vs
->vs_pager_ops
= &default_pager_ops
;
862 vstruct_list_insert(vs
);
863 *mem_objp
= vs_to_mem_obj(vs
);
868 default_pager_objects(
869 default_pager_t default_pager
,
870 default_pager_object_array_t
*objectsp
,
871 mach_msg_type_number_t
*ocountp
,
872 mach_port_array_t
*portsp
,
873 mach_msg_type_number_t
*pcountp
)
875 vm_offset_t oaddr
= 0; /* memory for objects */
876 vm_size_t osize
= 0; /* current size */
877 default_pager_object_t
* objects
;
878 unsigned int opotential
= 0;
880 vm_map_copy_t pcopy
= 0; /* copy handle for pagers */
881 vm_size_t psize
= 0; /* current size */
882 memory_object_t
* pagers
;
883 unsigned int ppotential
= 0;
886 unsigned int num_objects
;
890 if (default_pager
!= default_pager_object
)
891 return KERN_INVALID_ARGUMENT
;
894 * We will send no more than this many
896 actual
= vstruct_list
.vsl_count
;
899 * Out out-of-line port arrays are simply kalloc'ed.
901 psize
= round_page(actual
* sizeof * pagers
);
902 ppotential
= psize
/ sizeof * pagers
;
903 pagers
= (memory_object_t
*)kalloc(psize
);
905 return KERN_RESOURCE_SHORTAGE
;
908 * returned out of line data must be allocated out
909 * the ipc_kernel_map, wired down, filled in, and
910 * then "copied in" as if it had been sent by a
913 osize
= round_page(actual
* sizeof * objects
);
914 opotential
= osize
/ sizeof * objects
;
915 kr
= kmem_alloc(ipc_kernel_map
, &oaddr
, osize
);
916 if (KERN_SUCCESS
!= kr
) {
917 kfree(pagers
, psize
);
918 return KERN_RESOURCE_SHORTAGE
;
920 objects
= (default_pager_object_t
*)oaddr
;
930 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
, vs_links
) {
932 memory_object_t pager
;
935 if ((num_objects
>= opotential
) ||
936 (num_objects
>= ppotential
)) {
939 * This should be rare. In any case,
940 * we will only miss recent objects,
941 * because they are added at the end.
947 * Avoid interfering with normal operations
949 if (!VS_MAP_TRY_LOCK(entry
))
951 size
= ps_vstruct_allocated_size(entry
);
952 VS_MAP_UNLOCK(entry
);
957 * We need a reference for our caller. Adding this
958 * reference through the linked list could race with
959 * destruction of the object. If we find the object
960 * has no references, just give up on it.
963 if (entry
->vs_references
== 0) {
967 pager
= vs_to_mem_obj(entry
);
968 dp_memory_object_reference(pager
);
971 /* the arrays are wired, so no deadlock worries */
973 objects
[num_objects
].dpo_object
= (vm_offset_t
) entry
;
974 objects
[num_objects
].dpo_size
= size
;
975 pagers
[num_objects
++] = pager
;
980 * Do not return garbage
982 objects
[num_objects
].dpo_object
= (vm_offset_t
) 0;
983 objects
[num_objects
].dpo_size
= 0;
984 pagers
[num_objects
++] = MEMORY_OBJECT_NULL
;
990 /* clear out any excess allocation */
991 while (num_objects
< opotential
) {
992 objects
[--opotential
].dpo_object
= (vm_offset_t
) 0;
993 objects
[opotential
].dpo_size
= 0;
995 while (num_objects
< ppotential
) {
996 pagers
[--ppotential
] = MEMORY_OBJECT_NULL
;
999 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(oaddr
),
1000 vm_map_round_page(oaddr
+ osize
), FALSE
);
1001 assert(KERN_SUCCESS
== kr
);
1002 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)oaddr
,
1003 (vm_map_size_t
)osize
, TRUE
, &pcopy
);
1004 assert(KERN_SUCCESS
== kr
);
1006 *objectsp
= (default_pager_object_array_t
)objects
;
1007 *ocountp
= num_objects
;
1008 *portsp
= (mach_port_array_t
)pcopy
;
1009 *pcountp
= num_objects
;
1011 return KERN_SUCCESS
;
1015 default_pager_object_pages(
1016 default_pager_t default_pager
,
1017 mach_port_t memory_object
,
1018 default_pager_page_array_t
*pagesp
,
1019 mach_msg_type_number_t
*countp
)
1021 vm_offset_t addr
= 0; /* memory for page offsets */
1022 vm_size_t size
= 0; /* current memory size */
1024 default_pager_page_t
* pages
= 0;
1025 unsigned int potential
;
1026 unsigned int actual
;
1028 memory_object_t object
;
1030 if (default_pager
!= default_pager_object
)
1031 return KERN_INVALID_ARGUMENT
;
1033 object
= (memory_object_t
) memory_object
;
1040 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
,
1043 if (vs_to_mem_obj(entry
) == object
) {
1051 /* did not find the object */
1053 kmem_free(ipc_kernel_map
, addr
, size
);
1055 return KERN_INVALID_ARGUMENT
;
1059 if (!VS_MAP_TRY_LOCK(entry
)) {
1060 /* oh well bad luck */
1065 assert_wait_timeout((event_t
)assert_wait_timeout
, THREAD_UNINT
, 1, 1000*NSEC_PER_USEC
);
1066 wresult
= thread_block(THREAD_CONTINUE_NULL
);
1067 assert(wresult
== THREAD_TIMED_OUT
);
1071 actual
= ps_vstruct_allocated_pages(entry
, pages
, potential
);
1072 VS_MAP_UNLOCK(entry
);
1075 if (actual
<= potential
)
1078 /* allocate more memory */
1080 kmem_free(ipc_kernel_map
, addr
, size
);
1082 size
= round_page(actual
* sizeof * pages
);
1083 kr
= kmem_alloc(ipc_kernel_map
, &addr
, size
);
1084 if (KERN_SUCCESS
!= kr
)
1085 return KERN_RESOURCE_SHORTAGE
;
1087 pages
= (default_pager_page_t
*)addr
;
1088 potential
= size
/ sizeof * pages
;
1092 * Clear unused memory.
1094 while (actual
< potential
)
1095 pages
[--potential
].dpp_offset
= 0;
1097 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
1098 vm_map_round_page(addr
+ size
), FALSE
);
1099 assert(KERN_SUCCESS
== kr
);
1100 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
1101 (vm_map_size_t
)size
, TRUE
, ©
);
1102 assert(KERN_SUCCESS
== kr
);
1105 *pagesp
= (default_pager_page_array_t
)copy
;
1107 return KERN_SUCCESS
;