blob: cf846cade3542077bebff825bfbff581901f4ad0 [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 Wessel5cbad0e2008-02-20 13:33:40 -060013#include <linux/kgdb.h>
14#include <asm/traps.h>
15
Jason Wessel22eeef42010-08-05 09:22:21 -050016struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
Jason Wessel5cbad0e2008-02-20 13:33:40 -060017{
Jason Wessel22eeef42010-08-05 09:22:21 -050018 { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
19 { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
20 { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
21 { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
22 { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
23 { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
24 { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
25 { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
26 { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
27 { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
28 { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
29 { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
30 { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
31 { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
32 { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
33 { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
34 { "f0", 12, -1 },
35 { "f1", 12, -1 },
36 { "f2", 12, -1 },
37 { "f3", 12, -1 },
38 { "f4", 12, -1 },
39 { "f5", 12, -1 },
40 { "f6", 12, -1 },
41 { "f7", 12, -1 },
42 { "fps", 4, -1 },
43 { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
44};
Jason Wessel5cbad0e2008-02-20 13:33:40 -060045
Jason Wessel22eeef42010-08-05 09:22:21 -050046char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
47{
48 if (regno >= DBG_MAX_REG_NUM || regno < 0)
49 return NULL;
Jason Wessel5cbad0e2008-02-20 13:33:40 -060050
Jason Wessel22eeef42010-08-05 09:22:21 -050051 if (dbg_reg_def[regno].offset != -1)
52 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
53 dbg_reg_def[regno].size);
54 else
55 memset(mem, 0, dbg_reg_def[regno].size);
56 return dbg_reg_def[regno].name;
Jason Wessel5cbad0e2008-02-20 13:33:40 -060057}
58
Jason Wessel22eeef42010-08-05 09:22:21 -050059int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
Jason Wessel5cbad0e2008-02-20 13:33:40 -060060{
Jason Wessel22eeef42010-08-05 09:22:21 -050061 if (regno >= DBG_MAX_REG_NUM || regno < 0)
62 return -EINVAL;
63
64 if (dbg_reg_def[regno].offset != -1)
65 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
66 dbg_reg_def[regno].size);
67 return 0;
Jason Wessel5cbad0e2008-02-20 13:33:40 -060068}
69
70void
71sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
72{
73 struct pt_regs *thread_regs;
74 int regno;
75
76 /* Just making sure... */
77 if (task == NULL)
78 return;
79
80 /* Initialize to zero */
81 for (regno = 0; regno < GDB_MAX_REGS; regno++)
82 gdb_regs[regno] = 0;
83
84 /* Otherwise, we have only some registers from switch_to() */
85 thread_regs = task_pt_regs(task);
86 gdb_regs[_R0] = thread_regs->ARM_r0;
87 gdb_regs[_R1] = thread_regs->ARM_r1;
88 gdb_regs[_R2] = thread_regs->ARM_r2;
89 gdb_regs[_R3] = thread_regs->ARM_r3;
90 gdb_regs[_R4] = thread_regs->ARM_r4;
91 gdb_regs[_R5] = thread_regs->ARM_r5;
92 gdb_regs[_R6] = thread_regs->ARM_r6;
93 gdb_regs[_R7] = thread_regs->ARM_r7;
94 gdb_regs[_R8] = thread_regs->ARM_r8;
95 gdb_regs[_R9] = thread_regs->ARM_r9;
96 gdb_regs[_R10] = thread_regs->ARM_r10;
97 gdb_regs[_FP] = thread_regs->ARM_fp;
98 gdb_regs[_IP] = thread_regs->ARM_ip;
99 gdb_regs[_SPT] = thread_regs->ARM_sp;
100 gdb_regs[_LR] = thread_regs->ARM_lr;
101 gdb_regs[_PC] = thread_regs->ARM_pc;
102 gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
103}
104
Jason Wesseldcc78712010-05-20 21:04:21 -0500105void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
106{
107 regs->ARM_pc = pc;
108}
109
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600110static int compiled_break;
111
112int kgdb_arch_handle_exception(int exception_vector, int signo,
113 int err_code, char *remcom_in_buffer,
114 char *remcom_out_buffer,
115 struct pt_regs *linux_regs)
116{
117 unsigned long addr;
118 char *ptr;
119
120 switch (remcom_in_buffer[0]) {
121 case 'D':
122 case 'k':
123 case 'c':
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600124 /*
125 * Try to read optional parameter, pc unchanged if no parm.
126 * If this was a compiled breakpoint, we need to move
127 * to the next instruction or we will just breakpoint
128 * over and over again.
129 */
130 ptr = &remcom_in_buffer[1];
131 if (kgdb_hex2long(&ptr, &addr))
132 linux_regs->ARM_pc = addr;
133 else if (compiled_break == 1)
134 linux_regs->ARM_pc += 4;
135
136 compiled_break = 0;
137
138 return 0;
139 }
140
141 return -1;
142}
143
144static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
145{
146 kgdb_handle_exception(1, SIGTRAP, 0, regs);
147
148 return 0;
149}
150
151static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
152{
153 compiled_break = 1;
154 kgdb_handle_exception(1, SIGTRAP, 0, regs);
155
156 return 0;
157}
158
159static struct undef_hook kgdb_brkpt_hook = {
160 .instr_mask = 0xffffffff,
161 .instr_val = KGDB_BREAKINST,
162 .fn = kgdb_brk_fn
163};
164
165static struct undef_hook kgdb_compiled_brkpt_hook = {
166 .instr_mask = 0xffffffff,
167 .instr_val = KGDB_COMPILED_BREAK,
168 .fn = kgdb_compiled_brk_fn
169};
170
Will Deacon5d8614c2010-03-12 11:03:58 +0100171static void kgdb_call_nmi_hook(void *ignored)
172{
173 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
174}
175
176void kgdb_roundup_cpus(unsigned long flags)
177{
178 local_irq_enable();
179 smp_call_function(kgdb_call_nmi_hook, NULL, 0);
180 local_irq_disable();
181}
182
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600183/**
184 * kgdb_arch_init - Perform any architecture specific initalization.
185 *
186 * This function will handle the initalization of any architecture
187 * specific callbacks.
188 */
189int kgdb_arch_init(void)
190{
191 register_undef_hook(&kgdb_brkpt_hook);
192 register_undef_hook(&kgdb_compiled_brkpt_hook);
193
194 return 0;
195}
196
197/**
198 * kgdb_arch_exit - Perform any architecture specific uninitalization.
199 *
200 * This function will handle the uninitalization of any architecture
201 * specific callbacks, for dynamic registration and unregistration.
202 */
203void kgdb_arch_exit(void)
204{
205 unregister_undef_hook(&kgdb_brkpt_hook);
206 unregister_undef_hook(&kgdb_compiled_brkpt_hook);
207}
208
209/*
210 * Register our undef instruction hooks with ARM undef core.
211 * We regsiter a hook specifically looking for the KGB break inst
212 * and we handle the normal undef case within the do_undefinstr
213 * handler.
214 */
215struct kgdb_arch arch_kgdb_ops = {
216#ifndef __ARMEB__
217 .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
218#else /* ! __ARMEB__ */
219 .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
220#endif
221};