2 * arch/score/kernel/ptrace.c
4 * Score Processor version.
6 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
7 * Chen Liqin <liqin.chen@sunplusct.com>
8 * Lennox Wu <lennox.wu@sunplusct.com>
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 <linux/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
), FOLL_FORCE
);
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
), FOLL_FORCE
);
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
),
157 FOLL_FORCE
| FOLL_WRITE
);
159 return copied
!= sizeof(val
) ? -EIO
: 0;
163 write_tsk_long(struct task_struct
*child
,
164 unsigned long addr
, unsigned long val
)
168 copied
= access_process_vm(child
, addr
, &val
, sizeof(val
),
169 FOLL_FORCE
| FOLL_WRITE
);
171 return copied
!= sizeof(val
) ? -EIO
: 0;
174 void user_enable_single_step(struct task_struct
*child
)
176 /* far_epc is the target of branch */
177 unsigned int epc
, far_epc
= 0;
178 unsigned long epc_insn
, far_epc_insn
;
179 int ninsn_type
; /* next insn type 0=16b, 1=32b */
180 unsigned int tmp
, tmp2
;
181 struct pt_regs
*regs
= task_pt_regs(child
);
182 child
->thread
.single_step
= 1;
183 child
->thread
.ss_nextcnt
= 1;
186 read_tsk_long(child
, epc
, &epc_insn
);
188 if (is_16bitinsn(epc_insn
)) {
189 if ((epc_insn
& J16M
) == J16
) {
190 tmp
= epc_insn
& 0xFFE;
191 epc
= (epc
& 0xFFFFF000) | tmp
;
192 } else if ((epc_insn
& B16M
) == B16
) {
193 child
->thread
.ss_nextcnt
= 2;
194 tmp
= (epc_insn
& 0xFF) << 1;
196 tmp
= (unsigned int)((int) tmp
>> 23);
199 } else if ((epc_insn
& BR16M
) == BR16
) {
200 child
->thread
.ss_nextcnt
= 2;
201 tmp
= (epc_insn
>> 4) & 0xF;
202 far_epc
= regs
->regs
[tmp
];
207 if ((epc_insn
& J32M
) == J32
) {
208 tmp
= epc_insn
& 0x03FFFFFE;
210 tmp
= (((tmp
>> 16) & 0x3FF) << 15) | tmp2
;
211 epc
= (epc
& 0xFFC00000) | tmp
;
212 } else if ((epc_insn
& B32M
) == B32
) {
213 child
->thread
.ss_nextcnt
= 2;
214 tmp
= epc_insn
& 0x03FFFFFE; /* discard LK bit */
216 tmp
= (((tmp
>> 16) & 0x3FF) << 10) | tmp2
; /* 20bit */
218 tmp
= (unsigned int)((int) tmp
>> 12);
221 } else if ((epc_insn
& BR32M
) == BR32
) {
222 child
->thread
.ss_nextcnt
= 2;
223 tmp
= (epc_insn
>> 16) & 0x1F;
224 far_epc
= regs
->regs
[tmp
];
230 if (child
->thread
.ss_nextcnt
== 1) {
231 read_tsk_long(child
, epc
, &epc_insn
);
233 if (is_16bitinsn(epc_insn
)) {
234 write_tsk_short(child
, epc
, SINGLESTEP16_INSN
);
237 write_tsk_long(child
, epc
, SINGLESTEP32_INSN
);
241 if (ninsn_type
== 0) { /* 16bits */
242 child
->thread
.insn1_type
= 0;
243 child
->thread
.addr1
= epc
;
244 /* the insn may have 32bit data */
245 child
->thread
.insn1
= (short)epc_insn
;
247 child
->thread
.insn1_type
= 1;
248 child
->thread
.addr1
= epc
;
249 child
->thread
.insn1
= epc_insn
;
252 /* branch! have two target child->thread.ss_nextcnt=2 */
253 read_tsk_long(child
, epc
, &epc_insn
);
254 read_tsk_long(child
, far_epc
, &far_epc_insn
);
255 if (is_16bitinsn(epc_insn
)) {
256 write_tsk_short(child
, epc
, SINGLESTEP16_INSN
);
259 write_tsk_long(child
, epc
, SINGLESTEP32_INSN
);
263 if (ninsn_type
== 0) { /* 16bits */
264 child
->thread
.insn1_type
= 0;
265 child
->thread
.addr1
= epc
;
266 /* the insn may have 32bit data */
267 child
->thread
.insn1
= (short)epc_insn
;
269 child
->thread
.insn1_type
= 1;
270 child
->thread
.addr1
= epc
;
271 child
->thread
.insn1
= epc_insn
;
274 if (is_16bitinsn(far_epc_insn
)) {
275 write_tsk_short(child
, far_epc
, SINGLESTEP16_INSN
);
278 write_tsk_long(child
, far_epc
, SINGLESTEP32_INSN
);
282 if (ninsn_type
== 0) { /* 16bits */
283 child
->thread
.insn2_type
= 0;
284 child
->thread
.addr2
= far_epc
;
285 /* the insn may have 32bit data */
286 child
->thread
.insn2
= (short)far_epc_insn
;
288 child
->thread
.insn2_type
= 1;
289 child
->thread
.addr2
= far_epc
;
290 child
->thread
.insn2
= far_epc_insn
;
295 void user_disable_single_step(struct task_struct
*child
)
297 if (child
->thread
.insn1_type
== 0)
298 write_tsk_short(child
, child
->thread
.addr1
,
299 child
->thread
.insn1
);
301 if (child
->thread
.insn1_type
== 1)
302 write_tsk_long(child
, child
->thread
.addr1
,
303 child
->thread
.insn1
);
305 if (child
->thread
.ss_nextcnt
== 2) { /* branch */
306 if (child
->thread
.insn1_type
== 0)
307 write_tsk_short(child
, child
->thread
.addr1
,
308 child
->thread
.insn1
);
309 if (child
->thread
.insn1_type
== 1)
310 write_tsk_long(child
, child
->thread
.addr1
,
311 child
->thread
.insn1
);
312 if (child
->thread
.insn2_type
== 0)
313 write_tsk_short(child
, child
->thread
.addr2
,
314 child
->thread
.insn2
);
315 if (child
->thread
.insn2_type
== 1)
316 write_tsk_long(child
, child
->thread
.addr2
,
317 child
->thread
.insn2
);
320 child
->thread
.single_step
= 0;
321 child
->thread
.ss_nextcnt
= 0;
324 void ptrace_disable(struct task_struct
*child
)
326 user_disable_single_step(child
);
330 arch_ptrace(struct task_struct
*child
, long request
,
331 unsigned long addr
, unsigned long data
)
334 unsigned long __user
*datap
= (void __user
*)data
;
338 ret
= copy_regset_to_user(child
, &user_score_native_view
,
340 0, sizeof(struct pt_regs
),
345 ret
= copy_regset_from_user(child
, &user_score_native_view
,
347 0, sizeof(struct pt_regs
),
352 ret
= ptrace_request(child
, request
, addr
, data
);
360 * Notification of system call entry/exit
361 * - triggered by current->work.syscall_trace
363 asmlinkage
void do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
365 if (!(current
->ptrace
& PT_PTRACED
))
368 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
371 /* The 0x80 provides a way for the tracing parent to distinguish
372 between a syscall stop and SIGTRAP delivery. */
373 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) ?
377 * this isn't the same as continuing with a signal, but it will do
378 * for normal use. strace only continues with a signal if the
379 * stopping signal is not SIGTRAP. -brl
381 if (current
->exit_code
) {
382 send_sig(current
->exit_code
, current
, 1);
383 current
->exit_code
= 0;