diff -Naurp linux-2.6.0/drivers/serial/8250.c linux-2.6.0-kgdb-2.0-common/drivers/serial/8250.c --- linux-2.6.0/drivers/serial/8250.c 2003-11-24 07:01:06.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/drivers/serial/8250.c 2004-01-07 14:47:53.000000000 +0530 @@ -1205,12 +1205,17 @@ static void serial8250_break_ctl(struct spin_unlock_irqrestore(&up->port.lock, flags); } +static int released_irq = -1; + static int serial8250_startup(struct uart_port *port) { struct uart_8250_port *up = (struct uart_8250_port *)port; unsigned long flags; int retval; + if (up->port.irq == released_irq) + return -EBUSY; + up->capabilities = uart_config[up->port.type].flags; if (up->port.type == PORT_16C950) { @@ -1876,6 +1881,10 @@ static void __init serial8250_register_p for (i = 0; i < UART_NR; i++) { struct uart_8250_port *up = &serial8250_ports[i]; + if (up->port.irq == released_irq) { + continue; + } + up->port.line = i; up->port.ops = &serial8250_pops; init_timer(&up->timer); @@ -2145,6 +2154,36 @@ void serial8250_resume_port(int line) uart_resume_port(&serial8250_reg, &serial8250_ports[line].port); } +/* + * Find all the ports using the given irq and shut them down. + * Result should be that the irq will be released. + * At most one irq can be released this way. + * Once an irq is released, any attempts to initialize a port with that irq + * will fail with EBUSY. + */ + +int serial8250_release_irq(int irq) +{ + struct uart_8250_port *up; + int ttyS; + + if (released_irq != -1) { + return 1; + } + released_irq = irq; + for (ttyS = 0; ttyS < UART_NR; ttyS++){ + up = &serial8250_ports[ttyS]; + if (up->port.irq == irq && (irq_lists + irq)->head) { +#ifdef CONFIG_DEBUG_SPINLOCK /* ugly business... */ + if(up->port.lock.magic != SPINLOCK_MAGIC) + spin_lock_init(&up->port.lock); +#endif + serial8250_shutdown(&up->port); + } + } + return 0; +} + static int __init serial8250_init(void) { int ret, i; diff -Naurp linux-2.6.0/drivers/serial/kgdb_8250.c linux-2.6.0-kgdb-2.0-common/drivers/serial/kgdb_8250.c --- linux-2.6.0/drivers/serial/kgdb_8250.c 1970-01-01 05:30:00.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/drivers/serial/kgdb_8250.c 2004-01-07 14:47:53.000000000 +0530 @@ -0,0 +1,383 @@ +/* + * 8250 interface for kgdb. + * + * Restructured for making kgdb capable of handling different types of serial + * interfaces, by Amit Kale (amitkale@emsyssoft.com) + * + * Written (hacked together) by David Grothe (dave@gcom.com) + * + * Modified by Scott Foehner (sfoehner@engr.sgi.com) to allow connect + * on boot-up + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define GDB_BUF_SIZE 512 /* power of 2, please */ + +static char kgdb8250_buf[GDB_BUF_SIZE] ; +static int kgdb8250_buf_in_inx ; +static atomic_t kgdb8250_buf_in_cnt ; +static int kgdb8250_buf_out_inx ; + + +static int kgdb8250_got_dollar = -3, kgdb8250_got_H = -3, kgdb8250_interrupt_iteration = 0; + +extern void set_debug_traps(void) ; /* GDB routine */ +int serial8250_release_irq(int irq); + +int kgdb8250_irq; +int kgdb8250_port; + +static int initialized = -1; + +int kgdb8250_baud = 115200; +int kgdb8250_ttyS; + +/* + * Get a byte from the hardware data buffer and return it + */ +static int read_data_bfr(void) +{ + if (inb(kgdb8250_port + UART_LSR) & UART_LSR_DR) + return(inb(kgdb8250_port + UART_RX)); + + return( -1 ) ; + +} + +/* + * Get a char if available, return -1 if nothing available. + * Empty the receive buffer first, then look at the interface hardware. + * It waits for a character from the serial interface and then returns it. + */ +static int kgdb8250_read_char(void) +{ + int retchar; + if (atomic_read(&kgdb8250_buf_in_cnt) != 0) /* intr routine has q'd chars */ + { + int chr ; + + chr = kgdb8250_buf[kgdb8250_buf_out_inx++] ; + kgdb8250_buf_out_inx &= (GDB_BUF_SIZE - 1) ; + atomic_dec(&kgdb8250_buf_in_cnt) ; + return(chr) ; + } + do { + retchar = (read_data_bfr()) ; /* read from hardware */ + } while (retchar < 0); + return retchar; +} + +/* + * Wait until the interface can accept a char, then write it. + */ +static void kgdb8250_write_char(int chr) +{ + while ( !(inb(kgdb8250_port + UART_LSR) & UART_LSR_THRE) ) ; + + outb(chr, kgdb8250_port+UART_TX); + +} /* write_char */ + +/* + * This is the receiver interrupt routine for the GDB stub. + * It will receive a limited number of characters of input + * from the gdb host machine and save them up in a buffer. + * + * When kgdb8250_read_char() is called it + * draws characters out of the buffer until it is empty and + * then reads directly from the serial port. + * + * We do not attempt to write chars from the interrupt routine + * since the stubs do all of that via kgdb8250_write_char() which + * writes one byte after waiting for the interface to become + * ready. + * + * The debug stubs like to run with interrupts disabled since, + * after all, they run as a consequence of a breakpoint in + * the kernel. + * + * Perhaps someone who knows more about the tty driver than I + * care to learn can make this work for any low level serial + * driver. + */ +static irqreturn_t kgdb8250_interrupt(int irq, void *dev_id, struct pt_regs * regs) +{ + int chr; + int iir; + + do + { + chr = read_data_bfr(); + iir = inb(kgdb8250_port + UART_IIR); + if (chr < 0) continue ; + + /* Check whether gdb sent interrupt */ + if (chr == 3) + { + breakpoint(); + continue ; + } + + if(atomic_read(&kgdb_killed_or_detached)) { + if (chr == '$') + kgdb8250_got_dollar = kgdb8250_interrupt_iteration; + if (kgdb8250_interrupt_iteration == kgdb8250_got_dollar + 1 && + chr == 'H') + kgdb8250_got_H = kgdb8250_interrupt_iteration; + else if (kgdb8250_interrupt_iteration == kgdb8250_got_H + 1 && + chr == 'c') { + kgdb8250_buf[kgdb8250_buf_in_inx++] = chr; + atomic_inc(&kgdb8250_buf_in_cnt); + atomic_set(&kgdb_might_be_resumed, 1); + wmb(); + breakpoint(); + atomic_set(&kgdb_might_be_resumed, 0); + kgdb8250_interrupt_iteration = 0; + kgdb8250_got_dollar = -3; + kgdb8250_got_H = -3; + continue; + } + } + + if (atomic_read(&kgdb8250_buf_in_cnt) >= GDB_BUF_SIZE) + { + /* buffer overflow, clear it */ + kgdb8250_buf_in_inx = 0 ; + atomic_set(&kgdb8250_buf_in_cnt, 0) ; + kgdb8250_buf_out_inx = 0 ; + break ; + } + + kgdb8250_buf[kgdb8250_buf_in_inx++] = chr ; + kgdb8250_buf_in_inx &= (GDB_BUF_SIZE - 1) ; + atomic_inc(&kgdb8250_buf_in_cnt) ; + } + while (iir & UART_IIR_RDI); + + if (atomic_read(&kgdb_killed_or_detached)) + kgdb8250_interrupt_iteration ++; + + return IRQ_HANDLED; + +} + +/* + * Takes: + * ttyS - integer specifying which serial port to use for debugging + * baud - baud rate of specified serial port + * Returns: + * port for use by the gdb serial driver + */ +static int kgdb8250_init(int ttyS, int baud) +{ + unsigned cval; + int bits = 8; + int parity = 'n'; + int cflag = CREAD | HUPCL | CLOCAL; + int quot = 0; + + /* + * Now construct a cflag setting. + */ + switch(baud) { + case 1200: + cflag |= B1200; + break; + case 2400: + cflag |= B2400; + break; + case 4800: + cflag |= B4800; + break; + case 19200: + cflag |= B19200; + break; + case 38400: + cflag |= B38400; + break; + case 57600: + cflag |= B57600; + break; + case 115200: + cflag |= B115200; + break; + default: + baud = 9600; + /* Fall through */ + case 9600: + cflag |= B9600; + break; + } + switch(bits) { + case 7: + cflag |= CS7; + break; + default: + case 8: + cflag |= CS8; + break; + } + switch(parity) { + case 'o': case 'O': + cflag |= PARODD; + break; + case 'e': case 'E': + cflag |= PARENB; + break; + } + + /* + * Divisor, bytesize and parity + * + */ + + quot = (1843200/16) / baud; + cval = cflag & (CSIZE | CSTOPB); + cval >>= 4; + if (cflag & PARENB) + cval |= UART_LCR_PARITY; + if (!(cflag & PARODD)) + cval |= UART_LCR_EPAR; + + + /* + * Disable UART interrupts, set DTR and RTS high + * and set speed. + */ + cval = 0x3; + outb(cval | UART_LCR_DLAB, kgdb8250_port + UART_LCR); /* set DLAB */ + outb(quot & 0xff, kgdb8250_port + UART_DLL); /* LS of divisor */ + outb(quot >> 8, kgdb8250_port + UART_DLM); /* MS of divisor */ + outb(cval, kgdb8250_port + UART_LCR); /* reset DLAB */ + outb(UART_IER_RDI, kgdb8250_port + UART_IER); /* turn on interrupts*/ + outb(UART_MCR_OUT2 | UART_MCR_DTR | UART_MCR_RTS, kgdb8250_port + UART_MCR); + + /* + * If we read 0xff from the LSR, there is no UART here. + */ + if (inb(kgdb8250_port + UART_LSR) == 0xff) + return -1; + return 0; +} + +int kgdb8250_hook(void) +{ + int retval; + + /* + * Set port and irq number. + */ + switch(kgdb8250_ttyS) + { + case 0: + default: + kgdb8250_port = 0x3f8; + kgdb8250_irq = 4; + break; + case 1: + kgdb8250_port = 0x2f8; + kgdb8250_irq = 3; + break; + case 2: + kgdb8250_port = 0x3e8; + kgdb8250_irq = 4; + break; + case 3: + kgdb8250_port = 0x2e8; + kgdb8250_irq = 3; + break; + } + +#ifdef CONFIG_SERIAL_8250 + if (serial8250_release_irq(kgdb8250_irq)) + return -1; +#endif + + if (kgdb8250_init(kgdb8250_ttyS, kgdb8250_baud) == -1) + return -1; + + retval = request_irq(kgdb8250_irq, kgdb8250_interrupt, SA_INTERRUPT, + "GDB-stub", NULL); + if (retval == 0) + initialized = 1; + else + { + initialized = 0; + } + + /* + * Call GDB routine to setup the exception vectors for the debugger + */ + set_debug_traps() ; + + /* + * Call the breakpoint() routine in GDB to start the debugging + * session. + */ + printk("Waiting for connection from remote gdb... "); + breakpoint() ; + printk("Connected.\n"); + + return 0; + +} + +struct kgdb_serial kgdb8250_serial = { + .read_char = kgdb8250_read_char, + .write_char = kgdb8250_write_char, + .hook = kgdb8250_hook +}; + +/* + * Syntax for this cmdline option is + * kgdb8250=ttyno,baudrate + */ + +static int __init kgdb8250_opt(char *str) +{ + if (*str < '0' || *str > '3') + goto errout; + kgdb8250_ttyS = *str - '0'; + str++; + if (*str != ',') + goto errout; + str++; + kgdb8250_baud = simple_strtoul(str, NULL, 10); + if (kgdb8250_baud != 9600 && kgdb8250_baud != 19200 && + kgdb8250_baud != 38400 && kgdb8250_baud != 57600 && + kgdb8250_baud != 115200) + goto errout; + return 1; + +errout: + printk("Invalid syntax for option kgdb8250=\n"); + return 0; +} + +__setup("kgdb8250=", kgdb8250_opt); diff -Naurp linux-2.6.0/drivers/serial/Makefile linux-2.6.0-kgdb-2.0-common/drivers/serial/Makefile --- linux-2.6.0/drivers/serial/Makefile 2003-11-24 07:01:55.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/drivers/serial/Makefile 2004-01-07 14:47:53.000000000 +0530 @@ -32,3 +32,5 @@ obj-$(CONFIG_SERIAL_COLDFIRE) += mcfseri obj-$(CONFIG_V850E_UART) += v850e_uart.o obj-$(CONFIG_SERIAL98) += serial98.o obj-$(CONFIG_SERIAL_PMACZILOG) += pmac_zilog.o + +obj-$(CONFIG_KGDB_8250) += kgdb_8250.o diff -Naurp linux-2.6.0/drivers/serial/serial_core.c linux-2.6.0-kgdb-2.0-common/drivers/serial/serial_core.c --- linux-2.6.0/drivers/serial/serial_core.c 2003-11-24 07:01:25.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/drivers/serial/serial_core.c 2004-01-07 14:47:53.000000000 +0530 @@ -1209,7 +1209,11 @@ static void uart_set_termios(struct tty_ static void uart_close(struct tty_struct *tty, struct file *filp) { struct uart_state *state = tty->driver_data; - struct uart_port *port = state->port; + struct uart_port *port; + + if (!state) + return; + port = state->port; BUG_ON(!kernel_locked()); DPRINTK("uart_close(%d) called\n", port->line); diff -Naurp linux-2.6.0/include/linux/bitops.h linux-2.6.0-kgdb-2.0-common/include/linux/bitops.h --- linux-2.6.0/include/linux/bitops.h 2003-11-24 07:03:19.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/include/linux/bitops.h 2004-01-07 14:47:53.000000000 +0530 @@ -108,7 +108,7 @@ static inline unsigned int generic_hweig return (res & 0x0F) + ((res >> 4) & 0x0F); } -static inline unsigned long generic_hweight64(__u64 w) +static inline unsigned int generic_hweight64(__u64 w) { #if BITS_PER_LONG < 64 return generic_hweight32((unsigned int)(w >> 32)) + @@ -120,7 +120,8 @@ static inline unsigned long generic_hwei res = (res & 0x0F0F0F0F0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F0F0F0F0F); res = (res & 0x00FF00FF00FF00FF) + ((res >> 8) & 0x00FF00FF00FF00FF); res = (res & 0x0000FFFF0000FFFF) + ((res >> 16) & 0x0000FFFF0000FFFF); - return (res & 0x00000000FFFFFFFF) + ((res >> 32) & 0x00000000FFFFFFFF); + res = (res & 0x00000000FFFFFFFF) + ((res >> 32) & 0x00000000FFFFFFFF); + return (unsigned int)res; #endif } diff -Naurp linux-2.6.0/include/linux/debugger.h linux-2.6.0-kgdb-2.0-common/include/linux/debugger.h --- linux-2.6.0/include/linux/debugger.h 1970-01-01 05:30:00.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/include/linux/debugger.h 2004-01-07 14:47:53.000000000 +0530 @@ -0,0 +1,65 @@ +#ifndef _DEBUGGER_H_ +#define _DEBUGGER_H_ + +/* + * Copyright (C) 2003-2004 Amit S. Kale + * + * Definition switchout for debuggers + */ + +/* + * KGDB + */ +#ifdef CONFIG_KGDB + +typedef int gdb_debug_hook(int exVector, int signo, int err_code, + struct pt_regs *regs); +extern gdb_debug_hook *linux_debug_hook; +#define CHK_DEBUGGER(trapnr,signr,error_code,regs,after) \ + { \ + if (linux_debug_hook != (gdb_debug_hook *) NULL && !user_mode(regs)) \ + { \ + (*linux_debug_hook)(trapnr, signr, error_code, regs) ; \ + after; \ + } \ + } + +void kgdb_nmihook(int cpu, void *regs); +static inline void debugger_nmihook(int cpu, void *regs) +{ + kgdb_nmihook(cpu, regs); +} + +void kgdb_entry(void); +static inline void debugger_entry(void) +{ + kgdb_entry(); +} + +extern int debugger_step; +extern atomic_t debugger_active; +extern volatile int debugger_memerr_expected; + +/* + * No debugger in the kernel + */ +#else + +#define CHK_DEBUGGER(trapnr,signr,error_code,regs,after) + +static inline void debugger_nmihook(void) +{ +} + +static inline void debugger_entry(void) +{ +} + +#define debugger_step 0 +#define debugger_active 0 +#define debugger_memerr_expected 0 + +#endif + + +#endif /* _DEBUGGER_H_ */ diff -Naurp linux-2.6.0/include/linux/kgdb-asserts.h linux-2.6.0-kgdb-2.0-common/include/linux/kgdb-asserts.h --- linux-2.6.0/include/linux/kgdb-asserts.h 1970-01-01 05:30:00.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/include/linux/kgdb-asserts.h 2004-01-07 14:47:53.000000000 +0530 @@ -0,0 +1,31 @@ +#ifndef _KGDB_ASSERTS_H_ +#define _KGDB_ASSERTS_H_ + +/* + * Copyright (C) 2001 Amit S. Kale + */ + +#define KGDB_ASSERT(message, condition) do { \ + if (!(condition)) { \ + printk("kgdb assertion failed: %s\n", message); \ + breakpoint(); \ + } \ +} while (0) + +#define KA_VALID_ERRNO(errno) ((errno) > 0 && (errno) <= EMEDIUMTYPE) + +#define KA_VALID_PTR_ERR(ptr) KA_VALID_ERRNO(-PTR_ERR(ptr)) + +#define KA_VALID_KPTR(ptr) (!(ptr) || \ + ((void *)(ptr) >= (void *)PAGE_OFFSET && \ + (void *)(ptr) < ERR_PTR(-EMEDIUMTYPE))) + +#define KA_VALID_PTRORERR(errptr) (KA_VALID_KPTR(errptr) || KA_VALID_PTR_ERR(errptr)) + +#ifndef CONFIG_SMP +#define KA_HELD_GKL() 1 +#else +#define KA_HELD_GKL() (current->lock_depth >= 0) +#endif + +#endif /* _KGDB_ASSERTS_H_ */ diff -Naurp linux-2.6.0/include/linux/kgdb.h linux-2.6.0-kgdb-2.0-common/include/linux/kgdb.h --- linux-2.6.0/include/linux/kgdb.h 1970-01-01 05:30:00.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/include/linux/kgdb.h 2004-01-07 14:47:53.000000000 +0530 @@ -0,0 +1,103 @@ +#ifndef _KGDB_H_ +#define _KGDB_H_ + +/* + * Copyright (C) 2001-2004 Amit S. Kale + */ + +#include +#include +#include +#include +#include + +/* To enter the debugger explicitly. */ +void breakpoint(void); + +#ifndef KGDB_MAX_NO_CPUS +#if CONFIG_NR_CPUS > 8 +#error KGDB can handle max 8 CPUs +#endif +#define KGDB_MAX_NO_CPUS 8 +#endif + +extern atomic_t kgdb_setting_breakpoint; +extern atomic_t kgdb_killed_or_detached; +extern atomic_t kgdb_might_be_resumed; + +extern struct task_struct *kgdb_usethread, *kgdb_contthread; + +enum gdb_bptype +{ + bp_breakpoint = '0', + bp_hardware_breakpoint, + bp_write_watchpoint, + bp_read_watchpoint, + bp_access_watchpoint +}; + +enum gdb_bpstate +{ + bp_disabled, + bp_enabled +}; + +#ifndef BREAK_INSTR_SIZE +#error BREAK_INSTR_SIZE needed by kgdb +#endif + +struct gdb_breakpoint +{ + unsigned long bpt_addr; + unsigned char saved_instr[BREAK_INSTR_SIZE]; + enum gdb_bptype type; + enum gdb_bpstate state; +}; + +typedef struct gdb_breakpoint gdb_breakpoint_t; + +#ifndef MAX_BREAKPOINTS +#define MAX_BREAKPOINTS 16 +#endif + +#define KGDB_HW_BREAKPOINT 1 + +struct kgdb_arch { + unsigned char gdb_bpt_instr[BREAK_INSTR_SIZE]; + unsigned long flags; + unsigned shadowth; + + int (*kgdb_init) (void); + void (*regs_to_gdb_regs)(unsigned long *gdb_regs, struct pt_regs *regs); + void (*sleeping_thread_to_gdb_regs)(unsigned long *gdb_regs,struct task_struct *p); + void (*gdb_regs_to_regs)(unsigned long *gdb_regs, struct pt_regs *regs); + void (*printexceptioninfo)(int exceptionNo, int errorcode, char *buffer); + void (*disable_hw_debug) (struct pt_regs *regs); + void (*post_master_code) (struct pt_regs *regs, int eVector, int err_code); + int (*handle_exception) (int vector, int signo, int err_code, + char *InBuffer, char *outBuffer, + struct pt_regs *regs); + int (*set_break) (unsigned long addr, int type); + int (*remove_break) (unsigned long addr, int type); + void (*correct_hw_break) (void); + void (*handler_exit) (void); + void (*shadowinfo)(struct pt_regs *regs, char *buffer, unsigned threadid); + struct task_struct *(*get_shadow_thread)(struct pt_regs *regs, int threadid); + struct pt_regs *(*shadow_regs)(struct pt_regs *regs, int threadid); +}; + +/* Thread reference */ +typedef unsigned char threadref[8]; + +struct kgdb_serial { + int (*read_char)(void); + void (*write_char)(int); + int (*hook)(void); +}; + +extern struct kgdb_arch arch_kgdb_ops; + +int kgdb_hexToLong(char **ptr, long *longValue); +char *kgdb_mem2hex(char *mem, char *buf, int count, int can_fault); + +#endif /* _KGDB_H_ */ diff -Naurp linux-2.6.0/include/linux/sched.h linux-2.6.0-kgdb-2.0-common/include/linux/sched.h --- linux-2.6.0/include/linux/sched.h 2003-11-24 07:01:10.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/include/linux/sched.h 2004-01-07 14:47:53.000000000 +0530 @@ -173,7 +173,9 @@ extern unsigned long cache_decay_ticks; #define MAX_SCHEDULE_TIMEOUT LONG_MAX extern signed long FASTCALL(schedule_timeout(signed long timeout)); -asmlinkage void schedule(void); +asmlinkage void do_schedule(void); +asmlinkage void kern_schedule(void); +asmlinkage void kern_do_schedule(struct pt_regs); struct namespace; @@ -906,6 +908,15 @@ static inline void set_task_cpu(struct t #endif /* CONFIG_SMP */ +static inline void schedule(void) +{ +#ifdef CONFIG_KGDB_THREAD + kern_schedule(); +#else + do_schedule(); +#endif +} + #endif /* __KERNEL__ */ #endif diff -Naurp linux-2.6.0/init/main.c linux-2.6.0-kgdb-2.0-common/init/main.c --- linux-2.6.0/init/main.c 2003-11-24 07:01:14.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/init/main.c 2004-01-07 14:47:53.000000000 +0530 @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -576,6 +577,7 @@ static int init(void * unused) do_pre_smp_initcalls(); smp_init(); + debugger_entry(); do_basic_setup(); prepare_namespace(); diff -Naurp linux-2.6.0/kernel/kgdbstub.c linux-2.6.0-kgdb-2.0-common/kernel/kgdbstub.c --- linux-2.6.0/kernel/kgdbstub.c 1970-01-01 05:30:00.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/kernel/kgdbstub.c 2004-01-07 14:47:53.000000000 +0530 @@ -0,0 +1,1178 @@ +/* + * 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. + * Copyright (C) 2002-2004 Timesys Corporation + * Copyright (C) 2003-2004 Amit S. Kale + * + * Restructured KGDB for 2.6 kernels. + * thread support, support for multiple processors,support for ia-32(x86) + * hardware debugging, Console support, handling nmi watchdog + * - Amit S. Kale ( amitkale@emsyssoft.com ) + * + * Several enhancements by George Anzinger + * Generic KGDB Support + * Implemented by Anurekh Saxena (anurekh.saxena@timesys.com) + * + * Contributor: Lake Stevens Instrument Division + * Written by: Glenn Engel + * + * Modified for 386 by Jim Kingdon, Cygnus Support. + * Origianl kgdb, compatibility with 2.1.xx kernel by David Grothe + * Integrated into 2.2.5 kernel by Tigran Aivazian + * +* + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include /* for linux pt_regs struct */ +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_KGDB_CONSOLE +#include +#endif + +#include + + + +/* DEBUGGING THE DEBUGGER */ +#undef KGDB_DEG +/**************************/ + +struct kgdb_arch *kgdb_ops = &arch_kgdb_ops; +gdb_breakpoint_t kgdb_break[MAX_BREAKPOINTS]; +extern int pid_max; + +extern struct kgdb_serial kgdb8250_serial; +static struct kgdb_serial *kgdb_serial = &kgdb8250_serial; + +int kgdb_initialized = 0; +int kgdb_enter = 0; +static const char hexchars[] = "0123456789abcdef"; + +int get_char(char *addr, unsigned char *data, int can_fault); +int set_char(char *addr, int data, int can_fault); + +spinlock_t slavecpulocks[KGDB_MAX_NO_CPUS]; +volatile int procindebug[KGDB_MAX_NO_CPUS]; +atomic_t kgdb_setting_breakpoint; +atomic_t kgdb_killed_or_detached; +atomic_t kgdb_might_be_resumed; +volatile int kgdb_connected; +struct task_struct *kgdb_usethread, *kgdb_contthread; + + +int debugger_step; +atomic_t debugger_active; +volatile int debugger_memerr_expected; + +/* + * Indicate to caller of kgdb_mem2hex or hex2mem that there has been an + * error. + */ +volatile int kgdb_memerr = 0; +volatile int debugger_memerr_expected = 0; + +/* This will point to kgdb_handle_exception by default. + * The architecture code can override this in its init function + */ +gdb_debug_hook *linux_debug_hook; + + +static char remcomInBuffer[BUFMAX]; +static char remcomOutBuffer[BUFMAX]; +static short error; + +int module_event(struct notifier_block * self, unsigned long val, void * data); + +static struct notifier_block kgdb_module_load_nb = { + .notifier_call = module_event, +}; + +int hex(char ch) +{ + if ((ch >= 'a') && (ch <= 'f')) + return (ch - 'a' + 10); + if ((ch >= '0') && (ch <= '9')) + return (ch - '0'); + if ((ch >= 'A') && (ch <= 'F')) + return (ch - 'A' + 10); + return (-1); +} + + +/* scan for the sequence $# */ + +void getpacket(char *buffer) +{ + unsigned char checksum; + unsigned char xmitcsum; + int i; + int count; + char ch; + + do { + /* wait around for the start character, ignore all other characters */ + while ((ch = (kgdb_serial->read_char() & 0x7f)) != '$'); + checksum = 0; + xmitcsum = -1; + + count = 0; + + /* now, read until a # or end of buffer is found */ + while (count < BUFMAX) { + ch = kgdb_serial->read_char() & 0x7f; + if (ch == '#') + break; + checksum = checksum + ch; + buffer[count] = ch; + count = count + 1; + } + buffer[count] = 0; + + if (ch == '#') { + xmitcsum = hex(kgdb_serial->read_char() & 0x7f) << 4; + xmitcsum += hex(kgdb_serial->read_char() & 0x7f); + + if (checksum != xmitcsum) + kgdb_serial->write_char('-'); /* failed checksum */ + else { + kgdb_serial->write_char('+'); /* successful transfer */ + /* if a sequence char is present, reply the sequence ID */ + if (buffer[2] == ':') { + kgdb_serial->write_char(buffer[0]); + kgdb_serial->write_char(buffer[1]); + /* remove sequence chars from buffer */ + count = strlen(buffer); + for (i = 3; i <= count; i++) + buffer[i - 3] = buffer[i]; + } + } + } + } while (checksum != xmitcsum); + +} + + +/* + * Send the packet in buffer. + * Check for gdb connection if asked for. + */ +void putpacket(char *buffer, int checkconnect) +{ + unsigned char checksum; + int count; + char ch; + static char gdbseq[] = "$Hc-1#09"; + int i; + + /* $#. */ + do { + kgdb_serial->write_char('$'); + checksum = 0; + count = 0; + + while ((ch = buffer[count])) { + kgdb_serial->write_char(ch); + checksum += ch; + count += 1; + } + + kgdb_serial->write_char('#'); + kgdb_serial->write_char(hexchars[checksum >> 4]); + kgdb_serial->write_char(hexchars[checksum % 16]); + i = 0; + while ((ch = kgdb_serial->read_char()) == gdbseq[i++]) { + if (ch == 3) { + kgdb_serial->write_char('+'); + breakpoint(); + } + if (!gdbseq[i]) { + kgdb_serial->write_char('+'); + breakpoint(); + i = 0; + } + } + + } while (( ch & 0x7f) != '+'); + +} + +/* convert the memory pointed to by mem into hex, placing result in buf */ +/* return a pointer to the last char put in buf (null) */ +/* If MAY_FAULT is non-zero, then we should set kgdb_memerr in response to + a fault; if zero treat a fault like any other fault in the stub. */ + +char *kgdb_mem2hex(char *mem, char *buf, int count, int can_fault) +{ + int i; + unsigned char ch; + + for (i = 0; i < count; i++) { + + if (get_char(mem++, &ch, can_fault) < 0) + break; + + *buf++ = hexchars[ch >> 4]; + *buf++ = hexchars[ch % 16]; + } + *buf = 0; + return (buf); +} + +/* convert the hex array pointed to by buf into binary to be placed in mem */ +/* return a pointer to the character AFTER the last byte written */ + +char *hex2mem(char *buf, char *mem, int count, int can_fault) +{ + int i; + unsigned char ch; + + for (i = 0; i < count; i++) { + ch = hex(*buf++) << 4; + ch = ch + hex(*buf++); + if (set_char(mem++, ch, can_fault) < 0) + break; + } + return (mem); +} + +/* + * WHILE WE FIND NICE HEX CHARS, BUILD A LONG + * RETURN NUMBER OF CHARS PROCESSED + */ +int kgdb_hexToLong(char **ptr, long *longValue) +{ + int numChars = 0; + int hexValue; + + *longValue = 0; + + while (**ptr) { + hexValue = hex(**ptr); + if (hexValue >= 0) { + *longValue = (*longValue << 4) | hexValue; + numChars++; + } else + break; + + (*ptr)++; + } + + return (numChars); +} + +static inline char *pack_hex_byte(char *pkt, int byte) +{ + *pkt++ = hexchars[(byte >> 4) & 0xf]; + *pkt++ = hexchars[(byte & 0xf)]; + return pkt; +} + +#define BUF_THREAD_ID_SIZE 16 + +static char *pack_threadid(char *pkt, threadref *id) +{ + char *limit; + unsigned char *altid; + + altid = (unsigned char *) id; + limit = pkt + BUF_THREAD_ID_SIZE; + while (pkt < limit) + pkt = pack_hex_byte(pkt, *altid++); + + return pkt; +} + +void int_to_threadref(threadref *id, int value) +{ + unsigned char *scan; + + scan = (unsigned char *) id; + { + int i = 4; + while (i--) + *scan++ = 0; + } + *scan++ = (value >> 24) & 0xff; + *scan++ = (value >> 16) & 0xff; + *scan++ = (value >> 8) & 0xff; + *scan++ = (value & 0xff); +} + +static struct task_struct *getthread(struct pt_regs *regs,int tid) +{ + int i; + struct list_head *l; + struct task_struct *p; + struct pid *pid; + + if (tid >= pid_max + num_online_cpus() + kgdb_ops->shadowth) { + return NULL; + } + if (tid >= pid_max + num_online_cpus()) { + return kgdb_ops->get_shadow_thread(regs, tid - pid_max - + num_online_cpus()); + } + if (tid >= pid_max) { + i = 0; + for_each_task_pid(0, PIDTYPE_PID, p, l, pid) { + if (tid == pid_max + i) { + return p; + } + i++; + } + return NULL; + } + if (!tid) { + return NULL; + } + return find_task_by_pid(tid); +} + +#ifdef CONFIG_SMP +void kgdb_wait(struct pt_regs *regs) +{ + unsigned long flags; + int processor; + + local_irq_save(flags); + processor = smp_processor_id(); + procindebug[processor] = 1; + current->thread.debuggerinfo = regs; + + /* Wait till master processor goes completely into the debugger */ + while (!procindebug[atomic_read(&debugger_active) - 1]) { + int i = 10; /* an arbitrary number */ + + while (--i) + asm volatile ("nop": : : "memory"); + barrier(); + } + + /* Wait till master processor is done with debugging */ + spin_lock(slavecpulocks + processor); + + /* This has been taken from x86 kgdb implementation and + * will be needed by architectures that have SMP support + */ + if (kgdb_ops->correct_hw_break) + kgdb_ops->correct_hw_break(); + + /* Signal the master processor that we are done */ + procindebug[processor] = 0; + spin_unlock(slavecpulocks + processor); + local_irq_restore(flags); +} + +#endif + +static void get_mem (char *addr, unsigned char *buf, int count) +{ + while (count) { + if(get_char(addr++, buf, 1) < 0) + return; + buf++; + count--; + } +} + +static void set_mem (char *addr,unsigned char *buf, int count) +{ + while (count) { + if (set_char(addr++,*buf++, 1) < 0) + return; + count--; + } +} + +static int set_break (unsigned long addr) +{ + int i, breakno = -1; + + for (i = 0; i < MAX_BREAKPOINTS; i++) { + if ((kgdb_break[i].state == bp_enabled) && + (kgdb_break[i].bpt_addr == addr)) { + breakno = -1; + break; + } + + if (kgdb_break[i].state == bp_disabled) { + if ((breakno == -1) || (kgdb_break[i].bpt_addr == addr)) + breakno = i; + } + } + if (breakno == -1) + return -1; + + get_mem((char *)addr, kgdb_break[breakno].saved_instr, BREAK_INSTR_SIZE); + if (kgdb_memerr) + return -1; + + set_mem((char *)addr, kgdb_ops->gdb_bpt_instr, BREAK_INSTR_SIZE); + flush_cache_range (current->mm, addr, addr + BREAK_INSTR_SIZE); + flush_icache_range (addr, addr + BREAK_INSTR_SIZE); + if (kgdb_memerr) + return -1; + + kgdb_break[breakno].state = bp_enabled; + kgdb_break[breakno].type = bp_breakpoint; + kgdb_break[breakno].bpt_addr = addr; + + return 0; +} + +static int remove_break (unsigned long addr) +{ + int i; + + for (i=0; i < MAX_BREAKPOINTS; i++) { + if ((kgdb_break[i].state == bp_enabled) && + (kgdb_break[i].bpt_addr == addr)) { + set_mem((char *)addr, kgdb_break[i].saved_instr, + BREAK_INSTR_SIZE); + flush_cache_range (current->mm, addr, addr + BREAK_INSTR_SIZE); + flush_icache_range (addr, addr + BREAK_INSTR_SIZE); + if (kgdb_memerr) + return -1; + kgdb_break[i].state = bp_disabled; + return 0; + } + } + return -1; +} + +int remove_all_break(void) +{ + int i; + for (i=0; i < MAX_BREAKPOINTS; i++) { + if(kgdb_break[i].state == bp_enabled) { + unsigned long addr = kgdb_break[i].bpt_addr; + set_mem((char *)addr, kgdb_break[i].saved_instr, + BREAK_INSTR_SIZE); + flush_cache_range (current->mm, addr, addr + BREAK_INSTR_SIZE); + flush_icache_range (addr, addr + BREAK_INSTR_SIZE); + } + kgdb_break[i].state = bp_disabled; + } + return 0; +} + +int get_char(char *addr, unsigned char *data, int can_fault) +{ + mm_segment_t fs; + int ret = 0; + + kgdb_memerr = 0; + + if (can_fault) + debugger_memerr_expected = 1; + wmb(); + fs = get_fs(); + set_fs(KERNEL_DS); + + if (get_user(*data, addr) != 0) { + ret = -EFAULT; + kgdb_memerr = 1; + } + + debugger_memerr_expected = 0; + set_fs(fs); + return ret; +} + +int set_char(char *addr, int data, int can_fault) +{ + mm_segment_t fs; + int ret = 0; + + kgdb_memerr = 0; + + if (can_fault) + debugger_memerr_expected = 1; + wmb(); + fs = get_fs(); + set_fs(KERNEL_DS); + + if (put_user(data, addr) != 0) { + ret = -EFAULT; + kgdb_memerr = 1; + } + + debugger_memerr_expected = 0; + set_fs(fs); + return ret; +} + +int shadow_pid(int realpid) +{ + if (realpid) { + return realpid; + } + return pid_max + smp_processor_id(); +} + +/* + * This function does all command procesing for interfacing to gdb. + */ + +int kgdb_handle_exception(int exVector, int signo, int err_code, + struct pt_regs *linux_regs) +{ + unsigned long length, addr; + char *ptr; + unsigned long flags; + unsigned long gdb_regs[NUMREGBYTES / sizeof (unsigned long)]; + int i; + long threadid; +#ifdef CONFIG_KGDB_THREAD + threadref thref; + struct task_struct *thread = NULL; +#endif + unsigned procid; + int ret = 0; + static char tmpstr[256]; + int numshadowth = num_online_cpus() + kgdb_ops->shadowth; + int kgdb_usethreadid = 0; + + /* + * Interrupts will be restored by the 'trap return' code, except when + * single stepping. + */ + local_irq_save(flags); + + /* Hold debugger_active */ + procid = smp_processor_id(); + while (cmpxchg(&atomic_read(&debugger_active), 0, (procid + 1)) != 0) { + int i = 25; /* an arbitrary number */ + + while (--i) + asm volatile ("nop": : : "memory"); + } + + debugger_step = 0; + + + local_irq_save(flags); + + current->thread.debuggerinfo = linux_regs; + + if (kgdb_ops->disable_hw_debug) + kgdb_ops->disable_hw_debug(linux_regs); + + + for (i = 0; i < num_online_cpus(); i++) { + spin_lock(&slavecpulocks[i]); + } + + /* spin_lock code is good enough as a barrier so we don't + * need one here */ + procindebug[smp_processor_id()] = 1; + + /* Master processor is completely in the debugger */ + + if (kgdb_ops->post_master_code) + kgdb_ops->post_master_code(linux_regs, exVector, err_code); + + if (atomic_read(&kgdb_killed_or_detached) && + atomic_read(&kgdb_might_be_resumed)) { + getpacket(remcomInBuffer); + if(remcomInBuffer[0] == 'H' && remcomInBuffer[1] =='c') { + remove_all_break(); + atomic_set(&kgdb_killed_or_detached, 0); + remcomOutBuffer[0] = 'O'; + remcomOutBuffer[1] = 'K'; + remcomOutBuffer[2] = 0; + } + else + return 1; + } + else { + + /* reply to host that an exception has occurred */ + remcomOutBuffer[0] = 'S'; + remcomOutBuffer[1] = hexchars[signo >> 4]; + remcomOutBuffer[2] = hexchars[signo % 16]; + remcomOutBuffer[3] = 'p'; + + int_to_threadref(&thref, shadow_pid(current->pid)); + *pack_threadid(remcomOutBuffer + 4, &thref) = 0; + } + putpacket(remcomOutBuffer, 0); + kgdb_connected = 1; + + kgdb_usethread = current; + kgdb_usethreadid = shadow_pid(current->pid); + + while (1) { + int bpt_type = 0; + error = 0; + remcomOutBuffer[0] = 0; + remcomOutBuffer[1] = 0; + getpacket(remcomInBuffer); + +#if KGDB_DEBUG + bust_spinlocks(1); + printk("CPU%d pid%d GDB packet: %s\n", + smp_processor_id(), current->pid, remcomInBuffer); + bust_spinlocks(0); +#endif + switch (remcomInBuffer[0]) { + case '?': + remcomOutBuffer[0] = 'S'; + remcomOutBuffer[1] = hexchars[signo >> 4]; + remcomOutBuffer[2] = hexchars[signo % 16]; + remcomOutBuffer[3] = 0; + break; + + case 'g': /* return the value of the CPU registers */ + thread = kgdb_usethread; + + if (!thread) + thread = current; + + /* All threads that don't have debuggerinfo should be + in __schedule() sleeping, since all other CPUs + are in kgdb_wait, and thus have debuggerinfo. */ + + if (kgdb_usethreadid >= pid_max + num_online_cpus()) { + kgdb_ops->regs_to_gdb_regs(gdb_regs, + kgdb_ops->shadow_regs(linux_regs, + kgdb_usethreadid - pid_max - + num_online_cpus())); + } else if (thread->thread.debuggerinfo) + kgdb_ops->regs_to_gdb_regs(gdb_regs, + (struct pt_regs *) + thread->thread.debuggerinfo); + else { + /* Pull stuff saved during + * switch_to; nothing else is + accessible (or even particularly relevant). + This should be enough for a stack trace. */ + kgdb_ops->sleeping_thread_to_gdb_regs(gdb_regs, thread); + } + + kgdb_mem2hex((char *) gdb_regs, remcomOutBuffer, NUMREGBYTES, 0); + break; + + case 'G': /* set the value of the CPU registers - return OK */ + hex2mem(&remcomInBuffer[1], (char *) gdb_regs, + NUMREGBYTES, 0); + + if (kgdb_usethread && kgdb_usethread != current) + strcpy(remcomOutBuffer, "E00"); + else { + kgdb_ops->gdb_regs_to_regs(gdb_regs, + (struct pt_regs *) + current->thread.debuggerinfo); + strcpy(remcomOutBuffer, "OK"); + } + + break; + + /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ + case 'm': + /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */ + ptr = &remcomInBuffer[1]; + if (kgdb_hexToLong(&ptr, &addr) && *ptr++ == ',' && + kgdb_hexToLong(&ptr, &length)) { + ptr = 0; + kgdb_mem2hex((char *) addr, remcomOutBuffer, length, 1); + if (kgdb_memerr) + strcpy(remcomOutBuffer, "E03"); + + } + + if (ptr) + strcpy(remcomOutBuffer, "E01"); + break; + + /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */ + case 'M': + /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */ + ptr = &remcomInBuffer[1]; + if (kgdb_hexToLong(&ptr, &addr) && *(ptr++) == ',' && + kgdb_hexToLong(&ptr, &length) && *(ptr++) == ':') { + hex2mem(ptr, (char *)addr, length, 1); + if (kgdb_memerr) + strcpy(remcomOutBuffer, "E09"); + else + strcpy(remcomOutBuffer, "OK"); + ptr = 0; + } + if (ptr) { + strcpy(remcomOutBuffer, "E02"); + } + break; + + + /* Continue and Single Step are Architecture specific + * and will not be handled by the generic code. + */ + + + /* kill the program. KGDB should treat this like a + * continue. + */ + case 'D': + remcomOutBuffer[0] = 'O'; + remcomOutBuffer[1] = 'K'; + remcomOutBuffer[2] = '\0'; + remove_all_break(); + putpacket(remcomOutBuffer, 0); + kgdb_connected = 0; + goto default_handle; + + case 'k': + remove_all_break(); + kgdb_connected = 0; + goto default_handle; + + /* query */ + case 'q': + switch (remcomInBuffer[1]) { + case 's': + case 'f': + if (memcmp(remcomInBuffer+2, "ThreadInfo",10)) + { + strcpy(remcomOutBuffer, "E04"); + break; + } + if (remcomInBuffer[1] == 'f') { + threadid = 1; + } + remcomOutBuffer[0] = 'm'; + ptr = remcomOutBuffer + 1; + for (i = 0; i < 32 && threadid < pid_max + numshadowth; + threadid++) { + thread = getthread(linux_regs, threadid); + if (thread) { + int_to_threadref(&thref, + threadid); + pack_threadid(ptr, &thref); + ptr += 16; + *(ptr++) = ','; + i++; + } + } + *(--ptr) = '\0'; + break; + + case 'C': + /* Current thread id */ + remcomOutBuffer[0] = 'Q'; + remcomOutBuffer[1] = 'C'; + + threadid = shadow_pid(current->pid); + + int_to_threadref(&thref, threadid); + pack_threadid(remcomOutBuffer + 2, &thref); + remcomOutBuffer[18] = '\0'; + break; + + case 'E': + /* Print exception info */ + if (kgdb_ops->printexceptioninfo) + kgdb_ops->printexceptioninfo(exVector, + err_code, + remcomOutBuffer); + break; + case 'T': + if (memcmp(remcomInBuffer+1, "ThreadExtraInfo,",16)) + { + remcomOutBuffer[0] = 0; + strcpy(remcomOutBuffer, "E05"); + break; + } + threadid = 0; + ptr = remcomInBuffer+17; + kgdb_hexToLong(&ptr, &threadid); + if (!getthread(linux_regs, threadid)) { + sprintf(tmpstr, "invalid pid %d", + (int) threadid); + kgdb_mem2hex(tmpstr, remcomOutBuffer, + strlen(tmpstr), 0); + break; + } + if (threadid < pid_max) { + kgdb_mem2hex(getthread(linux_regs, threadid)->comm, + remcomOutBuffer, 16, 0); + } else if (threadid >= pid_max + + num_online_cpus()) { + kgdb_ops->shadowinfo(linux_regs, + remcomOutBuffer, + threadid - pid_max - + num_online_cpus()); + } else { + sprintf(tmpstr, "Shadow task %d for pid 0", + (int)(threadid - + pid_max)); + kgdb_mem2hex(tmpstr, remcomOutBuffer, + strlen(tmpstr), 0); + } + break; + } + break; + + /* task related */ + case 'H': + switch (remcomInBuffer[1]) { + case 'g': + ptr = &remcomInBuffer[2]; + kgdb_hexToLong(&ptr, &threadid); + thread = getthread(linux_regs, threadid); + if (!thread && threadid > 0) { + remcomOutBuffer[0] = 'E'; + remcomOutBuffer[1] = '\0'; + break; + } + kgdb_usethread = thread; + kgdb_usethreadid = threadid; + remcomOutBuffer[0] = 'O'; + remcomOutBuffer[1] = 'K'; + remcomOutBuffer[2] = '\0'; + break; + + case 'c': + atomic_set(&kgdb_killed_or_detached, 0); + ptr = &remcomInBuffer[2]; + kgdb_hexToLong(&ptr, &threadid); + if (!threadid) { + kgdb_contthread = 0; + } else { + thread = getthread(linux_regs, threadid); + if (!thread && threadid > 0) { + remcomOutBuffer[0] = 'E'; + remcomOutBuffer[1] = '\0'; + break; + } + kgdb_contthread = thread; + } + remcomOutBuffer[0] = 'O'; + remcomOutBuffer[1] = 'K'; + remcomOutBuffer[2] = '\0'; + break; + } + break; + + /* Query thread status */ + case 'T': + ptr = &remcomInBuffer[1]; + kgdb_hexToLong(&ptr, &threadid); + thread = getthread(linux_regs, threadid); + if (thread) { + remcomOutBuffer[0] = 'O'; + remcomOutBuffer[1] = 'K'; + remcomOutBuffer[2] = '\0'; + } else { + remcomOutBuffer[0] = 'E'; + remcomOutBuffer[1] = '\0'; + } + break; + case 'z': + case 'Z': + ptr = &remcomInBuffer[2]; + if (*(ptr++) != ',') { + strcpy(remcomOutBuffer, "E07"); + break; + } + kgdb_hexToLong(&ptr, &addr); + + bpt_type = remcomInBuffer[1]; + if (bpt_type != bp_breakpoint) { + if (bpt_type == bp_hardware_breakpoint && + !(kgdb_ops->flags & KGDB_HW_BREAKPOINT)) + break; + + /* if set_break is not defined, then + * remove_break does not matter + */ + if(!kgdb_ops->set_break) + break; + } + + if (remcomInBuffer[0] == 'Z') { + if (bpt_type == bp_breakpoint) + ret = set_break(addr); + else + ret = kgdb_ops->set_break(addr, bpt_type); + } + else { + if (bpt_type == bp_breakpoint) + ret = remove_break(addr); + else + ret = kgdb_ops->remove_break(addr, bpt_type); + } + + if (ret == 0) + strcpy(remcomOutBuffer, "OK"); + else + strcpy(remcomOutBuffer, "E08"); + + break; + + default: + default_handle: + ret = 0; + if (kgdb_ops->handle_exception) + ret= kgdb_ops->handle_exception(exVector, signo, + err_code, + remcomInBuffer, + remcomOutBuffer, + linux_regs); + if(ret >= 0 || remcomInBuffer[0] == 'D' || + remcomInBuffer[0] == 'k') + goto kgdb_exit; + + + } /* switch */ +#if KGDB_DEBUG + bust_spinlocks(1); + printk("Response to GDB: %s\n", remcomOutBuffer); + bust_spinlocks(0); +#endif + + /* reply to the request */ + putpacket(remcomOutBuffer, 0); + } +kgdb_exit: + + if(kgdb_ops->handler_exit) + kgdb_ops->handler_exit(); + + procindebug[smp_processor_id()] = 0; + + for (i = 0; i < num_online_cpus(); i++) { + spin_unlock(&slavecpulocks[i]); + } + /* Wait till all the processors have quit + * from the debugger + */ + for (i = 0; i < num_online_cpus(); i++) { + while (procindebug[i]) { + int j = 10; /* an arbitrary number */ + + while (--j) { + asm volatile ("nop" : : : "memory"); + } + barrier(); + } + } + + /* Free debugger_active */ + atomic_set(&debugger_active, 0); + atomic_set(&kgdb_killed_or_detached, 1); + local_irq_restore(flags); + return ret; +} + +/* + * GDB places a breakpoint at this function to know dynamically + * loaded objects. It's not defined static so that only one instance with this + * name exists in the kernel. + */ + +int module_event(struct notifier_block * self, unsigned long val, void * data) +{ + return 0; +} + +/* this function is used to set up exception handlers for tracing and + breakpoints */ +void set_debug_traps(void) +{ + int i; + + for (i = 0; i < KGDB_MAX_NO_CPUS; i++) + spin_lock_init(&slavecpulocks[i]); + + /* Free debugger_active */ + atomic_set(&debugger_active, 0); + + /* This flag is used, if gdb has detached and wants to start + * another session + */ + atomic_set(&kgdb_killed_or_detached, 1); + atomic_set(&kgdb_might_be_resumed, 0); + + for (i = 0; i < MAX_BREAKPOINTS; i++) + kgdb_break[i].state = bp_disabled; + + + /* + * In case GDB is started before us, ack any packets (presumably + * "$?#xx") sitting there. */ + kgdb_serial->write_char('+'); + + linux_debug_hook = kgdb_handle_exception; + + if (kgdb_ops->kgdb_init) + kgdb_ops->kgdb_init(); + + /* We can't do much if this fails */ + register_module_notifier(&kgdb_module_load_nb); + + kgdb_initialized = 1; + + atomic_set(&kgdb_setting_breakpoint, 0); +} + +/* This function will generate a breakpoint exception. It is used at the + beginning of a program to sync up with a debugger and can be used + otherwise as a quick means to stop program execution and "break" into + the debugger. */ + +void breakpoint(void) +{ + if (kgdb_initialized) { + atomic_set(&kgdb_setting_breakpoint, 1); + wmb(); + BREAKPOINT(); + wmb(); + atomic_set(&kgdb_setting_breakpoint, 0); + } +} + +void kgdb_nmihook(int cpu, void *regs) +{ +#ifdef CONFIG_SMP + if (!procindebug[cpu] && atomic_read(&debugger_active) != (cpu + 1)) { + kgdb_wait((struct pt_regs *)regs); + } +#endif +} + +void kgdb_entry(void) +{ + if (kgdb_enter) { + kgdb_serial->hook(); + } +} + +#ifdef CONFIG_KGDB_CONSOLE +char kgdbconbuf[BUFMAX]; + +void kgdb_console_write(struct console *co, const char *s, unsigned count) +{ + int i; + int wcount; + char *bufptr; + unsigned long flags; + + if (!kgdb_connected) { + return; + } + local_irq_save(flags); + + kgdbconbuf[0] = 'O'; + bufptr = kgdbconbuf + 1; + while (count > 0) { + if ((count << 1) > (BUFMAX - 2)) { + wcount = (BUFMAX - 2) >> 1; + } else { + wcount = count; + } + count -= wcount; + for (i = 0; i < wcount; i++) { + bufptr = pack_hex_byte(bufptr, s[i]); + } + *bufptr = '\0'; + s += wcount; + + putpacket(kgdbconbuf, 1); + + } + local_irq_restore(flags); +} + +/* Always fail so that kgdb console doesn't become the default console */ +static int __init kgdb_console_setup(struct console *co, char *options) +{ + return -1; +} + +static struct console kgdbcons = { + .name = "kgdb", + .write = kgdb_console_write, + .setup = kgdb_console_setup, + .flags = CON_PRINTBUFFER | CON_ENABLED, + .index = -1, +}; + +static int __init kgdb_console_init(void) +{ + register_console(&kgdbcons); + return 0; +} +console_initcall(kgdb_console_init); +#endif + +EXPORT_SYMBOL(breakpoint); + +static int __init opt_kgdb_enter(char *str) +{ + kgdb_enter = 1; + return 1; +} + +static int __init opt_gdb(char *str) +{ + kgdb_enter = 1; + return 1; +} + +/* + * These options have been deprecated and are present only to maintain + * compatibility with kgdb for 2.4 and earlier kernels. + */ +#ifdef CONFIG_KGDB_8250 +extern int kgdb8250_baud; +extern int kgdb8250_ttyS; +static int __init opt_gdbttyS(char *str) +{ + kgdb8250_ttyS = simple_strtoul(str, NULL, 10); + return 1; +} +static int __init opt_gdbbaud(char *str) +{ + kgdb8250_baud = simple_strtoul(str, NULL, 10); + return 1; +} +#endif + +/* + * + * Sequence of following lines has to be maintained because gdb option is a + * prefix of the other two options + */ + +#ifdef CONFIG_KGDB_8250 +__setup("gdbttyS=", opt_gdbttyS); +__setup("gdbbaud=", opt_gdbbaud); +#endif +__setup("gdb", opt_gdb); +__setup("kgdbwait", opt_kgdb_enter); diff -Naurp linux-2.6.0/kernel/Makefile linux-2.6.0-kgdb-2.0-common/kernel/Makefile --- linux-2.6.0/kernel/Makefile 2003-11-24 07:01:15.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/kernel/Makefile 2004-01-07 14:47:53.000000000 +0530 @@ -19,6 +19,7 @@ obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_IKCONFIG) += configs.o obj-$(CONFIG_IKCONFIG_PROC) += configs.o +obj-$(CONFIG_KGDB) += kgdbstub.o ifneq ($(CONFIG_IA64),y) # According to Alan Modra , the -fno-omit-frame-pointer is diff -Naurp linux-2.6.0/kernel/module.c linux-2.6.0-kgdb-2.0-common/kernel/module.c --- linux-2.6.0/kernel/module.c 2003-11-24 07:02:36.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/kernel/module.c 2004-01-07 14:47:53.000000000 +0530 @@ -727,6 +727,11 @@ sys_delete_module(const char __user *nam mod->state = MODULE_STATE_GOING; restart_refcounts(); + down(¬ify_mutex); + notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, + mod); + up(¬ify_mutex); + /* Never wait if forced. */ if (!forced && module_refcount(mod) != 0) wait_for_zero_refcount(mod); @@ -1734,7 +1739,12 @@ sys_init_module(void __user *umod, if (ret < 0) { /* Init routine failed: abort. Try to protect us from buggy refcounters. */ + mod->state = MODULE_STATE_GOING; + down(¬ify_mutex); + notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, + mod); + up(¬ify_mutex); synchronize_kernel(); if (mod->unsafe) printk(KERN_ERR "%s: module is now stuck!\n", diff -Naurp linux-2.6.0/kernel/sched.c linux-2.6.0-kgdb-2.0-common/kernel/sched.c --- linux-2.6.0/kernel/sched.c 2003-12-26 18:33:56.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/kernel/sched.c 2004-01-07 14:47:53.000000000 +0530 @@ -1468,7 +1468,7 @@ void scheduling_functions_start_here(voi /* * schedule() is the main scheduler function. */ -asmlinkage void schedule(void) +asmlinkage void do_schedule(void) { task_t *prev, *next; runqueue_t *rq; @@ -1644,6 +1644,22 @@ int default_wake_function(wait_queue_t * EXPORT_SYMBOL(default_wake_function); +asmlinkage void user_schedule(void) +{ +#ifdef CONFIG_KGDB_THREAD + current->thread.debuggerinfo = NULL; +#endif + do_schedule(); +} + +#ifdef CONFIG_KGDB_THREAD +asmlinkage void kern_do_schedule(struct pt_regs regs) +{ + current->thread.debuggerinfo = ®s; + do_schedule(); +} +#endif + /* * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve diff -Naurp linux-2.6.0/Makefile linux-2.6.0-kgdb-2.0-common/Makefile --- linux-2.6.0/Makefile 2003-12-26 18:33:55.000000000 +0530 +++ linux-2.6.0-kgdb-2.0-common/Makefile 2004-01-07 14:47:53.000000000 +0530 @@ -433,6 +433,8 @@ libs-y := $(libs-y1) $(libs-y2) ifndef CONFIG_FRAME_POINTER CFLAGS += -fomit-frame-pointer +else +CFLAGS += -fno-omit-frame-pointer endif ifdef CONFIG_DEBUG_INFO