BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / vm / vm_glue.c
index ddb74c0..c9294fe 100644 (file)
 /* 
 /* 
- * Copyright (c) 1987 Carnegie-Mellon University
  * Copyright (c) 1991 Regents of the University of California.
  * All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * The Mach Operating System project at Carnegie-Mellon University.
  *
  * Copyright (c) 1991 Regents of the University of California.
  * All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * The Mach Operating System project at Carnegie-Mellon University.
  *
- * The CMU software License Agreement specifies the terms and conditions
- * for use and redistribution.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)vm_glue.c   7.1 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)vm_glue.c   7.8 (Berkeley) 5/15/91
+ *
+ *
+ * Copyright (c) 1987, 1990 Carnegie-Mellon University.
+ * All rights reserved.
+ * 
+ * Permission to use, copy, modify and distribute this software and
+ * its documentation is hereby granted, provided that both the copyright
+ * notice and this permission notice appear in all copies of the
+ * software, derivative works or modified versions, and any portions
+ * thereof, and that both notices appear in supporting documentation.
+ * 
+ * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 
+ * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 
+ * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
+ * 
+ * Carnegie Mellon requests users of this software to return to
+ *
+ *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
+ *  School of Computer Science
+ *  Carnegie Mellon University
+ *  Pittsburgh PA 15213-3890
+ *
+ * any improvements or extensions that they make and grant Carnegie the
+ * rights to redistribute these changes.
  */
 
 #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 */
+int    readbuffers = 0;        /* XXX allow kgdb to read kernel buffer pool */
 
 kernacc(addr, len, rw)
        caddr_t addr;
        int len, rw;
 {
        boolean_t rv;
 
 kernacc(addr, len, rw)
        caddr_t addr;
        int len, rw;
 {
        boolean_t rv;
+       vm_offset_t saddr, eaddr;
        vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
 
        vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
 
-       rv = vm_map_check_protection(kernel_map, trunc_page(addr),
-                                    round_page(addr+len-1), prot);
+       saddr = trunc_page(addr);
+       eaddr = round_page(addr+len-1);
+       rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot);
+       /*
+        * XXX there are still some things (e.g. the buffer cache) that
+        * are managed behind the VM system's back so even though an
+        * address is accessible in the mind of the VM system, there may
+        * not be physical pages where the VM thinks there is.  This can
+        * lead to bogus allocation of pages in the kernel address space
+        * or worse, inconsistencies at the pmap level.  We only worry
+        * about the buffer cache for now.
+        */
+       if (!readbuffers && rv && (eaddr > (vm_offset_t)buffers &&
+                  saddr < (vm_offset_t)buffers + MAXBSIZE * nbuf))
+               rv = FALSE;
        return(rv == TRUE);
 }
 
        return(rv == TRUE);
 }
 
@@ -45,14 +108,14 @@ 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);
 }
 
 #ifdef KGDB
 /*
        return(rv == TRUE);
 }
 
 #ifdef KGDB
 /*
- * Change protections on kernel pages from addr to addr+size
+ * Change protections on kernel pages from addr to addr+len
  * (presumably so debugger can plant a breakpoint).
  * All addresses are assumed to reside in the Sysmap,
  */
  * (presumably so debugger can plant a breakpoint).
  * All addresses are assumed to reside in the Sysmap,
  */
@@ -71,7 +134,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,92 +146,95 @@ 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;
+/*
+ * Implement fork's actions on an address space.
+ * Here we arrange for the address space to be copied or referenced,
+ * allocate a user struct (pcb and kernel stack), then call the
+ * machine-dependent layer to fill those in and make the new process
+ * ready to run.
+ * NOTE: the kernel stack may be at a different location in the child
+ * process, and thus addresses of automatic variables may be invalid
+ * after cpu_fork returns in the child process.  We do nothing here
+ * after cpu_fork returns.
+ */
+vm_fork(p1, p2, isvfork)
+       register struct proc *p1, *p2;
        int isvfork;
 {
        register struct user *up;
        vm_offset_t addr;
        int isvfork;
 {
        register struct user *up;
        vm_offset_t addr;
-       vm_size_t size;
 
 
-#if 0
+#ifdef i386
        /*
        /*
-        * 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.
+        * avoid copying any of the parent's pagetables or other per-process
+        * objects that reside in the map by marking all of them non-inheritable
         */
         */
-       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);
-       }
+       (void)vm_map_inherit(&p1->p_vmspace->vm_map,
+               UPT_MIN_ADDRESS-UPAGES*NBPG, VM_MAX_ADDRESS, VM_INHERIT_NONE);
 #endif
 #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);
