Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1 | // Copyright 2014 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 | |
| 6 | // Declares a Simulator for PPC instructions if we are not generating a native |
| 7 | // PPC binary. This Simulator allows us to run and debug PPC code generation on |
| 8 | // regular desktop machines. |
| 9 | // V8 calls into generated code by "calling" the CALL_GENERATED_CODE macro, |
| 10 | // which will start execution in the Simulator or forwards to the real entry |
| 11 | // on a PPC HW platform. |
| 12 | |
| 13 | #ifndef V8_PPC_SIMULATOR_PPC_H_ |
| 14 | #define V8_PPC_SIMULATOR_PPC_H_ |
| 15 | |
| 16 | #include "src/allocation.h" |
| 17 | |
| 18 | #if !defined(USE_SIMULATOR) |
| 19 | // Running without a simulator on a native ppc platform. |
| 20 | |
| 21 | namespace v8 { |
| 22 | namespace internal { |
| 23 | |
| 24 | // When running without a simulator we call the entry directly. |
| 25 | #define CALL_GENERATED_CODE(entry, p0, p1, p2, p3, p4) \ |
| 26 | (entry(p0, p1, p2, p3, p4)) |
| 27 | |
| 28 | typedef int (*ppc_regexp_matcher)(String*, int, const byte*, const byte*, int*, |
| 29 | int, Address, int, void*, Isolate*); |
| 30 | |
| 31 | |
| 32 | // Call the generated regexp code directly. The code at the entry address |
| 33 | // should act as a function matching the type ppc_regexp_matcher. |
| 34 | // The ninth argument is a dummy that reserves the space used for |
| 35 | // the return address added by the ExitFrame in native calls. |
| 36 | #define CALL_GENERATED_REGEXP_CODE(entry, p0, p1, p2, p3, p4, p5, p6, p7, p8) \ |
| 37 | (FUNCTION_CAST<ppc_regexp_matcher>(entry)(p0, p1, p2, p3, p4, p5, p6, p7, \ |
| 38 | NULL, p8)) |
| 39 | |
| 40 | // The stack limit beyond which we will throw stack overflow errors in |
| 41 | // generated code. Because generated code on ppc uses the C stack, we |
| 42 | // just use the C stack limit. |
| 43 | class SimulatorStack : public v8::internal::AllStatic { |
| 44 | public: |
| 45 | static inline uintptr_t JsLimitFromCLimit(v8::internal::Isolate* isolate, |
| 46 | uintptr_t c_limit) { |
| 47 | USE(isolate); |
| 48 | return c_limit; |
| 49 | } |
| 50 | |
| 51 | static inline uintptr_t RegisterCTryCatch(uintptr_t try_catch_address) { |
| 52 | return try_catch_address; |
| 53 | } |
| 54 | |
| 55 | static inline void UnregisterCTryCatch() {} |
| 56 | }; |
| 57 | } |
| 58 | } // namespace v8::internal |
| 59 | |
| 60 | #else // !defined(USE_SIMULATOR) |
| 61 | // Running with a simulator. |
| 62 | |
| 63 | #include "src/assembler.h" |
| 64 | #include "src/hashmap.h" |
| 65 | #include "src/ppc/constants-ppc.h" |
| 66 | |
| 67 | namespace v8 { |
| 68 | namespace internal { |
| 69 | |
| 70 | class CachePage { |
| 71 | public: |
| 72 | static const int LINE_VALID = 0; |
| 73 | static const int LINE_INVALID = 1; |
| 74 | |
| 75 | static const int kPageShift = 12; |
| 76 | static const int kPageSize = 1 << kPageShift; |
| 77 | static const int kPageMask = kPageSize - 1; |
| 78 | static const int kLineShift = 2; // The cache line is only 4 bytes right now. |
| 79 | static const int kLineLength = 1 << kLineShift; |
| 80 | static const int kLineMask = kLineLength - 1; |
| 81 | |
| 82 | CachePage() { memset(&validity_map_, LINE_INVALID, sizeof(validity_map_)); } |
| 83 | |
| 84 | char* ValidityByte(int offset) { |
| 85 | return &validity_map_[offset >> kLineShift]; |
| 86 | } |
| 87 | |
| 88 | char* CachedData(int offset) { return &data_[offset]; } |
| 89 | |
| 90 | private: |
| 91 | char data_[kPageSize]; // The cached data. |
| 92 | static const int kValidityMapSize = kPageSize >> kLineShift; |
| 93 | char validity_map_[kValidityMapSize]; // One byte per line. |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | class Simulator { |
| 98 | public: |
| 99 | friend class PPCDebugger; |
| 100 | enum Register { |
| 101 | no_reg = -1, |
| 102 | r0 = 0, |
| 103 | sp, |
| 104 | r2, |
| 105 | r3, |
| 106 | r4, |
| 107 | r5, |
| 108 | r6, |
| 109 | r7, |
| 110 | r8, |
| 111 | r9, |
| 112 | r10, |
| 113 | r11, |
| 114 | r12, |
| 115 | r13, |
| 116 | r14, |
| 117 | r15, |
| 118 | r16, |
| 119 | r17, |
| 120 | r18, |
| 121 | r19, |
| 122 | r20, |
| 123 | r21, |
| 124 | r22, |
| 125 | r23, |
| 126 | r24, |
| 127 | r25, |
| 128 | r26, |
| 129 | r27, |
| 130 | r28, |
| 131 | r29, |
| 132 | r30, |
| 133 | fp, |
| 134 | kNumGPRs = 32, |
| 135 | d0 = 0, |
| 136 | d1, |
| 137 | d2, |
| 138 | d3, |
| 139 | d4, |
| 140 | d5, |
| 141 | d6, |
| 142 | d7, |
| 143 | d8, |
| 144 | d9, |
| 145 | d10, |
| 146 | d11, |
| 147 | d12, |
| 148 | d13, |
| 149 | d14, |
| 150 | d15, |
| 151 | d16, |
| 152 | d17, |
| 153 | d18, |
| 154 | d19, |
| 155 | d20, |
| 156 | d21, |
| 157 | d22, |
| 158 | d23, |
| 159 | d24, |
| 160 | d25, |
| 161 | d26, |
| 162 | d27, |
| 163 | d28, |
| 164 | d29, |
| 165 | d30, |
| 166 | d31, |
| 167 | kNumFPRs = 32 |
| 168 | }; |
| 169 | |
| 170 | explicit Simulator(Isolate* isolate); |
| 171 | ~Simulator(); |
| 172 | |
| 173 | // The currently executing Simulator instance. Potentially there can be one |
| 174 | // for each native thread. |
| 175 | static Simulator* current(v8::internal::Isolate* isolate); |
| 176 | |
| 177 | // Accessors for register state. |
| 178 | void set_register(int reg, intptr_t value); |
| 179 | intptr_t get_register(int reg) const; |
| 180 | double get_double_from_register_pair(int reg); |
| 181 | void set_d_register_from_double(int dreg, const double dbl) { |
| 182 | DCHECK(dreg >= 0 && dreg < kNumFPRs); |
| 183 | fp_registers_[dreg] = dbl; |
| 184 | } |
| 185 | double get_double_from_d_register(int dreg) { return fp_registers_[dreg]; } |
| 186 | |
| 187 | // Special case of set_register and get_register to access the raw PC value. |
| 188 | void set_pc(intptr_t value); |
| 189 | intptr_t get_pc() const; |
| 190 | |
| 191 | Address get_sp() { |
| 192 | return reinterpret_cast<Address>(static_cast<intptr_t>(get_register(sp))); |
| 193 | } |
| 194 | |
| 195 | // Accessor to the internal simulator stack area. |
| 196 | uintptr_t StackLimit() const; |
| 197 | |
| 198 | // Executes PPC instructions until the PC reaches end_sim_pc. |
| 199 | void Execute(); |
| 200 | |
| 201 | // Call on program start. |
| 202 | static void Initialize(Isolate* isolate); |
| 203 | |
| 204 | // V8 generally calls into generated JS code with 5 parameters and into |
| 205 | // generated RegExp code with 7 parameters. This is a convenience function, |
| 206 | // which sets up the simulator state and grabs the result on return. |
| 207 | intptr_t Call(byte* entry, int argument_count, ...); |
| 208 | // Alternative: call a 2-argument double function. |
| 209 | void CallFP(byte* entry, double d0, double d1); |
| 210 | int32_t CallFPReturnsInt(byte* entry, double d0, double d1); |
| 211 | double CallFPReturnsDouble(byte* entry, double d0, double d1); |
| 212 | |
| 213 | // Push an address onto the JS stack. |
| 214 | uintptr_t PushAddress(uintptr_t address); |
| 215 | |
| 216 | // Pop an address from the JS stack. |
| 217 | uintptr_t PopAddress(); |
| 218 | |
| 219 | // Debugger input. |
| 220 | void set_last_debugger_input(char* input); |
| 221 | char* last_debugger_input() { return last_debugger_input_; } |
| 222 | |
| 223 | // ICache checking. |
| 224 | static void FlushICache(v8::internal::HashMap* i_cache, void* start, |
| 225 | size_t size); |
| 226 | |
| 227 | // Returns true if pc register contains one of the 'special_values' defined |
| 228 | // below (bad_lr, end_sim_pc). |
| 229 | bool has_bad_pc() const; |
| 230 | |
| 231 | private: |
| 232 | enum special_values { |
| 233 | // Known bad pc value to ensure that the simulator does not execute |
| 234 | // without being properly setup. |
| 235 | bad_lr = -1, |
| 236 | // A pc value used to signal the simulator to stop execution. Generally |
| 237 | // the lr is set to this value on transition from native C code to |
| 238 | // simulated execution, so that the simulator can "return" to the native |
| 239 | // C code. |
| 240 | end_sim_pc = -2 |
| 241 | }; |
| 242 | |
| 243 | // Unsupported instructions use Format to print an error and stop execution. |
| 244 | void Format(Instruction* instr, const char* format); |
| 245 | |
| 246 | // Helper functions to set the conditional flags in the architecture state. |
| 247 | bool CarryFrom(int32_t left, int32_t right, int32_t carry = 0); |
| 248 | bool BorrowFrom(int32_t left, int32_t right); |
| 249 | bool OverflowFrom(int32_t alu_out, int32_t left, int32_t right, |
| 250 | bool addition); |
| 251 | |
| 252 | // Helper functions to decode common "addressing" modes |
| 253 | int32_t GetShiftRm(Instruction* instr, bool* carry_out); |
| 254 | int32_t GetImm(Instruction* instr, bool* carry_out); |
| 255 | void ProcessPUW(Instruction* instr, int num_regs, int operand_size, |
| 256 | intptr_t* start_address, intptr_t* end_address); |
| 257 | void HandleRList(Instruction* instr, bool load); |
| 258 | void HandleVList(Instruction* inst); |
| 259 | void SoftwareInterrupt(Instruction* instr); |
| 260 | |
| 261 | // Stop helper functions. |
| 262 | inline bool isStopInstruction(Instruction* instr); |
| 263 | inline bool isWatchedStop(uint32_t bkpt_code); |
| 264 | inline bool isEnabledStop(uint32_t bkpt_code); |
| 265 | inline void EnableStop(uint32_t bkpt_code); |
| 266 | inline void DisableStop(uint32_t bkpt_code); |
| 267 | inline void IncreaseStopCounter(uint32_t bkpt_code); |
| 268 | void PrintStopInfo(uint32_t code); |
| 269 | |
| 270 | // Read and write memory. |
| 271 | inline uint8_t ReadBU(intptr_t addr); |
| 272 | inline int8_t ReadB(intptr_t addr); |
| 273 | inline void WriteB(intptr_t addr, uint8_t value); |
| 274 | inline void WriteB(intptr_t addr, int8_t value); |
| 275 | |
| 276 | inline uint16_t ReadHU(intptr_t addr, Instruction* instr); |
| 277 | inline int16_t ReadH(intptr_t addr, Instruction* instr); |
| 278 | // Note: Overloaded on the sign of the value. |
| 279 | inline void WriteH(intptr_t addr, uint16_t value, Instruction* instr); |
| 280 | inline void WriteH(intptr_t addr, int16_t value, Instruction* instr); |
| 281 | |
| 282 | inline uint32_t ReadWU(intptr_t addr, Instruction* instr); |
| 283 | inline int32_t ReadW(intptr_t addr, Instruction* instr); |
| 284 | inline void WriteW(intptr_t addr, uint32_t value, Instruction* instr); |
| 285 | inline void WriteW(intptr_t addr, int32_t value, Instruction* instr); |
| 286 | |
| 287 | intptr_t* ReadDW(intptr_t addr); |
| 288 | void WriteDW(intptr_t addr, int64_t value); |
| 289 | |
| 290 | void Trace(Instruction* instr); |
| 291 | void SetCR0(intptr_t result, bool setSO = false); |
| 292 | void ExecuteBranchConditional(Instruction* instr); |
| 293 | void ExecuteExt1(Instruction* instr); |
| 294 | bool ExecuteExt2_10bit(Instruction* instr); |
| 295 | bool ExecuteExt2_9bit_part1(Instruction* instr); |
| 296 | void ExecuteExt2_9bit_part2(Instruction* instr); |
| 297 | void ExecuteExt2(Instruction* instr); |
| 298 | void ExecuteExt4(Instruction* instr); |
| 299 | #if V8_TARGET_ARCH_PPC64 |
| 300 | void ExecuteExt5(Instruction* instr); |
| 301 | #endif |
| 302 | void ExecuteGeneric(Instruction* instr); |
| 303 | |
| 304 | // Executes one instruction. |
| 305 | void ExecuteInstruction(Instruction* instr); |
| 306 | |
| 307 | // ICache. |
| 308 | static void CheckICache(v8::internal::HashMap* i_cache, Instruction* instr); |
| 309 | static void FlushOnePage(v8::internal::HashMap* i_cache, intptr_t start, |
| 310 | int size); |
| 311 | static CachePage* GetCachePage(v8::internal::HashMap* i_cache, void* page); |
| 312 | |
| 313 | // Runtime call support. |
| 314 | static void* RedirectExternalReference( |
| 315 | void* external_function, v8::internal::ExternalReference::Type type); |
| 316 | |
| 317 | // Handle arguments and return value for runtime FP functions. |
| 318 | void GetFpArgs(double* x, double* y, intptr_t* z); |
| 319 | void SetFpResult(const double& result); |
| 320 | void TrashCallerSaveRegisters(); |
| 321 | |
| 322 | void CallInternal(byte* entry); |
| 323 | |
| 324 | // Architecture state. |
| 325 | // Saturating instructions require a Q flag to indicate saturation. |
| 326 | // There is currently no way to read the CPSR directly, and thus read the Q |
| 327 | // flag, so this is left unimplemented. |
| 328 | intptr_t registers_[kNumGPRs]; |
| 329 | int32_t condition_reg_; |
| 330 | int32_t fp_condition_reg_; |
| 331 | intptr_t special_reg_lr_; |
| 332 | intptr_t special_reg_pc_; |
| 333 | intptr_t special_reg_ctr_; |
| 334 | int32_t special_reg_xer_; |
| 335 | |
| 336 | double fp_registers_[kNumFPRs]; |
| 337 | |
| 338 | // Simulator support. |
| 339 | char* stack_; |
| 340 | bool pc_modified_; |
| 341 | int icount_; |
| 342 | |
| 343 | // Debugger input. |
| 344 | char* last_debugger_input_; |
| 345 | |
| 346 | // Icache simulation |
| 347 | v8::internal::HashMap* i_cache_; |
| 348 | |
| 349 | // Registered breakpoints. |
| 350 | Instruction* break_pc_; |
| 351 | Instr break_instr_; |
| 352 | |
| 353 | v8::internal::Isolate* isolate_; |
| 354 | |
| 355 | // A stop is watched if its code is less than kNumOfWatchedStops. |
| 356 | // Only watched stops support enabling/disabling and the counter feature. |
| 357 | static const uint32_t kNumOfWatchedStops = 256; |
| 358 | |
| 359 | // Breakpoint is disabled if bit 31 is set. |
| 360 | static const uint32_t kStopDisabledBit = 1 << 31; |
| 361 | |
| 362 | // A stop is enabled, meaning the simulator will stop when meeting the |
| 363 | // instruction, if bit 31 of watched_stops_[code].count is unset. |
| 364 | // The value watched_stops_[code].count & ~(1 << 31) indicates how many times |
| 365 | // the breakpoint was hit or gone through. |
| 366 | struct StopCountAndDesc { |
| 367 | uint32_t count; |
| 368 | char* desc; |
| 369 | }; |
| 370 | StopCountAndDesc watched_stops_[kNumOfWatchedStops]; |
| 371 | }; |
| 372 | |
| 373 | |
| 374 | // When running with the simulator transition into simulated execution at this |
| 375 | // point. |
| 376 | #define CALL_GENERATED_CODE(entry, p0, p1, p2, p3, p4) \ |
| 377 | reinterpret_cast<Object*>(Simulator::current(Isolate::Current())->Call( \ |
| 378 | FUNCTION_ADDR(entry), 5, (intptr_t)p0, (intptr_t)p1, (intptr_t)p2, \ |
| 379 | (intptr_t)p3, (intptr_t)p4)) |
| 380 | |
| 381 | #define CALL_GENERATED_REGEXP_CODE(entry, p0, p1, p2, p3, p4, p5, p6, p7, p8) \ |
| 382 | Simulator::current(Isolate::Current()) \ |
| 383 | ->Call(entry, 10, (intptr_t)p0, (intptr_t)p1, (intptr_t)p2, \ |
| 384 | (intptr_t)p3, (intptr_t)p4, (intptr_t)p5, (intptr_t)p6, \ |
| 385 | (intptr_t)p7, (intptr_t)NULL, (intptr_t)p8) |
| 386 | |
| 387 | |
| 388 | // The simulator has its own stack. Thus it has a different stack limit from |
| 389 | // the C-based native code. Setting the c_limit to indicate a very small |
| 390 | // stack cause stack overflow errors, since the simulator ignores the input. |
| 391 | // This is unlikely to be an issue in practice, though it might cause testing |
| 392 | // trouble down the line. |
| 393 | class SimulatorStack : public v8::internal::AllStatic { |
| 394 | public: |
| 395 | static inline uintptr_t JsLimitFromCLimit(v8::internal::Isolate* isolate, |
| 396 | uintptr_t c_limit) { |
| 397 | return Simulator::current(isolate)->StackLimit(); |
| 398 | } |
| 399 | |
| 400 | static inline uintptr_t RegisterCTryCatch(uintptr_t try_catch_address) { |
| 401 | Simulator* sim = Simulator::current(Isolate::Current()); |
| 402 | return sim->PushAddress(try_catch_address); |
| 403 | } |
| 404 | |
| 405 | static inline void UnregisterCTryCatch() { |
| 406 | Simulator::current(Isolate::Current())->PopAddress(); |
| 407 | } |
| 408 | }; |
| 409 | } |
| 410 | } // namespace v8::internal |
| 411 | |
| 412 | #endif // !defined(USE_SIMULATOR) |
| 413 | #endif // V8_PPC_SIMULATOR_PPC_H_ |