Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[pandora-kernel.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 <linux/seccomp.h>
31 #include <linux/tracehook.h>
32 #include <asm/io.h>
33 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
35 #include <asm/system.h>
36 #include <asm/processor.h>
37 #include <asm/mmu_context.h>
38 #include <asm/fpu.h>
39
40 /* This mask defines the bits of the SR which the user is not allowed to
41    change, which are everything except S, Q, M, PR, SZ, FR. */
42 #define SR_MASK      (0xffff8cfd)
43
44 /*
45  * does not yet catch signals sent when the child dies.
46  * in exit.c or in signal.c.
47  */
48
49 /*
50  * This routine will get a word from the user area in the process kernel stack.
51  */
52 static inline int get_stack_long(struct task_struct *task, int offset)
53 {
54         unsigned char *stack;
55
56         stack = (unsigned char *)(task->thread.uregs);
57         stack += offset;
58         return (*((int *)stack));
59 }
60
61 static inline unsigned long
62 get_fpu_long(struct task_struct *task, unsigned long addr)
63 {
64         unsigned long tmp;
65         struct pt_regs *regs;
66         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
67
68         if (!tsk_used_math(task)) {
69                 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
70                         tmp = FPSCR_INIT;
71                 } else {
72                         tmp = 0xffffffffUL; /* matches initial value in fpu.c */
73                 }
74                 return tmp;
75         }
76
77         if (last_task_used_math == task) {
78                 enable_fpu();
79                 save_fpu(task, regs);
80                 disable_fpu();
81                 last_task_used_math = 0;
82                 regs->sr |= SR_FD;
83         }
84
85         tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
86         return tmp;
87 }
88
89 /*
90  * This routine will put a word into the user area in the process kernel stack.
91  */
92 static inline int put_stack_long(struct task_struct *task, int offset,
93                                  unsigned long data)
94 {
95         unsigned char *stack;
96
97         stack = (unsigned char *)(task->thread.uregs);
98         stack += offset;
99         *(unsigned long *) stack = data;
100         return 0;
101 }
102
103 static inline int
104 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
105 {
106         struct pt_regs *regs;
107
108         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
109
110         if (!tsk_used_math(task)) {
111                 fpinit(&task->thread.fpu.hard);
112                 set_stopped_child_used_math(task);
113         } else if (last_task_used_math == task) {
114                 enable_fpu();
115                 save_fpu(task, regs);
116                 disable_fpu();
117                 last_task_used_math = 0;
118                 regs->sr |= SR_FD;
119         }
120
121         ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
122         return 0;
123 }
124
125 void user_enable_single_step(struct task_struct *child)
126 {
127         struct pt_regs *regs = child->thread.uregs;
128
129         regs->sr |= SR_SSTEP;   /* auto-resetting upon exception */
130 }
131
132 void user_disable_single_step(struct task_struct *child)
133 {
134         struct pt_regs *regs = child->thread.uregs;
135
136         regs->sr &= ~SR_SSTEP;
137 }
138
139 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
140 {
141         int ret;
142
143         switch (request) {
144         /* read the word at location addr in the USER area. */
145         case PTRACE_PEEKUSR: {
146                 unsigned long tmp;
147
148                 ret = -EIO;
149                 if ((addr & 3) || addr < 0)
150                         break;
151
152                 if (addr < sizeof(struct pt_regs))
153                         tmp = get_stack_long(child, addr);
154                 else if ((addr >= offsetof(struct user, fpu)) &&
155                          (addr <  offsetof(struct user, u_fpvalid))) {
156                         tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
157                 } else if (addr == offsetof(struct user, u_fpvalid)) {
158                         tmp = !!tsk_used_math(child);
159                 } else {
160                         break;
161                 }
162                 ret = put_user(tmp, (unsigned long *)data);
163                 break;
164         }
165
166         case PTRACE_POKEUSR:
167                 /* write the word at location addr in the USER area. We must
168                    disallow any changes to certain SR bits or u_fpvalid, since
169                    this could crash the kernel or result in a security
170                    loophole. */
171                 ret = -EIO;
172                 if ((addr & 3) || addr < 0)
173                         break;
174
175                 if (addr < sizeof(struct pt_regs)) {
176                         /* Ignore change of top 32 bits of SR */
177                         if (addr == offsetof (struct pt_regs, sr)+4)
178                         {
179                                 ret = 0;
180                                 break;
181                         }
182                         /* If lower 32 bits of SR, ignore non-user bits */
183                         if (addr == offsetof (struct pt_regs, sr))
184                         {
185                                 long cursr = get_stack_long(child, addr);
186                                 data &= ~(SR_MASK);
187                                 data |= (cursr & SR_MASK);
188                         }
189                         ret = put_stack_long(child, addr, data);
190                 }
191                 else if ((addr >= offsetof(struct user, fpu)) &&
192                          (addr <  offsetof(struct user, u_fpvalid))) {
193                         ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
194                 }
195                 break;
196
197         default:
198                 ret = ptrace_request(child, request, addr, data);
199                 break;
200         }
201         return ret;
202 }
203
204 asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
205 {
206 #define WPC_DBRMODE 0x0d104008
207         static int first_call = 1;
208
209         lock_kernel();
210         if (first_call) {
211                 /* Set WPC.DBRMODE to 0.  This makes all debug events get
212                  * delivered through RESVEC, i.e. into the handlers in entry.S.
213                  * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
214                  * would normally be left set to 1, which makes debug events get
215                  * delivered through DBRVEC, i.e. into the remote gdb's
216                  * handlers.  This prevents ptrace getting them, and confuses
217                  * the remote gdb.) */
218                 printk("DBRMODE set to 0 to permit native debugging\n");
219                 poke_real_address_q(WPC_DBRMODE, 0);
220                 first_call = 0;
221         }
222         unlock_kernel();
223
224         return sys_ptrace(request, pid, addr, data);
225 }
226
227 static inline int audit_arch(void)
228 {
229         int arch = EM_SH;
230
231 #ifdef CONFIG_64BIT
232         arch |= __AUDIT_ARCH_64BIT;
233 #endif
234 #ifdef CONFIG_CPU_LITTLE_ENDIAN
235         arch |= __AUDIT_ARCH_LE;
236 #endif
237
238         return arch;
239 }
240
241 asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
242 {
243         long long ret = 0;
244
245         secure_computing(regs->regs[9]);
246
247         if (test_thread_flag(TIF_SYSCALL_TRACE) &&
248             tracehook_report_syscall_entry(regs))
249                 /*
250                  * Tracing decided this syscall should not happen.
251                  * We'll return a bogus call number to get an ENOSYS
252                  * error, but leave the original number in regs->regs[0].
253                  */
254                 ret = -1LL;
255
256         if (unlikely(current->audit_context))
257                 audit_syscall_entry(audit_arch(), regs->regs[1],
258                                     regs->regs[2], regs->regs[3],
259                                     regs->regs[4], regs->regs[5]);
260
261         return ret ?: regs->regs[9];
262 }
263
264 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
265 {
266         if (unlikely(current->audit_context))
267                 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
268                                    regs->regs[9]);
269
270         if (test_thread_flag(TIF_SYSCALL_TRACE))
271                 tracehook_report_syscall_exit(regs, 0);
272 }
273
274 /* Called with interrupts disabled */
275 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
276 {
277         /* This is called after a single step exception (DEBUGSS).
278            There is no need to change the PC, as it is a post-execution
279            exception, as entry.S does not do anything to the PC for DEBUGSS.
280            We need to clear the Single Step setting in SR to avoid
281            continually stepping. */
282         local_irq_enable();
283         regs->sr &= ~SR_SSTEP;
284         force_sig(SIGTRAP, current);
285 }
286
287 /* Called with interrupts disabled */
288 asmlinkage void do_software_break_point(unsigned long long vec,
289                                         struct pt_regs *regs)
290 {
291         /* We need to forward step the PC, to counteract the backstep done
292            in signal.c. */
293         local_irq_enable();
294         force_sig(SIGTRAP, current);
295         regs->pc += 4;
296 }
297
298 /*
299  * Called by kernel/ptrace.c when detaching..
300  *
301  * Make sure single step bits etc are not set.
302  */
303 void ptrace_disable(struct task_struct *child)
304 {
305         user_disable_single_step(child);
306 }