Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Kernel support for the ptrace() and syscall tracing interfaces. | |
3 | * | |
4 | * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc. | |
5 | * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx> | |
6 | * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org> | |
81e192d6 | 7 | * Copyright (C) 2008 Helge Deller <deller@gmx.de> |
1da177e4 LT |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/sched.h> | |
12 | #include <linux/mm.h> | |
13 | #include <linux/smp.h> | |
1da177e4 LT |
14 | #include <linux/errno.h> |
15 | #include <linux/ptrace.h> | |
16 | #include <linux/user.h> | |
17 | #include <linux/personality.h> | |
18 | #include <linux/security.h> | |
19 | #include <linux/compat.h> | |
7ed20e1a | 20 | #include <linux/signal.h> |
1da177e4 LT |
21 | |
22 | #include <asm/uaccess.h> | |
23 | #include <asm/pgtable.h> | |
24 | #include <asm/system.h> | |
25 | #include <asm/processor.h> | |
0013a854 | 26 | #include <asm/asm-offsets.h> |
1da177e4 LT |
27 | |
28 | /* PSW bits we allow the debugger to modify */ | |
29 | #define USER_PSW_BITS (PSW_N | PSW_V | PSW_CB) | |
30 | ||
81e192d6 HD |
31 | /* |
32 | * Called by kernel/ptrace.c when detaching.. | |
33 | * | |
34 | * Make sure single step bits etc are not set. | |
35 | */ | |
36 | void ptrace_disable(struct task_struct *task) | |
37 | { | |
38 | task->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP); | |
1da177e4 | 39 | |
81e192d6 HD |
40 | /* make sure the trap bits are not set */ |
41 | pa_psw(task)->r = 0; | |
42 | pa_psw(task)->t = 0; | |
43 | pa_psw(task)->h = 0; | |
44 | pa_psw(task)->l = 0; | |
45 | } | |
46 | ||
47 | /* | |
48 | * The following functions are called by ptrace_resume() when | |
49 | * enabling or disabling single/block tracing. | |
50 | */ | |
51 | void user_disable_single_step(struct task_struct *task) | |
52 | { | |
53 | ptrace_disable(task); | |
54 | } | |
55 | ||
56 | void user_enable_single_step(struct task_struct *task) | |
57 | { | |
58 | task->ptrace &= ~PT_BLOCKSTEP; | |
59 | task->ptrace |= PT_SINGLESTEP; | |
60 | ||
61 | if (pa_psw(task)->n) { | |
62 | struct siginfo si; | |
63 | ||
64 | /* Nullified, just crank over the queue. */ | |
65 | task_regs(task)->iaoq[0] = task_regs(task)->iaoq[1]; | |
66 | task_regs(task)->iasq[0] = task_regs(task)->iasq[1]; | |
67 | task_regs(task)->iaoq[1] = task_regs(task)->iaoq[0] + 4; | |
68 | pa_psw(task)->n = 0; | |
69 | pa_psw(task)->x = 0; | |
70 | pa_psw(task)->y = 0; | |
71 | pa_psw(task)->z = 0; | |
72 | pa_psw(task)->b = 0; | |
73 | ptrace_disable(task); | |
74 | /* Don't wake up the task, but let the | |
75 | parent know something happened. */ | |
76 | si.si_code = TRAP_TRACE; | |
77 | si.si_addr = (void __user *) (task_regs(task)->iaoq[0] & ~3); | |
78 | si.si_signo = SIGTRAP; | |
79 | si.si_errno = 0; | |
80 | force_sig_info(SIGTRAP, &si, task); | |
81 | /* notify_parent(task, SIGCHLD); */ | |
82 | return; | |
83 | } | |
84 | ||
85 | /* Enable recovery counter traps. The recovery counter | |
86 | * itself will be set to zero on a task switch. If the | |
87 | * task is suspended on a syscall then the syscall return | |
88 | * path will overwrite the recovery counter with a suitable | |
89 | * value such that it traps once back in user space. We | |
90 | * disable interrupts in the tasks PSW here also, to avoid | |
91 | * interrupts while the recovery counter is decrementing. | |
92 | */ | |
93 | pa_psw(task)->r = 1; | |
94 | pa_psw(task)->t = 0; | |
95 | pa_psw(task)->h = 0; | |
96 | pa_psw(task)->l = 0; | |
97 | } | |
98 | ||
99 | void user_enable_block_step(struct task_struct *task) | |
100 | { | |
101 | task->ptrace &= ~PT_SINGLESTEP; | |
102 | task->ptrace |= PT_BLOCKSTEP; | |
103 | ||
104 | /* Enable taken branch trap. */ | |
105 | pa_psw(task)->r = 0; | |
106 | pa_psw(task)->t = 1; | |
107 | pa_psw(task)->h = 0; | |
108 | pa_psw(task)->l = 0; | |
109 | } | |
110 | ||
111 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) | |
112 | { | |
113 | unsigned long tmp; | |
114 | long ret = -EIO; | |
1da177e4 | 115 | |
81e192d6 HD |
116 | switch (request) { |
117 | ||
118 | /* Read the word at location addr in the USER area. For ptraced | |
119 | processes, the kernel saves all regs on a syscall. */ | |
120 | case PTRACE_PEEKUSR: | |
121 | if ((addr & (sizeof(long)-1)) || | |
122 | (unsigned long) addr >= sizeof(struct pt_regs)) | |
123 | break; | |
124 | tmp = *(unsigned long *) ((char *) task_regs(child) + addr); | |
125 | ret = put_user(tmp, (unsigned long *) data); | |
126 | break; | |
127 | ||
128 | /* Write the word at location addr in the USER area. This will need | |
129 | to change when the kernel no longer saves all regs on a syscall. | |
130 | FIXME. There is a problem at the moment in that r3-r18 are only | |
131 | saved if the process is ptraced on syscall entry, and even then | |
132 | those values are overwritten by actual register values on syscall | |
133 | exit. */ | |
134 | case PTRACE_POKEUSR: | |
135 | /* Some register values written here may be ignored in | |
136 | * entry.S:syscall_restore_rfi; e.g. iaoq is written with | |
137 | * r31/r31+4, and not with the values in pt_regs. | |
138 | */ | |
139 | if (addr == PT_PSW) { | |
140 | /* Allow writing to Nullify, Divide-step-correction, | |
141 | * and carry/borrow bits. | |
142 | * BEWARE, if you set N, and then single step, it won't | |
143 | * stop on the nullified instruction. | |
144 | */ | |
145 | data &= USER_PSW_BITS; | |
146 | task_regs(child)->gr[0] &= ~USER_PSW_BITS; | |
147 | task_regs(child)->gr[0] |= data; | |
148 | ret = 0; | |
149 | break; | |
150 | } | |
151 | ||
152 | if ((addr & (sizeof(long)-1)) || | |
153 | (unsigned long) addr >= sizeof(struct pt_regs)) | |
154 | break; | |
155 | if ((addr >= PT_GR1 && addr <= PT_GR31) || | |
156 | addr == PT_IAOQ0 || addr == PT_IAOQ1 || | |
157 | (addr >= PT_FR0 && addr <= PT_FR31 + 4) || | |
158 | addr == PT_SAR) { | |
159 | *(unsigned long *) ((char *) task_regs(child) + addr) = data; | |
160 | ret = 0; | |
161 | } | |
162 | break; | |
163 | ||
164 | default: | |
165 | ret = ptrace_request(child, request, addr, data); | |
166 | break; | |
167 | } | |
168 | ||
169 | return ret; | |
170 | } | |
171 | ||
172 | ||
173 | #ifdef CONFIG_COMPAT | |
1da177e4 LT |
174 | |
175 | /* This function is needed to translate 32 bit pt_regs offsets in to | |
176 | * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel | |
177 | * will request offset 12 if it wants gr3, but the lower 32 bits of | |
178 | * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4). | |
179 | * This code relies on a 32 bit pt_regs being comprised of 32 bit values | |
180 | * except for the fp registers which (a) are 64 bits, and (b) follow | |
181 | * the gr registers at the start of pt_regs. The 32 bit pt_regs should | |
182 | * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[] | |
183 | * being 64 bit in both cases. | |
184 | */ | |
185 | ||
ed79b86d | 186 | static compat_ulong_t translate_usr_offset(compat_ulong_t offset) |
1da177e4 LT |
187 | { |
188 | if (offset < 0) | |
ed79b86d | 189 | return sizeof(struct pt_regs); |
1da177e4 LT |
190 | else if (offset <= 32*4) /* gr[0..31] */ |
191 | return offset * 2 + 4; | |
192 | else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */ | |
193 | return offset + 32*4; | |
194 | else if (offset < sizeof(struct pt_regs)/2 + 32*4) | |
195 | return offset * 2 + 4 - 32*8; | |
196 | else | |
ed79b86d | 197 | return sizeof(struct pt_regs); |
1da177e4 | 198 | } |
1da177e4 | 199 | |
81e192d6 HD |
200 | long compat_arch_ptrace(struct task_struct *child, compat_long_t request, |
201 | compat_ulong_t addr, compat_ulong_t data) | |
1da177e4 | 202 | { |
81e192d6 HD |
203 | compat_uint_t tmp; |
204 | long ret = -EIO; | |
1da177e4 | 205 | |
1da177e4 | 206 | switch (request) { |
1da177e4 | 207 | |
81e192d6 HD |
208 | case PTRACE_PEEKUSR: |
209 | if (addr & (sizeof(compat_uint_t)-1)) | |
210 | break; | |
211 | addr = translate_usr_offset(addr); | |
ed79b86d | 212 | if (addr >= sizeof(struct pt_regs)) |
81e192d6 | 213 | break; |
1da177e4 | 214 | |
81e192d6 HD |
215 | tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr); |
216 | ret = put_user(tmp, (compat_uint_t *) (unsigned long) data); | |
217 | break; | |
1da177e4 LT |
218 | |
219 | /* Write the word at location addr in the USER area. This will need | |
220 | to change when the kernel no longer saves all regs on a syscall. | |
221 | FIXME. There is a problem at the moment in that r3-r18 are only | |
222 | saved if the process is ptraced on syscall entry, and even then | |
223 | those values are overwritten by actual register values on syscall | |
224 | exit. */ | |
225 | case PTRACE_POKEUSR: | |
1da177e4 LT |
226 | /* Some register values written here may be ignored in |
227 | * entry.S:syscall_restore_rfi; e.g. iaoq is written with | |
228 | * r31/r31+4, and not with the values in pt_regs. | |
229 | */ | |
1da177e4 | 230 | if (addr == PT_PSW) { |
81e192d6 HD |
231 | /* Since PT_PSW==0, it is valid for 32 bit processes |
232 | * under 64 bit kernels as well. | |
1da177e4 | 233 | */ |
81e192d6 HD |
234 | ret = arch_ptrace(child, request, addr, data); |
235 | } else { | |
236 | if (addr & (sizeof(compat_uint_t)-1)) | |
237 | break; | |
238 | addr = translate_usr_offset(addr); | |
ed79b86d | 239 | if (addr >= sizeof(struct pt_regs)) |
81e192d6 | 240 | break; |
1da177e4 LT |
241 | if (addr >= PT_FR0 && addr <= PT_FR31 + 4) { |
242 | /* Special case, fp regs are 64 bits anyway */ | |
81e192d6 | 243 | *(__u64 *) ((char *) task_regs(child) + addr) = data; |
1da177e4 LT |
244 | ret = 0; |
245 | } | |
246 | else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) || | |
247 | addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 || | |
248 | addr == PT_SAR+4) { | |
249 | /* Zero the top 32 bits */ | |
81e192d6 HD |
250 | *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0; |
251 | *(__u32 *) ((char *) task_regs(child) + addr) = data; | |
1da177e4 LT |
252 | ret = 0; |
253 | } | |
1da177e4 | 254 | } |
81e192d6 | 255 | break; |
1da177e4 LT |
256 | |
257 | default: | |
81e192d6 HD |
258 | ret = compat_ptrace_request(child, request, addr, data); |
259 | break; | |
1da177e4 LT |
260 | } |
261 | ||
1da177e4 LT |
262 | return ret; |
263 | } | |
81e192d6 HD |
264 | #endif |
265 | ||
1da177e4 LT |
266 | |
267 | void syscall_trace(void) | |
268 | { | |
269 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) | |
270 | return; | |
271 | if (!(current->ptrace & PT_PTRACED)) | |
272 | return; | |
273 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | |
274 | ? 0x80 : 0)); | |
275 | /* | |
276 | * this isn't the same as continuing with a signal, but it will do | |
277 | * for normal use. strace only continues with a signal if the | |
278 | * stopping signal is not SIGTRAP. -brl | |
279 | */ | |
280 | if (current->exit_code) { | |
281 | send_sig(current->exit_code, current, 1); | |
282 | current->exit_code = 0; | |
283 | } | |
284 | } |