blob: f3c662299531351d62c7e4081c6d3158778d5e05 [file] [log] [blame]
Jason Wessel5cbad0e2008-02-20 13:33:40 -06001/*
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 */
Will Deacon5d8614c2010-03-12 11:03:58 +010012#include <linux/irq.h>
Jason Wessel62a03092010-08-05 09:22:22 -050013#include <linux/kdebug.h>
Jason Wessel5cbad0e2008-02-20 13:33:40 -060014#include <linux/kgdb.h>
Doug Anderson23a4e402014-04-22 15:14:51 -070015#include <linux/uaccess.h>
16
Wang Nanfca08f32015-01-09 10:19:49 +080017#include <asm/patch.h>
Jason Wessel5cbad0e2008-02-20 13:33:40 -060018#include <asm/traps.h>
19
Jason Wessel22eeef42010-08-05 09:22:21 -050020struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
Jason Wessel5cbad0e2008-02-20 13:33:40 -060021{
Jason Wessel22eeef42010-08-05 09:22:21 -050022 { "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)},
48};
Jason Wessel5cbad0e2008-02-20 13:33:40 -060049
Jason Wessel22eeef42010-08-05 09:22:21 -050050char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
51{
52 if (regno >= DBG_MAX_REG_NUM || regno < 0)
53 return NULL;
Jason Wessel5cbad0e2008-02-20 13:33:40 -060054
Jason Wessel22eeef42010-08-05 09:22:21 -050055 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;
Jason Wessel5cbad0e2008-02-20 13:33:40 -060061}
62
Jason Wessel22eeef42010-08-05 09:22:21 -050063int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
Jason Wessel5cbad0e2008-02-20 13:33:40 -060064{
Jason Wessel22eeef42010-08-05 09:22:21 -050065 if (regno >= DBG_MAX_REG_NUM || regno < 0)
66 return -EINVAL;
67
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;
Jason Wessel5cbad0e2008-02-20 13:33:40 -060072}
73
74void
75sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
76{
Doug Anderson001bf452015-09-02 03:39:19 +010077 struct thread_info *ti;
Jason Wessel5cbad0e2008-02-20 13:33:40 -060078 int regno;
79
80 /* Just making sure... */
81 if (task == NULL)
82 return;
83
84 /* Initialize to zero */
Rabin Vincent834b2962010-10-26 12:49:00 -050085 for (regno = 0; regno < GDB_MAX_REGS; regno++)
Jason Wessel5cbad0e2008-02-20 13:33:40 -060086 gdb_regs[regno] = 0;
87
88 /* Otherwise, we have only some registers from switch_to() */
Doug Anderson001bf452015-09-02 03:39:19 +010089 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;
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600100}
101
Jason Wesseldcc78712010-05-20 21:04:21 -0500102void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
103{
104 regs->ARM_pc = pc;
105}
106
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600107static int compiled_break;
108
109int 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)
113{
114 unsigned long addr;
115 char *ptr;
116
117 switch (remcom_in_buffer[0]) {
118 case 'D':
119 case 'k':
120 case 'c':
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600121 /*
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.
126 */
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;
132
133 compiled_break = 0;
134
135 return 0;
136 }
137
138 return -1;
139}
140
141static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
142{
Todd Poynordad56472013-07-03 15:48:04 -0700143 if (user_mode(regs))
144 return -1;
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600145 kgdb_handle_exception(1, SIGTRAP, 0, regs);
146
147 return 0;
148}
149
150static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
151{
Todd Poynordad56472013-07-03 15:48:04 -0700152 if (user_mode(regs))
153 return -1;
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600154 compiled_break = 1;
155 kgdb_handle_exception(1, SIGTRAP, 0, regs);
156
157 return 0;
158}
159
160static struct undef_hook kgdb_brkpt_hook = {
161 .instr_mask = 0xffffffff,
162 .instr_val = KGDB_BREAKINST,
Omar Sandoval6bf755d2014-08-01 18:14:06 +0100163 .cpsr_mask = MODE_MASK,
164 .cpsr_val = SVC_MODE,
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600165 .fn = kgdb_brk_fn
166};
167
168static struct undef_hook kgdb_compiled_brkpt_hook = {
169 .instr_mask = 0xffffffff,
170 .instr_val = KGDB_COMPILED_BREAK,
Omar Sandoval6bf755d2014-08-01 18:14:06 +0100171 .cpsr_mask = MODE_MASK,
172 .cpsr_val = SVC_MODE,
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600173 .fn = kgdb_compiled_brk_fn
174};
175
Will Deacon5d8614c2010-03-12 11:03:58 +0100176static void kgdb_call_nmi_hook(void *ignored)
177{
178 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
179}
180
181void kgdb_roundup_cpus(unsigned long flags)
182{
183 local_irq_enable();
184 smp_call_function(kgdb_call_nmi_hook, NULL, 0);
185 local_irq_disable();
186}
187
Jason Wessel62a03092010-08-05 09:22:22 -0500188static int __kgdb_notify(struct die_args *args, unsigned long cmd)
189{
190 struct pt_regs *regs = args->regs;
191
192 if (kgdb_handle_exception(1, args->signr, cmd, regs))
193 return NOTIFY_DONE;
194 return NOTIFY_STOP;
195}
196static int
197kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
198{
199 unsigned long flags;
200 int ret;
201
202 local_irq_save(flags);
203 ret = __kgdb_notify(ptr, cmd);
204 local_irq_restore(flags);
205
206 return ret;
207}
208
209static struct notifier_block kgdb_notifier = {
210 .notifier_call = kgdb_notify,
211 .priority = -INT_MAX,
212};
213
214
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600215/**
216 * kgdb_arch_init - Perform any architecture specific initalization.
217 *
218 * This function will handle the initalization of any architecture
219 * specific callbacks.
220 */
221int kgdb_arch_init(void)
222{
Jason Wessel62a03092010-08-05 09:22:22 -0500223 int ret = register_die_notifier(&kgdb_notifier);
224
225 if (ret != 0)
226 return ret;
227
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600228 register_undef_hook(&kgdb_brkpt_hook);
229 register_undef_hook(&kgdb_compiled_brkpt_hook);
230
231 return 0;
232}
233
234/**
235 * kgdb_arch_exit - Perform any architecture specific uninitalization.
236 *
237 * This function will handle the uninitalization of any architecture
238 * specific callbacks, for dynamic registration and unregistration.
239 */
240void kgdb_arch_exit(void)
241{
242 unregister_undef_hook(&kgdb_brkpt_hook);
243 unregister_undef_hook(&kgdb_compiled_brkpt_hook);
Jason Wessel62a03092010-08-05 09:22:22 -0500244 unregister_die_notifier(&kgdb_notifier);
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600245}
246
Doug Anderson23a4e402014-04-22 15:14:51 -0700247int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
248{
249 int err;
250
251 /* patch_text() only supports int-sized breakpoints */
252 BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
253
254 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
255 BREAK_INSTR_SIZE);
256 if (err)
257 return err;
258
Doug Anderson7ae85dc2015-08-26 18:26:49 +0100259 /* Machine is already stopped, so we can use __patch_text() directly */
260 __patch_text((void *)bpt->bpt_addr,
261 *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
Doug Anderson23a4e402014-04-22 15:14:51 -0700262
263 return err;
264}
265
266int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
267{
Doug Anderson7ae85dc2015-08-26 18:26:49 +0100268 /* Machine is already stopped, so we can use __patch_text() directly */
269 __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
Doug Anderson23a4e402014-04-22 15:14:51 -0700270
271 return 0;
272}
273
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600274/*
275 * Register our undef instruction hooks with ARM undef core.
276 * We regsiter a hook specifically looking for the KGB break inst
277 * and we handle the normal undef case within the do_undefinstr
278 * handler.
279 */
280struct kgdb_arch arch_kgdb_ops = {
281#ifndef __ARMEB__
282 .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
283#else /* ! __ARMEB__ */
284 .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
285#endif
286};