Sync c-tx39.c with c-r4k.c.
[deliverable/linux.git] / arch / mips / kernel / ptrace.c
CommitLineData
1da177e4
LT
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1992 Ross Biro
7 * Copyright (C) Linus Torvalds
8 * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
9 * Copyright (C) 1996 David S. Miller
10 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11 * Copyright (C) 1999 MIPS Technologies, Inc.
12 * Copyright (C) 2000 Ulf Carlsson
13 *
14 * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
15 * binaries.
16 */
17#include <linux/config.h>
18#include <linux/compiler.h>
19#include <linux/kernel.h>
20#include <linux/sched.h>
21#include <linux/mm.h>
22#include <linux/errno.h>
23#include <linux/ptrace.h>
24#include <linux/audit.h>
25#include <linux/smp.h>
26#include <linux/smp_lock.h>
27#include <linux/user.h>
28#include <linux/security.h>
7ed20e1a 29#include <linux/signal.h>
1da177e4 30
f8280c8d 31#include <asm/byteorder.h>
1da177e4 32#include <asm/cpu.h>
e50c0a8f 33#include <asm/dsp.h>
1da177e4
LT
34#include <asm/fpu.h>
35#include <asm/mipsregs.h>
36#include <asm/pgtable.h>
37#include <asm/page.h>
38#include <asm/system.h>
39#include <asm/uaccess.h>
40#include <asm/bootinfo.h>
41
42/*
43 * Called by kernel/ptrace.c when detaching..
44 *
45 * Make sure single step bits etc are not set.
46 */
47void ptrace_disable(struct task_struct *child)
48{
49 /* Nothing to do.. */
50}
51
52asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
53{
54 struct task_struct *child;
55 int ret;
56
57#if 0
58 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
59 (int) request, (int) pid, (unsigned long) addr,
60 (unsigned long) data);
61#endif
62 lock_kernel();
63 ret = -EPERM;
64 if (request == PTRACE_TRACEME) {
65 /* are we already being traced? */
66 if (current->ptrace & PT_PTRACED)
67 goto out;
68 if ((ret = security_ptrace(current->parent, current)))
69 goto out;
70 /* set the ptrace bit in the process flags. */
71 current->ptrace |= PT_PTRACED;
72 ret = 0;
73 goto out;
74 }
75 ret = -ESRCH;
76 read_lock(&tasklist_lock);
77 child = find_task_by_pid(pid);
78 if (child)
79 get_task_struct(child);
80 read_unlock(&tasklist_lock);
81 if (!child)
82 goto out;
83
84 ret = -EPERM;
85 if (pid == 1) /* you may not mess with init */
86 goto out_tsk;
87
88 if (request == PTRACE_ATTACH) {
89 ret = ptrace_attach(child);
90 goto out_tsk;
91 }
92
93 ret = ptrace_check_attach(child, request == PTRACE_KILL);
94 if (ret < 0)
95 goto out_tsk;
96
97 switch (request) {
98 /* when I and D space are separate, these will need to be fixed. */
99 case PTRACE_PEEKTEXT: /* read word at location addr. */
100 case PTRACE_PEEKDATA: {
101 unsigned long tmp;
102 int copied;
103
104 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
105 ret = -EIO;
106 if (copied != sizeof(tmp))
107 break;
fe00f943 108 ret = put_user(tmp,(unsigned long __user *) data);
1da177e4
LT
109 break;
110 }
111
112 /* Read the word at location addr in the USER area. */
113 case PTRACE_PEEKUSR: {
114 struct pt_regs *regs;
115 unsigned long tmp = 0;
116
117 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
118 THREAD_SIZE - 32 - sizeof(struct pt_regs));
119 ret = 0; /* Default return value. */
120
121 switch (addr) {
122 case 0 ... 31:
123 tmp = regs->regs[addr];
124 break;
125 case FPR_BASE ... FPR_BASE + 31:
126 if (tsk_used_math(child)) {
127 fpureg_t *fregs = get_fpu_regs(child);
128
875d43e7 129#ifdef CONFIG_32BIT
1da177e4
LT
130 /*
131 * The odd registers are actually the high
132 * order bits of the values stored in the even
133 * registers - unless we're using r2k_switch.S.
134 */
135 if (addr & 1)
136 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
137 else
138 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
139#endif
875d43e7 140#ifdef CONFIG_64BIT
1da177e4
LT
141 tmp = fregs[addr - FPR_BASE];
142#endif
143 } else {
144 tmp = -1; /* FP not yet used */
145 }
146 break;
147 case PC:
148 tmp = regs->cp0_epc;
149 break;
150 case CAUSE:
151 tmp = regs->cp0_cause;
152 break;
153 case BADVADDR:
154 tmp = regs->cp0_badvaddr;
155 break;
156 case MMHI:
157 tmp = regs->hi;
158 break;
159 case MMLO:
160 tmp = regs->lo;
161 break;
162 case FPC_CSR:
163 if (cpu_has_fpu)
164 tmp = child->thread.fpu.hard.fcr31;
165 else
166 tmp = child->thread.fpu.soft.fcr31;
167 break;
168 case FPC_EIR: { /* implementation / version register */
169 unsigned int flags;
170
171 if (!cpu_has_fpu)
172 break;
173
174 flags = read_c0_status();
175 __enable_fpu();
176 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
177 write_c0_status(flags);
178 break;
179 }
c134a5ec
RB
180 case DSP_BASE ... DSP_BASE + 5: {
181 dspreg_t *dregs;
182
e50c0a8f
RB
183 if (!cpu_has_dsp) {
184 tmp = 0;
185 ret = -EIO;
186 goto out_tsk;
187 }
188 if (child->thread.dsp.used_dsp) {
c134a5ec 189 dregs = __get_dsp_regs(child);
e50c0a8f
RB
190 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
191 } else {
192 tmp = -1; /* DSP registers yet used */
193 }
194 break;
c134a5ec 195 }
e50c0a8f
RB
196 case DSP_CONTROL:
197 if (!cpu_has_dsp) {
198 tmp = 0;
199 ret = -EIO;
200 goto out_tsk;
201 }
202 tmp = child->thread.dsp.dspcontrol;
203 break;
1da177e4
LT
204 default:
205 tmp = 0;
206 ret = -EIO;
207 goto out_tsk;
208 }
fe00f943 209 ret = put_user(tmp, (unsigned long __user *) data);
1da177e4
LT
210 break;
211 }
212
213 /* when I and D space are separate, this will have to be fixed. */
214 case PTRACE_POKETEXT: /* write the word at location addr. */
215 case PTRACE_POKEDATA:
216 ret = 0;
217 if (access_process_vm(child, addr, &data, sizeof(data), 1)
218 == sizeof(data))
219 break;
220 ret = -EIO;
221 break;
222
223 case PTRACE_POKEUSR: {
224 struct pt_regs *regs;
225 ret = 0;
226 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
227 THREAD_SIZE - 32 - sizeof(struct pt_regs));
228
229 switch (addr) {
230 case 0 ... 31:
231 regs->regs[addr] = data;
232 break;
233 case FPR_BASE ... FPR_BASE + 31: {
234 fpureg_t *fregs = get_fpu_regs(child);
235
236 if (!tsk_used_math(child)) {
237 /* FP not yet used */
238 memset(&child->thread.fpu.hard, ~0,
239 sizeof(child->thread.fpu.hard));
240 child->thread.fpu.hard.fcr31 = 0;
241 }
875d43e7 242#ifdef CONFIG_32BIT
1da177e4
LT
243 /*
244 * The odd registers are actually the high order bits
245 * of the values stored in the even registers - unless
246 * we're using r2k_switch.S.
247 */
248 if (addr & 1) {
249 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
250 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
251 } else {
252 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
253 fregs[addr - FPR_BASE] |= data;
254 }
255#endif
875d43e7 256#ifdef CONFIG_64BIT
1da177e4
LT
257 fregs[addr - FPR_BASE] = data;
258#endif
259 break;
260 }
261 case PC:
262 regs->cp0_epc = data;
263 break;
264 case MMHI:
265 regs->hi = data;
266 break;
267 case MMLO:
268 regs->lo = data;
269 break;
270 case FPC_CSR:
271 if (cpu_has_fpu)
272 child->thread.fpu.hard.fcr31 = data;
273 else
274 child->thread.fpu.soft.fcr31 = data;
275 break;
c134a5ec
RB
276 case DSP_BASE ... DSP_BASE + 5: {
277 dspreg_t *dregs;
278
e50c0a8f
RB
279 if (!cpu_has_dsp) {
280 ret = -EIO;
281 break;
282 }
283
c134a5ec 284 dregs = __get_dsp_regs(child);
e50c0a8f
RB
285 dregs[addr - DSP_BASE] = data;
286 break;
c134a5ec 287 }
e50c0a8f
RB
288 case DSP_CONTROL:
289 if (!cpu_has_dsp) {
290 ret = -EIO;
291 break;
292 }
293 child->thread.dsp.dspcontrol = data;
294 break;
1da177e4
LT
295 default:
296 /* The rest are not allowed. */
297 ret = -EIO;
298 break;
299 }
300 break;
301 }
302
303 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
304 case PTRACE_CONT: { /* restart after signal. */
305 ret = -EIO;
7ed20e1a 306 if (!valid_signal(data))
1da177e4
LT
307 break;
308 if (request == PTRACE_SYSCALL) {
309 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
310 }
311 else {
312 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
313 }
314 child->exit_code = data;
315 wake_up_process(child);
316 ret = 0;
317 break;
318 }
319
320 /*
321 * make the child exit. Best I can do is send it a sigkill.
322 * perhaps it should be put in the status that it wants to
323 * exit.
324 */
325 case PTRACE_KILL:
326 ret = 0;
327 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
328 break;
329 child->exit_code = SIGKILL;
330 wake_up_process(child);
331 break;
332
333 case PTRACE_DETACH: /* detach a process that was attached. */
334 ret = ptrace_detach(child, data);
335 break;
336
3c37026d
RB
337 case PTRACE_GET_THREAD_AREA:
338 ret = put_user(child->thread_info->tp_value,
339 (unsigned long __user *) data);
340 break;
341
1da177e4
LT
342 default:
343 ret = ptrace_request(child, request, addr, data);
344 break;
345 }
346
347out_tsk:
348 put_task_struct(child);
349out:
350 unlock_kernel();
351 return ret;
352}
353
67eb81e1 354static inline int audit_arch(void)
2fd6f58b 355{
f8280c8d 356 int arch = EM_MIPS;
875d43e7 357#ifdef CONFIG_64BIT
f8280c8d
RB
358 arch |= __AUDIT_ARCH_64BIT;
359#endif
360#if defined(__LITTLE_ENDIAN)
361 arch |= __AUDIT_ARCH_LE;
362#endif
363 return arch;
2fd6f58b 364}
365
1da177e4
LT
366/*
367 * Notification of system call entry/exit
368 * - triggered by current->work.syscall_trace
369 */
370asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
371{
2fd6f58b 372 if (unlikely(current->audit_context) && entryexit)
f8280c8d
RB
373 audit_syscall_exit(current, AUDITSC_RESULT(regs->regs[2]),
374 regs->regs[2]);
1da177e4 375
1da177e4 376 if (!(current->ptrace & PT_PTRACED))
2fd6f58b 377 goto out;
f8280c8d
RB
378 if (!test_thread_flag(TIF_SYSCALL_TRACE))
379 goto out;
1da177e4
LT
380
381 /* The 0x80 provides a way for the tracing parent to distinguish
382 between a syscall stop and SIGTRAP delivery */
383 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
384 0x80 : 0));
385
386 /*
387 * this isn't the same as continuing with a signal, but it will do
388 * for normal use. strace only continues with a signal if the
389 * stopping signal is not SIGTRAP. -brl
390 */
391 if (current->exit_code) {
392 send_sig(current->exit_code, current, 1);
393 current->exit_code = 0;
394 }
2fd6f58b 395 out:
396 if (unlikely(current->audit_context) && !entryexit)
397 audit_syscall_entry(current, audit_arch(), regs->regs[2],
398 regs->regs[4], regs->regs[5],
399 regs->regs[6], regs->regs[7]);
1da177e4 400}
This page took 0.078587 seconds and 5 git commands to generate.