add some missing returns, correct args to ipcaccess, general cleanup
[unix-history] / usr / src / sys / kern / sys_process.c
index a1073d5..20cd58c 100644 (file)
@@ -1,20 +1,24 @@
-/*     sys_process.c   5.3     82/09/04        */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/proc.h"
-#include "../h/inode.h"
-#include "../h/reg.h"
-#include "../h/text.h"
-#include "../h/seg.h"
-#include "../h/mtpr.h"
-#include "../h/pte.h"
-#include "../h/psl.h"
-#include "../h/vm.h"
-#include "../h/buf.h"
-#include "../h/acct.h"
+/*
+ * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)sys_process.c       7.10 (Berkeley) %G%
+ */
+
+#define IPCREG
+#include "param.h"
+#include "syscontext.h"
+#include "proc.h"
+#include "vnode.h"
+#include "text.h"
+#include "seg.h"
+#include "buf.h"
+#include "ptrace.h"
+
+#include "machine/reg.h"
+#include "machine/psl.h"
+#include "machine/pte.h"
 
 /*
  * Priority for tracing
 
 /*
  * Priority for tracing
@@ -39,26 +43,26 @@ struct {
 /*
  * sys-trace system call.
  */
 /*
  * sys-trace system call.
  */
-ptrace()
-{
-       register struct proc *p;
-       register struct a {
+ptrace(curp, uap, retval)
+       struct proc *curp;
+       register struct args {
                int     req;
                int     pid;
                int     *addr;
                int     data;
        } *uap;
                int     req;
                int     pid;
                int     *addr;
                int     data;
        } *uap;
+       int *retval;
+{
+       register struct proc *p;
 
 
-       uap = (struct a *)u.u_ap;
        if (uap->req <= 0) {
        if (uap->req <= 0) {
-               u.u_procp->p_flag |= STRC;
-               return;
+               curp->p_flag |= STRC;
+               RETURN (0);
        }
        p = pfind(uap->pid);
        }
        p = pfind(uap->pid);
-       if (p == 0 || p->p_stat != SSTOP || p->p_ppid != u.u_procp->p_pid) {
-               u.u_error = ESRCH;
-               return;
-       }
+       if (p == 0 || p->p_stat != SSTOP || p->p_ppid != curp->p_pid ||
+           !(p->p_flag & STRC))
+               RETURN (ESRCH);
        while (ipc.ip_lock)
                sleep((caddr_t)&ipc, IPCPRI);
        ipc.ip_lock = p->p_pid;
        while (ipc.ip_lock)
                sleep((caddr_t)&ipc, IPCPRI);
        ipc.ip_lock = p->p_pid;
@@ -71,120 +75,149 @@ ptrace()
                        setrun(p);
                sleep((caddr_t)&ipc, IPCPRI);
        }
                        setrun(p);
                sleep((caddr_t)&ipc, IPCPRI);
        }
-       u.u_r.r_val1 = ipc.ip_data;
-       if (ipc.ip_req < 0)
-               u.u_error = EIO;
+       *retval = ipc.ip_data;
        ipc.ip_lock = 0;
        wakeup((caddr_t)&ipc);
        ipc.ip_lock = 0;
        wakeup((caddr_t)&ipc);
+       if (ipc.ip_req < 0)
+               RETURN (EIO);
+       RETURN (0);
 }
 
 }
 
-int ipcreg[] = {R0,R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,AP,FP,SP,PC};
+#define        PHYSOFF(p, o) \
+       ((physadr)(p)+((o)/sizeof(((physadr)0)->r[0])))
+
 /*
  * Code that the child process
  * executes to implement the command
  * of the parent process in tracing.
  */
 /*
  * Code that the child process
  * executes to implement the command
  * of the parent process in tracing.
  */
