x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / arch / sh / kernel / ptrace_64.c
blob1e0656d9e7afa645780b2eb044479927c5e9a967
1 /*
2 * arch/sh/kernel/ptrace_64.c
4 * Copyright (C) 2000, 2001 Paolo Alberelli
5 * Copyright (C) 2003 - 2008 Paul Mundt
7 * Started from SH3/4 version:
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
10 * Original x86 implementation:
11 * By Ross Biro 1/23/92
12 * edited by Linus Torvalds
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.
18 #include <linux/kernel.h>
19 #include <linux/rwsem.h>
20 #include <linux/sched.h>
21 #include <linux/sched/task_stack.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/bitops.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/signal.h>
29 #include <linux/syscalls.h>
30 #include <linux/audit.h>
31 #include <linux/seccomp.h>
32 #include <linux/tracehook.h>
33 #include <linux/elf.h>
34 #include <linux/regset.h>
35 #include <asm/io.h>
36 #include <linux/uaccess.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
39 #include <asm/mmu_context.h>
40 #include <asm/syscalls.h>
41 #include <asm/fpu.h>
42 #include <asm/traps.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/syscalls.h>
47 /* This mask defines the bits of the SR which the user is not allowed to
48 change, which are everything except S, Q, M, PR, SZ, FR. */
49 #define SR_MASK (0xffff8cfd)
52 * does not yet catch signals sent when the child dies.
53 * in exit.c or in signal.c.
57 * This routine will get a word from the user area in the process kernel stack.
59 static inline int get_stack_long(struct task_struct *task, int offset)
61 unsigned char *stack;
63 stack = (unsigned char *)(task->thread.uregs);
64 stack += offset;
65 return (*((int *)stack));
68 static inline unsigned long
69 get_fpu_long(struct task_struct *task, unsigned long addr)
71 unsigned long tmp;
72 struct pt_regs *regs;
73 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
75 if (!tsk_used_math(task)) {
76 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
77 tmp = FPSCR_INIT;
78 } else {
79 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
81 return tmp;
84 if (last_task_used_math == task) {
85 enable_fpu();
86 save_fpu(task);
87 disable_fpu();
88 last_task_used_math = 0;
89 regs->sr |= SR_FD;
92 tmp = ((long *)task->thread.xstate)[addr / sizeof(unsigned long)];
93 return tmp;
97 * This routine will put a word into the user area in the process kernel stack.
99 static inline int put_stack_long(struct task_struct *task, int offset,
100 unsigned long data)
102 unsigned char *stack;
104 stack = (unsigned char *)(task->thread.uregs);
105 stack += offset;
106 *(unsigned long *) stack = data;
107 return 0;
110 static inline int
111 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
113 struct pt_regs *regs;
115 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
117 if (!tsk_used_math(task)) {
118 init_fpu(task);
119 } else if (last_task_used_math == task) {
120 enable_fpu();
121 save_fpu(task);
122 disable_fpu();
123 last_task_used_math = 0;
124 regs->sr |= SR_FD;
127 ((long *)task->thread.xstate)[addr / sizeof(unsigned long)] = data;
128 return 0;
131 void user_enable_single_step(struct task_struct *child)
133 struct pt_regs *regs = child->thread.uregs;
135 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
137 set_tsk_thread_flag(child, TIF_SINGLESTEP);
140 void user_disable_single_step(struct task_struct *child)
142 struct pt_regs *regs = child->thread.uregs;
144 regs->sr &= ~SR_SSTEP;
146 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
149 static int genregs_get(struct task_struct *target,
150 const struct user_regset *regset,
151 unsigned int pos, unsigned int count,
152 void *kbuf, void __user *ubuf)
154 const struct pt_regs *regs = task_pt_regs(target);
155 int ret;
157 /* PC, SR, SYSCALL */
158 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
159 &regs->pc,
160 0, 3 * sizeof(unsigned long long));
162 /* R1 -> R63 */
163 if (!ret)
164 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
165 regs->regs,
166 offsetof(struct pt_regs, regs[0]),
167 63 * sizeof(unsigned long long));
168 /* TR0 -> TR7 */
169 if (!ret)
170 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
171 regs->tregs,
172 offsetof(struct pt_regs, tregs[0]),
173 8 * sizeof(unsigned long long));
175 if (!ret)
176 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
177 sizeof(struct pt_regs), -1);
179 return ret;
182 static int genregs_set(struct task_struct *target,
183 const struct user_regset *regset,
184 unsigned int pos, unsigned int count,
185 const void *kbuf, const void __user *ubuf)
187 struct pt_regs *regs = task_pt_regs(target);
188 int ret;
190 /* PC, SR, SYSCALL */
191 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
192 &regs->pc,
193 0, 3 * sizeof(unsigned long long));
195 /* R1 -> R63 */
196 if (!ret && count > 0)
197 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
198 regs->regs,
199 offsetof(struct pt_regs, regs[0]),
200 63 * sizeof(unsigned long long));
202 /* TR0 -> TR7 */
203 if (!ret && count > 0)
204 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
205 regs->tregs,
206 offsetof(struct pt_regs, tregs[0]),
207 8 * sizeof(unsigned long long));
209 if (!ret)
210 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
211 sizeof(struct pt_regs), -1);
213 return ret;
216 #ifdef CONFIG_SH_FPU
217 int fpregs_get(struct task_struct *target,
218 const struct user_regset *regset,
219 unsigned int pos, unsigned int count,
220 void *kbuf, void __user *ubuf)
222 int ret;
224 ret = init_fpu(target);
225 if (ret)
226 return ret;
228 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
229 &target->thread.xstate->hardfpu, 0, -1);
232 static int fpregs_set(struct task_struct *target,
233 const struct user_regset *regset,
234 unsigned int pos, unsigned int count,
235 const void *kbuf, const void __user *ubuf)
237 int ret;
239 ret = init_fpu(target);
240 if (ret)
241 return ret;
243 set_stopped_child_used_math(target);
245 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
246 &target->thread.xstate->hardfpu, 0, -1);
249 static int fpregs_active(struct task_struct *target,
250 const struct user_regset *regset)
252 return tsk_used_math(target) ? regset->n : 0;
254 #endif
256 const struct pt_regs_offset regoffset_table[] = {
257 REG_OFFSET_NAME(pc),
258 REG_OFFSET_NAME(sr),
259 REG_OFFSET_NAME(syscall_nr),
260 REGS_OFFSET_NAME(0),
261 REGS_OFFSET_NAME(1),
262 REGS_OFFSET_NAME(2),
263 REGS_OFFSET_NAME(3),
264 REGS_OFFSET_NAME(4),
265 REGS_OFFSET_NAME(5),
266 REGS_OFFSET_NAME(6),
267 REGS_OFFSET_NAME(7),
268 REGS_OFFSET_NAME(8),
269 REGS_OFFSET_NAME(9),
270 REGS_OFFSET_NAME(10),
271 REGS_OFFSET_NAME(11),
272 REGS_OFFSET_NAME(12),
273 REGS_OFFSET_NAME(13),
274 REGS_OFFSET_NAME(14),
275 REGS_OFFSET_NAME(15),
276 REGS_OFFSET_NAME(16),
277 REGS_OFFSET_NAME(17),
278 REGS_OFFSET_NAME(18),
279 REGS_OFFSET_NAME(19),
280 REGS_OFFSET_NAME(20),
281 REGS_OFFSET_NAME(21),
282 REGS_OFFSET_NAME(22),
283 REGS_OFFSET_NAME(23),
284 REGS_OFFSET_NAME(24),
285 REGS_OFFSET_NAME(25),
286 REGS_OFFSET_NAME(26),
287 REGS_OFFSET_NAME(27),
288 REGS_OFFSET_NAME(28),
289 REGS_OFFSET_NAME(29),
290 REGS_OFFSET_NAME(30),
291 REGS_OFFSET_NAME(31),
292 REGS_OFFSET_NAME(32),
293 REGS_OFFSET_NAME(33),
294 REGS_OFFSET_NAME(34),
295 REGS_OFFSET_NAME(35),
296 REGS_OFFSET_NAME(36),
297 REGS_OFFSET_NAME(37),
298 REGS_OFFSET_NAME(38),
299 REGS_OFFSET_NAME(39),
300 REGS_OFFSET_NAME(40),
301 REGS_OFFSET_NAME(41),
302 REGS_OFFSET_NAME(42),
303 REGS_OFFSET_NAME(43),
304 REGS_OFFSET_NAME(44),
305 REGS_OFFSET_NAME(45),
306 REGS_OFFSET_NAME(46),
307 REGS_OFFSET_NAME(47),
308 REGS_OFFSET_NAME(48),
309 REGS_OFFSET_NAME(49),
310 REGS_OFFSET_NAME(50),
311 REGS_OFFSET_NAME(51),
312 REGS_OFFSET_NAME(52),
313 REGS_OFFSET_NAME(53),
314 REGS_OFFSET_NAME(54),
315 REGS_OFFSET_NAME(55),
316 REGS_OFFSET_NAME(56),
317 REGS_OFFSET_NAME(57),
318 REGS_OFFSET_NAME(58),
319 REGS_OFFSET_NAME(59),
320 REGS_OFFSET_NAME(60),
321 REGS_OFFSET_NAME(61),
322 REGS_OFFSET_NAME(62),
323 REGS_OFFSET_NAME(63),
324 TREGS_OFFSET_NAME(0),
325 TREGS_OFFSET_NAME(1),
326 TREGS_OFFSET_NAME(2),
327 TREGS_OFFSET_NAME(3),
328 TREGS_OFFSET_NAME(4),
329 TREGS_OFFSET_NAME(5),
330 TREGS_OFFSET_NAME(6),
331 TREGS_OFFSET_NAME(7),
332 REG_OFFSET_END,
336 * These are our native regset flavours.
338 enum sh_regset {
339 REGSET_GENERAL,
340 #ifdef CONFIG_SH_FPU
341 REGSET_FPU,
342 #endif
345 static const struct user_regset sh_regsets[] = {
347 * Format is:
348 * PC, SR, SYSCALL,
349 * R1 --> R63,
350 * TR0 --> TR7,
352 [REGSET_GENERAL] = {
353 .core_note_type = NT_PRSTATUS,
354 .n = ELF_NGREG,
355 .size = sizeof(long long),
356 .align = sizeof(long long),
357 .get = genregs_get,
358 .set = genregs_set,
361 #ifdef CONFIG_SH_FPU
362 [REGSET_FPU] = {
363 .core_note_type = NT_PRFPREG,
364 .n = sizeof(struct user_fpu_struct) /
365 sizeof(long long),
366 .size = sizeof(long long),
367 .align = sizeof(long long),
368 .get = fpregs_get,
369 .set = fpregs_set,
370 .active = fpregs_active,
372 #endif
375 static const struct user_regset_view user_sh64_native_view = {
376 .name = "sh64",
377 .e_machine = EM_SH,
378 .regsets = sh_regsets,
379 .n = ARRAY_SIZE(sh_regsets),
382 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
384 return &user_sh64_native_view;
387 long arch_ptrace(struct task_struct *child, long request,
388 unsigned long addr, unsigned long data)
390 int ret;
391 unsigned long __user *datap = (unsigned long __user *) data;
393 switch (request) {
394 /* read the word at location addr in the USER area. */
395 case PTRACE_PEEKUSR: {
396 unsigned long tmp;
398 ret = -EIO;
399 if ((addr & 3) || addr < 0)
400 break;
402 if (addr < sizeof(struct pt_regs))
403 tmp = get_stack_long(child, addr);
404 else if ((addr >= offsetof(struct user, fpu)) &&
405 (addr < offsetof(struct user, u_fpvalid))) {
406 unsigned long index;
407 ret = init_fpu(child);
408 if (ret)
409 break;
410 index = addr - offsetof(struct user, fpu);
411 tmp = get_fpu_long(child, index);
412 } else if (addr == offsetof(struct user, u_fpvalid)) {
413 tmp = !!tsk_used_math(child);
414 } else {
415 break;
417 ret = put_user(tmp, datap);
418 break;
421 case PTRACE_POKEUSR:
422 /* write the word at location addr in the USER area. We must
423 disallow any changes to certain SR bits or u_fpvalid, since
424 this could crash the kernel or result in a security
425 loophole. */
426 ret = -EIO;
427 if ((addr & 3) || addr < 0)
428 break;
430 if (addr < sizeof(struct pt_regs)) {
431 /* Ignore change of top 32 bits of SR */
432 if (addr == offsetof (struct pt_regs, sr)+4)
434 ret = 0;
435 break;
437 /* If lower 32 bits of SR, ignore non-user bits */
438 if (addr == offsetof (struct pt_regs, sr))
440 long cursr = get_stack_long(child, addr);
441 data &= ~(SR_MASK);
442 data |= (cursr & SR_MASK);
444 ret = put_stack_long(child, addr, data);
446 else if ((addr >= offsetof(struct user, fpu)) &&
447 (addr < offsetof(struct user, u_fpvalid))) {
448 unsigned long index;
449 ret = init_fpu(child);
450 if (ret)
451 break;
452 index = addr - offsetof(struct user, fpu);
453 ret = put_fpu_long(child, index, data);
455 break;
457 case PTRACE_GETREGS:
458 return copy_regset_to_user(child, &user_sh64_native_view,
459 REGSET_GENERAL,
460 0, sizeof(struct pt_regs),
461 datap);
462 case PTRACE_SETREGS:
463 return copy_regset_from_user(child, &user_sh64_native_view,
464 REGSET_GENERAL,
465 0, sizeof(struct pt_regs),
466 datap);
467 #ifdef CONFIG_SH_FPU
468 case PTRACE_GETFPREGS:
469 return copy_regset_to_user(child, &user_sh64_native_view,
470 REGSET_FPU,
471 0, sizeof(struct user_fpu_struct),
472 datap);
473 case PTRACE_SETFPREGS:
474 return copy_regset_from_user(child, &user_sh64_native_view,
475 REGSET_FPU,
476 0, sizeof(struct user_fpu_struct),
477 datap);
478 #endif
479 default:
480 ret = ptrace_request(child, request, addr, data);
481 break;
484 return ret;
487 asmlinkage int sh64_ptrace(long request, long pid,
488 unsigned long addr, unsigned long data)
490 #define WPC_DBRMODE 0x0d104008
491 static unsigned long first_call;
493 if (!test_and_set_bit(0, &first_call)) {
494 /* Set WPC.DBRMODE to 0. This makes all debug events get
495 * delivered through RESVEC, i.e. into the handlers in entry.S.
496 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
497 * would normally be left set to 1, which makes debug events get
498 * delivered through DBRVEC, i.e. into the remote gdb's
499 * handlers. This prevents ptrace getting them, and confuses
500 * the remote gdb.) */
501 printk("DBRMODE set to 0 to permit native debugging\n");
502 poke_real_address_q(WPC_DBRMODE, 0);
505 return sys_ptrace(request, pid, addr, data);
508 asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
510 long long ret = 0;
512 secure_computing_strict(regs->regs[9]);
514 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
515 tracehook_report_syscall_entry(regs))
517 * Tracing decided this syscall should not happen.
518 * We'll return a bogus call number to get an ENOSYS
519 * error, but leave the original number in regs->regs[0].
521 ret = -1LL;
523 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
524 trace_sys_enter(regs, regs->regs[9]);
526 audit_syscall_entry(regs->regs[1], regs->regs[2], regs->regs[3],
527 regs->regs[4], regs->regs[5]);
529 return ret ?: regs->regs[9];
532 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
534 int step;
536 audit_syscall_exit(regs);
538 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
539 trace_sys_exit(regs, regs->regs[9]);
541 step = test_thread_flag(TIF_SINGLESTEP);
542 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
543 tracehook_report_syscall_exit(regs, step);
546 /* Called with interrupts disabled */
547 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
549 /* This is called after a single step exception (DEBUGSS).
550 There is no need to change the PC, as it is a post-execution
551 exception, as entry.S does not do anything to the PC for DEBUGSS.
552 We need to clear the Single Step setting in SR to avoid
553 continually stepping. */
554 local_irq_enable();
555 regs->sr &= ~SR_SSTEP;
556 force_sig(SIGTRAP, current);
559 /* Called with interrupts disabled */
560 BUILD_TRAP_HANDLER(breakpoint)
562 TRAP_HANDLER_DECL;
564 /* We need to forward step the PC, to counteract the backstep done
565 in signal.c. */
566 local_irq_enable();
567 force_sig(SIGTRAP, current);
568 regs->pc += 4;
572 * Called by kernel/ptrace.c when detaching..
574 * Make sure single step bits etc are not set.
576 void ptrace_disable(struct task_struct *child)
578 user_disable_single_step(child);