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