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