-procxmt()
+procxmt(p)
+       register struct proc *p;
 {
 {
-       register int i;
-       register *p;
+       register int i, *poff;
        register struct text *xp;
        register struct text *xp;
+       struct vattr vattr;
+       struct vnode *vp;
 
 
-       if (ipc.ip_lock != u.u_procp->p_pid)
+       if (ipc.ip_lock != p->p_pid)
                return (0);
                return (0);
-       u.u_procp->p_slptime = 0;
+       p->p_slptime = 0;
        i = ipc.ip_req;
        ipc.ip_req = 0;
        switch (i) {
 
        i = ipc.ip_req;
        ipc.ip_req = 0;
        switch (i) {
 
-       /* read user I */
-       case 1:
+       case PT_READ_I:                 /* read the child's text space */
                if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ))
                        goto error;
                ipc.ip_data = fuiword((caddr_t)ipc.ip_addr);
                break;
 
                if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ))
                        goto error;
                ipc.ip_data = fuiword((caddr_t)ipc.ip_addr);
                break;
 
-       /* read user D */
-       case 2:
+       case PT_READ_D:                 /* read the child's data space */
                if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ))
                        goto error;
                ipc.ip_data = fuword((caddr_t)ipc.ip_addr);
                break;
 
                if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ))
                        goto error;
                ipc.ip_data = fuword((caddr_t)ipc.ip_addr);
                break;
 
-       /* read u */
-       case 3:
+       case PT_READ_U:                 /* read the child's u. */
+#ifdef HPUXCOMPAT
+               if (u.u_pcb.pcb_flags & PCB_HPUXTRACE)
+                       i = hpuxtobsduoff(ipc.ip_addr);
+               else
+#endif
                i = (int)ipc.ip_addr;
                i = (int)ipc.ip_addr;
-               if (i<0 || i >= ctob(UPAGES))
+               if (i<0 || i > ctob(UPAGES)-sizeof(int))
                        goto error;
                        goto error;
-               ipc.ip_data = ((physadr)&u)->r[i>>2];
+               ipc.ip_data = *(int *)PHYSOFF(&u, i);
                break;
 
                break;
 
-       /* write user I */
-       /* Must set up to allow writing */
-       case 4:
+       case PT_WRITE_I:                /* write the child's text space */
                /*
                 * If text, must assure exclusive use
                 */
                /*
                 * If text, must assure exclusive use
                 */
-               if (xp = u.u_procp->p_textp) {
-                       if (xp->x_count!=1 || xp->x_iptr->i_mode&ISVTX)
+               if (xp = p->p_textp) {
+                       vp = xp->x_vptr;
+                       VOP_GETATTR(vp, &vattr, u.u_cred);
+                       if (xp->x_count!=1 || (vattr.va_mode & VSVTX))
                                goto error;
                                goto error;
-                       xp->x_iptr->i_flag &= ~ITEXT;
+                       xp->x_flag |= XTRC;
                }
                i = -1;
                }
                i = -1;
-               if (chgprot((caddr_t)ipc.ip_addr, RW) &&
-                   chgprot((caddr_t)ipc.ip_addr+(sizeof(int)-1), RW))
-                       i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data);
-               (void) chgprot((caddr_t)ipc.ip_addr, RO);
-               (void) chgprot((caddr_t)ipc.ip_addr+(sizeof(int)-1), RO);
+               if ((i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data)) < 0) {
+                       if (!chgprot((caddr_t)ipc.ip_addr, RW) &&
+                           !chgprot((caddr_t)ipc.ip_addr+(sizeof(int)-1), RW))
+                               i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data);
+                       (void) chgprot((caddr_t)ipc.ip_addr, RO);
+                       (void) chgprot((caddr_t)ipc.ip_addr+(sizeof(int)-1), RO);
+               }
                if (i < 0)
                        goto error;
                if (i < 0)
                        goto error;
-               if (xp)
+#if defined(tahoe)
+               /* make sure the old value is not in cache */
+               ckeyrelease(p->p_ckey);
+               p->p_ckey = getcodekey();
+#endif
+               if (xp) {
                        xp->x_flag |= XWRIT;
                        xp->x_flag |= XWRIT;
+#if defined(tahoe)
+                       xp->x_ckey = p->p_ckey;
+#endif
+               }
                break;
 
                break;
 
