Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / core / gdbmach.c
1 /*
2  * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <assert.h>
25 #include <ipxe/uaccess.h>
26 #include <ipxe/gdbstub.h>
27 #include <librm.h>
28 #include <gdbmach.h>
29
30 /** @file
31  *
32  * GDB architecture-specific bits for i386
33  *
34  */
35
36 enum {
37         DR7_CLEAR = 0x00000400,    /* disable hardware breakpoints */
38         DR6_CLEAR = 0xffff0ff0,    /* clear breakpoint status */
39 };
40
41 /** Hardware breakpoint, fields stored in x86 bit pattern form */
42 struct hwbp {
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) */
46         int enabled;
47 };
48
49 static struct hwbp hwbps [ 4 ];
50 static gdbreg_t dr7 = DR7_CLEAR;
51
52 static struct hwbp *gdbmach_find_hwbp ( int type, unsigned long addr, size_t len ) {
53         struct hwbp *available = NULL;
54         unsigned int i;
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 ) {
57                         return &hwbps [ i ];
58                 }
59                 if ( !hwbps [ i ].enabled ) {
60                         available = &hwbps [ i ];
61                 }
62         }
63         return available;
64 }
65
66 static void gdbmach_commit_hwbp ( struct hwbp *bp ) {
67         unsigned int regnum = bp - hwbps;
68
69         /* Set breakpoint address */
70         assert ( regnum < ( sizeof hwbps / sizeof hwbps [ 0 ] ) );
71         switch ( regnum ) {
72                 case 0:
73                         __asm__ __volatile__ ( "movl %0, %%dr0\n" : : "r" ( bp->addr ) );
74                         break;
75                 case 1:
76                         __asm__ __volatile__ ( "movl %0, %%dr1\n" : : "r" ( bp->addr ) );
77                         break;
78                 case 2:
79                         __asm__ __volatile__ ( "movl %0, %%dr2\n" : : "r" ( bp->addr ) );
80                         break;
81                 case 3:
82                         __asm__ __volatile__ ( "movl %0, %%dr3\n" : : "r" ( bp->addr ) );
83                         break;
84         }
85
86         /* Set type */
87         dr7 &= ~( 0x3 << ( 16 + 4 * regnum ) );
88         dr7 |= bp->type << ( 16 + 4 * regnum );
89
90         /* Set length */
91         dr7 &= ~( 0x3 << ( 18 + 4 * regnum ) );
92         dr7 |= bp->len << ( 18 + 4 * regnum );
93
94         /* Set/clear local enable bit */
95         dr7 &= ~( 0x3 << 2 * regnum );
96         dr7 |= bp->enabled << 2 * regnum;
97 }
98
99 int gdbmach_set_breakpoint ( int type, unsigned long addr, size_t len, int enable ) {
100         struct hwbp *bp;
101         
102         /* Check and convert breakpoint type to x86 type */
103         switch ( type ) {
104                 case GDBMACH_WATCH:
105                         type = 0x1;
106                         break;
107                 case GDBMACH_AWATCH:
108                         type = 0x3;
109                         break;
110                 default:
111                         return 0; /* unsupported breakpoint type */
112         }
113
114         /* Only lengths 1, 2, and 4 are supported */
115         if ( len != 2 && len != 4 ) {
116                 len = 1;
117         }
118         len--; /* convert to x86 breakpoint length bit pattern */
119
120         /* Calculate linear address by adding segment base */
121         addr += virt_offset;
122
123         /* Set up the breakpoint */
124         bp = gdbmach_find_hwbp ( type, addr, len );
125         if ( !bp ) {
126                 return 0; /* ran out of hardware breakpoints */
127         }
128         bp->type = type;
129         bp->addr = addr;
130         bp->len = len;
131         bp->enabled = enable;
132         gdbmach_commit_hwbp ( bp );
133         return 1;
134 }
135
136 static void gdbmach_disable_hwbps ( void ) {
137         /* Store and clear hardware breakpoints */
138         __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( DR7_CLEAR ) );
139 }
140
141 static void gdbmach_enable_hwbps ( void ) {
142         /* Clear breakpoint status register */
143         __asm__ __volatile__ ( "movl %0, %%dr6\n" : : "r" ( DR6_CLEAR ) );
144
145         /* Restore hardware breakpoints */
146         __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( dr7 ) );
147 }
148
149 __asmcall void gdbmach_handler ( int signo, gdbreg_t *regs ) {
150         gdbmach_disable_hwbps();
151         gdbstub_handler ( signo, regs );
152         gdbmach_enable_hwbps();
153 }
154
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 */
171 };
172
173 void gdbmach_init ( void ) {
174         unsigned int i;
175
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] );
179         }
180 }