1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/spinlock.h>
35#include <linux/kdebug.h>
36#include <linux/string.h>
37#include <linux/kernel.h>
38#include <linux/ptrace.h>
39#include <linux/sched.h>
40#include <linux/delay.h>
41#include <linux/kgdb.h>
42#include <linux/init.h>
43#include <linux/smp.h>
44#include <linux/nmi.h>
45
46#include <asm/apicdef.h>
47#include <asm/system.h>
48
49#include <mach_ipi.h>
50
51
52
53
54static int gdb_x86errcode;
55
56
57
58
59
60static int gdb_x86vector = -1;
61
62
63
64
65
66
67
68
69
70void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
71{
72#ifndef CONFIG_X86_32
73 u32 *gdb_regs32 = (u32 *)gdb_regs;
74#endif
75 gdb_regs[GDB_AX] = regs->ax;
76 gdb_regs[GDB_BX] = regs->bx;
77 gdb_regs[GDB_CX] = regs->cx;
78 gdb_regs[GDB_DX] = regs->dx;
79 gdb_regs[GDB_SI] = regs->si;
80 gdb_regs[GDB_DI] = regs->di;
81 gdb_regs[GDB_BP] = regs->bp;
82 gdb_regs[GDB_PC] = regs->ip;
83#ifdef CONFIG_X86_32
84 gdb_regs[GDB_PS] = regs->flags;
85 gdb_regs[GDB_DS] = regs->ds;
86 gdb_regs[GDB_ES] = regs->es;
87 gdb_regs[GDB_CS] = regs->cs;
88 gdb_regs[GDB_SS] = __KERNEL_DS;
89 gdb_regs[GDB_FS] = 0xFFFF;
90 gdb_regs[GDB_GS] = 0xFFFF;
91#else
92 gdb_regs[GDB_R8] = regs->r8;
93 gdb_regs[GDB_R9] = regs->r9;
94 gdb_regs[GDB_R10] = regs->r10;
95 gdb_regs[GDB_R11] = regs->r11;
96 gdb_regs[GDB_R12] = regs->r12;
97 gdb_regs[GDB_R13] = regs->r13;
98 gdb_regs[GDB_R14] = regs->r14;
99 gdb_regs[GDB_R15] = regs->r15;
100 gdb_regs32[GDB_PS] = regs->flags;
101 gdb_regs32[GDB_CS] = regs->cs;
102 gdb_regs32[GDB_SS] = regs->ss;
103#endif
104 gdb_regs[GDB_SP] = regs->sp;
105}
106
107
108
109
110
111
112
113
114
115
116
117
118
119void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
120{
121#ifndef CONFIG_X86_32
122 u32 *gdb_regs32 = (u32 *)gdb_regs;
123#endif
124 gdb_regs[GDB_AX] = 0;
125 gdb_regs[GDB_BX] = 0;
126 gdb_regs[GDB_CX] = 0;
127 gdb_regs[GDB_DX] = 0;
128 gdb_regs[GDB_SI] = 0;
129 gdb_regs[GDB_DI] = 0;
130 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
131#ifdef CONFIG_X86_32
132 gdb_regs[GDB_DS] = __KERNEL_DS;
133 gdb_regs[GDB_ES] = __KERNEL_DS;
134 gdb_regs[GDB_PS] = 0;
135 gdb_regs[GDB_CS] = __KERNEL_CS;
136 gdb_regs[GDB_PC] = p->thread.ip;
137 gdb_regs[GDB_SS] = __KERNEL_DS;
138 gdb_regs[GDB_FS] = 0xFFFF;
139 gdb_regs[GDB_GS] = 0xFFFF;
140#else
141 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
142 gdb_regs32[GDB_CS] = __KERNEL_CS;
143 gdb_regs32[GDB_SS] = __KERNEL_DS;
144 gdb_regs[GDB_PC] = p->thread.ip;
145 gdb_regs[GDB_R8] = 0;
146 gdb_regs[GDB_R9] = 0;
147 gdb_regs[GDB_R10] = 0;
148 gdb_regs[GDB_R11] = 0;
149 gdb_regs[GDB_R12] = 0;
150 gdb_regs[GDB_R13] = 0;
151 gdb_regs[GDB_R14] = 0;
152 gdb_regs[GDB_R15] = 0;
153#endif
154 gdb_regs[GDB_SP] = p->thread.sp;
155}
156
157
158
159
160
161
162
163
164
165void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
166{
167#ifndef CONFIG_X86_32
168 u32 *gdb_regs32 = (u32 *)gdb_regs;
169#endif
170 regs->ax = gdb_regs[GDB_AX];
171 regs->bx = gdb_regs[GDB_BX];
172 regs->cx = gdb_regs[GDB_CX];
173 regs->dx = gdb_regs[GDB_DX];
174 regs->si = gdb_regs[GDB_SI];
175 regs->di = gdb_regs[GDB_DI];
176 regs->bp = gdb_regs[GDB_BP];
177 regs->ip = gdb_regs[GDB_PC];
178#ifdef CONFIG_X86_32
179 regs->flags = gdb_regs[GDB_PS];
180 regs->ds = gdb_regs[GDB_DS];
181 regs->es = gdb_regs[GDB_ES];
182 regs->cs = gdb_regs[GDB_CS];
183#else
184 regs->r8 = gdb_regs[GDB_R8];
185 regs->r9 = gdb_regs[GDB_R9];
186 regs->r10 = gdb_regs[GDB_R10];
187 regs->r11 = gdb_regs[GDB_R11];
188 regs->r12 = gdb_regs[GDB_R12];
189 regs->r13 = gdb_regs[GDB_R13];
190 regs->r14 = gdb_regs[GDB_R14];
191 regs->r15 = gdb_regs[GDB_R15];
192 regs->flags = gdb_regs32[GDB_PS];
193 regs->cs = gdb_regs32[GDB_CS];
194 regs->ss = gdb_regs32[GDB_SS];
195#endif
196}
197
198static struct hw_breakpoint {
199 unsigned enabled;
200 unsigned type;
201 unsigned len;
202 unsigned long addr;
203} breakinfo[4];
204
205static void kgdb_correct_hw_break(void)
206{
207 unsigned long dr7;
208 int correctit = 0;
209 int breakbit;
210 int breakno;
211
212 get_debugreg(dr7, 7);
213 for (breakno = 0; breakno < 4; breakno++) {
214 breakbit = 2 << (breakno << 1);
215 if (!(dr7 & breakbit) && breakinfo[breakno].enabled) {
216 correctit = 1;
217 dr7 |= breakbit;
218 dr7 &= ~(0xf0000 << (breakno << 2));
219 dr7 |= ((breakinfo[breakno].len << 2) |
220 breakinfo[breakno].type) <<
221 ((breakno << 2) + 16);
222 if (breakno >= 0 && breakno <= 3)
223 set_debugreg(breakinfo[breakno].addr, breakno);
224
225 } else {
226 if ((dr7 & breakbit) && !breakinfo[breakno].enabled) {
227 correctit = 1;
228 dr7 &= ~breakbit;
229 dr7 &= ~(0xf0000 << (breakno << 2));
230 }
231 }
232 }
233 if (correctit)
234 set_debugreg(dr7, 7);
235}
236
237static int
238kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
239{
240 int i;
241
242 for (i = 0; i < 4; i++)
243 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
244 break;
245 if (i == 4)
246 return -1;
247
248 breakinfo[i].enabled = 0;
249
250 return 0;
251}
252
253static void kgdb_remove_all_hw_break(void)
254{
255 int i;
256
257 for (i = 0; i < 4; i++)
258 memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint));
259}
260
261static int
262kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
263{
264 unsigned type;
265 int i;
266
267 for (i = 0; i < 4; i++)
268 if (!breakinfo[i].enabled)
269 break;
270 if (i == 4)
271 return -1;
272
273 switch (bptype) {
274 case BP_HARDWARE_BREAKPOINT:
275 type = 0;
276 len = 1;
277 break;
278 case BP_WRITE_WATCHPOINT:
279 type = 1;
280 break;
281 case BP_ACCESS_WATCHPOINT:
282 type = 3;
283 break;
284 default:
285 return -1;
286 }
287
288 if (len == 1 || len == 2 || len == 4)
289 breakinfo[i].len = len - 1;
290 else
291 return -1;
292
293 breakinfo[i].enabled = 1;
294 breakinfo[i].addr = addr;
295 breakinfo[i].type = type;
296
297 return 0;
298}
299
300
301
302
303
304
305
306
307
308void kgdb_disable_hw_debug(struct pt_regs *regs)
309{
310
311 set_debugreg(0UL, 7);
312}
313
314
315
316
317
318
319
320
321
322
323
324void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
325{
326
327 gdb_x86vector = e_vector;
328 gdb_x86errcode = err_code;
329}
330
331#ifdef CONFIG_SMP
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348void kgdb_roundup_cpus(unsigned long flags)
349{
350 send_IPI_allbutself(APIC_DM_NMI);
351}
352#endif
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
371 char *remcomInBuffer, char *remcomOutBuffer,
372 struct pt_regs *linux_regs)
373{
374 unsigned long addr;
375 unsigned long dr6;
376 char *ptr;
377 int newPC;
378
379 switch (remcomInBuffer[0]) {
380 case 'c':
381 case 's':
382
383 ptr = &remcomInBuffer[1];
384 if (kgdb_hex2long(&ptr, &addr))
385 linux_regs->ip = addr;
386 case 'D':
387 case 'k':
388 newPC = linux_regs->ip;
389
390
391 linux_regs->flags &= ~X86_EFLAGS_TF;
392 atomic_set(&kgdb_cpu_doing_single_step, -1);
393
394
395 if (remcomInBuffer[0] == 's') {
396 linux_regs->flags |= X86_EFLAGS_TF;
397 kgdb_single_step = 1;
398 atomic_set(&kgdb_cpu_doing_single_step,
399 raw_smp_processor_id());
400 }
401
402 get_debugreg(dr6, 6);
403 if (!(dr6 & 0x4000)) {
404 int breakno;
405
406 for (breakno = 0; breakno < 4; breakno++) {
407 if (dr6 & (1 << breakno) &&
408 breakinfo[breakno].type == 0) {
409
410 linux_regs->flags |= X86_EFLAGS_RF;
411 break;
412 }
413 }
414 }
415 set_debugreg(0UL, 6);
416 kgdb_correct_hw_break();
417
418 return 0;
419 }
420
421
422 return -1;
423}
424
425static inline int
426single_step_cont(struct pt_regs *regs, struct die_args *args)
427{
428
429
430
431
432 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
433 "resuming...\n");
434 kgdb_arch_handle_exception(args->trapnr, args->signr,
435 args->err, "c", "", regs);
436
437 return NOTIFY_STOP;
438}
439
440static int was_in_debug_nmi[NR_CPUS];
441
442static int __kgdb_notify(struct die_args *args, unsigned long cmd)
443{
444 struct pt_regs *regs = args->regs;
445
446 switch (cmd) {
447 case DIE_NMI:
448 if (atomic_read(&kgdb_active) != -1) {
449
450 kgdb_nmicallback(raw_smp_processor_id(), regs);
451 was_in_debug_nmi[raw_smp_processor_id()] = 1;
452 touch_nmi_watchdog();
453 return NOTIFY_STOP;
454 }
455 return NOTIFY_DONE;
456
457 case DIE_NMI_IPI:
458
459 return NOTIFY_DONE;
460
461 case DIE_NMIUNKNOWN:
462 if (was_in_debug_nmi[raw_smp_processor_id()]) {
463 was_in_debug_nmi[raw_smp_processor_id()] = 0;
464 return NOTIFY_STOP;
465 }
466 return NOTIFY_DONE;
467
468 case DIE_NMIWATCHDOG:
469 if (atomic_read(&kgdb_active) != -1) {
470
471 kgdb_nmicallback(raw_smp_processor_id(), regs);
472 return NOTIFY_STOP;
473 }
474
475 break;
476
477 case DIE_DEBUG:
478 if (atomic_read(&kgdb_cpu_doing_single_step) ==
479 raw_smp_processor_id()) {
480 if (user_mode(regs))
481 return single_step_cont(regs, args);
482 break;
483 } else if (test_thread_flag(TIF_SINGLESTEP))
484
485
486
487 return NOTIFY_DONE;
488
489 default:
490 if (user_mode(regs))
491 return NOTIFY_DONE;
492 }
493
494 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
495 return NOTIFY_DONE;
496
497
498 touch_nmi_watchdog();
499 return NOTIFY_STOP;
500}
501
502static int
503kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
504{
505 unsigned long flags;
506 int ret;
507
508 local_irq_save(flags);
509 ret = __kgdb_notify(ptr, cmd);
510 local_irq_restore(flags);
511
512 return ret;
513}
514
515static struct notifier_block kgdb_notifier = {
516 .notifier_call = kgdb_notify,
517
518
519
520
521 .priority = -INT_MAX,
522};
523
524
525
526
527
528
529
530int kgdb_arch_init(void)
531{
532 return register_die_notifier(&kgdb_notifier);
533}
534
535
536
537
538
539
540
541void kgdb_arch_exit(void)
542{
543 unregister_die_notifier(&kgdb_notifier);
544}
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559int kgdb_skipexception(int exception, struct pt_regs *regs)
560{
561 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
562 regs->ip -= 1;
563 return 1;
564 }
565 return 0;
566}
567
568unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
569{
570 if (exception == 3)
571 return instruction_pointer(regs) - 1;
572 return instruction_pointer(regs);
573}
574
575struct kgdb_arch arch_kgdb_ops = {
576
577 .gdb_bpt_instr = { 0xcc },
578 .flags = KGDB_HW_BREAKPOINT,
579 .set_hw_breakpoint = kgdb_set_hw_break,
580 .remove_hw_breakpoint = kgdb_remove_hw_break,
581 .remove_all_hw_break = kgdb_remove_all_hw_break,
582 .correct_hw_break = kgdb_correct_hw_break,
583};
584