2 * arch/score/kernel/ptrace.c
4 * Score Processor version.
6 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see the file COPYING, or write
22 * to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/elf.h>
27 #include <linux/kernel.h>
29 #include <linux/ptrace.h>
30 #include <linux/regset.h>
32 #include <asm/uaccess.h>
35 * retrieve the contents of SCORE userspace general registers
37 static int genregs_get(struct task_struct *target,
38 const struct user_regset *regset,
39 unsigned int pos, unsigned int count,
40 void *kbuf, void __user *ubuf)
42 const struct pt_regs *regs = task_pt_regs(target);
45 /* skip 9 * sizeof(unsigned long) not use for pt_regs */
46 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
47 0, offsetof(struct pt_regs, regs));
49 /* r0 - r31, cel, ceh, sr0, sr1, sr2, epc, ema, psr, ecr, condition */
50 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
52 offsetof(struct pt_regs, regs),
53 offsetof(struct pt_regs, cp0_condition));
56 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
57 sizeof(struct pt_regs), -1);
63 * update the contents of the SCORE userspace general registers
65 static int genregs_set(struct task_struct *target,
66 const struct user_regset *regset,
67 unsigned int pos, unsigned int count,
68 const void *kbuf, const void __user *ubuf)
70 struct pt_regs *regs = task_pt_regs(target);
73 /* skip 9 * sizeof(unsigned long) */
74 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
75 0, offsetof(struct pt_regs, regs));
77 /* r0 - r31, cel, ceh, sr0, sr1, sr2, epc, ema, psr, ecr, condition */
78 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
80 offsetof(struct pt_regs, regs),
81 offsetof(struct pt_regs, cp0_condition));
84 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
85 sizeof(struct pt_regs), -1);
91 * Define the register sets available on the score7 under Linux
97 static const struct user_regset score7_regsets[] = {
99 .core_note_type = NT_PRSTATUS,
101 .size = sizeof(long),
102 .align = sizeof(long),
108 static const struct user_regset_view user_score_native_view = {
110 .e_machine = EM_SCORE7,
111 .regsets = score7_regsets,
112 .n = ARRAY_SIZE(score7_regsets),
115 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
117 return &user_score_native_view;
120 static int is_16bitinsn(unsigned long insn)
122 if ((insn & INSN32_MASK) == INSN32_MASK)
129 read_tsk_long(struct task_struct *child,
130 unsigned long addr, unsigned long *res)
134 copied = access_process_vm(child, addr, res, sizeof(*res), 0);
136 return copied != sizeof(*res) ? -EIO : 0;
140 read_tsk_short(struct task_struct *child,
141 unsigned long addr, unsigned short *res)
145 copied = access_process_vm(child, addr, res, sizeof(*res), 0);
147 return copied != sizeof(*res) ? -EIO : 0;
151 write_tsk_short(struct task_struct *child,
152 unsigned long addr, unsigned short val)
156 copied = access_process_vm(child, addr, &val, sizeof(val), 1);
158 return copied != sizeof(val) ? -EIO : 0;
162 write_tsk_long(struct task_struct *child,
163 unsigned long addr, unsigned long val)
167 copied = access_process_vm(child, addr, &val, sizeof(val), 1);
169 return copied != sizeof(val) ? -EIO : 0;
172 void user_enable_single_step(struct task_struct *child)
174 /* far_epc is the target of branch */
175 unsigned int epc, far_epc = 0;
176 unsigned long epc_insn, far_epc_insn;
177 int ninsn_type; /* next insn type 0=16b, 1=32b */
178 unsigned int tmp, tmp2;
179 struct pt_regs *regs = task_pt_regs(child);
180 child->thread.single_step = 1;
181 child->thread.ss_nextcnt = 1;
184 read_tsk_long(child, epc, &epc_insn);
186 if (is_16bitinsn(epc_insn)) {
187 if ((epc_insn & J16M) == J16) {
188 tmp = epc_insn & 0xFFE;
189 epc = (epc & 0xFFFFF000) | tmp;
190 } else if ((epc_insn & B16M) == B16) {
191 child->thread.ss_nextcnt = 2;
192 tmp = (epc_insn & 0xFF) << 1;
194 tmp = (unsigned int)((int) tmp >> 23);
197 } else if ((epc_insn & BR16M) == BR16) {
198 child->thread.ss_nextcnt = 2;
199 tmp = (epc_insn >> 4) & 0xF;
200 far_epc = regs->regs[tmp];
205 if ((epc_insn & J32M) == J32) {
206 tmp = epc_insn & 0x03FFFFFE;
208 tmp = (((tmp >> 16) & 0x3FF) << 15) | tmp2;
209 epc = (epc & 0xFFC00000) | tmp;
210 } else if ((epc_insn & B32M) == B32) {
211 child->thread.ss_nextcnt = 2;
212 tmp = epc_insn & 0x03FFFFFE; /* discard LK bit */
214 tmp = (((tmp >> 16) & 0x3FF) << 10) | tmp2; /* 20bit */
216 tmp = (unsigned int)((int) tmp >> 12);
219 } else if ((epc_insn & BR32M) == BR32) {
220 child->thread.ss_nextcnt = 2;
221 tmp = (epc_insn >> 16) & 0x1F;
222 far_epc = regs->regs[tmp];
228 if (child->thread.ss_nextcnt == 1) {
229 read_tsk_long(child, epc, &epc_insn);
231 if (is_16bitinsn(epc_insn)) {
232 write_tsk_short(child, epc, SINGLESTEP16_INSN);
235 write_tsk_long(child, epc, SINGLESTEP32_INSN);
239 if (ninsn_type == 0) { /* 16bits */
240 child->thread.insn1_type = 0;
241 child->thread.addr1 = epc;
242 /* the insn may have 32bit data */
243 child->thread.insn1 = (short)epc_insn;
245 child->thread.insn1_type = 1;
246 child->thread.addr1 = epc;
247 child->thread.insn1 = epc_insn;
250 /* branch! have two target child->thread.ss_nextcnt=2 */
251 read_tsk_long(child, epc, &epc_insn);
252 read_tsk_long(child, far_epc, &far_epc_insn);
253 if (is_16bitinsn(epc_insn)) {
254 write_tsk_short(child, epc, SINGLESTEP16_INSN);
257 write_tsk_long(child, epc, SINGLESTEP32_INSN);
261 if (ninsn_type == 0) { /* 16bits */
262 child->thread.insn1_type = 0;
263 child->thread.addr1 = epc;
264 /* the insn may have 32bit data */
265 child->thread.insn1 = (short)epc_insn;
267 child->thread.insn1_type = 1;
268 child->thread.addr1 = epc;
269 child->thread.insn1 = epc_insn;
272 if (is_16bitinsn(far_epc_insn)) {
273 write_tsk_short(child, far_epc, SINGLESTEP16_INSN);
276 write_tsk_long(child, far_epc, SINGLESTEP32_INSN);
280 if (ninsn_type == 0) { /* 16bits */
281 child->thread.insn2_type = 0;
282 child->thread.addr2 = far_epc;
283 /* the insn may have 32bit data */
284 child->thread.insn2 = (short)far_epc_insn;
286 child->thread.insn2_type = 1;
287 child->thread.addr2 = far_epc;
288 child->thread.insn2 = far_epc_insn;
293 void user_disable_single_step(struct task_struct *child)
295 if (child->thread.insn1_type == 0)
296 write_tsk_short(child, child->thread.addr1,
297 child->thread.insn1);
299 if (child->thread.insn1_type == 1)
300 write_tsk_long(child, child->thread.addr1,
301 child->thread.insn1);
303 if (child->thread.ss_nextcnt == 2) { /* branch */
304 if (child->thread.insn1_type == 0)
305 write_tsk_short(child, child->thread.addr1,
306 child->thread.insn1);
307 if (child->thread.insn1_type == 1)
308 write_tsk_long(child, child->thread.addr1,
309 child->thread.insn1);
310 if (child->thread.insn2_type == 0)
311 write_tsk_short(child, child->thread.addr2,
312 child->thread.insn2);
313 if (child->thread.insn2_type == 1)
314 write_tsk_long(child, child->thread.addr2,
315 child->thread.insn2);
318 child->thread.single_step = 0;
319 child->thread.ss_nextcnt = 0;
322 void ptrace_disable(struct task_struct *child)
324 user_disable_single_step(child);
328 arch_ptrace(struct task_struct *child, long request,
329 unsigned long addr, unsigned long data)
332 unsigned long __user *datap = (void __user *)data;
336 ret = copy_regset_to_user(child, &user_score_native_view,
338 0, sizeof(struct pt_regs),
343 ret = copy_regset_from_user(child, &user_score_native_view,
345 0, sizeof(struct pt_regs),
350 ret = ptrace_request(child, request, addr, data);
358 * Notification of system call entry/exit
359 * - triggered by current->work.syscall_trace
361 asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
363 if (!(current->ptrace & PT_PTRACED))
366 if (!test_thread_flag(TIF_SYSCALL_TRACE))
369 /* The 0x80 provides a way for the tracing parent to distinguish
370 between a syscall stop and SIGTRAP delivery. */
371 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
375 * this isn't the same as continuing with a signal, but it will do
376 * for normal use. strace only continues with a signal if the
377 * stopping signal is not SIGTRAP. -brl
379 if (current->exit_code) {
380 send_sig(current->exit_code, current, 1);
381 current->exit_code = 0;