Skip to content

Commit f413d0d

Browse files
committed
sh: Use a jump call table for debug trap handlers.
This rips out most of the needlessly complicated sh_bios and kgdb trap handling, and forces it all through a common fast dispatch path. As more debug traps are inserted, it's important to keep them in sync for all of the parts, not just SH-3/4. As the SH-2 parts are unable to do traps in the >= 0x40 range, we restrict the debug traps to the 0x30-0x3f range on all parts, and also bump the kgdb breakpoint trap down in to this range (from 0xff to 0x3c) so it's possible to use for nommu. Optionally, this table can be padded out to catch spurious traps for SH-3/4, but we don't do that yet.. Signed-off-by: Paul Mundt <lethal@linux-sh.org>
1 parent ec2f9d1 commit f413d0d

8 files changed

Lines changed: 117 additions & 89 deletions

File tree

‎arch/sh/kernel/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ extra-y := head.o init_task.o vmlinux.lds
66

77
obj-y := process.o signal.o traps.o irq.o \
88
ptrace.o setup.o time.o sys_sh.o semaphore.o \
9-
io.o io_generic.o sh_ksyms.o syscalls.o
9+
io.o io_generic.o sh_ksyms.o syscalls.o \
10+
debugtraps.o
1011

1112
obj-y += cpu/ timers/
1213
obj-$(CONFIG_VSYSCALL) += vsyscall/

‎arch/sh/kernel/cpu/sh2/entry.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ trap_entry:
206206

207207
#if defined(CONFIG_SH_STANDARD_BIOS)
208208
/* Unwind the stack and jmp to the debug entry */
209-
debug_kernel_fw:
209+
ENTRY(sh_bios_handler)
210210
mov r15,r0
211211
add #(22-4)*4-4,r0
212212
ldc.l @r0+,gbr

‎arch/sh/kernel/cpu/sh3/entry.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ call_dae:
173173

174174
#if defined(CONFIG_SH_STANDARD_BIOS)
175175
/* Unwind the stack and jmp to the debug entry */
176-
debug_kernel_fw:
176+
ENTRY(sh_bios_handler)
177177
mov.l @r15+, r0
178178
mov.l @r15+, r1
179179
mov.l @r15+, r2

‎arch/sh/kernel/debugtraps.S‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* arch/sh/kernel/debugtraps.S
3+
*
4+
* Debug trap jump tables for SuperH
5+
*
6+
* Copyright (C) 2006 Paul Mundt
7+
*
8+
* This file is subject to the terms and conditions of the GNU General Public
9+
* License. See the file "COPYING" in the main directory of this archive
10+
* for more details.
11+
*/
12+
#include <linux/sys.h>
13+
#include <linux/linkage.h>
14+
15+
#if !defined(CONFIG_SH_KGDB)
16+
#define kgdb_handle_exception debug_trap_handler
17+
#endif
18+
19+
#if !defined(CONFIG_SH_STANDARD_BIOS)
20+
#define sh_bios_handler debug_trap_handler
21+
#endif
22+
23+
.data
24+
25+
ENTRY(debug_trap_table)
26+
.long debug_trap_handler /* 0x30 */
27+
.long debug_trap_handler /* 0x31 */
28+
.long debug_trap_handler /* 0x32 */
29+
.long debug_trap_handler /* 0x33 */
30+
.long debug_trap_handler /* 0x34 */
31+
.long debug_trap_handler /* 0x35 */
32+
.long debug_trap_handler /* 0x36 */
33+
.long debug_trap_handler /* 0x37 */
34+
.long debug_trap_handler /* 0x38 */
35+
.long debug_trap_handler /* 0x39 */
36+
.long debug_trap_handler /* 0x3a */
37+
.long debug_trap_handler /* 0x3b */
38+
.long kgdb_handle_exception /* 0x3c */
39+
.long debug_trap_handler /* 0x3d */
40+
.long bug_trap_handler /* 0x3e */
41+
.long sh_bios_handler /* 0x3f */

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

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -54,79 +54,24 @@
5454
# define resume_kernel __restore_all
5555
#endif
5656

