Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  */
0004 
0005 /*
0006  * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com>
0007  * Copyright (C) 2000-2001 VERITAS Software Corporation.
0008  * Copyright (C) 2002 Andi Kleen, SuSE Labs
0009  * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd.
0010  * Copyright (C) 2007 MontaVista Software, Inc.
0011  * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc.
0012  */
0013 /****************************************************************************
0014  *  Contributor:     Lake Stevens Instrument Division$
0015  *  Written by:      Glenn Engel $
0016  *  Updated by:      Amit Kale<akale@veritas.com>
0017  *  Updated by:      Tom Rini <trini@kernel.crashing.org>
0018  *  Updated by:      Jason Wessel <jason.wessel@windriver.com>
0019  *  Modified for 386 by Jim Kingdon, Cygnus Support.
0020  *  Original kgdb, compatibility with 2.1.xx kernel by
0021  *  David Grothe <dave@gcom.com>
0022  *  Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com>
0023  *  X86_64 changes from Andi Kleen's patch merged by Jim Houston
0024  */
0025 #include <linux/spinlock.h>
0026 #include <linux/kdebug.h>
0027 #include <linux/string.h>
0028 #include <linux/kernel.h>
0029 #include <linux/ptrace.h>
0030 #include <linux/sched.h>
0031 #include <linux/delay.h>
0032 #include <linux/kgdb.h>
0033 #include <linux/smp.h>
0034 #include <linux/nmi.h>
0035 #include <linux/hw_breakpoint.h>
0036 #include <linux/uaccess.h>
0037 #include <linux/memory.h>
0038 
0039 #include <asm/text-patching.h>
0040 #include <asm/debugreg.h>
0041 #include <asm/apicdef.h>
0042 #include <asm/apic.h>
0043 #include <asm/nmi.h>
0044 #include <asm/switch_to.h>
0045 
0046 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
0047 {
0048 #ifdef CONFIG_X86_32
0049     { "ax", 4, offsetof(struct pt_regs, ax) },
0050     { "cx", 4, offsetof(struct pt_regs, cx) },
0051     { "dx", 4, offsetof(struct pt_regs, dx) },
0052     { "bx", 4, offsetof(struct pt_regs, bx) },
0053     { "sp", 4, offsetof(struct pt_regs, sp) },
0054     { "bp", 4, offsetof(struct pt_regs, bp) },
0055     { "si", 4, offsetof(struct pt_regs, si) },
0056     { "di", 4, offsetof(struct pt_regs, di) },
0057     { "ip", 4, offsetof(struct pt_regs, ip) },
0058     { "flags", 4, offsetof(struct pt_regs, flags) },
0059     { "cs", 4, offsetof(struct pt_regs, cs) },
0060     { "ss", 4, offsetof(struct pt_regs, ss) },
0061     { "ds", 4, offsetof(struct pt_regs, ds) },
0062     { "es", 4, offsetof(struct pt_regs, es) },
0063 #else
0064     { "ax", 8, offsetof(struct pt_regs, ax) },
0065     { "bx", 8, offsetof(struct pt_regs, bx) },
0066     { "cx", 8, offsetof(struct pt_regs, cx) },
0067     { "dx", 8, offsetof(struct pt_regs, dx) },
0068     { "si", 8, offsetof(struct pt_regs, si) },
0069     { "di", 8, offsetof(struct pt_regs, di) },
0070     { "bp", 8, offsetof(struct pt_regs, bp) },
0071     { "sp", 8, offsetof(struct pt_regs, sp) },
0072     { "r8", 8, offsetof(struct pt_regs, r8) },
0073     { "r9", 8, offsetof(struct pt_regs, r9) },
0074     { "r10", 8, offsetof(struct pt_regs, r10) },
0075     { "r11", 8, offsetof(struct pt_regs, r11) },
0076     { "r12", 8, offsetof(struct pt_regs, r12) },
0077     { "r13", 8, offsetof(struct pt_regs, r13) },
0078     { "r14", 8, offsetof(struct pt_regs, r14) },
0079     { "r15", 8, offsetof(struct pt_regs, r15) },
0080     { "ip", 8, offsetof(struct pt_regs, ip) },
0081     { "flags", 4, offsetof(struct pt_regs, flags) },
0082     { "cs", 4, offsetof(struct pt_regs, cs) },
0083     { "ss", 4, offsetof(struct pt_regs, ss) },
0084     { "ds", 4, -1 },
0085     { "es", 4, -1 },
0086 #endif
0087     { "fs", 4, -1 },
0088     { "gs", 4, -1 },
0089 };
0090 
0091 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
0092 {
0093     if (
0094 #ifdef CONFIG_X86_32
0095         regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
0096 #endif
0097         regno == GDB_SP || regno == GDB_ORIG_AX)
0098         return 0;
0099 
0100     if (dbg_reg_def[regno].offset != -1)
0101         memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
0102                dbg_reg_def[regno].size);
0103     return 0;
0104 }
0105 
0106 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
0107 {
0108     if (regno == GDB_ORIG_AX) {
0109         memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
0110         return "orig_ax";
0111     }
0112     if (regno >= DBG_MAX_REG_NUM || regno < 0)
0113         return NULL;
0114 
0115     if (dbg_reg_def[regno].offset != -1)
0116         memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
0117                dbg_reg_def[regno].size);
0118 
0119 #ifdef CONFIG_X86_32
0120     switch (regno) {
0121     case GDB_GS:
0122     case GDB_FS:
0123         *(unsigned long *)mem = 0xFFFF;
0124         break;
0125     }
0126 #endif
0127     return dbg_reg_def[regno].name;
0128 }
0129 
0130 /**
0131  *  sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
0132  *  @gdb_regs: A pointer to hold the registers in the order GDB wants.
0133  *  @p: The &struct task_struct of the desired process.
0134  *
0135  *  Convert the register values of the sleeping process in @p to
0136  *  the format that GDB expects.
0137  *  This function is called when kgdb does not have access to the
0138  *  &struct pt_regs and therefore it should fill the gdb registers
0139  *  @gdb_regs with what has been saved in &struct thread_struct
0140  *  thread field during switch_to.
0141  */
0142 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
0143 {
0144 #ifndef CONFIG_X86_32
0145     u32 *gdb_regs32 = (u32 *)gdb_regs;
0146 #endif
0147     gdb_regs[GDB_AX]    = 0;
0148     gdb_regs[GDB_BX]    = 0;
0149     gdb_regs[GDB_CX]    = 0;
0150     gdb_regs[GDB_DX]    = 0;
0151     gdb_regs[GDB_SI]    = 0;
0152     gdb_regs[GDB_DI]    = 0;
0153     gdb_regs[GDB_BP]    = ((struct inactive_task_frame *)p->thread.sp)->bp;
0154 #ifdef CONFIG_X86_32
0155     gdb_regs[GDB_DS]    = __KERNEL_DS;
0156     gdb_regs[GDB_ES]    = __KERNEL_DS;
0157     gdb_regs[GDB_PS]    = 0;
0158     gdb_regs[GDB_CS]    = __KERNEL_CS;
0159     gdb_regs[GDB_SS]    = __KERNEL_DS;
0160     gdb_regs[GDB_FS]    = 0xFFFF;
0161     gdb_regs[GDB_GS]    = 0xFFFF;
0162 #else
0163     gdb_regs32[GDB_PS]  = 0;
0164     gdb_regs32[GDB_CS]  = __KERNEL_CS;
0165     gdb_regs32[GDB_SS]  = __KERNEL_DS;
0166     gdb_regs[GDB_R8]    = 0;
0167     gdb_regs[GDB_R9]    = 0;
0168     gdb_regs[GDB_R10]   = 0;
0169     gdb_regs[GDB_R11]   = 0;
0170     gdb_regs[GDB_R12]   = 0;
0171     gdb_regs[GDB_R13]   = 0;
0172     gdb_regs[GDB_R14]   = 0;
0173     gdb_regs[GDB_R15]   = 0;
0174 #endif
0175     gdb_regs[GDB_PC]    = 0;
0176     gdb_regs[GDB_SP]    = p->thread.sp;
0177 }
0178 
0179 static struct hw_breakpoint {
0180     unsigned        enabled;
0181     unsigned long       addr;
0182     int         len;
0183     int         type;
0184     struct perf_event   * __percpu *pev;
0185 } breakinfo[HBP_NUM];
0186 
0187 static unsigned long early_dr7;
0188 
0189 static void kgdb_correct_hw_break(void)
0190 {
0191     int breakno;
0192 
0193     for (breakno = 0; breakno < HBP_NUM; breakno++) {
0194         struct perf_event *bp;
0195         struct arch_hw_breakpoint *info;
0196         int val;
0197         int cpu = raw_smp_processor_id();
0198         if (!breakinfo[breakno].enabled)
0199             continue;
0200         if (dbg_is_early) {
0201             set_debugreg(breakinfo[breakno].addr, breakno);
0202             early_dr7 |= encode_dr7(breakno,
0203                         breakinfo[breakno].len,
0204                         breakinfo[breakno].type);
0205             set_debugreg(early_dr7, 7);
0206             continue;
0207         }
0208         bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
0209         info = counter_arch_bp(bp);
0210         if (bp->attr.disabled != 1)
0211             continue;
0212         bp->attr.bp_addr = breakinfo[breakno].addr;
0213         bp->attr.bp_len = breakinfo[breakno].len;
0214         bp->attr.bp_type = breakinfo[breakno].type;
0215         info->address = breakinfo[breakno].addr;
0216         info->len = breakinfo[breakno].len;
0217         info->type = breakinfo[breakno].type;
0218         val = arch_install_hw_breakpoint(bp);
0219         if (!val)
0220             bp->attr.disabled = 0;
0221     }
0222     if (!dbg_is_early)
0223         hw_breakpoint_restore();
0224 }
0225 
0226 static int hw_break_reserve_slot(int breakno)
0227 {
0228     int cpu;
0229     int cnt = 0;
0230     struct perf_event **pevent;
0231 
0232     if (dbg_is_early)
0233         return 0;
0234 
0235     for_each_online_cpu(cpu) {
0236         cnt++;
0237         pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
0238         if (dbg_reserve_bp_slot(*pevent))
0239             goto fail;
0240     }
0241 
0242     return 0;
0243 
0244 fail:
0245     for_each_online_cpu(cpu) {
0246         cnt--;
0247         if (!cnt)
0248             break;
0249         pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
0250         dbg_release_bp_slot(*pevent);
0251     }
0252     return -1;
0253 }
0254 
0255 static int hw_break_release_slot(int breakno)
0256 {
0257     struct perf_event **pevent;
0258     int cpu;
0259 
0260     if (dbg_is_early)
0261         return 0;
0262 
0263     for_each_online_cpu(cpu) {
0264         pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
0265         if (dbg_release_bp_slot(*pevent))
0266             /*
0267              * The debugger is responsible for handing the retry on
0268              * remove failure.
0269              */
0270             return -1;
0271     }
0272     return 0;
0273 }
0274 
0275 static int
0276 kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
0277 {
0278     int i;
0279 
0280     for (i = 0; i < HBP_NUM; i++)
0281         if (breakinfo[i].addr == addr && breakinfo[i].enabled)
0282             break;
0283     if (i == HBP_NUM)
0284         return -1;
0285 
0286     if (hw_break_release_slot(i)) {
0287         printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
0288         return -1;
0289     }
0290     breakinfo[i].enabled = 0;
0291 
0292     return 0;
0293 }
0294 
0295 static void kgdb_remove_all_hw_break(void)
0296 {
0297     int i;
0298     int cpu = raw_smp_processor_id();
0299     struct perf_event *bp;
0300 
0301     for (i = 0; i < HBP_NUM; i++) {
0302         if (!breakinfo[i].enabled)
0303             continue;
0304         bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
0305         if (!bp->attr.disabled) {
0306             arch_uninstall_hw_breakpoint(bp);
0307             bp->attr.disabled = 1;
0308             continue;
0309         }
0310         if (dbg_is_early)
0311             early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
0312                          breakinfo[i].type);
0313         else if (hw_break_release_slot(i))
0314             printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
0315                    breakinfo[i].addr);
0316         breakinfo[i].enabled = 0;
0317     }
0318 }
0319 
0320 static int
0321 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
0322 {
0323     int i;
0324 
0325     for (i = 0; i < HBP_NUM; i++)
0326         if (!breakinfo[i].enabled)
0327             break;
0328     if (i == HBP_NUM)
0329         return -1;
0330 
0331     switch (bptype) {
0332     case BP_HARDWARE_BREAKPOINT:
0333         len = 1;
0334         breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
0335         break;
0336     case BP_WRITE_WATCHPOINT:
0337         breakinfo[i].type = X86_BREAKPOINT_WRITE;
0338         break;
0339     case BP_ACCESS_WATCHPOINT:
0340         breakinfo[i].type = X86_BREAKPOINT_RW;
0341         break;
0342     default:
0343         return -1;
0344     }
0345     switch (len) {
0346     case 1:
0347         breakinfo[i].len = X86_BREAKPOINT_LEN_1;
0348         break;
0349     case 2:
0350         breakinfo[i].len = X86_BREAKPOINT_LEN_2;
0351         break;
0352     case 4:
0353         breakinfo[i].len = X86_BREAKPOINT_LEN_4;
0354         break;
0355 #ifdef CONFIG_X86_64
0356     case 8:
0357         breakinfo[i].len = X86_BREAKPOINT_LEN_8;
0358         break;
0359 #endif
0360     default:
0361         return -1;
0362     }
0363     breakinfo[i].addr = addr;
0364     if (hw_break_reserve_slot(i)) {
0365         breakinfo[i].addr = 0;
0366         return -1;
0367     }
0368     breakinfo[i].enabled = 1;
0369 
0370     return 0;
0371 }
0372 
0373 /**
0374  *  kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
0375  *  @regs: Current &struct pt_regs.
0376  *
0377  *  This function will be called if the particular architecture must
0378  *  disable hardware debugging while it is processing gdb packets or
0379  *  handling exception.
0380  */
0381 static void kgdb_disable_hw_debug(struct pt_regs *regs)
0382 {
0383     int i;
0384     int cpu = raw_smp_processor_id();
0385     struct perf_event *bp;
0386 
0387     /* Disable hardware debugging while we are in kgdb: */
0388     set_debugreg(0UL, 7);
0389     for (i = 0; i < HBP_NUM; i++) {
0390         if (!breakinfo[i].enabled)
0391             continue;
0392         if (dbg_is_early) {
0393             early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
0394                          breakinfo[i].type);
0395             continue;
0396         }
0397         bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
0398         if (bp->attr.disabled == 1)
0399             continue;
0400         arch_uninstall_hw_breakpoint(bp);
0401         bp->attr.disabled = 1;
0402     }
0403 }
0404 
0405 #ifdef CONFIG_SMP
0406 /**
0407  *  kgdb_roundup_cpus - Get other CPUs into a holding pattern
0408  *
0409  *  On SMP systems, we need to get the attention of the other CPUs
0410  *  and get them be in a known state.  This should do what is needed
0411  *  to get the other CPUs to call kgdb_wait(). Note that on some arches,
0412  *  the NMI approach is not used for rounding up all the CPUs. For example,
0413  *  in case of MIPS, smp_call_function() is used to roundup CPUs.
0414  *
0415  *  On non-SMP systems, this is not called.
0416  */
0417 void kgdb_roundup_cpus(void)
0418 {
0419     apic_send_IPI_allbutself(NMI_VECTOR);
0420 }
0421 #endif
0422 
0423 /**
0424  *  kgdb_arch_handle_exception - Handle architecture specific GDB packets.
0425  *  @e_vector: The error vector of the exception that happened.
0426  *  @signo: The signal number of the exception that happened.
0427  *  @err_code: The error code of the exception that happened.
0428  *  @remcomInBuffer: The buffer of the packet we have read.
0429  *  @remcomOutBuffer: The buffer of %BUFMAX bytes to write a packet into.
0430  *  @linux_regs: The &struct pt_regs of the current process.
0431  *
0432  *  This function MUST handle the 'c' and 's' command packets,
0433  *  as well packets to set / remove a hardware breakpoint, if used.
0434  *  If there are additional packets which the hardware needs to handle,
0435  *  they are handled here.  The code should return -1 if it wants to
0436  *  process more packets, and a %0 or %1 if it wants to exit from the
0437  *  kgdb callback.
0438  */
0439 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
0440                    char *remcomInBuffer, char *remcomOutBuffer,
0441                    struct pt_regs *linux_regs)
0442 {
0443     unsigned long addr;
0444     char *ptr;
0445 
0446     switch (remcomInBuffer[0]) {
0447     case 'c':
0448     case 's':
0449         /* try to read optional parameter, pc unchanged if no parm */
0450         ptr = &remcomInBuffer[1];
0451         if (kgdb_hex2long(&ptr, &addr))
0452             linux_regs->ip = addr;
0453         fallthrough;
0454     case 'D':
0455     case 'k':
0456         /* clear the trace bit */
0457         linux_regs->flags &= ~X86_EFLAGS_TF;
0458         atomic_set(&kgdb_cpu_doing_single_step, -1);
0459 
0460         /* set the trace bit if we're stepping */
0461         if (remcomInBuffer[0] == 's') {
0462             linux_regs->flags |= X86_EFLAGS_TF;
0463             atomic_set(&kgdb_cpu_doing_single_step,
0464                    raw_smp_processor_id());
0465         }
0466 
0467         return 0;
0468     }
0469 
0470     /* this means that we do not want to exit from the handler: */
0471     return -1;
0472 }
0473 
0474 static inline int
0475 single_step_cont(struct pt_regs *regs, struct die_args *args)
0476 {
0477     /*
0478      * Single step exception from kernel space to user space so
0479      * eat the exception and continue the process:
0480      */
0481     printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
0482             "resuming...\n");
0483     kgdb_arch_handle_exception(args->trapnr, args->signr,
0484                    args->err, "c", "", regs);
0485     /*
0486      * Reset the BS bit in dr6 (pointed by args->err) to
0487      * denote completion of processing
0488      */
0489     (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
0490 
0491     return NOTIFY_STOP;
0492 }
0493 
0494 static DECLARE_BITMAP(was_in_debug_nmi, NR_CPUS);
0495 
0496 static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
0497 {
0498     int cpu;
0499 
0500     switch (cmd) {
0501     case NMI_LOCAL:
0502         if (atomic_read(&kgdb_active) != -1) {
0503             /* KGDB CPU roundup */
0504             cpu = raw_smp_processor_id();
0505             kgdb_nmicallback(cpu, regs);
0506             set_bit(cpu, was_in_debug_nmi);
0507             touch_nmi_watchdog();
0508 
0509             return NMI_HANDLED;
0510         }
0511         break;
0512 
0513     case NMI_UNKNOWN:
0514         cpu = raw_smp_processor_id();
0515 
0516         if (__test_and_clear_bit(cpu, was_in_debug_nmi))
0517             return NMI_HANDLED;
0518 
0519         break;
0520     default:
0521         /* do nothing */
0522         break;
0523     }
0524     return NMI_DONE;
0525 }
0526 
0527 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
0528 {
0529     struct pt_regs *regs = args->regs;
0530 
0531     switch (cmd) {
0532     case DIE_DEBUG:
0533         if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
0534             if (user_mode(regs))
0535                 return single_step_cont(regs, args);
0536             break;
0537         } else if (test_thread_flag(TIF_SINGLESTEP))
0538             /* This means a user thread is single stepping
0539              * a system call which should be ignored
0540              */
0541             return NOTIFY_DONE;
0542         fallthrough;
0543     default:
0544         if (user_mode(regs))
0545             return NOTIFY_DONE;
0546     }
0547 
0548     if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
0549         return NOTIFY_DONE;
0550 
0551     /* Must touch watchdog before return to normal operation */
0552     touch_nmi_watchdog();
0553     return NOTIFY_STOP;
0554 }
0555 
0556 int kgdb_ll_trap(int cmd, const char *str,
0557          struct pt_regs *regs, long err, int trap, int sig)
0558 {
0559     struct die_args args = {
0560         .regs   = regs,
0561         .str    = str,
0562         .err    = err,
0563         .trapnr = trap,
0564         .signr  = sig,
0565 
0566     };
0567 
0568     if (!kgdb_io_module_registered)
0569         return NOTIFY_DONE;
0570 
0571     return __kgdb_notify(&args, cmd);
0572 }
0573 
0574 static int
0575 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
0576 {
0577     unsigned long flags;
0578     int ret;
0579 
0580     local_irq_save(flags);
0581     ret = __kgdb_notify(ptr, cmd);
0582     local_irq_restore(flags);
0583 
0584     return ret;
0585 }
0586 
0587 static struct notifier_block kgdb_notifier = {
0588     .notifier_call  = kgdb_notify,
0589 };
0590 
0591 /**
0592  *  kgdb_arch_init - Perform any architecture specific initialization.
0593  *
0594  *  This function will handle the initialization of any architecture
0595  *  specific callbacks.
0596  */
0597 int kgdb_arch_init(void)
0598 {
0599     int retval;
0600 
0601     retval = register_die_notifier(&kgdb_notifier);
0602     if (retval)
0603         goto out;
0604 
0605     retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
0606                     0, "kgdb");
0607     if (retval)
0608         goto out1;
0609 
0610     retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
0611                     0, "kgdb");
0612 
0613     if (retval)
0614         goto out2;
0615 
0616     return retval;
0617 
0618 out2:
0619     unregister_nmi_handler(NMI_LOCAL, "kgdb");
0620 out1:
0621     unregister_die_notifier(&kgdb_notifier);
0622 out:
0623     return retval;
0624 }
0625 
0626 static void kgdb_hw_overflow_handler(struct perf_event *event,
0627         struct perf_sample_data *data, struct pt_regs *regs)
0628 {
0629     struct task_struct *tsk = current;
0630     int i;
0631 
0632     for (i = 0; i < 4; i++) {
0633         if (breakinfo[i].enabled)
0634             tsk->thread.virtual_dr6 |= (DR_TRAP0 << i);
0635     }
0636 }
0637 
0638 void kgdb_arch_late(void)
0639 {
0640     int i, cpu;
0641     struct perf_event_attr attr;
0642     struct perf_event **pevent;
0643 
0644     /*
0645      * Pre-allocate the hw breakpoint instructions in the non-atomic
0646      * portion of kgdb because this operation requires mutexs to
0647      * complete.
0648      */
0649     hw_breakpoint_init(&attr);
0650     attr.bp_addr = (unsigned long)kgdb_arch_init;
0651     attr.bp_len = HW_BREAKPOINT_LEN_1;
0652     attr.bp_type = HW_BREAKPOINT_W;
0653     attr.disabled = 1;
0654     for (i = 0; i < HBP_NUM; i++) {
0655         if (breakinfo[i].pev)
0656             continue;
0657         breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
0658         if (IS_ERR((void * __force)breakinfo[i].pev)) {
0659             printk(KERN_ERR "kgdb: Could not allocate hw"
0660                    "breakpoints\nDisabling the kernel debugger\n");
0661             breakinfo[i].pev = NULL;
0662             kgdb_arch_exit();
0663             return;
0664         }
0665         for_each_online_cpu(cpu) {
0666             pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
0667             pevent[0]->hw.sample_period = 1;
0668             pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
0669             if (pevent[0]->destroy != NULL) {
0670                 pevent[0]->destroy = NULL;
0671                 release_bp_slot(*pevent);
0672             }
0673         }
0674     }
0675 }
0676 
0677 /**
0678  *  kgdb_arch_exit - Perform any architecture specific uninitalization.
0679  *
0680  *  This function will handle the uninitalization of any architecture
0681  *  specific callbacks, for dynamic registration and unregistration.
0682  */
0683 void kgdb_arch_exit(void)
0684 {
0685     int i;
0686     for (i = 0; i < 4; i++) {
0687         if (breakinfo[i].pev) {
0688             unregister_wide_hw_breakpoint(breakinfo[i].pev);
0689             breakinfo[i].pev = NULL;
0690         }
0691     }
0692     unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
0693     unregister_nmi_handler(NMI_LOCAL, "kgdb");
0694     unregister_die_notifier(&kgdb_notifier);
0695 }
0696 
0697 /**
0698  *
0699  *  kgdb_skipexception - Bail out of KGDB when we've been triggered.
0700  *  @exception: Exception vector number
0701  *  @regs: Current &struct pt_regs.
0702  *
0703  *  On some architectures we need to skip a breakpoint exception when
0704  *  it occurs after a breakpoint has been removed.
0705  *
0706  * Skip an int3 exception when it occurs after a breakpoint has been
0707  * removed. Backtrack eip by 1 since the int3 would have caused it to
0708  * increment by 1.
0709  */
0710 int kgdb_skipexception(int exception, struct pt_regs *regs)
0711 {
0712     if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
0713         regs->ip -= 1;
0714         return 1;
0715     }
0716     return 0;
0717 }
0718 
0719 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
0720 {
0721     if (exception == 3)
0722         return instruction_pointer(regs) - 1;
0723     return instruction_pointer(regs);
0724 }
0725 
0726 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
0727 {
0728     regs->ip = ip;
0729 }
0730 
0731 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
0732 {
0733     int err;
0734 
0735     bpt->type = BP_BREAKPOINT;
0736     err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
0737                 BREAK_INSTR_SIZE);
0738     if (err)
0739         return err;
0740     err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
0741                  arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
0742     if (!err)
0743         return err;
0744     /*
0745      * It is safe to call text_poke_kgdb() because normal kernel execution
0746      * is stopped on all cores, so long as the text_mutex is not locked.
0747      */
0748     if (mutex_is_locked(&text_mutex))
0749         return -EBUSY;
0750     text_poke_kgdb((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
0751                BREAK_INSTR_SIZE);
0752     bpt->type = BP_POKE_BREAKPOINT;
0753 
0754     return 0;
0755 }
0756 
0757 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
0758 {
0759     if (bpt->type != BP_POKE_BREAKPOINT)
0760         goto knl_write;
0761     /*
0762      * It is safe to call text_poke_kgdb() because normal kernel execution
0763      * is stopped on all cores, so long as the text_mutex is not locked.
0764      */
0765     if (mutex_is_locked(&text_mutex))
0766         goto knl_write;
0767     text_poke_kgdb((void *)bpt->bpt_addr, bpt->saved_instr,
0768                BREAK_INSTR_SIZE);
0769     return 0;
0770 
0771 knl_write:
0772     return copy_to_kernel_nofault((char *)bpt->bpt_addr,
0773                   (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
0774 }
0775 
0776 const struct kgdb_arch arch_kgdb_ops = {
0777     /* Breakpoint instruction: */
0778     .gdb_bpt_instr      = { 0xcc },
0779     .flags          = KGDB_HW_BREAKPOINT,
0780     .set_hw_breakpoint  = kgdb_set_hw_break,
0781     .remove_hw_breakpoint   = kgdb_remove_hw_break,
0782     .disable_hw_break   = kgdb_disable_hw_debug,
0783     .remove_all_hw_break    = kgdb_remove_all_hw_break,
0784     .correct_hw_break   = kgdb_correct_hw_break,
0785 };