v2.6.22.24-op1
[linux-2.6.22.y-op.git] / arch / x86_64 / ia32 / ptrace32.c
blob4de3a54318f4d6d4f3636144393ee7c88d13fea4
1 /*
2 * 32bit ptrace for x86-64.
4 * Copyright 2001,2002 Andi Kleen, SuSE Labs.
5 * Some parts copied from arch/i386/kernel/ptrace.c. See that file for earlier
6 * copyright.
7 *
8 * This allows to access 64bit processes too; but there is no way to see the extended
9 * register contents.
10 */
12 #include <linux/kernel.h>
13 #include <linux/stddef.h>
14 #include <linux/sched.h>
15 #include <linux/syscalls.h>
16 #include <linux/unistd.h>
17 #include <linux/mm.h>
18 #include <linux/ptrace.h>
19 #include <asm/ptrace.h>
20 #include <asm/compat.h>
21 #include <asm/uaccess.h>
22 #include <asm/user32.h>
23 #include <asm/user.h>
24 #include <asm/errno.h>
25 #include <asm/debugreg.h>
26 #include <asm/i387.h>
27 #include <asm/fpu32.h>
28 #include <asm/ia32.h>
31 * Determines which flags the user has access to [1 = access, 0 = no access].
32 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
33 * Also masks reserved bits (31-22, 15, 5, 3, 1).
35 #define FLAG_MASK 0x54dd5UL
37 #define R32(l,q) \
38 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
40 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
42 int i;
43 __u64 *stack = (__u64 *)task_pt_regs(child);
45 switch (regno) {
46 case offsetof(struct user32, regs.fs):
47 if (val && (val & 3) != 3) return -EIO;
48 child->thread.fsindex = val & 0xffff;
49 break;
50 case offsetof(struct user32, regs.gs):
51 if (val && (val & 3) != 3) return -EIO;
52 child->thread.gsindex = val & 0xffff;
53 break;
54 case offsetof(struct user32, regs.ds):
55 if (val && (val & 3) != 3) return -EIO;
56 child->thread.ds = val & 0xffff;
57 break;
58 case offsetof(struct user32, regs.es):
59 child->thread.es = val & 0xffff;
60 break;
61 case offsetof(struct user32, regs.ss):
62 if ((val & 3) != 3) return -EIO;
63 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
64 break;
65 case offsetof(struct user32, regs.cs):
66 if ((val & 3) != 3) return -EIO;
67 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
68 break;
70 R32(ebx, rbx);
71 R32(ecx, rcx);
72 R32(edx, rdx);
73 R32(edi, rdi);
74 R32(esi, rsi);
75 R32(ebp, rbp);
76 R32(eax, rax);
77 R32(orig_eax, orig_rax);
78 R32(eip, rip);
79 R32(esp, rsp);
81 case offsetof(struct user32, regs.eflags): {
82 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
83 val &= FLAG_MASK;
84 *flags = val | (*flags & ~FLAG_MASK);
85 break;
88 case offsetof(struct user32, u_debugreg[4]):
89 case offsetof(struct user32, u_debugreg[5]):
90 return -EIO;
92 case offsetof(struct user32, u_debugreg[0]):
93 child->thread.debugreg0 = val;
94 break;
96 case offsetof(struct user32, u_debugreg[1]):
97 child->thread.debugreg1 = val;
98 break;
100 case offsetof(struct user32, u_debugreg[2]):
101 child->thread.debugreg2 = val;
102 break;
104 case offsetof(struct user32, u_debugreg[3]):
105 child->thread.debugreg3 = val;
106 break;
108 case offsetof(struct user32, u_debugreg[6]):
109 child->thread.debugreg6 = val;
110 break;
112 case offsetof(struct user32, u_debugreg[7]):
113 val &= ~DR_CONTROL_RESERVED;
114 /* See arch/i386/kernel/ptrace.c for an explanation of
115 * this awkward check.*/
116 for(i=0; i<4; i++)
117 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
118 return -EIO;
119 child->thread.debugreg7 = val;
120 if (val)
121 set_tsk_thread_flag(child, TIF_DEBUG);
122 else
123 clear_tsk_thread_flag(child, TIF_DEBUG);
124 break;
126 default:
127 if (regno > sizeof(struct user32) || (regno & 3))
128 return -EIO;
130 /* Other dummy fields in the virtual user structure are ignored */
131 break;
133 return 0;
136 #undef R32
138 #define R32(l,q) \
139 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
141 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
143 __u64 *stack = (__u64 *)task_pt_regs(child);
145 switch (regno) {
146 case offsetof(struct user32, regs.fs):
147 *val = child->thread.fsindex;
148 break;
149 case offsetof(struct user32, regs.gs):
150 *val = child->thread.gsindex;
151 break;
152 case offsetof(struct user32, regs.ds):
153 *val = child->thread.ds;
154 break;
155 case offsetof(struct user32, regs.es):
156 *val = child->thread.es;
157 break;
159 R32(cs, cs);
160 R32(ss, ss);
161 R32(ebx, rbx);
162 R32(ecx, rcx);
163 R32(edx, rdx);
164 R32(edi, rdi);
165 R32(esi, rsi);
166 R32(ebp, rbp);
167 R32(eax, rax);
168 R32(orig_eax, orig_rax);
169 R32(eip, rip);
170 R32(eflags, eflags);
171 R32(esp, rsp);
173 case offsetof(struct user32, u_debugreg[0]):
174 *val = child->thread.debugreg0;
175 break;
176 case offsetof(struct user32, u_debugreg[1]):
177 *val = child->thread.debugreg1;
178 break;
179 case offsetof(struct user32, u_debugreg[2]):
180 *val = child->thread.debugreg2;
181 break;
182 case offsetof(struct user32, u_debugreg[3]):
183 *val = child->thread.debugreg3;
184 break;
185 case offsetof(struct user32, u_debugreg[6]):
186 *val = child->thread.debugreg6;
187 break;
188 case offsetof(struct user32, u_debugreg[7]):
189 *val = child->thread.debugreg7;
190 break;
192 default:
193 if (regno > sizeof(struct user32) || (regno & 3))
194 return -EIO;
196 /* Other dummy fields in the virtual user structure are ignored */
197 *val = 0;
198 break;
200 return 0;
203 #undef R32
205 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
207 int ret;
208 compat_siginfo_t __user *si32 = compat_ptr(data);
209 siginfo_t ssi;
210 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
211 if (request == PTRACE_SETSIGINFO) {
212 memset(&ssi, 0, sizeof(siginfo_t));
213 ret = copy_siginfo_from_user32(&ssi, si32);
214 if (ret)
215 return ret;
216 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
217 return -EFAULT;
219 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
220 if (ret)
221 return ret;
222 if (request == PTRACE_GETSIGINFO) {
223 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
224 return -EFAULT;
225 ret = copy_siginfo_to_user32(si32, &ssi);
227 return ret;
230 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
232 struct task_struct *child;
233 struct pt_regs *childregs;
234 void __user *datap = compat_ptr(data);
235 int ret;
236 __u32 val;
238 switch (request) {
239 case PTRACE_TRACEME:
240 case PTRACE_ATTACH:
241 case PTRACE_KILL:
242 case PTRACE_CONT:
243 case PTRACE_SINGLESTEP:
244 case PTRACE_DETACH:
245 case PTRACE_SYSCALL:
246 case PTRACE_OLDSETOPTIONS:
247 case PTRACE_SETOPTIONS:
248 case PTRACE_SET_THREAD_AREA:
249 case PTRACE_GET_THREAD_AREA:
250 return sys_ptrace(request, pid, addr, data);
252 default:
253 return -EINVAL;
255 case PTRACE_PEEKTEXT:
256 case PTRACE_PEEKDATA:
257 case PTRACE_POKEDATA:
258 case PTRACE_POKETEXT:
259 case PTRACE_POKEUSR:
260 case PTRACE_PEEKUSR:
261 case PTRACE_GETREGS:
262 case PTRACE_SETREGS:
263 case PTRACE_SETFPREGS:
264 case PTRACE_GETFPREGS:
265 case PTRACE_SETFPXREGS:
266 case PTRACE_GETFPXREGS:
267 case PTRACE_GETEVENTMSG:
268 break;
270 case PTRACE_SETSIGINFO:
271 case PTRACE_GETSIGINFO:
272 return ptrace32_siginfo(request, pid, addr, data);
275 child = ptrace_get_task_struct(pid);
276 if (IS_ERR(child))
277 return PTR_ERR(child);
279 ret = ptrace_check_attach(child, request == PTRACE_KILL);
280 if (ret < 0)
281 goto out;
283 childregs = task_pt_regs(child);
285 switch (request) {
286 case PTRACE_PEEKDATA:
287 case PTRACE_PEEKTEXT:
288 ret = 0;
289 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
290 ret = -EIO;
291 else
292 ret = put_user(val, (unsigned int __user *)datap);
293 break;
295 case PTRACE_POKEDATA:
296 case PTRACE_POKETEXT:
297 ret = 0;
298 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
299 ret = -EIO;
300 break;
302 case PTRACE_PEEKUSR:
303 ret = getreg32(child, addr, &val);
304 if (ret == 0)
305 ret = put_user(val, (__u32 __user *)datap);
306 break;
308 case PTRACE_POKEUSR:
309 ret = putreg32(child, addr, data);
310 break;
312 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
313 int i;
314 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
315 ret = -EIO;
316 break;
318 ret = 0;
319 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
320 getreg32(child, i, &val);
321 ret |= __put_user(val,(u32 __user *)datap);
322 datap += sizeof(u32);
324 break;
327 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
328 unsigned long tmp;
329 int i;
330 if (!access_ok(VERIFY_READ, datap, 16*4)) {
331 ret = -EIO;
332 break;
334 ret = 0;
335 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
336 ret |= __get_user(tmp, (u32 __user *)datap);
337 putreg32(child, i, tmp);
338 datap += sizeof(u32);
340 break;
343 case PTRACE_GETFPREGS:
344 ret = -EIO;
345 if (!access_ok(VERIFY_READ, compat_ptr(data),
346 sizeof(struct user_i387_struct)))
347 break;
348 save_i387_ia32(child, datap, childregs, 1);
349 ret = 0;
350 break;
352 case PTRACE_SETFPREGS:
353 ret = -EIO;
354 if (!access_ok(VERIFY_WRITE, datap,
355 sizeof(struct user_i387_struct)))
356 break;
357 ret = 0;
358 /* don't check EFAULT to be bug-to-bug compatible to i386 */
359 restore_i387_ia32(child, datap, 1);
360 break;
362 case PTRACE_GETFPXREGS: {
363 struct user32_fxsr_struct __user *u = datap;
364 init_fpu(child);
365 ret = -EIO;
366 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
367 break;
368 ret = -EFAULT;
369 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
370 break;
371 ret = __put_user(childregs->cs, &u->fcs);
372 ret |= __put_user(child->thread.ds, &u->fos);
373 break;
375 case PTRACE_SETFPXREGS: {
376 struct user32_fxsr_struct __user *u = datap;
377 unlazy_fpu(child);
378 ret = -EIO;
379 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
380 break;
381 /* no checking to be bug-to-bug compatible with i386. */
382 /* but silence warning */
383 if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
385 set_stopped_child_used_math(child);
386 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
387 ret = 0;
388 break;
391 case PTRACE_GETEVENTMSG:
392 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
393 break;
395 default:
396 BUG();
399 out:
400 put_task_struct(child);
401 return ret;