2 * Kernel Debugger Architecture Independent Support Functions
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/config.h>
12 #include <linux/string.h>
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/ptrace.h>
18 #include <linux/sched.h>
19 #include <linux/kdb.h>
20 #include <linux/kdbprivate.h>
21 #include <asm/kdebug.h>
22 #include <asm/processor.h>
24 #include <asm/uaccess.h>
27 #define SP_SANITY(ss, sp) 1
28 #define SS(sp) ((sp) & -THREAD_SIZE) /* XXX */
33 * Given a starting point on the stack and symtab data for the
34 * current function, scan up the stack looking for a return
35 * address for this function.
37 * sp Starting stack pointer for scan
38 * ss Start of stack for current process
39 * symtab kallsyms symbol data for the function
43 * Position on stack of return address, 0 if not found.
47 * This is sensitive to the calling sequence generated by gcc.
52 kdba_find_return(kdb_machreg_t sp, kdb_machreg_t ss, const kdb_symtab_t *symtab)
55 kdb_symtab_t caller_symtab;
58 unsigned char code[7];
59 #define retaddr(off) code[sizeof(code)+(off)]
62 kdb_printf(" kdba_find_return: start\n");
65 if (SP_SANITY(ss, sp) == 0) {
66 kdb_printf(" sp is in wrong stack 0x%lx\n", sp);
70 if ((sp & (THREAD_SIZE - 1)) < sizeof(struct task_struct)) {
71 kdb_printf(" sp is inside task_struct\n");
75 for (;ret = 0, sp & (THREAD_SIZE-1);sp += 8) {
77 kdb_printf(" sp=0x%lx", sp);
79 if (kdb_getword(&ret, sp, 8))
81 kdbnearsym(ret, &caller_symtab);
84 kdb_symbol_print(ret, &caller_symtab, KDB_SP_DEFAULT|KDB_SP_SYMSIZE);
86 if (!caller_symtab.sym_name) {
90 continue; /* not a valid kernel address */
92 KDB_STATE_SET(SUPPRESS);
93 if (kdb_getarea(code, ret-sizeof(code)) ||
94 kdb_getword(&disp32, ret-4, 4) ||
95 kdb_getword(&disp8, ret-1, 1))
96 continue; /* not a valid return address */
97 if (retaddr(-5) == 0xe8) {
100 kdb_printf(" call disp32");
102 if (ret + (s32) disp32 == symtab->sym_start) {
103 if (KDB_DEBUG(ARA)) {
104 kdb_printf(" matched\n");
106 break; /* call to this function */
108 if (KDB_DEBUG(ARA)) {
109 kdb_printf(" failed");
111 } else if (retaddr(-7) == 0xff && retaddr(-6) == 0x14 && retaddr(-5) == 0x85) {
112 /* call *disp32(,%rax,4), used by syscall.
113 * Cannot calculate address, assume it is valid
114 * if the current function name starts with
117 if (KDB_DEBUG(ARA)) {
119 kdb_printf(" call *0xnnnn(,%%rax,4)");
121 if (strncmp(symtab->sym_name, "sys_", 4) == 0 ||
122 strncmp(symtab->sym_name, "old_", 4) == 0) {
123 if (KDB_DEBUG(ARA)) {
124 kdb_printf(" matched\n");
126 break; /* probably call to this function */
128 if (KDB_DEBUG(ARA)) {
129 kdb_printf(" failed");
131 } else if (retaddr(-2) == 0xff && (retaddr(-1) & 0xf8) == 0xd0) {
132 /* call *%reg. Cannot validate, have to assume
135 if (KDB_DEBUG(ARA)) {
136 kdb_printf(" call *%%reg, assume valid\n");
138 break; /* hope it is a call to this function */
139 } else if (retaddr(-3) == 0xff && (retaddr(-2) & 0xf8) == 0x50) {
140 /* call *disp8(%reg). Cannot validate, have to assume
143 if (KDB_DEBUG(ARA)) {
144 kdb_printf(" call *disp8(%%reg), assume valid\n");
146 break; /* hope it is a call to this function */
147 } else if (retaddr(-5) == 0xe9) {
148 /* jmp disp32. I have been told that gcc may
149 * do function tail optimization and replace
152 if (KDB_DEBUG(ARA)) {
153 kdb_printf(" jmp disp32\n");
155 if (ret + (s32) disp32 == symtab->sym_start) {
156 if (KDB_DEBUG(ARA)) {
157 kdb_printf(" matched\n");
159 break; /* jmp to this function */
161 if (KDB_DEBUG(ARA)) {
162 kdb_printf(" failed");
164 } else if (retaddr(-2) == 0xeb) {
166 if (KDB_DEBUG(ARA)) {
167 kdb_printf(" jmp disp8\n");
169 if (ret + (s8) disp8 == symtab->sym_start) {
170 if (KDB_DEBUG(ARA)) {
171 kdb_printf(" matched\n");
173 break; /* jmp to this function */
175 if (KDB_DEBUG(ARA)) {
176 kdb_printf(" failed");
178 } else if (strcmp(caller_symtab.sym_name, "ret_from_intr") == 0
179 && ret == caller_symtab.sym_start) {
180 /* ret_from_intr is pushed on stack for interrupts */
181 if (KDB_DEBUG(ARA)) {
182 kdb_printf(" ret_from_intr matched\n");
184 break; /* special case, hand crafted frame */
186 if (KDB_DEBUG(ARA)) {
190 if (KDB_DEBUG(ARA)) {
191 kdb_printf(" end ret=0x%lx sp=0x%lx\n", ret, sp);
201 * This function analyzes a gcc-generated function prototype
202 * with or without frame pointers to determine the amount of
203 * automatic storage and register save storage is used on the
204 * stack of the target function. It only counts instructions
205 * that have been executed up to but excluding the current rip.
207 * code Start address of function code to analyze
208 * pc Current program counter within function
209 * sp Current stack pointer for function
210 * ss Start of stack for current process
211 * fp Current frame pointer for function, may not be valid
212 * caller 1 if looking for data on the caller frame, 0 for callee.
214 * ar Activation record, all fields may be set. fp and oldfp
215 * are 0 if they cannot be extracted. return is 0 if the
216 * code cannot find a valid return address. args and arg0
217 * are 0 if the number of arguments cannot be safely
220 * 1 if prologue is valid, 0 otherwise. If pc is 0 treat it as a
221 * valid prologue to allow bt on wild branches.
241 * mov %rdi,0xfffffffffffffff8(%rbp)
246 kdba_prologue(const kdb_symtab_t *symtab, kdb_machreg_t pc, kdb_machreg_t sp,
247 kdb_machreg_t fp, kdb_machreg_t ss, int caller, kdb_ar_t *ar)
249 kdb_machreg_t ret_p, code = symtab->sym_start;
250 int oldfp_present = 0, unwound = 0;
251 unsigned char instruction[6];
253 if (KDB_DEBUG(ARA)) {
254 kdb_printf("kdba_prologue: code=0x%lx %s pc=0x%lx sp=0x%lx fp=0x%lx\n",
255 code, symtab->sym_name, pc, sp, fp);
258 /* Special case for wild branches. Assumes top of stack is return address */
260 memset(ar, 0, sizeof(*ar));
263 ar->start = ar->end + 8;
264 kdb_getword(&(ar->ret), sp, 8);
265 if (KDB_DEBUG(ARA)) {
266 kdb_printf(" pc==0: ret=0x%lx\n", ar->ret);
271 if (code == 0 || sp & 3 || (SP_SANITY(ss, sp) == 0))
274 ar->end = sp; /* End of activation record +1 */
276 /* Special cases galore when the caller pc is within entry.S.
277 * The return address for these routines is outside the kernel,
278 * so the normal algorithm to find the frame does not work.
279 * Hand craft the frame to no setup, regs, locals etc, assume 6
281 * This list was extracted from entry.S by looking for all call
282 * instructions that were eventually followed by RESTORE_ALL,
283 * take the label before each such instruction.
286 (strcmp(symtab->sym_name, "lcall7") == 0 ||
287 strcmp(symtab->sym_name, "lcall27") == 0 ||
288 strcmp(symtab->sym_name, "kdb_call") == 0 ||
289 strcmp(symtab->sym_name, "system_call") == 0 ||
290 strcmp(symtab->sym_name, "tracesys") == 0 ||
291 strcmp(symtab->sym_name, "signal_return") == 0 ||
292 strcmp(symtab->sym_name, "v86_signal_return") == 0 ||
293 strcmp(symtab->sym_name, "tracesys") == 0 ||
294 strcmp(symtab->sym_name, "tracesys_exit") == 0 ||
295 strcmp(symtab->sym_name, "handle_softirq") == 0 ||
296 strcmp(symtab->sym_name, "reschedule") == 0 ||
297 strcmp(symtab->sym_name, "error_code") == 0 ||
298 strcmp(symtab->sym_name, "device_not_available") == 0 ||
299 strcmp(symtab->sym_name, "nmi") == 0)) {
300 ar->start = ar->end + 6*8; /* 6 parameters */
301 if (SP_SANITY(ss, ar->start) == 0)
306 ar->setup = 8; /* Return address is always on stack */
308 /* Kludge. If we are sitting on 'ret' then the stack has been unwound,
309 * ignore all the startup code.
311 if (kdb_getarea(instruction[0], pc))
313 if (instruction[0] == 0xc3) {
318 if (kdb_getarea(instruction, code))
320 if (!unwound && code < pc && instruction[0] == 0x55) {
322 ar->setup += 8; /* Frame pointer is on stack */
325 if (KDB_DEBUG(ARA)) {
326 kdb_printf(" pushq %%rbp\n");
328 if (code < pc && instruction[0] == 0x89 && instruction[1] == 0xe5) {
330 if (fp >= sp && SP_SANITY(ss, fp))
331 ar->fp = fp; /* %rbp has been set */
333 if (KDB_DEBUG(ARA)) {
334 kdb_printf(" movq %%rsp,%%rbp, fp=0x%lx\n", ar->fp);
339 if (!unwound && code < pc) {
340 if (instruction[0] == 0x83 && instruction[1] == 0xec) {
342 kdb_getword(&(ar->locals), (unsigned long)(instruction+2), 1);
344 if (KDB_DEBUG(ARA)) {
345 kdb_printf(" subl $xx,%%rsp, locals=%ld\n", ar->locals);
347 } else if (instruction[0] == 0x81 && instruction[1] == 0xec) {
348 /* subl $xxxxxxxx,%rsp */
349 kdb_getword(&(ar->locals), (unsigned long)(instruction+2), 8);
351 if (KDB_DEBUG(ARA)) {
352 kdb_printf(" subl $xxxxxxxx,%%rsp, locals=%ld\n", ar->locals);
357 while (!unwound && code < pc &&
358 kdb_getarea(instruction, code) == 0 &&
359 (instruction[0] & 0xf8) == 0x50) {
363 if (KDB_DEBUG(ARA)) {
364 kdb_printf(" pushq %%reg, regs=%ld\n", ar->regs);
368 /* Check the return address. It must point within the kernel
369 * and the code at that location must be a valid entry sequence.
372 ret_p = ar->fp + ar->setup;
375 ret_p = ar->end + ar->regs + ar->locals + ar->setup;
378 if (KDB_DEBUG(ARA)) {
379 kdb_printf(" ret_p(0)=0x%lx\n", ret_p);
382 if ((SP_SANITY(ss, ret_p) != 0) &&
383 (ret_p = kdba_find_return(ret_p, ss, symtab))) {
384 kdb_getword(&(ar->ret), ret_p, 8);
386 if (KDB_DEBUG(ARA)) {
387 kdb_printf(" ret_p(1)=0x%lx ret=0x%lx\n", ret_p, ar->ret);
390 ar->fp = ret_p - ar->setup + 8; /* "accurate" fp */
391 ar->start = ret_p + 8;
392 if (KDB_DEBUG(ARA)) {
393 kdb_printf(" fp=0x%lx start=0x%lx\n", ar->fp, ar->start);
398 kdb_getword(&(ar->oldfp), ar->fp, 8);
399 if (KDB_DEBUG(ARA)) {
400 kdb_printf(" oldfp=0x%lx", ar->oldfp);
402 if (ar->oldfp <= ar->fp || (SP_SANITY(ss, ar->oldfp) == 0)) {
404 if (KDB_DEBUG(ARA)) {
405 kdb_printf(" (out of range)");
408 if (KDB_DEBUG(ARA)) {
418 return kdba_getdr(6);
424 return kdba_getdr(7);
428 kdba_putdr6(kdb_machreg_t contents)
430 kdba_putdr(6, contents);
434 kdba_putdr7(kdb_machreg_t contents)
436 kdba_putdr(7, contents);
440 kdba_installdbreg(kdb_bp_t *bp)
446 kdba_putdr(bp->bp_hard->bph_reg, bp->bp_addr);
450 set_in_cr4(X86_CR4_DE);
452 switch (bp->bp_hard->bph_reg){
454 DR7_RW0SET(dr7,bp->bp_hard->bph_mode);
455 DR7_LEN0SET(dr7,bp->bp_hard->bph_length);
459 DR7_RW1SET(dr7,bp->bp_hard->bph_mode);
460 DR7_LEN1SET(dr7,bp->bp_hard->bph_length);
464 DR7_RW2SET(dr7,bp->bp_hard->bph_mode);
465 DR7_LEN2SET(dr7,bp->bp_hard->bph_length);
469 DR7_RW3SET(dr7,bp->bp_hard->bph_mode);
470 DR7_LEN3SET(dr7,bp->bp_hard->bph_length);
474 kdb_printf("kdb: Bad debug register!! %ld\n",
475 bp->bp_hard->bph_reg);
484 kdba_removedbreg(kdb_bp_t *bp)
492 regnum = bp->bp_hard->bph_reg;
496 kdba_putdr(regnum, 0);
516 kdb_printf("kdb: Bad debug register!! %d\n", regnum);
524 kdba_getdr(int regnum)
526 kdb_machreg_t contents = 0;
529 __asm__ ("movq %%db0,%0\n\t":"=r"(contents));
532 __asm__ ("movq %%db1,%0\n\t":"=r"(contents));
535 __asm__ ("movq %%db2,%0\n\t":"=r"(contents));
538 __asm__ ("movq %%db3,%0\n\t":"=r"(contents));
544 __asm__ ("movq %%db6,%0\n\t":"=r"(contents));
547 __asm__ ("movq %%db7,%0\n\t":"=r"(contents));
558 kdb_getcr(int regnum)
560 kdb_machreg_t contents = 0;
563 __asm__ ("movq %%cr0,%0\n\t":"=r"(contents));
568 __asm__ ("movq %%cr2,%0\n\t":"=r"(contents));
571 __asm__ ("movq %%cr3,%0\n\t":"=r"(contents));
574 __asm__ ("movq %%cr4,%0\n\t":"=r"(contents));
584 kdba_putdr(int regnum, kdb_machreg_t contents)
588 __asm__ ("movq %0,%%db0\n\t"::"r"(contents));
591 __asm__ ("movq %0,%%db1\n\t"::"r"(contents));
594 __asm__ ("movq %0,%%db2\n\t"::"r"(contents));
597 __asm__ ("movq %0,%%db3\n\t"::"r"(contents));
603 __asm__ ("movq %0,%%db6\n\t"::"r"(contents));
606 __asm__ ("movq %0,%%db7\n\t"::"r"(contents));
614 * kdba_getregcontents
616 * Return the contents of the register specified by the
617 * input string argument. Return an error if the string
618 * does not match a machine register.
620 * The following pseudo register names are supported:
621 * ®s - Prints address of exception frame
622 * krsp - Prints kernel stack pointer at time of fault
623 * crsp - Prints current kernel stack pointer, inside kdb
624 * ceflags - Prints current flags, inside kdb
625 * %<regname> - Uses the value of the registers at the
626 * last time the user process entered kernel
627 * mode, instead of the registers at the time
631 * regname Pointer to string naming register
632 * regs Pointer to structure containing registers.
634 * *contents Pointer to unsigned long to recieve register contents
637 * KDB_BADREG Invalid register name
641 * If kdb was entered via an interrupt from the kernel itself then
642 * ss and rsp are *not* on the stack.
645 static struct kdbregs {
649 { "r15", offsetof(struct pt_regs, r15) },
650 { "r14", offsetof(struct pt_regs, r14) },
651 { "r13", offsetof(struct pt_regs, r13) },
652 { "r12", offsetof(struct pt_regs, r12) },
653 { "rbp", offsetof(struct pt_regs, rbp) },
654 { "rbx", offsetof(struct pt_regs, rbx) },
655 { "r11", offsetof(struct pt_regs, r11) },
656 { "r10", offsetof(struct pt_regs, r10) },
657 { "r9", offsetof(struct pt_regs, r9) },
658 { "r8", offsetof(struct pt_regs, r8) },
659 { "rax", offsetof(struct pt_regs, rax) },
660 { "rcx", offsetof(struct pt_regs, rcx) },
661 { "rdx", offsetof(struct pt_regs, rdx) },
662 { "rsi", offsetof(struct pt_regs, rsi) },
663 { "rdi", offsetof(struct pt_regs, rdi) },
664 { "orig_rax", offsetof(struct pt_regs, orig_rax) },
665 { "rip", offsetof(struct pt_regs, rip) },
666 { "cs", offsetof(struct pt_regs, cs) },
667 { "eflags", offsetof(struct pt_regs, eflags) },
668 { "rsp", offsetof(struct pt_regs, rsp) },
669 { "ss", offsetof(struct pt_regs, ss) },
672 static const int nkdbreglist = sizeof(kdbreglist) / sizeof(struct kdbregs);
674 static struct kdbregs dbreglist[] = {
683 static const int ndbreglist = sizeof(dbreglist) / sizeof(struct kdbregs);
686 kdba_getregcontents(const char *regname,
687 struct pt_regs *regs,
688 kdb_machreg_t *contents)
692 if (strcmp(regname, "®s") == 0) {
693 *contents = (unsigned long)regs;
697 if (strcmp(regname, "krsp") == 0) {
698 *contents = (unsigned long)regs + sizeof(struct pt_regs);
699 if ((regs->cs & 0xffff) == __KERNEL_CS) {
700 /* rsp and ss are not on stack */
706 if (strcmp(regname, "crsp") == 0) {
707 asm volatile("movq %%rsp,%0":"=m" (*contents));
711 if (strcmp(regname, "ceflags") == 0) {
713 local_save_flags(flags);
718 if (regname[0] == '%') {
719 /* User registers: %%r[a-c]x, etc */
721 regs = (struct pt_regs *)
722 (current->thread.rsp0 - sizeof(struct pt_regs));
725 for (i=0; i<nkdbreglist; i++) {
726 if (strnicmp(kdbreglist[i].reg_name,
728 strlen(regname)) == 0)
732 if ((i < nkdbreglist)
733 && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
734 if ((regs->cs & 0xffff) == __KERNEL_CS) {
735 /* No cpl switch, rsp is not on stack */
736 if (strcmp(kdbreglist[i].reg_name, "rsp") == 0) {
737 *contents = (kdb_machreg_t)regs +
738 sizeof(struct pt_regs) - 2*8;
741 if (strcmp(kdbreglist[i].reg_name, "ss") == 0) {
744 r = (kdb_machreg_t)regs +
745 sizeof(struct pt_regs) - 2*8;
746 *contents = (kdb_machreg_t)SS(r); /* XXX */
750 *contents = *(unsigned long *)((unsigned long)regs +
751 kdbreglist[i].reg_offset);
755 for (i=0; i<ndbreglist; i++) {
756 if (strnicmp(dbreglist[i].reg_name,
758 strlen(regname)) == 0)
763 && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
764 *contents = kdba_getdr(dbreglist[i].reg_offset);
771 * kdba_setregcontents
773 * Set the contents of the register specified by the
774 * input string argument. Return an error if the string
775 * does not match a machine register.
777 * Supports modification of user-mode registers via
781 * regname Pointer to string naming register
782 * regs Pointer to structure containing registers.
783 * contents Unsigned long containing new register contents
787 * KDB_BADREG Invalid register name
794 kdba_setregcontents(const char *regname,
795 struct pt_regs *regs,
796 unsigned long contents)
800 if (regname[0] == '%') {
802 regs = (struct pt_regs *)
803 (current->thread.rsp0 - sizeof(struct pt_regs));
806 for (i=0; i<nkdbreglist; i++) {
807 if (strnicmp(kdbreglist[i].reg_name,
809 strlen(regname)) == 0)
813 if ((i < nkdbreglist)
814 && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
815 *(unsigned long *)((unsigned long)regs
816 + kdbreglist[i].reg_offset) = contents;
820 for (i=0; i<ndbreglist; i++) {
821 if (strnicmp(dbreglist[i].reg_name,
823 strlen(regname)) == 0)
828 && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
829 kdba_putdr(dbreglist[i].reg_offset, contents);
839 * Dump the specified register set to the display.
842 * regs Pointer to structure containing registers.
843 * type Character string identifying register set to dump
844 * extra string further identifying register (optional)
851 * This function will dump the general register set if the type
852 * argument is NULL (struct pt_regs). The alternate register
853 * set types supported by this function:
856 * c Control registers
857 * u User registers at most recent entry to kernel
858 * Following not yet implemented:
859 * m Model Specific Registers (extra defines register #)
860 * r Memory Type Range Registers (extra defines register)
864 kdba_dumpregs(struct pt_regs *regs,
872 && (type[0] == 'u')) {
874 regs = (struct pt_regs *)
875 (current->thread.rsp0 - sizeof(struct pt_regs));
880 kdb_machreg_t contents;
882 for (i=0, rlp=kdbreglist; i<nkdbreglist; i++,rlp++) {
883 kdb_printf("%8s = ", rlp->reg_name);
884 kdba_getregcontents(rlp->reg_name, regs, &contents);
885 kdb_printf("0x%016lx ", contents);
886 if ((++count % 2) == 0)
890 kdb_printf("®s = 0x%p\n", regs);
901 if ((i == 4) || (i == 5)) continue;
902 dr[i] = kdba_getdr(i);
904 kdb_printf("dr0 = 0x%08lx dr1 = 0x%08lx dr2 = 0x%08lx dr3 = 0x%08lx\n",
905 dr[0], dr[1], dr[2], dr[3]);
906 kdb_printf("dr6 = 0x%08lx dr7 = 0x%08lx\n",
914 for (i=0; i<5; i++) {
915 cr[i] = kdb_getcr(i);
917 kdb_printf("cr0 = 0x%08lx cr1 = 0x%08lx cr2 = 0x%08lx cr3 = 0x%08lx\ncr4 = 0x%08lx\n",
918 cr[0], cr[1], cr[2], cr[3], cr[4]);
934 kdba_getpc(struct pt_regs *regs)
940 kdba_setpc(struct pt_regs *regs, kdb_machreg_t newpc)
942 if (KDB_NULL_REGS(regs))
945 KDB_STATE_SET(IP_ADJUSTED);
952 * Do any architecture specific set up before entering the main kdb loop.
953 * The primary function of this routine is to make all processes look the
954 * same to kdb, kdb must be able to list a process without worrying if the
955 * process is running or blocked, so make all process look as though they
959 * reason The reason KDB was invoked
960 * error The hardware-defined error code
961 * error2 kdb's current reason code. Initially error but can change
962 * acording to kdb state.
963 * db_result Result from break or debug point.
964 * ef The exception frame at time of fault/breakpoint. If reason
965 * is KDB_REASON_SILENT then ef is NULL, otherwise it should
968 * 0 KDB was invoked for an event which it wasn't responsible
969 * 1 KDB handled the event for which it was invoked.
971 * Sets rip and rsp in current->thread.
979 kdba_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
980 kdb_dbtrap_t db_result, struct pt_regs *regs)
985 kdba_getregcontents("rsp", regs, &(current->thread.rsp));
986 kdb_save_running(regs);
987 ret = kdb_main_loop(reason, reason2, error, db_result, regs);
988 kdb_unsave_running(regs);
993 kdba_disableint(kdb_intstate_t *state)
995 unsigned long *fp = (unsigned long *)state;
998 local_irq_save(flags);
1003 kdba_restoreint(kdb_intstate_t *state)
1005 unsigned long flags = *(unsigned long *)state;
1006 local_irq_restore(flags);
1010 kdba_setsinglestep(struct pt_regs *regs)
1012 if (KDB_NULL_REGS(regs))
1014 if (regs->eflags & EF_IE)
1015 KDB_STATE_SET(A_IF);
1017 KDB_STATE_CLEAR(A_IF);
1018 regs->eflags = (regs->eflags | EF_TF) & ~EF_IE;
1022 kdba_clearsinglestep(struct pt_regs *regs)
1024 if (KDB_NULL_REGS(regs))
1026 if (KDB_STATE(A_IF))
1027 regs->eflags |= EF_IE;
1029 regs->eflags &= ~EF_IE;
1033 #ifdef KDB_HAVE_LONGJMP
1035 kdba_setjmp(kdb_jmp_buf *jb)
1037 #if defined(CONFIG_FRAME_POINTER)
1038 __asm__("movq %rbx, (0*8)(%rdi);"
1039 "movq %rbp, (1*8)(%rdi);"
1040 "movq %r12, (2*8)(%rdi);"
1041 "movq %r13, (3*8)(%rdi);"
1042 "movq %r14, (4*8)(%rdi);"
1043 "movq %r15, (5*8)(%rdi);"
1044 "leaq 16(%rsp), %rdx;"
1045 "movq %rdx, (6*8)(%rdi);"
1046 "movq (%rsp), %rax;"
1047 "movq %rax, (7*8)(%rdi)");
1048 #else /* CONFIG_FRAME_POINTER */
1049 __asm__("movq %rbx, (0*8)(%rdi);"
1050 "movq %rbp, (1*8)(%rdi);"
1051 "movq %r12, (2*8)(%rdi);"
1052 "movq %r13, (3*8)(%rdi);"
1053 "movq %r14, (4*8)(%rdi);"
1054 "movq %r15, (5*8)(%rdi);"
1055 "leaq 8(%rsp), %rdx;"
1056 "movq %rdx, (6*8)(%rdi);"
1057 "movq (%rsp), %rax;"
1058 "movq %rax, (7*8)(%rdi)");
1059 #endif /* CONFIG_FRAME_POINTER */
1060 KDB_STATE_SET(LONGJMP);
1065 kdba_longjmp(kdb_jmp_buf *jb, int reason)
1067 #if defined(CONFIG_FRAME_POINTER)
1068 __asm__("movq (0*8)(%rdi),%rbx;"
1069 "movq (1*8)(%rdi),%rbp;"
1070 "movq (2*8)(%rdi),%r12;"
1071 "movq (3*8)(%rdi),%r13;"
1072 "movq (4*8)(%rdi),%r14;"
1073 "movq (5*8)(%rdi),%r15;"
1078 "movq (7*8)(%rdi),%rdx;"
1079 "movq (6*8)(%rdi),%rsp;"
1081 #else /* CONFIG_FRAME_POINTER */
1082 __asm__("movq (0*8)(%rdi),%rbx;"
1083 "movq (1*8)(%rdi),%rbp;"
1084 "movq (2*8)(%rdi),%r12;"
1085 "movq (3*8)(%rdi),%r13;"
1086 "movq (4*8)(%rdi),%r14;"
1087 "movq (5*8)(%rdi),%r15;"
1092 "movq (7*8)(%rdi),%rdx;"
1093 "movq (6*8)(%rdi),%rsp;"
1095 #endif /* CONFIG_FRAME_POINTER */
1097 #endif /* KDB_HAVE_LONGJMP */
1103 * Enable last branch recording.
1115 static unsigned char lbr_warned;
1118 kdba_enable_lbr(void)
1122 if (!test_bit(X86_FEATURE_MCA, &boot_cpu_data.x86_capability)) {
1124 kdb_printf("kdb: machine does not support last branch recording\n");
1129 rdmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1130 lv |= 0x1; /* Set LBR enable */
1131 wrmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1137 * disable last branch recording.
1150 kdba_disable_lbr(void)
1154 if (!test_bit(X86_FEATURE_MCA, &boot_cpu_data.x86_capability)) {
1156 kdb_printf("kdb: machine does not support last branch recording\n");
1161 rdmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1162 lv &= ~0x1; /* Set LBR disable */
1163 wrmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1169 * Print last branch and last exception addresses
1182 kdba_print_lbr(void)
1184 u32 from, to, dummy;
1186 if (!test_bit(X86_FEATURE_MCA, &boot_cpu_data.x86_capability))
1189 rdmsr(MSR_IA32_LASTBRANCHFROMIP, from, dummy);
1190 rdmsr(MSR_IA32_LASTBRANCHTOIP, to, dummy);
1191 kdb_printf("Last Branch IP, from: ");
1192 kdb_symbol_print(from, NULL, KDB_SP_DEFAULT);
1193 kdb_printf(" to: ");
1194 kdb_symbol_print(to, NULL, KDB_SP_DEFAULT);
1196 rdmsr(MSR_IA32_LASTINTFROMIP, from, dummy);
1197 rdmsr(MSR_IA32_LASTINTTOIP, to, dummy);
1198 kdb_printf("Last Int IP, from: ");
1199 kdb_symbol_print(from, NULL, KDB_SP_DEFAULT);
1200 kdb_printf(" to: ");
1201 kdb_symbol_print(to, NULL, KDB_SP_DEFAULT);
1208 * This is the interface routine between
1209 * the notifier die_chain and kdb
1211 static int kdba_entry( struct notifier_block *b, unsigned long val, void *v)
1213 struct die_args *args = v;
1214 int err, trap, ret = 0;
1215 struct pt_regs *regs;
1219 trap = args->trapnr;
1221 #if defined(CONFIG_SMP)
1223 ret = kdb_ipi(regs, NULL);
1227 ret = kdb(KDB_REASON_OOPS, err, regs);
1230 ret = kdb(KDB_REASON_ENTER, err, regs);
1233 ret = kdb(KDB_REASON_DEBUG, err, regs);
1236 ret = kdb(KDB_REASON_BREAK, err, regs);
1241 return (ret ? NOTIFY_STOP : NOTIFY_DONE);
1245 * notifier block for kdb entry
1247 static struct notifier_block kdba_notifier = {
1248 .notifier_call = kdba_entry
1254 * Architecture specific initialization.
1270 notifier_chain_register(&die_chain, &kdba_notifier);
1277 * Architecture specific adjustment of instruction pointer before leaving
1281 * reason The reason KDB was invoked
1282 * error The hardware-defined error code
1283 * ef The exception frame at time of fault/breakpoint. If reason
1284 * is KDB_REASON_SILENT then ef is NULL, otherwise it should
1295 kdba_adjust_ip(kdb_reason_t reason, int error, struct pt_regs *ef)
1300 /* dummy code to make common layer happy for now */
1302 kdba_enable_mce(void)
1307 kdba_set_current_task(const struct task_struct *p)
1309 kdb_current_task = p;
1310 if (kdb_task_has_cpu(p)) {
1311 struct kdb_running_process *krp = kdb_running_process + kdb_process_cpu(p);
1312 kdb_current_regs = krp->regs;
1315 kdb_current_regs = NULL;
1318 #include <asm/mach_apic.h>
1319 #include <asm/hw_irq.h>
1320 void smp_kdb_stop(void)
1322 send_IPI_allbutself(NMI_VECTOR);