fix includes; new proc struct; rm *_NULL;
authorMike Karels <karels@ucbvax.Berkeley.EDU>
Sun, 21 Apr 1991 01:48:04 +0000 (17:48 -0800)
committerMike Karels <karels@ucbvax.Berkeley.EDU>
Sun, 21 Apr 1991 01:48:04 +0000 (17:48 -0800)
procdup => vm_fork; clean up sched, swapout

SCCS-vsn: sys/vm/vm_glue.c 7.2

usr/src/sys/vm/vm_glue.c

index ddb74c0..6321fcf 100644 (file)
@@ -9,19 +9,19 @@
  * The CMU software License Agreement specifies the terms and conditions
  * for use and redistribution.
  *
  * The CMU software License Agreement specifies the terms and conditions
  * for use and redistribution.
  *
- *     @(#)vm_glue.c   7.1 (Berkeley) %G%
+ *     @(#)vm_glue.c   7.2 (Berkeley) %G%
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "user.h"
 #include "proc.h"
 #include "proc.h"
+#include "resourcevar.h"
 #include "buf.h"
 #include "buf.h"
+#include "user.h"
 
 
-#include "../vm/vm_param.h"
-#include "../vm/vm_map.h"
-#include "../vm/vm_page.h"
-#include "../vm/vm_kern.h"
+#include "vm.h"
+#include "vm_page.h"
+#include "vm_kern.h"
 
 int    avefree = 0;            /* XXX */
 unsigned maxdmap = MAXDSIZ;    /* XXX */
 
 int    avefree = 0;            /* XXX */
 unsigned maxdmap = MAXDSIZ;    /* XXX */
@@ -45,8 +45,8 @@ useracc(addr, len, rw)
        boolean_t rv;
        vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
 
        boolean_t rv;
        vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
 
-       rv = vm_map_check_protection(u.u_procp->p_map, trunc_page(addr),
-                                    round_page(addr+len-1), prot);
+       rv = vm_map_check_protection(&curproc->p_vmspace->vm_map,
+           trunc_page(addr), round_page(addr+len-1), prot);
        return(rv == TRUE);
 }
 
        return(rv == TRUE);
 }
 
@@ -71,7 +71,7 @@ vslock(addr, len)
        caddr_t addr;
        u_int   len;
 {
        caddr_t addr;
        u_int   len;
 {
-       vm_map_pageable(u.u_procp->p_map, trunc_page(addr),
+       vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr),
                        round_page(addr+len-1), FALSE);
 }
 
                        round_page(addr+len-1), FALSE);
 }
 
@@ -83,65 +83,56 @@ vsunlock(addr, len, dirtied)
 #ifdef lint
        dirtied++;
 #endif lint
 #ifdef lint
        dirtied++;
 #endif lint
-       vm_map_pageable(u.u_procp->p_map, trunc_page(addr),
+       vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr),
                        round_page(addr+len-1), TRUE);
 }
 
                        round_page(addr+len-1), TRUE);
 }
 
