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