57-
#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
58-
! Handle kernel debug if either kgdb (SW) or gdb-stub (FW) is present.
59-
! If both are configured, handle the debug traps (breakpoints) in SW,
60-
! but still allow BIOS traps to FW.
61-
62-
.align 2
63-
debug_kernel:
64-
#if defined(CONFIG_SH_STANDARD_BIOS) && defined(CONFIG_SH_KGDB)
65-
/* Force BIOS call to FW (debug_trap put TRA in r8) */
66-
mov r8,r0
67-
shlr2 r0
68-
cmp/eq #0x3f,r0
69-
bt debug_kernel_fw
70-
#endif /* CONFIG_SH_STANDARD_BIOS && CONFIG_SH_KGDB */
71-
72-
debug_enter:
73-
#if defined(CONFIG_SH_KGDB)
74-
/* Jump to kgdb, pass stacked regs as arg */
75-
debug_kernel_sw:
76-
mov.l 3f, r0
77-
jmp @r0
78-
mov r15, r4
79-
.align 2
80-
3: .long kgdb_handle_exception
81-
#endif /* CONFIG_SH_KGDB */
82-
#ifdef CONFIG_SH_STANDARD_BIOS
83-
bra debug_kernel_fw
84-
nop
85-
#endif
86-
#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
87-
88-
.align 2
89-
debug_trap:
90-
#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
91-
mov r8, r0
92-
shlr2 r0
93-
cmp/eq #0x3f, r0 ! sh_bios() trap
94-
bf 1f
95-
#ifdef CONFIG_SH_KGDB
96-
cmp/eq #0xff, r0 ! XXX: KGDB trap, fix for SH-2.
97-
bf 1f
98-
#endif
99-
mov #OFF_SR, r0
100-
mov.l @(r0,r15), r0 ! get status register
101-
shll r0
102-
shll r0 ! kernel space?
103-
bt/s debug_kernel
104-
1:
105-
#endif
106-
mov.l @r15, r0 ! Restore R0 value
107-
mov.l 1f, r8
108-
jmp @r8
109-
nop
11057

11158
.align 2
11259
ENTRY(exception_error)
11360
!
11461
#ifdef CONFIG_TRACE_IRQFLAGS
115-
mov.l 3f, r0
62+
mov.l 2f, r0
11663
jsr @r0
11764
nop
11865
#endif
11966
sti
120-
mov.l 2f, r0
67+
mov.l 1f, r0
12168
jmp @r0
12269
nop
12370

124-
!
12571
.align 2
126-
1: .long break_point_trap_software
127-
2: .long do_exception_error
72+
1: .long do_exception_error
12873
#ifdef CONFIG_TRACE_IRQFLAGS
129-
3: .long trace_hardirqs_on
74+
2: .long trace_hardirqs_on
13075
#endif
13176

13277
.align 2
@@ -330,35 +275,52 @@ __restore_all:
330275
.align 2
331276
1: .long restore_all
332277

333-
.align 2
334-
not_syscall_tra:
335-
bra debug_trap
336-
nop
337-
338278
.align 2
339279
syscall_badsys: ! Bad syscall number
340280
mov #-ENOSYS, r0
341281
bra resume_userspace
342282
mov.l r0, @(OFF_R0,r15) ! Return value
343-
283+
284+
/*
285+
* The main debug trap handler.
286+
*
287+
* r8=TRA (not the trap number!)
288+
*
289+
* Note: This assumes that the trapa value is left in its original
290+
* form (without the shlr2 shift) so the calculation for the jump
291+
* call table offset remains a simple in place mask.
292+
*/
293+
debug_trap:
294+
mov r8, r0
295+
and #(0xf << 2), r0
296+
mov.l 1f, r8
297+
add r0, r8
298+
mov.l @r8, r8
299+
jmp @r8
300+
nop
301+
302+
.align 2
303+
1: .long debug_trap_table
344304

