blob: 26071305de2c783656f702701eff557d90883c87 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* By Ross Biro 1/23/92 */
2/*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
5 */
6
7#include <linux/kernel.h>
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/ptrace.h>
13#include <linux/user.h>
14#include <linux/security.h>
15#include <linux/audit.h>
16#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070017#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/uaccess.h>
20#include <asm/pgtable.h>
21#include <asm/system.h>
22#include <asm/processor.h>
23#include <asm/i387.h>
24#include <asm/debugreg.h>
25#include <asm/ldt.h>
26#include <asm/desc.h>
27
28/*
29 * does not yet catch signals sent when the child dies.
30 * in exit.c or in signal.c.
31 */
32
Chuck Ebbert9f155b92006-01-05 23:11:29 -050033/*
34 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert3c36c6a2006-03-23 02:59:39 -080035 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
Chuck Ebbert9f155b92006-01-05 23:11:29 -050036 * Also masks reserved bits (31-22, 15, 5, 3, 1).
37 */
Chuck Ebbert3c36c6a2006-03-23 02:59:39 -080038#define FLAG_MASK 0x00050dd5
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Roland McGrath62a97d42008-01-30 13:30:52 +010040static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010042 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Roland McGrath62a97d42008-01-30 13:30:52 +010043 if (regno > FS)
44 --regno;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010045 return &regs->bx + regno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48static int putreg(struct task_struct *child,
49 unsigned long regno, unsigned long value)
50{
Roland McGrath62a97d42008-01-30 13:30:52 +010051 struct pt_regs *regs = task_pt_regs(child);
52 regno >>= 2;
53 switch (regno) {
Roland McGrath9e714be2008-01-30 13:30:58 +010054 case GS:
55 if (value && (value & 3) != 3)
56 return -EIO;
57 child->thread.gs = value;
Roland McGrath5fd4d162008-01-30 13:30:58 +010058 if (child == current)
59 /*
60 * The user-mode %gs is not affected by
61 * kernel entry, so we must update the CPU.
62 */
63 loadsegment(gs, value);
Roland McGrath9e714be2008-01-30 13:30:58 +010064 return 0;
65 case DS:
66 case ES:
67 case FS:
68 if (value && (value & 3) != 3)
69 return -EIO;
70 value &= 0xffff;
71 break;
72 case SS:
73 case CS:
74 if ((value & 3) != 3)
75 return -EIO;
76 value &= 0xffff;
77 break;
78 case EFL:
79 value &= FLAG_MASK;
80 /*
81 * If the user value contains TF, mark that
82 * it was not "us" (the debugger) that set it.
83 * If not, make sure it stays set if we had.
84 */
85 if (value & X86_EFLAGS_TF)
86 clear_tsk_thread_flag(child, TIF_FORCED_TF);
87 else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
88 value |= X86_EFLAGS_TF;
89 value |= regs->flags & ~FLAG_MASK;
90 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Roland McGrath62a97d42008-01-30 13:30:52 +010092 *pt_regs_access(regs, regno) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94}
95
Roland McGrath62a97d42008-01-30 13:30:52 +010096static unsigned long getreg(struct task_struct *child, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Roland McGrath62a97d42008-01-30 13:30:52 +010098 struct pt_regs *regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned long retval = ~0UL;
100
Roland McGrath62a97d42008-01-30 13:30:52 +0100101 regno >>= 2;
102 switch (regno) {
Roland McGrath9e714be2008-01-30 13:30:58 +0100103 case EFL:
104 /*
105 * If the debugger set TF, hide it from the readout.
106 */
107 retval = regs->flags;
108 if (test_tsk_thread_flag(child, TIF_FORCED_TF))
109 retval &= ~X86_EFLAGS_TF;
110 break;
111 case GS:
112 retval = child->thread.gs;
Roland McGrath5fd4d162008-01-30 13:30:58 +0100113 if (child == current)
114 savesegment(gs, retval);
Roland McGrath9e714be2008-01-30 13:30:58 +0100115 break;
116 case DS:
117 case ES:
118 case FS:
119 case SS:
120 case CS:
121 retval = 0xffff;
122 /* fall through */
123 default:
124 retval &= *pt_regs_access(regs, regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 return retval;
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100130 * This function is trivial and will be inlined by the compiler.
131 * Having it separates the implementation details of debug
132 * registers from the interface details of ptrace.
133 */
134static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
135{
136 return child->thread.debugreg[n];
137}
138
139static int ptrace_set_debugreg(struct task_struct *child,
140 int n, unsigned long data)
141{
142 if (unlikely(n == 4 || n == 5))
143 return -EIO;
144
145 if (n < 4 && unlikely(data >= TASK_SIZE - 3))
146 return -EIO;
147
148 if (n == 7) {
149 /*
150 * Sanity-check data. Take one half-byte at once with
151 * check = (val >> (16 + 4*i)) & 0xf. It contains the
152 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
153 * 2 and 3 are LENi. Given a list of invalid values,
154 * we do mask |= 1 << invalid_value, so that
155 * (mask >> check) & 1 is a correct test for invalid
156 * values.
157 *
158 * R/Wi contains the type of the breakpoint /
159 * watchpoint, LENi contains the length of the watched
160 * data in the watchpoint case.
161 *
162 * The invalid values are:
163 * - LENi == 0x10 (undefined), so mask |= 0x0f00.
164 * - R/Wi == 0x10 (break on I/O reads or writes), so
165 * mask |= 0x4444.
166 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
167 * 0x1110.
168 *
169 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
170 *
171 * See the Intel Manual "System Programming Guide",
172 * 15.2.4
173 *
174 * Note that LENi == 0x10 is defined on x86_64 in long
175 * mode (i.e. even for 32-bit userspace software, but
176 * 64-bit kernel), so the x86_64 mask value is 0x5454.
177 * See the AMD manual no. 24593 (AMD64 System Programming)
178 */
179 int i;
180 data &= ~DR_CONTROL_RESERVED;
181 for (i = 0; i < 4; i++)
182 if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
183 return -EIO;
184 if (data)
185 set_tsk_thread_flag(child, TIF_DEBUG);
186 else
187 clear_tsk_thread_flag(child, TIF_DEBUG);
188 }
189
190 child->thread.debugreg[n] = data;
191
192 return 0;
193}
194
195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Called by kernel/ptrace.c when detaching..
197 *
198 * Make sure the single step bit is not set.
199 */
200void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100201{
Roland McGrath7f232342008-01-30 13:30:48 +0100202 user_disable_single_step(child);
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700203 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Christoph Hellwig481bed42005-11-07 00:59:47 -0800206long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct user * dummy = NULL;
209 int i, ret;
210 unsigned long __user *datap = (unsigned long __user *)data;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 switch (request) {
213 /* when I and D space are separate, these will need to be fixed. */
Roland McGrath9e714be2008-01-30 13:30:58 +0100214 case PTRACE_PEEKTEXT: /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700215 case PTRACE_PEEKDATA:
216 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* read the word at location addr in the USER area. */
220 case PTRACE_PEEKUSR: {
221 unsigned long tmp;
222
223 ret = -EIO;
Roland McGrath9e714be2008-01-30 13:30:58 +0100224 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 addr > sizeof(struct user) - 3)
226 break;
227
228 tmp = 0; /* Default return condition */
229 if(addr < FRAME_SIZE*sizeof(long))
230 tmp = getreg(child, addr);
231 if(addr >= (long) &dummy->u_debugreg[0] &&
232 addr <= (long) &dummy->u_debugreg[7]){
233 addr -= (long) &dummy->u_debugreg[0];
234 addr = addr >> 2;
Roland McGrathd9771e82008-01-30 13:30:52 +0100235 tmp = ptrace_get_debugreg(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 ret = put_user(tmp, datap);
238 break;
239 }
240
241 /* when I and D space are separate, this will have to be fixed. */
242 case PTRACE_POKETEXT: /* write the word at location addr. */
243 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700244 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246
247 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
248 ret = -EIO;
Roland McGrath9e714be2008-01-30 13:30:58 +0100249 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 addr > sizeof(struct user) - 3)
251 break;
252
253 if (addr < FRAME_SIZE*sizeof(long)) {
254 ret = putreg(child, addr, data);
255 break;
256 }
257 /* We need to be very careful here. We implicitly
258 want to modify a portion of the task_struct, and we
259 have to be selective about what portions we allow someone
260 to modify. */
261
262 ret = -EIO;
263 if(addr >= (long) &dummy->u_debugreg[0] &&
264 addr <= (long) &dummy->u_debugreg[7]){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 addr -= (long) &dummy->u_debugreg;
266 addr = addr >> 2;
Roland McGrathd9771e82008-01-30 13:30:52 +0100267 ret = ptrace_set_debugreg(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 break;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
272 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
273 ret = -EIO;
274 break;
275 }
276 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
277 __put_user(getreg(child, i), datap);
278 datap++;
279 }
280 ret = 0;
281 break;
282 }
283
284 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
285 unsigned long tmp;
286 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
287 ret = -EIO;
288 break;
289 }
290 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
291 __get_user(tmp, datap);
292 putreg(child, i, tmp);
293 datap++;
294 }
295 ret = 0;
296 break;
297 }
298
299 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
300 if (!access_ok(VERIFY_WRITE, datap,
301 sizeof(struct user_i387_struct))) {
302 ret = -EIO;
303 break;
304 }
305 ret = 0;
306 if (!tsk_used_math(child))
307 init_fpu(child);
308 get_fpregs((struct user_i387_struct __user *)data, child);
309 break;
310 }
311
312 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
313 if (!access_ok(VERIFY_READ, datap,
314 sizeof(struct user_i387_struct))) {
315 ret = -EIO;
316 break;
317 }
318 set_stopped_child_used_math(child);
319 set_fpregs(child, (struct user_i387_struct __user *)data);
320 ret = 0;
321 break;
322 }
323
324 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
325 if (!access_ok(VERIFY_WRITE, datap,
326 sizeof(struct user_fxsr_struct))) {
327 ret = -EIO;
328 break;
329 }
330 if (!tsk_used_math(child))
331 init_fpu(child);
332 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
333 break;
334 }
335
336 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
337 if (!access_ok(VERIFY_READ, datap,
338 sizeof(struct user_fxsr_struct))) {
339 ret = -EIO;
340 break;
341 }
342 set_stopped_child_used_math(child);
343 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
344 break;
345 }
346
347 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100348 if (addr < 0)
349 return -EIO;
350 ret = do_get_thread_area(child, addr,
351 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353
354 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100355 if (addr < 0)
356 return -EIO;
357 ret = do_set_thread_area(child, addr,
358 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
360
361 default:
362 ret = ptrace_request(child, request, addr, data);
363 break;
364 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return ret;
367}
368
369void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
370{
371 struct siginfo info;
372
373 tsk->thread.trap_no = 1;
374 tsk->thread.error_code = error_code;
375
376 memset(&info, 0, sizeof(info));
377 info.si_signo = SIGTRAP;
378 info.si_code = TRAP_BRKPT;
379
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100380 /* User-mode ip? */
381 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Simon Arlott27b46d72007-10-20 01:13:56 +0200383 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 force_sig_info(SIGTRAP, &info, tsk);
385}
386
387/* notification of system call entry/exit
388 * - triggered by current->work.syscall_trace
389 */
390__attribute__((regparm(3)))
Laurent Viviered75e8d2005-09-03 15:57:18 -0700391int do_syscall_trace(struct pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -0700393 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
394 /*
395 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
396 * interception
397 */
Bodo Stroesser1b38f002005-09-03 15:57:20 -0700398 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -0700399 int ret = 0;
Bodo Stroesser1b38f002005-09-03 15:57:20 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* do the secure computing check first */
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -0700402 if (!entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100403 secure_computing(regs->orig_ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700405 if (unlikely(current->audit_context)) {
406 if (entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100407 audit_syscall_exit(AUDITSC_RESULT(regs->ax),
408 regs->ax);
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700409 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
410 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
411 * not used, entry.S will call us only on syscall exit, not
412 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
413 * calling send_sigtrap() on syscall entry.
414 *
415 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
416 * is_singlestep is false, despite his name, so we will still do
417 * the correct thing.
418 */
419 else if (is_singlestep)
420 goto out;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (!(current->ptrace & PT_PTRACED))
2fd6f582005-04-29 16:08:28 +0100424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Bodo Stroesser1b38f002005-09-03 15:57:20 -0700426 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
427 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
428 * here. We have to check this and return */
429 if (is_sysemu && entryexit)
430 return 0;
Laurent Viviered75e8d2005-09-03 15:57:18 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* Fake a debug trap */
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -0700433 if (is_singlestep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 send_sigtrap(current, regs, 0);
435
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -0700436 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
2fd6f582005-04-29 16:08:28 +0100437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* the 0x80 provides a way for the tracing parent to distinguish
440 between a syscall stop and SIGTRAP delivery */
Laurent Viviered75e8d2005-09-03 15:57:18 -0700441 /* Note that the debugger could change the result of test_thread_flag!*/
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -0700442 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /*
445 * this isn't the same as continuing with a signal, but it will do
446 * for normal use. strace only continues with a signal if the
447 * stopping signal is not SIGTRAP. -brl
448 */
449 if (current->exit_code) {
450 send_sig(current->exit_code, current, 1);
451 current->exit_code = 0;
452 }
Laurent Viviered75e8d2005-09-03 15:57:18 -0700453 ret = is_sysemu;
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -0700454out:
2fd6f582005-04-29 16:08:28 +0100455 if (unlikely(current->audit_context) && !entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100456 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
457 regs->bx, regs->cx, regs->dx, regs->si);
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -0700458 if (ret == 0)
459 return 0;
460
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100461 regs->orig_ax = -1; /* force skip of syscall restarting */
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -0700462 if (unlikely(current->audit_context))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100463 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -0700464 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}