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