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 * Paging File Management.
64 #include <mach/host_priv.h>
65 #include <mach/memory_object_control.h>
66 #include <mach/memory_object_server.h>
68 #include <default_pager/default_pager_internal.h>
69 #include <default_pager/default_pager_alerts.h>
70 #include <default_pager/default_pager_object_server.h>
72 #include <ipc/ipc_types.h>
73 #include <ipc/ipc_port.h>
74 #include <ipc/ipc_space.h>
76 #include <kern/kern_types.h>
77 #include <kern/host.h>
78 #include <kern/queue.h>
79 #include <kern/counters.h>
80 #include <kern/sched_prim.h>
82 #include <vm/vm_kern.h>
83 #include <vm/vm_pageout.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_protos.h>
88 /* LP64todo - need large internal object support */
91 * ALLOC_STRIDE... the maximum number of bytes allocated from
92 * a swap file before moving on to the next swap file... if
93 * all swap files reside on a single disk, this value should
94 * be very large (this is the default assumption)... if the
95 * swap files are spread across multiple disks, than this value
96 * should be small (128 * 1024)...
98 * This should be determined dynamically in the future
101 #define ALLOC_STRIDE (1024 * 1024 * 1024)
102 int physical_transfer_cluster_count
= 0;
104 #define VM_SUPER_CLUSTER 0x40000
105 #define VM_SUPER_PAGES 64
108 * 0 means no shift to pages, so == 1 page/cluster. 1 would mean
109 * 2 pages/cluster, 2 means 4 pages/cluster, and so on.
111 #define VSTRUCT_DEF_CLSHIFT 2
112 int vstruct_def_clshift
= VSTRUCT_DEF_CLSHIFT
;
113 int default_pager_clsize
= 0;
116 unsigned int clustered_writes
[VM_SUPER_PAGES
+1];
117 unsigned int clustered_reads
[VM_SUPER_PAGES
+1];
120 * Globals used for asynchronous paging operations:
121 * vs_async_list: head of list of to-be-completed I/O ops
122 * async_num_queued: number of pages completed, but not yet
123 * processed by async thread.
124 * async_requests_out: number of pages of requests not completed.
128 struct vs_async
*vs_async_list
;
129 int async_num_queued
;
130 int async_requests_out
;
134 #define VS_ASYNC_REUSE 1
135 struct vs_async
*vs_async_free_list
;
137 mutex_t default_pager_async_lock
; /* Protects globals above */
140 int vs_alloc_async_failed
= 0; /* statistics */
141 int vs_alloc_async_count
= 0; /* statistics */
142 struct vs_async
*vs_alloc_async(void); /* forward */
143 void vs_free_async(struct vs_async
*vsa
); /* forward */
146 #define VS_ALLOC_ASYNC() vs_alloc_async()
147 #define VS_FREE_ASYNC(vsa) vs_free_async(vsa)
149 #define VS_ASYNC_LOCK() mutex_lock(&default_pager_async_lock)
150 #define VS_ASYNC_UNLOCK() mutex_unlock(&default_pager_async_lock)
151 #define VS_ASYNC_LOCK_INIT() mutex_init(&default_pager_async_lock, 0)
152 #define VS_ASYNC_LOCK_ADDR() (&default_pager_async_lock)
154 * Paging Space Hysteresis triggers and the target notification port
158 unsigned int minimum_pages_remaining
= 0;
159 unsigned int maximum_pages_free
= 0;
160 ipc_port_t min_pages_trigger_port
= NULL
;
161 ipc_port_t max_pages_trigger_port
= NULL
;
163 boolean_t bs_low
= FALSE
;
164 int backing_store_release_trigger_disable
= 0;
167 /* Have we decided if swap needs to be encrypted yet ? */
168 boolean_t dp_encryption_inited
= FALSE
;
169 /* Should we encrypt swap ? */
170 boolean_t dp_encryption
= FALSE
;
174 * Object sizes are rounded up to the next power of 2,
175 * unless they are bigger than a given maximum size.
177 vm_size_t max_doubled_size
= 4 * 1024 * 1024; /* 4 meg */
180 * List of all backing store and segments.
182 struct backing_store_list_head backing_store_list
;
183 paging_segment_t paging_segments
[MAX_NUM_PAGING_SEGMENTS
];
184 mutex_t paging_segments_lock
;
185 int paging_segment_max
= 0;
186 int paging_segment_count
= 0;
187 int ps_select_array
[BS_MAXPRI
+1] = { -1,-1,-1,-1,-1 };
191 * Total pages free in system
192 * This differs from clusters committed/avail which is a measure of the
193 * over commitment of paging segments to backing store. An idea which is
194 * likely to be deprecated.
196 unsigned int dp_pages_free
= 0;
197 unsigned int cluster_transfer_minimum
= 100;
199 /* forward declarations */
200 kern_return_t
ps_write_file(paging_segment_t
, upl_t
, upl_offset_t
, vm_offset_t
, unsigned int, int); /* forward */
201 kern_return_t
ps_read_file (paging_segment_t
, upl_t
, upl_offset_t
, vm_offset_t
, unsigned int, unsigned int *, int); /* forward */
202 default_pager_thread_t
*get_read_buffer( void );
203 kern_return_t
ps_vstruct_transfer_from_segment(
205 paging_segment_t segment
,
207 kern_return_t
ps_read_device(paging_segment_t
, vm_offset_t
, vm_offset_t
*, unsigned int, unsigned int *, int); /* forward */
208 kern_return_t
ps_write_device(paging_segment_t
, vm_offset_t
, vm_offset_t
, unsigned int, struct vs_async
*); /* forward */
209 kern_return_t
vs_cluster_transfer(
214 vs_map_t
vs_get_map_entry(
219 default_pager_thread_t
*
220 get_read_buffer( void )
226 for (i
=0; i
<default_pager_internal_count
; i
++) {
227 if(dpt_array
[i
]->checked_out
== FALSE
) {
228 dpt_array
[i
]->checked_out
= TRUE
;
229 DPT_UNLOCK(dpt_lock
);
233 DPT_SLEEP(dpt_lock
, &dpt_array
, THREAD_UNINT
);
243 * List of all backing store.
246 queue_init(&backing_store_list
.bsl_queue
);
249 VS_ASYNC_LOCK_INIT();
251 vs_async_free_list
= NULL
;
252 #endif /* VS_ASYNC_REUSE */
254 for (i
= 0; i
< VM_SUPER_PAGES
+ 1; i
++) {
255 clustered_writes
[i
] = 0;
256 clustered_reads
[i
] = 0;
262 * When things do not quite workout...
264 void bs_no_paging_space(boolean_t
); /* forward */
268 boolean_t out_of_memory
)
272 dprintf(("*** OUT OF MEMORY ***\n"));
273 panic("bs_no_paging_space: NOT ENOUGH PAGING SPACE");
276 void bs_more_space(int); /* forward */
277 void bs_commit(int); /* forward */
279 boolean_t user_warned
= FALSE
;
280 unsigned int clusters_committed
= 0;
281 unsigned int clusters_available
= 0;
282 unsigned int clusters_committed_peak
= 0;
290 * Account for new paging space.
292 clusters_available
+= nclusters
;
294 if (clusters_available
>= clusters_committed
) {
295 if (verbose
&& user_warned
) {
296 printf("%s%s - %d excess clusters now.\n",
298 "paging space is OK now",
299 clusters_available
- clusters_committed
);
301 clusters_committed_peak
= 0;
304 if (verbose
&& user_warned
) {
305 printf("%s%s - still short of %d clusters.\n",
307 "WARNING: paging space over-committed",
308 clusters_committed
- clusters_available
);
309 clusters_committed_peak
-= nclusters
;
322 clusters_committed
+= nclusters
;
323 if (clusters_committed
> clusters_available
) {
324 if (verbose
&& !user_warned
) {
326 printf("%s%s - short of %d clusters.\n",
328 "WARNING: paging space over-committed",
329 clusters_committed
- clusters_available
);
331 if (clusters_committed
> clusters_committed_peak
) {
332 clusters_committed_peak
= clusters_committed
;
335 if (verbose
&& user_warned
) {
336 printf("%s%s - was short of up to %d clusters.\n",
338 "paging space is OK now",
339 clusters_committed_peak
- clusters_available
);
341 clusters_committed_peak
= 0;
349 int default_pager_info_verbose
= 1;
356 vm_size_t pages_total
, pages_free
;
361 pages_total
= pages_free
= 0;
362 for (i
= 0; i
<= paging_segment_max
; i
++) {
363 ps
= paging_segments
[i
];
364 if (ps
== PAGING_SEGMENT_NULL
)
368 * no need to lock: by the time this data
369 * gets back to any remote requestor it
370 * will be obsolete anyways
372 pages_total
+= ps
->ps_pgnum
;
373 pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
374 DP_DEBUG(DEBUG_BS_INTERNAL
,
375 ("segment #%d: %d total, %d free\n",
376 i
, ps
->ps_pgnum
, ps
->ps_clcount
<< ps
->ps_clshift
));
378 *totalp
= pages_total
;
380 if (verbose
&& user_warned
&& default_pager_info_verbose
) {
381 if (clusters_available
< clusters_committed
) {
382 printf("%s %d clusters committed, %d available.\n",
391 backing_store_t
backing_store_alloc(void); /* forward */
394 backing_store_alloc(void)
398 bs
= (backing_store_t
) kalloc(sizeof (struct backing_store
));
399 if (bs
== BACKING_STORE_NULL
)
400 panic("backing_store_alloc: no memory");
403 bs
->bs_port
= MACH_PORT_NULL
;
406 bs
->bs_pages_total
= 0;
408 bs
->bs_pages_in_fail
= 0;
409 bs
->bs_pages_out
= 0;
410 bs
->bs_pages_out_fail
= 0;
415 backing_store_t
backing_store_lookup(MACH_PORT_FACE
); /* forward */
417 /* Even in both the component space and external versions of this pager, */
418 /* backing_store_lookup will be called from tasks in the application space */
420 backing_store_lookup(
426 port is currently backed with a vs structure in the alias field
427 we could create an ISBS alias and a port_is_bs call but frankly
428 I see no reason for the test, the bs->port == port check below
429 will work properly on junk entries.
431 if ((port == MACH_PORT_NULL) || port_is_vs(port))
433 if ((port
== MACH_PORT_NULL
))
434 return BACKING_STORE_NULL
;
437 queue_iterate(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
440 if (bs
->bs_port
== port
) {
442 /* Success, return it locked. */
448 return BACKING_STORE_NULL
;
451 void backing_store_add(backing_store_t
); /* forward */
455 __unused backing_store_t bs
)
457 // MACH_PORT_FACE port = bs->bs_port;
458 // MACH_PORT_FACE pset = default_pager_default_set;
459 kern_return_t kr
= KERN_SUCCESS
;
461 if (kr
!= KERN_SUCCESS
)
462 panic("backing_store_add: add to set");
467 * Set up default page shift, but only if not already
468 * set and argument is within range.
471 bs_set_default_clsize(unsigned int npages
)
478 if (default_pager_clsize
== 0) /* if not yet set */
479 vstruct_def_clshift
= local_log2(npages
);
485 int bs_get_global_clsize(int clsize
); /* forward */
488 bs_get_global_clsize(
492 memory_object_default_t dmm
;
496 * Only allow setting of cluster size once. If called
497 * with no cluster size (default), we use the compiled-in default
498 * for the duration. The same cluster size is used for all
501 if (default_pager_clsize
== 0) {
503 * Keep cluster size in bit shift because it's quicker
504 * arithmetic, and easier to keep at a power of 2.
506 if (clsize
!= NO_CLSIZE
) {
507 for (i
= 0; (1 << i
) < clsize
; i
++);
508 if (i
> MAX_CLUSTER_SHIFT
)
509 i
= MAX_CLUSTER_SHIFT
;
510 vstruct_def_clshift
= i
;
512 default_pager_clsize
= (1 << vstruct_def_clshift
);
515 * Let the user know the new (and definitive) cluster size.
518 printf("%scluster size = %d page%s\n",
519 my_name
, default_pager_clsize
,
520 (default_pager_clsize
== 1) ? "" : "s");
523 * Let the kernel know too, in case it hasn't used the
524 * default value provided in main() yet.
526 dmm
= default_pager_object
;
527 clsize
= default_pager_clsize
* vm_page_size
; /* in bytes */
528 kr
= host_default_memory_manager(host_priv_self(),
531 memory_object_default_deallocate(dmm
);
533 if (kr
!= KERN_SUCCESS
) {
534 panic("bs_get_global_cl_size:host_default_memory_manager");
536 if (dmm
!= default_pager_object
) {
537 panic("bs_get_global_cl_size:there is another default pager");
540 ASSERT(default_pager_clsize
> 0 &&
541 (default_pager_clsize
& (default_pager_clsize
- 1)) == 0);
543 return default_pager_clsize
;
547 default_pager_backing_store_create(
548 memory_object_default_t pager
,
550 int clsize
, /* in bytes */
551 MACH_PORT_FACE
*backing_store
)
556 struct vstruct_alias
*alias_struct
;
558 if (pager
!= default_pager_object
)
559 return KERN_INVALID_ARGUMENT
;
561 bs
= backing_store_alloc();
562 port
= ipc_port_alloc_kernel();
563 ipc_port_make_send(port
);
564 assert (port
!= IP_NULL
);
566 DP_DEBUG(DEBUG_BS_EXTERNAL
,
567 ("priority=%d clsize=%d bs_port=0x%x\n",
568 priority
, clsize
, (int) backing_store
));
570 alias_struct
= (struct vstruct_alias
*)
571 kalloc(sizeof (struct vstruct_alias
));
572 if(alias_struct
!= NULL
) {
573 alias_struct
->vs
= (struct vstruct
*)bs
;
574 alias_struct
->name
= &default_pager_ops
;
575 port
->alias
= (int) alias_struct
;
578 ipc_port_dealloc_kernel((MACH_PORT_FACE
)(port
));
579 kfree(bs
, sizeof (struct backing_store
));
580 return KERN_RESOURCE_SHORTAGE
;
584 if (priority
== DEFAULT_PAGER_BACKING_STORE_MAXPRI
)
585 priority
= BS_MAXPRI
;
586 else if (priority
== BS_NOPRI
)
587 priority
= BS_MAXPRI
;
589 priority
= BS_MINPRI
;
590 bs
->bs_priority
= priority
;
592 bs
->bs_clsize
= bs_get_global_clsize(atop_32(clsize
));
595 queue_enter(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
599 backing_store_add(bs
);
601 *backing_store
= port
;
606 default_pager_backing_store_info(
607 MACH_PORT_FACE backing_store
,
608 backing_store_flavor_t flavour
,
609 backing_store_info_t info
,
610 mach_msg_type_number_t
*size
)
613 backing_store_basic_info_t basic
;
617 if (flavour
!= BACKING_STORE_BASIC_INFO
||
618 *size
< BACKING_STORE_BASIC_INFO_COUNT
)
619 return KERN_INVALID_ARGUMENT
;
621 basic
= (backing_store_basic_info_t
)info
;
622 *size
= BACKING_STORE_BASIC_INFO_COUNT
;
624 VSTATS_LOCK(&global_stats
.gs_lock
);
625 basic
->pageout_calls
= global_stats
.gs_pageout_calls
;
626 basic
->pagein_calls
= global_stats
.gs_pagein_calls
;
627 basic
->pages_in
= global_stats
.gs_pages_in
;
628 basic
->pages_out
= global_stats
.gs_pages_out
;
629 basic
->pages_unavail
= global_stats
.gs_pages_unavail
;
630 basic
->pages_init
= global_stats
.gs_pages_init
;
631 basic
->pages_init_writes
= global_stats
.gs_pages_init_writes
;
632 VSTATS_UNLOCK(&global_stats
.gs_lock
);
634 if ((bs
= backing_store_lookup(backing_store
)) == BACKING_STORE_NULL
)
635 return KERN_INVALID_ARGUMENT
;
637 basic
->bs_pages_total
= bs
->bs_pages_total
;
639 bs
->bs_pages_free
= 0;
640 for (i
= 0; i
<= paging_segment_max
; i
++) {
641 ps
= paging_segments
[i
];
642 if (ps
!= PAGING_SEGMENT_NULL
&& ps
->ps_bs
== bs
) {
644 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
649 basic
->bs_pages_free
= bs
->bs_pages_free
;
650 basic
->bs_pages_in
= bs
->bs_pages_in
;
651 basic
->bs_pages_in_fail
= bs
->bs_pages_in_fail
;
652 basic
->bs_pages_out
= bs
->bs_pages_out
;
653 basic
->bs_pages_out_fail
= bs
->bs_pages_out_fail
;
655 basic
->bs_priority
= bs
->bs_priority
;
656 basic
->bs_clsize
= ptoa_32(bs
->bs_clsize
); /* in bytes */
663 int ps_delete(paging_segment_t
); /* forward */
670 kern_return_t error
= KERN_SUCCESS
;
673 VSL_LOCK(); /* get the lock on the list of vs's */
675 /* The lock relationship and sequence is farily complicated */
676 /* this code looks at a live list, locking and unlocking the list */
677 /* as it traverses it. It depends on the locking behavior of */
678 /* default_pager_no_senders. no_senders always locks the vstruct */
679 /* targeted for removal before locking the vstruct list. However */
680 /* it will remove that member of the list without locking its */
681 /* neighbors. We can be sure when we hold a lock on a vstruct */
682 /* it cannot be removed from the list but we must hold the list */
683 /* lock to be sure that its pointers to its neighbors are valid. */
684 /* Also, we can hold off destruction of a vstruct when the list */
685 /* lock and the vs locks are not being held by bumping the */
686 /* vs_async_pending count. */
689 while(backing_store_release_trigger_disable
!= 0) {
690 VSL_SLEEP(&backing_store_release_trigger_disable
, THREAD_UNINT
);
693 /* we will choose instead to hold a send right */
694 vs_count
= vstruct_list
.vsl_count
;
695 vs
= (vstruct_t
) queue_first((queue_entry_t
)&(vstruct_list
.vsl_queue
));
696 if(vs
== (vstruct_t
)&vstruct_list
) {
701 vs_async_wait(vs
); /* wait for any pending async writes */
702 if ((vs_count
!= 0) && (vs
!= NULL
))
703 vs
->vs_async_pending
+= 1; /* hold parties calling */
707 while((vs_count
!= 0) && (vs
!= NULL
)) {
708 /* We take the count of AMO's before beginning the */
709 /* transfer of of the target segment. */
710 /* We are guaranteed that the target segment cannot get */
711 /* more users. We also know that queue entries are */
712 /* made at the back of the list. If some of the entries */
713 /* we would check disappear while we are traversing the */
714 /* list then we will either check new entries which */
715 /* do not have any backing store in the target segment */
716 /* or re-check old entries. This might not be optimal */
717 /* but it will always be correct. The alternative is to */
718 /* take a snapshot of the list. */
721 if(dp_pages_free
< cluster_transfer_minimum
)
722 error
= KERN_FAILURE
;
724 vm_object_t transfer_object
;
728 transfer_object
= vm_object_allocate((vm_object_size_t
)VM_SUPER_CLUSTER
);
730 error
= vm_object_upl_request(transfer_object
,
731 (vm_object_offset_t
)0, VM_SUPER_CLUSTER
,
733 UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
735 if(error
== KERN_SUCCESS
) {
736 error
= ps_vstruct_transfer_from_segment(
738 upl_commit(upl
, NULL
, 0);
741 error
= KERN_FAILURE
;
743 vm_object_deallocate(transfer_object
);
747 vs
->vs_async_pending
-= 1; /* release vs_async_wait */
748 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
749 vs
->vs_waiting_async
= FALSE
;
751 thread_wakeup(&vs
->vs_async_pending
);
760 while(backing_store_release_trigger_disable
!= 0) {
761 VSL_SLEEP(&backing_store_release_trigger_disable
,
765 next_vs
= (vstruct_t
) queue_next(&(vs
->vs_links
));
766 if((next_vs
!= (vstruct_t
)&vstruct_list
) &&
767 (vs
!= next_vs
) && (vs_count
!= 1)) {
769 vs_async_wait(next_vs
); /* wait for any */
770 /* pending async writes */
771 next_vs
->vs_async_pending
+= 1; /* hold parties */
772 /* calling vs_async_wait */
777 vs
->vs_async_pending
-= 1;
778 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
779 vs
->vs_waiting_async
= FALSE
;
781 thread_wakeup(&vs
->vs_async_pending
);
785 if((vs
== next_vs
) || (next_vs
== (vstruct_t
)&vstruct_list
))
796 default_pager_backing_store_delete(
797 MACH_PORT_FACE backing_store
)
803 int interim_pages_removed
= 0;
806 if ((bs
= backing_store_lookup(backing_store
)) == BACKING_STORE_NULL
)
807 return KERN_INVALID_ARGUMENT
;
810 /* not implemented */
817 error
= KERN_SUCCESS
;
818 for (i
= 0; i
<= paging_segment_max
; i
++) {
819 ps
= paging_segments
[i
];
820 if (ps
!= PAGING_SEGMENT_NULL
&&
822 ! ps
->ps_going_away
) {
824 /* disable access to this segment */
825 ps
->ps_going_away
= TRUE
;
828 * The "ps" segment is "off-line" now,
829 * we can try and delete it...
831 if(dp_pages_free
< (cluster_transfer_minimum
833 error
= KERN_FAILURE
;
837 /* remove all pages associated with the */
838 /* segment from the list of free pages */
839 /* when transfer is through, all target */
840 /* segment pages will appear to be free */
842 dp_pages_free
-= ps
->ps_pgcount
;
843 interim_pages_removed
+= ps
->ps_pgcount
;
845 error
= ps_delete(ps
);
847 if (error
!= KERN_SUCCESS
) {
849 * We couldn't delete the segment,
850 * probably because there's not enough
851 * virtual memory left.
852 * Re-enable all the segments.
861 if (error
!= KERN_SUCCESS
) {
862 for (i
= 0; i
<= paging_segment_max
; i
++) {
863 ps
= paging_segments
[i
];
864 if (ps
!= PAGING_SEGMENT_NULL
&&
868 /* re-enable access to this segment */
869 ps
->ps_going_away
= FALSE
;
873 dp_pages_free
+= interim_pages_removed
;
879 for (i
= 0; i
<= paging_segment_max
; i
++) {
880 ps
= paging_segments
[i
];
881 if (ps
!= PAGING_SEGMENT_NULL
&&
883 if(ps
->ps_going_away
) {
884 paging_segments
[i
] = PAGING_SEGMENT_NULL
;
885 paging_segment_count
--;
887 kfree(ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
888 kfree(ps
, sizeof *ps
);
893 /* Scan the entire ps array separately to make certain we find the */
894 /* proper paging_segment_max */
895 for (i
= 0; i
< MAX_NUM_PAGING_SEGMENTS
; i
++) {
896 if(paging_segments
[i
] != PAGING_SEGMENT_NULL
)
897 paging_segment_max
= i
;
903 * All the segments have been deleted.
904 * We can remove the backing store.
908 * Disable lookups of this backing store.
910 if((void *)bs
->bs_port
->alias
!= NULL
)
911 kfree((void *) bs
->bs_port
->alias
,
912 sizeof (struct vstruct_alias
));
913 ipc_port_dealloc_kernel((ipc_port_t
) (bs
->bs_port
));
914 bs
->bs_port
= MACH_PORT_NULL
;
918 * Remove backing store from backing_store list.
921 queue_remove(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
926 * Free the backing store structure.
928 kfree(bs
, sizeof *bs
);
933 int ps_enter(paging_segment_t
); /* forward */
943 for (i
= 0; i
< MAX_NUM_PAGING_SEGMENTS
; i
++) {
944 if (paging_segments
[i
] == PAGING_SEGMENT_NULL
)
948 if (i
< MAX_NUM_PAGING_SEGMENTS
) {
949 paging_segments
[i
] = ps
;
950 if (i
> paging_segment_max
)
951 paging_segment_max
= i
;
952 paging_segment_count
++;
953 if ((ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_NOPRI
) ||
954 (ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_FULLPRI
))
955 ps_select_array
[ps
->ps_bs
->bs_priority
] = 0;
959 return KERN_RESOURCE_SHORTAGE
;
968 default_pager_add_segment(
969 MACH_PORT_FACE backing_store
,
970 MACH_PORT_FACE device
,
980 if ((bs
= backing_store_lookup(backing_store
))
981 == BACKING_STORE_NULL
)
982 return KERN_INVALID_ARGUMENT
;
985 for (i
= 0; i
<= paging_segment_max
; i
++) {
986 ps
= paging_segments
[i
];
987 if (ps
== PAGING_SEGMENT_NULL
)
991 * Check for overlap on same device.
993 if (!(ps
->ps_device
!= device
994 || offset
>= ps
->ps_offset
+ ps
->ps_recnum
995 || offset
+ count
<= ps
->ps_offset
)) {
998 return KERN_INVALID_ARGUMENT
;
1004 * Set up the paging segment
1006 ps
= (paging_segment_t
) kalloc(sizeof (struct paging_segment
));
1007 if (ps
== PAGING_SEGMENT_NULL
) {
1009 return KERN_RESOURCE_SHORTAGE
;
1012 ps
->ps_segtype
= PS_PARTITION
;
1013 ps
->ps_device
= device
;
1014 ps
->ps_offset
= offset
;
1015 ps
->ps_record_shift
= local_log2(vm_page_size
/ record_size
);
1016 ps
->ps_recnum
= count
;
1017 ps
->ps_pgnum
= count
>> ps
->ps_record_shift
;
1019 ps
->ps_pgcount
= ps
->ps_pgnum
;
1020 ps
->ps_clshift
= local_log2(bs
->bs_clsize
);
1021 ps
->ps_clcount
= ps
->ps_ncls
= ps
->ps_pgcount
>> ps
->ps_clshift
;
1025 ps
->ps_bmap
= (unsigned char *) kalloc(RMAPSIZE(ps
->ps_ncls
));
1027 kfree(ps
, sizeof *ps
);
1029 return KERN_RESOURCE_SHORTAGE
;
1031 for (i
= 0; i
< ps
->ps_ncls
; i
++) {
1032 clrbit(ps
->ps_bmap
, i
);
1035 ps
->ps_going_away
= FALSE
;
1038 if ((error
= ps_enter(ps
)) != 0) {
1039 kfree(ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
1040 kfree(ps
, sizeof *ps
);
1042 return KERN_RESOURCE_SHORTAGE
;
1045 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
1046 bs
->bs_pages_total
+= ps
->ps_clcount
<< ps
->ps_clshift
;
1050 dp_pages_free
+= ps
->ps_pgcount
;
1053 bs_more_space(ps
->ps_clcount
);
1055 DP_DEBUG(DEBUG_BS_INTERNAL
,
1056 ("device=0x%x,offset=0x%x,count=0x%x,record_size=0x%x,shift=%d,total_size=0x%x\n",
1057 device
, offset
, count
, record_size
,
1058 ps
->ps_record_shift
, ps
->ps_pgnum
));
1060 return KERN_SUCCESS
;
1066 MACH_PORT_FACE master
)
1068 security_token_t null_security_token
= {
1071 MACH_PORT_FACE device
;
1072 int info
[DEV_GET_SIZE_COUNT
];
1073 mach_msg_type_number_t info_count
;
1074 MACH_PORT_FACE bs
= MACH_PORT_NULL
;
1075 unsigned int rec_size
;
1078 MACH_PORT_FACE reply_port
;
1080 if (ds_device_open_sync(master
, MACH_PORT_NULL
, D_READ
| D_WRITE
,
1081 null_security_token
, dev_name
, &device
))
1084 info_count
= DEV_GET_SIZE_COUNT
;
1085 if (!ds_device_get_status(device
, DEV_GET_SIZE
, info
, &info_count
)) {
1086 rec_size
= info
[DEV_GET_SIZE_RECORD_SIZE
];
1087 count
= info
[DEV_GET_SIZE_DEVICE_SIZE
] / rec_size
;
1088 clsize
= bs_get_global_clsize(0);
1089 if (!default_pager_backing_store_create(
1090 default_pager_object
,
1091 DEFAULT_PAGER_BACKING_STORE_MAXPRI
,
1092 (clsize
* vm_page_size
),
1094 if (!default_pager_add_segment(bs
, device
,
1095 0, count
, rec_size
)) {
1098 ipc_port_release_receive(bs
);
1102 ipc_port_release_send(device
);
1105 #endif /* DEVICE_PAGING */
1110 vs_alloc_async(void)
1112 struct vs_async
*vsa
;
1113 MACH_PORT_FACE reply_port
;
1114 // kern_return_t kr;
1117 if (vs_async_free_list
== NULL
) {
1119 vsa
= (struct vs_async
*) kalloc(sizeof (struct vs_async
));
1122 * Try allocating a reply port named after the
1123 * address of the vs_async structure.
1125 struct vstruct_alias
*alias_struct
;
1127 reply_port
= ipc_port_alloc_kernel();
1128 alias_struct
= (struct vstruct_alias
*)
1129 kalloc(sizeof (struct vstruct_alias
));
1130 if(alias_struct
!= NULL
) {
1131 alias_struct
->vs
= (struct vstruct
*)vsa
;
1132 alias_struct
->name
= &default_pager_ops
;
1133 reply_port
->alias
= (int) alias_struct
;
1134 vsa
->reply_port
= reply_port
;
1135 vs_alloc_async_count
++;
1138 vs_alloc_async_failed
++;
1139 ipc_port_dealloc_kernel((MACH_PORT_FACE
)
1141 kfree(vsa
, sizeof (struct vs_async
));
1146 vsa
= vs_async_free_list
;
1147 vs_async_free_list
= vs_async_free_list
->vsa_next
;
1156 struct vs_async
*vsa
)
1159 vsa
->vsa_next
= vs_async_free_list
;
1160 vs_async_free_list
= vsa
;
1164 #else /* VS_ASYNC_REUSE */
1167 vs_alloc_async(void)
1169 struct vs_async
*vsa
;
1170 MACH_PORT_FACE reply_port
;
1173 vsa
= (struct vs_async
*) kalloc(sizeof (struct vs_async
));
1176 * Try allocating a reply port named after the
1177 * address of the vs_async structure.
1179 reply_port
= ipc_port_alloc_kernel();
1180 alias_struct
= (vstruct_alias
*)
1181 kalloc(sizeof (struct vstruct_alias
));
1182 if(alias_struct
!= NULL
) {
1183 alias_struct
->vs
= reply_port
;
1184 alias_struct
->name
= &default_pager_ops
;
1185 reply_port
->alias
= (int) vsa
;
1186 vsa
->reply_port
= reply_port
;
1187 vs_alloc_async_count
++;
1190 vs_alloc_async_failed
++;
1191 ipc_port_dealloc_kernel((MACH_PORT_FACE
)
1193 kfree(vsa
, sizeof (struct vs_async
));
1203 struct vs_async
*vsa
)
1205 MACH_PORT_FACE reply_port
;
1208 reply_port
= vsa
->reply_port
;
1209 kfree(reply_port
->alias
, sizeof (struct vstuct_alias
));
1210 kfree(vsa
, sizeof (struct vs_async
));
1211 ipc_port_dealloc_kernel((MACH_PORT_FACE
) (reply_port
));
1214 vs_alloc_async_count
--;
1219 #endif /* VS_ASYNC_REUSE */
1221 zone_t vstruct_zone
;
1230 vs
= (vstruct_t
) zalloc(vstruct_zone
);
1231 if (vs
== VSTRUCT_NULL
) {
1232 return VSTRUCT_NULL
;
1238 * The following fields will be provided later.
1240 vs
->vs_pager_ops
= NULL
;
1241 vs
->vs_control
= MEMORY_OBJECT_CONTROL_NULL
;
1242 vs
->vs_references
= 1;
1246 vs
->vs_waiting_seqno
= FALSE
;
1247 vs
->vs_waiting_read
= FALSE
;
1248 vs
->vs_waiting_write
= FALSE
;
1249 vs
->vs_waiting_async
= FALSE
;
1251 mutex_init(&vs
->vs_waiting_seqno
, 0);
1252 mutex_init(&vs
->vs_waiting_read
, 0);
1253 mutex_init(&vs
->vs_waiting_write
, 0);
1254 mutex_init(&vs
->vs_waiting_refs
, 0);
1255 mutex_init(&vs
->vs_waiting_async
, 0);
1263 vs
->vs_clshift
= local_log2(bs_get_global_clsize(0));
1264 vs
->vs_size
= ((atop_32(round_page_32(size
)) - 1) >> vs
->vs_clshift
) + 1;
1265 vs
->vs_async_pending
= 0;
1268 * Allocate the pmap, either CLMAP_SIZE or INDIRECT_CLMAP_SIZE
1269 * depending on the size of the memory object.
1271 if (INDIRECT_CLMAP(vs
->vs_size
)) {
1272 vs
->vs_imap
= (struct vs_map
**)
1273 kalloc(INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1274 vs
->vs_indirect
= TRUE
;
1276 vs
->vs_dmap
= (struct vs_map
*)
1277 kalloc(CLMAP_SIZE(vs
->vs_size
));
1278 vs
->vs_indirect
= FALSE
;
1280 vs
->vs_xfer_pending
= FALSE
;
1281 DP_DEBUG(DEBUG_VS_INTERNAL
,
1282 ("map=0x%x, indirect=%d\n", (int) vs
->vs_dmap
, vs
->vs_indirect
));
1285 * Check to see that we got the space.
1288 kfree(vs
, sizeof *vs
);
1289 return VSTRUCT_NULL
;
1293 * Zero the indirect pointers, or clear the direct pointers.
1295 if (vs
->vs_indirect
)
1296 memset(vs
->vs_imap
, 0,
1297 INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1299 for (i
= 0; i
< vs
->vs_size
; i
++)
1300 VSM_CLR(vs
->vs_dmap
[i
]);
1302 VS_MAP_LOCK_INIT(vs
);
1304 bs_commit(vs
->vs_size
);
1309 paging_segment_t
ps_select_segment(unsigned int, int *); /* forward */
1316 paging_segment_t ps
;
1321 * Optimize case where there's only one segment.
1322 * paging_segment_max will index the one and only segment.
1326 if (paging_segment_count
== 1) {
1327 paging_segment_t lps
; /* used to avoid extra PS_UNLOCK */
1328 ipc_port_t trigger
= IP_NULL
;
1330 ps
= paging_segments
[paging_segment_max
];
1331 *psindex
= paging_segment_max
;
1333 if (ps
->ps_going_away
) {
1334 /* this segment is being turned off */
1335 lps
= PAGING_SEGMENT_NULL
;
1337 ASSERT(ps
->ps_clshift
>= shift
);
1338 if (ps
->ps_clcount
) {
1340 dp_pages_free
-= 1 << ps
->ps_clshift
;
1341 if(min_pages_trigger_port
&&
1342 (dp_pages_free
< minimum_pages_remaining
)) {
1343 trigger
= min_pages_trigger_port
;
1344 min_pages_trigger_port
= NULL
;
1349 lps
= PAGING_SEGMENT_NULL
;
1354 if (trigger
!= IP_NULL
) {
1355 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1356 ipc_port_release_send(trigger
);
1361 if (paging_segment_count
== 0) {
1363 return PAGING_SEGMENT_NULL
;
1367 i
>= BS_MINPRI
; i
--) {
1370 if ((ps_select_array
[i
] == BS_NOPRI
) ||
1371 (ps_select_array
[i
] == BS_FULLPRI
))
1373 start_index
= ps_select_array
[i
];
1375 if(!(paging_segments
[start_index
])) {
1377 physical_transfer_cluster_count
= 0;
1379 else if ((physical_transfer_cluster_count
+1) == (ALLOC_STRIDE
>>
1380 (((paging_segments
[start_index
])->ps_clshift
)
1381 + vm_page_shift
))) {
1382 physical_transfer_cluster_count
= 0;
1383 j
= start_index
+ 1;
1385 physical_transfer_cluster_count
+=1;
1387 if(start_index
== 0)
1388 start_index
= paging_segment_max
;
1390 start_index
= start_index
- 1;
1394 if (j
> paging_segment_max
)
1396 if ((ps
= paging_segments
[j
]) &&
1397 (ps
->ps_bs
->bs_priority
== i
)) {
1399 * Force the ps cluster size to be
1400 * >= that of the vstruct.
1403 if (ps
->ps_going_away
) {
1404 /* this segment is being turned off */
1405 } else if ((ps
->ps_clcount
) &&
1406 (ps
->ps_clshift
>= shift
)) {
1407 ipc_port_t trigger
= IP_NULL
;
1410 dp_pages_free
-= 1 << ps
->ps_clshift
;
1411 if(min_pages_trigger_port
&&
1413 minimum_pages_remaining
)) {
1414 trigger
= min_pages_trigger_port
;
1415 min_pages_trigger_port
= NULL
;
1419 * found one, quit looking.
1421 ps_select_array
[i
] = j
;
1424 if (trigger
!= IP_NULL
) {
1425 default_pager_space_alert(
1428 ipc_port_release_send(trigger
);
1435 if (j
== start_index
) {
1437 * none at this priority -- mark it full
1439 ps_select_array
[i
] = BS_FULLPRI
;
1446 return PAGING_SEGMENT_NULL
;
1449 vm_offset_t
ps_allocate_cluster(vstruct_t
, int *, paging_segment_t
); /*forward*/
1452 ps_allocate_cluster(
1455 paging_segment_t use_ps
)
1457 unsigned int byte_num
;
1459 paging_segment_t ps
;
1460 vm_offset_t cluster
;
1461 ipc_port_t trigger
= IP_NULL
;
1464 * Find best paging segment.
1465 * ps_select_segment will decrement cluster count on ps.
1466 * Must pass cluster shift to find the most appropriate segment.
1468 /* NOTE: The addition of paging segment delete capability threatened
1469 * to seriously complicate the treatment of paging segments in this
1470 * module and the ones that call it (notably ps_clmap), because of the
1471 * difficulty in assuring that the paging segment would continue to
1472 * exist between being unlocked and locked. This was
1473 * avoided because all calls to this module are based in either
1474 * dp_memory_object calls which rely on the vs lock, or by
1475 * the transfer function which is part of the segment delete path.
1476 * The transfer function which is part of paging segment delete is
1477 * protected from multiple callers by the backing store lock.
1478 * The paging segment delete function treats mappings to a paging
1479 * segment on a vstruct by vstruct basis, locking the vstruct targeted
1480 * while data is transferred to the remaining segments. This is in
1481 * line with the view that incomplete or in-transition mappings between
1482 * data, a vstruct, and backing store are protected by the vs lock.
1483 * This and the ordering of the paging segment "going_away" bit setting
1486 if (use_ps
!= PAGING_SEGMENT_NULL
) {
1491 ASSERT(ps
->ps_clcount
!= 0);
1494 dp_pages_free
-= 1 << ps
->ps_clshift
;
1495 if(min_pages_trigger_port
&&
1496 (dp_pages_free
< minimum_pages_remaining
)) {
1497 trigger
= min_pages_trigger_port
;
1498 min_pages_trigger_port
= NULL
;
1502 if (trigger
!= IP_NULL
) {
1503 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1504 ipc_port_release_send(trigger
);
1507 } else if ((ps
= ps_select_segment(vs
->vs_clshift
, psindex
)) ==
1508 PAGING_SEGMENT_NULL
) {
1509 static uint32_t lastnotify
= 0;
1510 uint32_t now
, nanoseconds_dummy
;
1513 * Emit a notification of the low-paging resource condition
1514 * but don't issue it more than once every five seconds. This
1515 * prevents us from overflowing logs with thousands of
1516 * repetitions of the message.
1518 clock_get_system_nanotime(&now
, &nanoseconds_dummy
);
1519 if (now
> lastnotify
+ 5) {
1520 dprintf(("no space in available paging segments\n"));
1524 /* the count got off maybe, reset to zero */
1527 if(min_pages_trigger_port
) {
1528 trigger
= min_pages_trigger_port
;
1529 min_pages_trigger_port
= NULL
;
1533 if (trigger
!= IP_NULL
) {
1534 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1535 ipc_port_release_send(trigger
);
1537 return (vm_offset_t
) -1;
1541 * Look for an available cluster. At the end of the loop,
1542 * byte_num is the byte offset and bit_num is the bit offset of the
1543 * first zero bit in the paging segment bitmap.
1546 byte_num
= ps
->ps_hint
;
1547 for (; byte_num
< howmany(ps
->ps_ncls
, NBBY
); byte_num
++) {
1548 if (*(ps
->ps_bmap
+ byte_num
) != BYTEMASK
) {
1549 for (bit_num
= 0; bit_num
< NBBY
; bit_num
++) {
1550 if (isclr((ps
->ps_bmap
+ byte_num
), bit_num
))
1553 ASSERT(bit_num
!= NBBY
);
1557 ps
->ps_hint
= byte_num
;
1558 cluster
= (byte_num
*NBBY
) + bit_num
;
1560 /* Space was reserved, so this must be true */
1561 ASSERT(cluster
< ps
->ps_ncls
);
1563 setbit(ps
->ps_bmap
, cluster
);
1569 void ps_deallocate_cluster(paging_segment_t
, vm_offset_t
); /* forward */
1572 ps_deallocate_cluster(
1573 paging_segment_t ps
,
1574 vm_offset_t cluster
)
1577 if (cluster
>= (vm_offset_t
) ps
->ps_ncls
)
1578 panic("ps_deallocate_cluster: Invalid cluster number");
1581 * Lock the paging segment, clear the cluster's bitmap and increment the
1582 * number of free cluster.
1586 clrbit(ps
->ps_bmap
, cluster
);
1588 dp_pages_free
+= 1 << ps
->ps_clshift
;
1592 * Move the hint down to the freed cluster if it is
1593 * less than the current hint.
1595 if ((cluster
/NBBY
) < ps
->ps_hint
) {
1596 ps
->ps_hint
= (cluster
/NBBY
);
1602 * If we're freeing space on a full priority, reset the array.
1605 if (ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_FULLPRI
)
1606 ps_select_array
[ps
->ps_bs
->bs_priority
] = 0;
1612 void ps_dealloc_vsmap(struct vs_map
*, vm_size_t
); /* forward */
1616 struct vs_map
*vsmap
,
1620 for (i
= 0; i
< size
; i
++)
1621 if (!VSM_ISCLR(vsmap
[i
]) && !VSM_ISERR(vsmap
[i
]))
1622 ps_deallocate_cluster(VSM_PS(vsmap
[i
]),
1623 VSM_CLOFF(vsmap
[i
]));
1636 * If this is an indirect structure, then we walk through the valid
1637 * (non-zero) indirect pointers and deallocate the clusters
1638 * associated with each used map entry (via ps_dealloc_vsmap).
1639 * When all of the clusters in an indirect block have been
1640 * freed, we deallocate the block. When all of the indirect
1641 * blocks have been deallocated we deallocate the memory
1642 * holding the indirect pointers.
1644 if (vs
->vs_indirect
) {
1645 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
1646 if (vs
->vs_imap
[i
] != NULL
) {
1647 ps_dealloc_vsmap(vs
->vs_imap
[i
], CLMAP_ENTRIES
);
1648 kfree(vs
->vs_imap
[i
], CLMAP_THRESHOLD
);
1651 kfree(vs
->vs_imap
, INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1654 * Direct map. Free used clusters, then memory.
1656 ps_dealloc_vsmap(vs
->vs_dmap
, vs
->vs_size
);
1657 kfree(vs
->vs_dmap
, CLMAP_SIZE(vs
->vs_size
));
1661 bs_commit(- vs
->vs_size
);
1663 zfree(vstruct_zone
, vs
);
1666 int ps_map_extend(vstruct_t
, unsigned int); /* forward */
1670 unsigned int new_size
)
1672 struct vs_map
**new_imap
;
1673 struct vs_map
*new_dmap
= NULL
;
1676 void *old_map
= NULL
;
1677 int old_map_size
= 0;
1679 if (vs
->vs_size
>= new_size
) {
1681 * Someone has already done the work.
1687 * If the new size extends into the indirect range, then we have one
1688 * of two cases: we are going from indirect to indirect, or we are
1689 * going from direct to indirect. If we are going from indirect to
1690 * indirect, then it is possible that the new size will fit in the old
1691 * indirect map. If this is the case, then just reset the size of the
1692 * vstruct map and we are done. If the new size will not
1693 * fit into the old indirect map, then we have to allocate a new
1694 * indirect map and copy the old map pointers into this new map.
1696 * If we are going from direct to indirect, then we have to allocate a
1697 * new indirect map and copy the old direct pages into the first
1698 * indirect page of the new map.
1699 * NOTE: allocating memory here is dangerous, as we're in the
1702 if (INDIRECT_CLMAP(new_size
)) {
1703 int new_map_size
= INDIRECT_CLMAP_SIZE(new_size
);
1706 * Get a new indirect map and zero it.
1708 old_map_size
= INDIRECT_CLMAP_SIZE(vs
->vs_size
);
1709 if (vs
->vs_indirect
&&
1710 (new_map_size
== old_map_size
)) {
1711 bs_commit(new_size
- vs
->vs_size
);
1712 vs
->vs_size
= new_size
;
1716 new_imap
= (struct vs_map
**)kalloc(new_map_size
);
1717 if (new_imap
== NULL
) {
1720 memset(new_imap
, 0, new_map_size
);
1722 if (vs
->vs_indirect
) {
1723 /* Copy old entries into new map */
1724 memcpy(new_imap
, vs
->vs_imap
, old_map_size
);
1725 /* Arrange to free the old map */
1726 old_map
= (void *) vs
->vs_imap
;
1728 } else { /* Old map was a direct map */
1729 /* Allocate an indirect page */
1730 if ((new_imap
[0] = (struct vs_map
*)
1731 kalloc(CLMAP_THRESHOLD
)) == NULL
) {
1732 kfree(new_imap
, new_map_size
);
1735 new_dmap
= new_imap
[0];
1736 newdsize
= CLMAP_ENTRIES
;
1740 newdsize
= new_size
;
1742 * If the new map is a direct map, then the old map must
1743 * also have been a direct map. All we have to do is
1744 * to allocate a new direct map, copy the old entries
1745 * into it and free the old map.
1747 if ((new_dmap
= (struct vs_map
*)
1748 kalloc(CLMAP_SIZE(new_size
))) == NULL
) {
1754 /* Free the old map */
1755 old_map
= (void *) vs
->vs_dmap
;
1756 old_map_size
= CLMAP_SIZE(vs
->vs_size
);
1758 /* Copy info from the old map into the new map */
1759 memcpy(new_dmap
, vs
->vs_dmap
, old_map_size
);
1761 /* Initialize the rest of the new map */
1762 for (i
= vs
->vs_size
; i
< newdsize
; i
++)
1763 VSM_CLR(new_dmap
[i
]);
1766 vs
->vs_imap
= new_imap
;
1767 vs
->vs_indirect
= TRUE
;
1769 vs
->vs_dmap
= new_dmap
;
1770 bs_commit(new_size
- vs
->vs_size
);
1771 vs
->vs_size
= new_size
;
1773 kfree(old_map
, old_map_size
);
1781 struct clmap
*clmap
,
1786 vm_offset_t cluster
; /* The cluster of offset. */
1787 vm_offset_t newcl
; /* The new cluster allocated. */
1790 struct vs_map
*vsmap
;
1794 ASSERT(vs
->vs_dmap
);
1795 cluster
= atop_32(offset
) >> vs
->vs_clshift
;
1798 * Initialize cluster error value
1800 clmap
->cl_error
= 0;
1803 * If the object has grown, extend the page map.
1805 if (cluster
>= vs
->vs_size
) {
1806 if (flag
== CL_FIND
) {
1807 /* Do not allocate if just doing a lookup */
1809 return (vm_offset_t
) -1;
1811 if (ps_map_extend(vs
, cluster
+ 1)) {
1813 return (vm_offset_t
) -1;
1818 * Look for the desired cluster. If the map is indirect, then we
1819 * have a two level lookup. First find the indirect block, then
1820 * find the actual cluster. If the indirect block has not yet
1821 * been allocated, then do so. If the cluster has not yet been
1822 * allocated, then do so.
1824 * If any of the allocations fail, then return an error.
1825 * Don't allocate if just doing a lookup.
1827 if (vs
->vs_indirect
) {
1828 long ind_block
= cluster
/CLMAP_ENTRIES
;
1830 /* Is the indirect block allocated? */
1831 vsmap
= vs
->vs_imap
[ind_block
];
1832 if (vsmap
== NULL
) {
1833 if (flag
== CL_FIND
) {
1835 return (vm_offset_t
) -1;
1838 /* Allocate the indirect block */
1839 vsmap
= (struct vs_map
*) kalloc(CLMAP_THRESHOLD
);
1840 if (vsmap
== NULL
) {
1842 return (vm_offset_t
) -1;
1844 /* Initialize the cluster offsets */
1845 for (i
= 0; i
< CLMAP_ENTRIES
; i
++)
1847 vs
->vs_imap
[ind_block
] = vsmap
;
1850 vsmap
= vs
->vs_dmap
;
1853 vsmap
+= cluster%CLMAP_ENTRIES
;
1856 * At this point, vsmap points to the struct vs_map desired.
1858 * Look in the map for the cluster, if there was an error on a
1859 * previous write, flag it and return. If it is not yet
1860 * allocated, then allocate it, if we're writing; if we're
1861 * doing a lookup and the cluster's not allocated, return error.
1863 if (VSM_ISERR(*vsmap
)) {
1864 clmap
->cl_error
= VSM_GETERR(*vsmap
);
1866 return (vm_offset_t
) -1;
1867 } else if (VSM_ISCLR(*vsmap
)) {
1870 if (flag
== CL_FIND
) {
1872 * If there's an error and the entry is clear, then
1873 * we've run out of swap space. Record the error
1877 VSM_SETERR(*vsmap
, error
);
1880 return (vm_offset_t
) -1;
1883 * Attempt to allocate a cluster from the paging segment
1885 newcl
= ps_allocate_cluster(vs
, &psindex
,
1886 PAGING_SEGMENT_NULL
);
1887 if (newcl
== (vm_offset_t
) -1) {
1889 return (vm_offset_t
) -1;
1892 VSM_SETCLOFF(*vsmap
, newcl
);
1893 VSM_SETPS(*vsmap
, psindex
);
1896 newcl
= VSM_CLOFF(*vsmap
);
1899 * Fill in pertinent fields of the clmap
1901 clmap
->cl_ps
= VSM_PS(*vsmap
);
1902 clmap
->cl_numpages
= VSCLSIZE(vs
);
1903 clmap
->cl_bmap
.clb_map
= (unsigned int) VSM_BMAP(*vsmap
);
1906 * Byte offset in paging segment is byte offset to cluster plus
1907 * byte offset within cluster. It looks ugly, but should be
1910 ASSERT(trunc_page(offset
) == offset
);
1911 newcl
= ptoa_32(newcl
) << vs
->vs_clshift
;
1912 newoff
= offset
& ((1<<(vm_page_shift
+ vs
->vs_clshift
)) - 1);
1913 if (flag
== CL_ALLOC
) {
1915 * set bits in the allocation bitmap according to which
1916 * pages were requested. size is in bytes.
1918 i
= atop_32(newoff
);
1919 while ((size
> 0) && (i
< VSCLSIZE(vs
))) {
1920 VSM_SETALLOC(*vsmap
, i
);
1922 size
-= vm_page_size
;
1925 clmap
->cl_alloc
.clb_map
= (unsigned int) VSM_ALLOC(*vsmap
);
1928 * Offset is not cluster aligned, so number of pages
1929 * and bitmaps must be adjusted
1931 clmap
->cl_numpages
-= atop_32(newoff
);
1932 CLMAP_SHIFT(clmap
, vs
);
1933 CLMAP_SHIFTALLOC(clmap
, vs
);
1938 * The setting of valid bits and handling of write errors
1939 * must be done here, while we hold the lock on the map.
1940 * It logically should be done in ps_vs_write_complete().
1941 * The size and error information has been passed from
1942 * ps_vs_write_complete(). If the size parameter is non-zero,
1943 * then there is work to be done. If error is also non-zero,
1944 * then the error number is recorded in the cluster and the
1945 * entire cluster is in error.
1947 if (size
&& flag
== CL_FIND
) {
1948 vm_offset_t off
= (vm_offset_t
) 0;
1951 for (i
= VSCLSIZE(vs
) - clmap
->cl_numpages
; size
> 0;
1953 VSM_SETPG(*vsmap
, i
);
1954 size
-= vm_page_size
;
1956 ASSERT(i
<= VSCLSIZE(vs
));
1958 BS_STAT(clmap
->cl_ps
->ps_bs
,
1959 clmap
->cl_ps
->ps_bs
->bs_pages_out_fail
+=
1961 off
= VSM_CLOFF(*vsmap
);
1962 VSM_SETERR(*vsmap
, error
);
1965 * Deallocate cluster if error, and no valid pages
1968 if (off
!= (vm_offset_t
) 0)
1969 ps_deallocate_cluster(clmap
->cl_ps
, off
);
1971 return (vm_offset_t
) 0;
1975 DP_DEBUG(DEBUG_VS_INTERNAL
,
1976 ("returning 0x%X,vs=0x%X,vsmap=0x%X,flag=%d\n",
1977 newcl
+newoff
, (int) vs
, (int) vsmap
, flag
));
1978 DP_DEBUG(DEBUG_VS_INTERNAL
,
1979 (" clmap->cl_ps=0x%X,cl_numpages=%d,clbmap=0x%x,cl_alloc=%x\n",
1980 (int) clmap
->cl_ps
, clmap
->cl_numpages
,
1981 (int) clmap
->cl_bmap
.clb_map
, (int) clmap
->cl_alloc
.clb_map
));
1983 return (newcl
+ newoff
);
1986 void ps_clunmap(vstruct_t
, vm_offset_t
, vm_size_t
); /* forward */
1994 vm_offset_t cluster
; /* The cluster number of offset */
1995 struct vs_map
*vsmap
;
2000 * Loop through all clusters in this range, freeing paging segment
2001 * clusters and map entries as encountered.
2003 while (length
> 0) {
2007 cluster
= atop_32(offset
) >> vs
->vs_clshift
;
2008 if (vs
->vs_indirect
) /* indirect map */
2009 vsmap
= vs
->vs_imap
[cluster
/CLMAP_ENTRIES
];
2011 vsmap
= vs
->vs_dmap
;
2012 if (vsmap
== NULL
) {
2016 vsmap
+= cluster%CLMAP_ENTRIES
;
2017 if (VSM_ISCLR(*vsmap
)) {
2018 length
-= vm_page_size
;
2019 offset
+= vm_page_size
;
2023 * We've got a valid mapping. Clear it and deallocate
2024 * paging segment cluster pages.
2025 * Optimize for entire cluster cleraing.
2027 if ( (newoff
= (offset
&((1<<(vm_page_shift
+vs
->vs_clshift
))-1))) ) {
2029 * Not cluster aligned.
2031 ASSERT(trunc_page(newoff
) == newoff
);
2032 i
= atop_32(newoff
);
2035 while ((i
< VSCLSIZE(vs
)) && (length
> 0)) {
2036 VSM_CLRPG(*vsmap
, i
);
2037 VSM_CLRALLOC(*vsmap
, i
);
2038 length
-= vm_page_size
;
2039 offset
+= vm_page_size
;
2044 * If map entry is empty, clear and deallocate cluster.
2046 if (!VSM_ALLOC(*vsmap
)) {
2047 ps_deallocate_cluster(VSM_PS(*vsmap
),
2056 void ps_vs_write_complete(vstruct_t
, vm_offset_t
, vm_size_t
, int); /* forward */
2059 ps_vs_write_complete(
2068 * Get the struct vsmap for this cluster.
2069 * Use READ, even though it was written, because the
2070 * cluster MUST be present, unless there was an error
2071 * in the original ps_clmap (e.g. no space), in which
2072 * case, nothing happens.
2074 * Must pass enough information to ps_clmap to allow it
2075 * to set the vs_map structure bitmap under lock.
2077 (void) ps_clmap(vs
, offset
, &clmap
, CL_FIND
, size
, error
);
2080 void vs_cl_write_complete(vstruct_t
, paging_segment_t
, vm_offset_t
, vm_offset_t
, vm_size_t
, boolean_t
, int); /* forward */
2083 vs_cl_write_complete(
2085 __unused paging_segment_t ps
,
2087 __unused vm_offset_t addr
,
2092 // kern_return_t kr;
2096 * For internal objects, the error is recorded on a
2097 * per-cluster basis by ps_clmap() which is called
2098 * by ps_vs_write_complete() below.
2100 dprintf(("write failed error = 0x%x\n", error
));
2101 /* add upl_abort code here */
2103 GSTAT(global_stats
.gs_pages_out
+= atop_32(size
));
2105 * Notify the vstruct mapping code, so it can do its accounting.
2107 ps_vs_write_complete(vs
, offset
, size
, error
);
2111 ASSERT(vs
->vs_async_pending
> 0);
2112 vs
->vs_async_pending
-= size
;
2113 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
2114 vs
->vs_waiting_async
= FALSE
;
2116 /* mutex_unlock(&vs->vs_waiting_async); */
2117 thread_wakeup(&vs
->vs_async_pending
);
2124 #ifdef DEVICE_PAGING
2125 kern_return_t
device_write_reply(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2129 MACH_PORT_FACE reply_port
,
2130 kern_return_t device_code
,
2131 io_buf_len_t bytes_written
)
2133 struct vs_async
*vsa
;
2135 vsa
= (struct vs_async
*)
2136 ((struct vstruct_alias
*)(reply_port
->alias
))->vs
;
2138 if (device_code
== KERN_SUCCESS
&& bytes_written
!= vsa
->vsa_size
) {
2139 device_code
= KERN_FAILURE
;
2142 vsa
->vsa_error
= device_code
;
2145 ASSERT(vsa
->vsa_vs
!= VSTRUCT_NULL
);
2146 if(vsa
->vsa_flags
& VSA_TRANSFER
) {
2147 /* revisit when async disk segments redone */
2148 if(vsa
->vsa_error
) {
2149 /* need to consider error condition. re-write data or */
2150 /* throw it away here. */
2151 vm_map_copy_discard((vm_map_copy_t
)vsa
->vsa_addr
);
2153 ps_vs_write_complete(vsa
->vsa_vs
, vsa
->vsa_offset
,
2154 vsa
->vsa_size
, vsa
->vsa_error
);
2156 vs_cl_write_complete(vsa
->vsa_vs
, vsa
->vsa_ps
, vsa
->vsa_offset
,
2157 vsa
->vsa_addr
, vsa
->vsa_size
, TRUE
,
2162 return KERN_SUCCESS
;
2165 kern_return_t
device_write_reply_inband(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2167 device_write_reply_inband(
2168 MACH_PORT_FACE reply_port
,
2169 kern_return_t return_code
,
2170 io_buf_len_t bytes_written
)
2172 panic("device_write_reply_inband: illegal");
2173 return KERN_SUCCESS
;
2176 kern_return_t
device_read_reply(MACH_PORT_FACE
, kern_return_t
, io_buf_ptr_t
, mach_msg_type_number_t
);
2179 MACH_PORT_FACE reply_port
,
2180 kern_return_t return_code
,
2182 mach_msg_type_number_t dataCnt
)
2184 struct vs_async
*vsa
;
2185 vsa
= (struct vs_async
*)
2186 ((struct vstruct_alias
*)(reply_port
->alias
))->vs
;
2187 vsa
->vsa_addr
= (vm_offset_t
)data
;
2188 vsa
->vsa_size
= (vm_size_t
)dataCnt
;
2189 vsa
->vsa_error
= return_code
;
2190 thread_wakeup(&vsa
->vsa_lock
);
2191 return KERN_SUCCESS
;
2194 kern_return_t
device_read_reply_inband(MACH_PORT_FACE
, kern_return_t
, io_buf_ptr_inband_t
, mach_msg_type_number_t
);
2196 device_read_reply_inband(
2197 MACH_PORT_FACE reply_port
,
2198 kern_return_t return_code
,
2199 io_buf_ptr_inband_t data
,
2200 mach_msg_type_number_t dataCnt
)
2202 panic("device_read_reply_inband: illegal");
2203 return KERN_SUCCESS
;
2206 kern_return_t
device_read_reply_overwrite(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2208 device_read_reply_overwrite(
2209 MACH_PORT_FACE reply_port
,
2210 kern_return_t return_code
,
2211 io_buf_len_t bytes_read
)
2213 panic("device_read_reply_overwrite: illegal\n");
2214 return KERN_SUCCESS
;
2217 kern_return_t
device_open_reply(MACH_PORT_FACE
, kern_return_t
, MACH_PORT_FACE
);
2220 MACH_PORT_FACE reply_port
,
2221 kern_return_t return_code
,
2222 MACH_PORT_FACE device_port
)
2224 panic("device_open_reply: illegal\n");
2225 return KERN_SUCCESS
;
2230 paging_segment_t ps
,
2232 vm_offset_t
*bufferp
,
2234 unsigned int *residualp
,
2238 recnum_t dev_offset
;
2239 unsigned int bytes_wanted
;
2240 unsigned int bytes_read
;
2241 unsigned int total_read
;
2242 vm_offset_t dev_buffer
;
2243 vm_offset_t buf_ptr
;
2244 unsigned int records_read
;
2245 struct vs_async
*vsa
;
2246 mutex_t vs_waiting_read_reply
;
2249 vm_map_copy_t device_data
= NULL
;
2250 default_pager_thread_t
*dpt
= NULL
;
2252 device
= dev_port_lookup(ps
->ps_device
);
2253 clustered_reads
[atop_32(size
)]++;
2255 dev_offset
= (ps
->ps_offset
+
2256 (offset
>> (vm_page_shift
- ps
->ps_record_shift
)));
2257 bytes_wanted
= size
;
2259 *bufferp
= (vm_offset_t
)NULL
;
2262 vsa
= VS_ALLOC_ASYNC();
2266 vsa
->vsa_offset
= 0;
2270 mutex_init(&vsa
->vsa_lock
, 0);
2271 ip_lock(vsa
->reply_port
);
2272 vsa
->reply_port
->ip_sorights
++;
2273 ip_reference(vsa
->reply_port
);
2274 ip_unlock(vsa
->reply_port
);
2275 kr
= ds_device_read_common(device
,
2277 (mach_msg_type_name_t
)
2278 MACH_MSG_TYPE_MOVE_SEND_ONCE
,
2282 (IO_READ
| IO_CALL
),
2283 (io_buf_ptr_t
*) &dev_buffer
,
2284 (mach_msg_type_number_t
*) &bytes_read
);
2285 if(kr
== MIG_NO_REPLY
) {
2286 assert_wait(&vsa
->vsa_lock
, THREAD_UNINT
);
2287 thread_block(THREAD_CONTINUE_NULL
);
2289 dev_buffer
= vsa
->vsa_addr
;
2290 bytes_read
= (unsigned int)vsa
->vsa_size
;
2291 kr
= vsa
->vsa_error
;
2294 if (kr
!= KERN_SUCCESS
|| bytes_read
== 0) {
2297 total_read
+= bytes_read
;
2300 * If we got the entire range, use the returned dev_buffer.
2302 if (bytes_read
== size
) {
2303 *bufferp
= (vm_offset_t
)dev_buffer
;
2308 dprintf(("read only %d bytes out of %d\n",
2309 bytes_read
, bytes_wanted
));
2312 dpt
= get_read_buffer();
2313 buf_ptr
= dpt
->dpt_buffer
;
2314 *bufferp
= (vm_offset_t
)buf_ptr
;
2317 * Otherwise, copy the data into the provided buffer (*bufferp)
2318 * and append the rest of the range as it comes in.
2320 memcpy((void *) buf_ptr
, (void *) dev_buffer
, bytes_read
);
2321 buf_ptr
+= bytes_read
;
2322 bytes_wanted
-= bytes_read
;
2323 records_read
= (bytes_read
>>
2324 (vm_page_shift
- ps
->ps_record_shift
));
2325 dev_offset
+= records_read
;
2326 DP_DEBUG(DEBUG_VS_INTERNAL
,
2327 ("calling vm_deallocate(addr=0x%X,size=0x%X)\n",
2328 dev_buffer
, bytes_read
));
2329 if (vm_deallocate(kernel_map
, dev_buffer
, bytes_read
)
2331 Panic("dealloc buf");
2332 } while (bytes_wanted
);
2334 *residualp
= size
- total_read
;
2335 if((dev_buffer
!= *bufferp
) && (total_read
!= 0)) {
2336 vm_offset_t temp_buffer
;
2337 vm_allocate(kernel_map
, &temp_buffer
, total_read
, VM_FLAGS_ANYWHERE
);
2338 memcpy((void *) temp_buffer
, (void *) *bufferp
, total_read
);
2339 if(vm_map_copyin_page_list(kernel_map
, temp_buffer
, total_read
,
2340 VM_MAP_COPYIN_OPT_SRC_DESTROY
|
2341 VM_MAP_COPYIN_OPT_STEAL_PAGES
|
2342 VM_MAP_COPYIN_OPT_PMAP_ENTER
,
2343 (vm_map_copy_t
*)&device_data
, FALSE
))
2344 panic("ps_read_device: cannot copyin locally provided buffer\n");
2346 else if((kr
== KERN_SUCCESS
) && (total_read
!= 0) && (dev_buffer
!= 0)){
2347 if(vm_map_copyin_page_list(kernel_map
, dev_buffer
, bytes_read
,
2348 VM_MAP_COPYIN_OPT_SRC_DESTROY
|
2349 VM_MAP_COPYIN_OPT_STEAL_PAGES
|
2350 VM_MAP_COPYIN_OPT_PMAP_ENTER
,
2351 (vm_map_copy_t
*)&device_data
, FALSE
))
2352 panic("ps_read_device: cannot copyin backing store provided buffer\n");
2357 *bufferp
= (vm_offset_t
)device_data
;
2360 /* Free the receive buffer */
2361 dpt
->checked_out
= 0;
2362 thread_wakeup(&dpt_array
);
2364 return KERN_SUCCESS
;
2369 paging_segment_t ps
,
2373 struct vs_async
*vsa
)
2375 recnum_t dev_offset
;
2376 io_buf_len_t bytes_to_write
, bytes_written
;
2377 recnum_t records_written
;
2379 MACH_PORT_FACE reply_port
;
2383 clustered_writes
[atop_32(size
)]++;
2385 dev_offset
= (ps
->ps_offset
+
2386 (offset
>> (vm_page_shift
- ps
->ps_record_shift
)));
2387 bytes_to_write
= size
;
2391 * Asynchronous write.
2393 reply_port
= vsa
->reply_port
;
2394 ip_lock(reply_port
);
2395 reply_port
->ip_sorights
++;
2396 ip_reference(reply_port
);
2397 ip_unlock(reply_port
);
2400 device
= dev_port_lookup(ps
->ps_device
);
2402 vsa
->vsa_addr
= addr
;
2403 kr
=ds_device_write_common(device
,
2405 (mach_msg_type_name_t
) MACH_MSG_TYPE_MOVE_SEND_ONCE
,
2408 (io_buf_ptr_t
) addr
,
2410 (IO_WRITE
| IO_CALL
),
2413 if ((kr
!= KERN_SUCCESS
) && (kr
!= MIG_NO_REPLY
)) {
2415 dprintf(("%s0x%x, addr=0x%x,"
2416 "size=0x%x,offset=0x%x\n",
2417 "device_write_request returned ",
2418 kr
, addr
, size
, offset
));
2420 ps
->ps_bs
->bs_pages_out_fail
+= atop_32(size
));
2421 /* do the completion notification to free resources */
2422 device_write_reply(reply_port
, kr
, 0);
2427 * Synchronous write.
2431 device
= dev_port_lookup(ps
->ps_device
);
2432 kr
=ds_device_write_common(device
,
2436 (io_buf_ptr_t
) addr
,
2438 (IO_WRITE
| IO_SYNC
| IO_KERNEL_BUF
),
2441 if (kr
!= KERN_SUCCESS
) {
2442 dprintf(("%s0x%x, addr=0x%x,size=0x%x,offset=0x%x\n",
2443 "device_write returned ",
2444 kr
, addr
, size
, offset
));
2446 ps
->ps_bs
->bs_pages_out_fail
+= atop_32(size
));
2449 if (bytes_written
& ((vm_page_size
>> ps
->ps_record_shift
) - 1))
2450 Panic("fragmented write");
2451 records_written
= (bytes_written
>>
2452 (vm_page_shift
- ps
->ps_record_shift
));
2453 dev_offset
+= records_written
;
2455 if (bytes_written
!= bytes_to_write
) {
2456 dprintf(("wrote only %d bytes out of %d\n",
2457 bytes_written
, bytes_to_write
));
2460 bytes_to_write
-= bytes_written
;
2461 addr
+= bytes_written
;
2462 } while (bytes_to_write
> 0);
2464 return PAGER_SUCCESS
;
2468 #else /* !DEVICE_PAGING */
2472 __unused paging_segment_t ps
,
2473 __unused vm_offset_t offset
,
2474 __unused vm_offset_t
*bufferp
,
2475 __unused
unsigned int size
,
2476 __unused
unsigned int *residualp
,
2479 panic("ps_read_device not supported");
2480 return KERN_FAILURE
;
2485 __unused paging_segment_t ps
,
2486 __unused vm_offset_t offset
,
2487 __unused vm_offset_t addr
,
2488 __unused
unsigned int size
,
2489 __unused
struct vs_async
*vsa
)
2491 panic("ps_write_device not supported");
2492 return KERN_FAILURE
;
2495 #endif /* DEVICE_PAGING */
2496 void pvs_object_data_provided(vstruct_t
, upl_t
, upl_offset_t
, upl_size_t
); /* forward */
2499 pvs_object_data_provided(
2500 __unused vstruct_t vs
,
2502 __unused upl_offset_t offset
,
2506 DP_DEBUG(DEBUG_VS_INTERNAL
,
2507 ("buffer=0x%x,offset=0x%x,size=0x%x\n",
2508 upl
, offset
, size
));
2511 GSTAT(global_stats
.gs_pages_in
+= atop_32(size
));
2515 ps_clunmap(vs
, offset
, size
);
2516 #endif /* USE_PRECIOUS */
2523 vm_offset_t vs_offset
,
2527 kern_return_t error
= KERN_SUCCESS
;
2529 unsigned int residual
;
2530 unsigned int request_flags
;
2537 vm_offset_t ps_offset
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
];
2538 paging_segment_t psp
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
];
2541 pages_in_cl
= 1 << vs
->vs_clshift
;
2542 cl_size
= pages_in_cl
* vm_page_size
;
2543 cl_mask
= cl_size
- 1;
2546 * This loop will be executed multiple times until the entire
2547 * request has been satisfied... if the request spans cluster
2548 * boundaries, the clusters will be checked for logical continunity,
2549 * if contiguous the I/O request will span multiple clusters, otherwise
2550 * it will be broken up into the minimal set of I/O's
2552 * If there are holes in a request (either unallocated pages in a paging
2553 * segment or an unallocated paging segment), we stop
2554 * reading at the hole, inform the VM of any data read, inform
2555 * the VM of an unavailable range, then loop again, hoping to
2556 * find valid pages later in the requested range. This continues until
2557 * the entire range has been examined, and read, if present.
2561 request_flags
= UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_PRECIOUS
| UPL_RET_ONLY_ABSENT
;
2563 request_flags
= UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_RET_ONLY_ABSENT
;
2566 assert(dp_encryption_inited
);
2567 if (dp_encryption
) {
2570 * request that the UPL be prepared for
2573 request_flags
|= UPL_ENCRYPT
;
2576 while (cnt
&& (error
== KERN_SUCCESS
)) {
2578 unsigned int page_list_count
;
2580 if((vs_offset
& cl_mask
) &&
2581 (cnt
> (VM_SUPER_CLUSTER
-
2582 (vs_offset
& cl_mask
)))) {
2583 size
= VM_SUPER_CLUSTER
;
2584 size
-= vs_offset
& cl_mask
;
2585 } else if (cnt
> VM_SUPER_CLUSTER
) {
2586 size
= VM_SUPER_CLUSTER
;
2595 while (size
> 0 && error
== KERN_SUCCESS
) {
2600 vm_offset_t cur_offset
;
2603 if ( !ps_info_valid
) {
2604 ps_offset
[seg_index
] = ps_clmap(vs
, vs_offset
& ~cl_mask
, &clmap
, CL_FIND
, 0, 0);
2605 psp
[seg_index
] = CLMAP_PS(clmap
);
2609 * skip over unallocated physical segments
2611 if (ps_offset
[seg_index
] == (vm_offset_t
) -1) {
2612 abort_size
= cl_size
- (vs_offset
& cl_mask
);
2613 abort_size
= MIN(abort_size
, size
);
2615 page_list_count
= 0;
2616 memory_object_super_upl_request(
2618 (memory_object_offset_t
)vs_offset
,
2619 abort_size
, abort_size
,
2620 &upl
, NULL
, &page_list_count
,
2623 if (clmap
.cl_error
) {
2624 upl_abort(upl
, UPL_ABORT_ERROR
);
2626 upl_abort(upl
, UPL_ABORT_UNAVAILABLE
);
2628 upl_deallocate(upl
);
2631 vs_offset
+= abort_size
;
2637 cl_index
= (vs_offset
& cl_mask
) / vm_page_size
;
2639 for (abort_size
= 0; cl_index
< pages_in_cl
&& abort_size
< size
; cl_index
++) {
2641 * skip over unallocated pages
2643 if (CLMAP_ISSET(clmap
, cl_index
))
2645 abort_size
+= vm_page_size
;
2649 * Let VM system know about holes in clusters.
2651 GSTAT(global_stats
.gs_pages_unavail
+= atop_32(abort_size
));
2653 page_list_count
= 0;
2654 memory_object_super_upl_request(
2656 (memory_object_offset_t
)vs_offset
,
2657 abort_size
, abort_size
,
2658 &upl
, NULL
, &page_list_count
,
2661 upl_abort(upl
, UPL_ABORT_UNAVAILABLE
);
2662 upl_deallocate(upl
);
2665 vs_offset
+= abort_size
;
2667 if (cl_index
== pages_in_cl
) {
2669 * if we're at the end of this physical cluster
2670 * then bump to the next one and continue looking
2680 * remember the starting point of the first allocated page
2681 * for the I/O we're about to issue
2683 beg_pseg
= seg_index
;
2684 beg_indx
= cl_index
;
2685 cur_offset
= vs_offset
;
2688 * calculate the size of the I/O that we can do...
2689 * this may span multiple physical segments if
2690 * they are contiguous
2692 for (xfer_size
= 0; xfer_size
< size
; ) {
2694 while (cl_index
< pages_in_cl
2695 && xfer_size
< size
) {
2697 * accumulate allocated pages within
2698 * a physical segment
2700 if (CLMAP_ISSET(clmap
, cl_index
)) {
2701 xfer_size
+= vm_page_size
;
2702 cur_offset
+= vm_page_size
;
2705 BS_STAT(psp
[seg_index
]->ps_bs
,
2706 psp
[seg_index
]->ps_bs
->bs_pages_in
++);
2710 if (cl_index
< pages_in_cl
2711 || xfer_size
>= size
) {
2713 * we've hit an unallocated page or
2714 * the end of this request... go fire
2720 * we've hit the end of the current physical
2721 * segment and there's more to do, so try
2722 * moving to the next one
2726 ps_offset
[seg_index
] =
2728 cur_offset
& ~cl_mask
,
2729 &clmap
, CL_FIND
, 0, 0);
2730 psp
[seg_index
] = CLMAP_PS(clmap
);
2733 if ((ps_offset
[seg_index
- 1] != (ps_offset
[seg_index
] - cl_size
)) || (psp
[seg_index
- 1] != psp
[seg_index
])) {
2735 * if the physical segment we're about
2736 * to step into is not contiguous to
2737 * the one we're currently in, or it's
2738 * in a different paging file, or
2739 * it hasn't been allocated....
2740 * we stop here and generate the I/O
2745 * start with first page of the next physical
2752 * we have a contiguous range of allocated pages
2755 page_list_count
= 0;
2756 memory_object_super_upl_request(vs
->vs_control
,
2757 (memory_object_offset_t
)vs_offset
,
2758 xfer_size
, xfer_size
,
2759 &upl
, NULL
, &page_list_count
,
2760 request_flags
| UPL_SET_INTERNAL
);
2762 error
= ps_read_file(psp
[beg_pseg
],
2763 upl
, (upl_offset_t
) 0,
2764 ps_offset
[beg_pseg
] +
2765 (beg_indx
* vm_page_size
),
2766 xfer_size
, &residual
, 0);
2773 * Adjust counts and send response to VM. Optimize
2774 * for the common case, i.e. no error and/or partial
2775 * data. If there was an error, then we need to error
2776 * the entire range, even if some data was successfully
2777 * read. If there was a partial read we may supply some
2778 * data and may error some as well. In all cases the
2779 * VM must receive some notification for every page
2782 if ((error
== KERN_SUCCESS
) && (residual
== 0)) {
2784 * Got everything we asked for, supply the data
2785 * to the VM. Note that as a side effect of
2786 * supplying the data, the buffer holding the
2787 * supplied data is deallocated from the pager's
2790 pvs_object_data_provided(
2791 vs
, upl
, vs_offset
, xfer_size
);
2793 failed_size
= xfer_size
;
2795 if (error
== KERN_SUCCESS
) {
2796 if ((signed) residual
== xfer_size
) {
2798 * If a read operation returns no error
2799 * and no data moved, we turn it into
2800 * an error, assuming we're reading at
2802 * Fall through and error the entire
2805 error
= KERN_FAILURE
;
2808 * Otherwise, we have partial read. If
2809 * the part read is a integral number
2810 * of pages supply it. Otherwise round
2811 * it up to a page boundary, zero fill
2812 * the unread part, and supply it.
2813 * Fall through and error the remainder
2814 * of the range, if any.
2820 lsize
= (xfer_size
- residual
)
2822 pvs_object_data_provided(
2826 if (lsize
< xfer_size
) {
2829 error
= KERN_FAILURE
;
2835 * If there was an error in any part of the range, tell
2836 * the VM. Note that error is explicitly checked again
2837 * since it can be modified above.
2839 if (error
!= KERN_SUCCESS
) {
2840 BS_STAT(psp
[beg_pseg
]->ps_bs
,
2841 psp
[beg_pseg
]->ps_bs
->bs_pages_in_fail
2842 += atop_32(failed_size
));
2845 vs_offset
+= xfer_size
;
2848 } /* END while (cnt && (error == 0)) */
2852 int vs_do_async_write
= 1;
2858 upl_offset_t offset
,
2860 boolean_t dp_internal
,
2863 upl_size_t transfer_size
;
2867 vm_offset_t actual_offset
; /* Offset within paging segment */
2868 paging_segment_t ps
;
2869 vm_offset_t mobj_base_addr
;
2870 vm_offset_t mobj_target_addr
;
2873 upl_page_info_t
*pl
;
2877 unsigned int cl_size
;
2879 unsigned int seg_size
;
2881 pages_in_cl
= 1 << vs
->vs_clshift
;
2882 cl_size
= pages_in_cl
* vm_page_size
;
2885 unsigned int page_list_count
;
2887 unsigned int super_size
;
2892 upl_offset_t upl_offset
;
2893 vm_offset_t seg_offset
;
2894 vm_offset_t ps_offset
[((VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
) + 1];
2895 paging_segment_t psp
[((VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
) + 1];
2899 super_size
= cl_size
;
2901 request_flags
= UPL_NOBLOCK
|
2902 UPL_RET_ONLY_DIRTY
| UPL_COPYOUT_FROM
|
2903 UPL_NO_SYNC
| UPL_SET_INTERNAL
;
2905 super_size
= VM_SUPER_CLUSTER
;
2907 request_flags
= UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
|
2908 UPL_RET_ONLY_DIRTY
| UPL_COPYOUT_FROM
|
2909 UPL_NO_SYNC
| UPL_SET_INTERNAL
;
2912 if (!dp_encryption_inited
) {
2915 * Once we've started using swap, we
2916 * can't change our mind on whether
2917 * it needs to be encrypted or
2920 dp_encryption_inited
= TRUE
;
2922 if (dp_encryption
) {
2925 * request that the UPL be prepared for
2928 request_flags
|= UPL_ENCRYPT
;
2929 flags
|= UPL_PAGING_ENCRYPTED
;
2932 page_list_count
= 0;
2933 memory_object_super_upl_request(vs
->vs_control
,
2934 (memory_object_offset_t
)offset
,
2936 &upl
, NULL
, &page_list_count
,
2937 request_flags
| UPL_FOR_PAGEOUT
);
2939 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2941 seg_size
= cl_size
- (upl
->offset
% cl_size
);
2942 upl_offset
= upl
->offset
& ~(cl_size
- 1);
2944 for (seg_index
= 0, transfer_size
= upl
->size
;
2945 transfer_size
> 0; ) {
2946 ps_offset
[seg_index
] =
2952 if (ps_offset
[seg_index
] == (vm_offset_t
) -1) {
2954 upl_deallocate(upl
);
2956 return KERN_FAILURE
;
2959 psp
[seg_index
] = CLMAP_PS(clmap
);
2961 if (transfer_size
> seg_size
) {
2962 transfer_size
-= seg_size
;
2963 upl_offset
+= cl_size
;
2970 * Ignore any non-present pages at the end of the
2973 for (page_index
= upl
->size
/ vm_page_size
; page_index
> 0;)
2974 if (UPL_PAGE_PRESENT(pl
, --page_index
))
2976 num_of_pages
= page_index
+ 1;
2978 base_index
= (upl
->offset
% cl_size
) / PAGE_SIZE
;
2980 for (page_index
= 0; page_index
< num_of_pages
; ) {
2982 * skip over non-dirty pages
2984 for ( ; page_index
< num_of_pages
; page_index
++) {
2985 if (UPL_DIRTY_PAGE(pl
, page_index
)
2986 || UPL_PRECIOUS_PAGE(pl
, page_index
))
2988 * this is a page we need to write
2989 * go see if we can buddy it up with
2990 * others that are contiguous to it
2994 * if the page is not-dirty, but present we
2995 * need to commit it... This is an unusual
2996 * case since we only asked for dirty pages
2998 if (UPL_PAGE_PRESENT(pl
, page_index
)) {
2999 boolean_t empty
= FALSE
;
3000 upl_commit_range(upl
,
3001 page_index
* vm_page_size
,
3003 UPL_COMMIT_NOTIFY_EMPTY
,
3008 assert(page_index
==
3010 upl_deallocate(upl
);
3014 if (page_index
== num_of_pages
)
3016 * no more pages to look at, we're out of here
3021 * gather up contiguous dirty pages... we have at
3022 * least 1 * otherwise we would have bailed above
3023 * make sure that each physical segment that we step
3024 * into is contiguous to the one we're currently in
3025 * if it's not, we have to stop and write what we have
3027 for (first_dirty
= page_index
;
3028 page_index
< num_of_pages
; ) {
3029 if ( !UPL_DIRTY_PAGE(pl
, page_index
)
3030 && !UPL_PRECIOUS_PAGE(pl
, page_index
))
3034 * if we just looked at the last page in the UPL
3035 * we don't need to check for physical segment
3038 if (page_index
< num_of_pages
) {
3042 cur_seg
= (base_index
+ (page_index
- 1))/pages_in_cl
;
3043 nxt_seg
= (base_index
+ page_index
)/pages_in_cl
;
3045 if (cur_seg
!= nxt_seg
) {
3046 if ((ps_offset
[cur_seg
] != (ps_offset
[nxt_seg
] - cl_size
)) || (psp
[cur_seg
] != psp
[nxt_seg
]))
3048 * if the segment we're about
3049 * to step into is not
3050 * contiguous to the one we're
3051 * currently in, or it's in a
3052 * different paging file....
3053 * we stop here and generate
3060 num_dirty
= page_index
- first_dirty
;
3063 upl_offset
= first_dirty
* vm_page_size
;
3064 transfer_size
= num_dirty
* vm_page_size
;
3066 while (transfer_size
) {
3068 if ((seg_size
= cl_size
-
3069 ((upl
->offset
+ upl_offset
) % cl_size
))
3071 seg_size
= transfer_size
;
3073 ps_vs_write_complete(vs
,
3074 upl
->offset
+ upl_offset
,
3077 transfer_size
-= seg_size
;
3078 upl_offset
+= seg_size
;
3080 upl_offset
= first_dirty
* vm_page_size
;
3081 transfer_size
= num_dirty
* vm_page_size
;
3083 seg_index
= (base_index
+ first_dirty
) / pages_in_cl
;
3084 seg_offset
= (upl
->offset
+ upl_offset
) % cl_size
;
3086 error
= ps_write_file(psp
[seg_index
],
3088 ps_offset
[seg_index
]
3090 transfer_size
, flags
);
3092 boolean_t empty
= FALSE
;
3093 upl_abort_range(upl
,
3094 first_dirty
* vm_page_size
,
3095 num_dirty
* vm_page_size
,
3096 UPL_ABORT_NOTIFY_EMPTY
,
3099 assert(page_index
== num_of_pages
);
3100 upl_deallocate(upl
);
3106 assert(cnt
<= (vm_page_size
<< vs
->vs_clshift
));
3110 /* The caller provides a mapped_data which is derived */
3111 /* from a temporary object. The targeted pages are */
3112 /* guaranteed to be set at offset 0 in the mapped_data */
3113 /* The actual offset however must still be derived */
3114 /* from the offset in the vs in question */
3115 mobj_base_addr
= offset
;
3116 mobj_target_addr
= mobj_base_addr
;
3118 for (transfer_size
= list_size
; transfer_size
!= 0;) {
3119 actual_offset
= ps_clmap(vs
, mobj_target_addr
,
3121 transfer_size
< cl_size
?
3122 transfer_size
: cl_size
, 0);
3123 if(actual_offset
== (vm_offset_t
) -1) {
3127 cnt
= MIN(transfer_size
,
3128 CLMAP_NPGS(clmap
) * vm_page_size
);
3129 ps
= CLMAP_PS(clmap
);
3130 /* Assume that the caller has given us contiguous */
3133 ps_vs_write_complete(vs
, mobj_target_addr
,
3135 error
= ps_write_file(ps
, internal_upl
,
3143 actual_offset
+= cnt
;
3144 mobj_target_addr
+= cnt
;
3145 transfer_size
-= cnt
;
3153 return KERN_FAILURE
;
3155 return KERN_SUCCESS
;
3159 ps_vstruct_allocated_size(
3163 struct vs_map
*vsmap
;
3164 unsigned int i
, j
, k
;
3167 if (vs
->vs_indirect
) {
3168 /* loop on indirect maps */
3169 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3170 vsmap
= vs
->vs_imap
[i
];
3173 /* loop on clusters in this indirect map */
3174 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3175 if (VSM_ISCLR(vsmap
[j
]) ||
3176 VSM_ISERR(vsmap
[j
]))
3178 /* loop on pages in this cluster */
3179 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3180 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
))
3186 vsmap
= vs
->vs_dmap
;
3189 /* loop on clusters in the direct map */
3190 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3191 if (VSM_ISCLR(vsmap
[j
]) ||
3192 VSM_ISERR(vsmap
[j
]))
3194 /* loop on pages in this cluster */
3195 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3196 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
))
3202 return ptoa_32(num_pages
);
3206 ps_vstruct_allocated_pages(
3208 default_pager_page_t
*pages
,
3211 unsigned int num_pages
;
3212 struct vs_map
*vsmap
;
3214 unsigned int i
, j
, k
;
3218 if (vs
->vs_indirect
) {
3219 /* loop on indirect maps */
3220 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3221 vsmap
= vs
->vs_imap
[i
];
3222 if (vsmap
== NULL
) {
3223 offset
+= (vm_page_size
* CLMAP_ENTRIES
*
3227 /* loop on clusters in this indirect map */
3228 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3229 if (VSM_ISCLR(vsmap
[j
]) ||
3230 VSM_ISERR(vsmap
[j
])) {
3231 offset
+= vm_page_size
* VSCLSIZE(vs
);
3234 /* loop on pages in this cluster */
3235 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3236 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
)) {
3238 if (num_pages
< pages_size
)
3239 pages
++->dpp_offset
=
3242 offset
+= vm_page_size
;
3247 vsmap
= vs
->vs_dmap
;
3250 /* loop on clusters in the direct map */
3251 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3252 if (VSM_ISCLR(vsmap
[j
]) ||
3253 VSM_ISERR(vsmap
[j
])) {
3254 offset
+= vm_page_size
* VSCLSIZE(vs
);
3257 /* loop on pages in this cluster */
3258 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3259 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
)) {
3261 if (num_pages
< pages_size
)
3262 pages
++->dpp_offset
= offset
;
3264 offset
+= vm_page_size
;
3274 ps_vstruct_transfer_from_segment(
3276 paging_segment_t segment
,
3279 struct vs_map
*vsmap
;
3280 // struct vs_map old_vsmap;
3281 // struct vs_map new_vsmap;
3284 VS_LOCK(vs
); /* block all work on this vstruct */
3285 /* can't allow the normal multiple write */
3286 /* semantic because writes may conflict */
3287 vs
->vs_xfer_pending
= TRUE
;
3288 vs_wait_for_sync_writers(vs
);
3290 vs_wait_for_readers(vs
);
3291 /* we will unlock the vs to allow other writes while transferring */
3292 /* and will be guaranteed of the persistance of the vs struct */
3293 /* because the caller of ps_vstruct_transfer_from_segment bumped */
3294 /* vs_async_pending */
3295 /* OK we now have guaranteed no other parties are accessing this */
3296 /* vs. Now that we are also supporting simple lock versions of */
3297 /* vs_lock we cannot hold onto VS_LOCK as we may block below. */
3298 /* our purpose in holding it before was the multiple write case */
3299 /* we now use the boolean xfer_pending to do that. We can use */
3300 /* a boolean instead of a count because we have guaranteed single */
3301 /* file access to this code in its caller */
3304 if (vs
->vs_indirect
) {
3305 unsigned int vsmap_size
;
3307 /* loop on indirect maps */
3308 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3309 vsmap
= vs
->vs_imap
[i
];
3312 /* loop on clusters in this indirect map */
3313 clmap_off
= (vm_page_size
* CLMAP_ENTRIES
*
3315 if(i
+1 == INDIRECT_CLMAP_ENTRIES(vs
->vs_size
))
3316 vsmap_size
= vs
->vs_size
- (CLMAP_ENTRIES
* i
);
3318 vsmap_size
= CLMAP_ENTRIES
;
3319 for (j
= 0; j
< vsmap_size
; j
++) {
3320 if (VSM_ISCLR(vsmap
[j
]) ||
3321 VSM_ISERR(vsmap
[j
]) ||
3322 (VSM_PS(vsmap
[j
]) != segment
))
3324 if(vs_cluster_transfer(vs
,
3325 (vm_page_size
* (j
<< vs
->vs_clshift
))
3327 vm_page_size
<< vs
->vs_clshift
,
3331 vs
->vs_xfer_pending
= FALSE
;
3333 vs_finish_write(vs
);
3334 return KERN_FAILURE
;
3336 /* allow other readers/writers during transfer*/
3338 vs
->vs_xfer_pending
= FALSE
;
3340 vs_finish_write(vs
);
3342 vs
->vs_xfer_pending
= TRUE
;
3343 vs_wait_for_sync_writers(vs
);
3345 vs_wait_for_readers(vs
);
3347 if (!(vs
->vs_indirect
)) {
3353 vsmap
= vs
->vs_dmap
;
3354 if (vsmap
== NULL
) {
3356 vs
->vs_xfer_pending
= FALSE
;
3358 vs_finish_write(vs
);
3359 return KERN_SUCCESS
;
3361 /* loop on clusters in the direct map */
3362 for (j
= 0; j
< vs
->vs_size
; j
++) {
3363 if (VSM_ISCLR(vsmap
[j
]) ||
3364 VSM_ISERR(vsmap
[j
]) ||
3365 (VSM_PS(vsmap
[j
]) != segment
))
3367 if(vs_cluster_transfer(vs
,
3368 vm_page_size
* (j
<< vs
->vs_clshift
),
3369 vm_page_size
<< vs
->vs_clshift
,
3370 upl
) != KERN_SUCCESS
) {
3372 vs
->vs_xfer_pending
= FALSE
;
3374 vs_finish_write(vs
);
3375 return KERN_FAILURE
;
3377 /* allow other readers/writers during transfer*/
3379 vs
->vs_xfer_pending
= FALSE
;
3381 vs_finish_write(vs
);
3383 vs
->vs_xfer_pending
= TRUE
;
3385 vs_wait_for_sync_writers(vs
);
3387 vs_wait_for_readers(vs
);
3388 if (vs
->vs_indirect
) {
3395 vs
->vs_xfer_pending
= FALSE
;
3397 vs_finish_write(vs
);
3398 return KERN_SUCCESS
;
3408 struct vs_map
*vsmap
;
3409 vm_offset_t cluster
;
3411 cluster
= atop_32(offset
) >> vs
->vs_clshift
;
3412 if (vs
->vs_indirect
) {
3413 long ind_block
= cluster
/CLMAP_ENTRIES
;
3415 /* Is the indirect block allocated? */
3416 vsmap
= vs
->vs_imap
[ind_block
];
3417 if(vsmap
== (vs_map_t
) NULL
)
3420 vsmap
= vs
->vs_dmap
;
3421 vsmap
+= cluster%CLMAP_ENTRIES
;
3426 vs_cluster_transfer(
3432 vm_offset_t actual_offset
;
3433 paging_segment_t ps
;
3435 kern_return_t error
= KERN_SUCCESS
;
3436 unsigned int size
, size_wanted
;
3438 unsigned int residual
= 0;
3439 unsigned int unavail_size
;
3440 // default_pager_thread_t *dpt;
3441 // boolean_t dealloc;
3442 struct vs_map
*vsmap_ptr
= NULL
;
3443 struct vs_map read_vsmap
;
3444 struct vs_map original_read_vsmap
;
3445 struct vs_map write_vsmap
;
3447 // vm_offset_t ioaddr;
3449 /* vs_cluster_transfer reads in the pages of a cluster and
3450 * then writes these pages back to new backing store. The
3451 * segment the pages are being read from is assumed to have
3452 * been taken off-line and is no longer considered for new
3457 * This loop will be executed once per cluster referenced.
3458 * Typically this means once, since it's unlikely that the
3459 * VM system will ask for anything spanning cluster boundaries.
3461 * If there are holes in a cluster (in a paging segment), we stop
3462 * reading at the hole, then loop again, hoping to
3463 * find valid pages later in the cluster. This continues until
3464 * the entire range has been examined, and read, if present. The
3465 * pages are written as they are read. If a failure occurs after
3466 * some pages are written the unmap call at the bottom of the loop
3467 * recovers the backing store and the old backing store remains
3471 VSM_CLR(write_vsmap
);
3472 VSM_CLR(original_read_vsmap
);
3473 /* grab the actual object's pages to sync with I/O */
3474 while (cnt
&& (error
== KERN_SUCCESS
)) {
3475 vsmap_ptr
= vs_get_map_entry(vs
, offset
);
3476 actual_offset
= ps_clmap(vs
, offset
, &clmap
, CL_FIND
, 0, 0);
3478 if (actual_offset
== (vm_offset_t
) -1) {
3481 * Nothing left to write in this cluster at least
3482 * set write cluster information for any previous
3483 * write, clear for next cluster, if there is one
3485 unsigned int local_size
, clmask
, clsize
;
3487 clsize
= vm_page_size
<< vs
->vs_clshift
;
3488 clmask
= clsize
- 1;
3489 local_size
= clsize
- (offset
& clmask
);
3491 local_size
= MIN(local_size
, cnt
);
3493 /* This cluster has no data in it beyond what may */
3494 /* have been found on a previous iteration through */
3495 /* the loop "write_vsmap" */
3496 *vsmap_ptr
= write_vsmap
;
3497 VSM_CLR(write_vsmap
);
3498 VSM_CLR(original_read_vsmap
);
3501 offset
+= local_size
;
3506 * Count up contiguous available or unavailable
3509 ps
= CLMAP_PS(clmap
);
3514 (size
< cnt
) && (unavail_size
< cnt
) &&
3515 (i
< CLMAP_NPGS(clmap
)); i
++) {
3516 if (CLMAP_ISSET(clmap
, i
)) {
3517 if (unavail_size
!= 0)
3519 size
+= vm_page_size
;
3521 ps
->ps_bs
->bs_pages_in
++);
3525 unavail_size
+= vm_page_size
;
3530 ASSERT(unavail_size
);
3531 cnt
-= unavail_size
;
3532 offset
+= unavail_size
;
3533 if((offset
& ((vm_page_size
<< vs
->vs_clshift
) - 1))
3535 /* There is no more to transfer in this
3538 *vsmap_ptr
= write_vsmap
;
3539 VSM_CLR(write_vsmap
);
3540 VSM_CLR(original_read_vsmap
);
3545 if(VSM_ISCLR(original_read_vsmap
))
3546 original_read_vsmap
= *vsmap_ptr
;
3548 if(ps
->ps_segtype
== PS_PARTITION
) {
3549 panic("swap partition not supported\n");
3551 error
= KERN_FAILURE
;
3554 NEED TO ISSUE WITH SYNC & NO COMMIT
3555 error = ps_read_device(ps, actual_offset, &buffer,
3556 size, &residual, flags);
3559 /* NEED TO ISSUE WITH SYNC & NO COMMIT */
3560 error
= ps_read_file(ps
, upl
, (upl_offset_t
) 0, actual_offset
,
3562 (UPL_IOSYNC
| UPL_NOCOMMIT
));
3565 read_vsmap
= *vsmap_ptr
;
3569 * Adjust counts and put data in new BS. Optimize for the
3570 * common case, i.e. no error and/or partial data.
3571 * If there was an error, then we need to error the entire
3572 * range, even if some data was successfully read.
3575 if ((error
== KERN_SUCCESS
) && (residual
== 0)) {
3578 * Got everything we asked for, supply the data to
3579 * the new BS. Note that as a side effect of supplying
3580 * the data, the buffer holding the supplied data is
3581 * deallocated from the pager's address space unless
3582 * the write is unsuccessful.
3585 /* note buffer will be cleaned up in all cases by */
3586 /* internal_cluster_write or if an error on write */
3587 /* the vm_map_copy_page_discard call */
3588 *vsmap_ptr
= write_vsmap
;
3590 if(vs_cluster_write(vs
, upl
, offset
,
3591 size
, TRUE
, UPL_IOSYNC
| UPL_NOCOMMIT
) != KERN_SUCCESS
) {
3592 error
= KERN_FAILURE
;
3593 if(!(VSM_ISCLR(*vsmap_ptr
))) {
3594 /* unmap the new backing store object */
3595 ps_clunmap(vs
, offset
, size
);
3597 /* original vsmap */
3598 *vsmap_ptr
= original_read_vsmap
;
3599 VSM_CLR(write_vsmap
);
3601 if((offset
+ size
) &
3602 ((vm_page_size
<< vs
->vs_clshift
)
3604 /* There is more to transfer in this
3607 write_vsmap
= *vsmap_ptr
;
3608 *vsmap_ptr
= read_vsmap
;
3610 /* discard the old backing object */
3611 write_vsmap
= *vsmap_ptr
;
3612 *vsmap_ptr
= read_vsmap
;
3613 ps_clunmap(vs
, offset
, size
);
3614 *vsmap_ptr
= write_vsmap
;
3615 VSM_CLR(write_vsmap
);
3616 VSM_CLR(original_read_vsmap
);
3621 if (error
== KERN_SUCCESS
) {
3622 if (residual
== size
) {
3624 * If a read operation returns no error
3625 * and no data moved, we turn it into
3626 * an error, assuming we're reading at
3628 * Fall through and error the entire
3631 error
= KERN_FAILURE
;
3632 *vsmap_ptr
= write_vsmap
;
3633 if(!(VSM_ISCLR(*vsmap_ptr
))) {
3634 /* unmap the new backing store object */
3635 ps_clunmap(vs
, offset
, size
);
3637 *vsmap_ptr
= original_read_vsmap
;
3638 VSM_CLR(write_vsmap
);
3642 * Otherwise, we have partial read.
3643 * This is also considered an error
3644 * for the purposes of cluster transfer
3646 error
= KERN_FAILURE
;
3647 *vsmap_ptr
= write_vsmap
;
3648 if(!(VSM_ISCLR(*vsmap_ptr
))) {
3649 /* unmap the new backing store object */
3650 ps_clunmap(vs
, offset
, size
);
3652 *vsmap_ptr
= original_read_vsmap
;
3653 VSM_CLR(write_vsmap
);
3662 } /* END while (cnt && (error == 0)) */
3663 if(!VSM_ISCLR(write_vsmap
))
3664 *vsmap_ptr
= write_vsmap
;
3670 default_pager_add_file(
3671 MACH_PORT_FACE backing_store
,
3677 paging_segment_t ps
;
3682 if ((bs
= backing_store_lookup(backing_store
))
3683 == BACKING_STORE_NULL
)
3684 return KERN_INVALID_ARGUMENT
;
3687 for (i
= 0; i
<= paging_segment_max
; i
++) {
3688 ps
= paging_segments
[i
];
3689 if (ps
== PAGING_SEGMENT_NULL
)
3691 if (ps
->ps_segtype
!= PS_FILE
)
3695 * Check for overlap on same device.
3697 if (ps
->ps_vnode
== (struct vnode
*)vp
) {
3700 return KERN_INVALID_ARGUMENT
;
3706 * Set up the paging segment
3708 ps
= (paging_segment_t
) kalloc(sizeof (struct paging_segment
));
3709 if (ps
== PAGING_SEGMENT_NULL
) {
3711 return KERN_RESOURCE_SHORTAGE
;
3714 ps
->ps_segtype
= PS_FILE
;
3715 ps
->ps_vnode
= (struct vnode
*)vp
;
3717 ps
->ps_record_shift
= local_log2(vm_page_size
/ record_size
);
3718 ps
->ps_recnum
= size
;
3719 ps
->ps_pgnum
= size
>> ps
->ps_record_shift
;
3721 ps
->ps_pgcount
= ps
->ps_pgnum
;
3722 ps
->ps_clshift
= local_log2(bs
->bs_clsize
);
3723 ps
->ps_clcount
= ps
->ps_ncls
= ps
->ps_pgcount
>> ps
->ps_clshift
;
3727 ps
->ps_bmap
= (unsigned char *) kalloc(RMAPSIZE(ps
->ps_ncls
));
3729 kfree(ps
, sizeof *ps
);
3731 return KERN_RESOURCE_SHORTAGE
;
3733 for (j
= 0; j
< ps
->ps_ncls
; j
++) {
3734 clrbit(ps
->ps_bmap
, j
);
3737 ps
->ps_going_away
= FALSE
;
3740 if ((error
= ps_enter(ps
)) != 0) {
3741 kfree(ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
3742 kfree(ps
, sizeof *ps
);
3744 return KERN_RESOURCE_SHORTAGE
;
3747 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
3748 bs
->bs_pages_total
+= ps
->ps_clcount
<< ps
->ps_clshift
;
3750 dp_pages_free
+= ps
->ps_pgcount
;
3755 bs_more_space(ps
->ps_clcount
);
3757 DP_DEBUG(DEBUG_BS_INTERNAL
,
3758 ("device=0x%x,offset=0x%x,count=0x%x,record_size=0x%x,shift=%d,total_size=0x%x\n",
3759 device
, offset
, size
, record_size
,
3760 ps
->ps_record_shift
, ps
->ps_pgnum
));
3762 return KERN_SUCCESS
;
3769 paging_segment_t ps
,
3771 upl_offset_t upl_offset
,
3774 unsigned int *residualp
,
3777 vm_object_offset_t f_offset
;
3781 assert(dp_encryption_inited
);
3783 clustered_reads
[atop_32(size
)]++;
3785 f_offset
= (vm_object_offset_t
)(ps
->ps_offset
+ offset
);
3787 /* for transfer case we need to pass uploffset and flags */
3788 error
= vnode_pagein(ps
->ps_vnode
,
3789 upl
, upl_offset
, f_offset
, (vm_size_t
)size
, flags
| UPL_NORDAHEAD
, NULL
);
3791 /* The vnode_pagein semantic is somewhat at odds with the existing */
3792 /* device_read semantic. Partial reads are not experienced at this */
3793 /* level. It is up to the bit map code and cluster read code to */
3794 /* check that requested data locations are actually backed, and the */
3795 /* pagein code to either read all of the requested data or return an */
3799 result
= KERN_FAILURE
;
3802 result
= KERN_SUCCESS
;
3809 paging_segment_t ps
,
3811 upl_offset_t upl_offset
,
3816 vm_object_offset_t f_offset
;
3817 kern_return_t result
;
3819 assert(dp_encryption_inited
);
3821 clustered_writes
[atop_32(size
)]++;
3822 f_offset
= (vm_object_offset_t
)(ps
->ps_offset
+ offset
);
3824 if (flags
& UPL_PAGING_ENCRYPTED
) {
3827 * encrypt all the pages that we're going
3830 upl_encrypt(upl
, upl_offset
, size
);
3833 if (vnode_pageout(ps
->ps_vnode
,
3834 upl
, upl_offset
, f_offset
, (vm_size_t
)size
, flags
, NULL
))
3835 result
= KERN_FAILURE
;
3837 result
= KERN_SUCCESS
;
3843 default_pager_triggers( __unused MACH_PORT_FACE default_pager
,
3847 MACH_PORT_FACE trigger_port
)
3849 MACH_PORT_FACE release
;
3853 if (flags
== SWAP_ENCRYPT_ON
) {
3854 /* ENCRYPTED SWAP: turn encryption on */
3855 release
= trigger_port
;
3856 if (!dp_encryption_inited
) {
3857 dp_encryption_inited
= TRUE
;
3858 dp_encryption
= TRUE
;
3863 } else if (flags
== SWAP_ENCRYPT_OFF
) {
3864 /* ENCRYPTED SWAP: turn encryption off */
3865 release
= trigger_port
;
3866 if (!dp_encryption_inited
) {
3867 dp_encryption_inited
= TRUE
;
3868 dp_encryption
= FALSE
;
3873 } else if (flags
== HI_WAT_ALERT
) {
3874 release
= min_pages_trigger_port
;
3875 min_pages_trigger_port
= trigger_port
;
3876 minimum_pages_remaining
= hi_wat
/vm_page_size
;
3879 } else if (flags
== LO_WAT_ALERT
) {
3880 release
= max_pages_trigger_port
;
3881 max_pages_trigger_port
= trigger_port
;
3882 maximum_pages_free
= lo_wat
/vm_page_size
;
3885 release
= trigger_port
;
3886 kr
= KERN_INVALID_ARGUMENT
;
3890 if (IP_VALID(release
))
3891 ipc_port_release_send(release
);
3897 * Monitor the amount of available backing store vs. the amount of
3898 * required backing store, notify a listener (if present) when
3899 * backing store may safely be removed.
3901 * We attempt to avoid the situation where backing store is
3902 * discarded en masse, as this can lead to thrashing as the
3903 * backing store is compacted.
3906 #define PF_INTERVAL 3 /* time between free level checks */
3907 #define PF_LATENCY 10 /* number of intervals before release */
3909 static int dp_pages_free_low_count
= 0;
3910 thread_call_t default_pager_backing_store_monitor_callout
;
3913 default_pager_backing_store_monitor(__unused thread_call_param_t p1
,
3914 __unused thread_call_param_t p2
)
3916 // unsigned long long average;
3921 * We determine whether it will be safe to release some
3922 * backing store by watching the free page level. If
3923 * it remains below the maximum_pages_free threshold for
3924 * at least PF_LATENCY checks (taken at PF_INTERVAL seconds)
3925 * then we deem it safe.
3927 * Note that this establishes a maximum rate at which backing
3928 * store will be released, as each notification (currently)
3929 * only results in a single backing store object being
3932 if (dp_pages_free
> maximum_pages_free
) {
3933 dp_pages_free_low_count
++;
3935 dp_pages_free_low_count
= 0;
3938 /* decide whether to send notification */
3940 if (max_pages_trigger_port
&&
3941 (backing_store_release_trigger_disable
== 0) &&
3942 (dp_pages_free_low_count
> PF_LATENCY
)) {
3943 trigger
= max_pages_trigger_port
;
3944 max_pages_trigger_port
= NULL
;
3947 /* send notification */
3948 if (trigger
!= IP_NULL
) {
3950 if(backing_store_release_trigger_disable
!= 0) {
3951 assert_wait((event_t
)
3952 &backing_store_release_trigger_disable
,
3955 thread_block(THREAD_CONTINUE_NULL
);
3959 default_pager_space_alert(trigger
, LO_WAT_ALERT
);
3960 ipc_port_release_send(trigger
);
3961 dp_pages_free_low_count
= 0;
3964 clock_interval_to_deadline(PF_INTERVAL
, NSEC_PER_SEC
, &deadline
);
3965 thread_call_enter_delayed(default_pager_backing_store_monitor_callout
, deadline
);