[BACK]Return to uvm_glue.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / uvm

Annotation of src/sys/uvm/uvm_glue.c, Revision 1.21

1.21    ! thorpej     1: /*     $NetBSD: uvm_glue.c,v 1.20 1999/05/13 21:58:38 thorpej Exp $    */
1.1       mrg         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
1.4       mrg        42:  * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
1.1       mrg        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@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:
1.5       mrg        69: #include "opt_uvmhist.h"
1.15      tron       70: #include "opt_sysv.h"
1.5       mrg        71:
1.1       mrg        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:
1.6       mrg       117: boolean_t
                    118: uvm_kernacc(addr, len, rw)
                    119:        caddr_t addr;
1.11      kleink    120:        size_t len;
                    121:        int rw;
1.6       mrg       122: {
                    123:        boolean_t rv;
1.13      eeh       124:        vaddr_t saddr, eaddr;
1.6       mrg       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:         */
1.13      eeh       142:        if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
                    143:                             saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
1.6       mrg       144:                rv = FALSE;
                    145:        return(rv);
1.1       mrg       146: }
                    147:
                    148: /*
                    149:  * uvm_useracc: can the user access it?
                    150:  *
                    151:  * - called from physio() and sys___sysctl().
                    152:  */
                    153:
1.6       mrg       154: boolean_t
                    155: uvm_useracc(addr, len, rw)
                    156:        caddr_t addr;
1.11      kleink    157:        size_t len;
                    158:        int rw;
