2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * Memory Object Management.
59 #include "default_pager_internal.h"
60 #include <mach/memory_object_types.h>
61 #include <mach/memory_object_server.h>
62 #include <vm/memory_object.h>
63 #include <vm/vm_pageout.h>
67 * List of all vstructs. A specific vstruct is
68 * found directly via its port, this list is
69 * only used for monitoring purposes by the
70 * default_pager_object* calls and by ps_delete
71 * when abstract memory objects must be scanned
72 * to remove any live storage on a segment which
75 struct vstruct_list_head vstruct_list
;
77 __private_extern__
void
82 queue_enter(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
83 vstruct_list
.vsl_count
++;
88 __private_extern__
void
92 queue_remove(&vstruct_list
.vsl_queue
, vs
, vstruct_t
, vs_links
);
93 vstruct_list
.vsl_count
--;
97 * We use the sequence numbers on requests to regulate
98 * our parallelism. In general, we allow multiple reads and writes
99 * to proceed in parallel, with the exception that reads must
100 * wait for previous writes to finish. (Because the kernel might
101 * generate a data-request for a page on the heels of a data-write
102 * for the same page, and we must avoid returning stale data.)
103 * terminate requests wait for proceeding reads and writes to finish.
106 static unsigned int default_pager_total
= 0; /* debugging */
107 static unsigned int default_pager_wait_seqno
= 0; /* debugging */
108 static unsigned int default_pager_wait_read
= 0; /* debugging */
109 static unsigned int default_pager_wait_write
= 0; /* debugging */
110 static unsigned int default_pager_wait_refs
= 0; /* debugging */
112 __private_extern__
void
117 ASSERT(vs
->vs_async_pending
>= 0);
118 while (vs
->vs_async_pending
> 0) {
119 vs
->vs_waiting_async
= TRUE
;
120 assert_wait(&vs
->vs_async_pending
, THREAD_UNINT
);
122 thread_block(THREAD_CONTINUE_NULL
);
125 ASSERT(vs
->vs_async_pending
== 0);
131 * Waits for correct sequence number. Leaves pager locked.
133 * JMM - Sequence numbers guarantee ordering of requests generated
134 * by a single thread if the receiver is multithreaded and
135 * the interfaces are asynchronous (i.e. sender can generate
136 * more than one request before the first is received in the
137 * pager). Normally, IPC would generate these number in that
138 * case. But we are trying to avoid using IPC for the in-kernel
139 * scenario. Since these are actually invoked synchronously
140 * anyway (in-kernel), we can just fake the sequence number
141 * generation here (thus avoiding the dependence on IPC).
143 __private_extern__
void
147 mach_port_seqno_t seqno
;
149 default_pager_total
++;
152 seqno
= vs
->vs_next_seqno
++;
154 while (vs
->vs_seqno
!= seqno
) {
155 default_pager_wait_seqno
++;
156 vs
->vs_waiting_seqno
= TRUE
;
157 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
159 thread_block(THREAD_CONTINUE_NULL
);
165 * Increments sequence number and unlocks pager.
167 __private_extern__
void
168 vs_unlock(vstruct_t vs
)
171 if (vs
->vs_waiting_seqno
) {
172 vs
->vs_waiting_seqno
= FALSE
;
174 thread_wakeup(&vs
->vs_seqno
);
181 * Start a read - one more reader. Pager must be locked.
183 __private_extern__
void
191 * Wait for readers. Unlocks and relocks pager if wait needed.
193 __private_extern__
void
197 while (vs
->vs_readers
!= 0) {
198 default_pager_wait_read
++;
199 vs
->vs_waiting_read
= TRUE
;
200 assert_wait(&vs
->vs_readers
, THREAD_UNINT
);
202 thread_block(THREAD_CONTINUE_NULL
);
208 * Finish a read. Pager is unlocked and returns unlocked.
210 __private_extern__
void
215 if (--vs
->vs_readers
== 0 && vs
->vs_waiting_read
) {
216 vs
->vs_waiting_read
= FALSE
;
218 thread_wakeup(&vs
->vs_readers
);
225 * Start a write - one more writer. Pager must be locked.
227 __private_extern__
void
235 * Wait for writers. Unlocks and relocks pager if wait needed.
237 __private_extern__
void
241 while (vs
->vs_writers
!= 0) {
242 default_pager_wait_write
++;
243 vs
->vs_waiting_write
= TRUE
;
244 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
246 thread_block(THREAD_CONTINUE_NULL
);
252 /* This is to be used for the transfer from segment code ONLY */
253 /* The transfer code holds off vs destruction by keeping the */
254 /* vs_async_wait count non-zero. It will not ocnflict with */
255 /* other writers on an async basis because it only writes on */
256 /* a cluster basis into fresh (as of sync time) cluster locations */
258 __private_extern__
void
259 vs_wait_for_sync_writers(
262 while (vs
->vs_writers
!= 0) {
263 default_pager_wait_write
++;
264 vs
->vs_waiting_write
= TRUE
;
265 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
267 thread_block(THREAD_CONTINUE_NULL
);
274 * Finish a write. Pager is unlocked and returns unlocked.
276 __private_extern__
void
281 if (--vs
->vs_writers
== 0 && vs
->vs_waiting_write
) {
282 vs
->vs_waiting_write
= FALSE
;
284 thread_wakeup(&vs
->vs_writers
);
289 #endif /* PARALLEL */
298 * Allocate a vstruct. If there are any problems, then report them
301 vs
= ps_vstruct_create(size
);
302 if (vs
== VSTRUCT_NULL
) {
303 dprintf(("vs_object_create: unable to allocate %s\n",
304 "-- either run swapon command or reboot"));
312 void default_pager_add(vstruct_t
, boolean_t
); /* forward */
319 memory_object_t mem_obj
= vs
->vs_mem_obj
;
321 mach_port_mscount_t sync
;
322 mach_port_t previous
;
324 static char here
[] = "default_pager_add";
327 * The port currently has a make-send count of zero,
328 * because either we just created the port or we just
329 * received the port in a memory_object_create request.
333 /* possibly generate an immediate no-senders notification */
335 pset
= default_pager_internal_set
;
337 /* delay notification till send right is created */
339 pset
= default_pager_external_set
;
342 ipc_port_make_sonce(mem_obj
);
343 ip_lock(mem_obj
); /* unlocked in nsrequest below */
344 ipc_port_nsrequest(mem_obj
, sync
, mem_obj
, &previous
);
350 dp_memory_object_init(
351 memory_object_t mem_obj
,
352 memory_object_control_t control
,
353 vm_size_t pager_page_size
)
357 assert(pager_page_size
== vm_page_size
);
359 memory_object_control_reference(control
);
361 vs_lookup(mem_obj
, vs
);
364 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
365 Panic("bad request");
367 vs
->vs_control
= control
;
374 dp_memory_object_synchronize(
375 memory_object_t mem_obj
,
376 memory_object_offset_t offset
,
382 vs_lookup(mem_obj
, vs
);
386 memory_object_synchronize_completed(vs
->vs_control
, offset
, length
);
392 dp_memory_object_unmap(
393 memory_object_t mem_obj
)
395 panic("dp_memory_object_unmap");
401 dp_memory_object_terminate(
402 memory_object_t mem_obj
)
404 memory_object_control_t control
;
409 * control port is a receive right, not a send right.
412 vs_lookup(mem_obj
, vs
);
416 * Wait for read and write requests to terminate.
419 vs_wait_for_readers(vs
);
420 vs_wait_for_writers(vs
);
423 * After memory_object_terminate both memory_object_init
424 * and a no-senders notification are possible, so we need
425 * to clean up our reference to the memory_object_control
426 * to prepare for a new init.
429 control
= vs
->vs_control
;
430 vs
->vs_control
= MEMORY_OBJECT_CONTROL_NULL
;
432 /* a bit of special case ugliness here. Wakeup any waiting reads */
433 /* these data requests had to be removed from the seqno traffic */
434 /* based on a performance bottleneck with large memory objects */
435 /* the problem will right itself with the new component based */
436 /* synchronous interface. The new async will be able to return */
437 /* failure during its sync phase. In the mean time ... */
439 thread_wakeup(&vs
->vs_writers
);
440 thread_wakeup(&vs
->vs_async_pending
);
445 * Now we deallocate our reference on the control.
447 memory_object_control_deallocate(control
);
452 dp_memory_object_reference(
453 memory_object_t mem_obj
)
457 vs_lookup_safe(mem_obj
, vs
);
458 if (vs
== VSTRUCT_NULL
)
462 assert(vs
->vs_references
> 0);
467 extern ipc_port_t max_pages_trigger_port
;
468 extern int dp_pages_free
;
469 extern int maximum_pages_free
;
471 dp_memory_object_deallocate(
472 memory_object_t mem_obj
)
475 mach_port_seqno_t seqno
;
478 * Because we don't give out multiple first references
479 * for a memory object, there can't be a race
480 * between getting a deallocate call and creating
481 * a new reference for the object.
484 vs_lookup_safe(mem_obj
, vs
);
485 if (vs
== VSTRUCT_NULL
)
489 if (--vs
->vs_references
> 0) {
494 seqno
= vs
->vs_next_seqno
++;
495 while (vs
->vs_seqno
!= seqno
) {
496 default_pager_wait_seqno
++;
497 vs
->vs_waiting_seqno
= TRUE
;
498 assert_wait(&vs
->vs_seqno
, THREAD_UNINT
);
500 thread_block(THREAD_CONTINUE_NULL
);
504 vs_async_wait(vs
); /* wait for pending async IO */
506 /* do not delete the vs structure until the referencing pointers */
507 /* in the vstruct list have been expunged */
509 /* get VSL_LOCK out of order by using TRY mechanism */
510 while(!VSL_LOCK_TRY()) {
515 vs_async_wait(vs
); /* wait for pending async IO */
520 * We shouldn't get a deallocation call
521 * when the kernel has the object cached.
523 if (vs
->vs_control
!= MEMORY_OBJECT_CONTROL_NULL
)
524 Panic("bad request");
527 * Unlock the pager (though there should be no one
532 /* Lock out paging segment removal for the duration of this */
533 /* call. We are vulnerable to losing a paging segment we rely */
534 /* on as soon as we remove ourselves from the VSL and unlock */
536 /* Keep our thread from blocking on attempt to trigger backing */
538 backing_store_release_trigger_disable
+= 1;
541 * Remove the memory object port association, and then
542 * the destroy the port itself. We must remove the object
543 * from the port list before deallocating the pager,
544 * because of default_pager_objects.
546 vstruct_list_delete(vs
);
549 ps_vstruct_dealloc(vs
);
552 backing_store_release_trigger_disable
-= 1;
553 if(backing_store_release_trigger_disable
== 0) {
554 thread_wakeup((event_t
)&backing_store_release_trigger_disable
);
560 dp_memory_object_data_request(
561 memory_object_t mem_obj
,
562 memory_object_offset_t offset
,
564 vm_prot_t protection_required
)
568 GSTAT(global_stats
.gs_pagein_calls
++);
571 /* CDY at this moment vs_lookup panics when presented with the wrong */
572 /* port. As we are expanding this pager to support user interfaces */
573 /* this should be changed to return kern_failure */
574 vs_lookup(mem_obj
, vs
);
577 /* We are going to relax the strict sequencing here for performance */
578 /* reasons. We can do this because we know that the read and */
579 /* write threads are different and we rely on synchronization */
580 /* of read and write requests at the cache memory_object level */
581 /* break out wait_for_writers, all of this goes away when */
582 /* we get real control of seqno with the new component interface */
584 if (vs
->vs_writers
!= 0) {
585 /* you can't hold on to the seqno and go */
586 /* to sleep like that */
587 vs_unlock(vs
); /* bump internal count of seqno */
589 while (vs
->vs_writers
!= 0) {
590 default_pager_wait_write
++;
591 vs
->vs_waiting_write
= TRUE
;
592 assert_wait(&vs
->vs_writers
, THREAD_UNINT
);
594 thread_block(THREAD_CONTINUE_NULL
);
598 if(vs
->vs_control
== MEMORY_OBJECT_CONTROL_NULL
) {
610 * Request must be on a page boundary and a multiple of pages.
612 if ((offset
& vm_page_mask
) != 0 || (length
& vm_page_mask
) != 0)
613 Panic("bad alignment");
615 pvs_cluster_read(vs
, (vm_offset_t
)offset
, length
);
623 * memory_object_data_initialize: check whether we already have each page, and
624 * write it if we do not. The implementation is far from optimized, and
625 * also assumes that the default_pager is single-threaded.
627 /* It is questionable whether or not a pager should decide what is relevant */
628 /* and what is not in data sent from the kernel. Data initialize has been */
629 /* changed to copy back all data sent to it in preparation for its eventual */
630 /* merge with data return. It is the kernel that should decide what pages */
631 /* to write back. As of the writing of this note, this is indeed the case */
632 /* the kernel writes back one page at a time through this interface */
635 dp_memory_object_data_initialize(
636 memory_object_t mem_obj
,
637 memory_object_offset_t offset
,
642 DEBUG(DEBUG_MO_EXTERNAL
,
643 ("mem_obj=0x%x,offset=0x%x,cnt=0x%x\n",
644 (int)mem_obj
, (int)offset
, (int)size
));
645 GSTAT(global_stats
.gs_pages_init
+= atop_32(size
));
647 vs_lookup(mem_obj
, vs
);
653 * Write the data via clustered writes. vs_cluster_write will
654 * loop if the address range specified crosses cluster
657 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
665 dp_memory_object_data_unlock(
666 memory_object_t mem_obj
,
667 memory_object_offset_t offset
,
669 vm_prot_t desired_access
)
671 Panic("dp_memory_object_data_unlock: illegal");
677 dp_memory_object_data_return(
678 memory_object_t mem_obj
,
679 memory_object_offset_t offset
,
682 boolean_t kernel_copy
)
686 DEBUG(DEBUG_MO_EXTERNAL
,
687 ("mem_obj=0x%x,offset=0x%x,size=0x%x\n",
688 (int)mem_obj
, (int)offset
, (int)size
));
689 GSTAT(global_stats
.gs_pageout_calls
++);
691 /* This routine is called by the pageout thread. The pageout thread */
692 /* cannot be blocked by read activities unless the read activities */
693 /* Therefore the grant of vs lock must be done on a try versus a */
694 /* blocking basis. The code below relies on the fact that the */
695 /* interface is synchronous. Should this interface be again async */
696 /* for some type of pager in the future the pages will have to be */
697 /* returned through a separate, asynchronous path. */
699 vs_lookup(mem_obj
, vs
);
701 default_pager_total
++;
702 if(!VS_TRY_LOCK(vs
)) {
703 /* the call below will not be done by caller when we have */
704 /* a synchronous interface */
705 /* return KERN_LOCK_OWNED; */
707 int page_list_count
= 0;
708 memory_object_super_upl_request(vs
->vs_control
,
709 (memory_object_offset_t
)offset
,
711 &upl
, NULL
, &page_list_count
,
712 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
713 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
719 if ((vs
->vs_seqno
!= vs
->vs_next_seqno
++)
721 || (vs
->vs_xfer_pending
)) {
723 int page_list_count
= 0;
728 /* the call below will not be done by caller when we have */
729 /* a synchronous interface */
730 /* return KERN_LOCK_OWNED; */
731 memory_object_super_upl_request(vs
->vs_control
,
732 (memory_object_offset_t
)offset
,
734 &upl
, NULL
, &page_list_count
,
735 UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
736 | UPL_NO_SYNC
| UPL_COPYOUT_FROM
);
742 if ((size
% vm_page_size
) != 0)
743 Panic("bad alignment");
748 vs
->vs_async_pending
+= 1; /* protect from backing store contraction */
752 * Write the data via clustered writes. vs_cluster_write will
753 * loop if the address range specified crosses cluster
756 vs_cluster_write(vs
, 0, (vm_offset_t
)offset
, size
, FALSE
, 0);
760 /* temporary, need a finer lock based on cluster */
763 vs
->vs_async_pending
-= 1; /* release vs_async_wait */
764 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
765 vs
->vs_waiting_async
= FALSE
;
767 thread_wakeup(&vs
->vs_async_pending
);
777 * Routine: default_pager_memory_object_create
779 * Handle requests for memory objects from the
782 * Because we only give out the default memory
783 * manager port to the kernel, we don't have to
784 * be so paranoid about the contents.
787 default_pager_memory_object_create(
788 memory_object_default_t dmm
,
790 memory_object_t
*new_mem_obj
)
794 assert(dmm
== default_pager_object
);
796 vs
= vs_object_create(new_size
);
797 if (vs
== VSTRUCT_NULL
)
798 return KERN_RESOURCE_SHORTAGE
;
800 vs
->vs_next_seqno
= 0;
803 * Set up associations between this memory object
804 * and this default_pager structure
807 vs
->vs_mem_obj
= ISVS
;
808 vs
->vs_mem_obj_ikot
= IKOT_MEMORY_OBJECT
;
811 * After this, other threads might receive requests
812 * for this memory object or find it in the port list.
815 vstruct_list_insert(vs
);
816 *new_mem_obj
= vs_to_mem_obj(vs
);
821 * Create an external object.
824 default_pager_object_create(
825 default_pager_t pager
,
827 memory_object_t
*mem_objp
)
830 kern_return_t result
;
831 struct vstruct_alias
*alias_struct
;
834 if (pager
!= default_pager_object
)
835 return KERN_INVALID_ARGUMENT
;
837 vs
= vs_object_create(size
);
838 if (vs
== VSTRUCT_NULL
)
839 return KERN_RESOURCE_SHORTAGE
;
842 * Set up associations between the default pager
843 * and this vstruct structure
845 vs
->vs_mem_obj
= ISVS
;
846 vstruct_list_insert(vs
);
847 *mem_objp
= vs_to_mem_obj(vs
);
852 default_pager_objects(
853 default_pager_t pager
,
854 default_pager_object_array_t
*objectsp
,
855 mach_msg_type_number_t
*ocountp
,
856 memory_object_array_t
*pagersp
,
857 mach_msg_type_number_t
*pcountp
)
859 vm_offset_t oaddr
= 0; /* memory for objects */
860 vm_size_t osize
= 0; /* current size */
861 default_pager_object_t
* objects
;
862 unsigned int opotential
;
864 vm_offset_t paddr
= 0; /* memory for pagers */
865 vm_size_t psize
= 0; /* current size */
866 memory_object_t
* pagers
;
867 unsigned int ppotential
;
870 unsigned int num_objects
;
874 if (pager != default_pager_default_port)
875 return KERN_INVALID_ARGUMENT;
878 /* start with the inline memory */
880 kr
= vm_map_copyout(ipc_kernel_map
, (vm_offset_t
*)&objects
,
881 (vm_map_copy_t
) *objectsp
);
883 if (kr
!= KERN_SUCCESS
)
886 osize
= round_page_32(*ocountp
* sizeof * objects
);
887 kr
= vm_map_wire(ipc_kernel_map
,
888 trunc_page_32((vm_offset_t
)objects
),
889 round_page_32(((vm_offset_t
)objects
) + osize
),
890 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
894 /* we start with the inline space */
898 opotential
= *ocountp
;
900 pagers
= (memory_object_t
*) *pagersp
;
901 ppotential
= *pcountp
;
906 * We will send no more than this many
908 actual
= vstruct_list
.vsl_count
;
911 if (opotential
< actual
) {
915 newsize
= 2 * round_page_32(actual
* sizeof * objects
);
917 kr
= vm_allocate(kernel_map
, &newaddr
, newsize
, TRUE
);
918 if (kr
!= KERN_SUCCESS
)
923 opotential
= osize
/ sizeof * objects
;
924 objects
= (default_pager_object_t
*)oaddr
;
927 if (ppotential
< actual
) {
931 newsize
= 2 * round_page_32(actual
* sizeof * pagers
);
933 kr
= vm_allocate(kernel_map
, &newaddr
, newsize
, TRUE
);
934 if (kr
!= KERN_SUCCESS
)
939 ppotential
= psize
/ sizeof * pagers
;
940 pagers
= (memory_object_t
*)paddr
;
950 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
, vs_links
) {
952 memory_object_t pager
;
955 if ((num_objects
>= opotential
) ||
956 (num_objects
>= ppotential
)) {
959 * This should be rare. In any case,
960 * we will only miss recent objects,
961 * because they are added at the end.
967 * Avoid interfering with normal operations
969 if (!VS_MAP_TRY_LOCK(entry
))
971 size
= ps_vstruct_allocated_size(entry
);
972 VS_MAP_UNLOCK(entry
);
977 * We need a reference for our caller. Adding this
978 * reference through the linked list could race with
979 * destruction of the object. If we find the object
980 * has no references, just give up on it.
983 if (entry
->vs_references
== 0) {
987 dp_memory_object_reference(vs_to_mem_obj(entry
));
990 /* the arrays are wired, so no deadlock worries */
992 objects
[num_objects
].dpo_object
= (vm_offset_t
) entry
;
993 objects
[num_objects
].dpo_size
= size
;
994 pagers
[num_objects
++] = pager
;
999 * Do not return garbage
1001 objects
[num_objects
].dpo_object
= (vm_offset_t
) 0;
1002 objects
[num_objects
].dpo_size
= 0;
1003 pagers
[num_objects
++] = MEMORY_OBJECT_NULL
;
1010 * Deallocate and clear unused memory.
1011 * (Returned memory will automagically become pageable.)
1014 if (objects
== *objectsp
) {
1017 * Our returned information fit inline.
1018 * Nothing to deallocate.
1020 *ocountp
= num_objects
;
1021 } else if (actual
== 0) {
1022 (void) vm_deallocate(kernel_map
, oaddr
, osize
);
1024 /* return zero items inline */
1029 used
= round_page_32(actual
* sizeof * objects
);
1032 (void) vm_deallocate(kernel_map
,
1033 oaddr
+ used
, osize
- used
);
1035 *objectsp
= objects
;
1036 *ocountp
= num_objects
;
1039 if (pagers
== (memory_object_t
*)*pagersp
) {
1042 * Our returned information fit inline.
1043 * Nothing to deallocate.
1046 *pcountp
= num_objects
;
1047 } else if (actual
== 0) {
1048 (void) vm_deallocate(kernel_map
, paddr
, psize
);
1050 /* return zero items inline */
1055 used
= round_page_32(actual
* sizeof * pagers
);
1058 (void) vm_deallocate(kernel_map
,
1059 paddr
+ used
, psize
- used
);
1061 *pagersp
= (memory_object_array_t
)pagers
;
1062 *pcountp
= num_objects
;
1064 (void) vm_map_unwire(kernel_map
, (vm_offset_t
)objects
,
1065 *ocountp
+ (vm_offset_t
)objects
, FALSE
);
1066 (void) vm_map_copyin(kernel_map
, (vm_offset_t
)objects
,
1067 *ocountp
, TRUE
, (vm_map_copy_t
*)objectsp
);
1069 return KERN_SUCCESS
;
1074 for (i
= 0; i
< num_objects
; i
++)
1075 if (pagers
[i
] != MEMORY_OBJECT_NULL
)
1076 memory_object_deallocate(pagers
[i
]);
1079 if (objects
!= *objectsp
)
1080 (void) vm_deallocate(kernel_map
, oaddr
, osize
);
1082 if (pagers
!= (memory_object_t
*)*pagersp
)
1083 (void) vm_deallocate(kernel_map
, paddr
, psize
);
1085 return KERN_RESOURCE_SHORTAGE
;
1089 default_pager_object_pages(
1090 default_pager_t pager
,
1091 memory_object_t object
,
1092 default_pager_page_array_t
*pagesp
,
1093 mach_msg_type_number_t
*countp
)
1095 vm_offset_t addr
; /* memory for page offsets */
1096 vm_size_t size
= 0; /* current memory size */
1097 default_pager_page_t
* pages
;
1098 unsigned int potential
, actual
;
1102 if (pager
!= default_pager_object
)
1103 return KERN_INVALID_ARGUMENT
;
1105 kr
= vm_map_copyout(ipc_kernel_map
, (vm_offset_t
*)&pages
,
1106 (vm_map_copy_t
) *pagesp
);
1108 if (kr
!= KERN_SUCCESS
)
1111 size
= round_page_32(*countp
* sizeof * pages
);
1112 kr
= vm_map_wire(ipc_kernel_map
,
1113 trunc_page_32((vm_offset_t
)pages
),
1114 round_page_32(((vm_offset_t
)pages
) + size
),
1115 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
1119 /* we start with the inline space */
1121 addr
= (vm_offset_t
)pages
;
1122 potential
= *countp
;
1128 queue_iterate(&vstruct_list
.vsl_queue
, entry
, vstruct_t
,
1131 if (vs_to_mem_obj(entry
) == object
) {
1139 /* did not find the object */
1141 if (pages
!= *pagesp
)
1142 (void) vm_deallocate(kernel_map
, addr
, size
);
1143 return KERN_INVALID_ARGUMENT
;
1147 if (!VS_MAP_TRY_LOCK(entry
)) {
1148 /* oh well bad luck */
1153 assert_wait_timeout( 1, THREAD_UNINT
);
1154 wresult
= thread_block(THREAD_CONTINUE_NULL
);
1155 assert(wresult
== THREAD_TIMED_OUT
);
1159 actual
= ps_vstruct_allocated_pages(entry
, pages
, potential
);
1160 VS_MAP_UNLOCK(entry
);
1163 if (actual
<= potential
)
1166 /* allocate more memory */
1168 if (pages
!= *pagesp
)
1169 (void) vm_deallocate(kernel_map
, addr
, size
);
1170 size
= round_page_32(actual
* sizeof * pages
);
1171 kr
= vm_allocate(kernel_map
, &addr
, size
, TRUE
);
1172 if (kr
!= KERN_SUCCESS
)
1174 pages
= (default_pager_page_t
*)addr
;
1175 potential
= size
/ sizeof * pages
;
1179 * Deallocate and clear unused memory.
1180 * (Returned memory will automagically become pageable.)
1183 if (pages
== *pagesp
) {
1186 * Our returned information fit inline.
1187 * Nothing to deallocate.
1191 } else if (actual
== 0) {
1192 (void) vm_deallocate(kernel_map
, addr
, size
);
1194 /* return zero items inline */
1199 used
= round_page_32(actual
* sizeof * pages
);
1202 (void) vm_deallocate(kernel_map
,
1203 addr
+ used
, size
- used
);
1208 (void) vm_map_unwire(kernel_map
, (vm_offset_t
)pages
,
1209 *countp
+ (vm_offset_t
)pages
, FALSE
);
1210 (void) vm_map_copyin(kernel_map
, (vm_offset_t
)pages
,
1211 *countp
, TRUE
, (vm_map_copy_t
*)pagesp
);
1212 return KERN_SUCCESS
;