13b13311b792564f12d0adf78d64fd770be6d16c
[sfrench/cifs-2.6.git] / arch / x86 / kernel / kgdb.c
1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by the
4  * Free Software Foundation; either version 2, or (at your option) any
5  * later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * General Public License for more details.
11  *
12  */
13
14 /*
15  * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com>
16  * Copyright (C) 2000-2001 VERITAS Software Corporation.
17  * Copyright (C) 2002 Andi Kleen, SuSE Labs
18  * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd.
19  * Copyright (C) 2007 MontaVista Software, Inc.
20  * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc.
21  */
22 /****************************************************************************
23  *  Contributor:     Lake Stevens Instrument Division$
24  *  Written by:      Glenn Engel $
25  *  Updated by:      Amit Kale<akale@veritas.com>
26  *  Updated by:      Tom Rini <trini@kernel.crashing.org>
27  *  Updated by:      Jason Wessel <jason.wessel@windriver.com>
28  *  Modified for 386 by Jim Kingdon, Cygnus Support.
29  *  Origianl kgdb, compatibility with 2.1.xx kernel by
30  *  David Grothe <dave@gcom.com>
31  *  Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com>
32  *  X86_64 changes from Andi Kleen's patch merged by Jim Houston
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/smp.h>
43 #include <linux/nmi.h>
44 #include <linux/hw_breakpoint.h>
45 #include <linux/uaccess.h>
46 #include <linux/memory.h>
47
48 #include <asm/text-patching.h>
49 #include <asm/debugreg.h>
50 #include <asm/apicdef.h>
51 #include <asm/apic.h>
52 #include <asm/nmi.h>
53 #include <asm/switch_to.h>
54
55 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
56 {
57 #ifdef CONFIG_X86_32
58         { "ax", 4, offsetof(struct pt_regs, ax) },
59         { "cx", 4, offsetof(struct pt_regs, cx) },
60         { "dx", 4, offsetof(struct pt_regs, dx) },
61         { "bx", 4, offsetof(struct pt_regs, bx) },
62         { "sp", 4, offsetof(struct pt_regs, sp) },
63         { "bp", 4, offsetof(struct pt_regs, bp) },
64         { "si", 4, offsetof(struct pt_regs, si) },
65         { "di", 4, offsetof(struct pt_regs, di) },
66         { "ip", 4, offsetof(struct pt_regs, ip) },
67         { "flags", 4, offsetof(struct pt_regs, flags) },
68         { "cs", 4, offsetof(struct pt_regs, cs) },
69         { "ss", 4, offsetof(struct pt_regs, ss) },
70         { "ds", 4, offsetof(struct pt_regs, ds) },
71         { "es", 4, offsetof(struct pt_regs, es) },
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, si) },
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         { "ds", 4, -1 },
94         { "es", 4, -1 },
95 #endif
96         { "fs", 4, -1 },
97         { "gs", 4, -1 },
98 };
99
100 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
101 {
102         if (
103 #ifdef CONFIG_X86_32
104             regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
105 #endif
106             regno == GDB_SP || regno == GDB_ORIG_AX)
107                 return 0;
108
109         if (dbg_reg_def[regno].offset != -1)
110                 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
111                        dbg_reg_def[regno].size);
112         return 0;
113 }
114
115 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
116 {
117         if (regno == GDB_ORIG_AX) {
118                 memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
119                 return "orig_ax";
120         }
121         if (regno >= DBG_MAX_REG_NUM || regno < 0)
122                 return NULL;
123
124         if (dbg_reg_def[regno].offset != -1)
125                 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
126                        dbg_reg_def[regno].size);
127
128 #ifdef CONFIG_X86_32
129         switch (regno) {
130         case GDB_SS:
131                 if (!user_mode(regs))
132                         *(unsigned long *)mem = __KERNEL_DS;
133                 break;
134         case GDB_SP:
135                 if (!user_mode(regs))
136                         *(unsigned long *)mem = kernel_stack_pointer(regs);
137                 break;
138         case GDB_GS:
139         case GDB_FS:
140                 *(unsigned long *)mem = 0xFFFF;
141                 break;
142         }
143 #endif
144         return dbg_reg_def[regno].name;
145 }
146
147 /**
148  *      sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
149  *      @gdb_regs: A pointer to hold the registers in the order GDB wants.
150  *      @p: The &struct task_struct of the desired process.
151  *
152  *      Convert the register values of the sleeping process in @p to
153  *      the format that GDB expects.
154  *      This function is called when kgdb does not have access to the
155  *      &struct pt_regs and therefore it should fill the gdb registers
156  *      @gdb_regs with what has been saved in &struct thread_struct
157  *      thread field during switch_to.
158  */
159 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
160 {
161 #ifndef CONFIG_X86_32
162         u32 *gdb_regs32 = (u32 *)gdb_regs;
163 #endif
164         gdb_regs[GDB_AX]        = 0;
165         gdb_regs[GDB_BX]        = 0;
166         gdb_regs[GDB_CX]        = 0;
167         gdb_regs[GDB_DX]        = 0;
168         gdb_regs[GDB_SI]        = 0;
169         gdb_regs[GDB_DI]        = 0;
170         gdb_regs[GDB_BP]        = ((struct inactive_task_frame *)p->thread.sp)->bp;
171 #ifdef CONFIG_X86_32
172         gdb_regs[GDB_DS]        = __KERNEL_DS;
173         gdb_regs[GDB_ES]        = __KERNEL_DS;
174         gdb_regs[GDB_PS]        = 0;
175         gdb_regs[GDB_CS]        = __KERNEL_CS;
176         gdb_regs[GDB_SS]        = __KERNEL_DS;
177         gdb_regs[GDB_FS]        = 0xFFFF;
178         gdb_regs[GDB_GS]        = 0xFFFF;
179 #else
180         gdb_regs32[GDB_PS]      = 0;
181         gdb_regs32[GDB_CS]      = __KERNEL_CS;
182         gdb_regs32[GDB_SS]      = __KERNEL_DS;
183         gdb_regs[GDB_R8]        = 0;
184         gdb_regs[GDB_R9]        = 0;
185         gdb_regs[GDB_R10]       = 0;
186         gdb_regs[GDB_R11]       = 0;
187         gdb_regs[GDB_R12]       = 0;
188         gdb_regs[GDB_R13]       = 0;
189         gdb_regs[GDB_R14]       = 0;
190         gdb_regs[GDB_R15]       = 0;
191 #endif
192         gdb_regs[GDB_PC]        = 0;
193         gdb_regs[GDB_SP]        = p->thread.sp;
194 }
195
196 static struct hw_breakpoint {
197         unsigned                enabled;
198         unsigned long           addr;
199         int                     len;
200         int                     type;
201         struct perf_event       * __percpu *pev;
202 } breakinfo[HBP_NUM];
203
204 static unsigned long early_dr7;
205
206 static void kgdb_correct_hw_break(void)
207 {
208         int breakno;
209
210         for (breakno = 0; breakno < HBP_NUM; breakno++) {
211                 struct perf_event *bp;
212                 struct arch_hw_breakpoint *info;
213                 int val;
214                 int cpu = raw_smp_processor_id();
215                 if (!breakinfo[breakno].enabled)
216                         continue;
217                 if (dbg_is_early) {
218                         set_debugreg(breakinfo[breakno].addr, breakno);
219                         early_dr7 |= encode_dr7(breakno,
220                                                 breakinfo[breakno].len,
221                                                 breakinfo[breakno].type);
222                         set_debugreg(early_dr7, 7);
223                         continue;
224                 }
225                 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
226                 info = counter_arch_bp(bp);
227                 if (bp->attr.disabled != 1)
228                         continue;
229                 bp->attr.bp_addr = breakinfo[breakno].addr;
230                 bp->attr.bp_len = breakinfo[breakno].len;
231                 bp->attr.bp_type = breakinfo[breakno].type;
232                 info->address = breakinfo[breakno].addr;
233                 info->len = breakinfo[breakno].len;
234                 info->type = breakinfo[breakno].type;
235                 val = arch_install_hw_breakpoint(bp);
236                 if (!val)
237                         bp->attr.disabled = 0;
238         }
239         if (!dbg_is_early)
240                 hw_breakpoint_restore();
241 }
242
243 static int hw_break_reserve_slot(int breakno)
244 {
245         int cpu;
246         int cnt = 0;
247         struct perf_event **pevent;
248
249         if (dbg_is_early)
250                 return 0;
251
252         for_each_online_cpu(cpu) {
253                 cnt++;
254                 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
255                 if (dbg_reserve_bp_slot(*pevent))
256                         goto fail;
257         }
258
259         return 0;
260
261 fail:
262         for_each_online_cpu(cpu) {
263                 cnt--;
264                 if (!cnt)
265                         break;
266                 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
267                 dbg_release_bp_slot(*pevent);
268         }
269         return -1;
270 }
271
272 static int hw_break_release_slot(int breakno)
273 {
274         struct perf_event **pevent;
275         int cpu;
276
277         if (dbg_is_early)
278                 return 0;
279
280         for_each_online_cpu(cpu) {
281                 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
282                 if (dbg_release_bp_slot(*pevent))
283                         /*
284                          * The debugger is responsible for handing the retry on
285                          * remove failure.
286                          */
287                         return -1;
288         }
289         return 0;
290 }
291
292 static int
293 kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
294 {
295         int i;
296
297         for (i = 0; i < HBP_NUM; i++)
298                 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
299                         break;
300         if (i == HBP_NUM)
301                 return -1;
302
303         if (hw_break_release_slot(i)) {
304                 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
305                 return -1;
306         }
307         breakinfo[i].enabled = 0;
308
309         return 0;
310 }
311
312 static void kgdb_remove_all_hw_break(void)
313 {
314         int i;
315         int cpu = raw_smp_processor_id();
316         struct perf_event *bp;
317
318         for (i = 0; i < HBP_NUM; i++) {
319                 if (!breakinfo[i].enabled)
320                         continue;
321                 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
322                 if (!bp->attr.disabled) {
323                         arch_uninstall_hw_breakpoint(bp);
324                         bp->attr.disabled = 1;
325                         continue;
326                 }
327                 if (dbg_is_early)
328                         early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
329                                                  breakinfo[i].type);
330                 else if (hw_break_release_slot(i))
331                         printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
332                                breakinfo[i].addr);
333                 breakinfo[i].enabled = 0;
334         }
335 }
336
337 static int
338 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
339 {
340         int i;
341
342         for (i = 0; i < HBP_NUM; i++)
343                 if (!breakinfo[i].enabled)
344                         break;
345         if (i == HBP_NUM)
346                 return -1;
347
348         switch (bptype) {
349         case BP_HARDWARE_BREAKPOINT:
350                 len = 1;
351                 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
352                 break;
353         case BP_WRITE_WATCHPOINT:
354                 breakinfo[i].type = X86_BREAKPOINT_WRITE;
355                 break;
356         case BP_ACCESS_WATCHPOINT:
357                 breakinfo[i].type = X86_BREAKPOINT_RW;
358                 break;
359         default:
360                 return -1;
361         }
362         switch (len) {
363         case 1:
364                 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
365                 break;
366         case 2:
367                 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
368                 break;
369         case 4:
370                 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
371                 break;
372 #ifdef CONFIG_X86_64
373         case 8:
374                 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
375                 break;
376 #endif
377         default:
378                 return -1;
379         }
380         breakinfo[i].addr = addr;
381         if (hw_break_reserve_slot(i)) {
382                 breakinfo[i].addr = 0;
383                 return -1;
384         }
385         breakinfo[i].enabled = 1;
386
387         return 0;
388 }
389
390 /**
391  *      kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
392  *      @regs: Current &struct pt_regs.
393  *
394  *      This function will be called if the particular architecture must
395  *      disable hardware debugging while it is processing gdb packets or
396  *      handling exception.
397  */
398 static void kgdb_disable_hw_debug(struct pt_regs *regs)
399 {
400         int i;
401         int cpu = raw_smp_processor_id();
402         struct perf_event *bp;
403
404         /* Disable hardware debugging while we are in kgdb: */
405         set_debugreg(0UL, 7);
406         for (i = 0; i < HBP_NUM; i++) {
407                 if (!breakinfo[i].enabled)
408                         continue;
409                 if (dbg_is_early) {
410                         early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
411                                                  breakinfo[i].type);
412                         continue;
413                 }
414                 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
415                 if (bp->attr.disabled == 1)
416                         continue;
417                 arch_uninstall_hw_breakpoint(bp);
418                 bp->attr.disabled = 1;
419         }
420 }
421
422 #ifdef CONFIG_SMP
423 /**
424  *      kgdb_roundup_cpus - Get other CPUs into a holding pattern
425  *
426  *      On SMP systems, we need to get the attention of the other CPUs
427  *      and get them be in a known state.  This should do what is needed
428  *      to get the other CPUs to call kgdb_wait(). Note that on some arches,
429  *      the NMI approach is not used for rounding up all the CPUs. For example,
430  *      in case of MIPS, smp_call_function() is used to roundup CPUs.
431  *
432  *      On non-SMP systems, this is not called.
433  */
434 void kgdb_roundup_cpus(void)
435 {
436         apic->send_IPI_allbutself(APIC_DM_NMI);
437 }
438 #endif
439
440 /**
441  *      kgdb_arch_handle_exception - Handle architecture specific GDB packets.
442  *      @e_vector: The error vector of the exception that happened.
443  *      @signo: The signal number of the exception that happened.
444  *      @err_code: The error code of the exception that happened.
445  *      @remcomInBuffer: The buffer of the packet we have read.
446  *      @remcomOutBuffer: The buffer of %BUFMAX bytes to write a packet into.
447  *      @linux_regs: The &struct pt_regs of the current process.
448  *
449  *      This function MUST handle the 'c' and 's' command packets,
450  *      as well packets to set / remove a hardware breakpoint, if used.
451  *      If there are additional packets which the hardware needs to handle,
452  *      they are handled here.  The code should return -1 if it wants to
453  *      process more packets, and a %0 or %1 if it wants to exit from the
454  *      kgdb callback.
455  */
456 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
457                                char *remcomInBuffer, char *remcomOutBuffer,
458                                struct pt_regs *linux_regs)
459 {
460         unsigned long addr;
461         char *ptr;
462
463         switch (remcomInBuffer[0]) {
464         case 'c':
465         case 's':
466                 /* try to read optional parameter, pc unchanged if no parm */
467                 ptr = &remcomInBuffer[1];
468                 if (kgdb_hex2long(&ptr, &addr))
469                         linux_regs->ip = addr;
470                 /* fall through */
471         case 'D':
472         case 'k':
473                 /* clear the trace bit */
474                 linux_regs->flags &= ~X86_EFLAGS_TF;
475                 atomic_set(&kgdb_cpu_doing_single_step, -1);
476
477                 /* set the trace bit if we're stepping */
478                 if (remcomInBuffer[0] == 's') {
479                         linux_regs->flags |= X86_EFLAGS_TF;
480                         atomic_set(&kgdb_cpu_doing_single_step,
481                                    raw_smp_processor_id());
482                 }
483
484                 return 0;
485         }
486
487         /* this means that we do not want to exit from the handler: */
488         return -1;
489 }
490
491 static inline int
492 single_step_cont(struct pt_regs *regs, struct die_args *args)
493 {
494         /*
495          * Single step exception from kernel space to user space so
496          * eat the exception and continue the process:
497          */
498         printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
499                         "resuming...\n");
500         kgdb_arch_handle_exception(args->trapnr, args->signr,
501                                    args->err, "c", "", regs);
502         /*
503          * Reset the BS bit in dr6 (pointed by args->err) to
504          * denote completion of processing
505          */
506         (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
507
508         return NOTIFY_STOP;
509 }
510
511 static DECLARE_BITMAP(was_in_debug_nmi, NR_CPUS);
512
513 static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
514 {
515         int cpu;
516
517         switch (cmd) {
518         case NMI_LOCAL:
519                 if (atomic_read(&kgdb_active) != -1) {
520                         /* KGDB CPU roundup */
521                         cpu = raw_smp_processor_id();
522                         kgdb_nmicallback(cpu, regs);
523                         set_bit(cpu, was_in_debug_nmi);
524                         touch_nmi_watchdog();
525
526                         return NMI_HANDLED;
527                 }
528                 break;
529
530         case NMI_UNKNOWN:
531                 cpu = raw_smp_processor_id();
532
533                 if (__test_and_clear_bit(cpu, was_in_debug_nmi))
534                         return NMI_HANDLED;
535
536                 break;
537         default:
538                 /* do nothing */
539                 break;
540         }
541         return NMI_DONE;
542 }
543
544 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
545 {
546         struct pt_regs *regs = args->regs;
547
548         switch (cmd) {
549         case DIE_DEBUG:
550                 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
551                         if (user_mode(regs))
552                                 return single_step_cont(regs, args);
553                         break;
554                 } else if (test_thread_flag(TIF_SINGLESTEP))
555                         /* This means a user thread is single stepping
556                          * a system call which should be ignored
557                          */
558                         return NOTIFY_DONE;
559                 /* fall through */
560         default:
561                 if (user_mode(regs))
562                         return NOTIFY_DONE;
563         }
564
565         if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
566                 return NOTIFY_DONE;
567
568         /* Must touch watchdog before return to normal operation */
569         touch_nmi_watchdog();
570         return NOTIFY_STOP;
571 }
572
573 int kgdb_ll_trap(int cmd, const char *str,
574                  struct pt_regs *regs, long err, int trap, int sig)
575 {
576         struct die_args args = {
577                 .regs   = regs,
578                 .str    = str,
579                 .err    = err,
580                 .trapnr = trap,
581                 .signr  = sig,
582
583         };
584
585         if (!kgdb_io_module_registered)
586                 return NOTIFY_DONE;
587
588         return __kgdb_notify(&args, cmd);
589 }
590
591 static int
592 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
593 {
594         unsigned long flags;
595         int ret;
596
597         local_irq_save(flags);
598         ret = __kgdb_notify(ptr, cmd);
599         local_irq_restore(flags);
600
601         return ret;
602 }
603
604 static struct notifier_block kgdb_notifier = {
605         .notifier_call  = kgdb_notify,
606 };
607
608 /**
609  *      kgdb_arch_init - Perform any architecture specific initialization.
610  *
611  *      This function will handle the initialization of any architecture
612  *      specific callbacks.
613  */
614 int kgdb_arch_init(void)
615 {
616         int retval;
617
618         retval = register_die_notifier(&kgdb_notifier);
619         if (retval)
620                 goto out;
621
622         retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
623                                         0, "kgdb");
624         if (retval)
625                 goto out1;
626
627         retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
628                                         0, "kgdb");
629
630         if (retval)
631                 goto out2;
632
633         return retval;
634
635 out2:
636         unregister_nmi_handler(NMI_LOCAL, "kgdb");
637 out1:
638         unregister_die_notifier(&kgdb_notifier);
639 out:
640         return retval;
641 }
642
643 static void kgdb_hw_overflow_handler(struct perf_event *event,
644                 struct perf_sample_data *data, struct pt_regs *regs)
645 {
646         struct task_struct *tsk = current;
647         int i;
648
649         for (i = 0; i < 4; i++)
650                 if (breakinfo[i].enabled)
651                         tsk->thread.debugreg6 |= (DR_TRAP0 << i);
652 }
653
654 void kgdb_arch_late(void)
655 {
656         int i, cpu;
657         struct perf_event_attr attr;
658         struct perf_event **pevent;
659
660         /*
661          * Pre-allocate the hw breakpoint structions in the non-atomic
662          * portion of kgdb because this operation requires mutexs to
663          * complete.
664          */
665         hw_breakpoint_init(&attr);
666         attr.bp_addr = (unsigned long)kgdb_arch_init;
667         attr.bp_len = HW_BREAKPOINT_LEN_1;
668         attr.bp_type = HW_BREAKPOINT_W;
669         attr.disabled = 1;
670         for (i = 0; i < HBP_NUM; i++) {
671                 if (breakinfo[i].pev)
672                         continue;
673                 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
674                 if (IS_ERR((void * __force)breakinfo[i].pev)) {
675                         printk(KERN_ERR "kgdb: Could not allocate hw"
676                                "breakpoints\nDisabling the kernel debugger\n");
677                         breakinfo[i].pev = NULL;
678                         kgdb_arch_exit();
679                         return;
680                 }
681                 for_each_online_cpu(cpu) {
682                         pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
683                         pevent[0]->hw.sample_period = 1;
684                         pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
685                         if (pevent[0]->destroy != NULL) {
686                                 pevent[0]->destroy = NULL;
687                                 release_bp_slot(*pevent);
688                         }
689                 }
690         }
691 }
692
693 /**
694  *      kgdb_arch_exit - Perform any architecture specific uninitalization.
695  *
696  *      This function will handle the uninitalization of any architecture
697  *      specific callbacks, for dynamic registration and unregistration.
698  */
699 void kgdb_arch_exit(void)
700 {
701         int i;
702         for (i = 0; i < 4; i++) {
703                 if (breakinfo[i].pev) {
704                         unregister_wide_hw_breakpoint(breakinfo[i].pev);
705                         breakinfo[i].pev = NULL;
706                 }
707         }
708         unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
709         unregister_nmi_handler(NMI_LOCAL, "kgdb");
710         unregister_die_notifier(&kgdb_notifier);
711 }
712
713 /**
714  *
715  *      kgdb_skipexception - Bail out of KGDB when we've been triggered.
716  *      @exception: Exception vector number
717  *      @regs: Current &struct pt_regs.
718  *
719  *      On some architectures we need to skip a breakpoint exception when
720  *      it occurs after a breakpoint has been removed.
721  *
722  * Skip an int3 exception when it occurs after a breakpoint has been
723  * removed. Backtrack eip by 1 since the int3 would have caused it to
724  * increment by 1.
725  */
726 int kgdb_skipexception(int exception, struct pt_regs *regs)
727 {
728         if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
729                 regs->ip -= 1;
730                 return 1;
731         }
732         return 0;
733 }
734
735 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
736 {
737         if (exception == 3)
738                 return instruction_pointer(regs) - 1;
739         return instruction_pointer(regs);
740 }
741
742 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
743 {
744         regs->ip = ip;
745 }
746
747 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
748 {
749         int err;
750
751         bpt->type = BP_BREAKPOINT;
752         err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
753                                 BREAK_INSTR_SIZE);
754         if (err)
755                 return err;
756         err = probe_kernel_write((char *)bpt->bpt_addr,
757                                  arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
758         if (!err)
759                 return err;
760         /*
761          * It is safe to call text_poke_kgdb() because normal kernel execution
762          * is stopped on all cores, so long as the text_mutex is not locked.
763          */
764         if (mutex_is_locked(&text_mutex))
765                 return -EBUSY;
766         text_poke_kgdb((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
767                        BREAK_INSTR_SIZE);
768         bpt->type = BP_POKE_BREAKPOINT;
769
770         return err;
771 }
772
773 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
774 {
775         if (bpt->type != BP_POKE_BREAKPOINT)
776                 goto knl_write;
777         /*
778          * It is safe to call text_poke_kgdb() because normal kernel execution
779          * is stopped on all cores, so long as the text_mutex is not locked.
780          */
781         if (mutex_is_locked(&text_mutex))
782                 goto knl_write;
783         text_poke_kgdb((void *)bpt->bpt_addr, bpt->saved_instr,
784                        BREAK_INSTR_SIZE);
785         return 0;
786
787 knl_write:
788         return probe_kernel_write((char *)bpt->bpt_addr,
789                                   (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
790 }
791
792 const struct kgdb_arch arch_kgdb_ops = {
793         /* Breakpoint instruction: */
794         .gdb_bpt_instr          = { 0xcc },
795         .flags                  = KGDB_HW_BREAKPOINT,
796         .set_hw_breakpoint      = kgdb_set_hw_break,
797         .remove_hw_breakpoint   = kgdb_remove_hw_break,
798         .disable_hw_break       = kgdb_disable_hw_debug,
799         .remove_all_hw_break    = kgdb_remove_all_hw_break,
800         .correct_hw_break       = kgdb_correct_hw_break,
801 };