Skip to content

Commit 7c86ff9

Browse files
seehearfeeltsbogend
authored andcommitted
MIPS: Add basic support for ptrace single step
In the current code, arch_has_single_step() is not defined on MIPS, that means MIPS does not support instruction single-step for user mode. Delve is a debugger for the Go programming language, the ptrace syscall PtraceSingleStep() failed [1] on MIPS and then the single step function can not work well, we can see that PtraceSingleStep() definition returns ptrace(PTRACE_SINGLESTEP) [2]. So it is necessary to support ptrace single step on MIPS. At the beginning, we try to use the Debug Single Step exception on the Loongson 3A4000 platform, but it has no effect when set CP0_DEBUG SSt bit, this is because CP0_DEBUG NoSSt bit is 1 which indicates no single-step feature available [3], so this way which is dependent on the hardware is almost impossible. With further research, we find out there exists a common way used with break instruction in arch/alpha/kernel/ptrace.c, it is workable. For the above analysis, define arch_has_single_step(), add the common function user_enable_single_step() and user_disable_single_step(), set flag TIF_SINGLESTEP for child process, use break instruction to set breakpoint. We can use the following testcase to test it: tools/testing/selftests/breakpoints/step_after_suspend_test.c $ make -C tools/testing/selftests TARGETS=breakpoints $ cd tools/testing/selftests/breakpoints Without this patch: $ ./step_after_suspend_test -n TAP version 13 1..4 # ptrace(PTRACE_SINGLESTEP) not supported on this architecture: Input/output error ok 1 # SKIP CPU 0 # ptrace(PTRACE_SINGLESTEP) not supported on this architecture: Input/output error ok 2 # SKIP CPU 1 # ptrace(PTRACE_SINGLESTEP) not supported on this architecture: Input/output error ok 3 # SKIP CPU 2 # ptrace(PTRACE_SINGLESTEP) not supported on this architecture: Input/output error ok 4 # SKIP CPU 3 # Totals: pass:0 fail:0 xfail:0 xpass:0 skip:4 error:0 With this patch: $ ./step_after_suspend_test -n TAP version 13 1..4 ok 1 CPU 0 ok 2 CPU 1 ok 3 CPU 2 ok 4 CPU 3 # Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0 [1] https://github.com/go-delve/delve/blob/master/pkg/proc/native/threads_linux.go#L50 [2] https://github.com/go-delve/delve/blob/master/vendor/golang.org/x/sys/unix/syscall_linux.go#L1573 [3] http://www.t-es-t.hu/download/mips/md00047f.pdf Reported-by: Guoqi Chen <[email protected]> Signed-off-by: Xingxing Su <[email protected]> Signed-off-by: Tiezhu Yang <[email protected]> Reported-by: kernel test robot <[email protected]> Signed-off-by: Thomas Bogendoerfer <[email protected]>
1 parent bde258b commit 7c86ff9

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

arch/mips/include/asm/ptrace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,6 @@ static inline void user_stack_pointer_set(struct pt_regs *regs,
186186
regs->regs[29] = val;
187187
}
188188

189+
#define arch_has_single_step() (1)
190+
189191
#endif /* _ASM_PTRACE_H */

arch/mips/include/asm/thread_info.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ struct thread_info {
3535
*/
3636
struct pt_regs *regs;
3737
long syscall; /* syscall number */
38+
39+
int bpt_nsaved;
40+
unsigned long bpt_addr[1]; /* breakpoint handling */
41+
unsigned int bpt_insn[1];
3842
};
3943

4044
/*
@@ -117,6 +121,7 @@ static inline struct thread_info *current_thread_info(void)
117121
#define TIF_UPROBE 6 /* breakpointed or singlestepping */
118122
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
119123
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
124+
#define TIF_SINGLESTEP 10 /* restore singlestep on return to user mode */
120125
#define TIF_USEDFPU 16 /* FPU was used by this task this quantum (SMP) */
121126
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
122127
#define TIF_NOHZ 19 /* in adaptive nohz mode */

arch/mips/kernel/ptrace.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@
4545
#include <linux/uaccess.h>
4646
#include <asm/bootinfo.h>
4747
#include <asm/reg.h>
48+
#include <asm/branch.h>
4849

4950
#define CREATE_TRACE_POINTS
5051
#include <trace/events/syscalls.h>
5152

53+
#include "probes-common.h"
54+
55+
#define BREAKINST 0x0000000d
56+
5257
/*
5358
* Called by kernel/ptrace.c when detaching..
5459
*
@@ -58,6 +63,7 @@ void ptrace_disable(struct task_struct *child)
5863
{
5964
/* Don't load the watchpoint registers for the ex-child. */
6065
clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
66+
user_disable_single_step(child);
6167
}
6268

