1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/kernel.h>
18#include <linux/kgdb.h>
19#include <linux/smp.h>
20#include <linux/signal.h>
21#include <linux/ptrace.h>
22#include <linux/kdebug.h>
23#include <asm/current.h>
24#include <asm/processor.h>
25#include <asm/machdep.h>
26#include <asm/debug.h>
27#include <asm/code-patching.h>
28#include <linux/slab.h>
29
30
31
32
33
34
35static struct hard_trap_info
36{
37 unsigned int tt;
38 unsigned char signo;
39} hard_trap_info[] = {
40 { 0x0100, 0x02 },
41 { 0x0200, 0x0b },
42 { 0x0300, 0x0b },
43 { 0x0400, 0x0b },
44 { 0x0500, 0x02 },
45 { 0x0600, 0x0a },
46 { 0x0700, 0x05 },
47 { 0x0800, 0x08 },
48 { 0x0900, 0x0e },
49 { 0x0c00, 0x14 },
50#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
51 { 0x2002, 0x05 },
52#if defined(CONFIG_FSL_BOOKE)
53 { 0x2010, 0x08 },
54 { 0x2020, 0x08 },
55 { 0x2030, 0x08 },
56 { 0x2040, 0x08 },
57 { 0x2050, 0x08 },
58 { 0x2060, 0x0e },
59 { 0x2900, 0x08 },
60 { 0x3100, 0x0e },
61 { 0x3200, 0x02 },
62#else
63 { 0x1000, 0x0e },
64 { 0x1010, 0x0e },
65 { 0x1020, 0x02 },
66 { 0x2010, 0x08 },
67 { 0x2020, 0x08 },
68#endif
69#else
70 { 0x0d00, 0x05 },
71#if defined(CONFIG_PPC_8xx)
72 { 0x1000, 0x04 },
73#else
74 { 0x0f00, 0x04 },
75 { 0x0f20, 0x08 },
76 { 0x1300, 0x05 },
77#if defined(CONFIG_PPC64)
78 { 0x1200, 0x05 },
79 { 0x1500, 0x04 },
80 { 0x1600, 0x04 },
81 { 0x1700, 0x08 },
82 { 0x1800, 0x04 },
83#else
84 { 0x1400, 0x02 },
85 { 0x1600, 0x08 },
86 { 0x1700, 0x04 },
87 { 0x2000, 0x05 },
88#endif
89#endif
90#endif
91 { 0x0000, 0x00 }
92};
93
94static int computeSignal(unsigned int tt)
95{
96 struct hard_trap_info *ht;
97
98 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
99 if (ht->tt == tt)
100 return ht->signo;
101
102 return SIGHUP;
103}
104
105
106
107
108
109
110
111
112
113
114
115int kgdb_skipexception(int exception, struct pt_regs *regs)
116{
117 return kgdb_isremovedbreak(regs->nip);
118}
119
120static int kgdb_debugger_ipi(struct pt_regs *regs)
121{
122 kgdb_nmicallback(raw_smp_processor_id(), regs);
123 return 0;
124}
125
126#ifdef CONFIG_SMP
127void kgdb_roundup_cpus(void)
128{
129 smp_send_debugger_break();
130}
131#endif
132
133
134static int kgdb_debugger(struct pt_regs *regs)
135{
136 return !kgdb_handle_exception(1, computeSignal(TRAP(regs)),
137 DIE_OOPS, regs);
138}
139
140static int kgdb_handle_breakpoint(struct pt_regs *regs)
141{
142 if (user_mode(regs))
143 return 0;
144
145 if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0)
146 return 0;
147
148 if (*(u32 *)regs->nip == BREAK_INSTR)
149 regs->nip += BREAK_INSTR_SIZE;
150
151 return 1;
152}
153
154static int kgdb_singlestep(struct pt_regs *regs)
155{
156 if (user_mode(regs))
157 return 0;
158
159 kgdb_handle_exception(0, SIGTRAP, 0, regs);
160
161 return 1;
162}
163
164static int kgdb_iabr_match(struct pt_regs *regs)
165{
166 if (user_mode(regs))
167 return 0;
168
169 if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
170 return 0;
171 return 1;
172}
173
174static int kgdb_break_match(struct pt_regs *regs)
175{
176 if (user_mode(regs))
177 return 0;
178
179 if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
180 return 0;
181 return 1;
182}
183
184#define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
185
186#define PACK32(ptr, src) do { \
187 u32 *ptr32; \
188 ptr32 = (u32 *)ptr; \
189 *(ptr32++) = (src); \
190 ptr = (unsigned long *)ptr32; \
191 } while (0)
192
193void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
194{
195 struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp +
196 STACK_FRAME_OVERHEAD);
197 unsigned long *ptr = gdb_regs;
198 int reg;
199
200 memset(gdb_regs, 0, NUMREGBYTES);
201
202
203 for (reg = 0; reg < 3; reg++)
204 PACK64(ptr, regs->gpr[reg]);
205
206
207 ptr += 11;
208
209
210 for (reg = 14; reg < 32; reg++)
211 PACK64(ptr, regs->gpr[reg]);
212
213#ifdef CONFIG_FSL_BOOKE
214#ifdef CONFIG_SPE
215 for (reg = 0; reg < 32; reg++)
216 PACK64(ptr, p->thread.evr[reg]);
217#else
218 ptr += 32;
219#endif
220#else
221
222 ptr += 32 * 8 / sizeof(long);
223#endif
224
225 PACK64(ptr, regs->nip);
226 PACK64(ptr, regs->msr);
227 PACK32(ptr, regs->ccr);
228 PACK64(ptr, regs->link);
229 PACK64(ptr, regs->ctr);
230 PACK32(ptr, regs->xer);
231
232 BUG_ON((unsigned long)ptr >
233 (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
234}
235
236#define GDB_SIZEOF_REG sizeof(unsigned long)
237#define GDB_SIZEOF_REG_U32 sizeof(u32)
238
239#ifdef CONFIG_FSL_BOOKE
240#define GDB_SIZEOF_FLOAT_REG sizeof(unsigned long)
241#else
242#define GDB_SIZEOF_FLOAT_REG sizeof(u64)
243#endif
244
245struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
246{
247 { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[0]) },
248 { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[1]) },
249 { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[2]) },
250 { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[3]) },
251 { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[4]) },
252 { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[5]) },
253 { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[6]) },
254 { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[7]) },
255 { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[8]) },
256 { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[9]) },
257 { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[10]) },
258 { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[11]) },
259 { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[12]) },
260 { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[13]) },
261 { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[14]) },
262 { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[15]) },
263 { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[16]) },
264 { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[17]) },
265 { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[18]) },
266 { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[19]) },
267 { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[20]) },
268 { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[21]) },
269 { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[22]) },
270 { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[23]) },
271 { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[24]) },
272 { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[25]) },
273 { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[26]) },
274 { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[27]) },
275 { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[28]) },
276 { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[29]) },
277 { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[30]) },
278 { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[31]) },
279
280 { "f0", GDB_SIZEOF_FLOAT_REG, 0 },
281 { "f1", GDB_SIZEOF_FLOAT_REG, 1 },
282 { "f2", GDB_SIZEOF_FLOAT_REG, 2 },
283 { "f3", GDB_SIZEOF_FLOAT_REG, 3 },
284 { "f4", GDB_SIZEOF_FLOAT_REG, 4 },
285 { "f5", GDB_SIZEOF_FLOAT_REG, 5 },
286 { "f6", GDB_SIZEOF_FLOAT_REG, 6 },
287 { "f7", GDB_SIZEOF_FLOAT_REG, 7 },
288 { "f8", GDB_SIZEOF_FLOAT_REG, 8 },
289 { "f9", GDB_SIZEOF_FLOAT_REG, 9 },
290 { "f10", GDB_SIZEOF_FLOAT_REG, 10 },
291 { "f11", GDB_SIZEOF_FLOAT_REG, 11 },
292 { "f12", GDB_SIZEOF_FLOAT_REG, 12 },
293 { "f13", GDB_SIZEOF_FLOAT_REG, 13 },
294 { "f14", GDB_SIZEOF_FLOAT_REG, 14 },
295 { "f15", GDB_SIZEOF_FLOAT_REG, 15 },
296 { "f16", GDB_SIZEOF_FLOAT_REG, 16 },
297 { "f17", GDB_SIZEOF_FLOAT_REG, 17 },
298 { "f18", GDB_SIZEOF_FLOAT_REG, 18 },
299 { "f19", GDB_SIZEOF_FLOAT_REG, 19 },
300 { "f20", GDB_SIZEOF_FLOAT_REG, 20 },
301 { "f21", GDB_SIZEOF_FLOAT_REG, 21 },
302 { "f22", GDB_SIZEOF_FLOAT_REG, 22 },
303 { "f23", GDB_SIZEOF_FLOAT_REG, 23 },
304 { "f24", GDB_SIZEOF_FLOAT_REG, 24 },
305 { "f25", GDB_SIZEOF_FLOAT_REG, 25 },
306 { "f26", GDB_SIZEOF_FLOAT_REG, 26 },
307 { "f27", GDB_SIZEOF_FLOAT_REG, 27 },
308 { "f28", GDB_SIZEOF_FLOAT_REG, 28 },
309 { "f29", GDB_SIZEOF_FLOAT_REG, 29 },
310 { "f30", GDB_SIZEOF_FLOAT_REG, 30 },
311 { "f31", GDB_SIZEOF_FLOAT_REG, 31 },
312
313 { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, nip) },
314 { "msr", GDB_SIZEOF_REG, offsetof(struct pt_regs, msr) },
315 { "cr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ccr) },
316 { "lr", GDB_SIZEOF_REG, offsetof(struct pt_regs, link) },
317 { "ctr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ctr) },
318 { "xer", GDB_SIZEOF_REG, offsetof(struct pt_regs, xer) },
319};
320
321char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
322{
323 if (regno >= DBG_MAX_REG_NUM || regno < 0)
324 return NULL;
325
326 if (regno < 32 || regno >= 64)
327
328
329 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
330 dbg_reg_def[regno].size);
331
332 if (regno >= 32 && regno < 64) {
333
334#if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
335 if (current)
336 memcpy(mem, ¤t->thread.evr[regno-32],
337 dbg_reg_def[regno].size);
338#else
339
340 memset(mem, 0, dbg_reg_def[regno].size);
341#endif
342 }
343
344 return dbg_reg_def[regno].name;
345}
346
347int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
348{
349 if (regno >= DBG_MAX_REG_NUM || regno < 0)
350 return -EINVAL;
351
352 if (regno < 32 || regno >= 64)
353
354
355 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
356 dbg_reg_def[regno].size);
357
358 if (regno >= 32 && regno < 64) {
359
360#if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
361 memcpy(¤t->thread.evr[regno-32], mem,
362 dbg_reg_def[regno].size);
363#else
364
365 return 0;
366#endif
367 }
368
369 return 0;
370}
371
372void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
373{
374 regs->nip = pc;
375}
376
377
378
379
380int kgdb_arch_handle_exception(int vector, int signo, int err_code,
381 char *remcom_in_buffer, char *remcom_out_buffer,
382 struct pt_regs *linux_regs)
383{
384 char *ptr = &remcom_in_buffer[1];
385 unsigned long addr;
386
387 switch (remcom_in_buffer[0]) {
388
389
390
391
392 case 's':
393 case 'c':
394
395 if (kgdb_hex2long(&ptr, &addr))
396 linux_regs->nip = addr;
397
398 atomic_set(&kgdb_cpu_doing_single_step, -1);
399
400 if (remcom_in_buffer[0] == 's') {
401#ifdef CONFIG_PPC_ADV_DEBUG_REGS
402 mtspr(SPRN_DBCR0,
403 mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
404 linux_regs->msr |= MSR_DE;
405#else
406 linux_regs->msr |= MSR_SE;
407#endif
408 atomic_set(&kgdb_cpu_doing_single_step,
409 raw_smp_processor_id());
410 }
411 return 0;
412 }
413
414 return -1;
415}
416
417int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
418{
419 int err;
420 unsigned int instr;
421 unsigned int *addr = (unsigned int *)bpt->bpt_addr;
422
423 err = probe_kernel_address(addr, instr);
424 if (err)
425 return err;
426
427 err = patch_instruction(addr, BREAK_INSTR);
428 if (err)
429 return -EFAULT;
430
431 *(unsigned int *)bpt->saved_instr = instr;
432
433 return 0;
434}
435
436int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
437{
438 int err;
439 unsigned int instr = *(unsigned int *)bpt->saved_instr;
440 unsigned int *addr = (unsigned int *)bpt->bpt_addr;
441
442 err = patch_instruction(addr, instr);
443 if (err)
444 return -EFAULT;
445
446 return 0;
447}
448
449
450
451
452const struct kgdb_arch arch_kgdb_ops;
453
454static int kgdb_not_implemented(struct pt_regs *regs)
455{
456 return 0;
457}
458
459static void *old__debugger_ipi;
460static void *old__debugger;
461static void *old__debugger_bpt;
462static void *old__debugger_sstep;
463static void *old__debugger_iabr_match;
464static void *old__debugger_break_match;
465static void *old__debugger_fault_handler;
466
467int kgdb_arch_init(void)
468{
469 old__debugger_ipi = __debugger_ipi;
470 old__debugger = __debugger;
471 old__debugger_bpt = __debugger_bpt;
472 old__debugger_sstep = __debugger_sstep;
473 old__debugger_iabr_match = __debugger_iabr_match;
474 old__debugger_break_match = __debugger_break_match;
475 old__debugger_fault_handler = __debugger_fault_handler;
476
477 __debugger_ipi = kgdb_debugger_ipi;
478 __debugger = kgdb_debugger;
479 __debugger_bpt = kgdb_handle_breakpoint;
480 __debugger_sstep = kgdb_singlestep;
481 __debugger_iabr_match = kgdb_iabr_match;
482 __debugger_break_match = kgdb_break_match;
483 __debugger_fault_handler = kgdb_not_implemented;
484
485 return 0;
486}
487
488void kgdb_arch_exit(void)
489{
490 __debugger_ipi = old__debugger_ipi;
491 __debugger = old__debugger;
492 __debugger_bpt = old__debugger_bpt;
493 __debugger_sstep = old__debugger_sstep;
494 __debugger_iabr_match = old__debugger_iabr_match;
495 __debugger_break_match = old__debugger_break_match;
496 __debugger_fault_handler = old__debugger_fault_handler;
497}
498