2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
14 * Copied from i386: Ross Biro 1/23/92
17 #include <linux/kernel.h>
18 #include <linux/ptrace.h>
19 #include <linux/kprobes.h>
20 #include <linux/compat.h>
21 #include <linux/uaccess.h>
22 #include <linux/regset.h>
23 #include <linux/elf.h>
24 #include <linux/tracehook.h>
25 #include <asm/traps.h>
26 #include <arch/chip.h>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/syscalls.h>
31 void user_enable_single_step(struct task_struct
*child
)
33 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
36 void user_disable_single_step(struct task_struct
*child
)
38 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
42 * Called by kernel/ptrace.c when detaching..
44 void ptrace_disable(struct task_struct
*child
)
46 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
49 * These two are currently unused, but will be set by arch_ptrace()
50 * and used in the syscall assembly when we do support them.
52 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
56 * Get registers from task and ready the result for userspace.
57 * Note that we localize the API issues to getregs() and putregs() at
58 * some cost in performance, e.g. we need a full pt_regs copy for
59 * PEEKUSR, and two copies for POKEUSR. But in general we expect
60 * GETREGS/PUTREGS to be the API of choice anyway.
62 static char *getregs(struct task_struct
*child
, struct pt_regs
*uregs
)
64 *uregs
= *task_pt_regs(child
);
66 /* Set up flags ABI bits. */
69 if (task_thread_info(child
)->status
& TS_COMPAT
)
70 uregs
->flags
|= PT_FLAGS_COMPAT
;
76 /* Put registers back to task. */
77 static void putregs(struct task_struct
*child
, struct pt_regs
*uregs
)
79 struct pt_regs
*regs
= task_pt_regs(child
);
81 /* Don't allow overwriting the kernel-internal flags word. */
82 uregs
->flags
= regs
->flags
;
84 /* Only allow setting the ICS bit in the ex1 word. */
85 uregs
->ex1
= PL_ICS_EX1(USER_PL
, EX1_ICS(uregs
->ex1
));
94 static int tile_gpr_get(struct task_struct
*target
,
95 const struct user_regset
*regset
,
96 unsigned int pos
, unsigned int count
,
97 void *kbuf
, void __user
*ubuf
)
101 getregs(target
, ®s
);
103 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, ®s
, 0,
107 static int tile_gpr_set(struct task_struct
*target
,
108 const struct user_regset
*regset
,
109 unsigned int pos
, unsigned int count
,
110 const void *kbuf
, const void __user
*ubuf
)
115 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, ®s
, 0,
120 putregs(target
, ®s
);
125 static const struct user_regset tile_user_regset
[] = {
127 .core_note_type
= NT_PRSTATUS
,
129 .size
= sizeof(elf_greg_t
),
130 .align
= sizeof(elf_greg_t
),
136 static const struct user_regset_view tile_user_regset_view
= {
137 .name
= CHIP_ARCH_NAME
,
138 .e_machine
= ELF_ARCH
,
139 .ei_osabi
= ELF_OSABI
,
140 .regsets
= tile_user_regset
,
141 .n
= ARRAY_SIZE(tile_user_regset
),
144 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
146 return &tile_user_regset_view
;
149 long arch_ptrace(struct task_struct
*child
, long request
,
150 unsigned long addr
, unsigned long data
)
152 unsigned long __user
*datap
= (long __user __force
*)data
;
156 struct pt_regs copyregs
;
160 case PTRACE_PEEKUSR
: /* Read register from pt_regs. */
161 if (addr
>= PTREGS_SIZE
)
163 childreg
= getregs(child
, ©regs
) + addr
;
165 if (is_compat_task()) {
166 if (addr
& (sizeof(compat_long_t
)-1))
168 ret
= put_user(*(compat_long_t
*)childreg
,
169 (compat_long_t __user
*)datap
);
173 if (addr
& (sizeof(long)-1))
175 ret
= put_user(*(long *)childreg
, datap
);
179 case PTRACE_POKEUSR
: /* Write register in pt_regs. */
180 if (addr
>= PTREGS_SIZE
)
182 childreg
= getregs(child
, ©regs
) + addr
;
184 if (is_compat_task()) {
185 if (addr
& (sizeof(compat_long_t
)-1))
187 *(compat_long_t
*)childreg
= data
;
191 if (addr
& (sizeof(long)-1))
193 *(long *)childreg
= data
;
195 putregs(child
, ©regs
);
199 case PTRACE_GETREGS
: /* Get all registers from the child. */
200 ret
= copy_regset_to_user(child
, &tile_user_regset_view
,
202 sizeof(struct pt_regs
), datap
);
205 case PTRACE_SETREGS
: /* Set all registers in the child. */
206 ret
= copy_regset_from_user(child
, &tile_user_regset_view
,
208 sizeof(struct pt_regs
), datap
);
211 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
212 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
215 case PTRACE_SETOPTIONS
:
216 /* Support TILE-specific ptrace options. */
217 BUILD_BUG_ON(PTRACE_O_MASK_TILE
& PTRACE_O_MASK
);
218 tmp
= data
& PTRACE_O_MASK_TILE
;
219 data
&= ~PTRACE_O_MASK_TILE
;
220 ret
= ptrace_request(child
, request
, addr
, data
);
222 unsigned int flags
= child
->ptrace
;
223 flags
&= ~(PTRACE_O_MASK_TILE
<< PT_OPT_FLAG_SHIFT
);
224 flags
|= (tmp
<< PT_OPT_FLAG_SHIFT
);
225 child
->ptrace
= flags
;
231 if (task_thread_info(current
)->status
& TS_COMPAT
) {
232 ret
= compat_ptrace_request(child
, request
,
237 ret
= ptrace_request(child
, request
, addr
, data
);
245 /* Not used; we handle compat issues in arch_ptrace() directly. */
246 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
247 compat_ulong_t addr
, compat_ulong_t data
)
253 int do_syscall_trace_enter(struct pt_regs
*regs
)
255 if (test_thread_flag(TIF_SYSCALL_TRACE
)) {
256 if (tracehook_report_syscall_entry(regs
))
257 regs
->regs
[TREG_SYSCALL_NR
] = -1;
260 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT
))
261 trace_sys_enter(regs
, regs
->regs
[TREG_SYSCALL_NR
]);
263 return regs
->regs
[TREG_SYSCALL_NR
];
266 void do_syscall_trace_exit(struct pt_regs
*regs
)
271 * The standard tile calling convention returns the value (or negative
272 * errno) in r0, and zero (or positive errno) in r1.
273 * It saves a couple of cycles on the hot path to do this work in
274 * registers only as we return, rather than updating the in-memory
277 errno
= (long) regs
->regs
[0];
278 if (errno
< 0 && errno
> -4096)
279 regs
->regs
[1] = -errno
;
283 if (test_thread_flag(TIF_SYSCALL_TRACE
))
284 tracehook_report_syscall_exit(regs
, 0);
286 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT
))
287 trace_sys_exit(regs
, regs
->regs
[0]);
290 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
)
294 memset(&info
, 0, sizeof(info
));
295 info
.si_signo
= SIGTRAP
;
296 info
.si_code
= TRAP_BRKPT
;
297 info
.si_addr
= (void __user
*) regs
->pc
;
299 /* Send us the fakey SIGTRAP */
300 force_sig_info(SIGTRAP
, &info
, tsk
);
303 /* Handle synthetic interrupt delivered only by the simulator. */
304 void __kprobes
do_breakpoint(struct pt_regs
* regs
, int fault_num
)
306 send_sigtrap(current
, regs
);