blob: 6fe7c38cd5569de6c7eb8750f1f69be59422888f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m68knommu/kernel/ptrace.c
3 *
4 * Copyright (C) 1994 by Hamish Macdonald
5 * Taken from linux/kernel/ptrace.c and modified for M680x0.
6 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
7 *
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file COPYING in the main directory of
10 * this archive for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ptrace.h>
19#include <linux/user.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Greg Ungerer55f411d2010-10-22 15:12:34 +100021#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/uaccess.h>
24#include <asm/page.h>
25#include <asm/pgtable.h>
26#include <asm/system.h>
27#include <asm/processor.h>
28
29/*
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
32 */
33
34/* determines which bits in the SR the user has access to. */
35/* 1 = access 0 = no access */
36#define SR_MASK 0x001f
37
38/* sets the trace bits. */
39#define TRACE_BITS 0x8000
40
41/* Find the stack offset for a register, relative to thread.esp0. */
42#define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
43#define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
44 - sizeof(struct switch_stack))
45/* Mapping from PT_xxx to the stack offset at which the register is
46 saved. Notice that usp has no stack-slot and needs to be treated
47 specially (see get_reg/put_reg below). */
48static int regoff[] = {
49 PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
50 PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
51 PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
52 SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
53 PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
54};
55
56/*
57 * Get contents of register REGNO in task TASK.
58 */
59static inline long get_reg(struct task_struct *task, int regno)
60{
61 unsigned long *addr;
62
63 if (regno == PT_USP)
64 addr = &task->thread.usp;
Ahmed S. Darwishbf0059b2007-02-10 01:43:46 -080065 else if (regno < ARRAY_SIZE(regoff))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
67 else
68 return 0;
69 return *addr;
70}
71
72/*
73 * Write contents of register REGNO in task TASK.
74 */
75static inline int put_reg(struct task_struct *task, int regno,
76 unsigned long data)
77{
78 unsigned long *addr;
79
80 if (regno == PT_USP)
81 addr = &task->thread.usp;
Ahmed S. Darwishbf0059b2007-02-10 01:43:46 -080082 else if (regno < ARRAY_SIZE(regoff))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
84 else
85 return -1;
86 *addr = data;
87 return 0;
88}
89
Greg Ungererf60a5572009-07-07 15:54:54 +100090void user_enable_single_step(struct task_struct *task)
91{
92 unsigned long srflags;
93 srflags = get_reg(task, PT_SR) | (TRACE_BITS << 16);
94 put_reg(task, PT_SR, srflags);
95}
96
97void user_disable_single_step(struct task_struct *task)
98{
99 unsigned long srflags;
100 srflags = get_reg(task, PT_SR) & ~(TRACE_BITS << 16);
101 put_reg(task, PT_SR, srflags);
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Called by kernel/ptrace.c when detaching..
106 *
107 * Make sure the single step bit is not set.
108 */
109void ptrace_disable(struct task_struct *child)
110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* make sure the single step bit is not set. */
Greg Ungererf60a5572009-07-07 15:54:54 +1000112 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Greg Ungererafc7cd82006-01-10 16:42:18 +1000115long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int ret;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* read the word at location addr in the USER area. */
121 case PTRACE_PEEKUSR: {
122 unsigned long tmp;
123
124 ret = -EIO;
125 if ((addr & 3) || addr < 0 ||
126 addr > sizeof(struct user) - 3)
127 break;
128
129 tmp = 0; /* Default return condition */
130 addr = addr >> 2; /* temporary hack. */
131 ret = -EIO;
132 if (addr < 19) {
133 tmp = get_reg(child, addr);
134 if (addr == PT_SR)
135 tmp >>= 16;
136 } else if (addr >= 21 && addr < 49) {
137 tmp = child->thread.fp[addr - 21];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 } else if (addr == 49) {
139 tmp = child->mm->start_code;
140 } else if (addr == 50) {
141 tmp = child->mm->start_data;
142 } else if (addr == 51) {
143 tmp = child->mm->end_code;
144 } else
145 break;
146 ret = put_user(tmp,(unsigned long *) data);
147 break;
148 }
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
151 ret = -EIO;
152 if ((addr & 3) || addr < 0 ||
153 addr > sizeof(struct user) - 3)
154 break;
155
156 addr = addr >> 2; /* temporary hack. */
157
158 if (addr == PT_SR) {
159 data &= SR_MASK;
160 data <<= 16;
161 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
162 }
163 if (addr < 19) {
164 if (put_reg(child, addr, data))
165 break;
166 ret = 0;
167 break;
168 }
169 if (addr >= 21 && addr < 48)
170 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 child->thread.fp[addr - 21] = data;
172 ret = 0;
173 }
174 break;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
177 int i;
178 unsigned long tmp;
179 for (i = 0; i < 19; i++) {
180 tmp = get_reg(child, i);
181 if (i == PT_SR)
182 tmp >>= 16;
183 if (put_user(tmp, (unsigned long *) data)) {
184 ret = -EFAULT;
185 break;
186 }
187 data += sizeof(long);
188 }
189 ret = 0;
190 break;
191 }
192
193 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
194 int i;
195 unsigned long tmp;
196 for (i = 0; i < 19; i++) {
197 if (get_user(tmp, (unsigned long *) data)) {
198 ret = -EFAULT;
199 break;
200 }
201 if (i == PT_SR) {
202 tmp &= SR_MASK;
203 tmp <<= 16;
204 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
205 }
206 put_reg(child, i, tmp);
207 data += sizeof(long);
208 }
209 ret = 0;
210 break;
211 }
212
213#ifdef PTRACE_GETFPREGS
214 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
215 ret = 0;
216 if (copy_to_user((void *)data, &child->thread.fp,
217 sizeof(struct user_m68kfp_struct)))
218 ret = -EFAULT;
219 break;
220 }
221#endif
222
223#ifdef PTRACE_SETFPREGS
224 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
225 ret = 0;
226 if (copy_from_user(&child->thread.fp, (void *)data,
227 sizeof(struct user_m68kfp_struct)))
228 ret = -EFAULT;
229 break;
230 }
231#endif
232
Maxim Kuvyrkova58f7532009-12-06 10:08:14 -0800233 case PTRACE_GET_THREAD_AREA:
234 ret = put_user(task_thread_info(child)->tp_value,
235 (unsigned long __user *)data);
236 break;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 default:
Christoph Hellwigb3c1e012010-03-10 15:22:44 -0800239 ret = ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return ret;
243}
244
Greg Ungerer55f411d2010-10-22 15:12:34 +1000245asmlinkage int syscall_trace_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Greg Ungerer55f411d2010-10-22 15:12:34 +1000247 int ret = 0;
248
249 if (test_thread_flag(TIF_SYSCALL_TRACE))
250 ret = tracehook_report_syscall_entry(task_pt_regs(current));
251 return ret;
252}
253
254asmlinkage void syscall_trace_leave(void)
255{
256 if (test_thread_flag(TIF_SYSCALL_TRACE))
257 tracehook_report_syscall_exit(task_pt_regs(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}