blob: 5f4cdfa56901ef4528c4850a8a225f246a672a13 [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.
10 *
11 * $Id: ptrace32.c,v 1.16 2003/03/14 16:06:35 ak Exp $
12 */
13
14#include <linux/kernel.h>
15#include <linux/stddef.h>
16#include <linux/sched.h>
17#include <linux/syscalls.h>
18#include <linux/unistd.h>
19#include <linux/mm.h>
20#include <linux/ptrace.h>
21#include <asm/ptrace.h>
22#include <asm/compat.h>
23#include <asm/uaccess.h>
24#include <asm/user32.h>
25#include <asm/user.h>
26#include <asm/errno.h>
27#include <asm/debugreg.h>
28#include <asm/i387.h>
29#include <asm/fpu32.h>
30
31/* determines which flags the user has access to. */
32/* 1 = access 0 = no access */
33#define FLAG_MASK 0x44dd5UL
34
35#define R32(l,q) \
36 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
37
38static int putreg32(struct task_struct *child, unsigned regno, u32 val)
39{
40 int i;
41 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
42
43 switch (regno) {
44 case offsetof(struct user32, regs.fs):
45 if (val && (val & 3) != 3) return -EIO;
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -070046 child->thread.fsindex = val & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
48 case offsetof(struct user32, regs.gs):
49 if (val && (val & 3) != 3) return -EIO;
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -070050 child->thread.gsindex = val & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52 case offsetof(struct user32, regs.ds):
53 if (val && (val & 3) != 3) return -EIO;
54 child->thread.ds = val & 0xffff;
55 break;
56 case offsetof(struct user32, regs.es):
57 child->thread.es = val & 0xffff;
58 break;
59 case offsetof(struct user32, regs.ss):
60 if ((val & 3) != 3) return -EIO;
61 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
62 break;
63 case offsetof(struct user32, regs.cs):
64 if ((val & 3) != 3) return -EIO;
65 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
66 break;
67
68 R32(ebx, rbx);
69 R32(ecx, rcx);
70 R32(edx, rdx);
71 R32(edi, rdi);
72 R32(esi, rsi);
73 R32(ebp, rbp);
74 R32(eax, rax);
75 R32(orig_eax, orig_rax);
76 R32(eip, rip);
77 R32(esp, rsp);
78
79 case offsetof(struct user32, regs.eflags): {
80 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
81 val &= FLAG_MASK;
82 *flags = val | (*flags & ~FLAG_MASK);
83 break;
84 }
85
86 case offsetof(struct user32, u_debugreg[4]):
87 case offsetof(struct user32, u_debugreg[5]):
88 return -EIO;
89
90 case offsetof(struct user32, u_debugreg[0]):
91 child->thread.debugreg0 = val;
92 break;
93
94 case offsetof(struct user32, u_debugreg[1]):
95 child->thread.debugreg1 = val;
96 break;
97
98 case offsetof(struct user32, u_debugreg[2]):
99 child->thread.debugreg2 = val;
100 break;
101
102 case offsetof(struct user32, u_debugreg[3]):
103 child->thread.debugreg3 = val;
104 break;
105
106 case offsetof(struct user32, u_debugreg[6]):
107 child->thread.debugreg6 = val;
108 break;
109
110 case offsetof(struct user32, u_debugreg[7]):
111 val &= ~DR_CONTROL_RESERVED;
112 /* See arch/i386/kernel/ptrace.c for an explanation of
113 * this awkward check.*/
114 for(i=0; i<4; i++)
115 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
116 return -EIO;
117 child->thread.debugreg7 = val;
118 break;
119
120 default:
121 if (regno > sizeof(struct user32) || (regno & 3))
122 return -EIO;
123
124 /* Other dummy fields in the virtual user structure are ignored */
125 break;
126 }
127 return 0;
128}
129
130#undef R32
131
132#define R32(l,q) \
133 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
134
135static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
136{
137 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
138
139 switch (regno) {
140 case offsetof(struct user32, regs.fs):
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -0700141 *val = child->thread.fsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 break;
143 case offsetof(struct user32, regs.gs):
Daniel Jacobowitze8ed11b2005-08-04 13:41:09 -0700144 *val = child->thread.gsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 case offsetof(struct user32, regs.ds):
147 *val = child->thread.ds;
148 break;
149 case offsetof(struct user32, regs.es):
150 *val = child->thread.es;
151 break;
152
153 R32(cs, cs);
154 R32(ss, ss);
155 R32(ebx, rbx);
156 R32(ecx, rcx);
157 R32(edx, rdx);
158 R32(edi, rdi);
159 R32(esi, rsi);
160 R32(ebp, rbp);
161 R32(eax, rax);
162 R32(orig_eax, orig_rax);
163 R32(eip, rip);
164 R32(eflags, eflags);
165 R32(esp, rsp);
166
167 case offsetof(struct user32, u_debugreg[0]):
168 *val = child->thread.debugreg0;
169 break;
170 case offsetof(struct user32, u_debugreg[1]):
171 *val = child->thread.debugreg1;
172 break;
173 case offsetof(struct user32, u_debugreg[2]):
174 *val = child->thread.debugreg2;
175 break;
176 case offsetof(struct user32, u_debugreg[3]):
177 *val = child->thread.debugreg3;
178 break;
179 case offsetof(struct user32, u_debugreg[6]):
180 *val = child->thread.debugreg6;
181 break;
182 case offsetof(struct user32, u_debugreg[7]):
183 *val = child->thread.debugreg7;
184 break;
185
186 default:
187 if (regno > sizeof(struct user32) || (regno & 3))
188 return -EIO;
189
190 /* Other dummy fields in the virtual user structure are ignored */
191 *val = 0;
192 break;
193 }
194 return 0;
195}
196
197#undef R32
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
200{
201 struct task_struct *child;
202 struct pt_regs *childregs;
203 void __user *datap = compat_ptr(data);
204 int ret;
205 __u32 val;
206
207 switch (request) {
208 default:
209 return sys_ptrace(request, pid, addr, data);
210
211 case PTRACE_PEEKTEXT:
212 case PTRACE_PEEKDATA:
213 case PTRACE_POKEDATA:
214 case PTRACE_POKETEXT:
215 case PTRACE_POKEUSR:
216 case PTRACE_PEEKUSR:
217 case PTRACE_GETREGS:
218 case PTRACE_SETREGS:
219 case PTRACE_SETFPREGS:
220 case PTRACE_GETFPREGS:
221 case PTRACE_SETFPXREGS:
222 case PTRACE_GETFPXREGS:
223 case PTRACE_GETEVENTMSG:
224 break;
225 }
226
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800227 if (request == PTRACE_TRACEME)
228 return ptrace_traceme();
229
230 child = ptrace_get_task_struct(pid);
231 if (IS_ERR(child))
232 return PTR_ERR(child);
233
234 ret = ptrace_check_attach(child, request == PTRACE_KILL);
235 if (ret < 0)
236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 childregs = (struct pt_regs *)(child->thread.rsp0 - sizeof(struct pt_regs));
239
240 switch (request) {
241 case PTRACE_PEEKDATA:
242 case PTRACE_PEEKTEXT:
243 ret = 0;
244 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
245 ret = -EIO;
246 else
247 ret = put_user(val, (unsigned int __user *)datap);
248 break;
249
250 case PTRACE_POKEDATA:
251 case PTRACE_POKETEXT:
252 ret = 0;
253 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
254 ret = -EIO;
255 break;
256
257 case PTRACE_PEEKUSR:
258 ret = getreg32(child, addr, &val);
259 if (ret == 0)
260 ret = put_user(val, (__u32 __user *)datap);
261 break;
262
263 case PTRACE_POKEUSR:
264 ret = putreg32(child, addr, data);
265 break;
266
267 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
268 int i;
269 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
270 ret = -EIO;
271 break;
272 }
273 ret = 0;
274 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
275 getreg32(child, i, &val);
276 ret |= __put_user(val,(u32 __user *)datap);
277 datap += sizeof(u32);
278 }
279 break;
280 }
281
282 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
283 unsigned long tmp;
284 int i;
285 if (!access_ok(VERIFY_READ, datap, 16*4)) {
286 ret = -EIO;
287 break;
288 }
289 ret = 0;
290 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
291 ret |= __get_user(tmp, (u32 __user *)datap);
292 putreg32(child, i, tmp);
293 datap += sizeof(u32);
294 }
295 break;
296 }
297
298 case PTRACE_GETFPREGS:
299 ret = -EIO;
300 if (!access_ok(VERIFY_READ, compat_ptr(data),
301 sizeof(struct user_i387_struct)))
302 break;
303 save_i387_ia32(child, datap, childregs, 1);
304 ret = 0;
305 break;
306
307 case PTRACE_SETFPREGS:
308 ret = -EIO;
309 if (!access_ok(VERIFY_WRITE, datap,
310 sizeof(struct user_i387_struct)))
311 break;
312 ret = 0;
313 /* don't check EFAULT to be bug-to-bug compatible to i386 */
314 restore_i387_ia32(child, datap, 1);
315 break;
316
317 case PTRACE_GETFPXREGS: {
318 struct user32_fxsr_struct __user *u = datap;
319 init_fpu(child);
320 ret = -EIO;
321 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
322 break;
323 ret = -EFAULT;
324 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
325 break;
326 ret = __put_user(childregs->cs, &u->fcs);
327 ret |= __put_user(child->thread.ds, &u->fos);
328 break;
329 }
330 case PTRACE_SETFPXREGS: {
331 struct user32_fxsr_struct __user *u = datap;
332 unlazy_fpu(child);
333 ret = -EIO;
334 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
335 break;
336 /* no checking to be bug-to-bug compatible with i386 */
337 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
338 set_stopped_child_used_math(child);
339 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
340 ret = 0;
341 break;
342 }
343
344 case PTRACE_GETEVENTMSG:
345 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
346 break;
347
348 default:
349 ret = -EINVAL;
350 break;
351 }
352
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800353 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 put_task_struct(child);
355 return ret;
356}
357