1 // TODO some minor issues
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
7 * Copyright (C) 2001 - 2007 Tensilica Inc.
9 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
10 * Chris Zankel <chris@zankel.net>
11 * Scott Foehner<sfoehner@yahoo.com>,
13 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
16 #include <linux/errno.h>
17 #include <linux/hw_breakpoint.h>
18 #include <linux/kernel.h>
20 #include <linux/perf_event.h>
21 #include <linux/ptrace.h>
22 #include <linux/sched.h>
23 #include <linux/sched/task_stack.h>
24 #include <linux/security.h>
25 #include <linux/signal.h>
26 #include <linux/smp.h>
28 #include <asm/coprocessor.h>
31 #include <asm/pgtable.h>
32 #include <asm/ptrace.h>
33 #include <linux/uaccess.h>
36 void user_enable_single_step(struct task_struct *child)
38 child->ptrace |= PT_SINGLESTEP;
41 void user_disable_single_step(struct task_struct *child)
43 child->ptrace &= ~PT_SINGLESTEP;
47 * Called by kernel/ptrace.c when detaching to disable single stepping.
50 void ptrace_disable(struct task_struct *child)
55 int ptrace_getregs(struct task_struct *child, void __user *uregs)
57 struct pt_regs *regs = task_pt_regs(child);
58 xtensa_gregset_t __user *gregset = uregs;
59 unsigned long wb = regs->windowbase;
62 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
65 __put_user(regs->pc, &gregset->pc);
66 __put_user(regs->ps & ~(1 << PS_EXCM_BIT), &gregset->ps);
67 __put_user(regs->lbeg, &gregset->lbeg);
68 __put_user(regs->lend, &gregset->lend);
69 __put_user(regs->lcount, &gregset->lcount);
70 __put_user(regs->windowstart, &gregset->windowstart);
71 __put_user(regs->windowbase, &gregset->windowbase);
72 __put_user(regs->threadptr, &gregset->threadptr);
74 for (i = 0; i < XCHAL_NUM_AREGS; i++)
75 __put_user(regs->areg[i],
76 gregset->a + ((wb * 4 + i) % XCHAL_NUM_AREGS));
81 int ptrace_setregs(struct task_struct *child, void __user *uregs)
83 struct pt_regs *regs = task_pt_regs(child);
84 xtensa_gregset_t *gregset = uregs;
85 const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
89 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
92 __get_user(regs->pc, &gregset->pc);
93 __get_user(ps, &gregset->ps);
94 __get_user(regs->lbeg, &gregset->lbeg);
95 __get_user(regs->lend, &gregset->lend);
96 __get_user(regs->lcount, &gregset->lcount);
97 __get_user(ws, &gregset->windowstart);
98 __get_user(wb, &gregset->windowbase);
99 __get_user(regs->threadptr, &gregset->threadptr);
101 regs->ps = (regs->ps & ~ps_mask) | (ps & ps_mask) | (1 << PS_EXCM_BIT);
103 if (wb >= XCHAL_NUM_AREGS / 4)
106 if (wb != regs->windowbase || ws != regs->windowstart) {
107 unsigned long rotws, wmask;
109 rotws = (((ws | (ws << WSBITS)) >> wb) &
110 ((1 << WSBITS) - 1)) & ~1;
111 wmask = ((rotws ? WSBITS + 1 - ffs(rotws) : 0) << 4) |
113 regs->windowbase = wb;
114 regs->windowstart = ws;
118 if (wb != 0 && __copy_from_user(regs->areg + XCHAL_NUM_AREGS - wb * 4,
119 gregset->a, wb * 16))
122 if (__copy_from_user(regs->areg, gregset->a + wb * 4,
130 int ptrace_getxregs(struct task_struct *child, void __user *uregs)
132 struct pt_regs *regs = task_pt_regs(child);
133 struct thread_info *ti = task_thread_info(child);
134 elf_xtregs_t __user *xtregs = uregs;
137 if (!access_ok(VERIFY_WRITE, uregs, sizeof(elf_xtregs_t)))
140 #if XTENSA_HAVE_COPROCESSORS
141 /* Flush all coprocessor registers to memory. */
142 coprocessor_flush_all(ti);
143 ret |= __copy_to_user(&xtregs->cp0, &ti->xtregs_cp,
144 sizeof(xtregs_coprocessor_t));
146 ret |= __copy_to_user(&xtregs->opt, ®s->xtregs_opt,
147 sizeof(xtregs->opt));
148 ret |= __copy_to_user(&xtregs->user,&ti->xtregs_user,
149 sizeof(xtregs->user));
151 return ret ? -EFAULT : 0;
154 int ptrace_setxregs(struct task_struct *child, void __user *uregs)
156 struct thread_info *ti = task_thread_info(child);
157 struct pt_regs *regs = task_pt_regs(child);
158 elf_xtregs_t *xtregs = uregs;
161 if (!access_ok(VERIFY_READ, uregs, sizeof(elf_xtregs_t)))
164 #if XTENSA_HAVE_COPROCESSORS
165 /* Flush all coprocessors before we overwrite them. */
166 coprocessor_flush_all(ti);
167 coprocessor_release_all(ti);
169 ret |= __copy_from_user(&ti->xtregs_cp, &xtregs->cp0,
170 sizeof(xtregs_coprocessor_t));
172 ret |= __copy_from_user(®s->xtregs_opt, &xtregs->opt,
173 sizeof(xtregs->opt));
174 ret |= __copy_from_user(&ti->xtregs_user, &xtregs->user,
175 sizeof(xtregs->user));
177 return ret ? -EFAULT : 0;
180 int ptrace_peekusr(struct task_struct *child, long regno, long __user *ret)
182 struct pt_regs *regs;
185 regs = task_pt_regs(child);
186 tmp = 0; /* Default return value. */
190 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
191 tmp = regs->areg[regno - REG_AR_BASE];
194 case REG_A_BASE ... REG_A_BASE + 15:
195 tmp = regs->areg[regno - REG_A_BASE];
203 /* Note: PS.EXCM is not set while user task is running;
204 * its being set in regs is for exception handling
206 tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
214 unsigned long wb = regs->windowbase;
215 unsigned long ws = regs->windowstart;
216 tmp = ((ws>>wb) | (ws<<(WSBITS-wb))) & ((1<<WSBITS)-1);
242 return put_user(tmp, ret);
245 int ptrace_pokeusr(struct task_struct *child, long regno, long val)
247 struct pt_regs *regs;
248 regs = task_pt_regs(child);
251 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
252 regs->areg[regno - REG_AR_BASE] = val;
255 case REG_A_BASE ... REG_A_BASE + 15:
256 regs->areg[regno - REG_A_BASE] = val;
273 #ifdef CONFIG_HAVE_HW_BREAKPOINT
274 static void ptrace_hbptriggered(struct perf_event *bp,
275 struct perf_sample_data *data,
276 struct pt_regs *regs)
280 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
282 if (bp->attr.bp_type & HW_BREAKPOINT_X) {
283 for (i = 0; i < XCHAL_NUM_IBREAK; ++i)
284 if (current->thread.ptrace_bp[i] == bp)
288 for (i = 0; i < XCHAL_NUM_DBREAK; ++i)
289 if (current->thread.ptrace_wp[i] == bp)
294 info.si_signo = SIGTRAP;
296 info.si_code = TRAP_HWBKPT;
297 info.si_addr = (void __user *)bkpt->address;
299 force_sig_info(SIGTRAP, &info, current);
302 static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
304 struct perf_event_attr attr;
306 ptrace_breakpoint_init(&attr);
308 /* Initialise fields to sane defaults. */
314 return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
319 * Address bit 0 choose instruction (0) or data (1) break register, bits
320 * 31..1 are the register number.
321 * Both PTRACE_GETHBPREGS and PTRACE_SETHBPREGS transfer two 32-bit words:
322 * address (0) and control (1).
323 * Instruction breakpoint contorl word is 0 to clear breakpoint, 1 to set.
324 * Data breakpoint control word bit 31 is 'trigger on store', bit 30 is
325 * 'trigger on load, bits 29..0 are length. Length 0 is used to clear a
326 * breakpoint. To set a breakpoint length must be a power of 2 in the range
327 * 1..64 and the address must be length-aligned.
330 static long ptrace_gethbpregs(struct task_struct *child, long addr,
333 struct perf_event *bp;
334 u32 user_data[2] = {0};
335 bool dbreak = addr & 1;
336 unsigned idx = addr >> 1;
338 if ((!dbreak && idx >= XCHAL_NUM_IBREAK) ||
339 (dbreak && idx >= XCHAL_NUM_DBREAK))
343 bp = child->thread.ptrace_wp[idx];
345 bp = child->thread.ptrace_bp[idx];
348 user_data[0] = bp->attr.bp_addr;
349 user_data[1] = bp->attr.disabled ? 0 : bp->attr.bp_len;
351 if (bp->attr.bp_type & HW_BREAKPOINT_R)
352 user_data[1] |= DBREAKC_LOAD_MASK;
353 if (bp->attr.bp_type & HW_BREAKPOINT_W)
354 user_data[1] |= DBREAKC_STOR_MASK;
358 if (copy_to_user(datap, user_data, sizeof(user_data)))
364 static long ptrace_sethbpregs(struct task_struct *child, long addr,
367 struct perf_event *bp;
368 struct perf_event_attr attr;
370 bool dbreak = addr & 1;
371 unsigned idx = addr >> 1;
374 if ((!dbreak && idx >= XCHAL_NUM_IBREAK) ||
375 (dbreak && idx >= XCHAL_NUM_DBREAK))
378 if (copy_from_user(user_data, datap, sizeof(user_data)))
382 bp = child->thread.ptrace_wp[idx];
383 if (user_data[1] & DBREAKC_LOAD_MASK)
384 bp_type |= HW_BREAKPOINT_R;
385 if (user_data[1] & DBREAKC_STOR_MASK)
386 bp_type |= HW_BREAKPOINT_W;
388 bp = child->thread.ptrace_bp[idx];
389 bp_type = HW_BREAKPOINT_X;
393 bp = ptrace_hbp_create(child,
394 bp_type ? bp_type : HW_BREAKPOINT_RW);
398 child->thread.ptrace_wp[idx] = bp;
400 child->thread.ptrace_bp[idx] = bp;
404 attr.bp_addr = user_data[0];
405 attr.bp_len = user_data[1] & ~(DBREAKC_LOAD_MASK | DBREAKC_STOR_MASK);
406 attr.bp_type = bp_type;
407 attr.disabled = !attr.bp_len;
409 return modify_user_hw_breakpoint(bp, &attr);
413 long arch_ptrace(struct task_struct *child, long request,
414 unsigned long addr, unsigned long data)
417 void __user *datap = (void __user *) data;
420 case PTRACE_PEEKTEXT: /* read word at location addr. */
421 case PTRACE_PEEKDATA:
422 ret = generic_ptrace_peekdata(child, addr, data);
425 case PTRACE_PEEKUSR: /* read register specified by addr. */
426 ret = ptrace_peekusr(child, addr, datap);
429 case PTRACE_POKETEXT: /* write the word at location addr. */
430 case PTRACE_POKEDATA:
431 ret = generic_ptrace_pokedata(child, addr, data);
434 case PTRACE_POKEUSR: /* write register specified by addr. */
435 ret = ptrace_pokeusr(child, addr, data);
439 ret = ptrace_getregs(child, datap);
443 ret = ptrace_setregs(child, datap);
446 case PTRACE_GETXTREGS:
447 ret = ptrace_getxregs(child, datap);
450 case PTRACE_SETXTREGS:
451 ret = ptrace_setxregs(child, datap);
453 #ifdef CONFIG_HAVE_HW_BREAKPOINT
454 case PTRACE_GETHBPREGS:
455 ret = ptrace_gethbpregs(child, addr, datap);
458 case PTRACE_SETHBPREGS:
459 ret = ptrace_sethbpregs(child, addr, datap);
463 ret = ptrace_request(child, request, addr, data);
470 void do_syscall_trace(void)
473 * The 0x80 provides a way for the tracing parent to distinguish
474 * between a syscall stop and SIGTRAP delivery
476 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
479 * this isn't the same as continuing with a signal, but it will do
480 * for normal use. strace only continues with a signal if the
481 * stopping signal is not SIGTRAP. -brl
483 if (current->exit_code) {
484 send_sig(current->exit_code, current, 1);
485 current->exit_code = 0;
489 void do_syscall_trace_enter(struct pt_regs *regs)
491 if (test_thread_flag(TIF_SYSCALL_TRACE)
492 && (current->ptrace & PT_PTRACED))
496 audit_syscall_entry(...);
500 void do_syscall_trace_leave(struct pt_regs *regs)
502 if ((test_thread_flag(TIF_SYSCALL_TRACE))
503 && (current->ptrace & PT_PTRACED))