Home | History | Annotate | Line # | Download | only in uvm
uvm_glue.c revision 1.37
      1 /*	$NetBSD: uvm_glue.c,v 1.37 2000/06/26 14:21:17 mrg 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_uvmhist.h"
     70 #include "opt_sysv.h"
     71 
     72 /*
     73  * uvm_glue.c: glue functions
     74  */
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 #include <sys/resourcevar.h>
     80 #include <sys/buf.h>
     81 #include <sys/user.h>
     82 #ifdef SYSVSHM
     83 #include <sys/shm.h>
     84 #endif
     85 
     86 #include <vm/vm.h>
     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 proc *));
     97 
     98 /*
     99  * XXXCDC: do these really belong here?
    100  */
    101 
    102 unsigned maxdmap = MAXDSIZ;	/* kern_resource.c: RLIMIT_DATA max */
    103 unsigned maxsmap = MAXSSIZ;	/* kern_resource.c: RLIMIT_STACK max */
    104 
    105 int readbuffers = 0;		/* allow KGDB to read kern buffer pool */
    106 				/* XXX: see uvm_kernacc */
    107 
    108 
    109 /*
    110  * uvm_kernacc: can the kernel access a region of memory
    111  *
    112  * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
    113  */
    114 
    115 boolean_t
    116 uvm_kernacc(addr, len, rw)
    117 	caddr_t addr;
    118 	size_t len;
    119 	int rw;
    120 {
    121 	boolean_t rv;
    122 	vaddr_t saddr, eaddr;
    123 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
    124 
    125 	saddr = trunc_page((vaddr_t)addr);
    126 	eaddr = round_page((vaddr_t)addr+len);
    127 	vm_map_lock_read(kernel_map);
    128 	rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
    129 	vm_map_unlock_read(kernel_map);
    130 
    131 	/*
    132 	 * XXX there are still some things (e.g. the buffer cache) that
    133 	 * are managed behind the VM system's back so even though an
    134 	 * address is accessible in the mind of the VM system, there may
    135 	 * not be physical pages where the VM thinks there is.  This can
    136 	 * lead to bogus allocation of pages in the kernel address space
    137 	 * or worse, inconsistencies at the pmap level.  We only worry
    138 	 * about the buffer cache for now.
    139 	 */
    140 	if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
    141 			     saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
    142 		rv = FALSE;
    143 	return(rv);
    144 }
    145 
    146 /*
    147  * uvm_useracc: can the user access it?
    148  *
    149  * - called from physio() and sys___sysctl().
    150  */
    151 
    152 boolean_t
    153 uvm_useracc(addr, len, rw)
    154 	caddr_t addr;
    155 	size_t len;
    156 	int rw;
    157 {
    158 	vm_map_t map;
    159 	boolean_t rv;
    160 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
    161 
    162 	/* XXX curproc */
    163 	map = &curproc->p_vmspace->vm_map;
    164 
    165 	vm_map_lock_read(map);
    166 	rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr),
    167 	    round_page((vaddr_t)addr+len), prot);
    168 	vm_map_unlock_read(map);
    169 
    170 	return(rv);
    171 }
    172 
    173 #ifdef KGDB
    174 /*
    175  * Change protections on kernel pages from addr to addr+len
    176  * (presumably so debugger can plant a breakpoint).
    177  *
    178  * We force the protection change at the pmap level.  If we were
    179  * to use vm_map_protect a change to allow writing would be lazily-
    180  * applied meaning we would still take a protection fault, something
    181  * we really don't want to do.  It would also fragment the kernel
    182  * map unnecessarily.  We cannot use pmap_protect since it also won't
    183  * enforce a write-enable request.  Using pmap_enter is the only way
    184  * we can ensure the change takes place properly.
    185  */
    186 void
    187 uvm_chgkprot(addr, len, rw)
    188 	caddr_t addr;
    189 	size_t len;
    190 	int rw;
    191 {
    192 	vm_prot_t prot;
    193 	paddr_t pa;
    194 	vaddr_t sva, eva;
    195 
    196 	prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
    197 	eva = round_page((vaddr_t)addr + len);
    198 	for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
    199 		/*
    200 		 * Extract physical address for the page.
    201 		 * We use a cheezy hack to differentiate physical
    202 		 * page 0 from an invalid mapping, not that it
    203 		 * really matters...
    204 		 */
    205 		if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE)
    206 			panic("chgkprot: invalid page");
    207 		pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
    208 	}
    209 }
    210 #endif
    211 
    212 /*
    213  * vslock: wire user memory for I/O
    214  *
    215  * - called from physio and sys___sysctl
    216  * - XXXCDC: consider nuking this (or making it a macro?)
    217  */
    218 
    219 int
    220 uvm_vslock(p, addr, len, access_type)
    221 	struct proc *p;
    222 	caddr_t	addr;
    223 	size_t	len;
    224 	vm_prot_t access_type;
    225 {
    226 	vm_map_t map;
    227 	vaddr_t start, end;
    228 	int rv;
    229 
    230 	map = &p->p_vmspace->vm_map;
    231 	start = trunc_page((vaddr_t)addr);
    232 	end = round_page((vaddr_t)addr + len);
    233 
    234 	rv = uvm_fault_wire(map, start, end, access_type);
    235 
    236 	return (rv);
    237 }
    238 
    239 /*
    240  * vslock: wire user memory for I/O
    241  *
    242  * - called from physio and sys___sysctl
    243  * - XXXCDC: consider nuking this (or making it a macro?)
    244  */
    245 
    246 void
    247 uvm_vsunlock(p, addr, len)
    248 	struct proc *p;
    249 	caddr_t	addr;
    250 	size_t	len;
    251 {
    252 	uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr),
    253 		round_page((vaddr_t)addr+len));
    254 }
    255 
    256 /*
    257  * uvm_fork: fork a virtual address space
    258  *
    259  * - the address space is copied as per parent map's inherit values
    260  * - a new "user" structure is allocated for the child process
    261  *	[filled in by MD layer...]
    262  * - if specified, the child gets a new user stack described by
    263  *	stack and stacksize
    264  * - NOTE: the kernel stack may be at a different location in the child
    265  *	process, and thus addresses of automatic variables may be invalid
    266  *	after cpu_fork returns in the child process.  We do nothing here
    267  *	after cpu_fork returns.
    268  * - XXXCDC: we need a way for this to return a failure value rather
    269  *   than just hang
    270  */
    271 void
    272 uvm_fork(p1, p2, shared, stack, stacksize, func, arg)
    273 	struct proc *p1, *p2;
    274 	boolean_t shared;
    275 	void *stack;
    276 	size_t stacksize;
    277 	void (*func) __P((void *));
    278 	void *arg;
    279 {
    280 	struct user *up = p2->p_addr;
    281 	int rv;
    282 
    283 	if (shared == TRUE)
    284 		uvmspace_share(p1, p2);			/* share vmspace */
    285 	else
    286 		p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */
    287 
    288 	/*
    289 	 * Wire down the U-area for the process, which contains the PCB
    290 	 * and the kernel stack.  Wired state is stored in p->p_flag's
    291 	 * P_INMEM bit rather than in the vm_map_entry's wired count
    292 	 * to prevent kernel_map fragmentation.
    293 	 *
    294 	 * Note the kernel stack gets read/write accesses right off
    295 	 * the bat.
    296 	 */
    297 	rv = uvm_fault_wire(kernel_map, (vaddr_t)up,
    298 	    (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE);
    299 	if (rv != KERN_SUCCESS)
    300 		panic("uvm_fork: uvm_fault_wire failed: %d", rv);
    301 
    302 	/*
    303 	 * p_stats currently points at a field in the user struct.  Copy
    304 	 * parts of p_stats, and zero out the rest.
    305 	 */
    306 	p2->p_stats = &up->u_stats;
    307 	memset(&up->u_stats.pstat_startzero, 0,
    308 	(unsigned) ((caddr_t)&up->u_stats.pstat_endzero -
    309 		    (caddr_t)&up->u_stats.pstat_startzero));
    310 	memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy,
    311 	((caddr_t)&up->u_stats.pstat_endcopy -
    312 	 (caddr_t)&up->u_stats.pstat_startcopy));
    313 
    314 	/*
    315 	 * cpu_fork() copy and update the pcb, and make the child ready
    316 	 * to run.  If this is a normal user fork, the child will exit
    317 	 * directly to user mode via child_return() on its first time
    318 	 * slice and will not return here.  If this is a kernel thread,
    319 	 * the specified entry point will be executed.
    320 	 */
    321 	cpu_fork(p1, p2, stack, stacksize, func, arg);
    322 }
    323 
    324 /*
    325  * uvm_exit: exit a virtual address space
    326  *
    327  * - the process passed to us is a dead (pre-zombie) process; we
    328  *   are running on a different context now (the reaper).
    329  * - we must run in a separate thread because freeing the vmspace
    330  *   of the dead process may block.
    331  */
    332 void
    333 uvm_exit(p)
    334 	struct proc *p;
    335 {
    336 
    337 	uvmspace_free(p->p_vmspace);
    338 	uvm_km_free(kernel_map, (vaddr_t)p->p_addr, USPACE);
    339 	p->p_addr = NULL;
    340 }
    341 
    342 /*
    343  * uvm_init_limit: init per-process VM limits
    344  *
    345  * - called for process 0 and then inherited by all others.
    346  */
    347 void
    348 uvm_init_limits(p)
    349 	struct proc *p;
    350 {
    351 
    352 	/*
    353 	 * Set up the initial limits on process VM.  Set the maximum
    354 	 * resident set size to be all of (reasonably) available memory.
    355 	 * This causes any single, large process to start random page
    356 	 * replacement once it fills memory.
    357 	 */
    358 
    359 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
    360 	p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
    361 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
    362 	p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
    363 	p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
    364 }
    365 
    366 #ifdef DEBUG
    367 int	enableswap = 1;
    368 int	swapdebug = 0;
    369 #define	SDB_FOLLOW	1
    370 #define SDB_SWAPIN	2
    371 #define SDB_SWAPOUT	4
    372 #endif
    373 
    374 /*
    375  * uvm_swapin: swap in a process's u-area.
    376  */
    377 
    378 void
    379 uvm_swapin(p)
    380 	struct proc *p;
    381 {
    382 	vaddr_t addr;
    383 	int s;
    384 
    385 	addr = (vaddr_t)p->p_addr;
    386 	/* make P_INMEM true */
    387 	uvm_fault_wire(kernel_map, addr, addr + USPACE,
    388 	    VM_PROT_READ | VM_PROT_WRITE);
    389 
    390 	/*
    391 	 * Some architectures need to be notified when the user area has
    392 	 * moved to new physical page(s) (e.g.  see mips/mips/vm_machdep.c).
    393 	 */
    394 	cpu_swapin(p);
    395 	s = splstatclock();
    396 	if (p->p_stat == SRUN)
    397 		setrunqueue(p);
    398 	p->p_flag |= P_INMEM;
    399 	splx(s);
    400 	p->p_swtime = 0;
    401 	++uvmexp.swapins;
    402 }
    403 
    404 /*
    405  * uvm_scheduler: process zero main loop
    406  *
    407  * - attempt to swapin every swaped-out, runnable process in order of
    408  *	priority.
    409  * - if not enough memory, wake the pagedaemon and let it clear space.
    410  */
    411 
    412 void
    413 uvm_scheduler()
    414 {
    415 	struct proc *p;
    416 	int pri;
    417 	struct proc *pp;
    418 	int ppri;
    419 	UVMHIST_FUNC("uvm_scheduler"); UVMHIST_CALLED(maphist);
    420 
    421 loop:
    422 #ifdef DEBUG
    423 	while (!enableswap)
    424 		tsleep((caddr_t)&proc0, PVM, "noswap", 0);
    425 #endif
    426 	pp = NULL;		/* process to choose */
    427 	ppri = INT_MIN;	/* its priority */
    428 	proclist_lock_read();
    429 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    430 
    431 		/* is it a runnable swapped out process? */
    432 		if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
    433 			pri = p->p_swtime + p->p_slptime -
    434 			    (p->p_nice - NZERO) * 8;
    435 			if (pri > ppri) {   /* higher priority?  remember it. */
    436 				pp = p;
    437 				ppri = pri;
    438 			}
    439 		}
    440 	}
    441 	proclist_unlock_read();
    442 
    443 #ifdef DEBUG
    444 	if (swapdebug & SDB_FOLLOW)
    445 		printf("scheduler: running, procp %p pri %d\n", pp, ppri);
    446 #endif
    447 	/*
    448 	 * Nothing to do, back to sleep
    449 	 */
    450 	if ((p = pp) == NULL) {
    451 		tsleep((caddr_t)&proc0, PVM, "scheduler", 0);
    452 		goto loop;
    453 	}
    454 
    455 	/*
    456 	 * we have found swapped out process which we would like to bring
    457 	 * back in.
    458 	 *
    459 	 * XXX: this part is really bogus cuz we could deadlock on memory
    460 	 * despite our feeble check
    461 	 */
    462 	if (uvmexp.free > atop(USPACE)) {
    463 #ifdef DEBUG
    464 		if (swapdebug & SDB_SWAPIN)
    465 			printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
    466 	     p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free);
    467 #endif
    468 		uvm_swapin(p);
    469 		goto loop;
    470 	}
    471 	/*
    472 	 * not enough memory, jab the pageout daemon and wait til the coast
    473 	 * is clear
    474 	 */
    475 #ifdef DEBUG
    476 	if (swapdebug & SDB_FOLLOW)
    477 		printf("scheduler: no room for pid %d(%s), free %d\n",
    478 	   p->p_pid, p->p_comm, uvmexp.free);
    479 #endif
    480 	(void) splhigh();
    481 	uvm_wait("schedpwait");
    482 	(void) spl0();
    483 #ifdef DEBUG
    484 	if (swapdebug & SDB_FOLLOW)
    485 		printf("scheduler: room again, free %d\n", uvmexp.free);
    486 #endif
    487 	goto loop;
    488 }
    489 
    490 /*
    491  * swappable: is process "p" swappable?
    492  */
    493 
    494 #define	swappable(p)							\
    495 	(((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM &&	\
    496 	 (p)->p_holdcnt == 0)
    497 
    498 /*
    499  * swapout_threads: find threads that can be swapped and unwire their
    500  *	u-areas.
    501  *
    502  * - called by the pagedaemon
    503  * - try and swap at least one processs
    504  * - processes that are sleeping or stopped for maxslp or more seconds
    505  *   are swapped... otherwise the longest-sleeping or stopped process
    506  *   is swapped, otherwise the longest resident process...
    507  */
    508 void
    509 uvm_swapout_threads()
    510 {
    511 	struct proc *p;
    512 	struct proc *outp, *outp2;
    513 	int outpri, outpri2;
    514 	int didswap = 0;
    515 	extern int maxslp;
    516 	/* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
    517 
    518 #ifdef DEBUG
    519 	if (!enableswap)
    520 		return;
    521 #endif
    522 
    523 	/*
    524 	 * outp/outpri  : stop/sleep process with largest sleeptime < maxslp
    525 	 * outp2/outpri2: the longest resident process (its swap time)
    526 	 */
    527 	outp = outp2 = NULL;
    528 	outpri = outpri2 = 0;
    529 	proclist_lock_read();
    530 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    531 		if (!swappable(p))
    532 			continue;
    533 		switch (p->p_stat) {
    534 		case SRUN:
    535 		case SONPROC:
    536 			if (p->p_swtime > outpri2) {
    537 				outp2 = p;
    538 				outpri2 = p->p_swtime;
    539 			}
    540 			continue;
    541 
    542 		case SSLEEP:
    543 		case SSTOP:
    544 			if (p->p_slptime >= maxslp) {
    545 				uvm_swapout(p);			/* zap! */
    546 				didswap++;
    547 			} else if (p->p_slptime > outpri) {
    548 				outp = p;
    549 				outpri = p->p_slptime;
    550 			}
    551 			continue;
    552 		}
    553 	}
    554 	proclist_unlock_read();
    555 
    556 	/*
    557 	 * If we didn't get rid of any real duds, toss out the next most
    558 	 * likely sleeping/stopped or running candidate.  We only do this
    559 	 * if we are real low on memory since we don't gain much by doing
    560 	 * it (USPACE bytes).
    561 	 */
    562 	if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
    563 		if ((p = outp) == NULL)
    564 			p = outp2;
    565 #ifdef DEBUG
    566 		if (swapdebug & SDB_SWAPOUT)
    567 			printf("swapout_threads: no duds, try procp %p\n", p);
    568 #endif
    569 		if (p)
    570 			uvm_swapout(p);
    571 	}
    572 }
    573 
    574 /*
    575  * uvm_swapout: swap out process "p"
    576  *
    577  * - currently "swapout" means "unwire U-area" and "pmap_collect()"
    578  *   the pmap.
    579  * - XXXCDC: should deactivate all process' private anonymous memory
    580  */
    581 
    582 static void
    583 uvm_swapout(p)
    584 	struct proc *p;
    585 {
    586 	vaddr_t addr;
    587 	int s;
    588 
    589 #ifdef DEBUG
    590 	if (swapdebug & SDB_SWAPOUT)
    591 		printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
    592 	   p->p_pid, p->p_comm, p->p_addr, p->p_stat,
    593 	   p->p_slptime, uvmexp.free);
    594 #endif
    595 
    596 	/*
    597 	 * Do any machine-specific actions necessary before swapout.
    598 	 * This can include saving floating point state, etc.
    599 	 */
    600 	cpu_swapout(p);
    601 
    602 	/*
    603 	 * Unwire the to-be-swapped process's user struct and kernel stack.
    604 	 */
    605 	addr = (vaddr_t)p->p_addr;
    606 	uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */
    607 	pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
    608 
    609 	/*
    610 	 * Mark it as (potentially) swapped out.
    611 	 */
    612 	s = splstatclock();
    613 	p->p_flag &= ~P_INMEM;
    614 	if (p->p_stat == SRUN)
    615 		remrunqueue(p);
    616 	splx(s);
    617 	p->p_swtime = 0;
    618 	++uvmexp.swapouts;
    619 }
    620 
    621