2 * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/uaccess.h>
26 #include <ipxe/gdbstub.h>
32 * GDB architecture-specific bits for i386
37 DR7_CLEAR = 0x00000400, /* disable hardware breakpoints */
38 DR6_CLEAR = 0xffff0ff0, /* clear breakpoint status */
41 /** Hardware breakpoint, fields stored in x86 bit pattern form */
43 int type; /* type (1=write watchpoint, 3=access watchpoint) */
44 unsigned long addr; /* linear address */
45 size_t len; /* length (0=1-byte, 1=2-byte, 3=4-byte) */
49 static struct hwbp hwbps [ 4 ];
50 static gdbreg_t dr7 = DR7_CLEAR;
52 static struct hwbp *gdbmach_find_hwbp ( int type, unsigned long addr, size_t len ) {
53 struct hwbp *available = NULL;
55 for ( i = 0; i < sizeof hwbps / sizeof hwbps [ 0 ]; i++ ) {
56 if ( hwbps [ i ].type == type && hwbps [ i ].addr == addr && hwbps [ i ].len == len ) {
59 if ( !hwbps [ i ].enabled ) {
60 available = &hwbps [ i ];
66 static void gdbmach_commit_hwbp ( struct hwbp *bp ) {
67 unsigned int regnum = bp - hwbps;
69 /* Set breakpoint address */
70 assert ( regnum < ( sizeof hwbps / sizeof hwbps [ 0 ] ) );
73 __asm__ __volatile__ ( "movl %0, %%dr0\n" : : "r" ( bp->addr ) );
76 __asm__ __volatile__ ( "movl %0, %%dr1\n" : : "r" ( bp->addr ) );
79 __asm__ __volatile__ ( "movl %0, %%dr2\n" : : "r" ( bp->addr ) );
82 __asm__ __volatile__ ( "movl %0, %%dr3\n" : : "r" ( bp->addr ) );
87 dr7 &= ~( 0x3 << ( 16 + 4 * regnum ) );
88 dr7 |= bp->type << ( 16 + 4 * regnum );
91 dr7 &= ~( 0x3 << ( 18 + 4 * regnum ) );
92 dr7 |= bp->len << ( 18 + 4 * regnum );
94 /* Set/clear local enable bit */
95 dr7 &= ~( 0x3 << 2 * regnum );
96 dr7 |= bp->enabled << 2 * regnum;
99 int gdbmach_set_breakpoint ( int type, unsigned long addr, size_t len, int enable ) {
102 /* Check and convert breakpoint type to x86 type */
111 return 0; /* unsupported breakpoint type */
114 /* Only lengths 1, 2, and 4 are supported */
115 if ( len != 2 && len != 4 ) {
118 len--; /* convert to x86 breakpoint length bit pattern */
120 /* Calculate linear address by adding segment base */
123 /* Set up the breakpoint */
124 bp = gdbmach_find_hwbp ( type, addr, len );
126 return 0; /* ran out of hardware breakpoints */
131 bp->enabled = enable;
132 gdbmach_commit_hwbp ( bp );
136 static void gdbmach_disable_hwbps ( void ) {
137 /* Store and clear hardware breakpoints */
138 __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( DR7_CLEAR ) );
141 static void gdbmach_enable_hwbps ( void ) {
142 /* Clear breakpoint status register */
143 __asm__ __volatile__ ( "movl %0, %%dr6\n" : : "r" ( DR6_CLEAR ) );
145 /* Restore hardware breakpoints */
146 __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( dr7 ) );
149 __asmcall void gdbmach_handler ( int signo, gdbreg_t *regs ) {
150 gdbmach_disable_hwbps();
151 gdbstub_handler ( signo, regs );
152 gdbmach_enable_hwbps();
155 static void * gdbmach_interrupt_vectors[] = {
156 gdbmach_nocode_sigfpe, /* Divide by zero */
157 gdbmach_nocode_sigtrap, /* Debug trap */
158 NULL, /* Non-maskable interrupt */
159 gdbmach_nocode_sigtrap, /* Breakpoint */
160 gdbmach_nocode_sigstkflt, /* Overflow */
161 gdbmach_nocode_sigstkflt, /* Bound range exceeded */
162 gdbmach_nocode_sigill, /* Invalid opcode */
163 NULL, /* Device not available */
164 gdbmach_withcode_sigbus, /* Double fault */
165 NULL, /* Coprocessor segment overrun */
166 gdbmach_withcode_sigsegv, /* Invalid TSS */
167 gdbmach_withcode_sigsegv, /* Segment not present */
168 gdbmach_withcode_sigsegv, /* Stack segment fault */
169 gdbmach_withcode_sigsegv, /* General protection fault */
170 gdbmach_withcode_sigsegv, /* Page fault */
173 void gdbmach_init ( void ) {
176 for ( i = 0 ; i < ( sizeof ( gdbmach_interrupt_vectors ) /
177 sizeof ( gdbmach_interrupt_vectors[0] ) ) ; i++ ) {
178 set_interrupt_vector ( i, gdbmach_interrupt_vectors[i] );