6369
/*
@@ -1072,6 +1078,108 @@ const struct user_regset_view *task_user_regset_view(struct task_struct *task)
10721078
#endif
10731079
}
10741080

1081+
static int read_insn(struct task_struct *task, unsigned long addr, unsigned int *insn)
1082+
{
1083+
int copied = access_process_vm(task, addr, insn,
1084+
sizeof(unsigned int), FOLL_FORCE);
1085+
1086+
if (copied != sizeof(unsigned int)) {
1087+
pr_err("failed to read instruction from 0x%lx\n", addr);
1088+
return -EIO;
1089+
}
1090+
1091+
return 0;
1092+
}
1093+
1094+
static int write_insn(struct task_struct *task, unsigned long addr, unsigned int insn)
1095+
{
1096+
int copied = access_process_vm(task, addr, &insn,
1097+
sizeof(unsigned int), FOLL_FORCE | FOLL_WRITE);
1098+
1099+
if (copied != sizeof(unsigned int)) {
1100+
pr_err("failed to write instruction to 0x%lx\n", addr);
1101+
return -EIO;
1102+
}
1103+
1104+
return 0;
1105+
}
1106+
1107+
static int insn_has_delayslot(union mips_instruction insn)
1108+
{
1109+
return __insn_has_delay_slot(insn);
1110+
}
1111+
1112+
static void ptrace_set_bpt(struct task_struct *child)
1113+
{
1114+
union mips_instruction mips_insn = { 0 };
1115+
struct pt_regs *regs;
1116+
unsigned long pc;
1117+
unsigned int insn;
1118+
int i, ret, nsaved = 0;
1119+
1120+
regs = task_pt_regs(child);
1121+
pc = regs->cp0_epc;
1122+
1123+
ret = read_insn(child, pc, &insn);
1124+
if (ret < 0)
1125+
return;
1126+
1127+
if (insn_has_delayslot(mips_insn)) {
1128+
pr_info("executing branch insn\n");
1129+
ret = __compute_return_epc(regs);
1130+
if (ret < 0)
1131+
return;
1132+
task_thread_info(child)->bpt_addr[nsaved++] = regs->cp0_epc;
1133+
} else {
1134+
pr_info("executing normal insn\n");
1135+
task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
1136+
}
1137+
1138+
/* install breakpoints */
1139+
for (i = 0; i < nsaved; i++) {
1140+
ret = read_insn(child, task_thread_info(child)->bpt_addr[i], &insn);
1141+
if (ret < 0)
1142+
return;
1143+
1144+
task_thread_info(child)->bpt_insn[i] = insn;
1145+
1146+
ret = write_insn(child, task_thread_info(child)->bpt_addr[i], BREAKINST);
1147+
if (ret < 0)
1148+
return;
1149+
}
1150+
1151+
task_thread_info(child)->bpt_nsaved = nsaved;
1152+
}
1153+
1154+
static void ptrace_cancel_bpt(struct task_struct *child)
1155+
{
1156+
int i, nsaved = task_thread_info(child)->bpt_nsaved;
1157+
1158+
task_thread_info(child)->bpt_nsaved = 0;
1159+
1160+
if (nsaved > 1) {
1161+
pr_info("%s: bogus nsaved: %d!\n", __func__, nsaved);
1162+
nsaved = 1;
1163+
}
1164+
1165+
for (i = 0; i < nsaved; i++) {
1166+
write_insn(child, task_thread_info(child)->bpt_addr[i],
1167+
task_thread_info(child)->bpt_insn[i]);
1168+
}
1169+
}
1170+
1171+
void user_enable_single_step(struct task_struct *child)
1172+
{
1173+
set_tsk_thread_flag(child, TIF_SINGLESTEP);
1174+
ptrace_set_bpt(child);
1175+
}
1176+
1177+
void user_disable_single_step(struct task_struct *child)
1178+
{
1179+
clear_tsk_thread_flag(child, TIF_SINGLESTEP);
1180+
ptrace_cancel_bpt(child);
1181+
}
1182+
10751183
long arch_ptrace(struct task_struct *child, long request,
10761184
unsigned long addr, unsigned long data)
10771185
{

arch/mips/kernel/signal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
849849
ret = abi->setup_frame(vdso + abi->vdso->off_sigreturn,
850850
ksig, regs, oldset);
851851

852-
signal_setup_done(ret, ksig, 0);
852+
signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
853853
}
854854

855855
static void do_signal(struct pt_regs *regs)

0 commit comments

Comments
 (0)