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_compressor.h>
75 #include <vm/vm_pageout.h>
76 #include <kern/misc_protos.h>
81 #include <libkern/OSDebug.h>
82 #include <libkern/crypto/sha2.h>
83 #include <sys/kdebug.h>
85 #include <san/kasan.h>
88 * Variables exported by this module.
92 vm_map_t kernel_pageable_map
;
94 extern boolean_t vm_kernel_ready
;
97 * Forward declarations for internal functions.
99 extern kern_return_t
kmem_alloc_pages(
101 vm_object_offset_t offset
,
102 vm_object_size_t size
);
116 vm_object_offset_t offset
;
117 vm_map_offset_t map_addr
;
118 vm_map_offset_t map_mask
;
119 vm_map_size_t map_size
, i
;
120 vm_map_entry_t entry
;
124 assert(VM_KERN_MEMORY_NONE
!= tag
);
126 if (map
== VM_MAP_NULL
|| (flags
& ~(KMA_KOBJECT
| KMA_LOMEM
| KMA_NOPAGEWAIT
)))
127 return KERN_INVALID_ARGUMENT
;
129 map_size
= vm_map_round_page(size
,
130 VM_MAP_PAGE_MASK(map
));
131 map_mask
= (vm_map_offset_t
)mask
;
133 /* Check for zero allocation size (either directly or via overflow) */
136 return KERN_INVALID_ARGUMENT
;
140 * Allocate a new object (if necessary) and the reference we
141 * will be donating to the map entry. We must do this before
142 * locking the map, or risk deadlock with the default pager.
144 if ((flags
& KMA_KOBJECT
) != 0) {
145 object
= kernel_object
;
146 vm_object_reference(object
);
148 object
= vm_object_allocate(map_size
);
151 kr
= vm_map_find_space(map
, &map_addr
, map_size
, map_mask
, 0,
152 VM_MAP_KERNEL_FLAGS_NONE
, tag
, &entry
);
153 if (KERN_SUCCESS
!= kr
) {
154 vm_object_deallocate(object
);
158 if (object
== kernel_object
) {
163 VME_OBJECT_SET(entry
, object
);
164 VME_OFFSET_SET(entry
, offset
);
166 /* Take an extra object ref in case the map entry gets deleted */
167 vm_object_reference(object
);
170 kr
= cpm_allocate(CAST_DOWN(vm_size_t
, map_size
), &pages
, max_pnum
, pnum_mask
, FALSE
, flags
);
172 if (kr
!= KERN_SUCCESS
) {
174 vm_map_trunc_page(map_addr
,
175 VM_MAP_PAGE_MASK(map
)),
176 vm_map_round_page(map_addr
+ map_size
,
177 VM_MAP_PAGE_MASK(map
)),
178 VM_MAP_REMOVE_NO_FLAGS
);
179 vm_object_deallocate(object
);
184 vm_object_lock(object
);
185 for (i
= 0; i
< map_size
; i
+= PAGE_SIZE
) {
187 pages
= NEXT_PAGE(m
);
188 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
190 vm_page_insert(m
, object
, offset
+ i
);
192 vm_object_unlock(object
);
194 kr
= vm_map_wire_kernel(map
,
195 vm_map_trunc_page(map_addr
,
196 VM_MAP_PAGE_MASK(map
)),
197 vm_map_round_page(map_addr
+ map_size
,
198 VM_MAP_PAGE_MASK(map
)),
199 VM_PROT_DEFAULT
, tag
,
202 if (kr
!= KERN_SUCCESS
) {
203 if (object
== kernel_object
) {
204 vm_object_lock(object
);
205 vm_object_page_remove(object
, offset
, offset
+ map_size
);
206 vm_object_unlock(object
);
209 vm_map_trunc_page(map_addr
,
210 VM_MAP_PAGE_MASK(map
)),
211 vm_map_round_page(map_addr
+ map_size
,
212 VM_MAP_PAGE_MASK(map
)),
213 VM_MAP_REMOVE_NO_FLAGS
);
214 vm_object_deallocate(object
);
217 vm_object_deallocate(object
);
219 if (object
== kernel_object
) {
220 vm_map_simplify(map
, map_addr
);
221 vm_tag_update_size(tag
, map_size
);
223 *addrp
= (vm_offset_t
) map_addr
;
224 assert((vm_map_offset_t
) *addrp
== map_addr
);
230 * Master entry point for allocating kernel memory.
231 * NOTE: this routine is _never_ interrupt safe.
233 * map : map to allocate into
234 * addrp : pointer to start address of new memory
235 * size : size of memory requested
237 * KMA_HERE *addrp is base address, else "anywhere"
238 * KMA_NOPAGEWAIT don't wait for pages if unavailable
239 * KMA_KOBJECT use kernel_object
240 * KMA_LOMEM support for 32 bit devices in a 64 bit world
241 * if set and a lomemory pool is available
242 * grab pages from it... this also implies
247 kernel_memory_allocate(
256 vm_object_offset_t offset
;
257 vm_object_offset_t pg_offset
;
258 vm_map_entry_t entry
= NULL
;
259 vm_map_offset_t map_addr
, fill_start
;
260 vm_map_offset_t map_mask
;
261 vm_map_size_t map_size
, fill_size
;
262 kern_return_t kr
, pe_result
;
264 vm_page_t guard_page_list
= NULL
;
265 vm_page_t wired_page_list
= NULL
;
266 int guard_page_count
= 0;
267 int wired_page_count
= 0;
268 int page_grab_count
= 0;
271 vm_map_kernel_flags_t vmk_flags
;
274 if (! vm_kernel_ready
) {
275 panic("kernel_memory_allocate: VM is not ready");
278 map_size
= vm_map_round_page(size
,
279 VM_MAP_PAGE_MASK(map
));
280 map_mask
= (vm_map_offset_t
) mask
;
282 vm_alloc_flags
= 0; //VM_MAKE_TAG(tag);
283 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
285 /* Check for zero allocation size (either directly or via overflow) */
288 return KERN_INVALID_ARGUMENT
;
292 * limit the size of a single extent of wired memory
293 * to try and limit the damage to the system if
294 * too many pages get wired down
295 * limit raised to 2GB with 128GB max physical limit,
296 * but scaled by installed memory above this
298 if (!(flags
& (KMA_VAONLY
| KMA_PAGEABLE
)) &&
299 map_size
> MAX(1ULL<<31, sane_size
/64)) {
300 return KERN_RESOURCE_SHORTAGE
;
306 * Guard pages are implemented as ficticious pages. By placing guard pages
307 * on either end of a stack, they can help detect cases where a thread walks
308 * off either end of its stack. They are allocated and set up here and attempts
309 * to access those pages are trapped in vm_fault_page().
311 * The map_size we were passed may include extra space for
312 * guard pages. If those were requested, then back it out of fill_size
313 * since vm_map_find_space() takes just the actual size not including
314 * guard pages. Similarly, fill_start indicates where the actual pages
315 * will begin in the range.
319 fill_size
= map_size
;
321 if (flags
& KMA_GUARD_FIRST
) {
322 vmk_flags
.vmkf_guard_before
= TRUE
;
323 fill_start
+= PAGE_SIZE_64
;
324 fill_size
-= PAGE_SIZE_64
;
325 if (map_size
< fill_start
+ fill_size
) {
326 /* no space for a guard page */
328 return KERN_INVALID_ARGUMENT
;
332 if (flags
& KMA_GUARD_LAST
) {
333 vmk_flags
.vmkf_guard_after
= TRUE
;
334 fill_size
-= PAGE_SIZE_64
;
335 if (map_size
<= fill_start
+ fill_size
) {
336 /* no space for a guard page */
338 return KERN_INVALID_ARGUMENT
;
342 wired_page_count
= (int) (fill_size
/ PAGE_SIZE_64
);
343 assert(wired_page_count
* PAGE_SIZE_64
== fill_size
);
345 #if DEBUG || DEVELOPMENT
346 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_START
, size
, 0, 0, 0);
349 for (i
= 0; i
< guard_page_count
; i
++) {
351 mem
= vm_page_grab_guard();
353 if (mem
!= VM_PAGE_NULL
)
355 if (flags
& KMA_NOPAGEWAIT
) {
356 kr
= KERN_RESOURCE_SHORTAGE
;
359 vm_page_more_fictitious();
361 mem
->vmp_snext
= guard_page_list
;
362 guard_page_list
= mem
;
365 if (!(flags
& (KMA_VAONLY
| KMA_PAGEABLE
))) {
366 for (i
= 0; i
< wired_page_count
; i
++) {
367 uint64_t unavailable
;
370 if (flags
& KMA_LOMEM
)
371 mem
= vm_page_grablo();
373 mem
= vm_page_grab();
375 if (mem
!= VM_PAGE_NULL
)
378 if (flags
& KMA_NOPAGEWAIT
) {
379 kr
= KERN_RESOURCE_SHORTAGE
;
382 if ((flags
& KMA_LOMEM
) && (vm_lopage_needed
== TRUE
)) {
383 kr
= KERN_RESOURCE_SHORTAGE
;
386 unavailable
= (vm_page_wire_count
+ vm_page_free_target
) * PAGE_SIZE
;
388 if (unavailable
> max_mem
|| map_size
> (max_mem
- unavailable
)) {
389 kr
= KERN_RESOURCE_SHORTAGE
;
395 if (KMA_ZERO
& flags
) vm_page_zero_fill(mem
);
396 mem
->vmp_snext
= wired_page_list
;
397 wired_page_list
= mem
;
402 * Allocate a new object (if necessary). We must do this before
403 * locking the map, or risk deadlock with the default pager.
405 if ((flags
& KMA_KOBJECT
) != 0) {
406 object
= kernel_object
;
407 vm_object_reference(object
);
408 } else if ((flags
& KMA_COMPRESSOR
) != 0) {
409 object
= compressor_object
;
410 vm_object_reference(object
);
412 object
= vm_object_allocate(map_size
);
415 if (flags
& KMA_ATOMIC
)
416 vmk_flags
.vmkf_atomic_entry
= TRUE
;
418 kr
= vm_map_find_space(map
, &map_addr
,
420 vm_alloc_flags
, vmk_flags
, tag
, &entry
);
421 if (KERN_SUCCESS
!= kr
) {
422 vm_object_deallocate(object
);
426 if (object
== kernel_object
|| object
== compressor_object
) {
431 VME_OBJECT_SET(entry
, object
);
432 VME_OFFSET_SET(entry
, offset
);
434 if (!(flags
& (KMA_COMPRESSOR
| KMA_PAGEABLE
)))
435 entry
->wired_count
++;
437 if (flags
& KMA_PERMANENT
)
438 entry
->permanent
= TRUE
;
440 if (object
!= kernel_object
&& object
!= compressor_object
)
441 vm_object_reference(object
);
443 vm_object_lock(object
);
449 if (guard_page_list
== NULL
)
450 panic("kernel_memory_allocate: guard_page_list == NULL");
452 mem
= guard_page_list
;
453 guard_page_list
= mem
->vmp_snext
;
454 mem
->vmp_snext
= NULL
;
456 vm_page_insert(mem
, object
, offset
+ pg_offset
);
458 mem
->vmp_busy
= FALSE
;
459 pg_offset
+= PAGE_SIZE_64
;
462 kma_prot
= VM_PROT_READ
| VM_PROT_WRITE
;
465 if (!(flags
& KMA_VAONLY
)) {
466 /* for VAONLY mappings we notify in populate only */
467 kasan_notify_address(map_addr
, size
);
471 if (flags
& (KMA_VAONLY
| KMA_PAGEABLE
)) {
472 pg_offset
= fill_start
+ fill_size
;
474 for (pg_offset
= fill_start
; pg_offset
< fill_start
+ fill_size
; pg_offset
+= PAGE_SIZE_64
) {
475 if (wired_page_list
== NULL
)
476 panic("kernel_memory_allocate: wired_page_list == NULL");
478 mem
= wired_page_list
;
479 wired_page_list
= mem
->vmp_snext
;
480 mem
->vmp_snext
= NULL
;
482 assert(mem
->vmp_wire_count
== 0);
483 assert(mem
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
485 mem
->vmp_q_state
= VM_PAGE_IS_WIRED
;
486 mem
->vmp_wire_count
++;
487 if (__improbable(mem
->vmp_wire_count
== 0)) {
488 panic("kernel_memory_allocate(%p): wire_count overflow",
492 vm_page_insert_wired(mem
, object
, offset
+ pg_offset
, tag
);
494 mem
->vmp_busy
= FALSE
;
495 mem
->vmp_pmapped
= TRUE
;
496 mem
->vmp_wpmapped
= TRUE
;
498 PMAP_ENTER_OPTIONS(kernel_pmap
, map_addr
+ pg_offset
, mem
,
499 kma_prot
, VM_PROT_NONE
, ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
500 PMAP_OPTIONS_NOWAIT
, pe_result
);
502 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
503 vm_object_unlock(object
);
505 PMAP_ENTER(kernel_pmap
, map_addr
+ pg_offset
, mem
,
506 kma_prot
, VM_PROT_NONE
, ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
509 vm_object_lock(object
);
512 assert(pe_result
== KERN_SUCCESS
);
514 if (flags
& KMA_NOENCRYPT
) {
515 bzero(CAST_DOWN(void *, (map_addr
+ pg_offset
)), PAGE_SIZE
);
517 pmap_set_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem
));
520 if (kernel_object
== object
) vm_tag_update_size(tag
, fill_size
);
522 if ((fill_start
+ fill_size
) < map_size
) {
523 if (guard_page_list
== NULL
)
524 panic("kernel_memory_allocate: guard_page_list == NULL");
526 mem
= guard_page_list
;
527 guard_page_list
= mem
->vmp_snext
;
528 mem
->vmp_snext
= NULL
;
530 vm_page_insert(mem
, object
, offset
+ pg_offset
);
532 mem
->vmp_busy
= FALSE
;
534 if (guard_page_list
|| wired_page_list
)
535 panic("kernel_memory_allocate: non empty list\n");
537 if (!(flags
& (KMA_VAONLY
| KMA_PAGEABLE
))) {
538 vm_page_lockspin_queues();
539 vm_page_wire_count
+= wired_page_count
;
540 vm_page_unlock_queues();
543 vm_object_unlock(object
);
546 * now that the pages are wired, we no longer have to fear coalesce
548 if (object
== kernel_object
|| object
== compressor_object
)
549 vm_map_simplify(map
, map_addr
);
551 vm_object_deallocate(object
);
553 #if DEBUG || DEVELOPMENT
554 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_END
, page_grab_count
, 0, 0, 0);
558 * Return the memory, not zeroed.
560 *addrp
= CAST_DOWN(vm_offset_t
, map_addr
);
565 vm_page_free_list(guard_page_list
, FALSE
);
568 vm_page_free_list(wired_page_list
, FALSE
);
570 #if DEBUG || DEVELOPMENT
571 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_END
, page_grab_count
, 0, 0, 0);
578 kernel_memory_populate(
586 vm_object_offset_t offset
, pg_offset
;
587 kern_return_t kr
, pe_result
;
589 vm_page_t page_list
= NULL
;
591 int page_grab_count
= 0;
594 #if DEBUG || DEVELOPMENT
595 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_START
, size
, 0, 0, 0);
598 page_count
= (int) (size
/ PAGE_SIZE_64
);
600 assert((flags
& (KMA_COMPRESSOR
|KMA_KOBJECT
)) != (KMA_COMPRESSOR
|KMA_KOBJECT
));
602 if (flags
& KMA_COMPRESSOR
) {
604 pg_offset
= page_count
* PAGE_SIZE_64
;
608 mem
= vm_page_grab();
610 if (mem
!= VM_PAGE_NULL
)
616 if (KMA_ZERO
& flags
) vm_page_zero_fill(mem
);
617 mem
->vmp_snext
= page_list
;
620 pg_offset
-= PAGE_SIZE_64
;
622 kr
= pmap_enter_options(kernel_pmap
,
623 addr
+ pg_offset
, VM_PAGE_GET_PHYS_PAGE(mem
),
624 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
, 0, TRUE
,
625 PMAP_OPTIONS_INTERNAL
, NULL
);
626 assert(kr
== KERN_SUCCESS
);
631 object
= compressor_object
;
633 vm_object_lock(object
);
637 pg_offset
+= PAGE_SIZE_64
) {
640 page_list
= mem
->vmp_snext
;
641 mem
->vmp_snext
= NULL
;
643 vm_page_insert(mem
, object
, offset
+ pg_offset
);
644 assert(mem
->vmp_busy
);
646 mem
->vmp_busy
= FALSE
;
647 mem
->vmp_pmapped
= TRUE
;
648 mem
->vmp_wpmapped
= TRUE
;
649 mem
->vmp_q_state
= VM_PAGE_USED_BY_COMPRESSOR
;
651 vm_object_unlock(object
);
654 if (map
== compressor_map
) {
655 kasan_notify_address_nopoison(addr
, size
);
657 kasan_notify_address(addr
, size
);
661 #if DEBUG || DEVELOPMENT
662 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_END
, page_grab_count
, 0, 0, 0);
667 for (i
= 0; i
< page_count
; i
++) {
669 if (flags
& KMA_LOMEM
)
670 mem
= vm_page_grablo();
672 mem
= vm_page_grab();
674 if (mem
!= VM_PAGE_NULL
)
677 if (flags
& KMA_NOPAGEWAIT
) {
678 kr
= KERN_RESOURCE_SHORTAGE
;
681 if ((flags
& KMA_LOMEM
) &&
682 (vm_lopage_needed
== TRUE
)) {
683 kr
= KERN_RESOURCE_SHORTAGE
;
689 if (KMA_ZERO
& flags
) vm_page_zero_fill(mem
);
690 mem
->vmp_snext
= page_list
;
693 if (flags
& KMA_KOBJECT
) {
695 object
= kernel_object
;
697 vm_object_lock(object
);
700 * If it's not the kernel object, we need to:
704 * take reference on object;
707 panic("kernel_memory_populate(%p,0x%llx,0x%llx,0x%x): "
709 map
, (uint64_t) addr
, (uint64_t) size
, flags
);
714 pg_offset
+= PAGE_SIZE_64
) {
716 if (page_list
== NULL
)
717 panic("kernel_memory_populate: page_list == NULL");
720 page_list
= mem
->vmp_snext
;
721 mem
->vmp_snext
= NULL
;
723 assert(mem
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
724 mem
->vmp_q_state
= VM_PAGE_IS_WIRED
;
725 mem
->vmp_wire_count
++;
726 if (__improbable(mem
->vmp_wire_count
== 0)) {
727 panic("kernel_memory_populate(%p): wire_count overflow", mem
);
730 vm_page_insert_wired(mem
, object
, offset
+ pg_offset
, tag
);
732 mem
->vmp_busy
= FALSE
;
733 mem
->vmp_pmapped
= TRUE
;
734 mem
->vmp_wpmapped
= TRUE
;
736 PMAP_ENTER_OPTIONS(kernel_pmap
, addr
+ pg_offset
, mem
,
737 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
,
738 ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
739 PMAP_OPTIONS_NOWAIT
, pe_result
);
741 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
743 vm_object_unlock(object
);
745 PMAP_ENTER(kernel_pmap
, addr
+ pg_offset
, mem
,
746 VM_PROT_READ
| VM_PROT_WRITE
, VM_PROT_NONE
,
747 ((flags
& KMA_KSTACK
) ? VM_MEM_STACK
: 0), TRUE
,
750 vm_object_lock(object
);
753 assert(pe_result
== KERN_SUCCESS
);
755 if (flags
& KMA_NOENCRYPT
) {
756 bzero(CAST_DOWN(void *, (addr
+ pg_offset
)), PAGE_SIZE
);
757 pmap_set_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem
));
760 vm_page_lockspin_queues();
761 vm_page_wire_count
+= page_count
;
762 vm_page_unlock_queues();
764 #if DEBUG || DEVELOPMENT
765 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_END
, page_grab_count
, 0, 0, 0);
768 if (kernel_object
== object
) vm_tag_update_size(tag
, size
);
770 vm_object_unlock(object
);
773 if (map
== compressor_map
) {
774 kasan_notify_address_nopoison(addr
, size
);
776 kasan_notify_address(addr
, size
);
783 vm_page_free_list(page_list
, FALSE
);
785 #if DEBUG || DEVELOPMENT
786 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_END
, page_grab_count
, 0, 0, 0);
794 kernel_memory_depopulate(
801 vm_object_offset_t offset
, pg_offset
;
803 vm_page_t local_freeq
= NULL
;
805 assert((flags
& (KMA_COMPRESSOR
|KMA_KOBJECT
)) != (KMA_COMPRESSOR
|KMA_KOBJECT
));
807 if (flags
& KMA_COMPRESSOR
) {
809 object
= compressor_object
;
811 vm_object_lock(object
);
812 } else if (flags
& KMA_KOBJECT
) {
814 object
= kernel_object
;
815 vm_object_lock(object
);
820 * If it's not the kernel object, we need to:
826 panic("kernel_memory_depopulate(%p,0x%llx,0x%llx,0x%x): "
828 map
, (uint64_t) addr
, (uint64_t) size
, flags
);
830 pmap_protect(kernel_map
->pmap
, offset
, offset
+ size
, VM_PROT_NONE
);
834 pg_offset
+= PAGE_SIZE_64
) {
836 mem
= vm_page_lookup(object
, offset
+ pg_offset
);
840 if (mem
->vmp_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
)
841 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(mem
));
843 mem
->vmp_busy
= TRUE
;
845 assert(mem
->vmp_tabled
);
846 vm_page_remove(mem
, TRUE
);
847 assert(mem
->vmp_busy
);
849 assert(mem
->vmp_pageq
.next
== 0 && mem
->vmp_pageq
.prev
== 0);
850 assert((mem
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) ||
851 (mem
->vmp_q_state
== VM_PAGE_NOT_ON_Q
));
853 mem
->vmp_q_state
= VM_PAGE_NOT_ON_Q
;
854 mem
->vmp_snext
= local_freeq
;
857 vm_object_unlock(object
);
860 vm_page_free_list(local_freeq
, TRUE
);
866 * Allocate wired-down memory in the kernel's address map
867 * or a submap. The memory is not zero-filled.
876 return (kmem_alloc(map
, addrp
, size
, vm_tag_bt()));
887 return kmem_alloc_flags(map
, addrp
, size
, tag
, 0);
898 kern_return_t kr
= kernel_memory_allocate(map
, addrp
, size
, 0, flags
, tag
);
899 TRACE_MACHLEAKS(KMEM_ALLOC_CODE
, KMEM_ALLOC_CODE_2
, size
, *addrp
);
906 * Reallocate wired-down memory in the kernel's address map
907 * or a submap. Newly allocated pages are not zeroed.
908 * This can only be used on regions allocated with kmem_alloc.
910 * If successful, the pages in the old region are mapped twice.
911 * The old region is unchanged. Use kmem_free to get rid of it.
918 vm_offset_t
*newaddrp
,
923 vm_object_offset_t offset
;
924 vm_map_offset_t oldmapmin
;
925 vm_map_offset_t oldmapmax
;
926 vm_map_offset_t newmapaddr
;
927 vm_map_size_t oldmapsize
;
928 vm_map_size_t newmapsize
;
929 vm_map_entry_t oldentry
;
930 vm_map_entry_t newentry
;
934 oldmapmin
= vm_map_trunc_page(oldaddr
,
935 VM_MAP_PAGE_MASK(map
));
936 oldmapmax
= vm_map_round_page(oldaddr
+ oldsize
,
937 VM_MAP_PAGE_MASK(map
));
938 oldmapsize
= oldmapmax
- oldmapmin
;
939 newmapsize
= vm_map_round_page(newsize
,
940 VM_MAP_PAGE_MASK(map
));
941 if (newmapsize
< newsize
) {
944 return KERN_INVALID_ARGUMENT
;
948 * Find the VM object backing the old region.
953 if (!vm_map_lookup_entry(map
, oldmapmin
, &oldentry
))
954 panic("kmem_realloc");
955 object
= VME_OBJECT(oldentry
);
958 * Increase the size of the object and
959 * fill in the new region.
962 vm_object_reference(object
);
963 /* by grabbing the object lock before unlocking the map */
964 /* we guarantee that we will panic if more than one */
965 /* attempt is made to realloc a kmem_alloc'd area */
966 vm_object_lock(object
);
968 if (object
->vo_size
!= oldmapsize
)
969 panic("kmem_realloc");
970 object
->vo_size
= newmapsize
;
971 vm_object_unlock(object
);
973 /* allocate the new pages while expanded portion of the */
974 /* object is still not mapped */
975 kmem_alloc_pages(object
, vm_object_round_page(oldmapsize
),
976 vm_object_round_page(newmapsize
-oldmapsize
));
979 * Find space for the new region.
982 kr
= vm_map_find_space(map
, &newmapaddr
, newmapsize
,
983 (vm_map_offset_t
) 0, 0,
984 VM_MAP_KERNEL_FLAGS_NONE
,
987 if (kr
!= KERN_SUCCESS
) {
988 vm_object_lock(object
);
989 for(offset
= oldmapsize
;
990 offset
< newmapsize
; offset
+= PAGE_SIZE
) {
991 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
995 object
->vo_size
= oldmapsize
;
996 vm_object_unlock(object
);
997 vm_object_deallocate(object
);
1000 VME_OBJECT_SET(newentry
, object
);
1001 VME_OFFSET_SET(newentry
, 0);
1002 assert(newentry
->wired_count
== 0);
1005 /* add an extra reference in case we have someone doing an */
1006 /* unexpected deallocate */
1007 vm_object_reference(object
);
1010 kr
= vm_map_wire_kernel(map
, newmapaddr
, newmapaddr
+ newmapsize
,
1011 VM_PROT_DEFAULT
, tag
, FALSE
);
1012 if (KERN_SUCCESS
!= kr
) {
1013 vm_map_remove(map
, newmapaddr
, newmapaddr
+ newmapsize
, VM_MAP_REMOVE_NO_FLAGS
);
1014 vm_object_lock(object
);
1015 for(offset
= oldsize
; offset
< newmapsize
; offset
+= PAGE_SIZE
) {
1016 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
1020 object
->vo_size
= oldmapsize
;
1021 vm_object_unlock(object
);
1022 vm_object_deallocate(object
);
1025 vm_object_deallocate(object
);
1027 if (kernel_object
== object
) vm_tag_update_size(tag
, newmapsize
);
1029 *newaddrp
= CAST_DOWN(vm_offset_t
, newmapaddr
);
1030 return KERN_SUCCESS
;
1034 * kmem_alloc_kobject:
1036 * Allocate wired-down memory in the kernel's address map
1037 * or a submap. The memory is not zero-filled.
1039 * The memory is allocated in the kernel_object.
1040 * It may not be copied with vm_map_copy, and
1041 * it may not be reallocated with kmem_realloc.
1045 kmem_alloc_kobject_external(
1050 return (kmem_alloc_kobject(map
, addrp
, size
, vm_tag_bt()));
1060 return kernel_memory_allocate(map
, addrp
, size
, 0, KMA_KOBJECT
, tag
);
1064 * kmem_alloc_aligned:
1066 * Like kmem_alloc_kobject, except that the memory is aligned.
1067 * The size should be a power-of-2.
1077 if ((size
& (size
- 1)) != 0)
1078 panic("kmem_alloc_aligned: size not aligned");
1079 return kernel_memory_allocate(map
, addrp
, size
, size
- 1, KMA_KOBJECT
, tag
);
1083 * kmem_alloc_pageable:
1085 * Allocate pageable memory in the kernel's address map.
1089 kmem_alloc_pageable_external(
1094 return (kmem_alloc_pageable(map
, addrp
, size
, vm_tag_bt()));
1098 kmem_alloc_pageable(
1104 vm_map_offset_t map_addr
;
1105 vm_map_size_t map_size
;
1109 map_addr
= (vm_map_min(map
)) + PAGE_SIZE
;
1111 map_addr
= vm_map_min(map
);
1113 map_size
= vm_map_round_page(size
,
1114 VM_MAP_PAGE_MASK(map
));
1115 if (map_size
< size
) {
1118 return KERN_INVALID_ARGUMENT
;
1121 kr
= vm_map_enter(map
, &map_addr
, map_size
,
1122 (vm_map_offset_t
) 0,
1124 VM_MAP_KERNEL_FLAGS_NONE
,
1126 VM_OBJECT_NULL
, (vm_object_offset_t
) 0, FALSE
,
1127 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
1129 if (kr
!= KERN_SUCCESS
)
1133 kasan_notify_address(map_addr
, map_size
);
1135 *addrp
= CAST_DOWN(vm_offset_t
, map_addr
);
1136 return KERN_SUCCESS
;
1142 * Release a region of kernel virtual memory allocated
1143 * with kmem_alloc, kmem_alloc_kobject, or kmem_alloc_pageable,
1144 * and return the physical pages associated with that region.
1155 assert(addr
>= VM_MIN_KERNEL_AND_KEXT_ADDRESS
);
1157 TRACE_MACHLEAKS(KMEM_FREE_CODE
, KMEM_FREE_CODE_2
, size
, addr
);
1161 printf("kmem_free called with size==0 for map: %p with addr: 0x%llx\n",map
,(uint64_t)addr
);
1166 kr
= vm_map_remove(map
,
1167 vm_map_trunc_page(addr
,
1168 VM_MAP_PAGE_MASK(map
)),
1169 vm_map_round_page(addr
+ size
,
1170 VM_MAP_PAGE_MASK(map
)),
1171 VM_MAP_REMOVE_KUNWIRE
);
1172 if (kr
!= KERN_SUCCESS
)
1177 * Allocate new pages in an object.
1183 vm_object_offset_t offset
,
1184 vm_object_size_t size
)
1186 vm_object_size_t alloc_size
;
1188 alloc_size
= vm_object_round_page(size
);
1189 vm_object_lock(object
);
1190 while (alloc_size
) {
1197 while (VM_PAGE_NULL
==
1198 (mem
= vm_page_alloc(object
, offset
))) {
1199 vm_object_unlock(object
);
1201 vm_object_lock(object
);
1203 mem
->vmp_busy
= FALSE
;
1205 alloc_size
-= PAGE_SIZE
;
1206 offset
+= PAGE_SIZE
;
1208 vm_object_unlock(object
);
1209 return KERN_SUCCESS
;
1215 * Allocates a map to manage a subrange
1216 * of the kernel virtual address space.
1218 * Arguments are as follows:
1220 * parent Map to take range from
1221 * addr Address of start of range (IN/OUT)
1222 * size Size of range to find
1223 * pageable Can region be paged
1224 * anywhere Can region be located anywhere in map
1225 * new_map Pointer to new submap
1234 vm_map_kernel_flags_t vmk_flags
,
1239 vm_map_offset_t map_addr
;
1240 vm_map_size_t map_size
;
1243 map_size
= vm_map_round_page(size
,
1244 VM_MAP_PAGE_MASK(parent
));
1245 if (map_size
< size
) {
1248 return KERN_INVALID_ARGUMENT
;
1252 * Need reference on submap object because it is internal
1253 * to the vm_system. vm_object_enter will never be called
1254 * on it (usual source of reference for vm_map_enter).
1256 vm_object_reference(vm_submap_object
);
1258 map_addr
= ((flags
& VM_FLAGS_ANYWHERE
)
1259 ? vm_map_min(parent
)
1260 : vm_map_trunc_page(*addr
,
1261 VM_MAP_PAGE_MASK(parent
)));
1263 kr
= vm_map_enter(parent
, &map_addr
, map_size
,
1264 (vm_map_offset_t
) 0, flags
, vmk_flags
, tag
,
1265 vm_submap_object
, (vm_object_offset_t
) 0, FALSE
,
1266 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
1267 if (kr
!= KERN_SUCCESS
) {
1268 vm_object_deallocate(vm_submap_object
);
1272 pmap_reference(vm_map_pmap(parent
));
1273 map
= vm_map_create(vm_map_pmap(parent
), map_addr
, map_addr
+ map_size
, pageable
);
1274 if (map
== VM_MAP_NULL
)
1275 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
1276 /* inherit the parent map's page size */
1277 vm_map_set_page_shift(map
, VM_MAP_PAGE_SHIFT(parent
));
1279 kr
= vm_map_submap(parent
, map_addr
, map_addr
+ map_size
, map
, map_addr
, FALSE
);
1280 if (kr
!= KERN_SUCCESS
) {
1282 * See comment preceding vm_map_submap().
1284 vm_map_remove(parent
, map_addr
, map_addr
+ map_size
,
1285 VM_MAP_REMOVE_NO_FLAGS
);
1286 vm_map_deallocate(map
); /* also removes ref to pmap */
1287 vm_object_deallocate(vm_submap_object
);
1290 *addr
= CAST_DOWN(vm_offset_t
, map_addr
);
1292 return (KERN_SUCCESS
);
1298 * Initialize the kernel's virtual memory map, taking
1299 * into account all memory allocated up to this time.
1306 vm_map_offset_t map_start
;
1307 vm_map_offset_t map_end
;
1308 vm_map_kernel_flags_t vmk_flags
;
1310 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1311 vmk_flags
.vmkf_permanent
= TRUE
;
1312 vmk_flags
.vmkf_no_pmap_check
= TRUE
;
1314 map_start
= vm_map_trunc_page(start
,
1315 VM_MAP_PAGE_MASK(kernel_map
));
1316 map_end
= vm_map_round_page(end
,
1317 VM_MAP_PAGE_MASK(kernel_map
));
1319 #if defined(__arm__) || defined(__arm64__)
1320 kernel_map
= vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS
,
1321 VM_MAX_KERNEL_ADDRESS
, FALSE
);
1323 * Reserve virtual memory allocated up to this time.
1326 unsigned int region_select
= 0;
1327 vm_map_offset_t region_start
;
1328 vm_map_size_t region_size
;
1329 vm_map_offset_t map_addr
;
1332 while (pmap_virtual_region(region_select
, ®ion_start
, ®ion_size
)) {
1334 map_addr
= region_start
;
1335 kr
= vm_map_enter(kernel_map
, &map_addr
,
1336 vm_map_round_page(region_size
,
1337 VM_MAP_PAGE_MASK(kernel_map
)),
1338 (vm_map_offset_t
) 0,
1341 VM_KERN_MEMORY_NONE
,
1343 (vm_object_offset_t
) 0, FALSE
, VM_PROT_NONE
, VM_PROT_NONE
,
1344 VM_INHERIT_DEFAULT
);
1346 if (kr
!= KERN_SUCCESS
) {
1347 panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n",
1348 (uint64_t) start
, (uint64_t) end
, (uint64_t) region_start
,
1349 (uint64_t) region_size
, kr
);
1356 kernel_map
= vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS
,
1359 * Reserve virtual memory allocated up to this time.
1361 if (start
!= VM_MIN_KERNEL_AND_KEXT_ADDRESS
) {
1362 vm_map_offset_t map_addr
;
1365 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1366 vmk_flags
.vmkf_no_pmap_check
= TRUE
;
1368 map_addr
= VM_MIN_KERNEL_AND_KEXT_ADDRESS
;
1369 kr
= vm_map_enter(kernel_map
,
1371 (vm_map_size_t
)(map_start
- VM_MIN_KERNEL_AND_KEXT_ADDRESS
),
1372 (vm_map_offset_t
) 0,
1375 VM_KERN_MEMORY_NONE
,
1377 (vm_object_offset_t
) 0, FALSE
,
1378 VM_PROT_NONE
, VM_PROT_NONE
,
1379 VM_INHERIT_DEFAULT
);
1381 if (kr
!= KERN_SUCCESS
) {
1382 panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n",
1383 (uint64_t) start
, (uint64_t) end
,
1384 (uint64_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS
,
1385 (uint64_t) (map_start
- VM_MIN_KERNEL_AND_KEXT_ADDRESS
),
1392 * Set the default global user wire limit which limits the amount of
1393 * memory that can be locked via mlock(). We set this to the total
1394 * amount of memory that are potentially usable by a user app (max_mem)
1395 * minus a certain amount. This can be overridden via a sysctl.
1397 vm_global_no_user_wire_amount
= MIN(max_mem
*20/100,
1398 VM_NOT_USER_WIREABLE
);
1399 vm_global_user_wire_limit
= max_mem
- vm_global_no_user_wire_amount
;
1401 /* the default per user limit is the same as the global limit */
1402 vm_user_wire_limit
= vm_global_user_wire_limit
;
1407 * Routine: copyinmap
1409 * Like copyin, except that fromaddr is an address
1410 * in the specified VM map. This implementation
1411 * is incomplete; it handles the current user map
1412 * and the kernel map/submaps.
1417 vm_map_offset_t fromaddr
,
1421 kern_return_t kr
= KERN_SUCCESS
;
1424 if (vm_map_pmap(map
) == pmap_kernel())
1426 /* assume a correct copy */
1427 memcpy(todata
, CAST_DOWN(void *, fromaddr
), length
);
1429 else if (current_map() == map
)
1431 if (copyin(fromaddr
, todata
, length
) != 0)
1432 kr
= KERN_INVALID_ADDRESS
;
1436 vm_map_reference(map
);
1437 oldmap
= vm_map_switch(map
);
1438 if (copyin(fromaddr
, todata
, length
) != 0)
1439 kr
= KERN_INVALID_ADDRESS
;
1440 vm_map_switch(oldmap
);
1441 vm_map_deallocate(map
);
1447 * Routine: copyoutmap
1449 * Like copyout, except that toaddr is an address
1450 * in the specified VM map. This implementation
1451 * is incomplete; it handles the current user map
1452 * and the kernel map/submaps.
1458 vm_map_address_t toaddr
,
1461 if (vm_map_pmap(map
) == pmap_kernel()) {
1462 /* assume a correct copy */
1463 memcpy(CAST_DOWN(void *, toaddr
), fromdata
, length
);
1464 return KERN_SUCCESS
;
1467 if (current_map() != map
)
1468 return KERN_NOT_SUPPORTED
;
1470 if (copyout(fromdata
, toaddr
, length
) != 0)
1471 return KERN_INVALID_ADDRESS
;
1473 return KERN_SUCCESS
;
1478 * The following two functions are to be used when exposing kernel
1479 * addresses to userspace via any of the various debug or info
1480 * facilities that exist. These are basically the same as VM_KERNEL_ADDRPERM()
1481 * and VM_KERNEL_UNSLIDE_OR_PERM() except they use a different random seed and
1482 * are exported to KEXTs.
1484 * NOTE: USE THE MACRO VERSIONS OF THESE FUNCTIONS (in vm_param.h) FROM WITHIN THE KERNEL
1488 vm_kernel_addrhash_internal(
1490 vm_offset_t
*hash_addr
,
1500 if (VM_KERNEL_IS_SLID(addr
)) {
1501 *hash_addr
= VM_KERNEL_UNSLIDE(addr
);
1505 vm_offset_t sha_digest
[SHA256_DIGEST_LENGTH
/sizeof(vm_offset_t
)];
1508 SHA256_Init(&sha_ctx
);
1509 SHA256_Update(&sha_ctx
, &salt
, sizeof(salt
));
1510 SHA256_Update(&sha_ctx
, &addr
, sizeof(addr
));
1511 SHA256_Final(sha_digest
, &sha_ctx
);
1513 *hash_addr
= sha_digest
[0];
1517 vm_kernel_addrhash_external(
1519 vm_offset_t
*hash_addr
)
1521 return vm_kernel_addrhash_internal(addr
, hash_addr
, vm_kernel_addrhash_salt_ext
);
1525 vm_kernel_addrhash(vm_offset_t addr
)
1527 vm_offset_t hash_addr
;
1528 vm_kernel_addrhash_internal(addr
, &hash_addr
, vm_kernel_addrhash_salt
);
1535 vm_offset_t
*hide_addr
)
1537 *hide_addr
= VM_KERNEL_ADDRHIDE(addr
);
1541 * vm_kernel_addrperm_external:
1542 * vm_kernel_unslide_or_perm_external:
1544 * Use these macros when exposing an address to userspace that could come from
1545 * either kernel text/data *or* the heap.
1548 vm_kernel_addrperm_external(
1550 vm_offset_t
*perm_addr
)
1552 if (VM_KERNEL_IS_SLID(addr
)) {
1553 *perm_addr
= VM_KERNEL_UNSLIDE(addr
);
1554 } else if (VM_KERNEL_ADDRESS(addr
)) {
1555 *perm_addr
= addr
+ vm_kernel_addrperm_ext
;
1562 vm_kernel_unslide_or_perm_external(
1564 vm_offset_t
*up_addr
)
1566 vm_kernel_addrperm_external(addr
, up_addr
);