160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 343c4dd356SDavid Greenman * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38df8bae1dSRodney W. Grimes * All rights reserved. 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53df8bae1dSRodney W. Grimes * School of Computer Science 54df8bae1dSRodney W. Grimes * Carnegie Mellon University 55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 58df8bae1dSRodney W. Grimes * rights to redistribute these changes. 59df8bae1dSRodney W. Grimes */ 60df8bae1dSRodney W. Grimes 61874651b1SDavid E. O'Brien #include <sys/cdefs.h> 62874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 63874651b1SDavid E. O'Brien 64faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 6515a7ad60SPeter Wemm #include "opt_kstack_pages.h" 6615a7ad60SPeter Wemm #include "opt_kstack_max_pages.h" 67b7627840SKonstantin Belousov #include "opt_kstack_usage_prof.h" 68e9822d92SJoerg Wunsch 69df8bae1dSRodney W. Grimes #include <sys/param.h> 70df8bae1dSRodney W. Grimes #include <sys/systm.h> 71*b0058196SMark Johnston #include <sys/domainset.h> 72104a9b7eSAlexander Kabaev #include <sys/limits.h> 73fb919e4dSMark Murray #include <sys/lock.h> 745df87b21SJeff Roberson #include <sys/malloc.h> 75fb919e4dSMark Murray #include <sys/mutex.h> 76df8bae1dSRodney W. Grimes #include <sys/proc.h> 771ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 78df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 7989f6b863SAttilio Rao #include <sys/rwlock.h> 80da61b9a6SAlan Cox #include <sys/sched.h> 81da61b9a6SAlan Cox #include <sys/sf_buf.h> 823aa12267SBruce Evans #include <sys/shm.h> 83efeaf95aSDavid Greenman #include <sys/vmmeter.h> 845df87b21SJeff Roberson #include <sys/vmem.h> 851005a129SJohn Baldwin #include <sys/sx.h> 86ceb0cf87SJohn Dyson #include <sys/sysctl.h> 87e878d997SKonstantin Belousov #include <sys/_kstack_cache.h> 888a945d10SKonstantin Belousov #include <sys/eventhandler.h> 8926f9a767SRodney W. Grimes #include <sys/kernel.h> 900384fff8SJason Evans #include <sys/ktr.h> 91a2a1c95cSPeter Wemm #include <sys/unistd.h> 9226f9a767SRodney W. Grimes 93df8bae1dSRodney W. Grimes #include <vm/vm.h> 94efeaf95aSDavid Greenman #include <vm/vm_param.h> 95efeaf95aSDavid Greenman #include <vm/pmap.h> 9623984ce5SMark Johnston #include <vm/vm_domainset.h> 97efeaf95aSDavid Greenman #include <vm/vm_map.h> 98df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9926f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 100a136efe9SPeter Wemm #include <vm/vm_object.h> 101df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 102efeaf95aSDavid Greenman #include <vm/vm_extern.h> 103a136efe9SPeter Wemm #include <vm/vm_pager.h> 10492da00bbSMatthew Dillon #include <vm/swap_pager.h> 105efeaf95aSDavid Greenman 106b7627840SKonstantin Belousov #include <machine/cpu.h> 107b7627840SKonstantin Belousov 10843a90f3aSAlan Cox /* 10943a90f3aSAlan Cox * MPSAFE 1102d5c7e45SMatthew Dillon * 1112d5c7e45SMatthew Dillon * WARNING! This code calls vm_map_check_protection() which only checks 1122d5c7e45SMatthew Dillon * the associated vm_map_entry range. It does not determine whether the 1132d5c7e45SMatthew Dillon * contents of the memory is actually readable or writable. In most cases 1142d5c7e45SMatthew Dillon * just checking the vm_map_entry is sufficient within the kernel's address 1152d5c7e45SMatthew Dillon * space. 11643a90f3aSAlan Cox */ 117df8bae1dSRodney W. Grimes int 118d0389015SEd Maste kernacc(void *addr, int len, int rw) 119df8bae1dSRodney W. Grimes { 120df8bae1dSRodney W. Grimes boolean_t rv; 121df8bae1dSRodney W. Grimes vm_offset_t saddr, eaddr; 12202c58685SPoul-Henning Kamp vm_prot_t prot; 123df8bae1dSRodney W. Grimes 124e50f5c2eSBruce Evans KASSERT((rw & ~VM_PROT_ALL) == 0, 12502c58685SPoul-Henning Kamp ("illegal ``rw'' argument to kernacc (%x)\n", rw)); 12675337a56SAlan Cox 127f0165b1cSKonstantin Belousov if ((vm_offset_t)addr + len > vm_map_max(kernel_map) || 12875337a56SAlan Cox (vm_offset_t)addr + len < (vm_offset_t)addr) 12975337a56SAlan Cox return (FALSE); 13075337a56SAlan Cox 13102c58685SPoul-Henning Kamp prot = rw; 1326cde7a16SDavid Greenman saddr = trunc_page((vm_offset_t)addr); 1336cde7a16SDavid Greenman eaddr = round_page((vm_offset_t)addr + len); 134d8834602SAlan Cox vm_map_lock_read(kernel_map); 135df8bae1dSRodney W. Grimes rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 136d8834602SAlan Cox vm_map_unlock_read(kernel_map); 137df8bae1dSRodney W. Grimes return (rv == TRUE); 138df8bae1dSRodney W. Grimes } 139df8bae1dSRodney W. Grimes 14043a90f3aSAlan Cox /* 14143a90f3aSAlan Cox * MPSAFE 1422d5c7e45SMatthew Dillon * 1432d5c7e45SMatthew Dillon * WARNING! This code calls vm_map_check_protection() which only checks 1442d5c7e45SMatthew Dillon * the associated vm_map_entry range. It does not determine whether the 1452d5c7e45SMatthew Dillon * contents of the memory is actually readable or writable. vmapbuf(), 1462d5c7e45SMatthew Dillon * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be 147763df3ecSPedro F. Giffuni * used in conjunction with this call. 14843a90f3aSAlan Cox */ 149df8bae1dSRodney W. Grimes int 150d0389015SEd Maste useracc(void *addr, int len, int rw) 151df8bae1dSRodney W. Grimes { 152df8bae1dSRodney W. Grimes boolean_t rv; 15302c58685SPoul-Henning Kamp vm_prot_t prot; 15405ba50f5SJake Burkholder vm_map_t map; 155df8bae1dSRodney W. Grimes 156e50f5c2eSBruce Evans KASSERT((rw & ~VM_PROT_ALL) == 0, 15702c58685SPoul-Henning Kamp ("illegal ``rw'' argument to useracc (%x)\n", rw)); 15802c58685SPoul-Henning Kamp prot = rw; 15905ba50f5SJake Burkholder map = &curproc->p_vmspace->vm_map; 16005ba50f5SJake Burkholder if ((vm_offset_t)addr + len > vm_map_max(map) || 16105ba50f5SJake Burkholder (vm_offset_t)addr + len < (vm_offset_t)addr) { 16226f9a767SRodney W. Grimes return (FALSE); 16326f9a767SRodney W. Grimes } 164d8834602SAlan Cox vm_map_lock_read(map); 16505ba50f5SJake Burkholder rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr), 16605ba50f5SJake Burkholder round_page((vm_offset_t)addr + len), prot); 167d8834602SAlan Cox vm_map_unlock_read(map); 168df8bae1dSRodney W. Grimes return (rv == TRUE); 169df8bae1dSRodney W. Grimes } 170df8bae1dSRodney W. Grimes 17116929939SDon Lewis int 172f0ea4612SDon Lewis vslock(void *addr, size_t len) 17316929939SDon Lewis { 174bb734798SDon Lewis vm_offset_t end, last, start; 175bb734798SDon Lewis vm_size_t npages; 176bb734798SDon Lewis int error; 17716929939SDon Lewis 178bb734798SDon Lewis last = (vm_offset_t)addr + len; 179ce8660e3SDon Lewis start = trunc_page((vm_offset_t)addr); 180bb734798SDon Lewis end = round_page(last); 181bb734798SDon Lewis if (last < (vm_offset_t)addr || end < (vm_offset_t)addr) 18216929939SDon Lewis return (EINVAL); 18316929939SDon Lewis npages = atop(end - start); 18416929939SDon Lewis if (npages > vm_page_max_wired) 18516929939SDon Lewis return (ENOMEM); 18616929939SDon Lewis #if 0 18716929939SDon Lewis /* 18816929939SDon Lewis * XXX - not yet 18916929939SDon Lewis * 19016929939SDon Lewis * The limit for transient usage of wired pages should be 19116929939SDon Lewis * larger than for "permanent" wired pages (mlock()). 19216929939SDon Lewis * 19316929939SDon Lewis * Also, the sysctl code, which is the only present user 19416929939SDon Lewis * of vslock(), does a hard loop on EAGAIN. 19516929939SDon Lewis */ 196e958ad4cSJeff Roberson if (npages + vm_wire_count() > vm_page_max_wired) 19716929939SDon Lewis return (EAGAIN); 19816929939SDon Lewis #endif 199ce8660e3SDon Lewis error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end, 200d9b2500eSBrian Feldman VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 201ed9e8bc4SKonstantin Belousov if (error == KERN_SUCCESS) { 202ed9e8bc4SKonstantin Belousov curthread->td_vslock_sz += len; 203ed9e8bc4SKonstantin Belousov return (0); 204ed9e8bc4SKonstantin Belousov } 205ed9e8bc4SKonstantin Belousov 206ce8660e3SDon Lewis /* 207ce8660e3SDon Lewis * Return EFAULT on error to match copy{in,out}() behaviour 208ce8660e3SDon Lewis * rather than returning ENOMEM like mlock() would. 209ce8660e3SDon Lewis */ 210ed9e8bc4SKonstantin Belousov return (EFAULT); 21116929939SDon Lewis } 21216929939SDon Lewis 213ce8660e3SDon Lewis void 214f0ea4612SDon Lewis vsunlock(void *addr, size_t len) 21516929939SDon Lewis { 21616929939SDon Lewis 217ce8660e3SDon Lewis /* Rely on the parameter sanity checks performed by vslock(). */ 218ed9e8bc4SKonstantin Belousov MPASS(curthread->td_vslock_sz >= len); 219ed9e8bc4SKonstantin Belousov curthread->td_vslock_sz -= len; 220ce8660e3SDon Lewis (void)vm_map_unwire(&curproc->p_vmspace->vm_map, 221ce8660e3SDon Lewis trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), 22216929939SDon Lewis VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 22316929939SDon Lewis } 22416929939SDon Lewis 225da61b9a6SAlan Cox /* 226da61b9a6SAlan Cox * Pin the page contained within the given object at the given offset. If the 227da61b9a6SAlan Cox * page is not resident, allocate and load it using the given object's pager. 228da61b9a6SAlan Cox * Return the pinned page if successful; otherwise, return NULL. 229da61b9a6SAlan Cox */ 230da61b9a6SAlan Cox static vm_page_t 231be996836SAttilio Rao vm_imgact_hold_page(vm_object_t object, vm_ooffset_t offset) 232da61b9a6SAlan Cox { 233093c7f39SGleb Smirnoff vm_page_t m; 234da61b9a6SAlan Cox vm_pindex_t pindex; 235da61b9a6SAlan Cox int rv; 236da61b9a6SAlan Cox 23789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 238da61b9a6SAlan Cox pindex = OFF_TO_IDX(offset); 239ce3ee09bSAlan Cox m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY); 2400a2e596aSAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 241ce3ee09bSAlan Cox vm_page_xbusy(m); 242b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(object, &m, 1, NULL, NULL); 243d1a6e42dSAlan Cox if (rv != VM_PAGER_OK) { 2442965a453SKip Macy vm_page_lock(m); 245da61b9a6SAlan Cox vm_page_free(m); 2462965a453SKip Macy vm_page_unlock(m); 247da61b9a6SAlan Cox m = NULL; 248da61b9a6SAlan Cox goto out; 249da61b9a6SAlan Cox } 250c7aebda8SAttilio Rao vm_page_xunbusy(m); 251ce3ee09bSAlan Cox } 252be996836SAttilio Rao vm_page_lock(m); 253be996836SAttilio Rao vm_page_hold(m); 25470978c93SKonstantin Belousov vm_page_activate(m); 255be996836SAttilio Rao vm_page_unlock(m); 256da61b9a6SAlan Cox out: 25789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 258da61b9a6SAlan Cox return (m); 259da61b9a6SAlan Cox } 260da61b9a6SAlan Cox 261da61b9a6SAlan Cox /* 262da61b9a6SAlan Cox * Return a CPU private mapping to the page at the given offset within the 263da61b9a6SAlan Cox * given object. The page is pinned before it is mapped. 264da61b9a6SAlan Cox */ 265da61b9a6SAlan Cox struct sf_buf * 266da61b9a6SAlan Cox vm_imgact_map_page(vm_object_t object, vm_ooffset_t offset) 267da61b9a6SAlan Cox { 268da61b9a6SAlan Cox vm_page_t m; 269da61b9a6SAlan Cox 270be996836SAttilio Rao m = vm_imgact_hold_page(object, offset); 271da61b9a6SAlan Cox if (m == NULL) 272da61b9a6SAlan Cox return (NULL); 273da61b9a6SAlan Cox sched_pin(); 274da61b9a6SAlan Cox return (sf_buf_alloc(m, SFB_CPUPRIVATE)); 275da61b9a6SAlan Cox } 276da61b9a6SAlan Cox 277da61b9a6SAlan Cox /* 278da61b9a6SAlan Cox * Destroy the given CPU private mapping and unpin the page that it mapped. 279da61b9a6SAlan Cox */ 280da61b9a6SAlan Cox void 281be996836SAttilio Rao vm_imgact_unmap_page(struct sf_buf *sf) 282da61b9a6SAlan Cox { 283da61b9a6SAlan Cox vm_page_t m; 284da61b9a6SAlan Cox 285da61b9a6SAlan Cox m = sf_buf_page(sf); 286da61b9a6SAlan Cox sf_buf_free(sf); 287da61b9a6SAlan Cox sched_unpin(); 288be996836SAttilio Rao vm_page_lock(m); 289be996836SAttilio Rao vm_page_unhold(m); 290be996836SAttilio Rao vm_page_unlock(m); 291da61b9a6SAlan Cox } 292da61b9a6SAlan Cox 2931a4fcaebSMarcel Moolenaar void 2941a4fcaebSMarcel Moolenaar vm_sync_icache(vm_map_t map, vm_offset_t va, vm_offset_t sz) 2951a4fcaebSMarcel Moolenaar { 2961a4fcaebSMarcel Moolenaar 2971a4fcaebSMarcel Moolenaar pmap_sync_icache(map->pmap, va, sz); 2981a4fcaebSMarcel Moolenaar } 2991a4fcaebSMarcel Moolenaar 300e878d997SKonstantin Belousov struct kstack_cache_entry *kstack_cache; 3018a945d10SKonstantin Belousov static int kstack_cache_size = 128; 302*b0058196SMark Johnston static int kstacks, kstack_domain_iter; 3038a945d10SKonstantin Belousov static struct mtx kstack_cache_mtx; 30425c1e164SAndre Oppermann MTX_SYSINIT(kstack_cache, &kstack_cache_mtx, "kstkch", MTX_DEF); 30525c1e164SAndre Oppermann 3068a945d10SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, kstack_cache_size, CTLFLAG_RW, &kstack_cache_size, 0, 3078a945d10SKonstantin Belousov ""); 3088a945d10SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, kstacks, CTLFLAG_RD, &kstacks, 0, 3098a945d10SKonstantin Belousov ""); 3108a945d10SKonstantin Belousov 31149a2507bSAlan Cox /* 31249a2507bSAlan Cox * Create the kernel stack (including pcb for i386) for a new thread. 31349a2507bSAlan Cox * This routine directly affects the fork perf for a process and 31449a2507bSAlan Cox * create performance for a thread. 31549a2507bSAlan Cox */ 31689b57fcfSKonstantin Belousov int 31749a2507bSAlan Cox vm_thread_new(struct thread *td, int pages) 31849a2507bSAlan Cox { 31949a2507bSAlan Cox vm_object_t ksobj; 32049a2507bSAlan Cox vm_offset_t ks; 3215471caf6SAlan Cox vm_page_t ma[KSTACK_MAX_PAGES]; 3228a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce; 32349a2507bSAlan Cox int i; 32449a2507bSAlan Cox 32549a2507bSAlan Cox /* Bounds check */ 32649a2507bSAlan Cox if (pages <= 1) 327edc82223SKonstantin Belousov pages = kstack_pages; 32849a2507bSAlan Cox else if (pages > KSTACK_MAX_PAGES) 32949a2507bSAlan Cox pages = KSTACK_MAX_PAGES; 3308a945d10SKonstantin Belousov 331c0352925SMateusz Guzik if (pages == kstack_pages && kstack_cache != NULL) { 3328a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 3338a945d10SKonstantin Belousov if (kstack_cache != NULL) { 3348a945d10SKonstantin Belousov ks_ce = kstack_cache; 3358a945d10SKonstantin Belousov kstack_cache = ks_ce->next_ks_entry; 3368a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 3378a945d10SKonstantin Belousov 3388a945d10SKonstantin Belousov td->td_kstack_obj = ks_ce->ksobj; 3398a945d10SKonstantin Belousov td->td_kstack = (vm_offset_t)ks_ce; 340edc82223SKonstantin Belousov td->td_kstack_pages = kstack_pages; 3418a945d10SKonstantin Belousov return (1); 3428a945d10SKonstantin Belousov } 3438a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 3448a945d10SKonstantin Belousov } 3458a945d10SKonstantin Belousov 34649a2507bSAlan Cox /* 34749a2507bSAlan Cox * Allocate an object for the kstack. 34849a2507bSAlan Cox */ 34949a2507bSAlan Cox ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 350374ae2a3SJeff Roberson 35149a2507bSAlan Cox /* 35249a2507bSAlan Cox * Get a kernel virtual address for this thread's kstack. 35349a2507bSAlan Cox */ 354ca596a25SJuli Mallett #if defined(__mips__) 355ca596a25SJuli Mallett /* 356ca596a25SJuli Mallett * We need to align the kstack's mapped address to fit within 357ca596a25SJuli Mallett * a single TLB entry. 358ca596a25SJuli Mallett */ 3595df87b21SJeff Roberson if (vmem_xalloc(kernel_arena, (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE, 3605df87b21SJeff Roberson PAGE_SIZE * 2, 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX, 3615df87b21SJeff Roberson M_BESTFIT | M_NOWAIT, &ks)) { 3625df87b21SJeff Roberson ks = 0; 3635df87b21SJeff Roberson } 364ca596a25SJuli Mallett #else 3655df87b21SJeff Roberson ks = kva_alloc((pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 366ca596a25SJuli Mallett #endif 36789b57fcfSKonstantin Belousov if (ks == 0) { 36889b57fcfSKonstantin Belousov printf("vm_thread_new: kstack allocation failed\n"); 36989b57fcfSKonstantin Belousov vm_object_deallocate(ksobj); 37089b57fcfSKonstantin Belousov return (0); 37189b57fcfSKonstantin Belousov } 37289b57fcfSKonstantin Belousov 373*b0058196SMark Johnston /* 374*b0058196SMark Johnston * Ensure that kstack objects can draw pages from any memory 375*b0058196SMark Johnston * domain. Otherwise a local memory shortage can block a process 376*b0058196SMark Johnston * swap-in. 377*b0058196SMark Johnston */ 378*b0058196SMark Johnston if (vm_ndomains > 1) { 379*b0058196SMark Johnston ksobj->domain.dr_policy = DOMAINSET_RR(); 380*b0058196SMark Johnston ksobj->domain.dr_iter = atomic_fetchadd_int(&kstack_domain_iter, 381*b0058196SMark Johnston 1); 382*b0058196SMark Johnston } 383*b0058196SMark Johnston 3848a945d10SKonstantin Belousov atomic_add_int(&kstacks, 1); 38549a2507bSAlan Cox if (KSTACK_GUARD_PAGES != 0) { 38649a2507bSAlan Cox pmap_qremove(ks, KSTACK_GUARD_PAGES); 38749a2507bSAlan Cox ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 38849a2507bSAlan Cox } 38989b57fcfSKonstantin Belousov td->td_kstack_obj = ksobj; 39049a2507bSAlan Cox td->td_kstack = ks; 39149a2507bSAlan Cox /* 39249a2507bSAlan Cox * Knowing the number of pages allocated is useful when you 39349a2507bSAlan Cox * want to deallocate them. 39449a2507bSAlan Cox */ 39549a2507bSAlan Cox td->td_kstack_pages = pages; 39649a2507bSAlan Cox /* 39749a2507bSAlan Cox * For the length of the stack, link in a real page of ram for each 39849a2507bSAlan Cox * page of stack. 39949a2507bSAlan Cox */ 40089f6b863SAttilio Rao VM_OBJECT_WLOCK(ksobj); 4019df950b3SMark Johnston (void)vm_page_grab_pages(ksobj, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY | 4025471caf6SAlan Cox VM_ALLOC_WIRED, ma, pages); 4035471caf6SAlan Cox for (i = 0; i < pages; i++) 4045471caf6SAlan Cox ma[i]->valid = VM_PAGE_BITS_ALL; 40589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(ksobj); 40649a2507bSAlan Cox pmap_qenter(ks, ma, pages); 40789b57fcfSKonstantin Belousov return (1); 40849a2507bSAlan Cox } 40949a2507bSAlan Cox 4108a945d10SKonstantin Belousov static void 4118a945d10SKonstantin Belousov vm_thread_stack_dispose(vm_object_t ksobj, vm_offset_t ks, int pages) 41249a2507bSAlan Cox { 41349a2507bSAlan Cox vm_page_t m; 4148a945d10SKonstantin Belousov int i; 41549a2507bSAlan Cox 4168a945d10SKonstantin Belousov atomic_add_int(&kstacks, -1); 41749a2507bSAlan Cox pmap_qremove(ks, pages); 41889f6b863SAttilio Rao VM_OBJECT_WLOCK(ksobj); 41949a2507bSAlan Cox for (i = 0; i < pages; i++) { 42049a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 42149a2507bSAlan Cox if (m == NULL) 42249a2507bSAlan Cox panic("vm_thread_dispose: kstack already missing?"); 4232965a453SKip Macy vm_page_lock(m); 424e595970aSAlan Cox vm_page_unwire(m, PQ_NONE); 42549a2507bSAlan Cox vm_page_free(m); 4262965a453SKip Macy vm_page_unlock(m); 42749a2507bSAlan Cox } 42889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(ksobj); 42949a2507bSAlan Cox vm_object_deallocate(ksobj); 4305df87b21SJeff Roberson kva_free(ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 43149a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 432c3cf0b47SKonstantin Belousov } 433c3cf0b47SKonstantin Belousov 434c3cf0b47SKonstantin Belousov /* 4358a945d10SKonstantin Belousov * Dispose of a thread's kernel stack. 4368a945d10SKonstantin Belousov */ 4378a945d10SKonstantin Belousov void 4388a945d10SKonstantin Belousov vm_thread_dispose(struct thread *td) 4398a945d10SKonstantin Belousov { 4408a945d10SKonstantin Belousov vm_object_t ksobj; 4418a945d10SKonstantin Belousov vm_offset_t ks; 4428a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce; 4438a945d10SKonstantin Belousov int pages; 4448a945d10SKonstantin Belousov 4458a945d10SKonstantin Belousov pages = td->td_kstack_pages; 4468a945d10SKonstantin Belousov ksobj = td->td_kstack_obj; 4478a945d10SKonstantin Belousov ks = td->td_kstack; 4488a945d10SKonstantin Belousov td->td_kstack = 0; 4498a945d10SKonstantin Belousov td->td_kstack_pages = 0; 450edc82223SKonstantin Belousov if (pages == kstack_pages && kstacks <= kstack_cache_size) { 4518a945d10SKonstantin Belousov ks_ce = (struct kstack_cache_entry *)ks; 4528a945d10SKonstantin Belousov ks_ce->ksobj = ksobj; 4538a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 4548a945d10SKonstantin Belousov ks_ce->next_ks_entry = kstack_cache; 4558a945d10SKonstantin Belousov kstack_cache = ks_ce; 4568a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 4578a945d10SKonstantin Belousov return; 4588a945d10SKonstantin Belousov } 4598a945d10SKonstantin Belousov vm_thread_stack_dispose(ksobj, ks, pages); 4608a945d10SKonstantin Belousov } 4618a945d10SKonstantin Belousov 4628a945d10SKonstantin Belousov static void 4638a945d10SKonstantin Belousov vm_thread_stack_lowmem(void *nulll) 4648a945d10SKonstantin Belousov { 4658a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce, *ks_ce1; 4668a945d10SKonstantin Belousov 4678a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 4688a945d10SKonstantin Belousov ks_ce = kstack_cache; 4698a945d10SKonstantin Belousov kstack_cache = NULL; 4708a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 4718a945d10SKonstantin Belousov 4728a945d10SKonstantin Belousov while (ks_ce != NULL) { 4738a945d10SKonstantin Belousov ks_ce1 = ks_ce; 4748a945d10SKonstantin Belousov ks_ce = ks_ce->next_ks_entry; 4758a945d10SKonstantin Belousov 4768a945d10SKonstantin Belousov vm_thread_stack_dispose(ks_ce1->ksobj, (vm_offset_t)ks_ce1, 477edc82223SKonstantin Belousov kstack_pages); 4788a945d10SKonstantin Belousov } 4798a945d10SKonstantin Belousov } 4808a945d10SKonstantin Belousov 4818a945d10SKonstantin Belousov static void 4828a945d10SKonstantin Belousov kstack_cache_init(void *nulll) 4838a945d10SKonstantin Belousov { 4848a945d10SKonstantin Belousov 4858a945d10SKonstantin Belousov EVENTHANDLER_REGISTER(vm_lowmem, vm_thread_stack_lowmem, NULL, 4868a945d10SKonstantin Belousov EVENTHANDLER_PRI_ANY); 4878a945d10SKonstantin Belousov } 4888a945d10SKonstantin Belousov 4898a945d10SKonstantin Belousov SYSINIT(vm_kstacks, SI_SUB_KTHREAD_INIT, SI_ORDER_ANY, kstack_cache_init, NULL); 4908a945d10SKonstantin Belousov 491b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 492b7627840SKonstantin Belousov /* 493b7627840SKonstantin Belousov * Track maximum stack used by a thread in kernel. 494b7627840SKonstantin Belousov */ 495b7627840SKonstantin Belousov static int max_kstack_used; 496b7627840SKonstantin Belousov 497b7627840SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, max_kstack_used, CTLFLAG_RD, 498b7627840SKonstantin Belousov &max_kstack_used, 0, 499b7627840SKonstantin Belousov "Maxiumum stack depth used by a thread in kernel"); 500b7627840SKonstantin Belousov 501b7627840SKonstantin Belousov void 502b7627840SKonstantin Belousov intr_prof_stack_use(struct thread *td, struct trapframe *frame) 503b7627840SKonstantin Belousov { 504b7627840SKonstantin Belousov vm_offset_t stack_top; 505b7627840SKonstantin Belousov vm_offset_t current; 506b7627840SKonstantin Belousov int used, prev_used; 507b7627840SKonstantin Belousov 508b7627840SKonstantin Belousov /* 509b7627840SKonstantin Belousov * Testing for interrupted kernel mode isn't strictly 510b7627840SKonstantin Belousov * needed. It optimizes the execution, since interrupts from 511b7627840SKonstantin Belousov * usermode will have only the trap frame on the stack. 512b7627840SKonstantin Belousov */ 513b7627840SKonstantin Belousov if (TRAPF_USERMODE(frame)) 514b7627840SKonstantin Belousov return; 515b7627840SKonstantin Belousov 516b7627840SKonstantin Belousov stack_top = td->td_kstack + td->td_kstack_pages * PAGE_SIZE; 517b7627840SKonstantin Belousov current = (vm_offset_t)(uintptr_t)&stack_top; 518b7627840SKonstantin Belousov 519b7627840SKonstantin Belousov /* 520b7627840SKonstantin Belousov * Try to detect if interrupt is using kernel thread stack. 521b7627840SKonstantin Belousov * Hardware could use a dedicated stack for interrupt handling. 522b7627840SKonstantin Belousov */ 523b7627840SKonstantin Belousov if (stack_top <= current || current < td->td_kstack) 524b7627840SKonstantin Belousov return; 525b7627840SKonstantin Belousov 526b7627840SKonstantin Belousov used = stack_top - current; 527b7627840SKonstantin Belousov for (;;) { 528b7627840SKonstantin Belousov prev_used = max_kstack_used; 529b7627840SKonstantin Belousov if (prev_used >= used) 530b7627840SKonstantin Belousov break; 531b7627840SKonstantin Belousov if (atomic_cmpset_int(&max_kstack_used, prev_used, used)) 532b7627840SKonstantin Belousov break; 533b7627840SKonstantin Belousov } 534b7627840SKonstantin Belousov } 535b7627840SKonstantin Belousov #endif /* KSTACK_USAGE_PROF */ 536b7627840SKonstantin Belousov 537a136efe9SPeter Wemm /* 538df8bae1dSRodney W. Grimes * Implement fork's actions on an address space. 539df8bae1dSRodney W. Grimes * Here we arrange for the address space to be copied or referenced, 540df8bae1dSRodney W. Grimes * allocate a user struct (pcb and kernel stack), then call the 541df8bae1dSRodney W. Grimes * machine-dependent layer to fill those in and make the new process 542a2a1c95cSPeter Wemm * ready to run. The new process is set up so that it returns directly 543a2a1c95cSPeter Wemm * to user mode to avoid stack copying and relocation problems. 544df8bae1dSRodney W. Grimes */ 54589b57fcfSKonstantin Belousov int 546d0389015SEd Maste vm_forkproc(struct thread *td, struct proc *p2, struct thread *td2, 547d0389015SEd Maste struct vmspace *vm2, int flags) 548df8bae1dSRodney W. Grimes { 549b40ce416SJulian Elischer struct proc *p1 = td->td_proc; 55023984ce5SMark Johnston struct domainset *dset; 55189b57fcfSKonstantin Belousov int error; 552df8bae1dSRodney W. Grimes 55391c28bfdSLuoqi Chen if ((flags & RFPROC) == 0) { 55491c28bfdSLuoqi Chen /* 55591c28bfdSLuoqi Chen * Divorce the memory, if it is shared, essentially 55691c28bfdSLuoqi Chen * this changes shared memory amongst threads, into 55791c28bfdSLuoqi Chen * COW locally. 55891c28bfdSLuoqi Chen */ 55991c28bfdSLuoqi Chen if ((flags & RFMEM) == 0) { 56091c28bfdSLuoqi Chen if (p1->p_vmspace->vm_refcnt > 1) { 56189b57fcfSKonstantin Belousov error = vmspace_unshare(p1); 56289b57fcfSKonstantin Belousov if (error) 56389b57fcfSKonstantin Belousov return (error); 56491c28bfdSLuoqi Chen } 56591c28bfdSLuoqi Chen } 566079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 56789b57fcfSKonstantin Belousov return (0); 56891c28bfdSLuoqi Chen } 56991c28bfdSLuoqi Chen 5705856e12eSJohn Dyson if (flags & RFMEM) { 5715856e12eSJohn Dyson p2->p_vmspace = p1->p_vmspace; 5721a276a3fSAlan Cox atomic_add_int(&p1->p_vmspace->vm_refcnt, 1); 5735856e12eSJohn Dyson } 57423984ce5SMark Johnston dset = td2->td_domain.dr_policy; 57523984ce5SMark Johnston while (vm_page_count_severe_set(&dset->ds_mask)) { 57623984ce5SMark Johnston vm_wait_doms(&dset->ds_mask); 5770d94caffSDavid Greenman } 57826f9a767SRodney W. Grimes 5795856e12eSJohn Dyson if ((flags & RFMEM) == 0) { 58089b57fcfSKonstantin Belousov p2->p_vmspace = vm2; 581df8bae1dSRodney W. Grimes if (p1->p_vmspace->vm_shm) 582dabee6feSPeter Wemm shmfork(p1, p2); 583a2a1c95cSPeter Wemm } 584df8bae1dSRodney W. Grimes 58539fb8e6bSJulian Elischer /* 586a2a1c95cSPeter Wemm * cpu_fork will copy and update the pcb, set up the kernel stack, 587a2a1c95cSPeter Wemm * and make the child ready to run. 588df8bae1dSRodney W. Grimes */ 589079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 59089b57fcfSKonstantin Belousov return (0); 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes 593df8bae1dSRodney W. Grimes /* 594763df3ecSPedro F. Giffuni * Called after process has been wait(2)'ed upon and is being reaped. 595eb30c1c0SPeter Wemm * The idea is to reclaim resources that we could not reclaim while 596eb30c1c0SPeter Wemm * the process was still executing. 597eb30c1c0SPeter Wemm */ 598eb30c1c0SPeter Wemm void 599eb30c1c0SPeter Wemm vm_waitproc(p) 600eb30c1c0SPeter Wemm struct proc *p; 601eb30c1c0SPeter Wemm { 602eb30c1c0SPeter Wemm 603582ec34cSAlfred Perlstein vmspace_exitfree(p); /* and clean-out the vmspace */ 604eb30c1c0SPeter Wemm } 605eb30c1c0SPeter Wemm 60626f9a767SRodney W. Grimes void 607da7bbd2cSJohn Baldwin kick_proc0(void) 608d13ec713SStephan Uphoff { 609d13ec713SStephan Uphoff 610da7bbd2cSJohn Baldwin wakeup(&proc0); 611d13ec713SStephan Uphoff } 612