1 /* $OpenBSD: uvm_glue.c,v 1.49 2009/03/20 15:19:04 oga Exp $ */ 2 /* $NetBSD: uvm_glue.c,v 1.44 2001/02/06 19:54:44 eeh Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993, The Regents of the University of California. 7 * 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Charles D. Cranor, 24 * Washington University, the University of California, Berkeley and 25 * its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 43 * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp 44 * 45 * 46 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 47 * All rights reserved. 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70 /* 71 * uvm_glue.c: glue functions 72 */ 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/proc.h> 77 #include <sys/resourcevar.h> 78 #include <sys/buf.h> 79 #include <sys/user.h> 80 #ifdef SYSVSHM 81 #include <sys/shm.h> 82 #endif 83 #include <sys/sched.h> 84 85 #include <uvm/uvm.h> 86 87 #include <machine/cpu.h> 88 89 /* 90 * XXXCDC: do these really belong here? 91 */ 92 93 int readbuffers = 0; /* allow KGDB to read kern buffer pool */ 94 /* XXX: see uvm_kernacc */ 95 96 97 /* 98 * uvm_kernacc: can the kernel access a region of memory 99 * 100 * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c) 101 */ 102 103 boolean_t 104 uvm_kernacc(caddr_t addr, size_t len, int rw) 105 { 106 boolean_t rv; 107 vaddr_t saddr, eaddr; 108 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 109 110 saddr = trunc_page((vaddr_t)addr); 111 eaddr = round_page((vaddr_t)addr + len); 112 vm_map_lock_read(kernel_map); 113 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot); 114 vm_map_unlock_read(kernel_map); 115 116 return(rv); 117 } 118 119 #ifdef KGDB 120 /* 121 * Change protections on kernel pages from addr to addr+len 122 * (presumably so debugger can plant a breakpoint). 123 * 124 * We force the protection change at the pmap level. If we were 125 * to use vm_map_protect a change to allow writing would be lazily- 126 * applied meaning we would still take a protection fault, something 127 * we really don't want to do. It would also fragment the kernel 128 * map unnecessarily. We cannot use pmap_protect since it also won't 129 * enforce a write-enable request. Using pmap_enter is the only way 130 * we can ensure the change takes place properly. 131 */ 132 void 133 uvm_chgkprot(caddr_t addr, size_t len, int rw) 134 { 135 vm_prot_t prot; 136 paddr_t pa; 137 vaddr_t sva, eva; 138 139 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE; 140 eva = round_page((vaddr_t)addr + len); 141 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) { 142 /* 143 * Extract physical address for the page. 144 * We use a cheezy hack to differentiate physical 145 * page 0 from an invalid mapping, not that it 146 * really matters... 147 */ 148 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE) 149 panic("chgkprot: invalid page"); 150 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 151 } 152 pmap_update(pmap_kernel()); 153 } 154 #endif 155 156 /* 157 * uvm_vslock: wire user memory for I/O 158 * 159 * - called from physio and sys___sysctl 160 * - XXXCDC: consider nuking this (or making it a macro?) 161 */ 162 163 int 164 uvm_vslock(struct proc *p, caddr_t addr, size_t len, vm_prot_t access_type) 165 { 166 vm_map_t map; 167 vaddr_t start, end; 168 int rv; 169 170 map = &p->p_vmspace->vm_map; 171 start = trunc_page((vaddr_t)addr); 172 end = round_page((vaddr_t)addr + len); 173 if (end <= start) 174 return (EINVAL); 175 176 rv = uvm_fault_wire(map, start, end, access_type); 177 178 return (rv); 179 } 180 181 /* 182 * uvm_vsunlock: unwire user memory wired by uvm_vslock() 183 * 184 * - called from physio and sys___sysctl 185 * - XXXCDC: consider nuking this (or making it a macro?) 186 */ 187 188 void 189 uvm_vsunlock(struct proc *p, caddr_t addr, size_t len) 190 { 191 vaddr_t start, end; 192 193 start = trunc_page((vaddr_t)addr); 194 end = round_page((vaddr_t)addr + len); 195 if (end <= start) 196 return; 197 198 uvm_fault_unwire(&p->p_vmspace->vm_map, start, end); 199 } 200 201 /* 202 * uvm_fork: fork a virtual address space 203 * 204 * - the address space is copied as per parent map's inherit values 205 * - a new "user" structure is allocated for the child process 206 * [filled in by MD layer...] 207 * - if specified, the child gets a new user stack described by 208 * stack and stacksize 209 * - NOTE: the kernel stack may be at a different location in the child 210 * process, and thus addresses of automatic variables may be invalid 211 * after cpu_fork returns in the child process. We do nothing here 212 * after cpu_fork returns. 213 * - XXXCDC: we need a way for this to return a failure value rather 214 * than just hang 215 */ 216 void 217 uvm_fork(p1, p2, shared, stack, stacksize, func, arg) 218 struct proc *p1, *p2; 219 boolean_t shared; 220 void *stack; 221 size_t stacksize; 222 void (*func)(void *); 223 void *arg; 224 { 225 struct user *up = p2->p_addr; 226 227 if (shared == TRUE) { 228 p2->p_vmspace = NULL; 229 uvmspace_share(p1, p2); /* share vmspace */ 230 } else 231 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */ 232 233 #ifdef PMAP_UAREA 234 /* Tell the pmap this is a u-area mapping */ 235 PMAP_UAREA((vaddr_t)up); 236 #endif 237 238 /* 239 * p_stats currently points at a field in the user struct. Copy 240 * parts of p_stats, and zero out the rest. 241 */ 242 p2->p_stats = &up->u_stats; 243 memset(&up->u_stats.pstat_startzero, 0, 244 ((caddr_t)&up->u_stats.pstat_endzero - 245 (caddr_t)&up->u_stats.pstat_startzero)); 246 memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy, 247 ((caddr_t)&up->u_stats.pstat_endcopy - 248 (caddr_t)&up->u_stats.pstat_startcopy)); 249 250 /* 251 * cpu_fork() copy and update the pcb, and make the child ready 252 * to run. If this is a normal user fork, the child will exit 253 * directly to user mode via child_return() on its first time 254 * slice and will not return here. If this is a kernel thread, 255 * the specified entry point will be executed. 256 */ 257 cpu_fork(p1, p2, stack, stacksize, func, arg); 258 } 259 260 /* 261 * uvm_exit: exit a virtual address space 262 * 263 * - the process passed to us is a dead (pre-zombie) process; we 264 * are running on a different context now (the reaper). 265 * - we must run in a separate thread because freeing the vmspace 266 * of the dead process may block. 267 */ 268 void 269 uvm_exit(struct proc *p) 270 { 271 uvmspace_free(p->p_vmspace); 272 p->p_vmspace = NULL; 273 uvm_km_free(kernel_map, (vaddr_t)p->p_addr, USPACE); 274 p->p_addr = NULL; 275 } 276 277 /* 278 * uvm_init_limit: init per-process VM limits 279 * 280 * - called for process 0 and then inherited by all others. 281 */ 282 void 283 uvm_init_limits(struct proc *p) 284 { 285 286 /* 287 * Set up the initial limits on process VM. Set the maximum 288 * resident set size to be all of (reasonably) available memory. 289 * This causes any single, large process to start random page 290 * replacement once it fills memory. 291 */ 292 293 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 294 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 295 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 296 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 297 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free); 298 } 299 300 #ifdef DEBUG 301 int enableswap = 1; 302 int swapdebug = 0; 303 #define SDB_FOLLOW 1 304 #define SDB_SWAPIN 2 305 #define SDB_SWAPOUT 4 306 #endif 307 308 /* 309 * uvm_scheduler: process zero main loop 310 * 311 * - if not enough memory, wake the pagedaemon and let it clear space. 312 */ 313 314 void 315 uvm_scheduler(void) 316 { 317 /* 318 * Nothing to do, back to sleep 319 */ 320 while (1) 321 tsleep(&proc0, PVM, "scheduler", 0); 322 } 323 324 /* 325 * swappable: is process "p" swappable? 326 */ 327 328 #define swappable(p) (((p)->p_flag & (P_SYSTEM | P_WEXIT)) == 0) 329 330 /* 331 * swapout_threads: find threads that can be swapped 332 * 333 * - called by the pagedaemon 334 * - try and swap at least one processs 335 * - processes that are sleeping or stopped for maxslp or more seconds 336 * are swapped... otherwise the longest-sleeping or stopped process 337 * is swapped, otherwise the longest resident process... 338 */ 339 void 340 uvm_swapout_threads(void) 341 { 342 struct proc *p; 343 struct proc *outp, *outp2; 344 int outpri, outpri2; 345 int didswap = 0; 346 extern int maxslp; 347 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */ 348 349 #ifdef DEBUG 350 if (!enableswap) 351 return; 352 #endif 353 354 /* 355 * outp/outpri : stop/sleep process with largest sleeptime < maxslp 356 * outp2/outpri2: the longest resident process (its swap time) 357 */ 358 outp = outp2 = NULL; 359 outpri = outpri2 = 0; 360 LIST_FOREACH(p, &allproc, p_list) { 361 if (!swappable(p)) 362 continue; 363 switch (p->p_stat) { 364 case SRUN: 365 if (p->p_swtime > outpri2) { 366 outp2 = p; 367 outpri2 = p->p_swtime; 368 } 369 continue; 370 371 case SSLEEP: 372 case SSTOP: 373 if (p->p_slptime >= maxslp) { 374 pmap_collect(p->p_vmspace->vm_map.pmap); 375 didswap++; 376 } else if (p->p_slptime > outpri) { 377 outp = p; 378 outpri = p->p_slptime; 379 } 380 continue; 381 } 382 } 383 384 /* 385 * If we didn't get rid of any real duds, toss out the next most 386 * likely sleeping/stopped or running candidate. We only do this 387 * if we are real low on memory since we don't gain much by doing 388 * it. 389 */ 390 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) { 391 if ((p = outp) == NULL) 392 p = outp2; 393 #ifdef DEBUG 394 if (swapdebug & SDB_SWAPOUT) 395 printf("swapout_threads: no duds, try procp %p\n", p); 396 #endif 397 if (p) 398 pmap_collect(p->p_vmspace->vm_map.pmap); 399 } 400 } 401