• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_s390x.h"
37 
38 static struct reg regs[] = {
39   { "pswm", 0, 64 },
40   { "pswa", 64, 64 },
41   { "r0", 128, 64 },
42   { "r1", 192, 64 },
43   { "r2", 256, 64 },
44   { "r3", 320, 64 },
45   { "r4", 384, 64 },
46   { "r5", 448, 64 },
47   { "r6", 512, 64 },
48   { "r7", 576, 64 },
49   { "r8", 640, 64 },
50   { "r9", 704, 64 },
51   { "r10", 768, 64 },
52   { "r11", 832, 64 },
53   { "r12", 896, 64 },
54   { "r13", 960, 64 },
55   { "r14", 1024, 64 },
56   { "r15", 1088, 64 },
57   { "acr0", 1152, 32 },
58   { "acr1", 1184, 32 },
59   { "acr2", 1216, 32 },
60   { "acr3", 1248, 32 },
61   { "acr4", 1280, 32 },
62   { "acr5", 1312, 32 },
63   { "acr6", 1344, 32 },
64   { "acr7", 1376, 32 },
65   { "acr8", 1408, 32 },
66   { "acr9", 1440, 32 },
67   { "acr10", 1472, 32 },
68   { "acr11", 1504, 32 },
69   { "acr12", 1536, 32 },
70   { "acr13", 1568, 32 },
71   { "acr14", 1600, 32 },
72   { "acr15", 1632, 32 },
73   { "fpc", 1664, 32 },
74   { "f0", 1696, 64 },
75   { "f1", 1760, 64 },
76   { "f2", 1824, 64 },
77   { "f3", 1888, 64 },
78   { "f4", 1952, 64 },
79   { "f5", 2016, 64 },
80   { "f6", 2080, 64 },
81   { "f7", 2144, 64 },
82   { "f8", 2208, 64 },
83   { "f9", 2272, 64 },
84   { "f10", 2336, 64 },
85   { "f11", 2400, 64 },
86   { "f12", 2464, 64 },
87   { "f13", 2528, 64 },
88   { "f14", 2592, 64 },
89   { "f15", 2656, 64 },
90   { "orig_r2", 2720, 64 },
91 };
92 static const char *expedite_regs[] = { "r14", "r15", "pswa", 0 };
93 #define num_regs (sizeof (regs) / sizeof (regs[0]))
94 
95 static
get_pc(void)96 CORE_ADDR get_pc (void)
97 {
98    unsigned long pc;
99 
100    collect_register_by_name ("pswa", &pc);
101 
102    dlog(1, "stop pc is %p\n", (void *) pc);
103    return pc;
104 }
105 
106 static
set_pc(CORE_ADDR newpc)107 void set_pc (CORE_ADDR newpc)
108 {
109    Bool mod;
110    supply_register_by_name ("pswa", &newpc, &mod);
111    if (mod)
112       dlog(1, "set pc to %p\n", C2v (newpc));
113    else
114       dlog(1, "set pc not changed %p\n", C2v (newpc));
115 }
116 
117 /* store registers in the guest state (gdbserver_to_valgrind)
118    or fetch register from the guest state (valgrind_to_gdbserver). */
119 static
transfer_register(ThreadId tid,int abs_regno,void * buf,transfer_direction dir,int size,Bool * mod)120 void transfer_register (ThreadId tid, int abs_regno, void * buf,
121                         transfer_direction dir, int size, Bool *mod)
122 {
123    ThreadState* tst = VG_(get_ThreadState)(tid);
124    int set = abs_regno / num_regs;
125    int regno = abs_regno % num_regs;
126    *mod = False;
127 
128    VexGuestS390XState* s390x = (VexGuestS390XState*) get_arch (set, tst);
129 
130    switch (regno) {
131    // numbers here have to match the order of regs above
132    // Attention: gdb order does not match valgrind order.
133    case 0:  *mod = False; break; //GDBTD??? { "pswm", 0, 64 },
134    case 1:  VG_(transfer) (&s390x->guest_IA,  buf, dir, size, mod); break;
135    case 2:  VG_(transfer) (&s390x->guest_r0,  buf, dir, size, mod); break;
136    case 3:  VG_(transfer) (&s390x->guest_r1,  buf, dir, size, mod); break;
137    case 4:  VG_(transfer) (&s390x->guest_r2,  buf, dir, size, mod); break;
138    case 5:  VG_(transfer) (&s390x->guest_r3,  buf, dir, size, mod); break;
139    case 6:  VG_(transfer) (&s390x->guest_r4,  buf, dir, size, mod); break;
140    case 7:  VG_(transfer) (&s390x->guest_r5,  buf, dir, size, mod); break;
141    case 8:  VG_(transfer) (&s390x->guest_r6,  buf, dir, size, mod); break;
142    case 9:  VG_(transfer) (&s390x->guest_r7,  buf, dir, size, mod); break;
143    case 10: VG_(transfer) (&s390x->guest_r8,  buf, dir, size, mod); break;
144    case 11: VG_(transfer) (&s390x->guest_r9,  buf, dir, size, mod); break;
145    case 12: VG_(transfer) (&s390x->guest_r10, buf, dir, size, mod); break;
146    case 13: VG_(transfer) (&s390x->guest_r11, buf, dir, size, mod); break;
147    case 14: VG_(transfer) (&s390x->guest_r12, buf, dir, size, mod); break;
148    case 15: VG_(transfer) (&s390x->guest_r13, buf, dir, size, mod); break;
149    case 16: VG_(transfer) (&s390x->guest_r14, buf, dir, size, mod); break;
150    case 17: VG_(transfer) (&s390x->guest_r15, buf, dir, size, mod); break;
151    case 18: VG_(transfer) (&s390x->guest_a0,  buf, dir, size, mod); break;
152    case 19: VG_(transfer) (&s390x->guest_a1,  buf, dir, size, mod); break;
153    case 20: VG_(transfer) (&s390x->guest_a2,  buf, dir, size, mod); break;
154    case 21: VG_(transfer) (&s390x->guest_a3,  buf, dir, size, mod); break;
155    case 22: VG_(transfer) (&s390x->guest_a4,  buf, dir, size, mod); break;
156    case 23: VG_(transfer) (&s390x->guest_a5,  buf, dir, size, mod); break;
157    case 24: VG_(transfer) (&s390x->guest_a6,  buf, dir, size, mod); break;
158    case 25: VG_(transfer) (&s390x->guest_a7,  buf, dir, size, mod); break;
159    case 26: VG_(transfer) (&s390x->guest_a8,  buf, dir, size, mod); break;
160    case 27: VG_(transfer) (&s390x->guest_a9,  buf, dir, size, mod); break;
161    case 28: VG_(transfer) (&s390x->guest_a10, buf, dir, size, mod); break;
162    case 29: VG_(transfer) (&s390x->guest_a11, buf, dir, size, mod); break;
163    case 30: VG_(transfer) (&s390x->guest_a12, buf, dir, size, mod); break;
164    case 31: VG_(transfer) (&s390x->guest_a13, buf, dir, size, mod); break;
165    case 32: VG_(transfer) (&s390x->guest_a14, buf, dir, size, mod); break;
166    case 33: VG_(transfer) (&s390x->guest_a15, buf, dir, size, mod); break;
167    case 34: VG_(transfer) (&s390x->guest_fpc, buf, dir, size, mod); break;
168    case 35: VG_(transfer) (&s390x->guest_f0,  buf, dir, size, mod); break;
169    case 36: VG_(transfer) (&s390x->guest_f1,  buf, dir, size, mod); break;
170    case 37: VG_(transfer) (&s390x->guest_f2,  buf, dir, size, mod); break;
171    case 38: VG_(transfer) (&s390x->guest_f3,  buf, dir, size, mod); break;
172    case 39: VG_(transfer) (&s390x->guest_f4,  buf, dir, size, mod); break;
173    case 40: VG_(transfer) (&s390x->guest_f5,  buf, dir, size, mod); break;
174    case 41: VG_(transfer) (&s390x->guest_f6,  buf, dir, size, mod); break;
175    case 42: VG_(transfer) (&s390x->guest_f7,  buf, dir, size, mod); break;
176    case 43: VG_(transfer) (&s390x->guest_f8,  buf, dir, size, mod); break;
177    case 44: VG_(transfer) (&s390x->guest_f9,  buf, dir, size, mod); break;
178    case 45: VG_(transfer) (&s390x->guest_f10, buf, dir, size, mod); break;
179    case 46: VG_(transfer) (&s390x->guest_f11, buf, dir, size, mod); break;
180    case 47: VG_(transfer) (&s390x->guest_f12, buf, dir, size, mod); break;
181    case 48: VG_(transfer) (&s390x->guest_f13, buf, dir, size, mod); break;
182    case 49: VG_(transfer) (&s390x->guest_f14, buf, dir, size, mod); break;
183    case 50: VG_(transfer) (&s390x->guest_f15, buf, dir, size, mod); break;
184    case 51:  *mod = False; break; //GDBTD??? { "orig_r2", 0, 64 },
185    default: vg_assert(0);
186    }
187 }
188 
189 static
target_xml(Bool shadow_mode)190 const char* target_xml (Bool shadow_mode)
191 {
192    if (shadow_mode) {
193       return "s390x-generic-valgrind.xml";
194    } else {
195       return "s390x-generic.xml";
196    }
197 }
198 
target_get_dtv(ThreadState * tst)199 static CORE_ADDR** target_get_dtv (ThreadState *tst)
200 {
201    VexGuestS390XState* s390x = (VexGuestS390XState*)&tst->arch.vex;
202    // Thread pointer is in a0 (high 32 bits) and a1. Dtv is the second word.
203    return (CORE_ADDR**)((Addr)((Addr64)s390x->guest_a0 << 32
204                               | (Addr64)s390x->guest_a1)
205                         + sizeof(CORE_ADDR));
206 }
207 
208 static struct valgrind_target_ops low_target = {
209    num_regs,
210    regs,
211    17, //sp = r15, which is register offset 17 in regs
212    transfer_register,
213    get_pc,
214    set_pc,
215    "s390x",
216    target_xml,
217    target_get_dtv
218 };
219 
s390x_init_architecture(struct valgrind_target_ops * target)220 void s390x_init_architecture (struct valgrind_target_ops *target)
221 {
222    *target = low_target;
223    set_register_cache (regs, num_regs);
224    gdbserver_expedite_regs = expedite_regs;
225 }
226