blob: e5701d1a52d78ddec3c2f45635834155d235ecb7 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * Copied from i386: Ross Biro 1/23/92
15 */
16
17#include <linux/kernel.h>
18#include <linux/ptrace.h>
19#include <linux/kprobes.h>
20#include <linux/compat.h>
21#include <linux/uaccess.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040022#include <asm/traps.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040023
24void user_enable_single_step(struct task_struct *child)
25{
26 set_tsk_thread_flag(child, TIF_SINGLESTEP);
27}
28
29void user_disable_single_step(struct task_struct *child)
30{
31 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
32}
33
34/*
35 * This routine will put a word on the process's privileged stack.
36 */
37static void putreg(struct task_struct *task,
38 unsigned long addr, unsigned long value)
39{
40 unsigned int regno = addr / sizeof(unsigned long);
41 struct pt_regs *childregs = task_pt_regs(task);
42 childregs->regs[regno] = value;
43 childregs->flags |= PT_FLAGS_RESTORE_REGS;
44}
45
46static unsigned long getreg(struct task_struct *task, unsigned long addr)
47{
48 unsigned int regno = addr / sizeof(unsigned long);
49 struct pt_regs *childregs = task_pt_regs(task);
50 return childregs->regs[regno];
51}
52
53/*
54 * Called by kernel/ptrace.c when detaching..
55 */
56void ptrace_disable(struct task_struct *child)
57{
58 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
59
60 /*
61 * These two are currently unused, but will be set by arch_ptrace()
62 * and used in the syscall assembly when we do support them.
63 */
64 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
65}
66
67long arch_ptrace(struct task_struct *child, long request, long addr, long data)
68{
69 unsigned long __user *datap;
70 unsigned long tmp;
71 int i;
72 long ret = -EIO;
73
74#ifdef CONFIG_COMPAT
75 if (task_thread_info(current)->status & TS_COMPAT)
76 data = (u32)data;
77 if (task_thread_info(child)->status & TS_COMPAT)
78 addr = (u32)addr;
79#endif
Chris Metcalf0707ad32010-06-25 17:04:17 -040080 datap = (unsigned long __user __force *)data;
Chris Metcalf867e3592010-05-28 23:09:12 -040081
82 switch (request) {
83
84 case PTRACE_PEEKUSR: /* Read register from pt_regs. */
85 if (addr & (sizeof(data)-1))
86 break;
87 if (addr < 0 || addr >= PTREGS_SIZE)
88 break;
89 tmp = getreg(child, addr); /* Read register */
90 ret = put_user(tmp, datap);
91 break;
92
93 case PTRACE_POKEUSR: /* Write register in pt_regs. */
94 if (addr & (sizeof(data)-1))
95 break;
96 if (addr < 0 || addr >= PTREGS_SIZE)
97 break;
98 putreg(child, addr, data); /* Write register */
99 break;
100
101 case PTRACE_GETREGS: /* Get all registers from the child. */
102 if (!access_ok(VERIFY_WRITE, datap, PTREGS_SIZE))
103 break;
104 for (i = 0; i < PTREGS_SIZE; i += sizeof(long)) {
105 ret = __put_user(getreg(child, i), datap);
106 if (ret != 0)
107 break;
108 datap++;
109 }
110 break;
111
112 case PTRACE_SETREGS: /* Set all registers in the child. */
113 if (!access_ok(VERIFY_READ, datap, PTREGS_SIZE))
114 break;
115 for (i = 0; i < PTREGS_SIZE; i += sizeof(long)) {
116 ret = __get_user(tmp, datap);
117 if (ret != 0)
118 break;
119 putreg(child, i, tmp);
120 datap++;
121 }
122 break;
123
124 case PTRACE_GETFPREGS: /* Get the child FPU state. */
125 case PTRACE_SETFPREGS: /* Set the child FPU state. */
126 break;
127
128 case PTRACE_SETOPTIONS:
129 /* Support TILE-specific ptrace options. */
130 child->ptrace &= ~PT_TRACE_MASK_TILE;
131 tmp = data & PTRACE_O_MASK_TILE;
132 data &= ~PTRACE_O_MASK_TILE;
133 ret = ptrace_request(child, request, addr, data);
134 if (tmp & PTRACE_O_TRACEMIGRATE)
135 child->ptrace |= PT_TRACE_MIGRATE;
136 break;
137
138 default:
139#ifdef CONFIG_COMPAT
140 if (task_thread_info(current)->status & TS_COMPAT) {
141 ret = compat_ptrace_request(child, request,
142 addr, data);
143 break;
144 }
145#endif
146 ret = ptrace_request(child, request, addr, data);
147 break;
148 }
149
150 return ret;
151}
152
153#ifdef CONFIG_COMPAT
154/* Not used; we handle compat issues in arch_ptrace() directly. */
155long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
156 compat_ulong_t addr, compat_ulong_t data)
157{
158 BUG();
159}
160#endif
161
162void do_syscall_trace(void)
163{
164 if (!test_thread_flag(TIF_SYSCALL_TRACE))
165 return;
166
167 if (!(current->ptrace & PT_PTRACED))
168 return;
169
170 /*
171 * The 0x80 provides a way for the tracing parent to distinguish
172 * between a syscall stop and SIGTRAP delivery
173 */
174 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
175
176 /*
177 * this isn't the same as continuing with a signal, but it will do
178 * for normal use. strace only continues with a signal if the
179 * stopping signal is not SIGTRAP. -brl
180 */
181 if (current->exit_code) {
182 send_sig(current->exit_code, current, 1);
183 current->exit_code = 0;
184 }
185}
186
187void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
188{
189 struct siginfo info;
190
191 memset(&info, 0, sizeof(info));
192 info.si_signo = SIGTRAP;
193 info.si_code = TRAP_BRKPT;
194 info.si_addr = (void __user *) regs->pc;
195
196 /* Send us the fakey SIGTRAP */
197 force_sig_info(SIGTRAP, &info, tsk);
198}
199
200/* Handle synthetic interrupt delivered only by the simulator. */
201void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num)
202{
203 send_sigtrap(current, regs, fault_num);
204}