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_ppc64.h" 38 39 static struct reg regs[] = { 40 { "r0", 0, 64 }, 41 { "r1", 64, 64 }, 42 { "r2", 128, 64 }, 43 { "r3", 192, 64 }, 44 { "r4", 256, 64 }, 45 { "r5", 320, 64 }, 46 { "r6", 384, 64 }, 47 { "r7", 448, 64 }, 48 { "r8", 512, 64 }, 49 { "r9", 576, 64 }, 50 { "r10", 640, 64 }, 51 { "r11", 704, 64 }, 52 { "r12", 768, 64 }, 53 { "r13", 832, 64 }, 54 { "r14", 896, 64 }, 55 { "r15", 960, 64 }, 56 { "r16", 1024, 64 }, 57 { "r17", 1088, 64 }, 58 { "r18", 1152, 64 }, 59 { "r19", 1216, 64 }, 60 { "r20", 1280, 64 }, 61 { "r21", 1344, 64 }, 62 { "r22", 1408, 64 }, 63 { "r23", 1472, 64 }, 64 { "r24", 1536, 64 }, 65 { "r25", 1600, 64 }, 66 { "r26", 1664, 64 }, 67 { "r27", 1728, 64 }, 68 { "r28", 1792, 64 }, 69 { "r29", 1856, 64 }, 70 { "r30", 1920, 64 }, 71 { "r31", 1984, 64 }, 72 { "f0", 2048, 64 }, 73 { "f1", 2112, 64 }, 74 { "f2", 2176, 64 }, 75 { "f3", 2240, 64 }, 76 { "f4", 2304, 64 }, 77 { "f5", 2368, 64 }, 78 { "f6", 2432, 64 }, 79 { "f7", 2496, 64 }, 80 { "f8", 2560, 64 }, 81 { "f9", 2624, 64 }, 82 { "f10", 2688, 64 }, 83 { "f11", 2752, 64 }, 84 { "f12", 2816, 64 }, 85 { "f13", 2880, 64 }, 86 { "f14", 2944, 64 }, 87 { "f15", 3008, 64 }, 88 { "f16", 3072, 64 }, 89 { "f17", 3136, 64 }, 90 { "f18", 3200, 64 }, 91 { "f19", 3264, 64 }, 92 { "f20", 3328, 64 }, 93 { "f21", 3392, 64 }, 94 { "f22", 3456, 64 }, 95 { "f23", 3520, 64 }, 96 { "f24", 3584, 64 }, 97 { "f25", 3648, 64 }, 98 { "f26", 3712, 64 }, 99 { "f27", 3776, 64 }, 100 { "f28", 3840, 64 }, 101 { "f29", 3904, 64 }, 102 { "f30", 3968, 64 }, 103 { "f31", 4032, 64 }, 104 { "pc", 4096, 64 }, 105 { "msr", 4160, 64 }, 106 { "cr", 4224, 32 }, 107 { "lr", 4256, 64 }, 108 { "ctr", 4320, 64 }, 109 { "xer", 4384, 32 }, 110 { "fpscr", 4416, 32 }, 111 { "orig_r3", 4448, 64 }, 112 { "trap", 4512, 64 }, 113 { "vr0", 4576, 128 }, 114 { "vr1", 4704, 128 }, 115 { "vr2", 4832, 128 }, 116 { "vr3", 4960, 128 }, 117 { "vr4", 5088, 128 }, 118 { "vr5", 5216, 128 }, 119 { "vr6", 5344, 128 }, 120 { "vr7", 5472, 128 }, 121 { "vr8", 5600, 128 }, 122 { "vr9", 5728, 128 }, 123 { "vr10", 5856, 128 }, 124 { "vr11", 5984, 128 }, 125 { "vr12", 6112, 128 }, 126 { "vr13", 6240, 128 }, 127 { "vr14", 6368, 128 }, 128 { "vr15", 6496, 128 }, 129 { "vr16", 6624, 128 }, 130 { "vr17", 6752, 128 }, 131 { "vr18", 6880, 128 }, 132 { "vr19", 7008, 128 }, 133 { "vr20", 7136, 128 }, 134 { "vr21", 7264, 128 }, 135 { "vr22", 7392, 128 }, 136 { "vr23", 7520, 128 }, 137 { "vr24", 7648, 128 }, 138 { "vr25", 7776, 128 }, 139 { "vr26", 7904, 128 }, 140 { "vr27", 8032, 128 }, 141 { "vr28", 8160, 128 }, 142 { "vr29", 8288, 128 }, 143 { "vr30", 8416, 128 }, 144 { "vr31", 8544, 128 }, 145 { "vscr", 8672, 32 }, 146 { "vrsave", 8704, 32 }, 147 }; 148 static const char *expedite_regs[] = { "r1", "pc", 0 }; 149 #define num_regs (sizeof (regs) / sizeof (regs[0])) 150 151 static 152 CORE_ADDR get_pc (void) 153 { 154 unsigned long pc; 155 156 collect_register_by_name ("pc", &pc); 157 158 dlog(1, "stop pc is %p\n", (void *) pc); 159 return pc; 160 } 161 162 static 163 void set_pc (CORE_ADDR newpc) 164 { 165 Bool mod; 166 supply_register_by_name ("pc", &newpc, &mod); 167 if (mod) 168 dlog(1, "set pc to %p\n", C2v (newpc)); 169 else 170 dlog(1, "set pc not changed %p\n", C2v (newpc)); 171 } 172 173 /* store registers in the guest state (gdbserver_to_valgrind) 174 or fetch register from the guest state (valgrind_to_gdbserver). */ 175 static 176 void transfer_register (ThreadId tid, int abs_regno, void * buf, 177 transfer_direction dir, int size, Bool *mod) 178 { 179 ThreadState* tst = VG_(get_ThreadState)(tid); 180 int set = abs_regno / num_regs; 181 int regno = abs_regno % num_regs; 182 *mod = False; 183 184 VexGuestPPC64State* ppc64 = (VexGuestPPC64State*) get_arch (set, tst); 185 186 switch (regno) { 187 // numbers here have to match the order of regs above 188 // Attention: gdb order does not match valgrind order. 189 case 0: VG_(transfer) (&ppc64->guest_GPR0, buf, dir, size, mod); break; 190 case 1: VG_(transfer) (&ppc64->guest_GPR1, buf, dir, size, mod); break; 191 case 2: VG_(transfer) (&ppc64->guest_GPR2, buf, dir, size, mod); break; 192 case 3: VG_(transfer) (&ppc64->guest_GPR3, buf, dir, size, mod); break; 193 case 4: VG_(transfer) (&ppc64->guest_GPR4, buf, dir, size, mod); break; 194 case 5: VG_(transfer) (&ppc64->guest_GPR5, buf, dir, size, mod); break; 195 case 6: VG_(transfer) (&ppc64->guest_GPR6, buf, dir, size, mod); break; 196 case 7: VG_(transfer) (&ppc64->guest_GPR7, buf, dir, size, mod); break; 197 case 8: VG_(transfer) (&ppc64->guest_GPR8, buf, dir, size, mod); break; 198 case 9: VG_(transfer) (&ppc64->guest_GPR9, buf, dir, size, mod); break; 199 case 10: VG_(transfer) (&ppc64->guest_GPR10, buf, dir, size, mod); break; 200 case 11: VG_(transfer) (&ppc64->guest_GPR11, buf, dir, size, mod); break; 201 case 12: VG_(transfer) (&ppc64->guest_GPR12, buf, dir, size, mod); break; 202 case 13: VG_(transfer) (&ppc64->guest_GPR13, buf, dir, size, mod); break; 203 case 14: VG_(transfer) (&ppc64->guest_GPR14, buf, dir, size, mod); break; 204 case 15: VG_(transfer) (&ppc64->guest_GPR15, buf, dir, size, mod); break; 205 case 16: VG_(transfer) (&ppc64->guest_GPR16, buf, dir, size, mod); break; 206 case 17: VG_(transfer) (&ppc64->guest_GPR17, buf, dir, size, mod); break; 207 case 18: VG_(transfer) (&ppc64->guest_GPR18, buf, dir, size, mod); break; 208 case 19: VG_(transfer) (&ppc64->guest_GPR19, buf, dir, size, mod); break; 209 case 20: VG_(transfer) (&ppc64->guest_GPR20, buf, dir, size, mod); break; 210 case 21: VG_(transfer) (&ppc64->guest_GPR21, buf, dir, size, mod); break; 211 case 22: VG_(transfer) (&ppc64->guest_GPR22, buf, dir, size, mod); break; 212 case 23: VG_(transfer) (&ppc64->guest_GPR23, buf, dir, size, mod); break; 213 case 24: VG_(transfer) (&ppc64->guest_GPR24, buf, dir, size, mod); break; 214 case 25: VG_(transfer) (&ppc64->guest_GPR25, buf, dir, size, mod); break; 215 case 26: VG_(transfer) (&ppc64->guest_GPR26, buf, dir, size, mod); break; 216 case 27: VG_(transfer) (&ppc64->guest_GPR27, buf, dir, size, mod); break; 217 case 28: VG_(transfer) (&ppc64->guest_GPR28, buf, dir, size, mod); break; 218 case 29: VG_(transfer) (&ppc64->guest_GPR29, buf, dir, size, mod); break; 219 case 30: VG_(transfer) (&ppc64->guest_GPR30, buf, dir, size, mod); break; 220 case 31: VG_(transfer) (&ppc64->guest_GPR31, buf, dir, size, mod); break; 221 case 32: VG_(transfer) (&ppc64->guest_VSR0, buf, dir, size, mod); break; 222 case 33: VG_(transfer) (&ppc64->guest_VSR1, buf, dir, size, mod); break; 223 case 34: VG_(transfer) (&ppc64->guest_VSR2, buf, dir, size, mod); break; 224 case 35: VG_(transfer) (&ppc64->guest_VSR3, buf, dir, size, mod); break; 225 case 36: VG_(transfer) (&ppc64->guest_VSR4, buf, dir, size, mod); break; 226 case 37: VG_(transfer) (&ppc64->guest_VSR5, buf, dir, size, mod); break; 227 case 38: VG_(transfer) (&ppc64->guest_VSR6, buf, dir, size, mod); break; 228 case 39: VG_(transfer) (&ppc64->guest_VSR7, buf, dir, size, mod); break; 229 case 40: VG_(transfer) (&ppc64->guest_VSR8, buf, dir, size, mod); break; 230 case 41: VG_(transfer) (&ppc64->guest_VSR9, buf, dir, size, mod); break; 231 case 42: VG_(transfer) (&ppc64->guest_VSR10, buf, dir, size, mod); break; 232 case 43: VG_(transfer) (&ppc64->guest_VSR11, buf, dir, size, mod); break; 233 case 44: VG_(transfer) (&ppc64->guest_VSR12, buf, dir, size, mod); break; 234 case 45: VG_(transfer) (&ppc64->guest_VSR13, buf, dir, size, mod); break; 235 case 46: VG_(transfer) (&ppc64->guest_VSR14, buf, dir, size, mod); break; 236 case 47: VG_(transfer) (&ppc64->guest_VSR15, buf, dir, size, mod); break; 237 case 48: VG_(transfer) (&ppc64->guest_VSR16, buf, dir, size, mod); break; 238 case 49: VG_(transfer) (&ppc64->guest_VSR17, buf, dir, size, mod); break; 239 case 50: VG_(transfer) (&ppc64->guest_VSR18, buf, dir, size, mod); break; 240 case 51: VG_(transfer) (&ppc64->guest_VSR19, buf, dir, size, mod); break; 241 case 52: VG_(transfer) (&ppc64->guest_VSR20, buf, dir, size, mod); break; 242 case 53: VG_(transfer) (&ppc64->guest_VSR21, buf, dir, size, mod); break; 243 case 54: VG_(transfer) (&ppc64->guest_VSR22, buf, dir, size, mod); break; 244 case 55: VG_(transfer) (&ppc64->guest_VSR23, buf, dir, size, mod); break; 245 case 56: VG_(transfer) (&ppc64->guest_VSR24, buf, dir, size, mod); break; 246 case 57: VG_(transfer) (&ppc64->guest_VSR25, buf, dir, size, mod); break; 247 case 58: VG_(transfer) (&ppc64->guest_VSR26, buf, dir, size, mod); break; 248 case 59: VG_(transfer) (&ppc64->guest_VSR27, buf, dir, size, mod); break; 249 case 60: VG_(transfer) (&ppc64->guest_VSR28, buf, dir, size, mod); break; 250 case 61: VG_(transfer) (&ppc64->guest_VSR29, buf, dir, size, mod); break; 251 case 62: VG_(transfer) (&ppc64->guest_VSR30, buf, dir, size, mod); break; 252 case 63: VG_(transfer) (&ppc64->guest_VSR31, buf, dir, size, mod); break; 253 case 64: VG_(transfer) (&ppc64->guest_CIA, buf, dir, size, mod); break; 254 case 65: *mod = False; break; // VEX does not model Machine State Register 255 case 66: { 256 UInt cr = LibVEX_GuestPPC64_get_CR (ppc64); 257 if (dir == valgrind_to_gdbserver) { 258 VG_(transfer) (&cr, buf, dir, size, mod); 259 } else { 260 UInt newcr; 261 VG_(transfer) (&newcr, buf, dir, size, mod); 262 *mod = newcr != cr; 263 LibVEX_GuestPPC64_put_CR (newcr, ppc64); 264 } 265 break; 266 } 267 case 67: VG_(transfer) (&ppc64->guest_LR, buf, dir, size, mod); break; 268 case 68: VG_(transfer) (&ppc64->guest_CTR, buf, dir, size, mod); break; 269 case 69: { 270 UInt xer = LibVEX_GuestPPC64_get_XER (ppc64); 271 if (dir == valgrind_to_gdbserver) { 272 VG_(transfer) (&xer, buf, dir, size, mod); 273 } else { 274 UInt newxer; 275 VG_(transfer) (&newxer, buf, dir, size, mod); 276 *mod = newxer != xer; 277 LibVEX_GuestPPC64_put_XER (newxer, ppc64); 278 } 279 break; 280 } 281 case 70: VG_(transfer) (&ppc64->guest_FPROUND, buf, dir, size, mod); break; 282 case 71: *mod = False; break; // GDBTD???? VEX { "orig_r3", 4448, 64 }, 283 case 72: *mod = False; break; // GDBTD???? VEX { "trap", 4512, 64 }, 284 case 73: VG_(transfer) (&ppc64->guest_VSR32, buf, dir, size, mod); break; 285 case 74: VG_(transfer) (&ppc64->guest_VSR33, buf, dir, size, mod); break; 286 case 75: VG_(transfer) (&ppc64->guest_VSR34, buf, dir, size, mod); break; 287 case 76: VG_(transfer) (&ppc64->guest_VSR35, buf, dir, size, mod); break; 288 case 77: VG_(transfer) (&ppc64->guest_VSR36, buf, dir, size, mod); break; 289 case 78: VG_(transfer) (&ppc64->guest_VSR37, buf, dir, size, mod); break; 290 case 79: VG_(transfer) (&ppc64->guest_VSR38, buf, dir, size, mod); break; 291 case 80: VG_(transfer) (&ppc64->guest_VSR39, buf, dir, size, mod); break; 292 case 81: VG_(transfer) (&ppc64->guest_VSR40, buf, dir, size, mod); break; 293 case 82: VG_(transfer) (&ppc64->guest_VSR41, buf, dir, size, mod); break; 294 case 83: VG_(transfer) (&ppc64->guest_VSR42, buf, dir, size, mod); break; 295 case 84: VG_(transfer) (&ppc64->guest_VSR43, buf, dir, size, mod); break; 296 case 85: VG_(transfer) (&ppc64->guest_VSR44, buf, dir, size, mod); break; 297 case 86: VG_(transfer) (&ppc64->guest_VSR45, buf, dir, size, mod); break; 298 case 87: VG_(transfer) (&ppc64->guest_VSR46, buf, dir, size, mod); break; 299 case 88: VG_(transfer) (&ppc64->guest_VSR47, buf, dir, size, mod); break; 300 case 89: VG_(transfer) (&ppc64->guest_VSR48, buf, dir, size, mod); break; 301 case 90: VG_(transfer) (&ppc64->guest_VSR49, buf, dir, size, mod); break; 302 case 91: VG_(transfer) (&ppc64->guest_VSR50, buf, dir, size, mod); break; 303 case 92: VG_(transfer) (&ppc64->guest_VSR51, buf, dir, size, mod); break; 304 case 93: VG_(transfer) (&ppc64->guest_VSR52, buf, dir, size, mod); break; 305 case 94: VG_(transfer) (&ppc64->guest_VSR53, buf, dir, size, mod); break; 306 case 95: VG_(transfer) (&ppc64->guest_VSR54, buf, dir, size, mod); break; 307 case 96: VG_(transfer) (&ppc64->guest_VSR55, buf, dir, size, mod); break; 308 case 97: VG_(transfer) (&ppc64->guest_VSR56, buf, dir, size, mod); break; 309 case 98: VG_(transfer) (&ppc64->guest_VSR57, buf, dir, size, mod); break; 310 case 99: VG_(transfer) (&ppc64->guest_VSR58, buf, dir, size, mod); break; 311 case 100: VG_(transfer) (&ppc64->guest_VSR59, buf, dir, size, mod); break; 312 case 101: VG_(transfer) (&ppc64->guest_VSR60, buf, dir, size, mod); break; 313 case 102: VG_(transfer) (&ppc64->guest_VSR61, buf, dir, size, mod); break; 314 case 103: VG_(transfer) (&ppc64->guest_VSR62, buf, dir, size, mod); break; 315 case 104: VG_(transfer) (&ppc64->guest_VSR63, buf, dir, size, mod); break; 316 case 105: VG_(transfer) (&ppc64->guest_VSCR, buf, dir, size, mod); break; 317 case 106: VG_(transfer) (&ppc64->guest_VRSAVE, buf, dir, size, mod); break; 318 default: vg_assert(0); 319 } 320 } 321 322 static 323 char* target_xml (Bool shadow_mode) 324 { 325 if (shadow_mode) { 326 return "powerpc-altivec64l-valgrind.xml"; 327 } else { 328 return "powerpc-altivec64l.xml"; 329 } 330 } 331 332 static struct valgrind_target_ops low_target = { 333 num_regs, 334 regs, 335 1, //r1 336 transfer_register, 337 get_pc, 338 set_pc, 339 "ppc64", 340 target_xml 341 }; 342 343 void ppc64_init_architecture (struct valgrind_target_ops *target) 344 { 345 *target = low_target; 346 set_register_cache (regs, num_regs); 347 gdbserver_expedite_regs = expedite_regs; 348 } 349