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-2006 Silicon Graphics, Inc. All Rights Reserved.
11 #include <linux/string.h>
12 #include <linux/stddef.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/irq.h>
17 #include <linux/ptrace.h>
19 #include <linux/sched.h>
20 #include <linux/hardirq.h>
21 #include <linux/kdb.h>
22 #include <linux/kdbprivate.h>
24 #include <asm/processor.h>
26 #include <asm/uaccess.h>
30 kdba_getcr(int regnum)
32 kdb_machreg_t contents = 0;
35 __asm__ ("movl %%cr0,%0\n\t":"=r"(contents));
40 __asm__ ("movl %%cr2,%0\n\t":"=r"(contents));
43 __asm__ ("movl %%cr3,%0\n\t":"=r"(contents));
46 __asm__ ("movl %%cr4,%0\n\t":"=r"(contents));
56 kdba_putdr(int regnum, kdb_machreg_t contents)
60 __asm__ ("movl %0,%%db0\n\t"::"r"(contents));
63 __asm__ ("movl %0,%%db1\n\t"::"r"(contents));
66 __asm__ ("movl %0,%%db2\n\t"::"r"(contents));
69 __asm__ ("movl %0,%%db3\n\t"::"r"(contents));
75 __asm__ ("movl %0,%%db6\n\t"::"r"(contents));
78 __asm__ ("movl %0,%%db7\n\t"::"r"(contents));
86 kdba_getdr(int regnum)
88 kdb_machreg_t contents = 0;
91 __asm__ ("movl %%db0,%0\n\t":"=r"(contents));
94 __asm__ ("movl %%db1,%0\n\t":"=r"(contents));
97 __asm__ ("movl %%db2,%0\n\t":"=r"(contents));
100 __asm__ ("movl %%db3,%0\n\t":"=r"(contents));
106 __asm__ ("movl %%db6,%0\n\t":"=r"(contents));
109 __asm__ ("movl %%db7,%0\n\t":"=r"(contents));
121 return kdba_getdr(6);
127 return kdba_getdr(7);
131 kdba_putdr6(kdb_machreg_t contents)
133 kdba_putdr(6, contents);
137 kdba_putdr7(kdb_machreg_t contents)
139 kdba_putdr(7, contents);
143 kdba_installdbreg(kdb_bp_t *bp)
149 kdba_putdr(bp->bp_hard->bph_reg, bp->bp_addr);
153 set_in_cr4(X86_CR4_DE);
155 switch (bp->bp_hard->bph_reg){
157 DR7_RW0SET(dr7,bp->bp_hard->bph_mode);
158 DR7_LEN0SET(dr7,bp->bp_hard->bph_length);
162 DR7_RW1SET(dr7,bp->bp_hard->bph_mode);
163 DR7_LEN1SET(dr7,bp->bp_hard->bph_length);
167 DR7_RW2SET(dr7,bp->bp_hard->bph_mode);
168 DR7_LEN2SET(dr7,bp->bp_hard->bph_length);
172 DR7_RW3SET(dr7,bp->bp_hard->bph_mode);
173 DR7_LEN3SET(dr7,bp->bp_hard->bph_length);
177 kdb_printf("kdb: Bad debug register!! %ld\n",
178 bp->bp_hard->bph_reg);
187 kdba_removedbreg(kdb_bp_t *bp)
195 regnum = bp->bp_hard->bph_reg;
199 kdba_putdr(regnum, 0);
219 kdb_printf("kdb: Bad debug register!! %d\n", regnum);
228 * kdba_getregcontents
230 * Return the contents of the register specified by the
231 * input string argument. Return an error if the string
232 * does not match a machine register.
234 * The following pseudo register names are supported:
235 * ®s - Prints address of exception frame
236 * kesp - Prints kernel stack pointer at time of fault
237 * cesp - Prints current kernel stack pointer, inside kdb
238 * ceflags - Prints current flags, inside kdb
239 * %<regname> - Uses the value of the registers at the
240 * last time the user process entered kernel
241 * mode, instead of the registers at the time
245 * regname Pointer to string naming register
246 * regs Pointer to structure containing registers.
248 * *contents Pointer to unsigned long to recieve register contents
251 * KDB_BADREG Invalid register name
255 * If kdb was entered via an interrupt from the kernel itself then
256 * ss and esp are *not* on the stack.
259 static struct kdbregs {
263 { "eax", offsetof(struct pt_regs, ax) },
264 { "ebx", offsetof(struct pt_regs, bx) },
265 { "ecx", offsetof(struct pt_regs, cx) },
266 { "edx", offsetof(struct pt_regs, dx) },
268 { "esi", offsetof(struct pt_regs, si) },
269 { "edi", offsetof(struct pt_regs, di) },
270 { "esp", offsetof(struct pt_regs, sp) },
271 { "eip", offsetof(struct pt_regs, ip) },
273 { "ebp", offsetof(struct pt_regs, bp) },
274 { "xss", offsetof(struct pt_regs, ss) },
275 { "xcs", offsetof(struct pt_regs, cs) },
276 { "eflags", offsetof(struct pt_regs, flags) },
278 { "xds", offsetof(struct pt_regs, ds) },
279 { "xes", offsetof(struct pt_regs, es) },
280 { "origeax", offsetof(struct pt_regs, orig_ax) },
284 static const int nkdbreglist = sizeof(kdbreglist) / sizeof(struct kdbregs);
286 static struct kdbregs dbreglist[] = {
295 static const int ndbreglist = sizeof(dbreglist) / sizeof(struct kdbregs);
298 kdba_getregcontents(const char *regname,
299 struct pt_regs *regs,
300 kdb_machreg_t *contents)
304 if (strcmp(regname, "cesp") == 0) {
305 asm volatile("movl %%esp,%0":"=m" (*contents));
309 if (strcmp(regname, "ceflags") == 0) {
311 local_save_flags(flags);
316 if (regname[0] == '%') {
317 /* User registers: %%e[a-c]x, etc */
319 regs = (struct pt_regs *)
320 (kdb_current_task->thread.sp0 - sizeof(struct pt_regs));
323 for (i=0; i<ndbreglist; i++) {
324 if (strnicmp(dbreglist[i].reg_name,
326 strlen(regname)) == 0)
331 && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
332 *contents = kdba_getdr(dbreglist[i].reg_offset);
337 kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
341 if (strcmp(regname, "®s") == 0) {
342 *contents = (unsigned long)regs;
346 if (strcmp(regname, "kesp") == 0) {
347 *contents = (unsigned long)regs + sizeof(struct pt_regs);
348 if ((regs->cs & 0xffff) == __KERNEL_CS) {
349 /* esp and ss are not on stack */
355 for (i=0; i<nkdbreglist; i++) {
356 if (strnicmp(kdbreglist[i].reg_name,
358 strlen(regname)) == 0)
362 if ((i < nkdbreglist)
363 && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
364 if ((regs->cs & 0xffff) == __KERNEL_CS) {
365 /* No cpl switch, esp and ss are not on stack */
366 if (strcmp(kdbreglist[i].reg_name, "esp") == 0) {
367 *contents = (kdb_machreg_t)regs +
368 sizeof(struct pt_regs) - 2*4;
371 if (strcmp(kdbreglist[i].reg_name, "xss") == 0) {
379 *contents = *(unsigned long *)((unsigned long)regs +
380 kdbreglist[i].reg_offset);
388 * kdba_setregcontents
390 * Set the contents of the register specified by the
391 * input string argument. Return an error if the string
392 * does not match a machine register.
394 * Supports modification of user-mode registers via
398 * regname Pointer to string naming register
399 * regs Pointer to structure containing registers.
400 * contents Unsigned long containing new register contents
404 * KDB_BADREG Invalid register name
411 kdba_setregcontents(const char *regname,
412 struct pt_regs *regs,
413 unsigned long contents)
417 if (regname[0] == '%') {
419 regs = (struct pt_regs *)
420 (kdb_current_task->thread.sp0 - sizeof(struct pt_regs));
423 for (i=0; i<ndbreglist; i++) {
424 if (strnicmp(dbreglist[i].reg_name,
426 strlen(regname)) == 0)
431 && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
432 kdba_putdr(dbreglist[i].reg_offset, contents);
437 kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
441 for (i=0; i<nkdbreglist; i++) {
442 if (strnicmp(kdbreglist[i].reg_name,
444 strlen(regname)) == 0)
448 if ((i < nkdbreglist)
449 && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
450 *(unsigned long *)((unsigned long)regs
451 + kdbreglist[i].reg_offset) = contents;
461 * Dump the specified register set to the display.
464 * regs Pointer to structure containing registers.
465 * type Character string identifying register set to dump
466 * extra string further identifying register (optional)
473 * This function will dump the general register set if the type
474 * argument is NULL (struct pt_regs). The alternate register
475 * set types supported by this function:
478 * c Control registers
479 * u User registers at most recent entry to kernel
480 * for the process currently selected with "pid" command.
481 * Following not yet implemented:
482 * r Memory Type Range Registers (extra defines register)
484 * MSR on i386/x86_64 are handled by rdmsr/wrmsr commands.
488 kdba_dumpregs(struct pt_regs *regs,
496 && (type[0] == 'u')) {
498 regs = (struct pt_regs *)
499 (kdb_current_task->thread.sp0 - sizeof(struct pt_regs));
504 kdb_machreg_t contents;
507 kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
511 for (i=0, rlp=kdbreglist; i<nkdbreglist; i++,rlp++) {
512 kdb_printf("%s = ", rlp->reg_name);
513 kdba_getregcontents(rlp->reg_name, regs, &contents);
514 kdb_printf("0x%08lx ", contents);
515 if ((++count % 4) == 0)
519 kdb_printf("®s = 0x%p\n", regs);
530 if ((i == 4) || (i == 5)) continue;
531 dr[i] = kdba_getdr(i);
533 kdb_printf("dr0 = 0x%08lx dr1 = 0x%08lx dr2 = 0x%08lx dr3 = 0x%08lx\n",
534 dr[0], dr[1], dr[2], dr[3]);
535 kdb_printf("dr6 = 0x%08lx dr7 = 0x%08lx\n",
543 for (i=0; i<5; i++) {
544 cr[i] = kdba_getcr(i);
546 kdb_printf("cr0 = 0x%08lx cr1 = 0x%08lx cr2 = 0x%08lx cr3 = 0x%08lx\ncr4 = 0x%08lx\n",
547 cr[0], cr[1], cr[2], cr[3], cr[4]);
559 EXPORT_SYMBOL(kdba_dumpregs);
562 kdba_getpc(struct pt_regs *regs)
564 return regs ? regs->ip : 0;
568 kdba_setpc(struct pt_regs *regs, kdb_machreg_t newpc)
570 if (KDB_NULL_REGS(regs))
573 KDB_STATE_SET(IP_ADJUSTED);
580 * Do any architecture specific set up before entering the main kdb loop.
581 * The primary function of this routine is to make all processes look the
582 * same to kdb, kdb must be able to list a process without worrying if the
583 * process is running or blocked, so make all process look as though they
587 * reason The reason KDB was invoked
588 * error The hardware-defined error code
589 * error2 kdb's current reason code. Initially error but can change
590 * acording to kdb state.
591 * db_result Result from break or debug point.
592 * regs The exception frame at time of fault/breakpoint. If reason
593 * is SILENT or CPU_UP then regs is NULL, otherwise it should
596 * 0 KDB was invoked for an event which it wasn't responsible
597 * 1 KDB handled the event for which it was invoked.
599 * Sets eip and esp in current->thread.
607 kdba_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
608 kdb_dbtrap_t db_result, struct pt_regs *regs)
611 ret = kdb_save_running(regs, reason, reason2, error, db_result);
612 kdb_unsave_running(regs);
617 kdba_disableint(kdb_intstate_t *state)
619 unsigned long *fp = (unsigned long *)state;
622 local_irq_save(flags);
628 kdba_restoreint(kdb_intstate_t *state)
630 unsigned long flags = *(int *)state;
631 local_irq_restore(flags);
635 kdba_setsinglestep(struct pt_regs *regs)
637 if (KDB_NULL_REGS(regs))
639 if (regs->flags & EF_IE)
642 KDB_STATE_CLEAR(A_IF);
643 regs->flags = (regs->flags | EF_TF) & ~EF_IE;
647 kdba_clearsinglestep(struct pt_regs *regs)
649 if (KDB_NULL_REGS(regs))
652 regs->flags |= EF_IE;
654 regs->flags &= ~EF_IE;
658 kdba_setjmp(kdb_jmp_buf *jb)
660 #if defined(CONFIG_FRAME_POINTER)
661 __asm__ ("movl 8(%esp), %eax\n\t"
662 "movl %ebx, 0(%eax)\n\t"
663 "movl %esi, 4(%eax)\n\t"
664 "movl %edi, 8(%eax)\n\t"
665 "movl (%esp), %ecx\n\t"
666 "movl %ecx, 12(%eax)\n\t"
667 "leal 8(%esp), %ecx\n\t"
668 "movl %ecx, 16(%eax)\n\t"
669 "movl 4(%esp), %ecx\n\t"
670 "movl %ecx, 20(%eax)\n\t");
671 #else /* CONFIG_FRAME_POINTER */
672 __asm__ ("movl 4(%esp), %eax\n\t"
673 "movl %ebx, 0(%eax)\n\t"
674 "movl %esi, 4(%eax)\n\t"
675 "movl %edi, 8(%eax)\n\t"
676 "movl %ebp, 12(%eax)\n\t"
677 "leal 4(%esp), %ecx\n\t"
678 "movl %ecx, 16(%eax)\n\t"
679 "movl 0(%esp), %ecx\n\t"
680 "movl %ecx, 20(%eax)\n\t");
681 #endif /* CONFIG_FRAME_POINTER */
686 kdba_longjmp(kdb_jmp_buf *jb, int reason)
688 #if defined(CONFIG_FRAME_POINTER)
689 __asm__("movl 8(%esp), %ecx\n\t"
690 "movl 12(%esp), %eax\n\t"
691 "movl 20(%ecx), %edx\n\t"
692 "movl 0(%ecx), %ebx\n\t"
693 "movl 4(%ecx), %esi\n\t"
694 "movl 8(%ecx), %edi\n\t"
695 "movl 12(%ecx), %ebp\n\t"
696 "movl 16(%ecx), %esp\n\t"
698 #else /* CONFIG_FRAME_POINTER */
699 __asm__("movl 4(%esp), %ecx\n\t"
700 "movl 8(%esp), %eax\n\t"
701 "movl 20(%ecx), %edx\n\t"
702 "movl 0(%ecx), %ebx\n\t"
703 "movl 4(%ecx), %esi\n\t"
704 "movl 8(%ecx), %edi\n\t"
705 "movl 12(%ecx), %ebp\n\t"
706 "movl 16(%ecx), %esp\n\t"
708 #endif /* CONFIG_FRAME_POINTER */
714 * Format a struct pt_regs
717 * argc argument count
718 * argv argument vector
722 * zero for success, a kdb diagnostic if error
726 * If no address is supplied, it uses the last irq pt_regs.
730 kdba_pt_regs(int argc, const char **argv)
737 static const char *fmt = " %-11.11s 0x%lx\n";
740 addr = (kdb_machreg_t) get_irq_regs();
741 } else if (argc == 1) {
743 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
750 p = (struct pt_regs *) addr;
751 kdb_printf("struct pt_regs 0x%p-0x%p\n", p, (unsigned char *)p + sizeof(*p) - 1);
752 kdb_print_nameval("ebx", p->bx);
753 kdb_print_nameval("ecx", p->cx);
754 kdb_print_nameval("edx", p->dx);
755 kdb_print_nameval("esi", p->si);
756 kdb_print_nameval("edi", p->di);
757 kdb_print_nameval("ebp", p->bp);
758 kdb_print_nameval("eax", p->ax);
759 kdb_printf(fmt, "xds", p->ds);
760 kdb_printf(fmt, "xes", p->es);
761 kdb_print_nameval("orig_eax", p->orig_ax);
762 kdb_print_nameval("eip", p->ip);
763 kdb_printf(fmt, "xcs", p->cs);
764 kdb_printf(fmt, "eflags", p->flags);
765 kdb_printf(fmt, "esp", p->sp);
766 kdb_printf(fmt, "xss", p->ss);
773 * Print processes that are using more than a specific percentage of their
777 * argc argument count
778 * argv argument vector
782 * zero for success, a kdb diagnostic if error
786 * If no percentage is supplied, it uses 60.
790 kdba_stackdepth1(struct task_struct *p, unsigned long esp)
792 struct thread_info *tinfo;
797 tinfo = (struct thread_info *)(esp & -THREAD_SIZE);
798 used = sizeof(*tinfo) + THREAD_SIZE - (esp & (THREAD_SIZE-1));
800 if (kdb_task_has_cpu(p)) {
801 struct kdb_activation_record ar;
802 memset(&ar, 0, sizeof(ar));
803 kdba_get_stack_info_alternate(esp, -1, &ar);
808 kdb_printf(" %s stack %p esp %lx used %d\n", type, tinfo, esp, used);
809 esp = tinfo->previous_esp;
814 kdba_stackdepth(int argc, const char **argv)
816 int diag, cpu, threshold, used, over;
817 unsigned long percentage;
821 struct task_struct *p, *g;
822 struct kdb_running_process *krp;
823 struct thread_info *tinfo;
827 } else if (argc == 1) {
829 diag = kdbgetaddrarg(argc, argv, &nextarg, &percentage, &offset, NULL);
835 percentage = max_t(int, percentage, 1);
836 percentage = min_t(int, percentage, 100);
837 threshold = ((2 * THREAD_SIZE * percentage) / 100 + 1) >> 1;
838 kdb_printf("stackdepth: processes using more than %ld%% (%d bytes) of stack\n",
839 percentage, threshold);
841 /* Run the active tasks first, they can have multiple stacks */
842 for (cpu = 0, krp = kdb_running_process; cpu < NR_CPUS; ++cpu, ++krp) {
843 if (!cpu_online(cpu))
849 tinfo = (struct thread_info *)(esp & -THREAD_SIZE);
850 used = sizeof(*tinfo) + THREAD_SIZE - (esp & (THREAD_SIZE-1));
851 if (used >= threshold)
853 esp = tinfo->previous_esp;
856 kdba_stackdepth1(p, krp->arch.esp);
858 /* Now the tasks that are not on cpus */
859 kdb_do_each_thread(g, p) {
860 if (kdb_task_has_cpu(p))
863 used = sizeof(*tinfo) + THREAD_SIZE - (esp & (THREAD_SIZE-1));
864 over = used >= threshold;
866 kdba_stackdepth1(p, esp);
867 } kdb_while_each_thread(g, p);
872 asmlinkage int kdb_call(void);
874 /* Executed once on each cpu at startup. */
884 set_intr_gate(KDB_VECTOR, kdb_interrupt);
886 set_intr_gate(KDBENTER_VECTOR, kdb_call);
890 arch_initcall(kdba_arch_init);
895 * Architecture specific initialization.
910 kdba_arch_init(); /* Need to register KDBENTER_VECTOR early */
911 kdb_register("pt_regs", kdba_pt_regs, "address", "Format struct pt_regs", 0);
912 kdb_register("stackdepth", kdba_stackdepth, "[percentage]", "Print processes using >= stack percentage", 0);
920 * Architecture specific adjustment of instruction pointer before leaving
924 * reason The reason KDB was invoked
925 * error The hardware-defined error code
926 * regs The exception frame at time of fault/breakpoint. If reason
927 * is SILENT or CPU_UP then regs is NULL, otherwise it should
938 kdba_adjust_ip(kdb_reason_t reason, int error, struct pt_regs *regs)
944 kdba_set_current_task(const struct task_struct *p)
946 kdb_current_task = p;
947 if (kdb_task_has_cpu(p)) {
948 struct kdb_running_process *krp = kdb_running_process + kdb_process_cpu(p);
949 kdb_current_regs = krp->regs;
952 kdb_current_regs = NULL;
956 * asm-i386 uaccess.h supplies __copy_to_user which relies on MMU to
957 * trap invalid addresses in the _xxx fields. Verify the other address
958 * of the pair is valid by accessing the first and last byte ourselves,
959 * then any access violations should only be caused by the _xxx
964 kdba_putarea_size(unsigned long to_xxx, void *from, size_t size)
966 mm_segment_t oldfs = get_fs();
969 c = *((volatile char *)from);
970 c = *((volatile char *)from + size - 1);
972 if (to_xxx < PAGE_OFFSET) {
973 return kdb_putuserarea_size(to_xxx, from, size);
977 r = __copy_to_user_inatomic((void __user *)to_xxx, from, size);
983 kdba_getarea_size(void *to, unsigned long from_xxx, size_t size)
985 mm_segment_t oldfs = get_fs();
987 *((volatile char *)to) = '\0';
988 *((volatile char *)to + size - 1) = '\0';
990 if (from_xxx < PAGE_OFFSET) {
991 return kdb_getuserarea_size(to, from_xxx, size);
997 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 1);
1000 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 2);
1003 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 4);
1006 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 8);
1009 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, size);
1017 kdba_verify_rw(unsigned long addr, size_t size)
1019 unsigned char data[size];
1020 return(kdba_getarea_size(data, addr, size) || kdba_putarea_size(addr, data, size));
1025 #include <mach_ipi.h>
1027 /* When first entering KDB, try a normal IPI. That reduces backtrace problems
1028 * on the other cpus.
1033 if (!KDB_FLAG(NOIPI))
1034 send_IPI_allbutself(KDB_VECTOR);
1037 /* The normal KDB IPI handler */
1039 smp_kdb_interrupt(struct pt_regs *regs)
1041 struct pt_regs *old_regs = set_irq_regs(regs);
1044 kdb_ipi(regs, NULL);
1046 set_irq_regs(old_regs);
1049 /* Invoked once from kdb_wait_for_cpus when waiting for cpus. For those cpus
1050 * that have not responded to the normal KDB interrupt yet, hit them with an
1054 kdba_wait_for_cpus(void)
1057 if (KDB_FLAG(CATASTROPHIC))
1059 kdb_printf(" Sending NMI to cpus that have not responded yet\n");
1060 for_each_online_cpu(c)
1061 if (kdb_running_process[c].seqno < kdb_seqno - 1)
1062 send_IPI_mask(cpumask_of_cpu(c), NMI_VECTOR);
1065 #endif /* CONFIG_SMP */