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#include <linux/hw_breakpoint.h>
46
47#include <asm/debugreg.h>
48#include <asm/apicdef.h>
49#include <asm/system.h>
50#include <asm/apic.h>
51#include <asm/nmi.h>
52
53struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
54{
55#ifdef CONFIG_X86_32
56 { "ax", 4, offsetof(struct pt_regs, ax) },
57 { "cx", 4, offsetof(struct pt_regs, cx) },
58 { "dx", 4, offsetof(struct pt_regs, dx) },
59 { "bx", 4, offsetof(struct pt_regs, bx) },
60 { "sp", 4, offsetof(struct pt_regs, sp) },
61 { "bp", 4, offsetof(struct pt_regs, bp) },
62 { "si", 4, offsetof(struct pt_regs, si) },
63 { "di", 4, offsetof(struct pt_regs, di) },
64 { "ip", 4, offsetof(struct pt_regs, ip) },
65 { "flags", 4, offsetof(struct pt_regs, flags) },
66 { "cs", 4, offsetof(struct pt_regs, cs) },
67 { "ss", 4, offsetof(struct pt_regs, ss) },
68 { "ds", 4, offsetof(struct pt_regs, ds) },
69 { "es", 4, offsetof(struct pt_regs, es) },
70 { "fs", 4, -1 },
71 { "gs", 4, -1 },
72#else
73 { "ax", 8, offsetof(struct pt_regs, ax) },
74 { "bx", 8, offsetof(struct pt_regs, bx) },
75 { "cx", 8, offsetof(struct pt_regs, cx) },
76 { "dx", 8, offsetof(struct pt_regs, dx) },
77 { "si", 8, offsetof(struct pt_regs, dx) },
78 { "di", 8, offsetof(struct pt_regs, di) },
79 { "bp", 8, offsetof(struct pt_regs, bp) },
80 { "sp", 8, offsetof(struct pt_regs, sp) },
81 { "r8", 8, offsetof(struct pt_regs, r8) },
82 { "r9", 8, offsetof(struct pt_regs, r9) },
83 { "r10", 8, offsetof(struct pt_regs, r10) },
84 { "r11", 8, offsetof(struct pt_regs, r11) },
85 { "r12", 8, offsetof(struct pt_regs, r12) },
86 { "r13", 8, offsetof(struct pt_regs, r13) },
87 { "r14", 8, offsetof(struct pt_regs, r14) },
88 { "r15", 8, offsetof(struct pt_regs, r15) },
89 { "ip", 8, offsetof(struct pt_regs, ip) },
90 { "flags", 4, offsetof(struct pt_regs, flags) },
91 { "cs", 4, offsetof(struct pt_regs, cs) },
92 { "ss", 4, offsetof(struct pt_regs, ss) },
93#endif
94};
95
96int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
97{
98 if (
99#ifdef CONFIG_X86_32
100 regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
101#endif
102 regno == GDB_SP || regno == GDB_ORIG_AX)
103 return 0;
104
105 if (dbg_reg_def[regno].offset != -1)
106 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
107 dbg_reg_def[regno].size);
108 return 0;
109}
110
111char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
112{
113 if (regno == GDB_ORIG_AX) {
114 memcpy(mem, ®s->orig_ax, sizeof(regs->orig_ax));
115 return "orig_ax";
116 }
117 if (regno >= DBG_MAX_REG_NUM || regno < 0)
118 return NULL;
119
120 if (dbg_reg_def[regno].offset != -1)
121 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
122 dbg_reg_def[regno].size);
123
124#ifdef CONFIG_X86_32
125 switch (regno) {
126 case GDB_SS:
127 if (!user_mode_vm(regs))
128 *(unsigned long *)mem = __KERNEL_DS;
129 break;
130 case GDB_SP:
131 if (!user_mode_vm(regs))
132 *(unsigned long *)mem = kernel_stack_pointer(regs);
133 break;
134 case GDB_GS:
135 case GDB_FS:
136 *(unsigned long *)mem = 0xFFFF;
137 break;
138 }
139#endif
140 return dbg_reg_def[regno].name;
141}
142
143
144
145
146
147
148
149
150
151
152
153
154
155void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
156{
157#ifndef CONFIG_X86_32
158 u32 *gdb_regs32 = (u32 *)gdb_regs;
159#endif
160 gdb_regs[GDB_AX] = 0;
161 gdb_regs[GDB_BX] = 0;
162 gdb_regs[GDB_CX] = 0;
163 gdb_regs[GDB_DX] = 0;
164 gdb_regs[GDB_SI] = 0;
165 gdb_regs[GDB_DI] = 0;
166 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
167#ifdef CONFIG_X86_32
168 gdb_regs[GDB_DS] = __KERNEL_DS;
169 gdb_regs[GDB_ES] = __KERNEL_DS;
170 gdb_regs[GDB_PS] = 0;
171 gdb_regs[GDB_CS] = __KERNEL_CS;
172 gdb_regs[GDB_PC] = p->thread.ip;
173 gdb_regs[GDB_SS] = __KERNEL_DS;
174 gdb_regs[GDB_FS] = 0xFFFF;
175 gdb_regs[GDB_GS] = 0xFFFF;
176#else
177 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
178 gdb_regs32[GDB_CS] = __KERNEL_CS;
179 gdb_regs32[GDB_SS] = __KERNEL_DS;
180 gdb_regs[GDB_PC] = 0;
181 gdb_regs[GDB_R8] = 0;
182 gdb_regs[GDB_R9] = 0;
183 gdb_regs[GDB_R10] = 0;
184 gdb_regs[GDB_R11] = 0;
185 gdb_regs[GDB_R12] = 0;
186 gdb_regs[GDB_R13] = 0;
187 gdb_regs[GDB_R14] = 0;
188 gdb_regs[GDB_R15] = 0;
189#endif
190 gdb_regs[GDB_SP] = p->thread.sp;
191}
192
193static struct hw_breakpoint {
194 unsigned enabled;
195 unsigned long addr;
196 int len;
197 int type;
198 struct perf_event * __percpu *pev;
199} breakinfo[HBP_NUM];
200
201static unsigned long early_dr7;
202
203static void kgdb_correct_hw_break(void)
204{
205 int breakno;
206
207 for (breakno = 0; breakno < HBP_NUM; breakno++) {
208 struct perf_event *bp;
209 struct arch_hw_breakpoint *info;
210 int val;
211 int cpu = raw_smp_processor_id();
212 if (!breakinfo[breakno].enabled)
213 continue;
214 if (dbg_is_early) {
215 set_debugreg(breakinfo[breakno].addr, breakno);
216 early_dr7 |= encode_dr7(breakno,
217 breakinfo[breakno].len,
218 breakinfo[breakno].type);
219 set_debugreg(early_dr7, 7);
220 continue;
221 }
222 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
223 info = counter_arch_bp(bp);
224 if (bp->attr.disabled != 1)
225 continue;
226 bp->attr.bp_addr = breakinfo[breakno].addr;
227 bp->attr.bp_len = breakinfo[breakno].len;
228 bp->attr.bp_type = breakinfo[breakno].type;
229 info->address = breakinfo[breakno].addr;
230 info->len = breakinfo[breakno].len;
231 info->type = breakinfo[breakno].type;
232 val = arch_install_hw_breakpoint(bp);
233 if (!val)
234 bp->attr.disabled = 0;
235 }
236 if (!dbg_is_early)
237 hw_breakpoint_restore();
238}
239
240static int hw_break_reserve_slot(int breakno)
241{
242 int cpu;
243 int cnt = 0;
244 struct perf_event **pevent;
245
246 if (dbg_is_early)
247 return 0;
248
249 for_each_online_cpu(cpu) {
250 cnt++;
251 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
252 if (dbg_reserve_bp_slot(*pevent))
253 goto fail;
254 }
255
256 return 0;
257
258fail:
259 for_each_online_cpu(cpu) {
260 cnt--;
261 if (!cnt)
262 break;
263 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
264 dbg_release_bp_slot(*pevent);
265 }
266 return -1;
267}
268
269static int hw_break_release_slot(int breakno)
270{
271 struct perf_event **pevent;
272 int cpu;
273
274 if (dbg_is_early)
275 return 0;
276
277 for_each_online_cpu(cpu) {
278 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
279 if (dbg_release_bp_slot(*pevent))
280
281
282
283
284 return -1;
285 }
286 return 0;
287}
288
289static int
290kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
291{
292 int i;
293
294 for (i = 0; i < HBP_NUM; i++)
295 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
296 break;
297 if (i == HBP_NUM)
298 return -1;
299
300 if (hw_break_release_slot(i)) {
301 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
302 return -1;
303 }
304 breakinfo[i].enabled = 0;
305
306 return 0;
307}
308
309static void kgdb_remove_all_hw_break(void)
310{
311 int i;
312 int cpu = raw_smp_processor_id();
313 struct perf_event *bp;
314
315 for (i = 0; i < HBP_NUM; i++) {
316 if (!breakinfo[i].enabled)
317 continue;
318 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
319 if (!bp->attr.disabled) {
320 arch_uninstall_hw_breakpoint(bp);
321 bp->attr.disabled = 1;
322 continue;
323 }
324 if (dbg_is_early)
325 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
326 breakinfo[i].type);
327 else if (hw_break_release_slot(i))
328 printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
329 breakinfo[i].addr);
330 breakinfo[i].enabled = 0;
331 }
332}
333
334static int
335kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
336{
337 int i;
338
339 for (i = 0; i < HBP_NUM; i++)
340 if (!breakinfo[i].enabled)
341 break;
342 if (i == HBP_NUM)
343 return -1;
344
345 switch (bptype) {
346 case BP_HARDWARE_BREAKPOINT:
347 len = 1;
348 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
349 break;
350 case BP_WRITE_WATCHPOINT:
351 breakinfo[i].type = X86_BREAKPOINT_WRITE;
352 break;
353 case BP_ACCESS_WATCHPOINT:
354 breakinfo[i].type = X86_BREAKPOINT_RW;
355 break;
356 default:
357 return -1;
358 }
359 switch (len) {
360 case 1:
361 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
362 break;
363 case 2:
364 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
365 break;
366 case 4:
367 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
368 break;
369#ifdef CONFIG_X86_64
370 case 8:
371 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
372 break;
373#endif
374 default:
375 return -1;
376 }
377 breakinfo[i].addr = addr;
378 if (hw_break_reserve_slot(i)) {
379 breakinfo[i].addr = 0;
380 return -1;
381 }
382 breakinfo[i].enabled = 1;
383
384 return 0;
385}
386
387
388
389
390
391
392
393
394
395static void kgdb_disable_hw_debug(struct pt_regs *regs)
396{
397 int i;
398 int cpu = raw_smp_processor_id();
399 struct perf_event *bp;
400
401
402 set_debugreg(0UL, 7);
403 for (i = 0; i < HBP_NUM; i++) {
404 if (!breakinfo[i].enabled)
405 continue;
406 if (dbg_is_early) {
407 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
408 breakinfo[i].type);
409 continue;
410 }
411 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
412 if (bp->attr.disabled == 1)
413 continue;
414 arch_uninstall_hw_breakpoint(bp);
415 bp->attr.disabled = 1;
416 }
417}
418
419#ifdef CONFIG_SMP
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436void kgdb_roundup_cpus(unsigned long flags)
437{
438 apic->send_IPI_allbutself(APIC_DM_NMI);
439}
440#endif
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
459 char *remcomInBuffer, char *remcomOutBuffer,
460 struct pt_regs *linux_regs)
461{
462 unsigned long addr;
463 char *ptr;
464
465 switch (remcomInBuffer[0]) {
466 case 'c':
467 case 's':
468
469 ptr = &remcomInBuffer[1];
470 if (kgdb_hex2long(&ptr, &addr))
471 linux_regs->ip = addr;
472 case 'D':
473 case 'k':
474
475 linux_regs->flags &= ~X86_EFLAGS_TF;
476 atomic_set(&kgdb_cpu_doing_single_step, -1);
477
478
479 if (remcomInBuffer[0] == 's') {
480 linux_regs->flags |= X86_EFLAGS_TF;
481 atomic_set(&kgdb_cpu_doing_single_step,
482 raw_smp_processor_id());
483 }
484
485 return 0;
486 }
487
488
489 return -1;
490}
491
492static inline int
493single_step_cont(struct pt_regs *regs, struct die_args *args)
494{
495
496
497
498
499 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
500 "resuming...\n");
501 kgdb_arch_handle_exception(args->trapnr, args->signr,
502 args->err, "c", "", regs);
503
504
505
506
507 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
508
509 return NOTIFY_STOP;
510}
511
512static int was_in_debug_nmi[NR_CPUS];
513
514static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
515{
516 switch (cmd) {
517 case NMI_LOCAL:
518 if (atomic_read(&kgdb_active) != -1) {
519
520 kgdb_nmicallback(raw_smp_processor_id(), regs);
521 was_in_debug_nmi[raw_smp_processor_id()] = 1;
522 touch_nmi_watchdog();
523 return NMI_HANDLED;
524 }
525 break;
526
527 case NMI_UNKNOWN:
528 if (was_in_debug_nmi[raw_smp_processor_id()]) {
529 was_in_debug_nmi[raw_smp_processor_id()] = 0;
530 return NMI_HANDLED;
531 }
532 break;
533 default:
534
535 break;
536 }
537 return NMI_DONE;
538}
539
540static int __kgdb_notify(struct die_args *args, unsigned long cmd)
541{
542 struct pt_regs *regs = args->regs;
543
544 switch (cmd) {
545 case DIE_DEBUG:
546 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
547 if (user_mode(regs))
548 return single_step_cont(regs, args);
549 break;
550 } else if (test_thread_flag(TIF_SINGLESTEP))
551
552
553
554 return NOTIFY_DONE;
555
556 default:
557 if (user_mode(regs))
558 return NOTIFY_DONE;
559 }
560
561 if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
562 return NOTIFY_DONE;
563
564
565 touch_nmi_watchdog();
566 return NOTIFY_STOP;
567}
568
569int kgdb_ll_trap(int cmd, const char *str,
570 struct pt_regs *regs, long err, int trap, int sig)
571{
572 struct die_args args = {
573 .regs = regs,
574 .str = str,
575 .err = err,
576 .trapnr = trap,
577 .signr = sig,
578
579 };
580
581 if (!kgdb_io_module_registered)
582 return NOTIFY_DONE;
583
584 return __kgdb_notify(&args, cmd);
585}
586
587static int
588kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
589{
590 unsigned long flags;
591 int ret;
592
593 local_irq_save(flags);
594 ret = __kgdb_notify(ptr, cmd);
595 local_irq_restore(flags);
596
597 return ret;
598}
599
600static struct notifier_block kgdb_notifier = {
601 .notifier_call = kgdb_notify,
602};
603
604
605
606
607
608
609
610int kgdb_arch_init(void)
611{
612 int retval;
613
614 retval = register_die_notifier(&kgdb_notifier);
615 if (retval)
616 goto out;
617
618 retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
619 0, "kgdb");
620 if (retval)
621 goto out1;
622
623 retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
624 0, "kgdb");
625
626 if (retval)
627 goto out2;
628
629 return retval;
630
631out2:
632 unregister_nmi_handler(NMI_LOCAL, "kgdb");
633out1:
634 unregister_die_notifier(&kgdb_notifier);
635out:
636 return retval;
637}
638
639static void kgdb_hw_overflow_handler(struct perf_event *event,
640 struct perf_sample_data *data, struct pt_regs *regs)
641{
642 struct task_struct *tsk = current;
643 int i;
644
645 for (i = 0; i < 4; i++)
646 if (breakinfo[i].enabled)
647 tsk->thread.debugreg6 |= (DR_TRAP0 << i);
648}
649
650void kgdb_arch_late(void)
651{
652 int i, cpu;
653 struct perf_event_attr attr;
654 struct perf_event **pevent;
655
656
657
658
659
660
661 hw_breakpoint_init(&attr);
662 attr.bp_addr = (unsigned long)kgdb_arch_init;
663 attr.bp_len = HW_BREAKPOINT_LEN_1;
664 attr.bp_type = HW_BREAKPOINT_W;
665 attr.disabled = 1;
666 for (i = 0; i < HBP_NUM; i++) {
667 if (breakinfo[i].pev)
668 continue;
669 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
670 if (IS_ERR((void * __force)breakinfo[i].pev)) {
671 printk(KERN_ERR "kgdb: Could not allocate hw"
672 "breakpoints\nDisabling the kernel debugger\n");
673 breakinfo[i].pev = NULL;
674 kgdb_arch_exit();
675 return;
676 }
677 for_each_online_cpu(cpu) {
678 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
679 pevent[0]->hw.sample_period = 1;
680 pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
681 if (pevent[0]->destroy != NULL) {
682 pevent[0]->destroy = NULL;
683 release_bp_slot(*pevent);
684 }
685 }
686 }
687}
688
689
690
691
692
693
694
695void kgdb_arch_exit(void)
696{
697 int i;
698 for (i = 0; i < 4; i++) {
699 if (breakinfo[i].pev) {
700 unregister_wide_hw_breakpoint(breakinfo[i].pev);
701 breakinfo[i].pev = NULL;
702 }
703 }
704 unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
705 unregister_nmi_handler(NMI_LOCAL, "kgdb");
706 unregister_die_notifier(&kgdb_notifier);
707}
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722int kgdb_skipexception(int exception, struct pt_regs *regs)
723{
724 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
725 regs->ip -= 1;
726 return 1;
727 }
728 return 0;
729}
730
731unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
732{
733 if (exception == 3)
734 return instruction_pointer(regs) - 1;
735 return instruction_pointer(regs);
736}
737
738void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
739{
740 regs->ip = ip;
741}
742
743struct kgdb_arch arch_kgdb_ops = {
744
745 .gdb_bpt_instr = { 0xcc },
746 .flags = KGDB_HW_BREAKPOINT,
747 .set_hw_breakpoint = kgdb_set_hw_break,
748 .remove_hw_breakpoint = kgdb_remove_hw_break,
749 .disable_hw_break = kgdb_disable_hw_debug,
750 .remove_all_hw_break = kgdb_remove_all_hw_break,
751 .correct_hw_break = kgdb_correct_hw_break,
752};
753