Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
4b27c47c | 2 | * arch/sh/kernel/ptrace_64.c |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2000, 2001 Paolo Alberelli | |
dd76279b | 5 | * Copyright (C) 2003 - 2008 Paul Mundt |
1da177e4 LT |
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 | * | |
4b27c47c PM |
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. | |
1da177e4 | 17 | */ |
1da177e4 LT |
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> | |
7ed20e1a | 27 | #include <linux/signal.h> |
481bed45 | 28 | #include <linux/syscalls.h> |
4b27c47c | 29 | #include <linux/audit.h> |
c4637d47 | 30 | #include <linux/seccomp.h> |
ab99c733 | 31 | #include <linux/tracehook.h> |
dd76279b PM |
32 | #include <linux/elf.h> |
33 | #include <linux/regset.h> | |
1da177e4 LT |
34 | #include <asm/io.h> |
35 | #include <asm/uaccess.h> | |
36 | #include <asm/pgtable.h> | |
37 | #include <asm/system.h> | |
38 | #include <asm/processor.h> | |
39 | #include <asm/mmu_context.h> | |
fa43972f | 40 | #include <asm/syscalls.h> |
50387b3e | 41 | #include <asm/fpu.h> |
1da177e4 | 42 | |
a74f7e04 PM |
43 | #define CREATE_TRACE_POINTS |
44 | #include <trace/events/syscalls.h> | |
45 | ||
1da177e4 LT |
46 | /* This mask defines the bits of the SR which the user is not allowed to |
47 | change, which are everything except S, Q, M, PR, SZ, FR. */ | |
48 | #define SR_MASK (0xffff8cfd) | |
49 | ||
50 | /* | |
51 | * does not yet catch signals sent when the child dies. | |
52 | * in exit.c or in signal.c. | |
53 | */ | |
54 | ||
55 | /* | |
56 | * This routine will get a word from the user area in the process kernel stack. | |
57 | */ | |
58 | static inline int get_stack_long(struct task_struct *task, int offset) | |
59 | { | |
60 | unsigned char *stack; | |
61 | ||
62 | stack = (unsigned char *)(task->thread.uregs); | |
63 | stack += offset; | |
64 | return (*((int *)stack)); | |
65 | } | |
66 | ||
67 | static inline unsigned long | |
68 | get_fpu_long(struct task_struct *task, unsigned long addr) | |
69 | { | |
70 | unsigned long tmp; | |
71 | struct pt_regs *regs; | |
72 | regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1; | |
73 | ||
74 | if (!tsk_used_math(task)) { | |
75 | if (addr == offsetof(struct user_fpu_struct, fpscr)) { | |
76 | tmp = FPSCR_INIT; | |
77 | } else { | |
78 | tmp = 0xffffffffUL; /* matches initial value in fpu.c */ | |
79 | } | |
80 | return tmp; | |
81 | } | |
82 | ||
83 | if (last_task_used_math == task) { | |
256b22ca | 84 | enable_fpu(); |
332fd57b | 85 | save_fpu(task, regs); |
256b22ca | 86 | disable_fpu(); |
1da177e4 LT |
87 | last_task_used_math = 0; |
88 | regs->sr |= SR_FD; | |
89 | } | |
90 | ||
91 | tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)]; | |
92 | return tmp; | |
93 | } | |
94 | ||
95 | /* | |
96 | * This routine will put a word into the user area in the process kernel stack. | |
97 | */ | |
98 | static inline int put_stack_long(struct task_struct *task, int offset, | |
99 | unsigned long data) | |
100 | { | |
101 | unsigned char *stack; | |
102 | ||
103 | stack = (unsigned char *)(task->thread.uregs); | |
104 | stack += offset; | |
105 | *(unsigned long *) stack = data; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static inline int | |
110 | put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data) | |
111 | { | |
112 | struct pt_regs *regs; | |
113 | ||
114 | regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1; | |
115 | ||
116 | if (!tsk_used_math(task)) { | |
117 | fpinit(&task->thread.fpu.hard); | |
118 | set_stopped_child_used_math(task); | |
119 | } else if (last_task_used_math == task) { | |
256b22ca | 120 | enable_fpu(); |
332fd57b | 121 | save_fpu(task, regs); |
256b22ca | 122 | disable_fpu(); |
1da177e4 LT |
123 | last_task_used_math = 0; |
124 | regs->sr |= SR_FD; | |
125 | } | |
126 | ||
127 | ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data; | |
128 | return 0; | |
129 | } | |
130 | ||
c459dbf2 PM |
131 | void user_enable_single_step(struct task_struct *child) |
132 | { | |
133 | struct pt_regs *regs = child->thread.uregs; | |
134 | ||
135 | regs->sr |= SR_SSTEP; /* auto-resetting upon exception */ | |
136 | } | |
137 | ||
138 | void user_disable_single_step(struct task_struct *child) | |
139 | { | |
e311be52 AB |
140 | struct pt_regs *regs = child->thread.uregs; |
141 | ||
c459dbf2 PM |
142 | regs->sr &= ~SR_SSTEP; |
143 | } | |
481bed45 | 144 | |
dd76279b PM |
145 | static int genregs_get(struct task_struct *target, |
146 | const struct user_regset *regset, | |
147 | unsigned int pos, unsigned int count, | |
148 | void *kbuf, void __user *ubuf) | |
149 | { | |
150 | const struct pt_regs *regs = task_pt_regs(target); | |
151 | int ret; | |
152 | ||
153 | /* PC, SR, SYSCALL */ | |
154 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, | |
155 | ®s->pc, | |
156 | 0, 3 * sizeof(unsigned long long)); | |
157 | ||
158 | /* R1 -> R63 */ | |
159 | if (!ret) | |
160 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, | |
161 | regs->regs, | |
162 | offsetof(struct pt_regs, regs[0]), | |
163 | 63 * sizeof(unsigned long long)); | |
164 | /* TR0 -> TR7 */ | |
165 | if (!ret) | |
166 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, | |
167 | regs->tregs, | |
168 | offsetof(struct pt_regs, tregs[0]), | |
169 | 8 * sizeof(unsigned long long)); | |
170 | ||
171 | if (!ret) | |
172 | ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, | |
173 | sizeof(struct pt_regs), -1); | |
174 | ||
175 | return ret; | |
176 | } | |
177 | ||
178 | static int genregs_set(struct task_struct *target, | |
179 | const struct user_regset *regset, | |
180 | unsigned int pos, unsigned int count, | |
181 | const void *kbuf, const void __user *ubuf) | |
182 | { | |
183 | struct pt_regs *regs = task_pt_regs(target); | |
184 | int ret; | |
185 | ||
186 | /* PC, SR, SYSCALL */ | |
187 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, | |
188 | ®s->pc, | |
189 | 0, 3 * sizeof(unsigned long long)); | |
190 | ||
191 | /* R1 -> R63 */ | |
192 | if (!ret && count > 0) | |
193 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, | |
194 | regs->regs, | |
195 | offsetof(struct pt_regs, regs[0]), | |
196 | 63 * sizeof(unsigned long long)); | |
197 | ||
198 | /* TR0 -> TR7 */ | |
199 | if (!ret && count > 0) | |
200 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, | |
201 | regs->tregs, | |
202 | offsetof(struct pt_regs, tregs[0]), | |
203 | 8 * sizeof(unsigned long long)); | |
204 | ||
205 | if (!ret) | |
206 | ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, | |
207 | sizeof(struct pt_regs), -1); | |
208 | ||
209 | return ret; | |
210 | } | |
211 | ||
212 | #ifdef CONFIG_SH_FPU | |
213 | int fpregs_get(struct task_struct *target, | |
214 | const struct user_regset *regset, | |
215 | unsigned int pos, unsigned int count, | |
216 | void *kbuf, void __user *ubuf) | |
217 | { | |
218 | int ret; | |
219 | ||
220 | ret = init_fpu(target); | |
221 | if (ret) | |
222 | return ret; | |
223 | ||
224 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, | |
225 | &target->thread.fpu.hard, 0, -1); | |
226 | } | |
227 | ||
228 | static int fpregs_set(struct task_struct *target, | |
229 | const struct user_regset *regset, | |
230 | unsigned int pos, unsigned int count, | |
231 | const void *kbuf, const void __user *ubuf) | |
232 | { | |
233 | int ret; | |
234 | ||
235 | ret = init_fpu(target); | |
236 | if (ret) | |
237 | return ret; | |
238 | ||
239 | set_stopped_child_used_math(target); | |
240 | ||
241 | return user_regset_copyin(&pos, &count, &kbuf, &ubuf, | |
242 | &target->thread.fpu.hard, 0, -1); | |
243 | } | |
244 | ||
245 | static int fpregs_active(struct task_struct *target, | |
246 | const struct user_regset *regset) | |
247 | { | |
248 | return tsk_used_math(target) ? regset->n : 0; | |
249 | } | |
250 | #endif | |
251 | ||
252 | /* | |
253 | * These are our native regset flavours. | |
254 | */ | |
255 | enum sh_regset { | |
256 | REGSET_GENERAL, | |
257 | #ifdef CONFIG_SH_FPU | |
258 | REGSET_FPU, | |
259 | #endif | |
260 | }; | |
261 | ||
262 | static const struct user_regset sh_regsets[] = { | |
263 | /* | |
264 | * Format is: | |
265 | * PC, SR, SYSCALL, | |
266 | * R1 --> R63, | |
267 | * TR0 --> TR7, | |
268 | */ | |
269 | [REGSET_GENERAL] = { | |
270 | .core_note_type = NT_PRSTATUS, | |
271 | .n = ELF_NGREG, | |
272 | .size = sizeof(long long), | |
273 | .align = sizeof(long long), | |
274 | .get = genregs_get, | |
275 | .set = genregs_set, | |
276 | }, | |
277 | ||
278 | #ifdef CONFIG_SH_FPU | |
279 | [REGSET_FPU] = { | |
280 | .core_note_type = NT_PRFPREG, | |
281 | .n = sizeof(struct user_fpu_struct) / | |
282 | sizeof(long long), | |
283 | .size = sizeof(long long), | |
284 | .align = sizeof(long long), | |
285 | .get = fpregs_get, | |
286 | .set = fpregs_set, | |
287 | .active = fpregs_active, | |
288 | }, | |
289 | #endif | |
290 | }; | |
291 | ||
292 | static const struct user_regset_view user_sh64_native_view = { | |
293 | .name = "sh64", | |
294 | .e_machine = EM_SH, | |
295 | .regsets = sh_regsets, | |
296 | .n = ARRAY_SIZE(sh_regsets), | |
297 | }; | |
298 | ||
299 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) | |
300 | { | |
301 | return &user_sh64_native_view; | |
302 | } | |
303 | ||
481bed45 | 304 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
1da177e4 | 305 | { |
1da177e4 LT |
306 | int ret; |
307 | ||
1da177e4 | 308 | switch (request) { |
1da177e4 LT |
309 | /* read the word at location addr in the USER area. */ |
310 | case PTRACE_PEEKUSR: { | |
311 | unsigned long tmp; | |
312 | ||
313 | ret = -EIO; | |
314 | if ((addr & 3) || addr < 0) | |
315 | break; | |
316 | ||
317 | if (addr < sizeof(struct pt_regs)) | |
318 | tmp = get_stack_long(child, addr); | |
319 | else if ((addr >= offsetof(struct user, fpu)) && | |
320 | (addr < offsetof(struct user, u_fpvalid))) { | |
321 | tmp = get_fpu_long(child, addr - offsetof(struct user, fpu)); | |
322 | } else if (addr == offsetof(struct user, u_fpvalid)) { | |
323 | tmp = !!tsk_used_math(child); | |
324 | } else { | |
325 | break; | |
326 | } | |
327 | ret = put_user(tmp, (unsigned long *)data); | |
328 | break; | |
329 | } | |
330 | ||
1da177e4 LT |
331 | case PTRACE_POKEUSR: |
332 | /* write the word at location addr in the USER area. We must | |
333 | disallow any changes to certain SR bits or u_fpvalid, since | |
334 | this could crash the kernel or result in a security | |
335 | loophole. */ | |
336 | ret = -EIO; | |
337 | if ((addr & 3) || addr < 0) | |
338 | break; | |
339 | ||
340 | if (addr < sizeof(struct pt_regs)) { | |
341 | /* Ignore change of top 32 bits of SR */ | |
342 | if (addr == offsetof (struct pt_regs, sr)+4) | |
343 | { | |
344 | ret = 0; | |
345 | break; | |
346 | } | |
347 | /* If lower 32 bits of SR, ignore non-user bits */ | |
348 | if (addr == offsetof (struct pt_regs, sr)) | |
349 | { | |
350 | long cursr = get_stack_long(child, addr); | |
351 | data &= ~(SR_MASK); | |
352 | data |= (cursr & SR_MASK); | |
353 | } | |
354 | ret = put_stack_long(child, addr, data); | |
355 | } | |
356 | else if ((addr >= offsetof(struct user, fpu)) && | |
357 | (addr < offsetof(struct user, u_fpvalid))) { | |
358 | ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data); | |
359 | } | |
360 | break; | |
361 | ||
dd76279b PM |
362 | case PTRACE_GETREGS: |
363 | return copy_regset_to_user(child, &user_sh64_native_view, | |
364 | REGSET_GENERAL, | |
365 | 0, sizeof(struct pt_regs), | |
366 | (void __user *)data); | |
367 | case PTRACE_SETREGS: | |
368 | return copy_regset_from_user(child, &user_sh64_native_view, | |
369 | REGSET_GENERAL, | |
370 | 0, sizeof(struct pt_regs), | |
371 | (const void __user *)data); | |
372 | #ifdef CONFIG_SH_FPU | |
373 | case PTRACE_GETFPREGS: | |
374 | return copy_regset_to_user(child, &user_sh64_native_view, | |
375 | REGSET_FPU, | |
376 | 0, sizeof(struct user_fpu_struct), | |
377 | (void __user *)data); | |
378 | case PTRACE_SETFPREGS: | |
379 | return copy_regset_from_user(child, &user_sh64_native_view, | |
380 | REGSET_FPU, | |
381 | 0, sizeof(struct user_fpu_struct), | |
382 | (const void __user *)data); | |
383 | #endif | |
1da177e4 LT |
384 | default: |
385 | ret = ptrace_request(child, request, addr, data); | |
386 | break; | |
387 | } | |
dd76279b | 388 | |
1da177e4 LT |
389 | return ret; |
390 | } | |
391 | ||
481bed45 CH |
392 | asmlinkage int sh64_ptrace(long request, long pid, long addr, long data) |
393 | { | |
481bed45 CH |
394 | #define WPC_DBRMODE 0x0d104008 |
395 | static int first_call = 1; | |
396 | ||
397 | lock_kernel(); | |
398 | if (first_call) { | |
399 | /* Set WPC.DBRMODE to 0. This makes all debug events get | |
400 | * delivered through RESVEC, i.e. into the handlers in entry.S. | |
401 | * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE | |
402 | * would normally be left set to 1, which makes debug events get | |
403 | * delivered through DBRVEC, i.e. into the remote gdb's | |
404 | * handlers. This prevents ptrace getting them, and confuses | |
405 | * the remote gdb.) */ | |
406 | printk("DBRMODE set to 0 to permit native debugging\n"); | |
407 | poke_real_address_q(WPC_DBRMODE, 0); | |
408 | first_call = 0; | |
409 | } | |
410 | unlock_kernel(); | |
411 | ||
412 | return sys_ptrace(request, pid, addr, data); | |
413 | } | |
414 | ||
9e5e2117 PM |
415 | static inline int audit_arch(void) |
416 | { | |
417 | int arch = EM_SH; | |
418 | ||
419 | #ifdef CONFIG_64BIT | |
420 | arch |= __AUDIT_ARCH_64BIT; | |
421 | #endif | |
422 | #ifdef CONFIG_CPU_LITTLE_ENDIAN | |
423 | arch |= __AUDIT_ARCH_LE; | |
424 | #endif | |
425 | ||
426 | return arch; | |
427 | } | |
428 | ||
ab99c733 | 429 | asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs) |
1da177e4 | 430 | { |
ab99c733 | 431 | long long ret = 0; |
1da177e4 | 432 | |
c4637d47 PM |
433 | secure_computing(regs->regs[9]); |
434 | ||
ab99c733 PM |
435 | if (test_thread_flag(TIF_SYSCALL_TRACE) && |
436 | tracehook_report_syscall_entry(regs)) | |
437 | /* | |
438 | * Tracing decided this syscall should not happen. | |
439 | * We'll return a bogus call number to get an ENOSYS | |
440 | * error, but leave the original number in regs->regs[0]. | |
441 | */ | |
442 | ret = -1LL; | |
4b27c47c | 443 | |
a74f7e04 PM |
444 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
445 | trace_sys_enter(regs, regs->regs[9]); | |
446 | ||
ab99c733 | 447 | if (unlikely(current->audit_context)) |
9e5e2117 | 448 | audit_syscall_entry(audit_arch(), regs->regs[1], |
4b27c47c PM |
449 | regs->regs[2], regs->regs[3], |
450 | regs->regs[4], regs->regs[5]); | |
ab99c733 PM |
451 | |
452 | return ret ?: regs->regs[9]; | |
453 | } | |
454 | ||
455 | asmlinkage void do_syscall_trace_leave(struct pt_regs *regs) | |
456 | { | |
457 | if (unlikely(current->audit_context)) | |
458 | audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]), | |
459 | regs->regs[9]); | |
460 | ||
a74f7e04 PM |
461 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
462 | trace_sys_exit(regs, regs->regs[9]); | |
463 | ||
ab99c733 PM |
464 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
465 | tracehook_report_syscall_exit(regs, 0); | |
1da177e4 LT |
466 | } |
467 | ||
468 | /* Called with interrupts disabled */ | |
469 | asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs) | |
470 | { | |
471 | /* This is called after a single step exception (DEBUGSS). | |
472 | There is no need to change the PC, as it is a post-execution | |
473 | exception, as entry.S does not do anything to the PC for DEBUGSS. | |
474 | We need to clear the Single Step setting in SR to avoid | |
475 | continually stepping. */ | |
476 | local_irq_enable(); | |
477 | regs->sr &= ~SR_SSTEP; | |
478 | force_sig(SIGTRAP, current); | |
479 | } | |
480 | ||
481 | /* Called with interrupts disabled */ | |
482 | asmlinkage void do_software_break_point(unsigned long long vec, | |
483 | struct pt_regs *regs) | |
484 | { | |
485 | /* We need to forward step the PC, to counteract the backstep done | |
486 | in signal.c. */ | |
487 | local_irq_enable(); | |
488 | force_sig(SIGTRAP, current); | |
489 | regs->pc += 4; | |
490 | } | |
491 | ||
492 | /* | |
493 | * Called by kernel/ptrace.c when detaching.. | |
494 | * | |
495 | * Make sure single step bits etc are not set. | |
496 | */ | |
497 | void ptrace_disable(struct task_struct *child) | |
498 | { | |
c459dbf2 | 499 | user_disable_single_step(child); |
1da177e4 | 500 | } |