1 
2 /*--------------------------------------------------------------------*/
3 /*--- Machine-related things.                   pub_core_machine.h ---*/
4 /*--------------------------------------------------------------------*/
5 
6 /*
7    This file is part of Valgrind, a dynamic binary instrumentation
8    framework.
9 
10    Copyright (C) 2000-2013 Julian Seward
11       jseward@acm.org
12 
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of the
16    License, or (at your option) any later version.
17 
18    This program is distributed in the hope that it will be useful, but
19    WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26    02111-1307, USA.
27 
28    The GNU General Public License is contained in the file COPYING.
29 */
30 
31 #ifndef __PUB_CORE_MACHINE_H
32 #define __PUB_CORE_MACHINE_H
33 
34 //--------------------------------------------------------------------
35 // PURPOSE: This module contains code related to the particular
36 // architecture, things like accessing guest state, endianness, word size,
37 // etc.
38 //--------------------------------------------------------------------
39 
40 #include "pub_tool_machine.h"
41 #include "pub_core_basics.h"      // UnwindStartRegs
42 
43 // XXX: this is *really* the wrong spot for these things
44 #if defined(VGP_x86_linux)
45 #  define VG_ELF_DATA2XXX     ELFDATA2LSB
46 #  define VG_ELF_MACHINE      EM_386
47 #  define VG_ELF_CLASS        ELFCLASS32
48 #  undef  VG_PLAT_USES_PPCTOC
49 #elif defined(VGP_amd64_linux)
50 #  define VG_ELF_DATA2XXX     ELFDATA2LSB
51 #  define VG_ELF_MACHINE      EM_X86_64
52 #  define VG_ELF_CLASS        ELFCLASS64
53 #  undef  VG_PLAT_USES_PPCTOC
54 #elif defined(VGP_ppc32_linux)
55 #  define VG_ELF_DATA2XXX     ELFDATA2MSB
56 #  define VG_ELF_MACHINE      EM_PPC
57 #  define VG_ELF_CLASS        ELFCLASS32
58 #  undef  VG_PLAT_USES_PPCTOC
59 #elif defined(VGP_ppc64be_linux)
60 #  define VG_ELF_DATA2XXX     ELFDATA2MSB
61 #  define VG_ELF_MACHINE      EM_PPC64
62 #  define VG_ELF_CLASS        ELFCLASS64
63 #  define VG_PLAT_USES_PPCTOC 1
64 #elif defined(VGP_ppc64le_linux)
65 #  define VG_ELF_DATA2XXX     ELFDATA2LSB
66 #  define VG_ELF_MACHINE      EM_PPC64
67 #  define VG_ELF_CLASS        ELFCLASS64
68 #  undef VG_PLAT_USES_PPCTOC
69 #elif defined(VGP_arm_linux)
70 #  define VG_ELF_DATA2XXX     ELFDATA2LSB
71 #  define VG_ELF_MACHINE      EM_ARM
72 #  define VG_ELF_CLASS        ELFCLASS32
73 #  undef  VG_PLAT_USES_PPCTOC
74 #elif defined(VGP_arm64_linux)
75 #  define VG_ELF_DATA2XXX     ELFDATA2LSB
76 #  define VG_ELF_MACHINE      EM_AARCH64
77 #  define VG_ELF_CLASS        ELFCLASS64
78 #  undef  VG_PLAT_USES_PPCTOC
79 #elif defined(VGO_darwin)
80 #  undef  VG_ELF_DATA2XXX
81 #  undef  VG_ELF_MACHINE
82 #  undef  VG_ELF_CLASS
83 #  undef  VG_PLAT_USES_PPCTOC
84 #elif defined(VGP_s390x_linux)
85 #  define VG_ELF_DATA2XXX     ELFDATA2MSB
86 #  define VG_ELF_MACHINE      EM_S390
87 #  define VG_ELF_CLASS        ELFCLASS64
88 #  undef  VG_PLAT_USES_PPCTOC
89 #elif defined(VGP_mips32_linux)
90 #  if defined (VG_LITTLEENDIAN)
91 #    define VG_ELF_DATA2XXX   ELFDATA2LSB
92 #  elif defined (VG_BIGENDIAN)
93 #    define VG_ELF_DATA2XXX   ELFDATA2MSB
94 #  else
95 #    error "Unknown endianness"
96 #  endif
97 #  define VG_ELF_MACHINE      EM_MIPS
98 #  define VG_ELF_CLASS        ELFCLASS32
99 #  undef  VG_PLAT_USES_PPCTOC
100 #elif defined(VGP_mips64_linux)
101 #  if defined (VG_LITTLEENDIAN)
102 #    define VG_ELF_DATA2XXX     ELFDATA2LSB
103 #  elif defined (VG_BIGENDIAN)
104 #    define VG_ELF_DATA2XXX     ELFDATA2MSB
105 #  else
106 #    error "Unknown endianness"
107 #  endif
108 #  define VG_ELF_MACHINE      EM_MIPS
109 #  define VG_ELF_CLASS        ELFCLASS64
110 #  undef  VG_PLAT_USES_PPCTOC
111 #elif defined(VGP_tilegx_linux)
112 #  define VG_ELF_DATA2XXX     ELFDATA2LSB
113    #ifndef EM_TILEGX
114    #define EM_TILEGX 191
115    #endif
116 #  define VG_ELF_MACHINE      EM_TILEGX
117 #  define VG_ELF_CLASS        ELFCLASS64
118 #  undef  VG_PLAT_USES_PPCTOC
119 #else
120 #  error Unknown platform
121 #endif
122 
123 #if defined(VGA_x86)
124 #  define VG_INSTR_PTR        guest_EIP
125 #  define VG_STACK_PTR        guest_ESP
126 #  define VG_FRAME_PTR        guest_EBP
127 #elif defined(VGA_amd64)
128 #  define VG_INSTR_PTR        guest_RIP
129 #  define VG_STACK_PTR        guest_RSP
130 #  define VG_FRAME_PTR        guest_RBP
131 #elif defined(VGA_ppc32)
132 #  define VG_INSTR_PTR        guest_CIA
133 #  define VG_STACK_PTR        guest_GPR1
134 #  define VG_FRAME_PTR        guest_GPR1   // No frame ptr for PPC
135 #elif defined(VGA_ppc64be) || defined(VGA_ppc64le)
136 #  define VG_INSTR_PTR        guest_CIA
137 #  define VG_STACK_PTR        guest_GPR1
138 #  define VG_FRAME_PTR        guest_GPR1   // No frame ptr for PPC
139 #elif defined(VGA_arm)
140 #  define VG_INSTR_PTR        guest_R15T
141 #  define VG_STACK_PTR        guest_R13
142 #  define VG_FRAME_PTR        guest_R11
143 #elif defined(VGA_arm64)
144 #  define VG_INSTR_PTR        guest_PC
145 #  define VG_STACK_PTR        guest_XSP
146 #  define VG_FRAME_PTR        guest_X29   // FIXME: is this right?
147 #elif defined(VGA_s390x)
148 #  define VG_INSTR_PTR        guest_IA
149 #  define VG_STACK_PTR        guest_SP
150 #  define VG_FRAME_PTR        guest_FP
151 #  define VG_FPC_REG          guest_fpc
152 #elif defined(VGA_mips32)
153 #  define VG_INSTR_PTR        guest_PC
154 #  define VG_STACK_PTR        guest_r29
155 #  define VG_FRAME_PTR        guest_r30
156 #elif defined(VGA_mips64)
157 #  define VG_INSTR_PTR        guest_PC
158 #  define VG_STACK_PTR        guest_r29
159 #  define VG_FRAME_PTR        guest_r30
160 #elif defined(VGA_tilegx)
161 #  define VG_INSTR_PTR        guest_pc
162 #  define VG_STACK_PTR        guest_r54
163 #  define VG_FRAME_PTR        guest_r52
164 #else
165 #  error Unknown arch
166 #endif
167 
168 
169 // Offsets for the Vex state
170 #define VG_O_STACK_PTR        (offsetof(VexGuestArchState, VG_STACK_PTR))
171 #define VG_O_INSTR_PTR        (offsetof(VexGuestArchState, VG_INSTR_PTR))
172 #define VG_O_FPC_REG          (offsetof(VexGuestArchState, VG_FPC_REG))
173 
174 
175 //-------------------------------------------------------------
176 // Guest state accessors that are not visible to tools.  The only
177 // ones that are visible are get_IP and get_SP.
178 
179 //Addr VG_(get_IP) ( ThreadId tid );  // in pub_tool_machine.h
180 //Addr VG_(get_SP) ( ThreadId tid );  // in pub_tool_machine.h
181 Addr VG_(get_FP) ( ThreadId tid );
182 
183 void VG_(set_IP) ( ThreadId tid, Addr encip );
184 void VG_(set_SP) ( ThreadId tid, Addr sp );
185 
186 
187 //-------------------------------------------------------------
188 // Get hold of the values needed for a stack unwind, for the specified
189 // (client) thread.
190 void VG_(get_UnwindStartRegs) ( /*OUT*/UnwindStartRegs* regs,
191                                 ThreadId tid );
192 
193 
194 //-------------------------------------------------------------
195 /* Details about the capabilities of the underlying (host) CPU.  These
196    details are acquired by (1) enquiring with the CPU at startup, or
197    (2) from the AT_SYSINFO entries the kernel gave us (ppc cache
198    line size).  It's a bit nasty in the sense that there's no obvious
199    way to stop uses of some of this info before it's ready to go.
200 
201    Current dependencies are:
202 
203    x86:   initially:  call VG_(machine_get_hwcaps)
204 
205           then safe to use VG_(machine_get_VexArchInfo)
206                        and VG_(machine_x86_have_mxcsr)
207    -------------
208    amd64: initially:  call VG_(machine_get_hwcaps)
209 
210           then safe to use VG_(machine_get_VexArchInfo)
211    -------------
212    ppc32: initially:  call VG_(machine_get_hwcaps)
213                       call VG_(machine_ppc32_set_clszB)
214 
215           then safe to use VG_(machine_get_VexArchInfo)
216                        and VG_(machine_ppc32_has_FP)
217                        and VG_(machine_ppc32_has_VMX)
218    -------------
219    ppc64: initially:  call VG_(machine_get_hwcaps)
220                       call VG_(machine_ppc64_set_clszB)
221 
222           then safe to use VG_(machine_get_VexArchInfo)
223                        and VG_(machine_ppc64_has_VMX)
224    -------------
225    arm:   initially:  call VG_(machine_get_hwcaps)
226                       call VG_(machine_arm_set_has_NEON)
227 
228           then safe to use VG_(machine_get_VexArchInfo)
229    -------------
230    s390x: initially:  call VG_(machine_get_hwcaps)
231 
232           then safe to use VG_(machine_get_VexArchInfo)
233 
234    VG_(machine_get_hwcaps) may use signals (although it attempts to
235    leave signal state unchanged) and therefore should only be
236    called before m_main sets up the client's signal state.
237 */
238 
239 /* Determine what insn set and insn set variant the host has, and
240    record it.  To be called once at system startup.  Returns False if
241    this a CPU incapable of running Valgrind. */
242 extern Bool VG_(machine_get_hwcaps)( void );
243 
244 /* Determine information about the cache system this host has and
245    record it. Returns False, if cache information cannot be auto-detected. */
246 extern Bool VG_(machine_get_cache_info)( VexArchInfo * );
247 
248 /* Notify host cpu cache line size, as per above comment. */
249 #if defined(VGA_ppc32)
250 extern void VG_(machine_ppc32_set_clszB)( Int );
251 #endif
252 
253 #if defined(VGA_ppc64be) || defined(VGA_ppc64le)
254 extern void VG_(machine_ppc64_set_clszB)( Int );
255 #endif
256 
257 #if defined(VGA_arm)
258 extern void VG_(machine_arm_set_has_NEON)( Bool );
259 #endif
260 
261 /* X86: set to 1 if the host is able to do {ld,st}mxcsr (load/store
262    the SSE control/status register), else zero.  Is referenced from
263    assembly code, so do not change from a 32-bit int. */
264 #if defined(VGA_x86)
265 extern UInt VG_(machine_x86_have_mxcsr);
266 #endif
267 
268 /* PPC32: set to 1 if FP instructions are supported in user-space,
269    else 0.  Is referenced from assembly code, so do not change from a
270    32-bit int. */
271 #if defined(VGA_ppc32)
272 extern UInt VG_(machine_ppc32_has_FP);
273 #endif
274 
275 /* PPC32: set to 1 if Altivec instructions are supported in
276    user-space, else 0.  Is referenced from assembly code, so do not
277    change from a 32-bit int. */
278 #if defined(VGA_ppc32)
279 extern UInt VG_(machine_ppc32_has_VMX);
280 #endif
281 
282 /* PPC64: set to 1 if Altivec instructions are supported in
283    user-space, else 0.  Is referenced from assembly code, so do not
284    change from a 64-bit int. */
285 #if defined(VGA_ppc64be) || defined(VGA_ppc64le)
286 extern ULong VG_(machine_ppc64_has_VMX);
287 #endif
288 
289 #if defined(VGA_arm)
290 extern Int VG_(machine_arm_archlevel);
291 #endif
292 
293 #endif   // __PUB_CORE_MACHINE_H
294 
295 /*--------------------------------------------------------------------*/
296 /*--- end                                                          ---*/
297 /*--------------------------------------------------------------------*/
298