^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * arch/arm/kernel/kgdb.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * ARM KGDB support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2002-2004 MontaVista Software, Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2008 Wind River Systems, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Authors: George Davis <davis_g@mvista.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Deepak Saxena <dsaxena@plexity.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kgdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/patch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/traps.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) { "f0", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { "f1", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) { "f2", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { "f3", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) { "f4", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) { "f5", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { "f6", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) { "f7", 12, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) { "fps", 4, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (regno >= DBG_MAX_REG_NUM || regno < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (dbg_reg_def[regno].offset != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) memset(mem, 0, dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return dbg_reg_def[regno].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (regno >= DBG_MAX_REG_NUM || regno < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (dbg_reg_def[regno].offset != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct thread_info *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int regno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Just making sure... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (task == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Initialize to zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for (regno = 0; regno < GDB_MAX_REGS; regno++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) gdb_regs[regno] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Otherwise, we have only some registers from switch_to() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ti = task_thread_info(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) gdb_regs[_R4] = ti->cpu_context.r4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) gdb_regs[_R5] = ti->cpu_context.r5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) gdb_regs[_R6] = ti->cpu_context.r6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) gdb_regs[_R7] = ti->cpu_context.r7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) gdb_regs[_R8] = ti->cpu_context.r8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) gdb_regs[_R9] = ti->cpu_context.r9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) gdb_regs[_R10] = ti->cpu_context.sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) gdb_regs[_FP] = ti->cpu_context.fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) gdb_regs[_SPT] = ti->cpu_context.sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) gdb_regs[_PC] = ti->cpu_context.pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) regs->ARM_pc = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int compiled_break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int kgdb_arch_handle_exception(int exception_vector, int signo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int err_code, char *remcom_in_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) char *remcom_out_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct pt_regs *linux_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) switch (remcom_in_buffer[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) case 'D':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Try to read optional parameter, pc unchanged if no parm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * If this was a compiled breakpoint, we need to move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * to the next instruction or we will just breakpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * over and over again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ptr = &remcom_in_buffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (kgdb_hex2long(&ptr, &addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) linux_regs->ARM_pc = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) else if (compiled_break == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) linux_regs->ARM_pc += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) compiled_break = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kgdb_handle_exception(1, SIGTRAP, 0, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) compiled_break = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) kgdb_handle_exception(1, SIGTRAP, 0, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct undef_hook kgdb_brkpt_arm_hook = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .instr_mask = 0xffffffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .instr_val = KGDB_BREAKINST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .cpsr_mask = PSR_T_BIT | MODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .cpsr_val = SVC_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .fn = kgdb_brk_fn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static struct undef_hook kgdb_brkpt_thumb_hook = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .instr_mask = 0xffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .instr_val = KGDB_BREAKINST & 0xffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .cpsr_mask = PSR_T_BIT | MODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .cpsr_val = PSR_T_BIT | SVC_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .fn = kgdb_brk_fn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static struct undef_hook kgdb_compiled_brkpt_arm_hook = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .instr_mask = 0xffffffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .instr_val = KGDB_COMPILED_BREAK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .cpsr_mask = PSR_T_BIT | MODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .cpsr_val = SVC_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .fn = kgdb_compiled_brk_fn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static struct undef_hook kgdb_compiled_brkpt_thumb_hook = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .instr_mask = 0xffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .instr_val = KGDB_COMPILED_BREAK & 0xffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .cpsr_mask = PSR_T_BIT | MODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .cpsr_val = PSR_T_BIT | SVC_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .fn = kgdb_compiled_brk_fn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int __kgdb_notify(struct die_args *args, unsigned long cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct pt_regs *regs = args->regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (kgdb_handle_exception(1, args->signr, cmd, regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return NOTIFY_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ret = __kgdb_notify(ptr, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static struct notifier_block kgdb_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .notifier_call = kgdb_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .priority = -INT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * kgdb_arch_init - Perform any architecture specific initalization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * This function will handle the initalization of any architecture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * specific callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int kgdb_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int ret = register_die_notifier(&kgdb_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) register_undef_hook(&kgdb_brkpt_arm_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) register_undef_hook(&kgdb_brkpt_thumb_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) register_undef_hook(&kgdb_compiled_brkpt_arm_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) register_undef_hook(&kgdb_compiled_brkpt_thumb_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * kgdb_arch_exit - Perform any architecture specific uninitalization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * This function will handle the uninitalization of any architecture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * specific callbacks, for dynamic registration and unregistration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) void kgdb_arch_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) unregister_undef_hook(&kgdb_brkpt_arm_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) unregister_undef_hook(&kgdb_brkpt_thumb_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) unregister_undef_hook(&kgdb_compiled_brkpt_arm_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) unregister_undef_hook(&kgdb_compiled_brkpt_thumb_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) unregister_die_notifier(&kgdb_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* patch_text() only supports int-sized breakpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Machine is already stopped, so we can use __patch_text() directly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) __patch_text((void *)bpt->bpt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Machine is already stopped, so we can use __patch_text() directly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * Register our undef instruction hooks with ARM undef core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * We register a hook specifically looking for the KGB break inst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * and we handle the normal undef case within the do_undefinstr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) const struct kgdb_arch arch_kgdb_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #ifndef __ARMEB__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) #else /* ! __ARMEB__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) };