blob: e08ba4a46acd13cdfeb10cc48499eb7874e1f80e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ptrace.c: Sparc process tracing support.
2 *
David S. Miller8e3fe802008-02-06 21:00:44 -08003 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
6 * and David Mosberger.
7 *
Joe Perches5b2afff2007-12-20 13:55:45 -08008 * Added Linux support -miguel (weird, eh?, the original code was meant
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * to emulate SunOS).
10 */
11
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/mm.h>
15#include <linux/errno.h>
16#include <linux/ptrace.h>
17#include <linux/user.h>
18#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
David S. Miller8e3fe802008-02-06 21:00:44 -080021#include <linux/regset.h>
22#include <linux/elf.h>
David S. Miller1c133b42008-07-27 03:13:13 -070023#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/pgtable.h>
26#include <asm/system.h>
27#include <asm/uaccess.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* #define ALLOW_INIT_TRACING */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * Called by kernel/ptrace.c when detaching..
33 *
34 * Make sure single step bits etc are not set.
35 */
36void ptrace_disable(struct task_struct *child)
37{
38 /* nothing to do */
39}
40
David S. Miller8e3fe802008-02-06 21:00:44 -080041enum sparc_regset {
42 REGSET_GENERAL,
43 REGSET_FP,
44};
45
46static int genregs32_get(struct task_struct *target,
47 const struct user_regset *regset,
48 unsigned int pos, unsigned int count,
49 void *kbuf, void __user *ubuf)
50{
51 const struct pt_regs *regs = target->thread.kregs;
52 unsigned long __user *reg_window;
53 unsigned long *k = kbuf;
54 unsigned long __user *u = ubuf;
55 unsigned long reg;
56
57 if (target == current)
58 flush_user_windows();
59
60 pos /= sizeof(reg);
61 count /= sizeof(reg);
62
63 if (kbuf) {
64 for (; count > 0 && pos < 16; count--)
65 *k++ = regs->u_regs[pos++];
66
67 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
David S. Millerb857bd22010-03-31 18:05:05 -070068 reg_window -= 16;
David S. Miller8e3fe802008-02-06 21:00:44 -080069 for (; count > 0 && pos < 32; count--) {
70 if (get_user(*k++, &reg_window[pos++]))
71 return -EFAULT;
72 }
73 } else {
74 for (; count > 0 && pos < 16; count--) {
75 if (put_user(regs->u_regs[pos++], u++))
76 return -EFAULT;
77 }
78
79 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
David S. Millerb857bd22010-03-31 18:05:05 -070080 reg_window -= 16;
David S. Miller8e3fe802008-02-06 21:00:44 -080081 for (; count > 0 && pos < 32; count--) {
82 if (get_user(reg, &reg_window[pos++]) ||
83 put_user(reg, u++))
84 return -EFAULT;
85 }
86 }
87 while (count > 0) {
88 switch (pos) {
89 case 32: /* PSR */
90 reg = regs->psr;
91 break;
92 case 33: /* PC */
93 reg = regs->pc;
94 break;
95 case 34: /* NPC */
96 reg = regs->npc;
97 break;
98 case 35: /* Y */
99 reg = regs->y;
100 break;
101 case 36: /* WIM */
102 case 37: /* TBR */
103 reg = 0;
104 break;
105 default:
106 goto finish;
107 }
108
109 if (kbuf)
110 *k++ = reg;
111 else if (put_user(reg, u++))
112 return -EFAULT;
113 pos++;
114 count--;
115 }
116finish:
117 pos *= sizeof(reg);
118 count *= sizeof(reg);
119
120 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
121 38 * sizeof(reg), -1);
122}
123
124static 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)
128{
129 struct pt_regs *regs = target->thread.kregs;
130 unsigned long __user *reg_window;
131 const unsigned long *k = kbuf;
132 const unsigned long __user *u = ubuf;
133 unsigned long reg;
134
135 if (target == current)
136 flush_user_windows();
137
138 pos /= sizeof(reg);
139 count /= sizeof(reg);
140
141 if (kbuf) {
142 for (; count > 0 && pos < 16; count--)
143 regs->u_regs[pos++] = *k++;
144
145 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
David S. Millerb857bd22010-03-31 18:05:05 -0700146 reg_window -= 16;
David S. Miller8e3fe802008-02-06 21:00:44 -0800147 for (; count > 0 && pos < 32; count--) {
148 if (put_user(*k++, &reg_window[pos++]))
149 return -EFAULT;
150 }
151 } else {
152 for (; count > 0 && pos < 16; count--) {
153 if (get_user(reg, u++))
154 return -EFAULT;
155 regs->u_regs[pos++] = reg;
156 }
157
158 reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
David S. Millerb857bd22010-03-31 18:05:05 -0700159 reg_window -= 16;
David S. Miller8e3fe802008-02-06 21:00:44 -0800160 for (; count > 0 && pos < 32; count--) {
161 if (get_user(reg, u++) ||
162 put_user(reg, &reg_window[pos++]))
163 return -EFAULT;
164 }
165 }
166 while (count > 0) {
167 unsigned long psr;
168
169 if (kbuf)
170 reg = *k++;
171 else if (get_user(reg, u++))
172 return -EFAULT;
173
174 switch (pos) {
175 case 32: /* PSR */
176 psr = regs->psr;
David S. Miller28e61032008-05-11 02:07:19 -0700177 psr &= ~(PSR_ICC | PSR_SYSCALL);
178 psr |= (reg & (PSR_ICC | PSR_SYSCALL));
David S. Miller8e3fe802008-02-06 21:00:44 -0800179 regs->psr = psr;
180 break;
181 case 33: /* PC */
182 regs->pc = reg;
183 break;
184 case 34: /* NPC */
185 regs->npc = reg;
186 break;
187 case 35: /* Y */
188 regs->y = reg;
189 break;
190 case 36: /* WIM */
191 case 37: /* TBR */
192 break;
193 default:
194 goto finish;
195 }
196
197 pos++;
198 count--;
199 }
200finish:
201 pos *= sizeof(reg);
202 count *= sizeof(reg);
203
204 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
205 38 * sizeof(reg), -1);
206}
207
208static int fpregs32_get(struct task_struct *target,
209 const struct user_regset *regset,
210 unsigned int pos, unsigned int count,
211 void *kbuf, void __user *ubuf)
212{
213 const unsigned long *fpregs = target->thread.float_regs;
214 int ret = 0;
215
216#if 0
217 if (target == current)
218 save_and_clear_fpu();
219#endif
220
221 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
222 fpregs,
223 0, 32 * sizeof(u32));
224
225 if (!ret)
226 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
227 32 * sizeof(u32),
228 33 * sizeof(u32));
229 if (!ret)
230 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
231 &target->thread.fsr,
232 33 * sizeof(u32),
233 34 * sizeof(u32));
234
235 if (!ret) {
236 unsigned long val;
237
238 val = (1 << 8) | (8 << 16);
239 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
240 &val,
241 34 * sizeof(u32),
242 35 * sizeof(u32));
243 }
244
245 if (!ret)
246 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
247 35 * sizeof(u32), -1);
248
249 return ret;
250}
251
252static int fpregs32_set(struct task_struct *target,
253 const struct user_regset *regset,
254 unsigned int pos, unsigned int count,
255 const void *kbuf, const void __user *ubuf)
256{
257 unsigned long *fpregs = target->thread.float_regs;
258 int ret;
259
260#if 0
261 if (target == current)
262 save_and_clear_fpu();
263#endif
264 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
265 fpregs,
266 0, 32 * sizeof(u32));
267 if (!ret)
268 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
269 32 * sizeof(u32),
270 33 * sizeof(u32));
271 if (!ret && count > 0) {
272 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
273 &target->thread.fsr,
274 33 * sizeof(u32),
275 34 * sizeof(u32));
276 }
277
278 if (!ret)
279 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
280 34 * sizeof(u32), -1);
281 return ret;
282}
283
284static const struct user_regset sparc32_regsets[] = {
285 /* Format is:
286 * G0 --> G7
287 * O0 --> O7
288 * L0 --> L7
289 * I0 --> I7
290 * PSR, PC, nPC, Y, WIM, TBR
291 */
292 [REGSET_GENERAL] = {
293 .core_note_type = NT_PRSTATUS,
David S. Miller7d4ee282008-09-12 15:01:31 -0700294 .n = 38,
David S. Miller8e3fe802008-02-06 21:00:44 -0800295 .size = sizeof(u32), .align = sizeof(u32),
296 .get = genregs32_get, .set = genregs32_set
297 },
298 /* Format is:
299 * F0 --> F31
300 * empty 32-bit word
301 * FSR (32--bit word)
302 * FPU QUEUE COUNT (8-bit char)
303 * FPU QUEUE ENTRYSIZE (8-bit char)
304 * FPU ENABLED (8-bit char)
305 * empty 8-bit char
306 * FPU QUEUE (64 32-bit ints)
307 */
308 [REGSET_FP] = {
309 .core_note_type = NT_PRFPREG,
David S. Miller7d4ee282008-09-12 15:01:31 -0700310 .n = 99,
David S. Miller8e3fe802008-02-06 21:00:44 -0800311 .size = sizeof(u32), .align = sizeof(u32),
312 .get = fpregs32_get, .set = fpregs32_set
313 },
314};
315
316static const struct user_regset_view user_sparc32_view = {
317 .name = "sparc", .e_machine = EM_SPARC,
318 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
319};
320
321const struct user_regset_view *task_user_regset_view(struct task_struct *task)
322{
323 return &user_sparc32_view;
324}
325
Namhyung Kim9b05a692010-10-27 15:33:47 -0700326long arch_ptrace(struct task_struct *child, long request,
327 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
David S. Miller97753692008-02-07 03:00:17 -0800329 unsigned long addr2 = current->thread.kregs->u_regs[UREG_I4];
David S. Millerd256eb82008-02-07 05:06:51 -0800330 const struct user_regset_view *view;
331 int ret;
332
David S. Millerd786a4a2008-04-09 19:39:25 -0700333 view = task_user_regset_view(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 switch(request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 case PTRACE_GETREGS: {
337 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
David S. Millerd256eb82008-02-07 05:06:51 -0800339 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
340 32 * sizeof(u32),
341 4 * sizeof(u32),
342 &pregs->psr);
343 if (!ret)
344 copy_regset_to_user(child, view, REGSET_GENERAL,
345 1 * sizeof(u32),
346 15 * sizeof(u32),
347 &pregs->u_regs[0]);
David S. Miller97753692008-02-07 03:00:17 -0800348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 case PTRACE_SETREGS: {
352 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David S. Millerd256eb82008-02-07 05:06:51 -0800354 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
355 32 * sizeof(u32),
356 4 * sizeof(u32),
357 &pregs->psr);
358 if (!ret)
359 copy_regset_from_user(child, view, REGSET_GENERAL,
360 1 * sizeof(u32),
361 15 * sizeof(u32),
362 &pregs->u_regs[0]);
David S. Miller97753692008-02-07 03:00:17 -0800363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 case PTRACE_GETFPREGS: {
367 struct fps {
368 unsigned long regs[32];
369 unsigned long fsr;
370 unsigned long flags;
371 unsigned long extra;
372 unsigned long fpqd;
373 struct fq {
374 unsigned long *insnaddr;
375 unsigned long insn;
376 } fpq[16];
377 };
378 struct fps __user *fps = (struct fps __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David S. Millerd256eb82008-02-07 05:06:51 -0800380 ret = copy_regset_to_user(child, view, REGSET_FP,
381 0 * sizeof(u32),
382 32 * sizeof(u32),
383 &fps->regs[0]);
384 if (!ret)
385 ret = copy_regset_to_user(child, view, REGSET_FP,
386 33 * sizeof(u32),
387 1 * sizeof(u32),
388 &fps->fsr);
David S. Miller97753692008-02-07 03:00:17 -0800389
David S. Millerd256eb82008-02-07 05:06:51 -0800390 if (!ret) {
391 if (__put_user(0, &fps->fpqd) ||
392 __put_user(0, &fps->flags) ||
393 __put_user(0, &fps->extra) ||
394 clear_user(fps->fpq, sizeof(fps->fpq)))
395 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
David S. Miller97753692008-02-07 03:00:17 -0800397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 case PTRACE_SETFPREGS: {
401 struct fps {
402 unsigned long regs[32];
403 unsigned long fsr;
404 unsigned long flags;
405 unsigned long extra;
406 unsigned long fpqd;
407 struct fq {
408 unsigned long *insnaddr;
409 unsigned long insn;
410 } fpq[16];
411 };
412 struct fps __user *fps = (struct fps __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
David S. Millerd256eb82008-02-07 05:06:51 -0800414 ret = copy_regset_from_user(child, view, REGSET_FP,
415 0 * sizeof(u32),
416 32 * sizeof(u32),
417 &fps->regs[0]);
418 if (!ret)
419 ret = copy_regset_from_user(child, view, REGSET_FP,
420 33 * sizeof(u32),
421 1 * sizeof(u32),
422 &fps->fsr);
David S. Miller97753692008-02-07 03:00:17 -0800423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 case PTRACE_READTEXT:
David S. Miller97753692008-02-07 03:00:17 -0800427 case PTRACE_READDATA:
428 ret = ptrace_readdata(child, addr,
429 (void __user *) addr2, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David S. Miller97753692008-02-07 03:00:17 -0800431 if (ret == data)
432 ret = 0;
433 else if (ret >= 0)
434 ret = -EIO;
435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 case PTRACE_WRITETEXT:
David S. Miller97753692008-02-07 03:00:17 -0800438 case PTRACE_WRITEDATA:
439 ret = ptrace_writedata(child, (void __user *) addr2,
440 addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
David S. Miller97753692008-02-07 03:00:17 -0800442 if (ret == data)
443 ret = 0;
444 else if (ret >= 0)
445 ret = -EIO;
446 break;
447
448 default:
David S. Miller986bef82008-05-10 21:11:23 -0700449 if (request == PTRACE_SPARC_DETACH)
450 request = PTRACE_DETACH;
David S. Miller97753692008-02-07 03:00:17 -0800451 ret = ptrace_request(child, request, addr, data);
452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
David S. Miller97753692008-02-07 03:00:17 -0800455 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
David S. Miller1c133b42008-07-27 03:13:13 -0700458asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
David S. Miller1c133b42008-07-27 03:13:13 -0700460 int ret = 0;
461
462 if (test_thread_flag(TIF_SYSCALL_TRACE)) {
463 if (syscall_exit_p)
464 tracehook_report_syscall_exit(regs, 0);
465 else
466 ret = tracehook_report_syscall_entry(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
David S. Miller1c133b42008-07-27 03:13:13 -0700468
469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}