1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)sys_process.c 7.17 (Berkeley) 04/20/91 7 */ 8 9 #define IPCREG 10 #include "param.h" 11 #include "proc.h" 12 #include "vnode.h" 13 #include "seg.h" 14 #include "buf.h" 15 #include "ptrace.h" 16 17 #include "machine/reg.h" 18 #include "machine/psl.h" 19 #include "vm/vm.h" 20 #include "vm/vm_page.h" 21 22 #include "user.h" 23 24 /* 25 * Priority for tracing 26 */ 27 #define IPCPRI PZERO 28 29 /* 30 * Tracing variables. 31 * Used to pass trace command from 32 * parent to child being traced. 33 * This data base cannot be 34 * shared and is locked 35 * per user. 36 */ 37 struct { 38 int ip_lock; 39 int ip_req; 40 int *ip_addr; 41 int ip_data; 42 } ipc; 43 44 /* 45 * sys-trace system call. 46 */ 47 ptrace(curp, uap, retval) 48 struct proc *curp; 49 register struct args { 50 int req; 51 int pid; 52 int *addr; 53 int data; 54 } *uap; 55 int *retval; 56 { 57 register struct proc *p; 58 59 if (uap->req <= 0) { 60 curp->p_flag |= STRC; 61 return (0); 62 } 63 p = pfind(uap->pid); 64 if (p == 0 || p->p_stat != SSTOP || p->p_pptr != curp || 65 !(p->p_flag & STRC)) 66 return (ESRCH); 67 while (ipc.ip_lock) 68 sleep((caddr_t)&ipc, IPCPRI); 69 ipc.ip_lock = p->p_pid; 70 ipc.ip_data = uap->data; 71 ipc.ip_addr = uap->addr; 72 ipc.ip_req = uap->req; 73 p->p_flag &= ~SWTED; 74 while (ipc.ip_req > 0) { 75 if (p->p_stat==SSTOP) 76 setrun(p); 77 sleep((caddr_t)&ipc, IPCPRI); 78 } 79 *retval = ipc.ip_data; 80 ipc.ip_lock = 0; 81 wakeup((caddr_t)&ipc); 82 if (ipc.ip_req < 0) 83 return (EIO); 84 return (0); 85 } 86 87 #define PHYSOFF(p, o) \ 88 ((physadr)(p)+((o)/sizeof(((physadr)0)->r[0]))) 89 #if defined(i386) 90 #undef PC 91 #undef SP 92 #undef PS 93 #undef R0 94 #undef R1 95 96 #define PC tEIP 97 #define SP tESP 98 #define PS tEFLAGS 99 #define R0 tEDX 100 #define R1 tECX 101 #endif 102 103 /* 104 * Code that the child process 105 * executes to implement the command 106 * of the parent process in tracing. 107 */ 108 procxmt(p) 109 register struct proc *p; 110 { 111 register int i, *poff; 112 113 if (ipc.ip_lock != p->p_pid) 114 return (0); 115 p->p_slptime = 0; 116 u.u_kproc.kp_proc.p_regs = p->p_regs; /* u.u_ar0 */ 117 i = ipc.ip_req; 118 ipc.ip_req = 0; 119 switch (i) { 120 121 case PT_READ_I: /* read the child's text space */ 122 if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ)) 123 goto error; 124 ipc.ip_data = fuiword((caddr_t)ipc.ip_addr); 125 break; 126 127 case PT_READ_D: /* read the child's data space */ 128 if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ)) 129 goto error; 130 ipc.ip_data = fuword((caddr_t)ipc.ip_addr); 131 break; 132 133 case PT_READ_U: /* read the child's u. */ 134 #ifdef HPUXCOMPAT 135 if (u.u_pcb.pcb_flags & PCB_HPUXTRACE) 136 i = hpuxtobsduoff(ipc.ip_addr); 137 else 138 #endif 139 i = (int)ipc.ip_addr; 140 if (i<0 || i > ctob(UPAGES)-sizeof(int)) 141 goto error; 142 ipc.ip_data = *(int *)PHYSOFF(&u, i); 143 break; 144 145 case PT_WRITE_I: /* write the child's text space */ 146 if ((i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data)) < 0) { 147 vm_offset_t sa, ea; 148 int rv; 149 150 sa = trunc_page((vm_offset_t)ipc.ip_addr); 151 ea = round_page((vm_offset_t)ipc.ip_addr+sizeof(int)-1); 152 rv = vm_map_protect(&p->p_vmspace->vm_map, sa, ea, 153 VM_PROT_DEFAULT, FALSE); 154 if (rv == KERN_SUCCESS) { 155 i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data); 156 (void) vm_map_protect(&p->p_vmspace->vm_map, 157 sa, ea, VM_PROT_READ|VM_PROT_EXECUTE, 158 FALSE); 159 } 160 } 161 if (i < 0) 162 goto error; 163 break; 164 165 case PT_WRITE_D: /* write the child's data space */ 166 if (suword((caddr_t)ipc.ip_addr, 0) < 0) 167 goto error; 168 (void) suword((caddr_t)ipc.ip_addr, ipc.ip_data); 169 break; 170 171 case PT_WRITE_U: /* write the child's u. */ 172 #ifdef HPUXCOMPAT 173 if (u.u_pcb.pcb_flags & PCB_HPUXTRACE) 174 i = hpuxtobsduoff(ipc.ip_addr); 175 else 176 #endif 177 i = (int)ipc.ip_addr; 178 poff = (int *)PHYSOFF(&u, i); 179 for (i=0; i<NIPCREG; i++) 180 if (poff == &p->p_regs[ipcreg[i]]) 181 goto ok; 182 if (poff == &p->p_regs[PS]) { 183 ipc.ip_data |= PSL_USERSET; 184 ipc.ip_data &= ~PSL_USERCLR; 185 #ifdef PSL_CM_CLR 186 if (ipc.ip_data & PSL_CM) 187 ipc.ip_data &= ~PSL_CM_CLR; 188 #endif 189 goto ok; 190 } 191 #if defined(hp300) 192 #ifdef FPCOPROC 193 if (poff >= (int *)u.u_pcb.pcb_fpregs.fpf_regs && 194 poff <= (int *)&u.u_pcb.pcb_fpregs.fpf_fpiar) 195 goto ok; 196 #endif 197 #endif 198 goto error; 199 200 ok: 201 *poff = ipc.ip_data; 202 break; 203 204 case PT_STEP: /* single step the child */ 205 case PT_CONTINUE: /* continue the child */ 206 if ((int)ipc.ip_addr != 1) 207 p->p_regs[PC] = (int)ipc.ip_addr; 208 if ((unsigned)ipc.ip_data > NSIG) 209 goto error; 210 p->p_xstat = ipc.ip_data; /* see issig */ 211 if (i == PT_STEP) 212 p->p_regs[PS] |= PSL_T; 213 wakeup((caddr_t)&ipc); 214 return (1); 215 216 case PT_KILL: /* kill the child process */ 217 wakeup((caddr_t)&ipc); 218 exit(p, (int)p->p_xstat); 219 220 default: 221 error: 222 ipc.ip_req = -1; 223 } 224 wakeup((caddr_t)&ipc); 225 return (0); 226 } 227