Loading...
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
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.
7 *
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
12 * more details.
13 *
14 * Copied from i386: Ross Biro 1/23/92
15 */
16
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 <asm/traps.h>
23
24void user_enable_single_step(struct task_struct *child)
25{
26 set_tsk_thread_flag(child, TIF_SINGLESTEP);
27}
28
29void user_disable_single_step(struct task_struct *child)
30{
31 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
32}
33
34/*
35 * Called by kernel/ptrace.c when detaching..
36 */
37void ptrace_disable(struct task_struct *child)
38{
39 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
40
41 /*
42 * These two are currently unused, but will be set by arch_ptrace()
43 * and used in the syscall assembly when we do support them.
44 */
45 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
46}
47
48long arch_ptrace(struct task_struct *child, long request,
49 unsigned long addr, unsigned long data)
50{
51 unsigned long __user *datap = (long __user __force *)data;
52 unsigned long tmp;
53 long ret = -EIO;
54 char *childreg;
55 struct pt_regs copyregs;
56 int ex1_offset;
57
58 switch (request) {
59
60 case PTRACE_PEEKUSR: /* Read register from pt_regs. */
61 if (addr >= PTREGS_SIZE)
62 break;
63 childreg = (char *)task_pt_regs(child) + addr;
64#ifdef CONFIG_COMPAT
65 if (is_compat_task()) {
66 if (addr & (sizeof(compat_long_t)-1))
67 break;
68 ret = put_user(*(compat_long_t *)childreg,
69 (compat_long_t __user *)datap);
70 } else
71#endif
72 {
73 if (addr & (sizeof(long)-1))
74 break;
75 ret = put_user(*(long *)childreg, datap);
76 }
77 break;
78
79 case PTRACE_POKEUSR: /* Write register in pt_regs. */
80 if (addr >= PTREGS_SIZE)
81 break;
82 childreg = (char *)task_pt_regs(child) + addr;
83
84 /* Guard against overwrites of the privilege level. */
85 ex1_offset = PTREGS_OFFSET_EX1;
86#if defined(CONFIG_COMPAT) && defined(__BIG_ENDIAN)
87 if (is_compat_task()) /* point at low word */
88 ex1_offset += sizeof(compat_long_t);
89#endif
90 if (addr == ex1_offset)
91 data = PL_ICS_EX1(USER_PL, EX1_ICS(data));
92
93#ifdef CONFIG_COMPAT
94 if (is_compat_task()) {
95 if (addr & (sizeof(compat_long_t)-1))
96 break;
97 *(compat_long_t *)childreg = data;
98 } else
99#endif
100 {
101 if (addr & (sizeof(long)-1))
102 break;
103 *(long *)childreg = data;
104 }
105 ret = 0;
106 break;
107
108 case PTRACE_GETREGS: /* Get all registers from the child. */
109 if (copy_to_user(datap, task_pt_regs(child),
110 sizeof(struct pt_regs)) == 0) {
111 ret = 0;
112 }
113 break;
114
115 case PTRACE_SETREGS: /* Set all registers in the child. */
116 if (copy_from_user(©regs, datap,
117 sizeof(struct pt_regs)) == 0) {
118 copyregs.ex1 =
119 PL_ICS_EX1(USER_PL, EX1_ICS(copyregs.ex1));
120 *task_pt_regs(child) = copyregs;
121 ret = 0;
122 }
123 break;
124
125 case PTRACE_GETFPREGS: /* Get the child FPU state. */
126 case PTRACE_SETFPREGS: /* Set the child FPU state. */
127 break;
128
129 case PTRACE_SETOPTIONS:
130 /* Support TILE-specific ptrace options. */
131 child->ptrace &= ~PT_TRACE_MASK_TILE;
132 tmp = data & PTRACE_O_MASK_TILE;
133 data &= ~PTRACE_O_MASK_TILE;
134 ret = ptrace_request(child, request, addr, data);
135 if (tmp & PTRACE_O_TRACEMIGRATE)
136 child->ptrace |= PT_TRACE_MIGRATE;
137 break;
138
139 default:
140#ifdef CONFIG_COMPAT
141 if (task_thread_info(current)->status & TS_COMPAT) {
142 ret = compat_ptrace_request(child, request,
143 addr, data);
144 break;
145 }
146#endif
147 ret = ptrace_request(child, request, addr, data);
148 break;
149 }
150
151 return ret;
152}
153
154#ifdef CONFIG_COMPAT
155/* Not used; we handle compat issues in arch_ptrace() directly. */
156long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
157 compat_ulong_t addr, compat_ulong_t data)
158{
159 BUG();
160}
161#endif
162
163void do_syscall_trace(void)
164{
165 if (!test_thread_flag(TIF_SYSCALL_TRACE))
166 return;
167
168 if (!(current->ptrace & PT_PTRACED))
169 return;
170
171 /*
172 * The 0x80 provides a way for the tracing parent to distinguish
173 * between a syscall stop and SIGTRAP delivery
174 */
175 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
176
177 /*
178 * this isn't the same as continuing with a signal, but it will do
179 * for normal use. strace only continues with a signal if the
180 * stopping signal is not SIGTRAP. -brl
181 */
182 if (current->exit_code) {
183 send_sig(current->exit_code, current, 1);
184 current->exit_code = 0;
185 }
186}
187
188void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
189{
190 struct siginfo info;
191
192 memset(&info, 0, sizeof(info));
193 info.si_signo = SIGTRAP;
194 info.si_code = TRAP_BRKPT;
195 info.si_addr = (void __user *) regs->pc;
196
197 /* Send us the fakey SIGTRAP */
198 force_sig_info(SIGTRAP, &info, tsk);
199}
200
201/* Handle synthetic interrupt delivered only by the simulator. */
202void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num)
203{
204 send_sigtrap(current, regs, fault_num);
205}
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
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.
7 *
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
12 * more details.
13 *
14 * Copied from i386: Ross Biro 1/23/92
15 */
16
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 <linux/context_tracking.h>
26#include <asm/traps.h>
27#include <arch/chip.h>
28
29#define CREATE_TRACE_POINTS
30#include <trace/events/syscalls.h>
31
32void user_enable_single_step(struct task_struct *child)
33{
34 set_tsk_thread_flag(child, TIF_SINGLESTEP);
35}
36
37void user_disable_single_step(struct task_struct *child)
38{
39 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
40}
41
42/*
43 * Called by kernel/ptrace.c when detaching..
44 */
45void ptrace_disable(struct task_struct *child)
46{
47 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
48
49 /*
50 * These two are currently unused, but will be set by arch_ptrace()
51 * and used in the syscall assembly when we do support them.
52 */
53 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
54}
55
56/*
57 * Get registers from task and ready the result for userspace.
58 * Note that we localize the API issues to getregs() and putregs() at
59 * some cost in performance, e.g. we need a full pt_regs copy for
60 * PEEKUSR, and two copies for POKEUSR. But in general we expect
61 * GETREGS/PUTREGS to be the API of choice anyway.
62 */
63static char *getregs(struct task_struct *child, struct pt_regs *uregs)
64{
65 *uregs = *task_pt_regs(child);
66
67 /* Set up flags ABI bits. */
68 uregs->flags = 0;
69#ifdef CONFIG_COMPAT
70 if (task_thread_info(child)->status & TS_COMPAT)
71 uregs->flags |= PT_FLAGS_COMPAT;
72#endif
73
74 return (char *)uregs;
75}
76
77/* Put registers back to task. */
78static void putregs(struct task_struct *child, struct pt_regs *uregs)
79{
80 struct pt_regs *regs = task_pt_regs(child);
81
82 /* Don't allow overwriting the kernel-internal flags word. */
83 uregs->flags = regs->flags;
84
85 /* Only allow setting the ICS bit in the ex1 word. */
86 uregs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(uregs->ex1));
87
88 *regs = *uregs;
89}
90
91enum tile_regset {
92 REGSET_GPR,
93};
94
95static int tile_gpr_get(struct task_struct *target,
96 const struct user_regset *regset,
97 unsigned int pos, unsigned int count,
98 void *kbuf, void __user *ubuf)
99{
100 struct pt_regs regs;
101
102 getregs(target, ®s);
103
104 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, ®s, 0,
105 sizeof(regs));
106}
107
108static int tile_gpr_set(struct task_struct *target,
109 const struct user_regset *regset,
110 unsigned int pos, unsigned int count,
111 const void *kbuf, const void __user *ubuf)
112{
113 int ret;
114 struct pt_regs regs;
115
116 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, ®s, 0,
117 sizeof(regs));
118 if (ret)
119 return ret;
120
121 putregs(target, ®s);
122
123 return 0;
124}
125
126static const struct user_regset tile_user_regset[] = {
127 [REGSET_GPR] = {
128 .core_note_type = NT_PRSTATUS,
129 .n = ELF_NGREG,
130 .size = sizeof(elf_greg_t),
131 .align = sizeof(elf_greg_t),
132 .get = tile_gpr_get,
133 .set = tile_gpr_set,
134 },
135};
136
137static const struct user_regset_view tile_user_regset_view = {
138 .name = CHIP_ARCH_NAME,
139 .e_machine = ELF_ARCH,
140 .ei_osabi = ELF_OSABI,
141 .regsets = tile_user_regset,
142 .n = ARRAY_SIZE(tile_user_regset),
143};
144
145const struct user_regset_view *task_user_regset_view(struct task_struct *task)
146{
147 return &tile_user_regset_view;
148}
149
150long arch_ptrace(struct task_struct *child, long request,
151 unsigned long addr, unsigned long data)
152{
153 unsigned long __user *datap = (long __user __force *)data;
154 unsigned long tmp;
155 long ret = -EIO;
156 char *childreg;
157 struct pt_regs copyregs;
158
159 switch (request) {
160
161 case PTRACE_PEEKUSR: /* Read register from pt_regs. */
162 if (addr >= PTREGS_SIZE)
163 break;
164 childreg = getregs(child, ©regs) + addr;
165#ifdef CONFIG_COMPAT
166 if (is_compat_task()) {
167 if (addr & (sizeof(compat_long_t)-1))
168 break;
169 ret = put_user(*(compat_long_t *)childreg,
170 (compat_long_t __user *)datap);
171 } else
172#endif
173 {
174 if (addr & (sizeof(long)-1))
175 break;
176 ret = put_user(*(long *)childreg, datap);
177 }
178 break;
179
180 case PTRACE_POKEUSR: /* Write register in pt_regs. */
181 if (addr >= PTREGS_SIZE)
182 break;
183 childreg = getregs(child, ©regs) + addr;
184#ifdef CONFIG_COMPAT
185 if (is_compat_task()) {
186 if (addr & (sizeof(compat_long_t)-1))
187 break;
188 *(compat_long_t *)childreg = data;
189 } else
190#endif
191 {
192 if (addr & (sizeof(long)-1))
193 break;
194 *(long *)childreg = data;
195 }
196 putregs(child, ©regs);
197 ret = 0;
198 break;
199
200 case PTRACE_GETREGS: /* Get all registers from the child. */
201 ret = copy_regset_to_user(child, &tile_user_regset_view,
202 REGSET_GPR, 0,
203 sizeof(struct pt_regs), datap);
204 break;
205
206 case PTRACE_SETREGS: /* Set all registers in the child. */
207 ret = copy_regset_from_user(child, &tile_user_regset_view,
208 REGSET_GPR, 0,
209 sizeof(struct pt_regs), datap);
210 break;
211
212 case PTRACE_GETFPREGS: /* Get the child FPU state. */
213 case PTRACE_SETFPREGS: /* Set the child FPU state. */
214 break;
215
216 case PTRACE_SETOPTIONS:
217 /* Support TILE-specific ptrace options. */
218 BUILD_BUG_ON(PTRACE_O_MASK_TILE & PTRACE_O_MASK);
219 tmp = data & PTRACE_O_MASK_TILE;
220 data &= ~PTRACE_O_MASK_TILE;
221 ret = ptrace_request(child, request, addr, data);
222 if (ret == 0) {
223 unsigned int flags = child->ptrace;
224 flags &= ~(PTRACE_O_MASK_TILE << PT_OPT_FLAG_SHIFT);
225 flags |= (tmp << PT_OPT_FLAG_SHIFT);
226 child->ptrace = flags;
227 }
228 break;
229
230 default:
231#ifdef CONFIG_COMPAT
232 if (task_thread_info(current)->status & TS_COMPAT) {
233 ret = compat_ptrace_request(child, request,
234 addr, data);
235 break;
236 }
237#endif
238 ret = ptrace_request(child, request, addr, data);
239 break;
240 }
241
242 return ret;
243}
244
245#ifdef CONFIG_COMPAT
246/* Not used; we handle compat issues in arch_ptrace() directly. */
247long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
248 compat_ulong_t addr, compat_ulong_t data)
249{
250 BUG();
251}
252#endif
253
254int do_syscall_trace_enter(struct pt_regs *regs)
255{
256 u32 work = ACCESS_ONCE(current_thread_info()->flags);
257
258 if (secure_computing() == -1)
259 return -1;
260
261 if (work & _TIF_SYSCALL_TRACE) {
262 if (tracehook_report_syscall_entry(regs))
263 regs->regs[TREG_SYSCALL_NR] = -1;
264 }
265
266 if (work & _TIF_SYSCALL_TRACEPOINT)
267 trace_sys_enter(regs, regs->regs[TREG_SYSCALL_NR]);
268
269 return regs->regs[TREG_SYSCALL_NR];
270}
271
272void do_syscall_trace_exit(struct pt_regs *regs)
273{
274 long errno;
275
276 /*
277 * The standard tile calling convention returns the value (or negative
278 * errno) in r0, and zero (or positive errno) in r1.
279 * It saves a couple of cycles on the hot path to do this work in
280 * registers only as we return, rather than updating the in-memory
281 * struct ptregs.
282 */
283 errno = (long) regs->regs[0];
284 if (errno < 0 && errno > -4096)
285 regs->regs[1] = -errno;
286 else
287 regs->regs[1] = 0;
288
289 if (test_thread_flag(TIF_SYSCALL_TRACE))
290 tracehook_report_syscall_exit(regs, 0);
291
292 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
293 trace_sys_exit(regs, regs->regs[0]);
294}
295
296void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs)
297{
298 struct siginfo info;
299
300 memset(&info, 0, sizeof(info));
301 info.si_signo = SIGTRAP;
302 info.si_code = TRAP_BRKPT;
303 info.si_addr = (void __user *) regs->pc;
304
305 /* Send us the fakey SIGTRAP */
306 force_sig_info(SIGTRAP, &info, tsk);
307}
308
309/* Handle synthetic interrupt delivered only by the simulator. */
310void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num)
311{
312 send_sigtrap(current, regs);
313}