Skip to content

Commit 9432f96

Browse files
Stuart Menefypmundt
authored andcommitted
sh: Clear UBC when not in use.
This takes care of tearing down the UBC so it's not inadvertently left configured at the next context switch time. Failure to do this results in spurious SIGTRAPs in certain debug sequences. Signed-off-by: Stuart Menefy <stuart.menefy@st.com> Signed-off-by: Paul Mundt <lethal@linux-sh.org>
1 parent 20b0f65 commit 9432f96

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

‎arch/sh/kernel/entry-common.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ work_resched:
224224
syscall_exit_work:
225225
! r0: current_thread_info->flags
226226
! r8: current_thread_info
227-
tst #_TIF_SYSCALL_TRACE, r0
227+
tst #_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP, r0
228228
bt/s work_pending
229229
tst #_TIF_NEED_RESCHED, r0
230230
#ifdef CONFIG_TRACE_IRQFLAGS

‎arch/sh/kernel/ptrace.c‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
99
*
1010
*/
11-
1211
#include <linux/kernel.h>
1312
#include <linux/sched.h>
1413
#include <linux/mm.h>
@@ -20,8 +19,7 @@
2019
#include <linux/slab.h>
2120
#include <linux/security.h>
2221
#include <linux/signal.h>
23-
24-
#include <asm/io.h>
22+
#include <linux/io.h>
2523
#include <asm/uaccess.h>
2624
#include <asm/pgtable.h>
2725
#include <asm/system.h>
@@ -59,14 +57,31 @@ static inline int put_stack_long(struct task_struct *task, int offset,
5957
return 0;
6058
}
6159

60+
static void ptrace_disable_singlestep(struct task_struct *child)
61+
{
62+
clear_tsk_thread_flag(child, TIF_SINGLESTEP);
63+
64+
/*
65+
* Ensure the UBC is not programmed at the next context switch.
66+
*
67+
* Normally this is not needed but there are sequences such as
68+
* singlestep, signal delivery, and continue that leave the
69+
* ubc_pc non-zero leading to spurious SIGTRAPs.
70+
*/
71+
if (child->thread.ubc_pc != 0) {
72+
ubc_usercnt -= 1;
73+
child->thread.ubc_pc = 0;
74+
}
75+
}
76+
6277
/*
6378
* Called by kernel/ptrace.c when detaching..
6479
*
6580
* Make sure single step bits etc are not set.
6681
*/
6782
void ptrace_disable(struct task_struct *child)
6883
{
69-
/* nothing to do.. */
84+
ptrace_disable_singlestep(child);
7085
}
7186

7287
long arch_ptrace(struct task_struct *child, long request, long addr, long data)
@@ -76,7 +91,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
7691

7792
switch (request) {
7893
/* when I and D space are separate, these will need to be fixed. */
79-
case PTRACE_PEEKTEXT: /* read word at location addr. */
94+
case PTRACE_PEEKTEXT: /* read word at location addr. */
8095
case PTRACE_PEEKDATA: {
8196
unsigned long tmp;
8297
int copied;
@@ -94,7 +109,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
94109
unsigned long tmp;
95110

96111
ret = -EIO;
97-
if ((addr & 3) || addr < 0 ||
112+
if ((addr & 3) || addr < 0 ||
98113
addr > sizeof(struct user) - 3)
99114
break;
100115

@@ -129,7 +144,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
129144

130145
case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
131146
ret = -EIO;
132-
if ((addr & 3) || addr < 0 ||
147+
if ((addr & 3) || addr < 0 ||
133148
addr > sizeof(struct user) - 3)
134149
break;
135150

@@ -156,21 +171,25 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
156171
set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
157172
else
158173
clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
174+
175+
ptrace_disable_singlestep(child);
176+
159177
child->exit_code = data;
160178
wake_up_process(child);
161179
ret = 0;
162180
break;
163181
}
164182

165183
/*
166-
* make the child exit. Best I can do is send it a sigkill.
167-
* perhaps it should be put in the status that it wants to
184+
* make the child exit. Best I can do is send it a sigkill.
185+
* perhaps it should be put in the status that it wants to
168186
* exit.
169187
*/
170188
case PTRACE_KILL: {
171189
ret = 0;
172190
if (child->exit_state == EXIT_ZOMBIE) /* already dead */
173191
break;
192+
ptrace_disable_singlestep(child);
174193
child->exit_code = SIGKILL;
175194
wake_up_process(child);
176195
break;
@@ -196,6 +215,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
196215
ubc_usercnt += 1;
197216
child->thread.ubc_pc = pc;
198217

218+
set_tsk_thread_flag(child, TIF_SINGLESTEP);
199219
child->exit_code = data;
200220
/* give it a chance to run. */
201221
wake_up_process(child);
@@ -248,14 +268,15 @@ asmlinkage void do_syscall_trace(void)
248268
{
249269
struct task_struct *tsk = current;
250270

251-
if (!test_thread_flag(TIF_SYSCALL_TRACE))
271+
if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
272+
!test_thread_flag(TIF_SINGLESTEP))
252273
return;
253274
if (!(tsk->ptrace & PT_PTRACED))
254275
return;
255276
/* the 0x80 provides a way for the tracing parent to distinguish
256277
between a syscall stop and SIGTRAP delivery */
257-
ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
258-
? 0x80 : 0));
278+
ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
279+
!test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
259280

260281
/*
261282
* this isn't the same as continuing with a signal, but it will do

‎include/asm-sh/thread_info.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static inline struct thread_info *current_thread_info(void)
111111
#define TIF_SIGPENDING 2 /* signal pending */
112112
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
113113
#define TIF_RESTORE_SIGMASK 4 /* restore signal mask in do_signal() */
114+
#define TIF_SINGLESTEP 5 /* singlestepping active */
114115
#define TIF_USEDFPU 16 /* FPU was used by this task this quantum (SMP) */
115116
#define TIF_POLLING_NRFLAG 17 /* true if poll_idle() is polling TIF_NEED_RESCHED */
116117
#define TIF_MEMDIE 18
@@ -121,6 +122,7 @@ static inline struct thread_info *current_thread_info(void)
121122
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
122123
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
123124
#define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK)
125+
#define _TIF_SINGLESTEP (1<<TIF_SINGLESTEP)
124126
#define _TIF_USEDFPU (1<<TIF_USEDFPU)
125127
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
126128
#define _TIF_FREEZE (1<<TIF_FREEZE)

0 commit comments

Comments
 (0)