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/thread.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pageout.h>
75 #include <kern/misc_protos.h>
80 #include <libkern/OSDebug.h>
81 #include <sys/kdebug.h>
84 * Variables exported by this module.
88 vm_map_t kernel_pageable_map
;
90 extern boolean_t vm_kernel_ready
;
93 * Forward declarations for internal functions.
95 extern kern_return_t
kmem_alloc_pages(
96 register vm_object_t object
,
97 register vm_object_offset_t offset
,
98 register vm_object_size_t size
);
112 vm_object_offset_t offset
;
113 vm_map_offset_t map_addr
;
114 vm_map_offset_t map_mask
;
115 vm_map_size_t map_size
, i
;
116 vm_map_entry_t entry
;
120 if (map
== VM_MAP_NULL
|| (flags
& ~(KMA_KOBJECT
| KMA_LOMEM
| KMA_NOPAGEWAIT
)))
121 return KERN_INVALID_ARGUMENT
;
123 map_size
= vm_map_round_page(size
,
124 VM_MAP_PAGE_MASK(map
));
125 map_mask
= (vm_map_offset_t
)mask
;
127 /* Check for zero allocation size (either directly or via overflow) */
130 return KERN_INVALID_ARGUMENT
;
134 * Allocate a new object (if necessary) and the reference we
135 * will be donating to the map entry. We must do this before
136 * locking the map, or risk deadlock with the default pager.
138 if ((flags
& KMA_KOBJECT
) != 0) {
139 object
= kernel_object
;
140 vm_object_reference(object
);
142 object
= vm_object_allocate(map_size
);
145 kr
= vm_map_find_space(map
, &map_addr
, map_size
, map_mask
, 0, &entry
);
146 if (KERN_SUCCESS
!= kr
) {
147 vm_object_deallocate(object
);
151 if (object
== kernel_object
) {
156 VME_OBJECT_SET(entry
, object
);
157 VME_OFFSET_SET(entry
, offset
);
158 VME_ALIAS_SET(entry
, tag
);
160 /* Take an extra object ref in case the map entry gets deleted */
161 vm_object_reference(object
);
164 kr
= cpm_allocate(CAST_DOWN(vm_size_t
, map_size
), &pages
, max_pnum
, pnum_mask
, FALSE
, flags
);
166 if (kr
!= KERN_SUCCESS
) {
168 vm_map_trunc_page(map_addr
,
169 VM_MAP_PAGE_MASK(map
)),
170 vm_map_round_page(map_addr
+ map_size
,
171 VM_MAP_PAGE_MASK(map
)),
173 vm_object_deallocate(object
);
178 vm_object_lock(object
);
179 for (i
= 0; i
< map_size
; i
+= PAGE_SIZE
) {
181 pages
= NEXT_PAGE(m
);
182 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
184 vm_page_insert(m
, object
, offset
+ i
);
186 vm_object_unlock(object
);
188 kr
= vm_map_wire(map
,
189 vm_map_trunc_page(map_addr
,
190 VM_MAP_PAGE_MASK(map
)),
191 vm_map_round_page(map_addr
+ map_size
,
192 VM_MAP_PAGE_MASK(map
)),
193 VM_PROT_DEFAULT
| VM_PROT_MEMORY_TAG_MAKE(tag
),
196 if (kr
!= KERN_SUCCESS
) {
197 if (object
== kernel_object
) {
198 vm_object_lock(object
);
199 vm_object_page_remove(object
, offset
, offset
+ map_size
);
200 vm_object_unlock(object
);
203 vm_map_trunc_page(map_addr
,
204 VM_MAP_PAGE_MASK(map
)),
205 vm_map_round_page(map_addr
+ map_size
,
206 VM_MAP_PAGE_MASK(map
)),
208 vm_object_deallocate(object
);
211 vm_object_deallocate(object
);
213 if (object
== kernel_object
)
214 vm_map_simplify(map
, map_addr
);
216 *addrp
= (vm_offset_t
) map_addr
;
217 assert((vm_map_offset_t
) *addrp
== map_addr
);
222 * Master entry point for allocating kernel memory.
223 * NOTE: this routine is _never_ interrupt safe.
225 * map : map to allocate into
226 * addrp : pointer to start address of new memory
227 * size : size of memory requested
229 * KMA_HERE *addrp is base address, else "anywhere"
230 * KMA_NOPAGEWAIT don't wait for pages if unavailable
231 * KMA_KOBJECT use kernel_object
232 * KMA_LOMEM support for 32 bit devices in a 64 bit world
233 * if set and a lomemory pool is available
234 * grab pages from it... this also implies
239 kernel_memory_allocate(
240 register vm_map_t map
,
241 register vm_offset_t
*addrp
,
242 register vm_size_t size
,
243 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
;
272 vm_alloc_flags
= VM_MAKE_TAG(tag
);
274 /* Check for zero allocation size (either directly or via overflow) */
277 return KERN_INVALID_ARGUMENT
;
281 * limit the size of a single extent of wired memory
282 * to try and limit the damage to the system if
283 * too many pages get wired down
284 * limit raised to 2GB with 128GB max physical limit,
285 * but scaled by installed memory above this
287 if ( !(flags
& KMA_VAONLY
) && map_size
> MAX(1ULL<<31, sane_size
/64)) {
288 return KERN_RESOURCE_SHORTAGE
;
294 * Guard pages are implemented as ficticious pages. By placing guard pages
295 * on either end of a stack, they can help detect cases where a thread walks
296 * off either end of its stack. They are allocated and set up here and attempts
297 * to access those pages are trapped in vm_fault_page().
299 * The map_size we were passed may include extra space for
300 * guard pages. If those were requested, then back it out of fill_size
301 * since vm_map_find_space() takes just the actual size not including
302 * guard pages. Similarly, fill_start indicates where the actual pages
303 * will begin in the range.
307 fill_size
= map_size
;
309 if (flags
& KMA_GUARD_FIRST
) {
310 vm_alloc_flags
|= VM_FLAGS_GUARD_BEFORE
;
311 fill_start
+= PAGE_SIZE_64
;
312 fill_size
-= PAGE_SIZE_64
;
313 if (map_size
< fill_start
+ fill_size
) {
314 /* no space for a guard page */
316 return KERN_INVALID_ARGUMENT
;
320 if (flags
& KMA_GUARD_LAST
) {
321 vm_alloc_flags
|= VM_FLAGS_GUARD_AFTER
;
322 fill_size
-= PAGE_SIZE_64
;
323 if (map_size
<= fill_start
+ fill_size
) {
324 /* no space for a guard page */
326 return KERN_INVALID_ARGUMENT
;
330 wired_page_count
= (int) (fill_size
/ PAGE_SIZE_64
);
331 assert(wired_page_count
* PAGE_SIZE_64
== fill_size
);
333 for (i
= 0; i
< guard_page_count
; i
++) {
335 mem
= vm_page_grab_guard();
337 if (mem
!= VM_PAGE_NULL
)
339 if (flags
& KMA_NOPAGEWAIT
) {
340 kr
= KERN_RESOURCE_SHORTAGE
;
343 vm_page_more_fictitious();
345 mem
->pageq
.next
= (queue_entry_t
)guard_page_list
;
346 guard_page_list
= mem
;
349 if (! (flags
& KMA_VAONLY
)) {
350 for (i
= 0; i
< wired_page_count
; i
++) {
351 uint64_t unavailable
;
354 if (flags
& KMA_LOMEM
)
355 mem
= vm_page_grablo();
357 mem
= vm_page_grab();
359 if (mem
!= VM_PAGE_NULL
)
362 if (flags
& KMA_NOPAGEWAIT
) {
363 kr
= KERN_RESOURCE_SHORTAGE
;
366 if ((flags
& KMA_LOMEM
) && (vm_lopage_needed
== TRUE
)) {
367 kr
= KERN_RESOURCE_SHORTAGE
;
370 unavailable
= (vm_page_wire_count
+ vm_page_free_target
) * PAGE_SIZE
;
372 if (unavailable
> max_mem
|| map_size
> (max_mem
- unavailable
)) {
373 kr
= KERN_RESOURCE_SHORTAGE
;
378 mem
->pageq
.next
= (queue_entry_t
)wired_page_list
;
379 wired_page_list
= mem
;
384 * Allocate a new object (if necessary). We must do this before
385 * locking the map, or risk deadlock with the default pager.
387 if ((flags
& KMA_KOBJECT
) != 0) {
388 object
= kernel_object
;
389 vm_object_reference(object
);
390 } else if ((flags
& KMA_COMPRESSOR
) != 0) {
391 object
= compressor_object
;
392 vm_object_reference(object
);
394 object
= vm_object_allocate(map_size
);
397 kr
= vm_map_find_space(map
, &map_addr
,
399 vm_alloc_flags
, &entry
);
400 if (KERN_SUCCESS
!= kr
) {
401 vm_object_deallocate(object
);
405 if (object
== kernel_object
|| object
== compressor_object
) {
410 VME_OBJECT_SET(entry
, object
);
411 VME_OFFSET_SET(entry
, offset
);
413 if (object
!= compressor_object
)
414 entry
->wired_count
++;
416 if (flags
& KMA_PERMANENT
)
417 entry
->permanent
= TRUE
;
419 if (object
!= kernel_object
&& object
!= compressor_object
)
420 vm_object_reference(object
);
422 vm_object_lock(object
);
428 if (guard_page_list
== NULL
)
429 panic("kernel_memory_allocate: guard_page_list == NULL");
431 mem
= guard_page_list
;
432 guard_page_list
= (vm_page_t
)mem
->pageq
.next
;
433 mem
->pageq
.next
= NULL
;
435 vm_page_insert(mem
, object
, offset
+ pg_offset
);
438 pg_offset
+= PAGE_SIZE_64
;
441 kma_prot
= VM_PROT_READ
| VM_PROT_WRITE
;
443 if (flags
& KMA_VAONLY
) {
444 pg_offset
= fill_start
+ fill_size
;
446 for (pg_offset
= fill_start
; pg_offset
< fill_start
+ fill_size
; pg_offset
+= PAGE_SIZE_64
) {
447 if (wired_page_list
== NULL
)
448 panic("kernel_memory_allocate: wired_page_list == NULL");
450 mem
= wired_page_list
;
451 wired_page_list
= (vm_page_t
)mem
->pageq
.next
;
452 mem
->pageq
.next
= NULL
;
455 vm_page_insert_wired(mem
, object
, offset
+ pg_offset
, tag
);
459 mem
->wpmapped
= TRUE
;
461 PMAP_ENTER_OPTIONS(kernel_pmap
, map_addr
+ pg_offset
, mem
,
462 kma_prot
, VM_PROT_NONE
, ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
463 PMAP_OPTIONS_NOWAIT
, pe_result
);
465 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
466 vm_object_unlock(object
);
468 PMAP_ENTER(kernel_pmap
, map_addr
+ pg_offset
, mem
,
469 kma_prot
, VM_PROT_NONE
, ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
);
471 vm_object_lock(object
);
473 if (flags
& KMA_NOENCRYPT
) {
474 bzero(CAST_DOWN(void *, (map_addr
+ pg_offset
)), PAGE_SIZE
);
476 pmap_set_noencrypt(mem
->phys_page
);
480 if ((fill_start
+ fill_size
) < map_size
) {
481 if (guard_page_list
== NULL
)
482 panic("kernel_memory_allocate: guard_page_list == NULL");
484 mem
= guard_page_list
;
485 guard_page_list
= (vm_page_t
)mem
->pageq
.next
;
486 mem
->pageq
.next
= NULL
;
488 vm_page_insert(mem
, object
, offset
+ pg_offset
);
492 if (guard_page_list
|| wired_page_list
)
493 panic("kernel_memory_allocate: non empty list\n");
495 if (! (flags
& KMA_VAONLY
)) {
496 vm_page_lockspin_queues();
497 vm_page_wire_count
+= wired_page_count
;
498 vm_page_unlock_queues();
501 vm_object_unlock(object
);
504 * now that the pages are wired, we no longer have to fear coalesce
506 if (object
== kernel_object
|| object
== compressor_object
)
507 vm_map_simplify(map
, map_addr
);
509 vm_object_deallocate(object
);
512 * Return the memory, not zeroed.
514 *addrp
= CAST_DOWN(vm_offset_t
, map_addr
);
519 vm_page_free_list(guard_page_list
, FALSE
);
522 vm_page_free_list(wired_page_list
, FALSE
);
528 kernel_memory_populate(
536 vm_object_offset_t offset
, pg_offset
;
537 kern_return_t kr
, pe_result
;
539 vm_page_t page_list
= NULL
;
543 page_count
= (int) (size
/ PAGE_SIZE_64
);
545 assert((flags
& (KMA_COMPRESSOR
|KMA_KOBJECT
)) != (KMA_COMPRESSOR
|KMA_KOBJECT
));
547 if (flags
& KMA_COMPRESSOR
) {
549 pg_offset
= page_count
* PAGE_SIZE_64
;
553 mem
= vm_page_grab();
555 if (mem
!= VM_PAGE_NULL
)
560 mem
->pageq
.next
= (queue_entry_t
) page_list
;
563 pg_offset
-= PAGE_SIZE_64
;
565 kr
= pmap_enter_options(kernel_pmap
,
566 addr
+ pg_offset
, mem
->phys_page
,
567 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
, 0, TRUE
,
568 PMAP_OPTIONS_INTERNAL
, NULL
);
569 assert(kr
== KERN_SUCCESS
);
574 object
= compressor_object
;
576 vm_object_lock(object
);
580 pg_offset
+= PAGE_SIZE_64
) {
583 page_list
= (vm_page_t
) mem
->pageq
.next
;
584 mem
->pageq
.next
= NULL
;
586 vm_page_insert(mem
, object
, offset
+ pg_offset
);
591 mem
->wpmapped
= TRUE
;
592 mem
->compressor
= TRUE
;
594 vm_object_unlock(object
);
599 for (i
= 0; i
< page_count
; i
++) {
601 if (flags
& KMA_LOMEM
)
602 mem
= vm_page_grablo();
604 mem
= vm_page_grab();
606 if (mem
!= VM_PAGE_NULL
)
609 if (flags
& KMA_NOPAGEWAIT
) {
610 kr
= KERN_RESOURCE_SHORTAGE
;
613 if ((flags
& KMA_LOMEM
) &&
614 (vm_lopage_needed
== TRUE
)) {
615 kr
= KERN_RESOURCE_SHORTAGE
;
620 mem
->pageq
.next
= (queue_entry_t
) page_list
;
623 if (flags
& KMA_KOBJECT
) {
625 object
= kernel_object
;
627 vm_object_lock(object
);
630 * If it's not the kernel object, we need to:
634 * take reference on object;
637 panic("kernel_memory_populate(%p,0x%llx,0x%llx,0x%x): "
639 map
, (uint64_t) addr
, (uint64_t) size
, flags
);
644 pg_offset
+= PAGE_SIZE_64
) {
646 if (page_list
== NULL
)
647 panic("kernel_memory_populate: page_list == NULL");
650 page_list
= (vm_page_t
) mem
->pageq
.next
;
651 mem
->pageq
.next
= NULL
;
655 vm_page_insert_wired(mem
, object
, offset
+ pg_offset
, tag
);
659 mem
->wpmapped
= TRUE
;
661 PMAP_ENTER_OPTIONS(kernel_pmap
, addr
+ pg_offset
, mem
,
662 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
,
663 ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
664 PMAP_OPTIONS_NOWAIT
, pe_result
);
666 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
668 vm_object_unlock(object
);
670 PMAP_ENTER(kernel_pmap
, addr
+ pg_offset
, mem
,
671 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
,
672 ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
);
674 vm_object_lock(object
);
676 if (flags
& KMA_NOENCRYPT
) {
677 bzero(CAST_DOWN(void *, (addr
+ pg_offset
)), PAGE_SIZE
);
678 pmap_set_noencrypt(mem
->phys_page
);
681 vm_page_lock_queues();
682 vm_page_wire_count
+= page_count
;
683 vm_page_unlock_queues();
685 vm_object_unlock(object
);
691 vm_page_free_list(page_list
, FALSE
);
698 kernel_memory_depopulate(
705 vm_object_offset_t offset
, pg_offset
;
707 vm_page_t local_freeq
= NULL
;
709 assert((flags
& (KMA_COMPRESSOR
|KMA_KOBJECT
)) != (KMA_COMPRESSOR
|KMA_KOBJECT
));
711 if (flags
& KMA_COMPRESSOR
) {
713 object
= compressor_object
;
715 vm_object_lock(object
);
716 } else if (flags
& KMA_KOBJECT
) {
718 object
= kernel_object
;
720 vm_object_lock(object
);
725 * If it's not the kernel object, we need to:
731 panic("kernel_memory_depopulate(%p,0x%llx,0x%llx,0x%x): "
733 map
, (uint64_t) addr
, (uint64_t) size
, flags
);
735 pmap_protect(kernel_map
->pmap
, offset
, offset
+ size
, VM_PROT_NONE
);
739 pg_offset
+= PAGE_SIZE_64
) {
741 mem
= vm_page_lookup(object
, offset
+ pg_offset
);
745 pmap_disconnect(mem
->phys_page
);
750 vm_page_remove(mem
, TRUE
);
753 assert(mem
->pageq
.next
== NULL
&&
754 mem
->pageq
.prev
== NULL
);
755 mem
->pageq
.next
= (queue_entry_t
)local_freeq
;
758 vm_object_unlock(object
);
761 vm_page_free_list(local_freeq
, TRUE
);
767 * Allocate wired-down memory in the kernel's address map
768 * or a submap. The memory is not zero-filled.
777 return (kmem_alloc(map
, addrp
, size
, vm_tag_bt()));
787 kern_return_t kr
= kernel_memory_allocate(map
, addrp
, size
, 0, 0, tag
);
788 TRACE_MACHLEAKS(KMEM_ALLOC_CODE
, KMEM_ALLOC_CODE_2
, size
, *addrp
);
795 * Reallocate wired-down memory in the kernel's address map
796 * or a submap. Newly allocated pages are not zeroed.
797 * This can only be used on regions allocated with kmem_alloc.
799 * If successful, the pages in the old region are mapped twice.
800 * The old region is unchanged. Use kmem_free to get rid of it.
807 vm_offset_t
*newaddrp
,
812 vm_object_offset_t offset
;
813 vm_map_offset_t oldmapmin
;
814 vm_map_offset_t oldmapmax
;
815 vm_map_offset_t newmapaddr
;
816 vm_map_size_t oldmapsize
;
817 vm_map_size_t newmapsize
;
818 vm_map_entry_t oldentry
;
819 vm_map_entry_t newentry
;
823 oldmapmin
= vm_map_trunc_page(oldaddr
,
824 VM_MAP_PAGE_MASK(map
));
825 oldmapmax
= vm_map_round_page(oldaddr
+ oldsize
,
826 VM_MAP_PAGE_MASK(map
));
827 oldmapsize
= oldmapmax
- oldmapmin
;
828 newmapsize
= vm_map_round_page(newsize
,
829 VM_MAP_PAGE_MASK(map
));
833 * Find the VM object backing the old region.
838 if (!vm_map_lookup_entry(map
, oldmapmin
, &oldentry
))
839 panic("kmem_realloc");
840 object
= VME_OBJECT(oldentry
);
843 * Increase the size of the object and
844 * fill in the new region.
847 vm_object_reference(object
);
848 /* by grabbing the object lock before unlocking the map */
849 /* we guarantee that we will panic if more than one */
850 /* attempt is made to realloc a kmem_alloc'd area */
851 vm_object_lock(object
);
853 if (object
->vo_size
!= oldmapsize
)
854 panic("kmem_realloc");
855 object
->vo_size
= newmapsize
;
856 vm_object_unlock(object
);
858 /* allocate the new pages while expanded portion of the */
859 /* object is still not mapped */
860 kmem_alloc_pages(object
, vm_object_round_page(oldmapsize
),
861 vm_object_round_page(newmapsize
-oldmapsize
));
864 * Find space for the new region.
867 kr
= vm_map_find_space(map
, &newmapaddr
, newmapsize
,
868 (vm_map_offset_t
) 0, 0, &newentry
);
869 if (kr
!= KERN_SUCCESS
) {
870 vm_object_lock(object
);
871 for(offset
= oldmapsize
;
872 offset
< newmapsize
; offset
+= PAGE_SIZE
) {
873 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
877 object
->vo_size
= oldmapsize
;
878 vm_object_unlock(object
);
879 vm_object_deallocate(object
);
882 VME_OBJECT_SET(newentry
, object
);
883 VME_OFFSET_SET(newentry
, 0);
884 VME_ALIAS_SET(newentry
, tag
);
885 assert(newentry
->wired_count
== 0);
888 /* add an extra reference in case we have someone doing an */
889 /* unexpected deallocate */
890 vm_object_reference(object
);
893 kr
= vm_map_wire(map
, newmapaddr
, newmapaddr
+ newmapsize
,
894 VM_PROT_DEFAULT
| VM_PROT_MEMORY_TAG_MAKE(tag
), FALSE
);
895 if (KERN_SUCCESS
!= kr
) {
896 vm_map_remove(map
, newmapaddr
, newmapaddr
+ newmapsize
, 0);
897 vm_object_lock(object
);
898 for(offset
= oldsize
; offset
< newmapsize
; offset
+= PAGE_SIZE
) {
899 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
903 object
->vo_size
= oldmapsize
;
904 vm_object_unlock(object
);
905 vm_object_deallocate(object
);
908 vm_object_deallocate(object
);
910 *newaddrp
= CAST_DOWN(vm_offset_t
, newmapaddr
);
915 * kmem_alloc_kobject:
917 * Allocate wired-down memory in the kernel's address map
918 * or a submap. The memory is not zero-filled.
920 * The memory is allocated in the kernel_object.
921 * It may not be copied with vm_map_copy, and
922 * it may not be reallocated with kmem_realloc.
926 kmem_alloc_kobject_external(
931 return (kmem_alloc_kobject(map
, addrp
, size
, vm_tag_bt()));
941 return kernel_memory_allocate(map
, addrp
, size
, 0, KMA_KOBJECT
, tag
);
945 * kmem_alloc_aligned:
947 * Like kmem_alloc_kobject, except that the memory is aligned.
948 * The size should be a power-of-2.
958 if ((size
& (size
- 1)) != 0)
959 panic("kmem_alloc_aligned: size not aligned");
960 return kernel_memory_allocate(map
, addrp
, size
, size
- 1, KMA_KOBJECT
, tag
);
964 * kmem_alloc_pageable:
966 * Allocate pageable memory in the kernel's address map.
970 kmem_alloc_pageable_external(
975 return (kmem_alloc_pageable(map
, addrp
, size
, vm_tag_bt()));
985 vm_map_offset_t map_addr
;
986 vm_map_size_t map_size
;
990 map_addr
= (vm_map_min(map
)) + PAGE_SIZE
;
992 map_addr
= vm_map_min(map
);
994 map_size
= vm_map_round_page(size
,
995 VM_MAP_PAGE_MASK(map
));
997 kr
= vm_map_enter(map
, &map_addr
, map_size
,
999 VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(tag
),
1000 VM_OBJECT_NULL
, (vm_object_offset_t
) 0, FALSE
,
1001 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
1003 if (kr
!= KERN_SUCCESS
)
1006 *addrp
= CAST_DOWN(vm_offset_t
, map_addr
);
1007 return KERN_SUCCESS
;
1013 * Release a region of kernel virtual memory allocated
1014 * with kmem_alloc, kmem_alloc_kobject, or kmem_alloc_pageable,
1015 * and return the physical pages associated with that region.
1026 assert(addr
>= VM_MIN_KERNEL_AND_KEXT_ADDRESS
);
1028 TRACE_MACHLEAKS(KMEM_FREE_CODE
, KMEM_FREE_CODE_2
, size
, addr
);
1032 printf("kmem_free called with size==0 for map: %p with addr: 0x%llx\n",map
,(uint64_t)addr
);
1037 kr
= vm_map_remove(map
,
1038 vm_map_trunc_page(addr
,
1039 VM_MAP_PAGE_MASK(map
)),
1040 vm_map_round_page(addr
+ size
,
1041 VM_MAP_PAGE_MASK(map
)),
1042 VM_MAP_REMOVE_KUNWIRE
);
1043 if (kr
!= KERN_SUCCESS
)
1048 * Allocate new pages in an object.
1053 register vm_object_t object
,
1054 register vm_object_offset_t offset
,
1055 register vm_object_size_t size
)
1057 vm_object_size_t alloc_size
;
1059 alloc_size
= vm_object_round_page(size
);
1060 vm_object_lock(object
);
1061 while (alloc_size
) {
1062 register vm_page_t mem
;
1068 while (VM_PAGE_NULL
==
1069 (mem
= vm_page_alloc(object
, offset
))) {
1070 vm_object_unlock(object
);
1072 vm_object_lock(object
);
1076 alloc_size
-= PAGE_SIZE
;
1077 offset
+= PAGE_SIZE
;
1079 vm_object_unlock(object
);
1080 return KERN_SUCCESS
;
1086 * Allocates a map to manage a subrange
1087 * of the kernel virtual address space.
1089 * Arguments are as follows:
1091 * parent Map to take range from
1092 * addr Address of start of range (IN/OUT)
1093 * size Size of range to find
1094 * pageable Can region be paged
1095 * anywhere Can region be located anywhere in map
1096 * new_map Pointer to new submap
1108 vm_map_offset_t map_addr
;
1109 vm_map_size_t map_size
;
1112 map_size
= vm_map_round_page(size
,
1113 VM_MAP_PAGE_MASK(parent
));
1116 * Need reference on submap object because it is internal
1117 * to the vm_system. vm_object_enter will never be called
1118 * on it (usual source of reference for vm_map_enter).
1120 vm_object_reference(vm_submap_object
);
1122 map_addr
= ((flags
& VM_FLAGS_ANYWHERE
)
1123 ? vm_map_min(parent
)
1124 : vm_map_trunc_page(*addr
,
1125 VM_MAP_PAGE_MASK(parent
)));
1127 kr
= vm_map_enter(parent
, &map_addr
, map_size
,
1128 (vm_map_offset_t
) 0, flags
,
1129 vm_submap_object
, (vm_object_offset_t
) 0, FALSE
,
1130 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
1131 if (kr
!= KERN_SUCCESS
) {
1132 vm_object_deallocate(vm_submap_object
);
1136 pmap_reference(vm_map_pmap(parent
));
1137 map
= vm_map_create(vm_map_pmap(parent
), map_addr
, map_addr
+ map_size
, pageable
);
1138 if (map
== VM_MAP_NULL
)
1139 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
1140 /* inherit the parent map's page size */
1141 vm_map_set_page_shift(map
, VM_MAP_PAGE_SHIFT(parent
));
1143 kr
= vm_map_submap(parent
, map_addr
, map_addr
+ map_size
, map
, map_addr
, FALSE
);
1144 if (kr
!= KERN_SUCCESS
) {
1146 * See comment preceding vm_map_submap().
1148 vm_map_remove(parent
, map_addr
, map_addr
+ map_size
, VM_MAP_NO_FLAGS
);
1149 vm_map_deallocate(map
); /* also removes ref to pmap */
1150 vm_object_deallocate(vm_submap_object
);
1153 *addr
= CAST_DOWN(vm_offset_t
, map_addr
);
1155 return (KERN_SUCCESS
);
1161 * Initialize the kernel's virtual memory map, taking
1162 * into account all memory allocated up to this time.
1169 vm_map_offset_t map_start
;
1170 vm_map_offset_t map_end
;
1172 map_start
= vm_map_trunc_page(start
,
1173 VM_MAP_PAGE_MASK(kernel_map
));
1174 map_end
= vm_map_round_page(end
,
1175 VM_MAP_PAGE_MASK(kernel_map
));
1177 kernel_map
= vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS
,
1180 * Reserve virtual memory allocated up to this time.
1182 if (start
!= VM_MIN_KERNEL_AND_KEXT_ADDRESS
) {
1183 vm_map_offset_t map_addr
;
1186 map_addr
= VM_MIN_KERNEL_AND_KEXT_ADDRESS
;
1187 kr
= vm_map_enter(kernel_map
,
1189 (vm_map_size_t
)(map_start
- VM_MIN_KERNEL_AND_KEXT_ADDRESS
),
1190 (vm_map_offset_t
) 0,
1191 VM_FLAGS_FIXED
| VM_FLAGS_NO_PMAP_CHECK
,
1193 (vm_object_offset_t
) 0, FALSE
,
1194 VM_PROT_NONE
, VM_PROT_NONE
,
1195 VM_INHERIT_DEFAULT
);
1197 if (kr
!= KERN_SUCCESS
) {
1198 panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n",
1199 (uint64_t) start
, (uint64_t) end
,
1200 (uint64_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS
,
1201 (uint64_t) (map_start
- VM_MIN_KERNEL_AND_KEXT_ADDRESS
),
1207 * Set the default global user wire limit which limits the amount of
1208 * memory that can be locked via mlock(). We set this to the total
1209 * amount of memory that are potentially usable by a user app (max_mem)
1210 * minus a certain amount. This can be overridden via a sysctl.
1212 vm_global_no_user_wire_amount
= MIN(max_mem
*20/100,
1213 VM_NOT_USER_WIREABLE
);
1214 vm_global_user_wire_limit
= max_mem
- vm_global_no_user_wire_amount
;
1216 /* the default per user limit is the same as the global limit */
1217 vm_user_wire_limit
= vm_global_user_wire_limit
;
1222 * Routine: copyinmap
1224 * Like copyin, except that fromaddr is an address
1225 * in the specified VM map. This implementation
1226 * is incomplete; it handles the current user map
1227 * and the kernel map/submaps.
1232 vm_map_offset_t fromaddr
,
1236 kern_return_t kr
= KERN_SUCCESS
;
1239 if (vm_map_pmap(map
) == pmap_kernel())
1241 /* assume a correct copy */
1242 memcpy(todata
, CAST_DOWN(void *, fromaddr
), length
);
1244 else if (current_map() == map
)
1246 if (copyin(fromaddr
, todata
, length
) != 0)
1247 kr
= KERN_INVALID_ADDRESS
;
1251 vm_map_reference(map
);
1252 oldmap
= vm_map_switch(map
);
1253 if (copyin(fromaddr
, todata
, length
) != 0)
1254 kr
= KERN_INVALID_ADDRESS
;
1255 vm_map_switch(oldmap
);
1256 vm_map_deallocate(map
);
1262 * Routine: copyoutmap
1264 * Like copyout, except that toaddr is an address
1265 * in the specified VM map. This implementation
1266 * is incomplete; it handles the current user map
1267 * and the kernel map/submaps.
1273 vm_map_address_t toaddr
,
1276 if (vm_map_pmap(map
) == pmap_kernel()) {
1277 /* assume a correct copy */
1278 memcpy(CAST_DOWN(void *, toaddr
), fromdata
, length
);
1279 return KERN_SUCCESS
;
1282 if (current_map() != map
)
1283 return KERN_NOT_SUPPORTED
;
1285 if (copyout(fromdata
, toaddr
, length
) != 0)
1286 return KERN_INVALID_ADDRESS
;
1288 return KERN_SUCCESS
;
1295 vm_map_offset_t off
,
1297 memory_object_t pager
,
1298 vm_object_offset_t file_off
)
1300 vm_map_entry_t entry
;
1302 vm_object_offset_t obj_off
;
1304 vm_map_offset_t base_offset
;
1305 vm_map_offset_t original_offset
;
1307 vm_map_size_t local_len
;
1311 original_offset
= off
;
1314 while(vm_map_lookup_entry(map
, off
, &entry
)) {
1317 if (VME_OBJECT(entry
) == VM_OBJECT_NULL
) {
1319 return KERN_SUCCESS
;
1321 if (entry
->is_sub_map
) {
1325 vm_map_lock(VME_SUBMAP(entry
));
1326 map
= VME_SUBMAP(entry
);
1327 off
= VME_OFFSET(entry
) + (off
- entry
->vme_start
);
1328 vm_map_unlock(old_map
);
1331 obj
= VME_OBJECT(entry
);
1332 obj_off
= (off
- entry
->vme_start
) + VME_OFFSET(entry
);
1333 while(obj
->shadow
) {
1334 obj_off
+= obj
->vo_shadow_offset
;
1337 if((obj
->pager_created
) && (obj
->pager
== pager
)) {
1338 if(((obj
->paging_offset
) + obj_off
) == file_off
) {
1339 if(off
!= base_offset
) {
1341 return KERN_FAILURE
;
1343 kr
= KERN_ALREADY_WAITING
;
1345 vm_object_offset_t obj_off_aligned
;
1346 vm_object_offset_t file_off_aligned
;
1348 obj_off_aligned
= obj_off
& ~PAGE_MASK
;
1349 file_off_aligned
= file_off
& ~PAGE_MASK
;
1351 if (file_off_aligned
== (obj
->paging_offset
+ obj_off_aligned
)) {
1353 * the target map and the file offset start in the same page
1354 * but are not identical...
1357 return KERN_FAILURE
;
1359 if ((file_off
< (obj
->paging_offset
+ obj_off_aligned
)) &&
1360 ((file_off
+ len
) > (obj
->paging_offset
+ obj_off_aligned
))) {
1362 * some portion of the tail of the I/O will fall
1363 * within the encompass of the target map
1366 return KERN_FAILURE
;
1368 if ((file_off_aligned
> (obj
->paging_offset
+ obj_off
)) &&
1369 (file_off_aligned
< (obj
->paging_offset
+ obj_off
) + len
)) {
1371 * the beginning page of the file offset falls within
1372 * the target map's encompass
1375 return KERN_FAILURE
;
1378 } else if(kr
!= KERN_SUCCESS
) {
1380 return KERN_FAILURE
;
1383 if(len
<= ((entry
->vme_end
- entry
->vme_start
) -
1384 (off
- entry
->vme_start
))) {
1388 len
-= (entry
->vme_end
- entry
->vme_start
) -
1389 (off
- entry
->vme_start
);
1391 base_offset
= base_offset
+ (local_len
- len
);
1392 file_off
= file_off
+ (local_len
- len
);
1394 if(map
!= base_map
) {
1396 vm_map_lock(base_map
);
1407 * The following two functions are to be used when exposing kernel
1408 * addresses to userspace via any of the various debug or info
1409 * facilities that exist. These are basically the same as VM_KERNEL_ADDRPERM()
1410 * and VM_KERNEL_UNSLIDE_OR_PERM() except they use a different random seed and
1411 * are exported to KEXTs.
1413 * NOTE: USE THE MACRO VERSIONS OF THESE FUNCTIONS (in vm_param.h) FROM WITHIN THE KERNEL
1417 * vm_kernel_addrperm_external:
1419 * Used when exposing an address to userspace which is in the kernel's
1420 * "heap". These addresses are not loaded from anywhere and are resultingly
1421 * unslid. We apply a permutation value to obscure the address.
1424 vm_kernel_addrperm_external(
1426 vm_offset_t
*perm_addr
)
1433 *perm_addr
= (addr
+ vm_kernel_addrperm_ext
);
1438 * vm_kernel_unslide_or_perm_external:
1440 * Use this macro when exposing an address to userspace that could come from
1441 * either kernel text/data *or* the heap.
1444 vm_kernel_unslide_or_perm_external(
1446 vm_offset_t
*up_addr
)
1448 if (VM_KERNEL_IS_SLID(addr
) || VM_KERNEL_IS_KEXT(addr
) ||
1449 VM_KERNEL_IS_PRELINKTEXT(addr
) || VM_KERNEL_IS_PRELINKINFO(addr
) ||
1450 VM_KERNEL_IS_KEXT_LINKEDIT(addr
)) {
1451 *up_addr
= addr
- vm_kernel_slide
;
1455 vm_kernel_addrperm_external(addr
, up_addr
);