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