Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/arch/m68k/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> | |
1da177e4 LT |
17 | #include <linux/errno.h> |
18 | #include <linux/ptrace.h> | |
19 | #include <linux/user.h> | |
7ed20e1a | 20 | #include <linux/signal.h> |
1da177e4 LT |
21 | |
22 | #include <asm/uaccess.h> | |
23 | #include <asm/page.h> | |
24 | #include <asm/pgtable.h> | |
25 | #include <asm/system.h> | |
26 | #include <asm/processor.h> | |
27 | ||
28 | /* | |
29 | * does not yet catch signals sent when the child dies. | |
30 | * in exit.c or in signal.c. | |
31 | */ | |
32 | ||
33 | /* determines which bits in the SR the user has access to. */ | |
34 | /* 1 = access 0 = no access */ | |
35 | #define SR_MASK 0x001f | |
36 | ||
37 | /* sets the trace bits. */ | |
38 | #define TRACE_BITS 0x8000 | |
39 | ||
40 | /* Find the stack offset for a register, relative to thread.esp0. */ | |
41 | #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg) | |
42 | #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \ | |
43 | - sizeof(struct switch_stack)) | |
44 | /* Mapping from PT_xxx to the stack offset at which the register is | |
45 | saved. Notice that usp has no stack-slot and needs to be treated | |
46 | specially (see get_reg/put_reg below). */ | |
47 | static int regoff[] = { | |
48 | [0] = PT_REG(d1), | |
49 | [1] = PT_REG(d2), | |
50 | [2] = PT_REG(d3), | |
51 | [3] = PT_REG(d4), | |
52 | [4] = PT_REG(d5), | |
53 | [5] = SW_REG(d6), | |
54 | [6] = SW_REG(d7), | |
55 | [7] = PT_REG(a0), | |
56 | [8] = PT_REG(a1), | |
57 | [9] = PT_REG(a2), | |
58 | [10] = SW_REG(a3), | |
59 | [11] = SW_REG(a4), | |
60 | [12] = SW_REG(a5), | |
61 | [13] = SW_REG(a6), | |
62 | [14] = PT_REG(d0), | |
63 | [15] = -1, | |
64 | [16] = PT_REG(orig_d0), | |
65 | [17] = PT_REG(sr), | |
66 | [18] = PT_REG(pc), | |
67 | }; | |
68 | ||
69 | /* | |
70 | * Get contents of register REGNO in task TASK. | |
71 | */ | |
72 | static inline long get_reg(struct task_struct *task, int regno) | |
73 | { | |
74 | unsigned long *addr; | |
75 | ||
76 | if (regno == PT_USP) | |
77 | addr = &task->thread.usp; | |
ea5e1a82 | 78 | else if (regno < ARRAY_SIZE(regoff)) |
1da177e4 LT |
79 | addr = (unsigned long *)(task->thread.esp0 + regoff[regno]); |
80 | else | |
81 | return 0; | |
82 | return *addr; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Write contents of register REGNO in task TASK. | |
87 | */ | |
88 | static inline int put_reg(struct task_struct *task, int regno, | |
89 | unsigned long data) | |
90 | { | |
91 | unsigned long *addr; | |
92 | ||
93 | if (regno == PT_USP) | |
94 | addr = &task->thread.usp; | |
ea5e1a82 | 95 | else if (regno < ARRAY_SIZE(regoff)) |
b3319f50 | 96 | addr = (unsigned long *)(task->thread.esp0 + regoff[regno]); |
1da177e4 LT |
97 | else |
98 | return -1; | |
99 | *addr = data; | |
100 | return 0; | |
101 | } | |
102 | ||
103 | /* | |
1da177e4 LT |
104 | * Make sure the single step bit is not set. |
105 | */ | |
69f447cf | 106 | static inline void singlestep_disable(struct task_struct *child) |
1da177e4 | 107 | { |
69f447cf | 108 | unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16); |
1da177e4 | 109 | put_reg(child, PT_SR, tmp); |
3b66a1ed | 110 | clear_tsk_thread_flag(child, TIF_DELAYED_TRACE); |
69f447cf RZ |
111 | } |
112 | ||
113 | /* | |
114 | * Called by kernel/ptrace.c when detaching.. | |
115 | */ | |
116 | void ptrace_disable(struct task_struct *child) | |
117 | { | |
118 | singlestep_disable(child); | |
3b66a1ed | 119 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
1da177e4 LT |
120 | } |
121 | ||
481bed45 | 122 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
1da177e4 | 123 | { |
69f447cf RZ |
124 | unsigned long tmp; |
125 | int i, ret = 0; | |
1da177e4 | 126 | |
1da177e4 LT |
127 | switch (request) { |
128 | /* when I and D space are separate, these will need to be fixed. */ | |
b3319f50 | 129 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
69f447cf | 130 | case PTRACE_PEEKDATA: |
76647323 | 131 | ret = generic_ptrace_peekdata(child, addr, data); |
b3319f50 | 132 | break; |
1da177e4 LT |
133 | |
134 | /* read the word at location addr in the USER area. */ | |
69f447cf RZ |
135 | case PTRACE_PEEKUSR: |
136 | if (addr & 3) | |
137 | goto out_eio; | |
138 | addr >>= 2; /* temporary hack. */ | |
1da177e4 | 139 | |
69f447cf | 140 | if (addr >= 0 && addr < 19) { |
b3319f50 RZ |
141 | tmp = get_reg(child, addr); |
142 | if (addr == PT_SR) | |
143 | tmp >>= 16; | |
144 | } else if (addr >= 21 && addr < 49) { | |
145 | tmp = child->thread.fp[addr - 21]; | |
b3319f50 RZ |
146 | /* Convert internal fpu reg representation |
147 | * into long double format | |
148 | */ | |
149 | if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) | |
150 | tmp = ((tmp & 0xffff0000) << 15) | | |
151 | ((tmp & 0x0000ffff) << 16); | |
b3319f50 | 152 | } else |
1da177e4 | 153 | break; |
b3319f50 RZ |
154 | ret = put_user(tmp, (unsigned long *)data); |
155 | break; | |
1da177e4 | 156 | |
b3319f50 RZ |
157 | /* when I and D space are separate, this will have to be fixed. */ |
158 | case PTRACE_POKETEXT: /* write the word at location addr. */ | |
159 | case PTRACE_POKEDATA: | |
f284ce72 | 160 | ret = generic_ptrace_pokedata(child, addr, data); |
b3319f50 | 161 | break; |
1da177e4 | 162 | |
b3319f50 | 163 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
69f447cf RZ |
164 | if (addr & 3) |
165 | goto out_eio; | |
166 | addr >>= 2; /* temporary hack. */ | |
1da177e4 | 167 | |
b3319f50 RZ |
168 | if (addr == PT_SR) { |
169 | data &= SR_MASK; | |
170 | data <<= 16; | |
171 | data |= get_reg(child, PT_SR) & ~(SR_MASK << 16); | |
69f447cf | 172 | } else if (addr >= 0 && addr < 19) { |
b3319f50 | 173 | if (put_reg(child, addr, data)) |
69f447cf RZ |
174 | goto out_eio; |
175 | } else if (addr >= 21 && addr < 48) { | |
b3319f50 RZ |
176 | /* Convert long double format |
177 | * into internal fpu reg representation | |
178 | */ | |
179 | if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) { | |
180 | data = (unsigned long)data << 15; | |
181 | data = (data & 0xffff0000) | | |
182 | ((data & 0x0000ffff) >> 1); | |
183 | } | |
b3319f50 | 184 | child->thread.fp[addr - 21] = data; |
69f447cf RZ |
185 | } else |
186 | goto out_eio; | |
b3319f50 | 187 | break; |
1da177e4 | 188 | |
b3319f50 | 189 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
69f447cf | 190 | case PTRACE_CONT: /* restart after signal. */ |
b3319f50 | 191 | if (!valid_signal(data)) |
69f447cf RZ |
192 | goto out_eio; |
193 | ||
194 | if (request == PTRACE_SYSCALL) | |
3b66a1ed | 195 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
69f447cf | 196 | else |
3b66a1ed | 197 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
b3319f50 | 198 | child->exit_code = data; |
69f447cf | 199 | singlestep_disable(child); |
b3319f50 | 200 | wake_up_process(child); |
b3319f50 | 201 | break; |
1da177e4 | 202 | |
b3319f50 RZ |
203 | /* |
204 | * make the child exit. Best I can do is send it a sigkill. | |
205 | * perhaps it should be put in the status that it wants to | |
206 | * exit. | |
207 | */ | |
69f447cf | 208 | case PTRACE_KILL: |
b3319f50 | 209 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
1da177e4 | 210 | break; |
b3319f50 | 211 | child->exit_code = SIGKILL; |
69f447cf | 212 | singlestep_disable(child); |
b3319f50 RZ |
213 | wake_up_process(child); |
214 | break; | |
1da177e4 | 215 | |
69f447cf | 216 | case PTRACE_SINGLESTEP: /* set the trap flag. */ |
b3319f50 | 217 | if (!valid_signal(data)) |
69f447cf RZ |
218 | goto out_eio; |
219 | ||
3b66a1ed | 220 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
b3319f50 RZ |
221 | tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16); |
222 | put_reg(child, PT_SR, tmp); | |
3b66a1ed | 223 | set_tsk_thread_flag(child, TIF_DELAYED_TRACE); |
b3319f50 RZ |
224 | |
225 | child->exit_code = data; | |
226 | /* give it a chance to run. */ | |
227 | wake_up_process(child); | |
b3319f50 | 228 | break; |
b3319f50 RZ |
229 | |
230 | case PTRACE_DETACH: /* detach a process that was attached. */ | |
231 | ret = ptrace_detach(child, data); | |
232 | break; | |
1da177e4 | 233 | |
69f447cf | 234 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ |
b3319f50 RZ |
235 | for (i = 0; i < 19; i++) { |
236 | tmp = get_reg(child, i); | |
237 | if (i == PT_SR) | |
1da177e4 | 238 | tmp >>= 16; |
69f447cf RZ |
239 | ret = put_user(tmp, (unsigned long *)data); |
240 | if (ret) | |
1da177e4 | 241 | break; |
b3319f50 | 242 | data += sizeof(long); |
1da177e4 | 243 | } |
b3319f50 | 244 | break; |
1da177e4 | 245 | |
69f447cf | 246 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ |
b3319f50 | 247 | for (i = 0; i < 19; i++) { |
69f447cf RZ |
248 | ret = get_user(tmp, (unsigned long *)data); |
249 | if (ret) | |
1da177e4 | 250 | break; |
b3319f50 | 251 | if (i == PT_SR) { |
1da177e4 LT |
252 | tmp &= SR_MASK; |
253 | tmp <<= 16; | |
254 | tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16); | |
1da177e4 | 255 | } |
b3319f50 RZ |
256 | put_reg(child, i, tmp); |
257 | data += sizeof(long); | |
1da177e4 | 258 | } |
b3319f50 | 259 | break; |
1da177e4 | 260 | |
69f447cf | 261 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
b3319f50 RZ |
262 | if (copy_to_user((void *)data, &child->thread.fp, |
263 | sizeof(struct user_m68kfp_struct))) | |
264 | ret = -EFAULT; | |
265 | break; | |
1da177e4 | 266 | |
69f447cf | 267 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
b3319f50 RZ |
268 | if (copy_from_user(&child->thread.fp, (void *)data, |
269 | sizeof(struct user_m68kfp_struct))) | |
270 | ret = -EFAULT; | |
271 | break; | |
1da177e4 | 272 | |
b3319f50 RZ |
273 | default: |
274 | ret = ptrace_request(child, request, addr, data); | |
275 | break; | |
1da177e4 | 276 | } |
481bed45 | 277 | |
1da177e4 | 278 | return ret; |
69f447cf | 279 | out_eio: |
481bed45 | 280 | return -EIO; |
1da177e4 LT |
281 | } |
282 | ||
283 | asmlinkage void syscall_trace(void) | |
284 | { | |
1da177e4 LT |
285 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
286 | ? 0x80 : 0)); | |
287 | /* | |
288 | * this isn't the same as continuing with a signal, but it will do | |
289 | * for normal use. strace only continues with a signal if the | |
290 | * stopping signal is not SIGTRAP. -brl | |
291 | */ | |
292 | if (current->exit_code) { | |
293 | send_sig(current->exit_code, current, 1); | |
294 | current->exit_code = 0; | |
295 | } | |
296 | } |