1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/irq.h>
22#include <linux/sched.h>
23#include <linux/kdebug.h>
24#include <linux/kgdb.h>
25
26
27#define GDB_SIZEOF_REG 4
28
29
30
31struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
32 { " r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, r00)},
33 { " r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, r01)},
34 { " r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, r02)},
35 { " r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, r03)},
36 { " r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, r04)},
37 { " r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, r05)},
38 { " r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, r06)},
39 { " r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, r07)},
40 { " r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, r08)},
41 { " r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, r09)},
42 { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, r10)},
43 { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, r11)},
44 { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, r12)},
45 { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, r13)},
46 { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, r14)},
47 { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, r15)},
48 { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, r16)},
49 { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, r17)},
50 { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, r18)},
51 { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, r19)},
52 { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, r20)},
53 { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, r21)},
54 { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, r22)},
55 { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, r23)},
56 { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, r24)},
57 { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, r25)},
58 { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, r26)},
59 { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, r27)},
60 { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, r28)},
61 { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, r29)},
62 { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, r30)},
63 { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, r31)},
64
65 { "usr", GDB_SIZEOF_REG, offsetof(struct pt_regs, usr)},
66 { "preds", GDB_SIZEOF_REG, offsetof(struct pt_regs, preds)},
67 { " m0", GDB_SIZEOF_REG, offsetof(struct pt_regs, m0)},
68 { " m1", GDB_SIZEOF_REG, offsetof(struct pt_regs, m1)},
69 { "sa0", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa0)},
70 { "sa1", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa1)},
71 { "lc0", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc0)},
72 { "lc1", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc1)},
73 { " gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)},
74 { "ugp", GDB_SIZEOF_REG, offsetof(struct pt_regs, ugp)},
75 { "cs0", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs0)},
76 { "cs1", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs1)},
77 { "psp", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmpsp)},
78 { "elr", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmel)},
79 { "est", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmest)},
80 { "badva", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmbadva)},
81 { "restart_r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, restart_r0)},
82 { "syscall_nr", GDB_SIZEOF_REG, offsetof(struct pt_regs, syscall_nr)},
83};
84
85struct kgdb_arch arch_kgdb_ops = {
86
87 .gdb_bpt_instr = {0x54, 0x00, 0xdb, 0x0c},
88};
89
90char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
91{
92 if (regno >= DBG_MAX_REG_NUM || regno < 0)
93 return NULL;
94
95 *((unsigned long *) mem) = *((unsigned long *) ((void *)regs +
96 dbg_reg_def[regno].offset));
97
98 return dbg_reg_def[regno].name;
99}
100
101int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
102{
103 if (regno >= DBG_MAX_REG_NUM || regno < 0)
104 return -EINVAL;
105
106 *((unsigned long *) ((void *)regs + dbg_reg_def[regno].offset)) =
107 *((unsigned long *) mem);
108
109 return 0;
110}
111
112void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
113{
114 instruction_pointer(regs) = pc;
115}
116
117#ifdef CONFIG_SMP
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136static void hexagon_kgdb_nmi_hook(void *ignored)
137{
138 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
139}
140
141void kgdb_roundup_cpus(unsigned long flags)
142{
143 local_irq_enable();
144 smp_call_function(hexagon_kgdb_nmi_hook, NULL, 0);
145 local_irq_disable();
146}
147#endif
148
149
150
151void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
152 struct task_struct *task)
153{
154 struct pt_regs *thread_regs;
155
156 if (task == NULL)
157 return;
158
159
160 memset(gdb_regs, 0, NUMREGBYTES);
161
162
163 thread_regs = task_pt_regs(task);
164 gdb_regs[0] = thread_regs->r00;
165}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185int kgdb_arch_handle_exception(int vector, int signo, int err_code,
186 char *remcom_in_buffer, char *remcom_out_buffer,
187 struct pt_regs *linux_regs)
188{
189 switch (remcom_in_buffer[0]) {
190 case 's':
191 case 'c':
192 return 0;
193 }
194
195 return -1;
196}
197
198static int __kgdb_notify(struct die_args *args, unsigned long cmd)
199{
200
201 if (atomic_read(&kgdb_active) != -1) {
202 kgdb_nmicallback(smp_processor_id(), args->regs);
203 return NOTIFY_STOP;
204 }
205
206 if (user_mode(args->regs))
207 return NOTIFY_DONE;
208
209 if (kgdb_handle_exception(args->trapnr & 0xff, args->signr, args->err,
210 args->regs))
211 return NOTIFY_DONE;
212
213 return NOTIFY_STOP;
214}
215
216static int
217kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
218{
219 unsigned long flags;
220 int ret;
221
222 local_irq_save(flags);
223 ret = __kgdb_notify(ptr, cmd);
224 local_irq_restore(flags);
225
226 return ret;
227}
228
229static struct notifier_block kgdb_notifier = {
230 .notifier_call = kgdb_notify,
231
232
233
234
235 .priority = -INT_MAX,
236};
237
238
239
240
241
242
243
244int kgdb_arch_init(void)
245{
246 return register_die_notifier(&kgdb_notifier);
247}
248
249
250
251
252
253
254
255void kgdb_arch_exit(void)
256{
257 unregister_die_notifier(&kgdb_notifier);
258}
259