blob: 06b190390505f848a095648d2c555bd9d996604d [file] [log] [blame]
Jeff Dikeba180fd2007-10-16 01:27:00 -07001/*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/audit.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -07007#include "linux/ptrace.h"
8#include "linux/sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "asm/uaccess.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "skas_ptrace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Christoph Hellwig1bd09502010-03-10 15:22:56 -080012
13
14void user_enable_single_step(struct task_struct *child)
Bodo Stroesser82c1c112005-05-06 21:30:46 -070015{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080016 child->ptrace |= PT_DTRACE;
Jeff Dikeba180fd2007-10-16 01:27:00 -070017 child->thread.singlestep_syscall = 0;
Bodo Stroesser82c1c112005-05-06 21:30:46 -070018
19#ifdef SUBARCH_SET_SINGLESTEPPING
Christoph Hellwig1bd09502010-03-10 15:22:56 -080020 SUBARCH_SET_SINGLESTEPPING(child, 1);
21#endif
22}
23
24void user_disable_single_step(struct task_struct *child)
25{
26 child->ptrace &= ~PT_DTRACE;
27 child->thread.singlestep_syscall = 0;
28
29#ifdef SUBARCH_SET_SINGLESTEPPING
30 SUBARCH_SET_SINGLESTEPPING(child, 0);
Bodo Stroesser82c1c112005-05-06 21:30:46 -070031#endif
Jeff Dikeba9950c2005-05-20 13:59:07 -070032}
Bodo Stroesser82c1c112005-05-06 21:30:46 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * Called by kernel/ptrace.c when detaching..
36 */
37void ptrace_disable(struct task_struct *child)
Jeff Dikeba180fd2007-10-16 01:27:00 -070038{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080039 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Bodo Stroesser82c1c112005-05-06 21:30:46 -070042extern int peek_user(struct task_struct * child, long addr, long data);
43extern int poke_user(struct task_struct * child, long addr, long data);
44
Namhyung Kim9b05a692010-10-27 15:33:47 -070045long arch_ptrace(struct task_struct *child, long request,
46 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int i, ret;
Namhyung Kim9b05a692010-10-27 15:33:47 -070049 unsigned long __user *p = (void __user *)data;
Namhyung Kim0a3d7632010-10-27 15:34:04 -070050 void __user *vp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* read the word at location addr in the USER area. */
Jeff Dikeba180fd2007-10-16 01:27:00 -070054 case PTRACE_PEEKUSR:
55 ret = peek_user(child, addr, data);
56 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 /* write the word at location addr in the USER area */
59 case PTRACE_POKEUSR:
60 ret = poke_user(child, addr, data);
61 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Renzo Davoli86d6f2b2009-03-12 14:31:23 -070063 case PTRACE_SYSEMU:
64 case PTRACE_SYSEMU_SINGLESTEP:
65 ret = -EIO;
66 break;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifdef PTRACE_GETREGS
69 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -080070 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 ret = -EIO;
72 break;
73 }
74 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080075 __put_user(getreg(child, i), p);
76 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78 ret = 0;
79 break;
80 }
81#endif
82#ifdef PTRACE_SETREGS
83 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
84 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -080085 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 ret = -EIO;
87 break;
88 }
89 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080090 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -080092 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94 ret = 0;
95 break;
96 }
97#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -080098 case PTRACE_GET_THREAD_AREA:
Namhyung Kim0a3d7632010-10-27 15:34:04 -070099 ret = ptrace_get_thread_area(child, addr, vp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800100 break;
101
102 case PTRACE_SET_THREAD_AREA:
Richard Weinberger8818b672010-11-11 14:05:04 -0800103 ret = ptrace_set_thread_area(child, addr, vp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800104 break;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 case PTRACE_FAULTINFO: {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700107 /*
108 * Take the info from thread->arch->faultinfo,
Al Viro4d338e12006-03-31 02:30:15 -0800109 * but transfer max. sizeof(struct ptrace_faultinfo).
110 * On i386, ptrace_faultinfo is smaller!
111 */
112 ret = copy_to_user(p, &child->thread.arch.faultinfo,
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700113 sizeof(struct ptrace_faultinfo)) ?
114 -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Bodo Stroesserc5784552005-05-05 16:15:31 -0700118#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 case PTRACE_LDT: {
120 struct ptrace_ldt ldt;
121
Jeff Dikeba180fd2007-10-16 01:27:00 -0700122 if (copy_from_user(&ldt, p, sizeof(ldt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 ret = -EIO;
124 break;
125 }
126
Jeff Dikeba180fd2007-10-16 01:27:00 -0700127 /*
128 * This one is confusing, so just punt and return -EIO for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * now
130 */
131 ret = -EIO;
132 break;
133 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 default:
136 ret = ptrace_request(child, request, addr, data);
Jeff Dikee8012b52007-10-16 01:27:16 -0700137 if (ret == -EIO)
138 ret = subarch_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret;
143}
144
WANG Cong99764fa2008-07-23 21:28:49 -0700145static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int error_code)
147{
148 struct siginfo info;
149
150 memset(&info, 0, sizeof(info));
151 info.si_signo = SIGTRAP;
152 info.si_code = TRAP_BRKPT;
153
154 /* User-mode eip? */
155 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
156
Simon Arlottb60745b2007-10-20 01:23:03 +0200157 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 force_sig_info(SIGTRAP, &info, tsk);
159}
160
Jeff Dikeba180fd2007-10-16 01:27:00 -0700161/*
162 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
164 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700165void syscall_trace(struct uml_pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
168 int tracesysgood;
169
Eric Parisb05d8442012-01-03 14:23:06 -0500170 if (!entryexit)
171 audit_syscall_entry(HOST_AUDIT_ARCH,
172 UPT_SYSCALL_NR(regs),
173 UPT_SYSCALL_ARG1(regs),
174 UPT_SYSCALL_ARG2(regs),
175 UPT_SYSCALL_ARG3(regs),
176 UPT_SYSCALL_ARG4(regs));
177 else
178 audit_syscall_exit(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Fake a debug trap */
181 if (is_singlestep)
182 send_sigtrap(current, regs, 0);
183
184 if (!test_thread_flag(TIF_SYSCALL_TRACE))
185 return;
186
187 if (!(current->ptrace & PT_PTRACED))
188 return;
189
Jeff Dikeba180fd2007-10-16 01:27:00 -0700190 /*
191 * the 0x80 provides a way for the tracing parent to distinguish
192 * between a syscall stop and SIGTRAP delivery
193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
195 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
196
197 if (entryexit) /* force do_signal() --> is_syscall() */
198 set_thread_flag(TIF_SIGPENDING);
199
Jeff Dikeba180fd2007-10-16 01:27:00 -0700200 /*
201 * this isn't the same as continuing with a signal, but it will do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * for normal use. strace only continues with a signal if the
203 * stopping signal is not SIGTRAP. -brl
204 */
205 if (current->exit_code) {
206 send_sig(current->exit_code, current, 1);
207 current->exit_code = 0;
208 }
209}