2 * arch/arm/kernel/kgdb.c
6 * Copyright (c) 2002-2004 MontaVista Software, Inc
7 * Copyright (c) 2008 Wind River Systems, Inc.
9 * Authors: George Davis <davis_g@mvista.com>
10 * Deepak Saxena <dsaxena@plexity.net>
12 #include <linux/irq.h>
13 #include <linux/kdebug.h>
14 #include <linux/kgdb.h>
15 #include <linux/uaccess.h>
17 #include <asm/patch.h>
18 #include <asm/traps.h>
20 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
22 { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
23 { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
24 { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
25 { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
26 { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
27 { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
28 { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
29 { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
30 { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
31 { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
32 { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
33 { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
34 { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
35 { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
36 { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
37 { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
47 { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
50 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
52 if (regno >= DBG_MAX_REG_NUM || regno < 0)
55 if (dbg_reg_def[regno].offset != -1)
56 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
57 dbg_reg_def[regno].size);
59 memset(mem, 0, dbg_reg_def[regno].size);
60 return dbg_reg_def[regno].name;
63 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
65 if (regno >= DBG_MAX_REG_NUM || regno < 0)
68 if (dbg_reg_def[regno].offset != -1)
69 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
70 dbg_reg_def[regno].size);
75 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
77 struct thread_info *ti;
80 /* Just making sure... */
84 /* Initialize to zero */
85 for (regno = 0; regno < GDB_MAX_REGS; regno++)
88 /* Otherwise, we have only some registers from switch_to() */
89 ti = task_thread_info(task);
90 gdb_regs[_R4] = ti->cpu_context.r4;
91 gdb_regs[_R5] = ti->cpu_context.r5;
92 gdb_regs[_R6] = ti->cpu_context.r6;
93 gdb_regs[_R7] = ti->cpu_context.r7;
94 gdb_regs[_R8] = ti->cpu_context.r8;
95 gdb_regs[_R9] = ti->cpu_context.r9;
96 gdb_regs[_R10] = ti->cpu_context.sl;
97 gdb_regs[_FP] = ti->cpu_context.fp;
98 gdb_regs[_SPT] = ti->cpu_context.sp;
99 gdb_regs[_PC] = ti->cpu_context.pc;
102 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
107 static int compiled_break;
109 int kgdb_arch_handle_exception(int exception_vector, int signo,
110 int err_code, char *remcom_in_buffer,
111 char *remcom_out_buffer,
112 struct pt_regs *linux_regs)
117 switch (remcom_in_buffer[0]) {
122 * Try to read optional parameter, pc unchanged if no parm.
123 * If this was a compiled breakpoint, we need to move
124 * to the next instruction or we will just breakpoint
125 * over and over again.
127 ptr = &remcom_in_buffer[1];
128 if (kgdb_hex2long(&ptr, &addr))
129 linux_regs->ARM_pc = addr;
130 else if (compiled_break == 1)
131 linux_regs->ARM_pc += 4;
141 static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
143 kgdb_handle_exception(1, SIGTRAP, 0, regs);
148 static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
151 kgdb_handle_exception(1, SIGTRAP, 0, regs);
156 static struct undef_hook kgdb_brkpt_hook = {
157 .instr_mask = 0xffffffff,
158 .instr_val = KGDB_BREAKINST,
159 .cpsr_mask = MODE_MASK,
160 .cpsr_val = SVC_MODE,
164 static struct undef_hook kgdb_compiled_brkpt_hook = {
165 .instr_mask = 0xffffffff,
166 .instr_val = KGDB_COMPILED_BREAK,
167 .cpsr_mask = MODE_MASK,
168 .cpsr_val = SVC_MODE,
169 .fn = kgdb_compiled_brk_fn
172 static void kgdb_call_nmi_hook(void *ignored)
174 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
177 void kgdb_roundup_cpus(unsigned long flags)
180 smp_call_function(kgdb_call_nmi_hook, NULL, 0);
184 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
186 struct pt_regs *regs = args->regs;
188 if (kgdb_handle_exception(1, args->signr, cmd, regs))
193 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
198 local_irq_save(flags);
199 ret = __kgdb_notify(ptr, cmd);
200 local_irq_restore(flags);
205 static struct notifier_block kgdb_notifier = {
206 .notifier_call = kgdb_notify,
207 .priority = -INT_MAX,
212 * kgdb_arch_init - Perform any architecture specific initalization.
214 * This function will handle the initalization of any architecture
215 * specific callbacks.
217 int kgdb_arch_init(void)
219 int ret = register_die_notifier(&kgdb_notifier);
224 register_undef_hook(&kgdb_brkpt_hook);
225 register_undef_hook(&kgdb_compiled_brkpt_hook);
231 * kgdb_arch_exit - Perform any architecture specific uninitalization.
233 * This function will handle the uninitalization of any architecture
234 * specific callbacks, for dynamic registration and unregistration.
236 void kgdb_arch_exit(void)
238 unregister_undef_hook(&kgdb_brkpt_hook);
239 unregister_undef_hook(&kgdb_compiled_brkpt_hook);
240 unregister_die_notifier(&kgdb_notifier);
243 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
247 /* patch_text() only supports int-sized breakpoints */
248 BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
250 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
255 /* Machine is already stopped, so we can use __patch_text() directly */
256 __patch_text((void *)bpt->bpt_addr,
257 *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
262 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
264 /* Machine is already stopped, so we can use __patch_text() directly */
265 __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
271 * Register our undef instruction hooks with ARM undef core.
272 * We regsiter a hook specifically looking for the KGB break inst
273 * and we handle the normal undef case within the do_undefinstr
276 struct kgdb_arch arch_kgdb_ops = {
278 .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
279 #else /* ! __ARMEB__ */
280 .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}