Added patch headers.
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / kdb / kdba_bp.c
1 /*
2  * Kernel Debugger Architecture Dependent Breakpoint Handling
3  *
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
6  * for more details.
7  *
8  * Copyright (c) 1999-2004 Silicon Graphics, Inc.  All Rights Reserved.
9  */
10
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>
19
20
21 static char *kdba_rwtypes[] = { "Instruction(Register)", "Data Write",
22                         "I/O", "Data Access"};
23
24 /*
25  * Table describing processor architecture hardware
26  * breakpoint registers.
27  */
28
29 static kdbhard_bp_t kdb_hardbreaks[KDB_MAXHARDBPT];
30
31 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
32 /*
33  * Counters for number of debug registers used on each CPU.
34  * Used to detect when to enable and disable debug traps.
35  */
36 static unsigned char kdb_dbrs_used[NR_CPUS];
37 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
38
39 /*
40  * kdba_db_trap
41  *
42  *      Perform breakpoint processing upon entry to the
43  *      processor debugger fault.   Determine and print
44  *      the active breakpoint.
45  *
46  * Parameters:
47  *      regs    Exception frame containing machine register state
48  *      error   Error number passed to kdb.
49  * Outputs:
50  *      None.
51  * Returns:
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
57  * Locking:
58  *      None.
59  * Remarks:
60  *      Yup, there be goto's here.
61  *
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
72  *      been cleared.
73  *
74  *      WARNING: This routine clears the debug state.  It should be called
75  *               once per debug and the result cached.
76  */
77
78 kdb_dbtrap_t
79 kdba_db_trap(struct pt_regs *regs, int error)
80 {
81         int i;
82         kdb_dbtrap_t rv = KDB_DB_BPT;
83         kdb_bp_t *bp;
84
85         if (KDB_NULL_REGS(regs))
86                 return KDB_DB_NOBPT;
87
88         if (KDB_DEBUG(BP))
89                 kdb_printf("kdba_db_trap: error %d\n", error);
90
91         if (error == 36) {
92                 /* Single step */
93                 if (KDB_STATE(SSBPT)) {
94                         if (KDB_DEBUG(BP))
95                                 kdb_printf("ssbpt\n");
96                         KDB_STATE_CLEAR(SSBPT);
97                         for(i=0,bp=kdb_breakpoints;
98                             i < KDB_MAXBPT;
99                             i++, bp++) {
100                                 if (KDB_DEBUG(BP))
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);
103                                 if (!bp->bp_enabled)
104                                         continue;
105                                 if (!bp->bp_global && bp->bp_cpu != smp_processor_id())
106                                         continue;
107                                 if (KDB_DEBUG(BP))
108                                         kdb_printf("bp for this cpu\n");
109                                 if (bp->bp_delayed) {
110                                         bp->bp_delayed = 0;
111                                         if (KDB_DEBUG(BP))
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);
117                                         }
118                                         break;
119                                 }
120                         }
121                         if (i == KDB_MAXBPT) {
122                                 kdb_printf("kdb: Unable to find delayed breakpoint\n");
123                         }
124                         if (!KDB_STATE(DOING_SS)) {
125                                 kdba_clearsinglestep(regs);
126                                 return(KDB_DB_NOBPT);
127                         }
128                         /* FALLTHROUGH */
129                 }
130
131                 /*
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).
137                  */
138                 if (!KDB_STATE(DOING_SS))
139                         return(KDB_DB_NOBPT);
140
141                 /* single step */
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 */
147                 } else {
148                         /*
149                          * Print current insn
150                          */
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);
154                         kdb_id1(pc);
155                         KDB_STATE_CLEAR(DOING_SS);
156                 }
157
158                 if (rv != KDB_DB_SSB)
159                         kdba_clearsinglestep(regs);
160         }
161
162         return(rv);
163 }
164
165 /*
166  * kdba_bp_trap
167  *
168  *      Perform breakpoint processing upon entry to the
169  *      processor breakpoint instruction fault.   Determine and print
170  *      the active breakpoint.
171  *
172  * Parameters:
173  *      regs    Exception frame containing machine register state
174  *      error   Error number passed to kdb.
175  * Outputs:
176  *      None.
177  * Returns:
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
182  * Locking:
183  *      None.
184  * Remarks:
185  *
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.
196  *
197  *      WARNING: This routine resets the ip.  It should be called
198  *               once per breakpoint and the result cached.
199  */
200
201 kdb_dbtrap_t
202 kdba_bp_trap(struct pt_regs *regs, int error)
203 {
204         int i;
205         kdb_dbtrap_t rv;
206         kdb_bp_t *bp;
207
208         if (KDB_NULL_REGS(regs))
209                 return KDB_DB_NOBPT;
210
211         /*
212          * Determine which breakpoint was encountered.
213          */
214         if (KDB_DEBUG(BP))
215                 kdb_printf("kdba_bp_trap: ip=0x%lx "
216                            "regs=0x%p sp=0x%lx\n",
217                            regs->cr_iip, regs, regs->r12);
218
219         rv = KDB_DB_NOBPT;      /* Cause kdb() to return */
220
221         for(i=0, bp=kdb_breakpoints; i<KDB_MAXBPT; i++, bp++) {
222                 if (bp->bp_free)
223                         continue;
224                 if (!bp->bp_global && bp->bp_cpu != smp_processor_id())
225                         continue;
226                  if (bp->bp_addr == regs->cr_iip) {
227                         /* Hit this breakpoint.  */
228                         kdb_printf("Instruction(i) breakpoint #%d at 0x%lx\n",
229                                   i, regs->cr_iip);
230                         kdb_id1(regs->cr_iip);
231                         rv = KDB_DB_BPT;
232                         bp->bp_delay = 1;
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.
238                          */
239                         KDB_STATE_SET(SSBPT);
240                         break;
241                 }
242         }
243
244         return rv;
245 }
246
247 /*
248  * kdba_handle_bp
249  *
250  *      Handle an instruction-breakpoint trap.  Called when re-installing
251  *      an enabled breakpoint which has has the bp_delay bit set.
252  *
253  * Parameters:
254  * Returns:
255  * Locking:
256  * Remarks:
257  *
258  * Ok, we really need to:
259  *      1) Restore the original instruction byte(s)
260  *      2) Single Step
261  *      3) Restore breakpoint instruction
262  *      4) Continue.
263  *
264  *
265  */
266
267 static void
268 kdba_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
269 {
270         if (KDB_NULL_REGS(regs))
271                 return;
272
273         if (KDB_DEBUG(BP))
274                 kdb_printf("regs->cr_iip = 0x%lx\n", regs->cr_iip);
275
276         /*
277          * Setup single step
278          */
279         kdba_setsinglestep(regs);
280
281         /*
282          * Reset delay attribute
283          */
284         bp->bp_delay = 0;
285         bp->bp_delayed = 1;
286 }
287
288
289 /*
290  * kdba_bptype
291  *
292  *      Return a string describing type of breakpoint.
293  *
294  * Parameters:
295  *      bph     Pointer to hardware breakpoint description
296  * Outputs:
297  *      None.
298  * Returns:
299  *      Character string.
300  * Locking:
301  *      None.
302  * Remarks:
303  */
304
305 char *
306 kdba_bptype(kdbhard_bp_t *bph)
307 {
308         char *mode;
309
310         mode = kdba_rwtypes[bph->bph_mode];
311
312         return mode;
313 }
314
315 /*
316  * kdba_printbpreg
317  *
318  *      Print register name assigned to breakpoint
319  *
320  * Parameters:
321  *      bph     Pointer hardware breakpoint structure
322  * Outputs:
323  *      None.
324  * Returns:
325  *      None.
326  * Locking:
327  *      None.
328  * Remarks:
329  */
330
331 static void
332 kdba_printbpreg(kdbhard_bp_t *bph)
333 {
334         kdb_printf(" in dr%ld", bph->bph_reg);
335 }
336
337 /*
338  * kdba_printbp
339  *
340  *      Print string describing hardware breakpoint.
341  *
342  * Parameters:
343  *      bph     Pointer to hardware breakpoint description
344  * Outputs:
345  *      None.
346  * Returns:
347  *      None.
348  * Locking:
349  *      None.
350  * Remarks:
351  */
352
353 void
354 kdba_printbp(kdb_bp_t *bp)
355 {
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.
360                  */
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);
365                 }
366         }
367 }
368
369 /*
370  * kdba_parsebp
371  *
372  *      Parse architecture dependent portion of the
373  *      breakpoint command.
374  *
375  * Parameters:
376  *      None.
377  * Outputs:
378  *      None.
379  * Returns:
380  *      Zero for success, a kdb diagnostic for failure
381  * Locking:
382  *      None.
383  * Remarks:
384  *      for IA64 architure, data access, data write and
385  *      I/O breakpoints are supported in addition to instruction
386  *      breakpoints.
387  *
388  *      {datar|dataw|io|inst} [length]
389  */
390
391 int
392 kdba_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
393 {
394         int nextarg = *nextargp;
395         int diag;
396         kdbhard_bp_t *bph = &bp->bp_template;
397
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) {
402                         bph->bph_mode = 3;
403                 } else if (strnicmp(argv[nextarg], "dataw", sizeof("dataw")) == 0) {
404                         bph->bph_mode = 1;
405                 } else if (strnicmp(argv[nextarg], "io", sizeof("io")) == 0) {
406                         bph->bph_mode = 2;
407                 } else if (strnicmp(argv[nextarg], "inst", sizeof("inst")) == 0) {
408                         bph->bph_mode = 0;
409                 } else {
410                         return KDB_ARGCOUNT;
411                 }
412
413                 if (bph->bph_mode == 0)
414                         kdba_check_pc(&bp->bp_addr);
415
416                 bph->bph_length = 3;    /* Default to 4 byte */
417
418                 nextarg++;
419
420                 if ((argc + 1) != nextarg) {
421                         unsigned long len;
422
423                         diag = kdbgetularg((char *)argv[nextarg],
424                                            &len);
425                         if (diag)
426                                 return diag;
427
428
429                         if ((len > 4) || (len == 3))
430                                 return KDB_BADLENGTH;
431
432                         bph->bph_length = len;
433                         bph->bph_length--; /* Normalize for debug register */
434                         nextarg++;
435                 }
436
437                 if ((argc + 1) != nextarg)
438                         return KDB_ARGCOUNT;
439
440                 /*
441                  * Indicate to architecture independent level that
442                  * a hardware register assignment is required to enable
443                  * this breakpoint.
444                  */
445
446                 bph->bph_free = 0;
447         } else {
448                 if (KDB_DEBUG(BP))
449                         kdb_printf("kdba_bp: no args, forcehw is %d\n", bp->bp_forcehw);
450                 if (bp->bp_forcehw) {
451                         /*
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.
455                          */
456                         bph->bph_free = 0;
457                 } else {
458                         /*
459                          * Indicate to architecture dependent level that
460                          * the instruction replacement breakpoint technique
461                          * should be used for this breakpoint.
462                          */
463                         bph->bph_free = 1;
464                         bp->bp_adjust = 0;      /* software, break is fault, not trap */
465                 }
466         }
467
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");
470                 return KDB_BADADDR;
471         }
472
473         *nextargp = nextarg;
474 #ifndef CONFIG_KDB_HARDWARE_BREAKPOINTS
475         if (!bph->bph_free) {
476                 kdb_printf("kdba_parsebp hardware breakpoints are not supported yet\n");
477                 return KDB_NOTIMP;
478         }
479 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
480         return 0;
481 }
482
483 /*
484  * kdba_allocbp
485  *
486  *      Associate a hardware register with a breakpoint.
487  *
488  * Parameters:
489  *      None.
490  * Outputs:
491  *      None.
492  * Returns:
493  *      A pointer to the allocated register kdbhard_bp_t structure for
494  *      success, Null and a non-zero diagnostic for failure.
495  * Locking:
496  *      None.
497  * Remarks:
498  */
499
500 static kdbhard_bp_t *
501 kdba_allocbp(kdbhard_bp_t *bph, int *diagp)
502 {
503         int i;
504         kdbhard_bp_t *newbph;
505
506         for(i=0,newbph=kdb_hardbreaks; i < KDB_MAXHARDBPT; i++, newbph++) {
507                 if (newbph->bph_free) {
508                         break;
509                 }
510         }
511
512         if (i == KDB_MAXHARDBPT) {
513                 *diagp = KDB_TOOMANYDBREGS;
514                 return NULL;
515         }
516
517         *diagp = 0;
518
519         /*
520          * Copy data from template.  Can't just copy the entire template
521          * here because the register number in kdb_hardbreaks must be
522          * preserved.
523          */
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;
528
529         /*
530          * Mark entry allocated.
531          */
532         newbph->bph_free = 0;
533
534         return newbph;
535 }
536
537 /*
538  * kdba_alloc_hwbp
539  *
540  *      Associate a hardware registers with a breakpoint.
541  *      If hw bp is global hw registers descriptor will be allocated
542  *      on every CPU.
543  *
544  * Parameters:
545  *      bp - hardware bp
546  *      diagp - pointer to variable that will store error when
547  *      function complete
548  * Outputs:
549  *      None.
550  * Returns:
551  *      None
552  * Locking:
553  *      None.
554  * Remarks:
555  *      Should be called with correct bp->bp_template.
556  */
557
558 void
559 kdba_alloc_hwbp(kdb_bp_t *bp, int *diagp)
560 {
561         /* Note that bp->bp_hard[NR_CPU] is for x86.
562          * The ia64 uses bp->bp_hard[0] only.
563          */
564         bp->bp_hard[0] = kdba_allocbp(&bp->bp_template, diagp);
565         bp->bp_hardtype = 1;
566 }
567
568
569
570 /*
571  * kdba_freebp
572  *
573  *      Deallocate a hardware breakpoint
574  *
575  * Parameters:
576  *      None.
577  * Outputs:
578  *      None.
579  * Returns:
580  *      Zero for success, a kdb diagnostic for failure
581  * Locking:
582  *      None.
583  * Remarks:
584  */
585
586 static void
587 kdba_freebp(kdbhard_bp_t *bph)
588 {
589         bph->bph_free = 1;
590 }
591
592 /*
593  * kdba_free_hwbp
594  *
595  *     Frees allocated hw registers descriptors for bp.
596  *     If hw bp is global, hw registers descriptors will be freed
597  *     on every CPU.
598  *
599  * Parameters:
600  *     bp - hardware bp
601  * Outputs:
602  *     None.
603  * Returns:
604  *     None
605  * Locking:
606  *     None.
607  * Remarks:
608  *     Should be called with correct bp->bp_template
609  */
610
611 void
612 kdba_free_hwbp(kdb_bp_t *bp)
613 {
614         /* When kernel enters KDB, first, all local bps
615          * are removed, so here we don't need to clear
616          * debug registers.
617          */
618
619         kdba_freebp(bp->bp_hard[0]);
620         bp->bp_hard[0] = NULL;
621         bp->bp_hardtype = 0;
622 }
623
624
625 /*
626  * kdba_initbp
627  *
628  *      Initialize the breakpoint table for the hardware breakpoint
629  *      register.
630  *
631  * Parameters:
632  *      None.
633  * Outputs:
634  *      None.
635  * Returns:
636  *      Zero for success, a kdb diagnostic for failure
637  * Locking:
638  *      None.
639  * Remarks:
640  *
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.
644  */
645
646 void
647 kdba_initbp(void)
648 {
649         int i;
650         kdbhard_bp_t *bph;
651
652         /*
653          * Clear the hardware breakpoint table
654          */
655
656         memset(kdb_hardbreaks, '\0', sizeof(kdb_hardbreaks));
657
658         for(i=0,bph=kdb_hardbreaks; i<KDB_MAXHARDBPT; i++, bph++) {
659                 bph->bph_reg = i;
660                 bph->bph_free = 1;
661         }
662 }
663
664 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
665 /*
666  * Enable Instruction Debug & Data Debug faults on this CPU now.
667  */
668 static inline void kdba_enable_debug_faults(void)
669 {
670         unsigned long tmp;
671
672         tmp = ia64_getreg(_IA64_REG_PSR);
673         ia64_stop();
674         tmp |= IA64_PSR_DB;
675         ia64_stop();
676         ia64_setreg(_IA64_REG_PSR_L, tmp);
677         ia64_srlz_i();
678         if (KDB_DEBUG(BP))
679                 kdb_printf("enabling debug faults: [%d]PSR.L=%08x\n",
680                            smp_processor_id(), (unsigned int)tmp);
681 }
682
683 /*
684  * Disable Instruction Debug & Data Debug faults on this CPU now.
685  */
686 static inline void kdba_disable_debug_faults(void)
687 {
688         unsigned long tmp;
689
690         tmp = ia64_getreg(_IA64_REG_PSR);
691         ia64_stop();
692         tmp &= ~IA64_PSR_DB;
693         ia64_stop();
694         ia64_setreg(_IA64_REG_PSR_L, tmp);
695         ia64_srlz_i();
696         if (KDB_DEBUG(BP))
697                 kdb_printf("disabling debug faults: [%d]PSR.L=%08x\n",
698                            smp_processor_id(), (unsigned int)tmp);
699 }
700 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
701
702 /*
703  * kdba_installbp
704  *
705  *      Install a breakpoint
706  *
707  * Parameters:
708  *      regs    Exception frame
709  *      bp      Breakpoint structure for the breakpoint to be installed
710  * Outputs:
711  *      None.
712  * Returns:
713  *      0 if breakpoint set, otherwise error.
714  * Locking:
715  *      None.
716  * Remarks:
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
720  *      is disabled.
721  *
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.
727  */
728
729 int
730 kdba_installbp(struct pt_regs *regs, kdb_bp_t *bp)
731 {
732         /*
733          * Install the breakpoint, if it is not already installed.
734          */
735
736         if (KDB_DEBUG(BP)) {
737                 kdb_printf("kdba_installbp bp_installed %d\n", bp->bp_installed);
738         }
739         if (!KDB_STATE(SSBPT))
740                 bp->bp_delay = 0;
741
742         if (bp->bp_hardtype) {
743 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
744                 /*
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
748                  */
749                 kdba_installdbreg(bp);
750                 if (++kdb_dbrs_used[smp_processor_id()] == 1)
751                         kdba_enable_debug_faults();
752                 bp->bp_installed = 1;
753                 if (KDB_DEBUG(BP)) {
754                         kdb_printf("kdba_installbp hardware reg %ld at " kdb_bfd_vma_fmt0 "\n",
755                                    bp->bp_hard[0]->bph_reg, bp->bp_addr);
756                 }
757 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
758
759         } else if (bp->bp_delay) {
760                 if (!bp->bp_installed) {
761                         if (KDB_DEBUG(BP))
762                                 kdb_printf("kdba_installbp delayed bp\n");
763                         kdba_handle_bp(regs, bp);
764                 }
765         } else {
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.
771                          */
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);
775                                 return(1);
776                         }
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);
780                                 return(1);
781                         }
782                         if (KDB_DEBUG(BP))
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);
787                 }
788         }
789         return(0);
790 }
791
792 /*
793  * kdba_removebp
794  *
795  *      Make a breakpoint ineffective.
796  *
797  * Parameters:
798  *      None.
799  * Outputs:
800  *      None.
801  * Returns:
802  *      0 if breakpoint removed, otherwise error.
803  * Locking:
804  *      None.
805  * Remarks:
806  */
807
808 int
809 kdba_removebp(kdb_bp_t *bp)
810 {
811         /*
812          * For hardware breakpoints, remove it from the active register,
813          * for software breakpoints, restore the instruction stream.
814          */
815         if (KDB_DEBUG(BP)) {
816                 kdb_printf("kdba_removebp bp_installed %d\n", bp->bp_installed);
817         }
818
819         if (bp->bp_hardtype) {
820 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
821                 if (KDB_DEBUG(BP)) {
822                         kdb_printf("kdb: removing hardware reg %ld at " kdb_bfd_vma_fmt0 "\n",
823                                    bp->bp_hard[0]->bph_reg, bp->bp_addr);
824                 }
825                 if (--kdb_dbrs_used[smp_processor_id()] == 0)
826                         kdba_disable_debug_faults();
827                 kdba_removedbreg(bp);
828 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
829         } else {
830                 if (bp->bp_installed) {
831                         if (KDB_DEBUG(BP))
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)))
835                                 return(1);
836                 }
837                 bp->bp_installed = 0;
838                 flush_icache_range(bp->bp_addr, bp->bp_addr+16);
839         }
840         return(0);
841 }