13ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Copyright 2012 the V8 project authors. All rights reserved.
23100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Redistribution and use in source and binary forms, with or without
33100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// modification, are permitted provided that the following conditions are
43100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// met:
53100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//
63100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//     * Redistributions of source code must retain the above copyright
73100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       notice, this list of conditions and the following disclaimer.
83100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//     * Redistributions in binary form must reproduce the above
93100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       copyright notice, this list of conditions and the following
103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       disclaimer in the documentation and/or other materials provided
113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       with the distribution.
123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//     * Neither the name of Google Inc. nor the names of its
133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       contributors may be used to endorse or promote products derived
143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       from this software without specific prior written permission.
153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//
163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// CPU specific code for arm independent of OS goes here.
293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#include <sys/syscall.h>
313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#include <unistd.h>
323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#ifdef __mips
343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#include <asm/cachectl.h>
353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#endif  // #ifdef __mips
363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#include "v8.h"
38f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
39f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke#if defined(V8_TARGET_ARCH_MIPS)
40f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#include "cpu.h"
4244f0eee88ff00398ff7f715fab053374d808c90dSteve Block#include "macro-assembler.h"
4344f0eee88ff00398ff7f715fab053374d808c90dSteve Block
4444f0eee88ff00398ff7f715fab053374d808c90dSteve Block#include "simulator.h"  // For cache flushing.
453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescunamespace v8 {
473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescunamespace internal {
483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4944f0eee88ff00398ff7f715fab053374d808c90dSteve Block
503ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvoid CPU::SetUp() {
51257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  CpuFeatures::Probe();
52257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch}
53257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
54257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
55257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochbool CPU::SupportsCrankshaft() {
56257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  return CpuFeatures::IsSupported(FPU);
573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5944f0eee88ff00398ff7f715fab053374d808c90dSteve Block
603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuvoid CPU::FlushICache(void* start, size_t size) {
61257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // Nothing to do, flushing no instructions.
62257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  if (size == 0) {
63257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    return;
64257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  }
65257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
6644f0eee88ff00398ff7f715fab053374d808c90dSteve Block#if !defined (USE_SIMULATOR)
673ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#if defined(ANDROID)
683ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind  // Workaround for a deserializer bug. Bionic usermode cacheflush
693ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind  // fails in deserializer for a size of Page::kPageSize (1MB),
703ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind  // because that region contains protected pages. Switch to kernel
713ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind  // cacheflush in this case.
723ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind  if (size >= static_cast<size_t>(Page::kPageSize)) {
733ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    int res;
743ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    // See http://www.linux-mips.org/wiki/Cacheflush_Syscall.
753ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    res = syscall(__NR_cacheflush, start, size, ICACHE);
763ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    if (res) {
773ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind      V8_Fatal(__FILE__, __LINE__, "Failed to flush the instruction cache");
783ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    }
793ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind  } else {
803ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    // Bionic cacheflush can typically run in userland, avoiding kernel call.
813ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    char *end = reinterpret_cast<char *>(start) + size;
823ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind    cacheflush(
833ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind      reinterpret_cast<intptr_t>(start), reinterpret_cast<intptr_t>(end), 0);
843ee90b0f201427268e35d86d7ad95e45aa7491a8Paul Lind  }
853ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#else  // ANDROID
863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  int res;
87257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // See http://www.linux-mips.org/wiki/Cacheflush_Syscall.
883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  res = syscall(__NR_cacheflush, start, size, ICACHE);
893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (res) {
903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    V8_Fatal(__FILE__, __LINE__, "Failed to flush the instruction cache");
913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
923ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#endif  // ANDROID
9344f0eee88ff00398ff7f715fab053374d808c90dSteve Block#else  // USE_SIMULATOR.
9444f0eee88ff00398ff7f715fab053374d808c90dSteve Block  // Not generating mips instructions for C-code. This means that we are
9544f0eee88ff00398ff7f715fab053374d808c90dSteve Block  // building a mips emulator based target.  We should notify the simulator
9644f0eee88ff00398ff7f715fab053374d808c90dSteve Block  // that the Icache was flushed.
9744f0eee88ff00398ff7f715fab053374d808c90dSteve Block  // None of this code ends up in the snapshot so there are no issues
9844f0eee88ff00398ff7f715fab053374d808c90dSteve Block  // around whether or not to generate the code when building snapshots.
9944f0eee88ff00398ff7f715fab053374d808c90dSteve Block  Simulator::FlushICache(Isolate::Current()->simulator_i_cache(), start, size);
10044f0eee88ff00398ff7f715fab053374d808c90dSteve Block#endif  // USE_SIMULATOR.
1013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
1023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuvoid CPU::DebugBreak() {
1053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#ifdef __mips
1063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  asm volatile("break");
1073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu#endif  // #ifdef __mips
1083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
1093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11044f0eee88ff00398ff7f715fab053374d808c90dSteve Block
1113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu} }  // namespace v8::internal
1123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
113f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke#endif  // V8_TARGET_ARCH_MIPS
114