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.
62 * This module maintains information about the presence of
63 * pages not in memory. Since an external memory object
64 * must maintain a complete knowledge of its contents, this
65 * information takes the form of hints.
67 #include <string.h> /* for memcpy()/memset() */
69 #include <mach/boolean.h>
70 #include <vm/vm_external.h>
71 #include <kern/kalloc.h>
72 #include <mach/vm_param.h>
73 #include <kern/assert.h>
76 * The implementation uses bit arrays to record whether
77 * a page has been written to external storage. For
78 * convenience, these bit arrays come in various sizes.
79 * For example, a map N bytes long can record:
81 * 16 bytes = 128 pages = (@ 4KB/page) 512KB
82 * 1024 bytes = 8192 pages = (@ 4KB/page) 32MB
83 * 4096 bytes = 32768 pages = (@ 4KB/page) 128MB
85 * For a 32-bit machine with 4KB pages, the largest size
86 * would be 128KB = 32 pages. Machines with a larger page
87 * size are more efficient.
89 * This subsystem must be very careful about memory allocation,
90 * since vm_external_create() is almost always called with
91 * vm_privilege set. The largest map to be allocated must be less
92 * than or equal to a single page, and the kalloc subsystem must
93 * never allocate more than a single page in response to a kalloc()
94 * request. Also, vm_external_destroy() must not take any blocking
95 * locks, since it is called with a vm_object lock held. This
96 * implies that kfree() MUST be implemented in terms of zfree()
97 * NOT kmem_free() for all request sizes that this subsystem uses.
99 * For efficiency, this subsystem knows that the kalloc() subsystem
100 * is implemented in terms of power-of-2 allocation, and that the
101 * minimum allocation unit is KALLOC_MINSIZE
104 * Should consider using existence_map to hold bits directly
105 * when existence_size <= 4 bytes (i.e., 32 pages).
108 #define SMALL_SIZE KALLOC_MINSIZE
109 #define LARGE_SIZE PAGE_SIZE
111 static vm_size_t
power_of_2(vm_size_t size
);
114 power_of_2(vm_size_t size
)
118 power
= 2 * SMALL_SIZE
;
119 while (power
< size
) {
130 vm_external_map_t result
= VM_EXTERNAL_NULL
;
133 if (bytes
<= SMALL_SIZE
) {
134 if ((result
= (vm_external_map_t
)kalloc(SMALL_SIZE
)) != NULL
) {
135 memset(result
, 0, SMALL_SIZE
);
137 } else if (bytes
<= LARGE_SIZE
) {
138 bytes
= power_of_2(bytes
);
140 if ((result
= (vm_external_map_t
)kalloc(bytes
)) != NULL
) {
141 memset(result
, 0, bytes
);
149 vm_external_map_t map
,
154 if (map
== VM_EXTERNAL_NULL
)
158 if (bytes
<= SMALL_SIZE
) {
161 bytes
= power_of_2(bytes
);
167 * Return the number of bytes needed for a vm_external_map given the
168 * size of the object to be mapped, i.e. the size of the map that was
169 * created by vm_external_create.
172 vm_external_map_size(
179 if (bytes
<= SMALL_SIZE
) {
182 bytes
= power_of_2(bytes
);
190 vm_external_map_t old_map
,
192 vm_external_map_t new_map
)
195 * Cannot copy non-existent maps
197 if ((old_map
== VM_EXTERNAL_NULL
) || (new_map
== VM_EXTERNAL_NULL
))
201 * Copy old map to new
203 memcpy(new_map
, old_map
, stob(old_size
));
214 assert(new_size
>= old_size
);
217 * "old_bytes" is calculated to be the actual amount of space
218 * allocated for a map of size "old_size".
220 old_bytes
= stob(old_size
);
221 if (old_bytes
<= SMALL_SIZE
) old_bytes
= SMALL_SIZE
;
222 else if (old_bytes
<= LARGE_SIZE
) old_bytes
= power_of_2(old_bytes
);
225 * "new_bytes" is the map size required to map the "new_size" object.
226 * Since the rounding algorithms are the same, we needn't actually
227 * round up new_bytes to get the correct answer
229 new_bytes
= stob(new_size
);
231 return(new_bytes
<= old_bytes
);
235 _vm_external_state_get(
236 vm_external_map_t map
,
242 assert (map
!= VM_EXTERNAL_NULL
);
244 bit
= atop_32(offset
);
246 if (map
[byte
] & (1 << (bit
& 07))) {
247 return VM_EXTERNAL_STATE_EXISTS
;
249 return VM_EXTERNAL_STATE_ABSENT
;
254 vm_external_state_set(
255 vm_external_map_t map
,
261 if (map
== VM_EXTERNAL_NULL
)
264 bit
= atop_32(offset
);
266 map
[byte
] |= (1 << (bit
& 07));
270 vm_external_state_clr(
271 vm_external_map_t map
,
277 if (map
== VM_EXTERNAL_NULL
)
280 bit
= atop_32(offset
);
282 map
[byte
] &= ~(1 << (bit
& 07));
286 vm_external_module_initialize(void)