blob: 694d551c88996dbee7c974487b9299c9f1efc8fe [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
Al Viro1bfa2312012-05-23 00:18:33 -04006#include <linux/audit.h>
7#include <linux/ptrace.h>
8#include <linux/sched.h>
9#include <linux/tracehook.h>
10#include <asm/uaccess.h>
11#include <skas_ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Christoph Hellwig1bd09502010-03-10 15:22:56 -080013
14
15void user_enable_single_step(struct task_struct *child)
Bodo Stroesser82c1c112005-05-06 21:30:46 -070016{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080017 child->ptrace |= PT_DTRACE;
Jeff Dikeba180fd2007-10-16 01:27:00 -070018 child->thread.singlestep_syscall = 0;
Bodo Stroesser82c1c112005-05-06 21:30:46 -070019
20#ifdef SUBARCH_SET_SINGLESTEPPING
Christoph Hellwig1bd09502010-03-10 15:22:56 -080021 SUBARCH_SET_SINGLESTEPPING(child, 1);
22#endif
23}
24
25void user_disable_single_step(struct task_struct *child)
26{
27 child->ptrace &= ~PT_DTRACE;
28 child->thread.singlestep_syscall = 0;
29
30#ifdef SUBARCH_SET_SINGLESTEPPING
31 SUBARCH_SET_SINGLESTEPPING(child, 0);
Bodo Stroesser82c1c112005-05-06 21:30:46 -070032#endif
Jeff Dikeba9950c2005-05-20 13:59:07 -070033}
Bodo Stroesser82c1c112005-05-06 21:30:46 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * Called by kernel/ptrace.c when detaching..
37 */
38void ptrace_disable(struct task_struct *child)
Jeff Dikeba180fd2007-10-16 01:27:00 -070039{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080040 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Bodo Stroesser82c1c112005-05-06 21:30:46 -070043extern int peek_user(struct task_struct * child, long addr, long data);
44extern int poke_user(struct task_struct * child, long addr, long data);
45
Namhyung Kim9b05a692010-10-27 15:33:47 -070046long arch_ptrace(struct task_struct *child, long request,
47 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int i, ret;
Namhyung Kim9b05a692010-10-27 15:33:47 -070050 unsigned long __user *p = (void __user *)data;
Namhyung Kim0a3d7632010-10-27 15:34:04 -070051 void __user *vp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /* read the word at location addr in the USER area. */
Jeff Dikeba180fd2007-10-16 01:27:00 -070055 case PTRACE_PEEKUSR:
56 ret = peek_user(child, addr, data);
57 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jeff Dikeba180fd2007-10-16 01:27:00 -070059 /* write the word at location addr in the USER area */
60 case PTRACE_POKEUSR:
61 ret = poke_user(child, addr, data);
62 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Renzo Davoli86d6f2b2009-03-12 14:31:23 -070064 case PTRACE_SYSEMU:
65 case PTRACE_SYSEMU_SINGLESTEP:
66 ret = -EIO;
67 break;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#ifdef PTRACE_GETREGS
70 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -080071 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 ret = -EIO;
73 break;
74 }
75 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080076 __put_user(getreg(child, i), p);
77 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79 ret = 0;
80 break;
81 }
82#endif
83#ifdef PTRACE_SETREGS
84 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
85 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -080086 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ret = -EIO;
88 break;
89 }
90 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080091 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -080093 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 ret = 0;
96 break;
97 }
98#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -080099 case PTRACE_GET_THREAD_AREA:
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700100 ret = ptrace_get_thread_area(child, addr, vp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800101 break;
102
103 case PTRACE_SET_THREAD_AREA:
Richard Weinberger8818b672010-11-11 14:05:04 -0800104 ret = ptrace_set_thread_area(child, addr, vp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800105 break;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 case PTRACE_FAULTINFO: {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700108 /*
109 * Take the info from thread->arch->faultinfo,
Al Viro4d338e12006-03-31 02:30:15 -0800110 * but transfer max. sizeof(struct ptrace_faultinfo).
111 * On i386, ptrace_faultinfo is smaller!
112 */
113 ret = copy_to_user(p, &child->thread.arch.faultinfo,
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700114 sizeof(struct ptrace_faultinfo)) ?
115 -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Bodo Stroesserc5784552005-05-05 16:15:31 -0700119#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 case PTRACE_LDT: {
121 struct ptrace_ldt ldt;
122
Jeff Dikeba180fd2007-10-16 01:27:00 -0700123 if (copy_from_user(&ldt, p, sizeof(ldt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ret = -EIO;
125 break;
126 }
127
Jeff Dikeba180fd2007-10-16 01:27:00 -0700128 /*
129 * This one is confusing, so just punt and return -EIO for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * now
131 */
132 ret = -EIO;
133 break;
134 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 default:
137 ret = ptrace_request(child, request, addr, data);
Jeff Dikee8012b52007-10-16 01:27:16 -0700138 if (ret == -EIO)
139 ret = subarch_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
141 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return ret;
144}
145
WANG Cong99764fa2008-07-23 21:28:49 -0700146static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int error_code)
148{
149 struct siginfo info;
150
151 memset(&info, 0, sizeof(info));
152 info.si_signo = SIGTRAP;
153 info.si_code = TRAP_BRKPT;
154
155 /* User-mode eip? */
156 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
157
Simon Arlottb60745b2007-10-20 01:23:03 +0200158 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 force_sig_info(SIGTRAP, &info, tsk);
160}
161
Jeff Dikeba180fd2007-10-16 01:27:00 -0700162/*
163 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
165 */
Al Viro1bfa2312012-05-23 00:18:33 -0400166void syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Al Viro1bfa2312012-05-23 00:18:33 -0400168 audit_syscall_entry(HOST_AUDIT_ARCH,
169 UPT_SYSCALL_NR(&regs->regs),
170 UPT_SYSCALL_ARG1(&regs->regs),
171 UPT_SYSCALL_ARG2(&regs->regs),
172 UPT_SYSCALL_ARG3(&regs->regs),
173 UPT_SYSCALL_ARG4(&regs->regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!test_thread_flag(TIF_SYSCALL_TRACE))
176 return;
177
Al Viro1bfa2312012-05-23 00:18:33 -0400178 tracehook_report_syscall_entry(regs);
179}
180
181void syscall_trace_leave(struct pt_regs *regs)
182{
183 int ptraced = current->ptrace;
184
185 audit_syscall_exit(regs);
186
187 /* Fake a debug trap */
188 if (ptraced & PT_DTRACE)
189 send_sigtrap(current, &regs->regs, 0);
190
191 if (!test_thread_flag(TIF_SYSCALL_TRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return;
193
Al Viro1bfa2312012-05-23 00:18:33 -0400194 tracehook_report_syscall_exit(regs, 0);
195 /* force do_signal() --> is_syscall() */
196 if (ptraced & PT_PTRACED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}