345305
/*
346306
* Syscall interface:
347307
*
348308
* Syscall #: R3
349309
* Arguments #0 to #3: R4--R7
350310
* Arguments #4 to #6: R0, R1, R2
351-
* TRA: (number of arguments + 0x10) x 4
311+
* TRA: (number of arguments + ABI revision) x 4
352312
*
353313
* This code also handles delegating other traps to the BIOS/gdb stub
354314
* according to:
355315
*
356316
* Trap number
357-
* (TRA>>2) Purpose
358-
* -------- -------
359-
* 0x0-0xf old syscall ABI
360-
* 0x10-0x1f new syscall ABI
361-
* 0x20-0xff delegated through debug_trap to BIOS/gdb stub.
317+
* (TRA>>2) Purpose
318+
* -------- -------
319+
* 0x00-0x0f original SH-3/4 syscall ABI (not in general use).
320+
* 0x10-0x1f general SH-3/4 syscall ABI.
321+
* 0x20-0x2f syscall ABI for SH-2 parts.
322+
* 0x30-0x3f debug traps used by the kernel.
323+
* 0x40-0xff Not supported by all parts, so left unhandled.
362324
*
363325
* Note: When we're first called, the TRA value must be shifted
364326
* right 2 bits in order to get the value that was used as the "trapa"
@@ -375,17 +337,22 @@ ret_from_fork:
375337
nop
376338
.align 2
377339
1: .long schedule_tail
378-
!
340+
341+
/*
342+
* The poorly named main trapa decode and dispatch routine, for
343+
* system calls and debug traps through their respective jump tables.
344+
*/
379345
ENTRY(system_call)
380346
#if !defined(CONFIG_CPU_SH2)
381347
mov.l 1f, r9
382348
mov.l @r9, r8 ! Read from TRA (Trap Address) Register
383349
#endif
384-
!
385-
! Is the trap argument >= 0x20? (TRA will be >= 0x80)
386-
mov #0x7f, r9
350+
/*
351+
* Check the trap type
352+
*/
353+
mov #((0x20 << 2) - 1), r9
387354
cmp/hi r9, r8
388-
bt/s not_syscall_tra
355+
bt/s debug_trap ! it's a debug trap..
389356
mov #OFF_TRA, r9
390357
add r15, r9
391358
mov.l r8, @r9 ! set TRA value to tra

‎arch/sh/kernel/kgdb_stub.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,8 +1323,11 @@ static void kgdb_command_loop(const int excep_code, const int trapa_value)
13231323
}
13241324

13251325
/* There has been an exception, most likely a breakpoint. */
1326-
void kgdb_handle_exception(struct pt_regs *regs)
1326+
asmlinkage void kgdb_handle_exception(unsigned long r4, unsigned long r5,
1327+
unsigned long r6, unsigned long r7,
1328+
struct pt_regs __regs)
13271329
{
1330+
struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
13281331
int excep_code, vbr_val;
13291332
int count;
13301333
int trapa_value = ctrl_inl(TRA);
@@ -1368,8 +1371,6 @@ void kgdb_handle_exception(struct pt_regs *regs)
13681371

13691372
vbr_val = trap_registers.vbr;
13701373
asm("ldc %0, vbr": :"r"(vbr_val));
1371-
1372-
return;
13731374
}
13741375

13751376
/* Trigger a breakpoint by function */

‎arch/sh/kernel/process.c‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,27 @@ asmlinkage void break_point_trap(void)
493493
force_sig(SIGTRAP, current);
494494
}
495495

496-
asmlinkage void break_point_trap_software(unsigned long r4, unsigned long r5,
497-
unsigned long r6, unsigned long r7,
498-
struct pt_regs __regs)
496+
/*
497+
* Generic trap handler.
498+
*/
499+
asmlinkage void debug_trap_handler(unsigned long r4, unsigned long r5,
500+
unsigned long r6, unsigned long r7,
501+
struct pt_regs __regs)
502+
{
503+
struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
504+
505+
/* Rewind */
506+
regs->pc -= 2;
507+
508+
force_sig(SIGTRAP, current);
509+
}
510+
511+
/*
512+
* Special handler for BUG() traps.
513+
*/
514+
asmlinkage void bug_trap_handler(unsigned long r4, unsigned long r5,
515+
unsigned long r6, unsigned long r7,
516+
struct pt_regs __regs)
499517
{
500518
struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
501519

‎include/asm-sh/kgdb.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ extern int setjmp(jmp_buf __jmpb);
8585
#define KGDB_PRINTK(...) printk("KGDB: " __VA_ARGS__)
8686

8787
/* Forced breakpoint */
88-
#define BREAKPOINT() do { \
89-
if (kgdb_enabled) { \
90-
asm volatile("trapa #0xff"); \
91-
} \
88+
#define BREAKPOINT() \
89+
do { \
90+
if (kgdb_enabled) \
91+
__asm__ __volatile__("trapa #0x3c"); \
9292
} while (0)
9393

9494
/* KGDB should be able to flush all kernel text space */

0 commit comments

Comments
 (0)