2 * Copyright (c) 2000-2007 Apple 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,1988,1987 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 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Kernel memory management.
66 #include <mach/kern_return.h>
67 #include <mach/vm_param.h>
68 #include <kern/assert.h>
69 #include <kern/lock.h>
70 #include <kern/thread.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pageout.h>
76 #include <kern/misc_protos.h>
81 #include <libkern/OSDebug.h>
82 #include <sys/kdebug.h>
85 * Variables exported by this module.
89 vm_map_t kernel_pageable_map
;
91 extern boolean_t vm_kernel_ready
;
94 * Forward declarations for internal functions.
96 extern kern_return_t
kmem_alloc_pages(
97 register vm_object_t object
,
98 register vm_object_offset_t offset
,
99 register vm_object_size_t size
);
101 extern void kmem_remap_pages(
102 register vm_object_t object
,
103 register vm_object_offset_t offset
,
104 register vm_offset_t start
,
105 register vm_offset_t end
,
106 vm_prot_t protection
);
119 vm_object_offset_t offset
;
120 vm_map_offset_t map_addr
;
121 vm_map_offset_t map_mask
;
122 vm_map_size_t map_size
, i
;
123 vm_map_entry_t entry
;
127 if (map
== VM_MAP_NULL
|| (flags
& ~(KMA_KOBJECT
| KMA_LOMEM
| KMA_NOPAGEWAIT
)))
128 return KERN_INVALID_ARGUMENT
;
130 map_size
= vm_map_round_page(size
,
131 VM_MAP_PAGE_MASK(map
));
132 map_mask
= (vm_map_offset_t
)mask
;
134 /* Check for zero allocation size (either directly or via overflow) */
137 return KERN_INVALID_ARGUMENT
;
141 * Allocate a new object (if necessary) and the reference we
142 * will be donating to the map entry. We must do this before
143 * locking the map, or risk deadlock with the default pager.
145 if ((flags
& KMA_KOBJECT
) != 0) {
146 object
= kernel_object
;
147 vm_object_reference(object
);
149 object
= vm_object_allocate(map_size
);
152 kr
= vm_map_find_space(map
, &map_addr
, map_size
, map_mask
, 0, &entry
);
153 if (KERN_SUCCESS
!= kr
) {
154 vm_object_deallocate(object
);
158 entry
->object
.vm_object
= object
;
159 entry
->offset
= offset
= (object
== kernel_object
) ?
162 /* Take an extra object ref in case the map entry gets deleted */
163 vm_object_reference(object
);
166 kr
= cpm_allocate(CAST_DOWN(vm_size_t
, map_size
), &pages
, max_pnum
, pnum_mask
, FALSE
, flags
);
168 if (kr
!= KERN_SUCCESS
) {
170 vm_map_trunc_page(map_addr
,
171 VM_MAP_PAGE_MASK(map
)),
172 vm_map_round_page(map_addr
+ map_size
,
173 VM_MAP_PAGE_MASK(map
)),
175 vm_object_deallocate(object
);
180 vm_object_lock(object
);
181 for (i
= 0; i
< map_size
; i
+= PAGE_SIZE
) {
183 pages
= NEXT_PAGE(m
);
184 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
186 vm_page_insert(m
, object
, offset
+ i
);
188 vm_object_unlock(object
);
190 kr
= vm_map_wire(map
,
191 vm_map_trunc_page(map_addr
,
192 VM_MAP_PAGE_MASK(map
)),
193 vm_map_round_page(map_addr
+ map_size
,
194 VM_MAP_PAGE_MASK(map
)),
197 if (kr
!= KERN_SUCCESS
) {
198 if (object
== kernel_object
) {
199 vm_object_lock(object
);
200 vm_object_page_remove(object
, offset
, offset
+ map_size
);
201 vm_object_unlock(object
);
204 vm_map_trunc_page(map_addr
,
205 VM_MAP_PAGE_MASK(map
)),
206 vm_map_round_page(map_addr
+ map_size
,
207 VM_MAP_PAGE_MASK(map
)),
209 vm_object_deallocate(object
);
212 vm_object_deallocate(object
);
214 if (object
== kernel_object
)
215 vm_map_simplify(map
, map_addr
);
217 *addrp
= (vm_offset_t
) map_addr
;
218 assert((vm_map_offset_t
) *addrp
== map_addr
);
223 * Master entry point for allocating kernel memory.
224 * NOTE: this routine is _never_ interrupt safe.
226 * map : map to allocate into
227 * addrp : pointer to start address of new memory
228 * size : size of memory requested
230 * KMA_HERE *addrp is base address, else "anywhere"
231 * KMA_NOPAGEWAIT don't wait for pages if unavailable
232 * KMA_KOBJECT use kernel_object
233 * KMA_LOMEM support for 32 bit devices in a 64 bit world
234 * if set and a lomemory pool is available
235 * grab pages from it... this also implies
240 kernel_memory_allocate(
241 register vm_map_t map
,
242 register vm_offset_t
*addrp
,
243 register vm_size_t size
,
244 register vm_offset_t mask
,
248 vm_object_offset_t offset
;
249 vm_object_offset_t pg_offset
;
250 vm_map_entry_t entry
= NULL
;
251 vm_map_offset_t map_addr
, fill_start
;
252 vm_map_offset_t map_mask
;
253 vm_map_size_t map_size
, fill_size
;
254 kern_return_t kr
, pe_result
;
256 vm_page_t guard_page_list
= NULL
;
257 vm_page_t wired_page_list
= NULL
;
258 int guard_page_count
= 0;
259 int wired_page_count
= 0;
264 if (! vm_kernel_ready
) {
265 panic("kernel_memory_allocate: VM is not ready");
268 map_size
= vm_map_round_page(size
,
269 VM_MAP_PAGE_MASK(map
));
270 map_mask
= (vm_map_offset_t
) mask
;
273 /* Check for zero allocation size (either directly or via overflow) */
276 return KERN_INVALID_ARGUMENT
;
280 * limit the size of a single extent of wired memory
281 * to try and limit the damage to the system if
282 * too many pages get wired down
283 * limit raised to 2GB with 128GB max physical limit
285 if (map_size
> (1ULL << 31)) {
286 return KERN_RESOURCE_SHORTAGE
;
292 * Guard pages are implemented as ficticious pages. By placing guard pages
293 * on either end of a stack, they can help detect cases where a thread walks
294 * off either end of its stack. They are allocated and set up here and attempts
295 * to access those pages are trapped in vm_fault_page().
297 * The map_size we were passed may include extra space for
298 * guard pages. If those were requested, then back it out of fill_size
299 * since vm_map_find_space() takes just the actual size not including
300 * guard pages. Similarly, fill_start indicates where the actual pages
301 * will begin in the range.
305 fill_size
= map_size
;
307 if (flags
& KMA_GUARD_FIRST
) {
308 vm_alloc_flags
|= VM_FLAGS_GUARD_BEFORE
;
309 fill_start
+= PAGE_SIZE_64
;
310 fill_size
-= PAGE_SIZE_64
;
311 if (map_size
< fill_start
+ fill_size
) {
312 /* no space for a guard page */
314 return KERN_INVALID_ARGUMENT
;
318 if (flags
& KMA_GUARD_LAST
) {
319 vm_alloc_flags
|= VM_FLAGS_GUARD_AFTER
;
320 fill_size
-= PAGE_SIZE_64
;
321 if (map_size
<= fill_start
+ fill_size
) {
322 /* no space for a guard page */
324 return KERN_INVALID_ARGUMENT
;
328 wired_page_count
= (int) (fill_size
/ PAGE_SIZE_64
);
329 assert(wired_page_count
* PAGE_SIZE_64
== fill_size
);
331 for (i
= 0; i
< guard_page_count
; i
++) {
333 mem
= vm_page_grab_guard();
335 if (mem
!= VM_PAGE_NULL
)
337 if (flags
& KMA_NOPAGEWAIT
) {
338 kr
= KERN_RESOURCE_SHORTAGE
;
341 vm_page_more_fictitious();
343 mem
->pageq
.next
= (queue_entry_t
)guard_page_list
;
344 guard_page_list
= mem
;
347 if (! (flags
& KMA_VAONLY
)) {
348 for (i
= 0; i
< wired_page_count
; i
++) {
349 uint64_t unavailable
;
352 if (flags
& KMA_LOMEM
)
353 mem
= vm_page_grablo();
355 mem
= vm_page_grab();
357 if (mem
!= VM_PAGE_NULL
)
360 if (flags
& KMA_NOPAGEWAIT
) {
361 kr
= KERN_RESOURCE_SHORTAGE
;
364 if ((flags
& KMA_LOMEM
) && (vm_lopage_needed
== TRUE
)) {
365 kr
= KERN_RESOURCE_SHORTAGE
;
368 unavailable
= (vm_page_wire_count
+ vm_page_free_target
) * PAGE_SIZE
;
370 if (unavailable
> max_mem
|| map_size
> (max_mem
- unavailable
)) {
371 kr
= KERN_RESOURCE_SHORTAGE
;
376 mem
->pageq
.next
= (queue_entry_t
)wired_page_list
;
377 wired_page_list
= mem
;
382 * Allocate a new object (if necessary). We must do this before
383 * locking the map, or risk deadlock with the default pager.
385 if ((flags
& KMA_KOBJECT
) != 0) {
386 object
= kernel_object
;
387 vm_object_reference(object
);
388 } else if ((flags
& KMA_COMPRESSOR
) != 0) {
389 object
= compressor_object
;
390 vm_object_reference(object
);
392 object
= vm_object_allocate(map_size
);
395 kr
= vm_map_find_space(map
, &map_addr
,
397 vm_alloc_flags
, &entry
);
398 if (KERN_SUCCESS
!= kr
) {
399 vm_object_deallocate(object
);
403 entry
->object
.vm_object
= object
;
404 entry
->offset
= offset
= (object
== kernel_object
|| object
== compressor_object
) ?
407 if (object
!= compressor_object
)
408 entry
->wired_count
++;
410 if (flags
& KMA_PERMANENT
)
411 entry
->permanent
= TRUE
;
413 if (object
!= kernel_object
&& object
!= compressor_object
)
414 vm_object_reference(object
);
416 vm_object_lock(object
);
422 if (guard_page_list
== NULL
)
423 panic("kernel_memory_allocate: guard_page_list == NULL");
425 mem
= guard_page_list
;
426 guard_page_list
= (vm_page_t
)mem
->pageq
.next
;
427 mem
->pageq
.next
= NULL
;
429 vm_page_insert(mem
, object
, offset
+ pg_offset
);
432 pg_offset
+= PAGE_SIZE_64
;
435 kma_prot
= VM_PROT_READ
| VM_PROT_WRITE
;
437 if (flags
& KMA_VAONLY
) {
438 pg_offset
= fill_start
+ fill_size
;
440 for (pg_offset
= fill_start
; pg_offset
< fill_start
+ fill_size
; pg_offset
+= PAGE_SIZE_64
) {
441 if (wired_page_list
== NULL
)
442 panic("kernel_memory_allocate: wired_page_list == NULL");
444 mem
= wired_page_list
;
445 wired_page_list
= (vm_page_t
)mem
->pageq
.next
;
446 mem
->pageq
.next
= NULL
;
449 vm_page_insert(mem
, object
, offset
+ pg_offset
);
453 mem
->wpmapped
= TRUE
;
455 PMAP_ENTER_OPTIONS(kernel_pmap
, map_addr
+ pg_offset
, mem
,
456 kma_prot
, VM_PROT_NONE
, ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
457 PMAP_OPTIONS_NOWAIT
, pe_result
);
459 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
460 vm_object_unlock(object
);
462 PMAP_ENTER(kernel_pmap
, map_addr
+ pg_offset
, mem
,
463 kma_prot
, VM_PROT_NONE
, ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
);
465 vm_object_lock(object
);
467 if (flags
& KMA_NOENCRYPT
) {
468 bzero(CAST_DOWN(void *, (map_addr
+ pg_offset
)), PAGE_SIZE
);
470 pmap_set_noencrypt(mem
->phys_page
);
474 if ((fill_start
+ fill_size
) < map_size
) {
475 if (guard_page_list
== NULL
)
476 panic("kernel_memory_allocate: guard_page_list == NULL");
478 mem
= guard_page_list
;
479 guard_page_list
= (vm_page_t
)mem
->pageq
.next
;
480 mem
->pageq
.next
= NULL
;
482 vm_page_insert(mem
, object
, offset
+ pg_offset
);
486 if (guard_page_list
|| wired_page_list
)
487 panic("kernel_memory_allocate: non empty list\n");
489 if (! (flags
& KMA_VAONLY
)) {
490 vm_page_lockspin_queues();
491 vm_page_wire_count
+= wired_page_count
;
492 vm_page_unlock_queues();
495 vm_object_unlock(object
);
498 * now that the pages are wired, we no longer have to fear coalesce
500 if (object
== kernel_object
|| object
== compressor_object
)
501 vm_map_simplify(map
, map_addr
);
503 vm_object_deallocate(object
);
506 * Return the memory, not zeroed.
508 *addrp
= CAST_DOWN(vm_offset_t
, map_addr
);
513 vm_page_free_list(guard_page_list
, FALSE
);
516 vm_page_free_list(wired_page_list
, FALSE
);
522 kernel_memory_populate(
529 vm_object_offset_t offset
, pg_offset
;
530 kern_return_t kr
, pe_result
;
532 vm_page_t page_list
= NULL
;
536 page_count
= (int) (size
/ PAGE_SIZE_64
);
538 assert((flags
& (KMA_COMPRESSOR
|KMA_KOBJECT
)) != (KMA_COMPRESSOR
|KMA_KOBJECT
));
540 if (flags
& KMA_COMPRESSOR
) {
542 for (i
= 0; i
< page_count
; i
++) {
544 mem
= vm_page_grab();
546 if (mem
!= VM_PAGE_NULL
)
551 mem
->pageq
.next
= (queue_entry_t
) page_list
;
555 object
= compressor_object
;
557 vm_object_lock(object
);
561 pg_offset
+= PAGE_SIZE_64
) {
564 page_list
= (vm_page_t
) mem
->pageq
.next
;
565 mem
->pageq
.next
= NULL
;
567 vm_page_insert(mem
, object
, offset
+ pg_offset
);
570 PMAP_ENTER_OPTIONS(kernel_pmap
, addr
+ pg_offset
, mem
,
571 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
,
572 0, TRUE
, PMAP_OPTIONS_NOWAIT
, pe_result
);
574 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
576 vm_object_unlock(object
);
578 PMAP_ENTER(kernel_pmap
, addr
+ pg_offset
, mem
,
579 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
, 0, TRUE
);
581 vm_object_lock(object
);
585 mem
->wpmapped
= TRUE
;
586 mem
->compressor
= TRUE
;
588 vm_object_unlock(object
);
593 for (i
= 0; i
< page_count
; i
++) {
595 if (flags
& KMA_LOMEM
)
596 mem
= vm_page_grablo();
598 mem
= vm_page_grab();
600 if (mem
!= VM_PAGE_NULL
)
603 if (flags
& KMA_NOPAGEWAIT
) {
604 kr
= KERN_RESOURCE_SHORTAGE
;
607 if ((flags
& KMA_LOMEM
) &&
608 (vm_lopage_needed
== TRUE
)) {
609 kr
= KERN_RESOURCE_SHORTAGE
;
614 mem
->pageq
.next
= (queue_entry_t
) page_list
;
617 if (flags
& KMA_KOBJECT
) {
619 object
= kernel_object
;
621 vm_object_lock(object
);
624 * If it's not the kernel object, we need to:
628 * take reference on object;
631 panic("kernel_memory_populate(%p,0x%llx,0x%llx,0x%x): "
633 map
, (uint64_t) addr
, (uint64_t) size
, flags
);
638 pg_offset
+= PAGE_SIZE_64
) {
640 if (page_list
== NULL
)
641 panic("kernel_memory_populate: page_list == NULL");
644 page_list
= (vm_page_t
) mem
->pageq
.next
;
645 mem
->pageq
.next
= NULL
;
649 vm_page_insert(mem
, object
, offset
+ pg_offset
);
653 mem
->wpmapped
= TRUE
;
655 PMAP_ENTER_OPTIONS(kernel_pmap
, addr
+ pg_offset
, mem
,
656 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
,
657 ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
658 PMAP_OPTIONS_NOWAIT
, pe_result
);
660 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
662 vm_object_unlock(object
);
664 PMAP_ENTER(kernel_pmap
, addr
+ pg_offset
, mem
,
665 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
,
666 ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
);
668 vm_object_lock(object
);
670 if (flags
& KMA_NOENCRYPT
) {
671 bzero(CAST_DOWN(void *, (addr
+ pg_offset
)), PAGE_SIZE
);
672 pmap_set_noencrypt(mem
->phys_page
);
675 vm_page_lock_queues();
676 vm_page_wire_count
+= page_count
;
677 vm_page_unlock_queues();
679 vm_object_unlock(object
);
685 vm_page_free_list(page_list
, FALSE
);
692 kernel_memory_depopulate(
699 vm_object_offset_t offset
, pg_offset
;
701 vm_page_t local_freeq
= NULL
;
703 assert((flags
& (KMA_COMPRESSOR
|KMA_KOBJECT
)) != (KMA_COMPRESSOR
|KMA_KOBJECT
));
705 if (flags
& KMA_COMPRESSOR
) {
707 object
= compressor_object
;
709 vm_object_lock(object
);
710 } else if (flags
& KMA_KOBJECT
) {
712 object
= kernel_object
;
714 vm_object_lock(object
);
719 * If it's not the kernel object, we need to:
725 panic("kernel_memory_depopulate(%p,0x%llx,0x%llx,0x%x): "
727 map
, (uint64_t) addr
, (uint64_t) size
, flags
);
729 pmap_protect(kernel_map
->pmap
, offset
, offset
+ size
, VM_PROT_NONE
);
733 pg_offset
+= PAGE_SIZE_64
) {
735 mem
= vm_page_lookup(object
, offset
+ pg_offset
);
739 pmap_disconnect(mem
->phys_page
);
744 vm_page_remove(mem
, TRUE
);
747 assert(mem
->pageq
.next
== NULL
&&
748 mem
->pageq
.prev
== NULL
);
749 mem
->pageq
.next
= (queue_entry_t
)local_freeq
;
752 vm_object_unlock(object
);
755 vm_page_free_list(local_freeq
, TRUE
);
761 * Allocate wired-down memory in the kernel's address map
762 * or a submap. The memory is not zero-filled.
771 kern_return_t kr
= kernel_memory_allocate(map
, addrp
, size
, 0, 0);
772 TRACE_MACHLEAKS(KMEM_ALLOC_CODE
, KMEM_ALLOC_CODE_2
, size
, *addrp
);
779 * Reallocate wired-down memory in the kernel's address map
780 * or a submap. Newly allocated pages are not zeroed.
781 * This can only be used on regions allocated with kmem_alloc.
783 * If successful, the pages in the old region are mapped twice.
784 * The old region is unchanged. Use kmem_free to get rid of it.
791 vm_offset_t
*newaddrp
,
795 vm_object_offset_t offset
;
796 vm_map_offset_t oldmapmin
;
797 vm_map_offset_t oldmapmax
;
798 vm_map_offset_t newmapaddr
;
799 vm_map_size_t oldmapsize
;
800 vm_map_size_t newmapsize
;
801 vm_map_entry_t oldentry
;
802 vm_map_entry_t newentry
;
806 oldmapmin
= vm_map_trunc_page(oldaddr
,
807 VM_MAP_PAGE_MASK(map
));
808 oldmapmax
= vm_map_round_page(oldaddr
+ oldsize
,
809 VM_MAP_PAGE_MASK(map
));
810 oldmapsize
= oldmapmax
- oldmapmin
;
811 newmapsize
= vm_map_round_page(newsize
,
812 VM_MAP_PAGE_MASK(map
));
816 * Find the VM object backing the old region.
821 if (!vm_map_lookup_entry(map
, oldmapmin
, &oldentry
))
822 panic("kmem_realloc");
823 object
= oldentry
->object
.vm_object
;
826 * Increase the size of the object and
827 * fill in the new region.
830 vm_object_reference(object
);
831 /* by grabbing the object lock before unlocking the map */
832 /* we guarantee that we will panic if more than one */
833 /* attempt is made to realloc a kmem_alloc'd area */
834 vm_object_lock(object
);
836 if (object
->vo_size
!= oldmapsize
)
837 panic("kmem_realloc");
838 object
->vo_size
= newmapsize
;
839 vm_object_unlock(object
);
841 /* allocate the new pages while expanded portion of the */
842 /* object is still not mapped */
843 kmem_alloc_pages(object
, vm_object_round_page(oldmapsize
),
844 vm_object_round_page(newmapsize
-oldmapsize
));
847 * Find space for the new region.
850 kr
= vm_map_find_space(map
, &newmapaddr
, newmapsize
,
851 (vm_map_offset_t
) 0, 0, &newentry
);
852 if (kr
!= KERN_SUCCESS
) {
853 vm_object_lock(object
);
854 for(offset
= oldmapsize
;
855 offset
< newmapsize
; offset
+= PAGE_SIZE
) {
856 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
860 object
->vo_size
= oldmapsize
;
861 vm_object_unlock(object
);
862 vm_object_deallocate(object
);
865 newentry
->object
.vm_object
= object
;
866 newentry
->offset
= 0;
867 assert (newentry
->wired_count
== 0);
870 /* add an extra reference in case we have someone doing an */
871 /* unexpected deallocate */
872 vm_object_reference(object
);
875 kr
= vm_map_wire(map
, newmapaddr
, newmapaddr
+ newmapsize
, VM_PROT_DEFAULT
, FALSE
);
876 if (KERN_SUCCESS
!= kr
) {
877 vm_map_remove(map
, newmapaddr
, newmapaddr
+ newmapsize
, 0);
878 vm_object_lock(object
);
879 for(offset
= oldsize
; offset
< newmapsize
; offset
+= PAGE_SIZE
) {
880 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
884 object
->vo_size
= oldmapsize
;
885 vm_object_unlock(object
);
886 vm_object_deallocate(object
);
889 vm_object_deallocate(object
);
891 *newaddrp
= CAST_DOWN(vm_offset_t
, newmapaddr
);
896 * kmem_alloc_kobject:
898 * Allocate wired-down memory in the kernel's address map
899 * or a submap. The memory is not zero-filled.
901 * The memory is allocated in the kernel_object.
902 * It may not be copied with vm_map_copy, and
903 * it may not be reallocated with kmem_realloc.
912 return kernel_memory_allocate(map
, addrp
, size
, 0, KMA_KOBJECT
);
916 * kmem_alloc_aligned:
918 * Like kmem_alloc_kobject, except that the memory is aligned.
919 * The size should be a power-of-2.
928 if ((size
& (size
- 1)) != 0)
929 panic("kmem_alloc_aligned: size not aligned");
930 return kernel_memory_allocate(map
, addrp
, size
, size
- 1, KMA_KOBJECT
);
934 * kmem_alloc_pageable:
936 * Allocate pageable memory in the kernel's address map.
945 vm_map_offset_t map_addr
;
946 vm_map_size_t map_size
;
950 map_addr
= (vm_map_min(map
)) + 0x1000;
952 map_addr
= vm_map_min(map
);
954 map_size
= vm_map_round_page(size
,
955 VM_MAP_PAGE_MASK(map
));
957 kr
= vm_map_enter(map
, &map_addr
, map_size
,
958 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
959 VM_OBJECT_NULL
, (vm_object_offset_t
) 0, FALSE
,
960 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
962 if (kr
!= KERN_SUCCESS
)
965 *addrp
= CAST_DOWN(vm_offset_t
, map_addr
);
972 * Release a region of kernel virtual memory allocated
973 * with kmem_alloc, kmem_alloc_kobject, or kmem_alloc_pageable,
974 * and return the physical pages associated with that region.
985 assert(addr
>= VM_MIN_KERNEL_AND_KEXT_ADDRESS
);
987 TRACE_MACHLEAKS(KMEM_FREE_CODE
, KMEM_FREE_CODE_2
, size
, addr
);
991 printf("kmem_free called with size==0 for map: %p with addr: 0x%llx\n",map
,(uint64_t)addr
);
996 kr
= vm_map_remove(map
,
997 vm_map_trunc_page(addr
,
998 VM_MAP_PAGE_MASK(map
)),
999 vm_map_round_page(addr
+ size
,
1000 VM_MAP_PAGE_MASK(map
)),
1001 VM_MAP_REMOVE_KUNWIRE
);
1002 if (kr
!= KERN_SUCCESS
)
1007 * Allocate new pages in an object.
1012 register vm_object_t object
,
1013 register vm_object_offset_t offset
,
1014 register vm_object_size_t size
)
1016 vm_object_size_t alloc_size
;
1018 alloc_size
= vm_object_round_page(size
);
1019 vm_object_lock(object
);
1020 while (alloc_size
) {
1021 register vm_page_t mem
;
1027 while (VM_PAGE_NULL
==
1028 (mem
= vm_page_alloc(object
, offset
))) {
1029 vm_object_unlock(object
);
1031 vm_object_lock(object
);
1035 alloc_size
-= PAGE_SIZE
;
1036 offset
+= PAGE_SIZE
;
1038 vm_object_unlock(object
);
1039 return KERN_SUCCESS
;
1043 * Remap wired pages in an object into a new region.
1044 * The object is assumed to be mapped into the kernel map or
1049 register vm_object_t object
,
1050 register vm_object_offset_t offset
,
1051 register vm_offset_t start
,
1052 register vm_offset_t end
,
1053 vm_prot_t protection
)
1056 vm_map_offset_t map_start
;
1057 vm_map_offset_t map_end
;
1060 * Mark the pmap region as not pageable.
1062 map_start
= vm_map_trunc_page(start
,
1063 VM_MAP_PAGE_MASK(kernel_map
));
1064 map_end
= vm_map_round_page(end
,
1065 VM_MAP_PAGE_MASK(kernel_map
));
1067 pmap_pageable(kernel_pmap
, map_start
, map_end
, FALSE
);
1069 while (map_start
< map_end
) {
1070 register vm_page_t mem
;
1072 vm_object_lock(object
);
1077 if ((mem
= vm_page_lookup(object
, offset
)) == VM_PAGE_NULL
)
1078 panic("kmem_remap_pages");
1081 * Wire it down (again)
1083 vm_page_lockspin_queues();
1085 vm_page_unlock_queues();
1086 vm_object_unlock(object
);
1090 * The page is supposed to be wired now, so it
1091 * shouldn't be encrypted at this point. It can
1092 * safely be entered in the page table.
1094 ASSERT_PAGE_DECRYPTED(mem
);
1097 * Enter it in the kernel pmap. The page isn't busy,
1098 * but this shouldn't be a problem because it is wired.
1101 mem
->pmapped
= TRUE
;
1102 mem
->wpmapped
= TRUE
;
1104 PMAP_ENTER(kernel_pmap
, map_start
, mem
, protection
, VM_PROT_NONE
, 0, TRUE
);
1106 map_start
+= PAGE_SIZE
;
1107 offset
+= PAGE_SIZE
;
1114 * Allocates a map to manage a subrange
1115 * of the kernel virtual address space.
1117 * Arguments are as follows:
1119 * parent Map to take range from
1120 * addr Address of start of range (IN/OUT)
1121 * size Size of range to find
1122 * pageable Can region be paged
1123 * anywhere Can region be located anywhere in map
1124 * new_map Pointer to new submap
1136 vm_map_offset_t map_addr
;
1137 vm_map_size_t map_size
;
1140 map_size
= vm_map_round_page(size
,
1141 VM_MAP_PAGE_MASK(parent
));
1144 * Need reference on submap object because it is internal
1145 * to the vm_system. vm_object_enter will never be called
1146 * on it (usual source of reference for vm_map_enter).
1148 vm_object_reference(vm_submap_object
);
1150 map_addr
= ((flags
& VM_FLAGS_ANYWHERE
)
1151 ? vm_map_min(parent
)
1152 : vm_map_trunc_page(*addr
,
1153 VM_MAP_PAGE_MASK(parent
)));
1155 kr
= vm_map_enter(parent
, &map_addr
, map_size
,
1156 (vm_map_offset_t
) 0, flags
,
1157 vm_submap_object
, (vm_object_offset_t
) 0, FALSE
,
1158 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
1159 if (kr
!= KERN_SUCCESS
) {
1160 vm_object_deallocate(vm_submap_object
);
1164 pmap_reference(vm_map_pmap(parent
));
1165 map
= vm_map_create(vm_map_pmap(parent
), map_addr
, map_addr
+ map_size
, pageable
);
1166 if (map
== VM_MAP_NULL
)
1167 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
1168 /* inherit the parent map's page size */
1169 vm_map_set_page_shift(map
, VM_MAP_PAGE_SHIFT(parent
));
1171 kr
= vm_map_submap(parent
, map_addr
, map_addr
+ map_size
, map
, map_addr
, FALSE
);
1172 if (kr
!= KERN_SUCCESS
) {
1174 * See comment preceding vm_map_submap().
1176 vm_map_remove(parent
, map_addr
, map_addr
+ map_size
, VM_MAP_NO_FLAGS
);
1177 vm_map_deallocate(map
); /* also removes ref to pmap */
1178 vm_object_deallocate(vm_submap_object
);
1181 *addr
= CAST_DOWN(vm_offset_t
, map_addr
);
1183 return (KERN_SUCCESS
);
1189 * Initialize the kernel's virtual memory map, taking
1190 * into account all memory allocated up to this time.
1197 vm_map_offset_t map_start
;
1198 vm_map_offset_t map_end
;
1200 map_start
= vm_map_trunc_page(start
,
1201 VM_MAP_PAGE_MASK(kernel_map
));
1202 map_end
= vm_map_round_page(end
,
1203 VM_MAP_PAGE_MASK(kernel_map
));
1205 kernel_map
= vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS
,
1208 * Reserve virtual memory allocated up to this time.
1210 if (start
!= VM_MIN_KERNEL_AND_KEXT_ADDRESS
) {
1211 vm_map_offset_t map_addr
;
1214 map_addr
= VM_MIN_KERNEL_AND_KEXT_ADDRESS
;
1215 kr
= vm_map_enter(kernel_map
,
1217 (vm_map_size_t
)(map_start
- VM_MIN_KERNEL_AND_KEXT_ADDRESS
),
1218 (vm_map_offset_t
) 0,
1219 VM_FLAGS_FIXED
| VM_FLAGS_NO_PMAP_CHECK
,
1221 (vm_object_offset_t
) 0, FALSE
,
1222 VM_PROT_NONE
, VM_PROT_NONE
,
1223 VM_INHERIT_DEFAULT
);
1225 if (kr
!= KERN_SUCCESS
) {
1226 panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n",
1227 (uint64_t) start
, (uint64_t) end
,
1228 (uint64_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS
,
1229 (uint64_t) (map_start
- VM_MIN_KERNEL_AND_KEXT_ADDRESS
),
1235 * Set the default global user wire limit which limits the amount of
1236 * memory that can be locked via mlock(). We set this to the total
1237 * amount of memory that are potentially usable by a user app (max_mem)
1238 * minus a certain amount. This can be overridden via a sysctl.
1240 vm_global_no_user_wire_amount
= MIN(max_mem
*20/100,
1241 VM_NOT_USER_WIREABLE
);
1242 vm_global_user_wire_limit
= max_mem
- vm_global_no_user_wire_amount
;
1244 /* the default per user limit is the same as the global limit */
1245 vm_user_wire_limit
= vm_global_user_wire_limit
;
1250 * Routine: copyinmap
1252 * Like copyin, except that fromaddr is an address
1253 * in the specified VM map. This implementation
1254 * is incomplete; it handles the current user map
1255 * and the kernel map/submaps.
1260 vm_map_offset_t fromaddr
,
1264 kern_return_t kr
= KERN_SUCCESS
;
1267 if (vm_map_pmap(map
) == pmap_kernel())
1269 /* assume a correct copy */
1270 memcpy(todata
, CAST_DOWN(void *, fromaddr
), length
);
1272 else if (current_map() == map
)
1274 if (copyin(fromaddr
, todata
, length
) != 0)
1275 kr
= KERN_INVALID_ADDRESS
;
1279 vm_map_reference(map
);
1280 oldmap
= vm_map_switch(map
);
1281 if (copyin(fromaddr
, todata
, length
) != 0)
1282 kr
= KERN_INVALID_ADDRESS
;
1283 vm_map_switch(oldmap
);
1284 vm_map_deallocate(map
);
1290 * Routine: copyoutmap
1292 * Like copyout, except that toaddr is an address
1293 * in the specified VM map. This implementation
1294 * is incomplete; it handles the current user map
1295 * and the kernel map/submaps.
1301 vm_map_address_t toaddr
,
1304 if (vm_map_pmap(map
) == pmap_kernel()) {
1305 /* assume a correct copy */
1306 memcpy(CAST_DOWN(void *, toaddr
), fromdata
, length
);
1307 return KERN_SUCCESS
;
1310 if (current_map() != map
)
1311 return KERN_NOT_SUPPORTED
;
1313 if (copyout(fromdata
, toaddr
, length
) != 0)
1314 return KERN_INVALID_ADDRESS
;
1316 return KERN_SUCCESS
;
1323 vm_map_offset_t off
,
1325 memory_object_t pager
,
1326 vm_object_offset_t file_off
)
1328 vm_map_entry_t entry
;
1330 vm_object_offset_t obj_off
;
1332 vm_map_offset_t base_offset
;
1333 vm_map_offset_t original_offset
;
1335 vm_map_size_t local_len
;
1339 original_offset
= off
;
1342 while(vm_map_lookup_entry(map
, off
, &entry
)) {
1345 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
1347 return KERN_SUCCESS
;
1349 if (entry
->is_sub_map
) {
1353 vm_map_lock(entry
->object
.sub_map
);
1354 map
= entry
->object
.sub_map
;
1355 off
= entry
->offset
+ (off
- entry
->vme_start
);
1356 vm_map_unlock(old_map
);
1359 obj
= entry
->object
.vm_object
;
1360 obj_off
= (off
- entry
->vme_start
) + entry
->offset
;
1361 while(obj
->shadow
) {
1362 obj_off
+= obj
->vo_shadow_offset
;
1365 if((obj
->pager_created
) && (obj
->pager
== pager
)) {
1366 if(((obj
->paging_offset
) + obj_off
) == file_off
) {
1367 if(off
!= base_offset
) {
1369 return KERN_FAILURE
;
1371 kr
= KERN_ALREADY_WAITING
;
1373 vm_object_offset_t obj_off_aligned
;
1374 vm_object_offset_t file_off_aligned
;
1376 obj_off_aligned
= obj_off
& ~PAGE_MASK
;
1377 file_off_aligned
= file_off
& ~PAGE_MASK
;
1379 if (file_off_aligned
== (obj
->paging_offset
+ obj_off_aligned
)) {
1381 * the target map and the file offset start in the same page
1382 * but are not identical...
1385 return KERN_FAILURE
;
1387 if ((file_off
< (obj
->paging_offset
+ obj_off_aligned
)) &&
1388 ((file_off
+ len
) > (obj
->paging_offset
+ obj_off_aligned
))) {
1390 * some portion of the tail of the I/O will fall
1391 * within the encompass of the target map
1394 return KERN_FAILURE
;
1396 if ((file_off_aligned
> (obj
->paging_offset
+ obj_off
)) &&
1397 (file_off_aligned
< (obj
->paging_offset
+ obj_off
) + len
)) {
1399 * the beginning page of the file offset falls within
1400 * the target map's encompass
1403 return KERN_FAILURE
;
1406 } else if(kr
!= KERN_SUCCESS
) {
1408 return KERN_FAILURE
;
1411 if(len
<= ((entry
->vme_end
- entry
->vme_start
) -
1412 (off
- entry
->vme_start
))) {
1416 len
-= (entry
->vme_end
- entry
->vme_start
) -
1417 (off
- entry
->vme_start
);
1419 base_offset
= base_offset
+ (local_len
- len
);
1420 file_off
= file_off
+ (local_len
- len
);
1422 if(map
!= base_map
) {
1424 vm_map_lock(base_map
);