]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - arch/sh/kernel/ptrace_64.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6.git] / arch / sh / kernel / ptrace_64.c
1 /*
2  * arch/sh/kernel/ptrace_64.c
3  *
4  * Copyright (C) 2000, 2001  Paolo Alberelli
5  * Copyright (C) 2003 - 2007  Paul Mundt
6  *
7  * Started from SH3/4 version:
8  *   SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
9  *
10  *   Original x86 implementation:
11  *      By Ross Biro 1/23/92
12  *      edited by Linus Torvalds
13  *
14  * This file is subject to the terms and conditions of the GNU General Public
15  * License.  See the file "COPYING" in the main directory of this archive
16  * for more details.
17  */
18 #include <linux/kernel.h>
19 #include <linux/rwsem.h>
20 #include <linux/sched.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/user.h>
27 #include <linux/signal.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <asm/io.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <asm/system.h>
34 #include <asm/processor.h>
35 #include <asm/mmu_context.h>
36 #include <asm/fpu.h>
37
38 /* This mask defines the bits of the SR which the user is not allowed to
39    change, which are everything except S, Q, M, PR, SZ, FR. */
40 #define SR_MASK      (0xffff8cfd)
41
42 /*
43  * does not yet catch signals sent when the child dies.
44  * in exit.c or in signal.c.
45  */
46
47 /*
48  * This routine will get a word from the user area in the process kernel stack.
49  */
50 static inline int get_stack_long(struct task_struct *task, int offset)
51 {
52         unsigned char *stack;
53
54         stack = (unsigned char *)(task->thread.uregs);
55         stack += offset;
56         return (*((int *)stack));
57 }
58
59 static inline unsigned long
60 get_fpu_long(struct task_struct *task, unsigned long addr)
61 {
62         unsigned long tmp;
63         struct pt_regs *regs;
64         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
65
66         if (!tsk_used_math(task)) {
67                 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
68                         tmp = FPSCR_INIT;
69                 } else {
70                         tmp = 0xffffffffUL; /* matches initial value in fpu.c */
71                 }
72                 return tmp;
73         }
74
75         if (last_task_used_math == task) {
76                 enable_fpu();
77                 save_fpu(task, regs);
78                 disable_fpu();
79                 last_task_used_math = 0;
80                 regs->sr |= SR_FD;
81         }
82
83         tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
84         return tmp;
85 }
86
87 /*
88  * This routine will put a word into the user area in the process kernel stack.
89  */
90 static inline int put_stack_long(struct task_struct *task, int offset,
91                                  unsigned long data)
92 {
93         unsigned char *stack;
94
95         stack = (unsigned char *)(task->thread.uregs);
96         stack += offset;
97         *(unsigned long *) stack = data;
98         return 0;
99 }
100
101 static inline int
102 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
103 {
104         struct pt_regs *regs;
105
106         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
107
108         if (!tsk_used_math(task)) {
109                 fpinit(&task->thread.fpu.hard);
110                 set_stopped_child_used_math(task);
111         } else if (last_task_used_math == task) {
112                 enable_fpu();
113                 save_fpu(task, regs);
114                 disable_fpu();
115                 last_task_used_math = 0;
116                 regs->sr |= SR_FD;
117         }
118
119         ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
120         return 0;
121 }
122
123
124 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
125 {
126         int ret;
127
128         switch (request) {
129         /* when I and D space are separate, these will need to be fixed. */
130         case PTRACE_PEEKTEXT: /* read word at location addr. */
131         case PTRACE_PEEKDATA:
132                 ret = generic_ptrace_peekdata(child, addr, data);
133                 break;
134
135         /* read the word at location addr in the USER area. */
136         case PTRACE_PEEKUSR: {
137                 unsigned long tmp;
138
139                 ret = -EIO;
140                 if ((addr & 3) || addr < 0)
141                         break;
142
143                 if (addr < sizeof(struct pt_regs))
144                         tmp = get_stack_long(child, addr);
145                 else if ((addr >= offsetof(struct user, fpu)) &&
146                          (addr <  offsetof(struct user, u_fpvalid))) {
147                         tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
148                 } else if (addr == offsetof(struct user, u_fpvalid)) {
149                         tmp = !!tsk_used_math(child);
150                 } else {
151                         break;
152                 }
153                 ret = put_user(tmp, (unsigned long *)data);
154                 break;
155         }
156
157         /* when I and D space are separate, this will have to be fixed. */
158         case PTRACE_POKETEXT: /* write the word at location addr. */
159         case PTRACE_POKEDATA:
160                 ret = generic_ptrace_pokedata(child, addr, data);
161                 break;
162
163         case PTRACE_POKEUSR:
164                 /* write the word at location addr in the USER area. We must
165                    disallow any changes to certain SR bits or u_fpvalid, since
166                    this could crash the kernel or result in a security
167                    loophole. */
168                 ret = -EIO;
169                 if ((addr & 3) || addr < 0)
170                         break;
171
172                 if (addr < sizeof(struct pt_regs)) {
173                         /* Ignore change of top 32 bits of SR */
174                         if (addr == offsetof (struct pt_regs, sr)+4)
175                         {
176                                 ret = 0;
177                                 break;
178                         }
179                         /* If lower 32 bits of SR, ignore non-user bits */
180                         if (addr == offsetof (struct pt_regs, sr))
181                         {
182                                 long cursr = get_stack_long(child, addr);
183                                 data &= ~(SR_MASK);
184                                 data |= (cursr & SR_MASK);
185                         }
186                         ret = put_stack_long(child, addr, data);
187                 }
188                 else if ((addr >= offsetof(struct user, fpu)) &&
189                          (addr <  offsetof(struct user, u_fpvalid))) {
190                         ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
191                 }
192                 break;
193
194         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
195         case PTRACE_CONT: { /* restart after signal. */
196                 ret = -EIO;
197                 if (!valid_signal(data))
198                         break;
199                 if (request == PTRACE_SYSCALL)
200                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
201                 else
202                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
203                 child->exit_code = data;
204                 wake_up_process(child);
205                 ret = 0;
206                 break;
207         }
208
209 /*
210  * make the child exit.  Best I can do is send it a sigkill.
211  * perhaps it should be put in the status that it wants to
212  * exit.
213  */
214         case PTRACE_KILL: {
215                 ret = 0;
216                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
217                         break;
218                 child->exit_code = SIGKILL;
219                 wake_up_process(child);
220                 break;
221         }
222
223         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
224                 struct pt_regs *regs;
225
226                 ret = -EIO;
227                 if (!valid_signal(data))
228                         break;
229                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
230                 if ((child->ptrace & PT_DTRACE) == 0) {
231                         /* Spurious delayed TF traps may occur */
232                         child->ptrace |= PT_DTRACE;
233                 }
234
235                 regs = child->thread.uregs;
236
237                 regs->sr |= SR_SSTEP;   /* auto-resetting upon exception */
238
239                 child->exit_code = data;
240                 /* give it a chance to run. */
241                 wake_up_process(child);
242                 ret = 0;
243                 break;
244         }
245
246         default:
247                 ret = ptrace_request(child, request, addr, data);
248                 break;
249         }
250         return ret;
251 }
252
253 asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
254 {
255 #define WPC_DBRMODE 0x0d104008
256         static int first_call = 1;
257
258         lock_kernel();
259         if (first_call) {
260                 /* Set WPC.DBRMODE to 0.  This makes all debug events get
261                  * delivered through RESVEC, i.e. into the handlers in entry.S.
262                  * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
263                  * would normally be left set to 1, which makes debug events get
264                  * delivered through DBRVEC, i.e. into the remote gdb's
265                  * handlers.  This prevents ptrace getting them, and confuses
266                  * the remote gdb.) */
267                 printk("DBRMODE set to 0 to permit native debugging\n");
268                 poke_real_address_q(WPC_DBRMODE, 0);
269                 first_call = 0;
270         }
271         unlock_kernel();
272
273         return sys_ptrace(request, pid, addr, data);
274 }
275
276 asmlinkage void syscall_trace(struct pt_regs *regs, int entryexit)
277 {
278         struct task_struct *tsk = current;
279
280         if (unlikely(current->audit_context) && entryexit)
281                 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
282                                    regs->regs[9]);
283
284         if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
285             !test_thread_flag(TIF_SINGLESTEP))
286                 goto out;
287         if (!(tsk->ptrace & PT_PTRACED))
288                 goto out;
289
290         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
291                                 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
292
293         /*
294          * this isn't the same as continuing with a signal, but it will do
295          * for normal use.  strace only continues with a signal if the
296          * stopping signal is not SIGTRAP.  -brl
297          */
298         if (tsk->exit_code) {
299                 send_sig(tsk->exit_code, tsk, 1);
300                 tsk->exit_code = 0;
301         }
302
303 out:
304         if (unlikely(current->audit_context) && !entryexit)
305                 audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[1],
306                                     regs->regs[2], regs->regs[3],
307                                     regs->regs[4], regs->regs[5]);
308 }
309
310 /* Called with interrupts disabled */
311 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
312 {
313         /* This is called after a single step exception (DEBUGSS).
314            There is no need to change the PC, as it is a post-execution
315            exception, as entry.S does not do anything to the PC for DEBUGSS.
316            We need to clear the Single Step setting in SR to avoid
317            continually stepping. */
318         local_irq_enable();
319         regs->sr &= ~SR_SSTEP;
320         force_sig(SIGTRAP, current);
321 }
322
323 /* Called with interrupts disabled */
324 asmlinkage void do_software_break_point(unsigned long long vec,
325                                         struct pt_regs *regs)
326 {
327         /* We need to forward step the PC, to counteract the backstep done
328            in signal.c. */
329         local_irq_enable();
330         force_sig(SIGTRAP, current);
331         regs->pc += 4;
332 }
333
334 /*
335  * Called by kernel/ptrace.c when detaching..
336  *
337  * Make sure single step bits etc are not set.
338  */
339 void ptrace_disable(struct task_struct *child)
340 {
341         /* nothing to do.. */
342 }