-       /* write user D */
-       case 5:
+       case PT_WRITE_D:                /* write the child's data space */
                if (suword((caddr_t)ipc.ip_addr, 0) < 0)
                        goto error;
                (void) suword((caddr_t)ipc.ip_addr, ipc.ip_data);
                break;
 
                if (suword((caddr_t)ipc.ip_addr, 0) < 0)
                        goto error;
                (void) suword((caddr_t)ipc.ip_addr, ipc.ip_data);
                break;
 
-       /* write u */
-       case 6:
+       case PT_WRITE_U:                /* write the child's u. */
+#ifdef HPUXCOMPAT
+               if (u.u_pcb.pcb_flags & PCB_HPUXTRACE)
+                       i = hpuxtobsduoff(ipc.ip_addr);
+               else
+#endif
                i = (int)ipc.ip_addr;
                i = (int)ipc.ip_addr;
-               p = (int *)&((physadr)&u)->r[i>>2];
-               for (i=0; i<16; i++)
-                       if (p == &u.u_ar0[ipcreg[i]])
+               poff = (int *)PHYSOFF(&u, i);
+               for (i=0; i<NIPCREG; i++)
+                       if (poff == &u.u_ar0[ipcreg[i]])
                                goto ok;
                                goto ok;
-               if (p == &u.u_ar0[PS]) {
-                       ipc.ip_data |= PSL_CURMOD|PSL_PRVMOD;
-                       ipc.ip_data &=  ~PSL_USERCLR;
+               if (poff == &u.u_ar0[PS]) {
+                       ipc.ip_data |= PSL_USERSET;
+                       ipc.ip_data &= ~PSL_USERCLR;
+#ifdef PSL_CM_CLR
+                       if (ipc.ip_data & PSL_CM)
+                               ipc.ip_data &= ~PSL_CM_CLR;
+#endif
                        goto ok;
                }
                        goto ok;
                }
+#if defined(hp300)
+#ifdef FPCOPROC
+               if (poff >= (int *)u.u_pcb.pcb_fpregs.fpf_regs &&
+                   poff <= (int *)&u.u_pcb.pcb_fpregs.fpf_fpiar)
+                       goto ok;
+#endif
+#endif
                goto error;
 
        ok:
                goto error;
 
        ok:
-               *p = ipc.ip_data;
+               *poff = ipc.ip_data;
                break;
 
                break;
 
-       /* set signal and continue */
-       /* one version causes a trace-trap */
-       case 9:
-       case 7:
+       case PT_STEP:                   /* single step the child */
+       case PT_CONTINUE:               /* continue the child */
                if ((int)ipc.ip_addr != 1)
                        u.u_ar0[PC] = (int)ipc.ip_addr;
                if ((unsigned)ipc.ip_data > NSIG)
                        goto error;
                if ((int)ipc.ip_addr != 1)
                        u.u_ar0[PC] = (int)ipc.ip_addr;
                if ((unsigned)ipc.ip_data > NSIG)
                        goto error;
-               u.u_procp->p_cursig = ipc.ip_data;      /* see issig */
-               if (i == 9)
+               p->p_cursig = ipc.ip_data;      /* see issig */
+               if (i == PT_STEP) 
                        u.u_ar0[PS] |= PSL_T;
                wakeup((caddr_t)&ipc);
                return (1);
 
                        u.u_ar0[PS] |= PSL_T;
                wakeup((caddr_t)&ipc);
                return (1);
 
-       /* force exit */
-       case 8:
+       case PT_KILL:                   /* kill the child process */
                wakeup((caddr_t)&ipc);
                wakeup((caddr_t)&ipc);
-               exit(u.u_procp->p_cursig);
+               exit(p, p->p_cursig);
 
        default:
        error:
 
        default:
        error: