kernel: kill proc with bogus ipc address
[minix.git] / kernel / system / do_trace.c
blob83f30c5b6ff20c8127181cb193da58859e1417d5
1 /* The kernel call implemented in this file:
2 * m_type: SYS_TRACE
4 * The parameters for this kernel call are:
5 * m2_i1: CTL_ENDPT process that is traced
6 * m2_i2: CTL_REQUEST trace request
7 * m2_l1: CTL_ADDRESS address at traced process' space
8 * m2_l2: CTL_DATA data to be written or returned here
9 */
11 #include "kernel/system.h"
12 #include <sys/ptrace.h>
14 #if USE_TRACE
16 /*==========================================================================*
17 * do_trace *
18 *==========================================================================*/
19 int do_trace(struct proc * caller, message * m_ptr)
21 /* Handle the debugging commands supported by the ptrace system call
22 * The commands are:
23 * T_STOP stop the process
24 * T_OK enable tracing by parent for this process
25 * T_GETINS return value from instruction space
26 * T_GETDATA return value from data space
27 * T_GETUSER return value from user process table
28 * T_SETINS set value in instruction space
29 * T_SETDATA set value in data space
30 * T_SETUSER set value in user process table
31 * T_RESUME resume execution
32 * T_EXIT exit
33 * T_STEP set trace bit
34 * T_SYSCALL trace system call
35 * T_ATTACH attach to an existing process
36 * T_DETACH detach from a traced process
37 * T_SETOPT set trace options
38 * T_GETRANGE get range of values
39 * T_SETRANGE set range of values
41 * The T_OK, T_ATTACH, T_EXIT, and T_SETOPT commands are handled completely by
42 * the process manager. T_GETRANGE and T_SETRANGE use sys_vircopy(). All others
43 * come here.
46 register struct proc *rp;
47 vir_bytes tr_addr = (vir_bytes) m_ptr->CTL_ADDRESS;
48 long tr_data = m_ptr->CTL_DATA;
49 int tr_request = m_ptr->CTL_REQUEST;
50 int tr_proc_nr_e = m_ptr->CTL_ENDPT, tr_proc_nr;
51 unsigned char ub;
52 int i;
54 #define COPYTOPROC(addr, myaddr, length) { \
55 struct vir_addr fromaddr, toaddr; \
56 int r; \
57 fromaddr.proc_nr_e = KERNEL; \
58 toaddr.proc_nr_e = tr_proc_nr_e; \
59 fromaddr.offset = (myaddr); \
60 toaddr.offset = (addr); \
61 if((r=virtual_copy_vmcheck(caller, &fromaddr, \
62 &toaddr, length)) != OK) { \
63 printf("Can't copy in sys_trace: %d\n", r);\
64 return r;\
65 } \
68 #define COPYFROMPROC(addr, myaddr, length) { \
69 struct vir_addr fromaddr, toaddr; \
70 int r; \
71 fromaddr.proc_nr_e = tr_proc_nr_e; \
72 toaddr.proc_nr_e = KERNEL; \
73 fromaddr.offset = (addr); \
74 toaddr.offset = (myaddr); \
75 if((r=virtual_copy_vmcheck(caller, &fromaddr, \
76 &toaddr, length)) != OK) { \
77 printf("Can't copy in sys_trace: %d\n", r);\
78 return r;\
79 } \
82 if(!isokendpt(tr_proc_nr_e, &tr_proc_nr)) return(EINVAL);
83 if (iskerneln(tr_proc_nr)) return(EPERM);
85 rp = proc_addr(tr_proc_nr);
86 if (isemptyp(rp)) return(EINVAL);
87 switch (tr_request) {
88 case T_STOP: /* stop process */
89 RTS_SET(rp, RTS_P_STOP);
90 #if defined(__i386__)
91 rp->p_reg.psw &= ~TRACEBIT; /* clear trace bit */
92 #endif
93 rp->p_misc_flags &= ~MF_SC_TRACE; /* clear syscall trace flag */
94 return(OK);
96 case T_GETINS: /* return value from instruction space */
97 COPYFROMPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
98 m_ptr->CTL_DATA = tr_data;
99 break;
101 case T_GETDATA: /* return value from data space */
102 COPYFROMPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
103 m_ptr->CTL_DATA= tr_data;
104 break;
106 case T_GETUSER: /* return value from process table */
107 if ((tr_addr & (sizeof(long) - 1)) != 0) return(EFAULT);
109 if (tr_addr <= sizeof(struct proc) - sizeof(long)) {
110 m_ptr->CTL_DATA = *(long *) ((char *) rp + (int) tr_addr);
111 break;
114 /* The process's proc struct is followed by its priv struct.
115 * The alignment here should be unnecessary, but better safe..
117 i = sizeof(long) - 1;
118 tr_addr -= (sizeof(struct proc) + i) & ~i;
120 if (tr_addr > sizeof(struct priv) - sizeof(long)) return(EFAULT);
122 m_ptr->CTL_DATA = *(long *) ((char *) rp->p_priv + (int) tr_addr);
123 break;
125 case T_SETINS: /* set value in instruction space */
126 COPYTOPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
127 m_ptr->CTL_DATA = 0;
128 break;
130 case T_SETDATA: /* set value in data space */
131 COPYTOPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
132 m_ptr->CTL_DATA = 0;
133 break;
135 case T_SETUSER: /* set value in process table */
136 if ((tr_addr & (sizeof(reg_t) - 1)) != 0 ||
137 tr_addr > sizeof(struct stackframe_s) - sizeof(reg_t))
138 return(EFAULT);
139 i = (int) tr_addr;
140 #if defined(__i386__)
141 /* Altering segment registers might crash the kernel when it
142 * tries to load them prior to restarting a process, so do
143 * not allow it.
145 if (i == (int) &((struct proc *) 0)->p_reg.cs ||
146 i == (int) &((struct proc *) 0)->p_reg.ds ||
147 i == (int) &((struct proc *) 0)->p_reg.es ||
148 i == (int) &((struct proc *) 0)->p_reg.gs ||
149 i == (int) &((struct proc *) 0)->p_reg.fs ||
150 i == (int) &((struct proc *) 0)->p_reg.ss)
151 return(EFAULT);
152 #endif
153 #if defined(__i386__)
154 if (i == (int) &((struct proc *) 0)->p_reg.psw)
155 /* only selected bits are changeable */
156 SETPSW(rp, tr_data);
157 else
158 *(reg_t *) ((char *) &rp->p_reg + i) = (reg_t) tr_data;
159 #endif
160 m_ptr->CTL_DATA = 0;
161 break;
163 case T_DETACH: /* detach tracer */
164 rp->p_misc_flags &= ~MF_SC_ACTIVE;
166 /* fall through */
167 case T_RESUME: /* resume execution */
168 RTS_UNSET(rp, RTS_P_STOP);
169 m_ptr->CTL_DATA = 0;
170 break;
172 case T_STEP: /* set trace bit */
173 #if defined(__i386__)
174 rp->p_reg.psw |= TRACEBIT;
175 #endif
176 RTS_UNSET(rp, RTS_P_STOP);
177 m_ptr->CTL_DATA = 0;
178 break;
180 case T_SYSCALL: /* trace system call */
181 rp->p_misc_flags |= MF_SC_TRACE;
182 RTS_UNSET(rp, RTS_P_STOP);
183 m_ptr->CTL_DATA = 0;
184 break;
186 case T_READB_INS: /* get value from instruction space */
187 COPYFROMPROC(tr_addr, (vir_bytes) &ub, 1);
188 m_ptr->CTL_DATA = ub;
189 break;
191 case T_WRITEB_INS: /* set value in instruction space */
192 ub = (unsigned char) (tr_data & 0xff);
193 COPYTOPROC(tr_addr, (vir_bytes) &ub, 1);
194 m_ptr->CTL_DATA = 0;
195 break;
197 default:
198 return(EINVAL);
200 return(OK);
203 #endif /* USE_TRACE */