blob: e8d126c3e7cded59f2717d78497be6df2e9d21c3 [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>
Li Zhong22ecbe82013-05-13 16:16:40 +000035#include <linux/context_tracking.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
38#include <asm/page.h>
39#include <asm/pgtable.h>
David Howellsae3a1972012-03-28 18:30:02 +010040#include <asm/switch_to.h>
Paul Mackerras21a62902005-11-19 20:47:22 +110041
Ian Munsie02424d82011-02-02 17:27:24 +000042#define CREATE_TRACE_POINTS
43#include <trace/events/syscalls.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
Mahesh Salgaonkar359e4282010-04-07 18:10:20 +100046 * The parameter save area on the stack is used to store arguments being passed
47 * to callee function and is located at fixed offset from stack pointer.
48 */
49#ifdef CONFIG_PPC32
50#define PARAMETER_SAVE_AREA_OFFSET 24 /* bytes */
51#else /* CONFIG_PPC32 */
52#define PARAMETER_SAVE_AREA_OFFSET 48 /* bytes */
53#endif
54
55struct pt_regs_offset {
56 const char *name;
57 int offset;
58};
59
60#define STR(s) #s /* convert to string */
61#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
62#define GPR_OFFSET_NAME(num) \
Rashmica Gupta343c3322015-11-21 17:08:16 +110063 {.name = STR(r##num), .offset = offsetof(struct pt_regs, gpr[num])}, \
Mahesh Salgaonkar359e4282010-04-07 18:10:20 +100064 {.name = STR(gpr##num), .offset = offsetof(struct pt_regs, gpr[num])}
65#define REG_OFFSET_END {.name = NULL, .offset = 0}
66
67static const struct pt_regs_offset regoffset_table[] = {
68 GPR_OFFSET_NAME(0),
69 GPR_OFFSET_NAME(1),
70 GPR_OFFSET_NAME(2),
71 GPR_OFFSET_NAME(3),
72 GPR_OFFSET_NAME(4),
73 GPR_OFFSET_NAME(5),
74 GPR_OFFSET_NAME(6),
75 GPR_OFFSET_NAME(7),
76 GPR_OFFSET_NAME(8),
77 GPR_OFFSET_NAME(9),
78 GPR_OFFSET_NAME(10),
79 GPR_OFFSET_NAME(11),
80 GPR_OFFSET_NAME(12),
81 GPR_OFFSET_NAME(13),
82 GPR_OFFSET_NAME(14),
83 GPR_OFFSET_NAME(15),
84 GPR_OFFSET_NAME(16),
85 GPR_OFFSET_NAME(17),
86 GPR_OFFSET_NAME(18),
87 GPR_OFFSET_NAME(19),
88 GPR_OFFSET_NAME(20),
89 GPR_OFFSET_NAME(21),
90 GPR_OFFSET_NAME(22),
91 GPR_OFFSET_NAME(23),
92 GPR_OFFSET_NAME(24),
93 GPR_OFFSET_NAME(25),
94 GPR_OFFSET_NAME(26),
95 GPR_OFFSET_NAME(27),
96 GPR_OFFSET_NAME(28),
97 GPR_OFFSET_NAME(29),
98 GPR_OFFSET_NAME(30),
99 GPR_OFFSET_NAME(31),
100 REG_OFFSET_NAME(nip),
101 REG_OFFSET_NAME(msr),
102 REG_OFFSET_NAME(ctr),
103 REG_OFFSET_NAME(link),
104 REG_OFFSET_NAME(xer),
105 REG_OFFSET_NAME(ccr),
106#ifdef CONFIG_PPC64
107 REG_OFFSET_NAME(softe),
108#else
109 REG_OFFSET_NAME(mq),
110#endif
111 REG_OFFSET_NAME(trap),
112 REG_OFFSET_NAME(dar),
113 REG_OFFSET_NAME(dsisr),
114 REG_OFFSET_END,
115};
116
117/**
118 * regs_query_register_offset() - query register offset from its name
119 * @name: the name of a register
120 *
121 * regs_query_register_offset() returns the offset of a register in struct
122 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
123 */
124int regs_query_register_offset(const char *name)
125{
126 const struct pt_regs_offset *roff;
127 for (roff = regoffset_table; roff->name != NULL; roff++)
128 if (!strcmp(roff->name, name))
129 return roff->offset;
130 return -EINVAL;
131}
132
133/**
134 * regs_query_register_name() - query register name from its offset
135 * @offset: the offset of a register in struct pt_regs.
136 *
137 * regs_query_register_name() returns the name of a register from its
138 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
139 */
140const char *regs_query_register_name(unsigned int offset)
141{
142 const struct pt_regs_offset *roff;
143 for (roff = regoffset_table; roff->name != NULL; roff++)
144 if (roff->offset == offset)
145 return roff->name;
146 return NULL;
147}
148
149/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * does not yet catch signals sent when the child dies.
151 * in exit.c or in signal.c.
152 */
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000155 * Set of msr bits that gdb can change on behalf of a process.
156 */
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000157#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000158#define MSR_DEBUGCHANGE 0
159#else
160#define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
161#endif
162
163/*
164 * Max register writeable via put_reg
165 */
166#ifdef CONFIG_PPC32
167#define PT_MAX_PUT_REG PT_MQ
168#else
169#define PT_MAX_PUT_REG PT_CCR
170#endif
171
Roland McGrath26f77132007-12-20 03:57:51 -0800172static unsigned long get_user_msr(struct task_struct *task)
173{
174 return task->thread.regs->msr | task->thread.fpexc_mode;
175}
176
177static int set_user_msr(struct task_struct *task, unsigned long msr)
178{
179 task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
180 task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
181 return 0;
182}
183
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000184#ifdef CONFIG_PPC64
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000185static int get_user_dscr(struct task_struct *task, unsigned long *data)
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000186{
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000187 *data = task->thread.dscr;
188 return 0;
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000189}
190
191static int set_user_dscr(struct task_struct *task, unsigned long dscr)
192{
193 task->thread.dscr = dscr;
194 task->thread.dscr_inherit = 1;
195 return 0;
196}
197#else
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000198static int get_user_dscr(struct task_struct *task, unsigned long *data)
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000199{
200 return -EIO;
201}
202
203static int set_user_dscr(struct task_struct *task, unsigned long dscr)
204{
205 return -EIO;
206}
207#endif
208
Roland McGrath26f77132007-12-20 03:57:51 -0800209/*
210 * We prevent mucking around with the reserved area of trap
211 * which are used internally by the kernel.
212 */
213static int set_user_trap(struct task_struct *task, unsigned long trap)
214{
215 task->thread.regs->trap = trap & 0xfff0;
216 return 0;
217}
218
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +1000219/*
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000220 * Get contents of register REGNO in task TASK.
221 */
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000222int ptrace_get_reg(struct task_struct *task, int regno, unsigned long *data)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000223{
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000224 if ((task->thread.regs == NULL) || !data)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000225 return -EIO;
226
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000227 if (regno == PT_MSR) {
228 *data = get_user_msr(task);
229 return 0;
230 }
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000231
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000232 if (regno == PT_DSCR)
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000233 return get_user_dscr(task, data);
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000234
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000235 if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long))) {
236 *data = ((unsigned long *)task->thread.regs)[regno];
237 return 0;
238 }
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000239
240 return -EIO;
241}
242
243/*
244 * Write contents of register REGNO in task TASK.
245 */
246int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
247{
248 if (task->thread.regs == NULL)
249 return -EIO;
250
Roland McGrath26f77132007-12-20 03:57:51 -0800251 if (regno == PT_MSR)
252 return set_user_msr(task, data);
253 if (regno == PT_TRAP)
254 return set_user_trap(task, data);
Alexey Kardashevskiy1715a822013-01-10 20:29:09 +0000255 if (regno == PT_DSCR)
256 return set_user_dscr(task, data);
Roland McGrath26f77132007-12-20 03:57:51 -0800257
258 if (regno <= PT_MAX_PUT_REG) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000259 ((unsigned long *)task->thread.regs)[regno] = data;
260 return 0;
261 }
262 return -EIO;
263}
264
Roland McGrath44dd3f52007-12-20 03:57:55 -0800265static int gpr_get(struct task_struct *target, const struct user_regset *regset,
266 unsigned int pos, unsigned int count,
267 void *kbuf, void __user *ubuf)
268{
Mike Wolfa71f5d52011-03-21 11:14:53 +1100269 int i, ret;
Roland McGrath44dd3f52007-12-20 03:57:55 -0800270
271 if (target->thread.regs == NULL)
272 return -EIO;
273
Mike Wolfa71f5d52011-03-21 11:14:53 +1100274 if (!FULL_REGS(target->thread.regs)) {
275 /* We have a partial register set. Fill 14-31 with bogus values */
276 for (i = 14; i < 32; i++)
277 target->thread.regs->gpr[i] = NV_REG_POISON;
278 }
Roland McGrath44dd3f52007-12-20 03:57:55 -0800279
280 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
281 target->thread.regs,
282 0, offsetof(struct pt_regs, msr));
283 if (!ret) {
284 unsigned long msr = get_user_msr(target);
285 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
286 offsetof(struct pt_regs, msr),
287 offsetof(struct pt_regs, msr) +
288 sizeof(msr));
289 }
290
291 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
292 offsetof(struct pt_regs, msr) + sizeof(long));
293
294 if (!ret)
295 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
296 &target->thread.regs->orig_gpr3,
297 offsetof(struct pt_regs, orig_gpr3),
298 sizeof(struct pt_regs));
299 if (!ret)
300 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
301 sizeof(struct pt_regs), -1);
302
303 return ret;
304}
305
306static int gpr_set(struct task_struct *target, const struct user_regset *regset,
307 unsigned int pos, unsigned int count,
308 const void *kbuf, const void __user *ubuf)
309{
310 unsigned long reg;
311 int ret;
312
313 if (target->thread.regs == NULL)
314 return -EIO;
315
316 CHECK_FULL_REGS(target->thread.regs);
317
318 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
319 target->thread.regs,
320 0, PT_MSR * sizeof(reg));
321
322 if (!ret && count > 0) {
323 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
324 PT_MSR * sizeof(reg),
325 (PT_MSR + 1) * sizeof(reg));
326 if (!ret)
327 ret = set_user_msr(target, reg);
328 }
329
330 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
331 offsetof(struct pt_regs, msr) + sizeof(long));
332
333 if (!ret)
334 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
335 &target->thread.regs->orig_gpr3,
336 PT_ORIG_R3 * sizeof(reg),
337 (PT_MAX_PUT_REG + 1) * sizeof(reg));
338
339 if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
340 ret = user_regset_copyin_ignore(
341 &pos, &count, &kbuf, &ubuf,
342 (PT_MAX_PUT_REG + 1) * sizeof(reg),
343 PT_TRAP * sizeof(reg));
344
345 if (!ret && count > 0) {
346 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
347 PT_TRAP * sizeof(reg),
348 (PT_TRAP + 1) * sizeof(reg));
349 if (!ret)
350 ret = set_user_trap(target, reg);
351 }
352
353 if (!ret)
354 ret = user_regset_copyin_ignore(
355 &pos, &count, &kbuf, &ubuf,
356 (PT_TRAP + 1) * sizeof(reg), -1);
357
358 return ret;
359}
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000360
Anshuman Khandual1ec85492016-07-28 10:57:32 +0800361/*
362 * When the transaction is active, 'transact_fp' holds the current running
363 * value of all FPR registers and 'fp_state' holds the last checkpointed
364 * value of all FPR registers for the current transaction. When transaction
365 * is not active 'fp_state' holds the current running state of all the FPR
366 * registers. So this function which returns the current running values of
367 * all the FPR registers, needs to know whether any transaction is active
368 * or not.
369 *
370 * Userspace interface buffer layout:
371 *
372 * struct data {
373 * u64 fpr[32];
374 * u64 fpscr;
375 * };
376 *
377 * There are two config options CONFIG_VSX and CONFIG_PPC_TRANSACTIONAL_MEM
378 * which determines the final code in this function. All the combinations of
379 * these two config options are possible except the one below as transactional
380 * memory config pulls in CONFIG_VSX automatically.
381 *
382 * !defined(CONFIG_VSX) && defined(CONFIG_PPC_TRANSACTIONAL_MEM)
383 */
Roland McGrathf65255e2007-12-20 03:57:34 -0800384static int fpr_get(struct task_struct *target, const struct user_regset *regset,
385 unsigned int pos, unsigned int count,
386 void *kbuf, void __user *ubuf)
387{
Michael Neulingc6e67712008-06-25 14:07:18 +1000388#ifdef CONFIG_VSX
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000389 u64 buf[33];
Michael Neulingc6e67712008-06-25 14:07:18 +1000390 int i;
391#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800392 flush_fp_to_thread(target);
393
Anshuman Khandual1ec85492016-07-28 10:57:32 +0800394#if defined(CONFIG_VSX) && defined(CONFIG_PPC_TRANSACTIONAL_MEM)
395 /* copy to local buffer then write that out */
396 if (MSR_TM_ACTIVE(target->thread.regs->msr)) {
397 flush_altivec_to_thread(target);
398 flush_tmregs_to_thread(target);
399 for (i = 0; i < 32 ; i++)
400 buf[i] = target->thread.TS_TRANS_FPR(i);
401 buf[32] = target->thread.transact_fp.fpscr;
402 } else {
403 for (i = 0; i < 32 ; i++)
404 buf[i] = target->thread.TS_FPR(i);
405 buf[32] = target->thread.fp_state.fpscr;
406 }
407 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
408#endif
409
410#if defined(CONFIG_VSX) && !defined(CONFIG_PPC_TRANSACTIONAL_MEM)
Michael Neulingc6e67712008-06-25 14:07:18 +1000411 /* copy to local buffer then write that out */
412 for (i = 0; i < 32 ; i++)
413 buf[i] = target->thread.TS_FPR(i);
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000414 buf[32] = target->thread.fp_state.fpscr;
Michael Neulingc6e67712008-06-25 14:07:18 +1000415 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
Anshuman Khandual1ec85492016-07-28 10:57:32 +0800416#endif
Michael Neulingc6e67712008-06-25 14:07:18 +1000417
Anshuman Khandual1ec85492016-07-28 10:57:32 +0800418#if !defined(CONFIG_VSX) && !defined(CONFIG_PPC_TRANSACTIONAL_MEM)
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000419 BUILD_BUG_ON(offsetof(struct thread_fp_state, fpscr) !=
Khem Raj1e407ee2016-04-25 09:19:17 -0700420 offsetof(struct thread_fp_state, fpr[32]));
Roland McGrathf65255e2007-12-20 03:57:34 -0800421
422 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000423 &target->thread.fp_state, 0, -1);
Michael Neulingc6e67712008-06-25 14:07:18 +1000424#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800425}
426
Anshuman Khandual1ec85492016-07-28 10:57:32 +0800427/*
428 * When the transaction is active, 'transact_fp' holds the current running
429 * value of all FPR registers and 'fp_state' holds the last checkpointed
430 * value of all FPR registers for the current transaction. When transaction
431 * is not active 'fp_state' holds the current running state of all the FPR
432 * registers. So this function which setss the current running values of
433 * all the FPR registers, needs to know whether any transaction is active
434 * or not.
435 *
436 * Userspace interface buffer layout:
437 *
438 * struct data {
439 * u64 fpr[32];
440 * u64 fpscr;
441 * };
442 *
443 * There are two config options CONFIG_VSX and CONFIG_PPC_TRANSACTIONAL_MEM
444 * which determines the final code in this function. All the combinations of
445 * these two config options are possible except the one below as transactional
446 * memory config pulls in CONFIG_VSX automatically.
447 *
448 * !defined(CONFIG_VSX) && defined(CONFIG_PPC_TRANSACTIONAL_MEM)
449 */
Roland McGrathf65255e2007-12-20 03:57:34 -0800450static int fpr_set(struct task_struct *target, const struct user_regset *regset,
451 unsigned int pos, unsigned int count,
452 const void *kbuf, const void __user *ubuf)
453{
Michael Neulingc6e67712008-06-25 14:07:18 +1000454#ifdef CONFIG_VSX
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000455 u64 buf[33];
Michael Neulingc6e67712008-06-25 14:07:18 +1000456 int i;
457#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800458 flush_fp_to_thread(target);
459
Anshuman Khandual1ec85492016-07-28 10:57:32 +0800460#if defined(CONFIG_VSX) && defined(CONFIG_PPC_TRANSACTIONAL_MEM)
461 /* copy to local buffer then write that out */
462 i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
463 if (i)
464 return i;
465
466 if (MSR_TM_ACTIVE(target->thread.regs->msr)) {
467 flush_altivec_to_thread(target);
468 flush_tmregs_to_thread(target);
469 for (i = 0; i < 32 ; i++)
470 target->thread.TS_TRANS_FPR(i) = buf[i];
471 target->thread.transact_fp.fpscr = buf[32];
472 } else {
473 for (i = 0; i < 32 ; i++)
474 target->thread.TS_FPR(i) = buf[i];
475 target->thread.fp_state.fpscr = buf[32];
476 }
477 return 0;
478#endif
479
480#if defined(CONFIG_VSX) && !defined(CONFIG_PPC_TRANSACTIONAL_MEM)
Michael Neulingc6e67712008-06-25 14:07:18 +1000481 /* copy to local buffer then write that out */
482 i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
483 if (i)
484 return i;
485 for (i = 0; i < 32 ; i++)
486 target->thread.TS_FPR(i) = buf[i];
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000487 target->thread.fp_state.fpscr = buf[32];
Michael Neulingc6e67712008-06-25 14:07:18 +1000488 return 0;
Anshuman Khandual1ec85492016-07-28 10:57:32 +0800489#endif
490
491#if !defined(CONFIG_VSX) && !defined(CONFIG_PPC_TRANSACTIONAL_MEM)
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000492 BUILD_BUG_ON(offsetof(struct thread_fp_state, fpscr) !=
Khem Raj1e407ee2016-04-25 09:19:17 -0700493 offsetof(struct thread_fp_state, fpr[32]));
Roland McGrathf65255e2007-12-20 03:57:34 -0800494
495 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000496 &target->thread.fp_state, 0, -1);
Michael Neulingc6e67712008-06-25 14:07:18 +1000497#endif
Roland McGrathf65255e2007-12-20 03:57:34 -0800498}
499
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000500#ifdef CONFIG_ALTIVEC
501/*
502 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
503 * The transfer totals 34 quadword. Quadwords 0-31 contain the
504 * corresponding vector registers. Quadword 32 contains the vscr as the
505 * last word (offset 12) within that quadword. Quadword 33 contains the
506 * vrsave as the first word (offset 0) within the quadword.
507 *
508 * This definition of the VMX state is compatible with the current PPC32
509 * ptrace interface. This allows signal handling and ptrace to use the
510 * same structures. This also simplifies the implementation of a bi-arch
511 * (combined (32- and 64-bit) gdb.
512 */
513
Roland McGrath3caf06c2007-12-20 03:57:39 -0800514static int vr_active(struct task_struct *target,
515 const struct user_regset *regset)
516{
517 flush_altivec_to_thread(target);
518 return target->thread.used_vr ? regset->n : 0;
519}
520
521static int vr_get(struct task_struct *target, const struct user_regset *regset,
522 unsigned int pos, unsigned int count,
523 void *kbuf, void __user *ubuf)
524{
525 int ret;
526
527 flush_altivec_to_thread(target);
528
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000529 BUILD_BUG_ON(offsetof(struct thread_vr_state, vscr) !=
530 offsetof(struct thread_vr_state, vr[32]));
Roland McGrath3caf06c2007-12-20 03:57:39 -0800531
532 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000533 &target->thread.vr_state, 0,
Roland McGrath3caf06c2007-12-20 03:57:39 -0800534 33 * sizeof(vector128));
535 if (!ret) {
536 /*
537 * Copy out only the low-order word of vrsave.
538 */
539 union {
540 elf_vrreg_t reg;
541 u32 word;
542 } vrsave;
543 memset(&vrsave, 0, sizeof(vrsave));
544 vrsave.word = target->thread.vrsave;
545 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
546 33 * sizeof(vector128), -1);
547 }
548
549 return ret;
550}
551
552static int vr_set(struct task_struct *target, const struct user_regset *regset,
553 unsigned int pos, unsigned int count,
554 const void *kbuf, const void __user *ubuf)
555{
556 int ret;
557
558 flush_altivec_to_thread(target);
559
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000560 BUILD_BUG_ON(offsetof(struct thread_vr_state, vscr) !=
561 offsetof(struct thread_vr_state, vr[32]));
Roland McGrath3caf06c2007-12-20 03:57:39 -0800562
563 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000564 &target->thread.vr_state, 0,
565 33 * sizeof(vector128));
Roland McGrath3caf06c2007-12-20 03:57:39 -0800566 if (!ret && count > 0) {
567 /*
568 * We use only the first word of vrsave.
569 */
570 union {
571 elf_vrreg_t reg;
572 u32 word;
573 } vrsave;
574 memset(&vrsave, 0, sizeof(vrsave));
575 vrsave.word = target->thread.vrsave;
576 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
577 33 * sizeof(vector128), -1);
578 if (!ret)
579 target->thread.vrsave = vrsave.word;
580 }
581
582 return ret;
583}
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000584#endif /* CONFIG_ALTIVEC */
585
Michael Neulingce48b212008-06-25 14:07:18 +1000586#ifdef CONFIG_VSX
587/*
588 * Currently to set and and get all the vsx state, you need to call
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300589 * the fp and VMX calls as well. This only get/sets the lower 32
Michael Neulingce48b212008-06-25 14:07:18 +1000590 * 128bit VSX registers.
591 */
592
593static int vsr_active(struct task_struct *target,
594 const struct user_regset *regset)
595{
596 flush_vsx_to_thread(target);
597 return target->thread.used_vsr ? regset->n : 0;
598}
599
600static int vsr_get(struct task_struct *target, const struct user_regset *regset,
601 unsigned int pos, unsigned int count,
602 void *kbuf, void __user *ubuf)
603{
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000604 u64 buf[32];
Michael Neulingf3e909c2008-07-01 14:01:39 +1000605 int ret, i;
Michael Neulingce48b212008-06-25 14:07:18 +1000606
607 flush_vsx_to_thread(target);
608
Michael Neulingf3e909c2008-07-01 14:01:39 +1000609 for (i = 0; i < 32 ; i++)
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000610 buf[i] = target->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
Michael Neulingce48b212008-06-25 14:07:18 +1000611 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Michael Neulingf3e909c2008-07-01 14:01:39 +1000612 buf, 0, 32 * sizeof(double));
Michael Neulingce48b212008-06-25 14:07:18 +1000613
614 return ret;
615}
616
617static int vsr_set(struct task_struct *target, const struct user_regset *regset,
618 unsigned int pos, unsigned int count,
619 const void *kbuf, const void __user *ubuf)
620{
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000621 u64 buf[32];
Michael Neulingf3e909c2008-07-01 14:01:39 +1000622 int ret,i;
Michael Neulingce48b212008-06-25 14:07:18 +1000623
624 flush_vsx_to_thread(target);
625
626 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Michael Neulingf3e909c2008-07-01 14:01:39 +1000627 buf, 0, 32 * sizeof(double));
628 for (i = 0; i < 32 ; i++)
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000629 target->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
Michael Neulingf3e909c2008-07-01 14:01:39 +1000630
Michael Neulingce48b212008-06-25 14:07:18 +1000631
632 return ret;
633}
634#endif /* CONFIG_VSX */
635
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000636#ifdef CONFIG_SPE
637
638/*
639 * For get_evrregs/set_evrregs functions 'data' has the following layout:
640 *
641 * struct {
642 * u32 evr[32];
643 * u64 acc;
644 * u32 spefscr;
645 * }
646 */
647
Roland McGratha4e4b172007-12-20 03:57:48 -0800648static int evr_active(struct task_struct *target,
649 const struct user_regset *regset)
650{
651 flush_spe_to_thread(target);
652 return target->thread.used_spe ? regset->n : 0;
653}
654
655static int evr_get(struct task_struct *target, const struct user_regset *regset,
656 unsigned int pos, unsigned int count,
657 void *kbuf, void __user *ubuf)
658{
659 int ret;
660
661 flush_spe_to_thread(target);
662
663 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
664 &target->thread.evr,
665 0, sizeof(target->thread.evr));
666
667 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
668 offsetof(struct thread_struct, spefscr));
669
670 if (!ret)
671 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
672 &target->thread.acc,
673 sizeof(target->thread.evr), -1);
674
675 return ret;
676}
677
678static int evr_set(struct task_struct *target, const struct user_regset *regset,
679 unsigned int pos, unsigned int count,
680 const void *kbuf, const void __user *ubuf)
681{
682 int ret;
683
684 flush_spe_to_thread(target);
685
686 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
687 &target->thread.evr,
688 0, sizeof(target->thread.evr));
689
690 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
691 offsetof(struct thread_struct, spefscr));
692
693 if (!ret)
694 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
695 &target->thread.acc,
696 sizeof(target->thread.evr), -1);
697
698 return ret;
699}
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000700#endif /* CONFIG_SPE */
701
702
Roland McGrath80fdf472007-12-20 03:58:00 -0800703/*
704 * These are our native regset flavors.
705 */
706enum powerpc_regset {
707 REGSET_GPR,
708 REGSET_FPR,
709#ifdef CONFIG_ALTIVEC
710 REGSET_VMX,
711#endif
Michael Neulingce48b212008-06-25 14:07:18 +1000712#ifdef CONFIG_VSX
713 REGSET_VSX,
714#endif
Roland McGrath80fdf472007-12-20 03:58:00 -0800715#ifdef CONFIG_SPE
716 REGSET_SPE,
717#endif
718};
719
720static const struct user_regset native_regsets[] = {
721 [REGSET_GPR] = {
722 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
723 .size = sizeof(long), .align = sizeof(long),
724 .get = gpr_get, .set = gpr_set
725 },
726 [REGSET_FPR] = {
727 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
728 .size = sizeof(double), .align = sizeof(double),
729 .get = fpr_get, .set = fpr_set
730 },
731#ifdef CONFIG_ALTIVEC
732 [REGSET_VMX] = {
733 .core_note_type = NT_PPC_VMX, .n = 34,
734 .size = sizeof(vector128), .align = sizeof(vector128),
735 .active = vr_active, .get = vr_get, .set = vr_set
736 },
737#endif
Michael Neulingce48b212008-06-25 14:07:18 +1000738#ifdef CONFIG_VSX
739 [REGSET_VSX] = {
Michael Neulingf3e909c2008-07-01 14:01:39 +1000740 .core_note_type = NT_PPC_VSX, .n = 32,
741 .size = sizeof(double), .align = sizeof(double),
Michael Neulingce48b212008-06-25 14:07:18 +1000742 .active = vsr_active, .get = vsr_get, .set = vsr_set
743 },
744#endif
Roland McGrath80fdf472007-12-20 03:58:00 -0800745#ifdef CONFIG_SPE
746 [REGSET_SPE] = {
Suzuki Poulosea0b38b42013-08-27 13:22:14 +0530747 .core_note_type = NT_PPC_SPE, .n = 35,
Roland McGrath80fdf472007-12-20 03:58:00 -0800748 .size = sizeof(u32), .align = sizeof(u32),
749 .active = evr_active, .get = evr_get, .set = evr_set
750 },
751#endif
752};
753
754static const struct user_regset_view user_ppc_native_view = {
755 .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
756 .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
757};
758
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800759#ifdef CONFIG_PPC64
760#include <linux/compat.h>
761
762static int gpr32_get(struct task_struct *target,
763 const struct user_regset *regset,
764 unsigned int pos, unsigned int count,
765 void *kbuf, void __user *ubuf)
766{
767 const unsigned long *regs = &target->thread.regs->gpr[0];
768 compat_ulong_t *k = kbuf;
769 compat_ulong_t __user *u = ubuf;
770 compat_ulong_t reg;
Mike Wolfa71f5d52011-03-21 11:14:53 +1100771 int i;
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800772
773 if (target->thread.regs == NULL)
774 return -EIO;
775
Mike Wolfa71f5d52011-03-21 11:14:53 +1100776 if (!FULL_REGS(target->thread.regs)) {
777 /* We have a partial register set. Fill 14-31 with bogus values */
778 for (i = 14; i < 32; i++)
779 target->thread.regs->gpr[i] = NV_REG_POISON;
780 }
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800781
782 pos /= sizeof(reg);
783 count /= sizeof(reg);
784
785 if (kbuf)
786 for (; count > 0 && pos < PT_MSR; --count)
787 *k++ = regs[pos++];
788 else
789 for (; count > 0 && pos < PT_MSR; --count)
790 if (__put_user((compat_ulong_t) regs[pos++], u++))
791 return -EFAULT;
792
793 if (count > 0 && pos == PT_MSR) {
794 reg = get_user_msr(target);
795 if (kbuf)
796 *k++ = reg;
797 else if (__put_user(reg, u++))
798 return -EFAULT;
799 ++pos;
800 --count;
801 }
802
803 if (kbuf)
804 for (; count > 0 && pos < PT_REGS_COUNT; --count)
805 *k++ = regs[pos++];
806 else
807 for (; count > 0 && pos < PT_REGS_COUNT; --count)
808 if (__put_user((compat_ulong_t) regs[pos++], u++))
809 return -EFAULT;
810
811 kbuf = k;
812 ubuf = u;
813 pos *= sizeof(reg);
814 count *= sizeof(reg);
815 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
816 PT_REGS_COUNT * sizeof(reg), -1);
817}
818
819static int gpr32_set(struct task_struct *target,
820 const struct user_regset *regset,
821 unsigned int pos, unsigned int count,
822 const void *kbuf, const void __user *ubuf)
823{
824 unsigned long *regs = &target->thread.regs->gpr[0];
825 const compat_ulong_t *k = kbuf;
826 const compat_ulong_t __user *u = ubuf;
827 compat_ulong_t reg;
828
829 if (target->thread.regs == NULL)
830 return -EIO;
831
832 CHECK_FULL_REGS(target->thread.regs);
833
834 pos /= sizeof(reg);
835 count /= sizeof(reg);
836
837 if (kbuf)
838 for (; count > 0 && pos < PT_MSR; --count)
839 regs[pos++] = *k++;
840 else
841 for (; count > 0 && pos < PT_MSR; --count) {
842 if (__get_user(reg, u++))
843 return -EFAULT;
844 regs[pos++] = reg;
845 }
846
847
848 if (count > 0 && pos == PT_MSR) {
849 if (kbuf)
850 reg = *k++;
851 else if (__get_user(reg, u++))
852 return -EFAULT;
853 set_user_msr(target, reg);
854 ++pos;
855 --count;
856 }
857
Roland McGrathc2372eb2008-03-13 19:25:35 +1100858 if (kbuf) {
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800859 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
860 regs[pos++] = *k++;
Roland McGrathc2372eb2008-03-13 19:25:35 +1100861 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
862 ++k;
863 } else {
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800864 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
865 if (__get_user(reg, u++))
866 return -EFAULT;
867 regs[pos++] = reg;
868 }
Roland McGrathc2372eb2008-03-13 19:25:35 +1100869 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
870 if (__get_user(reg, u++))
871 return -EFAULT;
872 }
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800873
874 if (count > 0 && pos == PT_TRAP) {
875 if (kbuf)
876 reg = *k++;
877 else if (__get_user(reg, u++))
878 return -EFAULT;
879 set_user_trap(target, reg);
880 ++pos;
881 --count;
882 }
883
884 kbuf = k;
885 ubuf = u;
886 pos *= sizeof(reg);
887 count *= sizeof(reg);
888 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
889 (PT_TRAP + 1) * sizeof(reg), -1);
890}
891
892/*
893 * These are the regset flavors matching the CONFIG_PPC32 native set.
894 */
895static const struct user_regset compat_regsets[] = {
896 [REGSET_GPR] = {
897 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
898 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
899 .get = gpr32_get, .set = gpr32_set
900 },
901 [REGSET_FPR] = {
902 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
903 .size = sizeof(double), .align = sizeof(double),
904 .get = fpr_get, .set = fpr_set
905 },
906#ifdef CONFIG_ALTIVEC
907 [REGSET_VMX] = {
908 .core_note_type = NT_PPC_VMX, .n = 34,
909 .size = sizeof(vector128), .align = sizeof(vector128),
910 .active = vr_active, .get = vr_get, .set = vr_set
911 },
912#endif
913#ifdef CONFIG_SPE
914 [REGSET_SPE] = {
Roland McGrath24f1a842008-01-02 17:05:48 -0800915 .core_note_type = NT_PPC_SPE, .n = 35,
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800916 .size = sizeof(u32), .align = sizeof(u32),
917 .active = evr_active, .get = evr_get, .set = evr_set
918 },
919#endif
920};
921
922static const struct user_regset_view user_ppc_compat_view = {
923 .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
924 .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
925};
926#endif /* CONFIG_PPC64 */
927
Roland McGrath80fdf472007-12-20 03:58:00 -0800928const struct user_regset_view *task_user_regset_view(struct task_struct *task)
929{
Roland McGrathfa8f5cb2007-12-20 03:58:08 -0800930#ifdef CONFIG_PPC64
931 if (test_tsk_thread_flag(task, TIF_32BIT))
932 return &user_ppc_compat_view;
933#endif
Roland McGrath80fdf472007-12-20 03:58:00 -0800934 return &user_ppc_native_view;
935}
936
937
Roland McGrath2a84b0d2008-01-30 13:30:51 +0100938void user_enable_single_step(struct task_struct *task)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000939{
940 struct pt_regs *regs = task->thread.regs;
941
942 if (regs != NULL) {
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000943#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530944 task->thread.debug.dbcr0 &= ~DBCR0_BT;
945 task->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000946 regs->msr |= MSR_DE;
947#else
Roland McGrathec097c82009-05-28 21:26:38 +0000948 regs->msr &= ~MSR_BE;
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000949 regs->msr |= MSR_SE;
950#endif
951 }
952 set_tsk_thread_flag(task, TIF_SINGLESTEP);
953}
954
Roland McGrathec097c82009-05-28 21:26:38 +0000955void user_enable_block_step(struct task_struct *task)
956{
957 struct pt_regs *regs = task->thread.regs;
958
959 if (regs != NULL) {
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000960#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530961 task->thread.debug.dbcr0 &= ~DBCR0_IC;
962 task->thread.debug.dbcr0 = DBCR0_IDM | DBCR0_BT;
Roland McGrathec097c82009-05-28 21:26:38 +0000963 regs->msr |= MSR_DE;
964#else
965 regs->msr &= ~MSR_SE;
966 regs->msr |= MSR_BE;
967#endif
968 }
969 set_tsk_thread_flag(task, TIF_SINGLESTEP);
970}
971
Roland McGrath2a84b0d2008-01-30 13:30:51 +0100972void user_disable_single_step(struct task_struct *task)
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000973{
974 struct pt_regs *regs = task->thread.regs;
975
976 if (regs != NULL) {
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000977#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000978 /*
979 * The logic to disable single stepping should be as
980 * simple as turning off the Instruction Complete flag.
981 * And, after doing so, if all debug flags are off, turn
982 * off DBCR0(IDM) and MSR(DE) .... Torez
983 */
James Yang682775b2013-07-05 14:49:43 -0500984 task->thread.debug.dbcr0 &= ~(DBCR0_IC|DBCR0_BT);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000985 /*
986 * Test to see if any of the DBCR_ACTIVE_EVENTS bits are set.
987 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530988 if (!DBCR_ACTIVE_EVENTS(task->thread.debug.dbcr0,
989 task->thread.debug.dbcr1)) {
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000990 /*
991 * All debug events were off.....
992 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530993 task->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp28477fb2009-07-08 13:46:18 +0000994 regs->msr &= ~MSR_DE;
995 }
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000996#else
Roland McGrathec097c82009-05-28 21:26:38 +0000997 regs->msr &= ~(MSR_SE | MSR_BE);
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000998#endif
999 }
1000 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
1001}
1002
K.Prasad5aae8a52010-06-15 11:35:19 +05301003#ifdef CONFIG_HAVE_HW_BREAKPOINT
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001004void ptrace_triggered(struct perf_event *bp,
K.Prasad5aae8a52010-06-15 11:35:19 +05301005 struct perf_sample_data *data, struct pt_regs *regs)
1006{
1007 struct perf_event_attr attr;
1008
1009 /*
1010 * Disable the breakpoint request here since ptrace has defined a
1011 * one-shot behaviour for breakpoint exceptions in PPC64.
1012 * The SIGTRAP signal is generated automatically for us in do_dabr().
1013 * We don't have to do anything about that here
1014 */
1015 attr = bp->attr;
1016 attr.disabled = true;
1017 modify_user_hw_breakpoint(bp, &attr);
1018}
1019#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1020
Anton Blancharde51df2c2014-08-20 08:55:18 +10001021static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001022 unsigned long data)
1023{
K.Prasad5aae8a52010-06-15 11:35:19 +05301024#ifdef CONFIG_HAVE_HW_BREAKPOINT
1025 int ret;
1026 struct thread_struct *thread = &(task->thread);
1027 struct perf_event *bp;
1028 struct perf_event_attr attr;
1029#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Michael Neuling9422de32012-12-20 14:06:44 +00001030#ifndef CONFIG_PPC_ADV_DEBUG_REGS
1031 struct arch_hw_breakpoint hw_brk;
1032#endif
K.Prasad5aae8a52010-06-15 11:35:19 +05301033
Luis Machadod6a61bf2008-07-24 02:10:41 +10001034 /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
1035 * For embedded processors we support one DAC and no IAC's at the
1036 * moment.
1037 */
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001038 if (addr > 0)
1039 return -EINVAL;
1040
Kumar Gala2325f0a2008-07-26 05:27:33 +10001041 /* The bottom 3 bits in dabr are flags */
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001042 if ((data & ~0x7UL) >= TASK_SIZE)
1043 return -EIO;
1044
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001045#ifndef CONFIG_PPC_ADV_DEBUG_REGS
Luis Machadod6a61bf2008-07-24 02:10:41 +10001046 /* For processors using DABR (i.e. 970), the bottom 3 bits are flags.
1047 * It was assumed, on previous implementations, that 3 bits were
1048 * passed together with the data address, fitting the design of the
1049 * DABR register, as follows:
1050 *
1051 * bit 0: Read flag
1052 * bit 1: Write flag
1053 * bit 2: Breakpoint translation
1054 *
1055 * Thus, we use them here as so.
1056 */
1057
1058 /* Ensure breakpoint translation bit is set */
Michael Neuling9422de32012-12-20 14:06:44 +00001059 if (data && !(data & HW_BRK_TYPE_TRANSLATE))
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001060 return -EIO;
Michael Neuling9422de32012-12-20 14:06:44 +00001061 hw_brk.address = data & (~HW_BRK_TYPE_DABR);
1062 hw_brk.type = (data & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
1063 hw_brk.len = 8;
K.Prasad5aae8a52010-06-15 11:35:19 +05301064#ifdef CONFIG_HAVE_HW_BREAKPOINT
1065 bp = thread->ptrace_bps[0];
Michael Neuling9422de32012-12-20 14:06:44 +00001066 if ((!data) || !(hw_brk.type & HW_BRK_TYPE_RDWR)) {
K.Prasad5aae8a52010-06-15 11:35:19 +05301067 if (bp) {
1068 unregister_hw_breakpoint(bp);
1069 thread->ptrace_bps[0] = NULL;
1070 }
1071 return 0;
1072 }
1073 if (bp) {
1074 attr = bp->attr;
Michael Neuling9422de32012-12-20 14:06:44 +00001075 attr.bp_addr = hw_brk.address;
1076 arch_bp_generic_fields(hw_brk.type, &attr.bp_type);
Aravinda Prasada53fd612012-11-04 22:15:28 +00001077
1078 /* Enable breakpoint */
1079 attr.disabled = false;
1080
K.Prasad5aae8a52010-06-15 11:35:19 +05301081 ret = modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker925f83c2011-05-06 01:53:18 +02001082 if (ret) {
K.Prasad5aae8a52010-06-15 11:35:19 +05301083 return ret;
Frederic Weisbecker925f83c2011-05-06 01:53:18 +02001084 }
K.Prasad5aae8a52010-06-15 11:35:19 +05301085 thread->ptrace_bps[0] = bp;
Michael Neuling9422de32012-12-20 14:06:44 +00001086 thread->hw_brk = hw_brk;
K.Prasad5aae8a52010-06-15 11:35:19 +05301087 return 0;
1088 }
1089
1090 /* Create a new breakpoint request if one doesn't exist already */
1091 hw_breakpoint_init(&attr);
Michael Neuling9422de32012-12-20 14:06:44 +00001092 attr.bp_addr = hw_brk.address;
1093 arch_bp_generic_fields(hw_brk.type,
1094 &attr.bp_type);
K.Prasad5aae8a52010-06-15 11:35:19 +05301095
1096 thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
Avi Kivity4dc0da82011-06-29 18:42:35 +03001097 ptrace_triggered, NULL, task);
K.Prasad5aae8a52010-06-15 11:35:19 +05301098 if (IS_ERR(bp)) {
1099 thread->ptrace_bps[0] = NULL;
1100 return PTR_ERR(bp);
1101 }
1102
1103#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Michael Neuling9422de32012-12-20 14:06:44 +00001104 task->thread.hw_brk = hw_brk;
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001105#else /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +10001106 /* As described above, it was assumed 3 bits were passed with the data
1107 * address, but we will assume only the mode bits will be passed
1108 * as to not cause alignment restrictions for DAC-based processors.
1109 */
1110
1111 /* DAC's hold the whole address without any mode flags */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301112 task->thread.debug.dac1 = data & ~0x3UL;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001113
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301114 if (task->thread.debug.dac1 == 0) {
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001115 dbcr_dac(task) &= ~(DBCR_DAC1R | DBCR_DAC1W);
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301116 if (!DBCR_ACTIVE_EVENTS(task->thread.debug.dbcr0,
1117 task->thread.debug.dbcr1)) {
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001118 task->thread.regs->msr &= ~MSR_DE;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301119 task->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001120 }
Luis Machadod6a61bf2008-07-24 02:10:41 +10001121 return 0;
1122 }
1123
1124 /* Read or Write bits must be set */
1125
1126 if (!(data & 0x3UL))
1127 return -EINVAL;
1128
1129 /* Set the Internal Debugging flag (IDM bit 1) for the DBCR0
1130 register */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301131 task->thread.debug.dbcr0 |= DBCR0_IDM;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001132
1133 /* Check for write and read flags and set DBCR0
1134 accordingly */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001135 dbcr_dac(task) &= ~(DBCR_DAC1R|DBCR_DAC1W);
Luis Machadod6a61bf2008-07-24 02:10:41 +10001136 if (data & 0x1UL)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001137 dbcr_dac(task) |= DBCR_DAC1R;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001138 if (data & 0x2UL)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001139 dbcr_dac(task) |= DBCR_DAC1W;
Luis Machadod6a61bf2008-07-24 02:10:41 +10001140 task->thread.regs->msr |= MSR_DE;
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001141#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001142 return 0;
1143}
Benjamin Herrenschmidtabd06502007-06-04 15:15:47 +10001144
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +10001145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * Called by kernel/ptrace.c when detaching..
1147 *
1148 * Make sure single step bits etc are not set.
1149 */
1150void ptrace_disable(struct task_struct *child)
1151{
1152 /* make sure the single step bit is not set. */
Roland McGrath2a84b0d2008-01-30 13:30:51 +01001153 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001156#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling84295df2012-10-28 15:13:16 +00001157static long set_instruction_bp(struct task_struct *child,
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001158 struct ppc_hw_breakpoint *bp_info)
1159{
1160 int slot;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301161 int slot1_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC1) != 0);
1162 int slot2_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC2) != 0);
1163 int slot3_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC3) != 0);
1164 int slot4_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC4) != 0);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001165
1166 if (dbcr_iac_range(child) & DBCR_IAC12MODE)
1167 slot2_in_use = 1;
1168 if (dbcr_iac_range(child) & DBCR_IAC34MODE)
1169 slot4_in_use = 1;
1170
1171 if (bp_info->addr >= TASK_SIZE)
1172 return -EIO;
1173
1174 if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) {
1175
1176 /* Make sure range is valid. */
1177 if (bp_info->addr2 >= TASK_SIZE)
1178 return -EIO;
1179
1180 /* We need a pair of IAC regsisters */
1181 if ((!slot1_in_use) && (!slot2_in_use)) {
1182 slot = 1;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301183 child->thread.debug.iac1 = bp_info->addr;
1184 child->thread.debug.iac2 = bp_info->addr2;
1185 child->thread.debug.dbcr0 |= DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001186 if (bp_info->addr_mode ==
1187 PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
1188 dbcr_iac_range(child) |= DBCR_IAC12X;
1189 else
1190 dbcr_iac_range(child) |= DBCR_IAC12I;
1191#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1192 } else if ((!slot3_in_use) && (!slot4_in_use)) {
1193 slot = 3;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301194 child->thread.debug.iac3 = bp_info->addr;
1195 child->thread.debug.iac4 = bp_info->addr2;
1196 child->thread.debug.dbcr0 |= DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001197 if (bp_info->addr_mode ==
1198 PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
1199 dbcr_iac_range(child) |= DBCR_IAC34X;
1200 else
1201 dbcr_iac_range(child) |= DBCR_IAC34I;
1202#endif
1203 } else
1204 return -ENOSPC;
1205 } else {
1206 /* We only need one. If possible leave a pair free in
1207 * case a range is needed later
1208 */
1209 if (!slot1_in_use) {
1210 /*
1211 * Don't use iac1 if iac1-iac2 are free and either
1212 * iac3 or iac4 (but not both) are free
1213 */
1214 if (slot2_in_use || (slot3_in_use == slot4_in_use)) {
1215 slot = 1;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301216 child->thread.debug.iac1 = bp_info->addr;
1217 child->thread.debug.dbcr0 |= DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001218 goto out;
1219 }
1220 }
1221 if (!slot2_in_use) {
1222 slot = 2;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301223 child->thread.debug.iac2 = bp_info->addr;
1224 child->thread.debug.dbcr0 |= DBCR0_IAC2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001225#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1226 } else if (!slot3_in_use) {
1227 slot = 3;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301228 child->thread.debug.iac3 = bp_info->addr;
1229 child->thread.debug.dbcr0 |= DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001230 } else if (!slot4_in_use) {
1231 slot = 4;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301232 child->thread.debug.iac4 = bp_info->addr;
1233 child->thread.debug.dbcr0 |= DBCR0_IAC4;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001234#endif
1235 } else
1236 return -ENOSPC;
1237 }
1238out:
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301239 child->thread.debug.dbcr0 |= DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001240 child->thread.regs->msr |= MSR_DE;
1241
1242 return slot;
1243}
1244
1245static int del_instruction_bp(struct task_struct *child, int slot)
1246{
1247 switch (slot) {
1248 case 1:
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301249 if ((child->thread.debug.dbcr0 & DBCR0_IAC1) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001250 return -ENOENT;
1251
1252 if (dbcr_iac_range(child) & DBCR_IAC12MODE) {
1253 /* address range - clear slots 1 & 2 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301254 child->thread.debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001255 dbcr_iac_range(child) &= ~DBCR_IAC12MODE;
1256 }
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301257 child->thread.debug.iac1 = 0;
1258 child->thread.debug.dbcr0 &= ~DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001259 break;
1260 case 2:
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301261 if ((child->thread.debug.dbcr0 & DBCR0_IAC2) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001262 return -ENOENT;
1263
1264 if (dbcr_iac_range(child) & DBCR_IAC12MODE)
1265 /* used in a range */
1266 return -EINVAL;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301267 child->thread.debug.iac2 = 0;
1268 child->thread.debug.dbcr0 &= ~DBCR0_IAC2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001269 break;
1270#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1271 case 3:
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301272 if ((child->thread.debug.dbcr0 & DBCR0_IAC3) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001273 return -ENOENT;
1274
1275 if (dbcr_iac_range(child) & DBCR_IAC34MODE) {
1276 /* address range - clear slots 3 & 4 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301277 child->thread.debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001278 dbcr_iac_range(child) &= ~DBCR_IAC34MODE;
1279 }
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301280 child->thread.debug.iac3 = 0;
1281 child->thread.debug.dbcr0 &= ~DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001282 break;
1283 case 4:
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301284 if ((child->thread.debug.dbcr0 & DBCR0_IAC4) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001285 return -ENOENT;
1286
1287 if (dbcr_iac_range(child) & DBCR_IAC34MODE)
1288 /* Used in a range */
1289 return -EINVAL;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301290 child->thread.debug.iac4 = 0;
1291 child->thread.debug.dbcr0 &= ~DBCR0_IAC4;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001292 break;
1293#endif
1294 default:
1295 return -EINVAL;
1296 }
1297 return 0;
1298}
1299
1300static int set_dac(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
1301{
1302 int byte_enable =
1303 (bp_info->condition_mode >> PPC_BREAKPOINT_CONDITION_BE_SHIFT)
1304 & 0xf;
1305 int condition_mode =
1306 bp_info->condition_mode & PPC_BREAKPOINT_CONDITION_MODE;
1307 int slot;
1308
1309 if (byte_enable && (condition_mode == 0))
1310 return -EINVAL;
1311
1312 if (bp_info->addr >= TASK_SIZE)
1313 return -EIO;
1314
1315 if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0) {
1316 slot = 1;
1317 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
1318 dbcr_dac(child) |= DBCR_DAC1R;
1319 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
1320 dbcr_dac(child) |= DBCR_DAC1W;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301321 child->thread.debug.dac1 = (unsigned long)bp_info->addr;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001322#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
1323 if (byte_enable) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301324 child->thread.debug.dvc1 =
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001325 (unsigned long)bp_info->condition_value;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301326 child->thread.debug.dbcr2 |=
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001327 ((byte_enable << DBCR2_DVC1BE_SHIFT) |
1328 (condition_mode << DBCR2_DVC1M_SHIFT));
1329 }
1330#endif
1331#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301332 } else if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001333 /* Both dac1 and dac2 are part of a range */
1334 return -ENOSPC;
1335#endif
1336 } else if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0) {
1337 slot = 2;
1338 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
1339 dbcr_dac(child) |= DBCR_DAC2R;
1340 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
1341 dbcr_dac(child) |= DBCR_DAC2W;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301342 child->thread.debug.dac2 = (unsigned long)bp_info->addr;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001343#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
1344 if (byte_enable) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301345 child->thread.debug.dvc2 =
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001346 (unsigned long)bp_info->condition_value;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301347 child->thread.debug.dbcr2 |=
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001348 ((byte_enable << DBCR2_DVC2BE_SHIFT) |
1349 (condition_mode << DBCR2_DVC2M_SHIFT));
1350 }
1351#endif
1352 } else
1353 return -ENOSPC;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301354 child->thread.debug.dbcr0 |= DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001355 child->thread.regs->msr |= MSR_DE;
1356
1357 return slot + 4;
1358}
1359
1360static int del_dac(struct task_struct *child, int slot)
1361{
1362 if (slot == 1) {
Dave Kleikamp30124d12010-03-01 04:57:34 +00001363 if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001364 return -ENOENT;
1365
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301366 child->thread.debug.dac1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001367 dbcr_dac(child) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1368#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301369 if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
1370 child->thread.debug.dac2 = 0;
1371 child->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001372 }
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301373 child->thread.debug.dbcr2 &= ~(DBCR2_DVC1M | DBCR2_DVC1BE);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001374#endif
1375#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301376 child->thread.debug.dvc1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001377#endif
1378 } else if (slot == 2) {
Dave Kleikamp30124d12010-03-01 04:57:34 +00001379 if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001380 return -ENOENT;
1381
1382#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301383 if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE)
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001384 /* Part of a range */
1385 return -EINVAL;
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301386 child->thread.debug.dbcr2 &= ~(DBCR2_DVC2M | DBCR2_DVC2BE);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001387#endif
1388#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301389 child->thread.debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001390#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301391 child->thread.debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001392 dbcr_dac(child) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1393 } else
1394 return -EINVAL;
1395
1396 return 0;
1397}
1398#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
1399
1400#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1401static int set_dac_range(struct task_struct *child,
1402 struct ppc_hw_breakpoint *bp_info)
1403{
1404 int mode = bp_info->addr_mode & PPC_BREAKPOINT_MODE_MASK;
1405
1406 /* We don't allow range watchpoints to be used with DVC */
1407 if (bp_info->condition_mode)
1408 return -EINVAL;
1409
1410 /*
1411 * Best effort to verify the address range. The user/supervisor bits
1412 * prevent trapping in kernel space, but let's fail on an obvious bad
1413 * range. The simple test on the mask is not fool-proof, and any
1414 * exclusive range will spill over into kernel space.
1415 */
1416 if (bp_info->addr >= TASK_SIZE)
1417 return -EIO;
1418 if (mode == PPC_BREAKPOINT_MODE_MASK) {
1419 /*
1420 * dac2 is a bitmask. Don't allow a mask that makes a
1421 * kernel space address from a valid dac1 value
1422 */
1423 if (~((unsigned long)bp_info->addr2) >= TASK_SIZE)
1424 return -EIO;
1425 } else {
1426 /*
1427 * For range breakpoints, addr2 must also be a valid address
1428 */
1429 if (bp_info->addr2 >= TASK_SIZE)
1430 return -EIO;
1431 }
1432
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301433 if (child->thread.debug.dbcr0 &
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001434 (DBCR0_DAC1R | DBCR0_DAC1W | DBCR0_DAC2R | DBCR0_DAC2W))
1435 return -ENOSPC;
1436
1437 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301438 child->thread.debug.dbcr0 |= (DBCR0_DAC1R | DBCR0_IDM);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001439 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301440 child->thread.debug.dbcr0 |= (DBCR0_DAC1W | DBCR0_IDM);
1441 child->thread.debug.dac1 = bp_info->addr;
1442 child->thread.debug.dac2 = bp_info->addr2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001443 if (mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301444 child->thread.debug.dbcr2 |= DBCR2_DAC12M;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001445 else if (mode == PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301446 child->thread.debug.dbcr2 |= DBCR2_DAC12MX;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001447 else /* PPC_BREAKPOINT_MODE_MASK */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301448 child->thread.debug.dbcr2 |= DBCR2_DAC12MM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001449 child->thread.regs->msr |= MSR_DE;
1450
1451 return 5;
1452}
1453#endif /* CONFIG_PPC_ADV_DEBUG_DAC_RANGE */
1454
Dave Kleikamp3162d922010-02-08 11:51:05 +00001455static long ppc_set_hwdebug(struct task_struct *child,
1456 struct ppc_hw_breakpoint *bp_info)
1457{
K.Prasad6c7a2852012-10-28 15:13:15 +00001458#ifdef CONFIG_HAVE_HW_BREAKPOINT
1459 int len = 0;
1460 struct thread_struct *thread = &(child->thread);
1461 struct perf_event *bp;
1462 struct perf_event_attr attr;
1463#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001464#ifndef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling9422de32012-12-20 14:06:44 +00001465 struct arch_hw_breakpoint brk;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001466#endif
1467
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001468 if (bp_info->version != 1)
1469 return -ENOTSUPP;
1470#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3162d922010-02-08 11:51:05 +00001471 /*
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001472 * Check for invalid flags and combinations
1473 */
1474 if ((bp_info->trigger_type == 0) ||
1475 (bp_info->trigger_type & ~(PPC_BREAKPOINT_TRIGGER_EXECUTE |
1476 PPC_BREAKPOINT_TRIGGER_RW)) ||
1477 (bp_info->addr_mode & ~PPC_BREAKPOINT_MODE_MASK) ||
1478 (bp_info->condition_mode &
1479 ~(PPC_BREAKPOINT_CONDITION_MODE |
1480 PPC_BREAKPOINT_CONDITION_BE_ALL)))
1481 return -EINVAL;
1482#if CONFIG_PPC_ADV_DEBUG_DVCS == 0
1483 if (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
1484 return -EINVAL;
1485#endif
1486
1487 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_EXECUTE) {
1488 if ((bp_info->trigger_type != PPC_BREAKPOINT_TRIGGER_EXECUTE) ||
1489 (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE))
1490 return -EINVAL;
Michael Neuling84295df2012-10-28 15:13:16 +00001491 return set_instruction_bp(child, bp_info);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001492 }
1493 if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
1494 return set_dac(child, bp_info);
1495
1496#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1497 return set_dac_range(child, bp_info);
1498#else
1499 return -EINVAL;
1500#endif
1501#else /* !CONFIG_PPC_ADV_DEBUG_DVCS */
1502 /*
1503 * We only support one data breakpoint
Dave Kleikamp3162d922010-02-08 11:51:05 +00001504 */
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001505 if ((bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_RW) == 0 ||
1506 (bp_info->trigger_type & ~PPC_BREAKPOINT_TRIGGER_RW) != 0 ||
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001507 bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
Dave Kleikamp3162d922010-02-08 11:51:05 +00001508 return -EINVAL;
1509
Dave Kleikamp3162d922010-02-08 11:51:05 +00001510 if ((unsigned long)bp_info->addr >= TASK_SIZE)
1511 return -EIO;
1512
Michael Neuling9422de32012-12-20 14:06:44 +00001513 brk.address = bp_info->addr & ~7UL;
1514 brk.type = HW_BRK_TYPE_TRANSLATE;
Michael Neuling2bb78ef2013-03-11 16:42:49 +00001515 brk.len = 8;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001516 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
Michael Neuling9422de32012-12-20 14:06:44 +00001517 brk.type |= HW_BRK_TYPE_READ;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001518 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
Michael Neuling9422de32012-12-20 14:06:44 +00001519 brk.type |= HW_BRK_TYPE_WRITE;
K.Prasad6c7a2852012-10-28 15:13:15 +00001520#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad6c7a2852012-10-28 15:13:15 +00001521 /*
1522 * Check if the request is for 'range' breakpoints. We can
1523 * support it if range < 8 bytes.
1524 */
Oleg Nesterov6961ed92013-07-08 16:00:49 -07001525 if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
K.Prasad6c7a2852012-10-28 15:13:15 +00001526 len = bp_info->addr2 - bp_info->addr;
Oleg Nesterov6961ed92013-07-08 16:00:49 -07001527 else if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
Michael Neulingb0b0aa92013-06-24 15:47:22 +10001528 len = 1;
Oleg Nesterov6961ed92013-07-08 16:00:49 -07001529 else
K.Prasad6c7a2852012-10-28 15:13:15 +00001530 return -EINVAL;
K.Prasad6c7a2852012-10-28 15:13:15 +00001531 bp = thread->ptrace_bps[0];
Oleg Nesterov6961ed92013-07-08 16:00:49 -07001532 if (bp)
K.Prasad6c7a2852012-10-28 15:13:15 +00001533 return -ENOSPC;
K.Prasad6c7a2852012-10-28 15:13:15 +00001534
1535 /* Create a new breakpoint request if one doesn't exist already */
1536 hw_breakpoint_init(&attr);
1537 attr.bp_addr = (unsigned long)bp_info->addr & ~HW_BREAKPOINT_ALIGN;
1538 attr.bp_len = len;
Michael Neuling9422de32012-12-20 14:06:44 +00001539 arch_bp_generic_fields(brk.type, &attr.bp_type);
K.Prasad6c7a2852012-10-28 15:13:15 +00001540
1541 thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
1542 ptrace_triggered, NULL, child);
1543 if (IS_ERR(bp)) {
1544 thread->ptrace_bps[0] = NULL;
K.Prasad6c7a2852012-10-28 15:13:15 +00001545 return PTR_ERR(bp);
1546 }
1547
K.Prasad6c7a2852012-10-28 15:13:15 +00001548 return 1;
1549#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1550
1551 if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT)
1552 return -EINVAL;
1553
Michael Neuling9422de32012-12-20 14:06:44 +00001554 if (child->thread.hw_brk.address)
K.Prasad6c7a2852012-10-28 15:13:15 +00001555 return -ENOSPC;
Andreas Schwab4dfbf292010-11-27 14:24:53 +00001556
Michael Neuling9422de32012-12-20 14:06:44 +00001557 child->thread.hw_brk = brk;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001558
Dave Kleikamp3162d922010-02-08 11:51:05 +00001559 return 1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001560#endif /* !CONFIG_PPC_ADV_DEBUG_DVCS */
Dave Kleikamp3162d922010-02-08 11:51:05 +00001561}
1562
Michael Neulingec1b33d2012-10-28 15:13:17 +00001563static long ppc_del_hwdebug(struct task_struct *child, long data)
Dave Kleikamp3162d922010-02-08 11:51:05 +00001564{
K.Prasad6c7a2852012-10-28 15:13:15 +00001565#ifdef CONFIG_HAVE_HW_BREAKPOINT
1566 int ret = 0;
1567 struct thread_struct *thread = &(child->thread);
1568 struct perf_event *bp;
1569#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001570#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1571 int rc;
1572
1573 if (data <= 4)
1574 rc = del_instruction_bp(child, (int)data);
1575 else
1576 rc = del_dac(child, (int)data - 4);
1577
1578 if (!rc) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301579 if (!DBCR_ACTIVE_EVENTS(child->thread.debug.dbcr0,
1580 child->thread.debug.dbcr1)) {
1581 child->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001582 child->thread.regs->msr &= ~MSR_DE;
1583 }
1584 }
1585 return rc;
1586#else
Dave Kleikamp3162d922010-02-08 11:51:05 +00001587 if (data != 1)
1588 return -EINVAL;
K.Prasad6c7a2852012-10-28 15:13:15 +00001589
1590#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad6c7a2852012-10-28 15:13:15 +00001591 bp = thread->ptrace_bps[0];
1592 if (bp) {
1593 unregister_hw_breakpoint(bp);
1594 thread->ptrace_bps[0] = NULL;
1595 } else
1596 ret = -ENOENT;
K.Prasad6c7a2852012-10-28 15:13:15 +00001597 return ret;
1598#else /* CONFIG_HAVE_HW_BREAKPOINT */
Michael Neuling9422de32012-12-20 14:06:44 +00001599 if (child->thread.hw_brk.address == 0)
Dave Kleikamp3162d922010-02-08 11:51:05 +00001600 return -ENOENT;
1601
Michael Neuling9422de32012-12-20 14:06:44 +00001602 child->thread.hw_brk.address = 0;
1603 child->thread.hw_brk.type = 0;
K.Prasad6c7a2852012-10-28 15:13:15 +00001604#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001605
Dave Kleikamp3162d922010-02-08 11:51:05 +00001606 return 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001607#endif
Dave Kleikamp3162d922010-02-08 11:51:05 +00001608}
1609
Namhyung Kim9b05a692010-10-27 15:33:47 -07001610long arch_ptrace(struct task_struct *child, long request,
1611 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 int ret = -EPERM;
Namhyung Kimf68d2042010-10-27 15:34:01 -07001614 void __user *datavp = (void __user *) data;
1615 unsigned long __user *datalp = datavp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /* read the word at location addr in the USER area. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 case PTRACE_PEEKUSR: {
1620 unsigned long index, tmp;
1621
1622 ret = -EIO;
1623 /* convert to index and check */
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001624#ifdef CONFIG_PPC32
Namhyung Kim9b05a692010-10-27 15:33:47 -07001625 index = addr >> 2;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001626 if ((addr & 3) || (index > PT_FPSCR)
1627 || (child->thread.regs == NULL))
1628#else
Namhyung Kim9b05a692010-10-27 15:33:47 -07001629 index = addr >> 3;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001630 if ((addr & 7) || (index > PT_FPSCR))
1631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 break;
1633
1634 CHECK_FULL_REGS(child->thread.regs);
1635 if (index < PT_FPR0) {
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +00001636 ret = ptrace_get_reg(child, (int) index, &tmp);
1637 if (ret)
1638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 } else {
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001640 unsigned int fpidx = index - PT_FPR0;
1641
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001642 flush_fp_to_thread(child);
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001643 if (fpidx < (PT_FPSCR - PT_FPR0))
Ulrich Weigand36aa1b12013-12-12 15:59:34 +11001644 memcpy(&tmp, &child->thread.TS_FPR(fpidx),
Anton Blanchard87fec052013-09-23 12:04:38 +10001645 sizeof(long));
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001646 else
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001647 tmp = child->thread.fp_state.fpscr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Namhyung Kimf68d2042010-10-27 15:34:01 -07001649 ret = put_user(tmp, datalp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651 }
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* write the word at location addr in the USER area */
1654 case PTRACE_POKEUSR: {
1655 unsigned long index;
1656
1657 ret = -EIO;
1658 /* convert to index and check */
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001659#ifdef CONFIG_PPC32
Namhyung Kim9b05a692010-10-27 15:33:47 -07001660 index = addr >> 2;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001661 if ((addr & 3) || (index > PT_FPSCR)
1662 || (child->thread.regs == NULL))
1663#else
Namhyung Kim9b05a692010-10-27 15:33:47 -07001664 index = addr >> 3;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001665 if ((addr & 7) || (index > PT_FPSCR))
1666#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 break;
1668
1669 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +10001671 ret = ptrace_put_reg(child, index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 } else {
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001673 unsigned int fpidx = index - PT_FPR0;
1674
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001675 flush_fp_to_thread(child);
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001676 if (fpidx < (PT_FPSCR - PT_FPR0))
Ulrich Weigand36aa1b12013-12-12 15:59:34 +11001677 memcpy(&child->thread.TS_FPR(fpidx), &data,
Anton Blanchard87fec052013-09-23 12:04:38 +10001678 sizeof(long));
Benjamin Herrenschmidte69b7422011-09-26 19:37:57 +00001679 else
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001680 child->thread.fp_state.fpscr = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 ret = 0;
1682 }
1683 break;
1684 }
1685
Dave Kleikamp3162d922010-02-08 11:51:05 +00001686 case PPC_PTRACE_GETHWDBGINFO: {
1687 struct ppc_debug_info dbginfo;
1688
1689 dbginfo.version = 1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001690#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1691 dbginfo.num_instruction_bps = CONFIG_PPC_ADV_DEBUG_IACS;
1692 dbginfo.num_data_bps = CONFIG_PPC_ADV_DEBUG_DACS;
1693 dbginfo.num_condition_regs = CONFIG_PPC_ADV_DEBUG_DVCS;
1694 dbginfo.data_bp_alignment = 4;
1695 dbginfo.sizeof_condition = 4;
1696 dbginfo.features = PPC_DEBUG_FEATURE_INSN_BP_RANGE |
1697 PPC_DEBUG_FEATURE_INSN_BP_MASK;
1698#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1699 dbginfo.features |=
1700 PPC_DEBUG_FEATURE_DATA_BP_RANGE |
1701 PPC_DEBUG_FEATURE_DATA_BP_MASK;
1702#endif
1703#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Dave Kleikamp3162d922010-02-08 11:51:05 +00001704 dbginfo.num_instruction_bps = 0;
1705 dbginfo.num_data_bps = 1;
1706 dbginfo.num_condition_regs = 0;
1707#ifdef CONFIG_PPC64
1708 dbginfo.data_bp_alignment = 8;
1709#else
1710 dbginfo.data_bp_alignment = 4;
1711#endif
1712 dbginfo.sizeof_condition = 0;
K.Prasad6c7a2852012-10-28 15:13:15 +00001713#ifdef CONFIG_HAVE_HW_BREAKPOINT
1714 dbginfo.features = PPC_DEBUG_FEATURE_DATA_BP_RANGE;
Michael Neuling517b7312013-03-21 20:12:33 +00001715 if (cpu_has_feature(CPU_FTR_DAWR))
1716 dbginfo.features |= PPC_DEBUG_FEATURE_DATA_BP_DAWR;
K.Prasad6c7a2852012-10-28 15:13:15 +00001717#else
Dave Kleikamp3162d922010-02-08 11:51:05 +00001718 dbginfo.features = 0;
K.Prasad6c7a2852012-10-28 15:13:15 +00001719#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001720#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Dave Kleikamp3162d922010-02-08 11:51:05 +00001721
Namhyung Kimf68d2042010-10-27 15:34:01 -07001722 if (!access_ok(VERIFY_WRITE, datavp,
Dave Kleikamp3162d922010-02-08 11:51:05 +00001723 sizeof(struct ppc_debug_info)))
1724 return -EFAULT;
Namhyung Kimf68d2042010-10-27 15:34:01 -07001725 ret = __copy_to_user(datavp, &dbginfo,
1726 sizeof(struct ppc_debug_info)) ?
Dave Kleikamp3162d922010-02-08 11:51:05 +00001727 -EFAULT : 0;
1728 break;
1729 }
1730
1731 case PPC_PTRACE_SETHWDEBUG: {
1732 struct ppc_hw_breakpoint bp_info;
1733
Namhyung Kimf68d2042010-10-27 15:34:01 -07001734 if (!access_ok(VERIFY_READ, datavp,
Dave Kleikamp3162d922010-02-08 11:51:05 +00001735 sizeof(struct ppc_hw_breakpoint)))
1736 return -EFAULT;
Namhyung Kimf68d2042010-10-27 15:34:01 -07001737 ret = __copy_from_user(&bp_info, datavp,
Dave Kleikamp3162d922010-02-08 11:51:05 +00001738 sizeof(struct ppc_hw_breakpoint)) ?
1739 -EFAULT : 0;
1740 if (!ret)
1741 ret = ppc_set_hwdebug(child, &bp_info);
1742 break;
1743 }
1744
1745 case PPC_PTRACE_DELHWDEBUG: {
Michael Neulingec1b33d2012-10-28 15:13:17 +00001746 ret = ppc_del_hwdebug(child, data);
Dave Kleikamp3162d922010-02-08 11:51:05 +00001747 break;
1748 }
1749
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001750 case PTRACE_GET_DEBUGREG: {
Michael Neuling9422de32012-12-20 14:06:44 +00001751#ifndef CONFIG_PPC_ADV_DEBUG_REGS
1752 unsigned long dabr_fake;
1753#endif
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001754 ret = -EINVAL;
1755 /* We only support one DABR and no IABRS at the moment */
1756 if (addr > 0)
1757 break;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001758#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301759 ret = put_user(child->thread.debug.dac1, datalp);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001760#else
Michael Neuling9422de32012-12-20 14:06:44 +00001761 dabr_fake = ((child->thread.hw_brk.address & (~HW_BRK_TYPE_DABR)) |
1762 (child->thread.hw_brk.type & HW_BRK_TYPE_DABR));
1763 ret = put_user(dabr_fake, datalp);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001764#endif
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001765 break;
1766 }
1767
1768 case PTRACE_SET_DEBUGREG:
1769 ret = ptrace_set_debugreg(child, addr, data);
1770 break;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001771
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +10001772#ifdef CONFIG_PPC64
1773 case PTRACE_GETREGS64:
1774#endif
Roland McGrathc391cd02007-12-20 03:58:36 -08001775 case PTRACE_GETREGS: /* Get all pt_regs from the child. */
1776 return copy_regset_to_user(child, &user_ppc_native_view,
1777 REGSET_GPR,
1778 0, sizeof(struct pt_regs),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001779 datavp);
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001780
Benjamin Herrenschmidt0b3d5c42007-06-04 15:15:39 +10001781#ifdef CONFIG_PPC64
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +10001782 case PTRACE_SETREGS64:
1783#endif
Roland McGrathc391cd02007-12-20 03:58:36 -08001784 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1785 return copy_regset_from_user(child, &user_ppc_native_view,
1786 REGSET_GPR,
1787 0, sizeof(struct pt_regs),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001788 datavp);
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001789
Roland McGrathc391cd02007-12-20 03:58:36 -08001790 case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
1791 return copy_regset_to_user(child, &user_ppc_native_view,
1792 REGSET_FPR,
1793 0, sizeof(elf_fpregset_t),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001794 datavp);
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +10001795
Roland McGrathc391cd02007-12-20 03:58:36 -08001796 case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
1797 return copy_regset_from_user(child, &user_ppc_native_view,
1798 REGSET_FPR,
1799 0, sizeof(elf_fpregset_t),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001800 datavp);
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802#ifdef CONFIG_ALTIVEC
1803 case PTRACE_GETVRREGS:
Roland McGrathc391cd02007-12-20 03:58:36 -08001804 return copy_regset_to_user(child, &user_ppc_native_view,
1805 REGSET_VMX,
1806 0, (33 * sizeof(vector128) +
1807 sizeof(u32)),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001808 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 case PTRACE_SETVRREGS:
Roland McGrathc391cd02007-12-20 03:58:36 -08001811 return copy_regset_from_user(child, &user_ppc_native_view,
1812 REGSET_VMX,
1813 0, (33 * sizeof(vector128) +
1814 sizeof(u32)),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001815 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001817#ifdef CONFIG_VSX
1818 case PTRACE_GETVSRREGS:
1819 return copy_regset_to_user(child, &user_ppc_native_view,
1820 REGSET_VSX,
Michael Neuling1ac42ef82008-07-29 01:13:14 +10001821 0, 32 * sizeof(double),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001822 datavp);
Michael Neulingce48b212008-06-25 14:07:18 +10001823
1824 case PTRACE_SETVSRREGS:
1825 return copy_regset_from_user(child, &user_ppc_native_view,
1826 REGSET_VSX,
Michael Neuling1ac42ef82008-07-29 01:13:14 +10001827 0, 32 * sizeof(double),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001828 datavp);
Michael Neulingce48b212008-06-25 14:07:18 +10001829#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830#ifdef CONFIG_SPE
1831 case PTRACE_GETEVRREGS:
1832 /* Get the child spe register state. */
Roland McGrathc391cd02007-12-20 03:58:36 -08001833 return copy_regset_to_user(child, &user_ppc_native_view,
1834 REGSET_SPE, 0, 35 * sizeof(u32),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001835 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 case PTRACE_SETEVRREGS:
1838 /* Set the child spe register state. */
Roland McGrathc391cd02007-12-20 03:58:36 -08001839 return copy_regset_from_user(child, &user_ppc_native_view,
1840 REGSET_SPE, 0, 35 * sizeof(u32),
Namhyung Kimf68d2042010-10-27 15:34:01 -07001841 datavp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842#endif
1843
1844 default:
1845 ret = ptrace_request(child, request, addr, data);
1846 break;
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return ret;
1849}
1850
Michael Ellerman2449acc2015-07-23 20:21:09 +10001851#ifdef CONFIG_SECCOMP
1852static int do_seccomp(struct pt_regs *regs)
1853{
1854 if (!test_thread_flag(TIF_SECCOMP))
1855 return 0;
1856
1857 /*
1858 * The ABI we present to seccomp tracers is that r3 contains
1859 * the syscall return value and orig_gpr3 contains the first
1860 * syscall parameter. This is different to the ptrace ABI where
1861 * both r3 and orig_gpr3 contain the first syscall parameter.
1862 */
1863 regs->gpr[3] = -ENOSYS;
1864
1865 /*
1866 * We use the __ version here because we have already checked
1867 * TIF_SECCOMP. If this fails, there is nothing left to do, we
1868 * have already loaded -ENOSYS into r3, or seccomp has put
1869 * something else in r3 (via SECCOMP_RET_ERRNO/TRACE).
1870 */
Andy Lutomirski2f275de2016-05-27 12:57:02 -07001871 if (__secure_computing(NULL))
Michael Ellerman2449acc2015-07-23 20:21:09 +10001872 return -1;
1873
1874 /*
1875 * The syscall was allowed by seccomp, restore the register
Kees Cook1addc572016-06-02 19:55:09 -07001876 * state to what audit expects.
Michael Ellerman2449acc2015-07-23 20:21:09 +10001877 * Note that we use orig_gpr3, which means a seccomp tracer can
1878 * modify the first syscall parameter (in orig_gpr3) and also
1879 * allow the syscall to proceed.
1880 */
1881 regs->gpr[3] = regs->orig_gpr3;
1882
1883 return 0;
1884}
1885#else
1886static inline int do_seccomp(struct pt_regs *regs) { return 0; }
1887#endif /* CONFIG_SECCOMP */
1888
Michael Ellermand3837412015-07-23 20:21:02 +10001889/**
1890 * do_syscall_trace_enter() - Do syscall tracing on kernel entry.
1891 * @regs: the pt_regs of the task to trace (current)
1892 *
1893 * Performs various types of tracing on syscall entry. This includes seccomp,
1894 * ptrace, syscall tracepoints and audit.
1895 *
1896 * The pt_regs are potentially visible to userspace via ptrace, so their
1897 * contents is ABI.
1898 *
1899 * One or more of the tracers may modify the contents of pt_regs, in particular
1900 * to modify arguments or even the syscall number itself.
1901 *
1902 * It's also possible that a tracer can choose to reject the system call. In
1903 * that case this function will return an illegal syscall number, and will put
1904 * an appropriate return value in regs->r3.
1905 *
1906 * Return: the (possibly changed) syscall number.
Roland McGrath4f72c422008-07-27 16:51:03 +10001907 */
1908long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Li Zhong22ecbe82013-05-13 16:16:40 +00001910 user_exit();
1911
Kees Cook1addc572016-06-02 19:55:09 -07001912 /*
1913 * The tracer may decide to abort the syscall, if so tracehook
1914 * will return !0. Note that the tracer may also just change
1915 * regs->gpr[0] to an invalid syscall number, that is handled
1916 * below on the exit path.
1917 */
1918 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
1919 tracehook_report_syscall_entry(regs))
1920 goto skip;
1921
1922 /* Run seccomp after ptrace; allow it to set gpr[3]. */
Michael Ellerman2449acc2015-07-23 20:21:09 +10001923 if (do_seccomp(regs))
1924 return -1;
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001925
Kees Cook1addc572016-06-02 19:55:09 -07001926 /* Avoid trace and audit when syscall is invalid. */
1927 if (regs->gpr[0] >= NR_syscalls)
1928 goto skip;
David Woodhouseea9c1022005-05-08 15:56:09 +01001929
Ian Munsie02424d82011-02-02 17:27:24 +00001930 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1931 trace_sys_enter(regs, regs->gpr[0]);
1932
David Woodhousecfcd1702007-01-14 09:38:18 +08001933#ifdef CONFIG_PPC64
Eric Parisb05d8442012-01-03 14:23:06 -05001934 if (!is_32bit_task())
Eric Paris91397402014-03-11 13:29:28 -04001935 audit_syscall_entry(regs->gpr[0], regs->gpr[3], regs->gpr[4],
Eric Parisb05d8442012-01-03 14:23:06 -05001936 regs->gpr[5], regs->gpr[6]);
1937 else
Stephen Rothwelle8a30302005-10-13 15:52:04 +10001938#endif
Eric Paris91397402014-03-11 13:29:28 -04001939 audit_syscall_entry(regs->gpr[0],
Eric Parisb05d8442012-01-03 14:23:06 -05001940 regs->gpr[3] & 0xffffffff,
1941 regs->gpr[4] & 0xffffffff,
1942 regs->gpr[5] & 0xffffffff,
1943 regs->gpr[6] & 0xffffffff);
Roland McGrath4f72c422008-07-27 16:51:03 +10001944
Michael Ellermand3837412015-07-23 20:21:02 +10001945 /* Return the possibly modified but valid syscall number */
1946 return regs->gpr[0];
Kees Cook1addc572016-06-02 19:55:09 -07001947
1948skip:
1949 /*
1950 * If we are aborting explicitly, or if the syscall number is
1951 * now invalid, set the return value to -ENOSYS.
1952 */
1953 regs->gpr[3] = -ENOSYS;
1954 return -1;
David Woodhouseea9c1022005-05-08 15:56:09 +01001955}
1956
1957void do_syscall_trace_leave(struct pt_regs *regs)
1958{
Roland McGrath4f72c422008-07-27 16:51:03 +10001959 int step;
1960
Eric Parisd7e75282012-01-03 14:23:06 -05001961 audit_syscall_exit(regs);
David Woodhouseea9c1022005-05-08 15:56:09 +01001962
Ian Munsie02424d82011-02-02 17:27:24 +00001963 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1964 trace_sys_exit(regs, regs->result);
1965
Roland McGrath4f72c422008-07-27 16:51:03 +10001966 step = test_thread_flag(TIF_SINGLESTEP);
1967 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1968 tracehook_report_syscall_exit(regs, step);
Li Zhong22ecbe82013-05-13 16:16:40 +00001969
1970 user_enter();
David Woodhouseea9c1022005-05-08 15:56:09 +01001971}