njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Machine-related stuff. m_machine.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2007 Julian Seward |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_core_threadstate.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 34 | #include "pub_core_libcassert.h" |
| 35 | #include "pub_core_libcbase.h" |
| 36 | #include "pub_core_machine.h" |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 37 | #include "pub_core_cpuid.h" |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 38 | #include "pub_core_libcsignal.h" // for ppc32 messing with SIGILL |
| 39 | |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 40 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 41 | #define INSTR_PTR(regs) ((regs).vex.VG_INSTR_PTR) |
| 42 | #define STACK_PTR(regs) ((regs).vex.VG_STACK_PTR) |
| 43 | #define FRAME_PTR(regs) ((regs).vex.VG_FRAME_PTR) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 44 | |
| 45 | Addr VG_(get_SP) ( ThreadId tid ) |
| 46 | { |
| 47 | return STACK_PTR( VG_(threads)[tid].arch ); |
| 48 | } |
| 49 | |
| 50 | Addr VG_(get_IP) ( ThreadId tid ) |
| 51 | { |
| 52 | return INSTR_PTR( VG_(threads)[tid].arch ); |
| 53 | } |
| 54 | |
| 55 | Addr VG_(get_FP) ( ThreadId tid ) |
| 56 | { |
| 57 | return FRAME_PTR( VG_(threads)[tid].arch ); |
| 58 | } |
| 59 | |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 60 | Addr VG_(get_LR) ( ThreadId tid ) |
| 61 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 62 | # if defined(VGA_ppc32) || defined(VGA_ppc64) |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 63 | return VG_(threads)[tid].arch.vex.guest_LR; |
| 64 | # elif defined(VGA_x86) || defined(VGA_amd64) |
| 65 | return 0; |
| 66 | # else |
| 67 | # error "Unknown arch" |
| 68 | # endif |
| 69 | } |
| 70 | |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 71 | void VG_(set_SP) ( ThreadId tid, Addr sp ) |
| 72 | { |
| 73 | STACK_PTR( VG_(threads)[tid].arch ) = sp; |
| 74 | } |
| 75 | |
| 76 | void VG_(set_IP) ( ThreadId tid, Addr ip ) |
| 77 | { |
| 78 | INSTR_PTR( VG_(threads)[tid].arch ) = ip; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void VG_(get_shadow_regs_area) ( ThreadId tid, OffT offset, SizeT size, |
| 83 | UChar* area ) |
| 84 | { |
| 85 | ThreadState* tst; |
| 86 | |
| 87 | vg_assert(VG_(is_valid_tid)(tid)); |
| 88 | tst = & VG_(threads)[tid]; |
| 89 | |
| 90 | // Bounds check |
| 91 | vg_assert(0 <= offset && offset < sizeof(VexGuestArchState)); |
| 92 | vg_assert(offset + size <= sizeof(VexGuestArchState)); |
| 93 | |
| 94 | VG_(memcpy)( area, (void*)(((Addr)&(tst->arch.vex_shadow)) + offset), size); |
| 95 | } |
| 96 | |
| 97 | void VG_(set_shadow_regs_area) ( ThreadId tid, OffT offset, SizeT size, |
| 98 | const UChar* area ) |
| 99 | { |
| 100 | ThreadState* tst; |
| 101 | |
| 102 | vg_assert(VG_(is_valid_tid)(tid)); |
| 103 | tst = & VG_(threads)[tid]; |
| 104 | |
| 105 | // Bounds check |
| 106 | vg_assert(0 <= offset && offset < sizeof(VexGuestArchState)); |
| 107 | vg_assert(offset + size <= sizeof(VexGuestArchState)); |
| 108 | |
| 109 | VG_(memcpy)( (void*)(((Addr)(&tst->arch.vex_shadow)) + offset), area, size); |
| 110 | } |
| 111 | |
| 112 | |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 113 | static void apply_to_GPs_of_tid(VexGuestArchState* vex, void (*f)(Addr)) |
| 114 | { |
| 115 | #if defined(VGA_x86) |
| 116 | (*f)(vex->guest_EAX); |
| 117 | (*f)(vex->guest_ECX); |
| 118 | (*f)(vex->guest_EDX); |
| 119 | (*f)(vex->guest_EBX); |
| 120 | (*f)(vex->guest_ESI); |
| 121 | (*f)(vex->guest_EDI); |
| 122 | (*f)(vex->guest_ESP); |
| 123 | (*f)(vex->guest_EBP); |
| 124 | #elif defined(VGA_amd64) |
| 125 | (*f)(vex->guest_RAX); |
| 126 | (*f)(vex->guest_RCX); |
| 127 | (*f)(vex->guest_RDX); |
| 128 | (*f)(vex->guest_RBX); |
| 129 | (*f)(vex->guest_RSI); |
| 130 | (*f)(vex->guest_RDI); |
| 131 | (*f)(vex->guest_RSP); |
| 132 | (*f)(vex->guest_RBP); |
| 133 | (*f)(vex->guest_R8); |
| 134 | (*f)(vex->guest_R9); |
| 135 | (*f)(vex->guest_R10); |
| 136 | (*f)(vex->guest_R11); |
| 137 | (*f)(vex->guest_R12); |
| 138 | (*f)(vex->guest_R13); |
| 139 | (*f)(vex->guest_R14); |
| 140 | (*f)(vex->guest_R15); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 141 | #elif defined(VGA_ppc32) || defined(VGA_ppc64) |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 142 | /* XXX ask tool about validity? */ |
| 143 | (*f)(vex->guest_GPR0); |
| 144 | (*f)(vex->guest_GPR1); |
| 145 | (*f)(vex->guest_GPR2); |
| 146 | (*f)(vex->guest_GPR3); |
| 147 | (*f)(vex->guest_GPR4); |
| 148 | (*f)(vex->guest_GPR5); |
| 149 | (*f)(vex->guest_GPR6); |
| 150 | (*f)(vex->guest_GPR7); |
| 151 | (*f)(vex->guest_GPR8); |
| 152 | (*f)(vex->guest_GPR9); |
| 153 | (*f)(vex->guest_GPR10); |
| 154 | (*f)(vex->guest_GPR11); |
| 155 | (*f)(vex->guest_GPR12); |
| 156 | (*f)(vex->guest_GPR13); |
| 157 | (*f)(vex->guest_GPR14); |
| 158 | (*f)(vex->guest_GPR15); |
| 159 | (*f)(vex->guest_GPR16); |
| 160 | (*f)(vex->guest_GPR17); |
| 161 | (*f)(vex->guest_GPR18); |
| 162 | (*f)(vex->guest_GPR19); |
| 163 | (*f)(vex->guest_GPR20); |
| 164 | (*f)(vex->guest_GPR21); |
| 165 | (*f)(vex->guest_GPR22); |
| 166 | (*f)(vex->guest_GPR23); |
| 167 | (*f)(vex->guest_GPR24); |
| 168 | (*f)(vex->guest_GPR25); |
| 169 | (*f)(vex->guest_GPR26); |
| 170 | (*f)(vex->guest_GPR27); |
| 171 | (*f)(vex->guest_GPR28); |
| 172 | (*f)(vex->guest_GPR29); |
| 173 | (*f)(vex->guest_GPR30); |
| 174 | (*f)(vex->guest_GPR31); |
| 175 | (*f)(vex->guest_CTR); |
| 176 | (*f)(vex->guest_LR); |
| 177 | |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 178 | #else |
| 179 | # error Unknown arch |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | |
| 184 | void VG_(apply_to_GP_regs)(void (*f)(UWord)) |
| 185 | { |
| 186 | ThreadId tid; |
| 187 | |
| 188 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 189 | if (VG_(is_valid_tid)(tid)) { |
| 190 | ThreadState* tst = VG_(get_ThreadState)(tid); |
| 191 | apply_to_GPs_of_tid(&(tst->arch.vex), f); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 196 | static ThreadId thread_stack_iter = VG_INVALID_THREADID; |
| 197 | |
| 198 | void VG_(thread_stack_reset_iter)(void) |
njn | b506bd8 | 2005-06-21 04:01:51 +0000 | [diff] [blame] | 199 | { |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 200 | thread_stack_iter = 1; |
njn | b506bd8 | 2005-06-21 04:01:51 +0000 | [diff] [blame] | 201 | } |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 202 | |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 203 | Bool VG_(thread_stack_next)(ThreadId* tid, Addr* stack_min, Addr* stack_max) |
| 204 | { |
| 205 | ThreadId i; |
| 206 | for (i = thread_stack_iter; i < VG_N_THREADS; i++) { |
| 207 | if (VG_(threads)[i].status != VgTs_Empty) { |
| 208 | *tid = i; |
| 209 | *stack_min = VG_(get_SP)(i); |
| 210 | *stack_max = VG_(threads)[i].client_stack_highest_word; |
| 211 | thread_stack_iter = i + 1; |
| 212 | return True; |
| 213 | } |
| 214 | } |
| 215 | return False; |
| 216 | } |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 217 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 218 | //------------------------------------------------------------- |
| 219 | /* Details about the capabilities of the underlying (host) CPU. These |
| 220 | details are acquired by (1) enquiring with the CPU at startup, or |
| 221 | (2) from the AT_SYSINFO entries the kernel gave us (ppc32 cache |
| 222 | line size). It's a bit nasty in the sense that there's no obvious |
| 223 | way to stop uses of some of this info before it's ready to go. |
sewardj | 10f08cf | 2005-06-29 10:16:14 +0000 | [diff] [blame] | 224 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 225 | Current dependencies are: |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 226 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 227 | x86: initially: call VG_(machine_get_hwcaps) |
sewardj | 10f08cf | 2005-06-29 10:16:14 +0000 | [diff] [blame] | 228 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 229 | then safe to use VG_(machine_get_VexArchInfo) |
| 230 | and VG_(machine_x86_have_mxcsr) |
| 231 | ------------- |
| 232 | amd64: initially: call VG_(machine_get_hwcaps) |
| 233 | |
| 234 | then safe to use VG_(machine_get_VexArchInfo) |
| 235 | ------------- |
| 236 | ppc32: initially: call VG_(machine_get_hwcaps) |
| 237 | call VG_(machine_ppc32_set_clszB) |
| 238 | |
| 239 | then safe to use VG_(machine_get_VexArchInfo) |
sewardj | 2c36d42 | 2005-11-13 01:59:22 +0000 | [diff] [blame] | 240 | and VG_(machine_ppc32_has_FP) |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 241 | and VG_(machine_ppc32_has_VMX) |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 242 | ------------- |
| 243 | ppc64: initially: call VG_(machine_get_hwcaps) |
| 244 | call VG_(machine_ppc64_set_clszB) |
| 245 | |
| 246 | then safe to use VG_(machine_get_VexArchInfo) |
| 247 | and VG_(machine_ppc64_has_VMX) |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 248 | |
| 249 | VG_(machine_get_hwcaps) may use signals (although it attempts to |
| 250 | leave signal state unchanged) and therefore should only be |
| 251 | called before m_main sets up the client's signal state. |
| 252 | */ |
| 253 | |
| 254 | /* --------- State --------- */ |
| 255 | static Bool hwcaps_done = False; |
| 256 | |
| 257 | /* --- all archs --- */ |
| 258 | static VexArch va; |
| 259 | static VexArchInfo vai; |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 260 | |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 261 | #if defined(VGA_x86) |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 262 | UInt VG_(machine_x86_have_mxcsr) = 0; |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 263 | #endif |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 264 | #if defined(VGA_ppc32) |
sewardj | 2c36d42 | 2005-11-13 01:59:22 +0000 | [diff] [blame] | 265 | UInt VG_(machine_ppc32_has_FP) = 0; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 266 | UInt VG_(machine_ppc32_has_VMX) = 0; |
| 267 | #endif |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 268 | #if defined(VGA_ppc64) |
| 269 | ULong VG_(machine_ppc64_has_VMX) = 0; |
| 270 | #endif |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | /* Determine what insn set and insn set variant the host has, and |
| 274 | record it. To be called once at system startup. Returns False if |
| 275 | this a CPU incapable of running Valgrind. */ |
| 276 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 277 | #if defined(VGA_ppc32) || defined(VGA_ppc64) |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 278 | #include <setjmp.h> // For jmp_buf |
| 279 | static jmp_buf env_sigill; |
| 280 | static void handler_sigill ( Int x ) { __builtin_longjmp(env_sigill,1); } |
| 281 | #endif |
| 282 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 283 | Bool VG_(machine_get_hwcaps)( void ) |
| 284 | { |
| 285 | vg_assert(hwcaps_done == False); |
| 286 | hwcaps_done = True; |
| 287 | |
| 288 | // Whack default settings into vai, so that we only need to fill in |
| 289 | // any interesting bits. |
| 290 | LibVEX_default_VexArchInfo(&vai); |
| 291 | |
| 292 | #if defined(VGA_x86) |
| 293 | { Bool have_sse1, have_sse2; |
| 294 | UInt eax, ebx, ecx, edx; |
| 295 | |
| 296 | if (!VG_(has_cpuid)()) |
| 297 | /* we can't do cpuid at all. Give up. */ |
| 298 | return False; |
| 299 | |
| 300 | VG_(cpuid)(0, &eax, &ebx, &ecx, &edx); |
| 301 | if (eax < 1) |
| 302 | /* we can't ask for cpuid(x) for x > 0. Give up. */ |
| 303 | return False; |
| 304 | |
| 305 | /* get capabilities bits into edx */ |
| 306 | VG_(cpuid)(1, &eax, &ebx, &ecx, &edx); |
| 307 | |
| 308 | have_sse1 = (edx & (1<<25)) != 0; /* True => have sse insns */ |
| 309 | have_sse2 = (edx & (1<<26)) != 0; /* True => have sse2 insns */ |
| 310 | |
| 311 | if (have_sse2 && have_sse1) { |
| 312 | va = VexArchX86; |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 313 | vai.hwcaps = VEX_HWCAPS_X86_SSE1; |
| 314 | vai.hwcaps |= VEX_HWCAPS_X86_SSE2; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 315 | VG_(machine_x86_have_mxcsr) = 1; |
| 316 | return True; |
| 317 | } |
| 318 | |
| 319 | if (have_sse1) { |
| 320 | va = VexArchX86; |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 321 | vai.hwcaps = VEX_HWCAPS_X86_SSE1; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 322 | VG_(machine_x86_have_mxcsr) = 1; |
| 323 | return True; |
| 324 | } |
| 325 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 326 | va = VexArchX86; |
| 327 | vai.hwcaps = 0; /*baseline - no sse at all*/ |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 328 | VG_(machine_x86_have_mxcsr) = 0; |
| 329 | return True; |
| 330 | } |
| 331 | |
| 332 | #elif defined(VGA_amd64) |
| 333 | vg_assert(VG_(has_cpuid)()); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 334 | va = VexArchAMD64; |
| 335 | vai.hwcaps = 0; /*baseline - SSE2 */ |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 336 | return True; |
| 337 | |
| 338 | #elif defined(VGA_ppc32) |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 339 | { /* ppc32 doesn't seem to have a sane way to find out what insn |
| 340 | sets the CPU supports. So we have to arse around with |
| 341 | SIGILLs. Yuck. */ |
| 342 | vki_sigset_t saved_set, tmp_set; |
| 343 | struct vki_sigaction saved_act, tmp_act; |
| 344 | |
sewardj | b32bc73 | 2006-01-27 21:52:39 +0000 | [diff] [blame] | 345 | volatile Bool have_F, have_V, have_FX, have_GX; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 346 | Int r; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 347 | |
| 348 | VG_(sigemptyset)(&tmp_set); |
| 349 | VG_(sigaddset)(&tmp_set, VKI_SIGILL); |
| 350 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 351 | r = VG_(sigprocmask)(VKI_SIG_UNBLOCK, &tmp_set, &saved_set); |
| 352 | vg_assert(r == 0); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 353 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 354 | r = VG_(sigaction)(VKI_SIGILL, NULL, &saved_act); |
| 355 | vg_assert(r == 0); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 356 | tmp_act = saved_act; |
| 357 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 358 | /* NODEFER: signal handler does not return (from the kernel's point of |
| 359 | view), hence if it is to successfully catch a signal more than once, |
| 360 | we need the NODEFER flag. */ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 361 | tmp_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 362 | tmp_act.sa_flags &= ~VKI_SA_SIGINFO; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 363 | tmp_act.sa_flags |= VKI_SA_NODEFER; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 364 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 365 | /* standard FP insns */ |
| 366 | have_F = True; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 367 | tmp_act.ksa_handler = handler_sigill; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 368 | r = VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
| 369 | vg_assert(r == 0); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 370 | if (__builtin_setjmp(env_sigill)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 371 | have_F = False; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 372 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 373 | __asm__ __volatile__(".long 0xFC000090"); /*fmr 0,0 */ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 374 | } |
| 375 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 376 | /* Altivec insns */ |
| 377 | have_V = True; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 378 | tmp_act.ksa_handler = handler_sigill; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 379 | r = VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
| 380 | vg_assert(r == 0); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 381 | if (__builtin_setjmp(env_sigill)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 382 | have_V = False; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 383 | } else { |
sewardj | 7159212 | 2006-04-04 03:29:48 +0000 | [diff] [blame] | 384 | /* Unfortunately some older assemblers don't speak Altivec (or |
| 385 | choose not to), so to be safe we directly emit the 32-bit |
| 386 | word corresponding to "vor 0,0,0". This fixes a build |
| 387 | problem that happens on Debian 3.1 (ppc32), and probably |
| 388 | various other places. */ |
sewardj | 54d0dc7 | 2006-04-04 03:08:49 +0000 | [diff] [blame] | 389 | __asm__ __volatile__(".long 0x10000484"); /*vor 0,0,0*/ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 390 | } |
| 391 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 392 | /* General-Purpose optional (fsqrt, fsqrts) */ |
| 393 | have_FX = True; |
| 394 | tmp_act.ksa_handler = handler_sigill; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 395 | r = VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
| 396 | vg_assert(r == 0); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 397 | if (__builtin_setjmp(env_sigill)) { |
| 398 | have_FX = False; |
| 399 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 400 | __asm__ __volatile__(".long 0xFC00002C"); /*fsqrt 0,0 */ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* Graphics optional (stfiwx, fres, frsqrte, fsel) */ |
| 404 | have_GX = True; |
| 405 | tmp_act.ksa_handler = handler_sigill; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 406 | r = VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
| 407 | vg_assert(r == 0); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 408 | if (__builtin_setjmp(env_sigill)) { |
| 409 | have_GX = False; |
| 410 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 411 | __asm__ __volatile__(".long 0xFC000034"); /* frsqrte 0,0 */ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 412 | } |
| 413 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 414 | r = VG_(sigaction)(VKI_SIGILL, &saved_act, NULL); |
| 415 | vg_assert(r == 0); |
| 416 | r = VG_(sigprocmask)(VKI_SIG_SETMASK, &saved_set, NULL); |
| 417 | vg_assert(r == 0); |
sewardj | b32bc73 | 2006-01-27 21:52:39 +0000 | [diff] [blame] | 418 | /* |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 419 | VG_(printf)("F %d V %d FX %d GX %d\n", |
| 420 | (Int)have_F, (Int)have_V, (Int)have_FX, (Int)have_GX); |
sewardj | b32bc73 | 2006-01-27 21:52:39 +0000 | [diff] [blame] | 421 | */ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 422 | /* Make FP a prerequisite for VMX (bogusly so), and for FX and GX. */ |
| 423 | if (have_V && !have_F) |
| 424 | have_V = False; |
| 425 | if (have_FX && !have_F) |
| 426 | have_FX = False; |
| 427 | if (have_GX && !have_F) |
| 428 | have_GX = False; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 429 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 430 | VG_(machine_ppc32_has_FP) = have_F ? 1 : 0; |
| 431 | VG_(machine_ppc32_has_VMX) = have_V ? 1 : 0; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 432 | |
| 433 | va = VexArchPPC32; |
| 434 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 435 | vai.hwcaps = 0; |
| 436 | if (have_F) vai.hwcaps |= VEX_HWCAPS_PPC32_F; |
| 437 | if (have_V) vai.hwcaps |= VEX_HWCAPS_PPC32_V; |
| 438 | if (have_FX) vai.hwcaps |= VEX_HWCAPS_PPC32_FX; |
| 439 | if (have_GX) vai.hwcaps |= VEX_HWCAPS_PPC32_GX; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 440 | |
| 441 | /* But we're not done yet: VG_(machine_ppc32_set_clszB) must be |
| 442 | called before we're ready to go. */ |
| 443 | return True; |
| 444 | } |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 445 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 446 | #elif defined(VGA_ppc64) |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 447 | { /* Same idiocy as for ppc32 - arse around with SIGILLs. */ |
| 448 | vki_sigset_t saved_set, tmp_set; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 449 | struct vki_sigaction saved_act, tmp_act; |
| 450 | |
sewardj | b32bc73 | 2006-01-27 21:52:39 +0000 | [diff] [blame] | 451 | volatile Bool have_F, have_V, have_FX, have_GX; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 452 | |
| 453 | VG_(sigemptyset)(&tmp_set); |
| 454 | VG_(sigaddset)(&tmp_set, VKI_SIGILL); |
| 455 | |
| 456 | VG_(sigprocmask)(VKI_SIG_UNBLOCK, &tmp_set, &saved_set); |
| 457 | |
| 458 | VG_(sigaction)(VKI_SIGILL, NULL, &saved_act); |
| 459 | tmp_act = saved_act; |
| 460 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 461 | /* NODEFER: signal handler does not return (from the kernel's point of |
| 462 | view), hence if it is to successfully catch a signal more than once, |
| 463 | we need the NODEFER flag. */ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 464 | tmp_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 465 | tmp_act.sa_flags &= ~VKI_SA_SIGINFO; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 466 | tmp_act.sa_flags |= VKI_SA_NODEFER; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 467 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 468 | /* standard FP insns */ |
| 469 | have_F = True; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 470 | tmp_act.ksa_handler = handler_sigill; |
| 471 | VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 472 | if (__builtin_setjmp(env_sigill)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 473 | have_F = False; |
| 474 | } else { |
| 475 | __asm__ __volatile__("fmr 0,0"); |
| 476 | } |
| 477 | |
| 478 | /* Altivec insns */ |
| 479 | have_V = True; |
| 480 | tmp_act.ksa_handler = handler_sigill; |
| 481 | VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
| 482 | if (__builtin_setjmp(env_sigill)) { |
| 483 | have_V = False; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 484 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 485 | __asm__ __volatile__(".long 0x10000484"); /*vor 0,0,0*/ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 486 | } |
| 487 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 488 | /* General-Purpose optional (fsqrt, fsqrts) */ |
| 489 | have_FX = True; |
| 490 | tmp_act.ksa_handler = handler_sigill; |
| 491 | VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
| 492 | if (__builtin_setjmp(env_sigill)) { |
| 493 | have_FX = False; |
| 494 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 495 | __asm__ __volatile__(".long 0xFC00002C"); /*fsqrt 0,0*/ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /* Graphics optional (stfiwx, fres, frsqrte, fsel) */ |
| 499 | have_GX = True; |
| 500 | tmp_act.ksa_handler = handler_sigill; |
| 501 | VG_(sigaction)(VKI_SIGILL, &tmp_act, NULL); |
| 502 | if (__builtin_setjmp(env_sigill)) { |
| 503 | have_GX = False; |
| 504 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 505 | __asm__ __volatile__(".long 0xFC000034"); /*frsqrte 0,0*/ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 506 | } |
| 507 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 508 | VG_(sigaction)(VKI_SIGILL, &saved_act, NULL); |
| 509 | VG_(sigprocmask)(VKI_SIG_SETMASK, &saved_set, NULL); |
sewardj | b32bc73 | 2006-01-27 21:52:39 +0000 | [diff] [blame] | 510 | /* |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 511 | if (0) |
| 512 | VG_(printf)("F %d V %d FX %d GX %d\n", |
| 513 | (Int)have_F, (Int)have_V, (Int)have_FX, (Int)have_GX); |
sewardj | b32bc73 | 2006-01-27 21:52:39 +0000 | [diff] [blame] | 514 | */ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 515 | /* on ppc64, if we don't even have FP, just give up. */ |
| 516 | if (!have_F) |
| 517 | return False; |
| 518 | |
| 519 | VG_(machine_ppc64_has_VMX) = have_V ? 1 : 0; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 520 | |
| 521 | va = VexArchPPC64; |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 522 | |
| 523 | vai.hwcaps = 0; |
| 524 | if (have_V) vai.hwcaps |= VEX_HWCAPS_PPC64_V; |
| 525 | if (have_FX) vai.hwcaps |= VEX_HWCAPS_PPC64_FX; |
| 526 | if (have_GX) vai.hwcaps |= VEX_HWCAPS_PPC64_GX; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 527 | |
| 528 | /* But we're not done yet: VG_(machine_ppc64_set_clszB) must be |
| 529 | called before we're ready to go. */ |
| 530 | return True; |
| 531 | } |
| 532 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 533 | #else |
| 534 | # error "Unknown arch" |
| 535 | #endif |
| 536 | } |
| 537 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 538 | /* Notify host cpu cache line size. */ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 539 | #if defined(VGA_ppc32) |
| 540 | void VG_(machine_ppc32_set_clszB)( Int szB ) |
| 541 | { |
| 542 | vg_assert(hwcaps_done); |
| 543 | |
| 544 | /* Either the value must not have been set yet (zero) or we can |
| 545 | tolerate it being set to the same value multiple times, as the |
| 546 | stack scanning logic in m_main is a bit stupid. */ |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 547 | vg_assert(vai.ppc_cache_line_szB == 0 |
| 548 | || vai.ppc_cache_line_szB == szB); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 549 | |
| 550 | vg_assert(szB == 32 || szB == 128); |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 551 | vai.ppc_cache_line_szB = szB; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 552 | } |
| 553 | #endif |
| 554 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 555 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 556 | /* Notify host cpu cache line size. */ |
| 557 | #if defined(VGA_ppc64) |
| 558 | void VG_(machine_ppc64_set_clszB)( Int szB ) |
| 559 | { |
| 560 | vg_assert(hwcaps_done); |
| 561 | |
| 562 | /* Either the value must not have been set yet (zero) or we can |
| 563 | tolerate it being set to the same value multiple times, as the |
| 564 | stack scanning logic in m_main is a bit stupid. */ |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 565 | vg_assert(vai.ppc_cache_line_szB == 0 |
| 566 | || vai.ppc_cache_line_szB == szB); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 567 | |
| 568 | vg_assert(szB == 32 || szB == 128); |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 569 | vai.ppc_cache_line_szB = szB; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 570 | } |
| 571 | #endif |
| 572 | |
| 573 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 574 | /* Fetch host cpu info, once established. */ |
| 575 | void VG_(machine_get_VexArchInfo)( /*OUT*/VexArch* pVa, |
| 576 | /*OUT*/VexArchInfo* pVai ) |
| 577 | { |
| 578 | vg_assert(hwcaps_done); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 579 | if (pVa) *pVa = va; |
| 580 | if (pVai) *pVai = vai; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 581 | } |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 582 | |
| 583 | |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 584 | // Given a pointer to a function as obtained by "& functionname" in C, |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 585 | // produce a pointer to the actual entry point for the function. |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 586 | void* VG_(fnptr_to_fnentry)( void* f ) |
| 587 | { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 588 | #if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \ |
| 589 | || defined(VGP_ppc32_linux) |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 590 | return f; |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 591 | #elif defined(VGP_ppc64_linux) || defined(VGP_ppc32_aix5) \ |
| 592 | || defined(VGP_ppc64_aix5) |
| 593 | /* All other ppc variants use the AIX scheme, in which f is a |
| 594 | pointer to a 3-word function descriptor, of which the first word |
| 595 | is the entry address. */ |
| 596 | UWord* descr = (UWord*)f; |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 597 | return (void*)(descr[0]); |
| 598 | #else |
| 599 | # error "Unknown platform" |
| 600 | #endif |
| 601 | } |
| 602 | |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 603 | /*--------------------------------------------------------------------*/ |
| 604 | /*--- end ---*/ |
| 605 | /*--------------------------------------------------------------------*/ |