1.1       mrg       159: {
1.6       mrg       160:        boolean_t rv;
                    161:        vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
1.1       mrg       162:
                    163: #if defined(i386) || defined(pc532)
1.6       mrg       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:         */
1.13      eeh       168:        if ((vaddr_t) addr >= VM_MAXUSER_ADDRESS
                    169:            || (vaddr_t) addr + len > VM_MAXUSER_ADDRESS
                    170:            || (vaddr_t) addr + len <= (vaddr_t) addr)
1.6       mrg       171:                return (FALSE);
1.1       mrg       172: #endif
                    173:
1.6       mrg       174:        rv = uvm_map_checkprot(&curproc->p_vmspace->vm_map,
1.1       mrg       175:                        trunc_page(addr), round_page(addr+len), prot);
1.6       mrg       176:        return(rv);
1.1       mrg       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:  */
1.6       mrg       192: void
                    193: uvm_chgkprot(addr, len, rw)
                    194:        register caddr_t addr;
1.11      kleink    195:        size_t len;
                    196:        int rw;
1.6       mrg       197: {
                    198:        vm_prot_t prot;
1.13      eeh       199:        paddr_t pa;
                    200:        vaddr_t sva, eva;
1.6       mrg       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");
1.18      mycroft   214:                pmap_enter(pmap_kernel(), sva, pa&~1, prot, TRUE, 0);
1.6       mrg       215:        }
1.1       mrg       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:
1.6       mrg       226: void
1.9       thorpej   227: uvm_vslock(p, addr, len)
                    228:        struct proc *p;
1.6       mrg       229:        caddr_t addr;
1.11      kleink    230:        size_t  len;
1.1       mrg       231: {
1.21    ! thorpej   232:
        !           233:        /* XXX VM_PROT_NONE */
1.9       thorpej   234:        uvm_fault_wire(&p->p_vmspace->vm_map, trunc_page(addr),
1.21    ! thorpej   235:            round_page(addr+len), VM_PROT_NONE);
1.1       mrg       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:
1.6       mrg       245: void
1.9       thorpej   246: uvm_vsunlock(p, addr, len)
                    247:        struct proc *p;
1.6       mrg       248:        caddr_t addr;
1.11      kleink    249:        size_t  len;
1.1       mrg       250: {
1.10      kleink    251:        uvm_fault_unwire(p->p_vmspace->vm_map.pmap, trunc_page(addr),
1.6       mrg       252:                round_page(addr+len));
1.1       mrg       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...]
1.20      thorpej   261:  * - if specified, the child gets a new user stack described by
                    262:  *     stack and stacksize
1.1       mrg       263:  * - NOTE: the kernel stack may be at a different location in the child
                    264:  *     process, and thus addresses of automatic variables may be invalid
                    265:  *     after cpu_fork returns in the child process.  We do nothing here
                    266:  *     after cpu_fork returns.
                    267:  * - XXXCDC: we need a way for this to return a failure value rather
                    268:  *   than just hang
                    269:  */
1.6       mrg       270: void
1.20      thorpej   271: uvm_fork(p1, p2, shared, stack, stacksize)
1.6       mrg       272:        struct proc *p1, *p2;
                    273:        boolean_t shared;
1.20      thorpej   274:        void *stack;
                    275:        size_t stacksize;
1.6       mrg       276: {
1.7       thorpej   277:        struct user *up = p2->p_addr;
1.6       mrg       278:        int rv;
                    279:
                    280:        if (shared == TRUE)
                    281:                uvmspace_share(p1, p2);                 /* share vmspace */
                    282:        else
                    283:                p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */
1.1       mrg       284:
1.6       mrg       285:        /*
1.7       thorpej   286:         * Wire down the U-area for the process, which contains the PCB
                    287:         * and the kernel stack.  Wired state is stored in p->p_flag's
                    288:         * P_INMEM bit rather than in the vm_map_entry's wired count
                    289:         * to prevent kernel_map fragmentation.
1.21    ! thorpej   290:         *
        !           291:         * Note the kernel stack gets read/write accesses right off
        !           292:         * the bat.
1.6       mrg       293:         */
1.13      eeh       294:        rv = uvm_fault_wire(kernel_map, (vaddr_t)up,
1.21    ! thorpej   295:            (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE);
1.6       mrg       296:        if (rv != KERN_SUCCESS)
1.8       thorpej   297:                panic("uvm_fork: uvm_fault_wire failed: %d", rv);
1.6       mrg       298:
                    299:        /*
1.19      thorpej   300:         * p_stats currently points at a field in the user struct.  Copy
                    301:         * parts of p_stats, and zero out the rest.
1.6       mrg       302:         */
                    303:        p2->p_stats = &up->u_stats;
1.12      perry     304:        memset(&up->u_stats.pstat_startzero, 0,
1.1       mrg       305:        (unsigned) ((caddr_t)&up->u_stats.pstat_endzero -
                    306:                    (caddr_t)&up->u_stats.pstat_startzero));
1.12      perry     307:        memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy,
1.1       mrg       308:        ((caddr_t)&up->u_stats.pstat_endcopy -
                    309:         (caddr_t)&up->u_stats.pstat_startcopy));
1.6       mrg       310:
                    311:        /*
                    312:         * cpu_fork will copy and update the kernel stack and pcb, and make
                    313:         * the child ready to run.  The child will exit directly to user
                    314:         * mode on its first time slice, and will not return here.
                    315:         */
1.20      thorpej   316:        cpu_fork(p1, p2, stack, stacksize);
1.14      thorpej   317: }
                    318:
                    319: /*
                    320:  * uvm_exit: exit a virtual address space
                    321:  *
                    322:  * - the process passed to us is a dead (pre-zombie) process; we
                    323:  *   are running on a different context now (the reaper).
                    324:  * - we must run in a separate thread because freeing the vmspace
                    325:  *   of the dead process may block.
                    326:  */
                    327: void
                    328: uvm_exit(p)
                    329:        struct proc *p;
                    330: {
                    331:
                    332:        uvmspace_free(p->p_vmspace);
                    333:        uvm_km_free(kernel_map, (vaddr_t)p->p_addr, USPACE);
1.1       mrg       334: }
                    335:
                    336: /*
                    337:  * uvm_init_limit: init per-process VM limits
                    338:  *
                    339:  * - called for process 0 and then inherited by all others.
                    340:  */
1.6       mrg       341: void
                    342: uvm_init_limits(p)
                    343:        struct proc *p;
                    344: {
                    345:
                    346:        /*
                    347:         * Set up the initial limits on process VM.  Set the maximum
                    348:         * resident set size to be all of (reasonably) available memory.
                    349:         * This causes any single, large process to start random page
                    350:         * replacement once it fills memory.
                    351:         */
                    352:
                    353:        p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
                    354:        p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
                    355:        p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
                    356:        p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
                    357:        p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
1.1       mrg       358: }
                    359:
                    360: #ifdef DEBUG
                    361: int    enableswap = 1;
                    362: int    swapdebug = 0;
                    363: #define        SDB_FOLLOW      1
                    364: #define SDB_SWAPIN     2
                    365: #define SDB_SWAPOUT    4
                    366: #endif
                    367:
                    368: /*
                    369:  * uvm_swapin: swap in a process's u-area.
                    370:  */
                    371:
1.6       mrg       372: void
                    373: uvm_swapin(p)
                    374:        struct proc *p;
                    375: {
1.13      eeh       376:        vaddr_t addr;
1.6       mrg       377:        int s;
                    378:
1.13      eeh       379:        addr = (vaddr_t)p->p_addr;
1.6       mrg       380:        /* make P_INMEM true */
1.21    ! thorpej   381:        uvm_fault_wire(kernel_map, addr, addr + USPACE,
        !           382:            VM_PROT_READ | VM_PROT_WRITE);
1.6       mrg       383:
                    384:        /*
                    385:         * Some architectures need to be notified when the user area has
                    386:         * moved to new physical page(s) (e.g.  see mips/mips/vm_machdep.c).
                    387:         */
                    388:        cpu_swapin(p);
                    389:        s = splstatclock();
                    390:        if (p->p_stat == SRUN)
                    391:                setrunqueue(p);
                    392:        p->p_flag |= P_INMEM;
                    393:        splx(s);
                    394:        p->p_swtime = 0;
                    395:        ++uvmexp.swapins;
1.1       mrg       396: }
                    397:
                    398: /*
                    399:  * uvm_scheduler: process zero main loop
                    400:  *
                    401:  * - attempt to swapin every swaped-out, runnable process in order of
                    402:  *     priority.
                    403:  * - if not enough memory, wake the pagedaemon and let it clear space.
                    404:  */
                    405:
1.6       mrg       406: void
                    407: uvm_scheduler()
1.1       mrg       408: {
1.6       mrg       409:        register struct proc *p;
                    410:        register int pri;
                    411:        struct proc *pp;
                    412:        int ppri;
                    413:        UVMHIST_FUNC("uvm_scheduler"); UVMHIST_CALLED(maphist);
1.1       mrg       414:
                    415: loop:
                    416: #ifdef DEBUG
1.6       mrg       417:        while (!enableswap)
                    418:                tsleep((caddr_t)&proc0, PVM, "noswap", 0);
1.1       mrg       419: #endif
1.6       mrg       420:        pp = NULL;              /* process to choose */
                    421:        ppri = INT_MIN; /* its priority */
                    422:        for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
                    423:
                    424:                /* is it a runnable swapped out process? */
                    425:                if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
                    426:                        pri = p->p_swtime + p->p_slptime -
                    427:                            (p->p_nice - NZERO) * 8;
                    428:                        if (pri > ppri) {   /* higher priority?  remember it. */
                    429:                                pp = p;
                    430:                                ppri = pri;
                    431:                        }
                    432:                }
                    433:        }
1.1       mrg       434:
                    435: #ifdef DEBUG
1.6       mrg       436:        if (swapdebug & SDB_FOLLOW)
                    437:                printf("scheduler: running, procp %p pri %d\n", pp, ppri);
1.1       mrg       438: #endif
1.6       mrg       439:        /*
                    440:         * Nothing to do, back to sleep
                    441:         */
                    442:        if ((p = pp) == NULL) {
                    443:                tsleep((caddr_t)&proc0, PVM, "scheduler", 0);
                    444:                goto loop;
                    445:        }
                    446:
                    447:        /*
                    448:         * we have found swapped out process which we would like to bring
                    449:         * back in.
                    450:         *
                    451:         * XXX: this part is really bogus cuz we could deadlock on memory
                    452:         * despite our feeble check
                    453:         */
                    454:        if (uvmexp.free > atop(USPACE)) {
1.1       mrg       455: #ifdef DEBUG
1.6       mrg       456:                if (swapdebug & SDB_SWAPIN)
                    457:                        printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
1.1       mrg       458:             p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free);
                    459: #endif
1.6       mrg       460:                uvm_swapin(p);
                    461:                goto loop;
                    462:        }
                    463:        /*
                    464:         * not enough memory, jab the pageout daemon and wait til the coast
                    465:         * is clear
                    466:         */
