1 /* Low level interface to valgrind, for the remote server for GDB integrated
2 in valgrind.
3 Copyright (C) 2011
4 Free Software Foundation, Inc.
5
6 This file is part of VALGRIND.
7 It has been inspired from a file from gdbserver in gdb 6.6.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24 #include "server.h"
25 #include "target.h"
26 #include "regdef.h"
27 #include "regcache.h"
28
29 #include "pub_core_aspacemgr.h"
30 #include "pub_tool_machine.h"
31 #include "pub_core_threadstate.h"
32 #include "pub_core_transtab.h"
33 #include "pub_core_gdbserver.h"
34
35 #include "valgrind_low.h"
36
37 #include "libvex_guest_x86.h"
38 /* GDBTD: ??? have a cleaner way to get the f80 <> f64 conversion functions */
39 /* below include needed for conversion f80 <> f64 */
40 #include "../../VEX/priv/guest_generic_x87.h"
41
42
43 /* below loosely inspired from file generated with gdb regdat.sh */
44
45 static struct reg regs[] = {
46 { "eax", 0, 32 },
47 { "ecx", 32, 32 },
48 { "edx", 64, 32 },
49 { "ebx", 96, 32 },
50 { "esp", 128, 32 },
51 { "ebp", 160, 32 },
52 { "esi", 192, 32 },
53 { "edi", 224, 32 },
54 { "eip", 256, 32 },
55 { "eflags", 288, 32 },
56 { "cs", 320, 32 },
57 { "ss", 352, 32 },
58 { "ds", 384, 32 },
59 { "es", 416, 32 },
60 { "fs", 448, 32 },
61 { "gs", 480, 32 },
62 { "st0", 512, 80 },
63 { "st1", 592, 80 },
64 { "st2", 672, 80 },
65 { "st3", 752, 80 },
66 { "st4", 832, 80 },
67 { "st5", 912, 80 },
68 { "st6", 992, 80 },
69 { "st7", 1072, 80 },
70 { "fctrl", 1152, 32 },
71 { "fstat", 1184, 32 },
72 { "ftag", 1216, 32 },
73 { "fiseg", 1248, 32 },
74 { "fioff", 1280, 32 },
75 { "foseg", 1312, 32 },
76 { "fooff", 1344, 32 },
77 { "fop", 1376, 32 },
78 { "xmm0", 1408, 128 },
79 { "xmm1", 1536, 128 },
80 { "xmm2", 1664, 128 },
81 { "xmm3", 1792, 128 },
82 { "xmm4", 1920, 128 },
83 { "xmm5", 2048, 128 },
84 { "xmm6", 2176, 128 },
85 { "xmm7", 2304, 128 },
86 { "mxcsr", 2432, 32 },
87 #if defined(VGO_linux)
88 { "orig_eax", 2464, 32 }
89 #endif
90 };
91 static const char *expedite_regs[] = { "ebp", "esp", "eip", 0 };
92 #define num_regs (sizeof (regs) / sizeof (regs[0]))
93
94 static
get_pc(void)95 CORE_ADDR get_pc (void)
96 {
97 unsigned long pc;
98
99 collect_register_by_name ("eip", &pc);
100
101 dlog(1, "stop pc is %p\n", (void *) pc);
102 return pc;
103 }
104
105 static
set_pc(CORE_ADDR newpc)106 void set_pc (CORE_ADDR newpc)
107 {
108 Bool mod;
109 supply_register_by_name ("eip", &newpc, &mod);
110 if (mod)
111 dlog(1, "set pc to %p\n", C2v (newpc));
112 else
113 dlog(1, "set pc not changed %p\n", C2v (newpc));
114 }
115
116 /* store registers in the guest state (gdbserver_to_valgrind)
117 or fetch register from the guest state (valgrind_to_gdbserver). */
118 static
transfer_register(ThreadId tid,int abs_regno,void * buf,transfer_direction dir,int size,Bool * mod)119 void transfer_register (ThreadId tid, int abs_regno, void * buf,
120 transfer_direction dir, int size, Bool *mod)
121 {
122 ThreadState* tst = VG_(get_ThreadState)(tid);
123 int set = abs_regno / num_regs;
124 int regno = abs_regno % num_regs;
125 *mod = False;
126
127 VexGuestX86State* x86 = (VexGuestX86State*) get_arch (set, tst);
128
129 switch (regno) {
130 // numbers here have to match the order of regs above
131 // Attention: gdb order does not match valgrind order.
132 case 0: VG_(transfer) (&x86->guest_EAX, buf, dir, size, mod); break;
133 case 1: VG_(transfer) (&x86->guest_ECX, buf, dir, size, mod); break;
134 case 2: VG_(transfer) (&x86->guest_EDX, buf, dir, size, mod); break;
135 case 3: VG_(transfer) (&x86->guest_EBX, buf, dir, size, mod); break;
136 case 4: VG_(transfer) (&x86->guest_ESP, buf, dir, size, mod); break;
137 case 5: VG_(transfer) (&x86->guest_EBP, buf, dir, size, mod); break;
138 case 6: VG_(transfer) (&x86->guest_ESI, buf, dir, size, mod); break;
139 case 7: VG_(transfer) (&x86->guest_EDI, buf, dir, size, mod); break;
140 case 8:
141 VG_(transfer) (&x86->guest_EIP, buf, dir, size, mod);
142 if (*mod && VG_(debugLog_getLevel)() > 2) {
143 char bufimage [2*sizeof(x86->guest_IP_AT_SYSCALL) + 1];
144 heximage (bufimage,
145 (char *) &x86->guest_IP_AT_SYSCALL,
146 sizeof(x86->guest_IP_AT_SYSCALL));
147 dlog(3, "guest_IP_AT_SYSCALL %s\n", bufimage);
148 }
149 break;
150 case 9:
151 if (dir == valgrind_to_gdbserver) {
152 UInt eflags;
153 /* we can only retrieve the real flags (set 0)
154 retrieving shadow flags is not ok */
155 if (set == 0)
156 eflags = LibVEX_GuestX86_get_eflags (x86);
157 else
158 eflags = 0;
159 VG_(transfer) (&eflags, buf, dir, size, mod); break;
160 } else {
161 *mod = False; //GDBTD? how do we store eflags in libvex_guest_x86.h ???
162 }
163 break;
164 case 10: VG_(transfer) (&x86->guest_CS, buf, dir, size, mod); break;
165 case 11: VG_(transfer) (&x86->guest_SS, buf, dir, size, mod); break;
166 case 12: VG_(transfer) (&x86->guest_DS, buf, dir, size, mod); break;
167 case 13: VG_(transfer) (&x86->guest_ES, buf, dir, size, mod); break;
168 case 14: VG_(transfer) (&x86->guest_FS, buf, dir, size, mod); break;
169 case 15: VG_(transfer) (&x86->guest_GS, buf, dir, size, mod); break;
170 case 16:
171 case 17:
172 case 18:
173 case 19: /* register 16 to 23 are float registers 80 bits but 64 bits in valgrind */
174 case 20:
175 case 21:
176 case 22:
177 case 23: {
178 if (dir == valgrind_to_gdbserver) {
179 UChar fpreg80[10];
180 convert_f64le_to_f80le ((UChar *)&x86->guest_FPREG[regno-16],
181 fpreg80);
182 VG_(transfer) (&fpreg80, buf, dir, sizeof(fpreg80), mod);
183 } else {
184 ULong fpreg64;
185 convert_f80le_to_f64le (buf, (UChar *)&fpreg64);
186 VG_(transfer) (&x86->guest_FPREG[regno-16], &fpreg64,
187 dir, sizeof(fpreg64), mod);
188 }
189 break;
190 }
191 case 24:
192 if (dir == valgrind_to_gdbserver) {
193 // vex only models the rounding bits (see libvex_guest_x86.h)
194 UWord value = 0x037f;
195 value |= x86->guest_FPROUND << 10;
196 VG_(transfer)(&value, buf, dir, size, mod);
197 } else {
198 *mod = False; // GDBTD???? VEX { "fctrl", 1152, 32 },
199 }
200 break;
201 case 25:
202 if (dir == valgrind_to_gdbserver) {
203 UWord value = x86->guest_FC3210;
204 value |= (x86->guest_FTOP & 7) << 11;
205 VG_(transfer)(&value, buf, dir, size, mod);
206 } else {
207 *mod = False; // GDBTD???? VEX { "fstat", 1184, 32 },
208 }
209 break;
210 case 26:
211 if (dir == valgrind_to_gdbserver) {
212 // vex doesn't model these precisely
213 UWord value =
214 ((x86->guest_FPTAG[0] ? 0 : 3) << 0) |
215 ((x86->guest_FPTAG[1] ? 0 : 3) << 2) |
216 ((x86->guest_FPTAG[2] ? 0 : 3) << 4) |
217 ((x86->guest_FPTAG[3] ? 0 : 3) << 6) |
218 ((x86->guest_FPTAG[4] ? 0 : 3) << 8) |
219 ((x86->guest_FPTAG[5] ? 0 : 3) << 10) |
220 ((x86->guest_FPTAG[6] ? 0 : 3) << 12) |
221 ((x86->guest_FPTAG[7] ? 0 : 3) << 14);
222 VG_(transfer)(&value, buf, dir, size, mod);
223 } else {
224 *mod = False; // GDBTD???? VEX { "ftag", 1216, 32 },
225 }
226 break;
227 case 27: *mod = False; break; // GDBTD???? VEX { "fiseg", 1248, 32 },
228 case 28: *mod = False; break; // GDBTD???? VEX { "fioff", 1280, 32 },
229 case 29: *mod = False; break; // GDBTD???? VEX { "foseg", 1312, 32 },
230 case 30: *mod = False; break; // GDBTD???? VEX { "fooff", 1344, 32 },
231 case 31: *mod = False; break; // GDBTD???? VEX { "fop", 1376, 32 },
232 case 32: VG_(transfer) (&x86->guest_XMM0, buf, dir, size, mod); break;
233 case 33: VG_(transfer) (&x86->guest_XMM1, buf, dir, size, mod); break;
234 case 34: VG_(transfer) (&x86->guest_XMM2, buf, dir, size, mod); break;
235 case 35: VG_(transfer) (&x86->guest_XMM3, buf, dir, size, mod); break;
236 case 36: VG_(transfer) (&x86->guest_XMM4, buf, dir, size, mod); break;
237 case 37: VG_(transfer) (&x86->guest_XMM5, buf, dir, size, mod); break;
238 case 38: VG_(transfer) (&x86->guest_XMM6, buf, dir, size, mod); break;
239 case 39: VG_(transfer) (&x86->guest_XMM7, buf, dir, size, mod); break;
240 case 40:
241 if (dir == valgrind_to_gdbserver) {
242 // vex only models the rounding bits (see libvex_guest_x86.h)
243 UWord value = 0x1f80;
244 value |= x86->guest_SSEROUND << 13;
245 VG_(transfer)(&value, buf, dir, size, mod);
246 } else {
247 *mod = False; // GDBTD???? VEX { "mxcsr", 2432, 32 },
248 }
249 break;
250 case 41: *mod = False; break; // GDBTD???? VEX { "orig_eax", 2464, 32 },
251 default: vg_assert(0);
252 }
253 }
254
255 static struct valgrind_target_ops low_target = {
256 num_regs,
257 regs,
258 4, //ESP
259 transfer_register,
260 get_pc,
261 set_pc,
262 "i386",
263 NULL, // target_xml not needed.
264 #if defined(VGO_linux)
265 "i386-linux-valgrind.xml"
266 #else
267 "i386-coresse-valgrind.xml"
268 #endif
269 };
270
x86_init_architecture(struct valgrind_target_ops * target)271 void x86_init_architecture (struct valgrind_target_ops *target)
272 {
273 *target = low_target;
274 set_register_cache (regs, num_regs);
275 gdbserver_expedite_regs = expedite_regs;
276 }
277