1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000-2007, Axis Communications AB.
6 #include <linux/kernel.h>
7 #include <linux/sched.h>
8 #include <linux/sched/task_stack.h>
10 #include <linux/smp.h>
11 #include <linux/errno.h>
12 #include <linux/ptrace.h>
13 #include <linux/user.h>
14 #include <linux/signal.h>
15 #include <linux/security.h>
17 #include <linux/uaccess.h>
19 #include <asm/pgtable.h>
20 #include <asm/processor.h>
21 #include <arch/hwregs/supp_reg.h>
24 * Determines which bits in CCS the user has access to.
25 * 1 = access, 0 = no access.
27 #define CCS_MASK 0x00087c00 /* SXNZVC */
29 #define SBIT_USER (1 << (S_CCS_BITNR + CCS_SHIFT))
31 static int put_debugreg(long pid
, unsigned int regno
, long data
);
32 static long get_debugreg(long pid
, unsigned int regno
);
33 static unsigned long get_pseudo_pc(struct task_struct
*child
);
34 void deconfigure_bp(long pid
);
36 extern unsigned long cris_signal_return_page
;
39 * Get contents of register REGNO in task TASK.
41 long get_reg(struct task_struct
*task
, unsigned int regno
)
43 /* USP is a special case, it's not in the pt_regs struct but
44 * in the tasks thread struct
49 ret
= ((unsigned long *)task_pt_regs(task
))[regno
];
50 else if (regno
== PT_USP
)
51 ret
= task
->thread
.usp
;
52 else if (regno
== PT_PPC
)
53 ret
= get_pseudo_pc(task
);
54 else if (regno
<= PT_MAX
)
55 ret
= get_debugreg(task
->pid
, regno
);
63 * Write contents of register REGNO in task TASK.
65 int put_reg(struct task_struct
*task
, unsigned int regno
, unsigned long data
)
68 ((unsigned long *)task_pt_regs(task
))[regno
] = data
;
69 else if (regno
== PT_USP
)
70 task
->thread
.usp
= data
;
71 else if (regno
== PT_PPC
) {
72 /* Write pseudo-PC to ERP only if changed. */
73 if (data
!= get_pseudo_pc(task
))
74 task_pt_regs(task
)->erp
= data
;
75 } else if (regno
<= PT_MAX
)
76 return put_debugreg(task
->pid
, regno
, data
);
82 void user_enable_single_step(struct task_struct
*child
)
87 * Set up SPC if not set already (in which case we have no other
88 * choice but to trust it).
90 if (!get_reg(child
, PT_SPC
)) {
91 /* In case we're stopped in a delay slot. */
92 tmp
= get_reg(child
, PT_ERP
) & ~1;
93 put_reg(child
, PT_SPC
, tmp
);
95 tmp
= get_reg(child
, PT_CCS
) | SBIT_USER
;
96 put_reg(child
, PT_CCS
, tmp
);
99 void user_disable_single_step(struct task_struct
*child
)
101 put_reg(child
, PT_SPC
, 0);
103 if (!get_debugreg(child
->pid
, PT_BP_CTRL
)) {
105 /* If no h/w bp configured, disable S bit. */
106 tmp
= get_reg(child
, PT_CCS
) & ~SBIT_USER
;
107 put_reg(child
, PT_CCS
, tmp
);
112 * Called by kernel/ptrace.c when detaching.
114 * Make sure the single step bit is not set.
117 ptrace_disable(struct task_struct
*child
)
119 /* Deconfigure SPC and S-bit. */
120 user_disable_single_step(child
);
121 put_reg(child
, PT_SPC
, 0);
123 /* Deconfigure any watchpoints associated with the child. */
124 deconfigure_bp(child
->pid
);
128 long arch_ptrace(struct task_struct
*child
, long request
,
129 unsigned long addr
, unsigned long data
)
132 unsigned int regno
= addr
>> 2;
133 unsigned long __user
*datap
= (unsigned long __user
*)data
;
136 /* Read word at location address. */
137 case PTRACE_PEEKTEXT
:
138 case PTRACE_PEEKDATA
: {
144 /* The signal trampoline page is outside the normal user-addressable
145 * space but still accessible. This is hack to make it possible to
146 * access the signal handler code in GDB.
148 if ((addr
& PAGE_MASK
) == cris_signal_return_page
) {
149 /* The trampoline page is globally mapped, no page table to traverse.*/
150 tmp
= *(unsigned long*)addr
;
152 copied
= ptrace_access_vm(child
, addr
, &tmp
, sizeof(tmp
), FOLL_FORCE
);
154 if (copied
!= sizeof(tmp
))
158 ret
= put_user(tmp
,datap
);
162 /* Read the word at location address in the USER area. */
163 case PTRACE_PEEKUSR
: {
167 if ((addr
& 3) || regno
> PT_MAX
)
170 tmp
= get_reg(child
, regno
);
171 ret
= put_user(tmp
, datap
);
175 /* Write the word at location address. */
176 case PTRACE_POKETEXT
:
177 case PTRACE_POKEDATA
:
178 ret
= generic_ptrace_pokedata(child
, addr
, data
);
181 /* Write the word at location address in the USER area. */
184 if ((addr
& 3) || regno
> PT_MAX
)
187 if (regno
== PT_CCS
) {
188 /* don't allow the tracing process to change stuff like
189 * interrupt enable, kernel/user bit, dma enables etc.
192 data
|= get_reg(child
, PT_CCS
) & ~CCS_MASK
;
194 if (put_reg(child
, regno
, data
))
199 /* Get all GP registers from the child. */
200 case PTRACE_GETREGS
: {
204 for (i
= 0; i
<= PT_MAX
; i
++) {
205 tmp
= get_reg(child
, i
);
207 if (put_user(tmp
, datap
)) {
219 /* Set all GP registers in the child. */
220 case PTRACE_SETREGS
: {
224 for (i
= 0; i
<= PT_MAX
; i
++) {
225 if (get_user(tmp
, datap
)) {
232 tmp
|= get_reg(child
, PT_CCS
) & ~CCS_MASK
;
235 put_reg(child
, i
, tmp
);
244 ret
= ptrace_request(child
, request
, addr
, data
);
252 void do_syscall_trace(void)
254 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
257 if (!(current
->ptrace
& PT_PTRACED
))
260 /* the 0x80 provides a way for the tracing parent to distinguish
261 between a syscall stop and SIGTRAP delivery */
262 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
266 * This isn't the same as continuing with a signal, but it will do for
269 if (current
->exit_code
) {
270 send_sig(current
->exit_code
, current
, 1);
271 current
->exit_code
= 0;
275 /* Returns the size of an instruction that has a delay slot. */
277 static int insn_size(struct task_struct
*child
, unsigned long pc
)
279 unsigned long opcode
;
283 /* Read the opcode at pc (do what PTRACE_PEEKTEXT would do). */
284 copied
= access_process_vm(child
, pc
, &opcode
, sizeof(opcode
), FOLL_FORCE
);
285 if (copied
!= sizeof(opcode
))
288 switch ((opcode
& 0x0f00) >> 8) {
299 /* Could be 4 or 6; check more bits. */
300 if ((opcode
& 0xff) == 0xff)
306 panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
313 static unsigned long get_pseudo_pc(struct task_struct
*child
)
315 /* Default value for PC is ERP. */
316 unsigned long pc
= get_reg(child
, PT_ERP
);
319 unsigned long spc
= get_reg(child
, PT_SPC
);
320 /* Delay slot bit set. Report as stopped on proper
323 /* Rely on SPC if set. FIXME: We might want to check
324 that EXS indicates we stopped due to a single-step
328 /* Calculate the PC from the size of the instruction
329 that the delay slot we're in belongs to. */
330 pc
+= insn_size(child
, pc
& ~1) - 1;
336 static long bp_owner
= 0;
338 /* Reachable from exit_thread in signal.c, so not static. */
339 void deconfigure_bp(long pid
)
343 /* Only deconfigure if the pid is the owner. */
347 for (bp
= 0; bp
< 6; bp
++) {
349 /* Deconfigure start and end address (also gets rid of ownership). */
350 put_debugreg(pid
, PT_BP
+ 3 + (bp
* 2), 0);
351 put_debugreg(pid
, PT_BP
+ 4 + (bp
* 2), 0);
353 /* Deconfigure relevant bits in control register. */
354 tmp
= get_debugreg(pid
, PT_BP_CTRL
) & ~(3 << (2 + (bp
* 4)));
355 put_debugreg(pid
, PT_BP_CTRL
, tmp
);
361 static int put_debugreg(long pid
, unsigned int regno
, long data
)
364 register int old_srs
;
366 #ifdef CONFIG_ETRAX_KGDB
367 /* Ignore write, but pretend it was ok if value is 0
368 (we don't want POKEUSR/SETREGS failing unnessecarily). */
369 return (data
== 0) ? ret
: -1;
372 /* Simple owner management. */
375 else if (bp_owner
!= pid
) {
376 /* Ignore write, but pretend it was ok if value is 0
377 (we don't want POKEUSR/SETREGS failing unnessecarily). */
378 return (data
== 0) ? ret
: -1;
381 /* Remember old SRS. */
382 SPEC_REG_RD(SPEC_REG_SRS
, old_srs
);
383 /* Switch to BP bank. */
384 SUPP_BANK_SEL(BANK_BP
);
386 switch (regno
- PT_BP
) {
388 SUPP_REG_WR(0, data
); break;
395 SUPP_REG_WR(3, data
); break;
397 SUPP_REG_WR(4, data
); break;
399 SUPP_REG_WR(5, data
); break;
401 SUPP_REG_WR(6, data
); break;
403 SUPP_REG_WR(7, data
); break;
405 SUPP_REG_WR(8, data
); break;
407 SUPP_REG_WR(9, data
); break;
409 SUPP_REG_WR(10, data
); break;
411 SUPP_REG_WR(11, data
); break;
413 SUPP_REG_WR(12, data
); break;
415 SUPP_REG_WR(13, data
); break;
417 SUPP_REG_WR(14, data
); break;
424 SPEC_REG_WR(SPEC_REG_SRS
, old_srs
);
433 static long get_debugreg(long pid
, unsigned int regno
)
435 register int old_srs
;
438 if (pid
!= bp_owner
) {
442 /* Remember old SRS. */
443 SPEC_REG_RD(SPEC_REG_SRS
, old_srs
);
444 /* Switch to BP bank. */
445 SUPP_BANK_SEL(BANK_BP
);
447 switch (regno
- PT_BP
) {
449 SUPP_REG_RD(0, data
); break;
452 /* error return value? */
456 SUPP_REG_RD(3, data
); break;
458 SUPP_REG_RD(4, data
); break;
460 SUPP_REG_RD(5, data
); break;
462 SUPP_REG_RD(6, data
); break;
464 SUPP_REG_RD(7, data
); break;
466 SUPP_REG_RD(8, data
); break;
468 SUPP_REG_RD(9, data
); break;
470 SUPP_REG_RD(10, data
); break;
472 SUPP_REG_RD(11, data
); break;
474 SUPP_REG_RD(12, data
); break;
476 SUPP_REG_RD(13, data
); break;
478 SUPP_REG_RD(14, data
); break;
480 /* error return value? */
485 SPEC_REG_WR(SPEC_REG_SRS
, old_srs
);