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>
25 #include <linux/uaccess.h>
26 #include <asm/cacheflush.h>
30 /* #define ALLOW_INIT_TRACING */
33 * Called by kernel/ptrace.c when detaching..
35 * Make sure single step bits etc are not set.
37 void ptrace_disable(struct task_struct *child)
47 static int regwindow32_get(struct task_struct *target,
48 const struct pt_regs *regs,
51 unsigned long reg_window = regs->u_regs[UREG_I6];
52 int size = 16 * sizeof(u32);
54 if (target == current) {
55 if (copy_from_user(uregs, (void __user *)reg_window, size))
58 if (access_process_vm(target, reg_window, uregs, size,
65 static int regwindow32_set(struct task_struct *target,
66 const struct pt_regs *regs,
69 unsigned long reg_window = regs->u_regs[UREG_I6];
70 int size = 16 * sizeof(u32);
72 if (target == current) {
73 if (copy_to_user((void __user *)reg_window, uregs, size))
76 if (access_process_vm(target, reg_window, uregs, size,
77 FOLL_FORCE | FOLL_WRITE) != size)
83 static int genregs32_get(struct task_struct *target,
84 const struct user_regset *regset,
87 const struct pt_regs *regs = target->thread.kregs;
90 if (target == current)
93 membuf_write(&to, regs->u_regs, 16 * sizeof(u32));
96 if (regwindow32_get(target, regs, uregs))
98 membuf_write(&to, uregs, 16 * sizeof(u32));
99 membuf_store(&to, regs->psr);
100 membuf_store(&to, regs->pc);
101 membuf_store(&to, regs->npc);
102 membuf_store(&to, regs->y);
103 return membuf_zero(&to, 2 * sizeof(u32));
106 static int genregs32_set(struct task_struct *target,
107 const struct user_regset *regset,
108 unsigned int pos, unsigned int count,
109 const void *kbuf, const void __user *ubuf)
111 struct pt_regs *regs = target->thread.kregs;
116 if (target == current)
117 flush_user_windows();
119 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
121 0, 16 * sizeof(u32));
125 if (regwindow32_get(target, regs, uregs))
127 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
129 16 * sizeof(u32), 32 * sizeof(u32));
132 if (regwindow32_set(target, regs, uregs))
137 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
139 32 * sizeof(u32), 33 * sizeof(u32));
142 regs->psr = (regs->psr & ~(PSR_ICC | PSR_SYSCALL)) |
143 (psr & (PSR_ICC | PSR_SYSCALL));
146 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
148 33 * sizeof(u32), 34 * sizeof(u32));
151 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
153 34 * sizeof(u32), 35 * sizeof(u32));
156 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
158 35 * sizeof(u32), 36 * sizeof(u32));
161 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
162 36 * sizeof(u32), 38 * sizeof(u32));
165 static int fpregs32_get(struct task_struct *target,
166 const struct user_regset *regset,
170 if (target == current)
171 save_and_clear_fpu();
174 membuf_write(&to, target->thread.float_regs, 32 * sizeof(u32));
175 membuf_zero(&to, sizeof(u32));
176 membuf_write(&to, &target->thread.fsr, sizeof(u32));
177 membuf_store(&to, (u32)((1 << 8) | (8 << 16)));
178 return membuf_zero(&to, 64 * sizeof(u32));
181 static int fpregs32_set(struct task_struct *target,
182 const struct user_regset *regset,
183 unsigned int pos, unsigned int count,
184 const void *kbuf, const void __user *ubuf)
186 unsigned long *fpregs = target->thread.float_regs;
190 if (target == current)
191 save_and_clear_fpu();
193 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
195 0, 32 * sizeof(u32));
197 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
201 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
206 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
207 34 * sizeof(u32), -1);
211 static const struct user_regset sparc32_regsets[] = {
217 * PSR, PC, nPC, Y, WIM, TBR
220 .core_note_type = NT_PRSTATUS,
222 .size = sizeof(u32), .align = sizeof(u32),
223 .regset_get = genregs32_get, .set = genregs32_set
229 * FPU QUEUE COUNT (8-bit char)
230 * FPU QUEUE ENTRYSIZE (8-bit char)
231 * FPU ENABLED (8-bit char)
233 * FPU QUEUE (64 32-bit ints)
236 .core_note_type = NT_PRFPREG,
238 .size = sizeof(u32), .align = sizeof(u32),
239 .regset_get = fpregs32_get, .set = fpregs32_set
243 static int getregs_get(struct task_struct *target,
244 const struct user_regset *regset,
247 const struct pt_regs *regs = target->thread.kregs;
249 if (target == current)
250 flush_user_windows();
252 membuf_store(&to, regs->psr);
253 membuf_store(&to, regs->pc);
254 membuf_store(&to, regs->npc);
255 membuf_store(&to, regs->y);
256 return membuf_write(&to, regs->u_regs + 1, 15 * sizeof(u32));
259 static int setregs_set(struct task_struct *target,
260 const struct user_regset *regset,
261 unsigned int pos, unsigned int count,
262 const void *kbuf, const void __user *ubuf)
264 struct pt_regs *regs = target->thread.kregs;
268 if (target == current)
269 flush_user_windows();
271 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
276 regs->psr = (regs->psr & ~(PSR_ICC | PSR_SYSCALL)) |
277 (v[0] & (PSR_ICC | PSR_SYSCALL));
281 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
283 4 * sizeof(u32) , 19 * sizeof(u32));
286 static int getfpregs_get(struct task_struct *target,
287 const struct user_regset *regset,
291 if (target == current)
292 save_and_clear_fpu();
294 membuf_write(&to, &target->thread.float_regs, 32 * sizeof(u32));
295 membuf_write(&to, &target->thread.fsr, sizeof(u32));
296 return membuf_zero(&to, 35 * sizeof(u32));
299 static int setfpregs_set(struct task_struct *target,
300 const struct user_regset *regset,
301 unsigned int pos, unsigned int count,
302 const void *kbuf, const void __user *ubuf)
304 unsigned long *fpregs = target->thread.float_regs;
308 if (target == current)
309 save_and_clear_fpu();
311 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
313 0, 32 * sizeof(u32));
316 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
322 static const struct user_regset ptrace32_regsets[] = {
324 .n = 19, .size = sizeof(u32),
325 .regset_get = getregs_get, .set = setregs_set,
328 .n = 68, .size = sizeof(u32),
329 .regset_get = getfpregs_get, .set = setfpregs_set,
333 static const struct user_regset_view ptrace32_view = {
334 .regsets = ptrace32_regsets, .n = ARRAY_SIZE(ptrace32_regsets)
337 static const struct user_regset_view user_sparc32_view = {
338 .name = "sparc", .e_machine = EM_SPARC,
339 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
342 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
344 return &user_sparc32_view;
348 unsigned long regs[32];
354 unsigned long *insnaddr;
359 long arch_ptrace(struct task_struct *child, long request,
360 unsigned long addr, unsigned long data)
362 unsigned long addr2 = current->thread.kregs->u_regs[UREG_I4];
364 struct pt_regs __user *pregs;
365 struct fps __user *fps;
368 addr2p = (void __user *) addr2;
369 pregs = (struct pt_regs __user *) addr;
370 fps = (struct fps __user *) addr;
373 case PTRACE_GETREGS: {
374 ret = copy_regset_to_user(child, &ptrace32_view,
381 case PTRACE_SETREGS: {
382 ret = copy_regset_from_user(child, &ptrace32_view,
389 case PTRACE_GETFPREGS: {
390 ret = copy_regset_to_user(child, &ptrace32_view,
397 case PTRACE_SETFPREGS: {
398 ret = copy_regset_from_user(child, &ptrace32_view,
405 case PTRACE_READTEXT:
406 case PTRACE_READDATA:
407 ret = ptrace_readdata(child, addr, addr2p, data);
415 case PTRACE_WRITETEXT:
416 case PTRACE_WRITEDATA:
417 ret = ptrace_writedata(child, addr2p, addr, data);
426 if (request == PTRACE_SPARC_DETACH)
427 request = PTRACE_DETACH;
428 ret = ptrace_request(child, request, addr, data);
435 asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
439 if (test_thread_flag(TIF_SYSCALL_TRACE)) {
441 ptrace_report_syscall_exit(regs, 0);
443 ret = ptrace_report_syscall_entry(regs);