160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 17df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 18df8bae1dSRodney W. Grimes * without specific prior written permission. 19df8bae1dSRodney W. Grimes * 20df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30df8bae1dSRodney W. Grimes * SUCH DAMAGE. 31df8bae1dSRodney W. Grimes * 323c4dd356SDavid Greenman * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36df8bae1dSRodney W. Grimes * All rights reserved. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 39df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 40df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 41df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 42df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 45df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 46df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 51df8bae1dSRodney W. Grimes * School of Computer Science 52df8bae1dSRodney W. Grimes * Carnegie Mellon University 53df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 54df8bae1dSRodney W. Grimes * 55df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 56df8bae1dSRodney W. Grimes * rights to redistribute these changes. 57df8bae1dSRodney W. Grimes */ 58df8bae1dSRodney W. Grimes 59874651b1SDavid E. O'Brien #include <sys/cdefs.h> 60874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 61874651b1SDavid E. O'Brien 62faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 6315a7ad60SPeter Wemm #include "opt_kstack_pages.h" 6415a7ad60SPeter Wemm #include "opt_kstack_max_pages.h" 65e9822d92SJoerg Wunsch 66df8bae1dSRodney W. Grimes #include <sys/param.h> 67df8bae1dSRodney W. Grimes #include <sys/systm.h> 68104a9b7eSAlexander Kabaev #include <sys/limits.h> 69fb919e4dSMark Murray #include <sys/lock.h> 70fb919e4dSMark Murray #include <sys/mutex.h> 71df8bae1dSRodney W. Grimes #include <sys/proc.h> 72df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 73da61b9a6SAlan Cox #include <sys/sched.h> 74da61b9a6SAlan Cox #include <sys/sf_buf.h> 753aa12267SBruce Evans #include <sys/shm.h> 76efeaf95aSDavid Greenman #include <sys/vmmeter.h> 771005a129SJohn Baldwin #include <sys/sx.h> 78ceb0cf87SJohn Dyson #include <sys/sysctl.h> 79df8bae1dSRodney W. Grimes 8026f9a767SRodney W. Grimes #include <sys/kernel.h> 810384fff8SJason Evans #include <sys/ktr.h> 82a2a1c95cSPeter Wemm #include <sys/unistd.h> 8326f9a767SRodney W. Grimes 84df8bae1dSRodney W. Grimes #include <vm/vm.h> 85efeaf95aSDavid Greenman #include <vm/vm_param.h> 86efeaf95aSDavid Greenman #include <vm/pmap.h> 87efeaf95aSDavid Greenman #include <vm/vm_map.h> 88df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8926f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 90a136efe9SPeter Wemm #include <vm/vm_object.h> 91df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 92efeaf95aSDavid Greenman #include <vm/vm_extern.h> 93a136efe9SPeter Wemm #include <vm/vm_pager.h> 9492da00bbSMatthew Dillon #include <vm/swap_pager.h> 95efeaf95aSDavid Greenman 96ea754954SJohn Baldwin extern int maxslp; 97ea754954SJohn Baldwin 982b14f991SJulian Elischer /* 992b14f991SJulian Elischer * System initialization 1002b14f991SJulian Elischer * 1012b14f991SJulian Elischer * Note: proc0 from proc.h 1022b14f991SJulian Elischer */ 10311caded3SAlfred Perlstein static void vm_init_limits(void *); 1044590fd3aSDavid Greenman SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0) 1052b14f991SJulian Elischer 1062b14f991SJulian Elischer /* 1072b14f991SJulian Elischer * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 1082b14f991SJulian Elischer * 1092b14f991SJulian Elischer * Note: run scheduling should be divorced from the vm system. 1102b14f991SJulian Elischer */ 11111caded3SAlfred Perlstein static void scheduler(void *); 1129a44a82bSBruce Evans SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_ANY, scheduler, NULL) 1132b14f991SJulian Elischer 114e50f5c2eSBruce Evans #ifndef NO_SWAPPING 11511caded3SAlfred Perlstein static void swapout(struct proc *); 116e50f5c2eSBruce Evans #endif 117f708ef1bSPoul-Henning Kamp 118d13ec713SStephan Uphoff 119d13ec713SStephan Uphoff static volatile int proc0_rescan; 120d13ec713SStephan Uphoff 121d13ec713SStephan Uphoff 12243a90f3aSAlan Cox /* 12343a90f3aSAlan Cox * MPSAFE 1242d5c7e45SMatthew Dillon * 1252d5c7e45SMatthew Dillon * WARNING! This code calls vm_map_check_protection() which only checks 1262d5c7e45SMatthew Dillon * the associated vm_map_entry range. It does not determine whether the 1272d5c7e45SMatthew Dillon * contents of the memory is actually readable or writable. In most cases 1282d5c7e45SMatthew Dillon * just checking the vm_map_entry is sufficient within the kernel's address 1292d5c7e45SMatthew Dillon * space. 13043a90f3aSAlan Cox */ 131df8bae1dSRodney W. Grimes int 132df8bae1dSRodney W. Grimes kernacc(addr, len, rw) 133c3dfdfd1SAlfred Perlstein void *addr; 134df8bae1dSRodney W. Grimes int len, rw; 135df8bae1dSRodney W. Grimes { 136df8bae1dSRodney W. Grimes boolean_t rv; 137df8bae1dSRodney W. Grimes vm_offset_t saddr, eaddr; 13802c58685SPoul-Henning Kamp vm_prot_t prot; 139df8bae1dSRodney W. Grimes 140e50f5c2eSBruce Evans KASSERT((rw & ~VM_PROT_ALL) == 0, 14102c58685SPoul-Henning Kamp ("illegal ``rw'' argument to kernacc (%x)\n", rw)); 14275337a56SAlan Cox 14375337a56SAlan Cox if ((vm_offset_t)addr + len > kernel_map->max_offset || 14475337a56SAlan Cox (vm_offset_t)addr + len < (vm_offset_t)addr) 14575337a56SAlan Cox return (FALSE); 14675337a56SAlan Cox 14702c58685SPoul-Henning Kamp prot = rw; 1486cde7a16SDavid Greenman saddr = trunc_page((vm_offset_t)addr); 1496cde7a16SDavid Greenman eaddr = round_page((vm_offset_t)addr + len); 150d8834602SAlan Cox vm_map_lock_read(kernel_map); 151df8bae1dSRodney W. Grimes rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 152d8834602SAlan Cox vm_map_unlock_read(kernel_map); 153df8bae1dSRodney W. Grimes return (rv == TRUE); 154df8bae1dSRodney W. Grimes } 155df8bae1dSRodney W. Grimes 15643a90f3aSAlan Cox /* 15743a90f3aSAlan Cox * MPSAFE 1582d5c7e45SMatthew Dillon * 1592d5c7e45SMatthew Dillon * WARNING! This code calls vm_map_check_protection() which only checks 1602d5c7e45SMatthew Dillon * the associated vm_map_entry range. It does not determine whether the 1612d5c7e45SMatthew Dillon * contents of the memory is actually readable or writable. vmapbuf(), 1622d5c7e45SMatthew Dillon * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be 1632d5c7e45SMatthew Dillon * used in conjuction with this call. 16443a90f3aSAlan Cox */ 165df8bae1dSRodney W. Grimes int 166df8bae1dSRodney W. Grimes useracc(addr, len, rw) 167c3dfdfd1SAlfred Perlstein void *addr; 168df8bae1dSRodney W. Grimes int len, rw; 169df8bae1dSRodney W. Grimes { 170df8bae1dSRodney W. Grimes boolean_t rv; 17102c58685SPoul-Henning Kamp vm_prot_t prot; 17205ba50f5SJake Burkholder vm_map_t map; 173df8bae1dSRodney W. Grimes 174e50f5c2eSBruce Evans KASSERT((rw & ~VM_PROT_ALL) == 0, 17502c58685SPoul-Henning Kamp ("illegal ``rw'' argument to useracc (%x)\n", rw)); 17602c58685SPoul-Henning Kamp prot = rw; 17705ba50f5SJake Burkholder map = &curproc->p_vmspace->vm_map; 17805ba50f5SJake Burkholder if ((vm_offset_t)addr + len > vm_map_max(map) || 17905ba50f5SJake Burkholder (vm_offset_t)addr + len < (vm_offset_t)addr) { 18026f9a767SRodney W. Grimes return (FALSE); 18126f9a767SRodney W. Grimes } 182d8834602SAlan Cox vm_map_lock_read(map); 18305ba50f5SJake Burkholder rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr), 18405ba50f5SJake Burkholder round_page((vm_offset_t)addr + len), prot); 185d8834602SAlan Cox vm_map_unlock_read(map); 186df8bae1dSRodney W. Grimes return (rv == TRUE); 187df8bae1dSRodney W. Grimes } 188df8bae1dSRodney W. Grimes 18916929939SDon Lewis int 190f0ea4612SDon Lewis vslock(void *addr, size_t len) 19116929939SDon Lewis { 192bb734798SDon Lewis vm_offset_t end, last, start; 193bb734798SDon Lewis vm_size_t npages; 194bb734798SDon Lewis int error; 19516929939SDon Lewis 196bb734798SDon Lewis last = (vm_offset_t)addr + len; 197ce8660e3SDon Lewis start = trunc_page((vm_offset_t)addr); 198bb734798SDon Lewis end = round_page(last); 199bb734798SDon Lewis if (last < (vm_offset_t)addr || end < (vm_offset_t)addr) 20016929939SDon Lewis return (EINVAL); 20116929939SDon Lewis npages = atop(end - start); 20216929939SDon Lewis if (npages > vm_page_max_wired) 20316929939SDon Lewis return (ENOMEM); 204ce8660e3SDon Lewis PROC_LOCK(curproc); 205bb734798SDon Lewis if (ptoa(npages + 206bb734798SDon Lewis pmap_wired_count(vm_map_pmap(&curproc->p_vmspace->vm_map))) > 207bb734798SDon Lewis lim_cur(curproc, RLIMIT_MEMLOCK)) { 208ce8660e3SDon Lewis PROC_UNLOCK(curproc); 20916929939SDon Lewis return (ENOMEM); 21016929939SDon Lewis } 211ce8660e3SDon Lewis PROC_UNLOCK(curproc); 21216929939SDon Lewis #if 0 21316929939SDon Lewis /* 21416929939SDon Lewis * XXX - not yet 21516929939SDon Lewis * 21616929939SDon Lewis * The limit for transient usage of wired pages should be 21716929939SDon Lewis * larger than for "permanent" wired pages (mlock()). 21816929939SDon Lewis * 21916929939SDon Lewis * Also, the sysctl code, which is the only present user 22016929939SDon Lewis * of vslock(), does a hard loop on EAGAIN. 22116929939SDon Lewis */ 22216929939SDon Lewis if (npages + cnt.v_wire_count > vm_page_max_wired) 22316929939SDon Lewis return (EAGAIN); 22416929939SDon Lewis #endif 225ce8660e3SDon Lewis error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end, 226d9b2500eSBrian Feldman VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 227ce8660e3SDon Lewis /* 228ce8660e3SDon Lewis * Return EFAULT on error to match copy{in,out}() behaviour 229ce8660e3SDon Lewis * rather than returning ENOMEM like mlock() would. 230ce8660e3SDon Lewis */ 231ce8660e3SDon Lewis return (error == KERN_SUCCESS ? 0 : EFAULT); 23216929939SDon Lewis } 23316929939SDon Lewis 234ce8660e3SDon Lewis void 235f0ea4612SDon Lewis vsunlock(void *addr, size_t len) 23616929939SDon Lewis { 23716929939SDon Lewis 238ce8660e3SDon Lewis /* Rely on the parameter sanity checks performed by vslock(). */ 239ce8660e3SDon Lewis (void)vm_map_unwire(&curproc->p_vmspace->vm_map, 240ce8660e3SDon Lewis trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), 24116929939SDon Lewis VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 24216929939SDon Lewis } 24316929939SDon Lewis 244da61b9a6SAlan Cox /* 245da61b9a6SAlan Cox * Pin the page contained within the given object at the given offset. If the 246da61b9a6SAlan Cox * page is not resident, allocate and load it using the given object's pager. 247da61b9a6SAlan Cox * Return the pinned page if successful; otherwise, return NULL. 248da61b9a6SAlan Cox */ 249da61b9a6SAlan Cox static vm_page_t 250da61b9a6SAlan Cox vm_imgact_hold_page(vm_object_t object, vm_ooffset_t offset) 251da61b9a6SAlan Cox { 252da61b9a6SAlan Cox vm_page_t m, ma[1]; 253da61b9a6SAlan Cox vm_pindex_t pindex; 254da61b9a6SAlan Cox int rv; 255da61b9a6SAlan Cox 256da61b9a6SAlan Cox VM_OBJECT_LOCK(object); 257da61b9a6SAlan Cox pindex = OFF_TO_IDX(offset); 258da61b9a6SAlan Cox m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 259da61b9a6SAlan Cox if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 260da61b9a6SAlan Cox ma[0] = m; 261da61b9a6SAlan Cox rv = vm_pager_get_pages(object, ma, 1, 0); 262da61b9a6SAlan Cox m = vm_page_lookup(object, pindex); 263da61b9a6SAlan Cox if (m == NULL) 264da61b9a6SAlan Cox goto out; 265da61b9a6SAlan Cox if (m->valid == 0 || rv != VM_PAGER_OK) { 266da61b9a6SAlan Cox vm_page_lock_queues(); 267da61b9a6SAlan Cox vm_page_free(m); 268da61b9a6SAlan Cox vm_page_unlock_queues(); 269da61b9a6SAlan Cox m = NULL; 270da61b9a6SAlan Cox goto out; 271da61b9a6SAlan Cox } 272da61b9a6SAlan Cox } 273da61b9a6SAlan Cox vm_page_lock_queues(); 274da61b9a6SAlan Cox vm_page_hold(m); 275da61b9a6SAlan Cox vm_page_wakeup(m); 276da61b9a6SAlan Cox vm_page_unlock_queues(); 277da61b9a6SAlan Cox out: 278da61b9a6SAlan Cox VM_OBJECT_UNLOCK(object); 279da61b9a6SAlan Cox return (m); 280da61b9a6SAlan Cox } 281da61b9a6SAlan Cox 282da61b9a6SAlan Cox /* 283da61b9a6SAlan Cox * Return a CPU private mapping to the page at the given offset within the 284da61b9a6SAlan Cox * given object. The page is pinned before it is mapped. 285da61b9a6SAlan Cox */ 286da61b9a6SAlan Cox struct sf_buf * 287da61b9a6SAlan Cox vm_imgact_map_page(vm_object_t object, vm_ooffset_t offset) 288da61b9a6SAlan Cox { 289da61b9a6SAlan Cox vm_page_t m; 290da61b9a6SAlan Cox 291da61b9a6SAlan Cox m = vm_imgact_hold_page(object, offset); 292da61b9a6SAlan Cox if (m == NULL) 293da61b9a6SAlan Cox return (NULL); 294da61b9a6SAlan Cox sched_pin(); 295da61b9a6SAlan Cox return (sf_buf_alloc(m, SFB_CPUPRIVATE)); 296da61b9a6SAlan Cox } 297da61b9a6SAlan Cox 298da61b9a6SAlan Cox /* 299da61b9a6SAlan Cox * Destroy the given CPU private mapping and unpin the page that it mapped. 300da61b9a6SAlan Cox */ 301da61b9a6SAlan Cox void 302da61b9a6SAlan Cox vm_imgact_unmap_page(struct sf_buf *sf) 303da61b9a6SAlan Cox { 304da61b9a6SAlan Cox vm_page_t m; 305da61b9a6SAlan Cox 306da61b9a6SAlan Cox m = sf_buf_page(sf); 307da61b9a6SAlan Cox sf_buf_free(sf); 308da61b9a6SAlan Cox sched_unpin(); 309da61b9a6SAlan Cox vm_page_lock_queues(); 310da61b9a6SAlan Cox vm_page_unhold(m); 311da61b9a6SAlan Cox vm_page_unlock_queues(); 312da61b9a6SAlan Cox } 313da61b9a6SAlan Cox 31449a2507bSAlan Cox #ifndef KSTACK_MAX_PAGES 31549a2507bSAlan Cox #define KSTACK_MAX_PAGES 32 31649a2507bSAlan Cox #endif 31749a2507bSAlan Cox 31849a2507bSAlan Cox /* 31949a2507bSAlan Cox * Create the kernel stack (including pcb for i386) for a new thread. 32049a2507bSAlan Cox * This routine directly affects the fork perf for a process and 32149a2507bSAlan Cox * create performance for a thread. 32249a2507bSAlan Cox */ 32349a2507bSAlan Cox void 32449a2507bSAlan Cox vm_thread_new(struct thread *td, int pages) 32549a2507bSAlan Cox { 32649a2507bSAlan Cox vm_object_t ksobj; 32749a2507bSAlan Cox vm_offset_t ks; 32849a2507bSAlan Cox vm_page_t m, ma[KSTACK_MAX_PAGES]; 32949a2507bSAlan Cox int i; 33049a2507bSAlan Cox 33149a2507bSAlan Cox /* Bounds check */ 33249a2507bSAlan Cox if (pages <= 1) 33349a2507bSAlan Cox pages = KSTACK_PAGES; 33449a2507bSAlan Cox else if (pages > KSTACK_MAX_PAGES) 33549a2507bSAlan Cox pages = KSTACK_MAX_PAGES; 33649a2507bSAlan Cox /* 33749a2507bSAlan Cox * Allocate an object for the kstack. 33849a2507bSAlan Cox */ 33949a2507bSAlan Cox ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 34049a2507bSAlan Cox td->td_kstack_obj = ksobj; 34149a2507bSAlan Cox /* 34249a2507bSAlan Cox * Get a kernel virtual address for this thread's kstack. 34349a2507bSAlan Cox */ 34449a2507bSAlan Cox ks = kmem_alloc_nofault(kernel_map, 34549a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 34649a2507bSAlan Cox if (ks == 0) 34749a2507bSAlan Cox panic("vm_thread_new: kstack allocation failed"); 34849a2507bSAlan Cox if (KSTACK_GUARD_PAGES != 0) { 34949a2507bSAlan Cox pmap_qremove(ks, KSTACK_GUARD_PAGES); 35049a2507bSAlan Cox ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 35149a2507bSAlan Cox } 35249a2507bSAlan Cox td->td_kstack = ks; 35349a2507bSAlan Cox /* 35449a2507bSAlan Cox * Knowing the number of pages allocated is useful when you 35549a2507bSAlan Cox * want to deallocate them. 35649a2507bSAlan Cox */ 35749a2507bSAlan Cox td->td_kstack_pages = pages; 35849a2507bSAlan Cox /* 35949a2507bSAlan Cox * For the length of the stack, link in a real page of ram for each 36049a2507bSAlan Cox * page of stack. 36149a2507bSAlan Cox */ 36249a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 36349a2507bSAlan Cox for (i = 0; i < pages; i++) { 36449a2507bSAlan Cox /* 36549a2507bSAlan Cox * Get a kernel stack page. 36649a2507bSAlan Cox */ 367ddf4bb37SAlan Cox m = vm_page_grab(ksobj, i, VM_ALLOC_NOBUSY | 36849a2507bSAlan Cox VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED); 36949a2507bSAlan Cox ma[i] = m; 37049a2507bSAlan Cox m->valid = VM_PAGE_BITS_ALL; 37149a2507bSAlan Cox } 37249a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 37349a2507bSAlan Cox pmap_qenter(ks, ma, pages); 37449a2507bSAlan Cox } 37549a2507bSAlan Cox 37649a2507bSAlan Cox /* 37749a2507bSAlan Cox * Dispose of a thread's kernel stack. 37849a2507bSAlan Cox */ 37949a2507bSAlan Cox void 38049a2507bSAlan Cox vm_thread_dispose(struct thread *td) 38149a2507bSAlan Cox { 38249a2507bSAlan Cox vm_object_t ksobj; 38349a2507bSAlan Cox vm_offset_t ks; 38449a2507bSAlan Cox vm_page_t m; 38549a2507bSAlan Cox int i, pages; 38649a2507bSAlan Cox 38749a2507bSAlan Cox pages = td->td_kstack_pages; 38849a2507bSAlan Cox ksobj = td->td_kstack_obj; 38949a2507bSAlan Cox ks = td->td_kstack; 39049a2507bSAlan Cox pmap_qremove(ks, pages); 39149a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 39249a2507bSAlan Cox for (i = 0; i < pages; i++) { 39349a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 39449a2507bSAlan Cox if (m == NULL) 39549a2507bSAlan Cox panic("vm_thread_dispose: kstack already missing?"); 39649a2507bSAlan Cox vm_page_lock_queues(); 39749a2507bSAlan Cox vm_page_unwire(m, 0); 39849a2507bSAlan Cox vm_page_free(m); 39949a2507bSAlan Cox vm_page_unlock_queues(); 40049a2507bSAlan Cox } 40149a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 40249a2507bSAlan Cox vm_object_deallocate(ksobj); 40349a2507bSAlan Cox kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 40449a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 40549a2507bSAlan Cox } 40649a2507bSAlan Cox 40749a2507bSAlan Cox /* 40849a2507bSAlan Cox * Allow a thread's kernel stack to be paged out. 40949a2507bSAlan Cox */ 41049a2507bSAlan Cox void 41149a2507bSAlan Cox vm_thread_swapout(struct thread *td) 41249a2507bSAlan Cox { 41349a2507bSAlan Cox vm_object_t ksobj; 41449a2507bSAlan Cox vm_page_t m; 41549a2507bSAlan Cox int i, pages; 41649a2507bSAlan Cox 417710338e9SMarcel Moolenaar cpu_thread_swapout(td); 41849a2507bSAlan Cox pages = td->td_kstack_pages; 41949a2507bSAlan Cox ksobj = td->td_kstack_obj; 42049a2507bSAlan Cox pmap_qremove(td->td_kstack, pages); 42149a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 42249a2507bSAlan Cox for (i = 0; i < pages; i++) { 42349a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 42449a2507bSAlan Cox if (m == NULL) 42549a2507bSAlan Cox panic("vm_thread_swapout: kstack already missing?"); 42649a2507bSAlan Cox vm_page_lock_queues(); 42749a2507bSAlan Cox vm_page_dirty(m); 42849a2507bSAlan Cox vm_page_unwire(m, 0); 42949a2507bSAlan Cox vm_page_unlock_queues(); 43049a2507bSAlan Cox } 43149a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 43249a2507bSAlan Cox } 43349a2507bSAlan Cox 43449a2507bSAlan Cox /* 43549a2507bSAlan Cox * Bring the kernel stack for a specified thread back in. 43649a2507bSAlan Cox */ 43749a2507bSAlan Cox void 43849a2507bSAlan Cox vm_thread_swapin(struct thread *td) 43949a2507bSAlan Cox { 44049a2507bSAlan Cox vm_object_t ksobj; 44149a2507bSAlan Cox vm_page_t m, ma[KSTACK_MAX_PAGES]; 44249a2507bSAlan Cox int i, pages, rv; 44349a2507bSAlan Cox 44449a2507bSAlan Cox pages = td->td_kstack_pages; 44549a2507bSAlan Cox ksobj = td->td_kstack_obj; 44649a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 44749a2507bSAlan Cox for (i = 0; i < pages; i++) { 44849a2507bSAlan Cox m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 44949a2507bSAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 45049a2507bSAlan Cox rv = vm_pager_get_pages(ksobj, &m, 1, 0); 45149a2507bSAlan Cox if (rv != VM_PAGER_OK) 45249a2507bSAlan Cox panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid); 45349a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 45449a2507bSAlan Cox m->valid = VM_PAGE_BITS_ALL; 45549a2507bSAlan Cox } 45649a2507bSAlan Cox ma[i] = m; 45749a2507bSAlan Cox vm_page_lock_queues(); 45849a2507bSAlan Cox vm_page_wire(m); 45949a2507bSAlan Cox vm_page_wakeup(m); 46049a2507bSAlan Cox vm_page_unlock_queues(); 46149a2507bSAlan Cox } 46249a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 46349a2507bSAlan Cox pmap_qenter(td->td_kstack, ma, pages); 464710338e9SMarcel Moolenaar cpu_thread_swapin(td); 46549a2507bSAlan Cox } 46649a2507bSAlan Cox 467a136efe9SPeter Wemm /* 46889f4fca2SAlan Cox * Set up a variable-sized alternate kstack. 46989f4fca2SAlan Cox */ 47089f4fca2SAlan Cox void 47189f4fca2SAlan Cox vm_thread_new_altkstack(struct thread *td, int pages) 47289f4fca2SAlan Cox { 47389f4fca2SAlan Cox 47489f4fca2SAlan Cox td->td_altkstack = td->td_kstack; 47589f4fca2SAlan Cox td->td_altkstack_obj = td->td_kstack_obj; 47689f4fca2SAlan Cox td->td_altkstack_pages = td->td_kstack_pages; 47789f4fca2SAlan Cox 47849a2507bSAlan Cox vm_thread_new(td, pages); 47989f4fca2SAlan Cox } 48089f4fca2SAlan Cox 48189f4fca2SAlan Cox /* 48289f4fca2SAlan Cox * Restore the original kstack. 48389f4fca2SAlan Cox */ 48489f4fca2SAlan Cox void 48589f4fca2SAlan Cox vm_thread_dispose_altkstack(struct thread *td) 48689f4fca2SAlan Cox { 48789f4fca2SAlan Cox 48849a2507bSAlan Cox vm_thread_dispose(td); 48989f4fca2SAlan Cox 49089f4fca2SAlan Cox td->td_kstack = td->td_altkstack; 49189f4fca2SAlan Cox td->td_kstack_obj = td->td_altkstack_obj; 49289f4fca2SAlan Cox td->td_kstack_pages = td->td_altkstack_pages; 49389f4fca2SAlan Cox td->td_altkstack = 0; 49489f4fca2SAlan Cox td->td_altkstack_obj = NULL; 49589f4fca2SAlan Cox td->td_altkstack_pages = 0; 49689f4fca2SAlan Cox } 49789f4fca2SAlan Cox 49889f4fca2SAlan Cox /* 499df8bae1dSRodney W. Grimes * Implement fork's actions on an address space. 500df8bae1dSRodney W. Grimes * Here we arrange for the address space to be copied or referenced, 501df8bae1dSRodney W. Grimes * allocate a user struct (pcb and kernel stack), then call the 502df8bae1dSRodney W. Grimes * machine-dependent layer to fill those in and make the new process 503a2a1c95cSPeter Wemm * ready to run. The new process is set up so that it returns directly 504a2a1c95cSPeter Wemm * to user mode to avoid stack copying and relocation problems. 505df8bae1dSRodney W. Grimes */ 506a2a1c95cSPeter Wemm void 507079b7badSJulian Elischer vm_forkproc(td, p2, td2, flags) 508b40ce416SJulian Elischer struct thread *td; 509b40ce416SJulian Elischer struct proc *p2; 510079b7badSJulian Elischer struct thread *td2; 511a2a1c95cSPeter Wemm int flags; 512df8bae1dSRodney W. Grimes { 513b40ce416SJulian Elischer struct proc *p1 = td->td_proc; 514df8bae1dSRodney W. Grimes 51591c28bfdSLuoqi Chen if ((flags & RFPROC) == 0) { 51691c28bfdSLuoqi Chen /* 51791c28bfdSLuoqi Chen * Divorce the memory, if it is shared, essentially 51891c28bfdSLuoqi Chen * this changes shared memory amongst threads, into 51991c28bfdSLuoqi Chen * COW locally. 52091c28bfdSLuoqi Chen */ 52191c28bfdSLuoqi Chen if ((flags & RFMEM) == 0) { 52291c28bfdSLuoqi Chen if (p1->p_vmspace->vm_refcnt > 1) { 52391c28bfdSLuoqi Chen vmspace_unshare(p1); 52491c28bfdSLuoqi Chen } 52591c28bfdSLuoqi Chen } 526079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 52791c28bfdSLuoqi Chen return; 52891c28bfdSLuoqi Chen } 52991c28bfdSLuoqi Chen 5305856e12eSJohn Dyson if (flags & RFMEM) { 5315856e12eSJohn Dyson p2->p_vmspace = p1->p_vmspace; 5321a276a3fSAlan Cox atomic_add_int(&p1->p_vmspace->vm_refcnt, 1); 5335856e12eSJohn Dyson } 5345856e12eSJohn Dyson 53590ecac61SMatthew Dillon while (vm_page_count_severe()) { 53626f9a767SRodney W. Grimes VM_WAIT; 5370d94caffSDavid Greenman } 53826f9a767SRodney W. Grimes 5395856e12eSJohn Dyson if ((flags & RFMEM) == 0) { 540df8bae1dSRodney W. Grimes p2->p_vmspace = vmspace_fork(p1->p_vmspace); 541df8bae1dSRodney W. Grimes if (p1->p_vmspace->vm_shm) 542dabee6feSPeter Wemm shmfork(p1, p2); 543a2a1c95cSPeter Wemm } 544df8bae1dSRodney W. Grimes 54539fb8e6bSJulian Elischer /* 546a2a1c95cSPeter Wemm * cpu_fork will copy and update the pcb, set up the kernel stack, 547a2a1c95cSPeter Wemm * and make the child ready to run. 548df8bae1dSRodney W. Grimes */ 549079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes 552df8bae1dSRodney W. Grimes /* 553eb30c1c0SPeter Wemm * Called after process has been wait(2)'ed apon and is being reaped. 554eb30c1c0SPeter Wemm * The idea is to reclaim resources that we could not reclaim while 555eb30c1c0SPeter Wemm * the process was still executing. 556eb30c1c0SPeter Wemm */ 557eb30c1c0SPeter Wemm void 558eb30c1c0SPeter Wemm vm_waitproc(p) 559eb30c1c0SPeter Wemm struct proc *p; 560eb30c1c0SPeter Wemm { 561eb30c1c0SPeter Wemm 562582ec34cSAlfred Perlstein vmspace_exitfree(p); /* and clean-out the vmspace */ 563eb30c1c0SPeter Wemm } 564eb30c1c0SPeter Wemm 565eb30c1c0SPeter Wemm /* 566df8bae1dSRodney W. Grimes * Set default limits for VM system. 567df8bae1dSRodney W. Grimes * Called for proc 0, and then inherited by all others. 5682b14f991SJulian Elischer * 5692b14f991SJulian Elischer * XXX should probably act directly on proc0. 570df8bae1dSRodney W. Grimes */ 5712b14f991SJulian Elischer static void 5722b14f991SJulian Elischer vm_init_limits(udata) 5734590fd3aSDavid Greenman void *udata; 574df8bae1dSRodney W. Grimes { 57554d92145SMatthew Dillon struct proc *p = udata; 57691d5354aSJohn Baldwin struct plimit *limp; 577bbc0ec52SDavid Greenman int rss_limit; 578df8bae1dSRodney W. Grimes 579df8bae1dSRodney W. Grimes /* 5800d94caffSDavid Greenman * Set up the initial limits on process VM. Set the maximum resident 5810d94caffSDavid Greenman * set size to be half of (reasonably) available memory. Since this 5820d94caffSDavid Greenman * is a soft limit, it comes into effect only when the system is out 5830d94caffSDavid Greenman * of memory - half of main memory helps to favor smaller processes, 584bbc0ec52SDavid Greenman * and reduces thrashing of the object cache. 585df8bae1dSRodney W. Grimes */ 58691d5354aSJohn Baldwin limp = p->p_limit; 58791d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 58891d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 58991d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 59091d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 591dd0bd066SDavid Greenman /* limit the limit to no less than 2MB */ 592f2daac0cSDavid Greenman rss_limit = max(cnt.v_free_count, 512); 59391d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 59491d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 595df8bae1dSRodney W. Grimes } 596df8bae1dSRodney W. Grimes 59726f9a767SRodney W. Grimes void 59826f9a767SRodney W. Grimes faultin(p) 59926f9a767SRodney W. Grimes struct proc *p; 60026f9a767SRodney W. Grimes { 60111edc1e0SJohn Baldwin #ifdef NO_SWAPPING 60211edc1e0SJohn Baldwin 60311edc1e0SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 60411edc1e0SJohn Baldwin if ((p->p_sflag & PS_INMEM) == 0) 60511edc1e0SJohn Baldwin panic("faultin: proc swapped out with NO_SWAPPING!"); 60611edc1e0SJohn Baldwin #else /* !NO_SWAPPING */ 607664f718bSJohn Baldwin struct thread *td; 60826f9a767SRodney W. Grimes 609c96d52a9SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 6101d7b9ed2SJulian Elischer /* 6111d7b9ed2SJulian Elischer * If another process is swapping in this process, 6121d7b9ed2SJulian Elischer * just wait until it finishes. 6131d7b9ed2SJulian Elischer */ 614664f718bSJohn Baldwin if (p->p_sflag & PS_SWAPPINGIN) 6151d7b9ed2SJulian Elischer msleep(&p->p_sflag, &p->p_mtx, PVM, "faultin", 0); 616664f718bSJohn Baldwin else if ((p->p_sflag & PS_INMEM) == 0) { 617664f718bSJohn Baldwin /* 618664f718bSJohn Baldwin * Don't let another thread swap process p out while we are 619664f718bSJohn Baldwin * busy swapping it in. 620664f718bSJohn Baldwin */ 621664f718bSJohn Baldwin ++p->p_lock; 6221d7b9ed2SJulian Elischer mtx_lock_spin(&sched_lock); 6231d7b9ed2SJulian Elischer p->p_sflag |= PS_SWAPPINGIN; 6249ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 62545ece682SJohn Baldwin PROC_UNLOCK(p); 62626f9a767SRodney W. Grimes 627664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) 62849a2507bSAlan Cox vm_thread_swapin(td); 62926f9a767SRodney W. Grimes 63045ece682SJohn Baldwin PROC_LOCK(p); 6319ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 6329eb881f8SSeigo Tanimura p->p_sflag &= ~PS_SWAPPINGIN; 6339eb881f8SSeigo Tanimura p->p_sflag |= PS_INMEM; 634664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) { 635664f718bSJohn Baldwin TD_CLR_SWAPPED(td); 63671fad9fdSJulian Elischer if (TD_CAN_RUN(td)) 63771fad9fdSJulian Elischer setrunnable(td); 638664f718bSJohn Baldwin } 639664f718bSJohn Baldwin mtx_unlock_spin(&sched_lock); 64026f9a767SRodney W. Grimes 6411d7b9ed2SJulian Elischer wakeup(&p->p_sflag); 64226f9a767SRodney W. Grimes 643664f718bSJohn Baldwin /* Allow other threads to swap p out now. */ 64426f9a767SRodney W. Grimes --p->p_lock; 64526f9a767SRodney W. Grimes } 64611edc1e0SJohn Baldwin #endif /* NO_SWAPPING */ 64726f9a767SRodney W. Grimes } 64826f9a767SRodney W. Grimes 649df8bae1dSRodney W. Grimes /* 65026f9a767SRodney W. Grimes * This swapin algorithm attempts to swap-in processes only if there 65126f9a767SRodney W. Grimes * is enough space for them. Of course, if a process waits for a long 65226f9a767SRodney W. Grimes * time, it will be swapped in anyway. 6530384fff8SJason Evans * 654e602ba25SJulian Elischer * XXXKSE - process with the thread with highest priority counts.. 655b40ce416SJulian Elischer * 65610c447faSAlan Cox * Giant is held on entry. 657df8bae1dSRodney W. Grimes */ 6582b14f991SJulian Elischer /* ARGSUSED*/ 6592b14f991SJulian Elischer static void 660d841aaa7SBruce Evans scheduler(dummy) 661d841aaa7SBruce Evans void *dummy; 662df8bae1dSRodney W. Grimes { 66354d92145SMatthew Dillon struct proc *p; 664e602ba25SJulian Elischer struct thread *td; 66554d92145SMatthew Dillon int pri; 666df8bae1dSRodney W. Grimes struct proc *pp; 667df8bae1dSRodney W. Grimes int ppri; 668df8bae1dSRodney W. Grimes 669c96d52a9SJohn Baldwin mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 67010c447faSAlan Cox mtx_unlock(&Giant); 6710384fff8SJason Evans 672df8bae1dSRodney W. Grimes loop: 67390ecac61SMatthew Dillon if (vm_page_count_min()) { 6740d94caffSDavid Greenman VM_WAIT; 675d13ec713SStephan Uphoff mtx_lock_spin(&sched_lock); 676d13ec713SStephan Uphoff proc0_rescan = 0; 677d13ec713SStephan Uphoff mtx_unlock_spin(&sched_lock); 67890ecac61SMatthew Dillon goto loop; 6790d94caffSDavid Greenman } 68026f9a767SRodney W. Grimes 681df8bae1dSRodney W. Grimes pp = NULL; 682df8bae1dSRodney W. Grimes ppri = INT_MIN; 6831005a129SJohn Baldwin sx_slock(&allproc_lock); 684b40ce416SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 685b40ce416SJulian Elischer struct ksegrp *kg; 686664f718bSJohn Baldwin if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) { 687e602ba25SJulian Elischer continue; 688e602ba25SJulian Elischer } 6899ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 690e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 6911d7b9ed2SJulian Elischer /* 69271fad9fdSJulian Elischer * An otherwise runnable thread of a process 69371fad9fdSJulian Elischer * swapped out has only the TDI_SWAPPED bit set. 69471fad9fdSJulian Elischer * 6951d7b9ed2SJulian Elischer */ 69671fad9fdSJulian Elischer if (td->td_inhibitors == TDI_SWAPPED) { 697e602ba25SJulian Elischer kg = td->td_ksegrp; 698b40ce416SJulian Elischer pri = p->p_swtime + kg->kg_slptime; 6995074aecdSJohn Baldwin if ((p->p_sflag & PS_SWAPINREQ) == 0) { 700fa885116SJulian Elischer pri -= p->p_nice * 8; 701a669a6e9SJohn Dyson } 70295461b45SJohn Dyson 70326f9a767SRodney W. Grimes /* 704b40ce416SJulian Elischer * if this ksegrp is higher priority 705b40ce416SJulian Elischer * and there is enough space, then select 706b40ce416SJulian Elischer * this process instead of the previous 707b40ce416SJulian Elischer * selection. 70826f9a767SRodney W. Grimes */ 7090d94caffSDavid Greenman if (pri > ppri) { 710df8bae1dSRodney W. Grimes pp = p; 711df8bae1dSRodney W. Grimes ppri = pri; 712df8bae1dSRodney W. Grimes } 713df8bae1dSRodney W. Grimes } 714b40ce416SJulian Elischer } 7159ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 716df8bae1dSRodney W. Grimes } 7171005a129SJohn Baldwin sx_sunlock(&allproc_lock); 71826f9a767SRodney W. Grimes 719df8bae1dSRodney W. Grimes /* 720a669a6e9SJohn Dyson * Nothing to do, back to sleep. 721df8bae1dSRodney W. Grimes */ 722df8bae1dSRodney W. Grimes if ((p = pp) == NULL) { 723d13ec713SStephan Uphoff mtx_lock_spin(&sched_lock); 724d13ec713SStephan Uphoff if (!proc0_rescan) { 725d13ec713SStephan Uphoff TD_SET_IWAIT(&thread0); 726d13ec713SStephan Uphoff mi_switch(SW_VOL, NULL); 727d13ec713SStephan Uphoff } 728d13ec713SStephan Uphoff proc0_rescan = 0; 729d13ec713SStephan Uphoff mtx_unlock_spin(&sched_lock); 730df8bae1dSRodney W. Grimes goto loop; 731df8bae1dSRodney W. Grimes } 7321d7b9ed2SJulian Elischer PROC_LOCK(p); 7331d7b9ed2SJulian Elischer 7341d7b9ed2SJulian Elischer /* 7351d7b9ed2SJulian Elischer * Another process may be bringing or may have already 7361d7b9ed2SJulian Elischer * brought this process in while we traverse all threads. 7371d7b9ed2SJulian Elischer * Or, this process may even be being swapped out again. 7381d7b9ed2SJulian Elischer */ 739664f718bSJohn Baldwin if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) { 7401d7b9ed2SJulian Elischer PROC_UNLOCK(p); 741d13ec713SStephan Uphoff mtx_lock_spin(&sched_lock); 742d13ec713SStephan Uphoff proc0_rescan = 0; 743d13ec713SStephan Uphoff mtx_unlock_spin(&sched_lock); 7441d7b9ed2SJulian Elischer goto loop; 7451d7b9ed2SJulian Elischer } 7461d7b9ed2SJulian Elischer 747664f718bSJohn Baldwin mtx_lock_spin(&sched_lock); 7481d7b9ed2SJulian Elischer p->p_sflag &= ~PS_SWAPINREQ; 749664f718bSJohn Baldwin mtx_unlock_spin(&sched_lock); 750a669a6e9SJohn Dyson 751df8bae1dSRodney W. Grimes /* 75226f9a767SRodney W. Grimes * We would like to bring someone in. (only if there is space). 753e602ba25SJulian Elischer * [What checks the space? ] 754df8bae1dSRodney W. Grimes */ 75526f9a767SRodney W. Grimes faultin(p); 75645ece682SJohn Baldwin PROC_UNLOCK(p); 757664f718bSJohn Baldwin mtx_lock_spin(&sched_lock); 758df8bae1dSRodney W. Grimes p->p_swtime = 0; 759d13ec713SStephan Uphoff proc0_rescan = 0; 7609ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 761df8bae1dSRodney W. Grimes goto loop; 762df8bae1dSRodney W. Grimes } 763df8bae1dSRodney W. Grimes 764d13ec713SStephan Uphoff void kick_proc0(void) 765d13ec713SStephan Uphoff { 766d13ec713SStephan Uphoff struct thread *td = &thread0; 767d13ec713SStephan Uphoff 768d13ec713SStephan Uphoff 769d13ec713SStephan Uphoff if (TD_AWAITING_INTR(td)) { 770d13ec713SStephan Uphoff CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, 0); 771d13ec713SStephan Uphoff TD_CLR_IWAIT(td); 772d13ec713SStephan Uphoff setrunqueue(td, SRQ_INTR); 773d13ec713SStephan Uphoff } else { 774d13ec713SStephan Uphoff proc0_rescan = 1; 775d13ec713SStephan Uphoff CTR2(KTR_INTR, "%s: state %d", 776d13ec713SStephan Uphoff __func__, td->td_state); 777d13ec713SStephan Uphoff } 778d13ec713SStephan Uphoff 779d13ec713SStephan Uphoff } 780d13ec713SStephan Uphoff 781d13ec713SStephan Uphoff 7825afce282SDavid Greenman #ifndef NO_SWAPPING 7835afce282SDavid Greenman 784ceb0cf87SJohn Dyson /* 785ceb0cf87SJohn Dyson * Swap_idle_threshold1 is the guaranteed swapped in time for a process 786ceb0cf87SJohn Dyson */ 787303b270bSEivind Eklund static int swap_idle_threshold1 = 2; 7882a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW, 7899faaf3b3STom Rhodes &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process"); 790ceb0cf87SJohn Dyson 791ceb0cf87SJohn Dyson /* 792ceb0cf87SJohn Dyson * Swap_idle_threshold2 is the time that a process can be idle before 793ceb0cf87SJohn Dyson * it will be swapped out, if idle swapping is enabled. 794ceb0cf87SJohn Dyson */ 795303b270bSEivind Eklund static int swap_idle_threshold2 = 10; 7962a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW, 7979faaf3b3STom Rhodes &swap_idle_threshold2, 0, "Time before a process will be swapped out"); 798ceb0cf87SJohn Dyson 799df8bae1dSRodney W. Grimes /* 800df8bae1dSRodney W. Grimes * Swapout is driven by the pageout daemon. Very simple, we find eligible 801df8bae1dSRodney W. Grimes * procs and unwire their u-areas. We try to always "swap" at least one 802df8bae1dSRodney W. Grimes * process in case we need the room for a swapin. 803df8bae1dSRodney W. Grimes * If any procs have been sleeping/stopped for at least maxslp seconds, 804df8bae1dSRodney W. Grimes * they are swapped. Else, we swap the longest-sleeping or stopped process, 805df8bae1dSRodney W. Grimes * if any, otherwise the longest-resident process. 806df8bae1dSRodney W. Grimes */ 807df8bae1dSRodney W. Grimes void 8083a2dc656SJohn Dyson swapout_procs(action) 8093a2dc656SJohn Dyson int action; 810df8bae1dSRodney W. Grimes { 81154d92145SMatthew Dillon struct proc *p; 812e602ba25SJulian Elischer struct thread *td; 813b40ce416SJulian Elischer struct ksegrp *kg; 814df8bae1dSRodney W. Grimes int didswap = 0; 815df8bae1dSRodney W. Grimes 8160d94caffSDavid Greenman retry: 8173a2189d4SJohn Baldwin sx_slock(&allproc_lock); 818e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 819b18bfc3dSJohn Dyson struct vmspace *vm; 820b40ce416SJulian Elischer int minslptime = 100000; 821b18bfc3dSJohn Dyson 8229eb881f8SSeigo Tanimura /* 823b1f99ebeSSeigo Tanimura * Watch out for a process in 824b1f99ebeSSeigo Tanimura * creation. It may have no 8251c865ac7SJohn Baldwin * address space or lock yet. 8261c865ac7SJohn Baldwin */ 8271c865ac7SJohn Baldwin mtx_lock_spin(&sched_lock); 8281c865ac7SJohn Baldwin if (p->p_state == PRS_NEW) { 8291c865ac7SJohn Baldwin mtx_unlock_spin(&sched_lock); 8301c865ac7SJohn Baldwin continue; 8311c865ac7SJohn Baldwin } 8321c865ac7SJohn Baldwin mtx_unlock_spin(&sched_lock); 8331c865ac7SJohn Baldwin 8341c865ac7SJohn Baldwin /* 835b1f99ebeSSeigo Tanimura * An aio daemon switches its 836b1f99ebeSSeigo Tanimura * address space while running. 837b1f99ebeSSeigo Tanimura * Perform a quick check whether 838b1f99ebeSSeigo Tanimura * a process has P_SYSTEM. 8399eb881f8SSeigo Tanimura */ 8408f887403SJohn Baldwin if ((p->p_flag & P_SYSTEM) != 0) 841b1f99ebeSSeigo Tanimura continue; 8421c865ac7SJohn Baldwin 8431c865ac7SJohn Baldwin /* 8441c865ac7SJohn Baldwin * Do not swapout a process that 8451c865ac7SJohn Baldwin * is waiting for VM data 8461c865ac7SJohn Baldwin * structures as there is a possible 8471c865ac7SJohn Baldwin * deadlock. Test this first as 8481c865ac7SJohn Baldwin * this may block. 8491c865ac7SJohn Baldwin * 8501c865ac7SJohn Baldwin * Lock the map until swapout 8511c865ac7SJohn Baldwin * finishes, or a thread of this 8521c865ac7SJohn Baldwin * process may attempt to alter 8531c865ac7SJohn Baldwin * the map. 8541c865ac7SJohn Baldwin */ 8558f887403SJohn Baldwin PROC_LOCK(p); 8569eb881f8SSeigo Tanimura vm = p->p_vmspace; 857b1f99ebeSSeigo Tanimura KASSERT(vm != NULL, 858b1f99ebeSSeigo Tanimura ("swapout_procs: a process has no address space")); 8591a276a3fSAlan Cox atomic_add_int(&vm->vm_refcnt, 1); 860b1f99ebeSSeigo Tanimura PROC_UNLOCK(p); 8619eb881f8SSeigo Tanimura if (!vm_map_trylock(&vm->vm_map)) 8629eb881f8SSeigo Tanimura goto nextproc1; 8639eb881f8SSeigo Tanimura 8645074aecdSJohn Baldwin PROC_LOCK(p); 86569b40456SJohn Baldwin if (p->p_lock != 0 || 8661279572aSDavid Xu (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT) 8671279572aSDavid Xu ) != 0) { 8689eb881f8SSeigo Tanimura goto nextproc2; 8695074aecdSJohn Baldwin } 87023955314SAlfred Perlstein /* 87123955314SAlfred Perlstein * only aiod changes vmspace, however it will be 87223955314SAlfred Perlstein * skipped because of the if statement above checking 87323955314SAlfred Perlstein * for P_SYSTEM 87423955314SAlfred Perlstein */ 875664f718bSJohn Baldwin if ((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) != PS_INMEM) 876664f718bSJohn Baldwin goto nextproc2; 87769b40456SJohn Baldwin 878e602ba25SJulian Elischer switch (p->p_state) { 8790d94caffSDavid Greenman default: 880e602ba25SJulian Elischer /* Don't swap out processes in any sort 881e602ba25SJulian Elischer * of 'special' state. */ 8828f887403SJohn Baldwin break; 883df8bae1dSRodney W. Grimes 884e602ba25SJulian Elischer case PRS_NORMAL: 8858f887403SJohn Baldwin mtx_lock_spin(&sched_lock); 88626f9a767SRodney W. Grimes /* 887bfbfac11SDavid Greenman * do not swapout a realtime process 888b40ce416SJulian Elischer * Check all the thread groups.. 889bfbfac11SDavid Greenman */ 890b40ce416SJulian Elischer FOREACH_KSEGRP_IN_PROC(p, kg) { 8919eb881f8SSeigo Tanimura if (PRI_IS_REALTIME(kg->kg_pri_class)) 892b40ce416SJulian Elischer goto nextproc; 893bfbfac11SDavid Greenman 894bfbfac11SDavid Greenman /* 8959eb881f8SSeigo Tanimura * Guarantee swap_idle_threshold1 896ceb0cf87SJohn Dyson * time in memory. 8970d94caffSDavid Greenman */ 8989eb881f8SSeigo Tanimura if (kg->kg_slptime < swap_idle_threshold1) 899b40ce416SJulian Elischer goto nextproc; 9009eb881f8SSeigo Tanimura 9011d7b9ed2SJulian Elischer /* 9029eb881f8SSeigo Tanimura * Do not swapout a process if it is 9039eb881f8SSeigo Tanimura * waiting on a critical event of some 9049eb881f8SSeigo Tanimura * kind or there is a thread whose 9059eb881f8SSeigo Tanimura * pageable memory may be accessed. 9061d7b9ed2SJulian Elischer * 9071d7b9ed2SJulian Elischer * This could be refined to support 9081d7b9ed2SJulian Elischer * swapping out a thread. 9091d7b9ed2SJulian Elischer */ 9109eb881f8SSeigo Tanimura FOREACH_THREAD_IN_GROUP(kg, td) { 9111d7b9ed2SJulian Elischer if ((td->td_priority) < PSOCK || 9129eb881f8SSeigo Tanimura !thread_safetoswapout(td)) 913e602ba25SJulian Elischer goto nextproc; 914e602ba25SJulian Elischer } 915ceb0cf87SJohn Dyson /* 916b40ce416SJulian Elischer * If the system is under memory stress, 917b40ce416SJulian Elischer * or if we are swapping 918b40ce416SJulian Elischer * idle processes >= swap_idle_threshold2, 919b40ce416SJulian Elischer * then swap the process out. 920ceb0cf87SJohn Dyson */ 921ceb0cf87SJohn Dyson if (((action & VM_SWAP_NORMAL) == 0) && 922ceb0cf87SJohn Dyson (((action & VM_SWAP_IDLE) == 0) || 9239eb881f8SSeigo Tanimura (kg->kg_slptime < swap_idle_threshold2))) 924b40ce416SJulian Elischer goto nextproc; 9259eb881f8SSeigo Tanimura 926b40ce416SJulian Elischer if (minslptime > kg->kg_slptime) 927b40ce416SJulian Elischer minslptime = kg->kg_slptime; 928b40ce416SJulian Elischer } 9290d94caffSDavid Greenman 93011b224dcSDavid Greenman /* 93117d9d0d0SDavid Schultz * If the pageout daemon didn't free enough pages, 93217d9d0d0SDavid Schultz * or if this process is idle and the system is 93317d9d0d0SDavid Schultz * configured to swap proactively, swap it out. 93411b224dcSDavid Greenman */ 935ceb0cf87SJohn Dyson if ((action & VM_SWAP_NORMAL) || 936ceb0cf87SJohn Dyson ((action & VM_SWAP_IDLE) && 937b40ce416SJulian Elischer (minslptime > swap_idle_threshold2))) { 938df8bae1dSRodney W. Grimes swapout(p); 939df8bae1dSRodney W. Grimes didswap++; 9409eb881f8SSeigo Tanimura mtx_unlock_spin(&sched_lock); 941664f718bSJohn Baldwin PROC_UNLOCK(p); 9429eb881f8SSeigo Tanimura vm_map_unlock(&vm->vm_map); 9439eb881f8SSeigo Tanimura vmspace_free(vm); 9449eb881f8SSeigo Tanimura sx_sunlock(&allproc_lock); 9450d94caffSDavid Greenman goto retry; 946c96d52a9SJohn Baldwin } 947b40ce416SJulian Elischer nextproc: 9489eb881f8SSeigo Tanimura mtx_unlock_spin(&sched_lock); 9498f887403SJohn Baldwin } 9509eb881f8SSeigo Tanimura nextproc2: 9519eb881f8SSeigo Tanimura PROC_UNLOCK(p); 9529eb881f8SSeigo Tanimura vm_map_unlock(&vm->vm_map); 9539eb881f8SSeigo Tanimura nextproc1: 9549eb881f8SSeigo Tanimura vmspace_free(vm); 95530171114SPeter Wemm continue; 956ceb0cf87SJohn Dyson } 9571005a129SJohn Baldwin sx_sunlock(&allproc_lock); 95826f9a767SRodney W. Grimes /* 95926f9a767SRodney W. Grimes * If we swapped something out, and another process needed memory, 96026f9a767SRodney W. Grimes * then wakeup the sched process. 96126f9a767SRodney W. Grimes */ 9620d94caffSDavid Greenman if (didswap) 96324a1cce3SDavid Greenman wakeup(&proc0); 964df8bae1dSRodney W. Grimes } 965df8bae1dSRodney W. Grimes 966f708ef1bSPoul-Henning Kamp static void 967df8bae1dSRodney W. Grimes swapout(p) 96854d92145SMatthew Dillon struct proc *p; 969df8bae1dSRodney W. Grimes { 970b40ce416SJulian Elischer struct thread *td; 971df8bae1dSRodney W. Grimes 972ea754954SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 9739eb881f8SSeigo Tanimura mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); 974d3a34985SJohn Dyson #if defined(SWAP_DEBUG) 975d3a34985SJohn Dyson printf("swapping out %d\n", p->p_pid); 976d3a34985SJohn Dyson #endif 9771d7b9ed2SJulian Elischer 9781d7b9ed2SJulian Elischer /* 9799eb881f8SSeigo Tanimura * The states of this process and its threads may have changed 9809eb881f8SSeigo Tanimura * by now. Assuming that there is only one pageout daemon thread, 9819eb881f8SSeigo Tanimura * this process should still be in memory. 9829eb881f8SSeigo Tanimura */ 983664f718bSJohn Baldwin KASSERT((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) == PS_INMEM, 9849eb881f8SSeigo Tanimura ("swapout: lost a swapout race?")); 9859eb881f8SSeigo Tanimura 9869eb881f8SSeigo Tanimura #if defined(INVARIANTS) 9879eb881f8SSeigo Tanimura /* 9881d7b9ed2SJulian Elischer * Make sure that all threads are safe to be swapped out. 9891d7b9ed2SJulian Elischer * 9901d7b9ed2SJulian Elischer * Alternatively, we could swap out only safe threads. 9911d7b9ed2SJulian Elischer */ 9921d7b9ed2SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 9939eb881f8SSeigo Tanimura KASSERT(thread_safetoswapout(td), 9949eb881f8SSeigo Tanimura ("swapout: there is a thread not safe for swapout")); 9951d7b9ed2SJulian Elischer } 9969eb881f8SSeigo Tanimura #endif /* INVARIANTS */ 9971d7b9ed2SJulian Elischer 99826f9a767SRodney W. Grimes ++p->p_stats->p_ru.ru_nswap; 999df8bae1dSRodney W. Grimes /* 100026f9a767SRodney W. Grimes * remember the process resident count 1001df8bae1dSRodney W. Grimes */ 1002b1028ad1SLuoqi Chen p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 1003df8bae1dSRodney W. Grimes 10049eb881f8SSeigo Tanimura p->p_sflag &= ~PS_INMEM; 1005664f718bSJohn Baldwin p->p_sflag |= PS_SWAPPINGOUT; 1006664f718bSJohn Baldwin PROC_UNLOCK(p); 1007664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) 1008664f718bSJohn Baldwin TD_SET_SWAPPED(td); 10099ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 101026f9a767SRodney W. Grimes 1011664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) 101249a2507bSAlan Cox vm_thread_swapout(td); 1013664f718bSJohn Baldwin 1014664f718bSJohn Baldwin PROC_LOCK(p); 10159ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1016664f718bSJohn Baldwin p->p_sflag &= ~PS_SWAPPINGOUT; 1017df8bae1dSRodney W. Grimes p->p_swtime = 0; 1018df8bae1dSRodney W. Grimes } 10195afce282SDavid Greenman #endif /* !NO_SWAPPING */ 1020