iPXE
gdbmach.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
3  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  *
20  * You can also choose to distribute this program under the terms of
21  * the Unmodified Binary Distribution Licence (as given in the file
22  * COPYING.UBDL), provided that you have satisfied its requirements.
23  */
24 
25 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
26 
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <assert.h>
31 #include <ipxe/uaccess.h>
32 #include <ipxe/gdbstub.h>
33 #include <librm.h>
34 
35 /** @file
36  *
37  * GDB architecture-specific bits for x86
38  *
39  */
40 
41 /** Number of hardware breakpoints */
42 #define NUM_HWBP 4
43 
44 /** Debug register 7: Global breakpoint enable */
45 #define DR7_G( bp ) ( 2 << ( 2 * (bp) ) )
46 
47 /** Debug register 7: Global exact breakpoint enable */
48 #define DR7_GE ( 1 << 9 )
49 
50 /** Debug register 7: Break on data writes */
51 #define DR7_RWLEN_WRITE 0x11110000
52 
53 /** Debug register 7: Break on data access */
54 #define DR7_RWLEN_ACCESS 0x33330000
55 
56 /** Debug register 7: One-byte length */
57 #define DR7_RWLEN_1 0x00000000
58 
59 /** Debug register 7: Two-byte length */
60 #define DR7_RWLEN_2 0x44440000
61 
62 /** Debug register 7: Four-byte length */
63 #define DR7_RWLEN_4 0xcccc0000
64 
65 /** Debug register 7: Eight-byte length */
66 #define DR7_RWLEN_8 0x88880000
67 
68 /** Debug register 7: Breakpoint R/W and length mask */
69 #define DR7_RWLEN_MASK( bp ) ( 0xf0000 << ( 4 * (bp) ) )
70 
71 /** Hardware breakpoint addresses (debug registers 0-3) */
72 static unsigned long dr[NUM_HWBP];
73 
74 /** Active value of debug register 7 */
75 static unsigned long dr7 = DR7_GE;
76 
77 /**
78  * Update debug registers
79  *
80  */
81 static void gdbmach_update ( void ) {
82 
83  /* Set debug registers */
84  __asm__ __volatile__ ( "mov %0, %%dr0" : : "r" ( dr[0] ) );
85  __asm__ __volatile__ ( "mov %0, %%dr1" : : "r" ( dr[1] ) );
86  __asm__ __volatile__ ( "mov %0, %%dr2" : : "r" ( dr[2] ) );
87  __asm__ __volatile__ ( "mov %0, %%dr3" : : "r" ( dr[3] ) );
88  __asm__ __volatile__ ( "mov %0, %%dr7" : : "r" ( dr7 ) );
89 }
90 
91 /**
92  * Find reusable or available hardware breakpoint
93  *
94  * @v addr Linear address
95  * @v rwlen Control bits
96  * @ret bp Hardware breakpoint, or negative error
97  */
98 static int gdbmach_find ( unsigned long addr, unsigned int rwlen ) {
99  unsigned int i;
100  int bp = -ENOENT;
101 
102  /* Look for a reusable or available breakpoint */
103  for ( i = 0 ; i < NUM_HWBP ; i++ ) {
104 
105  /* If breakpoint is not enabled, then it is available */
106  if ( ! ( dr7 & DR7_G ( i ) ) ) {
107  bp = i;
108  continue;
109  }
110 
111  /* If breakpoint is enabled and has the same address
112  * and control bits, then reuse it.
113  */
114  if ( ( dr[i] == addr ) &&
115  ( ( ( dr7 ^ rwlen ) & DR7_RWLEN_MASK ( i ) ) == 0 ) ) {
116  bp = i;
117  break;
118  }
119  }
120 
121  return bp;
122 }
123 
124 /**
125  * Set hardware breakpoint
126  *
127  * @v type GDB breakpoint type
128  * @v addr Virtual address
129  * @v len Length
130  * @v enable Enable (not disable) breakpoint
131  * @ret rc Return status code
132  */
133 int gdbmach_set_breakpoint ( int type, unsigned long addr, size_t len,
134  int enable ) {
135  unsigned int rwlen;
136  unsigned long mask;
137  int bp;
138 
139  /* Parse breakpoint type */
140  switch ( type ) {
141  case GDBMACH_WATCH:
142  rwlen = DR7_RWLEN_WRITE;
143  break;
144  case GDBMACH_AWATCH:
145  rwlen = DR7_RWLEN_ACCESS;
146  break;
147  default:
148  return -ENOTSUP;
149  }
150 
151  /* Parse breakpoint length */
152  switch ( len ) {
153  case 1:
154  rwlen |= DR7_RWLEN_1;
155  break;
156  case 2:
157  rwlen |= DR7_RWLEN_2;
158  break;
159  case 4:
160  rwlen |= DR7_RWLEN_4;
161  break;
162  case 8:
163  rwlen |= DR7_RWLEN_8;
164  break;
165  default:
166  return -ENOTSUP;
167  }
168 
169  /* Convert to linear address */
170  if ( sizeof ( physaddr_t ) <= sizeof ( uint32_t ) )
171  addr = virt_to_phys ( ( void * ) addr );
172 
173  /* Find reusable or available hardware breakpoint */
174  bp = gdbmach_find ( addr, rwlen );
175  if ( bp < 0 )
176  return ( enable ? -ENOBUFS : 0 );
177 
178  /* Configure this breakpoint */
179  DBGC ( &dr[0], "GDB bp %d at %p+%zx type %d (%sabled)\n",
180  bp, ( ( void * ) addr ), len, type, ( enable ? "en" : "dis" ) );
181  dr[bp] = addr;
182  mask = DR7_RWLEN_MASK ( bp );
183  dr7 = ( ( dr7 & ~mask ) | ( rwlen & mask ) );
184  mask = DR7_G ( bp );
185  dr7 &= ~mask;
186  if ( enable )
187  dr7 |= mask;
188 
189  /* Update debug registers */
190  gdbmach_update();
191 
192  return 0;
193 }
194 
195 /**
196  * Handle exception
197  *
198  * @v signo GDB signal number
199  * @v regs Register dump
200  */
201 __asmcall void gdbmach_handler ( int signo, gdbreg_t *regs ) {
202  unsigned long dr7_disabled = DR7_GE;
203  unsigned long dr6_clear = 0;
204 
205  /* Temporarily disable breakpoints */
206  __asm__ __volatile__ ( "mov %0, %%dr7\n" : : "r" ( dr7_disabled ) );
207 
208  /* Handle exception */
209  DBGC ( &dr[0], "GDB signal %d\n", signo );
210  DBGC2_HDA ( &dr[0], 0, regs, ( GDBMACH_NREGS * sizeof ( *regs ) ) );
211  gdbstub_handler ( signo, regs );
212  DBGC ( &dr[0], "GDB signal %d returning\n", signo );
213  DBGC2_HDA ( &dr[0], 0, regs, ( GDBMACH_NREGS * sizeof ( *regs ) ) );
214 
215  /* Clear breakpoint status register */
216  __asm__ __volatile__ ( "mov %0, %%dr6\n" : : "r" ( dr6_clear ) );
217 
218  /* Re-enable breakpoints */
219  __asm__ __volatile__ ( "mov %0, %%dr7\n" : : "r" ( dr7 ) );
220 }
221 
222 /**
223  * CPU exception vectors
224  *
225  * Note that we cannot intercept anything from INT8 (double fault)
226  * upwards, since these overlap by default with IRQ0-7.
227  */
228 static void * gdbmach_vectors[] = {
229  gdbmach_sigfpe, /* Divide by zero */
230  gdbmach_sigtrap, /* Debug trap */
231  NULL, /* Non-maskable interrupt */
232  gdbmach_sigtrap, /* Breakpoint */
233  gdbmach_sigstkflt, /* Overflow */
234  gdbmach_sigstkflt, /* Bound range exceeded */
235  gdbmach_sigill, /* Invalid opcode */
236 };
237 
238 /**
239  * Initialise GDB
240  */
241 void gdbmach_init ( void ) {
242  unsigned int i;
243 
244  /* Hook CPU exception vectors */
245  for ( i = 0 ; i < ( sizeof ( gdbmach_vectors ) /
246  sizeof ( gdbmach_vectors[0] ) ) ; i++ ) {
247  if ( gdbmach_vectors[i] )
249  }
250 }
Error codes.
#define DR7_RWLEN_MASK(bp)
Debug register 7: Breakpoint R/W and length mask.
Definition: gdbmach.c:69
unsigned long gdbreg_t
Definition: gdbmach.h:15
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
uint16_t bp
Definition: registers.h:23
int gdbmach_set_breakpoint(int type, unsigned long addr, size_t len, int enable)
Set hardware breakpoint.
Definition: gdbmach.c:133
void gdbmach_init(void)
Initialise GDB.
Definition: gdbmach.c:241
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
Access to external ("user") memory.
#define DR7_RWLEN_1
Debug register 7: One-byte length.
Definition: gdbmach.c:57
#define DR7_GE
Debug register 7: Global exact breakpoint enable.
Definition: gdbmach.c:48
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
static int gdbmach_find(unsigned long addr, unsigned int rwlen)
Find reusable or available hardware breakpoint.
Definition: gdbmach.c:98
#define __asmcall
Declare a function with standard calling conventions.
Definition: compiler.h:15
Assertions.
#define DBGC2_HDA(...)
Definition: compiler.h:523
#define NUM_HWBP
Number of hardware breakpoints.
Definition: gdbmach.c:42
__asmcall void gdbmach_handler(int signo, gdbreg_t *regs)
Handle exception.
Definition: gdbmach.c:201
#define DR7_RWLEN_ACCESS
Debug register 7: Break on data access.
Definition: gdbmach.c:54
__asm__ __volatile__("call *%9" :"=a"(result), "=c"(discard_ecx), "=d"(discard_edx) :"d"(0), "a"(code), "b"(0), "c"(in_phys), "D"(0), "S"(out_phys), "m"(hypercall))
void gdbstub_handler(int signo, gdbreg_t *regs)
Interrupt handler.
Definition: gdbstub.c:371
void gdbmach_sigstkflt(void)
static unsigned long dr[NUM_HWBP]
Hardware breakpoint addresses (debug registers 0-3)
Definition: gdbmach.c:72
unsigned int uint32_t
Definition: stdint.h:12
struct i386_regs regs
Definition: registers.h:15
void gdbmach_sigfpe(void)
void gdbmach_sigtrap(void)
GDB remote debugging.
unsigned long physaddr_t
Definition: stdint.h:20
#define DR7_RWLEN_2
Debug register 7: Two-byte length.
Definition: gdbmach.c:60
static void * gdbmach_vectors[]
CPU exception vectors.
Definition: gdbmach.c:228
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
#define DR7_G(bp)
Debug register 7: Global breakpoint enable.
Definition: gdbmach.c:45
static void gdbmach_update(void)
Update debug registers.
Definition: gdbmach.c:81
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
void set_interrupt_vector(unsigned int intr, void *vector)
Set interrupt vector.
Definition: librm_mgmt.c:97
u32 addr
Definition: sky2.h:8
void gdbmach_sigill(void)
static unsigned long dr7
Active value of debug register 7.
Definition: gdbmach.c:75
#define DR7_RWLEN_8
Debug register 7: Eight-byte length.
Definition: gdbmach.c:66
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define DR7_RWLEN_WRITE
Debug register 7: Break on data writes.
Definition: gdbmach.c:51
#define DR7_RWLEN_4
Debug register 7: Four-byte length.
Definition: gdbmach.c:63