]>
Commit | Line | Data |
---|---|---|
1394f032 | 1 | /* |
96f1050d | 2 | * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds |
e8f263df | 3 | * these modifications are Copyright 2004-2010 Analog Devices Inc. |
1394f032 | 4 | * |
96f1050d | 5 | * Licensed under the GPL-2 |
1394f032 BW |
6 | */ |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/sched.h> | |
10 | #include <linux/mm.h> | |
11 | #include <linux/smp.h> | |
e50e2f25 | 12 | #include <linux/elf.h> |
1394f032 BW |
13 | #include <linux/errno.h> |
14 | #include <linux/ptrace.h> | |
15 | #include <linux/user.h> | |
e50e2f25 | 16 | #include <linux/regset.h> |
1394f032 | 17 | #include <linux/signal.h> |
e8f263df | 18 | #include <linux/tracehook.h> |
1f83b8f1 | 19 | #include <linux/uaccess.h> |
1394f032 | 20 | |
1394f032 BW |
21 | #include <asm/page.h> |
22 | #include <asm/pgtable.h> | |
1394f032 BW |
23 | #include <asm/processor.h> |
24 | #include <asm/asm-offsets.h> | |
25 | #include <asm/dma.h> | |
26156397 | 26 | #include <asm/fixed_code.h> |
7786ce82 | 27 | #include <asm/cacheflush.h> |
dbc895f9 | 28 | #include <asm/mem_map.h> |
175671e7 | 29 | #include <asm/mmu_context.h> |
1394f032 | 30 | |
1394f032 BW |
31 | /* |
32 | * does not yet catch signals sent when the child dies. | |
33 | * in exit.c or in signal.c. | |
34 | */ | |
35 | ||
1394f032 BW |
36 | /* |
37 | * Get contents of register REGNO in task TASK. | |
38 | */ | |
f2ce4802 | 39 | static inline long |
aeebd3a3 NK |
40 | get_reg(struct task_struct *task, unsigned long regno, |
41 | unsigned long __user *datap) | |
1394f032 | 42 | { |
f2ce4802 MF |
43 | long tmp; |
44 | struct pt_regs *regs = task_pt_regs(task); | |
1394f032 | 45 | |
aeebd3a3 | 46 | if (regno & 3 || regno > PT_LAST_PSEUDO) |
f2ce4802 | 47 | return -EIO; |
1394f032 BW |
48 | |
49 | switch (regno) { | |
f2ce4802 MF |
50 | case PT_TEXT_ADDR: |
51 | tmp = task->mm->start_code; | |
52 | break; | |
53 | case PT_TEXT_END_ADDR: | |
54 | tmp = task->mm->end_code; | |
55 | break; | |
56 | case PT_DATA_ADDR: | |
57 | tmp = task->mm->start_data; | |
58 | break; | |
1394f032 | 59 | case PT_USP: |
f2ce4802 MF |
60 | tmp = task->thread.usp; |
61 | break; | |
1394f032 | 62 | default: |
f2ce4802 MF |
63 | if (regno < sizeof(*regs)) { |
64 | void *reg_ptr = regs; | |
65 | tmp = *(long *)(reg_ptr + regno); | |
66 | } else | |
67 | return -EIO; | |
1394f032 | 68 | } |
1394f032 | 69 | |
f2ce4802 | 70 | return put_user(tmp, datap); |
1394f032 BW |
71 | } |
72 | ||
73 | /* | |
74 | * Write contents of register REGNO in task TASK. | |
75 | */ | |
76 | static inline int | |
aeebd3a3 | 77 | put_reg(struct task_struct *task, unsigned long regno, unsigned long data) |
1394f032 | 78 | { |
f2ce4802 | 79 | struct pt_regs *regs = task_pt_regs(task); |
1394f032 | 80 | |
aeebd3a3 | 81 | if (regno & 3 || regno > PT_LAST_PSEUDO) |
f2ce4802 | 82 | return -EIO; |
1394f032 BW |
83 | |
84 | switch (regno) { | |
85 | case PT_PC: | |
86 | /*********************************************************************/ | |
87 | /* At this point the kernel is most likely in exception. */ | |
88 | /* The RETX register will be used to populate the pc of the process. */ | |
89 | /*********************************************************************/ | |
90 | regs->retx = data; | |
91 | regs->pc = data; | |
92 | break; | |
93 | case PT_RETX: | |
94 | break; /* regs->retx = data; break; */ | |
95 | case PT_USP: | |
96 | regs->usp = data; | |
97 | task->thread.usp = data; | |
98 | break; | |
f2ce4802 MF |
99 | case PT_SYSCFG: /* don't let userspace screw with this */ |
100 | if ((data & ~1) != 0x6) | |
101 | pr_warning("ptrace: ignore syscfg write of %#lx\n", data); | |
102 | break; /* regs->syscfg = data; break; */ | |
1394f032 | 103 | default: |
f2ce4802 MF |
104 | if (regno < sizeof(*regs)) { |
105 | void *reg_offset = regs; | |
106 | *(long *)(reg_offset + regno) = data; | |
107 | } | |
108 | /* Ignore writes to pseudo registers */ | |
1394f032 | 109 | } |
f2ce4802 | 110 | |
1394f032 BW |
111 | return 0; |
112 | } | |
113 | ||
114 | /* | |
115 | * check that an address falls within the bounds of the target process's memory mappings | |
116 | */ | |
99a5b287 SZ |
117 | int |
118 | is_user_addr_valid(struct task_struct *child, unsigned long start, unsigned long len) | |
1394f032 | 119 | { |
b951242a | 120 | bool valid; |
8feae131 | 121 | struct vm_area_struct *vma; |
1394f032 BW |
122 | struct sram_list_struct *sraml; |
123 | ||
3c08f1d1 MF |
124 | /* overflow */ |
125 | if (start + len < start) | |
126 | return -EIO; | |
127 | ||
b951242a | 128 | down_read(&child->mm->mmap_sem); |
8feae131 | 129 | vma = find_vma(child->mm, start); |
b951242a DB |
130 | valid = vma && start >= vma->vm_start && start + len <= vma->vm_end; |
131 | up_read(&child->mm->mmap_sem); | |
132 | if (valid) | |
133 | return 0; | |
1394f032 BW |
134 | |
135 | for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next) | |
136 | if (start >= (unsigned long)sraml->addr | |
d207a8c7 | 137 | && start + len < (unsigned long)sraml->addr + sraml->length) |
1394f032 BW |
138 | return 0; |
139 | ||
d207a8c7 | 140 | if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END) |
26156397 JZ |
141 | return 0; |
142 | ||
175671e7 BS |
143 | #ifdef CONFIG_APP_STACK_L1 |
144 | if (child->mm->context.l1_stack_save) | |
145 | if (start >= (unsigned long)l1_stack_base && | |
146 | start + len < (unsigned long)l1_stack_base + l1_stack_len) | |
147 | return 0; | |
148 | #endif | |
149 | ||
1394f032 BW |
150 | return -EIO; |
151 | } | |
152 | ||
e50e2f25 MF |
153 | /* |
154 | * retrieve the contents of Blackfin userspace general registers | |
155 | */ | |
156 | static int genregs_get(struct task_struct *target, | |
157 | const struct user_regset *regset, | |
158 | unsigned int pos, unsigned int count, | |
159 | void *kbuf, void __user *ubuf) | |
160 | { | |
161 | struct pt_regs *regs = task_pt_regs(target); | |
162 | int ret; | |
163 | ||
164 | /* This sucks ... */ | |
165 | regs->usp = target->thread.usp; | |
166 | ||
167 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, | |
168 | regs, 0, sizeof(*regs)); | |
169 | if (ret < 0) | |
170 | return ret; | |
171 | ||
172 | return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, | |
173 | sizeof(*regs), -1); | |
174 | } | |
175 | ||
176 | /* | |
177 | * update the contents of the Blackfin userspace general registers | |
178 | */ | |
179 | static int genregs_set(struct task_struct *target, | |
180 | const struct user_regset *regset, | |
181 | unsigned int pos, unsigned int count, | |
182 | const void *kbuf, const void __user *ubuf) | |
183 | { | |
184 | struct pt_regs *regs = task_pt_regs(target); | |
185 | int ret; | |
186 | ||
187 | /* Don't let people set SYSCFG (it's at the end of pt_regs) */ | |
188 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, | |
189 | regs, 0, PT_SYSCFG); | |
190 | if (ret < 0) | |
191 | return ret; | |
192 | ||
193 | /* This sucks ... */ | |
194 | target->thread.usp = regs->usp; | |
195 | /* regs->retx = regs->pc; */ | |
196 | ||
197 | return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, | |
198 | PT_SYSCFG, -1); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Define the register sets available on the Blackfin under Linux | |
203 | */ | |
204 | enum bfin_regset { | |
205 | REGSET_GENERAL, | |
206 | }; | |
207 | ||
208 | static const struct user_regset bfin_regsets[] = { | |
209 | [REGSET_GENERAL] = { | |
210 | .core_note_type = NT_PRSTATUS, | |
211 | .n = sizeof(struct pt_regs) / sizeof(long), | |
212 | .size = sizeof(long), | |
213 | .align = sizeof(long), | |
214 | .get = genregs_get, | |
215 | .set = genregs_set, | |
216 | }, | |
217 | }; | |
218 | ||
219 | static const struct user_regset_view user_bfin_native_view = { | |
220 | .name = "Blackfin", | |
221 | .e_machine = EM_BLACKFIN, | |
222 | .regsets = bfin_regsets, | |
223 | .n = ARRAY_SIZE(bfin_regsets), | |
224 | }; | |
225 | ||
226 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) | |
227 | { | |
228 | return &user_bfin_native_view; | |
229 | } | |
230 | ||
e8f263df | 231 | void user_enable_single_step(struct task_struct *child) |
cb4c173d | 232 | { |
5f09c77d MF |
233 | struct pt_regs *regs = task_pt_regs(child); |
234 | regs->syscfg |= SYSCFG_SSSTEP; | |
600482c1 MF |
235 | |
236 | set_tsk_thread_flag(child, TIF_SINGLESTEP); | |
cb4c173d MF |
237 | } |
238 | ||
e8f263df | 239 | void user_disable_single_step(struct task_struct *child) |
1394f032 | 240 | { |
5f09c77d MF |
241 | struct pt_regs *regs = task_pt_regs(child); |
242 | regs->syscfg &= ~SYSCFG_SSSTEP; | |
600482c1 MF |
243 | |
244 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); | |
1394f032 BW |
245 | } |
246 | ||
9b05a69e NK |
247 | long arch_ptrace(struct task_struct *child, long request, |
248 | unsigned long addr, unsigned long data) | |
1394f032 BW |
249 | { |
250 | int ret; | |
0ddeeca2 | 251 | unsigned long __user *datap = (unsigned long __user *)data; |
c014e15a | 252 | void *paddr = (void *)addr; |
1394f032 BW |
253 | |
254 | switch (request) { | |
255 | /* when I and D space are separate, these will need to be fixed. */ | |
256 | case PTRACE_PEEKDATA: | |
257 | pr_debug("ptrace: PEEKDATA\n"); | |
1394f032 BW |
258 | /* fall through */ |
259 | case PTRACE_PEEKTEXT: /* read word at location addr. */ | |
260 | { | |
261 | unsigned long tmp = 0; | |
c014e15a | 262 | int copied = 0, to_copy = sizeof(tmp); |
1394f032 BW |
263 | |
264 | ret = -EIO; | |
c014e15a MF |
265 | pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy); |
266 | if (is_user_addr_valid(child, addr, to_copy) < 0) | |
1394f032 BW |
267 | break; |
268 | pr_debug("ptrace: user address is valid\n"); | |
269 | ||
c014e15a MF |
270 | switch (bfin_mem_access_type(addr, to_copy)) { |
271 | case BFIN_MEM_ACCESS_CORE: | |
272 | case BFIN_MEM_ACCESS_CORE_ONLY: | |
dabaad5b | 273 | copied = access_process_vm(child, addr, &tmp, |
c014e15a MF |
274 | to_copy, 0); |
275 | if (copied) | |
276 | break; | |
277 | ||
278 | /* hrm, why didn't that work ... maybe no mapping */ | |
279 | if (addr >= FIXED_CODE_START && | |
280 | addr + to_copy <= FIXED_CODE_END) { | |
281 | copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy); | |
282 | copied = to_copy; | |
283 | } else if (addr >= BOOT_ROM_START) { | |
284 | memcpy(&tmp, paddr, to_copy); | |
285 | copied = to_copy; | |
286 | } | |
d207a8c7 | 287 | |
1394f032 | 288 | break; |
c014e15a MF |
289 | case BFIN_MEM_ACCESS_DMA: |
290 | if (safe_dma_memcpy(&tmp, paddr, to_copy)) | |
291 | copied = to_copy; | |
292 | break; | |
293 | case BFIN_MEM_ACCESS_ITEST: | |
294 | if (isram_memcpy(&tmp, paddr, to_copy)) | |
295 | copied = to_copy; | |
296 | break; | |
297 | default: | |
298 | copied = 0; | |
299 | break; | |
300 | } | |
301 | ||
302 | pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp); | |
303 | if (copied == to_copy) | |
304 | ret = put_user(tmp, datap); | |
1394f032 BW |
305 | break; |
306 | } | |
307 | ||
1394f032 BW |
308 | /* when I and D space are separate, this will have to be fixed. */ |
309 | case PTRACE_POKEDATA: | |
d3ab3a62 | 310 | pr_debug("ptrace: PTRACE_PEEKDATA\n"); |
1394f032 BW |
311 | /* fall through */ |
312 | case PTRACE_POKETEXT: /* write the word at location addr. */ | |
313 | { | |
c014e15a | 314 | int copied = 0, to_copy = sizeof(data); |
1394f032 BW |
315 | |
316 | ret = -EIO; | |
c014e15a MF |
317 | pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n", |
318 | addr, to_copy, data); | |
319 | if (is_user_addr_valid(child, addr, to_copy) < 0) | |
1394f032 BW |
320 | break; |
321 | pr_debug("ptrace: user address is valid\n"); | |
322 | ||
c014e15a MF |
323 | switch (bfin_mem_access_type(addr, to_copy)) { |
324 | case BFIN_MEM_ACCESS_CORE: | |
325 | case BFIN_MEM_ACCESS_CORE_ONLY: | |
dabaad5b | 326 | copied = access_process_vm(child, addr, &data, |
46b60faf | 327 | to_copy, 1); |
1394f032 | 328 | break; |
c014e15a MF |
329 | case BFIN_MEM_ACCESS_DMA: |
330 | if (safe_dma_memcpy(paddr, &data, to_copy)) | |
331 | copied = to_copy; | |
332 | break; | |
333 | case BFIN_MEM_ACCESS_ITEST: | |
334 | if (isram_memcpy(paddr, &data, to_copy)) | |
335 | copied = to_copy; | |
336 | break; | |
337 | default: | |
338 | copied = 0; | |
339 | break; | |
340 | } | |
341 | ||
342 | pr_debug("ptrace: copied size %d\n", copied); | |
343 | if (copied == to_copy) | |
344 | ret = 0; | |
1394f032 BW |
345 | break; |
346 | } | |
347 | ||
f2ce4802 MF |
348 | case PTRACE_PEEKUSR: |
349 | switch (addr) { | |
350 | #ifdef CONFIG_BINFMT_ELF_FDPIC /* backwards compat */ | |
9c1a1259 MF |
351 | case PT_FDPIC_EXEC: |
352 | request = PTRACE_GETFDPIC; | |
353 | addr = PTRACE_GETFDPIC_EXEC; | |
354 | goto case_default; | |
355 | case PT_FDPIC_INTERP: | |
356 | request = PTRACE_GETFDPIC; | |
357 | addr = PTRACE_GETFDPIC_INTERP; | |
358 | goto case_default; | |
f2ce4802 MF |
359 | #endif |
360 | default: | |
361 | ret = get_reg(child, addr, datap); | |
1394f032 | 362 | } |
f2ce4802 MF |
363 | pr_debug("ptrace: PEEKUSR reg %li with %#lx = %i\n", addr, data, ret); |
364 | break; | |
1394f032 | 365 | |
f2ce4802 | 366 | case PTRACE_POKEUSR: |
1394f032 | 367 | ret = put_reg(child, addr, data); |
f2ce4802 | 368 | pr_debug("ptrace: POKEUSR reg %li with %li = %i\n", addr, data, ret); |
1394f032 BW |
369 | break; |
370 | ||
1394f032 | 371 | case PTRACE_GETREGS: |
e50e2f25 MF |
372 | pr_debug("ptrace: PTRACE_GETREGS\n"); |
373 | return copy_regset_to_user(child, &user_bfin_native_view, | |
374 | REGSET_GENERAL, | |
375 | 0, sizeof(struct pt_regs), | |
aeebd3a3 | 376 | datap); |
1394f032 BW |
377 | |
378 | case PTRACE_SETREGS: | |
e50e2f25 MF |
379 | pr_debug("ptrace: PTRACE_SETREGS\n"); |
380 | return copy_regset_from_user(child, &user_bfin_native_view, | |
381 | REGSET_GENERAL, | |
382 | 0, sizeof(struct pt_regs), | |
aeebd3a3 | 383 | datap); |
d3ab3a62 | 384 | |
9c1a1259 | 385 | case_default: |
1394f032 BW |
386 | default: |
387 | ret = ptrace_request(child, request, addr, data); | |
388 | break; | |
389 | } | |
390 | ||
391 | return ret; | |
392 | } | |
393 | ||
e8f263df | 394 | asmlinkage int syscall_trace_enter(struct pt_regs *regs) |
1394f032 | 395 | { |
e8f263df MF |
396 | int ret = 0; |
397 | ||
398 | if (test_thread_flag(TIF_SYSCALL_TRACE)) | |
399 | ret = tracehook_report_syscall_entry(regs); | |
400 | ||
401 | return ret; | |
402 | } | |
403 | ||
404 | asmlinkage void syscall_trace_leave(struct pt_regs *regs) | |
405 | { | |
600482c1 MF |
406 | int step; |
407 | ||
408 | step = test_thread_flag(TIF_SINGLESTEP); | |
409 | if (step || test_thread_flag(TIF_SYSCALL_TRACE)) | |
410 | tracehook_report_syscall_exit(regs, step); | |
1394f032 | 411 | } |