2 * Kernel Debugger Architecture Dependent Breakpoint Handling
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/ptrace.h>
16 #include <linux/kdb.h>
17 #include <linux/kdbprivate.h>
18 #include <asm/pgalloc.h>
21 static char *kdba_rwtypes[] = { "Instruction(Register)", "Data Write",
22 "I/O", "Data Access"};
25 * Table describing processor architecture hardware
26 * breakpoint registers.
29 static kdbhard_bp_t kdb_hardbreaks[KDB_MAXHARDBPT];
31 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
33 * Counters for number of debug registers used on each CPU.
34 * Used to detect when to enable and disable debug traps.
36 static unsigned char kdb_dbrs_used[NR_CPUS];
37 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
42 * Perform breakpoint processing upon entry to the
43 * processor debugger fault. Determine and print
44 * the active breakpoint.
47 * regs Exception frame containing machine register state
48 * error Error number passed to kdb.
52 * KDB_DB_BPT Standard instruction or data breakpoint encountered
53 * KDB_DB_SS Single Step fault ('ss' command or end of 'ssb' command)
54 * KDB_DB_SSB Single Step fault, caller should continue ('ssb' command)
55 * KDB_DB_SSBPT Single step over breakpoint
56 * KDB_DB_NOBPT No existing kdb breakpoint matches this debug exception
60 * Yup, there be goto's here.
62 * If multiple processors receive debug exceptions simultaneously,
63 * one may be waiting at the kdb fence in kdb() while the user
64 * issues a 'bc' command to clear the breakpoint the processor
65 * which is waiting has already encountered. If this is the case,
66 * the debug registers will no longer match any entry in the
67 * breakpoint table, and we'll return the value KDB_DB_NOBPT.
68 * This can cause a panic in die_if_kernel(). It is safer to
69 * disable the breakpoint (bd), go until all processors are past
70 * the breakpoint then clear the breakpoint (bc). This code
71 * recognises a breakpoint even when disabled but not when it has
74 * WARNING: This routine clears the debug state. It should be called
75 * once per debug and the result cached.
79 kdba_db_trap(struct pt_regs *regs, int error)
82 kdb_dbtrap_t rv = KDB_DB_BPT;
85 if (KDB_NULL_REGS(regs))
89 kdb_printf("kdba_db_trap: error %d\n", error);
93 if (KDB_STATE(SSBPT)) {
95 kdb_printf("ssbpt\n");
96 KDB_STATE_CLEAR(SSBPT);
97 for(i=0,bp=kdb_breakpoints;
101 kdb_printf("bp 0x%p enabled %d delayed %d global %d cpu %d\n",
102 bp, bp->bp_enabled, bp->bp_delayed, bp->bp_global, bp->bp_cpu);
105 if (!bp->bp_global && bp->bp_cpu != smp_processor_id())
108 kdb_printf("bp for this cpu\n");
109 if (bp->bp_delayed) {
112 kdb_printf("kdba_installbp\n");
113 kdba_installbp(regs, bp);
114 if (!KDB_STATE(DOING_SS)) {
115 kdba_clearsinglestep(regs);
116 return(KDB_DB_SSBPT);
121 if (i == KDB_MAXBPT) {
122 kdb_printf("kdb: Unable to find delayed breakpoint\n");
124 if (!KDB_STATE(DOING_SS)) {
125 kdba_clearsinglestep(regs);
126 return(KDB_DB_NOBPT);
132 * KDB_STATE_DOING_SS is set when the kernel debugger is using
133 * the processor trap flag to single-step a processor. If a
134 * single step trap occurs and this flag is clear, the SS trap
135 * will be ignored by KDB and the kernel will be allowed to deal
136 * with it as necessary (e.g. for ptrace).
138 if (!KDB_STATE(DOING_SS))
139 return(KDB_DB_NOBPT);
142 rv = KDB_DB_SS; /* Indicate single step */
143 if (KDB_STATE(DOING_SSB)) /* No ia64 ssb support yet */
144 KDB_STATE_CLEAR(DOING_SSB); /* No ia64 ssb support yet */
145 if (KDB_STATE(DOING_SSB)) {
146 /* No IA64 ssb support yet */
151 kdb_machreg_t pc = regs->cr_iip + ia64_psr(regs)->ri * 6;
152 kdb_printf("SS trap at ");
153 kdb_symbol_print(pc, NULL, KDB_SP_DEFAULT|KDB_SP_NEWLINE);
155 KDB_STATE_CLEAR(DOING_SS);
158 if (rv != KDB_DB_SSB)
159 kdba_clearsinglestep(regs);
168 * Perform breakpoint processing upon entry to the
169 * processor breakpoint instruction fault. Determine and print
170 * the active breakpoint.
173 * regs Exception frame containing machine register state
174 * error Error number passed to kdb.
178 * 0 Standard instruction or data breakpoint encountered
179 * 1 Single Step fault ('ss' command)
180 * 2 Single Step fault, caller should continue ('ssb' command)
181 * 3 No existing kdb breakpoint matches this debug exception
186 * If multiple processors receive debug exceptions simultaneously,
187 * one may be waiting at the kdb fence in kdb() while the user
188 * issues a 'bc' command to clear the breakpoint the processor which
189 * is waiting has already encountered. If this is the case, the
190 * debug registers will no longer match any entry in the breakpoint
191 * table, and we'll return the value '3'. This can cause a panic
192 * in die_if_kernel(). It is safer to disable the breakpoint (bd),
193 * 'go' until all processors are past the breakpoint then clear the
194 * breakpoint (bc). This code recognises a breakpoint even when
195 * disabled but not when it has been cleared.
197 * WARNING: This routine resets the ip. It should be called
198 * once per breakpoint and the result cached.
202 kdba_bp_trap(struct pt_regs *regs, int error)
208 if (KDB_NULL_REGS(regs))
212 * Determine which breakpoint was encountered.
215 kdb_printf("kdba_bp_trap: ip=0x%lx "
216 "regs=0x%p sp=0x%lx\n",
217 regs->cr_iip, regs, regs->r12);
219 rv = KDB_DB_NOBPT; /* Cause kdb() to return */
221 for(i=0, bp=kdb_breakpoints; i<KDB_MAXBPT; i++, bp++) {
224 if (!bp->bp_global && bp->bp_cpu != smp_processor_id())
226 if (bp->bp_addr == regs->cr_iip) {
227 /* Hit this breakpoint. */
228 kdb_printf("Instruction(i) breakpoint #%d at 0x%lx\n",
230 kdb_id1(regs->cr_iip);
233 /* SSBPT is set when the kernel debugger must single
234 * step a task in order to re-establish an instruction
235 * breakpoint which uses the instruction replacement
236 * mechanism. It is cleared by any action that removes
237 * the need to single-step the breakpoint.
239 KDB_STATE_SET(SSBPT);
250 * Handle an instruction-breakpoint trap. Called when re-installing
251 * an enabled breakpoint which has has the bp_delay bit set.
258 * Ok, we really need to:
259 * 1) Restore the original instruction byte(s)
261 * 3) Restore breakpoint instruction
268 kdba_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
270 if (KDB_NULL_REGS(regs))
274 kdb_printf("regs->cr_iip = 0x%lx\n", regs->cr_iip);
279 kdba_setsinglestep(regs);
282 * Reset delay attribute
292 * Return a string describing type of breakpoint.
295 * bph Pointer to hardware breakpoint description
306 kdba_bptype(kdbhard_bp_t *bph)
310 mode = kdba_rwtypes[bph->bph_mode];
318 * Print register name assigned to breakpoint
321 * bph Pointer hardware breakpoint structure
332 kdba_printbpreg(kdbhard_bp_t *bph)
334 kdb_printf(" in dr%ld", bph->bph_reg);
340 * Print string describing hardware breakpoint.
343 * bph Pointer to hardware breakpoint description
354 kdba_printbp(kdb_bp_t *bp)
356 kdb_printf("\n is enabled");
357 if (bp->bp_hardtype) {
358 /* Note that bp->bp_hard[NR_CPU] is for x86.
359 * The ia64 uses bp->bp_hard[0] only.
361 kdba_printbpreg(bp->bp_hard[0]);
362 if (bp->bp_hard[0]->bph_mode != 0) {
363 kdb_printf(" for %d bytes",
364 bp->bp_hard[0]->bph_length+1);
372 * Parse architecture dependent portion of the
373 * breakpoint command.
380 * Zero for success, a kdb diagnostic for failure
384 * for IA64 architure, data access, data write and
385 * I/O breakpoints are supported in addition to instruction
388 * {datar|dataw|io|inst} [length]
392 kdba_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
394 int nextarg = *nextargp;
396 kdbhard_bp_t *bph = &bp->bp_template;
398 bph->bph_mode = 0; /* Default to instruction breakpoint */
399 bph->bph_length = 0; /* Length must be zero for insn bp */
400 if ((argc + 1) != nextarg) {
401 if (strnicmp(argv[nextarg], "datar", sizeof("datar")) == 0) {
403 } else if (strnicmp(argv[nextarg], "dataw", sizeof("dataw")) == 0) {
405 } else if (strnicmp(argv[nextarg], "io", sizeof("io")) == 0) {
407 } else if (strnicmp(argv[nextarg], "inst", sizeof("inst")) == 0) {
413 if (bph->bph_mode == 0)
414 kdba_check_pc(&bp->bp_addr);
416 bph->bph_length = 3; /* Default to 4 byte */
420 if ((argc + 1) != nextarg) {
423 diag = kdbgetularg((char *)argv[nextarg],
429 if ((len > 4) || (len == 3))
430 return KDB_BADLENGTH;
432 bph->bph_length = len;
433 bph->bph_length--; /* Normalize for debug register */
437 if ((argc + 1) != nextarg)
441 * Indicate to architecture independent level that
442 * a hardware register assignment is required to enable
449 kdb_printf("kdba_bp: no args, forcehw is %d\n", bp->bp_forcehw);
450 if (bp->bp_forcehw) {
452 * We are forced to use a hardware register for this
453 * breakpoint because either the bph or bpha
454 * commands were used to establish this breakpoint.
459 * Indicate to architecture dependent level that
460 * the instruction replacement breakpoint technique
461 * should be used for this breakpoint.
464 bp->bp_adjust = 0; /* software, break is fault, not trap */
468 if (bph->bph_mode == 0 && kdba_verify_rw(bp->bp_addr, bph->bph_length+1)) {
469 kdb_printf("Invalid address for breakpoint, ignoring bp command\n");
474 #ifndef CONFIG_KDB_HARDWARE_BREAKPOINTS
475 if (!bph->bph_free) {
476 kdb_printf("kdba_parsebp hardware breakpoints are not supported yet\n");
479 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
486 * Associate a hardware register with a breakpoint.
493 * A pointer to the allocated register kdbhard_bp_t structure for
494 * success, Null and a non-zero diagnostic for failure.
500 static kdbhard_bp_t *
501 kdba_allocbp(kdbhard_bp_t *bph, int *diagp)
504 kdbhard_bp_t *newbph;
506 for(i=0,newbph=kdb_hardbreaks; i < KDB_MAXHARDBPT; i++, newbph++) {
507 if (newbph->bph_free) {
512 if (i == KDB_MAXHARDBPT) {
513 *diagp = KDB_TOOMANYDBREGS;
520 * Copy data from template. Can't just copy the entire template
521 * here because the register number in kdb_hardbreaks must be
524 newbph->bph_data = bph->bph_data;
525 newbph->bph_write = bph->bph_write;
526 newbph->bph_mode = bph->bph_mode;
527 newbph->bph_length = bph->bph_length;
530 * Mark entry allocated.
532 newbph->bph_free = 0;
540 * Associate a hardware registers with a breakpoint.
541 * If hw bp is global hw registers descriptor will be allocated
546 * diagp - pointer to variable that will store error when
555 * Should be called with correct bp->bp_template.
559 kdba_alloc_hwbp(kdb_bp_t *bp, int *diagp)
561 /* Note that bp->bp_hard[NR_CPU] is for x86.
562 * The ia64 uses bp->bp_hard[0] only.
564 bp->bp_hard[0] = kdba_allocbp(&bp->bp_template, diagp);
573 * Deallocate a hardware breakpoint
580 * Zero for success, a kdb diagnostic for failure
587 kdba_freebp(kdbhard_bp_t *bph)
595 * Frees allocated hw registers descriptors for bp.
596 * If hw bp is global, hw registers descriptors will be freed
608 * Should be called with correct bp->bp_template
612 kdba_free_hwbp(kdb_bp_t *bp)
614 /* When kernel enters KDB, first, all local bps
615 * are removed, so here we don't need to clear
619 kdba_freebp(bp->bp_hard[0]);
620 bp->bp_hard[0] = NULL;
628 * Initialize the breakpoint table for the hardware breakpoint
636 * Zero for success, a kdb diagnostic for failure
641 * There is one entry per register. On the ia64 architecture
642 * all the registers are interchangeable, so no special allocation
643 * criteria are required.
653 * Clear the hardware breakpoint table
656 memset(kdb_hardbreaks, '\0', sizeof(kdb_hardbreaks));
658 for(i=0,bph=kdb_hardbreaks; i<KDB_MAXHARDBPT; i++, bph++) {
664 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
666 * Enable Instruction Debug & Data Debug faults on this CPU now.
668 static inline void kdba_enable_debug_faults(void)
672 tmp = ia64_getreg(_IA64_REG_PSR);
676 ia64_setreg(_IA64_REG_PSR_L, tmp);
679 kdb_printf("enabling debug faults: [%d]PSR.L=%08x\n",
680 smp_processor_id(), (unsigned int)tmp);
684 * Disable Instruction Debug & Data Debug faults on this CPU now.
686 static inline void kdba_disable_debug_faults(void)
690 tmp = ia64_getreg(_IA64_REG_PSR);
694 ia64_setreg(_IA64_REG_PSR_L, tmp);
697 kdb_printf("disabling debug faults: [%d]PSR.L=%08x\n",
698 smp_processor_id(), (unsigned int)tmp);
700 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
705 * Install a breakpoint
708 * regs Exception frame
709 * bp Breakpoint structure for the breakpoint to be installed
713 * 0 if breakpoint set, otherwise error.
717 * For hardware breakpoints, a debug register is allocated
718 * and assigned to the breakpoint. If no debug register is
719 * available, a warning message is printed and the breakpoint
722 * For instruction replacement breakpoints, we must single-step
723 * over the replaced instruction at this point so we can re-install
724 * the breakpoint instruction after the single-step. SSBPT is set
725 * when the breakpoint is initially hit and is cleared by any action
726 * that removes the need for single-step over the breakpoint.
730 kdba_installbp(struct pt_regs *regs, kdb_bp_t *bp)
733 * Install the breakpoint, if it is not already installed.
737 kdb_printf("kdba_installbp bp_installed %d\n", bp->bp_installed);
739 if (!KDB_STATE(SSBPT))
742 if (bp->bp_hardtype) {
743 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
745 * Hardware breakpoints are always local for the
746 * purposes of installation (i.e. they use per-cpu
747 * registers), so we don't need to check bp_installed
749 kdba_installdbreg(bp);
750 if (++kdb_dbrs_used[smp_processor_id()] == 1)
751 kdba_enable_debug_faults();
752 bp->bp_installed = 1;
754 kdb_printf("kdba_installbp hardware reg %ld at " kdb_bfd_vma_fmt0 "\n",
755 bp->bp_hard[0]->bph_reg, bp->bp_addr);
757 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
759 } else if (bp->bp_delay) {
760 if (!bp->bp_installed) {
762 kdb_printf("kdba_installbp delayed bp\n");
763 kdba_handle_bp(regs, bp);
766 if (!bp->bp_installed) {
767 /* Software breakpoints always use slot 0 in the 128 bit
768 * bundle. The template type does not matter, slot 0
769 * can only be M or B and the encodings for break.m and
770 * break.b are the same.
772 unsigned long break_inst;
773 if (kdb_getarea_size(bp->bp_inst.inst, bp->bp_addr, sizeof(bp->bp_inst.inst))) {
774 kdb_printf("kdba_installbp failed to read software breakpoint at 0x%lx\n", bp->bp_addr);
777 break_inst = (bp->bp_inst.inst[0] & ~INST_SLOT0_MASK) | BREAK_INSTR;
778 if (kdb_putarea_size(bp->bp_addr, &break_inst, sizeof(break_inst))) {
779 kdb_printf("kdba_installbp failed to set software breakpoint at 0x%lx\n", bp->bp_addr);
783 kdb_printf("kdba_installbp instruction 0x%lx at " kdb_bfd_vma_fmt0 "\n",
784 BREAK_INSTR, bp->bp_addr);
785 bp->bp_installed = 1;
786 flush_icache_range(bp->bp_addr, bp->bp_addr+16);
795 * Make a breakpoint ineffective.
802 * 0 if breakpoint removed, otherwise error.
809 kdba_removebp(kdb_bp_t *bp)
812 * For hardware breakpoints, remove it from the active register,
813 * for software breakpoints, restore the instruction stream.
816 kdb_printf("kdba_removebp bp_installed %d\n", bp->bp_installed);
819 if (bp->bp_hardtype) {
820 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
822 kdb_printf("kdb: removing hardware reg %ld at " kdb_bfd_vma_fmt0 "\n",
823 bp->bp_hard[0]->bph_reg, bp->bp_addr);
825 if (--kdb_dbrs_used[smp_processor_id()] == 0)
826 kdba_disable_debug_faults();
827 kdba_removedbreg(bp);
828 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
830 if (bp->bp_installed) {
832 kdb_printf("kdb: restoring instruction 0x%016lx%016lx at " kdb_bfd_vma_fmt0 "\n",
833 bp->bp_inst.inst[0], bp->bp_inst.inst[1], bp->bp_addr);
834 if (kdba_putarea_size(bp->bp_addr, bp->bp_inst.inst, sizeof(bp->bp_inst.inst)))
837 bp->bp_installed = 0;
838 flush_icache_range(bp->bp_addr, bp->bp_addr+16);