| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <setjmp.h> | 5 #include <setjmp.h> |
| 6 #include <stdlib.h> | 6 #include <stdlib.h> |
| 7 | 7 |
| 8 #include "vm/globals.h" | 8 #include "vm/globals.h" |
| 9 #if defined(TARGET_ARCH_ARM64) | 9 #if defined(TARGET_ARCH_ARM64) |
| 10 | 10 |
| 11 // Only build the simulator if not compiling for real ARM hardware. | 11 // Only build the simulator if not compiling for real ARM hardware. |
| 12 #if !defined(HOST_ARCH_ARM64) | 12 #if !defined(HOST_ARCH_ARM64) |
| 13 | 13 |
| 14 #include "vm/simulator.h" | 14 #include "vm/simulator.h" |
| 15 | 15 |
| 16 #include "vm/assembler.h" | 16 #include "vm/assembler.h" |
| 17 #include "vm/constants_arm64.h" | 17 #include "vm/constants_arm64.h" |
| 18 #include "vm/cpu.h" | 18 #include "vm/cpu.h" |
| 19 #include "vm/disassembler.h" | 19 #include "vm/disassembler.h" |
| 20 #include "vm/lockers.h" |
| 20 #include "vm/native_arguments.h" | 21 #include "vm/native_arguments.h" |
| 21 #include "vm/stack_frame.h" | 22 #include "vm/stack_frame.h" |
| 22 #include "vm/thread.h" | 23 #include "vm/thread.h" |
| 23 | 24 |
| 24 namespace dart { | 25 namespace dart { |
| 25 | 26 |
| 26 DEFINE_FLAG(bool, trace_sim, false, "Trace simulator execution."); | 27 DEFINE_FLAG(bool, trace_sim, false, "Trace simulator execution."); |
| 27 DEFINE_FLAG(int, stop_sim_at, 0, "Address to stop simulator at."); | 28 DEFINE_FLAG(int, stop_sim_at, 0, "Address to stop simulator at."); |
| 28 | 29 |
| 29 | 30 |
| (...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 // Copy the newly read line into the result. | 523 // Copy the newly read line into the result. |
| 523 memmove(result + offset, line_buf, len); | 524 memmove(result + offset, line_buf, len); |
| 524 offset += len; | 525 offset += len; |
| 525 } | 526 } |
| 526 ASSERT(result != NULL); | 527 ASSERT(result != NULL); |
| 527 result[offset] = '\0'; | 528 result[offset] = '\0'; |
| 528 return result; | 529 return result; |
| 529 } | 530 } |
| 530 | 531 |
| 531 | 532 |
| 533 // Synchronization primitives support. |
| 534 Mutex* Simulator::exclusive_access_lock_ = NULL; |
| 535 Simulator::AddressTag Simulator::exclusive_access_state_[kNumAddressTags] = |
| 536 {{NULL, 0}}; |
| 537 int Simulator::next_address_tag_ = 0; |
| 538 |
| 539 |
| 540 void Simulator::InitOnce() { |
| 541 // Setup exclusive access state lock. |
| 542 exclusive_access_lock_ = new Mutex(); |
| 543 } |
| 544 |
| 545 |
| 532 Simulator::Simulator() { | 546 Simulator::Simulator() { |
| 533 // Setup simulator support first. Some of this information is needed to | 547 // Setup simulator support first. Some of this information is needed to |
| 534 // setup the architecture state. | 548 // setup the architecture state. |
| 535 // We allocate the stack here, the size is computed as the sum of | 549 // We allocate the stack here, the size is computed as the sum of |
| 536 // the size specified by the user and the buffer space needed for | 550 // the size specified by the user and the buffer space needed for |
| 537 // handling stack overflow exceptions. To be safe in potential | 551 // handling stack overflow exceptions. To be safe in potential |
| 538 // stack underflows we also add some underflow buffer space. | 552 // stack underflows we also add some underflow buffer space. |
| 539 stack_ = new char[(Isolate::GetSpecifiedStackSize() + | 553 stack_ = new char[(Isolate::GetSpecifiedStackSize() + |
| 540 Isolate::kStackSizeBuffer + | 554 Isolate::kStackSizeBuffer + |
| 541 kSimulatorStackUnderflowSize)]; | 555 kSimulatorStackUnderflowSize)]; |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 916 return *ptr; | 930 return *ptr; |
| 917 } | 931 } |
| 918 | 932 |
| 919 | 933 |
| 920 void Simulator::WriteB(uword addr, uint8_t value) { | 934 void Simulator::WriteB(uword addr, uint8_t value) { |
| 921 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); | 935 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); |
| 922 *ptr = value; | 936 *ptr = value; |
| 923 } | 937 } |
| 924 | 938 |
| 925 | 939 |
| 940 // Synchronization primitives support. |
| 941 void Simulator::SetExclusiveAccess(uword addr) { |
| 942 Isolate* isolate = Isolate::Current(); |
| 943 ASSERT(isolate != NULL); |
| 944 int i = 0; |
| 945 // Find an entry for this isolate in the exclusive access state. |
| 946 while ((i < kNumAddressTags) && |
| 947 (exclusive_access_state_[i].isolate != isolate)) { |
| 948 i++; |
| 949 } |
| 950 // Round-robin replacement of previously used entries. |
| 951 if (i == kNumAddressTags) { |
| 952 i = next_address_tag_; |
| 953 if (++next_address_tag_ == kNumAddressTags) { |
| 954 next_address_tag_ = 0; |
| 955 } |
| 956 exclusive_access_state_[i].isolate = isolate; |
| 957 } |
| 958 // Remember the address being reserved. |
| 959 exclusive_access_state_[i].addr = addr; |
| 960 } |
| 961 |
| 962 |
| 963 bool Simulator::HasExclusiveAccessAndOpen(uword addr) { |
| 964 Isolate* isolate = Isolate::Current(); |
| 965 ASSERT(isolate != NULL); |
| 966 ASSERT(addr != 0); |
| 967 bool result = false; |
| 968 for (int i = 0; i < kNumAddressTags; i++) { |
| 969 if (exclusive_access_state_[i].isolate == isolate) { |
| 970 // Check whether the current isolates address reservation matches. |
| 971 if (exclusive_access_state_[i].addr == addr) { |
| 972 result = true; |
| 973 } |
| 974 exclusive_access_state_[i].addr = 0; |
| 975 } else if (exclusive_access_state_[i].addr == addr) { |
| 976 // Other isolates with matching address lose their reservations. |
| 977 exclusive_access_state_[i].addr = 0; |
| 978 } |
| 979 } |
| 980 return result; |
| 981 } |
| 982 |
| 983 |
| 984 void Simulator::ClearExclusive() { |
| 985 MutexLocker ml(exclusive_access_lock_); |
| 986 // Remove the reservation for this isolate. |
| 987 SetExclusiveAccess(NULL); |
| 988 } |
| 989 |
| 990 |
| 991 intptr_t Simulator::ReadExclusiveW(uword addr, Instr* instr) { |
| 992 MutexLocker ml(exclusive_access_lock_); |
| 993 SetExclusiveAccess(addr); |
| 994 return ReadW(addr, instr); |
| 995 } |
| 996 |
| 997 |
| 998 intptr_t Simulator::WriteExclusiveW(uword addr, intptr_t value, Instr* instr) { |
| 999 MutexLocker ml(exclusive_access_lock_); |
| 1000 bool write_allowed = HasExclusiveAccessAndOpen(addr); |
| 1001 if (write_allowed) { |
| 1002 WriteW(addr, value, instr); |
| 1003 return 0; // Success. |
| 1004 } |
| 1005 return 1; // Failure. |
| 1006 } |
| 1007 |
| 1008 |
| 1009 uword Simulator::CompareExchange(uword* address, |
| 1010 uword compare_value, |
| 1011 uword new_value) { |
| 1012 MutexLocker ml(exclusive_access_lock_); |
| 1013 // We do not check for a reservation as it would be guaranteed to be found. No |
| 1014 // other isolate is able to make a reservation while we hold the lock. |
| 1015 uword value = *address; |
| 1016 if (value == compare_value) { |
| 1017 *address = new_value; |
| 1018 // Same effect on exclusive access state as a successful STREX. |
| 1019 HasExclusiveAccessAndOpen(reinterpret_cast<uword>(address)); |
| 1020 } else { |
| 1021 // Same effect on exclusive access state as an LDREX. |
| 1022 SetExclusiveAccess(reinterpret_cast<uword>(address)); |
| 1023 } |
| 1024 return value; |
| 1025 } |
| 1026 |
| 1027 |
| 926 // Unsupported instructions use Format to print an error and stop execution. | 1028 // Unsupported instructions use Format to print an error and stop execution. |
| 927 void Simulator::Format(Instr* instr, const char* format) { | 1029 void Simulator::Format(Instr* instr, const char* format) { |
| 928 OS::Print("Simulator found unsupported instruction:\n 0x%p: %s\n", | 1030 OS::Print("Simulator found unsupported instruction:\n 0x%p: %s\n", |
| 929 instr, | 1031 instr, |
| 930 format); | 1032 format); |
| 931 UNIMPLEMENTED(); | 1033 UNIMPLEMENTED(); |
| 932 } | 1034 } |
| 933 | 1035 |
| 934 | 1036 |
| 935 // Calculate and set the Negative and Zero flags. | 1037 // Calculate and set the Negative and Zero flags. |
| (...skipping 2205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3141 set_register(NULL, kExceptionObjectReg, bit_cast<int64_t>(raw_exception)); | 3243 set_register(NULL, kExceptionObjectReg, bit_cast<int64_t>(raw_exception)); |
| 3142 set_register(NULL, kStackTraceObjectReg, bit_cast<int64_t>(raw_stacktrace)); | 3244 set_register(NULL, kStackTraceObjectReg, bit_cast<int64_t>(raw_stacktrace)); |
| 3143 buf->Longjmp(); | 3245 buf->Longjmp(); |
| 3144 } | 3246 } |
| 3145 | 3247 |
| 3146 } // namespace dart | 3248 } // namespace dart |
| 3147 | 3249 |
| 3148 #endif // !defined(HOST_ARCH_ARM64) | 3250 #endif // !defined(HOST_ARCH_ARM64) |
| 3149 | 3251 |
| 3150 #endif // defined TARGET_ARCH_ARM64 | 3252 #endif // defined TARGET_ARCH_ARM64 |
| OLD | NEW |