2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * This module maintains information about the presence of
61 * pages not in memory. Since an external memory object
62 * must maintain a complete knowledge of its contents, this
63 * information takes the form of hints.
65 #include <string.h> /* for memcpy()/memset() */
67 #include <mach/boolean.h>
68 #include <vm/vm_external.h>
69 #include <kern/kalloc.h>
70 #include <mach/vm_param.h>
71 #include <kern/assert.h>
74 * The implementation uses bit arrays to record whether
75 * a page has been written to external storage. For
76 * convenience, these bit arrays come in various sizes.
77 * For example, a map N bytes long can record:
79 * 16 bytes = 128 pages = (@ 4KB/page) 512KB
80 * 1024 bytes = 8192 pages = (@ 4KB/page) 32MB
81 * 4096 bytes = 32768 pages = (@ 4KB/page) 128MB
83 * For a 32-bit machine with 4KB pages, the largest size
84 * would be 128KB = 32 pages. Machines with a larger page
85 * size are more efficient.
87 * This subsystem must be very careful about memory allocation,
88 * since vm_external_create() is almost always called with
89 * vm_privilege set. The largest map to be allocated must be less
90 * than or equal to a single page, and the kalloc subsystem must
91 * never allocate more than a single page in response to a kalloc()
92 * request. Also, vm_external_destroy() must not take any blocking
93 * locks, since it is called with a vm_object lock held. This
94 * implies that kfree() MUST be implemented in terms of zfree()
95 * NOT kmem_free() for all request sizes that this subsystem uses.
97 * For efficiency, this subsystem knows that the kalloc() subsystem
98 * is implemented in terms of power-of-2 allocation, and that the
99 * minimum allocation unit is KALLOC_MINSIZE
102 * Should consider using existence_map to hold bits directly
103 * when existence_size <= 4 bytes (i.e., 32 pages).
106 #define SMALL_SIZE KALLOC_MINSIZE
107 #define LARGE_SIZE PAGE_SIZE
109 static vm_size_t
power_of_2(vm_size_t size
);
112 power_of_2(vm_size_t size
)
116 power
= 2 * SMALL_SIZE
;
117 while (power
< size
) {
128 vm_external_map_t result
= VM_EXTERNAL_NULL
;
131 if (bytes
<= SMALL_SIZE
) {
132 if ((result
= (vm_external_map_t
)kalloc(SMALL_SIZE
)) != NULL
) {
133 memset(result
, 0, SMALL_SIZE
);
135 } else if (bytes
<= LARGE_SIZE
) {
136 bytes
= power_of_2(bytes
);
138 if ((result
= (vm_external_map_t
)kalloc(bytes
)) != NULL
) {
139 memset(result
, 0, bytes
);
147 vm_external_map_t map
,
152 if (map
== VM_EXTERNAL_NULL
)
156 if (bytes
<= SMALL_SIZE
) {
159 bytes
= power_of_2(bytes
);
165 * Return the number of bytes needed for a vm_external_map given the
166 * size of the object to be mapped, i.e. the size of the map that was
167 * created by vm_external_create.
170 vm_external_map_size(
177 if (bytes
<= SMALL_SIZE
) {
180 bytes
= power_of_2(bytes
);
188 vm_external_map_t old_map
,
190 vm_external_map_t new_map
)
193 * Cannot copy non-existent maps
195 if ((old_map
== VM_EXTERNAL_NULL
) || (new_map
== VM_EXTERNAL_NULL
))
199 * Copy old map to new
201 memcpy(new_map
, old_map
, stob(old_size
));
212 assert(new_size
>= old_size
);
215 * "old_bytes" is calculated to be the actual amount of space
216 * allocated for a map of size "old_size".
218 old_bytes
= stob(old_size
);
219 if (old_bytes
<= SMALL_SIZE
) old_bytes
= SMALL_SIZE
;
220 else if (old_bytes
<= LARGE_SIZE
) old_bytes
= power_of_2(old_bytes
);
223 * "new_bytes" is the map size required to map the "new_size" object.
224 * Since the rounding algorithms are the same, we needn't actually
225 * round up new_bytes to get the correct answer
227 new_bytes
= stob(new_size
);
229 return(new_bytes
<= old_bytes
);
233 _vm_external_state_get(
234 vm_external_map_t map
,
240 assert (map
!= VM_EXTERNAL_NULL
);
242 bit
= atop_32(offset
);
244 if (map
[byte
] & (1 << (bit
& 07))) {
245 return VM_EXTERNAL_STATE_EXISTS
;
247 return VM_EXTERNAL_STATE_ABSENT
;
252 vm_external_state_set(
253 vm_external_map_t map
,
259 if (map
== VM_EXTERNAL_NULL
)
262 bit
= atop_32(offset
);
264 map
[byte
] |= (1 << (bit
& 07));
268 vm_external_state_clr(
269 vm_external_map_t map
,
275 if (map
== VM_EXTERNAL_NULL
)
278 bit
= atop_32(offset
);
280 map
[byte
] &= ~(1 << (bit
& 07));
284 vm_external_module_initialize(void)