gdbmach.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #include <stddef.h>
00020 #include <stdio.h>
00021 #include <assert.h>
00022 #include <gpxe/uaccess.h>
00023 #include <gpxe/gdbstub.h>
00024 #include <gdbmach.h>
00025 
00026 /** @file
00027  *
00028  * GDB architecture-specific bits for i386
00029  *
00030  */
00031 
00032 enum {
00033         DR7_CLEAR = 0x00000400,    /* disable hardware breakpoints */
00034         DR6_CLEAR = 0xffff0ff0,    /* clear breakpoint status */
00035 };
00036 
00037 /** Hardware breakpoint, fields stored in x86 bit pattern form */
00038 struct hwbp {
00039         int type;           /* type (1=write watchpoint, 3=access watchpoint) */
00040         unsigned long addr; /* linear address */
00041         size_t len;         /* length (0=1-byte, 1=2-byte, 3=4-byte) */
00042         int enabled;
00043 };
00044 
00045 static struct hwbp hwbps [ 4 ];
00046 static gdbreg_t dr7 = DR7_CLEAR;
00047 
00048 static struct hwbp *gdbmach_find_hwbp ( int type, unsigned long addr, size_t len ) {
00049         struct hwbp *available = NULL;
00050         unsigned int i;
00051         for ( i = 0; i < sizeof hwbps / sizeof hwbps [ 0 ]; i++ ) {
00052                 if ( hwbps [ i ].type == type && hwbps [ i ].addr == addr && hwbps [ i ].len == len ) {
00053                         return &hwbps [ i ];
00054                 }
00055                 if ( !hwbps [ i ].enabled ) {
00056                         available = &hwbps [ i ];
00057                 }
00058         }
00059         return available;
00060 }
00061 
00062 static void gdbmach_commit_hwbp ( struct hwbp *bp ) {
00063         unsigned int regnum = bp - hwbps;
00064 
00065         /* Set breakpoint address */
00066         assert ( regnum < ( sizeof hwbps / sizeof hwbps [ 0 ] ) );
00067         switch ( regnum ) {
00068                 case 0:
00069                         __asm__ __volatile__ ( "movl %0, %%dr0\n" : : "r" ( bp->addr ) );
00070                         break;
00071                 case 1:
00072                         __asm__ __volatile__ ( "movl %0, %%dr1\n" : : "r" ( bp->addr ) );
00073                         break;
00074                 case 2:
00075                         __asm__ __volatile__ ( "movl %0, %%dr2\n" : : "r" ( bp->addr ) );
00076                         break;
00077                 case 3:
00078                         __asm__ __volatile__ ( "movl %0, %%dr3\n" : : "r" ( bp->addr ) );
00079                         break;
00080         }
00081 
00082         /* Set type */
00083         dr7 &= ~( 0x3 << ( 16 + 4 * regnum ) );
00084         dr7 |= bp->type << ( 16 + 4 * regnum );
00085 
00086         /* Set length */
00087         dr7 &= ~( 0x3 << ( 18 + 4 * regnum ) );
00088         dr7 |= bp->len << ( 18 + 4 * regnum );
00089 
00090         /* Set/clear local enable bit */
00091         dr7 &= ~( 0x3 << 2 * regnum );
00092         dr7 |= bp->enabled << 2 * regnum;
00093 }
00094 
00095 int gdbmach_set_breakpoint ( int type, unsigned long addr, size_t len, int enable ) {
00096         struct hwbp *bp;
00097         
00098         /* Check and convert breakpoint type to x86 type */
00099         switch ( type ) {
00100                 case GDBMACH_WATCH:
00101                         type = 0x1;
00102                         break;
00103                 case GDBMACH_AWATCH:
00104                         type = 0x3;
00105                         break;
00106                 default:
00107                         return 0; /* unsupported breakpoint type */
00108         }
00109 
00110         /* Only lengths 1, 2, and 4 are supported */
00111         if ( len != 2 && len != 4 ) {
00112                 len = 1;
00113         }
00114         len--; /* convert to x86 breakpoint length bit pattern */
00115 
00116         /* Calculate linear address by adding segment base */
00117         addr += virt_offset;
00118 
00119         /* Set up the breakpoint */
00120         bp = gdbmach_find_hwbp ( type, addr, len );
00121         if ( !bp ) {
00122                 return 0; /* ran out of hardware breakpoints */
00123         }
00124         bp->type = type;
00125         bp->addr = addr;
00126         bp->len = len;
00127         bp->enabled = enable;
00128         gdbmach_commit_hwbp ( bp );
00129         return 1;
00130 }
00131 
00132 static void gdbmach_disable_hwbps ( void ) {
00133         /* Store and clear hardware breakpoints */
00134         __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( DR7_CLEAR ) );
00135 }
00136 
00137 static void gdbmach_enable_hwbps ( void ) {
00138         /* Clear breakpoint status register */
00139         __asm__ __volatile__ ( "movl %0, %%dr6\n" : : "r" ( DR6_CLEAR ) );
00140 
00141         /* Restore hardware breakpoints */
00142         __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( dr7 ) );
00143 }
00144 
00145 __asmcall void gdbmach_handler ( int signo, gdbreg_t *regs ) {
00146         gdbmach_disable_hwbps();
00147         gdbstub_handler ( signo, regs );
00148         gdbmach_enable_hwbps();
00149 }

Generated on Tue Apr 6 20:00:49 2010 for gPXE by  doxygen 1.5.7.1