-procdup(p, isvfork)
-       register struct proc *p;
+vm_fork(p1, p2, isvfork)
+       register struct proc *p1, *p2;
        int isvfork;
 {
        register struct user *up;
        vm_offset_t addr;
        vm_size_t size;
 
        int isvfork;
 {
        register struct user *up;
        vm_offset_t addr;
        vm_size_t size;
 
-#if 0
-       /*
-        * Duplicate the process address space.
-        * XXX if this is a vfork we arrange to share data/stack to
-        *     preserve brain-dead semantics of vfork().
-        * XXX this doesn't work due to a bug in the VM code.
-        *     Once a process has done a vfork setting up sharing maps,
-        *     any future forks may fail as the source VM range doesn't
-        *     properly get write-protected.  This causes the parent to
-        *     not create copies and instead modifies the originals.
-        *     If the parent activates before the child, the child will
-        *     get a corrupted address space.
-        */
-       if (isvfork) {
-               addr = trunc_page(u.u_daddr);
-               size = ctob(u.u_dsize);
-               (void) vm_map_inherit(u.u_procp->p_map, addr,
-                                     addr + size, VM_INHERIT_SHARE);
-               (void) vm_map_inherit(u.u_procp->p_map, u.u_maxsaddr,
-                                     VM_MAX_ADDRESS, VM_INHERIT_SHARE);
-       }
-#endif
-       p->p_map = vm_map_fork(u.u_procp->p_map);
-#if 0
-       if (isvfork) {
-               (void) vm_map_inherit(u.u_procp->p_map, addr,
-                                     addr + size, VM_INHERIT_COPY);
-               (void) vm_map_inherit(u.u_procp->p_map, u.u_maxsaddr,
-                                     VM_MAX_ADDRESS, VM_INHERIT_COPY);
-       }
+       p2->p_vmspace = vmspace_fork(p1->p_vmspace);
+
+#ifdef SYSVSHM
+       if (p1->p_vmspace->vm_shm)
+               shmfork(p1, p2, isvfork);
 #endif
 #endif
+
        /*
         * Allocate a wired-down (for now) u-area for the process
         */
        size = round_page(ctob(UPAGES));
        addr = kmem_alloc_pageable(kernel_map, size);
        /*
         * Allocate a wired-down (for now) u-area for the process
         */
        size = round_page(ctob(UPAGES));
        addr = kmem_alloc_pageable(kernel_map, size);
-       vm_map_pageable(kernel_map, addr, addr+size, FALSE);
-       p->p_addr = (caddr_t)addr;
+       vm_map_pageable(kernel_map, addr, addr + size, FALSE);
+       p2->p_addr = (caddr_t)addr;
        up = (struct user *)addr;
 
        /*
         * Update the current u-area and copy it to the new one
        up = (struct user *)addr;
 
        /*
         * Update the current u-area and copy it to the new one
+        * THIS SHOULD BE DONE DIFFERENTLY, probably with a single
+        * machine-dependent call that copies and updates the pcb+stack,
+        * replacing the resume and savectx.
+        */
+       resume(pcbb(p1));
+       bcopy(p1->p_addr, p2->p_addr, size);
+       /*
+        * p_stats and p_sigacts currently point at fields
+        * in the user struct but not at &u, instead at p_addr.
         */
         */
-       resume(pcbb(u.u_procp));
-       bcopy(u.u_procp->p_addr, p->p_addr, size);
-       up->u_procp = p;
-       PMAP_ACTIVATE(p->p_map->pmap, (struct pcb *)p->p_addr);
+       p2->p_stats = &((struct user *)p2->p_addr)->u_stats;
+       p2->p_sigacts = &((struct user *)p2->p_addr)->u_sigacts;
+
+       /*
+        * Clear vm statistics of new process.
+        */
+       bzero((caddr_t)&up->u_stats.p_ru, sizeof (struct rusage));
+       bzero((caddr_t)&up->u_stats.p_cru, sizeof (struct rusage));
+
+       PMAP_ACTIVATE(&p2->p_vmspace->vm_pmap, (struct pcb *)p2->p_addr, 0);
 
        /*
         * Arrange for a non-local goto when the new process
 
        /*
         * Arrange for a non-local goto when the new process
@@ -154,21 +145,17 @@ procdup(p, isvfork)
                 */
                return (1);
        }
                 */
                return (1);
        }
-
-       /*
-        * Clear vm statistics of new process.
-        */
-       bzero((caddr_t)&up->u_ru, sizeof (struct rusage));
-       bzero((caddr_t)&up->u_cru, sizeof (struct rusage));
-       up->u_outime = 0;
        return (0);
 }
 
 /*
        return (0);
 }
 
 /*
- * XXX Scaled down version from vm_page.c
+ * Set default limits for VM system.
+ * Called for proc 0, and then inherited by all others.
  */
  */
