Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S262432AbUCHJsX (ORCPT ); Mon, 8 Mar 2004 04:48:23 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S262437AbUCHJsX (ORCPT ); Mon, 8 Mar 2004 04:48:23 -0500 Received: from svr44.ehostpros.com ([66.98.192.92]:38619 "EHLO svr44.ehostpros.com") by vger.kernel.org with ESMTP id S262432AbUCHJkC (ORCPT ); Mon, 8 Mar 2004 04:40:02 -0500 From: "Amit S. Kale" Organization: EmSysSoft To: Andrew Morton , Linux Kernel Subject: kgdb for mainline kernel: i386-lite [patch 2/3] Date: Mon, 8 Mar 2004 15:05:24 +0530 User-Agent: KMail/1.5 Cc: Tom Rini , George Anzinger , Pavel Machek MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_c5DTAPO9tFLS3iW" Message-Id: <200403081505.24622.amitkale@emsyssoft.com> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - svr44.ehostpros.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - emsyssoft.com Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 19056 Lines: 683 --Boundary-00=_c5DTAPO9tFLS3iW Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline -Amit --Boundary-00=_c5DTAPO9tFLS3iW Content-Type: text/x-diff; charset="us-ascii"; name="i386-lite.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="i386-lite.patch" Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/Kconfig =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/Kconfig 2004-03-08 14:30:07.074879168 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/Kconfig 2004-03-08 14:31:50.042225752 +0530 @@ -1253,6 +1253,8 @@ If you say Y here, various routines which may sleep will become very noisy if they are called with a spinlock held. +source "kernel/Kconfig.kgdb" + config FRAME_POINTER bool "Compile the kernel with frame pointers" help Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/Makefile =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/kernel/Makefile 2004-03-08 14:30:06.719933128 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/Makefile 2004-03-08 14:31:50.042225752 +0530 @@ -31,6 +31,7 @@ obj-$(CONFIG_ACPI_SRAT) += srat.o obj-$(CONFIG_HPET_TIMER) += time_hpet.o obj-$(CONFIG_EFI) += efi.o efi_stub.o +obj-$(CONFIG_KGDB) += kgdb.o EXTRA_AFLAGS := -traditional Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/irq.c =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/kernel/irq.c 2004-03-08 14:30:06.717933432 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/irq.c 2004-03-08 14:31:50.043225600 +0530 @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -508,6 +509,8 @@ irq_exit(); + kgdb_process_breakpoint(); + return 1; } Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/kgdb.c =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/kernel/kgdb.c 2003-01-30 15:54:37.000000000 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/kgdb.c 2004-03-08 14:31:50.044225448 +0530 @@ -0,0 +1,373 @@ +/* + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + */ + +/* + * Copyright (C) 2000-2001 VERITAS Software Corporation. + */ +/* + * Contributor: Lake Stevens Instrument Division$ + * Written by: Glenn Engel $ + * Updated by: Amit Kale + * Modified for 386 by Jim Kingdon, Cygnus Support. + * Origianl kgdb, compatibility with 2.1.xx kernel by David Grothe + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include /* for linux pt_regs struct */ +#include +#ifdef CONFIG_GDB_CONSOLE +#include +#endif +#include +#include + +/* Put the error code here just in case the user cares. */ +int gdb_i386errcode; +/* Likewise, the vector number here (since GDB only gets the signal + number through the usual means, and that's not very specific). */ +int gdb_i386vector = -1; + +#if KGDB_MAX_NO_CPUS != 8 +#error change the definition of slavecpulocks +#endif + +void regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs) +{ + gdb_regs[_EAX] = regs->eax; + gdb_regs[_EBX] = regs->ebx; + gdb_regs[_ECX] = regs->ecx; + gdb_regs[_EDX] = regs->edx; + gdb_regs[_ESI] = regs->esi; + gdb_regs[_EDI] = regs->edi; + gdb_regs[_EBP] = regs->ebp; + gdb_regs[_DS] = regs->xds; + gdb_regs[_ES] = regs->xes; + gdb_regs[_PS] = regs->eflags; + gdb_regs[_CS] = regs->xcs; + gdb_regs[_PC] = regs->eip; + gdb_regs[_ESP] = (int)(®s->esp); + gdb_regs[_SS] = __KERNEL_DS; + gdb_regs[_FS] = 0xFFFF; + gdb_regs[_GS] = 0xFFFF; +} + +void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p) +{ + gdb_regs[_EAX] = 0; + gdb_regs[_EBX] = 0; + gdb_regs[_ECX] = 0; + gdb_regs[_EDX] = 0; + gdb_regs[_ESI] = 0; + gdb_regs[_EDI] = 0; + gdb_regs[_EBP] = *(int *)p->thread.esp; + gdb_regs[_DS] = __KERNEL_DS; + gdb_regs[_ES] = __KERNEL_DS; + gdb_regs[_PS] = 0; + gdb_regs[_CS] = __KERNEL_CS; + gdb_regs[_PC] = p->thread.eip; + gdb_regs[_ESP] = p->thread.esp; + gdb_regs[_SS] = __KERNEL_DS; + gdb_regs[_FS] = 0xFFFF; + gdb_regs[_GS] = 0xFFFF; +} + +void gdb_regs_to_regs(unsigned long *gdb_regs, struct pt_regs *regs) +{ + regs->eax = gdb_regs[_EAX]; + regs->ebx = gdb_regs[_EBX]; + regs->ecx = gdb_regs[_ECX]; + regs->edx = gdb_regs[_EDX]; + regs->esi = gdb_regs[_ESI]; + regs->edi = gdb_regs[_EDI]; + regs->ebp = gdb_regs[_EBP]; + regs->xds = gdb_regs[_DS]; + regs->xes = gdb_regs[_ES]; + regs->eflags = gdb_regs[_PS]; + regs->xcs = gdb_regs[_CS]; + regs->eip = gdb_regs[_PC]; +#if 0 /* can't change these */ + regs->esp = gdb_regs[_ESP]; + regs->xss = gdb_regs[_SS]; + regs->fs = gdb_regs[_FS]; + regs->gs = gdb_regs[_GS]; +#endif + +} + +struct hw_breakpoint { + unsigned enabled; + unsigned type; + unsigned len; + unsigned addr; +} breakinfo[4] = { { +enabled:0}, { +enabled:0}, { +enabled:0}, { +enabled:0}}; + +void kgdb_correct_hw_break(void) +{ + int breakno; + int correctit; + int breakbit; + unsigned dr7; + + asm volatile ("movl %%db7, %0\n":"=r" (dr7) + :); + do { + unsigned addr0, addr1, addr2, addr3; + asm volatile ("movl %%db0, %0\n" + "movl %%db1, %1\n" + "movl %%db2, %2\n" + "movl %%db3, %3\n":"=r" (addr0), "=r"(addr1), + "=r"(addr2), "=r"(addr3):); + } while (0); + correctit = 0; + for (breakno = 0; breakno < 3; breakno++) { + breakbit = 2 << (breakno << 1); + if (!(dr7 & breakbit) && breakinfo[breakno].enabled) { + correctit = 1; + dr7 |= breakbit; + dr7 &= ~(0xf0000 << (breakno << 2)); + dr7 |= (((breakinfo[breakno].len << 2) | + breakinfo[breakno].type) << 16) << + (breakno << 2); + switch (breakno) { + case 0: + asm volatile ("movl %0, %%dr0\n"::"r" + (breakinfo[breakno].addr)); + break; + + case 1: + asm volatile ("movl %0, %%dr1\n"::"r" + (breakinfo[breakno].addr)); + break; + + case 2: + asm volatile ("movl %0, %%dr2\n"::"r" + (breakinfo[breakno].addr)); + break; + + case 3: + asm volatile ("movl %0, %%dr3\n"::"r" + (breakinfo[breakno].addr)); + break; + } + } else if ((dr7 & breakbit) && !breakinfo[breakno].enabled) { + correctit = 1; + dr7 &= ~breakbit; + dr7 &= ~(0xf0000 << (breakno << 2)); + } + } + if (correctit) { + asm volatile ("movl %0, %%db7\n"::"r" (dr7)); + } +} + +int kgdb_remove_hw_break(unsigned long addr, int type) +{ + int i, idx = -1; + for (i = 0; i < 4; i++) { + if (breakinfo[i].addr == addr && breakinfo[i].enabled) { + idx = i; + break; + } + } + if (idx == -1) + return -1; + + breakinfo[idx].enabled = 0; + return 0; +} + +int kgdb_set_hw_break(unsigned long addr, int type) +{ + int i, idx = -1; + for (i = 0; i < 4; i++) { + if (!breakinfo[i].enabled) { + idx = i; + break; + } + } + if (idx == -1) + return -1; + + breakinfo[idx].enabled = 1; + breakinfo[idx].type = type; + breakinfo[idx].len = 1; + breakinfo[idx].addr = addr; + return 0; +} + +int remove_hw_break(unsigned breakno) +{ + if (!breakinfo[breakno].enabled) { + return -1; + } + breakinfo[breakno].enabled = 0; + return 0; +} + +int set_hw_break(unsigned breakno, unsigned type, unsigned len, unsigned addr) +{ + if (breakinfo[breakno].enabled) { + return -1; + } + breakinfo[breakno].enabled = 1; + breakinfo[breakno].type = type; + breakinfo[breakno].len = len; + breakinfo[breakno].addr = addr; + return 0; +} + +void kgdb_printexceptioninfo(int exceptionNo, int errorcode, char *buffer) +{ + unsigned dr6; + int i; + switch (exceptionNo) { + case 1: /* debug exception */ + break; + case 3: /* breakpoint */ + sprintf(buffer, "Software breakpoint"); + return; + default: + sprintf(buffer, "Details not available"); + return; + } + asm volatile ("movl %%db6, %0\n":"=r" (dr6) + :); + if (dr6 & 0x4000) { + sprintf(buffer, "Single step"); + return; + } + for (i = 0; i < 4; ++i) { + if (dr6 & (1 << i)) { + sprintf(buffer, "Hardware breakpoint %d", i); + return; + } + } + sprintf(buffer, "Unknown trap"); + return; +} + +void kgdb_disable_hw_debug(struct pt_regs *regs) +{ + /* Disable hardware debugging while we are in kgdb */ + asm volatile ("movl %0,%%db7": /* no output */ :"r" (0)); +} + +void kgdb_post_master_code(struct pt_regs *regs, int eVector, int err_code) +{ + /* Master processor is completely in the debugger */ + gdb_i386vector = eVector; + gdb_i386errcode = err_code; +} + +int kgdb_arch_handle_exception(int exceptionVector, int signo, + int err_code, char *remcom_in_buffer, + char *remcom_out_buffer, + struct pt_regs *linux_regs) +{ + long addr, length; + long breakno, breaktype; + char *ptr; + int newPC; + int dr6; + + switch (remcom_in_buffer[0]) { + case 'c': + case 's': + if (kgdb_contthread && kgdb_contthread != current) { + strcpy(remcom_out_buffer, "E00"); + break; + } + + kgdb_contthread = NULL; + + /* try to read optional parameter, pc unchanged if no parm */ + ptr = &remcom_in_buffer[1]; + if (kgdb_hex2long(&ptr, &addr)) { + linux_regs->eip = addr; + } + newPC = linux_regs->eip; + + /* clear the trace bit */ + linux_regs->eflags &= 0xfffffeff; + + /* set the trace bit if we're stepping */ + if (remcom_in_buffer[0] == 's') { + linux_regs->eflags |= 0x100; + debugger_step = 1; + } + + asm volatile ("movl %%db6, %0\n":"=r" (dr6)); + if (!(dr6 & 0x4000)) { + for (breakno = 0; breakno < 4; ++breakno) { + if (dr6 & (1 << breakno)) { + if (breakinfo[breakno].type == 0) { + /* Set restore flag */ + linux_regs->eflags |= 0x10000; + break; + } + } + } + } + kgdb_correct_hw_break(); + asm volatile ("movl %0, %%db6\n"::"r" (0)); + + return (0); + + case 'Y': + ptr = &remcom_in_buffer[1]; + kgdb_hex2long(&ptr, &breakno); + ptr++; + kgdb_hex2long(&ptr, &breaktype); + ptr++; + kgdb_hex2long(&ptr, &length); + ptr++; + kgdb_hex2long(&ptr, &addr); + if (set_hw_break(breakno & 0x3, breaktype & 0x3, + length & 0x3, addr) == 0) { + strcpy(remcom_out_buffer, "OK"); + } else { + strcpy(remcom_out_buffer, "ERROR"); + } + break; + + /* Remove hardware breakpoint */ + case 'y': + ptr = &remcom_in_buffer[1]; + kgdb_hex2long(&ptr, &breakno); + if (remove_hw_break(breakno & 0x3) == 0) { + strcpy(remcom_out_buffer, "OK"); + } else { + strcpy(remcom_out_buffer, "ERROR"); + } + break; + + } /* switch */ + return -1; /* this means that we do not want to exit from the handler */ +} + +struct kgdb_arch arch_kgdb_ops = { + .gdb_bpt_instr = {0xcc}, + .flags = KGDB_HW_BREAKPOINT, +}; Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/nmi.c =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/kernel/nmi.c 2004-03-08 14:30:06.787922792 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/nmi.c 2004-03-08 14:31:50.044225448 +0530 @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -420,14 +421,25 @@ int sum, cpu = smp_processor_id(); sum = irq_stat[cpu].apic_timer_irqs; + if (atomic_read(&debugger_active)) { - if (last_irq_sums[cpu] == sum) { + /* + * The machine is in debugger, hold this cpu if already + * not held. + */ + debugger_nmihook(cpu, regs); + alert_counter[cpu] = 0; + + } else if (last_irq_sums[cpu] == sum) { /* * Ayiee, looks like this CPU is stuck ... * wait a few IRQs (5 seconds) before doing the oops ... */ alert_counter[cpu]++; if (alert_counter[cpu] == 5*nmi_hz) { + + CHK_DEBUGGER(2,SIGSEGV,0,regs,) + spin_lock(&nmi_print_lock); /* * We are in trouble anyway, lets at least try Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/signal.c =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/kernel/signal.c 2004-03-08 14:30:06.765926136 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/signal.c 2004-03-08 14:31:50.045225296 +0530 @@ -580,7 +580,8 @@ * have been cleared if the watchpoint triggered * inside the kernel. */ - __asm__("movl %0,%%db7" : : "r" (current->thread.debugreg[7])); + if (current->thread.debugreg[7]) + __asm__("movl %0,%%db7" : : "r" (current->thread.debugreg[7])); /* Whee! Actually deliver the signal. */ handle_signal(signr, &info, oldset, regs); Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/traps.c =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/kernel/traps.c 2004-03-08 14:30:06.719933128 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/kernel/traps.c 2004-03-08 14:31:50.046225144 +0530 @@ -51,6 +51,7 @@ #include #include +#include #include "mach_traps.h" @@ -256,6 +257,7 @@ { static int die_counter; + CHK_DEBUGGER(1,SIGTRAP,err,regs,) console_verbose(); spin_lock_irq(&die_lock); bust_spinlocks(1); @@ -330,6 +332,7 @@ #define DO_ERROR(trapnr, signr, str, name) \ asmlinkage void do_##name(struct pt_regs * regs, long error_code) \ { \ + CHK_DEBUGGER(trapnr,signr,error_code,regs,)\ do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \ } @@ -347,7 +350,10 @@ #define DO_VM86_ERROR(trapnr, signr, str, name) \ asmlinkage void do_##name(struct pt_regs * regs, long error_code) \ { \ + CHK_DEBUGGER(trapnr,signr,error_code,regs,goto skip_trap)\ do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \ +skip_trap: \ + return; \ } #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \ @@ -547,7 +553,7 @@ tsk->thread.debugreg[6] = condition; /* Mask out spurious TF errors due to lazy TF clearing */ - if (condition & DR_STEP) { + if (condition & DR_STEP && !debugger_step) { /* * The TF error should be masked out only if the current * process is not traced and if the TRAP flag has been set @@ -570,11 +576,13 @@ info.si_errno = 0; info.si_code = TRAP_BRKPT; - /* If this is a kernel mode trap, save the user PC on entry to - * the kernel, that's what the debugger can make sense of. - */ - info.si_addr = ((regs->xcs & 3) == 0) ? (void *)tsk->thread.eip : - (void *)regs->eip; + + /* If this is a kernel mode trap, we need to reset db7 to allow us + * to continue sanely */ + if ((regs->xcs & 3) == 0) + goto clear_dr7; + + info.si_addr = (void *)regs->eip; force_sig_info(SIGTRAP, &info, tsk); /* Disable additional traps. They'll be re-enabled when @@ -584,6 +592,7 @@ __asm__("movl %0,%%db7" : /* no output */ : "r" (0)); + CHK_DEBUGGER(1,SIGTRAP,error_code,regs,) return; debug_vm86: Index: linux-2.6.4-rc2-bk3-kgdb/arch/i386/mm/fault.c =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/arch/i386/mm/fault.c 2004-03-08 14:30:06.915903336 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/arch/i386/mm/fault.c 2004-03-08 14:31:50.046225144 +0530 @@ -2,6 +2,11 @@ * linux/arch/i386/mm/fault.c * * Copyright (C) 1995 Linus Torvalds + * + * Change History + * + * Tigran Aivazian Remote debugging support. + * */ #include @@ -21,6 +26,7 @@ #include /* For unblank_screen() */ #include #include +#include #include #include @@ -399,6 +405,8 @@ if (is_prefetch(regs, address)) return; + CHK_DEBUGGER(14, SIGSEGV,error_code, regs,) + /* * Oops. The kernel tried to access some bad page. We'll have to * terminate things with extreme prejudice. @@ -406,6 +414,7 @@ bust_spinlocks(1); + if (address < PAGE_SIZE) printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference"); else Index: linux-2.6.4-rc2-bk3-kgdb/include/asm-i386/kgdb.h =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/include/asm-i386/kgdb.h 2003-01-30 15:54:37.000000000 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/include/asm-i386/kgdb.h 2004-03-08 14:31:50.047224992 +0530 @@ -0,0 +1,49 @@ +#ifndef _ASM_KGDB_H_ +#define _ASM_KGDB_H_ + +/* + * Copyright (C) 2001-2004 Amit S. Kale + */ + +#include + +/* gdb locks */ +#define KGDB_MAX_NO_CPUS 8 + +/************************************************************************/ +/* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/ +/* at least NUMREGBYTES*2 are needed for register packets */ +/* Longer buffer is needed to list all threads */ +#define BUFMAX 1024 + +/* Number of bytes of registers. */ +#define NUMREGBYTES 64 +/* + * Note that this register image is in a different order than + * the register image that Linux produces at interrupt time. + * + * Linux's register image is defined by struct pt_regs in ptrace.h. + * Just why GDB uses a different order is a historical mystery. + */ +enum regnames { _EAX, /* 0 */ + _ECX, /* 1 */ + _EDX, /* 2 */ + _EBX, /* 3 */ + _ESP, /* 4 */ + _EBP, /* 5 */ + _ESI, /* 6 */ + _EDI, /* 7 */ + _PC, /* 8 also known as eip */ + _PS, /* 9 also known as eflags */ + _CS, /* 10 */ + _SS, /* 11 */ + _DS, /* 12 */ + _ES, /* 13 */ + _FS, /* 14 */ + _GS /* 15 */ +}; + +#define BREAKPOINT() asm(" int $3"); +#define BREAK_INSTR_SIZE 1 + +#endif /* _ASM_KGDB_H_ */ Index: linux-2.6.4-rc2-bk3-kgdb/include/asm-i386/processor.h =================================================================== --- linux-2.6.4-rc2-bk3-kgdb.orig/include/asm-i386/processor.h 2004-03-08 14:30:19.929924904 +0530 +++ linux-2.6.4-rc2-bk3-kgdb/include/asm-i386/processor.h 2004-03-08 14:31:50.047224992 +0530 @@ -425,6 +425,8 @@ unsigned int saved_fs, saved_gs; /* IO permissions */ unsigned long *io_bitmap_ptr; + void * debuggerinfo; + }; #define INIT_THREAD { \ --Boundary-00=_c5DTAPO9tFLS3iW-- - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/