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> 71b0058196SMark 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> 878a945d10SKonstantin Belousov #include <sys/eventhandler.h> 8826f9a767SRodney W. Grimes #include <sys/kernel.h> 890384fff8SJason Evans #include <sys/ktr.h> 90a2a1c95cSPeter Wemm #include <sys/unistd.h> 9126f9a767SRodney W. Grimes 92*0b26119bSJeff Roberson #include <vm/uma.h> 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); 18454a3a114SMark Johnston if (npages > vm_page_max_user_wired) 18516929939SDon Lewis return (ENOMEM); 186ce8660e3SDon Lewis error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end, 187d9b2500eSBrian Feldman VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 188ed9e8bc4SKonstantin Belousov if (error == KERN_SUCCESS) { 189ed9e8bc4SKonstantin Belousov curthread->td_vslock_sz += len; 190ed9e8bc4SKonstantin Belousov return (0); 191ed9e8bc4SKonstantin Belousov } 192ed9e8bc4SKonstantin Belousov 193ce8660e3SDon Lewis /* 194ce8660e3SDon Lewis * Return EFAULT on error to match copy{in,out}() behaviour 195ce8660e3SDon Lewis * rather than returning ENOMEM like mlock() would. 196ce8660e3SDon Lewis */ 197ed9e8bc4SKonstantin Belousov return (EFAULT); 19816929939SDon Lewis } 19916929939SDon Lewis 200ce8660e3SDon Lewis void 201f0ea4612SDon Lewis vsunlock(void *addr, size_t len) 20216929939SDon Lewis { 20316929939SDon Lewis 204ce8660e3SDon Lewis /* Rely on the parameter sanity checks performed by vslock(). */ 205ed9e8bc4SKonstantin Belousov MPASS(curthread->td_vslock_sz >= len); 206ed9e8bc4SKonstantin Belousov curthread->td_vslock_sz -= len; 207ce8660e3SDon Lewis (void)vm_map_unwire(&curproc->p_vmspace->vm_map, 208ce8660e3SDon Lewis trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), 20916929939SDon Lewis VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 21016929939SDon Lewis } 21116929939SDon Lewis 212da61b9a6SAlan Cox /* 213da61b9a6SAlan Cox * Pin the page contained within the given object at the given offset. If the 214da61b9a6SAlan Cox * page is not resident, allocate and load it using the given object's pager. 215da61b9a6SAlan Cox * Return the pinned page if successful; otherwise, return NULL. 216da61b9a6SAlan Cox */ 217da61b9a6SAlan Cox static vm_page_t 218be996836SAttilio Rao vm_imgact_hold_page(vm_object_t object, vm_ooffset_t offset) 219da61b9a6SAlan Cox { 220093c7f39SGleb Smirnoff vm_page_t m; 221da61b9a6SAlan Cox vm_pindex_t pindex; 222da61b9a6SAlan Cox int rv; 223da61b9a6SAlan Cox 22489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 225da61b9a6SAlan Cox pindex = OFF_TO_IDX(offset); 226eeacb3b0SMark Johnston m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY | 227eeacb3b0SMark Johnston VM_ALLOC_WIRED); 2280a2e596aSAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 229ce3ee09bSAlan Cox vm_page_xbusy(m); 230b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(object, &m, 1, NULL, NULL); 231d1a6e42dSAlan Cox if (rv != VM_PAGER_OK) { 2322965a453SKip Macy vm_page_lock(m); 233eeacb3b0SMark Johnston vm_page_unwire(m, PQ_NONE); 234da61b9a6SAlan Cox vm_page_free(m); 2352965a453SKip Macy vm_page_unlock(m); 236da61b9a6SAlan Cox m = NULL; 237da61b9a6SAlan Cox goto out; 238da61b9a6SAlan Cox } 239c7aebda8SAttilio Rao vm_page_xunbusy(m); 240ce3ee09bSAlan Cox } 241da61b9a6SAlan Cox out: 24289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 243da61b9a6SAlan Cox return (m); 244da61b9a6SAlan Cox } 245da61b9a6SAlan Cox 246da61b9a6SAlan Cox /* 247da61b9a6SAlan Cox * Return a CPU private mapping to the page at the given offset within the 248da61b9a6SAlan Cox * given object. The page is pinned before it is mapped. 249da61b9a6SAlan Cox */ 250da61b9a6SAlan Cox struct sf_buf * 251da61b9a6SAlan Cox vm_imgact_map_page(vm_object_t object, vm_ooffset_t offset) 252da61b9a6SAlan Cox { 253da61b9a6SAlan Cox vm_page_t m; 254da61b9a6SAlan Cox 255be996836SAttilio Rao m = vm_imgact_hold_page(object, offset); 256da61b9a6SAlan Cox if (m == NULL) 257da61b9a6SAlan Cox return (NULL); 258da61b9a6SAlan Cox sched_pin(); 259da61b9a6SAlan Cox return (sf_buf_alloc(m, SFB_CPUPRIVATE)); 260da61b9a6SAlan Cox } 261da61b9a6SAlan Cox 262da61b9a6SAlan Cox /* 263da61b9a6SAlan Cox * Destroy the given CPU private mapping and unpin the page that it mapped. 264da61b9a6SAlan Cox */ 265da61b9a6SAlan Cox void 266be996836SAttilio Rao vm_imgact_unmap_page(struct sf_buf *sf) 267da61b9a6SAlan Cox { 268da61b9a6SAlan Cox vm_page_t m; 269da61b9a6SAlan Cox 270da61b9a6SAlan Cox m = sf_buf_page(sf); 271da61b9a6SAlan Cox sf_buf_free(sf); 272da61b9a6SAlan Cox sched_unpin(); 273be996836SAttilio Rao vm_page_lock(m); 274eeacb3b0SMark Johnston vm_page_unwire(m, PQ_ACTIVE); 275be996836SAttilio Rao vm_page_unlock(m); 276da61b9a6SAlan Cox } 277da61b9a6SAlan Cox 2781a4fcaebSMarcel Moolenaar void 2791a4fcaebSMarcel Moolenaar vm_sync_icache(vm_map_t map, vm_offset_t va, vm_offset_t sz) 2801a4fcaebSMarcel Moolenaar { 2811a4fcaebSMarcel Moolenaar 2821a4fcaebSMarcel Moolenaar pmap_sync_icache(map->pmap, va, sz); 2831a4fcaebSMarcel Moolenaar } 2841a4fcaebSMarcel Moolenaar 285*0b26119bSJeff Roberson static uma_zone_t kstack_cache; 2868a945d10SKonstantin Belousov static int kstack_cache_size = 128; 287*0b26119bSJeff Roberson static int kstack_domain_iter; 28825c1e164SAndre Oppermann 289*0b26119bSJeff Roberson static int 290*0b26119bSJeff Roberson sysctl_kstack_cache_size(SYSCTL_HANDLER_ARGS) 291*0b26119bSJeff Roberson { 292*0b26119bSJeff Roberson int error, newsize; 293*0b26119bSJeff Roberson 294*0b26119bSJeff Roberson newsize = kstack_cache_size; 295*0b26119bSJeff Roberson error = sysctl_handle_int(oidp, &newsize, 0, req); 296*0b26119bSJeff Roberson if (error == 0 && req->newptr && newsize != kstack_cache_size) 297*0b26119bSJeff Roberson kstack_cache_size = 298*0b26119bSJeff Roberson uma_zone_set_maxcache(kstack_cache, newsize); 299*0b26119bSJeff Roberson return (error); 300*0b26119bSJeff Roberson } 301*0b26119bSJeff Roberson SYSCTL_PROC(_vm, OID_AUTO, kstack_cache_size, CTLTYPE_INT|CTLFLAG_RW, 302*0b26119bSJeff Roberson &kstack_cache_size, 0, sysctl_kstack_cache_size, "IU", 303*0b26119bSJeff Roberson "Maximum number of cached kernel stacks"); 3048a945d10SKonstantin Belousov 30549a2507bSAlan Cox /* 30649a2507bSAlan Cox * Create the kernel stack (including pcb for i386) for a new thread. 30749a2507bSAlan Cox * This routine directly affects the fork perf for a process and 30849a2507bSAlan Cox * create performance for a thread. 30949a2507bSAlan Cox */ 310*0b26119bSJeff Roberson static vm_offset_t 311*0b26119bSJeff Roberson vm_thread_stack_create(struct domainset *ds, vm_object_t *ksobjp, int pages) 31249a2507bSAlan Cox { 313*0b26119bSJeff Roberson vm_page_t ma[KSTACK_MAX_PAGES]; 31449a2507bSAlan Cox vm_object_t ksobj; 31549a2507bSAlan Cox vm_offset_t ks; 31649a2507bSAlan Cox int i; 31749a2507bSAlan Cox 31849a2507bSAlan Cox /* 31949a2507bSAlan Cox * Allocate an object for the kstack. 32049a2507bSAlan Cox */ 32149a2507bSAlan Cox ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 322374ae2a3SJeff Roberson 32349a2507bSAlan Cox /* 32449a2507bSAlan Cox * Get a kernel virtual address for this thread's kstack. 32549a2507bSAlan Cox */ 326ca596a25SJuli Mallett #if defined(__mips__) 327ca596a25SJuli Mallett /* 328ca596a25SJuli Mallett * We need to align the kstack's mapped address to fit within 329ca596a25SJuli Mallett * a single TLB entry. 330ca596a25SJuli Mallett */ 3315df87b21SJeff Roberson if (vmem_xalloc(kernel_arena, (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE, 3325df87b21SJeff Roberson PAGE_SIZE * 2, 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX, 3335df87b21SJeff Roberson M_BESTFIT | M_NOWAIT, &ks)) { 3345df87b21SJeff Roberson ks = 0; 3355df87b21SJeff Roberson } 336ca596a25SJuli Mallett #else 3375df87b21SJeff Roberson ks = kva_alloc((pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 338ca596a25SJuli Mallett #endif 33989b57fcfSKonstantin Belousov if (ks == 0) { 34089b57fcfSKonstantin Belousov printf("vm_thread_new: kstack allocation failed\n"); 34189b57fcfSKonstantin Belousov vm_object_deallocate(ksobj); 34289b57fcfSKonstantin Belousov return (0); 34389b57fcfSKonstantin Belousov } 344b0058196SMark Johnston if (vm_ndomains > 1) { 345*0b26119bSJeff Roberson ksobj->domain.dr_policy = ds; 3464c29d2deSMark Johnston ksobj->domain.dr_iter = 3472801dd08SMark Johnston atomic_fetchadd_int(&kstack_domain_iter, 1); 348b0058196SMark Johnston } 349b0058196SMark Johnston 35049a2507bSAlan Cox if (KSTACK_GUARD_PAGES != 0) { 35149a2507bSAlan Cox pmap_qremove(ks, KSTACK_GUARD_PAGES); 35249a2507bSAlan Cox ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 35349a2507bSAlan Cox } 354*0b26119bSJeff Roberson 35549a2507bSAlan Cox /* 35649a2507bSAlan Cox * For the length of the stack, link in a real page of ram for each 35749a2507bSAlan Cox * page of stack. 35849a2507bSAlan Cox */ 35989f6b863SAttilio Rao VM_OBJECT_WLOCK(ksobj); 3609df950b3SMark Johnston (void)vm_page_grab_pages(ksobj, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY | 3615471caf6SAlan Cox VM_ALLOC_WIRED, ma, pages); 3625471caf6SAlan Cox for (i = 0; i < pages; i++) 3635471caf6SAlan Cox ma[i]->valid = VM_PAGE_BITS_ALL; 36489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(ksobj); 36549a2507bSAlan Cox pmap_qenter(ks, ma, pages); 366*0b26119bSJeff Roberson *ksobjp = ksobj; 367*0b26119bSJeff Roberson 368*0b26119bSJeff Roberson return (ks); 36949a2507bSAlan Cox } 37049a2507bSAlan Cox 3718a945d10SKonstantin Belousov static void 3728a945d10SKonstantin Belousov vm_thread_stack_dispose(vm_object_t ksobj, vm_offset_t ks, int pages) 37349a2507bSAlan Cox { 37449a2507bSAlan Cox vm_page_t m; 3758a945d10SKonstantin Belousov int i; 37649a2507bSAlan Cox 37749a2507bSAlan Cox pmap_qremove(ks, pages); 37889f6b863SAttilio Rao VM_OBJECT_WLOCK(ksobj); 37949a2507bSAlan Cox for (i = 0; i < pages; i++) { 38049a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 38149a2507bSAlan Cox if (m == NULL) 38249a2507bSAlan Cox panic("vm_thread_dispose: kstack already missing?"); 3832965a453SKip Macy vm_page_lock(m); 38488ea538aSMark Johnston vm_page_unwire_noq(m); 38549a2507bSAlan Cox vm_page_free(m); 3862965a453SKip Macy vm_page_unlock(m); 38749a2507bSAlan Cox } 38889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(ksobj); 38949a2507bSAlan Cox vm_object_deallocate(ksobj); 3905df87b21SJeff Roberson kva_free(ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 39149a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 392c3cf0b47SKonstantin Belousov } 393c3cf0b47SKonstantin Belousov 394c3cf0b47SKonstantin Belousov /* 395*0b26119bSJeff Roberson * Allocate the kernel stack for a new thread. 396*0b26119bSJeff Roberson */ 397*0b26119bSJeff Roberson int 398*0b26119bSJeff Roberson vm_thread_new(struct thread *td, int pages) 399*0b26119bSJeff Roberson { 400*0b26119bSJeff Roberson vm_object_t ksobj; 401*0b26119bSJeff Roberson vm_offset_t ks; 402*0b26119bSJeff Roberson 403*0b26119bSJeff Roberson /* Bounds check */ 404*0b26119bSJeff Roberson if (pages <= 1) 405*0b26119bSJeff Roberson pages = kstack_pages; 406*0b26119bSJeff Roberson else if (pages > KSTACK_MAX_PAGES) 407*0b26119bSJeff Roberson pages = KSTACK_MAX_PAGES; 408*0b26119bSJeff Roberson 409*0b26119bSJeff Roberson ks = 0; 410*0b26119bSJeff Roberson ksobj = NULL; 411*0b26119bSJeff Roberson if (pages == kstack_pages && kstack_cache != NULL) { 412*0b26119bSJeff Roberson ks = (vm_offset_t)uma_zalloc(kstack_cache, M_NOWAIT); 413*0b26119bSJeff Roberson if (ks != 0) 414*0b26119bSJeff Roberson ksobj = PHYS_TO_VM_PAGE(pmap_kextract(ks))->object; 415*0b26119bSJeff Roberson } 416*0b26119bSJeff Roberson 417*0b26119bSJeff Roberson /* 418*0b26119bSJeff Roberson * Ensure that kstack objects can draw pages from any memory 419*0b26119bSJeff Roberson * domain. Otherwise a local memory shortage can block a process 420*0b26119bSJeff Roberson * swap-in. 421*0b26119bSJeff Roberson */ 422*0b26119bSJeff Roberson if (ks == 0) 423*0b26119bSJeff Roberson ks = vm_thread_stack_create(DOMAINSET_PREF(PCPU_GET(domain)), 424*0b26119bSJeff Roberson &ksobj, pages); 425*0b26119bSJeff Roberson if (ks == 0) 426*0b26119bSJeff Roberson return (0); 427*0b26119bSJeff Roberson td->td_kstack_obj = ksobj; 428*0b26119bSJeff Roberson td->td_kstack = ks; 429*0b26119bSJeff Roberson td->td_kstack_pages = pages; 430*0b26119bSJeff Roberson return (1); 431*0b26119bSJeff Roberson } 432*0b26119bSJeff Roberson 433*0b26119bSJeff Roberson /* 4348a945d10SKonstantin Belousov * Dispose of a thread's kernel stack. 4358a945d10SKonstantin Belousov */ 4368a945d10SKonstantin Belousov void 4378a945d10SKonstantin Belousov vm_thread_dispose(struct thread *td) 4388a945d10SKonstantin Belousov { 4398a945d10SKonstantin Belousov vm_object_t ksobj; 4408a945d10SKonstantin Belousov vm_offset_t ks; 4418a945d10SKonstantin Belousov int pages; 4428a945d10SKonstantin Belousov 4438a945d10SKonstantin Belousov pages = td->td_kstack_pages; 4448a945d10SKonstantin Belousov ksobj = td->td_kstack_obj; 4458a945d10SKonstantin Belousov ks = td->td_kstack; 4468a945d10SKonstantin Belousov td->td_kstack = 0; 4478a945d10SKonstantin Belousov td->td_kstack_pages = 0; 448*0b26119bSJeff Roberson if (pages == kstack_pages) 449*0b26119bSJeff Roberson uma_zfree(kstack_cache, (void *)ks); 450*0b26119bSJeff Roberson else 4518a945d10SKonstantin Belousov vm_thread_stack_dispose(ksobj, ks, pages); 4528a945d10SKonstantin Belousov } 4538a945d10SKonstantin Belousov 454*0b26119bSJeff Roberson static int 455*0b26119bSJeff Roberson kstack_import(void *arg, void **store, int cnt, int domain, int flags) 4568a945d10SKonstantin Belousov { 457*0b26119bSJeff Roberson vm_object_t ksobj; 458*0b26119bSJeff Roberson int i; 4598a945d10SKonstantin Belousov 460*0b26119bSJeff Roberson for (i = 0; i < cnt; i++) { 461*0b26119bSJeff Roberson store[i] = (void *)vm_thread_stack_create( 462*0b26119bSJeff Roberson DOMAINSET_PREF(domain), &ksobj, kstack_pages); 463*0b26119bSJeff Roberson if (store[i] == NULL) 464*0b26119bSJeff Roberson break; 465*0b26119bSJeff Roberson } 466*0b26119bSJeff Roberson return (i); 467*0b26119bSJeff Roberson } 4688a945d10SKonstantin Belousov 469*0b26119bSJeff Roberson static void 470*0b26119bSJeff Roberson kstack_release(void *arg, void **store, int cnt) 471*0b26119bSJeff Roberson { 472*0b26119bSJeff Roberson vm_offset_t ks; 473*0b26119bSJeff Roberson int i; 4748a945d10SKonstantin Belousov 475*0b26119bSJeff Roberson for (i = 0; i < cnt; i++) { 476*0b26119bSJeff Roberson ks = (vm_offset_t)store[i]; 477*0b26119bSJeff Roberson vm_thread_stack_dispose( 478*0b26119bSJeff Roberson PHYS_TO_VM_PAGE(pmap_kextract(ks))->object, 479*0b26119bSJeff Roberson ks, kstack_pages); 4808a945d10SKonstantin Belousov } 4818a945d10SKonstantin Belousov } 4828a945d10SKonstantin Belousov 4838a945d10SKonstantin Belousov static void 484*0b26119bSJeff Roberson kstack_cache_init(void *null) 4858a945d10SKonstantin Belousov { 486*0b26119bSJeff Roberson kstack_cache = uma_zcache_create("kstack_cache", 487*0b26119bSJeff Roberson kstack_pages * PAGE_SIZE, NULL, NULL, NULL, NULL, 488*0b26119bSJeff Roberson kstack_import, kstack_release, NULL, 489*0b26119bSJeff Roberson UMA_ZONE_NUMA|UMA_ZONE_MINBUCKET); 490*0b26119bSJeff Roberson uma_zone_set_maxcache(kstack_cache, kstack_cache_size); 4918a945d10SKonstantin Belousov } 4928a945d10SKonstantin Belousov 4938a945d10SKonstantin Belousov SYSINIT(vm_kstacks, SI_SUB_KTHREAD_INIT, SI_ORDER_ANY, kstack_cache_init, NULL); 4948a945d10SKonstantin Belousov 495b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 496b7627840SKonstantin Belousov /* 497b7627840SKonstantin Belousov * Track maximum stack used by a thread in kernel. 498b7627840SKonstantin Belousov */ 499b7627840SKonstantin Belousov static int max_kstack_used; 500b7627840SKonstantin Belousov 501b7627840SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, max_kstack_used, CTLFLAG_RD, 502b7627840SKonstantin Belousov &max_kstack_used, 0, 503b7627840SKonstantin Belousov "Maxiumum stack depth used by a thread in kernel"); 504b7627840SKonstantin Belousov 505b7627840SKonstantin Belousov void 506b7627840SKonstantin Belousov intr_prof_stack_use(struct thread *td, struct trapframe *frame) 507b7627840SKonstantin Belousov { 508b7627840SKonstantin Belousov vm_offset_t stack_top; 509b7627840SKonstantin Belousov vm_offset_t current; 510b7627840SKonstantin Belousov int used, prev_used; 511b7627840SKonstantin Belousov 512b7627840SKonstantin Belousov /* 513b7627840SKonstantin Belousov * Testing for interrupted kernel mode isn't strictly 514b7627840SKonstantin Belousov * needed. It optimizes the execution, since interrupts from 515b7627840SKonstantin Belousov * usermode will have only the trap frame on the stack. 516b7627840SKonstantin Belousov */ 517b7627840SKonstantin Belousov if (TRAPF_USERMODE(frame)) 518b7627840SKonstantin Belousov return; 519b7627840SKonstantin Belousov 520b7627840SKonstantin Belousov stack_top = td->td_kstack + td->td_kstack_pages * PAGE_SIZE; 521b7627840SKonstantin Belousov current = (vm_offset_t)(uintptr_t)&stack_top; 522b7627840SKonstantin Belousov 523b7627840SKonstantin Belousov /* 524b7627840SKonstantin Belousov * Try to detect if interrupt is using kernel thread stack. 525b7627840SKonstantin Belousov * Hardware could use a dedicated stack for interrupt handling. 526b7627840SKonstantin Belousov */ 527b7627840SKonstantin Belousov if (stack_top <= current || current < td->td_kstack) 528b7627840SKonstantin Belousov return; 529b7627840SKonstantin Belousov 530b7627840SKonstantin Belousov used = stack_top - current; 531b7627840SKonstantin Belousov for (;;) { 532b7627840SKonstantin Belousov prev_used = max_kstack_used; 533b7627840SKonstantin Belousov if (prev_used >= used) 534b7627840SKonstantin Belousov break; 535b7627840SKonstantin Belousov if (atomic_cmpset_int(&max_kstack_used, prev_used, used)) 536b7627840SKonstantin Belousov break; 537b7627840SKonstantin Belousov } 538b7627840SKonstantin Belousov } 539b7627840SKonstantin Belousov #endif /* KSTACK_USAGE_PROF */ 540b7627840SKonstantin Belousov 541a136efe9SPeter Wemm /* 542df8bae1dSRodney W. Grimes * Implement fork's actions on an address space. 543df8bae1dSRodney W. Grimes * Here we arrange for the address space to be copied or referenced, 544df8bae1dSRodney W. Grimes * allocate a user struct (pcb and kernel stack), then call the 545df8bae1dSRodney W. Grimes * machine-dependent layer to fill those in and make the new process 546a2a1c95cSPeter Wemm * ready to run. The new process is set up so that it returns directly 547a2a1c95cSPeter Wemm * to user mode to avoid stack copying and relocation problems. 548df8bae1dSRodney W. Grimes */ 54989b57fcfSKonstantin Belousov int 550d0389015SEd Maste vm_forkproc(struct thread *td, struct proc *p2, struct thread *td2, 551d0389015SEd Maste struct vmspace *vm2, int flags) 552df8bae1dSRodney W. Grimes { 553b40ce416SJulian Elischer struct proc *p1 = td->td_proc; 55423984ce5SMark Johnston struct domainset *dset; 55589b57fcfSKonstantin Belousov int error; 556df8bae1dSRodney W. Grimes 55791c28bfdSLuoqi Chen if ((flags & RFPROC) == 0) { 55891c28bfdSLuoqi Chen /* 55991c28bfdSLuoqi Chen * Divorce the memory, if it is shared, essentially 56091c28bfdSLuoqi Chen * this changes shared memory amongst threads, into 56191c28bfdSLuoqi Chen * COW locally. 56291c28bfdSLuoqi Chen */ 56391c28bfdSLuoqi Chen if ((flags & RFMEM) == 0) { 56491c28bfdSLuoqi Chen if (p1->p_vmspace->vm_refcnt > 1) { 56589b57fcfSKonstantin Belousov error = vmspace_unshare(p1); 56689b57fcfSKonstantin Belousov if (error) 56789b57fcfSKonstantin Belousov return (error); 56891c28bfdSLuoqi Chen } 56991c28bfdSLuoqi Chen } 570079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 57189b57fcfSKonstantin Belousov return (0); 57291c28bfdSLuoqi Chen } 57391c28bfdSLuoqi Chen 5745856e12eSJohn Dyson if (flags & RFMEM) { 5755856e12eSJohn Dyson p2->p_vmspace = p1->p_vmspace; 5761a276a3fSAlan Cox atomic_add_int(&p1->p_vmspace->vm_refcnt, 1); 5775856e12eSJohn Dyson } 57823984ce5SMark Johnston dset = td2->td_domain.dr_policy; 57923984ce5SMark Johnston while (vm_page_count_severe_set(&dset->ds_mask)) { 58023984ce5SMark Johnston vm_wait_doms(&dset->ds_mask); 5810d94caffSDavid Greenman } 58226f9a767SRodney W. Grimes 5835856e12eSJohn Dyson if ((flags & RFMEM) == 0) { 58489b57fcfSKonstantin Belousov p2->p_vmspace = vm2; 585df8bae1dSRodney W. Grimes if (p1->p_vmspace->vm_shm) 586dabee6feSPeter Wemm shmfork(p1, p2); 587a2a1c95cSPeter Wemm } 588df8bae1dSRodney W. Grimes 58939fb8e6bSJulian Elischer /* 590a2a1c95cSPeter Wemm * cpu_fork will copy and update the pcb, set up the kernel stack, 591a2a1c95cSPeter Wemm * and make the child ready to run. 592df8bae1dSRodney W. Grimes */ 593079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 59489b57fcfSKonstantin Belousov return (0); 595df8bae1dSRodney W. Grimes } 596df8bae1dSRodney W. Grimes 597df8bae1dSRodney W. Grimes /* 598763df3ecSPedro F. Giffuni * Called after process has been wait(2)'ed upon and is being reaped. 599eb30c1c0SPeter Wemm * The idea is to reclaim resources that we could not reclaim while 600eb30c1c0SPeter Wemm * the process was still executing. 601eb30c1c0SPeter Wemm */ 602eb30c1c0SPeter Wemm void 603eb30c1c0SPeter Wemm vm_waitproc(p) 604eb30c1c0SPeter Wemm struct proc *p; 605eb30c1c0SPeter Wemm { 606eb30c1c0SPeter Wemm 607582ec34cSAlfred Perlstein vmspace_exitfree(p); /* and clean-out the vmspace */ 608eb30c1c0SPeter Wemm } 609eb30c1c0SPeter Wemm 61026f9a767SRodney W. Grimes void 611da7bbd2cSJohn Baldwin kick_proc0(void) 612d13ec713SStephan Uphoff { 613d13ec713SStephan Uphoff 614da7bbd2cSJohn Baldwin wakeup(&proc0); 615d13ec713SStephan Uphoff } 616