blob: dc03ffc8174a9ee632c892105a1331c93c02dcd8 [file] [log] [blame]
Michal Simek2b438452009-03-27 14:25:27 +01001/*
2 * `ptrace' system call
3 *
4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2007-2009 PetaLogix
6 * Copyright (C) 2004-2007 John Williams <john.williams@petalogix.com>
7 *
8 * derived from arch/v850/kernel/ptrace.c
9 *
10 * Copyright (C) 2002,03 NEC Electronics Corporation
11 * Copyright (C) 2002,03 Miles Bader <miles@gnu.org>
12 *
13 * Derived from arch/mips/kernel/ptrace.c:
14 *
15 * Copyright (C) 1992 Ross Biro
16 * Copyright (C) Linus Torvalds
17 * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
18 * Copyright (C) 1996 David S. Miller
19 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
20 * Copyright (C) 1999 MIPS Technologies, Inc.
21 *
22 * This file is subject to the terms and conditions of the GNU General
23 * Public License. See the file COPYING in the main directory of this
24 * archive for more details.
25 */
26
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/sched.h>
Michal Simek2b438452009-03-27 14:25:27 +010030#include <linux/ptrace.h>
31#include <linux/signal.h>
Michal Simek23575482009-08-24 13:26:04 +020032#include <linux/elf.h>
33#include <linux/audit.h>
34#include <linux/seccomp.h>
35#include <linux/tracehook.h>
Michal Simek2b438452009-03-27 14:25:27 +010036
37#include <linux/errno.h>
Michal Simek2b438452009-03-27 14:25:27 +010038#include <asm/processor.h>
39#include <linux/uaccess.h>
40#include <asm/asm-offsets.h>
Michal Simek6847ba92010-05-24 12:13:24 +020041#include <asm/cacheflush.h>
42#include <asm/io.h>
Michal Simek2b438452009-03-27 14:25:27 +010043
44/* Returns the address where the register at REG_OFFS in P is stashed away. */
45static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
46 struct task_struct *t)
47{
48 struct pt_regs *regs;
49
50 /*
51 * Three basic cases:
52 *
53 * (1) A register normally saved before calling the scheduler, is
54 * available in the kernel entry pt_regs structure at the top
55 * of the kernel stack. The kernel trap/irq exit path takes
56 * care to save/restore almost all registers for ptrace'd
57 * processes.
58 *
59 * (2) A call-clobbered register, where the process P entered the
60 * kernel via [syscall] trap, is not stored anywhere; that's
61 * OK, because such registers are not expected to be preserved
62 * when the trap returns anyway (so we don't actually bother to
63 * test for this case).
64 *
65 * (3) A few registers not used at all by the kernel, and so
66 * normally never saved except by context-switches, are in the
67 * context switch state.
68 */
69
70 /* Register saved during kernel entry (or not available). */
71 regs = task_pt_regs(t);
72
73 return (microblaze_reg_t *)((char *)regs + reg_offs);
74}
75
76long arch_ptrace(struct task_struct *child, long request, long addr, long data)
77{
78 int rval;
79 unsigned long val = 0;
Michal Simek2b438452009-03-27 14:25:27 +010080
81 switch (request) {
Michal Simek2b438452009-03-27 14:25:27 +010082 /* Read/write the word at location ADDR in the registers. */
83 case PTRACE_PEEKUSR:
84 case PTRACE_POKEUSR:
85 pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
86 rval = 0;
87 if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
88 /*
89 * Special requests that don't actually correspond
90 * to offsets in struct pt_regs.
91 */
92 if (addr == PT_TEXT_ADDR) {
93 val = child->mm->start_code;
94 } else if (addr == PT_DATA_ADDR) {
95 val = child->mm->start_data;
96 } else if (addr == PT_TEXT_LEN) {
97 val = child->mm->end_code
98 - child->mm->start_code;
99 } else {
100 rval = -EIO;
101 }
102 } else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) {
103 microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
104 if (request == PTRACE_PEEKUSR)
105 val = *reg_addr;
Michal Simek6847ba92010-05-24 12:13:24 +0200106 else {
107#if 1
Michal Simek2b438452009-03-27 14:25:27 +0100108 *reg_addr = data;
Michal Simek6847ba92010-05-24 12:13:24 +0200109#else
110 /* MS potential problem on WB system
111 * Be aware that reg_addr is virtual address
112 * virt_to_phys conversion is necessary.
113 * This could be sensible solution.
114 */
115 u32 paddr = virt_to_phys((u32)reg_addr);
116 invalidate_icache_range(paddr, paddr + 4);
117 *reg_addr = data;
118 flush_dcache_range(paddr, paddr + 4);
119#endif
120 }
Michal Simek2b438452009-03-27 14:25:27 +0100121 } else
122 rval = -EIO;
123
124 if (rval == 0 && request == PTRACE_PEEKUSR)
125 rval = put_user(val, (unsigned long *)data);
126 break;
Michal Simek2b438452009-03-27 14:25:27 +0100127 default:
Christoph Hellwigb3c1e012010-03-10 15:22:44 -0800128 rval = ptrace_request(child, request, addr, data);
Michal Simek2b438452009-03-27 14:25:27 +0100129 }
130 return rval;
131}
132
Michal Simek23575482009-08-24 13:26:04 +0200133asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
134{
135 long ret = 0;
136
137 secure_computing(regs->r12);
138
139 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
140 tracehook_report_syscall_entry(regs))
141 /*
142 * Tracing decided this syscall should not happen.
143 * We'll return a bogus call number to get an ENOSYS
144 * error, but leave the original number in regs->regs[0].
145 */
146 ret = -1L;
147
148 if (unlikely(current->audit_context))
149 audit_syscall_entry(EM_XILINX_MICROBLAZE, regs->r12,
150 regs->r5, regs->r6,
151 regs->r7, regs->r8);
152
153 return ret ?: regs->r12;
154}
155
156asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
157{
158 int step;
159
160 if (unlikely(current->audit_context))
161 audit_syscall_exit(AUDITSC_RESULT(regs->r3), regs->r3);
162
163 step = test_thread_flag(TIF_SINGLESTEP);
164 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
165 tracehook_report_syscall_exit(regs, step);
166}
167
168#if 0
169static asmlinkage void syscall_trace(void)
170{
171 if (!test_thread_flag(TIF_SYSCALL_TRACE))
172 return;
173 if (!(current->ptrace & PT_PTRACED))
174 return;
175 /* The 0x80 provides a way for the tracing parent to distinguish
176 between a syscall stop and SIGTRAP delivery */
177 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
178 ? 0x80 : 0));
179 /*
180 * this isn't the same as continuing with a signal, but it will do
181 * for normal use. strace only continues with a signal if the
182 * stopping signal is not SIGTRAP. -brl
183 */
184 if (current->exit_code) {
185 send_sig(current->exit_code, current, 1);
186 current->exit_code = 0;
187 }
188}
189#endif
190
Michal Simek2b438452009-03-27 14:25:27 +0100191void ptrace_disable(struct task_struct *child)
192{
193 /* nothing to do */
194}