Commit | Line | Data |
---|---|---|
5a0015d6 CZ |
1 | // TODO some minor issues |
2 | /* | |
3 | * This file is subject to the terms and conditions of the GNU General Public | |
4 | * License. See the file "COPYING" in the main directory of this archive | |
5 | * for more details. | |
6 | * | |
7 | * Copyright (C) 2001 - 2005 Tensilica Inc. | |
8 | * | |
9 | * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> | |
10 | * Chris Zankel <chris@zankel.net> | |
11 | * Scott Foehner<sfoehner@yahoo.com>, | |
12 | * Kevin Chea | |
13 | * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca> | |
14 | */ | |
15 | ||
5a0015d6 CZ |
16 | #include <linux/kernel.h> |
17 | #include <linux/sched.h> | |
18 | #include <linux/mm.h> | |
19 | #include <linux/errno.h> | |
20 | #include <linux/ptrace.h> | |
21 | #include <linux/smp.h> | |
5a0015d6 | 22 | #include <linux/security.h> |
0ee23b50 | 23 | #include <linux/signal.h> |
5a0015d6 CZ |
24 | |
25 | #include <asm/pgtable.h> | |
26 | #include <asm/page.h> | |
27 | #include <asm/system.h> | |
28 | #include <asm/uaccess.h> | |
29 | #include <asm/ptrace.h> | |
30 | #include <asm/elf.h> | |
31 | ||
32 | #define TEST_KERNEL // verify kernel operations FIXME: remove | |
33 | ||
34 | ||
35 | /* | |
36 | * Called by kernel/ptrace.c when detaching.. | |
37 | * | |
38 | * Make sure single step bits etc are not set. | |
39 | */ | |
40 | ||
41 | void ptrace_disable(struct task_struct *child) | |
42 | { | |
43 | /* Nothing to do.. */ | |
44 | } | |
45 | ||
481bed45 | 46 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
5a0015d6 | 47 | { |
5a0015d6 CZ |
48 | int ret = -EPERM; |
49 | ||
5a0015d6 CZ |
50 | switch (request) { |
51 | case PTRACE_PEEKTEXT: /* read word at location addr. */ | |
52 | case PTRACE_PEEKDATA: | |
76647323 | 53 | ret = generic_ptrace_peekdata(child, addr, data); |
5a0015d6 | 54 | goto out; |
5a0015d6 CZ |
55 | |
56 | /* Read the word at location addr in the USER area. */ | |
57 | ||
58 | case PTRACE_PEEKUSR: | |
59 | { | |
60 | struct pt_regs *regs; | |
61 | unsigned long tmp; | |
62 | ||
04fe6faf | 63 | regs = task_pt_regs(child); |
5a0015d6 CZ |
64 | tmp = 0; /* Default return value. */ |
65 | ||
66 | switch(addr) { | |
67 | ||
68 | case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1: | |
69 | { | |
70 | int ar = addr - REG_AR_BASE - regs->windowbase * 4; | |
71 | ar &= (XCHAL_NUM_AREGS - 1); | |
72 | if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0) | |
73 | tmp = regs->areg[ar]; | |
74 | else | |
75 | ret = -EIO; | |
76 | break; | |
77 | } | |
78 | case REG_A_BASE ... REG_A_BASE + 15: | |
79 | tmp = regs->areg[addr - REG_A_BASE]; | |
80 | break; | |
81 | case REG_PC: | |
82 | tmp = regs->pc; | |
83 | break; | |
84 | case REG_PS: | |
85 | /* Note: PS.EXCM is not set while user task is running; | |
86 | * its being set in regs is for exception handling | |
87 | * convenience. */ | |
173d6681 | 88 | tmp = (regs->ps & ~(1 << PS_EXCM_BIT)); |
5a0015d6 CZ |
89 | break; |
90 | case REG_WB: | |
91 | tmp = regs->windowbase; | |
92 | break; | |
93 | case REG_WS: | |
94 | tmp = regs->windowstart; | |
95 | break; | |
96 | case REG_LBEG: | |
97 | tmp = regs->lbeg; | |
98 | break; | |
99 | case REG_LEND: | |
100 | tmp = regs->lend; | |
101 | break; | |
102 | case REG_LCOUNT: | |
103 | tmp = regs->lcount; | |
104 | break; | |
105 | case REG_SAR: | |
106 | tmp = regs->sar; | |
107 | break; | |
108 | case REG_DEPC: | |
109 | tmp = regs->depc; | |
110 | break; | |
111 | case REG_EXCCAUSE: | |
112 | tmp = regs->exccause; | |
113 | break; | |
114 | case REG_EXCVADDR: | |
115 | tmp = regs->excvaddr; | |
116 | break; | |
117 | case SYSCALL_NR: | |
118 | tmp = regs->syscall; | |
119 | break; | |
120 | default: | |
121 | tmp = 0; | |
122 | ret = -EIO; | |
123 | goto out; | |
124 | } | |
125 | ret = put_user(tmp, (unsigned long *) data); | |
126 | goto out; | |
127 | } | |
128 | ||
129 | case PTRACE_POKETEXT: /* write the word at location addr. */ | |
130 | case PTRACE_POKEDATA: | |
f284ce72 | 131 | ret = generic_ptrace_pokedata(child, addr, data); |
5a0015d6 CZ |
132 | goto out; |
133 | ||
134 | case PTRACE_POKEUSR: | |
135 | { | |
136 | struct pt_regs *regs; | |
04fe6faf | 137 | regs = task_pt_regs(child); |
5a0015d6 CZ |
138 | |
139 | switch (addr) { | |
140 | case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1: | |
141 | { | |
142 | int ar = addr - REG_AR_BASE - regs->windowbase * 4; | |
143 | if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0) | |
144 | regs->areg[ar & (XCHAL_NUM_AREGS - 1)] = data; | |
145 | else | |
146 | ret = -EIO; | |
147 | break; | |
148 | } | |
149 | case REG_A_BASE ... REG_A_BASE + 15: | |
150 | regs->areg[addr - REG_A_BASE] = data; | |
151 | break; | |
152 | case REG_PC: | |
153 | regs->pc = data; | |
154 | break; | |
155 | case SYSCALL_NR: | |
156 | regs->syscall = data; | |
157 | break; | |
158 | #ifdef TEST_KERNEL | |
159 | case REG_WB: | |
160 | regs->windowbase = data; | |
161 | break; | |
162 | case REG_WS: | |
163 | regs->windowstart = data; | |
164 | break; | |
165 | #endif | |
166 | ||
167 | default: | |
168 | /* The rest are not allowed. */ | |
169 | ret = -EIO; | |
170 | break; | |
171 | } | |
172 | break; | |
173 | } | |
174 | ||
175 | /* continue and stop at next (return from) syscall */ | |
176 | case PTRACE_SYSCALL: | |
177 | case PTRACE_CONT: /* restart after signal. */ | |
178 | { | |
179 | ret = -EIO; | |
0ee23b50 | 180 | if (!valid_signal(data)) |
5a0015d6 CZ |
181 | break; |
182 | if (request == PTRACE_SYSCALL) | |
183 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
184 | else | |
185 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
186 | child->exit_code = data; | |
187 | /* Make sure the single step bit is not set. */ | |
188 | child->ptrace &= ~PT_SINGLESTEP; | |
189 | wake_up_process(child); | |
190 | ret = 0; | |
191 | break; | |
192 | } | |
193 | ||
194 | /* | |
195 | * make the child exit. Best I can do is send it a sigkill. | |
196 | * perhaps it should be put in the status that it wants to | |
197 | * exit. | |
198 | */ | |
199 | case PTRACE_KILL: | |
200 | ret = 0; | |
d742eae8 | 201 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
5a0015d6 CZ |
202 | break; |
203 | child->exit_code = SIGKILL; | |
204 | child->ptrace &= ~PT_SINGLESTEP; | |
205 | wake_up_process(child); | |
206 | break; | |
207 | ||
208 | case PTRACE_SINGLESTEP: | |
209 | ret = -EIO; | |
0ee23b50 | 210 | if (!valid_signal(data)) |
5a0015d6 CZ |
211 | break; |
212 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
213 | child->ptrace |= PT_SINGLESTEP; | |
214 | child->exit_code = data; | |
215 | wake_up_process(child); | |
216 | ret = 0; | |
217 | break; | |
218 | ||
219 | case PTRACE_GETREGS: | |
220 | { | |
221 | /* 'data' points to user memory in which to write. | |
222 | * Mainly due to the non-live register values, we | |
223 | * reformat the register values into something more | |
224 | * standard. For convenience, we use the handy | |
225 | * elf_gregset_t format. */ | |
226 | ||
227 | xtensa_gregset_t format; | |
04fe6faf | 228 | struct pt_regs *regs = task_pt_regs(child); |
5a0015d6 CZ |
229 | |
230 | do_copy_regs (&format, regs, child); | |
231 | ||
232 | /* Now, copy to user space nice and easy... */ | |
233 | ret = 0; | |
234 | if (copy_to_user((void *)data, &format, sizeof(elf_gregset_t))) | |
235 | ret = -EFAULT; | |
236 | break; | |
237 | } | |
238 | ||
239 | case PTRACE_SETREGS: | |
240 | { | |
241 | /* 'data' points to user memory that contains the new | |
242 | * values in the elf_gregset_t format. */ | |
243 | ||
244 | xtensa_gregset_t format; | |
04fe6faf | 245 | struct pt_regs *regs = task_pt_regs(child); |
5a0015d6 CZ |
246 | |
247 | if (copy_from_user(&format,(void *)data,sizeof(elf_gregset_t))){ | |
248 | ret = -EFAULT; | |
249 | break; | |
250 | } | |
251 | ||
252 | /* FIXME: Perhaps we want some sanity checks on | |
253 | * these user-space values? See ARM version. Are | |
254 | * debuggers a security concern? */ | |
255 | ||
256 | do_restore_regs (&format, regs, child); | |
257 | ||
258 | ret = 0; | |
259 | break; | |
260 | } | |
261 | ||
262 | case PTRACE_GETFPREGS: | |
263 | { | |
264 | /* 'data' points to user memory in which to write. | |
265 | * For convenience, we use the handy | |
266 | * elf_fpregset_t format. */ | |
267 | ||
268 | elf_fpregset_t fpregs; | |
04fe6faf | 269 | struct pt_regs *regs = task_pt_regs(child); |
5a0015d6 CZ |
270 | |
271 | do_save_fpregs (&fpregs, regs, child); | |
272 | ||
273 | /* Now, copy to user space nice and easy... */ | |
274 | ret = 0; | |
275 | if (copy_to_user((void *)data, &fpregs, sizeof(elf_fpregset_t))) | |
276 | ret = -EFAULT; | |
277 | ||
278 | break; | |
279 | } | |
280 | ||
281 | case PTRACE_SETFPREGS: | |
282 | { | |
283 | /* 'data' points to user memory that contains the new | |
284 | * values in the elf_fpregset_t format. | |
285 | */ | |
286 | elf_fpregset_t fpregs; | |
04fe6faf | 287 | struct pt_regs *regs = task_pt_regs(child); |
5a0015d6 CZ |
288 | |
289 | ret = 0; | |
290 | if (copy_from_user(&fpregs, (void *)data, sizeof(elf_fpregset_t))) { | |
291 | ret = -EFAULT; | |
292 | break; | |
293 | } | |
294 | ||
295 | if (do_restore_fpregs (&fpregs, regs, child)) | |
296 | ret = -EIO; | |
297 | break; | |
298 | } | |
299 | ||
300 | case PTRACE_GETFPREGSIZE: | |
301 | /* 'data' points to 'unsigned long' set to the size | |
302 | * of elf_fpregset_t | |
303 | */ | |
304 | ret = put_user(sizeof(elf_fpregset_t), (unsigned long *) data); | |
305 | break; | |
306 | ||
307 | case PTRACE_DETACH: /* detach a process that was attached. */ | |
308 | ret = ptrace_detach(child, data); | |
309 | break; | |
310 | ||
311 | default: | |
312 | ret = ptrace_request(child, request, addr, data); | |
313 | goto out; | |
314 | } | |
481bed45 | 315 | out: |
5a0015d6 CZ |
316 | return ret; |
317 | } | |
318 | ||
319 | void do_syscall_trace(void) | |
320 | { | |
5a0015d6 CZ |
321 | /* |
322 | * The 0x80 provides a way for the tracing parent to distinguish | |
323 | * between a syscall stop and SIGTRAP delivery | |
324 | */ | |
325 | ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0)); | |
326 | ||
327 | /* | |
328 | * this isn't the same as continuing with a signal, but it will do | |
329 | * for normal use. strace only continues with a signal if the | |
330 | * stopping signal is not SIGTRAP. -brl | |
331 | */ | |
332 | if (current->exit_code) { | |
333 | send_sig(current->exit_code, current, 1); | |
334 | current->exit_code = 0; | |
335 | } | |
336 | } | |
fc4fb2ad CZ |
337 | |
338 | void do_syscall_trace_enter(struct pt_regs *regs) | |
339 | { | |
340 | if (test_thread_flag(TIF_SYSCALL_TRACE) | |
341 | && (current->ptrace & PT_PTRACED)) | |
342 | do_syscall_trace(); | |
343 | ||
344 | #if 0 | |
345 | if (unlikely(current->audit_context)) | |
346 | audit_syscall_entry(current, AUDIT_ARCH_XTENSA..); | |
347 | #endif | |
348 | } | |
349 | ||
350 | void do_syscall_trace_leave(struct pt_regs *regs) | |
351 | { | |
352 | if ((test_thread_flag(TIF_SYSCALL_TRACE)) | |
353 | && (current->ptrace & PT_PTRACED)) | |
354 | do_syscall_trace(); | |
355 | } | |
356 |