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
34 * from: @(#)vm_kern.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Kernel memory management.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h> /* for ticks and hz */
75 #include <sys/domainset.h>
76 #include <sys/eventhandler.h>
79 #include <sys/malloc.h>
80 #include <sys/rwlock.h>
81 #include <sys/sysctl.h>
83 #include <sys/vmmeter.h>
86 #include <vm/vm_param.h>
87 #include <vm/vm_domainset.h>
88 #include <vm/vm_kern.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_phys.h>
95 #include <vm/vm_pagequeue.h>
96 #include <vm/vm_radix.h>
97 #include <vm/vm_extern.h>
100 struct vm_map kernel_map_store;
101 struct vm_map exec_map_store;
102 struct vm_map pipe_map_store;
104 const void *zero_region;
105 CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
107 /* NB: Used by kernel debuggers. */
108 const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
110 u_int exec_map_entry_size;
111 u_int exec_map_entries;
113 SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
114 SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
116 SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
118 &vm_max_kernel_address, 0,
120 SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
122 "Max kernel address");
124 #if VM_NRESERVLEVEL > 0
125 #define KVA_QUANTUM_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT)
127 /* On non-superpage architectures we want large import sizes. */
128 #define KVA_QUANTUM_SHIFT (8 + PAGE_SHIFT)
130 #define KVA_QUANTUM (1 << KVA_QUANTUM_SHIFT)
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)
148 size = round_page(size);
149 if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
158 * Release a region of kernel virtual memory allocated
159 * with kva_alloc, and return the physical pages
160 * associated with that region.
162 * This routine may not block on kernel maps.
165 kva_free(vm_offset_t addr, vm_size_t size)
168 size = round_page(size);
169 vmem_free(kernel_arena, addr, size);
173 kmem_alloc_contig_pages(vm_object_t object, vm_pindex_t pindex, int domain,
174 int pflags, u_long npages, vm_paddr_t low, vm_paddr_t high,
175 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
181 VM_OBJECT_ASSERT_WLOCKED(object);
183 wait = (pflags & VM_ALLOC_WAITOK) != 0;
184 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
185 pflags |= VM_ALLOC_NOWAIT;
186 for (tries = wait ? 3 : 1;; tries--) {
187 m = vm_page_alloc_contig_domain(object, pindex, domain, pflags,
188 npages, low, high, alignment, boundary, memattr);
189 if (m != NULL || tries == 0)
192 VM_OBJECT_WUNLOCK(object);
193 if (!vm_page_reclaim_contig_domain(domain, pflags, npages,
194 low, high, alignment, boundary) && wait)
195 vm_wait_domain(domain);
196 VM_OBJECT_WLOCK(object);
202 * Allocates a region from the kernel address map and physical pages
203 * within the specified address range to the kernel object. Creates a
204 * wired mapping from this region to these pages, and returns the
205 * region's starting virtual address. The allocated pages are not
206 * necessarily physically contiguous. If M_ZERO is specified through the
207 * given flags, then the pages are zeroed before they are mapped.
210 kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
211 vm_paddr_t high, vm_memattr_t memattr)
215 vm_offset_t addr, i, offset;
220 object = kernel_object;
221 size = round_page(size);
222 vmem = vm_dom[domain].vmd_kernel_arena;
223 if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
225 offset = addr - VM_MIN_KERNEL_ADDRESS;
226 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
227 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
228 VM_OBJECT_WLOCK(object);
229 for (i = 0; i < size; i += PAGE_SIZE) {
230 m = kmem_alloc_contig_pages(object, atop(offset + i),
231 domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
233 VM_OBJECT_WUNLOCK(object);
234 kmem_unback(object, addr, i);
235 vmem_free(vmem, addr, size);
238 KASSERT(vm_phys_domain(m) == domain,
239 ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
240 vm_phys_domain(m), domain));
241 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
244 pmap_enter(kernel_pmap, addr + i, m, prot,
245 prot | PMAP_ENTER_WIRED, 0);
247 VM_OBJECT_WUNLOCK(object);
252 kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
253 vm_memattr_t memattr)
256 return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low,
261 kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size, int flags,
262 vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr)
264 struct vm_domainset_iter di;
268 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
270 addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
274 } while (vm_domainset_iter_policy(&di, &domain) == 0);
280 * Allocates a region from the kernel address map and physically
281 * contiguous pages within the specified address range to the kernel
282 * object. Creates a wired mapping from this region to these pages, and
283 * returns the region's starting virtual address. If M_ZERO is specified
284 * through the given flags, then the pages are zeroed before they are
288 kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
289 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
290 vm_memattr_t memattr)
294 vm_offset_t addr, offset, tmp;
299 object = kernel_object;
300 size = round_page(size);
301 vmem = vm_dom[domain].vmd_kernel_arena;
302 if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
304 offset = addr - VM_MIN_KERNEL_ADDRESS;
305 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
307 VM_OBJECT_WLOCK(object);
308 m = kmem_alloc_contig_pages(object, atop(offset), domain,
309 pflags, npages, low, high, alignment, boundary, memattr);
311 VM_OBJECT_WUNLOCK(object);
312 vmem_free(vmem, addr, size);
315 KASSERT(vm_phys_domain(m) == domain,
316 ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
317 vm_phys_domain(m), domain));
320 for (; m < end_m; m++) {
321 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
324 pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
325 VM_PROT_RW | PMAP_ENTER_WIRED, 0);
328 VM_OBJECT_WUNLOCK(object);
333 kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
334 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
337 return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low,
338 high, alignment, boundary, memattr));
342 kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags,
343 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
344 vm_memattr_t memattr)
346 struct vm_domainset_iter di;
350 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
352 addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
353 alignment, boundary, memattr);
356 } while (vm_domainset_iter_policy(&di, &domain) == 0);
364 * Initializes a map to manage a subrange
365 * of the kernel virtual address space.
367 * Arguments are as follows:
369 * parent Map to take range from
370 * min, max Returned endpoints of map
371 * size Size of range to find
372 * superpage_align Request that min is superpage aligned
375 kmem_subinit(vm_map_t map, vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
376 vm_size_t size, bool superpage_align)
380 size = round_page(size);
382 *min = vm_map_min(parent);
383 ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
384 VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
386 if (ret != KERN_SUCCESS)
387 panic("kmem_subinit: bad status return of %d", ret);
389 vm_map_init(map, vm_map_pmap(parent), *min, *max);
390 if (vm_map_submap(parent, *min, *max, map) != KERN_SUCCESS)
391 panic("kmem_subinit: unable to change range to submap");
395 * kmem_malloc_domain:
397 * Allocate wired-down pages in the kernel's address space.
400 kmem_malloc_domain(int domain, vm_size_t size, int flags)
406 if (__predict_true((flags & M_EXEC) == 0))
407 arena = vm_dom[domain].vmd_kernel_arena;
409 arena = vm_dom[domain].vmd_kernel_rwx_arena;
410 size = round_page(size);
411 if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr))
414 rv = kmem_back_domain(domain, kernel_object, addr, size, flags);
415 if (rv != KERN_SUCCESS) {
416 vmem_free(arena, addr, size);
423 kmem_malloc(vm_size_t size, int flags)
426 return (kmem_malloc_domainset(DOMAINSET_RR(), size, flags));
430 kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags)
432 struct vm_domainset_iter di;
436 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
438 addr = kmem_malloc_domain(domain, size, flags);
441 } while (vm_domainset_iter_policy(&di, &domain) == 0);
449 * Allocate physical pages from the specified domain for the specified
450 * virtual address range.
453 kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
454 vm_size_t size, int flags)
456 vm_offset_t offset, i;
461 KASSERT(object == kernel_object,
462 ("kmem_back_domain: only supports kernel object."));
464 offset = addr - VM_MIN_KERNEL_ADDRESS;
465 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
466 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
467 if (flags & M_WAITOK)
468 pflags |= VM_ALLOC_WAITFAIL;
469 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
472 VM_OBJECT_WLOCK(object);
474 mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
475 for (; i < size; i += PAGE_SIZE, mpred = m) {
476 m = vm_page_alloc_domain_after(object, atop(offset + i),
477 domain, pflags, mpred);
480 * Ran out of space, free everything up and return. Don't need
481 * to lock page queues here as we know that the pages we got
482 * aren't on any queues.
485 if ((flags & M_NOWAIT) == 0)
487 VM_OBJECT_WUNLOCK(object);
488 kmem_unback(object, addr, i);
489 return (KERN_NO_SPACE);
491 KASSERT(vm_phys_domain(m) == domain,
492 ("kmem_back_domain: Domain mismatch %d != %d",
493 vm_phys_domain(m), domain));
494 if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
496 KASSERT((m->oflags & VPO_UNMANAGED) != 0,
497 ("kmem_malloc: page %p is managed", m));
499 pmap_enter(kernel_pmap, addr + i, m, prot,
500 prot | PMAP_ENTER_WIRED, 0);
501 if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
502 m->oflags |= VPO_KMEM_EXEC;
504 VM_OBJECT_WUNLOCK(object);
506 return (KERN_SUCCESS);
512 * Allocate physical pages for the specified virtual address range.
515 kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
517 vm_offset_t end, next, start;
520 KASSERT(object == kernel_object,
521 ("kmem_back: only supports kernel object."));
523 for (start = addr, end = addr + size; addr < end; addr = next) {
525 * We must ensure that pages backing a given large virtual page
526 * all come from the same physical domain.
528 if (vm_ndomains > 1) {
529 domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
530 while (VM_DOMAIN_EMPTY(domain))
532 next = roundup2(addr + 1, KVA_QUANTUM);
533 if (next > end || next < start)
539 rv = kmem_back_domain(domain, object, addr, next - addr, flags);
540 if (rv != KERN_SUCCESS) {
541 kmem_unback(object, start, addr - start);
551 * Unmap and free the physical pages underlying the specified virtual
554 * A physical page must exist within the specified object at each index
555 * that is being unmapped.
558 _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
562 vm_offset_t end, offset;
565 KASSERT(object == kernel_object,
566 ("kmem_unback: only supports kernel object."));
570 pmap_remove(kernel_pmap, addr, addr + size);
571 offset = addr - VM_MIN_KERNEL_ADDRESS;
573 VM_OBJECT_WLOCK(object);
574 m = vm_page_lookup(object, atop(offset));
575 domain = vm_phys_domain(m);
576 if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
577 arena = vm_dom[domain].vmd_kernel_arena;
579 arena = vm_dom[domain].vmd_kernel_rwx_arena;
580 for (; offset < end; offset += PAGE_SIZE, m = next) {
581 next = vm_page_next(m);
582 vm_page_xbusy_claim(m);
583 vm_page_unwire_noq(m);
586 VM_OBJECT_WUNLOCK(object);
592 kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
595 (void)_kmem_unback(object, addr, size);
601 * Free memory allocated with kmem_malloc. The size must match the
602 * original allocation.
605 kmem_free(vm_offset_t addr, vm_size_t size)
609 size = round_page(size);
610 arena = _kmem_unback(kernel_object, addr, size);
612 vmem_free(arena, addr, size);
618 * Allocates pageable memory from a sub-map of the kernel. If the submap
619 * has no room, the caller sleeps waiting for more memory in the submap.
621 * This routine may block.
624 kmap_alloc_wait(vm_map_t map, vm_size_t size)
628 size = round_page(size);
629 if (!swap_reserve(size))
634 * To make this work for more than one map, use the map's lock
635 * to lock out sleepers/wakers.
638 addr = vm_map_findspace(map, vm_map_min(map), size);
639 if (addr + size <= vm_map_max(map))
641 /* no space now; see if we can ever get space */
642 if (vm_map_max(map) - vm_map_min(map) < size) {
647 map->needs_wakeup = TRUE;
648 vm_map_unlock_and_wait(map, 0);
650 vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW,
659 * Returns memory to a submap of the kernel, and wakes up any processes
660 * waiting for memory in that map.
663 kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
667 (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
668 if (map->needs_wakeup) {
669 map->needs_wakeup = FALSE;
676 kmem_init_zero_region(void)
682 * Map a single physical page of zeros to a larger virtual range.
683 * This requires less looping in places that want large amounts of
684 * zeros, while not using much more physical resources.
686 addr = kva_alloc(ZERO_REGION_SIZE);
687 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
688 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
689 if ((m->flags & PG_ZERO) == 0)
691 for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
692 pmap_qenter(addr + i, &m, 1);
693 pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
695 zero_region = (const void *)addr;
699 * Import KVA from the kernel map into the kernel arena.
702 kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
707 KASSERT((size % KVA_QUANTUM) == 0,
708 ("kva_import: Size %jd is not a multiple of %d",
709 (intmax_t)size, (int)KVA_QUANTUM));
710 addr = vm_map_min(kernel_map);
711 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
712 VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
713 if (result != KERN_SUCCESS)
722 * Import KVA from a parent arena into a per-domain arena. Imports must be
723 * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
726 kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
729 KASSERT((size % KVA_QUANTUM) == 0,
730 ("kva_import_domain: Size %jd is not a multiple of %d",
731 (intmax_t)size, (int)KVA_QUANTUM));
732 return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
733 VMEM_ADDR_MAX, flags, addrp));
739 * Create the kernel map; insert a mapping covering kernel text,
740 * data, bss, and all space allocated thus far (`boostrap' data). The
741 * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
742 * `start' as allocated, and the range between `start' and `end' as free.
743 * Create the kernel vmem arena and its per-domain children.
746 kmem_init(vm_offset_t start, vm_offset_t end)
750 vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
751 kernel_map->system_map = 1;
752 vm_map_lock(kernel_map);
753 /* N.B.: cannot use kgdb to debug, starting with this assignment ... */
754 (void)vm_map_insert(kernel_map, NULL, 0,
758 VM_MIN_KERNEL_ADDRESS,
760 start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
761 /* ... and ending with the completion of the above `insert' */
765 * Mark KVA used for the page array as allocated. Other platforms
766 * that handle vm_page_array allocation can simply adjust virtual_avail
769 (void)vm_map_insert(kernel_map, NULL, 0, (vm_offset_t)vm_page_array,
770 (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size *
771 sizeof(struct vm_page)),
772 VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
774 vm_map_unlock(kernel_map);
777 * Initialize the kernel_arena. This can grow on demand.
779 vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
780 vmem_set_import(kernel_arena, kva_import, NULL, NULL, KVA_QUANTUM);
782 for (domain = 0; domain < vm_ndomains; domain++) {
784 * Initialize the per-domain arenas. These are used to color
785 * the KVA space in a way that ensures that virtual large pages
786 * are backed by memory from the same physical domain,
787 * maximizing the potential for superpage promotion.
789 vm_dom[domain].vmd_kernel_arena = vmem_create(
790 "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
791 vmem_set_import(vm_dom[domain].vmd_kernel_arena,
792 kva_import_domain, NULL, kernel_arena, KVA_QUANTUM);
795 * In architectures with superpages, maintain separate arenas
796 * for allocations with permissions that differ from the
797 * "standard" read/write permissions used for kernel memory,
798 * so as not to inhibit superpage promotion.
800 #if VM_NRESERVLEVEL > 0
801 vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
802 "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
803 vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
804 kva_import_domain, (vmem_release_t *)vmem_xfree,
805 kernel_arena, KVA_QUANTUM);
807 vm_dom[domain].vmd_kernel_rwx_arena =
808 vm_dom[domain].vmd_kernel_arena;
813 * This must be the very first call so that the virtual address
814 * space used for early allocations is properly marked used in
821 * kmem_bootstrap_free:
823 * Free pages backing preloaded data (e.g., kernel modules) to the
824 * system. Currently only supported on platforms that create a
825 * vm_phys segment for preloaded data.
828 kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
830 #if defined(__i386__) || defined(__amd64__)
831 struct vm_domain *vmd;
836 end = trunc_page(start + size);
837 start = round_page(start);
841 * Preloaded files do not have execute permissions by default on amd64.
842 * Restore the default permissions to ensure that the direct map alias
845 pmap_change_prot(start, end - start, VM_PROT_RW);
847 for (va = start; va < end; va += PAGE_SIZE) {
848 pa = pmap_kextract(va);
849 m = PHYS_TO_VM_PAGE(pa);
851 vmd = vm_pagequeue_domain(m);
852 vm_domain_free_lock(vmd);
853 vm_phys_free_pages(m, 0);
854 vm_domain_free_unlock(vmd);
856 vm_domain_freecnt_inc(vmd, 1);
857 vm_cnt.v_page_count++;
859 pmap_remove(kernel_pmap, start, end);
860 (void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
865 * Allow userspace to directly trigger the VM drain routine for testing
869 debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
874 error = sysctl_handle_int(oidp, &i, 0, req);
877 if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
880 EVENTHANDLER_INVOKE(vm_lowmem, i);
884 SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0,
885 debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");