2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
62 * Kernel memory management.
65 #include <sys/cdefs.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/domainset.h>
72 #include <sys/eventhandler.h>
73 #include <sys/kernel.h>
75 #include <sys/malloc.h>
78 #include <sys/rwlock.h>
80 #include <sys/sysctl.h>
82 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_domainset.h>
87 #include <vm/vm_kern.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_pagequeue.h>
94 #include <vm/vm_phys.h>
95 #include <vm/vm_radix.h>
96 #include <vm/vm_extern.h>
99 struct vm_map kernel_map_store;
100 struct vm_map exec_map_store;
101 struct vm_map pipe_map_store;
103 const void *zero_region;
104 CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
106 /* NB: Used by kernel debuggers. */
107 const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
109 u_int exec_map_entry_size;
110 u_int exec_map_entries;
112 SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
113 SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
115 SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
117 &vm_max_kernel_address, 0,
119 SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
121 "Max kernel address");
123 #if VM_NRESERVLEVEL > 0
124 #define KVA_QUANTUM_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT)
126 /* On non-superpage architectures we want large import sizes. */
127 #define KVA_QUANTUM_SHIFT (8 + PAGE_SHIFT)
129 #define KVA_QUANTUM (1ul << KVA_QUANTUM_SHIFT)
130 #define KVA_NUMA_IMPORT_QUANTUM (KVA_QUANTUM * 128)
132 extern void uma_startup2(void);
137 * Allocate a virtual address range with no underlying object and
138 * no initial mapping to physical memory. Any mapping from this
139 * range to physical memory must be explicitly created prior to
140 * its use, typically with pmap_qenter(). Any attempt to create
141 * a mapping on demand through vm_fault() will result in a panic.
144 kva_alloc(vm_size_t size)
149 size = round_page(size);
150 if (vmem_xalloc(kernel_arena, size, 0, 0, 0, VMEM_ADDR_MIN,
151 VMEM_ADDR_MAX, M_BESTFIT | M_NOWAIT, &addr))
161 * Allocate a virtual address range as in kva_alloc where the base
162 * address is aligned to align.
165 kva_alloc_aligned(vm_size_t size, vm_size_t align)
170 size = round_page(size);
171 if (vmem_xalloc(kernel_arena, size, align, 0, 0, VMEM_ADDR_MIN,
172 VMEM_ADDR_MAX, M_BESTFIT | M_NOWAIT, &addr))
182 * Release a region of kernel virtual memory allocated
183 * with kva_alloc, and return the physical pages
184 * associated with that region.
186 * This routine may not block on kernel maps.
189 kva_free(vm_offset_t addr, vm_size_t size)
192 size = round_page(size);
193 vmem_xfree(kernel_arena, addr, size);
197 * Update sanitizer shadow state to reflect a new allocation. Force inlining to
198 * help make KMSAN origin tracking more precise.
200 static __always_inline void
201 kmem_alloc_san(vm_offset_t addr, vm_size_t size, vm_size_t asize, int flags)
203 if ((flags & M_ZERO) == 0) {
204 kmsan_mark((void *)addr, asize, KMSAN_STATE_UNINIT);
205 kmsan_orig((void *)addr, asize, KMSAN_TYPE_KMEM,
208 kmsan_mark((void *)addr, asize, KMSAN_STATE_INITED);
210 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE);
214 kmem_alloc_contig_pages(vm_object_t object, vm_pindex_t pindex, int domain,
215 int pflags, u_long npages, vm_paddr_t low, vm_paddr_t high,
216 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
222 VM_OBJECT_ASSERT_WLOCKED(object);
224 wait = (pflags & VM_ALLOC_WAITOK) != 0;
225 reclaim = (pflags & VM_ALLOC_NORECLAIM) == 0;
226 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
227 pflags |= VM_ALLOC_NOWAIT;
228 for (tries = wait ? 3 : 1;; tries--) {
229 m = vm_page_alloc_contig_domain(object, pindex, domain, pflags,
230 npages, low, high, alignment, boundary, memattr);
231 if (m != NULL || tries == 0 || !reclaim)
234 VM_OBJECT_WUNLOCK(object);
235 if (!vm_page_reclaim_contig_domain(domain, pflags, npages,
236 low, high, alignment, boundary) && wait)
237 vm_wait_domain(domain);
238 VM_OBJECT_WLOCK(object);
244 * Allocates a region from the kernel address map and physical pages
245 * within the specified address range to the kernel object. Creates a
246 * wired mapping from this region to these pages, and returns the
247 * region's starting virtual address. The allocated pages are not
248 * necessarily physically contiguous. If M_ZERO is specified through the
249 * given flags, then the pages are zeroed before they are mapped.
252 kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
253 vm_paddr_t high, vm_memattr_t memattr)
257 vm_offset_t addr, i, offset;
263 object = kernel_object;
264 asize = round_page(size);
265 vmem = vm_dom[domain].vmd_kernel_arena;
266 if (vmem_alloc(vmem, asize, M_BESTFIT | flags, &addr))
268 offset = addr - VM_MIN_KERNEL_ADDRESS;
269 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
270 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
271 VM_OBJECT_WLOCK(object);
272 for (i = 0; i < asize; i += PAGE_SIZE) {
273 m = kmem_alloc_contig_pages(object, atop(offset + i),
274 domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
276 VM_OBJECT_WUNLOCK(object);
277 kmem_unback(object, addr, i);
278 vmem_free(vmem, addr, asize);
281 KASSERT(vm_page_domain(m) == domain,
282 ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
283 vm_page_domain(m), domain));
284 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
287 pmap_enter(kernel_pmap, addr + i, m, prot,
288 prot | PMAP_ENTER_WIRED, 0);
290 VM_OBJECT_WUNLOCK(object);
291 kmem_alloc_san(addr, size, asize, flags);
292 return ((void *)addr);
296 kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
297 vm_memattr_t memattr)
300 return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low,
305 kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size, int flags,
306 vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr)
308 struct vm_domainset_iter di;
312 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
314 addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
318 } while (vm_domainset_iter_policy(&di, &domain) == 0);
324 * Allocates a region from the kernel address map and physically
325 * contiguous pages within the specified address range to the kernel
326 * object. Creates a wired mapping from this region to these pages, and
327 * returns the region's starting virtual address. If M_ZERO is specified
328 * through the given flags, then the pages are zeroed before they are
332 kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
333 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
334 vm_memattr_t memattr)
338 vm_offset_t addr, offset, tmp;
344 object = kernel_object;
345 asize = round_page(size);
346 vmem = vm_dom[domain].vmd_kernel_arena;
347 if (vmem_alloc(vmem, asize, flags | M_BESTFIT, &addr))
349 offset = addr - VM_MIN_KERNEL_ADDRESS;
350 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
351 npages = atop(asize);
352 VM_OBJECT_WLOCK(object);
353 m = kmem_alloc_contig_pages(object, atop(offset), domain,
354 pflags, npages, low, high, alignment, boundary, memattr);
356 VM_OBJECT_WUNLOCK(object);
357 vmem_free(vmem, addr, asize);
360 KASSERT(vm_page_domain(m) == domain,
361 ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
362 vm_page_domain(m), domain));
365 for (; m < end_m; m++) {
366 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
369 pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
370 VM_PROT_RW | PMAP_ENTER_WIRED, 0);
373 VM_OBJECT_WUNLOCK(object);
374 kmem_alloc_san(addr, size, asize, flags);
375 return ((void *)addr);
379 kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
380 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
383 return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low,
384 high, alignment, boundary, memattr));
388 kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags,
389 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
390 vm_memattr_t memattr)
392 struct vm_domainset_iter di;
396 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
398 addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
399 alignment, boundary, memattr);
402 } while (vm_domainset_iter_policy(&di, &domain) == 0);
410 * Initializes a map to manage a subrange
411 * of the kernel virtual address space.
413 * Arguments are as follows:
415 * parent Map to take range from
416 * min, max Returned endpoints of map
417 * size Size of range to find
418 * superpage_align Request that min is superpage aligned
421 kmem_subinit(vm_map_t map, vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
422 vm_size_t size, bool superpage_align)
426 size = round_page(size);
428 *min = vm_map_min(parent);
429 ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
430 VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
432 if (ret != KERN_SUCCESS)
433 panic("kmem_subinit: bad status return of %d", ret);
435 vm_map_init(map, vm_map_pmap(parent), *min, *max);
436 if (vm_map_submap(parent, *min, *max, map) != KERN_SUCCESS)
437 panic("kmem_subinit: unable to change range to submap");
441 * kmem_malloc_domain:
443 * Allocate wired-down pages in the kernel's address space.
446 kmem_malloc_domain(int domain, vm_size_t size, int flags)
453 if (__predict_true((flags & M_EXEC) == 0))
454 arena = vm_dom[domain].vmd_kernel_arena;
456 arena = vm_dom[domain].vmd_kernel_rwx_arena;
457 asize = round_page(size);
458 if (vmem_alloc(arena, asize, flags | M_BESTFIT, &addr))
461 rv = kmem_back_domain(domain, kernel_object, addr, asize, flags);
462 if (rv != KERN_SUCCESS) {
463 vmem_free(arena, addr, asize);
466 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE);
467 return ((void *)addr);
471 kmem_malloc(vm_size_t size, int flags)
476 p = kmem_malloc_domainset(DOMAINSET_RR(), size, flags);
482 kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags)
484 struct vm_domainset_iter di;
488 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
490 addr = kmem_malloc_domain(domain, size, flags);
493 } while (vm_domainset_iter_policy(&di, &domain) == 0);
501 * Allocate physical pages from the specified domain for the specified
502 * virtual address range.
505 kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
506 vm_size_t size, int flags)
508 vm_offset_t offset, i;
513 KASSERT(object == kernel_object,
514 ("kmem_back_domain: only supports kernel object."));
516 offset = addr - VM_MIN_KERNEL_ADDRESS;
517 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
518 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
519 if (flags & M_WAITOK)
520 pflags |= VM_ALLOC_WAITFAIL;
521 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
524 VM_OBJECT_WLOCK(object);
526 mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
527 for (; i < size; i += PAGE_SIZE, mpred = m) {
528 m = vm_page_alloc_domain_after(object, atop(offset + i),
529 domain, pflags, mpred);
532 * Ran out of space, free everything up and return. Don't need
533 * to lock page queues here as we know that the pages we got
534 * aren't on any queues.
537 if ((flags & M_NOWAIT) == 0)
539 VM_OBJECT_WUNLOCK(object);
540 kmem_unback(object, addr, i);
541 return (KERN_NO_SPACE);
543 KASSERT(vm_page_domain(m) == domain,
544 ("kmem_back_domain: Domain mismatch %d != %d",
545 vm_page_domain(m), domain));
546 if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
548 KASSERT((m->oflags & VPO_UNMANAGED) != 0,
549 ("kmem_malloc: page %p is managed", m));
551 pmap_enter(kernel_pmap, addr + i, m, prot,
552 prot | PMAP_ENTER_WIRED, 0);
553 if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
554 m->oflags |= VPO_KMEM_EXEC;
556 VM_OBJECT_WUNLOCK(object);
557 kmem_alloc_san(addr, size, size, flags);
558 return (KERN_SUCCESS);
564 * Allocate physical pages for the specified virtual address range.
567 kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
569 vm_offset_t end, next, start;
572 KASSERT(object == kernel_object,
573 ("kmem_back: only supports kernel object."));
575 for (start = addr, end = addr + size; addr < end; addr = next) {
577 * We must ensure that pages backing a given large virtual page
578 * all come from the same physical domain.
580 if (vm_ndomains > 1) {
581 domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
582 while (VM_DOMAIN_EMPTY(domain))
584 next = roundup2(addr + 1, KVA_QUANTUM);
585 if (next > end || next < start)
591 rv = kmem_back_domain(domain, object, addr, next - addr, flags);
592 if (rv != KERN_SUCCESS) {
593 kmem_unback(object, start, addr - start);
603 * Unmap and free the physical pages underlying the specified virtual
606 * A physical page must exist within the specified object at each index
607 * that is being unmapped.
610 _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
614 vm_offset_t end, offset;
617 KASSERT(object == kernel_object,
618 ("kmem_unback: only supports kernel object."));
622 pmap_remove(kernel_pmap, addr, addr + size);
623 offset = addr - VM_MIN_KERNEL_ADDRESS;
625 VM_OBJECT_WLOCK(object);
626 m = vm_page_lookup(object, atop(offset));
627 domain = vm_page_domain(m);
628 if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
629 arena = vm_dom[domain].vmd_kernel_arena;
631 arena = vm_dom[domain].vmd_kernel_rwx_arena;
632 for (; offset < end; offset += PAGE_SIZE, m = next) {
633 next = vm_page_next(m);
634 vm_page_xbusy_claim(m);
635 vm_page_unwire_noq(m);
638 VM_OBJECT_WUNLOCK(object);
644 kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
647 (void)_kmem_unback(object, addr, size);
653 * Free memory allocated with kmem_malloc. The size must match the
654 * original allocation.
657 kmem_free(void *addr, vm_size_t size)
661 size = round_page(size);
662 kasan_mark(addr, size, size, 0);
663 arena = _kmem_unback(kernel_object, (uintptr_t)addr, size);
665 vmem_free(arena, (uintptr_t)addr, size);
671 * Allocates pageable memory from a sub-map of the kernel. If the submap
672 * has no room, the caller sleeps waiting for more memory in the submap.
674 * This routine may block.
677 kmap_alloc_wait(vm_map_t map, vm_size_t size)
681 size = round_page(size);
682 if (!swap_reserve(size))
687 * To make this work for more than one map, use the map's lock
688 * to lock out sleepers/wakers.
691 addr = vm_map_findspace(map, vm_map_min(map), size);
692 if (addr + size <= vm_map_max(map))
694 /* no space now; see if we can ever get space */
695 if (vm_map_max(map) - vm_map_min(map) < size) {
700 map->needs_wakeup = TRUE;
701 vm_map_unlock_and_wait(map, 0);
703 vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW,
712 * Returns memory to a submap of the kernel, and wakes up any processes
713 * waiting for memory in that map.
716 kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
720 (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
721 if (map->needs_wakeup) {
722 map->needs_wakeup = FALSE;
729 kmem_init_zero_region(void)
735 * Map a single physical page of zeros to a larger virtual range.
736 * This requires less looping in places that want large amounts of
737 * zeros, while not using much more physical resources.
739 addr = kva_alloc(ZERO_REGION_SIZE);
740 m = vm_page_alloc_noobj(VM_ALLOC_WIRED | VM_ALLOC_ZERO);
741 for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
742 pmap_qenter(addr + i, &m, 1);
743 pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
745 zero_region = (const void *)addr;
749 * Import KVA from the kernel map into the kernel arena.
752 kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
758 KASSERT((size % KVA_QUANTUM) == 0,
759 ("kva_import: Size %jd is not a multiple of %d",
760 (intmax_t)size, (int)KVA_QUANTUM));
761 addr = vm_map_min(kernel_map);
762 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
763 VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
764 if (result != KERN_SUCCESS) {
776 * Import KVA from a parent arena into a per-domain arena. Imports must be
777 * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
780 kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
783 KASSERT((size % KVA_QUANTUM) == 0,
784 ("kva_import_domain: Size %jd is not a multiple of %d",
785 (intmax_t)size, (int)KVA_QUANTUM));
786 return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
787 VMEM_ADDR_MAX, flags, addrp));
793 * Create the kernel map; insert a mapping covering kernel text,
794 * data, bss, and all space allocated thus far (`boostrap' data). The
795 * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
796 * `start' as allocated, and the range between `start' and `end' as free.
797 * Create the kernel vmem arena and its per-domain children.
800 kmem_init(vm_offset_t start, vm_offset_t end)
805 vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
806 kernel_map->system_map = 1;
807 vm_map_lock(kernel_map);
808 /* N.B.: cannot use kgdb to debug, starting with this assignment ... */
809 (void)vm_map_insert(kernel_map, NULL, 0,
813 VM_MIN_KERNEL_ADDRESS,
815 start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
816 /* ... and ending with the completion of the above `insert' */
820 * Mark KVA used for the page array as allocated. Other platforms
821 * that handle vm_page_array allocation can simply adjust virtual_avail
824 (void)vm_map_insert(kernel_map, NULL, 0, (vm_offset_t)vm_page_array,
825 (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size *
826 sizeof(struct vm_page)),
827 VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
829 vm_map_unlock(kernel_map);
832 * Use a large import quantum on NUMA systems. This helps minimize
833 * interleaving of superpages, reducing internal fragmentation within
834 * the per-domain arenas.
836 if (vm_ndomains > 1 && PMAP_HAS_DMAP)
837 quantum = KVA_NUMA_IMPORT_QUANTUM;
839 quantum = KVA_QUANTUM;
842 * Initialize the kernel_arena. This can grow on demand.
844 vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
845 vmem_set_import(kernel_arena, kva_import, NULL, NULL, quantum);
847 for (domain = 0; domain < vm_ndomains; domain++) {
849 * Initialize the per-domain arenas. These are used to color
850 * the KVA space in a way that ensures that virtual large pages
851 * are backed by memory from the same physical domain,
852 * maximizing the potential for superpage promotion.
854 vm_dom[domain].vmd_kernel_arena = vmem_create(
855 "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
856 vmem_set_import(vm_dom[domain].vmd_kernel_arena,
857 kva_import_domain, NULL, kernel_arena, quantum);
860 * In architectures with superpages, maintain separate arenas
861 * for allocations with permissions that differ from the
862 * "standard" read/write permissions used for kernel memory,
863 * so as not to inhibit superpage promotion.
865 * Use the base import quantum since this arena is rarely used.
867 #if VM_NRESERVLEVEL > 0
868 vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
869 "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
870 vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
871 kva_import_domain, (vmem_release_t *)vmem_xfree,
872 kernel_arena, KVA_QUANTUM);
874 vm_dom[domain].vmd_kernel_rwx_arena =
875 vm_dom[domain].vmd_kernel_arena;
880 * This must be the very first call so that the virtual address
881 * space used for early allocations is properly marked used in
888 * kmem_bootstrap_free:
890 * Free pages backing preloaded data (e.g., kernel modules) to the
891 * system. Currently only supported on platforms that create a
892 * vm_phys segment for preloaded data.
895 kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
897 #if defined(__i386__) || defined(__amd64__)
898 struct vm_domain *vmd;
903 end = trunc_page(start + size);
904 start = round_page(start);
908 * Preloaded files do not have execute permissions by default on amd64.
909 * Restore the default permissions to ensure that the direct map alias
912 pmap_change_prot(start, end - start, VM_PROT_RW);
914 for (va = start; va < end; va += PAGE_SIZE) {
915 pa = pmap_kextract(va);
916 m = PHYS_TO_VM_PAGE(pa);
918 vmd = vm_pagequeue_domain(m);
919 vm_domain_free_lock(vmd);
920 vm_phys_free_pages(m, 0);
921 vm_domain_free_unlock(vmd);
923 vm_domain_freecnt_inc(vmd, 1);
924 vm_cnt.v_page_count++;
926 pmap_remove(kernel_pmap, start, end);
927 (void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
931 #ifdef PMAP_WANT_ACTIVE_CPUS_NAIVE
933 pmap_active_cpus(pmap_t pmap, cpuset_t *res)
942 td = cpuid_to_pcpu[c]->pc_curthread;
946 vm = vmspace_acquire_ref(p);
949 if (pmap == vmspace_pmap(vm))
957 * Allow userspace to directly trigger the VM drain routine for testing
961 debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
966 error = sysctl_handle_int(oidp, &i, 0, req);
969 if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
972 EVENTHANDLER_INVOKE(vm_lowmem, i);
975 SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem,
976 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_vm_lowmem, "I",
977 "set to trigger vm_lowmem event with given flags");
980 debug_uma_reclaim(SYSCTL_HANDLER_ARGS)
985 error = sysctl_handle_int(oidp, &i, 0, req);
986 if (error != 0 || req->newptr == NULL)
988 if (i != UMA_RECLAIM_TRIM && i != UMA_RECLAIM_DRAIN &&
989 i != UMA_RECLAIM_DRAIN_CPU)
994 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim,
995 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_uma_reclaim, "I",
996 "set to generate request to reclaim uma caches");
999 debug_uma_reclaim_domain(SYSCTL_HANDLER_ARGS)
1001 int domain, error, request;
1004 error = sysctl_handle_int(oidp, &request, 0, req);
1005 if (error != 0 || req->newptr == NULL)
1008 domain = request >> 4;
1010 if (request != UMA_RECLAIM_TRIM && request != UMA_RECLAIM_DRAIN &&
1011 request != UMA_RECLAIM_DRAIN_CPU)
1013 if (domain < 0 || domain >= vm_ndomains)
1015 uma_reclaim_domain(request, domain);
1018 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim_domain,
1019 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0,
1020 debug_uma_reclaim_domain, "I",