2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2001 - 2007 Tensilica Inc.
8 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
9 * Chris Zankel <chris@zankel.net>
10 * Scott Foehner<sfoehner@yahoo.com>,
12 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
15 #include <linux/audit.h>
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/regset.h>
23 #include <linux/sched.h>
24 #include <linux/sched/task_stack.h>
25 #include <linux/seccomp.h>
26 #include <linux/security.h>
27 #include <linux/signal.h>
28 #include <linux/smp.h>
29 #include <linux/tracehook.h>
30 #include <linux/uaccess.h>
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/syscalls.h>
35 #include <asm/coprocessor.h>
38 #include <asm/ptrace.h>
40 static int gpr_get(struct task_struct
*target
,
41 const struct user_regset
*regset
,
44 struct pt_regs
*regs
= task_pt_regs(target
);
45 struct user_pt_regs newregs
= {
47 .ps
= regs
->ps
& ~(1 << PS_EXCM_BIT
),
50 .lcount
= regs
->lcount
,
52 .threadptr
= regs
->threadptr
,
53 .windowbase
= regs
->windowbase
,
54 .windowstart
= regs
->windowstart
,
55 .syscall
= regs
->syscall
,
59 regs
->areg
+ XCHAL_NUM_AREGS
- regs
->windowbase
* 4,
60 regs
->windowbase
* 16);
61 memcpy(newregs
.a
+ regs
->windowbase
* 4,
63 (WSBITS
- regs
->windowbase
) * 16);
65 return membuf_write(&to
, &newregs
, sizeof(newregs
));
68 static int gpr_set(struct task_struct
*target
,
69 const struct user_regset
*regset
,
70 unsigned int pos
, unsigned int count
,
71 const void *kbuf
, const void __user
*ubuf
)
74 struct user_pt_regs newregs
= {0};
76 const u32 ps_mask
= PS_CALLINC_MASK
| PS_OWB_MASK
;
78 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &newregs
, 0, -1);
82 if (newregs
.windowbase
>= XCHAL_NUM_AREGS
/ 4)
85 regs
= task_pt_regs(target
);
86 regs
->pc
= newregs
.pc
;
87 regs
->ps
= (regs
->ps
& ~ps_mask
) | (newregs
.ps
& ps_mask
);
88 regs
->lbeg
= newregs
.lbeg
;
89 regs
->lend
= newregs
.lend
;
90 regs
->lcount
= newregs
.lcount
;
91 regs
->sar
= newregs
.sar
;
92 regs
->threadptr
= newregs
.threadptr
;
95 regs
->syscall
= newregs
.syscall
;
97 if (newregs
.windowbase
!= regs
->windowbase
||
98 newregs
.windowstart
!= regs
->windowstart
) {
101 rotws
= (((newregs
.windowstart
|
102 (newregs
.windowstart
<< WSBITS
)) >>
103 newregs
.windowbase
) &
104 ((1 << WSBITS
) - 1)) & ~1;
105 wmask
= ((rotws
? WSBITS
+ 1 - ffs(rotws
) : 0) << 4) |
107 regs
->windowbase
= newregs
.windowbase
;
108 regs
->windowstart
= newregs
.windowstart
;
112 memcpy(regs
->areg
+ XCHAL_NUM_AREGS
- newregs
.windowbase
* 4,
113 newregs
.a
, newregs
.windowbase
* 16);
114 memcpy(regs
->areg
, newregs
.a
+ newregs
.windowbase
* 4,
115 (WSBITS
- newregs
.windowbase
) * 16);
120 static int tie_get(struct task_struct
*target
,
121 const struct user_regset
*regset
,
125 struct pt_regs
*regs
= task_pt_regs(target
);
126 struct thread_info
*ti
= task_thread_info(target
);
127 elf_xtregs_t
*newregs
= kzalloc(sizeof(elf_xtregs_t
), GFP_KERNEL
);
132 newregs
->opt
= regs
->xtregs_opt
;
133 newregs
->user
= ti
->xtregs_user
;
135 #if XTENSA_HAVE_COPROCESSORS
136 /* Flush all coprocessor registers to memory. */
137 coprocessor_flush_all(ti
);
138 newregs
->cp0
= ti
->xtregs_cp
.cp0
;
139 newregs
->cp1
= ti
->xtregs_cp
.cp1
;
140 newregs
->cp2
= ti
->xtregs_cp
.cp2
;
141 newregs
->cp3
= ti
->xtregs_cp
.cp3
;
142 newregs
->cp4
= ti
->xtregs_cp
.cp4
;
143 newregs
->cp5
= ti
->xtregs_cp
.cp5
;
144 newregs
->cp6
= ti
->xtregs_cp
.cp6
;
145 newregs
->cp7
= ti
->xtregs_cp
.cp7
;
147 ret
= membuf_write(&to
, newregs
, sizeof(*newregs
));
152 static int tie_set(struct task_struct
*target
,
153 const struct user_regset
*regset
,
154 unsigned int pos
, unsigned int count
,
155 const void *kbuf
, const void __user
*ubuf
)
158 struct pt_regs
*regs
= task_pt_regs(target
);
159 struct thread_info
*ti
= task_thread_info(target
);
160 elf_xtregs_t
*newregs
= kzalloc(sizeof(elf_xtregs_t
), GFP_KERNEL
);
165 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
170 regs
->xtregs_opt
= newregs
->opt
;
171 ti
->xtregs_user
= newregs
->user
;
173 #if XTENSA_HAVE_COPROCESSORS
174 /* Flush all coprocessors before we overwrite them. */
175 coprocessor_flush_all(ti
);
176 coprocessor_release_all(ti
);
177 ti
->xtregs_cp
.cp0
= newregs
->cp0
;
178 ti
->xtregs_cp
.cp1
= newregs
->cp1
;
179 ti
->xtregs_cp
.cp2
= newregs
->cp2
;
180 ti
->xtregs_cp
.cp3
= newregs
->cp3
;
181 ti
->xtregs_cp
.cp4
= newregs
->cp4
;
182 ti
->xtregs_cp
.cp5
= newregs
->cp5
;
183 ti
->xtregs_cp
.cp6
= newregs
->cp6
;
184 ti
->xtregs_cp
.cp7
= newregs
->cp7
;
196 static const struct user_regset xtensa_regsets
[] = {
198 .core_note_type
= NT_PRSTATUS
,
199 .n
= sizeof(struct user_pt_regs
) / sizeof(u32
),
201 .align
= sizeof(u32
),
202 .regset_get
= gpr_get
,
206 .core_note_type
= NT_PRFPREG
,
207 .n
= sizeof(elf_xtregs_t
) / sizeof(u32
),
209 .align
= sizeof(u32
),
210 .regset_get
= tie_get
,
215 static const struct user_regset_view user_xtensa_view
= {
217 .e_machine
= EM_XTENSA
,
218 .regsets
= xtensa_regsets
,
219 .n
= ARRAY_SIZE(xtensa_regsets
)
222 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
224 return &user_xtensa_view
;
227 void user_enable_single_step(struct task_struct
*child
)
229 child
->ptrace
|= PT_SINGLESTEP
;
232 void user_disable_single_step(struct task_struct
*child
)
234 child
->ptrace
&= ~PT_SINGLESTEP
;
238 * Called by kernel/ptrace.c when detaching to disable single stepping.
241 void ptrace_disable(struct task_struct
*child
)
243 /* Nothing to do.. */
246 static int ptrace_getregs(struct task_struct
*child
, void __user
*uregs
)
248 return copy_regset_to_user(child
, &user_xtensa_view
, REGSET_GPR
,
249 0, sizeof(xtensa_gregset_t
), uregs
);
252 static int ptrace_setregs(struct task_struct
*child
, void __user
*uregs
)
254 return copy_regset_from_user(child
, &user_xtensa_view
, REGSET_GPR
,
255 0, sizeof(xtensa_gregset_t
), uregs
);
258 static int ptrace_getxregs(struct task_struct
*child
, void __user
*uregs
)
260 return copy_regset_to_user(child
, &user_xtensa_view
, REGSET_TIE
,
261 0, sizeof(elf_xtregs_t
), uregs
);
264 static int ptrace_setxregs(struct task_struct
*child
, void __user
*uregs
)
266 return copy_regset_from_user(child
, &user_xtensa_view
, REGSET_TIE
,
267 0, sizeof(elf_xtregs_t
), uregs
);
270 static int ptrace_peekusr(struct task_struct
*child
, long regno
,
273 struct pt_regs
*regs
;
276 regs
= task_pt_regs(child
);
277 tmp
= 0; /* Default return value. */
280 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
281 tmp
= regs
->areg
[regno
- REG_AR_BASE
];
284 case REG_A_BASE
... REG_A_BASE
+ 15:
285 tmp
= regs
->areg
[regno
- REG_A_BASE
];
293 /* Note: PS.EXCM is not set while user task is running;
294 * its being set in regs is for exception handling
297 tmp
= (regs
->ps
& ~(1 << PS_EXCM_BIT
));
305 unsigned long wb
= regs
->windowbase
;
306 unsigned long ws
= regs
->windowstart
;
307 tmp
= ((ws
>> wb
) | (ws
<< (WSBITS
- wb
))) &
334 return put_user(tmp
, ret
);
337 static int ptrace_pokeusr(struct task_struct
*child
, long regno
, long val
)
339 struct pt_regs
*regs
;
340 regs
= task_pt_regs(child
);
343 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
344 regs
->areg
[regno
- REG_AR_BASE
] = val
;
347 case REG_A_BASE
... REG_A_BASE
+ 15:
348 regs
->areg
[regno
- REG_A_BASE
] = val
;
365 #ifdef CONFIG_HAVE_HW_BREAKPOINT
366 static void ptrace_hbptriggered(struct perf_event
*bp
,
367 struct perf_sample_data
*data
,
368 struct pt_regs
*regs
)
371 struct arch_hw_breakpoint
*bkpt
= counter_arch_bp(bp
);
373 if (bp
->attr
.bp_type
& HW_BREAKPOINT_X
) {
374 for (i
= 0; i
< XCHAL_NUM_IBREAK
; ++i
)
375 if (current
->thread
.ptrace_bp
[i
] == bp
)
379 for (i
= 0; i
< XCHAL_NUM_DBREAK
; ++i
)
380 if (current
->thread
.ptrace_wp
[i
] == bp
)
385 force_sig_ptrace_errno_trap(i
, (void __user
*)bkpt
->address
);
388 static struct perf_event
*ptrace_hbp_create(struct task_struct
*tsk
, int type
)
390 struct perf_event_attr attr
;
392 ptrace_breakpoint_init(&attr
);
394 /* Initialise fields to sane defaults. */
400 return register_user_hw_breakpoint(&attr
, ptrace_hbptriggered
, NULL
,
405 * Address bit 0 choose instruction (0) or data (1) break register, bits
406 * 31..1 are the register number.
407 * Both PTRACE_GETHBPREGS and PTRACE_SETHBPREGS transfer two 32-bit words:
408 * address (0) and control (1).
409 * Instruction breakpoint contorl word is 0 to clear breakpoint, 1 to set.
410 * Data breakpoint control word bit 31 is 'trigger on store', bit 30 is
411 * 'trigger on load, bits 29..0 are length. Length 0 is used to clear a
412 * breakpoint. To set a breakpoint length must be a power of 2 in the range
413 * 1..64 and the address must be length-aligned.
416 static long ptrace_gethbpregs(struct task_struct
*child
, long addr
,
419 struct perf_event
*bp
;
420 u32 user_data
[2] = {0};
421 bool dbreak
= addr
& 1;
422 unsigned idx
= addr
>> 1;
424 if ((!dbreak
&& idx
>= XCHAL_NUM_IBREAK
) ||
425 (dbreak
&& idx
>= XCHAL_NUM_DBREAK
))
429 bp
= child
->thread
.ptrace_wp
[idx
];
431 bp
= child
->thread
.ptrace_bp
[idx
];
434 user_data
[0] = bp
->attr
.bp_addr
;
435 user_data
[1] = bp
->attr
.disabled
? 0 : bp
->attr
.bp_len
;
437 if (bp
->attr
.bp_type
& HW_BREAKPOINT_R
)
438 user_data
[1] |= DBREAKC_LOAD_MASK
;
439 if (bp
->attr
.bp_type
& HW_BREAKPOINT_W
)
440 user_data
[1] |= DBREAKC_STOR_MASK
;
444 if (copy_to_user(datap
, user_data
, sizeof(user_data
)))
450 static long ptrace_sethbpregs(struct task_struct
*child
, long addr
,
453 struct perf_event
*bp
;
454 struct perf_event_attr attr
;
456 bool dbreak
= addr
& 1;
457 unsigned idx
= addr
>> 1;
460 if ((!dbreak
&& idx
>= XCHAL_NUM_IBREAK
) ||
461 (dbreak
&& idx
>= XCHAL_NUM_DBREAK
))
464 if (copy_from_user(user_data
, datap
, sizeof(user_data
)))
468 bp
= child
->thread
.ptrace_wp
[idx
];
469 if (user_data
[1] & DBREAKC_LOAD_MASK
)
470 bp_type
|= HW_BREAKPOINT_R
;
471 if (user_data
[1] & DBREAKC_STOR_MASK
)
472 bp_type
|= HW_BREAKPOINT_W
;
474 bp
= child
->thread
.ptrace_bp
[idx
];
475 bp_type
= HW_BREAKPOINT_X
;
479 bp
= ptrace_hbp_create(child
,
480 bp_type
? bp_type
: HW_BREAKPOINT_RW
);
484 child
->thread
.ptrace_wp
[idx
] = bp
;
486 child
->thread
.ptrace_bp
[idx
] = bp
;
490 attr
.bp_addr
= user_data
[0];
491 attr
.bp_len
= user_data
[1] & ~(DBREAKC_LOAD_MASK
| DBREAKC_STOR_MASK
);
492 attr
.bp_type
= bp_type
;
493 attr
.disabled
= !attr
.bp_len
;
495 return modify_user_hw_breakpoint(bp
, &attr
);
499 long arch_ptrace(struct task_struct
*child
, long request
,
500 unsigned long addr
, unsigned long data
)
503 void __user
*datap
= (void __user
*) data
;
506 case PTRACE_PEEKUSR
: /* read register specified by addr. */
507 ret
= ptrace_peekusr(child
, addr
, datap
);
510 case PTRACE_POKEUSR
: /* write register specified by addr. */
511 ret
= ptrace_pokeusr(child
, addr
, data
);
515 ret
= ptrace_getregs(child
, datap
);
519 ret
= ptrace_setregs(child
, datap
);
522 case PTRACE_GETXTREGS
:
523 ret
= ptrace_getxregs(child
, datap
);
526 case PTRACE_SETXTREGS
:
527 ret
= ptrace_setxregs(child
, datap
);
529 #ifdef CONFIG_HAVE_HW_BREAKPOINT
530 case PTRACE_GETHBPREGS
:
531 ret
= ptrace_gethbpregs(child
, addr
, datap
);
534 case PTRACE_SETHBPREGS
:
535 ret
= ptrace_sethbpregs(child
, addr
, datap
);
539 ret
= ptrace_request(child
, request
, addr
, data
);
546 void do_syscall_trace_leave(struct pt_regs
*regs
);
547 int do_syscall_trace_enter(struct pt_regs
*regs
)
549 if (regs
->syscall
== NO_SYSCALL
)
550 regs
->areg
[2] = -ENOSYS
;
552 if (test_thread_flag(TIF_SYSCALL_TRACE
) &&
553 tracehook_report_syscall_entry(regs
)) {
554 regs
->areg
[2] = -ENOSYS
;
555 regs
->syscall
= NO_SYSCALL
;
559 if (regs
->syscall
== NO_SYSCALL
||
560 secure_computing() == -1) {
561 do_syscall_trace_leave(regs
);
565 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT
))
566 trace_sys_enter(regs
, syscall_get_nr(current
, regs
));
568 audit_syscall_entry(regs
->syscall
, regs
->areg
[6],
569 regs
->areg
[3], regs
->areg
[4],
574 void do_syscall_trace_leave(struct pt_regs
*regs
)
578 audit_syscall_exit(regs
);
580 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT
))
581 trace_sys_exit(regs
, regs_return_value(regs
));
583 step
= test_thread_flag(TIF_SINGLESTEP
);
585 if (step
|| test_thread_flag(TIF_SYSCALL_TRACE
))
586 tracehook_report_syscall_exit(regs
, step
);