1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * arch/sh64/kernel/ptrace.c
7 *
8 * Copyright (C) 2000, 2001 Paolo Alberelli
9 *
10 * Started from SH3/4 version:
11 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
12 *
13 * Original x86 implementation:
14 * By Ross Biro 1/23/92
15 * edited by Linus Torvalds
16 *
17 */
18
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
33 #include <asm/processor.h>
34 #include <asm/mmu_context.h>
35
36 /* This mask defines the bits of the SR which the user is not allowed to
37 change, which are everything except S, Q, M, PR, SZ, FR. */
38 #define SR_MASK (0xffff8cfd)
39
40 /*
41 * does not yet catch signals sent when the child dies.
42 * in exit.c or in signal.c.
43 */
44
45 /*
46 * This routine will get a word off of the process kernel stack.
47 */
get_stack_long(struct task_struct * task,int offset)48 static inline int get_stack_long(struct task_struct *task, int offset)
49 {
50 unsigned char *stack;
51
52 stack = (unsigned char *)(task->thread.kregs);
53 stack += offset;
54 return (*((int *)stack));
55 }
56
57 static inline unsigned long
get_fpu_long(struct task_struct * task,unsigned long addr)58 get_fpu_long(struct task_struct *task, unsigned long addr)
59 {
60 unsigned long tmp;
61 struct pt_regs *regs;
62 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
63
64 if (!task->used_math) {
65 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
66 tmp = FPSCR_INIT;
67 } else {
68 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
69 }
70 return tmp;
71 }
72
73 if (last_task_used_math == task) {
74 grab_fpu();
75 fpsave(&task->thread.fpu.hard);
76 release_fpu();
77 last_task_used_math = 0;
78 regs->sr |= SR_FD;
79 }
80
81 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
82 return tmp;
83 }
84
85 /*
86 * This routine will put a word on the process kernel stack.
87 */
put_stack_long(struct task_struct * task,int offset,unsigned long data)88 static inline int put_stack_long(struct task_struct *task, int offset,
89 unsigned long data)
90 {
91 unsigned char *stack;
92
93 stack = (unsigned char *)(task->thread.kregs);
94 stack += offset;
95 *(unsigned long *) stack = data;
96 return 0;
97 }
98
99 static inline int
put_fpu_long(struct task_struct * task,unsigned long addr,unsigned long data)100 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
101 {
102 struct pt_regs *regs;
103
104 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
105
106 if (!task->used_math) {
107 fpinit(&task->thread.fpu.hard);
108 task->used_math = 1;
109 } else if (last_task_used_math == task) {
110 grab_fpu();
111 fpsave(&task->thread.fpu.hard);
112 release_fpu();
113 last_task_used_math = 0;
114 regs->sr |= SR_FD;
115 }
116
117 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
118 return 0;
119 }
120
sys_ptrace(long request,long pid,long addr,long data)121 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
122 {
123 struct task_struct *child, *tsk = current;
124 int ret;
125
126 lock_kernel();
127 ret = -EPERM;
128 if (request == PTRACE_TRACEME) {
129 /* are we already being traced? */
130 if (current->ptrace & PT_PTRACED)
131 goto out;
132 /* set the ptrace bit in the process flags. */
133 current->ptrace |= PT_PTRACED;
134 ret = 0;
135 goto out;
136 }
137 ret = -ESRCH;
138 read_lock(&tasklist_lock);
139 child = find_task_by_pid(pid);
140 if (child)
141 get_task_struct(child);
142 read_unlock(&tasklist_lock);
143 if (!child)
144 goto out;
145
146 ret = -EPERM;
147 if (pid == 1) /* you may not mess with init */
148 goto out_tsk;
149
150 if (request == PTRACE_ATTACH) {
151 ret = ptrace_attach(child);
152 goto out_tsk;
153 }
154 ret = -ESRCH;
155 if (!(child->ptrace & PT_PTRACED))
156 goto out_tsk;
157 if (child->state != TASK_STOPPED) {
158 if (request != PTRACE_KILL)
159 goto out_tsk;
160 }
161 if (child->p_pptr != tsk)
162 goto out_tsk;
163 switch (request) {
164 /* when I and D space are separate, these will need to be fixed. */
165 case PTRACE_PEEKTEXT: /* read word at location addr. */
166 case PTRACE_PEEKDATA: {
167 unsigned long tmp;
168 int copied;
169
170 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
171 ret = -EIO;
172 if (copied != sizeof(tmp))
173 break;
174 ret = put_user(tmp,(unsigned long *) data);
175 break;
176 }
177
178 /* read the word at location addr in the USER area. */
179 case PTRACE_PEEKUSR: {
180 unsigned long tmp;
181
182 ret = -EIO;
183 if ((addr & 3) || addr < 0)
184 break;
185
186 if (addr < sizeof(struct pt_regs))
187 tmp = get_stack_long(child, addr);
188 else if ((addr >= offsetof(struct user, fpu)) &&
189 (addr < offsetof(struct user, u_fpvalid))) {
190 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
191 } else if (addr == offsetof(struct user, u_fpvalid)) {
192 tmp = child->used_math;
193 } else {
194 break;
195 }
196 ret = put_user(tmp, (unsigned long *)data);
197 break;
198 }
199
200 /* when I and D space are separate, this will have to be fixed. */
201 case PTRACE_POKETEXT: /* write the word at location addr. */
202 case PTRACE_POKEDATA:
203 ret = 0;
204 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
205 break;
206 ret = -EIO;
207 break;
208
209 case PTRACE_POKEUSR:
210 /* write the word at location addr in the USER area. We must
211 disallow any changes to certain SR bits or u_fpvalid, since
212 this could crash the kernel or result in a security
213 loophole. */
214 ret = -EIO;
215 if ((addr & 3) || addr < 0)
216 break;
217
218 if (addr < sizeof(struct pt_regs)) {
219 /* Ignore change of top 32 bits of SR */
220 if (addr == offsetof (struct pt_regs, sr)+4)
221 {
222 ret = 0;
223 break;
224 }
225 /* If lower 32 bits of SR, ignore non-user bits */
226 if (addr == offsetof (struct pt_regs, sr))
227 {
228 long cursr = get_stack_long(child, addr);
229 data &= ~(SR_MASK);
230 data |= (cursr & SR_MASK);
231 }
232 ret = put_stack_long(child, addr, data);
233 }
234 else if ((addr >= offsetof(struct user, fpu)) &&
235 (addr < offsetof(struct user, u_fpvalid))) {
236 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
237 }
238 break;
239
240 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
241 case PTRACE_CONT: { /* restart after signal. */
242 ret = -EIO;
243 if ((unsigned long) data > _NSIG)
244 break;
245 if (request == PTRACE_SYSCALL)
246 child->ptrace |= PT_TRACESYS;
247 else
248 child->ptrace &= ~PT_TRACESYS;
249 child->exit_code = data;
250 wake_up_process(child);
251 ret = 0;
252 break;
253 }
254
255 /*
256 * make the child exit. Best I can do is send it a sigkill.
257 * perhaps it should be put in the status that it wants to
258 * exit.
259 */
260 case PTRACE_KILL: {
261 ret = 0;
262 if (child->state == TASK_ZOMBIE) /* already dead */
263 break;
264 child->exit_code = SIGKILL;
265 wake_up_process(child);
266 break;
267 }
268
269 case PTRACE_SINGLESTEP: { /* set the trap flag. */
270 struct pt_regs *regs;
271
272 ret = -EIO;
273 if ((unsigned long) data > _NSIG)
274 break;
275 child->ptrace &= ~PT_TRACESYS;
276 if ((child->ptrace & PT_DTRACE) == 0) {
277 /* Spurious delayed TF traps may occur */
278 child->ptrace |= PT_DTRACE;
279 }
280
281 regs = child->thread.kregs;
282
283 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
284
285 child->exit_code = data;
286 /* give it a chance to run. */
287 wake_up_process(child);
288 ret = 0;
289 break;
290 }
291
292 case PTRACE_DETACH: /* detach a process that was attached. */
293 ret = ptrace_detach(child, data);
294 break;
295
296 case PTRACE_SETOPTIONS: {
297 if (data & PTRACE_O_TRACESYSGOOD)
298 child->ptrace |= PT_TRACESYSGOOD;
299 else
300 child->ptrace &= ~PT_TRACESYSGOOD;
301
302
303
304 ret = 0;
305 break;
306 }
307 default:
308 ret = -EIO;
309 break;
310 }
311 out_tsk:
312 free_task_struct(child);
313 out:
314 unlock_kernel();
315 return ret;
316 }
317
syscall_trace(void)318 asmlinkage void syscall_trace(void)
319 {
320 struct task_struct *tsk = current;
321
322 if ((tsk->ptrace & (PT_PTRACED|PT_TRACESYS))
323 != (PT_PTRACED|PT_TRACESYS))
324 return;
325 tsk->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
326 ? 0x80 : 0);
327 tsk->state = TASK_STOPPED;
328 notify_parent(tsk, SIGCHLD);
329 schedule();
330 /*
331 * this isn't the same as continuing with a signal, but it will do
332 * for normal use. strace only continues with a signal if the
333 * stopping signal is not SIGTRAP. -brl
334 */
335 if (tsk->exit_code) {
336 send_sig(tsk->exit_code, tsk, 1);
337 tsk->exit_code = 0;
338 }
339 }
340
341 /* Called with interrupts disabled */
do_single_step(unsigned long long vec,struct pt_regs * regs)342 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
343 {
344 /* This is called after a single step exception (DEBUGSS).
345 There is no need to change the PC, as it is a post-execution
346 exception, as entry.S does not do anything to the PC for DEBUGSS.
347 We need to clear the Single Step setting in SR to avoid
348 continually stepping. */
349 __sti();
350 regs->sr &= ~SR_SSTEP;
351 force_sig(SIGTRAP, current);
352 }
353
354 /* Called with interrupts disabled */
do_software_break_point(unsigned long long vec,struct pt_regs * regs)355 asmlinkage void do_software_break_point(unsigned long long vec,
356 struct pt_regs *regs)
357 {
358 /* We need to forward step the PC, to counteract the backstep done
359 in signal.c. */
360 __sti();
361 force_sig(SIGTRAP, current);
362 regs->pc += 4;
363 }
364
365
366 /*
367 * Called by kernel/ptrace.c when detaching..
368 *
369 * Make sure single step bits etc are not set.
370 */
ptrace_disable(struct task_struct * child)371 void ptrace_disable(struct task_struct *child)
372 {
373 /* nothing to do.. */
374 }
375