2 * Copyright (c) 2000-2006 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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * Memory Object Management.
62 #include "default_pager_internal.h"
63 #include <default_pager/default_pager_object_server.h>
64 #include <mach/memory_object_default_server.h>
65 #include <mach/memory_object_control.h>
66 #include <mach/memory_object_types.h>
67 #include <mach/memory_object_server.h>
69 #include <mach/vm_map.h>
70 #include <vm/memory_object.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_protos.h>
75 /* forward declaration */
76 vstruct_t
vs_object_create(vm_size_t size
);
79 * List of all vstructs. A specific vstruct is
80 * found directly via its port, this list is
81 * only used for monitoring purposes by the
82 * default_pager_object* calls and by ps_delete
83 * when abstract memory objects must be scanned
84 * to remove any live storage on a segment which
87 struct vstruct_list_head vstruct_list
;
89 __private_extern__
void
94 queue_enter(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
95 vstruct_list
.vsl_count
++;
100 __private_extern__
void
104 queue_remove(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
105 vstruct_list
.vsl_count
--;
109 * We use the sequence numbers on requests to regulate
110 * our parallelism. In general, we allow multiple reads and writes
111 * to proceed in parallel, with the exception that reads must
112 * wait for previous writes to finish. (Because the kernel might
113 * generate a data-request for a page on the heels of a data-write
114 * for the same page, and we must avoid returning stale data.)
115 * terminate requests wait for proceeding reads and writes to finish.
118 static unsigned int default_pager_total
= 0; /* debugging */
119 static unsigned int default_pager_wait_seqno
= 0; /* debugging */
120 static unsigned int default_pager_wait_read
= 0; /* debugging */
121 static unsigned int default_pager_wait_write
= 0; /* debugging */
123 __private_extern__
void
128 ASSERT(vs
->vs_async_pending
>= 0);
129 while (vs
->vs_async_pending
> 0) {
130 vs
->vs_waiting_async
= TRUE
;
131 assert_wait(&vs
->vs_async_pending
, THREAD_UNINT
);
133 thread_block(THREAD_CONTINUE_NULL
);
136 ASSERT(vs
->vs_async_pending
== 0);
142 * Waits for correct sequence number. Leaves pager locked.
144 * JMM - Sequence numbers guarantee ordering of requests generated
145 * by a single thread if the receiver is multithreaded and
146 * the interfaces are asynchronous (i.e. sender can generate
147 * more than one request before the first is received in the
148 * pager). Normally, IPC would generate these number in that
149 * case. But we are trying to avoid using IPC for the in-kernel
150 * scenario. Since these are actually invoked synchronously
151 * anyway (in-kernel), we can just fake the sequence number
152 * generation here (thus avoiding the dependence on IPC).
154 __private_extern__
void
158 mach_port_seqno_t seqno
;
160 default_pager_total
++;
163 seqno
= vs
->vs_next_seqno
++;
165 while (vs
->vs_seqno
!= seqno
) {
166 default_pager_wait_seqno
++;
167 vs
->vs_waiting_seqno
= TRUE
;
168 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
170 thread_block(THREAD_CONTINUE_NULL
);
176 * Increments sequence number and unlocks pager.
178 __private_extern__
void
179 vs_unlock(vstruct_t vs
)
182 if (vs
->vs_waiting_seqno
) {
183 vs
->vs_waiting_seqno
= FALSE
;
185 thread_wakeup(&vs
->vs_seqno
);
192 * Start a read - one more reader. Pager must be locked.
194 __private_extern__
void
202 * Wait for readers. Unlocks and relocks pager if wait needed.
204 __private_extern__
void
208 while (vs
->vs_readers
!= 0) {
209 default_pager_wait_read
++;
210 vs
->vs_waiting_read
= TRUE
;
211 assert_wait(&vs
->vs_readers
, THREAD_UNINT
);
213 thread_block(THREAD_CONTINUE_NULL
);
219 * Finish a read. Pager is unlocked and returns unlocked.
221 __private_extern__
void
226 if (--vs
->vs_readers
== 0 && vs
->vs_waiting_read
) {
227 vs
->vs_waiting_read
= FALSE
;
229 thread_wakeup(&vs
->vs_readers
);
236 * Start a write - one more writer. Pager must be locked.
238 __private_extern__
void
246 * Wait for writers. Unlocks and relocks pager if wait needed.
248 __private_extern__
void
252 while (vs
->vs_writers
!= 0) {
253 default_pager_wait_write
++;
254 vs
->vs_waiting_write
= TRUE
;
255 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
257 thread_block(THREAD_CONTINUE_NULL
);
263 /* This is to be used for the transfer from segment code ONLY */
264 /* The transfer code holds off vs destruction by keeping the */
265 /* vs_async_wait count non-zero. It will not ocnflict with */
266 /* other writers on an async basis because it only writes on */
267 /* a cluster basis into fresh (as of sync time) cluster locations */
269 __private_extern__
void
270 vs_wait_for_sync_writers(
273 while (vs
->vs_writers
!= 0) {
274 default_pager_wait_write
++;
275 vs
->vs_waiting_write
= TRUE
;
276 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
278 thread_block(THREAD_CONTINUE_NULL
);
285 * Finish a write. Pager is unlocked and returns unlocked.
287 __private_extern__
void
292 if (--vs
->vs_writers
== 0 && vs
->vs_waiting_write
) {
293 vs
->vs_waiting_write
= FALSE
;
295 thread_wakeup(&vs
->vs_writers
);
300 #endif /* PARALLEL */
309 * Allocate a vstruct. If there are any problems, then report them
312 vs
= ps_vstruct_create(size
);
313 if (vs
== VSTRUCT_NULL
) {
314 dprintf(("vs_object_create: unable to allocate %s\n",
315 "-- either run swapon command or reboot"));
323 void default_pager_add(vstruct_t
, boolean_t
); /* forward */
330 memory_object_t mem_obj
= vs
->vs_mem_obj
;
332 mach_port_mscount_t sync
;
333 mach_port_t previous
;
335 static char here
[] = "default_pager_add";
338 * The port currently has a make-send count of zero,
339 * because either we just created the port or we just
340 * received the port in a memory_object_create request.
344 /* possibly generate an immediate no-senders notification */
346 pset
= default_pager_internal_set
;
348 /* delay notification till send right is created */
350 pset
= default_pager_external_set
;
353 ipc_port_make_sonce(mem_obj
);
354 ip_lock(mem_obj
); /* unlocked in nsrequest below */
355 ipc_port_nsrequest(mem_obj
, sync
, mem_obj
, &previous
);
360 const struct memory_object_pager_ops default_pager_ops
= {
361 dp_memory_object_reference
,
362 dp_memory_object_deallocate
,
363 dp_memory_object_init
,
364 dp_memory_object_terminate
,
365 dp_memory_object_data_request
,
366 dp_memory_object_data_return
,
367 dp_memory_object_data_initialize
,
368 dp_memory_object_data_unlock
,
369 dp_memory_object_synchronize
,
370 dp_memory_object_unmap
,
375 dp_memory_object_init(
376 memory_object_t mem_obj
,
377 memory_object_control_t control
,
378 __unused vm_size_t pager_page_size
)
382 assert(pager_page_size
== vm_page_size
);
384 memory_object_control_reference(control
);
386 vs_lookup(mem_obj
, vs
);
389 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
390 Panic("bad request");
392 vs
->vs_control
= control
;
399 dp_memory_object_synchronize(
400 memory_object_t mem_obj
,
401 memory_object_offset_t offset
,
403 __unused vm_sync_t flags
)
407 vs_lookup(mem_obj
, vs
);
411 memory_object_synchronize_completed(vs
->vs_control
, offset
, length
);
417 dp_memory_object_unmap(
418 __unused memory_object_t mem_obj
)
420 panic("dp_memory_object_unmap");
426 dp_memory_object_terminate(
427 memory_object_t mem_obj
)
429 memory_object_control_t control
;
433 * control port is a receive right, not a send right.
436 vs_lookup(mem_obj
, vs
);
440 * Wait for read and write requests to terminate.
443 vs_wait_for_readers(vs
);
444 vs_wait_for_writers(vs
);
447 * After memory_object_terminate both memory_object_init
448 * and a no-senders notification are possible, so we need
449 * to clean up our reference to the memory_object_control
450 * to prepare for a new init.
453 control
= vs
->vs_control
;
454 vs
->vs_control
= MEMORY_OBJECT_CONTROL_NULL
;
456 /* a bit of special case ugliness here. Wakeup any waiting reads */
457 /* these data requests had to be removed from the seqno traffic */
458 /* based on a performance bottleneck with large memory objects */
459 /* the problem will right itself with the new component based */
460 /* synchronous interface. The new async will be able to return */
461 /* failure during its sync phase. In the mean time ... */
463 thread_wakeup(&vs
->vs_writers
);
464 thread_wakeup(&vs
->vs_async_pending
);
469 * Now we deallocate our reference on the control.
471 memory_object_control_deallocate(control
);
476 dp_memory_object_reference(
477 memory_object_t mem_obj
)
481 vs_lookup_safe(mem_obj
, vs
);
482 if (vs
== VSTRUCT_NULL
)
486 assert(vs
->vs_references
> 0);
492 dp_memory_object_deallocate(
493 memory_object_t mem_obj
)
496 mach_port_seqno_t seqno
;
499 * Because we don't give out multiple first references
500 * for a memory object, there can't be a race
501 * between getting a deallocate call and creating
502 * a new reference for the object.
505 vs_lookup_safe(mem_obj
, vs
);
506 if (vs
== VSTRUCT_NULL
)
510 if (--vs
->vs_references
> 0) {
515 seqno
= vs
->vs_next_seqno
++;
516 while (vs
->vs_seqno
!= seqno
) {
517 default_pager_wait_seqno
++;
518 vs
->vs_waiting_seqno
= TRUE
;
519 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
521 thread_block(THREAD_CONTINUE_NULL
);
525 vs_async_wait(vs
); /* wait for pending async IO */
527 /* do not delete the vs structure until the referencing pointers */
528 /* in the vstruct list have been expunged */
530 /* get VSL_LOCK out of order by using TRY mechanism */
531 while(!VSL_LOCK_TRY()) {
536 vs_async_wait(vs
); /* wait for pending async IO */
541 * We shouldn't get a deallocation call
542 * when the kernel has the object cached.
544 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
545 Panic("bad request");
548 * Unlock the pager (though there should be no one
553 /* Lock out paging segment removal for the duration of this */
554 /* call. We are vulnerable to losing a paging segment we rely */
555 /* on as soon as we remove ourselves from the VSL and unlock */
557 /* Keep our thread from blocking on attempt to trigger backing */
559 backing_store_release_trigger_disable
+= 1;
562 * Remove the memory object port association, and then
563 * the destroy the port itself. We must remove the object
564 * from the port list before deallocating the pager,
565 * because of default_pager_objects.
567 vstruct_list_delete(vs
);
570 ps_vstruct_dealloc(vs
);
573 backing_store_release_trigger_disable
-= 1;
574 if(backing_store_release_trigger_disable
== 0) {
575 thread_wakeup((event_t
)&backing_store_release_trigger_disable
);
581 dp_memory_object_data_request(
582 memory_object_t mem_obj
,
583 memory_object_offset_t offset
,
585 __unused vm_prot_t protection_required
,
586 memory_object_fault_info_t fault_info
)
590 GSTAT(global_stats
.gs_pagein_calls
++);
593 /* CDY at this moment vs_lookup panics when presented with the wrong */
594 /* port. As we are expanding this pager to support user interfaces */
595 /* this should be changed to return kern_failure */
596 vs_lookup(mem_obj
, vs
);
599 /* We are going to relax the strict sequencing here for performance */
600 /* reasons. We can do this because we know that the read and */
601 /* write threads are different and we rely on synchronization */
602 /* of read and write requests at the cache memory_object level */
603 /* break out wait_for_writers, all of this goes away when */
604 /* we get real control of seqno with the new component interface */
606 if (vs
->vs_writers
!= 0) {
607 /* you can't hold on to the seqno and go */
608 /* to sleep like that */
609 vs_unlock(vs
); /* bump internal count of seqno */
611 while (vs
->vs_writers
!= 0) {
612 default_pager_wait_write
++;
613 vs
->vs_waiting_write
= TRUE
;
614 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
616 thread_block(THREAD_CONTINUE_NULL
);
620 if(vs
->vs_control
== MEMORY_OBJECT_CONTROL_NULL
) {
632 * Request must be on a page boundary and a multiple of pages.
634 if ((offset
& vm_page_mask
) != 0 || (length
& vm_page_mask
) != 0)
635 Panic("bad alignment");
637 pvs_cluster_read(vs
, (vm_offset_t
)offset
, length
, fault_info
);
645 * memory_object_data_initialize: check whether we already have each page, and
646 * write it if we do not. The implementation is far from optimized, and
647 * also assumes that the default_pager is single-threaded.
649 /* It is questionable whether or not a pager should decide what is relevant */
650 /* and what is not in data sent from the kernel. Data initialize has been */
651 /* changed to copy back all data sent to it in preparation for its eventual */
652 /* merge with data return. It is the kernel that should decide what pages */
653 /* to write back. As of the writing of this note, this is indeed the case */
654 /* the kernel writes back one page at a time through this interface */
657 dp_memory_object_data_initialize(
658 memory_object_t mem_obj
,
659 memory_object_offset_t offset
,
664 DP_DEBUG(DEBUG_MO_EXTERNAL
,
665 ("mem_obj=0x%x,offset=0x%x,cnt=0x%x\n",
666 (int)mem_obj
, (int)offset
, (int)size
));
667 GSTAT(global_stats
.gs_pages_init
+= atop_32(size
));
669 vs_lookup(mem_obj
, vs
);
675 * Write the data via clustered writes. vs_cluster_write will
676 * loop if the address range specified crosses cluster
679 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
687 dp_memory_object_data_unlock(
688 __unused memory_object_t mem_obj
,
689 __unused memory_object_offset_t offset
,
690 __unused vm_size_t size
,
691 __unused vm_prot_t desired_access
)
693 Panic("dp_memory_object_data_unlock: illegal");
700 dp_memory_object_data_return(
701 memory_object_t mem_obj
,
702 memory_object_offset_t offset
,
704 __unused memory_object_offset_t
*resid_offset
,
705 __unused
int *io_error
,
706 __unused boolean_t dirty
,
707 __unused boolean_t kernel_copy
,
708 __unused
int upl_flags
)
712 DP_DEBUG(DEBUG_MO_EXTERNAL
,
713 ("mem_obj=0x%x,offset=0x%x,size=0x%x\n",
714 (int)mem_obj
, (int)offset
, (int)size
));
715 GSTAT(global_stats
.gs_pageout_calls
++);
717 /* This routine is called by the pageout thread. The pageout thread */
718 /* cannot be blocked by read activities unless the read activities */
719 /* Therefore the grant of vs lock must be done on a try versus a */
720 /* blocking basis. The code below relies on the fact that the */
721 /* interface is synchronous. Should this interface be again async */
722 /* for some type of pager in the future the pages will have to be */
723 /* returned through a separate, asynchronous path. */
725 vs_lookup(mem_obj
, vs
);
727 default_pager_total
++;
728 if(!VS_TRY_LOCK(vs
)) {
729 /* the call below will not be done by caller when we have */
730 /* a synchronous interface */
731 /* return KERN_LOCK_OWNED; */
733 unsigned int page_list_count
= 0;
734 memory_object_super_upl_request(vs
->vs_control
,
735 (memory_object_offset_t
)offset
,
737 &upl
, NULL
, &page_list_count
,
738 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
739 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
745 if ((vs
->vs_seqno
!= vs
->vs_next_seqno
++)
747 || (vs
->vs_xfer_pending
)) {
749 unsigned int page_list_count
= 0;
754 /* the call below will not be done by caller when we have */
755 /* a synchronous interface */
756 /* return KERN_LOCK_OWNED; */
757 memory_object_super_upl_request(vs
->vs_control
,
758 (memory_object_offset_t
)offset
,
760 &upl
, NULL
, &page_list_count
,
761 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
762 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
768 if ((size
% vm_page_size
) != 0)
769 Panic("bad alignment");
774 vs
->vs_async_pending
+= 1; /* protect from backing store contraction */
778 * Write the data via clustered writes. vs_cluster_write will
779 * loop if the address range specified crosses cluster
782 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
786 /* temporary, need a finer lock based on cluster */
789 vs
->vs_async_pending
-= 1; /* release vs_async_wait */
790 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
791 vs
->vs_waiting_async
= FALSE
;
793 thread_wakeup(&vs
->vs_async_pending
);
803 * Routine: default_pager_memory_object_create
805 * Handle requests for memory objects from the
808 * Because we only give out the default memory
809 * manager port to the kernel, we don't have to
810 * be so paranoid about the contents.
813 default_pager_memory_object_create(
814 __unused memory_object_default_t dmm
,
816 memory_object_t
*new_mem_obj
)
820 assert(dmm
== default_pager_object
);
822 vs
= vs_object_create(new_size
);
823 if (vs
== VSTRUCT_NULL
)
824 return KERN_RESOURCE_SHORTAGE
;
826 vs
->vs_next_seqno
= 0;
829 * Set up associations between this memory object
830 * and this default_pager structure
833 vs
->vs_pager_ops
= &default_pager_ops
;
834 vs
->vs_mem_obj_ikot
= IKOT_MEMORY_OBJECT
;
837 * After this, other threads might receive requests
838 * for this memory object or find it in the port list.
841 vstruct_list_insert(vs
);
842 *new_mem_obj
= vs_to_mem_obj(vs
);
847 * Create an external object.
850 default_pager_object_create(
851 default_pager_t default_pager
,
853 memory_object_t
*mem_objp
)
857 if (default_pager
!= default_pager_object
)
858 return KERN_INVALID_ARGUMENT
;
860 vs
= vs_object_create(size
);
861 if (vs
== VSTRUCT_NULL
)
862 return KERN_RESOURCE_SHORTAGE
;
865 * Set up associations between the default pager
866 * and this vstruct structure
868 vs
->vs_pager_ops
= &default_pager_ops
;
869 vstruct_list_insert(vs
);
870 *mem_objp
= vs_to_mem_obj(vs
);
875 default_pager_objects(
876 default_pager_t default_pager
,
877 default_pager_object_array_t
*objectsp
,
878 mach_msg_type_number_t
*ocountp
,
879 mach_port_array_t
*portsp
,
880 mach_msg_type_number_t
*pcountp
)
882 vm_offset_t oaddr
= 0; /* memory for objects */
883 vm_size_t osize
= 0; /* current size */
884 default_pager_object_t
* objects
;
885 unsigned int opotential
= 0;
887 vm_map_copy_t pcopy
= 0; /* copy handle for pagers */
888 vm_size_t psize
= 0; /* current size */
889 memory_object_t
* pagers
;
890 unsigned int ppotential
= 0;
893 unsigned int num_objects
;
897 if (default_pager
!= default_pager_object
)
898 return KERN_INVALID_ARGUMENT
;
901 * We will send no more than this many
903 actual
= vstruct_list
.vsl_count
;
906 * Out out-of-line port arrays are simply kalloc'ed.
908 psize
= round_page(actual
* sizeof * pagers
);
909 ppotential
= psize
/ sizeof * pagers
;
910 pagers
= (memory_object_t
*)kalloc(psize
);
912 return KERN_RESOURCE_SHORTAGE
;
915 * returned out of line data must be allocated out
916 * the ipc_kernel_map, wired down, filled in, and
917 * then "copied in" as if it had been sent by a
920 osize
= round_page(actual
* sizeof * objects
);
921 opotential
= osize
/ sizeof * objects
;
922 kr
= kmem_alloc(ipc_kernel_map
, &oaddr
, osize
);
923 if (KERN_SUCCESS
!= kr
) {
924 kfree(pagers
, psize
);
925 return KERN_RESOURCE_SHORTAGE
;
927 objects
= (default_pager_object_t
*)oaddr
;
937 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
, vs_links
) {
939 memory_object_t pager
;
942 if ((num_objects
>= opotential
) ||
943 (num_objects
>= ppotential
)) {
946 * This should be rare. In any case,
947 * we will only miss recent objects,
948 * because they are added at the end.
954 * Avoid interfering with normal operations
956 if (!VS_MAP_TRY_LOCK(entry
))
958 size
= ps_vstruct_allocated_size(entry
);
959 VS_MAP_UNLOCK(entry
);
964 * We need a reference for our caller. Adding this
965 * reference through the linked list could race with
966 * destruction of the object. If we find the object
967 * has no references, just give up on it.
970 if (entry
->vs_references
== 0) {
974 pager
= vs_to_mem_obj(entry
);
975 dp_memory_object_reference(pager
);
978 /* the arrays are wired, so no deadlock worries */
980 objects
[num_objects
].dpo_object
= (vm_offset_t
) entry
;
981 objects
[num_objects
].dpo_size
= size
;
982 pagers
[num_objects
++] = pager
;
987 * Do not return garbage
989 objects
[num_objects
].dpo_object
= (vm_offset_t
) 0;
990 objects
[num_objects
].dpo_size
= 0;
991 pagers
[num_objects
++] = MEMORY_OBJECT_NULL
;
997 /* clear out any excess allocation */
998 while (num_objects
< opotential
) {
999 objects
[--opotential
].dpo_object
= (vm_offset_t
) 0;
1000 objects
[opotential
].dpo_size
= 0;
1002 while (num_objects
< ppotential
) {
1003 pagers
[--ppotential
] = MEMORY_OBJECT_NULL
;
1006 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(oaddr
),
1007 vm_map_round_page(oaddr
+ osize
), FALSE
);
1008 assert(KERN_SUCCESS
== kr
);
1009 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)oaddr
,
1010 (vm_map_size_t
)osize
, TRUE
, &pcopy
);
1011 assert(KERN_SUCCESS
== kr
);
1013 *objectsp
= (default_pager_object_array_t
)objects
;
1014 *ocountp
= num_objects
;
1015 *portsp
= (mach_port_array_t
)pcopy
;
1016 *pcountp
= num_objects
;
1018 return KERN_SUCCESS
;
1022 default_pager_object_pages(
1023 default_pager_t default_pager
,
1024 mach_port_t memory_object
,
1025 default_pager_page_array_t
*pagesp
,
1026 mach_msg_type_number_t
*countp
)
1028 vm_offset_t addr
= 0; /* memory for page offsets */
1029 vm_size_t size
= 0; /* current memory size */
1031 default_pager_page_t
* pages
= 0;
1032 unsigned int potential
;
1033 unsigned int actual
;
1035 memory_object_t object
;
1037 if (default_pager
!= default_pager_object
)
1038 return KERN_INVALID_ARGUMENT
;
1040 object
= (memory_object_t
) memory_object
;
1047 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
,
1050 if (vs_to_mem_obj(entry
) == object
) {
1058 /* did not find the object */
1060 kmem_free(ipc_kernel_map
, addr
, size
);
1062 return KERN_INVALID_ARGUMENT
;
1066 if (!VS_MAP_TRY_LOCK(entry
)) {
1067 /* oh well bad luck */
1072 assert_wait_timeout((event_t
)assert_wait_timeout
, THREAD_UNINT
, 1, 1000*NSEC_PER_USEC
);
1073 wresult
= thread_block(THREAD_CONTINUE_NULL
);
1074 assert(wresult
== THREAD_TIMED_OUT
);
1078 actual
= ps_vstruct_allocated_pages(entry
, pages
, potential
);
1079 VS_MAP_UNLOCK(entry
);
1082 if (actual
<= potential
)
1085 /* allocate more memory */
1087 kmem_free(ipc_kernel_map
, addr
, size
);
1089 size
= round_page(actual
* sizeof * pages
);
1090 kr
= kmem_alloc(ipc_kernel_map
, &addr
, size
);
1091 if (KERN_SUCCESS
!= kr
)
1092 return KERN_RESOURCE_SHORTAGE
;
1094 pages
= (default_pager_page_t
*)addr
;
1095 potential
= size
/ sizeof * pages
;
1099 * Clear unused memory.
1101 while (actual
< potential
)
1102 pages
[--potential
].dpp_offset
= 0;
1104 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
1105 vm_map_round_page(addr
+ size
), FALSE
);
1106 assert(KERN_SUCCESS
== kr
);
1107 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
1108 (vm_map_size_t
)size
, TRUE
, ©
);
1109 assert(KERN_SUCCESS
== kr
);
1112 *pagesp
= (default_pager_page_array_t
)copy
;
1114 return KERN_SUCCESS
;