1.1       mrg       467: #ifdef DEBUG
1.6       mrg       468:        if (swapdebug & SDB_FOLLOW)
                    469:                printf("scheduler: no room for pid %d(%s), free %d\n",
1.1       mrg       470:           p->p_pid, p->p_comm, uvmexp.free);
                    471: #endif
1.6       mrg       472:        (void) splhigh();
                    473:        uvm_wait("schedpwait");
                    474:        (void) spl0();
1.1       mrg       475: #ifdef DEBUG
1.6       mrg       476:        if (swapdebug & SDB_FOLLOW)
                    477:                printf("scheduler: room again, free %d\n", uvmexp.free);
1.1       mrg       478: #endif
1.6       mrg       479:        goto loop;
1.1       mrg       480: }
                    481:
                    482: /*
                    483:  * swappable: is process "p" swappable?
                    484:  */
                    485:
                    486: #define        swappable(p)                                                    \
                    487:        (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM &&   \
                    488:         (p)->p_holdcnt == 0)
                    489:
                    490: /*
                    491:  * swapout_threads: find threads that can be swapped and unwire their
                    492:  *     u-areas.
                    493:  *
                    494:  * - called by the pagedaemon
                    495:  * - try and swap at least one processs
                    496:  * - processes that are sleeping or stopped for maxslp or more seconds
                    497:  *   are swapped... otherwise the longest-sleeping or stopped process
                    498:  *   is swapped, otherwise the longest resident process...
                    499:  */
