simulator-base.cc 2.91 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/simulator-base.h"

#include "src/isolate.h"
8
#include "src/simulator.h"
9 10 11 12 13 14 15 16 17 18 19 20

#if defined(USE_SIMULATOR)

namespace v8 {
namespace internal {

// static
base::Mutex* SimulatorBase::redirection_mutex_ = nullptr;

// static
Redirection* SimulatorBase::redirection_ = nullptr;

21 22 23 24 25 26
// static
base::Mutex* SimulatorBase::i_cache_mutex_ = nullptr;

// static
base::CustomMatcherHashMap* SimulatorBase::i_cache_ = nullptr;

27 28 29 30
// static
void SimulatorBase::InitializeOncePerProcess() {
  DCHECK_NULL(redirection_mutex_);
  redirection_mutex_ = new base::Mutex();
31 32 33 34 35 36

  DCHECK_NULL(i_cache_mutex_);
  i_cache_mutex_ = new base::Mutex();

  DCHECK_NULL(i_cache_);
  i_cache_ = new base::CustomMatcherHashMap(&Simulator::ICacheMatch);
37 38
}

39 40 41 42 43 44 45 46
// static
void SimulatorBase::GlobalTearDown() {
  delete redirection_mutex_;
  redirection_mutex_ = nullptr;

  Redirection::DeleteChain(redirection_);
  redirection_ = nullptr;

47 48
  delete i_cache_mutex_;
  i_cache_mutex_ = nullptr;
49

50 51 52
  if (i_cache_ != nullptr) {
    for (base::HashMap::Entry* entry = i_cache_->Start(); entry != nullptr;
         entry = i_cache_->Next(entry)) {
53 54 55
      delete static_cast<CachePage*>(entry->value);
    }
  }
56 57 58 59
  delete i_cache_;
  i_cache_ = nullptr;
}

60
// static
61 62
Address SimulatorBase::RedirectExternalReference(Address external_function,
                                                 ExternalReference::Type type) {
63
  base::MutexGuard lock_guard(Simulator::redirection_mutex());
64
  Redirection* redirection = Redirection::Get(external_function, type);
65 66 67
  return redirection->address_of_instruction();
}

68 69
Redirection::Redirection(Address external_function,
                         ExternalReference::Type type)
70 71
    : external_function_(external_function), type_(type), next_(nullptr) {
  next_ = Simulator::redirection();
72
  base::MutexGuard lock_guard(Simulator::i_cache_mutex());
73 74
  Simulator::SetRedirectInstruction(
      reinterpret_cast<Instruction*>(address_of_instruction()));
75
  Simulator::FlushICache(Simulator::i_cache(),
76 77 78 79 80 81 82 83 84 85 86
                         reinterpret_cast<void*>(&instruction_),
                         sizeof(instruction_));
  Simulator::set_redirection(this);
#if ABI_USES_FUNCTION_DESCRIPTORS
  function_descriptor_[0] = reinterpret_cast<intptr_t>(&instruction_);
  function_descriptor_[1] = 0;
  function_descriptor_[2] = 0;
#endif
}

// static
87
Redirection* Redirection::Get(Address external_function,
88 89 90 91 92 93 94 95
                              ExternalReference::Type type) {
  Redirection* current = Simulator::redirection();
  for (; current != nullptr; current = current->next_) {
    if (current->external_function_ == external_function &&
        current->type_ == type) {
      return current;
    }
  }
96
  return new Redirection(external_function, type);
97 98
}

99 100 101 102
}  // namespace internal
}  // namespace v8

#endif  // defined(USE_SIMULATOR)