1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/kgdb.h>
20#include <linux/smp.h>
21#include <linux/signal.h>
22#include <linux/ptrace.h>
23#include <asm/current.h>
24#include <asm/processor.h>
25#include <asm/machdep.h>
26
27
28
29
30
31
32static struct hard_trap_info
33{
34 unsigned int tt;
35 unsigned char signo;
36} hard_trap_info[] = {
37 { 0x0100, 0x02 },
38 { 0x0200, 0x0b },
39 { 0x0300, 0x0b },
40 { 0x0400, 0x0b },
41 { 0x0500, 0x02 },
42 { 0x0600, 0x0a },
43 { 0x0700, 0x05 },
44 { 0x0800, 0x08 },
45 { 0x0900, 0x0e },
46 { 0x0c00, 0x14 },
47#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
48 { 0x2002, 0x05 },
49#if defined(CONFIG_FSL_BOOKE)
50 { 0x2010, 0x08 },
51 { 0x2020, 0x08 },
52 { 0x2030, 0x08 },
53 { 0x2040, 0x08 },
54 { 0x2050, 0x08 },
55 { 0x2060, 0x0e },
56 { 0x2900, 0x08 },
57 { 0x3100, 0x0e },
58 { 0x3200, 0x02 },
59#else
60 { 0x1000, 0x0e },
61 { 0x1010, 0x0e },
62 { 0x1020, 0x02 },
63 { 0x2010, 0x08 },
64 { 0x2020, 0x08 },
65#endif
66#else
67 { 0x0d00, 0x05 },
68#if defined(CONFIG_8xx)
69 { 0x1000, 0x04 },
70#else
71 { 0x0f00, 0x04 },
72 { 0x0f20, 0x08 },
73 { 0x1300, 0x05 },
74#if defined(CONFIG_PPC64)
75 { 0x1200, 0x05 },
76 { 0x1500, 0x04 },
77 { 0x1600, 0x04 },
78 { 0x1700, 0x08 },
79 { 0x1800, 0x04 },
80#else
81 { 0x1400, 0x02 },
82 { 0x1600, 0x08 },
83 { 0x1700, 0x04 },
84 { 0x2000, 0x05 },
85#endif
86#endif
87#endif
88 { 0x0000, 0x00 }
89};
90
91static int computeSignal(unsigned int tt)
92{
93 struct hard_trap_info *ht;
94
95 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
96 if (ht->tt == tt)
97 return ht->signo;
98
99 return SIGHUP;
100}
101
102static int kgdb_call_nmi_hook(struct pt_regs *regs)
103{
104 kgdb_nmicallback(raw_smp_processor_id(), regs);
105 return 0;
106}
107
108#ifdef CONFIG_SMP
109void kgdb_roundup_cpus(unsigned long flags)
110{
111 smp_send_debugger_break(MSG_ALL_BUT_SELF);
112}
113#endif
114
115
116static int kgdb_debugger(struct pt_regs *regs)
117{
118 return kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs);
119}
120
121static int kgdb_handle_breakpoint(struct pt_regs *regs)
122{
123 if (user_mode(regs))
124 return 0;
125
126 if (kgdb_handle_exception(0, SIGTRAP, 0, regs) != 0)
127 return 0;
128
129 if (*(u32 *) (regs->nip) == *(u32 *) (&arch_kgdb_ops.gdb_bpt_instr))
130 regs->nip += 4;
131
132 return 1;
133}
134
135static int kgdb_singlestep(struct pt_regs *regs)
136{
137 struct thread_info *thread_info, *exception_thread_info;
138
139 if (user_mode(regs))
140 return 0;
141
142
143
144
145
146
147
148
149
150
151
152 thread_info = (struct thread_info *)(regs->gpr[1] & ~(THREAD_SIZE-1));
153 exception_thread_info = current_thread_info();
154
155 if (thread_info != exception_thread_info)
156 memcpy(exception_thread_info, thread_info, sizeof *thread_info);
157
158 kgdb_handle_exception(0, SIGTRAP, 0, regs);
159
160 if (thread_info != exception_thread_info)
161 memcpy(thread_info, exception_thread_info, sizeof *thread_info);
162
163 return 1;
164}
165
166static int kgdb_iabr_match(struct pt_regs *regs)
167{
168 if (user_mode(regs))
169 return 0;
170
171 if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
172 return 0;
173 return 1;
174}
175
176static int kgdb_dabr_match(struct pt_regs *regs)
177{
178 if (user_mode(regs))
179 return 0;
180
181 if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
182 return 0;
183 return 1;
184}
185
186#define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
187
188#define PACK32(ptr, src) do { \
189 u32 *ptr32; \
190 ptr32 = (u32 *)ptr; \
191 *(ptr32++) = (src); \
192 ptr = (unsigned long *)ptr32; \
193 } while (0)
194
195
196void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
197{
198 unsigned long *ptr = gdb_regs;
199 int reg;
200
201 memset(gdb_regs, 0, NUMREGBYTES);
202
203 for (reg = 0; reg < 32; reg++)
204 PACK64(ptr, regs->gpr[reg]);
205
206#ifdef CONFIG_FSL_BOOKE
207#ifdef CONFIG_SPE
208 for (reg = 0; reg < 32; reg++)
209 PACK64(ptr, current->thread.evr[reg]);
210#else
211 ptr += 32;
212#endif
213#else
214
215 ptr += 32 * 8 / sizeof(long);
216#endif
217
218 PACK64(ptr, regs->nip);
219 PACK64(ptr, regs->msr);
220 PACK32(ptr, regs->ccr);
221 PACK64(ptr, regs->link);
222 PACK64(ptr, regs->ctr);
223 PACK32(ptr, regs->xer);
224
225 BUG_ON((unsigned long)ptr >
226 (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
227}
228
229void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
230{
231 struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp +
232 STACK_FRAME_OVERHEAD);
233 unsigned long *ptr = gdb_regs;
234 int reg;
235
236 memset(gdb_regs, 0, NUMREGBYTES);
237
238
239 for (reg = 0; reg < 3; reg++)
240 PACK64(ptr, regs->gpr[reg]);
241
242
243 ptr += 11;
244
245
246 for (reg = 14; reg < 32; reg++)
247 PACK64(ptr, regs->gpr[reg]);
248
249#ifdef CONFIG_FSL_BOOKE
250#ifdef CONFIG_SPE
251 for (reg = 0; reg < 32; reg++)
252 PACK64(ptr, p->thread.evr[reg]);
253#else
254 ptr += 32;
255#endif
256#else
257
258 ptr += 32 * 8 / sizeof(long);
259#endif
260
261 PACK64(ptr, regs->nip);
262 PACK64(ptr, regs->msr);
263 PACK32(ptr, regs->ccr);
264 PACK64(ptr, regs->link);
265 PACK64(ptr, regs->ctr);
266 PACK32(ptr, regs->xer);
267
268 BUG_ON((unsigned long)ptr >
269 (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
270}
271
272#define UNPACK64(dest, ptr) do { dest = *(ptr++); } while (0)
273
274#define UNPACK32(dest, ptr) do { \
275 u32 *ptr32; \
276 ptr32 = (u32 *)ptr; \
277 dest = *(ptr32++); \
278 ptr = (unsigned long *)ptr32; \
279 } while (0)
280
281void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
282{
283 unsigned long *ptr = gdb_regs;
284 int reg;
285
286 for (reg = 0; reg < 32; reg++)
287 UNPACK64(regs->gpr[reg], ptr);
288
289#ifdef CONFIG_FSL_BOOKE
290#ifdef CONFIG_SPE
291 for (reg = 0; reg < 32; reg++)
292 UNPACK64(current->thread.evr[reg], ptr);
293#else
294 ptr += 32;
295#endif
296#else
297
298 ptr += 32 * 8 / sizeof(int);
299#endif
300
301 UNPACK64(regs->nip, ptr);
302 UNPACK64(regs->msr, ptr);
303 UNPACK32(regs->ccr, ptr);
304 UNPACK64(regs->link, ptr);
305 UNPACK64(regs->ctr, ptr);
306 UNPACK32(regs->xer, ptr);
307
308 BUG_ON((unsigned long)ptr >
309 (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
310}
311
312
313
314
315int kgdb_arch_handle_exception(int vector, int signo, int err_code,
316 char *remcom_in_buffer, char *remcom_out_buffer,
317 struct pt_regs *linux_regs)
318{
319 char *ptr = &remcom_in_buffer[1];
320 unsigned long addr;
321
322 switch (remcom_in_buffer[0]) {
323
324
325
326
327 case 's':
328 case 'c':
329
330 if (kgdb_hex2long(&ptr, &addr))
331 linux_regs->nip = addr;
332
333 atomic_set(&kgdb_cpu_doing_single_step, -1);
334
335 if (remcom_in_buffer[0] == 's') {
336#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
337 mtspr(SPRN_DBCR0,
338 mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
339 linux_regs->msr |= MSR_DE;
340#else
341 linux_regs->msr |= MSR_SE;
342#endif
343 kgdb_single_step = 1;
344 atomic_set(&kgdb_cpu_doing_single_step,
345 raw_smp_processor_id());
346 }
347 return 0;
348 }
349
350 return -1;
351}
352
353
354
355
356struct kgdb_arch arch_kgdb_ops = {
357 .gdb_bpt_instr = {0x7d, 0x82, 0x10, 0x08},
358};
359
360static int kgdb_not_implemented(struct pt_regs *regs)
361{
362 return 0;
363}
364
365static void *old__debugger_ipi;
366static void *old__debugger;
367static void *old__debugger_bpt;
368static void *old__debugger_sstep;
369static void *old__debugger_iabr_match;
370static void *old__debugger_dabr_match;
371static void *old__debugger_fault_handler;
372
373int kgdb_arch_init(void)
374{
375 old__debugger_ipi = __debugger_ipi;
376 old__debugger = __debugger;
377 old__debugger_bpt = __debugger_bpt;
378 old__debugger_sstep = __debugger_sstep;
379 old__debugger_iabr_match = __debugger_iabr_match;
380 old__debugger_dabr_match = __debugger_dabr_match;
381 old__debugger_fault_handler = __debugger_fault_handler;
382
383 __debugger_ipi = kgdb_call_nmi_hook;
384 __debugger = kgdb_debugger;
385 __debugger_bpt = kgdb_handle_breakpoint;
386 __debugger_sstep = kgdb_singlestep;
387 __debugger_iabr_match = kgdb_iabr_match;
388 __debugger_dabr_match = kgdb_dabr_match;
389 __debugger_fault_handler = kgdb_not_implemented;
390
391 return 0;
392}
393
394void kgdb_arch_exit(void)
395{
396 __debugger_ipi = old__debugger_ipi;
397 __debugger = old__debugger;
398 __debugger_bpt = old__debugger_bpt;
399 __debugger_sstep = old__debugger_sstep;
400 __debugger_iabr_match = old__debugger_iabr_match;
401 __debugger_dabr_match = old__debugger_dabr_match;
402 __debugger_fault_handler = old__debugger_fault_handler;
403}
404