gdbmach.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00027
00028
00029
00030
00031
00032 enum {
00033 DR7_CLEAR = 0x00000400,
00034 DR6_CLEAR = 0xffff0ff0,
00035 };
00036
00037
00038 struct hwbp {
00039 int type;
00040 unsigned long addr;
00041 size_t len;
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
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
00083 dr7 &= ~( 0x3 << ( 16 + 4 * regnum ) );
00084 dr7 |= bp->type << ( 16 + 4 * regnum );
00085
00086
00087 dr7 &= ~( 0x3 << ( 18 + 4 * regnum ) );
00088 dr7 |= bp->len << ( 18 + 4 * regnum );
00089
00090
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
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;
00108 }
00109
00110
00111 if ( len != 2 && len != 4 ) {
00112 len = 1;
00113 }
00114 len--;
00115
00116
00117 addr += virt_offset;
00118
00119
00120 bp = gdbmach_find_hwbp ( type, addr, len );
00121 if ( !bp ) {
00122 return 0;
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
00134 __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( DR7_CLEAR ) );
00135 }
00136
00137 static void gdbmach_enable_hwbps ( void ) {
00138
00139 __asm__ __volatile__ ( "movl %0, %%dr6\n" : : "r" ( DR6_CLEAR ) );
00140
00141
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 }