1 /* $NetBSD: uvm_glue.c,v 1.184 2026/08/23 22:08:41 riastradh 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 (at) 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.184 2026/08/23 22:08:41 riastradh Exp $"); 66 67 #include "opt_kgdb.h" 68 #include "opt_kstack.h" 69 #include "opt_uvmhist.h" 70 71 /* 72 * uvm_glue.c: glue functions 73 */ 74 75 #include <sys/param.h> 76 #include <sys/kernel.h> 77 78 #include <sys/systm.h> 79 #include <sys/proc.h> 80 #include <sys/resourcevar.h> 81 #include <sys/buf.h> 82 #include <sys/syncobj.h> 83 #include <sys/cpu.h> 84 #include <sys/atomic.h> 85 #include <sys/lwp.h> 86 #include <sys/asan.h> 87 88 #include <uvm/uvm.h> 89 #include <uvm/uvm_pdpolicy.h> 90 #include <uvm/uvm_pgflcache.h> 91 92 /* 93 * uvm_kernacc: test if kernel can access a memory region. 94 * 95 * => Currently used only by /dev/kmem driver (dev/mm.c). 96 */ 97 bool 98 uvm_kernacc(void *addr, size_t len, vm_prot_t prot) 99 { 100 vaddr_t saddr = trunc_page((vaddr_t)addr); 101 vaddr_t eaddr = round_page(saddr + len); 102 bool rv; 103 104 vm_map_lock_read(kernel_map); 105 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot); 106 vm_map_unlock_read(kernel_map); 107 108 return rv; 109 } 110 111 #ifdef KGDB 112 /* 113 * Change protections on kernel pages from addr to addr+len 114 * (presumably so debugger can plant a breakpoint). 115 * 116 * We force the protection change at the pmap level. If we were 117 * to use vm_map_protect a change to allow writing would be lazily- 118 * applied meaning we would still take a protection fault, something 119 * we really don't want to do. It would also fragment the kernel 120 * map unnecessarily. We cannot use pmap_protect since it also won't 121 * enforce a write-enable request. Using pmap_enter is the only way 122 * we can ensure the change takes place properly. 123 */ 124 void 125 uvm_chgkprot(void *addr, size_t len, int rw) 126 { 127 vm_prot_t prot; 128 paddr_t pa; 129 vaddr_t sva, eva; 130 131 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE; 132 eva = round_page((vaddr_t)addr + len); 133 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) { 134 /* 135 * Extract physical address for the page. 136 */ 137 if (pmap_extract(pmap_kernel(), sva, &pa) == false) 138 panic("%s: invalid page", __func__); 139 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 140 } 141 pmap_update(pmap_kernel()); 142 } 143 #endif 144 145 /* 146 * uvm_vslock: wire user memory for I/O 147 * 148 * - called from physio and sys___sysctl 149 * - XXXCDC: consider nuking this (or making it a macro?) 150 */ 151 152 int 153 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access_type) 154 { 155 struct vm_map *map; 156 vaddr_t start, end; 157 int error; 158 159 map = &vs->vm_map; 160 start = trunc_page((vaddr_t)addr); 161 end = round_page((vaddr_t)addr + len); 162 error = uvm_fault_wire(map, start, end, access_type, 0); 163 return error; 164 } 165 166 /* 167 * uvm_vsunlock: unwire user memory wired by uvm_vslock() 168 * 169 * - called from physio and sys___sysctl 170 * - XXXCDC: consider nuking this (or making it a macro?) 171 */ 172 173 void 174 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len) 175 { 176 uvm_fault_unwire(&vs->vm_map, trunc_page((vaddr_t)addr), 177 round_page((vaddr_t)addr + len)); 178 } 179 180 /* 181 * uvm_resident_count: return the resident page count for the 182 * spcified vmspace. 183 */ 184 185 long 186 uvm_resident_count(struct vmspace *vm) 187 { 188 return pmap_resident_count(vm->vm_map.pmap); 189 } 190 191 /* 192 * uvm_proc_fork: fork a virtual address space 193 * 194 * - the address space is copied as per parent map's inherit values 195 */ 196 void 197 uvm_proc_fork(struct proc *p1, struct proc *p2, bool shared) 198 { 199 200 if (shared == true) { 201 p2->p_vmspace = NULL; 202 uvmspace_share(p1, p2); 203 } else { 204 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); 205 } 206 207 cpu_proc_fork(p1, p2); 208 } 209 210 /* 211 * uvm_lwp_fork: fork a thread 212 * 213 * - a new PCB structure is allocated for the child process, 214 * and filled in by MD layer 215 * - if specified, the child gets a new user stack described by 216 * stack and stacksize 217 * - NOTE: the kernel stack may be at a different location in the child 218 * process, and thus addresses of automatic variables may be invalid 219 * after cpu_lwp_fork returns in the child process. We do nothing here 220 * after cpu_lwp_fork returns. 221 */ 222 void 223 uvm_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize, 224 void (*func)(void *), void *arg) 225 { 226 227 /* Fill stack with magic number. */ 228 kstack_setup_magic(l2); 229 230 /* 231 * cpu_lwp_fork() copy and update the pcb, and make the child ready 232 * to run. If this is a normal user fork, the child will exit 233 * directly to user mode via child_return() on its first time 234 * slice and will not return here. If this is a kernel thread, 235 * the specified entry point will be executed. 236 */ 237 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg); 238 } 239 240 #ifndef USPACE_ALIGN 241 #define USPACE_ALIGN 0 242 #endif 243 244 static pool_cache_t uvm_uarea_cache; 245 #if defined(__HAVE_CPU_UAREA_ROUTINES) 246 static pool_cache_t uvm_uarea_system_cache; 247 #else 248 #define uvm_uarea_system_cache uvm_uarea_cache 249 #endif 250 251 static void * 252 uarea_poolpage_alloc(struct pool *pp, int flags) 253 { 254 255 KASSERT((flags & PR_WAITOK) != 0); 256 257 #if defined(PMAP_MAP_POOLPAGE) 258 while (USPACE == PAGE_SIZE && 259 (USPACE_ALIGN == 0 || USPACE_ALIGN == PAGE_SIZE)) { 260 uint64_t ticket; 261 struct vm_page *pg; 262 vaddr_t va; 263 264 ticket = uvm_wait_prepare(); 265 #if defined(PMAP_ALLOC_POOLPAGE) 266 pg = PMAP_ALLOC_POOLPAGE(0); 267 #else 268 pg = uvm_pagealloc(NULL, 0, NULL, 0); 269 #endif 270 if (pg == NULL) { 271 uvm_wait("uarea", ticket); 272 continue; 273 } 274 va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg)); 275 KASSERT(va != 0); 276 return (void *)va; 277 } 278 #endif 279 #if defined(__HAVE_CPU_UAREA_ROUTINES) 280 void *va = cpu_uarea_alloc(false); 281 if (va) 282 return (void *)va; 283 #endif 284 return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz, 285 USPACE_ALIGN, UVM_KMF_WIRED | UVM_KMF_WAITVA); 286 } 287 288 static void 289 uarea_poolpage_free(struct pool *pp, void *addr) 290 { 291 #if defined(PMAP_MAP_POOLPAGE) 292 if (USPACE == PAGE_SIZE && 293 (USPACE_ALIGN == 0 || USPACE_ALIGN == PAGE_SIZE)) { 294 paddr_t pa; 295 296 pa = PMAP_UNMAP_POOLPAGE((vaddr_t) addr); 297 KASSERT(pa != 0); 298 uvm_pagefree(PHYS_TO_VM_PAGE(pa)); 299 return; 300 } 301 #endif 302 #if defined(__HAVE_CPU_UAREA_ROUTINES) 303 if (cpu_uarea_free(addr)) 304 return; 305 #endif 306 uvm_km_free(kernel_map, (vaddr_t)addr, pp->pr_alloc->pa_pagesz, 307 UVM_KMF_WIRED); 308 } 309 310 static struct pool_allocator uvm_uarea_allocator = { 311 .pa_alloc = uarea_poolpage_alloc, 312 .pa_free = uarea_poolpage_free, 313 .pa_pagesz = USPACE, 314 }; 315 316 #if defined(__HAVE_CPU_UAREA_ROUTINES) 317 static void * 318 uarea_system_poolpage_alloc(struct pool *pp, int flags) 319 { 320 void * const va = cpu_uarea_alloc(true); 321 if (va != NULL) 322 return va; 323 324 return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz, 325 USPACE_ALIGN, UVM_KMF_WIRED | 326 ((flags & PR_WAITOK) ? UVM_KMF_WAITVA : 327 (UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK))); 328 } 329 330 static void 331 uarea_system_poolpage_free(struct pool *pp, void *addr) 332 { 333 if (cpu_uarea_free(addr)) 334 return; 335 336 uvm_km_free(kernel_map, (vaddr_t)addr, pp->pr_alloc->pa_pagesz, 337 UVM_KMF_WIRED); 338 } 339 340 static struct pool_allocator uvm_uarea_system_allocator = { 341 .pa_alloc = uarea_system_poolpage_alloc, 342 .pa_free = uarea_system_poolpage_free, 343 .pa_pagesz = USPACE, 344 }; 345 #endif /* __HAVE_CPU_UAREA_ROUTINES */ 346 347 void 348 uvm_uarea_init(void) 349 { 350 int flags = PR_NOTOUCH; 351 352 /* 353 * specify PR_NOALIGN unless the alignment provided by 354 * the backend (USPACE_ALIGN) is sufficient to provide 355 * pool page size (UPSACE) alignment. 356 */ 357 358 if ((USPACE_ALIGN == 0 && USPACE != PAGE_SIZE) || 359 (USPACE_ALIGN % USPACE) != 0) { 360 flags |= PR_NOALIGN; 361 } 362 363 uvm_uarea_cache = pool_cache_init(USPACE, USPACE_ALIGN, 0, flags, 364 "uarea", &uvm_uarea_allocator, IPL_NONE, NULL, NULL, NULL); 365 #if defined(__HAVE_CPU_UAREA_ROUTINES) 366 uvm_uarea_system_cache = pool_cache_init(USPACE, USPACE_ALIGN, 367 0, flags, "uareasys", &uvm_uarea_system_allocator, 368 IPL_NONE, NULL, NULL, NULL); 369 #endif 370 } 371 372 /* 373 * uvm_uarea_alloc: allocate a u-area 374 */ 375 376 vaddr_t 377 uvm_uarea_alloc(void) 378 { 379 380 return (vaddr_t)pool_cache_get(uvm_uarea_cache, PR_WAITOK); 381 } 382 383 vaddr_t 384 uvm_uarea_system_alloc(struct cpu_info *ci) 385 { 386 #ifdef __HAVE_CPU_UAREA_ALLOC_IDLELWP 387 if (__predict_false(ci != NULL)) 388 return cpu_uarea_alloc_idlelwp(ci); 389 #endif 390 391 return (vaddr_t)pool_cache_get(uvm_uarea_system_cache, PR_WAITOK); 392 } 393 394 /* 395 * uvm_uarea_free: free a u-area 396 */ 397 398 void 399 uvm_uarea_free(vaddr_t uaddr) 400 { 401 402 kasan_mark((void *)uaddr, USPACE, USPACE, 0); 403 pool_cache_put(uvm_uarea_cache, (void *)uaddr); 404 } 405 406 void 407 uvm_uarea_system_free(vaddr_t uaddr) 408 { 409 410 kasan_mark((void *)uaddr, USPACE, USPACE, 0); 411 pool_cache_put(uvm_uarea_system_cache, (void *)uaddr); 412 } 413 414 vaddr_t 415 uvm_lwp_getuarea(lwp_t *l) 416 { 417 418 return (vaddr_t)l->l_addr - UAREA_PCB_OFFSET; 419 } 420 421 void 422 uvm_lwp_setuarea(lwp_t *l, vaddr_t addr) 423 { 424 425 l->l_addr = (void *)(addr + UAREA_PCB_OFFSET); 426 } 427 428 /* 429 * uvm_proc_exit: exit a virtual address space 430 * 431 * - borrow proc0's address space because freeing the vmspace 432 * of the dead process may block. 433 */ 434 435 void 436 uvm_proc_exit(struct proc *p) 437 { 438 struct lwp *l = curlwp; /* XXX */ 439 struct vmspace *ovm; 440 441 KASSERT(p == l->l_proc); 442 ovm = p->p_vmspace; 443 KASSERT(ovm != NULL); 444 445 if (__predict_false(ovm == proc0.p_vmspace)) 446 return; 447 448 /* 449 * borrow proc0's address space. 450 */ 451 kpreempt_disable(); 452 pmap_deactivate(l); 453 p->p_vmspace = proc0.p_vmspace; 454 pmap_activate(l); 455 kpreempt_enable(); 456 457 uvmspace_free(ovm); 458 } 459 460 void 461 uvm_lwp_exit(struct lwp *l) 462 { 463 vaddr_t va = uvm_lwp_getuarea(l); 464 bool system = (l->l_flag & LW_SYSTEM) != 0; 465 466 if (system) 467 uvm_uarea_system_free(va); 468 else 469 uvm_uarea_free(va); 470 #ifdef DIAGNOSTIC 471 uvm_lwp_setuarea(l, (vaddr_t)NULL); 472 #endif 473 } 474 475 /* 476 * uvm_init_limit: init per-process VM limits 477 * 478 * - called for process 0 and then inherited by all others. 479 */ 480 481 void 482 uvm_init_limits(struct proc *p) 483 { 484 485 /* 486 * Set up the initial limits on process VM. Set the maximum 487 * resident set size to be all of (reasonably) available memory. 488 * This causes any single, large process to start random page 489 * replacement once it fills memory. 490 */ 491 492 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 493 p->p_rlimit[RLIMIT_STACK].rlim_max = maxsmap; 494 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 495 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdmap; 496 p->p_rlimit[RLIMIT_AS].rlim_cur = RLIM_INFINITY; 497 p->p_rlimit[RLIMIT_AS].rlim_max = RLIM_INFINITY; 498 p->p_rlimit[RLIMIT_RSS].rlim_cur = MIN(VM_MAXUSER_ADDRESS, 499 ctob((rlim_t)uvm_availmem(false))); 500 } 501 502 /* 503 * uvm_scheduler: process zero main loop. 504 */ 505 506 extern struct loadavg averunnable; 507 508 void 509 uvm_scheduler(void) 510 { 511 lwp_t *l = curlwp; 512 513 lwp_lock(l); 514 l->l_class = SCHED_FIFO; 515 lwp_changepri(l, PRI_VM); 516 lwp_unlock(l); 517 518 /* Start the freelist cache. */ 519 uvm_pgflcache_start(); 520 521 for (;;) { 522 /* Update legacy stats for post-mortem debugging. */ 523 uvm_update_uvmexp(); 524 525 /* See if the pagedaemon needs to generate some free pages. */ 526 uvm_kick_pdaemon(); 527 528 /* Calculate process statistics. */ 529 sched_pstats(); 530 (void)kpause("uvm", false, hz, NULL); 531 } 532 } 533 534 /* 535 * uvm_idle: called from the idle loop. 536 */ 537 538 void 539 uvm_idle(void) 540 { 541 struct cpu_info *ci = curcpu(); 542 struct uvm_cpu *ucpu = ci->ci_data.cpu_uvm; 543 544 KASSERT(kpreempt_disabled()); 545 546 uvmpdpol_idle(ucpu); 547 } 548