1 // Copyright 2017 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_SIMULATOR_BASE_H_ 6 #define V8_SIMULATOR_BASE_H_ 7 8 #include <type_traits> 9 10 #include "src/assembler.h" 11 #include "src/globals.h" 12 #include "src/isolate.h" 13 14 #if defined(USE_SIMULATOR) 15 16 namespace v8 { 17 namespace internal { 18 19 class Instruction; 20 class Redirection; 21 22 class SimulatorBase { 23 public: 24 // Call on process start and exit. 25 static void InitializeOncePerProcess(); 26 static void GlobalTearDown(); 27 redirection_mutex()28 static base::Mutex* redirection_mutex() { return redirection_mutex_; } redirection()29 static Redirection* redirection() { return redirection_; } set_redirection(Redirection * r)30 static void set_redirection(Redirection* r) { redirection_ = r; } 31 i_cache_mutex()32 static base::Mutex* i_cache_mutex() { return i_cache_mutex_; } i_cache()33 static base::CustomMatcherHashMap* i_cache() { return i_cache_; } 34 35 // Runtime call support. 36 static Address RedirectExternalReference(Address external_function, 37 ExternalReference::Type type); 38 39 protected: 40 template <typename Return, typename SimT, typename CallImpl, typename... Args> VariadicCall(SimT * sim,CallImpl call,Address entry,Args...args)41 static Return VariadicCall(SimT* sim, CallImpl call, Address entry, 42 Args... args) { 43 // Convert all arguments to intptr_t. Fails if any argument is not integral 44 // or pointer. 45 std::array<intptr_t, sizeof...(args)> args_arr{{ConvertArg(args)...}}; 46 intptr_t ret = (sim->*call)(entry, args_arr.size(), args_arr.data()); 47 return ConvertReturn<Return>(ret); 48 } 49 50 // Convert back integral return types. This is always a narrowing conversion. 51 template <typename T> 52 static typename std::enable_if<std::is_integral<T>::value, T>::type ConvertReturn(intptr_t ret)53 ConvertReturn(intptr_t ret) { 54 static_assert(sizeof(T) <= sizeof(intptr_t), "type bigger than ptrsize"); 55 return static_cast<T>(ret); 56 } 57 58 // Convert back pointer-typed return types. 59 template <typename T> 60 static typename std::enable_if<std::is_pointer<T>::value, T>::type ConvertReturn(intptr_t ret)61 ConvertReturn(intptr_t ret) { 62 return reinterpret_cast<T>(ret); 63 } 64 65 // Convert back void return type (i.e. no return). 66 template <typename T> ConvertReturn(intptr_t ret)67 static typename std::enable_if<std::is_void<T>::value, T>::type ConvertReturn( 68 intptr_t ret) {} 69 70 private: 71 static base::Mutex* redirection_mutex_; 72 static Redirection* redirection_; 73 74 static base::Mutex* i_cache_mutex_; 75 static base::CustomMatcherHashMap* i_cache_; 76 77 // Helper methods to convert arbitrary integer or pointer arguments to the 78 // needed generic argument type intptr_t. 79 80 // Convert integral argument to intptr_t. 81 template <typename T> 82 static typename std::enable_if<std::is_integral<T>::value, intptr_t>::type ConvertArg(T arg)83 ConvertArg(T arg) { 84 static_assert(sizeof(T) <= sizeof(intptr_t), "type bigger than ptrsize"); 85 #if V8_TARGET_ARCH_MIPS64 86 // The MIPS64 calling convention is to sign extend all values, even unsigned 87 // ones. 88 using signed_t = typename std::make_signed<T>::type; 89 return static_cast<intptr_t>(static_cast<signed_t>(arg)); 90 #else 91 // Standard C++ convertion: Sign-extend signed values, zero-extend unsigned 92 // values. 93 return static_cast<intptr_t>(arg); 94 #endif 95 } 96 97 // Convert pointer-typed argument to intptr_t. 98 template <typename T> 99 static typename std::enable_if<std::is_pointer<T>::value, intptr_t>::type ConvertArg(T arg)100 ConvertArg(T arg) { 101 return reinterpret_cast<intptr_t>(arg); 102 } 103 }; 104 105 // When the generated code calls an external reference we need to catch that in 106 // the simulator. The external reference will be a function compiled for the 107 // host architecture. We need to call that function instead of trying to 108 // execute it with the simulator. We do that by redirecting the external 109 // reference to a trapping instruction that is handled by the simulator. We 110 // write the original destination of the jump just at a known offset from the 111 // trapping instruction so the simulator knows what to call. 112 // 113 // The following are trapping instructions used for various architectures: 114 // - V8_TARGET_ARCH_ARM: svc (Supervisor Call) 115 // - V8_TARGET_ARCH_ARM64: svc (Supervisor Call) 116 // - V8_TARGET_ARCH_MIPS: swi (software-interrupt) 117 // - V8_TARGET_ARCH_MIPS64: swi (software-interrupt) 118 // - V8_TARGET_ARCH_PPC: svc (Supervisor Call) 119 // - V8_TARGET_ARCH_S390: svc (Supervisor Call) 120 class Redirection { 121 public: 122 Redirection(Address external_function, ExternalReference::Type type); 123 address_of_instruction()124 Address address_of_instruction() { 125 #if ABI_USES_FUNCTION_DESCRIPTORS 126 return reinterpret_cast<Address>(function_descriptor_); 127 #else 128 return reinterpret_cast<Address>(&instruction_); 129 #endif 130 } 131 external_function()132 void* external_function() { 133 return reinterpret_cast<void*>(external_function_); 134 } type()135 ExternalReference::Type type() { return type_; } 136 137 static Redirection* Get(Address external_function, 138 ExternalReference::Type type); 139 FromInstruction(Instruction * instruction)140 static Redirection* FromInstruction(Instruction* instruction) { 141 Address addr_of_instruction = reinterpret_cast<Address>(instruction); 142 Address addr_of_redirection = 143 addr_of_instruction - offsetof(Redirection, instruction_); 144 return reinterpret_cast<Redirection*>(addr_of_redirection); 145 } 146 ReverseRedirection(intptr_t reg)147 static void* ReverseRedirection(intptr_t reg) { 148 Redirection* redirection = FromInstruction( 149 reinterpret_cast<Instruction*>(reinterpret_cast<void*>(reg))); 150 return redirection->external_function(); 151 } 152 DeleteChain(Redirection * redirection)153 static void DeleteChain(Redirection* redirection) { 154 while (redirection != nullptr) { 155 Redirection* next = redirection->next_; 156 delete redirection; 157 redirection = next; 158 } 159 } 160 161 private: 162 Address external_function_; 163 uint32_t instruction_; 164 ExternalReference::Type type_; 165 Redirection* next_; 166 #if ABI_USES_FUNCTION_DESCRIPTORS 167 intptr_t function_descriptor_[3]; 168 #endif 169 }; 170 171 } // namespace internal 172 } // namespace v8 173 174 #endif // defined(USE_SIMULATOR) 175 #endif // V8_SIMULATOR_BASE_H_ 176