#include "mozilla/DebugOnly.h"
#include "jit/arm64/vixl/Debugger-vixl.h"
#include "jit/arm64/vixl/Simulator-vixl.h"
#include "jit/IonTypes.h"
#include "js/UniquePtr.h"
#include "js/Utility.h"
#include "threading/LockGuard.h"
#include "vm/Runtime.h"
js::jit::SimulatorProcess* js::jit::SimulatorProcess::singleton_ = nullptr;
namespace vixl {
using mozilla::DebugOnly;
using js::jit::ABIFunctionType;
using js::jit::JitActivation;
using js::jit::SimulatorProcess;
Simulator::Simulator(Decoder* decoder, FILE* stream)
: stream_(nullptr)
, print_disasm_(nullptr)
, instrumentation_(nullptr)
, stack_(nullptr)
, stack_limit_(nullptr)
, decoder_(nullptr)
, oom_(false)
{
this->init(decoder, stream);
}
Simulator::~Simulator() {
js_free(stack_);
stack_ = nullptr;
if (print_disasm_) {
decoder_->RemoveVisitor(print_disasm_);
js_delete(print_disasm_);
print_disasm_ = nullptr;
}
if (instrumentation_) {
decoder_->RemoveVisitor(instrumentation_);
js_delete(instrumentation_);
instrumentation_ = nullptr;
}
}
void Simulator::ResetState() {
nzcv_ = SimSystemRegister::DefaultValueFor(NZCV);
fpcr_ = SimSystemRegister::DefaultValueFor(FPCR);
pc_ = nullptr;
pc_modified_ = false;
for (unsigned i = 0; i < kNumberOfRegisters; i++) {
set_xreg(i, 0xbadbeef);
}
uint64_t nan_bits = UINT64_C(0x7ff0dead7f8beef1);
VIXL_ASSERT(IsSignallingNaN(rawbits_to_double(nan_bits & kDRegMask)));
VIXL_ASSERT(IsSignallingNaN(rawbits_to_float(nan_bits & kSRegMask)));
for (unsigned i = 0; i < kNumberOfFPRegisters; i++) {
set_dreg_bits(i, nan_bits);
}
set_lr(kEndOfSimAddress);
}
void Simulator::init(Decoder* decoder, FILE* stream) {
VIXL_ASSERT((static_cast<int32_t>(-1) >> 1) == -1);
VIXL_ASSERT((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF);
instruction_stats_ = false;
decoder_ = decoder;
decoder_->AppendVisitor(this);
stream_ = stream;
print_disasm_ = js_new<PrintDisassembler>(stream_);
if (!print_disasm_) {
oom_ = true;
return;
}
set_coloured_trace(false);
trace_parameters_ = LOG_NONE;
ResetState();
stack_ = js_pod_malloc<byte>(stack_size_);
if (!stack_) {
oom_ = true;
return;
}
stack_limit_ = stack_ + stack_protection_size_;
byte * tos = stack_ + stack_size_;
tos -= stack_protection_size_;
tos = AlignDown(tos, 16);
set_sp(tos);
if (getenv("VIXL_STATS")) {
instrumentation_ = js_new<Instrument>("vixl_stats.csv", 10);
if (!instrumentation_) {
oom_ = true;
return;
}
}
print_exclusive_access_warning_ = true;
}
Simulator* Simulator::Current() {
JSContext* cx = js::TlsContext.get();
MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(cx->runtime()));
return cx->simulator();
}
Simulator* Simulator::Create() {
Decoder *decoder = js_new<vixl::Decoder>();
if (!decoder)
return nullptr;
js::UniquePtr<Simulator> sim;
if (getenv("USE_DEBUGGER") != nullptr)
sim.reset(js_new<Debugger>(decoder, stdout));
else
sim.reset(js_new<Simulator>(decoder, stdout));
if (sim && sim->oom())
return nullptr;
return sim.release();
}
void Simulator::Destroy(Simulator* sim) {
js_delete(sim);
}
void Simulator::ExecuteInstruction() {
VIXL_ASSERT(IsWordAligned(pc_));
decoder_->Decode(pc_);
increment_pc();
}
uintptr_t Simulator::stackLimit() const {
return reinterpret_cast<uintptr_t>(stack_limit_);
}
uintptr_t* Simulator::addressOfStackLimit() {
return (uintptr_t*)&stack_limit_;
}
bool Simulator::overRecursed(uintptr_t newsp) const {
if (newsp)
newsp = get_sp();
return newsp <= stackLimit();
}
bool Simulator::overRecursedWithExtra(uint32_t extra) const {
uintptr_t newsp = get_sp() - extra;
return newsp <= stackLimit();
}
JS::ProfilingFrameIterator::RegisterState
Simulator::registerState()
{
JS::ProfilingFrameIterator::RegisterState state;
state.pc = (uint8_t*) get_pc();
state.fp = (uint8_t*) get_fp();
state.lr = (uint8_t*) get_lr();
state.sp = (uint8_t*) get_sp();
return state;
}
int64_t Simulator::call(uint8_t* entry, int argument_count, ...) {
va_list parameters;
va_start(parameters, argument_count);
VIXL_ASSERT(argument_count <= 8);
if (argument_count == 8) {
set_xreg(0, va_arg(parameters, int64_t));
set_xreg(1, va_arg(parameters, unsigned));
set_xreg(2, va_arg(parameters, int64_t));
set_xreg(3, va_arg(parameters, int64_t));
set_xreg(4, va_arg(parameters, int64_t));
set_xreg(5, va_arg(parameters, int64_t));
set_xreg(6, va_arg(parameters, unsigned));
set_xreg(7, va_arg(parameters, int64_t));
} else if (argument_count == 2) {
set_xreg(0, va_arg(parameters, int64_t));
set_xreg(1, va_arg(parameters, int64_t));
} else if (argument_count == 1) { set_xreg(0, va_arg(parameters, int64_t));
} else {
MOZ_CRASH("Unknown number of arguments");
}
va_end(parameters);
VIXL_ASSERT(get_lr() == int64_t(kEndOfSimAddress));
DebugOnly<int64_t> entryStack = get_sp();
RunFrom((Instruction*)entry);
DebugOnly<int64_t> exitStack = get_sp();
VIXL_ASSERT(entryStack == exitStack);
int64_t result = xreg(0);
if (getenv("USE_DEBUGGER"))
printf("LEAVE\n");
return result;
}
class AutoLockSimulatorCache : public js::LockGuard<js::Mutex>
{
friend class Simulator;
using Base = js::LockGuard<js::Mutex>;
public:
explicit AutoLockSimulatorCache()
: Base(SimulatorProcess::singleton_->lock_)
{
}
};
class Redirection
{
friend class Simulator;
Redirection(void* nativeFunction, ABIFunctionType type)
: nativeFunction_(nativeFunction),
type_(type),
next_(nullptr)
{
next_ = SimulatorProcess::redirection();
SimulatorProcess::setRedirection(this);
Instruction* instr = (Instruction*)(&svcInstruction_);
vixl::Assembler::svc(instr, kCallRtRedirected);
}
public:
void* addressOfSvcInstruction() { return &svcInstruction_; }
void* nativeFunction() const { return nativeFunction_; }
ABIFunctionType type() const { return type_; }
static Redirection* Get(void* nativeFunction, ABIFunctionType type) {
AutoLockSimulatorCache alsr;
Redirection* current = SimulatorProcess::redirection();
for (; current != nullptr; current = current->next_) {
if (current->nativeFunction_ == nativeFunction) {
VIXL_ASSERT(current->type() == type);
return current;
}
}
js::AutoEnterOOMUnsafeRegion oomUnsafe;
Redirection* redir = js_pod_malloc<Redirection>(1);
if (!redir)
oomUnsafe.crash("Simulator redirection");
new(redir) Redirection(nativeFunction, type);
return redir;
}
static const Redirection* FromSvcInstruction(const Instruction* svcInstruction) {
const uint8_t* addrOfSvc = reinterpret_cast<const uint8_t*>(svcInstruction);
const uint8_t* addrOfRedirection = addrOfSvc - offsetof(Redirection, svcInstruction_);
return reinterpret_cast<const Redirection*>(addrOfRedirection);
}
private:
void* nativeFunction_;
uint32_t svcInstruction_;
ABIFunctionType type_;
Redirection* next_;
};
void* Simulator::RedirectNativeFunction(void* nativeFunction, ABIFunctionType type) {
Redirection* redirection = Redirection::Get(nativeFunction, type);
return redirection->addressOfSvcInstruction();
}
void Simulator::VisitException(const Instruction* instr) {
switch (instr->Mask(ExceptionMask)) {
case BRK: {
int lowbit = ImmException_offset;
int highbit = ImmException_offset + ImmException_width - 1;
HostBreakpoint(instr->Bits(highbit, lowbit));
break;
}
case HLT:
switch (instr->ImmException()) {
case kUnreachableOpcode: {
uint8_t* newPC;
if (js::wasm::HandleIllegalInstruction(registerState(), &newPC)) {
set_pc((Instruction*)newPC);
return;
}
DoUnreachable(instr);
return;
}
case kTraceOpcode:
DoTrace(instr);
return;
case kLogOpcode:
DoLog(instr);
return;
case kPrintfOpcode:
DoPrintf(instr);
return;
default:
HostBreakpoint();
return;
}
case SVC:
switch (instr->ImmException()) {
case kCallRtRedirected:
VisitCallRedirection(instr);
return;
case kMarkStackPointer: {
js::AutoEnterOOMUnsafeRegion oomUnsafe;
if (!spStack_.append(get_sp()))
oomUnsafe.crash("tracking stack for ARM64 simulator");
return;
}
case kCheckStackPointer: {
int64_t current = get_sp();
int64_t expected = spStack_.popCopy();
VIXL_ASSERT(current == expected);
return;
}
default:
VIXL_UNIMPLEMENTED();
}
break;
default:
VIXL_UNIMPLEMENTED();
}
}
void Simulator::setGPR32Result(int32_t result) {
set_wreg(0, result);
}
void Simulator::setGPR64Result(int64_t result) {
set_xreg(0, result);
}
void Simulator::setFP32Result(float result) {
set_sreg(0, result);
}
void Simulator::setFP64Result(double result) {
set_dreg(0, result);
}
typedef int64_t (*Prototype_General0)();
typedef int64_t (*Prototype_General1)(int64_t arg0);
typedef int64_t (*Prototype_General2)(int64_t arg0, int64_t arg1);
typedef int64_t (*Prototype_General3)(int64_t arg0, int64_t arg1, int64_t arg2);
typedef int64_t (*Prototype_General4)(int64_t arg0, int64_t arg1, int64_t arg2, int64_t arg3);
typedef int64_t (*Prototype_General5)(int64_t arg0, int64_t arg1, int64_t arg2, int64_t arg3,
int64_t arg4);
typedef int64_t (*Prototype_General6)(int64_t arg0, int64_t arg1, int64_t arg2, int64_t arg3,
int64_t arg4, int64_t arg5);
typedef int64_t (*Prototype_General7)(int64_t arg0, int64_t arg1, int64_t arg2, int64_t arg3,
int64_t arg4, int64_t arg5, int64_t arg6);
typedef int64_t (*Prototype_General8)(int64_t arg0, int64_t arg1, int64_t arg2, int64_t arg3,
int64_t arg4, int64_t arg5, int64_t arg6, int64_t arg7);
typedef int64_t (*Prototype_GeneralGeneralGeneralInt64)(int64_t arg0, int64_t arg1, int64_t arg2,
int64_t arg3);
typedef int64_t (*Prototype_GeneralGeneralInt64Int64)(int64_t arg0, int64_t arg1, int64_t arg2,
int64_t arg3);
typedef int64_t (*Prototype_Int_Double)(double arg0);
typedef int64_t (*Prototype_Int_IntDouble)(int64_t arg0, double arg1);
typedef int64_t (*Prototype_Int_DoubleIntInt)(double arg0, uint64_t arg1, uint64_t arg2);
typedef int64_t (*Prototype_Int_IntDoubleIntInt)(uint64_t arg0, double arg1,
uint64_t arg2, uint64_t arg3);
typedef float (*Prototype_Float32_Float32)(float arg0);
typedef float (*Prototype_Float32_Float32Float32)(float arg0, float arg1);
typedef double (*Prototype_Double_None)();
typedef double (*Prototype_Double_Double)(double arg0);
typedef double (*Prototype_Double_Int)(int64_t arg0);
typedef double (*Prototype_Double_DoubleInt)(double arg0, int64_t arg1);
typedef double (*Prototype_Double_IntDouble)(int64_t arg0, double arg1);
typedef double (*Prototype_Double_DoubleDouble)(double arg0, double arg1);
typedef double (*Prototype_Double_DoubleDoubleDouble)(double arg0, double arg1, double arg2);
typedef double (*Prototype_Double_DoubleDoubleDoubleDouble)(double arg0, double arg1,
double arg2, double arg3);
void
Simulator::VisitCallRedirection(const Instruction* instr)
{
VIXL_ASSERT(instr->Mask(ExceptionMask) == SVC);
VIXL_ASSERT(instr->ImmException() == kCallRtRedirected);
const Redirection* redir = Redirection::FromSvcInstruction(instr);
uintptr_t nativeFn = reinterpret_cast<uintptr_t>(redir->nativeFunction());
DebugOnly<int64_t> x19 = xreg(19);
DebugOnly<int64_t> x20 = xreg(20);
DebugOnly<int64_t> x21 = xreg(21);
DebugOnly<int64_t> x22 = xreg(22);
DebugOnly<int64_t> x23 = xreg(23);
DebugOnly<int64_t> x24 = xreg(24);
DebugOnly<int64_t> x25 = xreg(25);
DebugOnly<int64_t> x26 = xreg(26);
DebugOnly<int64_t> x27 = xreg(27);
DebugOnly<int64_t> x28 = xreg(28);
DebugOnly<int64_t> x29 = xreg(29);
DebugOnly<int64_t> savedSP = get_sp();
int64_t savedLR = xreg(30);
set_xreg(30, int64_t(kEndOfSimAddress));
int64_t x0 = xreg(0);
int64_t x1 = xreg(1);
int64_t x2 = xreg(2);
int64_t x3 = xreg(3);
int64_t x4 = xreg(4);
int64_t x5 = xreg(5);
int64_t x6 = xreg(6);
int64_t x7 = xreg(7);
double d0 = dreg(0);
double d1 = dreg(1);
double d2 = dreg(2);
double d3 = dreg(3);
float s0 = sreg(0);
float s1 = sreg(1);
switch (redir->type()) {
case js::jit::Args_General0: {
int64_t ret = reinterpret_cast<Prototype_General0>(nativeFn)();
setGPR64Result(ret);
break;
}
case js::jit::Args_General1: {
int64_t ret = reinterpret_cast<Prototype_General1>(nativeFn)(x0);
setGPR64Result(ret);
break;
}
case js::jit::Args_General2: {
int64_t ret = reinterpret_cast<Prototype_General2>(nativeFn)(x0, x1);
setGPR64Result(ret);
break;
}
case js::jit::Args_General3: {
int64_t ret = reinterpret_cast<Prototype_General3>(nativeFn)(x0, x1, x2);
setGPR64Result(ret);
break;
}
case js::jit::Args_General4: {
int64_t ret = reinterpret_cast<Prototype_General4>(nativeFn)(x0, x1, x2, x3);
setGPR64Result(ret);
break;
}
case js::jit::Args_General5: {
int64_t ret = reinterpret_cast<Prototype_General5>(nativeFn)(x0, x1, x2, x3, x4);
setGPR64Result(ret);
break;
}
case js::jit::Args_General6: {
int64_t ret = reinterpret_cast<Prototype_General6>(nativeFn)(x0, x1, x2, x3, x4, x5);
setGPR64Result(ret);
break;
}
case js::jit::Args_General7: {
int64_t ret = reinterpret_cast<Prototype_General7>(nativeFn)(x0, x1, x2, x3, x4, x5, x6);
setGPR64Result(ret);
break;
}
case js::jit::Args_General8: {
int64_t ret = reinterpret_cast<Prototype_General8>(nativeFn)(x0, x1, x2, x3, x4, x5, x6, x7);
setGPR64Result(ret);
break;
}
case js::jit::Args_Int_GeneralGeneralGeneralInt64: {
int64_t ret = reinterpret_cast<Prototype_GeneralGeneralGeneralInt64>(nativeFn)(x0, x1, x2, x3);
setGPR64Result(ret);
break;
}
case js::jit::Args_Int_GeneralGeneralInt64Int64: {
int64_t ret = reinterpret_cast<Prototype_GeneralGeneralInt64Int64>(nativeFn)(x0, x1, x2, x3);
setGPR64Result(ret);
break;
}
case js::jit::Args_Int_Double: {
int64_t ret = reinterpret_cast<Prototype_Int_Double>(nativeFn)(d0);
setGPR64Result(ret);
break;
}
case js::jit::Args_Int_IntDouble: {
int64_t ret = reinterpret_cast<Prototype_Int_IntDouble>(nativeFn)(x0, d0);
setGPR64Result(ret);
break;
}
case js::jit::Args_Int_IntDoubleIntInt: {
int64_t ret = reinterpret_cast<Prototype_Int_IntDoubleIntInt>(nativeFn)(x0, d0, x1, x2);
setGPR64Result(ret);
break;
}
case js::jit::Args_Int_DoubleIntInt: {
int64_t ret = reinterpret_cast<Prototype_Int_DoubleIntInt>(nativeFn)(d0, x0, x1);
setGPR64Result(ret);
break;
}
case js::jit::Args_Float32_Float32: {
float ret = reinterpret_cast<Prototype_Float32_Float32>(nativeFn)(s0);
setFP32Result(ret);
break;
}
case js::jit::Args_Float32_Float32Float32: {
float ret = reinterpret_cast<Prototype_Float32_Float32Float32>(nativeFn)(s0, s1);
setFP32Result(ret);
break;
}
case js::jit::Args_Double_None: {
double ret = reinterpret_cast<Prototype_Double_None>(nativeFn)();
setFP64Result(ret);
break;
}
case js::jit::Args_Double_Double: {
double ret = reinterpret_cast<Prototype_Double_Double>(nativeFn)(d0);
setFP64Result(ret);
break;
}
case js::jit::Args_Double_Int: {
double ret = reinterpret_cast<Prototype_Double_Int>(nativeFn)(x0);
setFP64Result(ret);
break;
}
case js::jit::Args_Double_DoubleInt: {
double ret = reinterpret_cast<Prototype_Double_DoubleInt>(nativeFn)(d0, x0);
setFP64Result(ret);
break;
}
case js::jit::Args_Double_DoubleDouble: {
double ret = reinterpret_cast<Prototype_Double_DoubleDouble>(nativeFn)(d0, d1);
setFP64Result(ret);
break;
}
case js::jit::Args_Double_DoubleDoubleDouble: {
double ret = reinterpret_cast<Prototype_Double_DoubleDoubleDouble>(nativeFn)(d0, d1, d2);
setFP64Result(ret);
break;
}
case js::jit::Args_Double_DoubleDoubleDoubleDouble: {
double ret = reinterpret_cast<Prototype_Double_DoubleDoubleDoubleDouble>(nativeFn)(d0, d1, d2, d3);
setFP64Result(ret);
break;
}
case js::jit::Args_Double_IntDouble: {
double ret = reinterpret_cast<Prototype_Double_IntDouble>(nativeFn)(x0, d0);
setFP64Result(ret);
break;
}
default:
MOZ_CRASH("Unknown function type.");
}
for (int i = 1; i <= 18; i++) {
set_xreg(i, int64_t(0xc0defeed1badda7a));
}
VIXL_ASSERT(xreg(19) == x19);
VIXL_ASSERT(xreg(20) == x20);
VIXL_ASSERT(xreg(21) == x21);
VIXL_ASSERT(xreg(22) == x22);
VIXL_ASSERT(xreg(23) == x23);
VIXL_ASSERT(xreg(24) == x24);
VIXL_ASSERT(xreg(25) == x25);
VIXL_ASSERT(xreg(26) == x26);
VIXL_ASSERT(xreg(27) == x27);
VIXL_ASSERT(xreg(28) == x28);
VIXL_ASSERT(xreg(29) == x29);
VIXL_ASSERT(savedSP == get_sp());
set_lr(savedLR);
set_pc((Instruction*)savedLR);
if (getenv("USE_DEBUGGER"))
printf("SVCRET\n");
}
}
vixl::Simulator* JSContext::simulator() const {
return simulator_;
}
uintptr_t* JSContext::addressOfSimulatorStackLimit() {
return simulator_->addressOfStackLimit();
}