blob: 659c0722f6b825c75ca7d86743a872630cd9c45f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * 32bit ptrace for x86-64.
3 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
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>
Andi Kleenf0f2d652006-06-26 13:56:34 +020028#include <asm/ia32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Chuck Ebbert60923df2006-01-11 22:46:03 +010030/*
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).
34 */
35#define FLAG_MASK 0x54dd5UL
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define R32(l,q) \
38 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
39
40static int putreg32(struct task_struct *child, unsigned regno, u32 val)
41{
42 int i;
Al Virobb049232006-01-12 01:05:38 -080043 __u64 *stack = (__u64 *)task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 switch (regno) {
46 case offsetof(struct user32, regs.fs):
47 if (val && (val & 3) != 3) return -EIO;
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -070048 child->thread.fsindex = val & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 break;
50 case offsetof(struct user32, regs.gs):
51 if (val && (val & 3) != 3) return -EIO;
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -070052 child->thread.gsindex = val & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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;
69
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);
80
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;
86 }
87
88 case offsetof(struct user32, u_debugreg[4]):
89 case offsetof(struct user32, u_debugreg[5]):
90 return -EIO;
91
92 case offsetof(struct user32, u_debugreg[0]):
93 child->thread.debugreg0 = val;
94 break;
95
96 case offsetof(struct user32, u_debugreg[1]):
97 child->thread.debugreg1 = val;
98 break;
99
100 case offsetof(struct user32, u_debugreg[2]):
101 child->thread.debugreg2 = val;
102 break;
103
104 case offsetof(struct user32, u_debugreg[3]):
105 child->thread.debugreg3 = val;
106 break;
107
108 case offsetof(struct user32, u_debugreg[6]):
109 child->thread.debugreg6 = val;
110 break;
111
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 break;
121
122 default:
123 if (regno > sizeof(struct user32) || (regno & 3))
124 return -EIO;
125
126 /* Other dummy fields in the virtual user structure are ignored */
127 break;
128 }
129 return 0;
130}
131
132#undef R32
133
134#define R32(l,q) \
135 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
136
137static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
138{
Al Virobb049232006-01-12 01:05:38 -0800139 __u64 *stack = (__u64 *)task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 switch (regno) {
142 case offsetof(struct user32, regs.fs):
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -0700143 *val = child->thread.fsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 break;
145 case offsetof(struct user32, regs.gs):
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -0700146 *val = child->thread.gsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148 case offsetof(struct user32, regs.ds):
149 *val = child->thread.ds;
150 break;
151 case offsetof(struct user32, regs.es):
152 *val = child->thread.es;
153 break;
154
155 R32(cs, cs);
156 R32(ss, ss);
157 R32(ebx, rbx);
158 R32(ecx, rcx);
159 R32(edx, rdx);
160 R32(edi, rdi);
161 R32(esi, rsi);
162 R32(ebp, rbp);
163 R32(eax, rax);
164 R32(orig_eax, orig_rax);
165 R32(eip, rip);
166 R32(eflags, eflags);
167 R32(esp, rsp);
168
169 case offsetof(struct user32, u_debugreg[0]):
170 *val = child->thread.debugreg0;
171 break;
172 case offsetof(struct user32, u_debugreg[1]):
173 *val = child->thread.debugreg1;
174 break;
175 case offsetof(struct user32, u_debugreg[2]):
176 *val = child->thread.debugreg2;
177 break;
178 case offsetof(struct user32, u_debugreg[3]):
179 *val = child->thread.debugreg3;
180 break;
181 case offsetof(struct user32, u_debugreg[6]):
182 *val = child->thread.debugreg6;
183 break;
184 case offsetof(struct user32, u_debugreg[7]):
185 *val = child->thread.debugreg7;
186 break;
187
188 default:
189 if (regno > sizeof(struct user32) || (regno & 3))
190 return -EIO;
191
192 /* Other dummy fields in the virtual user structure are ignored */
193 *val = 0;
194 break;
195 }
196 return 0;
197}
198
199#undef R32
200
Andi Kleenf0f2d652006-06-26 13:56:34 +0200201static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
202{
203 int ret;
204 compat_siginfo_t *si32 = (compat_siginfo_t *)compat_ptr(data);
Andi Kleen2c87e2c2006-07-10 17:06:24 +0200205 siginfo_t ssi;
Andi Kleenf0f2d652006-06-26 13:56:34 +0200206 siginfo_t *si = compat_alloc_user_space(sizeof(siginfo_t));
207 if (request == PTRACE_SETSIGINFO) {
Andi Kleen2c87e2c2006-07-10 17:06:24 +0200208 memset(&ssi, 0, sizeof(siginfo_t));
209 ret = copy_siginfo_from_user32(&ssi, si32);
Andi Kleenf0f2d652006-06-26 13:56:34 +0200210 if (ret)
211 return ret;
Andi Kleen2c87e2c2006-07-10 17:06:24 +0200212 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
213 return -EFAULT;
Andi Kleenf0f2d652006-06-26 13:56:34 +0200214 }
215 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
216 if (ret)
217 return ret;
Andi Kleen2c87e2c2006-07-10 17:06:24 +0200218 if (request == PTRACE_GETSIGINFO) {
219 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
220 return -EFAULT;
221 ret = copy_siginfo_to_user32(si32, &ssi);
222 }
Andi Kleenf0f2d652006-06-26 13:56:34 +0200223 return ret;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
227{
228 struct task_struct *child;
229 struct pt_regs *childregs;
230 void __user *datap = compat_ptr(data);
231 int ret;
232 __u32 val;
233
234 switch (request) {
Andi Kleenf0f2d652006-06-26 13:56:34 +0200235 case PTRACE_TRACEME:
236 case PTRACE_ATTACH:
237 case PTRACE_KILL:
238 case PTRACE_CONT:
239 case PTRACE_SINGLESTEP:
240 case PTRACE_DETACH:
241 case PTRACE_SYSCALL:
242 case PTRACE_SETOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return sys_ptrace(request, pid, addr, data);
244
Andi Kleenf0f2d652006-06-26 13:56:34 +0200245 default:
246 return -EINVAL;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 case PTRACE_PEEKTEXT:
249 case PTRACE_PEEKDATA:
250 case PTRACE_POKEDATA:
251 case PTRACE_POKETEXT:
252 case PTRACE_POKEUSR:
253 case PTRACE_PEEKUSR:
254 case PTRACE_GETREGS:
255 case PTRACE_SETREGS:
256 case PTRACE_SETFPREGS:
257 case PTRACE_GETFPREGS:
258 case PTRACE_SETFPXREGS:
259 case PTRACE_GETFPXREGS:
260 case PTRACE_GETEVENTMSG:
261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Andi Kleenf0f2d652006-06-26 13:56:34 +0200263 case PTRACE_SETSIGINFO:
264 case PTRACE_GETSIGINFO:
265 return ptrace32_siginfo(request, pid, addr, data);
266 }
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800267
268 child = ptrace_get_task_struct(pid);
269 if (IS_ERR(child))
270 return PTR_ERR(child);
271
272 ret = ptrace_check_attach(child, request == PTRACE_KILL);
273 if (ret < 0)
274 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Al Virobb049232006-01-12 01:05:38 -0800276 childregs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 switch (request) {
279 case PTRACE_PEEKDATA:
280 case PTRACE_PEEKTEXT:
281 ret = 0;
282 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
283 ret = -EIO;
284 else
285 ret = put_user(val, (unsigned int __user *)datap);
286 break;
287
288 case PTRACE_POKEDATA:
289 case PTRACE_POKETEXT:
290 ret = 0;
291 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
292 ret = -EIO;
293 break;
294
295 case PTRACE_PEEKUSR:
296 ret = getreg32(child, addr, &val);
297 if (ret == 0)
298 ret = put_user(val, (__u32 __user *)datap);
299 break;
300
301 case PTRACE_POKEUSR:
302 ret = putreg32(child, addr, data);
303 break;
304
305 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
306 int i;
307 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
308 ret = -EIO;
309 break;
310 }
311 ret = 0;
312 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
313 getreg32(child, i, &val);
314 ret |= __put_user(val,(u32 __user *)datap);
315 datap += sizeof(u32);
316 }
317 break;
318 }
319
320 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
321 unsigned long tmp;
322 int i;
323 if (!access_ok(VERIFY_READ, datap, 16*4)) {
324 ret = -EIO;
325 break;
326 }
327 ret = 0;
328 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
329 ret |= __get_user(tmp, (u32 __user *)datap);
330 putreg32(child, i, tmp);
331 datap += sizeof(u32);
332 }
333 break;
334 }
335
336 case PTRACE_GETFPREGS:
337 ret = -EIO;
338 if (!access_ok(VERIFY_READ, compat_ptr(data),
339 sizeof(struct user_i387_struct)))
340 break;
341 save_i387_ia32(child, datap, childregs, 1);
342 ret = 0;
343 break;
344
345 case PTRACE_SETFPREGS:
346 ret = -EIO;
347 if (!access_ok(VERIFY_WRITE, datap,
348 sizeof(struct user_i387_struct)))
349 break;
350 ret = 0;
351 /* don't check EFAULT to be bug-to-bug compatible to i386 */
352 restore_i387_ia32(child, datap, 1);
353 break;
354
355 case PTRACE_GETFPXREGS: {
356 struct user32_fxsr_struct __user *u = datap;
357 init_fpu(child);
358 ret = -EIO;
359 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
360 break;
361 ret = -EFAULT;
362 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
363 break;
364 ret = __put_user(childregs->cs, &u->fcs);
365 ret |= __put_user(child->thread.ds, &u->fos);
366 break;
367 }
368 case PTRACE_SETFPXREGS: {
369 struct user32_fxsr_struct __user *u = datap;
370 unlazy_fpu(child);
371 ret = -EIO;
372 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
373 break;
374 /* no checking to be bug-to-bug compatible with i386 */
375 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
376 set_stopped_child_used_math(child);
377 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
378 ret = 0;
379 break;
380 }
381
382 case PTRACE_GETEVENTMSG:
383 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
384 break;
385
386 default:
Andi Kleenf0f2d652006-06-26 13:56:34 +0200387 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800390 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 put_task_struct(child);
392 return ret;
393}
394