-       vm_map_pageable(kernel_map, addr, addr+size, FALSE);
-       p->p_addr = (caddr_t)addr;
-       up = (struct user *)addr;
 
        /*
 
        /*
-        * Update the current u-area and copy it to the new one
+        * Allocate a wired-down (for now) pcb and kernel stack for the process
         */
         */
-       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);
+       addr = kmem_alloc_pageable(kernel_map, ctob(UPAGES));
+       vm_map_pageable(kernel_map, addr, addr + ctob(UPAGES), FALSE);
+       up = (struct user *)addr;
+       p2->p_addr = up;
 
        /*
 
        /*
-        * Arrange for a non-local goto when the new process
-        * is started, to resume here, returning nonzero from setjmp.
+        * p_stats and p_sigacts currently point at fields
+        * in the user struct but not at &u, instead at p_addr.
+        * Copy p_sigacts and parts of p_stats; zero the rest
+        * of p_stats (statistics).
         */
         */
-       up->u_pcb.pcb_sswap = (int *)&u.u_ssave;
-       if (savectx(&up->u_ssave)) {
-               /*
-                * Return 1 in child.
-                */
-               return (1);
+       p2->p_stats = &up->u_stats;
+       p2->p_sigacts = &up->u_sigacts;
+       up->u_sigacts = *p1->p_sigacts;
+       bzero(&up->u_stats.pstat_startzero,
+           (unsigned) ((caddr_t)&up->u_stats.pstat_endzero -
+           (caddr_t)&up->u_stats.pstat_startzero));
+       bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy,
+           ((caddr_t)&up->u_stats.pstat_endcopy -
+            (caddr_t)&up->u_stats.pstat_startcopy));
+
+#ifdef i386
+       { u_int addr = UPT_MIN_ADDRESS - UPAGES*NBPG; struct vm_map *vp;
+
+       vp = &p2->p_vmspace->vm_map;
+       (void)vm_map_pageable(vp, addr, 0xfe000000 - addr, TRUE);
+       (void)vm_deallocate(vp, addr, 0xfe000000 - addr);
+       (void)vm_allocate(vp, &addr, UPT_MAX_ADDRESS - addr, FALSE);
+       (void)vm_map_inherit(vp, addr, UPT_MAX_ADDRESS, VM_INHERIT_NONE);
        }
        }
-
+#endif
        /*
        /*
-        * Clear vm statistics of new process.
+        * cpu_fork will copy and update the kernel stack and pcb,
+        * and make the child ready to run.  It marks the child
+        * so that it can return differently than the parent.
+        * It returns twice, once in the parent process and
+        * once in the child.
         */
         */
-       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 (cpu_fork(p1, p2));
 }
 
 /*
 }
 
 /*
- * 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 +242,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 +269,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 +336,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 +349,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 +372,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 +404,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);
        }
 }
 
        }
 }
 
@@ -365,8 +429,28 @@ swapout(p)
 #endif
        size = round_page(ctob(UPAGES));
        addr = (vm_offset_t) p->p_addr;
 #endif
        size = round_page(ctob(UPAGES));
        addr = (vm_offset_t) p->p_addr;
+#ifdef hp300
+       /*
+        * Ugh!  u-area is double mapped to a fixed address behind the
+        * back of the VM system and accesses are usually through that
+        * address rather than the per-process address.  Hence reference
+        * and modify information are recorded at the fixed address and
+        * lost at context switch time.  We assume the u-struct and
+        * kernel stack are always accessed/modified and force it to be so.
+        */
+       {
+               register int i;
+               volatile long tmp;
+
+               for (i = 0; i < UPAGES; i++) {
+                       tmp = *(long *)addr; *(long *)addr = tmp;
+                       addr += NBPG;
+               }
+               addr = (vm_offset_t) p->p_addr;
+       }
+#endif
        vm_map_pageable(kernel_map, addr, addr+size, TRUE);
        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 +471,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 +479,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 +494,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;
 {
@@ -440,14 +522,12 @@ iprintf(a, b, c, d, e, f, g, h)
 {
        register int i;
 
 {
        register int i;
 
-       for (i = indent; i > 0; ) {
-               if (i >= 8) {
-                       putchar('\t', 1, (caddr_t)0);
-                       i -= 8;
-               } else {
-                       putchar(' ', 1, (caddr_t)0);
-                       i--;
-               }
+       i = indent;
+       while (i >= 8) {
+               printf("\t");
+               i -= 8;
        }
        }
+       for (; i > 0; --i)
+               printf(" ");
        printf(a, b, c, d, e, f, g, h);
 }
        printf(a, b, c, d, e, f, g, h);
 }