1 // SPDX-License-Identifier: GPL-2.0
2 /* ptrace.c: Sparc process tracing support.
6 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
9 * Added Linux support -miguel (weird, eh?, the original code was meant
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/user.h>
19 #include <linux/smp.h>
20 #include <linux/security.h>
21 #include <linux/signal.h>
22 #include <linux/regset.h>
23 #include <linux/elf.h>
24 #include <linux/tracehook.h>
26 #include <asm/pgtable.h>
27 #include <linux/uaccess.h>
28 #include <asm/cacheflush.h>
32 /* #define ALLOW_INIT_TRACING */
35 * Called by kernel/ptrace.c when detaching..
37 * Make sure single step bits etc are not set.
39 void ptrace_disable(struct task_struct *child)
49 static int genregs32_get(struct task_struct *target,
50 const struct user_regset *regset,
51 unsigned int pos, unsigned int count,
52 void *kbuf, void __user *ubuf)
54 const struct pt_regs *regs = target->thread.kregs;
55 unsigned long __user *reg_window;
56 unsigned long *k = kbuf;
57 unsigned long __user *u = ubuf;
60 if (target == current)
67 for (; count > 0 && pos < 16; count--)
68 *k++ = regs->u_regs[pos++];
70 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
72 for (; count > 0 && pos < 32; count--) {
73 if (get_user(*k++, ®_window[pos++]))
77 for (; count > 0 && pos < 16; count--) {
78 if (put_user(regs->u_regs[pos++], u++))
82 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
84 for (; count > 0 && pos < 32; count--) {
85 if (get_user(reg, ®_window[pos++]) ||
114 else if (put_user(reg, u++))
121 count *= sizeof(reg);
123 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
124 38 * sizeof(reg), -1);
127 static int genregs32_set(struct task_struct *target,
128 const struct user_regset *regset,
129 unsigned int pos, unsigned int count,
130 const void *kbuf, const void __user *ubuf)
132 struct pt_regs *regs = target->thread.kregs;
133 unsigned long __user *reg_window;
134 const unsigned long *k = kbuf;
135 const unsigned long __user *u = ubuf;
138 if (target == current)
139 flush_user_windows();
142 count /= sizeof(reg);
145 for (; count > 0 && pos < 16; count--)
146 regs->u_regs[pos++] = *k++;
148 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
150 for (; count > 0 && pos < 32; count--) {
151 if (put_user(*k++, ®_window[pos++]))
155 for (; count > 0 && pos < 16; count--) {
156 if (get_user(reg, u++))
158 regs->u_regs[pos++] = reg;
161 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
163 for (; count > 0 && pos < 32; count--) {
164 if (get_user(reg, u++) ||
165 put_user(reg, ®_window[pos++]))
174 else if (get_user(reg, u++))
180 psr &= ~(PSR_ICC | PSR_SYSCALL);
181 psr |= (reg & (PSR_ICC | PSR_SYSCALL));
205 count *= sizeof(reg);
207 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
208 38 * sizeof(reg), -1);
211 static int fpregs32_get(struct task_struct *target,
212 const struct user_regset *regset,
213 unsigned int pos, unsigned int count,
214 void *kbuf, void __user *ubuf)
216 const unsigned long *fpregs = target->thread.float_regs;
220 if (target == current)
221 save_and_clear_fpu();
224 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
226 0, 32 * sizeof(u32));
229 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
233 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
241 val = (1 << 8) | (8 << 16);
242 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
249 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
250 35 * sizeof(u32), -1);
255 static int fpregs32_set(struct task_struct *target,
256 const struct user_regset *regset,
257 unsigned int pos, unsigned int count,
258 const void *kbuf, const void __user *ubuf)
260 unsigned long *fpregs = target->thread.float_regs;
264 if (target == current)
265 save_and_clear_fpu();
267 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
269 0, 32 * sizeof(u32));
271 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
274 if (!ret && count > 0) {
275 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
282 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
283 34 * sizeof(u32), -1);
287 static const struct user_regset sparc32_regsets[] = {
293 * PSR, PC, nPC, Y, WIM, TBR
296 .core_note_type = NT_PRSTATUS,
298 .size = sizeof(u32), .align = sizeof(u32),
299 .get = genregs32_get, .set = genregs32_set
305 * FPU QUEUE COUNT (8-bit char)
306 * FPU QUEUE ENTRYSIZE (8-bit char)
307 * FPU ENABLED (8-bit char)
309 * FPU QUEUE (64 32-bit ints)
312 .core_note_type = NT_PRFPREG,
314 .size = sizeof(u32), .align = sizeof(u32),
315 .get = fpregs32_get, .set = fpregs32_set
319 static const struct user_regset_view user_sparc32_view = {
320 .name = "sparc", .e_machine = EM_SPARC,
321 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
324 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
326 return &user_sparc32_view;
330 unsigned long regs[32];
336 unsigned long *insnaddr;
341 long arch_ptrace(struct task_struct *child, long request,
342 unsigned long addr, unsigned long data)
344 unsigned long addr2 = current->thread.kregs->u_regs[UREG_I4];
346 const struct user_regset_view *view;
347 struct pt_regs __user *pregs;
348 struct fps __user *fps;
351 view = task_user_regset_view(current);
352 addr2p = (void __user *) addr2;
353 pregs = (struct pt_regs __user *) addr;
354 fps = (struct fps __user *) addr;
357 case PTRACE_GETREGS: {
358 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
363 copy_regset_to_user(child, view, REGSET_GENERAL,
370 case PTRACE_SETREGS: {
371 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
376 copy_regset_from_user(child, view, REGSET_GENERAL,
383 case PTRACE_GETFPREGS: {
384 ret = copy_regset_to_user(child, view, REGSET_FP,
389 ret = copy_regset_to_user(child, view, REGSET_FP,
395 if (__put_user(0, &fps->fpqd) ||
396 __put_user(0, &fps->flags) ||
397 __put_user(0, &fps->extra) ||
398 clear_user(fps->fpq, sizeof(fps->fpq)))
404 case PTRACE_SETFPREGS: {
405 ret = copy_regset_from_user(child, view, REGSET_FP,
410 ret = copy_regset_from_user(child, view, REGSET_FP,
417 case PTRACE_READTEXT:
418 case PTRACE_READDATA:
419 ret = ptrace_readdata(child, addr, addr2p, data);
427 case PTRACE_WRITETEXT:
428 case PTRACE_WRITEDATA:
429 ret = ptrace_writedata(child, addr2p, addr, data);
438 if (request == PTRACE_SPARC_DETACH)
439 request = PTRACE_DETACH;
440 ret = ptrace_request(child, request, addr, data);
447 asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
451 if (test_thread_flag(TIF_SYSCALL_TRACE)) {
453 tracehook_report_syscall_exit(regs, 0);
455 ret = tracehook_report_syscall_entry(regs);