blob: 10ade3c0c88af79a30135574e42eed28a8f1f3f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 *
5 * Derived from "arch/m68k/kernel/ptrace.c"
6 * Copyright (C) 1994 by Hamish Macdonald
7 * Taken from linux/kernel/ptrace.c and modified for M680x0.
8 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
9 *
10 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
Paul Mackerrasb1239232005-10-20 09:11:29 +100011 * and Paul Mackerras (paulus@samba.org).
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * This file is subject to the terms and conditions of the GNU General
14 * Public License. See the file README.legal in the main directory of
15 * this archive for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/mm.h>
21#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/ptrace.h>
Roland McGrathf65255e2007-12-20 03:57:34 -080024#include <linux/regset.h>
Roland McGrath4f72c422008-07-27 16:51:03 +100025#include <linux/tracehook.h>
Roland McGrath3caf06c2007-12-20 03:57:39 -080026#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/user.h>
28#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
David Woodhouseea9c1022005-05-08 15:56:09 +010030#include <linux/seccomp.h>
31#include <linux/audit.h>
Ian Munsie02424d82011-02-02 17:27:24 +000032#include <trace/syscall.h>
K.Prasad5aae8a52010-06-15 11:35:19 +053033#include <linux/hw_breakpoint.h>
34#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/uaccess.h>
37#include <asm/page.h>
38#include <asm/pgtable.h>
David Howellsae3a1972012-03-28 18:30:02 +010039#include <asm/switch_to.h>
Paul Mackerras21a62902005-11-19 20:47:22 +110040
Ian Munsie02424d82011-02-02 17:27:24 +000041#define CREATE_TRACE_POINTS
42#include <trace/events/syscalls.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
Mahesh Salgaonkar359e4282010-04-07 18:10:20 +100045 * The parameter save area on the stack is used to store arguments being passed
46 * to callee function and is located at fixed offset from stack pointer.
47 */
48#ifdef CONFIG_PPC32
49#define PARAMETER_SAVE_AREA_OFFSET 24 /* bytes */
50#else /* CONFIG_PPC32 */
51#define PARAMETER_SAVE_AREA_OFFSET 48 /* bytes */
52#endif
53
54struct pt_regs_offset {
55 const char *name;
56 int offset;
57};
58
59#define STR(s) #s /* convert to string */
60#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61#define GPR_OFFSET_NAME(num) \
62 {.name = STR(gpr##num), .offset = offsetof(struct pt_regs, gpr[num])}
63#define REG_OFFSET_END {.name = NULL, .offset = 0}
64
65static const struct pt_regs_offset regoffset_table[] = {
66 GPR_OFFSET_NAME(0),
67 GPR_OFFSET_NAME(1),
68 GPR_OFFSET_NAME(2),
69 GPR_OFFSET_NAME(3),
70 GPR_OFFSET_NAME(4),
71 GPR_OFFSET_NAME(5),
72 GPR_OFFSET_NAME(6),
73 GPR_OFFSET_NAME(7),
74 GPR_OFFSET_NAME(8),
75 GPR_OFFSET_NAME(9),
76 GPR_OFFSET_NAME(10),
77 GPR_OFFSET_NAME(11),
78 GPR_OFFSET_NAME(12),
79 GPR_OFFSET_NAME(13),
80 GPR_OFFSET_NAME(14),
81 GPR_OFFSET_NAME(15),
82 GPR_OFFSET_NAME(16),
83 GPR_OFFSET_NAME(17),
84 GPR_OFFSET_NAME(18),
85 GPR_OFFSET_NAME(19),
86 GPR_OFFSET_NAME(20),
87 GPR_OFFSET_NAME(21),
88 GPR_OFFSET_NAME(22),
89 GPR_OFFSET_NAME(23),
90 GPR_OFFSET_NAME(24),
91 GPR_OFFSET_NAME(25),
92 GPR_OFFSET_NAME(26),
93 GPR_OFFSET_NAME(27),
94 GPR_OFFSET_NAME(28),
95 GPR_OFFSET_NAME(29),
96 GPR_OFFSET_NAME(30),
97 GPR_OFFSET_NAME(31),
98 REG_OFFSET_NAME(nip),
99 REG_OFFSET_NAME(msr),
100 REG_OFFSET_NAME(ctr),
101 REG_OFFSET_NAME(link),
102 REG_OFFSET_NAME(xer),
103 REG_OFFSET_NAME(ccr),
104#ifdef CONFIG_PPC64
105 REG_OFFSET_NAME(softe),
106#else
107 REG_OFFSET_NAME(mq),
108#endif
109 REG_OFFSET_NAME(trap),
110 REG_OFFSET_NAME(dar),
111 REG_OFFSET_NAME(dsisr),
112 REG_OFFSET_END,
113};
114
115/**
116 * regs_query_register_offset() - query register offset from its name
117 * @name: the name of a register
118 *
119 * regs_query_register_offset() returns the offset of a register in struct
120 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
121 */
122int regs_query_register_offset(const char *name)
123{
124 const struct pt_regs_offset *roff;
125 for (roff = regoffset_table; roff->name != NULL; roff++)
126 if (!strcmp(roff->name, name))
127 return roff->offset;
128 return -EINVAL;
129}
130
131/**
132 * regs_query_register_name() - query register name from its offset
133 * @offset: the offset of a register in struct pt_regs.
134 *
135 * regs_query_register_name() returns the name of a register from its
136 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
137 */
138const char *regs_query_register_name(unsigned int offset)
139{
140 const struct pt_regs_offset *roff;
141 for (roff = regoffset_table; roff->name != NULL; roff++)
142 if (roff->offset == offset)
143 return roff->name;
144 return NULL;
145}
146
147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * does not yet catch signals sent when the child dies.
149 * in exit.c or in signal.c.
150 */
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000153 * Set of msr bits that gdb can change on behalf of a process.
154 */
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000155#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000156#define MSR_DEBUGCHANGE 0
157#else
158#define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
159#endif
160
161/*
162 * Max register writeable via put_reg
163 */
164#ifdef CONFIG_PPC32
165#define PT_MAX_PUT_REG PT_MQ
166#else
167#define PT_MAX_PUT_REG PT_CCR
168#endif
169
Roland McGrath26f77132007-12-20 03:57:51 -0800170static unsigned long get_user_msr(struct task_struct *task)
171{
172 return task->thread.regs->msr | task->thread.fpexc_mode;
173}
174
175static int set_user_msr(struct task_struct *task, unsigned long msr)
176{
177 task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
178 task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
179 return 0;
180}
181
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000182#ifdef CONFIG_PPC64
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000183static int get_user_dscr(struct task_struct *task, unsigned long *data)
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000184{
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000185 *data = task->thread.dscr;
186 return 0;
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000187}
188
189static int set_user_dscr(struct task_struct *task, unsigned long dscr)
190{
191 task->thread.dscr = dscr;
192 task->thread.dscr_inherit = 1;
193 return 0;
194}
195#else
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000196static int get_user_dscr(struct task_struct *task, unsigned long *data)
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000197{
198 return -EIO;
199}
200
201static int set_user_dscr(struct task_struct *task, unsigned long dscr)
202{
203 return -EIO;
204}
205#endif
206
Roland McGrath26f77132007-12-20 03:57:51 -0800207/*
208 * We prevent mucking around with the reserved area of trap
209 * which are used internally by the kernel.
210 */
211static int set_user_trap(struct task_struct *task, unsigned long trap)
212{
213 task->thread.regs->trap = trap & 0xfff0;
214 return 0;
215}
216
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000217/*
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000218 * Get contents of register REGNO in task TASK.
219 */
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000220int ptrace_get_reg(struct task_struct *task, int regno, unsigned long *data)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000221{
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000222 if ((task->thread.regs == NULL) || !data)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000223 return -EIO;
224
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000225 if (regno == PT_MSR) {
226 *data = get_user_msr(task);
227 return 0;
228 }
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000229
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000230 if (regno == PT_DSCR)
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000231 return get_user_dscr(task, data);
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000232
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000233 if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long))) {
234 *data = ((unsigned long *)task->thread.regs)[regno];
235 return 0;
236 }
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000237
238 return -EIO;
239}
240
241/*
242 * Write contents of register REGNO in task TASK.
243 */
244int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
245{
246 if (task->thread.regs == NULL)
247 return -EIO;
248
Roland McGrath26f77132007-12-20 03:57:51 -0800249 if (regno == PT_MSR)
250 return set_user_msr(task, data);
251 if (regno == PT_TRAP)
252 return set_user_trap(task, data);
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000253 if (regno == PT_DSCR)
254 return set_user_dscr(task, data);
Roland McGrath26f77132007-12-20 03:57:51 -0800255
256 if (regno <= PT_MAX_PUT_REG) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000257 ((unsigned long *)task->thread.regs)[regno] = data;
258 return 0;
259 }
260 return -EIO;
261}
262
Roland McGrath44dd3f52007-12-20 03:57:55 -0800263static int gpr_get(struct task_struct *target, const struct user_regset *regset,
264 unsigned int pos, unsigned int count,
265 void *kbuf, void __user *ubuf)
266{
Mike Wolfa71f5d52011-03-21 11:14:53 +1100267 int i, ret;
Roland McGrath44dd3f52007-12-20 03:57:55 -0800268
269 if (target->thread.regs == NULL)
270 return -EIO;
271
Mike Wolfa71f5d52011-03-21 11:14:53 +1100272 if (!FULL_REGS(target->thread.regs)) {
273 /* We have a partial register set. Fill 14-31 with bogus values */
274 for (i = 14; i < 32; i++)
275 target->thread.regs->gpr[i] = NV_REG_POISON;
276 }
Roland McGrath44dd3f52007-12-20 03:57:55 -0800277
278 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
279 target->thread.regs,
280 0, offsetof(struct pt_regs, msr));
281 if (!ret) {
282 unsigned long msr = get_user_msr(target);
283 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
284 offsetof(struct pt_regs, msr),
285 offsetof(struct pt_regs, msr) +
286 sizeof(msr));
287 }
288
289 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
290 offsetof(struct pt_regs, msr) + sizeof(long));
291
292 if (!ret)
293 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
294 &target->thread.regs->orig_gpr3,
295 offsetof(struct pt_regs, orig_gpr3),
296 sizeof(struct pt_regs));
297 if (!ret)
298 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
299 sizeof(struct pt_regs), -1);
300
301 return ret;
302}
303
304static int gpr_set(struct task_struct *target, const struct user_regset *regset,
305 unsigned int pos, unsigned int count,
306 const void *kbuf, const void __user *ubuf)
307{
308 unsigned long reg;
309 int ret;
310
311 if (target->thread.regs == NULL)
312 return -EIO;
313
314 CHECK_FULL_REGS(target->thread.regs);
315
316 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
317 target->thread.regs,
318 0, PT_MSR * sizeof(reg));
319
320 if (!ret && count > 0) {
321 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
322 PT_MSR * sizeof(reg),
323 (PT_MSR + 1) * sizeof(reg));
324 if (!ret)
325 ret = set_user_msr(target, reg);
326 }
327
328 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
329 offsetof(struct pt_regs, msr) + sizeof(long));
330
331 if (!ret)
332 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
333 &target->thread.regs->orig_gpr3,
334 PT_ORIG_R3 * sizeof(reg),
335 (PT_MAX_PUT_REG + 1) * sizeof(reg));
336
337 if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
338 ret = user_regset_copyin_ignore(
339 &pos, &count, &kbuf, &ubuf,
340 (PT_MAX_PUT_REG + 1) * sizeof(reg),
341 PT_TRAP * sizeof(reg));
342
343 if (!ret && count > 0) {
344 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
345 PT_TRAP * sizeof(reg),
346 (PT_TRAP + 1) * sizeof(reg));
347 if (!ret)
348 ret = set_user_trap(target, reg);
349 }
350
351 if (!ret)
352 ret = user_regset_copyin_ignore(
353 &pos, &count, &kbuf, &ubuf,
354 (PT_TRAP + 1) * sizeof(reg), -1);
355
356 return ret;
357}
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000358
Roland McGrathf65255e2007-12-20 03:57:34 -0800359static int fpr_get(struct task_struct *target, const struct user_regset *regset,
360 unsigned int pos, unsigned int count,
361 void *kbuf, void __user *ubuf)
362{
Michael Neulingc6e67712008-06-25 14:07:18 +1000363#ifdef CONFIG_VSX
364 double buf[33];
365 int i;
366#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800367 flush_fp_to_thread(target);
368
Michael Neulingc6e67712008-06-25 14:07:18 +1000369#ifdef CONFIG_VSX
370 /* copy to local buffer then write that out */
371 for (i = 0; i < 32 ; i++)
372 buf[i] = target->thread.TS_FPR(i);
373 memcpy(&buf[32], &target->thread.fpscr, sizeof(double));
374 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
375
376#else
Roland McGrathf65255e2007-12-20 03:57:34 -0800377 BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
Michael Neuling9c75a312008-06-26 17:07:48 +1000378 offsetof(struct thread_struct, TS_FPR(32)));
Roland McGrathf65255e2007-12-20 03:57:34 -0800379
380 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
381 &target->thread.fpr, 0, -1);
Michael Neulingc6e67712008-06-25 14:07:18 +1000382#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800383}
384
385static int fpr_set(struct task_struct *target, const struct user_regset *regset,
386 unsigned int pos, unsigned int count,
387 const void *kbuf, const void __user *ubuf)
388{
Michael Neulingc6e67712008-06-25 14:07:18 +1000389#ifdef CONFIG_VSX
390 double buf[33];
391 int i;
392#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800393 flush_fp_to_thread(target);
394
Michael Neulingc6e67712008-06-25 14:07:18 +1000395#ifdef CONFIG_VSX
396 /* copy to local buffer then write that out */
397 i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
398 if (i)
399 return i;
400 for (i = 0; i < 32 ; i++)
401 target->thread.TS_FPR(i) = buf[i];
402 memcpy(&target->thread.fpscr, &buf[32], sizeof(double));
403 return 0;
404#else
Roland McGrathf65255e2007-12-20 03:57:34 -0800405 BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
Michael Neuling9c75a312008-06-26 17:07:48 +1000406 offsetof(struct thread_struct, TS_FPR(32)));
Roland McGrathf65255e2007-12-20 03:57:34 -0800407
408 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
409 &target->thread.fpr, 0, -1);
Michael Neulingc6e67712008-06-25 14:07:18 +1000410#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800411}
412
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000413#ifdef CONFIG_ALTIVEC
414/*
415 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
416 * The transfer totals 34 quadword. Quadwords 0-31 contain the
417 * corresponding vector registers. Quadword 32 contains the vscr as the
418 * last word (offset 12) within that quadword. Quadword 33 contains the
419 * vrsave as the first word (offset 0) within the quadword.
420 *
421 * This definition of the VMX state is compatible with the current PPC32
422 * ptrace interface. This allows signal handling and ptrace to use the
423 * same structures. This also simplifies the implementation of a bi-arch
424 * (combined (32- and 64-bit) gdb.
425 */
426
Roland McGrath3caf06c2007-12-20 03:57:39 -0800427static int vr_active(struct task_struct *target,
428 const struct user_regset *regset)
429{
430 flush_altivec_to_thread(target);
431 return target->thread.used_vr ? regset->n : 0;
432}
433
434static int vr_get(struct task_struct *target, const struct user_regset *regset,
435 unsigned int pos, unsigned int count,
436 void *kbuf, void __user *ubuf)
437{
438 int ret;
439
440 flush_altivec_to_thread(target);
441
442 BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
443 offsetof(struct thread_struct, vr[32]));
444
445 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
446 &target->thread.vr, 0,
447 33 * sizeof(vector128));
448 if (!ret) {
449 /*
450 * Copy out only the low-order word of vrsave.
451 */
452 union {
453 elf_vrreg_t reg;
454 u32 word;
455 } vrsave;
456 memset(&vrsave, 0, sizeof(vrsave));
457 vrsave.word = target->thread.vrsave;
458 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
459 33 * sizeof(vector128), -1);
460 }
461
462 return ret;
463}
464
465static int vr_set(struct task_struct *target, const struct user_regset *regset,
466 unsigned int pos, unsigned int count,
467 const void *kbuf, const void __user *ubuf)
468{
469 int ret;
470
471 flush_altivec_to_thread(target);
472
473 BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
474 offsetof(struct thread_struct, vr[32]));
475
476 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
477 &target->thread.vr, 0, 33 * sizeof(vector128));
478 if (!ret && count > 0) {
479 /*
480 * We use only the first word of vrsave.
481 */
482 union {
483 elf_vrreg_t reg;
484 u32 word;
485 } vrsave;
486 memset(&vrsave, 0, sizeof(vrsave));
487 vrsave.word = target->thread.vrsave;
488 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
489 33 * sizeof(vector128), -1);
490 if (!ret)
491 target->thread.vrsave = vrsave.word;
492 }
493
494 return ret;
495}
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000496#endif /* CONFIG_ALTIVEC */
497
Michael Neulingce48b212008-06-25 14:07:18 +1000498#ifdef CONFIG_VSX
499/*
500 * Currently to set and and get all the vsx state, you need to call
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300501 * the fp and VMX calls as well. This only get/sets the lower 32
Michael Neulingce48b212008-06-25 14:07:18 +1000502 * 128bit VSX registers.
503 */
504
505static int vsr_active(struct task_struct *target,
506 const struct user_regset *regset)
507{
508 flush_vsx_to_thread(target);
509 return target->thread.used_vsr ? regset->n : 0;
510}
511
512static int vsr_get(struct task_struct *target, const struct user_regset *regset,
513 unsigned int pos, unsigned int count,
514 void *kbuf, void __user *ubuf)
515{
Michael Neulingf3e909c2008-07-01 14:01:39 +1000516 double buf[32];
517 int ret, i;
Michael Neulingce48b212008-06-25 14:07:18 +1000518
519 flush_vsx_to_thread(target);
520
Michael Neulingf3e909c2008-07-01 14:01:39 +1000521 for (i = 0; i < 32 ; i++)
Michael Neuling7d2a1752008-07-29 01:13:14 +1000522 buf[i] = target->thread.fpr[i][TS_VSRLOWOFFSET];
Michael Neulingce48b212008-06-25 14:07:18 +1000523 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Michael Neulingf3e909c2008-07-01 14:01:39 +1000524 buf, 0, 32 * sizeof(double));
Michael Neulingce48b212008-06-25 14:07:18 +1000525
526 return ret;
527}
528
529static int vsr_set(struct task_struct *target, const struct user_regset *regset,
530 unsigned int pos, unsigned int count,
531 const void *kbuf, const void __user *ubuf)
532{
Michael Neulingf3e909c2008-07-01 14:01:39 +1000533 double buf[32];
534 int ret,i;
Michael Neulingce48b212008-06-25 14:07:18 +1000535
536 flush_vsx_to_thread(target);
537
538 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Michael Neulingf3e909c2008-07-01 14:01:39 +1000539 buf, 0, 32 * sizeof(double));
540 for (i = 0; i < 32 ; i++)
Michael Neuling7d2a1752008-07-29 01:13:14 +1000541 target->thread.fpr[i][TS_VSRLOWOFFSET] = buf[i];
Michael Neulingf3e909c2008-07-01 14:01:39 +1000542
Michael Neulingce48b212008-06-25 14:07:18 +1000543
544 return ret;
545}
546#endif /* CONFIG_VSX */
547
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000548#ifdef CONFIG_SPE
549
550/*
551 * For get_evrregs/set_evrregs functions 'data' has the following layout:
552 *
553 * struct {
554 * u32 evr[32];
555 * u64 acc;
556 * u32 spefscr;
557 * }
558 */
559
Roland McGratha4e4b172007-12-20 03:57:48 -0800560static int evr_active(struct task_struct *target,
561 const struct user_regset *regset)
562{
563 flush_spe_to_thread(target);
564 return target->thread.used_spe ? regset->n : 0;
565}
566
567static int evr_get(struct task_struct *target, const struct user_regset *regset,
568 unsigned int pos, unsigned int count,
569 void *kbuf, void __user *ubuf)
570{
571 int ret;
572
573 flush_spe_to_thread(target);
574
575 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
576 &target->thread.evr,
577 0, sizeof(target->thread.evr));
578
579 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
580 offsetof(struct thread_struct, spefscr));
581
582 if (!ret)
583 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
584 &target->thread.acc,
585 sizeof(target->thread.evr), -1);
586
587 return ret;
588}
589
590static int evr_set(struct task_struct *target, const struct user_regset *regset,
591 unsigned int pos, unsigned int count,
592 const void *kbuf, const void __user *ubuf)
593{
594 int ret;
595
596 flush_spe_to_thread(target);
597
598 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
599 &target->thread.evr,
600 0, sizeof(target->thread.evr));
601
602 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
603 offsetof(struct thread_struct, spefscr));
604
605 if (!ret)
606 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
607 &target->thread.acc,
608 sizeof(target->thread.evr), -1);
609
610 return ret;
611}
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000612#endif /* CONFIG_SPE */
613
614
Roland McGrath80fdf472007-12-20 03:58:00 -0800615/*
616 * These are our native regset flavors.
617 */
618enum powerpc_regset {
619 REGSET_GPR,
620 REGSET_FPR,
621#ifdef CONFIG_ALTIVEC
622 REGSET_VMX,
623#endif
Michael Neulingce48b212008-06-25 14:07:18 +1000624#ifdef CONFIG_VSX
625 REGSET_VSX,
626#endif
Roland McGrath80fdf472007-12-20 03:58:00 -0800627#ifdef CONFIG_SPE
628 REGSET_SPE,
629#endif
630};
631
632static const struct user_regset native_regsets[] = {
633 [REGSET_GPR] = {
634 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
635 .size = sizeof(long), .align = sizeof(long),
636 .get = gpr_get, .set = gpr_set
637 },
638 [REGSET_FPR] = {
639 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
640 .size = sizeof(double), .align = sizeof(double),
641 .get = fpr_get, .set = fpr_set
642 },
643#ifdef CONFIG_ALTIVEC
644 [REGSET_VMX] = {
645 .core_note_type = NT_PPC_VMX, .n = 34,
646 .size = sizeof(vector128), .align = sizeof(vector128),
647 .active = vr_active, .get = vr_get, .set = vr_set
648 },
649#endif
Michael Neulingce48b212008-06-25 14:07:18 +1000650#ifdef CONFIG_VSX
651 [REGSET_VSX] = {
Michael Neulingf3e909c2008-07-01 14:01:39 +1000652 .core_note_type = NT_PPC_VSX, .n = 32,
653 .size = sizeof(double), .align = sizeof(double),
Michael Neulingce48b212008-06-25 14:07:18 +1000654 .active = vsr_active, .get = vsr_get, .set = vsr_set
655 },
656#endif
Roland McGrath80fdf472007-12-20 03:58:00 -0800657#ifdef CONFIG_SPE
658 [REGSET_SPE] = {
659 .n = 35,
660 .size = sizeof(u32), .align = sizeof(u32),
661 .active = evr_active, .get = evr_get, .set = evr_set
662 },
663#endif
664};
665
666static const struct user_regset_view user_ppc_native_view = {
667 .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
668 .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
669};
670
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800671#ifdef CONFIG_PPC64
672#include <linux/compat.h>
673
674static int gpr32_get(struct task_struct *target,
675 const struct user_regset *regset,
676 unsigned int pos, unsigned int count,
677 void *kbuf, void __user *ubuf)
678{
679 const unsigned long *regs = &target->thread.regs->gpr[0];
680 compat_ulong_t *k = kbuf;
681 compat_ulong_t __user *u = ubuf;
682 compat_ulong_t reg;
Mike Wolfa71f5d52011-03-21 11:14:53 +1100683 int i;
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800684
685 if (target->thread.regs == NULL)
686 return -EIO;
687
Mike Wolfa71f5d52011-03-21 11:14:53 +1100688 if (!FULL_REGS(target->thread.regs)) {
689 /* We have a partial register set. Fill 14-31 with bogus values */
690 for (i = 14; i < 32; i++)
691 target->thread.regs->gpr[i] = NV_REG_POISON;
692 }
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800693
694 pos /= sizeof(reg);
695 count /= sizeof(reg);
696
697 if (kbuf)
698 for (; count > 0 && pos < PT_MSR; --count)
699 *k++ = regs[pos++];
700 else
701 for (; count > 0 && pos < PT_MSR; --count)
702 if (__put_user((compat_ulong_t) regs[pos++], u++))
703 return -EFAULT;
704
705 if (count > 0 && pos == PT_MSR) {
706 reg = get_user_msr(target);
707 if (kbuf)
708 *k++ = reg;
709 else if (__put_user(reg, u++))
710 return -EFAULT;
711 ++pos;
712 --count;
713 }
714
715 if (kbuf)
716 for (; count > 0 && pos < PT_REGS_COUNT; --count)
717 *k++ = regs[pos++];
718 else
719 for (; count > 0 && pos < PT_REGS_COUNT; --count)
720 if (__put_user((compat_ulong_t) regs[pos++], u++))
721 return -EFAULT;
722
723 kbuf = k;
724 ubuf = u;
725 pos *= sizeof(reg);
726 count *= sizeof(reg);
727 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
728 PT_REGS_COUNT * sizeof(reg), -1);
729}
730
731static int gpr32_set(struct task_struct *target,
732 const struct user_regset *regset,
733 unsigned int pos, unsigned int count,
734 const void *kbuf, const void __user *ubuf)
735{
736 unsigned long *regs = &target->thread.regs->gpr[0];
737 const compat_ulong_t *k = kbuf;
738 const compat_ulong_t __user *u = ubuf;
739 compat_ulong_t reg;
740
741 if (target->thread.regs == NULL)
742 return -EIO;
743
744 CHECK_FULL_REGS(target->thread.regs);
745
746 pos /= sizeof(reg);
747 count /= sizeof(reg);
748
749 if (kbuf)
750 for (; count > 0 && pos < PT_MSR; --count)
751 regs[pos++] = *k++;
752 else
753 for (; count > 0 && pos < PT_MSR; --count) {
754 if (__get_user(reg, u++))
755 return -EFAULT;
756 regs[pos++] = reg;
757 }
758
759
760 if (count > 0 && pos == PT_MSR) {
761 if (kbuf)
762 reg = *k++;
763 else if (__get_user(reg, u++))
764 return -EFAULT;
765 set_user_msr(target, reg);
766 ++pos;
767 --count;
768 }
769
Roland McGrathc2372eb2008-03-13 19:25:35 +1100770 if (kbuf) {
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800771 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
772 regs[pos++] = *k++;
Roland McGrathc2372eb2008-03-13 19:25:35 +1100773 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
774 ++k;
775 } else {
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800776 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
777 if (__get_user(reg, u++))
778 return -EFAULT;
779 regs[pos++] = reg;
780 }
Roland McGrathc2372eb2008-03-13 19:25:35 +1100781 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
782 if (__get_user(reg, u++))
783 return -EFAULT;
784 }
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800785
786 if (count > 0 && pos == PT_TRAP) {
787 if (kbuf)
788 reg = *k++;
789 else if (__get_user(reg, u++))
790 return -EFAULT;
791 set_user_trap(target, reg);
792 ++pos;
793 --count;
794 }
795
796 kbuf = k;
797 ubuf = u;
798 pos *= sizeof(reg);
799 count *= sizeof(reg);
800 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
801 (PT_TRAP + 1) * sizeof(reg), -1);
802}
803
804/*
805 * These are the regset flavors matching the CONFIG_PPC32 native set.
806 */
807static const struct user_regset compat_regsets[] = {
808 [REGSET_GPR] = {
809 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
810 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
811 .get = gpr32_get, .set = gpr32_set
812 },
813 [REGSET_FPR] = {
814 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
815 .size = sizeof(double), .align = sizeof(double),
816 .get = fpr_get, .set = fpr_set
817 },
818#ifdef CONFIG_ALTIVEC
819 [REGSET_VMX] = {
820 .core_note_type = NT_PPC_VMX, .n = 34,
821 .size = sizeof(vector128), .align = sizeof(vector128),
822 .active = vr_active, .get = vr_get, .set = vr_set
823 },
824#endif
825#ifdef CONFIG_SPE
826 [REGSET_SPE] = {
Roland McGrath24f1a842008-01-02 17:05:48 -0800827 .core_note_type = NT_PPC_SPE, .n = 35,
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800828 .size = sizeof(u32), .align = sizeof(u32),
829 .active = evr_active, .get = evr_get, .set = evr_set
830 },
831#endif
832};
833
834static const struct user_regset_view user_ppc_compat_view = {
835 .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
836 .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
837};
838#endif /* CONFIG_PPC64 */
839
Roland McGrath80fdf472007-12-20 03:58:00 -0800840const struct user_regset_view *task_user_regset_view(struct task_struct *task)
841{
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800842#ifdef CONFIG_PPC64
843 if (test_tsk_thread_flag(task, TIF_32BIT))
844 return &user_ppc_compat_view;
845#endif
Roland McGrath80fdf472007-12-20 03:58:00 -0800846 return &user_ppc_native_view;
847}
848
849
Roland McGrath2a84b0d2008-01-30 13:30:51 +0100850void user_enable_single_step(struct task_struct *task)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000851{
852 struct pt_regs *regs = task->thread.regs;
853
854 if (regs != NULL) {
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000855#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Roland McGrathec097c82009-05-28 21:26:38 +0000856 task->thread.dbcr0 &= ~DBCR0_BT;
Luis Machadod6a61bf2008-07-24 02:10:41 +1000857 task->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000858 regs->msr |= MSR_DE;
859#else
Roland McGrathec097c82009-05-28 21:26:38 +0000860 regs->msr &= ~MSR_BE;
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000861 regs->msr |= MSR_SE;
862#endif
863 }
864 set_tsk_thread_flag(task, TIF_SINGLESTEP);
865}
866
Roland McGrathec097c82009-05-28 21:26:38 +0000867void user_enable_block_step(struct task_struct *task)
868{
869 struct pt_regs *regs = task->thread.regs;
870
871 if (regs != NULL) {
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000872#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Roland McGrathec097c82009-05-28 21:26:38 +0000873 task->thread.dbcr0 &= ~DBCR0_IC;
874 task->thread.dbcr0 = DBCR0_IDM | DBCR0_BT;
875 regs->msr |= MSR_DE;
876#else
877 regs->msr &= ~MSR_SE;
878 regs->msr |= MSR_BE;
879#endif
880 }
881 set_tsk_thread_flag(task, TIF_SINGLESTEP);
882}
883
Roland McGrath2a84b0d2008-01-30 13:30:51 +0100884void user_disable_single_step(struct task_struct *task)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000885{
886 struct pt_regs *regs = task->thread.regs;
887
888 if (regs != NULL) {
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000889#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000890 /*
891 * The logic to disable single stepping should be as
892 * simple as turning off the Instruction Complete flag.
893 * And, after doing so, if all debug flags are off, turn
894 * off DBCR0(IDM) and MSR(DE) .... Torez
895 */
896 task->thread.dbcr0 &= ~DBCR0_IC;
897 /*
898 * Test to see if any of the DBCR_ACTIVE_EVENTS bits are set.
899 */
900 if (!DBCR_ACTIVE_EVENTS(task->thread.dbcr0,
901 task->thread.dbcr1)) {
902 /*
903 * All debug events were off.....
904 */
905 task->thread.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp28477fb2009-07-08 13:46:18 +0000906 regs->msr &= ~MSR_DE;
907 }
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000908#else
Roland McGrathec097c82009-05-28 21:26:38 +0000909 regs->msr &= ~(MSR_SE | MSR_BE);
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000910#endif
911 }
912 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
913}
914
K.Prasad5aae8a52010-06-15 11:35:19 +0530915#ifdef CONFIG_HAVE_HW_BREAKPOINT
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200916void ptrace_triggered(struct perf_event *bp,
K.Prasad5aae8a52010-06-15 11:35:19 +0530917 struct perf_sample_data *data, struct pt_regs *regs)
918{
919 struct perf_event_attr attr;
920
921 /*
922 * Disable the breakpoint request here since ptrace has defined a
923 * one-shot behaviour for breakpoint exceptions in PPC64.
924 * The SIGTRAP signal is generated automatically for us in do_dabr().
925 * We don't have to do anything about that here
926 */
927 attr = bp->attr;
928 attr.disabled = true;
929 modify_user_hw_breakpoint(bp, &attr);
930}
931#endif /* CONFIG_HAVE_HW_BREAKPOINT */
932
Luis Machadod6a61bf2008-07-24 02:10:41 +1000933int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000934 unsigned long data)
935{
K.Prasad5aae8a52010-06-15 11:35:19 +0530936#ifdef CONFIG_HAVE_HW_BREAKPOINT
937 int ret;
938 struct thread_struct *thread = &(task->thread);
939 struct perf_event *bp;
940 struct perf_event_attr attr;
941#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Michael Neuling9422de32012-12-20 14:06:44 +0000942#ifndef CONFIG_PPC_ADV_DEBUG_REGS
943 struct arch_hw_breakpoint hw_brk;
944#endif
K.Prasad5aae8a52010-06-15 11:35:19 +0530945
Luis Machadod6a61bf2008-07-24 02:10:41 +1000946 /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
947 * For embedded processors we support one DAC and no IAC's at the
948 * moment.
949 */
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000950 if (addr > 0)
951 return -EINVAL;
952
Kumar Gala2325f0a2008-07-26 05:27:33 +1000953 /* The bottom 3 bits in dabr are flags */
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000954 if ((data & ~0x7UL) >= TASK_SIZE)
955 return -EIO;
956
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000957#ifndef CONFIG_PPC_ADV_DEBUG_REGS
Luis Machadod6a61bf2008-07-24 02:10:41 +1000958 /* For processors using DABR (i.e. 970), the bottom 3 bits are flags.
959 * It was assumed, on previous implementations, that 3 bits were
960 * passed together with the data address, fitting the design of the
961 * DABR register, as follows:
962 *
963 * bit 0: Read flag
964 * bit 1: Write flag
965 * bit 2: Breakpoint translation
966 *
967 * Thus, we use them here as so.
968 */
969
970 /* Ensure breakpoint translation bit is set */
Michael Neuling9422de32012-12-20 14:06:44 +0000971 if (data && !(data & HW_BRK_TYPE_TRANSLATE))
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000972 return -EIO;
Michael Neuling9422de32012-12-20 14:06:44 +0000973 hw_brk.address = data & (~HW_BRK_TYPE_DABR);
974 hw_brk.type = (data & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
975 hw_brk.len = 8;
K.Prasad5aae8a52010-06-15 11:35:19 +0530976#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbecker925f83c2011-05-06 01:53:18 +0200977 if (ptrace_get_breakpoints(task) < 0)
978 return -ESRCH;
979
K.Prasad5aae8a52010-06-15 11:35:19 +0530980 bp = thread->ptrace_bps[0];
Michael Neuling9422de32012-12-20 14:06:44 +0000981 if ((!data) || !(hw_brk.type & HW_BRK_TYPE_RDWR)) {
K.Prasad5aae8a52010-06-15 11:35:19 +0530982 if (bp) {
983 unregister_hw_breakpoint(bp);
984 thread->ptrace_bps[0] = NULL;
985 }
Frederic Weisbecker925f83c2011-05-06 01:53:18 +0200986 ptrace_put_breakpoints(task);
K.Prasad5aae8a52010-06-15 11:35:19 +0530987 return 0;
988 }
989 if (bp) {
990 attr = bp->attr;
Michael Neuling9422de32012-12-20 14:06:44 +0000991 attr.bp_addr = hw_brk.address;
992 arch_bp_generic_fields(hw_brk.type, &attr.bp_type);
Aravinda Prasada53fd612012-11-04 22:15:28 +0000993
994 /* Enable breakpoint */
995 attr.disabled = false;
996
K.Prasad5aae8a52010-06-15 11:35:19 +0530997 ret = modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker925f83c2011-05-06 01:53:18 +0200998 if (ret) {
999 ptrace_put_breakpoints(task);
K.Prasad5aae8a52010-06-15 11:35:19 +05301000 return ret;
Frederic Weisbecker925f83c2011-05-06 01:53:18 +02001001 }
K.Prasad5aae8a52010-06-15 11:35:19 +05301002 thread->ptrace_bps[0] = bp;
Frederic Weisbecker925f83c2011-05-06 01:53:18 +02001003 ptrace_put_breakpoints(task);
Michael Neuling9422de32012-12-20 14:06:44 +00001004 thread->hw_brk = hw_brk;
K.Prasad5aae8a52010-06-15 11:35:19 +05301005 return 0;
1006 }
1007
1008 /* Create a new breakpoint request if one doesn't exist already */
1009 hw_breakpoint_init(&attr);
Michael Neuling9422de32012-12-20 14:06:44 +00001010 attr.bp_addr = hw_brk.address;
1011 arch_bp_generic_fields(hw_brk.type,
1012 &attr.bp_type);
K.Prasad5aae8a52010-06-15 11:35:19 +05301013
1014 thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
Avi Kivity4dc0da82011-06-29 18:42:35 +03001015 ptrace_triggered, NULL, task);
K.Prasad5aae8a52010-06-15 11:35:19 +05301016 if (IS_ERR(bp)) {
1017 thread->ptrace_bps[0] = NULL;
Frederic Weisbecker925f83c2011-05-06 01:53:18 +02001018 ptrace_put_breakpoints(task);
K.Prasad5aae8a52010-06-15 11:35:19 +05301019 return PTR_ERR(bp);
1020 }
1021
Frederic Weisbecker925f83c2011-05-06 01:53:18 +02001022 ptrace_put_breakpoints(task);
1023
K.Prasad5aae8a52010-06-15 11:35:19 +05301024#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Michael Neuling9422de32012-12-20 14:06:44 +00001025 task->thread.hw_brk = hw_brk;
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001026#else /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +10001027 /* As described above, it was assumed 3 bits were passed with the data
1028 * address, but we will assume only the mode bits will be passed
1029 * as to not cause alignment restrictions for DAC-based processors.
1030 */
1031
1032 /* DAC's hold the whole address without any mode flags */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001033 task->thread.dac1 = data & ~0x3UL;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001034
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001035 if (task->thread.dac1 == 0) {
1036 dbcr_dac(task) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1037 if (!DBCR_ACTIVE_EVENTS(task->thread.dbcr0,
1038 task->thread.dbcr1)) {
1039 task->thread.regs->msr &= ~MSR_DE;
1040 task->thread.dbcr0 &= ~DBCR0_IDM;
1041 }
Luis Machadod6a61bf2008-07-24 02:10:41 +10001042 return 0;
1043 }
1044
1045 /* Read or Write bits must be set */
1046
1047 if (!(data & 0x3UL))
1048 return -EINVAL;
1049
1050 /* Set the Internal Debugging flag (IDM bit 1) for the DBCR0
1051 register */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001052 task->thread.dbcr0 |= DBCR0_IDM;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001053
1054 /* Check for write and read flags and set DBCR0
1055 accordingly */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001056 dbcr_dac(task) &= ~(DBCR_DAC1R|DBCR_DAC1W);
Luis Machadod6a61bf2008-07-24 02:10:41 +10001057 if (data & 0x1UL)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001058 dbcr_dac(task) |= DBCR_DAC1R;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001059 if (data & 0x2UL)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001060 dbcr_dac(task) |= DBCR_DAC1W;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001061 task->thread.regs->msr |= MSR_DE;
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001062#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001063 return 0;
1064}
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001065
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +10001066/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * Called by kernel/ptrace.c when detaching..
1068 *
1069 * Make sure single step bits etc are not set.
1070 */
1071void ptrace_disable(struct task_struct *child)
1072{
1073 /* make sure the single step bit is not set. */
Roland McGrath2a84b0d2008-01-30 13:30:51 +01001074 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001077#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling84295df2012-10-28 15:13:16 +00001078static long set_instruction_bp(struct task_struct *child,
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001079 struct ppc_hw_breakpoint *bp_info)
1080{
1081 int slot;
1082 int slot1_in_use = ((child->thread.dbcr0 & DBCR0_IAC1) != 0);
1083 int slot2_in_use = ((child->thread.dbcr0 & DBCR0_IAC2) != 0);
1084 int slot3_in_use = ((child->thread.dbcr0 & DBCR0_IAC3) != 0);
1085 int slot4_in_use = ((child->thread.dbcr0 & DBCR0_IAC4) != 0);
1086
1087 if (dbcr_iac_range(child) & DBCR_IAC12MODE)
1088 slot2_in_use = 1;
1089 if (dbcr_iac_range(child) & DBCR_IAC34MODE)
1090 slot4_in_use = 1;
1091
1092 if (bp_info->addr >= TASK_SIZE)
1093 return -EIO;
1094
1095 if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) {
1096
1097 /* Make sure range is valid. */
1098 if (bp_info->addr2 >= TASK_SIZE)
1099 return -EIO;
1100
1101 /* We need a pair of IAC regsisters */
1102 if ((!slot1_in_use) && (!slot2_in_use)) {
1103 slot = 1;
1104 child->thread.iac1 = bp_info->addr;
1105 child->thread.iac2 = bp_info->addr2;
1106 child->thread.dbcr0 |= DBCR0_IAC1;
1107 if (bp_info->addr_mode ==
1108 PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
1109 dbcr_iac_range(child) |= DBCR_IAC12X;
1110 else
1111 dbcr_iac_range(child) |= DBCR_IAC12I;
1112#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1113 } else if ((!slot3_in_use) && (!slot4_in_use)) {
1114 slot = 3;
1115 child->thread.iac3 = bp_info->addr;
1116 child->thread.iac4 = bp_info->addr2;
1117 child->thread.dbcr0 |= DBCR0_IAC3;
1118 if (bp_info->addr_mode ==
1119 PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
1120 dbcr_iac_range(child) |= DBCR_IAC34X;
1121 else
1122 dbcr_iac_range(child) |= DBCR_IAC34I;
1123#endif
1124 } else
1125 return -ENOSPC;
1126 } else {
1127 /* We only need one. If possible leave a pair free in
1128 * case a range is needed later
1129 */
1130 if (!slot1_in_use) {
1131 /*
1132 * Don't use iac1 if iac1-iac2 are free and either
1133 * iac3 or iac4 (but not both) are free
1134 */
1135 if (slot2_in_use || (slot3_in_use == slot4_in_use)) {
1136 slot = 1;
1137 child->thread.iac1 = bp_info->addr;
1138 child->thread.dbcr0 |= DBCR0_IAC1;
1139 goto out;
1140 }
1141 }
1142 if (!slot2_in_use) {
1143 slot = 2;
1144 child->thread.iac2 = bp_info->addr;
1145 child->thread.dbcr0 |= DBCR0_IAC2;
1146#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1147 } else if (!slot3_in_use) {
1148 slot = 3;
1149 child->thread.iac3 = bp_info->addr;
1150 child->thread.dbcr0 |= DBCR0_IAC3;
1151 } else if (!slot4_in_use) {
1152 slot = 4;
1153 child->thread.iac4 = bp_info->addr;
1154 child->thread.dbcr0 |= DBCR0_IAC4;
1155#endif
1156 } else
1157 return -ENOSPC;
1158 }
1159out:
1160 child->thread.dbcr0 |= DBCR0_IDM;
1161 child->thread.regs->msr |= MSR_DE;
1162
1163 return slot;
1164}
1165
1166static int del_instruction_bp(struct task_struct *child, int slot)
1167{
1168 switch (slot) {
1169 case 1:
Dave Kleikamp30124d12010-03-01 04:57:34 +00001170 if ((child->thread.dbcr0 & DBCR0_IAC1) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001171 return -ENOENT;
1172
1173 if (dbcr_iac_range(child) & DBCR_IAC12MODE) {
1174 /* address range - clear slots 1 & 2 */
1175 child->thread.iac2 = 0;
1176 dbcr_iac_range(child) &= ~DBCR_IAC12MODE;
1177 }
1178 child->thread.iac1 = 0;
1179 child->thread.dbcr0 &= ~DBCR0_IAC1;
1180 break;
1181 case 2:
Dave Kleikamp30124d12010-03-01 04:57:34 +00001182 if ((child->thread.dbcr0 & DBCR0_IAC2) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001183 return -ENOENT;
1184
1185 if (dbcr_iac_range(child) & DBCR_IAC12MODE)
1186 /* used in a range */
1187 return -EINVAL;
1188 child->thread.iac2 = 0;
1189 child->thread.dbcr0 &= ~DBCR0_IAC2;
1190 break;
1191#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1192 case 3:
Dave Kleikamp30124d12010-03-01 04:57:34 +00001193 if ((child->thread.dbcr0 & DBCR0_IAC3) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001194 return -ENOENT;
1195
1196 if (dbcr_iac_range(child) & DBCR_IAC34MODE) {
1197 /* address range - clear slots 3 & 4 */
1198 child->thread.iac4 = 0;
1199 dbcr_iac_range(child) &= ~DBCR_IAC34MODE;
1200 }
1201 child->thread.iac3 = 0;
1202 child->thread.dbcr0 &= ~DBCR0_IAC3;
1203 break;
1204 case 4:
Dave Kleikamp30124d12010-03-01 04:57:34 +00001205 if ((child->thread.dbcr0 & DBCR0_IAC4) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001206 return -ENOENT;
1207
1208 if (dbcr_iac_range(child) & DBCR_IAC34MODE)
1209 /* Used in a range */
1210 return -EINVAL;
1211 child->thread.iac4 = 0;
1212 child->thread.dbcr0 &= ~DBCR0_IAC4;
1213 break;
1214#endif
1215 default:
1216 return -EINVAL;
1217 }
1218 return 0;
1219}
1220
1221static int set_dac(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
1222{
1223 int byte_enable =
1224 (bp_info->condition_mode >> PPC_BREAKPOINT_CONDITION_BE_SHIFT)
1225 & 0xf;
1226 int condition_mode =
1227 bp_info->condition_mode & PPC_BREAKPOINT_CONDITION_MODE;
1228 int slot;
1229
1230 if (byte_enable && (condition_mode == 0))
1231 return -EINVAL;
1232
1233 if (bp_info->addr >= TASK_SIZE)
1234 return -EIO;
1235
1236 if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0) {
1237 slot = 1;
1238 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
1239 dbcr_dac(child) |= DBCR_DAC1R;
1240 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
1241 dbcr_dac(child) |= DBCR_DAC1W;
1242 child->thread.dac1 = (unsigned long)bp_info->addr;
1243#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
1244 if (byte_enable) {
1245 child->thread.dvc1 =
1246 (unsigned long)bp_info->condition_value;
1247 child->thread.dbcr2 |=
1248 ((byte_enable << DBCR2_DVC1BE_SHIFT) |
1249 (condition_mode << DBCR2_DVC1M_SHIFT));
1250 }
1251#endif
1252#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1253 } else if (child->thread.dbcr2 & DBCR2_DAC12MODE) {
1254 /* Both dac1 and dac2 are part of a range */
1255 return -ENOSPC;
1256#endif
1257 } else if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0) {
1258 slot = 2;
1259 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
1260 dbcr_dac(child) |= DBCR_DAC2R;
1261 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
1262 dbcr_dac(child) |= DBCR_DAC2W;
1263 child->thread.dac2 = (unsigned long)bp_info->addr;
1264#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
1265 if (byte_enable) {
1266 child->thread.dvc2 =
1267 (unsigned long)bp_info->condition_value;
1268 child->thread.dbcr2 |=
1269 ((byte_enable << DBCR2_DVC2BE_SHIFT) |
1270 (condition_mode << DBCR2_DVC2M_SHIFT));
1271 }
1272#endif
1273 } else
1274 return -ENOSPC;
1275 child->thread.dbcr0 |= DBCR0_IDM;
1276 child->thread.regs->msr |= MSR_DE;
1277
1278 return slot + 4;
1279}
1280
1281static int del_dac(struct task_struct *child, int slot)
1282{
1283 if (slot == 1) {
Dave Kleikamp30124d12010-03-01 04:57:34 +00001284 if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001285 return -ENOENT;
1286
1287 child->thread.dac1 = 0;
1288 dbcr_dac(child) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1289#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1290 if (child->thread.dbcr2 & DBCR2_DAC12MODE) {
1291 child->thread.dac2 = 0;
1292 child->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1293 }
1294 child->thread.dbcr2 &= ~(DBCR2_DVC1M | DBCR2_DVC1BE);
1295#endif
1296#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
1297 child->thread.dvc1 = 0;
1298#endif
1299 } else if (slot == 2) {
Dave Kleikamp30124d12010-03-01 04:57:34 +00001300 if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001301 return -ENOENT;
1302
1303#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1304 if (child->thread.dbcr2 & DBCR2_DAC12MODE)
1305 /* Part of a range */
1306 return -EINVAL;
1307 child->thread.dbcr2 &= ~(DBCR2_DVC2M | DBCR2_DVC2BE);
1308#endif
1309#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
1310 child->thread.dvc2 = 0;
1311#endif
1312 child->thread.dac2 = 0;
1313 dbcr_dac(child) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1314 } else
1315 return -EINVAL;
1316
1317 return 0;
1318}
1319#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
1320
1321#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1322static int set_dac_range(struct task_struct *child,
1323 struct ppc_hw_breakpoint *bp_info)
1324{
1325 int mode = bp_info->addr_mode & PPC_BREAKPOINT_MODE_MASK;
1326
1327 /* We don't allow range watchpoints to be used with DVC */
1328 if (bp_info->condition_mode)
1329 return -EINVAL;
1330
1331 /*
1332 * Best effort to verify the address range. The user/supervisor bits
1333 * prevent trapping in kernel space, but let's fail on an obvious bad
1334 * range. The simple test on the mask is not fool-proof, and any
1335 * exclusive range will spill over into kernel space.
1336 */
1337 if (bp_info->addr >= TASK_SIZE)
1338 return -EIO;
1339 if (mode == PPC_BREAKPOINT_MODE_MASK) {
1340 /*
1341 * dac2 is a bitmask. Don't allow a mask that makes a
1342 * kernel space address from a valid dac1 value
1343 */
1344 if (~((unsigned long)bp_info->addr2) >= TASK_SIZE)
1345 return -EIO;
1346 } else {
1347 /*
1348 * For range breakpoints, addr2 must also be a valid address
1349 */
1350 if (bp_info->addr2 >= TASK_SIZE)
1351 return -EIO;
1352 }
1353
1354 if (child->thread.dbcr0 &
1355 (DBCR0_DAC1R | DBCR0_DAC1W | DBCR0_DAC2R | DBCR0_DAC2W))
1356 return -ENOSPC;
1357
1358 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
1359 child->thread.dbcr0 |= (DBCR0_DAC1R | DBCR0_IDM);
1360 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
1361 child->thread.dbcr0 |= (DBCR0_DAC1W | DBCR0_IDM);
1362 child->thread.dac1 = bp_info->addr;
1363 child->thread.dac2 = bp_info->addr2;
1364 if (mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
1365 child->thread.dbcr2 |= DBCR2_DAC12M;
1366 else if (mode == PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
1367 child->thread.dbcr2 |= DBCR2_DAC12MX;
1368 else /* PPC_BREAKPOINT_MODE_MASK */
1369 child->thread.dbcr2 |= DBCR2_DAC12MM;
1370 child->thread.regs->msr |= MSR_DE;
1371
1372 return 5;
1373}
1374#endif /* CONFIG_PPC_ADV_DEBUG_DAC_RANGE */
1375
Dave Kleikamp3162d922010-02-08 11:51:05 +00001376static long ppc_set_hwdebug(struct task_struct *child,
1377 struct ppc_hw_breakpoint *bp_info)
1378{
K.Prasad6c7a2852012-10-28 15:13:15 +00001379#ifdef CONFIG_HAVE_HW_BREAKPOINT
1380 int len = 0;
1381 struct thread_struct *thread = &(child->thread);
1382 struct perf_event *bp;
1383 struct perf_event_attr attr;
1384#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001385#ifndef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling9422de32012-12-20 14:06:44 +00001386 struct arch_hw_breakpoint brk;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001387#endif
1388
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001389 if (bp_info->version != 1)
1390 return -ENOTSUPP;
1391#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3162d922010-02-08 11:51:05 +00001392 /*
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001393 * Check for invalid flags and combinations
1394 */
1395 if ((bp_info->trigger_type == 0) ||
1396 (bp_info->trigger_type & ~(PPC_BREAKPOINT_TRIGGER_EXECUTE |
1397 PPC_BREAKPOINT_TRIGGER_RW)) ||
1398 (bp_info->addr_mode & ~PPC_BREAKPOINT_MODE_MASK) ||
1399 (bp_info->condition_mode &
1400 ~(PPC_BREAKPOINT_CONDITION_MODE |
1401 PPC_BREAKPOINT_CONDITION_BE_ALL)))
1402 return -EINVAL;
1403#if CONFIG_PPC_ADV_DEBUG_DVCS == 0
1404 if (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
1405 return -EINVAL;
1406#endif
1407
1408 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_EXECUTE) {
1409 if ((bp_info->trigger_type != PPC_BREAKPOINT_TRIGGER_EXECUTE) ||
1410 (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE))
1411 return -EINVAL;
Michael Neuling84295df2012-10-28 15:13:16 +00001412 return set_instruction_bp(child, bp_info);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001413 }
1414 if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
1415 return set_dac(child, bp_info);
1416
1417#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1418 return set_dac_range(child, bp_info);
1419#else
1420 return -EINVAL;
1421#endif
1422#else /* !CONFIG_PPC_ADV_DEBUG_DVCS */
1423 /*
1424 * We only support one data breakpoint
Dave Kleikamp3162d922010-02-08 11:51:05 +00001425 */
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001426 if ((bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_RW) == 0 ||
1427 (bp_info->trigger_type & ~PPC_BREAKPOINT_TRIGGER_RW) != 0 ||
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001428 bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
Dave Kleikamp3162d922010-02-08 11:51:05 +00001429 return -EINVAL;
1430
Dave Kleikamp3162d922010-02-08 11:51:05 +00001431 if ((unsigned long)bp_info->addr >= TASK_SIZE)
1432 return -EIO;
1433
Michael Neuling9422de32012-12-20 14:06:44 +00001434 brk.address = bp_info->addr & ~7UL;
1435 brk.type = HW_BRK_TYPE_TRANSLATE;
Michael Neuling2bb78ef2013-03-11 16:42:49 +00001436 brk.len = 8;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001437 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
Michael Neuling9422de32012-12-20 14:06:44 +00001438 brk.type |= HW_BRK_TYPE_READ;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001439 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
Michael Neuling9422de32012-12-20 14:06:44 +00001440 brk.type |= HW_BRK_TYPE_WRITE;
K.Prasad6c7a2852012-10-28 15:13:15 +00001441#ifdef CONFIG_HAVE_HW_BREAKPOINT
1442 if (ptrace_get_breakpoints(child) < 0)
1443 return -ESRCH;
1444
1445 /*
1446 * Check if the request is for 'range' breakpoints. We can
1447 * support it if range < 8 bytes.
1448 */
1449 if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE) {
1450 len = bp_info->addr2 - bp_info->addr;
1451 } else if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) {
1452 ptrace_put_breakpoints(child);
1453 return -EINVAL;
1454 }
1455 bp = thread->ptrace_bps[0];
1456 if (bp) {
1457 ptrace_put_breakpoints(child);
1458 return -ENOSPC;
1459 }
1460
1461 /* Create a new breakpoint request if one doesn't exist already */
1462 hw_breakpoint_init(&attr);
1463 attr.bp_addr = (unsigned long)bp_info->addr & ~HW_BREAKPOINT_ALIGN;
1464 attr.bp_len = len;
Michael Neuling9422de32012-12-20 14:06:44 +00001465 arch_bp_generic_fields(brk.type, &attr.bp_type);
K.Prasad6c7a2852012-10-28 15:13:15 +00001466
1467 thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
1468 ptrace_triggered, NULL, child);
1469 if (IS_ERR(bp)) {
1470 thread->ptrace_bps[0] = NULL;
1471 ptrace_put_breakpoints(child);
1472 return PTR_ERR(bp);
1473 }
1474
1475 ptrace_put_breakpoints(child);
1476 return 1;
1477#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1478
1479 if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT)
1480 return -EINVAL;
1481
Michael Neuling9422de32012-12-20 14:06:44 +00001482 if (child->thread.hw_brk.address)
K.Prasad6c7a2852012-10-28 15:13:15 +00001483 return -ENOSPC;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001484
Michael Neuling9422de32012-12-20 14:06:44 +00001485 child->thread.hw_brk = brk;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001486
Dave Kleikamp3162d922010-02-08 11:51:05 +00001487 return 1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001488#endif /* !CONFIG_PPC_ADV_DEBUG_DVCS */
Dave Kleikamp3162d922010-02-08 11:51:05 +00001489}
1490
Michael Neulingec1b33d2012-10-28 15:13:17 +00001491static long ppc_del_hwdebug(struct task_struct *child, long data)
Dave Kleikamp3162d922010-02-08 11:51:05 +00001492{
K.Prasad6c7a2852012-10-28 15:13:15 +00001493#ifdef CONFIG_HAVE_HW_BREAKPOINT
1494 int ret = 0;
1495 struct thread_struct *thread = &(child->thread);
1496 struct perf_event *bp;
1497#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001498#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1499 int rc;
1500
1501 if (data <= 4)
1502 rc = del_instruction_bp(child, (int)data);
1503 else
1504 rc = del_dac(child, (int)data - 4);
1505
1506 if (!rc) {
1507 if (!DBCR_ACTIVE_EVENTS(child->thread.dbcr0,
1508 child->thread.dbcr1)) {
1509 child->thread.dbcr0 &= ~DBCR0_IDM;
1510 child->thread.regs->msr &= ~MSR_DE;
1511 }
1512 }
1513 return rc;
1514#else
Dave Kleikamp3162d922010-02-08 11:51:05 +00001515 if (data != 1)
1516 return -EINVAL;
K.Prasad6c7a2852012-10-28 15:13:15 +00001517
1518#ifdef CONFIG_HAVE_HW_BREAKPOINT
1519 if (ptrace_get_breakpoints(child) < 0)
1520 return -ESRCH;
1521
1522 bp = thread->ptrace_bps[0];
1523 if (bp) {
1524 unregister_hw_breakpoint(bp);
1525 thread->ptrace_bps[0] = NULL;
1526 } else
1527 ret = -ENOENT;
1528 ptrace_put_breakpoints(child);
1529 return ret;
1530#else /* CONFIG_HAVE_HW_BREAKPOINT */
Michael Neuling9422de32012-12-20 14:06:44 +00001531 if (child->thread.hw_brk.address == 0)
Dave Kleikamp3162d922010-02-08 11:51:05 +00001532 return -ENOENT;
1533
Michael Neuling9422de32012-12-20 14:06:44 +00001534 child->thread.hw_brk.address = 0;
1535 child->thread.hw_brk.type = 0;
K.Prasad6c7a2852012-10-28 15:13:15 +00001536#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001537
Dave Kleikamp3162d922010-02-08 11:51:05 +00001538 return 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001539#endif
Dave Kleikamp3162d922010-02-08 11:51:05 +00001540}
1541
Namhyung Kim9b05a692010-10-27 15:33:47 -07001542long arch_ptrace(struct task_struct *child, long request,
1543 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int ret = -EPERM;
Namhyung Kimf68d2042010-10-27 15:34:01 -07001546 void __user *datavp = (void __user *) data;
1547 unsigned long __user *datalp = datavp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /* read the word at location addr in the USER area. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 case PTRACE_PEEKUSR: {
1552 unsigned long index, tmp;
1553
1554 ret = -EIO;
1555 /* convert to index and check */
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001556#ifdef CONFIG_PPC32
Namhyung Kim9b05a692010-10-27 15:33:47 -07001557 index = addr >> 2;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001558 if ((addr & 3) || (index > PT_FPSCR)
1559 || (child->thread.regs == NULL))
1560#else
Namhyung Kim9b05a692010-10-27 15:33:47 -07001561 index = addr >> 3;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001562 if ((addr & 7) || (index > PT_FPSCR))
1563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
1565
1566 CHECK_FULL_REGS(child->thread.regs);
1567 if (index < PT_FPR0) {
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +00001568 ret = ptrace_get_reg(child, (int) index, &tmp);
1569 if (ret)
1570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 } else {
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001572 unsigned int fpidx = index - PT_FPR0;
1573
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001574 flush_fp_to_thread(child);
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001575 if (fpidx < (PT_FPSCR - PT_FPR0))
1576 tmp = ((unsigned long *)child->thread.fpr)
1577 [fpidx * TS_FPRWIDTH];
1578 else
1579 tmp = child->thread.fpscr.val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Namhyung Kimf68d2042010-10-27 15:34:01 -07001581 ret = put_user(tmp, datalp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
1583 }
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* write the word at location addr in the USER area */
1586 case PTRACE_POKEUSR: {
1587 unsigned long index;
1588
1589 ret = -EIO;
1590 /* convert to index and check */
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001591#ifdef CONFIG_PPC32
Namhyung Kim9b05a692010-10-27 15:33:47 -07001592 index = addr >> 2;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001593 if ((addr & 3) || (index > PT_FPSCR)
1594 || (child->thread.regs == NULL))
1595#else
Namhyung Kim9b05a692010-10-27 15:33:47 -07001596 index = addr >> 3;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001597 if ((addr & 7) || (index > PT_FPSCR))
1598#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600
1601 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +10001603 ret = ptrace_put_reg(child, index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 } else {
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001605 unsigned int fpidx = index - PT_FPR0;
1606
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001607 flush_fp_to_thread(child);
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001608 if (fpidx < (PT_FPSCR - PT_FPR0))
1609 ((unsigned long *)child->thread.fpr)
1610 [fpidx * TS_FPRWIDTH] = data;
1611 else
1612 child->thread.fpscr.val = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 ret = 0;
1614 }
1615 break;
1616 }
1617
Dave Kleikamp3162d922010-02-08 11:51:05 +00001618 case PPC_PTRACE_GETHWDBGINFO: {
1619 struct ppc_debug_info dbginfo;
1620
1621 dbginfo.version = 1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001622#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1623 dbginfo.num_instruction_bps = CONFIG_PPC_ADV_DEBUG_IACS;
1624 dbginfo.num_data_bps = CONFIG_PPC_ADV_DEBUG_DACS;
1625 dbginfo.num_condition_regs = CONFIG_PPC_ADV_DEBUG_DVCS;
1626 dbginfo.data_bp_alignment = 4;
1627 dbginfo.sizeof_condition = 4;
1628 dbginfo.features = PPC_DEBUG_FEATURE_INSN_BP_RANGE |
1629 PPC_DEBUG_FEATURE_INSN_BP_MASK;
1630#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1631 dbginfo.features |=
1632 PPC_DEBUG_FEATURE_DATA_BP_RANGE |
1633 PPC_DEBUG_FEATURE_DATA_BP_MASK;
1634#endif
1635#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Dave Kleikamp3162d922010-02-08 11:51:05 +00001636 dbginfo.num_instruction_bps = 0;
1637 dbginfo.num_data_bps = 1;
1638 dbginfo.num_condition_regs = 0;
1639#ifdef CONFIG_PPC64
1640 dbginfo.data_bp_alignment = 8;
1641#else
1642 dbginfo.data_bp_alignment = 4;
1643#endif
1644 dbginfo.sizeof_condition = 0;
K.Prasad6c7a2852012-10-28 15:13:15 +00001645#ifdef CONFIG_HAVE_HW_BREAKPOINT
1646 dbginfo.features = PPC_DEBUG_FEATURE_DATA_BP_RANGE;
1647#else
Dave Kleikamp3162d922010-02-08 11:51:05 +00001648 dbginfo.features = 0;
K.Prasad6c7a2852012-10-28 15:13:15 +00001649#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001650#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Dave Kleikamp3162d922010-02-08 11:51:05 +00001651
Namhyung Kimf68d2042010-10-27 15:34:01 -07001652 if (!access_ok(VERIFY_WRITE, datavp,
Dave Kleikamp3162d922010-02-08 11:51:05 +00001653 sizeof(struct ppc_debug_info)))
1654 return -EFAULT;
Namhyung Kimf68d2042010-10-27 15:34:01 -07001655 ret = __copy_to_user(datavp, &dbginfo,
1656 sizeof(struct ppc_debug_info)) ?
Dave Kleikamp3162d922010-02-08 11:51:05 +00001657 -EFAULT : 0;
1658 break;
1659 }
1660
1661 case PPC_PTRACE_SETHWDEBUG: {
1662 struct ppc_hw_breakpoint bp_info;
1663
Namhyung Kimf68d2042010-10-27 15:34:01 -07001664 if (!access_ok(VERIFY_READ, datavp,
Dave Kleikamp3162d922010-02-08 11:51:05 +00001665 sizeof(struct ppc_hw_breakpoint)))
1666 return -EFAULT;
Namhyung Kimf68d2042010-10-27 15:34:01 -07001667 ret = __copy_from_user(&bp_info, datavp,
Dave Kleikamp3162d922010-02-08 11:51:05 +00001668 sizeof(struct ppc_hw_breakpoint)) ?
1669 -EFAULT : 0;
1670 if (!ret)
1671 ret = ppc_set_hwdebug(child, &bp_info);
1672 break;
1673 }
1674
1675 case PPC_PTRACE_DELHWDEBUG: {
Michael Neulingec1b33d2012-10-28 15:13:17 +00001676 ret = ppc_del_hwdebug(child, data);
Dave Kleikamp3162d922010-02-08 11:51:05 +00001677 break;
1678 }
1679
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001680 case PTRACE_GET_DEBUGREG: {
Michael Neuling9422de32012-12-20 14:06:44 +00001681#ifndef CONFIG_PPC_ADV_DEBUG_REGS
1682 unsigned long dabr_fake;
1683#endif
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001684 ret = -EINVAL;
1685 /* We only support one DABR and no IABRS at the moment */
1686 if (addr > 0)
1687 break;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001688#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Namhyung Kimf68d2042010-10-27 15:34:01 -07001689 ret = put_user(child->thread.dac1, datalp);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001690#else
Michael Neuling9422de32012-12-20 14:06:44 +00001691 dabr_fake = ((child->thread.hw_brk.address & (~HW_BRK_TYPE_DABR)) |
1692 (child->thread.hw_brk.type & HW_BRK_TYPE_DABR));
1693 ret = put_user(dabr_fake, datalp);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001694#endif
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001695 break;
1696 }
1697
1698 case PTRACE_SET_DEBUGREG:
1699 ret = ptrace_set_debugreg(child, addr, data);
1700 break;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001701
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +10001702#ifdef CONFIG_PPC64
1703 case PTRACE_GETREGS64:
1704#endif
Roland McGrathc391cd02007-12-20 03:58:36 -08001705 case PTRACE_GETREGS: /* Get all pt_regs from the child. */
1706 return copy_regset_to_user(child, &user_ppc_native_view,
1707 REGSET_GPR,
1708 0, sizeof(struct pt_regs),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001709 datavp);
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001710
Benjamin Herrenschmidt0b3d5c42007-06-04 15:15:39 +10001711#ifdef CONFIG_PPC64
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +10001712 case PTRACE_SETREGS64:
1713#endif
Roland McGrathc391cd02007-12-20 03:58:36 -08001714 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1715 return copy_regset_from_user(child, &user_ppc_native_view,
1716 REGSET_GPR,
1717 0, sizeof(struct pt_regs),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001718 datavp);
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001719
Roland McGrathc391cd02007-12-20 03:58:36 -08001720 case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
1721 return copy_regset_to_user(child, &user_ppc_native_view,
1722 REGSET_FPR,
1723 0, sizeof(elf_fpregset_t),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001724 datavp);
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +10001725
Roland McGrathc391cd02007-12-20 03:58:36 -08001726 case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
1727 return copy_regset_from_user(child, &user_ppc_native_view,
1728 REGSET_FPR,
1729 0, sizeof(elf_fpregset_t),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001730 datavp);
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732#ifdef CONFIG_ALTIVEC
1733 case PTRACE_GETVRREGS:
Roland McGrathc391cd02007-12-20 03:58:36 -08001734 return copy_regset_to_user(child, &user_ppc_native_view,
1735 REGSET_VMX,
1736 0, (33 * sizeof(vector128) +
1737 sizeof(u32)),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001738 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 case PTRACE_SETVRREGS:
Roland McGrathc391cd02007-12-20 03:58:36 -08001741 return copy_regset_from_user(child, &user_ppc_native_view,
1742 REGSET_VMX,
1743 0, (33 * sizeof(vector128) +
1744 sizeof(u32)),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001745 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001747#ifdef CONFIG_VSX
1748 case PTRACE_GETVSRREGS:
1749 return copy_regset_to_user(child, &user_ppc_native_view,
1750 REGSET_VSX,
Michael Neuling1ac42ef82008-07-29 01:13:14 +10001751 0, 32 * sizeof(double),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001752 datavp);
Michael Neulingce48b212008-06-25 14:07:18 +10001753
1754 case PTRACE_SETVSRREGS:
1755 return copy_regset_from_user(child, &user_ppc_native_view,
1756 REGSET_VSX,
Michael Neuling1ac42ef82008-07-29 01:13:14 +10001757 0, 32 * sizeof(double),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001758 datavp);
Michael Neulingce48b212008-06-25 14:07:18 +10001759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760#ifdef CONFIG_SPE
1761 case PTRACE_GETEVRREGS:
1762 /* Get the child spe register state. */
Roland McGrathc391cd02007-12-20 03:58:36 -08001763 return copy_regset_to_user(child, &user_ppc_native_view,
1764 REGSET_SPE, 0, 35 * sizeof(u32),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001765 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 case PTRACE_SETEVRREGS:
1768 /* Set the child spe register state. */
Roland McGrathc391cd02007-12-20 03:58:36 -08001769 return copy_regset_from_user(child, &user_ppc_native_view,
1770 REGSET_SPE, 0, 35 * sizeof(u32),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001771 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772#endif
1773
1774 default:
1775 ret = ptrace_request(child, request, addr, data);
1776 break;
1777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return ret;
1779}
1780
Roland McGrath4f72c422008-07-27 16:51:03 +10001781/*
1782 * We must return the syscall number to actually look up in the table.
1783 * This can be -1L to skip running any syscall at all.
1784 */
1785long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Roland McGrath4f72c422008-07-27 16:51:03 +10001787 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Will Drewrye4da89d2012-04-17 14:48:57 -05001789 secure_computing_strict(regs->gpr[0]);
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001790
Roland McGrath4f72c422008-07-27 16:51:03 +10001791 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
1792 tracehook_report_syscall_entry(regs))
1793 /*
1794 * Tracing decided this syscall should not happen.
1795 * We'll return a bogus call number to get an ENOSYS
1796 * error, but leave the original number in regs->gpr[0].
1797 */
1798 ret = -1L;
David Woodhouseea9c1022005-05-08 15:56:09 +01001799
Ian Munsie02424d82011-02-02 17:27:24 +00001800 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1801 trace_sys_enter(regs, regs->gpr[0]);
1802
David Woodhousecfcd1702007-01-14 09:38:18 +08001803#ifdef CONFIG_PPC64
Eric Parisb05d8442012-01-03 14:23:06 -05001804 if (!is_32bit_task())
1805 audit_syscall_entry(AUDIT_ARCH_PPC64,
1806 regs->gpr[0],
1807 regs->gpr[3], regs->gpr[4],
1808 regs->gpr[5], regs->gpr[6]);
1809 else
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001810#endif
Eric Parisb05d8442012-01-03 14:23:06 -05001811 audit_syscall_entry(AUDIT_ARCH_PPC,
1812 regs->gpr[0],
1813 regs->gpr[3] & 0xffffffff,
1814 regs->gpr[4] & 0xffffffff,
1815 regs->gpr[5] & 0xffffffff,
1816 regs->gpr[6] & 0xffffffff);
Roland McGrath4f72c422008-07-27 16:51:03 +10001817
1818 return ret ?: regs->gpr[0];
David Woodhouseea9c1022005-05-08 15:56:09 +01001819}
1820
1821void do_syscall_trace_leave(struct pt_regs *regs)
1822{
Roland McGrath4f72c422008-07-27 16:51:03 +10001823 int step;
1824
Eric Parisd7e75282012-01-03 14:23:06 -05001825 audit_syscall_exit(regs);
David Woodhouseea9c1022005-05-08 15:56:09 +01001826
Ian Munsie02424d82011-02-02 17:27:24 +00001827 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1828 trace_sys_exit(regs, regs->result);
1829
Roland McGrath4f72c422008-07-27 16:51:03 +10001830 step = test_thread_flag(TIF_SINGLESTEP);
1831 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1832 tracehook_report_syscall_exit(regs, step);
David Woodhouseea9c1022005-05-08 15:56:09 +01001833}