2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
55 * This module maintains information about the presence of
56 * pages not in memory. Since an external memory object
57 * must maintain a complete knowledge of its contents, this
58 * information takes the form of hints.
60 #include <string.h> /* for memcpy()/memset() */
62 #include <mach/boolean.h>
63 #include <vm/vm_external.h>
64 #include <kern/kalloc.h>
65 #include <mach/vm_param.h>
66 #include <kern/assert.h>
69 * The implementation uses bit arrays to record whether
70 * a page has been written to external storage. For
71 * convenience, these bit arrays come in various sizes.
72 * For example, a map N bytes long can record:
74 * 16 bytes = 128 pages = (@ 4KB/page) 512KB
75 * 1024 bytes = 8192 pages = (@ 4KB/page) 32MB
76 * 4096 bytes = 32768 pages = (@ 4KB/page) 128MB
78 * For a 32-bit machine with 4KB pages, the largest size
79 * would be 128KB = 32 pages. Machines with a larger page
80 * size are more efficient.
82 * This subsystem must be very careful about memory allocation,
83 * since vm_external_create() is almost always called with
84 * vm_privilege set. The largest map to be allocated must be less
85 * than or equal to a single page, and the kalloc subsystem must
86 * never allocate more than a single page in response to a kalloc()
87 * request. Also, vm_external_destroy() must not take any blocking
88 * locks, since it is called with a vm_object lock held. This
89 * implies that kfree() MUST be implemented in terms of zfree()
90 * NOT kmem_free() for all request sizes that this subsystem uses.
92 * For efficiency, this subsystem knows that the kalloc() subsystem
93 * is implemented in terms of power-of-2 allocation, and that the
94 * minimum allocation unit is KALLOC_MINSIZE
97 * Should consider using existence_map to hold bits directly
98 * when existence_size <= 4 bytes (i.e., 32 pages).
101 #define SMALL_SIZE KALLOC_MINSIZE
102 #define LARGE_SIZE PAGE_SIZE
104 static vm_size_t
power_of_2(vm_size_t size
);
107 power_of_2(vm_size_t size
)
111 power
= 2 * SMALL_SIZE
;
112 while (power
< size
) {
123 vm_external_map_t result
= VM_EXTERNAL_NULL
;
126 if (bytes
<= SMALL_SIZE
) {
127 if ((result
= (vm_external_map_t
)kalloc(SMALL_SIZE
)) != NULL
) {
128 memset(result
, 0, SMALL_SIZE
);
130 } else if (bytes
<= LARGE_SIZE
) {
131 bytes
= power_of_2(bytes
);
133 if ((result
= (vm_external_map_t
)kalloc(bytes
)) != NULL
) {
134 memset(result
, 0, bytes
);
142 vm_external_map_t map
,
147 if (map
== VM_EXTERNAL_NULL
)
151 if (bytes
<= SMALL_SIZE
) {
154 bytes
= power_of_2(bytes
);
160 * Return the number of bytes needed for a vm_external_map given the
161 * size of the object to be mapped, i.e. the size of the map that was
162 * created by vm_external_create.
165 vm_external_map_size(
172 if (bytes
<= SMALL_SIZE
) {
175 bytes
= power_of_2(bytes
);
183 vm_external_map_t old_map
,
185 vm_external_map_t new_map
)
188 * Cannot copy non-existent maps
190 if ((old_map
== VM_EXTERNAL_NULL
) || (new_map
== VM_EXTERNAL_NULL
))
194 * Copy old map to new
196 memcpy(new_map
, old_map
, stob(old_size
));
207 assert(new_size
>= old_size
);
210 * "old_bytes" is calculated to be the actual amount of space
211 * allocated for a map of size "old_size".
213 old_bytes
= stob(old_size
);
214 if (old_bytes
<= SMALL_SIZE
) old_bytes
= SMALL_SIZE
;
215 else if (old_bytes
<= LARGE_SIZE
) old_bytes
= power_of_2(old_bytes
);
218 * "new_bytes" is the map size required to map the "new_size" object.
219 * Since the rounding algorithms are the same, we needn't actually
220 * round up new_bytes to get the correct answer
222 new_bytes
= stob(new_size
);
224 return(new_bytes
<= old_bytes
);
228 _vm_external_state_get(
229 vm_external_map_t map
,
235 assert (map
!= VM_EXTERNAL_NULL
);
237 bit
= atop_32(offset
);
239 if (map
[byte
] & (1 << (bit
& 07))) {
240 return VM_EXTERNAL_STATE_EXISTS
;
242 return VM_EXTERNAL_STATE_ABSENT
;
247 vm_external_state_set(
248 vm_external_map_t map
,
254 if (map
== VM_EXTERNAL_NULL
)
257 bit
= atop_32(offset
);
259 map
[byte
] |= (1 << (bit
& 07));
263 vm_external_state_clr(
264 vm_external_map_t map
,
270 if (map
== VM_EXTERNAL_NULL
)
273 bit
= atop_32(offset
);
275 map
[byte
] &= ~(1 << (bit
& 07));
279 vm_external_module_initialize(void)