Home | History | Annotate | Line # | Download | only in uvm
uvm_glue.c revision 1.1
      1 /*	$Id: uvm_glue.c,v 1.1 1998/02/05 06:25:10 mrg 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  *
     47  *
     48  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     49  * All rights reserved.
     50  *
     51  * Permission to use, copy, modify and distribute this software and
     52  * its documentation is hereby granted, provided that both the copyright
     53  * notice and this permission notice appear in all copies of the
     54  * software, derivative works or modified versions, and any portions
     55  * thereof, and that both notices appear in supporting documentation.
     56  *
     57  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     58  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     59  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     60  *
     61  * Carnegie Mellon requests users of this software to return to
     62  *
     63  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     64  *  School of Computer Science
     65  *  Carnegie Mellon University
     66  *  Pittsburgh PA 15213-3890
     67  *
     68  * any improvements or extensions that they make and grant Carnegie the
     69  * rights to redistribute these changes.
     70  */
     71 
     72 /*
     73  * uvm_glue.c: glue functions
     74  */
     75 
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/proc.h>
     80 #include <sys/resourcevar.h>
     81 #include <sys/buf.h>
     82 #include <sys/user.h>
     83 #ifdef SYSVSHM
     84 #include <sys/shm.h>
     85 #endif
     86 
     87 #include <vm/vm.h>
     88 #include <vm/vm_page.h>
     89 #include <vm/vm_kern.h>
     90 
     91 #include <uvm/uvm.h>
     92 
     93 #include <machine/cpu.h>
     94 
     95 UVMHIST_DECL(maphist);
     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 uvm_kernacc(addr, len, rw)
    121 
    122 caddr_t addr;
    123 int len, rw;
    124 
    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   rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
    133 
    134   /*
    135    * XXX there are still some things (e.g. the buffer cache) that
    136    * are managed behind the VM system's back so even though an
    137    * address is accessible in the mind of the VM system, there may
    138    * not be physical pages where the VM thinks there is.  This can
    139    * lead to bogus allocation of pages in the kernel address space
    140    * or worse, inconsistencies at the pmap level.  We only worry
    141    * about the buffer cache for now.
    142    */
    143   if (!readbuffers && rv && (eaddr > (vm_offset_t)buffers &&
    144 			     saddr < (vm_offset_t)buffers + MAXBSIZE * nbuf))
    145     rv = FALSE;
    146   return(rv);
    147 }
    148 
    149 /*
    150  * uvm_useracc: can the user access it?
    151  *
    152  * - called from physio() and sys___sysctl().
    153  */
    154 
    155 boolean_t uvm_useracc(addr, len, rw)
    156 
    157 caddr_t addr;
    158 int len, rw;
    159 
    160 {
    161   boolean_t rv;
    162   vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
    163 
    164 #if defined(i386) || defined(pc532)
    165   /*
    166    * XXX - specially disallow access to user page tables - they are
    167    * in the map.  This is here until i386 & pc532 pmaps are fixed...
    168    */
    169   if ((vm_offset_t) addr >= VM_MAXUSER_ADDRESS
    170       || (vm_offset_t) addr + len > VM_MAXUSER_ADDRESS
    171       || (vm_offset_t) addr + len <= (vm_offset_t) addr)
    172     return (FALSE);
    173 #endif
    174 
    175   rv = uvm_map_checkprot(&curproc->p_vmspace->vm_map,
    176 			trunc_page(addr), round_page(addr+len), prot);
    177   return(rv);
    178 }
    179 
    180 #ifdef KGDB
    181 /*
    182  * Change protections on kernel pages from addr to addr+len
    183  * (presumably so debugger can plant a breakpoint).
    184  *
    185  * We force the protection change at the pmap level.  If we were
    186  * to use vm_map_protect a change to allow writing would be lazily-
    187  * applied meaning we would still take a protection fault, something
    188  * we really don't want to do.  It would also fragment the kernel
    189  * map unnecessarily.  We cannot use pmap_protect since it also won't
    190  * enforce a write-enable request.  Using pmap_enter is the only way
    191  * we can ensure the change takes place properly.
    192  */
    193 void uvm_chgkprot(addr, len, rw)
    194 
    195 register caddr_t addr;
    196 int len, rw;
    197 
    198 {
    199   vm_prot_t prot;
    200   vm_offset_t pa, 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);
    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 uvm_vslock(addr, len)
    227 
    228 caddr_t	addr;
    229 u_int	len;
    230 
    231 {
    232   uvm_fault_wire(&curproc->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 uvm_vsunlock(addr, len)
    244 
    245 caddr_t	addr;
    246 u_int	len;
    247 
    248 {
    249   uvm_fault_unwire(curproc->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 uvm_fork(p1, p2, shared)
    267 
    268 struct proc *p1, *p2;
    269 boolean_t shared;
    270 
    271 {
    272   register struct user *up;
    273   vm_offset_t addr;
    274 
    275   if (shared == TRUE)
    276     uvmspace_share(p1, p2);				/* share vmspace */
    277   else
    278     p2->p_vmspace = uvmspace_fork(p1->p_vmspace);	/* fork vmspace */
    279 
    280 #if !defined(vax)
    281   /*
    282    * Allocate a wired-down (for now) pcb and kernel stack for the process
    283    * "wired" state is stored in p->p_flag's P_INMEM bit rather than in
    284    * vm_map_entry's wired count to prevent kernel_map fragmentation.
    285    */
    286   addr = uvm_km_valloc(kernel_map, USPACE);
    287   if (addr == 0)
    288     panic("uvm_fork: no more kernel virtual memory");
    289   uvm_fault_wire(kernel_map, addr, addr + USPACE);  /* P_INMEM true to start */
    290 #else
    291   /*
    292    * XXXCDC: Why does VAX need this?
    293    *
    294    * XXX somehow, on 386, ocassionally pageout removes active, wired down
    295    * kstack and pagetables, WITHOUT going thru vm_page_unwire! Why this
    296    * appears to work is not yet clear, yet it does...
    297    */
    298   addr = uvm_km_alloc(kernel_map, USPACE);
    299   if (addr == 0)
    300     panic("uvm_fork: no more kernel virtual memory");
    301 #endif
    302   up = (struct user *)addr;
    303   p2->p_addr = up;
    304 
    305   /*
    306    * p_stats and p_sigacts currently point at fields in the user
    307    * struct but not at &u, instead at p_addr.  Copy p_sigacts and
    308    * parts of p_stats; zero the rest of p_stats (statistics).
    309    */
    310   p2->p_stats = &up->u_stats;
    311   p2->p_sigacts = &up->u_sigacts;
    312   up->u_sigacts = *p1->p_sigacts;
    313   bzero(&up->u_stats.pstat_startzero,
    314 	(unsigned) ((caddr_t)&up->u_stats.pstat_endzero -
    315 		    (caddr_t)&up->u_stats.pstat_startzero));
    316   bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy,
    317 	((caddr_t)&up->u_stats.pstat_endcopy -
    318 	 (caddr_t)&up->u_stats.pstat_startcopy));
    319 
    320   /*
    321    * cpu_fork will copy and update the kernel stack and pcb, and make
    322    * the child ready to run.  The child will exit directly to user
    323    * mode on its first time slice, and will not return here.
    324    */
    325   cpu_fork(p1, p2);
    326 }
    327 
    328 /*
    329  * uvm_init_limit: init per-process VM limits
    330  *
    331  * - called for process 0 and then inherited by all others.
    332  */
    333 void uvm_init_limits(p)
    334 
    335 struct proc *p;
    336 
    337 {
    338     /*
    339      * Set up the initial limits on process VM.  Set the maximum
    340      * resident set size to be all of (reasonably) available memory.
    341      * This causes any single, large process to start random page
    342      * replacement once it fills memory.
    343      */
    344 
    345   p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
    346   p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
    347   p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
    348   p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
    349   p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
    350 }
    351 
    352 #ifdef DEBUG
    353 int	enableswap = 1;
    354 int	swapdebug = 0;
    355 #define	SDB_FOLLOW	1
    356 #define SDB_SWAPIN	2
    357 #define SDB_SWAPOUT	4
    358 #endif
    359 
    360 /*
    361  * uvm_swapin: swap in a process's u-area.
    362  */
    363 
    364 void uvm_swapin(p)
    365 
    366 struct proc *p;
    367 
    368 {
    369   vm_offset_t addr;
    370   int s;
    371 
    372   addr = (vm_offset_t)p->p_addr;
    373   uvm_fault_wire(kernel_map, addr, addr + USPACE);	/* make P_INMEM true */
    374   /*
    375    * Some architectures need to be notified when the user area has
    376    * moved to new physical page(s) (e.g.  see pmax/pmax/vm_machdep.c).
    377    */
    378   cpu_swapin(p);
    379   s = splstatclock();
    380   if (p->p_stat == SRUN)
    381     setrunqueue(p);
    382   p->p_flag |= P_INMEM;
    383   splx(s);
    384   p->p_swtime = 0;
    385   ++uvmexp.swapins;
    386 }
    387 
    388 /*
    389  * uvm_scheduler: process zero main loop
    390  *
    391  * - attempt to swapin every swaped-out, runnable process in order of
    392  *	priority.
    393  * - if not enough memory, wake the pagedaemon and let it clear space.
    394  */
    395 
    396 void uvm_scheduler()
    397 
    398 {
    399   register struct proc *p;
    400   register int pri;
    401   struct proc *pp;
    402   int ppri;
    403   UVMHIST_FUNC("uvm_scheduler"); UVMHIST_CALLED(maphist);
    404 
    405   /*XXXCDC: UVM: DISABLE SWAP */
    406   while (1) {
    407     UVMHIST_LOG(maphist,">>SLEEPING<<",0,0,0,0);
    408     tsleep((caddr_t)&proc0, PVM, "noswap", 0);
    409     UVMHIST_LOG(maphist,">>WOKE UP<<",0,0,0,0);
    410   }
    411 
    412 loop:
    413 #ifdef DEBUG
    414   while (!enableswap)
    415     tsleep((caddr_t)&proc0, PVM, "noswap", 0);
    416 #endif
    417   pp = NULL;		/* process to choose */
    418   ppri = INT_MIN;	/* its priority */
    419   for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    420 
    421     /* is it a runnable swapped out process? */
    422     if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
    423       pri = p->p_swtime + p->p_slptime	- (p->p_nice - NZERO) * 8;
    424       if (pri > ppri) {   /* higher priority?  remember it. */
    425 	pp = p;
    426 	ppri = pri;
    427       }
    428     }
    429 
    430   }
    431 
    432 #ifdef DEBUG
    433   if (swapdebug & SDB_FOLLOW)
    434     printf("scheduler: running, procp %p pri %d\n", pp, ppri);
    435 #endif
    436   /*
    437    * Nothing to do, back to sleep
    438    */
    439   if ((p = pp) == NULL) {
    440     tsleep((caddr_t)&proc0, PVM, "scheduler", 0);
    441     goto loop;
    442   }
    443 
    444   /*
    445    * we have found swapped out process which we would like to bring
    446    * back in.
    447    *
    448    * XXX: this part is really bogus cuz we could deadlock on memory
    449    * despite our feeble check
    450    */
    451   if (uvmexp.free > atop(USPACE)) {
    452 #ifdef DEBUG
    453     if (swapdebug & SDB_SWAPIN)
    454       printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
    455 	     p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free);
    456 #endif
    457     uvm_swapin(p);
    458     goto loop;
    459   }
    460   /*
    461    * not enough memory, jab the pageout daemon and wait til the coast is clear
    462    */
    463 #ifdef DEBUG
    464   if (swapdebug & SDB_FOLLOW)
    465     printf("scheduler: no room for pid %d(%s), free %d\n",
    466 	   p->p_pid, p->p_comm, uvmexp.free);
    467 #endif
    468   printf("scheduler: no room for pid %d(%s), free %d\n",
    469 	   p->p_pid, p->p_comm, uvmexp.free);/*XXXCDC: HIGHLY BOGUS */
    470   (void) splhigh();
    471   uvm_wait("schedpwait");
    472   (void) spl0();
    473 #ifdef DEBUG
    474   if (swapdebug & SDB_FOLLOW)
    475     printf("scheduler: room again, free %d\n", uvmexp.free);
    476 #endif
    477   goto loop;
    478 }
    479 
    480 /*
    481  * swappable: is process "p" swappable?
    482  */
    483 
    484 #define	swappable(p)							\
    485 	(((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM &&	\
    486 	 (p)->p_holdcnt == 0)
    487 
    488 /*
    489  * swapout_threads: find threads that can be swapped and unwire their
    490  *	u-areas.
    491  *
    492  * - called by the pagedaemon
    493  * - try and swap at least one processs
    494  * - processes that are sleeping or stopped for maxslp or more seconds
    495  *   are swapped... otherwise the longest-sleeping or stopped process
    496  *   is swapped, otherwise the longest resident process...
    497  */
    498 void uvm_swapout_threads()
    499 
    500 {
    501   register struct proc *p;
    502   struct proc *outp, *outp2;
    503   int outpri, outpri2;
    504   int didswap = 0;
    505   extern int maxslp;
    506   /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
    507 
    508   if (1)
    509     return; /*XXXCDC: UVM: NO SWAP NOW */
    510 #ifdef DEBUG
    511   if (!enableswap)
    512     return;
    513 #endif
    514 
    515   /*
    516    * outp/outpri  : stop/sleep process with largest sleeptime < maxslp
    517    * outp2/outpri2: the longest resident process (its swap time)
    518    */
    519   outp = outp2 = NULL;
    520   outpri = outpri2 = 0;
    521   for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    522     if (!swappable(p))
    523       continue;
    524     switch (p->p_stat) {
    525     case SRUN:
    526       if (p->p_swtime > outpri2) {
    527 	outp2 = p;
    528 	outpri2 = p->p_swtime;
    529       }
    530       continue;
    531 
    532     case SSLEEP:
    533     case SSTOP:
    534       if (p->p_slptime >= maxslp) {
    535 	uvm_swapout(p);			/* zap! */
    536 	didswap++;
    537       } else if (p->p_slptime > outpri) {
    538 	outp = p;
    539 	outpri = p->p_slptime;
    540       }
    541       continue;
    542     }
    543   }
    544 
    545   /*
    546    * If we didn't get rid of any real duds, toss out the next most
    547    * likely sleeping/stopped or running candidate.  We only do this
    548    * if we are real low on memory since we don't gain much by doing
    549    * it (USPACE bytes).
    550    */
    551   if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
    552     if ((p = outp) == NULL)
    553       p = outp2;
    554 #ifdef DEBUG
    555     if (swapdebug & SDB_SWAPOUT)
    556       printf("swapout_threads: no duds, try procp %p\n", p);
    557 #endif
    558     if (p)
    559       uvm_swapout(p);
    560   }
    561 }
    562 
    563 /*
    564  * uvm_swapout: swap out process "p"
    565  *
    566  * - currently "swapout" means "unwire U-area" and "pmap_collect()"
    567  *   the pmap.
    568  * - XXXCDC: should deactivate all process' private anonymous memory
    569  */
    570 
    571 static void uvm_swapout(p)
    572 
    573 register struct proc *p;
    574 
    575 {
    576   vm_offset_t addr;
    577   int s;
    578 
    579 #ifdef DEBUG
    580   if (swapdebug & SDB_SWAPOUT)
    581     printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
    582 	   p->p_pid, p->p_comm, p->p_addr, p->p_stat,
    583 	   p->p_slptime, uvmexp.free);
    584 #endif
    585 
    586   /*
    587    * Do any machine-specific actions necessary before swapout.
    588    * This can include saving floating point state, etc.
    589    */
    590   cpu_swapout(p);
    591 
    592   /*
    593    * Unwire the to-be-swapped process's user struct and kernel stack.
    594    */
    595   addr = (vm_offset_t)p->p_addr;
    596   uvm_fault_unwire(kernel_map->pmap, addr, addr + USPACE); /* !P_INMEM */
    597   pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
    598 
    599   /*
    600    * Mark it as (potentially) swapped out.
    601    */
    602   s = splstatclock();
    603   p->p_flag &= ~P_INMEM;
    604   if (p->p_stat == SRUN)
    605     remrunqueue(p);
    606   splx(s);
    607   p->p_swtime = 0;
    608   ++uvmexp.swapouts;
    609 }
    610 
    611