OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Declares a Simulator for ARM instructions if we are not generating a native |
| 6 // ARM binary. This Simulator allows us to run and debug ARM code generation on |
| 7 // regular desktop machines. |
| 8 // Dart calls into generated code by "calling" the InvokeDartCode stub, |
| 9 // which will start execution in the Simulator or forwards to the real entry |
| 10 // on a ARM HW platform. |
| 11 |
| 12 #ifndef VM_SIMULATOR_ARM_H_ |
| 13 #define VM_SIMULATOR_ARM_H_ |
| 14 |
| 15 #ifndef VM_SIMULATOR_H_ |
| 16 #error Do not include simulator_arm.h directly; use simulator.h. |
| 17 #endif |
| 18 |
| 19 #include "vm/allocation.h" |
| 20 #include "vm/constants_arm.h" |
| 21 #include "vm/object.h" |
| 22 |
| 23 namespace dart { |
| 24 |
| 25 class Isolate; |
| 26 class SimulatorSetjmpBuffer; |
| 27 |
| 28 class Simulator { |
| 29 public: |
| 30 static const size_t kSimulatorStackUnderflowSize = 64; |
| 31 |
| 32 Simulator(); |
| 33 ~Simulator(); |
| 34 |
| 35 // The currently executing Simulator instance, which is associated to the |
| 36 // current isolate |
| 37 static Simulator* Current(); |
| 38 |
| 39 // Accessors for register state. Reading the pc value adheres to the ARM |
| 40 // architecture specification and is off by 8 from the currently executing |
| 41 // instruction. |
| 42 void set_register(Register reg, int32_t value); |
| 43 int32_t get_register(Register reg) const; |
| 44 |
| 45 // Special case of set_register and get_register to access the raw PC value. |
| 46 void set_pc(int32_t value); |
| 47 int32_t get_pc() const; |
| 48 |
| 49 // Accessors for VFP register state. |
| 50 void set_sregister(SRegister reg, float value); |
| 51 float get_sregister(SRegister reg) const; |
| 52 void set_dregister(DRegister reg, double value); |
| 53 double get_dregister(DRegister reg) const; |
| 54 |
| 55 // Accessor to the internal simulator stack area. |
| 56 uintptr_t StackTop() const; |
| 57 uintptr_t StackLimit() const; |
| 58 |
| 59 // Executes ARM instructions until the PC reaches end_sim_pc. |
| 60 void Execute(); |
| 61 |
| 62 // Call on program start. |
| 63 static void InitOnce(); |
| 64 |
| 65 // Dart generally calls into generated code with 5 parameters. This is a |
| 66 // convenience function, which sets up the simulator state and grabs the |
| 67 // result on return. |
| 68 int64_t Call(int32_t entry, |
| 69 int32_t parameter0, |
| 70 int32_t parameter1, |
| 71 int32_t parameter2, |
| 72 int32_t parameter3, |
| 73 int32_t parameter4); |
| 74 |
| 75 // Implementation of atomic compare and exchange in the same synchronization |
| 76 // domain as other synchronization primitive instructions (e.g. ldrex, strex). |
| 77 static uword CompareExchange(uword* address, |
| 78 uword compare_value, |
| 79 uword new_value); |
| 80 |
| 81 // Runtime call support. |
| 82 static uword RedirectExternalReference(void* function, |
| 83 uint32_t argument_count); |
| 84 |
| 85 void Longjmp(int32_t pc, int32_t sp, int32_t fp, const Instance& object); |
| 86 |
| 87 private: |
| 88 // Known bad pc value to ensure that the simulator does not execute |
| 89 // without being properly setup. |
| 90 static const uword kBadLR = -1; |
| 91 // A pc value used to signal the simulator to stop execution. Generally |
| 92 // the lr is set to this value on transition from native C code to |
| 93 // simulated execution, so that the simulator can "return" to the native |
| 94 // C code. |
| 95 static const uword kEndSimulatingPC = -2; |
| 96 |
| 97 // CPU state. |
| 98 int32_t registers_[kNumberOfCpuRegisters]; |
| 99 bool n_flag_; |
| 100 bool z_flag_; |
| 101 bool c_flag_; |
| 102 bool v_flag_; |
| 103 |
| 104 // VFP state. |
| 105 union { // S and D register banks are overlapping. |
| 106 float sregisters_[kNumberOfSRegisters]; |
| 107 double dregisters_[kNumberOfDRegisters]; |
| 108 }; |
| 109 bool fp_n_flag_; |
| 110 bool fp_z_flag_; |
| 111 bool fp_c_flag_; |
| 112 bool fp_v_flag_; |
| 113 |
| 114 // Simulator support. |
| 115 char* stack_; |
| 116 bool pc_modified_; |
| 117 int icount_; |
| 118 static bool flag_trace_sim_; |
| 119 static int32_t flag_stop_sim_at_; |
| 120 SimulatorSetjmpBuffer* last_setjmp_buffer_; |
| 121 |
| 122 // Registered breakpoints. |
| 123 Instr* break_pc_; |
| 124 int32_t break_instr_; |
| 125 |
| 126 // Illegal memory access support. |
| 127 static bool IsIllegalAddress(uword addr) { |
| 128 return addr < 64*1024; |
| 129 } |
| 130 void HandleIllegalAccess(uword addr, Instr* instr); |
| 131 |
| 132 // Unsupported instructions use Format to print an error and stop execution. |
| 133 void Format(Instr* instr, const char* format); |
| 134 |
| 135 // Checks if the current instruction should be executed based on its |
| 136 // condition bits. |
| 137 bool ConditionallyExecute(Instr* instr); |
| 138 |
| 139 // Helper functions to set the conditional flags in the architecture state. |
| 140 void SetNZFlags(int32_t val); |
| 141 void SetCFlag(bool val); |
| 142 void SetVFlag(bool val); |
| 143 bool CarryFrom(int32_t left, int32_t right); |
| 144 bool BorrowFrom(int32_t left, int32_t right); |
| 145 bool OverflowFrom(int32_t alu_out, |
| 146 int32_t left, |
| 147 int32_t right, |
| 148 bool addition); |
| 149 |
| 150 // Helper functions to decode common "addressing" modes. |
| 151 int32_t GetShiftRm(Instr* instr, bool* carry_out); |
| 152 int32_t GetImm(Instr* instr, bool* carry_out); |
| 153 void HandleRList(Instr* instr, bool load); |
| 154 void SupervisorCall(Instr* instr); |
| 155 |
| 156 // Read and write memory. |
| 157 void UnalignedAccess(const char* msg, uword addr, Instr* instr); |
| 158 |
| 159 inline uint8_t ReadBU(uword addr); |
| 160 inline int8_t ReadB(uword addr); |
| 161 inline void WriteB(uword addr, uint8_t value); |
| 162 |
| 163 inline uint16_t ReadHU(uword addr, Instr* instr); |
| 164 inline int16_t ReadH(uword addr, Instr* instr); |
| 165 inline void WriteH(uword addr, uint16_t value, Instr* instr); |
| 166 |
| 167 inline int ReadW(uword addr, Instr* instr); |
| 168 inline void WriteW(uword addr, int value, Instr* instr); |
| 169 |
| 170 // Synchronization primitives support. |
| 171 void ClearExclusive(); |
| 172 int ReadExclusiveW(uword addr, Instr* instr); |
| 173 int WriteExclusiveW(uword addr, int value, Instr* instr); |
| 174 |
| 175 // TODO(regis): Remove exclusive access support machinery if not needed. |
| 176 // In Dart, there is at most one thread per isolate. |
| 177 // We keep track of 16 exclusive access address tags across all isolates. |
| 178 // Since we cannot simulate a native context switch, which clears |
| 179 // the exclusive access state of the local monitor (using the CLREX |
| 180 // instruction), we associate the isolate requesting exclusive access to the |
| 181 // address tag. Multiple isolates requesting exclusive access (using the LDREX |
| 182 // instruction) to the same address will result in multiple address tags being |
| 183 // created for the same address, one per isolate. |
| 184 // At any given time, each isolate is associated to at most one address tag. |
| 185 static Mutex* exclusive_access_lock_; |
| 186 static const int kNumAddressTags = 16; |
| 187 static struct AddressTag { |
| 188 Isolate* isolate; |
| 189 uword addr; |
| 190 } exclusive_access_state_[kNumAddressTags]; |
| 191 static int next_address_tag_; |
| 192 |
| 193 // Set access to given address to 'exclusive state' for current isolate. |
| 194 static void SetExclusiveAccess(uword addr); |
| 195 |
| 196 // Returns true if the current isolate has exclusive access to given address, |
| 197 // returns false otherwise. In either case, set access to given address to |
| 198 // 'open state' for all isolates. |
| 199 // If given addr is NULL, set access to 'open state' for current |
| 200 // isolate (CLREX). |
| 201 static bool HasExclusiveAccessAndOpen(uword addr); |
| 202 |
| 203 // Executing is handled based on the instruction type. |
| 204 void DecodeType01(Instr* instr); // Both type 0 and type 1 rolled into one. |
| 205 void DecodeType2(Instr* instr); |
| 206 void DecodeType3(Instr* instr); |
| 207 void DecodeType4(Instr* instr); |
| 208 void DecodeType5(Instr* instr); |
| 209 void DecodeType6(Instr* instr); |
| 210 void DecodeType7(Instr* instr); |
| 211 |
| 212 // Executes one instruction. |
| 213 void InstructionDecode(Instr* instr); |
| 214 |
| 215 // Longjmp support for exceptions. |
| 216 SimulatorSetjmpBuffer* last_setjmp_buffer() { |
| 217 return last_setjmp_buffer_; |
| 218 } |
| 219 void set_last_setjmp_buffer(SimulatorSetjmpBuffer* buffer) { |
| 220 last_setjmp_buffer_ = buffer; |
| 221 } |
| 222 |
| 223 friend class SimulatorDebugger; |
| 224 friend class SimulatorSetjmpBuffer; |
| 225 DISALLOW_COPY_AND_ASSIGN(Simulator); |
| 226 }; |
| 227 |
| 228 } // namespace dart |
| 229 |
| 230 #endif // VM_SIMULATOR_ARM_H_ |
OLD | NEW |