1 /* Low level interface to valgrind, for the remote server for GDB integrated
4 Free Software Foundation, Inc.
6 This file is part of VALGRIND.
7 It has been inspired from a file from gdbserver in gdb 6.6.
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.
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.
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. */
29 #include "pub_core_machine.h"
30 #include "pub_core_threadstate.h"
31 #include "pub_core_transtab.h"
32 #include "pub_core_gdbserver.h"
34 #include "valgrind_low.h"
36 #include "libvex_guest_amd64.h"
37 /* GDBTD: ??? have a cleaner way to get the f80 <> f64 conversion functions */
38 /* below include needed for conversion f80 <> f64 */
39 #include "../../VEX/priv/guest_generic_x87.h"
41 /* below loosely inspired from file generated with gdb regdat.sh */
43 static struct reg regs
[] = {
61 { "eflags", 1088, 32 },
76 { "fctrl", 1952, 32 },
77 { "fstat", 1984, 32 },
79 { "fiseg", 2048, 32 },
80 { "fioff", 2080, 32 },
81 { "foseg", 2112, 32 },
82 { "fooff", 2144, 32 },
84 { "xmm0", 2208, 128 },
85 { "xmm1", 2336, 128 },
86 { "xmm2", 2464, 128 },
87 { "xmm3", 2592, 128 },
88 { "xmm4", 2720, 128 },
89 { "xmm5", 2848, 128 },
90 { "xmm6", 2976, 128 },
91 { "xmm7", 3104, 128 },
92 { "xmm8", 3232, 128 },
93 { "xmm9", 3360, 128 },
94 { "xmm10", 3488, 128 },
95 { "xmm11", 3616, 128 },
96 { "xmm12", 3744, 128 },
97 { "xmm13", 3872, 128 },
98 { "xmm14", 4000, 128 },
99 { "xmm15", 4128, 128 },
100 { "mxcsr", 4256, 32 },
101 #if defined(VGO_linux)
102 { "orig_rax", 4288, 64 },
104 { "ymm0h", 4352, 128 }, // The ymm?h registers only to be given to GDB
105 { "ymm1h", 4480, 128 }, // if Valgrind is running with AVX instructions.
106 { "ymm2h", 4608, 128 },
107 { "ymm3h", 4736, 128 },
108 { "ymm4h", 4864, 128 },
109 { "ymm5h", 4992, 128 },
110 { "ymm6h", 5120, 128 },
111 { "ymm7h", 5248, 128 },
112 { "ymm8h", 5376, 128 },
113 { "ymm9h", 5504, 128 },
114 { "ymm10h", 5632, 128 },
115 { "ymm11h", 5760, 128 },
116 { "ymm12h", 5888, 128 },
117 { "ymm13h", 6016, 128 },
118 { "ymm14h", 6144, 128 },
119 { "ymm15h", 6272, 128 }
121 static const char *expedite_regs
[] = { "rbp", "rsp", "rip", 0 };
122 #define max_num_regs (sizeof (regs) / sizeof (regs[0]))
123 static int dyn_num_regs
; // if no AVX, we have to give less registers to gdb.
127 CORE_ADDR
get_pc (void)
131 collect_register_by_name ("rip", &pc
);
133 dlog(1, "stop pc is %p\n", (void *) pc
);
138 void set_pc (CORE_ADDR newpc
)
140 supply_register_by_name ("rip", &newpc
);
143 /* store registers in the guest state (gdbserver_to_valgrind)
144 or fetch register from the guest state (valgrind_to_gdbserver). */
146 void transfer_register (ThreadId tid
, int abs_regno
, void * buf
,
147 transfer_direction dir
, int size
, Bool
*mod
)
149 ThreadState
* tst
= VG_(get_ThreadState
)(tid
);
150 int set
= abs_regno
/ dyn_num_regs
;
151 int regno
= abs_regno
% dyn_num_regs
;
154 VexGuestAMD64State
* amd64
= (VexGuestAMD64State
*) get_arch (set
, tst
);
157 // numbers here have to match the order of regs above.
158 // Attention: gdb order does not match valgrind order.
159 case 0: VG_(transfer
) (&amd64
->guest_RAX
, buf
, dir
, size
, mod
); break;
160 case 1: VG_(transfer
) (&amd64
->guest_RBX
, buf
, dir
, size
, mod
); break;
161 case 2: VG_(transfer
) (&amd64
->guest_RCX
, buf
, dir
, size
, mod
); break;
162 case 3: VG_(transfer
) (&amd64
->guest_RDX
, buf
, dir
, size
, mod
); break;
163 case 4: VG_(transfer
) (&amd64
->guest_RSI
, buf
, dir
, size
, mod
); break;
164 case 5: VG_(transfer
) (&amd64
->guest_RDI
, buf
, dir
, size
, mod
); break;
165 case 6: VG_(transfer
) (&amd64
->guest_RBP
, buf
, dir
, size
, mod
); break;
166 case 7: VG_(transfer
) (&amd64
->guest_RSP
, buf
, dir
, size
, mod
); break;
167 case 8: VG_(transfer
) (&amd64
->guest_R8
, buf
, dir
, size
, mod
); break;
168 case 9: VG_(transfer
) (&amd64
->guest_R9
, buf
, dir
, size
, mod
); break;
169 case 10: VG_(transfer
) (&amd64
->guest_R10
, buf
, dir
, size
, mod
); break;
170 case 11: VG_(transfer
) (&amd64
->guest_R11
, buf
, dir
, size
, mod
); break;
171 case 12: VG_(transfer
) (&amd64
->guest_R12
, buf
, dir
, size
, mod
); break;
172 case 13: VG_(transfer
) (&amd64
->guest_R13
, buf
, dir
, size
, mod
); break;
173 case 14: VG_(transfer
) (&amd64
->guest_R14
, buf
, dir
, size
, mod
); break;
174 case 15: VG_(transfer
) (&amd64
->guest_R15
, buf
, dir
, size
, mod
); break;
175 case 16: VG_(transfer
) (&amd64
->guest_RIP
, buf
, dir
, size
, mod
); break;
177 if (dir
== valgrind_to_gdbserver
) {
179 /* we can only retrieve the real flags (set 0)
180 retrieving shadow flags is not ok */
182 rflags
= LibVEX_GuestAMD64_get_rflags (amd64
);
185 VG_(transfer
) (&rflags
, buf
, dir
, size
, mod
);
187 *mod
= False
; //GDBTD? how do we store rflags in libvex_guest_amd64.h ???
190 case 18: *mod
= False
; break; //GDBTD VG_(transfer) (&amd64->guest_CS, buf, dir, size, mod);
191 case 19: *mod
= False
; break; //GDBTD VG_(transfer) (&amd64->guest_SS, buf, dir, size, mod);
192 case 20: *mod
= False
; break; //GDBTD VG_(transfer) (&amd64->guest_DS, buf, dir, size, mod);
193 case 21: *mod
= False
; break; //GDBTD VG_(transfer) (&amd64->guest_ES, buf, dir, size, mod);
194 case 22: *mod
= False
; break; //GDBTD VG_(transfer) (&amd64->guest_FS, buf, dir, size, mod);
195 case 23: VG_(transfer
) (&amd64
->guest_GS_CONST
, buf
, dir
, size
, mod
); break;
199 case 27: /* register 24 to 31 are float registers 80 bits but 64 bits in valgrind */
204 if (dir
== valgrind_to_gdbserver
) {
206 convert_f64le_to_f80le ((UChar
*)&amd64
->guest_FPREG
[regno
-24],
208 VG_(transfer
) (&fpreg80
, buf
, dir
, sizeof(fpreg80
), mod
);
211 convert_f80le_to_f64le (buf
, (UChar
*)&fpreg64
);
212 VG_(transfer
) (&amd64
->guest_FPREG
[regno
-24], &fpreg64
,
213 dir
, sizeof(fpreg64
), mod
);
217 if (dir
== valgrind_to_gdbserver
) {
218 // vex only models the rounding bits (see libvex_guest_amd64.h)
219 UWord value
= 0x037f;
220 value
|= amd64
->guest_FPROUND
<< 10;
221 VG_(transfer
)(&value
, buf
, dir
, size
, mod
);
223 *mod
= False
; // GDBTD???? VEX equivalent fcrtl
227 if (dir
== valgrind_to_gdbserver
) {
228 UWord value
= amd64
->guest_FC3210
;
229 value
|= (amd64
->guest_FTOP
& 7) << 11;
230 VG_(transfer
)(&value
, buf
, dir
, size
, mod
);
232 *mod
= False
; // GDBTD???? VEX equivalent fstat
236 if (dir
== valgrind_to_gdbserver
) {
237 // vex doesn't model these precisely
239 ((amd64
->guest_FPTAG
[0] ? 0 : 3) << 0) |
240 ((amd64
->guest_FPTAG
[1] ? 0 : 3) << 2) |
241 ((amd64
->guest_FPTAG
[2] ? 0 : 3) << 4) |
242 ((amd64
->guest_FPTAG
[3] ? 0 : 3) << 6) |
243 ((amd64
->guest_FPTAG
[4] ? 0 : 3) << 8) |
244 ((amd64
->guest_FPTAG
[5] ? 0 : 3) << 10) |
245 ((amd64
->guest_FPTAG
[6] ? 0 : 3) << 12) |
246 ((amd64
->guest_FPTAG
[7] ? 0 : 3) << 14);
247 VG_(transfer
)(&value
, buf
, dir
, size
, mod
);
249 *mod
= False
; // GDBTD???? VEX equivalent ftag
252 case 35: *mod
= False
; break; // GDBTD ??? equivalent of fiseg
253 case 36: *mod
= False
; break; // GDBTD ??? equivalent of fioff
254 case 37: *mod
= False
; break; // GDBTD ??? equivalent of foseg
255 case 38: *mod
= False
; break; // GDBTD ??? equivalent of fooff
256 case 39: *mod
= False
; break; // GDBTD ??? equivalent of fop
257 case 40: VG_(transfer
) (&amd64
->guest_YMM0
[0], buf
, dir
, size
, mod
); break;
258 case 41: VG_(transfer
) (&amd64
->guest_YMM1
[0], buf
, dir
, size
, mod
); break;
259 case 42: VG_(transfer
) (&amd64
->guest_YMM2
[0], buf
, dir
, size
, mod
); break;
260 case 43: VG_(transfer
) (&amd64
->guest_YMM3
[0], buf
, dir
, size
, mod
); break;
261 case 44: VG_(transfer
) (&amd64
->guest_YMM4
[0], buf
, dir
, size
, mod
); break;
262 case 45: VG_(transfer
) (&amd64
->guest_YMM5
[0], buf
, dir
, size
, mod
); break;
263 case 46: VG_(transfer
) (&amd64
->guest_YMM6
[0], buf
, dir
, size
, mod
); break;
264 case 47: VG_(transfer
) (&amd64
->guest_YMM7
[0], buf
, dir
, size
, mod
); break;
265 case 48: VG_(transfer
) (&amd64
->guest_YMM8
[0], buf
, dir
, size
, mod
); break;
266 case 49: VG_(transfer
) (&amd64
->guest_YMM9
[0], buf
, dir
, size
, mod
); break;
267 case 50: VG_(transfer
) (&amd64
->guest_YMM10
[0], buf
, dir
, size
, mod
); break;
268 case 51: VG_(transfer
) (&amd64
->guest_YMM11
[0], buf
, dir
, size
, mod
); break;
269 case 52: VG_(transfer
) (&amd64
->guest_YMM12
[0], buf
, dir
, size
, mod
); break;
270 case 53: VG_(transfer
) (&amd64
->guest_YMM13
[0], buf
, dir
, size
, mod
); break;
271 case 54: VG_(transfer
) (&amd64
->guest_YMM14
[0], buf
, dir
, size
, mod
); break;
272 case 55: VG_(transfer
) (&amd64
->guest_YMM15
[0], buf
, dir
, size
, mod
); break;
274 if (dir
== valgrind_to_gdbserver
) {
275 // vex only models the rounding bits (see libvex_guest_x86.h)
276 UWord value
= 0x1f80;
277 value
|= amd64
->guest_SSEROUND
<< 13;
278 VG_(transfer
)(&value
, buf
, dir
, size
, mod
);
280 *mod
= False
; // GDBTD???? VEX equivalent mxcsr
283 case 57: *mod
= False
; break; // GDBTD???? VEX equivalent { "orig_rax"},
284 case 58: VG_(transfer
) (&amd64
->guest_YMM0
[4], buf
, dir
, size
, mod
); break;
285 case 59: VG_(transfer
) (&amd64
->guest_YMM1
[4], buf
, dir
, size
, mod
); break;
286 case 60: VG_(transfer
) (&amd64
->guest_YMM2
[4], buf
, dir
, size
, mod
); break;
287 case 61: VG_(transfer
) (&amd64
->guest_YMM3
[4], buf
, dir
, size
, mod
); break;
288 case 62: VG_(transfer
) (&amd64
->guest_YMM4
[4], buf
, dir
, size
, mod
); break;
289 case 63: VG_(transfer
) (&amd64
->guest_YMM5
[4], buf
, dir
, size
, mod
); break;
290 case 64: VG_(transfer
) (&amd64
->guest_YMM6
[4], buf
, dir
, size
, mod
); break;
291 case 65: VG_(transfer
) (&amd64
->guest_YMM7
[4], buf
, dir
, size
, mod
); break;
292 case 66: VG_(transfer
) (&amd64
->guest_YMM8
[4], buf
, dir
, size
, mod
); break;
293 case 67: VG_(transfer
) (&amd64
->guest_YMM9
[4], buf
, dir
, size
, mod
); break;
294 case 68: VG_(transfer
) (&amd64
->guest_YMM10
[4], buf
, dir
, size
, mod
); break;
295 case 69: VG_(transfer
) (&amd64
->guest_YMM11
[4], buf
, dir
, size
, mod
); break;
296 case 70: VG_(transfer
) (&amd64
->guest_YMM12
[4], buf
, dir
, size
, mod
); break;
297 case 71: VG_(transfer
) (&amd64
->guest_YMM13
[4], buf
, dir
, size
, mod
); break;
298 case 72: VG_(transfer
) (&amd64
->guest_YMM14
[4], buf
, dir
, size
, mod
); break;
299 case 73: VG_(transfer
) (&amd64
->guest_YMM15
[4], buf
, dir
, size
, mod
); break;
300 default: vg_assert(0);
309 VG_(machine_get_VexArchInfo
) (&va
, &vai
);
310 return (vai
.hwcaps
& VEX_HWCAPS_AMD64_AVX
? True
: False
);
314 const char* target_xml (Bool shadow_mode
)
317 #if defined(VGO_linux)
319 return "amd64-avx-linux-valgrind.xml";
321 return "amd64-linux-valgrind.xml";
324 return "amd64-avx-coresse-valgrind.xml";
326 return "amd64-coresse-valgrind.xml";
329 #if defined(VGO_linux)
331 return "amd64-avx-linux.xml";
336 return "amd64-avx-coresse.xml";
343 static CORE_ADDR
** target_get_dtv (ThreadState
*tst
)
345 VexGuestAMD64State
* amd64
= (VexGuestAMD64State
*)&tst
->arch
.vex
;
346 return (CORE_ADDR
**)((CORE_ADDR
)amd64
->guest_FS_CONST
+ 0x8);
349 static struct valgrind_target_ops low_target
= {
350 -1, // Must be computed at init time.
361 void amd64_init_architecture (struct valgrind_target_ops
*target
)
363 *target
= low_target
;
365 dyn_num_regs
= max_num_regs
;
367 dyn_num_regs
= max_num_regs
- 16; // remove the AVX "high" registers.
368 target
->num_regs
= dyn_num_regs
;
369 set_register_cache (regs
, dyn_num_regs
);
370 gdbserver_expedite_regs
= expedite_regs
;