Home | History | Annotate | Line # | Download | only in uvm
uvm_glue.c revision 1.44.2.4
      1 /*	$NetBSD: uvm_glue.c,v 1.44.2.4 2001/06/21 20:10:30 nathanw 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by Charles D. Cranor,
     23  *      Washington University, the University of California, Berkeley and
     24  *      its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)vm_glue.c	8.6 (Berkeley) 1/5/94
     42  * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
     43  *
     44  *
     45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     46  * All rights reserved.
     47  *
     48  * Permission to use, copy, modify and distribute this software and
     49  * its documentation is hereby granted, provided that both the copyright
     50  * notice and this permission notice appear in all copies of the
     51  * software, derivative works or modified versions, and any portions
     52  * thereof, and that both notices appear in supporting documentation.
     53  *
     54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     57  *
     58  * Carnegie Mellon requests users of this software to return to
     59  *
     60  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     61  *  School of Computer Science
     62  *  Carnegie Mellon University
     63  *  Pittsburgh PA 15213-3890
     64  *
     65  * any improvements or extensions that they make and grant Carnegie the
     66  * rights to redistribute these changes.
     67  */
     68 
     69 #include "opt_kgdb.h"
     70 #include "opt_sysv.h"
     71 #include "opt_uvmhist.h"
     72 
     73 /*
     74  * uvm_glue.c: glue functions
     75  */
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/lwp.h>
     80 #include <sys/proc.h>
     81 #include <sys/resourcevar.h>
     82 #include <sys/buf.h>
     83 #include <sys/user.h>
     84 #ifdef SYSVSHM
     85 #include <sys/shm.h>
     86 #endif
     87 
     88 #include <uvm/uvm.h>
     89 
     90 #include <machine/cpu.h>
     91 
     92 /*
     93  * local prototypes
     94  */
     95 
     96 static void uvm_swapout __P((struct lwp *));
     97 
     98 /*
     99  * XXXCDC: do these really belong here?
    100  */
    101 
    102 int readbuffers = 0;		/* allow KGDB to read kern buffer pool */
    103 				/* XXX: see uvm_kernacc */
    104 
    105 
    106 /*
    107  * uvm_kernacc: can the kernel access a region of memory
    108  *
    109  * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
    110  */
    111 
    112 boolean_t
    113 uvm_kernacc(addr, len, rw)
    114 	caddr_t addr;
    115 	size_t len;
    116 	int rw;
    117 {
    118 	boolean_t rv;
    119 	vaddr_t saddr, eaddr;
    120 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
    121 
    122 	saddr = trunc_page((vaddr_t)addr);
    123 	eaddr = round_page((vaddr_t)addr + len);
    124 	vm_map_lock_read(kernel_map);
    125 	rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
    126 	vm_map_unlock_read(kernel_map);
    127 
    128 	/*
    129 	 * XXX there are still some things (e.g. the buffer cache) that
    130 	 * are managed behind the VM system's back so even though an
    131 	 * address is accessible in the mind of the VM system, there may
    132 	 * not be physical pages where the VM thinks there is.  This can
    133 	 * lead to bogus allocation of pages in the kernel address space
    134 	 * or worse, inconsistencies at the pmap level.  We only worry
    135 	 * about the buffer cache for now.
    136 	 */
    137 	if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
    138 			     saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
    139 		rv = FALSE;
    140 	return(rv);
    141 }
    142 
    143 /*
    144  * uvm_useracc: can the user access it?
    145  *
    146  * - called from physio() and sys___sysctl().
    147  */
    148 
    149 boolean_t
    150 uvm_useracc(addr, len, rw)
    151 	caddr_t addr;
    152 	size_t len;
    153 	int rw;
    154 {
    155 	struct vm_map *map;
    156 	boolean_t rv;
    157 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
    158 
    159 	/* XXX curproc */
    160 	map = &curproc->l_proc->p_vmspace->vm_map;
    161 
    162 	vm_map_lock_read(map);
    163 	rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr),
    164 	    round_page((vaddr_t)addr + len), prot);
    165 	vm_map_unlock_read(map);
    166 
    167 	return(rv);
    168 }
    169 
    170 #ifdef KGDB
    171 /*
    172  * Change protections on kernel pages from addr to addr+len
    173  * (presumably so debugger can plant a breakpoint).
    174  *
    175  * We force the protection change at the pmap level.  If we were
    176  * to use vm_map_protect a change to allow writing would be lazily-
    177  * applied meaning we would still take a protection fault, something
    178  * we really don't want to do.  It would also fragment the kernel
    179  * map unnecessarily.  We cannot use pmap_protect since it also won't
    180  * enforce a write-enable request.  Using pmap_enter is the only way
    181  * we can ensure the change takes place properly.
    182  */
    183 void
    184 uvm_chgkprot(addr, len, rw)
    185 	caddr_t addr;
    186 	size_t len;
    187 	int rw;
    188 {
    189 	vm_prot_t prot;
    190 	paddr_t pa;
    191 	vaddr_t sva, eva;
    192 
    193 	prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
    194 	eva = round_page((vaddr_t)addr + len);
    195 	for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
    196 		/*
    197 		 * Extract physical address for the page.
    198 		 */
    199 		if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE)
    200 			panic("chgkprot: invalid page");
    201 		pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
    202 	}
    203 	pmap_update();
    204 }
    205 #endif
    206 
    207 /*
    208  * vslock: wire user memory for I/O
    209  *
    210  * - called from physio and sys___sysctl
    211  * - XXXCDC: consider nuking this (or making it a macro?)
    212  */
    213 
    214 int
    215 uvm_vslock(p, addr, len, access_type)
    216 	struct proc *p;
    217 	caddr_t	addr;
    218 	size_t	len;
    219 	vm_prot_t access_type;
    220 {
    221 	struct vm_map *map;
    222 	vaddr_t start, end;
    223 	int error;
    224 
    225 	map = &p->p_vmspace->vm_map;
    226 	start = trunc_page((vaddr_t)addr);
    227 	end = round_page((vaddr_t)addr + len);
    228 	error = uvm_fault_wire(map, start, end, access_type);
    229 	return error;
    230 }
    231 
    232 /*
    233  * vslock: wire user memory for I/O
    234  *
    235  * - called from physio and sys___sysctl
    236  * - XXXCDC: consider nuking this (or making it a macro?)
    237  */
    238 
    239 void
    240 uvm_vsunlock(p, addr, len)
    241 	struct proc *p;
    242 	caddr_t	addr;
    243 	size_t	len;
    244 {
    245 	uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr),
    246 		round_page((vaddr_t)addr + len));
    247 }
    248 
    249 /*
    250  * uvm_proc_fork: fork a virtual address space
    251  *
    252  * - the address space is copied as per parent map's inherit values
    253  */
    254 void
    255 uvm_proc_fork(p1, p2, shared)
    256 	struct proc *p1, *p2;
    257 	boolean_t shared;
    258 {
    259 
    260 	if (shared == TRUE) {
    261 		p2->p_vmspace = NULL;
    262 		uvmspace_share(p1, p2);			/* share vmspace */
    263 	} else {
    264 		p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */
    265 	}
    266 }
    267 
    268 
    269 /*
    270  * uvm_lwp_fork: fork a thread
    271  *
    272  * - a new "user" structure is allocated for the child process
    273  *	[filled in by MD layer...]
    274  * - if specified, the child gets a new user stack described by
    275  *	stack and stacksize
    276  * - NOTE: the kernel stack may be at a different location in the child
    277  *	process, and thus addresses of automatic variables may be invalid
    278  *	after cpu_fork returns in the child process.  We do nothing here
    279  *	after cpu_fork returns.
    280  * - XXXCDC: we need a way for this to return a failure value rather
    281  *   than just hang
    282  */
    283 void
    284 uvm_lwp_fork(l1, l2, stack, stacksize, func, arg)
    285 	struct lwp *l1, *l2;
    286 	void *stack;
    287 	size_t stacksize;
    288 	void (*func) __P((void *));
    289 	void *arg;
    290 {
    291 	struct user *up = l2->l_addr;
    292 	int error;
    293 
    294 	/*
    295 	 * Wire down the U-area for the process, which contains the PCB
    296 	 * and the kernel stack.  Wired state is stored in p->p_flag's
    297 	 * P_INMEM bit rather than in the vm_map_entry's wired count
    298 	 * to prevent kernel_map fragmentation.
    299 	 *
    300 	 * Note the kernel stack gets read/write accesses right off
    301 	 * the bat.
    302 	 */
    303 	error = uvm_fault_wire(kernel_map, (vaddr_t)up,
    304 	    (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE);
    305 	if (error)
    306 		panic("uvm_fork: uvm_fault_wire failed: %d", error);
    307 
    308 	/*
    309 	 * cpu_fork() copy and update the pcb, and make the child ready
    310  	 * to run.  If this is a normal user fork, the child will exit
    311 	 * directly to user mode via child_return() on its first time
    312 	 * slice and will not return here.  If this is a kernel thread,
    313 	 * the specified entry point will be executed.
    314 	 */
    315 	cpu_fork(l1, l2, stack, stacksize, func, arg);
    316 }
    317 
    318 /*
    319  * uvm_exit: exit a virtual address space
    320  *
    321  * - the process passed to us is a dead (pre-zombie) process; we
    322  *   are running on a different context now (the reaper).
    323  * - we must run in a separate thread because freeing the vmspace
    324  *   of the dead process may block.
    325  */
    326 void
    327 uvm_proc_exit(p)
    328 	struct proc *p;
    329 {
    330 	uvmspace_free(p->p_vmspace);
    331 }
    332 
    333 void
    334 uvm_lwp_exit(l)
    335 	struct lwp *l;
    336 {
    337 	vaddr_t va = (vaddr_t)l->l_addr;
    338 
    339 	uvm_fault_unwire(kernel_map, va, va + USPACE);
    340 	uvm_km_free(kernel_map, va, USPACE);
    341 
    342 	l->l_flag &= ~L_INMEM;
    343 	l->l_addr = NULL;
    344 }
    345 
    346 /*
    347  * uvm_init_limit: init per-process VM limits
    348  *
    349  * - called for process 0 and then inherited by all others.
    350  */
    351 void
    352 uvm_init_limits(p)
    353 	struct proc *p;
    354 {
    355 
    356 	/*
    357 	 * Set up the initial limits on process VM.  Set the maximum
    358 	 * resident set size to be all of (reasonably) available memory.
    359 	 * This causes any single, large process to start random page
    360 	 * replacement once it fills memory.
    361 	 */
    362 
    363 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
    364 	p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
    365 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
    366 	p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
    367 	p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
    368 }
    369 
    370 #ifdef DEBUG
    371 int	enableswap = 1;
    372 int	swapdebug = 0;
    373 #define	SDB_FOLLOW	1
    374 #define SDB_SWAPIN	2
    375 #define SDB_SWAPOUT	4
    376 #endif
    377 
    378 /*
    379  * uvm_swapin: swap in a process's u-area.
    380  */
    381 
    382 void
    383 uvm_swapin(l)
    384 	struct lwp *l;
    385 {
    386 	vaddr_t addr;
    387 	int s;
    388 
    389 	addr = (vaddr_t)l->l_addr;
    390 	/* make L_INMEM true */
    391 	uvm_fault_wire(kernel_map, addr, addr + USPACE,
    392 	    VM_PROT_READ | VM_PROT_WRITE);
    393 
    394 	/*
    395 	 * Some architectures need to be notified when the user area has
    396 	 * moved to new physical page(s) (e.g.  see mips/mips/vm_machdep.c).
    397 	 */
    398 	cpu_swapin(l);
    399 	SCHED_LOCK(s);
    400 	if (l->l_stat == LSRUN)
    401 		setrunqueue(l);
    402 	l->l_flag |= L_INMEM;
    403 	SCHED_UNLOCK(s);
    404 	l->l_swtime = 0;
    405 	++uvmexp.swapins;
    406 }
    407 
    408 /*
    409  * uvm_scheduler: process zero main loop
    410  *
    411  * - attempt to swapin every swaped-out, runnable process in order of
    412  *	priority.
    413  * - if not enough memory, wake the pagedaemon and let it clear space.
    414  */
    415 
    416 void
    417 uvm_scheduler()
    418 {
    419 	struct lwp *l, *ll;
    420 	int pri;
    421 	int ppri;
    422 
    423 loop:
    424 #ifdef DEBUG
    425 	while (!enableswap)
    426 		tsleep(&proc0, PVM, "noswap", 0);
    427 #endif
    428 	ll = NULL;		/* process to choose */
    429 	ppri = INT_MIN;	/* its priority */
    430 	proclist_lock_read();
    431 
    432 	LIST_FOREACH(l, &alllwp, l_list) {
    433 		/* is it a runnable swapped out process? */
    434 		if (l->l_stat == LSRUN && (l->l_flag & L_INMEM) == 0) {
    435 			pri = l->l_swtime + l->l_slptime -
    436 			    (l->l_proc->p_nice - NZERO) * 8;
    437 			if (pri > ppri) {   /* higher priority?  remember it. */
    438 				ll = l;
    439 				ppri = pri;
    440 			}
    441 		}
    442 	}
    443 	/*
    444 	 * XXXSMP: possible unlock/sleep race between here and the
    445 	 * "scheduler" tsleep below..
    446 	 */
    447 	proclist_unlock_read();
    448 
    449 #ifdef DEBUG
    450 	if (swapdebug & SDB_FOLLOW)
    451 		printf("scheduler: running, procp %p pri %d\n", ll, ppri);
    452 #endif
    453 	/*
    454 	 * Nothing to do, back to sleep
    455 	 */
    456 	if ((l = ll) == NULL) {
    457 		tsleep(&proc0, PVM, "scheduler", 0);
    458 		goto loop;
    459 	}
    460 
    461 	/*
    462 	 * we have found swapped out process which we would like to bring
    463 	 * back in.
    464 	 *
    465 	 * XXX: this part is really bogus cuz we could deadlock on memory
    466 	 * despite our feeble check
    467 	 */
    468 	if (uvmexp.free > atop(USPACE)) {
    469 #ifdef DEBUG
    470 		if (swapdebug & SDB_SWAPIN)
    471 			printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
    472 	     l->l_proc->p_pid, l->l_proc->p_comm, l->l_addr, ppri, uvmexp.free);
    473 #endif
    474 		uvm_swapin(l);
    475 		goto loop;
    476 	}
    477 	/*
    478 	 * not enough memory, jab the pageout daemon and wait til the coast
    479 	 * is clear
    480 	 */
    481 #ifdef DEBUG
    482 	if (swapdebug & SDB_FOLLOW)
    483 		printf("scheduler: no room for pid %d(%s), free %d\n",
    484 	   l->l_proc->p_pid, l->l_proc->p_comm, uvmexp.free);
    485 #endif
    486 	uvm_wait("schedpwait");
    487 #ifdef DEBUG
    488 	if (swapdebug & SDB_FOLLOW)
    489 		printf("scheduler: room again, free %d\n", uvmexp.free);
    490 #endif
    491 	goto loop;
    492 }
    493 
    494 /*
    495  * swappable: is LWP "l" swappable?
    496  */
    497 
    498 #define	swappable(l)							\
    499 	(((l)->l_flag & (L_INMEM)) &&					\
    500 	 ((((l)->l_proc->p_flag) & (P_SYSTEM | P_WEXIT)) == 0) &&	\
    501 	 (l)->l_holdcnt == 0)
    502 
    503 /*
    504  * swapout_threads: find threads that can be swapped and unwire their
    505  *	u-areas.
    506  *
    507  * - called by the pagedaemon
    508  * - try and swap at least one processs
    509  * - processes that are sleeping or stopped for maxslp or more seconds
    510  *   are swapped... otherwise the longest-sleeping or stopped process
    511  *   is swapped, otherwise the longest resident process...
    512  */
    513 void
    514 uvm_swapout_threads()
    515 {
    516 	struct lwp *l;
    517 	struct lwp *outl, *outl2;
    518 	int outpri, outpri2;
    519 	int didswap = 0;
    520 	extern int maxslp;
    521 	/* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
    522 
    523 #ifdef DEBUG
    524 	if (!enableswap)
    525 		return;
    526 #endif
    527 
    528 	/*
    529 	 * outl/outpri  : stop/sleep thread with largest sleeptime < maxslp
    530 	 * outl2/outpri2: the longest resident thread (its swap time)
    531 	 */
    532 	outl = outl2 = NULL;
    533 	outpri = outpri2 = 0;
    534 	proclist_lock_read();
    535 	LIST_FOREACH(l, &alllwp, l_list) {
    536 		if (!swappable(l))
    537 			continue;
    538 		switch (l->l_stat) {
    539 		case LSRUN:
    540 		case LSONPROC:
    541 			if (l->l_swtime > outpri2) {
    542 				outl2 = l;
    543 				outpri2 = l->l_swtime;
    544 			}
    545 			continue;
    546 
    547 		case LSSLEEP:
    548 		case LSSTOP:
    549 			if (l->l_slptime >= maxslp) {
    550 				uvm_swapout(l);
    551 				didswap++;
    552 			} else if (l->l_slptime > outpri) {
    553 				outl = l;
    554 				outpri = l->l_slptime;
    555 			}
    556 			continue;
    557 		}
    558 	}
    559 	proclist_unlock_read();
    560 
    561 	/*
    562 	 * If we didn't get rid of any real duds, toss out the next most
    563 	 * likely sleeping/stopped or running candidate.  We only do this
    564 	 * if we are real low on memory since we don't gain much by doing
    565 	 * it (USPACE bytes).
    566 	 */
    567 	if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
    568 		if ((l = outl) == NULL)
    569 			l = outl2;
    570 #ifdef DEBUG
    571 		if (swapdebug & SDB_SWAPOUT)
    572 			printf("swapout_threads: no duds, try procp %p\n", l);
    573 #endif
    574 		if (l)
    575 			uvm_swapout(l);
    576 	}
    577 }
    578 
    579 /*
    580  * uvm_swapout: swap out lwp "l"
    581  *
    582  * - currently "swapout" means "unwire U-area" and "pmap_collect()"
    583  *   the pmap.
    584  * - XXXCDC: should deactivate all process' private anonymous memory
    585  */
    586 
    587 static void
    588 uvm_swapout(l)
    589 	struct lwp *l;
    590 {
    591 	vaddr_t addr;
    592 	int s;
    593 	struct proc *p = l->l_proc;
    594 
    595 #ifdef DEBUG
    596 	if (swapdebug & SDB_SWAPOUT)
    597 		printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
    598 	   p->p_pid, p->p_comm, l->l_addr, l->l_stat,
    599 	   l->l_slptime, uvmexp.free);
    600 #endif
    601 
    602 	/*
    603 	 * Do any machine-specific actions necessary before swapout.
    604 	 * This can include saving floating point state, etc.
    605 	 */
    606 	cpu_swapout(l);
    607 
    608 	/*
    609 	 * Mark it as (potentially) swapped out.
    610 	 */
    611 	SCHED_LOCK(s);
    612 	s = splstatclock();
    613 	l->l_flag &= ~L_INMEM;
    614 	if (l->l_stat == LSRUN)
    615 		remrunqueue(l);
    616 	SCHED_UNLOCK(s);
    617 	l->l_swtime = 0;
    618 	++uvmexp.swapouts;
    619 
    620 	/*
    621 	 * Unwire the to-be-swapped process's user struct and kernel stack.
    622 	 */
    623 	addr = (vaddr_t)l->l_addr;
    624 	uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */
    625 	pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
    626 }
    627 
    628