Linux 4.1.18
[linux/fpc-iii.git] / arch / arm / kernel / kgdb.c
blobfd9eefce0a7b8d66e019b048e4ee24753da7acc0
1 /*
2 * arch/arm/kernel/kgdb.c
4 * ARM KGDB support
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)},
38 { "f0", 12, -1 },
39 { "f1", 12, -1 },
40 { "f2", 12, -1 },
41 { "f3", 12, -1 },
42 { "f4", 12, -1 },
43 { "f5", 12, -1 },
44 { "f6", 12, -1 },
45 { "f7", 12, -1 },
46 { "fps", 4, -1 },
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)
53 return NULL;
55 if (dbg_reg_def[regno].offset != -1)
56 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
57 dbg_reg_def[regno].size);
58 else
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)
66 return -EINVAL;
68 if (dbg_reg_def[regno].offset != -1)
69 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
70 dbg_reg_def[regno].size);
71 return 0;
74 void
75 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
77 struct pt_regs *thread_regs;
78 int regno;
80 /* Just making sure... */
81 if (task == NULL)
82 return;
84 /* Initialize to zero */
85 for (regno = 0; regno < GDB_MAX_REGS; regno++)
86 gdb_regs[regno] = 0;
88 /* Otherwise, we have only some registers from switch_to() */
89 thread_regs = task_pt_regs(task);
90 gdb_regs[_R0] = thread_regs->ARM_r0;
91 gdb_regs[_R1] = thread_regs->ARM_r1;
92 gdb_regs[_R2] = thread_regs->ARM_r2;
93 gdb_regs[_R3] = thread_regs->ARM_r3;
94 gdb_regs[_R4] = thread_regs->ARM_r4;
95 gdb_regs[_R5] = thread_regs->ARM_r5;
96 gdb_regs[_R6] = thread_regs->ARM_r6;
97 gdb_regs[_R7] = thread_regs->ARM_r7;
98 gdb_regs[_R8] = thread_regs->ARM_r8;
99 gdb_regs[_R9] = thread_regs->ARM_r9;
100 gdb_regs[_R10] = thread_regs->ARM_r10;
101 gdb_regs[_FP] = thread_regs->ARM_fp;
102 gdb_regs[_IP] = thread_regs->ARM_ip;
103 gdb_regs[_SPT] = thread_regs->ARM_sp;
104 gdb_regs[_LR] = thread_regs->ARM_lr;
105 gdb_regs[_PC] = thread_regs->ARM_pc;
106 gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
109 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
111 regs->ARM_pc = pc;
114 static int compiled_break;
116 int kgdb_arch_handle_exception(int exception_vector, int signo,
117 int err_code, char *remcom_in_buffer,
118 char *remcom_out_buffer,
119 struct pt_regs *linux_regs)
121 unsigned long addr;
122 char *ptr;
124 switch (remcom_in_buffer[0]) {
125 case 'D':
126 case 'k':
127 case 'c':
129 * Try to read optional parameter, pc unchanged if no parm.
130 * If this was a compiled breakpoint, we need to move
131 * to the next instruction or we will just breakpoint
132 * over and over again.
134 ptr = &remcom_in_buffer[1];
135 if (kgdb_hex2long(&ptr, &addr))
136 linux_regs->ARM_pc = addr;
137 else if (compiled_break == 1)
138 linux_regs->ARM_pc += 4;
140 compiled_break = 0;
142 return 0;
145 return -1;
148 static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
150 kgdb_handle_exception(1, SIGTRAP, 0, regs);
152 return 0;
155 static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
157 compiled_break = 1;
158 kgdb_handle_exception(1, SIGTRAP, 0, regs);
160 return 0;
163 static struct undef_hook kgdb_brkpt_hook = {
164 .instr_mask = 0xffffffff,
165 .instr_val = KGDB_BREAKINST,
166 .cpsr_mask = MODE_MASK,
167 .cpsr_val = SVC_MODE,
168 .fn = kgdb_brk_fn
171 static struct undef_hook kgdb_compiled_brkpt_hook = {
172 .instr_mask = 0xffffffff,
173 .instr_val = KGDB_COMPILED_BREAK,
174 .cpsr_mask = MODE_MASK,
175 .cpsr_val = SVC_MODE,
176 .fn = kgdb_compiled_brk_fn
179 static void kgdb_call_nmi_hook(void *ignored)
181 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
184 void kgdb_roundup_cpus(unsigned long flags)
186 local_irq_enable();
187 smp_call_function(kgdb_call_nmi_hook, NULL, 0);
188 local_irq_disable();
191 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
193 struct pt_regs *regs = args->regs;
195 if (kgdb_handle_exception(1, args->signr, cmd, regs))
196 return NOTIFY_DONE;
197 return NOTIFY_STOP;
199 static int
200 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
202 unsigned long flags;
203 int ret;
205 local_irq_save(flags);
206 ret = __kgdb_notify(ptr, cmd);
207 local_irq_restore(flags);
209 return ret;
212 static struct notifier_block kgdb_notifier = {
213 .notifier_call = kgdb_notify,
214 .priority = -INT_MAX,
219 * kgdb_arch_init - Perform any architecture specific initalization.
221 * This function will handle the initalization of any architecture
222 * specific callbacks.
224 int kgdb_arch_init(void)
226 int ret = register_die_notifier(&kgdb_notifier);
228 if (ret != 0)
229 return ret;
231 register_undef_hook(&kgdb_brkpt_hook);
232 register_undef_hook(&kgdb_compiled_brkpt_hook);
234 return 0;
238 * kgdb_arch_exit - Perform any architecture specific uninitalization.
240 * This function will handle the uninitalization of any architecture
241 * specific callbacks, for dynamic registration and unregistration.
243 void kgdb_arch_exit(void)
245 unregister_undef_hook(&kgdb_brkpt_hook);
246 unregister_undef_hook(&kgdb_compiled_brkpt_hook);
247 unregister_die_notifier(&kgdb_notifier);
250 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
252 int err;
254 /* patch_text() only supports int-sized breakpoints */
255 BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
257 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
258 BREAK_INSTR_SIZE);
259 if (err)
260 return err;
262 /* Machine is already stopped, so we can use __patch_text() directly */
263 __patch_text((void *)bpt->bpt_addr,
264 *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
266 return err;
269 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
271 /* Machine is already stopped, so we can use __patch_text() directly */
272 __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
274 return 0;
278 * Register our undef instruction hooks with ARM undef core.
279 * We regsiter a hook specifically looking for the KGB break inst
280 * and we handle the normal undef case within the do_undefinstr
281 * handler.
283 struct kgdb_arch arch_kgdb_ops = {
284 #ifndef __ARMEB__
285 .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
286 #else /* ! __ARMEB__ */
287 .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
288 #endif