-vminit()
+vm_init_limits(p)
+       register struct proc *p;
 {
 {
+
        /*
         * Set up the initial limits on process VM.
         * Set the maximum resident set size to be all
        /*
         * Set up the initial limits on process VM.
         * Set the maximum resident set size to be all
@@ -176,13 +163,12 @@ vminit()
         * any single, large process to start random page
         * replacement once it fills memory.
         */
         * any single, large process to start random page
         * replacement once it fills memory.
         */
-        u.u_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
-        u.u_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
-        u.u_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
-        u.u_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
-       u.u_rlimit[RLIMIT_RSS].rlim_cur = u.u_rlimit[RLIMIT_RSS].rlim_max =
+        p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
+        p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
+        p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
+        p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
+       p->p_rlimit[RLIMIT_RSS].rlim_cur = p->p_rlimit[RLIMIT_RSS].rlim_max =
                ptoa(vm_page_free_count);
                ptoa(vm_page_free_count);
-       proc[0].p_maxrss = vm_page_free_count;
 }
 
 #include "../vm/vm_pageout.h"
 }
 
 #include "../vm/vm_pageout.h"
@@ -204,68 +190,64 @@ int       swapdebug = 0;
  */
 sched()
 {
  */
 sched()
 {
-       register struct proc *rp;
-       register int rppri;
-       struct proc *inp;
-       int inpri;
+       register struct proc *p;
+       register int pri;
+       struct proc *pp;
+       int ppri;
        vm_offset_t addr;
        vm_size_t size;
 
 loop:
 #ifdef DEBUG
        if (!enableswap) {
        vm_offset_t addr;
        vm_size_t size;
 
 loop:
 #ifdef DEBUG
        if (!enableswap) {
-               inp = NULL;
+               pp = NULL;
                goto noswap;
        }
 #endif
                goto noswap;
        }
 #endif
-       wantin = 0;
-       inp = NULL;
-       inpri = -20000;
-       for (rp = allproc; rp != NULL; rp = rp->p_nxt)
-               if (rp->p_stat == SRUN && (rp->p_flag & SLOAD) == 0) {
-                       rppri = rp->p_time +
-                               rp->p_slptime - (rp->p_nice-NZERO)*8;
-                       if (rppri > inpri) {
-                               inp = rp;
-                               inpri = rppri;
+       pp = NULL;
+       ppri = INT_MIN;
+       for (p = allproc; p != NULL; p = p->p_nxt)
+               if (p->p_stat == SRUN && (p->p_flag & SLOAD) == 0) {
+                       pri = p->p_time + p->p_slptime - p->p_nice * 8;
+                       if (pri > ppri) {
+                               pp = p;
+                               ppri = pri;
                        }
                }
 #ifdef DEBUG
        if (swapdebug & SDB_FOLLOW)
                        }
                }
 #ifdef DEBUG
        if (swapdebug & SDB_FOLLOW)
-               printf("sched: running, procp %x pri %d\n", inp, inpri);
+               printf("sched: running, procp %x pri %d\n", pp, ppri);
 noswap:
 #endif
        /*
         * Nothing to do, back to sleep
         */
 noswap:
 #endif
        /*
         * Nothing to do, back to sleep
         */
-       if ((rp = inp) == NULL) {
-               (void) splhigh();
-               runout++;
-               sleep((caddr_t)&runout, PVM);
-               (void) spl0();
+       if ((p = pp) == NULL) {
+               sleep((caddr_t)&proc0, PVM);
                goto loop;
        }
                goto loop;
        }
+
        /*
         * We would like to bring someone in.
         * This part is really bogus cuz we could deadlock on memory
         * despite our feeble check.
         */
        size = round_page(ctob(UPAGES));
        /*
         * We would like to bring someone in.
         * This part is really bogus cuz we could deadlock on memory
         * despite our feeble check.
         */
        size = round_page(ctob(UPAGES));
-       addr = (vm_offset_t) rp->p_addr;
+       addr = (vm_offset_t) p->p_addr;
        if (vm_page_free_count > atop(size)) {
 #ifdef DEBUG
                if (swapdebug & SDB_SWAPIN)
                        printf("swapin: pid %d(%s)@%x, pri %d free %d\n",
        if (vm_page_free_count > atop(size)) {
 #ifdef DEBUG
                if (swapdebug & SDB_SWAPIN)
                        printf("swapin: pid %d(%s)@%x, pri %d free %d\n",
-                              rp->p_pid, rp->p_comm, rp->p_addr,
-                              inpri, vm_page_free_count);
+                              p->p_pid, p->p_comm, p->p_addr,
+                              ppri, vm_page_free_count);
 #endif
                vm_map_pageable(kernel_map, addr, addr+size, FALSE);
                (void) splclock();
 #endif
                vm_map_pageable(kernel_map, addr, addr+size, FALSE);
                (void) splclock();
-               if (rp->p_stat == SRUN)
-                       setrq(rp);
-               rp->p_flag |= SLOAD;
+               if (p->p_stat == SRUN)
+                       setrq(p);
+               p->p_flag |= SLOAD;
                (void) spl0();
                (void) spl0();
-               rp->p_time = 0;
+               p->p_time = 0;
                goto loop;
        }
        /*
                goto loop;
        }
        /*
@@ -275,7 +257,7 @@ noswap:
 #ifdef DEBUG
        if (swapdebug & SDB_FOLLOW)
                printf("sched: no room for pid %d(%s), free %d\n",
 #ifdef DEBUG
        if (swapdebug & SDB_FOLLOW)
                printf("sched: no room for pid %d(%s), free %d\n",
-                      rp->p_pid, rp->p_comm, vm_page_free_count);
+                      p->p_pid, p->p_comm, vm_page_free_count);
 #endif
        (void) splhigh();
        VM_WAIT;
 #endif
        (void) splhigh();
        VM_WAIT;
@@ -288,16 +270,19 @@ noswap:
 }
 
 #define        swappable(p) \
 }
 
 #define        swappable(p) \
-       (((p)->p_flag & (SSYS|SULOCK|SLOAD|SKEEP|SWEXIT|SPHYSIO)) == SLOAD)
+       (((p)->p_flag & (SSYS|SLOAD|SKEEP|SWEXIT|SPHYSIO)) == SLOAD)
 
 /*
  * Swapout is driven by the pageout daemon.  Very simple, we find eligible
  * procs and unwire their u-areas.  We try to always "swap" at least one
  * process in case we need the room for a swapin.
 
 /*
  * Swapout is driven by the pageout daemon.  Very simple, we find eligible
  * procs and unwire their u-areas.  We try to always "swap" at least one
  * process in case we need the room for a swapin.
+ * If any procs have been sleeping/stopped for at least maxslp seconds,
+ * they are swapped.  Else, we swap the longest-sleeping or stopped process,
+ * if any, otherwise the longest-resident process.
  */
 swapout_threads()
 {
  */
 swapout_threads()
 {
-       register struct proc *rp;
+       register struct proc *p;
        struct proc *outp, *outp2;
        int outpri, outpri2;
        int didswap = 0;
        struct proc *outp, *outp2;
        int outpri, outpri2;
        int didswap = 0;
@@ -308,26 +293,26 @@ swapout_threads()
                return;
 #endif
        outp = outp2 = NULL;
                return;
 #endif
        outp = outp2 = NULL;
-       outpri = outpri2 = -20000;
-       for (rp = allproc; rp != NULL; rp = rp->p_nxt) {
-               if (!swappable(rp))
+       outpri = outpri2 = 0;
+       for (p = allproc; p != NULL; p = p->p_nxt) {
+               if (!swappable(p))
                        continue;
                        continue;
-               switch(rp->p_stat) {
+               switch (p->p_stat) {
                case SRUN:
                case SRUN:
-                       if (rp->p_slptime > outpri2) {
-                               outp2 = rp;
-                               outpri2 = rp->p_slptime;
+                       if (p->p_time > outpri2) {
+                               outp2 = p;
+                               outpri2 = p->p_time;
                        }
                        continue;
                        
                case SSLEEP:
                case SSTOP:
                        }
                        continue;
                        
                case SSLEEP:
                case SSTOP:
-                       if (rp->p_slptime > maxslp) {
-                               swapout(rp);
+                       if (p->p_slptime > maxslp) {
+                               swapout(p);
                                didswap++;
                                didswap++;
-                       } else if (rp->p_slptime > outpri) {
-                               outp = rp;
-                               outpri = rp->p_slptime;
+                       } else if (p->p_slptime > outpri) {
+                               outp = p;
+                               outpri = p->p_slptime;
                        }
                        continue;
                }
                        }
                        continue;
                }
@@ -340,14 +325,14 @@ swapout_threads()
         */
        if (didswap == 0 &&
            vm_page_free_count <= atop(round_page(ctob(UPAGES)))) {
         */
        if (didswap == 0 &&
            vm_page_free_count <= atop(round_page(ctob(UPAGES)))) {
-               if ((rp = outp) == 0)
-                       rp = outp2;
+               if ((p = outp) == 0)
+                       p = outp2;
 #ifdef DEBUG
                if (swapdebug & SDB_SWAPOUT)
 #ifdef DEBUG
                if (swapdebug & SDB_SWAPOUT)
-                       printf("swapout_threads: no duds, try procp %x\n", rp);
+                       printf("swapout_threads: no duds, try procp %x\n", p);
 #endif
 #endif
-               if (rp)
-                       swapout(rp);
+               if (p)
+                       swapout(p);
        }
 }
 
        }
 }
 
@@ -366,7 +351,7 @@ swapout(p)
        size = round_page(ctob(UPAGES));
        addr = (vm_offset_t) p->p_addr;
        vm_map_pageable(kernel_map, addr, addr+size, TRUE);
        size = round_page(ctob(UPAGES));
        addr = (vm_offset_t) p->p_addr;
        vm_map_pageable(kernel_map, addr, addr+size, TRUE);
-       pmap_collect(vm_map_pmap(p->p_map));
+       pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
        (void) splhigh();
        p->p_flag &= ~SLOAD;
        if (p->p_stat == SRUN)
        (void) splhigh();
        p->p_flag &= ~SLOAD;
        if (p->p_stat == SRUN)
@@ -387,7 +372,7 @@ assert_wait(event, ruptible)
 #ifdef lint
        ruptible++;
 #endif
 #ifdef lint
        ruptible++;
 #endif
-       u.u_procp->p_thread = event;
+       curproc->p_thread = event;
 }
 
 void
 }
 
 void
@@ -395,12 +380,11 @@ thread_block()
 {
        int s = splhigh();
 
 {
        int s = splhigh();
 
-       if (u.u_procp->p_thread)
-               sleep((caddr_t)u.u_procp->p_thread, PVM);
+       if (curproc->p_thread)
+               sleep((caddr_t)curproc->p_thread, PVM);
        splx(s);
 }
 
        splx(s);
 }
 
-void
 thread_sleep(event, lock, ruptible)
        int event;
        simple_lock_t lock;
 thread_sleep(event, lock, ruptible)
        int event;
        simple_lock_t lock;
@@ -411,14 +395,13 @@ thread_sleep(event, lock, ruptible)
 #endif
        int s = splhigh();
 
 #endif
        int s = splhigh();
 
-       u.u_procp->p_thread = event;
+       curproc->p_thread = event;
        simple_unlock(lock);
        simple_unlock(lock);
-       if (u.u_procp->p_thread)
-               sleep((caddr_t)u.u_procp->p_thread, PVM);
+       if (curproc->p_thread)
+               sleep((caddr_t)event, PVM);
        splx(s);
 }
 
        splx(s);
 }
 
-void
 thread_wakeup(event)
        int event;
 {
 thread_wakeup(event)
        int event;
 {