Debugging patches
PATCH 1/1 Debugging patches
I've been actively working on GDB for the past two weeks now. A good half of the problems I've had have turned out to be kernel bugs; one of them (shadowing res in the POKEUSR case in sys_ptrace) was already fixed in the OSS tree and not in our local tree, but the others were still somewhat interesting.
The biggest one was the fact that passing arguments to the inferior in floating point registers just didn't work. I tracked this down to at least three separate problems:
- We would set last_task_used_math without clearing the ST0_CU1 bit in the previous task owning the FPU. When that previous task swapped in again, it would use the existing FP registers, and lazy_fpu_switch would never be called. This happened in signal.c and in ptrace.c.
- ptrace didn't look for the FP registers in the right places. This's been broken since the FPU emulator merge a while back.
- We would create new processes with the ST0_CU1 bit already set if their parent process had it set.
Of course, the lazy switching isn't quite as useful as it could be, since every program will eventually use the FPU if not build -msoft-float - I think it's happening in glibc. But we can possibly work around that later. It still does save a great number of switches, so it's worthwhile - when it works.
Other patches in my directory that I'm submitting along with that one:
- kgdb-crash-resistant.diff
This makes kgdb survive an attempt to dereference bad memory. It only works after memory management has been set up, though. It's possible to do it in such a way that it works even earlier - see PowerPC for an example. We would have to set the fault handler temporarily, and basically longjmp out of the fault handler. This is much more straightforward, and met my needs at the time.
- mips-gdb-with-kgdb.diff
There's no good reason to trigger kgdb on any trap whose origin is in userland - it's a kernel debugger, right? So I save the traps, and pass them along to the old handlers if necessary. This way kgdb won't stop on SIGTRAP when you set a breakpoint in gdb.
- mips-rtsignal.diff
Thought I'd sent this already... but I guess not. setup_frame and setup_rt_frame build structures of different sizes, matching sys_sigreturn and sys_rt_sigreturn respectively. Wouldn't it be useful if the frame setup_rt_frame built returned into the right function?
Signature · 2 lines
Daniel Jacobowitz Debian GNU/Linux Developer Monta Vista Software Debian Security Team
Index: arch/mips/kernel/ptrace.c
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/arch/mips/kernel/ptrace.c,v retrieving revision 1.3
mips/kernel/ptrace.c +26 -2
@@ -149,8 +149,19 @@save_fp (child);disable_cp1 (); last_task_used_math =NULL ; + regs->cp0_status &= ~ST0_CU1 ; } - tmp = (unsigned long ) fregs[(addr -32 )]; +/* The odd registers are actually the high order bits of the values +stored in the even registers - unless we're using r2k_switch.S. */ +#if defined(CONFIG_CPU_R3000 ) || defined(CONFIG_CPU_R3912 ) +if (mips_cpu_options &MIPS_CPU_FPU ) + tmp = *(unsigned long *)(fregs + addr); + else +#endif +if (addr &1 ) + tmp = (unsigned long ) (fregs[((addr & ~1 ) -32 )] >>32 ); +else + tmp = (unsigned long ) (fregs[(addr -32 )] &0xffffffff ); }else { tmp =-1 ;/* FP not yet used */ }
@@ -238,8 +249,21 @@memset (&child->thread.fpu.hard, ~0 ,sizeof (child->thread.fpu.hard)); child->thread.fpu.hard.control =0 ; + } +/* The odd registers are actually the high order bits of the values +stored in the even registers - unless we're using r2k_switch.S. */ +#if defined(CONFIG_CPU_R3000 ) || defined(CONFIG_CPU_R3912 ) +if (mips_cpu_options &MIPS_CPU_FPU ) + *(unsigned long *)(fregs + addr) = data; + else +#endif +if (addr &1 ) { + fregs[(addr & ~1 ) -FPR_BASE ] &=0xffffffff ; + fregs[(addr & ~1 ) -FPR_BASE ] |= ((unsigned long long ) data) <<32 ; + }else { + fregs[addr -FPR_BASE ] &= ~0xffffffffLL ; + fregs[addr -FPR_BASE ] |= data; } - fregs[addr -FPR_BASE ] = data;break ; }case PC :
Index: arch/mips/kernel/signal.c
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/arch/mips/kernel/signal.c,v retrieving revision 1.2
mips/kernel/signal.c +3 -2
@@ -220,8 +220,10 @@ err |=__get_user (owned_fp, &sc->sc_ownedfp);if (owned_fp) { +if (last_task_used_math && (last_task_used_math != current)) + last_task_used_math->thread.cp0_status &= ~ST0_CU1 ; err |=restore_fp_context (sc); last_task_used_math = current; }return err;
@@ -353,12 +355,11 @@ owned_fp = (current == last_task_used_math); err |=__put_user (owned_fp, &sc->sc_ownedfp); -if (current->used_math) {/* fp is active. */ +if (owned_fp) {/* fp is active. */ set_cp0_status (ST0_CU1 ); err |=save_fp_context (sc); last_task_used_math =NULL ; regs->cp0_status &= ~ST0_CU1 ; - current->used_math =0 ; }return err;
Index: include/asm-mips/processor.h
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/include/asm-mips/processor.h,v retrieving revision 1.1.1.2
asm-mips/processor.h +2 -2
@@ -235,8 +235,8 @@* Do necessary setup to start up a newly executed thread. */ #define start_thread (regs, new_pc, new_sp) do { \ -/* New thread looses kernel privileges. */ \ - regs->cp0_status = (regs->cp0_status & ~(ST0_CU0 |ST0_KSU )) |KU_USER ;\ +/* New thread loses kernel and FPU privileges. */ \ + regs->cp0_status = (regs->cp0_status & ~(ST0_CU0 |ST0_KSU |ST0_CU1 )) |KU_USER ;\ regs->cp0_epc = new_pc; \ regs->regs[29 ] = new_sp; \ current->thread.current_ds =USER_DS ; \
Index: arch/mips/kernel/gdb-low.S
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/arch/mips/kernel/gdb-low.S,v retrieving revision 1.2
mips/kernel/gdb-low.S +34 -0
@@ -11,6 +11,7 @@ #include <linux/sys.h> #include <asm/asm.h> +#include <asm/errno.h> #include <asm/mipsregs.h> #include <asm/regdef.h> #include <asm/stackframe.h>
@@ -314,3 +315,36 @@ .set at .set reorder END(trap_low) + +LEAF(kgdb_read_byte) + .set push + .set noreorder + .set nomacro +4: lb t0, (a0) + .set pop + sb t0, (a1) + li v0, 0 + jr ra + .section __ex_table,"a" + PTR 4b, kgdbfault + .previous + END(kgdb_read_byte) + +LEAF(kgdb_write_byte) + .set push + .set noreorder + .set nomacro +5: sb a0, (a1) + .set pop + li v0, 0 + jr ra + .section __ex_table,"a" + PTR 5b, kgdbfault + .previous + END(kgdb_write_byte) + + .type kgdbfault@function + .ent kgdbfault +kgdbfault: li v0, -EFAULT + jr ra + .end kgdbfault
Index: arch/mips/kernel/gdb-stub.c
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/arch/mips/kernel/gdb-stub.c,v retrieving revision 1.2
mips/kernel/gdb-stub.c +10 -48
116 lines not on this page · raw message
Index: arch/mips/kernel/gdb-low.S
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/arch/mips/kernel/gdb-low.S,v retrieving revision 1.1.1.1
mips/kernel/gdb-low.S +7 -3
@@ -30,10 +30,14 @@ move k1,sp /* - * Called from user mode, new stack + * Called from user mode, go somewhere else. */ - lui k1,%hi(kernelsp) - lw k1,%lo(kernelsp)(k1) + lui k1,%hi(saved_vectors) + mfc0 k0,CP0_CAUSE + andi k0,k0,0x7c + add k1,k1,k0 + lw k0,%lo(saved_vectors)(k1) + jr k0 1: move k0,sp subu sp,k1,GDB_FR_SIZE
Index: arch/mips/kernel/gdb-stub.c
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/arch/mips/kernel/gdb-stub.c,v retrieving revision 1.1.1.2
mips/kernel/gdb-stub.c +3 -1
@@ -388,6 +388,8 @@ { 0, 0} /* Must be last */ }; +/* Save the normal trap handlers for user-mode traps. */ +void *saved_vectors[32 ];/* * Set up exception handlers for tracing and breakpoints
@@ -400,7 +402,7 @@save_and_cli (flags);for (ht = hard_trap_info; ht->tt && ht->signo; ht++) -set_except_vector (ht->tt, trap_low); + saved_vectors[ht->tt] =set_except_vector (ht->tt, trap_low);/* * In case GDB is started before us, ack any packets
Index: signal.c
===================================================================
RCS file: /cvsroot/hhl-2.4.2/linux/arch/mips/kernel/signal.c,v retrieving revision 1.1.1.2
signal.c +2 -2
@@ -464,10 +464,10 @@/* * Set up the return code ... * -* li v0, __NR_sigreturn +* li v0, __NR_rt_sigreturn * syscall */ - err |=__put_user (0x24020000 + __NR_sigreturn, + err |=__put_user (0x24020000 + __NR_rt_sigreturn, frame->rs_code +0 ); err |=__put_user (0x0000000c , frame->rs_code +1 );
Quoted 7 lines · Daniel Jacobowitz
The biggest one was the fact that passing arguments to the inferior in floating point registers just didn't work. I tracked this down to at least three separate problems:
- We would setlast_task_used_mathwithout clearing theST0_CU1bit in the previous task owning the FPU. When that previous task swapped in again, it would use the existing FP registers, andlazy_fpu_switchwould never be called. This happened insignal.cand inptrace.c.
First signal.c segment - calling restore_fp_context should result in a proper FPU context switch.
Quoted 5 lines
- ptrace didn't look for the FP registers in the right places. This's been broken since the FPU emulator merge a while back.
- We would create new processes with the
ST0_CU1bit already set if their parent process had it set.
No, copy_thread clears CU1.
(Have to breed about this patch a bit more, stuff for the plane ...)
Quoted 5 lines
Of course, the lazy switching isn't quite as useful as it could be, since every program will eventually use the FPU if not build -msoft-float - I think it's happening in glibc. But we can possibly work around that later. It still does save a great number of switches, so it's worthwhile - when it works.
Newer libcs shouldn't try to initialize $fcr31 to zero because that's already the default.
Quoted 4 lines
Other patches in my directory that I'm submitting along with that one:
- kgdb-crash-resistant.diff
- mips-gdb-with-kgdb.diff
- mips-rtsignal.diff
These three look good, applied.
Ralf