Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/kgdb: add kgdb_arch_set/remove_breakpoint()

Generic implementation fails to remove breakpoints after init
when CONFIG_STRICT_KERNEL_RWX is selected:

[ 13.251285] KGDB: BP remove failed: c001c338
[ 13.259587] kgdbts: ERROR PUT: end of test buffer on 'do_fork_test' line 8 expected OK got $E14#aa
[ 13.268969] KGDB: re-enter exception: ALL breakpoints killed
[ 13.275099] CPU: 0 PID: 1 Comm: init Not tainted 4.18.0-g82bbb913ffd8 #860
[ 13.282836] Call Trace:
[ 13.285313] [c60e1ba0] [c0080ef0] kgdb_handle_exception+0x6f4/0x720 (unreliable)
[ 13.292618] [c60e1c30] [c000e97c] kgdb_handle_breakpoint+0x3c/0x98
[ 13.298709] [c60e1c40] [c000af54] program_check_exception+0x104/0x700
[ 13.305083] [c60e1c60] [c000e45c] ret_from_except_full+0x0/0x4
[ 13.310845] [c60e1d20] [c02a22ac] run_simple_test+0x2b4/0x2d4
[ 13.316532] [c60e1d30] [c0081698] put_packet+0xb8/0x158
[ 13.321694] [c60e1d60] [c00820b4] gdb_serial_stub+0x230/0xc4c
[ 13.327374] [c60e1dc0] [c0080af8] kgdb_handle_exception+0x2fc/0x720
[ 13.333573] [c60e1e50] [c000e928] kgdb_singlestep+0xb4/0xcc
[ 13.339068] [c60e1e70] [c000ae1c] single_step_exception+0x90/0xac
[ 13.345100] [c60e1e80] [c000e45c] ret_from_except_full+0x0/0x4
[ 13.350865] [c60e1f40] [c000e11c] ret_from_syscall+0x0/0x38
[ 13.356346] Kernel panic - not syncing: Recursive entry to debugger

This patch creates powerpc specific version of
kgdb_arch_set_breakpoint() and kgdb_arch_remove_breakpoint()
using patch_instruction()

Fixes: 1e0fc9d1eb2b ("powerpc/Kconfig: Enable STRICT_KERNEL_RWX for some configs")
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Christophe Leroy and committed by
Michael Ellerman
fb978ca2 6beb3381

+39 -9
+4 -1
arch/powerpc/include/asm/kgdb.h
··· 26 26 #define BREAK_INSTR_SIZE 4 27 27 #define BUFMAX ((NUMREGBYTES * 2) + 512) 28 28 #define OUTBUFMAX ((NUMREGBYTES * 2) + 512) 29 + 30 + #define BREAK_INSTR 0x7d821008 /* twge r2, r2 */ 31 + 29 32 static inline void arch_kgdb_breakpoint(void) 30 33 { 31 - asm(".long 0x7d821008"); /* twge r2, r2 */ 34 + asm(stringify_in_c(.long BREAK_INSTR)); 32 35 } 33 36 #define CACHE_FLUSH_IS_SAFE 1 34 37 #define DBG_MAX_REG_NUM 70
+35 -8
arch/powerpc/kernel/kgdb.c
··· 24 24 #include <asm/processor.h> 25 25 #include <asm/machdep.h> 26 26 #include <asm/debug.h> 27 + #include <asm/code-patching.h> 27 28 #include <linux/slab.h> 28 29 29 30 /* ··· 145 144 if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0) 146 145 return 0; 147 146 148 - if (*(u32 *) (regs->nip) == *(u32 *) (&arch_kgdb_ops.gdb_bpt_instr)) 147 + if (*(u32 *)regs->nip == BREAK_INSTR) 149 148 regs->nip += BREAK_INSTR_SIZE; 150 149 151 150 return 1; ··· 442 441 return -1; 443 442 } 444 443 444 + int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) 445 + { 446 + int err; 447 + unsigned int instr; 448 + unsigned int *addr = (unsigned int *)bpt->bpt_addr; 449 + 450 + err = probe_kernel_address(addr, instr); 451 + if (err) 452 + return err; 453 + 454 + err = patch_instruction(addr, BREAK_INSTR); 455 + if (err) 456 + return -EFAULT; 457 + 458 + *(unsigned int *)bpt->saved_instr = instr; 459 + 460 + return 0; 461 + } 462 + 463 + int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt) 464 + { 465 + int err; 466 + unsigned int instr = *(unsigned int *)bpt->saved_instr; 467 + unsigned int *addr = (unsigned int *)bpt->bpt_addr; 468 + 469 + err = patch_instruction(addr, instr); 470 + if (err) 471 + return -EFAULT; 472 + 473 + return 0; 474 + } 475 + 445 476 /* 446 477 * Global data 447 478 */ 448 - struct kgdb_arch arch_kgdb_ops = { 449 - #ifdef __LITTLE_ENDIAN__ 450 - .gdb_bpt_instr = {0x08, 0x10, 0x82, 0x7d}, 451 - #else 452 - .gdb_bpt_instr = {0x7d, 0x82, 0x10, 0x08}, 453 - #endif 454 - }; 479 + struct kgdb_arch arch_kgdb_ops; 455 480 456 481 static int kgdb_not_implemented(struct pt_regs *regs) 457 482 {