]>
Commit | Line | Data |
---|---|---|
5f97f7f9 HS |
1 | /* |
2 | * Copyright (C) 2004-2006 Atmel Corporation | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | */ | |
8 | #undef DEBUG | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/sched.h> | |
68db0cf1 | 11 | #include <linux/sched/task_stack.h> |
5f97f7f9 | 12 | #include <linux/mm.h> |
5f97f7f9 HS |
13 | #include <linux/ptrace.h> |
14 | #include <linux/errno.h> | |
15 | #include <linux/user.h> | |
16 | #include <linux/security.h> | |
17 | #include <linux/unistd.h> | |
18 | #include <linux/notifier.h> | |
19 | ||
20 | #include <asm/traps.h> | |
7c0f6ba6 | 21 | #include <linux/uaccess.h> |
5f97f7f9 HS |
22 | #include <asm/ocd.h> |
23 | #include <asm/mmu_context.h> | |
1eeb66a1 | 24 | #include <linux/kdebug.h> |
5f97f7f9 HS |
25 | |
26 | static struct pt_regs *get_user_regs(struct task_struct *tsk) | |
27 | { | |
c9f4f06d | 28 | return (struct pt_regs *)((unsigned long)task_stack_page(tsk) + |
5f97f7f9 HS |
29 | THREAD_SIZE - sizeof(struct pt_regs)); |
30 | } | |
31 | ||
9e584fbb | 32 | void user_enable_single_step(struct task_struct *tsk) |
5f97f7f9 | 33 | { |
1d839317 | 34 | pr_debug("user_enable_single_step: pid=%u, PC=0x%08lx, SR=0x%08lx\n", |
2507bc13 | 35 | tsk->pid, task_pt_regs(tsk)->pc, task_pt_regs(tsk)->sr); |
5f97f7f9 | 36 | |
2507bc13 HS |
37 | /* |
38 | * We can't schedule in Debug mode, so when TIF_BREAKPOINT is | |
39 | * set, the system call or exception handler will do a | |
40 | * breakpoint to enter monitor mode before returning to | |
41 | * userspace. | |
42 | * | |
43 | * The monitor code will then notice that TIF_SINGLE_STEP is | |
44 | * set and return to userspace with single stepping enabled. | |
45 | * The CPU will then enter monitor mode again after exactly | |
46 | * one instruction has been executed, and the monitor code | |
47 | * will then send a SIGTRAP to the process. | |
48 | */ | |
49 | set_tsk_thread_flag(tsk, TIF_BREAKPOINT); | |
5f97f7f9 HS |
50 | set_tsk_thread_flag(tsk, TIF_SINGLE_STEP); |
51 | } | |
52 | ||
1d839317 CH |
53 | void user_disable_single_step(struct task_struct *child) |
54 | { | |
55 | /* XXX(hch): a no-op here seems wrong.. */ | |
56 | } | |
57 | ||
5f97f7f9 HS |
58 | /* |
59 | * Called by kernel/ptrace.c when detaching | |
60 | * | |
61 | * Make sure any single step bits, etc. are not set | |
62 | */ | |
63 | void ptrace_disable(struct task_struct *child) | |
64 | { | |
65 | clear_tsk_thread_flag(child, TIF_SINGLE_STEP); | |
2507bc13 | 66 | clear_tsk_thread_flag(child, TIF_BREAKPOINT); |
13b54a50 | 67 | ocd_disable(child); |
5f97f7f9 HS |
68 | } |
69 | ||
70 | /* | |
71 | * Read the word at offset "offset" into the task's "struct user". We | |
72 | * actually access the pt_regs struct stored on the kernel stack. | |
73 | */ | |
74 | static int ptrace_read_user(struct task_struct *tsk, unsigned long offset, | |
75 | unsigned long __user *data) | |
76 | { | |
77 | unsigned long *regs; | |
78 | unsigned long value; | |
79 | ||
5f97f7f9 HS |
80 | if (offset & 3 || offset >= sizeof(struct user)) { |
81 | printk("ptrace_read_user: invalid offset 0x%08lx\n", offset); | |
82 | return -EIO; | |
83 | } | |
84 | ||
85 | regs = (unsigned long *)get_user_regs(tsk); | |
86 | ||
87 | value = 0; | |
88 | if (offset < sizeof(struct pt_regs)) | |
89 | value = regs[offset / sizeof(regs[0])]; | |
90 | ||
2507bc13 HS |
91 | pr_debug("ptrace_read_user(%s[%u], %#lx, %p) -> %#lx\n", |
92 | tsk->comm, tsk->pid, offset, data, value); | |
93 | ||
5f97f7f9 HS |
94 | return put_user(value, data); |
95 | } | |
96 | ||
97 | /* | |
98 | * Write the word "value" to offset "offset" into the task's "struct | |
99 | * user". We actually access the pt_regs struct stored on the kernel | |
100 | * stack. | |
101 | */ | |
102 | static int ptrace_write_user(struct task_struct *tsk, unsigned long offset, | |
103 | unsigned long value) | |
104 | { | |
105 | unsigned long *regs; | |
106 | ||
2507bc13 HS |
107 | pr_debug("ptrace_write_user(%s[%u], %#lx, %#lx)\n", |
108 | tsk->comm, tsk->pid, offset, value); | |
109 | ||
5f97f7f9 | 110 | if (offset & 3 || offset >= sizeof(struct user)) { |
2507bc13 | 111 | pr_debug(" invalid offset 0x%08lx\n", offset); |
5f97f7f9 HS |
112 | return -EIO; |
113 | } | |
114 | ||
115 | if (offset >= sizeof(struct pt_regs)) | |
116 | return 0; | |
117 | ||
118 | regs = (unsigned long *)get_user_regs(tsk); | |
119 | regs[offset / sizeof(regs[0])] = value; | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | static int ptrace_getregs(struct task_struct *tsk, void __user *uregs) | |
125 | { | |
126 | struct pt_regs *regs = get_user_regs(tsk); | |
127 | ||
128 | return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0; | |
129 | } | |
130 | ||
131 | static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs) | |
132 | { | |
133 | struct pt_regs newregs; | |
134 | int ret; | |
135 | ||
136 | ret = -EFAULT; | |
137 | if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) { | |
138 | struct pt_regs *regs = get_user_regs(tsk); | |
139 | ||
140 | ret = -EINVAL; | |
141 | if (valid_user_regs(&newregs)) { | |
142 | *regs = newregs; | |
143 | ret = 0; | |
144 | } | |
145 | } | |
146 | ||
147 | return ret; | |
148 | } | |
149 | ||
9b05a69e NK |
150 | long arch_ptrace(struct task_struct *child, long request, |
151 | unsigned long addr, unsigned long data) | |
5f97f7f9 | 152 | { |
5f97f7f9 | 153 | int ret; |
9f29b8fb | 154 | void __user *datap = (void __user *) data; |
5f97f7f9 | 155 | |
5f97f7f9 HS |
156 | switch (request) { |
157 | /* Read the word at location addr in the child process */ | |
158 | case PTRACE_PEEKTEXT: | |
159 | case PTRACE_PEEKDATA: | |
76647323 | 160 | ret = generic_ptrace_peekdata(child, addr, data); |
5f97f7f9 HS |
161 | break; |
162 | ||
163 | case PTRACE_PEEKUSR: | |
9f29b8fb | 164 | ret = ptrace_read_user(child, addr, datap); |
5f97f7f9 HS |
165 | break; |
166 | ||
167 | /* Write the word in data at location addr */ | |
168 | case PTRACE_POKETEXT: | |
169 | case PTRACE_POKEDATA: | |
f284ce72 | 170 | ret = generic_ptrace_pokedata(child, addr, data); |
5f97f7f9 HS |
171 | break; |
172 | ||
173 | case PTRACE_POKEUSR: | |
174 | ret = ptrace_write_user(child, addr, data); | |
175 | break; | |
176 | ||
5f97f7f9 | 177 | case PTRACE_GETREGS: |
9f29b8fb | 178 | ret = ptrace_getregs(child, datap); |
5f97f7f9 HS |
179 | break; |
180 | ||
181 | case PTRACE_SETREGS: | |
9f29b8fb | 182 | ret = ptrace_setregs(child, datap); |
5f97f7f9 HS |
183 | break; |
184 | ||
185 | default: | |
186 | ret = ptrace_request(child, request, addr, data); | |
187 | break; | |
188 | } | |
189 | ||
5f97f7f9 HS |
190 | return ret; |
191 | } | |
192 | ||
193 | asmlinkage void syscall_trace(void) | |
194 | { | |
5f97f7f9 HS |
195 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
196 | return; | |
197 | if (!(current->ptrace & PT_PTRACED)) | |
198 | return; | |
199 | ||
5f97f7f9 HS |
200 | /* The 0x80 provides a way for the tracing parent to |
201 | * distinguish between a syscall stop and SIGTRAP delivery */ | |
202 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | |
203 | ? 0x80 : 0)); | |
204 | ||
205 | /* | |
206 | * this isn't the same as continuing with a signal, but it | |
207 | * will do for normal use. strace only continues with a | |
208 | * signal if the stopping signal is not SIGTRAP. -brl | |
209 | */ | |
210 | if (current->exit_code) { | |
211 | pr_debug("syscall_trace: sending signal %d to PID %u\n", | |
212 | current->exit_code, current->pid); | |
213 | send_sig(current->exit_code, current, 1); | |
214 | current->exit_code = 0; | |
215 | } | |
216 | } | |
217 | ||
5f97f7f9 | 218 | /* |
2507bc13 HS |
219 | * debug_trampoline() is an assembly stub which will store all user |
220 | * registers on the stack and execute a breakpoint instruction. | |
221 | * | |
222 | * If we single-step into an exception handler which runs with | |
223 | * interrupts disabled the whole time so it doesn't have to check for | |
224 | * pending work, its return address will be modified so that it ends | |
225 | * up returning to debug_trampoline. | |
226 | * | |
227 | * If the exception handler decides to store the user context and | |
228 | * enable interrupts after all, it will restore the original return | |
229 | * address and status register value. Before it returns, it will | |
230 | * notice that TIF_BREAKPOINT is set and execute a breakpoint | |
231 | * instruction. | |
5f97f7f9 | 232 | */ |
2507bc13 | 233 | extern void debug_trampoline(void); |
5f97f7f9 | 234 | |
2507bc13 HS |
235 | asmlinkage struct pt_regs *do_debug(struct pt_regs *regs) |
236 | { | |
237 | struct thread_info *ti; | |
238 | unsigned long trampoline_addr; | |
239 | u32 status; | |
240 | u32 ctrl; | |
241 | int code; | |
242 | ||
243 | status = ocd_read(DS); | |
244 | ti = current_thread_info(); | |
245 | code = TRAP_BRKPT; | |
246 | ||
247 | pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n", | |
248 | status, regs->pc, regs->sr, ti->flags); | |
249 | ||
250 | if (!user_mode(regs)) { | |
251 | unsigned long die_val = DIE_BREAKPOINT; | |
252 | ||
253 | if (status & (1 << OCD_DS_SSS_BIT)) | |
254 | die_val = DIE_SSTEP; | |
255 | ||
256 | if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) | |
257 | == NOTIFY_STOP) | |
258 | return regs; | |
259 | ||
260 | if ((status & (1 << OCD_DS_SWB_BIT)) | |
261 | && test_and_clear_ti_thread_flag( | |
262 | ti, TIF_BREAKPOINT)) { | |
263 | /* | |
264 | * Explicit breakpoint from trampoline or | |
265 | * exception/syscall/interrupt handler. | |
266 | * | |
267 | * The real saved regs are on the stack right | |
268 | * after the ones we saved on entry. | |
269 | */ | |
270 | regs++; | |
271 | pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:" | |
272 | "PC=0x%08lx SR=0x%08lx\n", | |
273 | regs->pc, regs->sr); | |
274 | BUG_ON(!user_mode(regs)); | |
275 | ||
276 | if (test_thread_flag(TIF_SINGLE_STEP)) { | |
277 | pr_debug("Going to do single step...\n"); | |
278 | return regs; | |
279 | } | |
280 | ||
281 | /* | |
282 | * No TIF_SINGLE_STEP means we're done | |
283 | * stepping over a syscall. Do the trap now. | |
284 | */ | |
285 | code = TRAP_TRACE; | |
286 | } else if ((status & (1 << OCD_DS_SSS_BIT)) | |
287 | && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) { | |
288 | ||
289 | pr_debug("Stepped into something, " | |
290 | "setting TIF_BREAKPOINT...\n"); | |
291 | set_ti_thread_flag(ti, TIF_BREAKPOINT); | |
292 | ||
293 | /* | |
294 | * We stepped into an exception, interrupt or | |
295 | * syscall handler. Some exception handlers | |
296 | * don't check for pending work, so we need to | |
297 | * set up a trampoline just in case. | |
298 | * | |
299 | * The exception entry code will undo the | |
300 | * trampoline stuff if it does a full context | |
301 | * save (which also means that it'll check for | |
302 | * pending work later.) | |
303 | */ | |
304 | if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) { | |
305 | trampoline_addr | |
306 | = (unsigned long)&debug_trampoline; | |
307 | ||
308 | pr_debug("Setting up trampoline...\n"); | |
309 | ti->rar_saved = sysreg_read(RAR_EX); | |
310 | ti->rsr_saved = sysreg_read(RSR_EX); | |
311 | sysreg_write(RAR_EX, trampoline_addr); | |
312 | sysreg_write(RSR_EX, (MODE_EXCEPTION | |
313 | | SR_EM | SR_GM)); | |
314 | BUG_ON(ti->rsr_saved & MODE_MASK); | |
315 | } | |
316 | ||
317 | /* | |
318 | * If we stepped into a system call, we | |
319 | * shouldn't do a single step after we return | |
320 | * since the return address is right after the | |
321 | * "scall" instruction we were told to step | |
322 | * over. | |
323 | */ | |
324 | if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) { | |
325 | pr_debug("Supervisor; no single step\n"); | |
326 | clear_ti_thread_flag(ti, TIF_SINGLE_STEP); | |
327 | } | |
328 | ||
329 | ctrl = ocd_read(DC); | |
330 | ctrl &= ~(1 << OCD_DC_SS_BIT); | |
331 | ocd_write(DC, ctrl); | |
332 | ||
333 | return regs; | |
334 | } else { | |
335 | printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n", | |
336 | status); | |
337 | printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags); | |
338 | die("Unhandled debug trap in kernel mode", | |
339 | regs, SIGTRAP); | |
340 | } | |
341 | } else if (status & (1 << OCD_DS_SSS_BIT)) { | |
342 | /* Single step in user mode */ | |
343 | code = TRAP_TRACE; | |
5f97f7f9 | 344 | |
2507bc13 HS |
345 | ctrl = ocd_read(DC); |
346 | ctrl &= ~(1 << OCD_DC_SS_BIT); | |
347 | ocd_write(DC, ctrl); | |
5f97f7f9 HS |
348 | } |
349 | ||
2507bc13 HS |
350 | pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n", |
351 | code, regs->pc, regs->sr); | |
5f97f7f9 | 352 | |
2507bc13 HS |
353 | clear_thread_flag(TIF_SINGLE_STEP); |
354 | _exception(SIGTRAP, regs, code, instruction_pointer(regs)); | |
355 | ||
356 | return regs; | |
5f97f7f9 | 357 | } |