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