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