njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1 | /*--------------------------------------------------------------------*/ |
| 2 | /*--- Machine-related stuff. m_machine.c ---*/ |
| 3 | /*--------------------------------------------------------------------*/ |
| 4 | |
| 5 | /* |
| 6 | This file is part of Valgrind, a dynamic binary instrumentation |
| 7 | framework. |
| 8 | |
sewardj | ec062e8 | 2011-10-23 07:32:08 +0000 | [diff] [blame] | 9 | Copyright (C) 2000-2011 Julian Seward |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 10 | jseward@acm.org |
| 11 | |
| 12 | This program is free software; you can redistribute it and/or |
| 13 | modify it under the terms of the GNU General Public License as |
| 14 | published by the Free Software Foundation; either version 2 of the |
| 15 | License, or (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, but |
| 18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program; if not, write to the Free Software |
| 24 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 25 | 02111-1307, USA. |
| 26 | |
| 27 | The GNU General Public License is contained in the file COPYING. |
| 28 | */ |
| 29 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 30 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 31 | #include "pub_core_vki.h" |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 32 | #include "pub_core_libcsetjmp.h" // setjmp facilities |
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" |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 36 | #include "pub_core_libcfile.h" |
| 37 | #include "pub_core_mallocfree.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 38 | #include "pub_core_machine.h" |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 39 | #include "pub_core_cpuid.h" |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 40 | #include "pub_core_libcsignal.h" // for ppc32 messing with SIGILL and SIGFPE |
sewardj | 3b0cae7 | 2009-05-28 17:15:41 +0000 | [diff] [blame] | 41 | #include "pub_core_debuglog.h" |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 42 | |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 43 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 44 | #define INSTR_PTR(regs) ((regs).vex.VG_INSTR_PTR) |
| 45 | #define STACK_PTR(regs) ((regs).vex.VG_STACK_PTR) |
| 46 | #define FRAME_PTR(regs) ((regs).vex.VG_FRAME_PTR) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 47 | |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 48 | Addr VG_(get_IP) ( ThreadId tid ) { |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 49 | return INSTR_PTR( VG_(threads)[tid].arch ); |
| 50 | } |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 51 | Addr VG_(get_SP) ( ThreadId tid ) { |
| 52 | return STACK_PTR( VG_(threads)[tid].arch ); |
| 53 | } |
| 54 | Addr VG_(get_FP) ( ThreadId tid ) { |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 55 | return FRAME_PTR( VG_(threads)[tid].arch ); |
| 56 | } |
| 57 | |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 58 | void VG_(set_IP) ( ThreadId tid, Addr ip ) { |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 59 | INSTR_PTR( VG_(threads)[tid].arch ) = ip; |
| 60 | } |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 61 | void VG_(set_SP) ( ThreadId tid, Addr sp ) { |
| 62 | STACK_PTR( VG_(threads)[tid].arch ) = sp; |
| 63 | } |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 64 | |
| 65 | void VG_(get_UnwindStartRegs) ( /*OUT*/UnwindStartRegs* regs, |
| 66 | ThreadId tid ) |
| 67 | { |
| 68 | # if defined(VGA_x86) |
| 69 | regs->r_pc = (ULong)VG_(threads)[tid].arch.vex.guest_EIP; |
| 70 | regs->r_sp = (ULong)VG_(threads)[tid].arch.vex.guest_ESP; |
| 71 | regs->misc.X86.r_ebp |
| 72 | = VG_(threads)[tid].arch.vex.guest_EBP; |
| 73 | # elif defined(VGA_amd64) |
| 74 | regs->r_pc = VG_(threads)[tid].arch.vex.guest_RIP; |
| 75 | regs->r_sp = VG_(threads)[tid].arch.vex.guest_RSP; |
| 76 | regs->misc.AMD64.r_rbp |
| 77 | = VG_(threads)[tid].arch.vex.guest_RBP; |
| 78 | # elif defined(VGA_ppc32) |
| 79 | regs->r_pc = (ULong)VG_(threads)[tid].arch.vex.guest_CIA; |
sewardj | f5f1e12 | 2010-01-02 13:24:58 +0000 | [diff] [blame] | 80 | regs->r_sp = (ULong)VG_(threads)[tid].arch.vex.guest_GPR1; |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 81 | regs->misc.PPC32.r_lr |
| 82 | = VG_(threads)[tid].arch.vex.guest_LR; |
| 83 | # elif defined(VGA_ppc64) |
| 84 | regs->r_pc = VG_(threads)[tid].arch.vex.guest_CIA; |
sewardj | f5f1e12 | 2010-01-02 13:24:58 +0000 | [diff] [blame] | 85 | regs->r_sp = VG_(threads)[tid].arch.vex.guest_GPR1; |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 86 | regs->misc.PPC64.r_lr |
| 87 | = VG_(threads)[tid].arch.vex.guest_LR; |
| 88 | # elif defined(VGA_arm) |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 89 | regs->r_pc = (ULong)VG_(threads)[tid].arch.vex.guest_R15T; |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 90 | regs->r_sp = (ULong)VG_(threads)[tid].arch.vex.guest_R13; |
| 91 | regs->misc.ARM.r14 |
| 92 | = VG_(threads)[tid].arch.vex.guest_R14; |
| 93 | regs->misc.ARM.r12 |
| 94 | = VG_(threads)[tid].arch.vex.guest_R12; |
| 95 | regs->misc.ARM.r11 |
| 96 | = VG_(threads)[tid].arch.vex.guest_R11; |
sewardj | fa5ce56 | 2010-09-23 22:05:59 +0000 | [diff] [blame] | 97 | regs->misc.ARM.r7 |
| 98 | = VG_(threads)[tid].arch.vex.guest_R7; |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 99 | # elif defined(VGA_s390x) |
| 100 | regs->r_pc = (ULong)VG_(threads)[tid].arch.vex.guest_IA; |
| 101 | regs->r_sp = (ULong)VG_(threads)[tid].arch.vex.guest_SP; |
| 102 | regs->misc.S390X.r_fp |
| 103 | = VG_(threads)[tid].arch.vex.guest_r11; |
| 104 | regs->misc.S390X.r_lr |
| 105 | = VG_(threads)[tid].arch.vex.guest_r14; |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 106 | # else |
| 107 | # error "Unknown arch" |
| 108 | # endif |
| 109 | } |
| 110 | |
| 111 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 112 | void VG_(set_syscall_return_shadows) ( ThreadId tid, |
| 113 | /* shadow vals for the result */ |
| 114 | UWord s1res, UWord s2res, |
| 115 | /* shadow vals for the error val */ |
| 116 | UWord s1err, UWord s2err ) |
| 117 | { |
| 118 | # if defined(VGP_x86_linux) |
| 119 | VG_(threads)[tid].arch.vex_shadow1.guest_EAX = s1res; |
| 120 | VG_(threads)[tid].arch.vex_shadow2.guest_EAX = s2res; |
| 121 | # elif defined(VGP_amd64_linux) |
| 122 | VG_(threads)[tid].arch.vex_shadow1.guest_RAX = s1res; |
| 123 | VG_(threads)[tid].arch.vex_shadow2.guest_RAX = s2res; |
| 124 | # elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
| 125 | VG_(threads)[tid].arch.vex_shadow1.guest_GPR3 = s1res; |
| 126 | VG_(threads)[tid].arch.vex_shadow2.guest_GPR3 = s2res; |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 127 | # elif defined(VGP_arm_linux) |
| 128 | VG_(threads)[tid].arch.vex_shadow1.guest_R0 = s1res; |
| 129 | VG_(threads)[tid].arch.vex_shadow2.guest_R0 = s2res; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 130 | # elif defined(VGO_darwin) |
| 131 | // GrP fixme darwin syscalls may return more values (2 registers plus error) |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 132 | # elif defined(VGP_s390x_linux) |
| 133 | VG_(threads)[tid].arch.vex_shadow1.guest_r2 = s1res; |
| 134 | VG_(threads)[tid].arch.vex_shadow2.guest_r2 = s2res; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 135 | # else |
| 136 | # error "Unknown plat" |
| 137 | # endif |
| 138 | } |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 139 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 140 | void |
| 141 | VG_(get_shadow_regs_area) ( ThreadId tid, |
| 142 | /*DST*/UChar* dst, |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 143 | /*SRC*/Int shadowNo, PtrdiffT offset, SizeT size ) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 144 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 145 | void* src; |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 146 | ThreadState* tst; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 147 | vg_assert(shadowNo == 0 || shadowNo == 1 || shadowNo == 2); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 148 | vg_assert(VG_(is_valid_tid)(tid)); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 149 | // Bounds check |
| 150 | vg_assert(0 <= offset && offset < sizeof(VexGuestArchState)); |
| 151 | vg_assert(offset + size <= sizeof(VexGuestArchState)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 152 | // Copy |
| 153 | tst = & VG_(threads)[tid]; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 154 | src = NULL; |
| 155 | switch (shadowNo) { |
| 156 | case 0: src = (void*)(((Addr)&(tst->arch.vex)) + offset); break; |
| 157 | case 1: src = (void*)(((Addr)&(tst->arch.vex_shadow1)) + offset); break; |
| 158 | case 2: src = (void*)(((Addr)&(tst->arch.vex_shadow2)) + offset); break; |
| 159 | } |
| 160 | tl_assert(src != NULL); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 161 | VG_(memcpy)( dst, src, size); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 162 | } |
| 163 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 164 | void |
| 165 | VG_(set_shadow_regs_area) ( ThreadId tid, |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 166 | /*DST*/Int shadowNo, PtrdiffT offset, SizeT size, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 167 | /*SRC*/const UChar* src ) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 168 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 169 | void* dst; |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 170 | ThreadState* tst; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 171 | vg_assert(shadowNo == 0 || shadowNo == 1 || shadowNo == 2); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 172 | vg_assert(VG_(is_valid_tid)(tid)); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 173 | // Bounds check |
| 174 | vg_assert(0 <= offset && offset < sizeof(VexGuestArchState)); |
| 175 | vg_assert(offset + size <= sizeof(VexGuestArchState)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 176 | // Copy |
| 177 | tst = & VG_(threads)[tid]; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 178 | dst = NULL; |
| 179 | switch (shadowNo) { |
| 180 | case 0: dst = (void*)(((Addr)&(tst->arch.vex)) + offset); break; |
| 181 | case 1: dst = (void*)(((Addr)&(tst->arch.vex_shadow1)) + offset); break; |
| 182 | case 2: dst = (void*)(((Addr)&(tst->arch.vex_shadow2)) + offset); break; |
| 183 | } |
| 184 | tl_assert(dst != NULL); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 185 | VG_(memcpy)( dst, src, size); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 189 | static void apply_to_GPs_of_tid(ThreadId tid, void (*f)(ThreadId, HChar*, Addr)) |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 190 | { |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 191 | VexGuestArchState* vex = &(VG_(get_ThreadState)(tid)->arch.vex); |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 192 | #if defined(VGA_x86) |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 193 | (*f)(tid, "EAX", vex->guest_EAX); |
| 194 | (*f)(tid, "ECX", vex->guest_ECX); |
| 195 | (*f)(tid, "EDX", vex->guest_EDX); |
| 196 | (*f)(tid, "EBX", vex->guest_EBX); |
| 197 | (*f)(tid, "ESI", vex->guest_ESI); |
| 198 | (*f)(tid, "EDI", vex->guest_EDI); |
| 199 | (*f)(tid, "ESP", vex->guest_ESP); |
| 200 | (*f)(tid, "EBP", vex->guest_EBP); |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 201 | #elif defined(VGA_amd64) |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 202 | (*f)(tid, "RAX", vex->guest_RAX); |
| 203 | (*f)(tid, "RCX", vex->guest_RCX); |
| 204 | (*f)(tid, "RDX", vex->guest_RDX); |
| 205 | (*f)(tid, "RBX", vex->guest_RBX); |
| 206 | (*f)(tid, "RSI", vex->guest_RSI); |
| 207 | (*f)(tid, "RDI", vex->guest_RDI); |
| 208 | (*f)(tid, "RSP", vex->guest_RSP); |
| 209 | (*f)(tid, "RBP", vex->guest_RBP); |
| 210 | (*f)(tid, "R8" , vex->guest_R8 ); |
| 211 | (*f)(tid, "R9" , vex->guest_R9 ); |
| 212 | (*f)(tid, "R10", vex->guest_R10); |
| 213 | (*f)(tid, "R11", vex->guest_R11); |
| 214 | (*f)(tid, "R12", vex->guest_R12); |
| 215 | (*f)(tid, "R13", vex->guest_R13); |
| 216 | (*f)(tid, "R14", vex->guest_R14); |
| 217 | (*f)(tid, "R15", vex->guest_R15); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 218 | #elif defined(VGA_ppc32) || defined(VGA_ppc64) |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 219 | (*f)(tid, "GPR0" , vex->guest_GPR0 ); |
| 220 | (*f)(tid, "GPR1" , vex->guest_GPR1 ); |
| 221 | (*f)(tid, "GPR2" , vex->guest_GPR2 ); |
| 222 | (*f)(tid, "GPR3" , vex->guest_GPR3 ); |
| 223 | (*f)(tid, "GPR4" , vex->guest_GPR4 ); |
| 224 | (*f)(tid, "GPR5" , vex->guest_GPR5 ); |
| 225 | (*f)(tid, "GPR6" , vex->guest_GPR6 ); |
| 226 | (*f)(tid, "GPR7" , vex->guest_GPR7 ); |
| 227 | (*f)(tid, "GPR8" , vex->guest_GPR8 ); |
| 228 | (*f)(tid, "GPR9" , vex->guest_GPR9 ); |
| 229 | (*f)(tid, "GPR10", vex->guest_GPR10); |
| 230 | (*f)(tid, "GPR11", vex->guest_GPR11); |
| 231 | (*f)(tid, "GPR12", vex->guest_GPR12); |
| 232 | (*f)(tid, "GPR13", vex->guest_GPR13); |
| 233 | (*f)(tid, "GPR14", vex->guest_GPR14); |
| 234 | (*f)(tid, "GPR15", vex->guest_GPR15); |
| 235 | (*f)(tid, "GPR16", vex->guest_GPR16); |
| 236 | (*f)(tid, "GPR17", vex->guest_GPR17); |
| 237 | (*f)(tid, "GPR18", vex->guest_GPR18); |
| 238 | (*f)(tid, "GPR19", vex->guest_GPR19); |
| 239 | (*f)(tid, "GPR20", vex->guest_GPR20); |
| 240 | (*f)(tid, "GPR21", vex->guest_GPR21); |
| 241 | (*f)(tid, "GPR22", vex->guest_GPR22); |
| 242 | (*f)(tid, "GPR23", vex->guest_GPR23); |
| 243 | (*f)(tid, "GPR24", vex->guest_GPR24); |
| 244 | (*f)(tid, "GPR25", vex->guest_GPR25); |
| 245 | (*f)(tid, "GPR26", vex->guest_GPR26); |
| 246 | (*f)(tid, "GPR27", vex->guest_GPR27); |
| 247 | (*f)(tid, "GPR28", vex->guest_GPR28); |
| 248 | (*f)(tid, "GPR29", vex->guest_GPR29); |
| 249 | (*f)(tid, "GPR30", vex->guest_GPR30); |
| 250 | (*f)(tid, "GPR31", vex->guest_GPR31); |
| 251 | (*f)(tid, "CTR" , vex->guest_CTR ); |
| 252 | (*f)(tid, "LR" , vex->guest_LR ); |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 253 | #elif defined(VGA_arm) |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 254 | (*f)(tid, "R0" , vex->guest_R0 ); |
| 255 | (*f)(tid, "R1" , vex->guest_R1 ); |
| 256 | (*f)(tid, "R2" , vex->guest_R2 ); |
| 257 | (*f)(tid, "R3" , vex->guest_R3 ); |
| 258 | (*f)(tid, "R4" , vex->guest_R4 ); |
| 259 | (*f)(tid, "R5" , vex->guest_R5 ); |
| 260 | (*f)(tid, "R6" , vex->guest_R6 ); |
| 261 | (*f)(tid, "R8" , vex->guest_R8 ); |
| 262 | (*f)(tid, "R9" , vex->guest_R9 ); |
| 263 | (*f)(tid, "R10", vex->guest_R10); |
| 264 | (*f)(tid, "R11", vex->guest_R11); |
| 265 | (*f)(tid, "R12", vex->guest_R12); |
| 266 | (*f)(tid, "R13", vex->guest_R13); |
| 267 | (*f)(tid, "R14", vex->guest_R14); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 268 | #elif defined(VGA_s390x) |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 269 | (*f)(tid, "r0" , vex->guest_r0 ); |
| 270 | (*f)(tid, "r1" , vex->guest_r1 ); |
| 271 | (*f)(tid, "r2" , vex->guest_r2 ); |
| 272 | (*f)(tid, "r3" , vex->guest_r3 ); |
| 273 | (*f)(tid, "r4" , vex->guest_r4 ); |
| 274 | (*f)(tid, "r5" , vex->guest_r5 ); |
| 275 | (*f)(tid, "r6" , vex->guest_r6 ); |
| 276 | (*f)(tid, "r7" , vex->guest_r7 ); |
| 277 | (*f)(tid, "r8" , vex->guest_r8 ); |
| 278 | (*f)(tid, "r9" , vex->guest_r9 ); |
| 279 | (*f)(tid, "r10", vex->guest_r10); |
| 280 | (*f)(tid, "r11", vex->guest_r11); |
| 281 | (*f)(tid, "r12", vex->guest_r12); |
| 282 | (*f)(tid, "r13", vex->guest_r13); |
| 283 | (*f)(tid, "r14", vex->guest_r14); |
| 284 | (*f)(tid, "r15", vex->guest_r15); |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 285 | #else |
| 286 | # error Unknown arch |
| 287 | #endif |
| 288 | } |
| 289 | |
| 290 | |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 291 | void VG_(apply_to_GP_regs)(void (*f)(ThreadId, HChar*, UWord)) |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 292 | { |
| 293 | ThreadId tid; |
| 294 | |
| 295 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 296 | if (VG_(is_valid_tid)(tid)) { |
philippe | a22f59d | 2012-01-26 23:13:52 +0000 | [diff] [blame] | 297 | apply_to_GPs_of_tid(tid, f); |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 302 | void VG_(thread_stack_reset_iter)(/*OUT*/ThreadId* tid) |
njn | b506bd8 | 2005-06-21 04:01:51 +0000 | [diff] [blame] | 303 | { |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 304 | *tid = (ThreadId)(-1); |
njn | b506bd8 | 2005-06-21 04:01:51 +0000 | [diff] [blame] | 305 | } |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 306 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 307 | Bool VG_(thread_stack_next)(/*MOD*/ThreadId* tid, |
| 308 | /*OUT*/Addr* stack_min, |
| 309 | /*OUT*/Addr* stack_max) |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 310 | { |
| 311 | ThreadId i; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 312 | for (i = (*tid)+1; i < VG_N_THREADS; i++) { |
| 313 | if (i == VG_INVALID_THREADID) |
| 314 | continue; |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 315 | if (VG_(threads)[i].status != VgTs_Empty) { |
| 316 | *tid = i; |
| 317 | *stack_min = VG_(get_SP)(i); |
| 318 | *stack_max = VG_(threads)[i].client_stack_highest_word; |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 319 | return True; |
| 320 | } |
| 321 | } |
| 322 | return False; |
| 323 | } |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 324 | |
sewardj | bbec772 | 2007-11-25 14:08:53 +0000 | [diff] [blame] | 325 | Addr VG_(thread_get_stack_max)(ThreadId tid) |
| 326 | { |
| 327 | vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); |
| 328 | vg_assert(VG_(threads)[tid].status != VgTs_Empty); |
| 329 | return VG_(threads)[tid].client_stack_highest_word; |
| 330 | } |
| 331 | |
bart | 0fb0320 | 2008-03-29 09:25:53 +0000 | [diff] [blame] | 332 | SizeT VG_(thread_get_stack_size)(ThreadId tid) |
| 333 | { |
| 334 | vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); |
| 335 | vg_assert(VG_(threads)[tid].status != VgTs_Empty); |
| 336 | return VG_(threads)[tid].client_stack_szB; |
| 337 | } |
| 338 | |
bart | 83c5a92 | 2010-09-02 14:38:38 +0000 | [diff] [blame] | 339 | Addr VG_(thread_get_altstack_min)(ThreadId tid) |
| 340 | { |
| 341 | vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); |
| 342 | vg_assert(VG_(threads)[tid].status != VgTs_Empty); |
| 343 | return (Addr)VG_(threads)[tid].altstack.ss_sp; |
| 344 | } |
| 345 | |
| 346 | SizeT VG_(thread_get_altstack_size)(ThreadId tid) |
| 347 | { |
| 348 | vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); |
| 349 | vg_assert(VG_(threads)[tid].status != VgTs_Empty); |
| 350 | return VG_(threads)[tid].altstack.ss_size; |
| 351 | } |
| 352 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 353 | //------------------------------------------------------------- |
| 354 | /* Details about the capabilities of the underlying (host) CPU. These |
| 355 | details are acquired by (1) enquiring with the CPU at startup, or |
| 356 | (2) from the AT_SYSINFO entries the kernel gave us (ppc32 cache |
| 357 | line size). It's a bit nasty in the sense that there's no obvious |
| 358 | way to stop uses of some of this info before it's ready to go. |
florian | 0daa4e3 | 2012-04-22 02:48:20 +0000 | [diff] [blame] | 359 | See pub_core_machine.h for more information about that. |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 360 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 361 | VG_(machine_get_hwcaps) may use signals (although it attempts to |
| 362 | leave signal state unchanged) and therefore should only be |
| 363 | called before m_main sets up the client's signal state. |
| 364 | */ |
| 365 | |
| 366 | /* --------- State --------- */ |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 367 | static Bool hwcaps_done = False; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 368 | |
| 369 | /* --- all archs --- */ |
| 370 | static VexArch va; |
| 371 | static VexArchInfo vai; |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 372 | |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 373 | #if defined(VGA_x86) |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 374 | UInt VG_(machine_x86_have_mxcsr) = 0; |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 375 | #endif |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 376 | #if defined(VGA_ppc32) |
sewardj | 2c36d42 | 2005-11-13 01:59:22 +0000 | [diff] [blame] | 377 | UInt VG_(machine_ppc32_has_FP) = 0; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 378 | UInt VG_(machine_ppc32_has_VMX) = 0; |
| 379 | #endif |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 380 | #if defined(VGA_ppc64) |
| 381 | ULong VG_(machine_ppc64_has_VMX) = 0; |
| 382 | #endif |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 383 | #if defined(VGA_arm) |
| 384 | Int VG_(machine_arm_archlevel) = 4; |
| 385 | #endif |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 386 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 387 | /* fixs390: anything for s390x here ? */ |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 388 | |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 389 | /* For hwcaps detection on ppc32/64, s390x, and arm we'll need to do SIGILL |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 390 | testing, so we need a VG_MINIMAL_JMP_BUF. */ |
| 391 | #if defined(VGA_ppc32) || defined(VGA_ppc64) \ |
| 392 | || defined(VGA_arm) || defined(VGA_s390x) |
| 393 | #include "pub_tool_libcsetjmp.h" |
sewardj | 97d3ebb | 2011-04-11 18:36:34 +0000 | [diff] [blame] | 394 | static VG_MINIMAL_JMP_BUF(env_unsup_insn); |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 395 | static void handler_unsup_insn ( Int x ) { |
| 396 | VG_MINIMAL_LONGJMP(env_unsup_insn); |
| 397 | } |
sewardj | 5ae70ce | 2010-09-08 15:12:33 +0000 | [diff] [blame] | 398 | #endif |
| 399 | |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 400 | |
| 401 | /* Helper function for VG_(machine_get_hwcaps), assumes the SIGILL/etc |
| 402 | * handlers are installed. Determines the the sizes affected by dcbz |
| 403 | * and dcbzl instructions and updates the given VexArchInfo structure |
| 404 | * accordingly. |
| 405 | * |
| 406 | * Not very defensive: assumes that as long as the dcbz/dcbzl |
| 407 | * instructions don't raise a SIGILL, that they will zero an aligned, |
| 408 | * contiguous block of memory of a sensible size. */ |
sewardj | 5ae70ce | 2010-09-08 15:12:33 +0000 | [diff] [blame] | 409 | #if defined(VGA_ppc32) || defined(VGA_ppc64) |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 410 | static void find_ppc_dcbz_sz(VexArchInfo *arch_info) |
| 411 | { |
sewardj | dcb3a59 | 2010-09-04 00:43:14 +0000 | [diff] [blame] | 412 | Int dcbz_szB = 0; |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 413 | Int dcbzl_szB; |
sewardj | 5ae70ce | 2010-09-08 15:12:33 +0000 | [diff] [blame] | 414 | # define MAX_DCBZL_SZB (128) /* largest known effect of dcbzl */ |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 415 | char test_block[4*MAX_DCBZL_SZB]; |
| 416 | char *aligned = test_block; |
| 417 | Int i; |
| 418 | |
| 419 | /* round up to next max block size, assumes MAX_DCBZL_SZB is pof2 */ |
| 420 | aligned = (char *)(((HWord)aligned + MAX_DCBZL_SZB) & ~(MAX_DCBZL_SZB - 1)); |
| 421 | vg_assert((aligned + MAX_DCBZL_SZB) <= &test_block[sizeof(test_block)]); |
| 422 | |
| 423 | /* dcbz often clears 32B, although sometimes whatever the native cache |
| 424 | * block size is */ |
| 425 | VG_(memset)(test_block, 0xff, sizeof(test_block)); |
| 426 | __asm__ __volatile__("dcbz 0,%0" |
| 427 | : /*out*/ |
| 428 | : "r" (aligned) /*in*/ |
| 429 | : "memory" /*clobber*/); |
| 430 | for (dcbz_szB = 0, i = 0; i < sizeof(test_block); ++i) { |
| 431 | if (!test_block[i]) |
| 432 | ++dcbz_szB; |
| 433 | } |
| 434 | vg_assert(dcbz_szB == 32 || dcbz_szB == 64 || dcbz_szB == 128); |
| 435 | |
| 436 | /* dcbzl clears 128B on G5/PPC970, and usually 32B on other platforms */ |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 437 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 438 | dcbzl_szB = 0; /* indicates unsupported */ |
| 439 | } |
| 440 | else { |
| 441 | VG_(memset)(test_block, 0xff, sizeof(test_block)); |
| 442 | /* some older assemblers won't understand the dcbzl instruction |
| 443 | * variant, so we directly emit the instruction ourselves */ |
| 444 | __asm__ __volatile__("mr 9, %0 ; .long 0x7C204FEC" /*dcbzl 0,9*/ |
| 445 | : /*out*/ |
| 446 | : "r" (aligned) /*in*/ |
| 447 | : "memory", "r9" /*clobber*/); |
| 448 | for (dcbzl_szB = 0, i = 0; i < sizeof(test_block); ++i) { |
| 449 | if (!test_block[i]) |
| 450 | ++dcbzl_szB; |
| 451 | } |
| 452 | vg_assert(dcbzl_szB == 32 || dcbzl_szB == 64 || dcbzl_szB == 128); |
| 453 | } |
| 454 | |
| 455 | arch_info->ppc_dcbz_szB = dcbz_szB; |
| 456 | arch_info->ppc_dcbzl_szB = dcbzl_szB; |
| 457 | |
| 458 | VG_(debugLog)(1, "machine", "dcbz_szB=%d dcbzl_szB=%d\n", |
| 459 | dcbz_szB, dcbzl_szB); |
sewardj | 5ae70ce | 2010-09-08 15:12:33 +0000 | [diff] [blame] | 460 | # undef MAX_DCBZL_SZB |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 461 | } |
sewardj | 5ae70ce | 2010-09-08 15:12:33 +0000 | [diff] [blame] | 462 | #endif /* defined(VGA_ppc32) || defined(VGA_ppc64) */ |
| 463 | |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 464 | #ifdef VGA_s390x |
sewardj | 5ae70ce | 2010-09-08 15:12:33 +0000 | [diff] [blame] | 465 | |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 466 | /* Read /proc/cpuinfo. Look for lines like these |
| 467 | |
| 468 | processor 0: version = FF, identification = 0117C9, machine = 2064 |
| 469 | |
florian | 4e63b8e | 2012-04-22 03:50:20 +0000 | [diff] [blame^] | 470 | and return the machine model. If the machine model could not be determined |
| 471 | or it is an unknown model, return VEX_S390X_MODEL_UNKNOWN. */ |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 472 | |
| 473 | static UInt VG_(get_machine_model)(void) |
| 474 | { |
| 475 | static struct model_map { |
| 476 | HChar name[5]; |
| 477 | UInt id; |
| 478 | } model_map[] = { |
| 479 | { "2064", VEX_S390X_MODEL_Z900 }, |
| 480 | { "2066", VEX_S390X_MODEL_Z800 }, |
| 481 | { "2084", VEX_S390X_MODEL_Z990 }, |
| 482 | { "2086", VEX_S390X_MODEL_Z890 }, |
| 483 | { "2094", VEX_S390X_MODEL_Z9_EC }, |
| 484 | { "2096", VEX_S390X_MODEL_Z9_BC }, |
| 485 | { "2097", VEX_S390X_MODEL_Z10_EC }, |
| 486 | { "2098", VEX_S390X_MODEL_Z10_BC }, |
| 487 | { "2817", VEX_S390X_MODEL_Z196 }, |
florian | 258ede7 | 2011-09-02 22:20:41 +0000 | [diff] [blame] | 488 | { "2818", VEX_S390X_MODEL_Z114 }, |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | Int model, n, fh; |
| 492 | SysRes fd; |
| 493 | SizeT num_bytes, file_buf_size; |
| 494 | HChar *p, *m, *model_name, *file_buf; |
| 495 | |
| 496 | /* Slurp contents of /proc/cpuinfo into FILE_BUF */ |
| 497 | fd = VG_(open)( "/proc/cpuinfo", 0, VKI_S_IRUSR ); |
florian | 4e63b8e | 2012-04-22 03:50:20 +0000 | [diff] [blame^] | 498 | if ( sr_isError(fd) ) return VEX_S390X_MODEL_UNKNOWN; |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 499 | |
| 500 | fh = sr_Res(fd); |
| 501 | |
| 502 | /* Determine the size of /proc/cpuinfo. |
| 503 | Work around broken-ness in /proc file system implementation. |
| 504 | fstat returns a zero size for /proc/cpuinfo although it is |
| 505 | claimed to be a regular file. */ |
| 506 | num_bytes = 0; |
| 507 | file_buf_size = 1000; |
| 508 | file_buf = VG_(malloc)("cpuinfo", file_buf_size + 1); |
| 509 | while (42) { |
| 510 | n = VG_(read)(fh, file_buf, file_buf_size); |
| 511 | if (n < 0) break; |
| 512 | |
| 513 | num_bytes += n; |
| 514 | if (n < file_buf_size) break; /* reached EOF */ |
| 515 | } |
| 516 | |
| 517 | if (n < 0) num_bytes = 0; /* read error; ignore contents */ |
| 518 | |
| 519 | if (num_bytes > file_buf_size) { |
| 520 | VG_(free)( file_buf ); |
| 521 | VG_(lseek)( fh, 0, VKI_SEEK_SET ); |
| 522 | file_buf = VG_(malloc)( "cpuinfo", num_bytes + 1 ); |
| 523 | n = VG_(read)( fh, file_buf, num_bytes ); |
| 524 | if (n < 0) num_bytes = 0; |
| 525 | } |
| 526 | |
| 527 | file_buf[num_bytes] = '\0'; |
| 528 | VG_(close)(fh); |
| 529 | |
| 530 | /* Parse file */ |
florian | 4e63b8e | 2012-04-22 03:50:20 +0000 | [diff] [blame^] | 531 | model = VEX_S390X_MODEL_UNKNOWN; |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 532 | for (p = file_buf; *p; ++p) { |
| 533 | /* Beginning of line */ |
| 534 | if (VG_(strncmp)( p, "processor", sizeof "processor" - 1 ) != 0) continue; |
| 535 | |
| 536 | m = VG_(strstr)( p, "machine" ); |
| 537 | if (m == NULL) continue; |
| 538 | |
| 539 | p = m + sizeof "machine" - 1; |
| 540 | while ( VG_(isspace)( *p ) || *p == '=') { |
| 541 | if (*p == '\n') goto next_line; |
| 542 | ++p; |
| 543 | } |
| 544 | |
| 545 | model_name = p; |
| 546 | for (n = 0; n < sizeof model_map / sizeof model_map[0]; ++n) { |
| 547 | struct model_map *mm = model_map + n; |
| 548 | SizeT len = VG_(strlen)( mm->name ); |
| 549 | if ( VG_(strncmp)( mm->name, model_name, len ) == 0 && |
| 550 | VG_(isspace)( model_name[len] )) { |
| 551 | if (mm->id < model) model = mm->id; |
| 552 | p = model_name + len; |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | /* Skip until end-of-line */ |
| 557 | while (*p != '\n') |
| 558 | ++p; |
| 559 | next_line: ; |
| 560 | } |
| 561 | |
| 562 | VG_(free)( file_buf ); |
florian | 0016492 | 2011-10-25 21:37:15 +0000 | [diff] [blame] | 563 | VG_(debugLog)(1, "machine", "model = %s\n", |
florian | 4e63b8e | 2012-04-22 03:50:20 +0000 | [diff] [blame^] | 564 | model == VEX_S390X_MODEL_UNKNOWN ? "UNKNOWN" |
florian | 0016492 | 2011-10-25 21:37:15 +0000 | [diff] [blame] | 565 | : model_map[model].name); |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 566 | return model; |
| 567 | } |
| 568 | |
| 569 | #endif /* VGA_s390x */ |
sewardj | 5ae70ce | 2010-09-08 15:12:33 +0000 | [diff] [blame] | 570 | |
| 571 | /* Determine what insn set and insn set variant the host has, and |
| 572 | record it. To be called once at system startup. Returns False if |
| 573 | this a CPU incapable of running Valgrind. */ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 574 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 575 | Bool VG_(machine_get_hwcaps)( void ) |
| 576 | { |
| 577 | vg_assert(hwcaps_done == False); |
| 578 | hwcaps_done = True; |
| 579 | |
| 580 | // Whack default settings into vai, so that we only need to fill in |
| 581 | // any interesting bits. |
| 582 | LibVEX_default_VexArchInfo(&vai); |
| 583 | |
| 584 | #if defined(VGA_x86) |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 585 | { Bool have_sse1, have_sse2, have_cx8, have_lzcnt; |
sewardj | c7ffc94 | 2011-03-28 16:26:42 +0000 | [diff] [blame] | 586 | UInt eax, ebx, ecx, edx, max_extended; |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 587 | UChar vstr[13]; |
| 588 | vstr[0] = 0; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 589 | |
| 590 | if (!VG_(has_cpuid)()) |
| 591 | /* we can't do cpuid at all. Give up. */ |
| 592 | return False; |
| 593 | |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 594 | VG_(cpuid)(0, 0, &eax, &ebx, &ecx, &edx); |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 595 | if (eax < 1) |
| 596 | /* we can't ask for cpuid(x) for x > 0. Give up. */ |
| 597 | return False; |
| 598 | |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 599 | /* Get processor ID string, and max basic/extended index |
| 600 | values. */ |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 601 | VG_(memcpy)(&vstr[0], &ebx, 4); |
| 602 | VG_(memcpy)(&vstr[4], &edx, 4); |
| 603 | VG_(memcpy)(&vstr[8], &ecx, 4); |
| 604 | vstr[12] = 0; |
| 605 | |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 606 | VG_(cpuid)(0x80000000, 0, &eax, &ebx, &ecx, &edx); |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 607 | max_extended = eax; |
| 608 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 609 | /* get capabilities bits into edx */ |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 610 | VG_(cpuid)(1, 0, &eax, &ebx, &ecx, &edx); |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 611 | |
| 612 | have_sse1 = (edx & (1<<25)) != 0; /* True => have sse insns */ |
| 613 | have_sse2 = (edx & (1<<26)) != 0; /* True => have sse2 insns */ |
| 614 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 615 | /* cmpxchg8b is a minimum requirement now; if we don't have it we |
| 616 | must simply give up. But all CPUs since Pentium-I have it, so |
| 617 | that doesn't seem like much of a restriction. */ |
| 618 | have_cx8 = (edx & (1<<8)) != 0; /* True => have cmpxchg8b */ |
| 619 | if (!have_cx8) |
| 620 | return False; |
| 621 | |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 622 | /* Figure out if this is an AMD that can do LZCNT. */ |
| 623 | have_lzcnt = False; |
| 624 | if (0 == VG_(strcmp)(vstr, "AuthenticAMD") |
| 625 | && max_extended >= 0x80000001) { |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 626 | VG_(cpuid)(0x80000001, 0, &eax, &ebx, &ecx, &edx); |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 627 | have_lzcnt = (ecx & (1<<5)) != 0; /* True => have LZCNT */ |
| 628 | } |
| 629 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 630 | if (have_sse2 && have_sse1) { |
| 631 | va = VexArchX86; |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 632 | vai.hwcaps = VEX_HWCAPS_X86_SSE1; |
| 633 | vai.hwcaps |= VEX_HWCAPS_X86_SSE2; |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 634 | if (have_lzcnt) |
| 635 | vai.hwcaps |= VEX_HWCAPS_X86_LZCNT; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 636 | VG_(machine_x86_have_mxcsr) = 1; |
| 637 | return True; |
| 638 | } |
| 639 | |
| 640 | if (have_sse1) { |
| 641 | va = VexArchX86; |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 642 | vai.hwcaps = VEX_HWCAPS_X86_SSE1; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 643 | VG_(machine_x86_have_mxcsr) = 1; |
| 644 | return True; |
| 645 | } |
| 646 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 647 | va = VexArchX86; |
| 648 | vai.hwcaps = 0; /*baseline - no sse at all*/ |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 649 | VG_(machine_x86_have_mxcsr) = 0; |
| 650 | return True; |
| 651 | } |
| 652 | |
| 653 | #elif defined(VGA_amd64) |
sewardj | c7ffc94 | 2011-03-28 16:26:42 +0000 | [diff] [blame] | 654 | { Bool have_sse3, have_cx8, have_cx16; |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 655 | Bool have_lzcnt; |
sewardj | c7ffc94 | 2011-03-28 16:26:42 +0000 | [diff] [blame] | 656 | UInt eax, ebx, ecx, edx, max_extended; |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 657 | UChar vstr[13]; |
| 658 | vstr[0] = 0; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 659 | |
| 660 | if (!VG_(has_cpuid)()) |
| 661 | /* we can't do cpuid at all. Give up. */ |
| 662 | return False; |
| 663 | |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 664 | VG_(cpuid)(0, 0, &eax, &ebx, &ecx, &edx); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 665 | if (eax < 1) |
| 666 | /* we can't ask for cpuid(x) for x > 0. Give up. */ |
| 667 | return False; |
| 668 | |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 669 | /* Get processor ID string, and max basic/extended index |
| 670 | values. */ |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 671 | VG_(memcpy)(&vstr[0], &ebx, 4); |
| 672 | VG_(memcpy)(&vstr[4], &edx, 4); |
| 673 | VG_(memcpy)(&vstr[8], &ecx, 4); |
| 674 | vstr[12] = 0; |
| 675 | |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 676 | VG_(cpuid)(0x80000000, 0, &eax, &ebx, &ecx, &edx); |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 677 | max_extended = eax; |
| 678 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 679 | /* get capabilities bits into edx */ |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 680 | VG_(cpuid)(1, 0, &eax, &ebx, &ecx, &edx); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 681 | |
sewardj | c7ffc94 | 2011-03-28 16:26:42 +0000 | [diff] [blame] | 682 | // we assume that SSE1 and SSE2 are available by default |
sewardj | 598971e | 2009-07-12 13:19:04 +0000 | [diff] [blame] | 683 | have_sse3 = (ecx & (1<<0)) != 0; /* True => have sse3 insns */ |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 684 | // ssse3 is ecx:9 |
| 685 | // sse41 is ecx:19 |
| 686 | // sse42 is ecx:20 |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 687 | |
| 688 | /* cmpxchg8b is a minimum requirement now; if we don't have it we |
| 689 | must simply give up. But all CPUs since Pentium-I have it, so |
| 690 | that doesn't seem like much of a restriction. */ |
| 691 | have_cx8 = (edx & (1<<8)) != 0; /* True => have cmpxchg8b */ |
| 692 | if (!have_cx8) |
| 693 | return False; |
| 694 | |
| 695 | /* on amd64 we tolerate older cpus, which don't have cmpxchg16b */ |
| 696 | have_cx16 = (ecx & (1<<13)) != 0; /* True => have cmpxchg16b */ |
| 697 | |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 698 | /* Figure out if this is an AMD that can do LZCNT. */ |
| 699 | have_lzcnt = False; |
| 700 | if (0 == VG_(strcmp)(vstr, "AuthenticAMD") |
| 701 | && max_extended >= 0x80000001) { |
tom | ad8a591 | 2011-06-10 15:04:22 +0000 | [diff] [blame] | 702 | VG_(cpuid)(0x80000001, 0, &eax, &ebx, &ecx, &edx); |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 703 | have_lzcnt = (ecx & (1<<5)) != 0; /* True => have LZCNT */ |
| 704 | } |
| 705 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 706 | va = VexArchAMD64; |
| 707 | vai.hwcaps = (have_sse3 ? VEX_HWCAPS_AMD64_SSE3 : 0) |
sewardj | 40d91c7 | 2010-07-29 15:40:32 +0000 | [diff] [blame] | 708 | | (have_cx16 ? VEX_HWCAPS_AMD64_CX16 : 0) |
| 709 | | (have_lzcnt ? VEX_HWCAPS_AMD64_LZCNT : 0); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 710 | return True; |
| 711 | } |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 712 | |
| 713 | #elif defined(VGA_ppc32) |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 714 | { |
| 715 | /* Find out which subset of the ppc32 instruction set is supported by |
| 716 | verifying whether various ppc32 instructions generate a SIGILL |
| 717 | or a SIGFPE. An alternative approach is to check the AT_HWCAP and |
| 718 | AT_PLATFORM entries in the ELF auxiliary table -- see also |
| 719 | the_iifii.client_auxv in m_main.c. |
| 720 | */ |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 721 | vki_sigset_t saved_set, tmp_set; |
| 722 | vki_sigaction_fromK_t saved_sigill_act, saved_sigfpe_act; |
| 723 | vki_sigaction_toK_t tmp_sigill_act, tmp_sigfpe_act; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 724 | |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 725 | volatile Bool have_F, have_V, have_FX, have_GX, have_VX, have_DFP; |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 726 | Int r; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 727 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 728 | /* This is a kludge. Really we ought to back-convert saved_act |
| 729 | into a toK_t using VG_(convert_sigaction_fromK_to_toK), but |
| 730 | since that's a no-op on all ppc32 platforms so far supported, |
| 731 | it's not worth the typing effort. At least include most basic |
| 732 | sanity check: */ |
| 733 | vg_assert(sizeof(vki_sigaction_fromK_t) == sizeof(vki_sigaction_toK_t)); |
| 734 | |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 735 | VG_(sigemptyset)(&tmp_set); |
| 736 | VG_(sigaddset)(&tmp_set, VKI_SIGILL); |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 737 | VG_(sigaddset)(&tmp_set, VKI_SIGFPE); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 738 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 739 | r = VG_(sigprocmask)(VKI_SIG_UNBLOCK, &tmp_set, &saved_set); |
| 740 | vg_assert(r == 0); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 741 | |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 742 | r = VG_(sigaction)(VKI_SIGILL, NULL, &saved_sigill_act); |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 743 | vg_assert(r == 0); |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 744 | tmp_sigill_act = saved_sigill_act; |
| 745 | |
| 746 | r = VG_(sigaction)(VKI_SIGFPE, NULL, &saved_sigfpe_act); |
| 747 | vg_assert(r == 0); |
| 748 | tmp_sigfpe_act = saved_sigfpe_act; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 749 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 750 | /* NODEFER: signal handler does not return (from the kernel's point of |
| 751 | view), hence if it is to successfully catch a signal more than once, |
| 752 | we need the NODEFER flag. */ |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 753 | tmp_sigill_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 754 | tmp_sigill_act.sa_flags &= ~VKI_SA_SIGINFO; |
| 755 | tmp_sigill_act.sa_flags |= VKI_SA_NODEFER; |
| 756 | tmp_sigill_act.ksa_handler = handler_unsup_insn; |
| 757 | r = VG_(sigaction)(VKI_SIGILL, &tmp_sigill_act, NULL); |
| 758 | vg_assert(r == 0); |
| 759 | |
| 760 | tmp_sigfpe_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 761 | tmp_sigfpe_act.sa_flags &= ~VKI_SA_SIGINFO; |
| 762 | tmp_sigfpe_act.sa_flags |= VKI_SA_NODEFER; |
| 763 | tmp_sigfpe_act.ksa_handler = handler_unsup_insn; |
| 764 | r = VG_(sigaction)(VKI_SIGFPE, &tmp_sigfpe_act, NULL); |
| 765 | vg_assert(r == 0); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 766 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 767 | /* standard FP insns */ |
| 768 | have_F = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 769 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 770 | have_F = False; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 771 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 772 | __asm__ __volatile__(".long 0xFC000090"); /*fmr 0,0 */ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 773 | } |
| 774 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 775 | /* Altivec insns */ |
| 776 | have_V = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 777 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 778 | have_V = False; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 779 | } else { |
sewardj | 7159212 | 2006-04-04 03:29:48 +0000 | [diff] [blame] | 780 | /* Unfortunately some older assemblers don't speak Altivec (or |
| 781 | choose not to), so to be safe we directly emit the 32-bit |
| 782 | word corresponding to "vor 0,0,0". This fixes a build |
| 783 | problem that happens on Debian 3.1 (ppc32), and probably |
| 784 | various other places. */ |
sewardj | 54d0dc7 | 2006-04-04 03:08:49 +0000 | [diff] [blame] | 785 | __asm__ __volatile__(".long 0x10000484"); /*vor 0,0,0*/ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 786 | } |
| 787 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 788 | /* General-Purpose optional (fsqrt, fsqrts) */ |
| 789 | have_FX = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 790 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 791 | have_FX = False; |
| 792 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 793 | __asm__ __volatile__(".long 0xFC00002C"); /*fsqrt 0,0 */ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* Graphics optional (stfiwx, fres, frsqrte, fsel) */ |
| 797 | have_GX = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 798 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 799 | have_GX = False; |
| 800 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 801 | __asm__ __volatile__(".long 0xFC000034"); /* frsqrte 0,0 */ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 802 | } |
| 803 | |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 804 | /* VSX support implies Power ISA 2.06 */ |
| 805 | have_VX = True; |
| 806 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
| 807 | have_VX = False; |
| 808 | } else { |
| 809 | __asm__ __volatile__(".long 0xf0000564"); /* xsabsdp XT,XB */ |
| 810 | } |
| 811 | |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 812 | /* Check for Decimal Floating Point (DFP) support. */ |
| 813 | have_DFP = True; |
| 814 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
| 815 | have_DFP = False; |
| 816 | } else { |
| 817 | __asm__ __volatile__(".long 0xee4e8005"); /* dadd FRT,FRA, FRB */ |
| 818 | } |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 819 | |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 820 | /* determine dcbz/dcbzl sizes while we still have the signal |
| 821 | * handlers registered */ |
| 822 | find_ppc_dcbz_sz(&vai); |
| 823 | |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 824 | r = VG_(sigaction)(VKI_SIGILL, &saved_sigill_act, NULL); |
| 825 | vg_assert(r == 0); |
| 826 | r = VG_(sigaction)(VKI_SIGFPE, &saved_sigfpe_act, NULL); |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 827 | vg_assert(r == 0); |
| 828 | r = VG_(sigprocmask)(VKI_SIG_SETMASK, &saved_set, NULL); |
| 829 | vg_assert(r == 0); |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 830 | VG_(debugLog)(1, "machine", "F %d V %d FX %d GX %d VX %d DFP %d\n", |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 831 | (Int)have_F, (Int)have_V, (Int)have_FX, |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 832 | (Int)have_GX, (Int)have_VX, (Int)have_DFP); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 833 | /* Make FP a prerequisite for VMX (bogusly so), and for FX and GX. */ |
| 834 | if (have_V && !have_F) |
| 835 | have_V = False; |
| 836 | if (have_FX && !have_F) |
| 837 | have_FX = False; |
| 838 | if (have_GX && !have_F) |
| 839 | have_GX = False; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 840 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 841 | VG_(machine_ppc32_has_FP) = have_F ? 1 : 0; |
| 842 | VG_(machine_ppc32_has_VMX) = have_V ? 1 : 0; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 843 | |
| 844 | va = VexArchPPC32; |
| 845 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 846 | vai.hwcaps = 0; |
| 847 | if (have_F) vai.hwcaps |= VEX_HWCAPS_PPC32_F; |
| 848 | if (have_V) vai.hwcaps |= VEX_HWCAPS_PPC32_V; |
| 849 | if (have_FX) vai.hwcaps |= VEX_HWCAPS_PPC32_FX; |
| 850 | if (have_GX) vai.hwcaps |= VEX_HWCAPS_PPC32_GX; |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 851 | if (have_VX) vai.hwcaps |= VEX_HWCAPS_PPC32_VX; |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 852 | if (have_DFP) vai.hwcaps |= VEX_HWCAPS_PPC32_DFP; |
| 853 | |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 854 | |
| 855 | /* But we're not done yet: VG_(machine_ppc32_set_clszB) must be |
| 856 | called before we're ready to go. */ |
| 857 | return True; |
| 858 | } |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 859 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 860 | #elif defined(VGA_ppc64) |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 861 | { |
| 862 | /* Same instruction set detection algorithm as for ppc32. */ |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 863 | vki_sigset_t saved_set, tmp_set; |
| 864 | vki_sigaction_fromK_t saved_sigill_act, saved_sigfpe_act; |
| 865 | vki_sigaction_toK_t tmp_sigill_act, tmp_sigfpe_act; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 866 | |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 867 | volatile Bool have_F, have_V, have_FX, have_GX, have_VX, have_DFP; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 868 | Int r; |
| 869 | |
| 870 | /* This is a kludge. Really we ought to back-convert saved_act |
| 871 | into a toK_t using VG_(convert_sigaction_fromK_to_toK), but |
| 872 | since that's a no-op on all ppc64 platforms so far supported, |
| 873 | it's not worth the typing effort. At least include most basic |
| 874 | sanity check: */ |
| 875 | vg_assert(sizeof(vki_sigaction_fromK_t) == sizeof(vki_sigaction_toK_t)); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 876 | |
| 877 | VG_(sigemptyset)(&tmp_set); |
| 878 | VG_(sigaddset)(&tmp_set, VKI_SIGILL); |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 879 | VG_(sigaddset)(&tmp_set, VKI_SIGFPE); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 880 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 881 | r = VG_(sigprocmask)(VKI_SIG_UNBLOCK, &tmp_set, &saved_set); |
| 882 | vg_assert(r == 0); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 883 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 884 | r = VG_(sigaction)(VKI_SIGILL, NULL, &saved_sigill_act); |
| 885 | vg_assert(r == 0); |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 886 | tmp_sigill_act = saved_sigill_act; |
| 887 | |
| 888 | VG_(sigaction)(VKI_SIGFPE, NULL, &saved_sigfpe_act); |
| 889 | tmp_sigfpe_act = saved_sigfpe_act; |
| 890 | |
sewardj | 7637e9e | 2006-02-21 17:11:11 +0000 | [diff] [blame] | 891 | /* NODEFER: signal handler does not return (from the kernel's point of |
| 892 | view), hence if it is to successfully catch a signal more than once, |
| 893 | we need the NODEFER flag. */ |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 894 | tmp_sigill_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 895 | tmp_sigill_act.sa_flags &= ~VKI_SA_SIGINFO; |
| 896 | tmp_sigill_act.sa_flags |= VKI_SA_NODEFER; |
| 897 | tmp_sigill_act.ksa_handler = handler_unsup_insn; |
| 898 | VG_(sigaction)(VKI_SIGILL, &tmp_sigill_act, NULL); |
| 899 | |
| 900 | tmp_sigfpe_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 901 | tmp_sigfpe_act.sa_flags &= ~VKI_SA_SIGINFO; |
| 902 | tmp_sigfpe_act.sa_flags |= VKI_SA_NODEFER; |
| 903 | tmp_sigfpe_act.ksa_handler = handler_unsup_insn; |
| 904 | VG_(sigaction)(VKI_SIGFPE, &tmp_sigfpe_act, NULL); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 905 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 906 | /* standard FP insns */ |
| 907 | have_F = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 908 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 909 | have_F = False; |
| 910 | } else { |
| 911 | __asm__ __volatile__("fmr 0,0"); |
| 912 | } |
| 913 | |
| 914 | /* Altivec insns */ |
| 915 | have_V = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 916 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 917 | have_V = False; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 918 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 919 | __asm__ __volatile__(".long 0x10000484"); /*vor 0,0,0*/ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 920 | } |
| 921 | |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 922 | /* General-Purpose optional (fsqrt, fsqrts) */ |
| 923 | have_FX = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 924 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 925 | have_FX = False; |
| 926 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 927 | __asm__ __volatile__(".long 0xFC00002C"); /*fsqrt 0,0*/ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /* Graphics optional (stfiwx, fres, frsqrte, fsel) */ |
| 931 | have_GX = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 932 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 933 | have_GX = False; |
| 934 | } else { |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 935 | __asm__ __volatile__(".long 0xFC000034"); /*frsqrte 0,0*/ |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 936 | } |
| 937 | |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 938 | /* VSX support implies Power ISA 2.06 */ |
| 939 | have_VX = True; |
| 940 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
| 941 | have_VX = False; |
| 942 | } else { |
| 943 | __asm__ __volatile__(".long 0xf0000564"); /* xsabsdp XT,XB */ |
| 944 | } |
| 945 | |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 946 | /* Check for Decimal Floating Point (DFP) support. */ |
| 947 | have_DFP = True; |
| 948 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
| 949 | have_DFP = False; |
| 950 | } else { |
| 951 | __asm__ __volatile__(".long 0xee4e8005"); /* dadd FRT,FRA, FRB */ |
| 952 | } |
| 953 | |
sewardj | b9c815b | 2010-09-03 15:51:34 +0000 | [diff] [blame] | 954 | /* determine dcbz/dcbzl sizes while we still have the signal |
| 955 | * handlers registered */ |
| 956 | find_ppc_dcbz_sz(&vai); |
| 957 | |
bart | 1581e74 | 2009-01-13 07:49:14 +0000 | [diff] [blame] | 958 | VG_(sigaction)(VKI_SIGILL, &saved_sigill_act, NULL); |
| 959 | VG_(sigaction)(VKI_SIGFPE, &saved_sigfpe_act, NULL); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 960 | VG_(sigprocmask)(VKI_SIG_SETMASK, &saved_set, NULL); |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 961 | VG_(debugLog)(1, "machine", "F %d V %d FX %d GX %d VX %d DFP %d\n", |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 962 | (Int)have_F, (Int)have_V, (Int)have_FX, |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 963 | (Int)have_GX, (Int)have_VX, (Int)have_DFP); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 964 | /* on ppc64, if we don't even have FP, just give up. */ |
| 965 | if (!have_F) |
| 966 | return False; |
| 967 | |
| 968 | VG_(machine_ppc64_has_VMX) = have_V ? 1 : 0; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 969 | |
| 970 | va = VexArchPPC64; |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 971 | |
| 972 | vai.hwcaps = 0; |
| 973 | if (have_V) vai.hwcaps |= VEX_HWCAPS_PPC64_V; |
| 974 | if (have_FX) vai.hwcaps |= VEX_HWCAPS_PPC64_FX; |
| 975 | if (have_GX) vai.hwcaps |= VEX_HWCAPS_PPC64_GX; |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 976 | if (have_VX) vai.hwcaps |= VEX_HWCAPS_PPC64_VX; |
sewardj | 5ba075a | 2012-04-02 21:25:14 +0000 | [diff] [blame] | 977 | if (have_DFP) vai.hwcaps |= VEX_HWCAPS_PPC64_DFP; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 978 | |
| 979 | /* But we're not done yet: VG_(machine_ppc64_set_clszB) must be |
| 980 | called before we're ready to go. */ |
| 981 | return True; |
| 982 | } |
| 983 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 984 | #elif defined(VGA_s390x) |
| 985 | { |
| 986 | /* Instruction set detection code borrowed from ppc above. */ |
| 987 | vki_sigset_t saved_set, tmp_set; |
| 988 | vki_sigaction_fromK_t saved_sigill_act; |
| 989 | vki_sigaction_toK_t tmp_sigill_act; |
| 990 | |
sewardj | 1c6f6ab | 2011-04-27 12:00:51 +0000 | [diff] [blame] | 991 | volatile Bool have_LDISP, have_EIMM, have_GIE, have_DFP, have_FGX; |
florian | d19733c | 2012-01-15 21:02:44 +0000 | [diff] [blame] | 992 | volatile Bool have_STFLE, have_ETF2; |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 993 | Int r, model; |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 994 | |
| 995 | /* Unblock SIGILL and stash away the old action for that signal */ |
| 996 | VG_(sigemptyset)(&tmp_set); |
| 997 | VG_(sigaddset)(&tmp_set, VKI_SIGILL); |
| 998 | |
| 999 | r = VG_(sigprocmask)(VKI_SIG_UNBLOCK, &tmp_set, &saved_set); |
| 1000 | vg_assert(r == 0); |
| 1001 | |
| 1002 | r = VG_(sigaction)(VKI_SIGILL, NULL, &saved_sigill_act); |
| 1003 | vg_assert(r == 0); |
| 1004 | tmp_sigill_act = saved_sigill_act; |
| 1005 | |
| 1006 | /* NODEFER: signal handler does not return (from the kernel's point of |
| 1007 | view), hence if it is to successfully catch a signal more than once, |
| 1008 | we need the NODEFER flag. */ |
| 1009 | tmp_sigill_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 1010 | tmp_sigill_act.sa_flags &= ~VKI_SA_SIGINFO; |
| 1011 | tmp_sigill_act.sa_flags |= VKI_SA_NODEFER; |
| 1012 | tmp_sigill_act.ksa_handler = handler_unsup_insn; |
| 1013 | VG_(sigaction)(VKI_SIGILL, &tmp_sigill_act, NULL); |
| 1014 | |
| 1015 | /* Determine hwcaps. Note, we cannot use the stfle insn because it |
| 1016 | is not supported on z900. */ |
| 1017 | |
| 1018 | have_LDISP = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1019 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1020 | have_LDISP = False; |
| 1021 | } else { |
| 1022 | /* BASR loads the address of the next insn into r1. Needed to avoid |
| 1023 | a segfault in XY. */ |
| 1024 | __asm__ __volatile__("basr %%r1,%%r0\n\t" |
| 1025 | ".long 0xe3001000\n\t" /* XY 0,0(%r1) */ |
| 1026 | ".short 0x0057" : : : "r0", "r1", "cc", "memory"); |
| 1027 | } |
| 1028 | |
| 1029 | have_EIMM = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1030 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1031 | have_EIMM = False; |
| 1032 | } else { |
| 1033 | __asm__ __volatile__(".long 0xc0090000\n\t" /* iilf r0,0 */ |
| 1034 | ".short 0x0000" : : : "r0", "memory"); |
| 1035 | } |
| 1036 | |
| 1037 | have_GIE = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1038 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1039 | have_GIE = False; |
| 1040 | } else { |
| 1041 | __asm__ __volatile__(".long 0xc2010000\n\t" /* msfi r0,0 */ |
| 1042 | ".short 0x0000" : : : "r0", "memory"); |
| 1043 | } |
| 1044 | |
| 1045 | have_DFP = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1046 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1047 | have_DFP = False; |
| 1048 | } else { |
| 1049 | __asm__ __volatile__(".long 0xb3d20000" |
| 1050 | : : : "r0", "cc", "memory"); /* adtr r0,r0,r0 */ |
| 1051 | } |
| 1052 | |
sewardj | 1c6f6ab | 2011-04-27 12:00:51 +0000 | [diff] [blame] | 1053 | have_FGX = True; |
| 1054 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
| 1055 | have_FGX = False; |
| 1056 | } else { |
| 1057 | __asm__ __volatile__(".long 0xb3cd0000" : : : "r0"); /* lgdr r0,f0 */ |
| 1058 | } |
| 1059 | |
florian | d19733c | 2012-01-15 21:02:44 +0000 | [diff] [blame] | 1060 | /* Detect presence of the ETF2-enhancement facility using the |
| 1061 | STFLE insn. Note, that STFLE and ETF2 were introduced at the same |
| 1062 | time, so the absence of STLFE implies the absence of ETF2. */ |
| 1063 | have_STFLE = True; |
| 1064 | have_ETF2 = False; |
| 1065 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
| 1066 | have_STFLE = False; |
| 1067 | } else { |
| 1068 | ULong hoststfle[1]; |
| 1069 | register ULong reg0 asm("0") = 0; /* one double word available */ |
| 1070 | |
| 1071 | __asm__ __volatile__(" .insn s,0xb2b00000,%0\n" /* stfle */ |
| 1072 | : "=m" (hoststfle), "+d"(reg0) |
| 1073 | : : "cc", "memory"); |
| 1074 | if (hoststfle[0] & (1ULL << (63 - 24))) |
| 1075 | have_ETF2 = True; |
| 1076 | } |
| 1077 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1078 | /* Restore signals */ |
| 1079 | r = VG_(sigaction)(VKI_SIGILL, &saved_sigill_act, NULL); |
| 1080 | vg_assert(r == 0); |
| 1081 | r = VG_(sigprocmask)(VKI_SIG_SETMASK, &saved_set, NULL); |
| 1082 | vg_assert(r == 0); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1083 | va = VexArchS390X; |
| 1084 | |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 1085 | model = VG_(get_machine_model)(); |
| 1086 | |
florian | 4e63b8e | 2012-04-22 03:50:20 +0000 | [diff] [blame^] | 1087 | /* If the model is "unknown" don't treat this as an error. Assume |
| 1088 | this is a brand-new machine model for which we don't have the |
| 1089 | identification yet. Keeping fingers crossed. */ |
| 1090 | |
sewardj | 1c6f6ab | 2011-04-27 12:00:51 +0000 | [diff] [blame] | 1091 | VG_(debugLog)(1, "machine", "machine %d LDISP %d EIMM %d GIE %d DFP %d " |
florian | d19733c | 2012-01-15 21:02:44 +0000 | [diff] [blame] | 1092 | "FGX %d STFLE %d ETF2 %d\n", model, have_LDISP, have_EIMM, |
| 1093 | have_GIE, have_DFP, have_FGX, have_STFLE, have_ETF2); |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 1094 | |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 1095 | vai.hwcaps = model; |
| 1096 | if (have_LDISP) { |
| 1097 | /* Use long displacement only on machines >= z990. For all other machines |
| 1098 | it is millicoded and therefore slow. */ |
| 1099 | if (model >= VEX_S390X_MODEL_Z990) |
| 1100 | vai.hwcaps |= VEX_HWCAPS_S390X_LDISP; |
| 1101 | } |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1102 | if (have_EIMM) vai.hwcaps |= VEX_HWCAPS_S390X_EIMM; |
| 1103 | if (have_GIE) vai.hwcaps |= VEX_HWCAPS_S390X_GIE; |
| 1104 | if (have_DFP) vai.hwcaps |= VEX_HWCAPS_S390X_DFP; |
sewardj | 1c6f6ab | 2011-04-27 12:00:51 +0000 | [diff] [blame] | 1105 | if (have_FGX) vai.hwcaps |= VEX_HWCAPS_S390X_FGX; |
florian | d19733c | 2012-01-15 21:02:44 +0000 | [diff] [blame] | 1106 | if (have_ETF2) vai.hwcaps |= VEX_HWCAPS_S390X_ETF2; |
florian | 68aba52 | 2012-04-21 15:43:25 +0000 | [diff] [blame] | 1107 | if (have_STFLE) vai.hwcaps |= VEX_HWCAPS_S390X_STFLE; |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1108 | |
sewardj | 2c3f2ea | 2011-04-13 15:40:10 +0000 | [diff] [blame] | 1109 | VG_(debugLog)(1, "machine", "hwcaps = 0x%x\n", vai.hwcaps); |
| 1110 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1111 | return True; |
| 1112 | } |
| 1113 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1114 | #elif defined(VGA_arm) |
| 1115 | { |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 1116 | /* Same instruction set detection algorithm as for ppc32. */ |
| 1117 | vki_sigset_t saved_set, tmp_set; |
| 1118 | vki_sigaction_fromK_t saved_sigill_act, saved_sigfpe_act; |
| 1119 | vki_sigaction_toK_t tmp_sigill_act, tmp_sigfpe_act; |
| 1120 | |
| 1121 | volatile Bool have_VFP, have_VFP2, have_VFP3, have_NEON; |
| 1122 | volatile Int archlevel; |
| 1123 | Int r; |
| 1124 | |
| 1125 | /* This is a kludge. Really we ought to back-convert saved_act |
| 1126 | into a toK_t using VG_(convert_sigaction_fromK_to_toK), but |
| 1127 | since that's a no-op on all ppc64 platforms so far supported, |
| 1128 | it's not worth the typing effort. At least include most basic |
| 1129 | sanity check: */ |
| 1130 | vg_assert(sizeof(vki_sigaction_fromK_t) == sizeof(vki_sigaction_toK_t)); |
| 1131 | |
| 1132 | VG_(sigemptyset)(&tmp_set); |
| 1133 | VG_(sigaddset)(&tmp_set, VKI_SIGILL); |
| 1134 | VG_(sigaddset)(&tmp_set, VKI_SIGFPE); |
| 1135 | |
| 1136 | r = VG_(sigprocmask)(VKI_SIG_UNBLOCK, &tmp_set, &saved_set); |
| 1137 | vg_assert(r == 0); |
| 1138 | |
| 1139 | r = VG_(sigaction)(VKI_SIGILL, NULL, &saved_sigill_act); |
| 1140 | vg_assert(r == 0); |
| 1141 | tmp_sigill_act = saved_sigill_act; |
| 1142 | |
| 1143 | VG_(sigaction)(VKI_SIGFPE, NULL, &saved_sigfpe_act); |
| 1144 | tmp_sigfpe_act = saved_sigfpe_act; |
| 1145 | |
| 1146 | /* NODEFER: signal handler does not return (from the kernel's point of |
| 1147 | view), hence if it is to successfully catch a signal more than once, |
| 1148 | we need the NODEFER flag. */ |
| 1149 | tmp_sigill_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 1150 | tmp_sigill_act.sa_flags &= ~VKI_SA_SIGINFO; |
| 1151 | tmp_sigill_act.sa_flags |= VKI_SA_NODEFER; |
| 1152 | tmp_sigill_act.ksa_handler = handler_unsup_insn; |
| 1153 | VG_(sigaction)(VKI_SIGILL, &tmp_sigill_act, NULL); |
| 1154 | |
| 1155 | tmp_sigfpe_act.sa_flags &= ~VKI_SA_RESETHAND; |
| 1156 | tmp_sigfpe_act.sa_flags &= ~VKI_SA_SIGINFO; |
| 1157 | tmp_sigfpe_act.sa_flags |= VKI_SA_NODEFER; |
| 1158 | tmp_sigfpe_act.ksa_handler = handler_unsup_insn; |
| 1159 | VG_(sigaction)(VKI_SIGFPE, &tmp_sigfpe_act, NULL); |
| 1160 | |
| 1161 | /* VFP insns */ |
| 1162 | have_VFP = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1163 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 1164 | have_VFP = False; |
| 1165 | } else { |
| 1166 | __asm__ __volatile__(".word 0xEEB02B42"); /* VMOV.F64 d2, d2 */ |
| 1167 | } |
| 1168 | /* There are several generation of VFP extension but they differs very |
| 1169 | little so for now we will not distinguish them. */ |
| 1170 | have_VFP2 = have_VFP; |
| 1171 | have_VFP3 = have_VFP; |
| 1172 | |
| 1173 | /* NEON insns */ |
| 1174 | have_NEON = True; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1175 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 1176 | have_NEON = False; |
| 1177 | } else { |
| 1178 | __asm__ __volatile__(".word 0xF2244154"); /* VMOV q2, q2 */ |
| 1179 | } |
| 1180 | |
| 1181 | /* ARM architecture level */ |
| 1182 | archlevel = 5; /* v5 will be base level */ |
| 1183 | if (archlevel < 7) { |
| 1184 | archlevel = 7; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1185 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 1186 | archlevel = 5; |
| 1187 | } else { |
| 1188 | __asm__ __volatile__(".word 0xF45FF000"); /* PLI [PC,#-0] */ |
| 1189 | } |
| 1190 | } |
| 1191 | if (archlevel < 6) { |
| 1192 | archlevel = 6; |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 1193 | if (VG_MINIMAL_SETJMP(env_unsup_insn)) { |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 1194 | archlevel = 5; |
| 1195 | } else { |
| 1196 | __asm__ __volatile__(".word 0xE6822012"); /* PKHBT r2, r2, r2 */ |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | VG_(convert_sigaction_fromK_to_toK)(&saved_sigill_act, &tmp_sigill_act); |
| 1201 | VG_(convert_sigaction_fromK_to_toK)(&saved_sigfpe_act, &tmp_sigfpe_act); |
| 1202 | VG_(sigaction)(VKI_SIGILL, &tmp_sigill_act, NULL); |
| 1203 | VG_(sigaction)(VKI_SIGFPE, &tmp_sigfpe_act, NULL); |
| 1204 | VG_(sigprocmask)(VKI_SIG_SETMASK, &saved_set, NULL); |
| 1205 | |
| 1206 | VG_(debugLog)(1, "machine", "ARMv%d VFP %d VFP2 %d VFP3 %d NEON %d\n", |
| 1207 | archlevel, (Int)have_VFP, (Int)have_VFP2, (Int)have_VFP3, |
| 1208 | (Int)have_NEON); |
| 1209 | |
| 1210 | VG_(machine_arm_archlevel) = archlevel; |
| 1211 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1212 | va = VexArchARM; |
sewardj | 1dbd337 | 2010-08-22 12:21:14 +0000 | [diff] [blame] | 1213 | |
| 1214 | vai.hwcaps = VEX_ARM_ARCHLEVEL(archlevel); |
| 1215 | if (have_VFP3) vai.hwcaps |= VEX_HWCAPS_ARM_VFP3; |
| 1216 | if (have_VFP2) vai.hwcaps |= VEX_HWCAPS_ARM_VFP2; |
| 1217 | if (have_VFP) vai.hwcaps |= VEX_HWCAPS_ARM_VFP; |
| 1218 | if (have_NEON) vai.hwcaps |= VEX_HWCAPS_ARM_NEON; |
| 1219 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1220 | return True; |
| 1221 | } |
| 1222 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1223 | #else |
| 1224 | # error "Unknown arch" |
| 1225 | #endif |
| 1226 | } |
| 1227 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1228 | /* Notify host cpu cache line size. */ |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 1229 | #if defined(VGA_ppc32) |
| 1230 | void VG_(machine_ppc32_set_clszB)( Int szB ) |
| 1231 | { |
| 1232 | vg_assert(hwcaps_done); |
| 1233 | |
| 1234 | /* Either the value must not have been set yet (zero) or we can |
| 1235 | tolerate it being set to the same value multiple times, as the |
| 1236 | stack scanning logic in m_main is a bit stupid. */ |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 1237 | vg_assert(vai.ppc_cache_line_szB == 0 |
| 1238 | || vai.ppc_cache_line_szB == szB); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 1239 | |
sewardj | 89230ca | 2008-05-29 16:39:57 +0000 | [diff] [blame] | 1240 | vg_assert(szB == 32 || szB == 64 || szB == 128); |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 1241 | vai.ppc_cache_line_szB = szB; |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 1242 | } |
| 1243 | #endif |
| 1244 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1245 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1246 | /* Notify host cpu cache line size. */ |
| 1247 | #if defined(VGA_ppc64) |
| 1248 | void VG_(machine_ppc64_set_clszB)( Int szB ) |
| 1249 | { |
| 1250 | vg_assert(hwcaps_done); |
| 1251 | |
| 1252 | /* Either the value must not have been set yet (zero) or we can |
| 1253 | tolerate it being set to the same value multiple times, as the |
| 1254 | stack scanning logic in m_main is a bit stupid. */ |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 1255 | vg_assert(vai.ppc_cache_line_szB == 0 |
| 1256 | || vai.ppc_cache_line_szB == szB); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1257 | |
sewardj | 89230ca | 2008-05-29 16:39:57 +0000 | [diff] [blame] | 1258 | vg_assert(szB == 32 || szB == 64 || szB == 128); |
cerion | 1f0d814 | 2005-12-23 00:57:03 +0000 | [diff] [blame] | 1259 | vai.ppc_cache_line_szB = szB; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1260 | } |
| 1261 | #endif |
| 1262 | |
| 1263 | |
sewardj | a3551be | 2010-09-09 07:25:58 +0000 | [diff] [blame] | 1264 | /* Notify host's ability to handle NEON instructions. */ |
| 1265 | #if defined(VGA_arm) |
| 1266 | void VG_(machine_arm_set_has_NEON)( Bool has_neon ) |
| 1267 | { |
| 1268 | vg_assert(hwcaps_done); |
| 1269 | /* There's nothing else we can sanity check. */ |
| 1270 | |
| 1271 | if (has_neon) { |
| 1272 | vai.hwcaps |= VEX_HWCAPS_ARM_NEON; |
| 1273 | } else { |
| 1274 | vai.hwcaps &= ~VEX_HWCAPS_ARM_NEON; |
| 1275 | } |
| 1276 | } |
| 1277 | #endif |
| 1278 | |
| 1279 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1280 | /* Fetch host cpu info, once established. */ |
| 1281 | void VG_(machine_get_VexArchInfo)( /*OUT*/VexArch* pVa, |
| 1282 | /*OUT*/VexArchInfo* pVai ) |
| 1283 | { |
| 1284 | vg_assert(hwcaps_done); |
sewardj | e3826cf | 2005-11-13 00:30:22 +0000 | [diff] [blame] | 1285 | if (pVa) *pVa = va; |
| 1286 | if (pVai) *pVai = vai; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1287 | } |
sewardj | 7821e2e | 2005-08-08 00:35:46 +0000 | [diff] [blame] | 1288 | |
| 1289 | |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 1290 | // Given a pointer to a function as obtained by "& functionname" in C, |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 1291 | // produce a pointer to the actual entry point for the function. |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 1292 | void* VG_(fnptr_to_fnentry)( void* f ) |
| 1293 | { |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 1294 | # if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \ |
| 1295 | || defined(VGP_arm_linux) \ |
| 1296 | || defined(VGP_ppc32_linux) || defined(VGO_darwin) \ |
| 1297 | || defined(VGP_s390x_linux) |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 1298 | return f; |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 1299 | # elif defined(VGP_ppc64_linux) |
| 1300 | /* ppc64-linux uses the AIX scheme, in which f is a pointer to a |
| 1301 | 3-word function descriptor, of which the first word is the entry |
| 1302 | address. */ |
sewardj | f1c91e0 | 2006-10-17 01:35:58 +0000 | [diff] [blame] | 1303 | UWord* descr = (UWord*)f; |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 1304 | return (void*)(descr[0]); |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 1305 | # else |
| 1306 | # error "Unknown platform" |
| 1307 | # endif |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1310 | /*--------------------------------------------------------------------*/ |
| 1311 | /*--- end ---*/ |
| 1312 | /*--------------------------------------------------------------------*/ |