1 /* $NetBSD: uvm_glue.c,v 1.168 2019/05/08 16:00:01 chs Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 * Copyright (c) 1991, 1993, The Regents of the University of California. 6 * 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 37 * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp 38 * 39 * 40 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 41 * All rights reserved. 42 * 43 * Permission to use, copy, modify and distribute this software and 44 * its documentation is hereby granted, provided that both the copyright 45 * notice and this permission notice appear in all copies of the 46 * software, derivative works or modified versions, and any portions 47 * thereof, and that both notices appear in supporting documentation. 48 * 49 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 50 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 51 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 52 * 53 * Carnegie Mellon requests users of this software to return to 54 * 55 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 56 * School of Computer Science 57 * Carnegie Mellon University 58 * Pittsburgh PA 15213-3890 59 * 60 * any improvements or extensions that they make and grant Carnegie the 61 * rights to redistribute these changes. 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.168 2019/05/08 16:00:01 chs Exp $"); 66 67 #include "opt_kgdb.h" 68 #include "opt_kstack.h" 69 #include "opt_uvmhist.h" 70 #include "opt_kasan.h" 71 72 /* 73 * uvm_glue.c: glue functions 74 */ 75 76 #include <sys/param.h> 77 #include <sys/kernel.h> 78 79 #include <sys/systm.h> 80 #include <sys/proc.h> 81 #include <sys/resourcevar.h> 82 #include <sys/buf.h> 83 #include <sys/syncobj.h> 84 #include <sys/cpu.h> 85 #include <sys/atomic.h> 86 #include <sys/lwp.h> 87 #include <sys/asan.h> 88 89 #include <uvm/uvm.h> 90 91 /* 92 * uvm_kernacc: test if kernel can access a memory region. 93 * 94 * => Currently used only by /dev/kmem driver (dev/mm.c). 95 */ 96 bool 97 uvm_kernacc(void *addr, size_t len, vm_prot_t prot) 98 { 99 vaddr_t saddr = trunc_page((vaddr_t)addr); 100 vaddr_t eaddr = round_page(saddr + len); 101 bool rv; 102 103 vm_map_lock_read(kernel_map); 104 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot); 105 vm_map_unlock_read(kernel_map); 106 107 return rv; 108 } 109 110 #ifdef KGDB 111 /* 112 * Change protections on kernel pages from addr to addr+len 113 * (presumably so debugger can plant a breakpoint). 114 * 115 * We force the protection change at the pmap level. If we were 116 * to use vm_map_protect a change to allow writing would be lazily- 117 * applied meaning we would still take a protection fault, something 118 * we really don't want to do. It would also fragment the kernel 119 * map unnecessarily. We cannot use pmap_protect since it also won't 120 * enforce a write-enable request. Using pmap_enter is the only way 121 * we can ensure the change takes place properly. 122 */ 123 void 124 uvm_chgkprot(void *addr, size_t len, int rw) 125 { 126 vm_prot_t prot; 127 paddr_t pa; 128 vaddr_t sva, eva; 129 130 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE; 131 eva = round_page((vaddr_t)addr + len); 132 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) { 133 /* 134 * Extract physical address for the page. 135 */ 136 if (pmap_extract(pmap_kernel(), sva, &pa) == false) 137 panic("%s: invalid page", __func__); 138 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 139 } 140 pmap_update(pmap_kernel()); 141 } 142 #endif 143 144 /* 145 * uvm_vslock: wire user memory for I/O 146 * 147 * - called from physio and sys___sysctl 148 * - XXXCDC: consider nuking this (or making it a macro?) 149 */ 150 151 int 152 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access_type) 153 { 154 struct vm_map *map; 155 vaddr_t start, end; 156 int error; 157 158 map = &vs->vm_map; 159 start = trunc_page((vaddr_t)addr); 160 end = round_page((vaddr_t)addr + len); 161 error = uvm_fault_wire(map, start, end, access_type, 0); 162 return error; 163 } 164 165 /* 166 * uvm_vsunlock: unwire user memory wired by uvm_vslock() 167 * 168 * - called from physio and sys___sysctl 169 * - XXXCDC: consider nuking this (or making it a macro?) 170 */ 171 172 void 173 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len) 174 { 175 uvm_fault_unwire(&vs->vm_map, trunc_page((vaddr_t)addr), 176 round_page((vaddr_t)addr + len)); 177 } 178 179 /* 180 * uvm_proc_fork: fork a virtual address space 181 * 182 * - the address space is copied as per parent map's inherit values 183 */ 184 void 185 uvm_proc_fork(struct proc *p1, struct proc *p2, bool shared) 186 { 187 188 if (shared == true) { 189 p2->p_vmspace = NULL; 190 uvmspace_share(p1, p2); 191 } else { 192 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); 193 } 194 195 cpu_proc_fork(p1, p2); 196 } 197 198 /* 199 * uvm_lwp_fork: fork a thread 200 * 201 * - a new PCB structure is allocated for the child process, 202 * and filled in by MD layer 203 * - if specified, the child gets a new user stack described by 204 * stack and stacksize 205 * - NOTE: the kernel stack may be at a different location in the child 206 * process, and thus addresses of automatic variables may be invalid 207 * after cpu_lwp_fork returns in the child process. We do nothing here 208 * after cpu_lwp_fork returns. 209 */ 210 void 211 uvm_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize, 212 void (*func)(void *), void *arg) 213 { 214 215 /* Fill stack with magic number. */ 216 kstack_setup_magic(l2); 217 218 /* 219 * cpu_lwp_fork() copy and update the pcb, and make the child ready 220 * to run. If this is a normal user fork, the child will exit 221 * directly to user mode via child_return() on its first time 222 * slice and will not return here. If this is a kernel thread, 223 * the specified entry point will be executed. 224 */ 225 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg); 226 227 /* Inactive emap for new LWP. */ 228 l2->l_emap_gen = UVM_EMAP_INACTIVE; 229 } 230 231 #ifndef USPACE_ALIGN 232 #define USPACE_ALIGN 0 233 #endif 234 235 static pool_cache_t uvm_uarea_cache; 236 #if defined(__HAVE_CPU_UAREA_ROUTINES) 237 static pool_cache_t uvm_uarea_system_cache; 238 #else 239 #define uvm_uarea_system_cache uvm_uarea_cache 240 #endif 241 242 static void * 243 uarea_poolpage_alloc(struct pool *pp, int flags) 244 { 245 246 KASSERT((flags & PR_WAITOK) != 0); 247 248 #if defined(PMAP_MAP_POOLPAGE) 249 while (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) { 250 struct vm_page *pg; 251 vaddr_t va; 252 #if defined(PMAP_ALLOC_POOLPAGE) 253 pg = PMAP_ALLOC_POOLPAGE(0); 254 #else 255 pg = uvm_pagealloc(NULL, 0, NULL, 0); 256 #endif 257 if (pg == NULL) { 258 uvm_wait("uarea"); 259 continue; 260 } 261 va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg)); 262 KASSERT(va != 0); 263 return (void *)va; 264 } 265 #endif 266 #if defined(__HAVE_CPU_UAREA_ROUTINES) 267 void *va = cpu_uarea_alloc(false); 268 if (va) 269 return (void *)va; 270 #endif 271 return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz, 272 USPACE_ALIGN, UVM_KMF_WIRED | UVM_KMF_WAITVA); 273 } 274 275 static void 276 uarea_poolpage_free(struct pool *pp, void *addr) 277 { 278 #if defined(PMAP_MAP_POOLPAGE) 279 if (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) { 280 paddr_t pa; 281 282 pa = PMAP_UNMAP_POOLPAGE((vaddr_t) addr); 283 KASSERT(pa != 0); 284 uvm_pagefree(PHYS_TO_VM_PAGE(pa)); 285 return; 286 } 287 #endif 288 #if defined(__HAVE_CPU_UAREA_ROUTINES) 289 if (cpu_uarea_free(addr)) 290 return; 291 #endif 292 uvm_km_free(kernel_map, (vaddr_t)addr, pp->pr_alloc->pa_pagesz, 293 UVM_KMF_WIRED); 294 } 295 296 static struct pool_allocator uvm_uarea_allocator = { 297 .pa_alloc = uarea_poolpage_alloc, 298 .pa_free = uarea_poolpage_free, 299 .pa_pagesz = USPACE, 300 }; 301 302 #if defined(__HAVE_CPU_UAREA_ROUTINES) 303 static void * 304 uarea_system_poolpage_alloc(struct pool *pp, int flags) 305 { 306 void * const va = cpu_uarea_alloc(true); 307 if (va != NULL) 308 return va; 309 310 return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz, 311 USPACE_ALIGN, UVM_KMF_WIRED | 312 ((flags & PR_WAITOK) ? UVM_KMF_WAITVA : 313 (UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK))); 314 } 315 316 static void 317 uarea_system_poolpage_free(struct pool *pp, void *addr) 318 { 319 if (cpu_uarea_free(addr)) 320 return; 321 322 uvm_km_free(kernel_map, (vaddr_t)addr, pp->pr_alloc->pa_pagesz, 323 UVM_KMF_WIRED); 324 } 325 326 static struct pool_allocator uvm_uarea_system_allocator = { 327 .pa_alloc = uarea_system_poolpage_alloc, 328 .pa_free = uarea_system_poolpage_free, 329 .pa_pagesz = USPACE, 330 }; 331 #endif /* __HAVE_CPU_UAREA_ROUTINES */ 332 333 void 334 uvm_uarea_init(void) 335 { 336 int flags = PR_NOTOUCH; 337 338 /* 339 * specify PR_NOALIGN unless the alignment provided by 340 * the backend (USPACE_ALIGN) is sufficient to provide 341 * pool page size (UPSACE) alignment. 342 */ 343 344 if ((USPACE_ALIGN == 0 && USPACE != PAGE_SIZE) || 345 (USPACE_ALIGN % USPACE) != 0) { 346 flags |= PR_NOALIGN; 347 } 348 349 uvm_uarea_cache = pool_cache_init(USPACE, USPACE_ALIGN, 0, flags, 350 "uarea", &uvm_uarea_allocator, IPL_NONE, NULL, NULL, NULL); 351 #if defined(__HAVE_CPU_UAREA_ROUTINES) 352 uvm_uarea_system_cache = pool_cache_init(USPACE, USPACE_ALIGN, 353 0, flags, "uareasys", &uvm_uarea_system_allocator, 354 IPL_NONE, NULL, NULL, NULL); 355 #endif 356 } 357 358 /* 359 * uvm_uarea_alloc: allocate a u-area 360 */ 361 362 vaddr_t 363 uvm_uarea_alloc(void) 364 { 365 366 return (vaddr_t)pool_cache_get(uvm_uarea_cache, PR_WAITOK); 367 } 368 369 vaddr_t 370 uvm_uarea_system_alloc(struct cpu_info *ci) 371 { 372 #ifdef __HAVE_CPU_UAREA_ALLOC_IDLELWP 373 if (__predict_false(ci != NULL)) 374 return cpu_uarea_alloc_idlelwp(ci); 375 #endif 376 377 return (vaddr_t)pool_cache_get(uvm_uarea_system_cache, PR_WAITOK); 378 } 379 380 /* 381 * uvm_uarea_free: free a u-area 382 */ 383 384 void 385 uvm_uarea_free(vaddr_t uaddr) 386 { 387 388 kasan_mark((void *)uaddr, USPACE, USPACE, 0); 389 pool_cache_put(uvm_uarea_cache, (void *)uaddr); 390 } 391 392 void 393 uvm_uarea_system_free(vaddr_t uaddr) 394 { 395 396 kasan_mark((void *)uaddr, USPACE, USPACE, 0); 397 pool_cache_put(uvm_uarea_system_cache, (void *)uaddr); 398 } 399 400 vaddr_t 401 uvm_lwp_getuarea(lwp_t *l) 402 { 403 404 return (vaddr_t)l->l_addr - UAREA_PCB_OFFSET; 405 } 406 407 void 408 uvm_lwp_setuarea(lwp_t *l, vaddr_t addr) 409 { 410 411 l->l_addr = (void *)(addr + UAREA_PCB_OFFSET); 412 } 413 414 /* 415 * uvm_proc_exit: exit a virtual address space 416 * 417 * - borrow proc0's address space because freeing the vmspace 418 * of the dead process may block. 419 */ 420 421 void 422 uvm_proc_exit(struct proc *p) 423 { 424 struct lwp *l = curlwp; /* XXX */ 425 struct vmspace *ovm; 426 427 KASSERT(p == l->l_proc); 428 ovm = p->p_vmspace; 429 KASSERT(ovm != NULL); 430 431 if (__predict_false(ovm == proc0.p_vmspace)) 432 return; 433 434 /* 435 * borrow proc0's address space. 436 */ 437 kpreempt_disable(); 438 pmap_deactivate(l); 439 p->p_vmspace = proc0.p_vmspace; 440 pmap_activate(l); 441 kpreempt_enable(); 442 443 uvmspace_free(ovm); 444 } 445 446 void 447 uvm_lwp_exit(struct lwp *l) 448 { 449 vaddr_t va = uvm_lwp_getuarea(l); 450 bool system = (l->l_flag & LW_SYSTEM) != 0; 451 452 if (system) 453 uvm_uarea_system_free(va); 454 else 455 uvm_uarea_free(va); 456 #ifdef DIAGNOSTIC 457 uvm_lwp_setuarea(l, (vaddr_t)NULL); 458 #endif 459 } 460 461 /* 462 * uvm_init_limit: init per-process VM limits 463 * 464 * - called for process 0 and then inherited by all others. 465 */ 466 467 void 468 uvm_init_limits(struct proc *p) 469 { 470 471 /* 472 * Set up the initial limits on process VM. Set the maximum 473 * resident set size to be all of (reasonably) available memory. 474 * This causes any single, large process to start random page 475 * replacement once it fills memory. 476 */ 477 478 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 479 p->p_rlimit[RLIMIT_STACK].rlim_max = maxsmap; 480 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 481 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdmap; 482 p->p_rlimit[RLIMIT_AS].rlim_cur = RLIM_INFINITY; 483 p->p_rlimit[RLIMIT_AS].rlim_max = RLIM_INFINITY; 484 p->p_rlimit[RLIMIT_RSS].rlim_cur = MIN( 485 VM_MAXUSER_ADDRESS, ctob((rlim_t)uvmexp.free)); 486 } 487 488 /* 489 * uvm_scheduler: process zero main loop. 490 */ 491 492 extern struct loadavg averunnable; 493 494 void 495 uvm_scheduler(void) 496 { 497 lwp_t *l = curlwp; 498 499 lwp_lock(l); 500 l->l_priority = PRI_VM; 501 l->l_class = SCHED_FIFO; 502 lwp_unlock(l); 503 504 for (;;) { 505 sched_pstats(); 506 (void)kpause("uvm", false, hz, NULL); 507 } 508 } 509