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>
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,
117 #if defined(__arm__) || defined(__sparc64__)
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 want large import sizes. */
128 #define KVA_QUANTUM_SHIFT (10 + PAGE_SHIFT)
130 #define KVA_QUANTUM (1 << KVA_QUANTUM_SHIFT)
135 * Allocate a virtual address range with no underlying object and
136 * no initial mapping to physical memory. Any mapping from this
137 * range to physical memory must be explicitly created prior to
138 * its use, typically with pmap_qenter(). Any attempt to create
139 * a mapping on demand through vm_fault() will result in a panic.
142 kva_alloc(vm_size_t size)
146 size = round_page(size);
147 if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
156 * Release a region of kernel virtual memory allocated
157 * with kva_alloc, and return the physical pages
158 * associated with that region.
160 * This routine may not block on kernel maps.
163 kva_free(vm_offset_t addr, vm_size_t size)
166 size = round_page(size);
167 vmem_free(kernel_arena, addr, size);
171 * Allocates a region from the kernel address map and physical pages
172 * within the specified address range to the kernel object. Creates a
173 * wired mapping from this region to these pages, and returns the
174 * region's starting virtual address. The allocated pages are not
175 * necessarily physically contiguous. If M_ZERO is specified through the
176 * given flags, then the pages are zeroed before they are mapped.
179 kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
180 vm_paddr_t high, vm_memattr_t memattr)
183 vm_object_t object = kernel_object;
184 vm_offset_t addr, i, offset;
188 size = round_page(size);
189 vmem = vm_dom[domain].vmd_kernel_arena;
190 if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
192 offset = addr - VM_MIN_KERNEL_ADDRESS;
193 pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
194 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
195 pflags |= VM_ALLOC_NOWAIT;
196 VM_OBJECT_WLOCK(object);
197 for (i = 0; i < size; i += PAGE_SIZE) {
200 m = vm_page_alloc_contig_domain(object, atop(offset + i),
201 domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
203 VM_OBJECT_WUNLOCK(object);
204 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
205 if (!vm_page_reclaim_contig_domain(domain,
206 pflags, 1, low, high, PAGE_SIZE, 0) &&
207 (flags & M_WAITOK) != 0)
208 vm_wait_domain(domain);
209 VM_OBJECT_WLOCK(object);
213 kmem_unback(object, addr, i);
214 vmem_free(vmem, addr, size);
217 KASSERT(vm_phys_domain(m) == domain,
218 ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
219 vm_phys_domain(m), domain));
220 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
222 m->valid = VM_PAGE_BITS_ALL;
223 pmap_enter(kernel_pmap, addr + i, m, VM_PROT_RW,
224 VM_PROT_RW | PMAP_ENTER_WIRED, 0);
226 VM_OBJECT_WUNLOCK(object);
231 kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
232 vm_memattr_t memattr)
234 struct vm_domainset_iter di;
238 vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
240 addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
244 } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
250 * Allocates a region from the kernel address map and physically
251 * contiguous pages within the specified address range to the kernel
252 * object. Creates a wired mapping from this region to these pages, and
253 * returns the region's starting virtual address. If M_ZERO is specified
254 * through the given flags, then the pages are zeroed before they are
258 kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
259 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
260 vm_memattr_t memattr)
263 vm_object_t object = kernel_object;
264 vm_offset_t addr, offset, tmp;
269 size = round_page(size);
270 vmem = vm_dom[domain].vmd_kernel_arena;
271 if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
273 offset = addr - VM_MIN_KERNEL_ADDRESS;
274 pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
275 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
276 pflags |= VM_ALLOC_NOWAIT;
278 VM_OBJECT_WLOCK(object);
281 m = vm_page_alloc_contig_domain(object, atop(offset), domain, pflags,
282 npages, low, high, alignment, boundary, memattr);
284 VM_OBJECT_WUNLOCK(object);
285 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
286 if (!vm_page_reclaim_contig_domain(domain, pflags,
287 npages, low, high, alignment, boundary) &&
288 (flags & M_WAITOK) != 0)
289 vm_wait_domain(domain);
290 VM_OBJECT_WLOCK(object);
294 vmem_free(vmem, addr, size);
297 KASSERT(vm_phys_domain(m) == domain,
298 ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
299 vm_phys_domain(m), domain));
302 for (; m < end_m; m++) {
303 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
305 m->valid = VM_PAGE_BITS_ALL;
306 pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
307 VM_PROT_RW | PMAP_ENTER_WIRED, 0);
310 VM_OBJECT_WUNLOCK(object);
315 kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
316 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
318 struct vm_domainset_iter di;
322 vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
324 addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
325 alignment, boundary, memattr);
328 } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
336 * Allocates a map to manage a subrange
337 * of the kernel virtual address space.
339 * Arguments are as follows:
341 * parent Map to take range from
342 * min, max Returned endpoints of map
343 * size Size of range to find
344 * superpage_align Request that min is superpage aligned
347 kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
348 vm_size_t size, boolean_t superpage_align)
353 size = round_page(size);
355 *min = vm_map_min(parent);
356 ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
357 VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
359 if (ret != KERN_SUCCESS)
360 panic("kmem_suballoc: bad status return of %d", ret);
362 result = vm_map_create(vm_map_pmap(parent), *min, *max);
364 panic("kmem_suballoc: cannot create submap");
365 if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
366 panic("kmem_suballoc: unable to change range to submap");
373 * Allocate wired-down pages in the kernel's address space.
376 kmem_malloc_domain(int domain, vm_size_t size, int flags)
382 #if VM_NRESERVLEVEL > 0
383 if (__predict_true((flags & M_EXEC) == 0))
384 arena = vm_dom[domain].vmd_kernel_arena;
386 arena = vm_dom[domain].vmd_kernel_rwx_arena;
388 arena = vm_dom[domain].vmd_kernel_arena;
390 size = round_page(size);
391 if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr))
394 rv = kmem_back_domain(domain, kernel_object, addr, size, flags);
395 if (rv != KERN_SUCCESS) {
396 vmem_free(arena, addr, size);
403 kmem_malloc(vm_size_t size, int flags)
405 struct vm_domainset_iter di;
409 vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
411 addr = kmem_malloc_domain(domain, size, flags);
414 } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
422 * Allocate physical pages from the specified domain for the specified
423 * virtual address range.
426 kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
427 vm_size_t size, int flags)
429 vm_offset_t offset, i;
434 KASSERT(object == kernel_object,
435 ("kmem_back_domain: only supports kernel object."));
437 offset = addr - VM_MIN_KERNEL_ADDRESS;
438 pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
439 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
440 if (flags & M_WAITOK)
441 pflags |= VM_ALLOC_WAITFAIL;
442 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
445 VM_OBJECT_WLOCK(object);
447 mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
448 for (; i < size; i += PAGE_SIZE, mpred = m) {
449 m = vm_page_alloc_domain_after(object, atop(offset + i),
450 domain, pflags, mpred);
453 * Ran out of space, free everything up and return. Don't need
454 * to lock page queues here as we know that the pages we got
455 * aren't on any queues.
458 if ((flags & M_NOWAIT) == 0)
460 VM_OBJECT_WUNLOCK(object);
461 kmem_unback(object, addr, i);
462 return (KERN_NO_SPACE);
464 KASSERT(vm_phys_domain(m) == domain,
465 ("kmem_back_domain: Domain mismatch %d != %d",
466 vm_phys_domain(m), domain));
467 if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
469 KASSERT((m->oflags & VPO_UNMANAGED) != 0,
470 ("kmem_malloc: page %p is managed", m));
471 m->valid = VM_PAGE_BITS_ALL;
472 pmap_enter(kernel_pmap, addr + i, m, prot,
473 prot | PMAP_ENTER_WIRED, 0);
474 #if VM_NRESERVLEVEL > 0
475 if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
476 m->oflags |= VPO_KMEM_EXEC;
479 VM_OBJECT_WUNLOCK(object);
481 return (KERN_SUCCESS);
487 * Allocate physical pages for the specified virtual address range.
490 kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
492 vm_offset_t end, next, start;
495 KASSERT(object == kernel_object,
496 ("kmem_back: only supports kernel object."));
498 for (start = addr, end = addr + size; addr < end; addr = next) {
500 * We must ensure that pages backing a given large virtual page
501 * all come from the same physical domain.
503 if (vm_ndomains > 1) {
504 domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
505 next = roundup2(addr + 1, KVA_QUANTUM);
506 if (next > end || next < start)
512 rv = kmem_back_domain(domain, object, addr, next - addr, flags);
513 if (rv != KERN_SUCCESS) {
514 kmem_unback(object, start, addr - start);
524 * Unmap and free the physical pages underlying the specified virtual
527 * A physical page must exist within the specified object at each index
528 * that is being unmapped.
531 _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
535 vm_offset_t end, offset;
538 KASSERT(object == kernel_object,
539 ("kmem_unback: only supports kernel object."));
543 pmap_remove(kernel_pmap, addr, addr + size);
544 offset = addr - VM_MIN_KERNEL_ADDRESS;
546 VM_OBJECT_WLOCK(object);
547 m = vm_page_lookup(object, atop(offset));
548 domain = vm_phys_domain(m);
549 #if VM_NRESERVLEVEL > 0
550 if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
551 arena = vm_dom[domain].vmd_kernel_arena;
553 arena = vm_dom[domain].vmd_kernel_rwx_arena;
555 arena = vm_dom[domain].vmd_kernel_arena;
557 for (; offset < end; offset += PAGE_SIZE, m = next) {
558 next = vm_page_next(m);
559 vm_page_unwire(m, PQ_NONE);
562 VM_OBJECT_WUNLOCK(object);
568 kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
571 (void)_kmem_unback(object, addr, size);
577 * Free memory allocated with kmem_malloc. The size must match the
578 * original allocation.
581 kmem_free(vm_offset_t addr, vm_size_t size)
585 size = round_page(size);
586 arena = _kmem_unback(kernel_object, addr, size);
588 vmem_free(arena, addr, size);
594 * Allocates pageable memory from a sub-map of the kernel. If the submap
595 * has no room, the caller sleeps waiting for more memory in the submap.
597 * This routine may block.
600 kmap_alloc_wait(vm_map_t map, vm_size_t size)
604 size = round_page(size);
605 if (!swap_reserve(size))
610 * To make this work for more than one map, use the map's lock
611 * to lock out sleepers/wakers.
614 if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
616 /* no space now; see if we can ever get space */
617 if (vm_map_max(map) - vm_map_min(map) < size) {
622 map->needs_wakeup = TRUE;
623 vm_map_unlock_and_wait(map, 0);
625 vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
626 VM_PROT_ALL, MAP_ACC_CHARGED);
634 * Returns memory to a submap of the kernel, and wakes up any processes
635 * waiting for memory in that map.
638 kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
642 (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
643 if (map->needs_wakeup) {
644 map->needs_wakeup = FALSE;
651 kmem_init_zero_region(void)
657 * Map a single physical page of zeros to a larger virtual range.
658 * This requires less looping in places that want large amounts of
659 * zeros, while not using much more physical resources.
661 addr = kva_alloc(ZERO_REGION_SIZE);
662 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
663 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
664 if ((m->flags & PG_ZERO) == 0)
666 for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
667 pmap_qenter(addr + i, &m, 1);
668 pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
670 zero_region = (const void *)addr;
674 * Import KVA from the kernel map into the kernel arena.
677 kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
682 KASSERT((size % KVA_QUANTUM) == 0,
683 ("kva_import: Size %jd is not a multiple of %d",
684 (intmax_t)size, (int)KVA_QUANTUM));
685 addr = vm_map_min(kernel_map);
686 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
687 VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
688 if (result != KERN_SUCCESS)
697 * Import KVA from a parent arena into a per-domain arena. Imports must be
698 * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
701 kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
704 KASSERT((size % KVA_QUANTUM) == 0,
705 ("kva_import_domain: Size %jd is not a multiple of %d",
706 (intmax_t)size, (int)KVA_QUANTUM));
707 return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
708 VMEM_ADDR_MAX, flags, addrp));
714 * Create the kernel map; insert a mapping covering kernel text,
715 * data, bss, and all space allocated thus far (`boostrap' data). The
716 * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
717 * `start' as allocated, and the range between `start' and `end' as free.
718 * Create the kernel vmem arena and its per-domain children.
721 kmem_init(vm_offset_t start, vm_offset_t end)
726 m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
729 /* N.B.: cannot use kgdb to debug, starting with this assignment ... */
731 (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
735 VM_MIN_KERNEL_ADDRESS,
737 start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
738 /* ... and ending with the completion of the above `insert' */
742 * Initialize the kernel_arena. This can grow on demand.
744 vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
745 vmem_set_import(kernel_arena, kva_import, NULL, NULL, KVA_QUANTUM);
747 for (domain = 0; domain < vm_ndomains; domain++) {
749 * Initialize the per-domain arenas. These are used to color
750 * the KVA space in a way that ensures that virtual large pages
751 * are backed by memory from the same physical domain,
752 * maximizing the potential for superpage promotion.
754 vm_dom[domain].vmd_kernel_arena = vmem_create(
755 "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
756 vmem_set_import(vm_dom[domain].vmd_kernel_arena,
757 kva_import_domain, NULL, kernel_arena, KVA_QUANTUM);
760 * In architectures with superpages, maintain separate arenas
761 * for allocations with permissions that differ from the
762 * "standard" read/write permissions used for kernel memory,
763 * so as not to inhibit superpage promotion.
765 #if VM_NRESERVLEVEL > 0
766 vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
767 "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
768 vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
769 kva_import_domain, (vmem_release_t *)vmem_xfree,
770 kernel_arena, KVA_QUANTUM);
776 * kmem_bootstrap_free:
778 * Free pages backing preloaded data (e.g., kernel modules) to the
779 * system. Currently only supported on platforms that create a
780 * vm_phys segment for preloaded data.
783 kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
785 #if defined(__i386__) || defined(__amd64__)
786 struct vm_domain *vmd;
791 end = trunc_page(start + size);
792 start = round_page(start);
794 for (va = start; va < end; va += PAGE_SIZE) {
795 pa = pmap_kextract(va);
796 m = PHYS_TO_VM_PAGE(pa);
798 vmd = vm_pagequeue_domain(m);
799 vm_domain_free_lock(vmd);
800 vm_phys_free_pages(m, 0);
801 vmd->vmd_page_count++;
802 vm_domain_free_unlock(vmd);
804 vm_domain_freecnt_inc(vmd, 1);
805 vm_cnt.v_page_count++;
807 pmap_remove(kernel_pmap, start, end);
808 (void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
814 * Allow userspace to directly trigger the VM drain routine for testing
818 debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
823 error = sysctl_handle_int(oidp, &i, 0, req);
826 if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
829 EVENTHANDLER_INVOKE(vm_lowmem, i);
833 SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
834 debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");