x86: use generic register names in struct user_regs_struct
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / ptrace_64.c
blob56b31cd3b8650d714075cfe19d18059ffd0fe494
1 /* By Ross Biro 1/23/92 */
2 /*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
5 *
6 * x86-64 port 2000-2002 Andi Kleen
7 */
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
18 #include <linux/seccomp.h>
19 #include <linux/signal.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
23 #include <asm/system.h>
24 #include <asm/processor.h>
25 #include <asm/prctl.h>
26 #include <asm/i387.h>
27 #include <asm/debugreg.h>
28 #include <asm/ldt.h>
29 #include <asm/desc.h>
30 #include <asm/proto.h>
31 #include <asm/ia32.h>
34 * does not yet catch signals sent when the child dies.
35 * in exit.c or in signal.c.
39 * Determines which flags the user has access to [1 = access, 0 = no access].
40 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
41 * Also masks reserved bits (63-22, 15, 5, 3, 1).
43 #define FLAG_MASK 0x54dd5UL
46 * Called by kernel/ptrace.c when detaching..
48 * Make sure the single step bit is not set.
50 void ptrace_disable(struct task_struct *child)
52 user_disable_single_step(child);
55 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
57 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
58 return &regs->r15 + (offset / sizeof(regs->r15));
61 static int putreg(struct task_struct *child,
62 unsigned long regno, unsigned long value)
64 struct pt_regs *regs = task_pt_regs(child);
65 switch (regno) {
66 case offsetof(struct user_regs_struct,fs):
67 if (value && (value & 3) != 3)
68 return -EIO;
69 child->thread.fsindex = value & 0xffff;
70 return 0;
71 case offsetof(struct user_regs_struct,gs):
72 if (value && (value & 3) != 3)
73 return -EIO;
74 child->thread.gsindex = value & 0xffff;
75 return 0;
76 case offsetof(struct user_regs_struct,ds):
77 if (value && (value & 3) != 3)
78 return -EIO;
79 child->thread.ds = value & 0xffff;
80 return 0;
81 case offsetof(struct user_regs_struct,es):
82 if (value && (value & 3) != 3)
83 return -EIO;
84 child->thread.es = value & 0xffff;
85 return 0;
86 case offsetof(struct user_regs_struct,ss):
87 if ((value & 3) != 3)
88 return -EIO;
89 value &= 0xffff;
90 return 0;
91 case offsetof(struct user_regs_struct,fs_base):
92 if (value >= TASK_SIZE_OF(child))
93 return -EIO;
95 * When changing the segment base, use do_arch_prctl
96 * to set either thread.fs or thread.fsindex and the
97 * corresponding GDT slot.
99 if (child->thread.fs != value)
100 return do_arch_prctl(child, ARCH_SET_FS, value);
101 return 0;
102 case offsetof(struct user_regs_struct,gs_base):
104 * Exactly the same here as the %fs handling above.
106 if (value >= TASK_SIZE_OF(child))
107 return -EIO;
108 if (child->thread.gs != value)
109 return do_arch_prctl(child, ARCH_SET_GS, value);
110 return 0;
111 case offsetof(struct user_regs_struct,flags):
112 value &= FLAG_MASK;
114 * If the user value contains TF, mark that
115 * it was not "us" (the debugger) that set it.
116 * If not, make sure it stays set if we had.
118 if (value & X86_EFLAGS_TF)
119 clear_tsk_thread_flag(child, TIF_FORCED_TF);
120 else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
121 value |= X86_EFLAGS_TF;
122 value |= regs->flags & ~FLAG_MASK;
123 break;
124 case offsetof(struct user_regs_struct,cs):
125 if ((value & 3) != 3)
126 return -EIO;
127 value &= 0xffff;
128 break;
130 *pt_regs_access(regs, regno) = value;
131 return 0;
134 static unsigned long getreg(struct task_struct *child, unsigned long regno)
136 struct pt_regs *regs = task_pt_regs(child);
137 unsigned long val;
138 switch (regno) {
139 case offsetof(struct user_regs_struct, fs):
140 return child->thread.fsindex;
141 case offsetof(struct user_regs_struct, gs):
142 return child->thread.gsindex;
143 case offsetof(struct user_regs_struct, ds):
144 return child->thread.ds;
145 case offsetof(struct user_regs_struct, es):
146 return child->thread.es;
147 case offsetof(struct user_regs_struct, fs_base):
149 * do_arch_prctl may have used a GDT slot instead of
150 * the MSR. To userland, it appears the same either
151 * way, except the %fs segment selector might not be 0.
153 if (child->thread.fs != 0)
154 return child->thread.fs;
155 if (child->thread.fsindex != FS_TLS_SEL)
156 return 0;
157 return get_desc_base(&child->thread.tls_array[FS_TLS]);
158 case offsetof(struct user_regs_struct, gs_base):
160 * Exactly the same here as the %fs handling above.
162 if (child->thread.gs != 0)
163 return child->thread.gs;
164 if (child->thread.gsindex != GS_TLS_SEL)
165 return 0;
166 return get_desc_base(&child->thread.tls_array[GS_TLS]);
167 case offsetof(struct user_regs_struct, flags):
169 * If the debugger set TF, hide it from the readout.
171 val = regs->flags;
172 if (test_tsk_thread_flag(child, TIF_IA32))
173 val &= 0xffffffff;
174 if (test_tsk_thread_flag(child, TIF_FORCED_TF))
175 val &= ~X86_EFLAGS_TF;
176 return val;
177 default:
178 val = *pt_regs_access(regs, regno);
179 if (test_tsk_thread_flag(child, TIF_IA32))
180 val &= 0xffffffff;
181 return val;
186 unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
188 switch (n) {
189 case 0: return child->thread.debugreg0;
190 case 1: return child->thread.debugreg1;
191 case 2: return child->thread.debugreg2;
192 case 3: return child->thread.debugreg3;
193 case 6: return child->thread.debugreg6;
194 case 7: return child->thread.debugreg7;
196 return 0;
199 int ptrace_set_debugreg(struct task_struct *child, int n, unsigned long data)
201 int i;
203 if (n < 4) {
204 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
205 if (unlikely(data >= TASK_SIZE_OF(child) - dsize))
206 return -EIO;
209 switch (n) {
210 case 0: child->thread.debugreg0 = data; break;
211 case 1: child->thread.debugreg1 = data; break;
212 case 2: child->thread.debugreg2 = data; break;
213 case 3: child->thread.debugreg3 = data; break;
215 case 6:
216 if (data >> 32)
217 return -EIO;
218 child->thread.debugreg6 = data;
219 break;
221 case 7:
223 * See ptrace_32.c for an explanation of this awkward check.
225 data &= ~DR_CONTROL_RESERVED;
226 for (i = 0; i < 4; i++)
227 if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
228 return -EIO;
229 child->thread.debugreg7 = data;
230 if (data)
231 set_tsk_thread_flag(child, TIF_DEBUG);
232 else
233 clear_tsk_thread_flag(child, TIF_DEBUG);
234 break;
237 return 0;
240 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
242 long ret;
243 unsigned ui;
245 switch (request) {
246 /* when I and D space are separate, these will need to be fixed. */
247 case PTRACE_PEEKTEXT: /* read word at location addr. */
248 case PTRACE_PEEKDATA:
249 ret = generic_ptrace_peekdata(child, addr, data);
250 break;
252 /* read the word at location addr in the USER area. */
253 case PTRACE_PEEKUSR: {
254 unsigned long tmp;
256 ret = -EIO;
257 if ((addr & 7) ||
258 addr > sizeof(struct user) - 7)
259 break;
261 tmp = 0;
262 if (addr < sizeof(struct user_regs_struct))
263 tmp = getreg(child, addr);
264 else if (addr >= offsetof(struct user, u_debugreg[0])) {
265 addr -= offsetof(struct user, u_debugreg[0]);
266 tmp = ptrace_get_debugreg(child, addr / sizeof(long));
269 ret = put_user(tmp,(unsigned long __user *) data);
270 break;
273 /* when I and D space are separate, this will have to be fixed. */
274 case PTRACE_POKETEXT: /* write the word at location addr. */
275 case PTRACE_POKEDATA:
276 ret = generic_ptrace_pokedata(child, addr, data);
277 break;
279 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
280 ret = -EIO;
281 if ((addr & 7) ||
282 addr > sizeof(struct user) - 7)
283 break;
285 if (addr < sizeof(struct user_regs_struct))
286 ret = putreg(child, addr, data);
287 else if (addr >= offsetof(struct user, u_debugreg[0])) {
288 addr -= offsetof(struct user, u_debugreg[0]);
289 ret = ptrace_set_debugreg(child,
290 addr / sizeof(long), data);
292 break;
294 #ifdef CONFIG_IA32_EMULATION
295 /* This makes only sense with 32bit programs. Allow a
296 64bit debugger to fully examine them too. Better
297 don't use it against 64bit processes, use
298 PTRACE_ARCH_PRCTL instead. */
299 case PTRACE_GET_THREAD_AREA:
300 if (addr < 0)
301 return -EIO;
302 ret = do_get_thread_area(child, addr,
303 (struct user_desc __user *) data);
305 break;
306 case PTRACE_SET_THREAD_AREA:
307 if (addr < 0)
308 return -EIO;
309 ret = do_set_thread_area(child, addr,
310 (struct user_desc __user *) data, 0);
311 break;
312 #endif
313 /* normal 64bit interface to access TLS data.
314 Works just like arch_prctl, except that the arguments
315 are reversed. */
316 case PTRACE_ARCH_PRCTL:
317 ret = do_arch_prctl(child, data, addr);
318 break;
320 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
321 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
322 sizeof(struct user_regs_struct))) {
323 ret = -EIO;
324 break;
326 ret = 0;
327 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
328 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
329 data += sizeof(long);
331 break;
334 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
335 unsigned long tmp;
336 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
337 sizeof(struct user_regs_struct))) {
338 ret = -EIO;
339 break;
341 ret = 0;
342 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
343 ret = __get_user(tmp, (unsigned long __user *) data);
344 if (ret)
345 break;
346 ret = putreg(child, ui, tmp);
347 if (ret)
348 break;
349 data += sizeof(long);
351 break;
354 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
355 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
356 sizeof(struct user_i387_struct))) {
357 ret = -EIO;
358 break;
360 ret = get_fpregs((struct user_i387_struct __user *)data, child);
361 break;
364 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
365 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
366 sizeof(struct user_i387_struct))) {
367 ret = -EIO;
368 break;
370 set_stopped_child_used_math(child);
371 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
372 break;
375 default:
376 ret = ptrace_request(child, request, addr, data);
377 break;
379 return ret;
382 static void syscall_trace(struct pt_regs *regs)
385 #if 0
386 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
387 current->comm,
388 regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
389 current_thread_info()->flags, current->ptrace);
390 #endif
392 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
393 ? 0x80 : 0));
395 * this isn't the same as continuing with a signal, but it will do
396 * for normal use. strace only continues with a signal if the
397 * stopping signal is not SIGTRAP. -brl
399 if (current->exit_code) {
400 send_sig(current->exit_code, current, 1);
401 current->exit_code = 0;
405 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
407 /* do the secure computing check first */
408 secure_computing(regs->orig_ax);
410 if (test_thread_flag(TIF_SYSCALL_TRACE)
411 && (current->ptrace & PT_PTRACED))
412 syscall_trace(regs);
414 if (unlikely(current->audit_context)) {
415 if (test_thread_flag(TIF_IA32)) {
416 audit_syscall_entry(AUDIT_ARCH_I386,
417 regs->orig_ax,
418 regs->bx, regs->cx,
419 regs->dx, regs->si);
420 } else {
421 audit_syscall_entry(AUDIT_ARCH_X86_64,
422 regs->orig_ax,
423 regs->di, regs->si,
424 regs->dx, regs->r10);
429 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
431 if (unlikely(current->audit_context))
432 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
434 if ((test_thread_flag(TIF_SYSCALL_TRACE)
435 || test_thread_flag(TIF_SINGLESTEP))
436 && (current->ptrace & PT_PTRACED))
437 syscall_trace(regs);