1 // SPDX-License-Identifier: GPL-2.0
2 /* ptrace.c: Sparc process tracing support.
4 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
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 regwindow32_get(struct task_struct *target,
50 const struct pt_regs *regs,
53 unsigned long reg_window = regs->u_regs[UREG_I6];
54 int size = 16 * sizeof(u32);
56 if (target == current) {
57 if (copy_from_user(uregs, (void __user *)reg_window, size))
60 if (access_process_vm(target, reg_window, uregs, size,
67 static int regwindow32_set(struct task_struct *target,
68 const struct pt_regs *regs,
71 unsigned long reg_window = regs->u_regs[UREG_I6];
72 int size = 16 * sizeof(u32);
74 if (target == current) {
75 if (copy_to_user((void __user *)reg_window, uregs, size))
78 if (access_process_vm(target, reg_window, uregs, size,
79 FOLL_FORCE | FOLL_WRITE) != size)
85 static int genregs32_get(struct task_struct *target,
86 const struct user_regset *regset,
87 unsigned int pos, unsigned int count,
88 void *kbuf, void __user *ubuf)
90 const struct pt_regs *regs = target->thread.kregs;
94 if (target == current)
97 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
103 if (pos < 32 * sizeof(u32)) {
104 if (regwindow32_get(target, regs, uregs))
106 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
108 16 * sizeof(u32), 32 * sizeof(u32));
113 uregs[0] = regs->psr;
115 uregs[2] = regs->npc;
117 uregs[4] = 0; /* WIM */
118 uregs[5] = 0; /* TBR */
119 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
121 32 * sizeof(u32), 38 * sizeof(u32));
124 static int genregs32_set(struct task_struct *target,
125 const struct user_regset *regset,
126 unsigned int pos, unsigned int count,
127 const void *kbuf, const void __user *ubuf)
129 struct pt_regs *regs = target->thread.kregs;
134 if (target == current)
135 flush_user_windows();
137 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
139 0, 16 * sizeof(u32));
143 if (pos < 32 * sizeof(u32)) {
144 if (regwindow32_get(target, regs, uregs))
146 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
148 16 * sizeof(u32), 32 * sizeof(u32));
151 if (regwindow32_set(target, regs, uregs))
156 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
158 32 * sizeof(u32), 33 * sizeof(u32));
161 regs->psr = (regs->psr & ~(PSR_ICC | PSR_SYSCALL)) |
162 (psr & (PSR_ICC | PSR_SYSCALL));
165 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
167 33 * sizeof(u32), 34 * sizeof(u32));
170 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
172 34 * sizeof(u32), 35 * sizeof(u32));
175 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
177 35 * sizeof(u32), 36 * sizeof(u32));
180 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
181 36 * sizeof(u32), 38 * sizeof(u32));
184 static int fpregs32_get(struct task_struct *target,
185 const struct user_regset *regset,
186 unsigned int pos, unsigned int count,
187 void *kbuf, void __user *ubuf)
189 const unsigned long *fpregs = target->thread.float_regs;
193 if (target == current)
194 save_and_clear_fpu();
197 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
199 0, 32 * sizeof(u32));
202 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
206 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
214 val = (1 << 8) | (8 << 16);
215 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
222 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
223 35 * sizeof(u32), -1);
228 static int fpregs32_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)
233 unsigned long *fpregs = target->thread.float_regs;
237 if (target == current)
238 save_and_clear_fpu();
240 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
242 0, 32 * sizeof(u32));
244 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
247 if (!ret && count > 0) {
248 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
255 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
256 34 * sizeof(u32), -1);
260 static const struct user_regset sparc32_regsets[] = {
266 * PSR, PC, nPC, Y, WIM, TBR
269 .core_note_type = NT_PRSTATUS,
271 .size = sizeof(u32), .align = sizeof(u32),
272 .get = genregs32_get, .set = genregs32_set
278 * FPU QUEUE COUNT (8-bit char)
279 * FPU QUEUE ENTRYSIZE (8-bit char)
280 * FPU ENABLED (8-bit char)
282 * FPU QUEUE (64 32-bit ints)
285 .core_note_type = NT_PRFPREG,
287 .size = sizeof(u32), .align = sizeof(u32),
288 .get = fpregs32_get, .set = fpregs32_set
292 static const struct user_regset_view user_sparc32_view = {
293 .name = "sparc", .e_machine = EM_SPARC,
294 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
297 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
299 return &user_sparc32_view;
303 unsigned long regs[32];
309 unsigned long *insnaddr;
314 long arch_ptrace(struct task_struct *child, long request,
315 unsigned long addr, unsigned long data)
317 unsigned long addr2 = current->thread.kregs->u_regs[UREG_I4];
319 const struct user_regset_view *view;
320 struct pt_regs __user *pregs;
321 struct fps __user *fps;
324 view = task_user_regset_view(current);
325 addr2p = (void __user *) addr2;
326 pregs = (struct pt_regs __user *) addr;
327 fps = (struct fps __user *) addr;
330 case PTRACE_GETREGS: {
331 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
336 copy_regset_to_user(child, view, REGSET_GENERAL,
343 case PTRACE_SETREGS: {
344 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
349 copy_regset_from_user(child, view, REGSET_GENERAL,
356 case PTRACE_GETFPREGS: {
357 ret = copy_regset_to_user(child, view, REGSET_FP,
362 ret = copy_regset_to_user(child, view, REGSET_FP,
368 if (__put_user(0, &fps->fpqd) ||
369 __put_user(0, &fps->flags) ||
370 __put_user(0, &fps->extra) ||
371 clear_user(fps->fpq, sizeof(fps->fpq)))
377 case PTRACE_SETFPREGS: {
378 ret = copy_regset_from_user(child, view, REGSET_FP,
383 ret = copy_regset_from_user(child, view, REGSET_FP,
390 case PTRACE_READTEXT:
391 case PTRACE_READDATA:
392 ret = ptrace_readdata(child, addr, addr2p, data);
400 case PTRACE_WRITETEXT:
401 case PTRACE_WRITEDATA:
402 ret = ptrace_writedata(child, addr2p, addr, data);
411 if (request == PTRACE_SPARC_DETACH)
412 request = PTRACE_DETACH;
413 ret = ptrace_request(child, request, addr, data);
420 asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
424 if (test_thread_flag(TIF_SYSCALL_TRACE)) {
426 tracehook_report_syscall_exit(regs, 0);
428 ret = tracehook_report_syscall_entry(regs);