1.6       mrg       500: void
                    501: uvm_swapout_threads()
1.1       mrg       502: {
1.6       mrg       503:        register struct proc *p;
                    504:        struct proc *outp, *outp2;
                    505:        int outpri, outpri2;
                    506:        int didswap = 0;
                    507:        extern int maxslp;
                    508:        /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
1.1       mrg       509:
                    510: #ifdef DEBUG
1.6       mrg       511:        if (!enableswap)
                    512:                return;
1.1       mrg       513: #endif
                    514:
1.6       mrg       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;
1.1       mrg       531:
1.6       mrg       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;
1.1       mrg       554: #ifdef DEBUG
1.6       mrg       555:                if (swapdebug & SDB_SWAPOUT)
                    556:                        printf("swapout_threads: no duds, try procp %p\n", p);
1.1       mrg       557: #endif
1.6       mrg       558:                if (p)
                    559:                        uvm_swapout(p);
                    560:        }
1.1       mrg       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:
1.6       mrg       571: static void
                    572: uvm_swapout(p)
                    573:        register struct proc *p;
1.1       mrg       574: {
1.13      eeh       575:        vaddr_t addr;
1.6       mrg       576:        int s;
1.1       mrg       577:
                    578: #ifdef DEBUG
1.6       mrg       579:        if (swapdebug & SDB_SWAPOUT)
                    580:                printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
1.1       mrg       581:           p->p_pid, p->p_comm, p->p_addr, p->p_stat,
                    582:           p->p_slptime, uvmexp.free);
                    583: #endif
                    584:
1.6       mrg       585:        /*
                    586:         * Do any machine-specific actions necessary before swapout.
                    587:         * This can include saving floating point state, etc.
                    588:         */
                    589:        cpu_swapout(p);
                    590:
                    591:        /*
                    592:         * Unwire the to-be-swapped process's user struct and kernel stack.
                    593:         */
1.13      eeh       594:        addr = (vaddr_t)p->p_addr;
1.6       mrg       595:        uvm_fault_unwire(kernel_map->pmap, addr, addr + USPACE); /* !P_INMEM */
                    596:        pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
                    597:
                    598:        /*
                    599:         * Mark it as (potentially) swapped out.
                    600:         */
                    601:        s = splstatclock();
                    602:        p->p_flag &= ~P_INMEM;
                    603:        if (p->p_stat == SRUN)
                    604:                remrunqueue(p);
                    605:        splx(s);
                    606:        p->p_swtime = 0;
                    607:        ++uvmexp.swapouts;
1.1       mrg       608: }
                    609:

CVSweb <webmaster@jp.NetBSD.org>