| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
// Copyright 2021 the V8 project authors. All rights reserved. |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following |
| 12 |
// disclaimer in the documentation and/or other materials provided |
| 13 |
// with the distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived |
| 16 |
// from this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
#ifndef jit_riscv64_Simulator_riscv64_h |
| 31 |
#define jit_riscv64_Simulator_riscv64_h |
| 32 |
|
| 33 |
#ifdef JS_SIMULATOR_RISCV64 |
| 34 |
# include "mozilla/Atomics.h" |
| 35 |
|
| 36 |
# include <vector> |
| 37 |
|
| 38 |
# include "jit/IonTypes.h" |
| 39 |
# include "jit/riscv64/constant/Constant-riscv64.h" |
| 40 |
# include "jit/riscv64/constant/util-riscv64.h" |
| 41 |
# include "jit/riscv64/disasm/Disasm-riscv64.h" |
| 42 |
# include "js/ProfilingFrameIterator.h" |
| 43 |
# include "threading/Thread.h" |
| 44 |
# include "vm/MutexIDs.h" |
| 45 |
# include "wasm/WasmSignalHandlers.h" |
| 46 |
|
| 47 |
namespace js { |
| 48 |
|
| 49 |
namespace jit { |
| 50 |
|
| 51 |
template <class Dest, class Source> |
| 52 |
inline Dest bit_cast(const Source& source) { |
| 53 |
static_assert(sizeof(Dest) == sizeof(Source), |
| 54 |
"bit_cast requires source and destination to be the same size"); |
| 55 |
static_assert(std::is_trivially_copyable<Dest>::value, |
| 56 |
"bit_cast requires the destination type to be copyable"); |
| 57 |
static_assert(std::is_trivially_copyable<Source>::value, |
| 58 |
"bit_cast requires the source type to be copyable"); |
| 59 |
|
| 60 |
Dest dest; |
| 61 |
memcpy(&dest, &source, sizeof(dest)); |
| 62 |
return dest; |
| 63 |
} |
| 64 |
|
| 65 |
# define ASSERT_TRIVIALLY_COPYABLE(T) \ |
| 66 |
static_assert(std::is_trivially_copyable<T>::value, \ |
| 67 |
#T " should be trivially copyable") |
| 68 |
# define ASSERT_NOT_TRIVIALLY_COPYABLE(T) \ |
| 69 |
static_assert(!std::is_trivially_copyable<T>::value, \ |
| 70 |
#T " should not be trivially copyable") |
| 71 |
|
| 72 |
constexpr uint32_t kHoleNanUpper32 = 0xFFF7FFFF; |
| 73 |
constexpr uint32_t kHoleNanLower32 = 0xFFF7FFFF; |
| 74 |
|
| 75 |
constexpr uint64_t kHoleNanInt64 = |
| 76 |
(static_cast<uint64_t>(kHoleNanUpper32) << 32) | kHoleNanLower32; |
| 77 |
// Safety wrapper for a 32-bit floating-point value to make sure we don't lose |
| 78 |
// the exact bit pattern during deoptimization when passing this value. |
| 79 |
class Float32 { |
| 80 |
public: |
| 81 |
Float32() = default; |
| 82 |
|
| 83 |
// This constructor does not guarantee that bit pattern of the input value |
| 84 |
// is preserved if the input is a NaN. |
| 85 |
explicit Float32(float value) : bit_pattern_(bit_cast<uint32_t>(value)) { |
| 86 |
// Check that the provided value is not a NaN, because the bit pattern of a |
| 87 |
// NaN may be changed by a bit_cast, e.g. for signalling NaNs on |
| 88 |
// ia32. |
| 89 |
MOZ_ASSERT(!std::isnan(value)); |
| 90 |
} |
| 91 |
|
| 92 |
uint32_t get_bits() const { return bit_pattern_; } |
| 93 |
|
| 94 |
float get_scalar() const { return bit_cast<float>(bit_pattern_); } |
| 95 |
|
| 96 |
bool is_nan() const { |
| 97 |
// Even though {get_scalar()} might flip the quiet NaN bit, it's ok here, |
| 98 |
// because this does not change the is_nan property. |
| 99 |
return std::isnan(get_scalar()); |
| 100 |
} |
| 101 |
|
| 102 |
// Return a pointer to the field storing the bit pattern. Used in code |
| 103 |
// generation tests to store generated values there directly. |
| 104 |
uint32_t* get_bits_address() { return &bit_pattern_; } |
| 105 |
|
| 106 |
static constexpr Float32 FromBits(uint32_t bits) { return Float32(bits); } |
| 107 |
|
| 108 |
private: |
| 109 |
uint32_t bit_pattern_ = 0; |
| 110 |
|
| 111 |
explicit constexpr Float32(uint32_t bit_pattern) |
| 112 |
: bit_pattern_(bit_pattern) {} |
| 113 |
}; |
| 114 |
|
| 115 |
ASSERT_TRIVIALLY_COPYABLE(Float32); |
| 116 |
|
| 117 |
// Safety wrapper for a 64-bit floating-point value to make sure we don't lose |
| 118 |
// the exact bit pattern during deoptimization when passing this value. |
| 119 |
// TODO(ahaas): Unify this class with Double in double.h |
| 120 |
class Float64 { |
| 121 |
public: |
| 122 |
Float64() = default; |
| 123 |
|
| 124 |
// This constructor does not guarantee that bit pattern of the input value |
| 125 |
// is preserved if the input is a NaN. |
| 126 |
explicit Float64(double value) : bit_pattern_(bit_cast<uint64_t>(value)) { |
| 127 |
// Check that the provided value is not a NaN, because the bit pattern of a |
| 128 |
// NaN may be changed by a bit_cast, e.g. for signalling NaNs on |
| 129 |
// ia32. |
| 130 |
MOZ_ASSERT(!std::isnan(value)); |
| 131 |
} |
| 132 |
|
| 133 |
uint64_t get_bits() const { return bit_pattern_; } |
| 134 |
double get_scalar() const { return bit_cast<double>(bit_pattern_); } |
| 135 |
bool is_hole_nan() const { return bit_pattern_ == kHoleNanInt64; } |
| 136 |
bool is_nan() const { |
| 137 |
// Even though {get_scalar()} might flip the quiet NaN bit, it's ok here, |
| 138 |
// because this does not change the is_nan property. |
| 139 |
return std::isnan(get_scalar()); |
| 140 |
} |
| 141 |
|
| 142 |
// Return a pointer to the field storing the bit pattern. Used in code |
| 143 |
// generation tests to store generated values there directly. |
| 144 |
uint64_t* get_bits_address() { return &bit_pattern_; } |
| 145 |
|
| 146 |
static constexpr Float64 FromBits(uint64_t bits) { return Float64(bits); } |
| 147 |
|
| 148 |
private: |
| 149 |
uint64_t bit_pattern_ = 0; |
| 150 |
|
| 151 |
explicit constexpr Float64(uint64_t bit_pattern) |
| 152 |
: bit_pattern_(bit_pattern) {} |
| 153 |
}; |
| 154 |
|
| 155 |
ASSERT_TRIVIALLY_COPYABLE(Float64); |
| 156 |
|
| 157 |
class JitActivation; |
| 158 |
|
| 159 |
class Simulator; |
| 160 |
class Redirection; |
| 161 |
class CachePage; |
| 162 |
class AutoLockSimulator; |
| 163 |
|
| 164 |
// When the SingleStepCallback is called, the simulator is about to execute |
| 165 |
// sim->get_pc() and the current machine state represents the completed |
| 166 |
// execution of the previous pc. |
| 167 |
typedef void (*SingleStepCallback)(void* arg, Simulator* sim, void* pc); |
| 168 |
|
| 169 |
const intptr_t kPointerAlignment = 8; |
| 170 |
const intptr_t kPointerAlignmentMask = kPointerAlignment - 1; |
| 171 |
|
| 172 |
const intptr_t kDoubleAlignment = 8; |
| 173 |
const intptr_t kDoubleAlignmentMask = kDoubleAlignment - 1; |
| 174 |
|
| 175 |
// Number of general purpose registers. |
| 176 |
const int kNumRegisters = 32; |
| 177 |
|
| 178 |
// In the simulator, the PC register is simulated as the 34th register. |
| 179 |
const int kPCRegister = 32; |
| 180 |
|
| 181 |
// Number coprocessor registers. |
| 182 |
const int kNumFPURegisters = 32; |
| 183 |
|
| 184 |
// FPU (coprocessor 1) control registers. Currently only FCSR is implemented. |
| 185 |
const int kFCSRRegister = 31; |
| 186 |
const int kInvalidFPUControlRegister = -1; |
| 187 |
const uint32_t kFPUInvalidResult = static_cast<uint32_t>(1 << 31) - 1; |
| 188 |
const uint64_t kFPUInvalidResult64 = static_cast<uint64_t>(1ULL << 63) - 1; |
| 189 |
|
| 190 |
// FCSR constants. |
| 191 |
const uint32_t kFCSRInexactFlagBit = 2; |
| 192 |
const uint32_t kFCSRUnderflowFlagBit = 3; |
| 193 |
const uint32_t kFCSROverflowFlagBit = 4; |
| 194 |
const uint32_t kFCSRDivideByZeroFlagBit = 5; |
| 195 |
const uint32_t kFCSRInvalidOpFlagBit = 6; |
| 196 |
|
| 197 |
const uint32_t kFCSRInexactCauseBit = 12; |
| 198 |
const uint32_t kFCSRUnderflowCauseBit = 13; |
| 199 |
const uint32_t kFCSROverflowCauseBit = 14; |
| 200 |
const uint32_t kFCSRDivideByZeroCauseBit = 15; |
| 201 |
const uint32_t kFCSRInvalidOpCauseBit = 16; |
| 202 |
|
| 203 |
const uint32_t kFCSRInexactFlagMask = 1 << kFCSRInexactFlagBit; |
| 204 |
const uint32_t kFCSRUnderflowFlagMask = 1 << kFCSRUnderflowFlagBit; |
| 205 |
const uint32_t kFCSROverflowFlagMask = 1 << kFCSROverflowFlagBit; |
| 206 |
const uint32_t kFCSRDivideByZeroFlagMask = 1 << kFCSRDivideByZeroFlagBit; |
| 207 |
const uint32_t kFCSRInvalidOpFlagMask = 1 << kFCSRInvalidOpFlagBit; |
| 208 |
|
| 209 |
const uint32_t kFCSRFlagMask = |
| 210 |
kFCSRInexactFlagMask | kFCSRUnderflowFlagMask | kFCSROverflowFlagMask | |
| 211 |
kFCSRDivideByZeroFlagMask | kFCSRInvalidOpFlagMask; |
| 212 |
|
| 213 |
const uint32_t kFCSRExceptionFlagMask = kFCSRFlagMask ^ kFCSRInexactFlagMask; |
| 214 |
|
| 215 |
// ----------------------------------------------------------------------------- |
| 216 |
// Utility types and functions for RISCV |
| 217 |
# ifdef JS_CODEGEN_RISCV32 |
| 218 |
using sreg_t = int32_t; |
| 219 |
using reg_t = uint32_t; |
| 220 |
using freg_t = uint64_t; |
| 221 |
using sfreg_t = int64_t; |
| 222 |
# elif JS_CODEGEN_RISCV64 |
| 223 |
using sreg_t = int64_t; |
| 224 |
using reg_t = uint64_t; |
| 225 |
using freg_t = uint64_t; |
| 226 |
using sfreg_t = int64_t; |
| 227 |
# else |
| 228 |
# error "Cannot detect Riscv's bitwidth" |
| 229 |
# endif |
| 230 |
|
| 231 |
# define sext32(x) ((sreg_t)(int32_t)(x)) |
| 232 |
# define zext32(x) ((reg_t)(uint32_t)(x)) |
| 233 |
|
| 234 |
# ifdef JS_CODEGEN_RISCV64 |
| 235 |
# define sext_xlen(x) (((sreg_t)(x) << (64 - xlen)) >> (64 - xlen)) |
| 236 |
# define zext_xlen(x) (((reg_t)(x) << (64 - xlen)) >> (64 - xlen)) |
| 237 |
# elif JS_CODEGEN_RISCV32 |
| 238 |
# define sext_xlen(x) (((sreg_t)(x) << (32 - xlen)) >> (32 - xlen)) |
| 239 |
# define zext_xlen(x) (((reg_t)(x) << (32 - xlen)) >> (32 - xlen)) |
| 240 |
# endif |
| 241 |
|
| 242 |
# define BIT(n) (0x1LL << n) |
| 243 |
# define QUIET_BIT_S(nan) (bit_cast<int32_t>(nan) & BIT(22)) |
| 244 |
# define QUIET_BIT_D(nan) (bit_cast<int64_t>(nan) & BIT(51)) |
| 245 |
static inline bool isSnan(float fp) { return !QUIET_BIT_S(fp); } |
| 246 |
static inline bool isSnan(double fp) { return !QUIET_BIT_D(fp); } |
| 247 |
# undef QUIET_BIT_S |
| 248 |
# undef QUIET_BIT_D |
| 249 |
|
| 250 |
# ifdef JS_CODEGEN_RISCV64 |
| 251 |
inline uint64_t mulhu(uint64_t a, uint64_t b) { |
| 252 |
__uint128_t full_result = ((__uint128_t)a) * ((__uint128_t)b); |
| 253 |
return full_result >> 64; |
| 254 |
} |
| 255 |
|
| 256 |
inline int64_t mulh(int64_t a, int64_t b) { |
| 257 |
__int128_t full_result = ((__int128_t)a) * ((__int128_t)b); |
| 258 |
return full_result >> 64; |
| 259 |
} |
| 260 |
|
| 261 |
inline int64_t mulhsu(int64_t a, uint64_t b) { |
| 262 |
__int128_t full_result = ((__int128_t)a) * ((__uint128_t)b); |
| 263 |
return full_result >> 64; |
| 264 |
} |
| 265 |
# elif JS_CODEGEN_RISCV32 |
| 266 |
inline uint32_t mulhu(uint32_t a, uint32_t b) { |
| 267 |
uint64_t full_result = ((uint64_t)a) * ((uint64_t)b); |
| 268 |
uint64_t upper_part = full_result >> 32; |
| 269 |
return (uint32_t)upper_part; |
| 270 |
} |
| 271 |
|
| 272 |
inline int32_t mulh(int32_t a, int32_t b) { |
| 273 |
int64_t full_result = ((int64_t)a) * ((int64_t)b); |
| 274 |
int64_t upper_part = full_result >> 32; |
| 275 |
return (int32_t)upper_part; |
| 276 |
} |
| 277 |
|
| 278 |
inline int32_t mulhsu(int32_t a, uint32_t b) { |
| 279 |
int64_t full_result = ((int64_t)a) * ((uint64_t)b); |
| 280 |
int64_t upper_part = full_result >> 32; |
| 281 |
return (int32_t)upper_part; |
| 282 |
} |
| 283 |
# endif |
| 284 |
|
| 285 |
// Floating point helpers |
| 286 |
# define F32_SIGN ((uint32_t)1 << 31) |
| 287 |
union u32_f32 { |
| 288 |
uint32_t u; |
| 289 |
float f; |
| 290 |
}; |
| 291 |
inline float fsgnj32(float rs1, float rs2, bool n, bool x) { |
| 292 |
u32_f32 a = {.f = rs1}, b = {.f = rs2}; |
| 293 |
u32_f32 res; |
| 294 |
res.u = (a.u & ~F32_SIGN) | ((((x) ? a.u |
| 295 |
: (n) ? F32_SIGN |
| 296 |
: 0) ^ |
| 297 |
b.u) & |
| 298 |
F32_SIGN); |
| 299 |
return res.f; |
| 300 |
} |
| 301 |
|
| 302 |
inline Float32 fsgnj32(Float32 rs1, Float32 rs2, bool n, bool x) { |
| 303 |
u32_f32 a = {.u = rs1.get_bits()}, b = {.u = rs2.get_bits()}; |
| 304 |
u32_f32 res; |
| 305 |
if (x) { // RO_FSQNJX_S |
| 306 |
res.u = (a.u & ~F32_SIGN) | ((a.u ^ b.u) & F32_SIGN); |
| 307 |
} else { |
| 308 |
if (n) { // RO_FSGNJN_S |
| 309 |
res.u = (a.u & ~F32_SIGN) | ((F32_SIGN ^ b.u) & F32_SIGN); |
| 310 |
} else { // RO_FSGNJ_S |
| 311 |
res.u = (a.u & ~F32_SIGN) | ((0 ^ b.u) & F32_SIGN); |
| 312 |
} |
| 313 |
} |
| 314 |
return Float32::FromBits(res.u); |
| 315 |
} |
| 316 |
# define F64_SIGN ((uint64_t)1 << 63) |
| 317 |
union u64_f64 { |
| 318 |
uint64_t u; |
| 319 |
double d; |
| 320 |
}; |
| 321 |
inline double fsgnj64(double rs1, double rs2, bool n, bool x) { |
| 322 |
u64_f64 a = {.d = rs1}, b = {.d = rs2}; |
| 323 |
u64_f64 res; |
| 324 |
res.u = (a.u & ~F64_SIGN) | ((((x) ? a.u |
| 325 |
: (n) ? F64_SIGN |
| 326 |
: 0) ^ |
| 327 |
b.u) & |
| 328 |
F64_SIGN); |
| 329 |
return res.d; |
| 330 |
} |
| 331 |
|
| 332 |
inline Float64 fsgnj64(Float64 rs1, Float64 rs2, bool n, bool x) { |
| 333 |
u64_f64 a = {.d = rs1.get_scalar()}, b = {.d = rs2.get_scalar()}; |
| 334 |
u64_f64 res; |
| 335 |
if (x) { // RO_FSQNJX_D |
| 336 |
res.u = (a.u & ~F64_SIGN) | ((a.u ^ b.u) & F64_SIGN); |
| 337 |
} else { |
| 338 |
if (n) { // RO_FSGNJN_D |
| 339 |
res.u = (a.u & ~F64_SIGN) | ((F64_SIGN ^ b.u) & F64_SIGN); |
| 340 |
} else { // RO_FSGNJ_D |
| 341 |
res.u = (a.u & ~F64_SIGN) | ((0 ^ b.u) & F64_SIGN); |
| 342 |
} |
| 343 |
} |
| 344 |
return Float64::FromBits(res.u); |
| 345 |
} |
| 346 |
inline bool is_boxed_float(int64_t v) { return (uint32_t)((v >> 32) + 1) == 0; } |
| 347 |
inline int64_t box_float(float v) { |
| 348 |
return (0xFFFFFFFF00000000 | bit_cast<int32_t>(v)); |
| 349 |
} |
| 350 |
|
| 351 |
inline uint64_t box_float(uint32_t v) { return (0xFFFFFFFF00000000 | v); } |
| 352 |
|
| 353 |
// ----------------------------------------------------------------------------- |
| 354 |
// Utility functions |
| 355 |
|
| 356 |
class SimInstructionBase : public InstructionBase { |
| 357 |
public: |
| 358 |
Type InstructionType() const { return type_; } |
| 359 |
inline Instruction* instr() const { return instr_; } |
| 360 |
inline int32_t operand() const { return operand_; } |
| 361 |
|
| 362 |
protected: |
| 363 |
SimInstructionBase() : operand_(-1), instr_(nullptr), type_(kUnsupported) {} |
| 364 |
explicit SimInstructionBase(Instruction* instr) {} |
| 365 |
|
| 366 |
int32_t operand_; |
| 367 |
Instruction* instr_; |
| 368 |
Type type_; |
| 369 |
|
| 370 |
private: |
| 371 |
SimInstructionBase& operator=(const SimInstructionBase&) = delete; |
| 372 |
}; |
| 373 |
|
| 374 |
class SimInstruction : public InstructionGetters<SimInstructionBase> { |
| 375 |
public: |
| 376 |
SimInstruction() {} |
| 377 |
|
| 378 |
explicit SimInstruction(Instruction* instr) { *this = instr; } |
| 379 |
|
| 380 |
SimInstruction& operator=(Instruction* instr) { |
| 381 |
operand_ = *reinterpret_cast<const int32_t*>(instr); |
| 382 |
instr_ = instr; |
| 383 |
type_ = InstructionBase::InstructionType(); |
| 384 |
MOZ_ASSERT(reinterpret_cast<void*>(&operand_) == this); |
| 385 |
return *this; |
| 386 |
} |
| 387 |
}; |
| 388 |
|
| 389 |
// Per thread simulator state. |
| 390 |
class Simulator { |
| 391 |
friend class RiscvDebugger; |
| 392 |
|
| 393 |
public: |
| 394 |
static bool FLAG_riscv_trap_to_simulator_debugger; |
| 395 |
static bool FLAG_trace_sim; |
| 396 |
static bool FLAG_debug_sim; |
| 397 |
static bool FLAG_riscv_print_watchpoint; |
| 398 |
// Registers are declared in order. |
| 399 |
enum Register { |
| 400 |
no_reg = -1, |
| 401 |
x0 = 0, |
| 402 |
x1, |
| 403 |
x2, |
| 404 |
x3, |
| 405 |
x4, |
| 406 |
x5, |
| 407 |
x6, |
| 408 |
x7, |
| 409 |
x8, |
| 410 |
x9, |
| 411 |
x10, |
| 412 |
x11, |
| 413 |
x12, |
| 414 |
x13, |
| 415 |
x14, |
| 416 |
x15, |
| 417 |
x16, |
| 418 |
x17, |
| 419 |
x18, |
| 420 |
x19, |
| 421 |
x20, |
| 422 |
x21, |
| 423 |
x22, |
| 424 |
x23, |
| 425 |
x24, |
| 426 |
x25, |
| 427 |
x26, |
| 428 |
x27, |
| 429 |
x28, |
| 430 |
x29, |
| 431 |
x30, |
| 432 |
x31, |
| 433 |
pc, |
| 434 |
kNumSimuRegisters, |
| 435 |
// alias |
| 436 |
zero = x0, |
| 437 |
ra = x1, |
| 438 |
sp = x2, |
| 439 |
gp = x3, |
| 440 |
tp = x4, |
| 441 |
t0 = x5, |
| 442 |
t1 = x6, |
| 443 |
t2 = x7, |
| 444 |
fp = x8, |
| 445 |
s1 = x9, |
| 446 |
a0 = x10, |
| 447 |
a1 = x11, |
| 448 |
a2 = x12, |
| 449 |
a3 = x13, |
| 450 |
a4 = x14, |
| 451 |
a5 = x15, |
| 452 |
a6 = x16, |
| 453 |
a7 = x17, |
| 454 |
s2 = x18, |
| 455 |
s3 = x19, |
| 456 |
s4 = x20, |
| 457 |
s5 = x21, |
| 458 |
s6 = x22, |
| 459 |
s7 = x23, |
| 460 |
s8 = x24, |
| 461 |
s9 = x25, |
| 462 |
s10 = x26, |
| 463 |
s11 = x27, |
| 464 |
t3 = x28, |
| 465 |
t4 = x29, |
| 466 |
t5 = x30, |
| 467 |
t6 = x31, |
| 468 |
}; |
| 469 |
|
| 470 |
// Coprocessor registers. |
| 471 |
enum FPURegister { |
| 472 |
f0, |
| 473 |
f1, |
| 474 |
f2, |
| 475 |
f3, |
| 476 |
f4, |
| 477 |
f5, |
| 478 |
f6, |
| 479 |
f7, |
| 480 |
f8, |
| 481 |
f9, |
| 482 |
f10, |
| 483 |
f11, |
| 484 |
f12, |
| 485 |
f13, |
| 486 |
f14, |
| 487 |
f15, |
| 488 |
f16, |
| 489 |
f17, |
| 490 |
f18, |
| 491 |
f19, |
| 492 |
f20, |
| 493 |
f21, |
| 494 |
f22, |
| 495 |
f23, |
| 496 |
f24, |
| 497 |
f25, |
| 498 |
f26, |
| 499 |
f27, |
| 500 |
f28, |
| 501 |
f29, |
| 502 |
f30, |
| 503 |
f31, |
| 504 |
kNumFPURegisters, |
| 505 |
// alias |
| 506 |
ft0 = f0, |
| 507 |
ft1 = f1, |
| 508 |
ft2 = f2, |
| 509 |
ft3 = f3, |
| 510 |
ft4 = f4, |
| 511 |
ft5 = f5, |
| 512 |
ft6 = f6, |
| 513 |
ft7 = f7, |
| 514 |
fs0 = f8, |
| 515 |
fs1 = f9, |
| 516 |
fa0 = f10, |
| 517 |
fa1 = f11, |
| 518 |
fa2 = f12, |
| 519 |
fa3 = f13, |
| 520 |
fa4 = f14, |
| 521 |
fa5 = f15, |
| 522 |
fa6 = f16, |
| 523 |
fa7 = f17, |
| 524 |
fs2 = f18, |
| 525 |
fs3 = f19, |
| 526 |
fs4 = f20, |
| 527 |
fs5 = f21, |
| 528 |
fs6 = f22, |
| 529 |
fs7 = f23, |
| 530 |
fs8 = f24, |
| 531 |
fs9 = f25, |
| 532 |
fs10 = f26, |
| 533 |
fs11 = f27, |
| 534 |
ft8 = f28, |
| 535 |
ft9 = f29, |
| 536 |
ft10 = f30, |
| 537 |
ft11 = f31 |
| 538 |
}; |
| 539 |
|
| 540 |
// Returns nullptr on OOM. |
| 541 |
static Simulator* Create(); |
| 542 |
|
| 543 |
static void Destroy(Simulator* simulator); |
| 544 |
|
| 545 |
// Constructor/destructor are for internal use only; use the static methods |
| 546 |
// above. |
| 547 |
Simulator(); |
| 548 |
~Simulator(); |
| 549 |
|
| 550 |
// RISCV decoding routine |
| 551 |
void DecodeRVRType(); |
| 552 |
void DecodeRVR4Type(); |
| 553 |
void DecodeRVRFPType(); // Special routine for R/OP_FP type |
| 554 |
void DecodeRVRAType(); // Special routine for R/AMO type |
| 555 |
void DecodeRVIType(); |
| 556 |
void DecodeRVSType(); |
| 557 |
void DecodeRVBType(); |
| 558 |
void DecodeRVUType(); |
| 559 |
void DecodeRVJType(); |
| 560 |
void DecodeCRType(); |
| 561 |
void DecodeCAType(); |
| 562 |
void DecodeCIType(); |
| 563 |
void DecodeCIWType(); |
| 564 |
void DecodeCSSType(); |
| 565 |
void DecodeCLType(); |
| 566 |
void DecodeCSType(); |
| 567 |
void DecodeCJType(); |
| 568 |
void DecodeCBType(); |
| 569 |
# ifdef CAN_USE_RVV_INSTRUCTIONS |
| 570 |
void DecodeVType(); |
| 571 |
void DecodeRvvIVV(); |
| 572 |
void DecodeRvvIVI(); |
| 573 |
void DecodeRvvIVX(); |
| 574 |
void DecodeRvvMVV(); |
| 575 |
void DecodeRvvMVX(); |
| 576 |
void DecodeRvvFVV(); |
| 577 |
void DecodeRvvFVF(); |
| 578 |
bool DecodeRvvVL(); |
| 579 |
bool DecodeRvvVS(); |
| 580 |
# endif |
| 581 |
// The currently executing Simulator instance. Potentially there can be one |
| 582 |
// for each native thread. |
| 583 |
static Simulator* Current(); |
| 584 |
|
| 585 |
static inline uintptr_t StackLimit() { |
| 586 |
return Simulator::Current()->stackLimit(); |
| 587 |
} |
| 588 |
|
| 589 |
uintptr_t* addressOfStackLimit(); |
| 590 |
|
| 591 |
// Accessors for register state. Reading the pc value adheres to the MIPS |
| 592 |
// architecture specification and is off by a 8 from the currently executing |
| 593 |
// instruction. |
| 594 |
void setRegister(int reg, int64_t value); |
| 595 |
int64_t getRegister(int reg) const; |
| 596 |
// Same for FPURegisters. |
| 597 |
void setFpuRegister(int fpureg, int64_t value); |
| 598 |
void setFpuRegisterLo(int fpureg, int32_t value); |
| 599 |
void setFpuRegisterHi(int fpureg, int32_t value); |
| 600 |
void setFpuRegisterFloat(int fpureg, float value); |
| 601 |
void setFpuRegisterDouble(int fpureg, double value); |
| 602 |
void setFpuRegisterFloat(int fpureg, Float32 value); |
| 603 |
void setFpuRegisterDouble(int fpureg, Float64 value); |
| 604 |
|
| 605 |
int64_t getFpuRegister(int fpureg) const; |
| 606 |
int32_t getFpuRegisterLo(int fpureg) const; |
| 607 |
int32_t getFpuRegisterHi(int fpureg) const; |
| 608 |
float getFpuRegisterFloat(int fpureg) const; |
| 609 |
double getFpuRegisterDouble(int fpureg) const; |
| 610 |
Float32 getFpuRegisterFloat32(int fpureg) const; |
| 611 |
Float64 getFpuRegisterFloat64(int fpureg) const; |
| 612 |
|
| 613 |
inline int16_t shamt6() const { return (imm12() & 0x3F); } |
| 614 |
inline int16_t shamt5() const { return (imm12() & 0x1F); } |
| 615 |
inline int16_t rvc_shamt6() const { return instr_.RvcShamt6(); } |
| 616 |
inline int32_t s_imm12() const { return instr_.StoreOffset(); } |
| 617 |
inline int32_t u_imm20() const { return instr_.Imm20UValue() << 12; } |
| 618 |
inline int32_t rvc_u_imm6() const { return instr_.RvcImm6Value() << 12; } |
| 619 |
inline void require(bool check) { |
| 620 |
if (!check) { |
| 621 |
SignalException(kIllegalInstruction); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
// Special case of setRegister and getRegister to access the raw PC value. |
| 626 |
void set_pc(int64_t value); |
| 627 |
int64_t get_pc() const; |
| 628 |
|
| 629 |
SimInstruction instr_; |
| 630 |
// RISCV utlity API to access register value |
| 631 |
// Helpers for data value tracing. |
| 632 |
enum TraceType { |
| 633 |
BYTE, |
| 634 |
HALF, |
| 635 |
WORD, |
| 636 |
# if JS_CODEGEN_RISCV64 |
| 637 |
DWORD, |
| 638 |
# endif |
| 639 |
FLOAT, |
| 640 |
DOUBLE, |
| 641 |
// FLOAT_DOUBLE, |
| 642 |
// WORD_DWORD |
| 643 |
}; |
| 644 |
inline int32_t rs1_reg() const { return instr_.Rs1Value(); } |
| 645 |
inline sreg_t rs1() const { return getRegister(rs1_reg()); } |
| 646 |
inline float frs1() const { return getFpuRegisterFloat(rs1_reg()); } |
| 647 |
inline double drs1() const { return getFpuRegisterDouble(rs1_reg()); } |
| 648 |
inline Float32 frs1_boxed() const { return getFpuRegisterFloat32(rs1_reg()); } |
| 649 |
inline Float64 drs1_boxed() const { return getFpuRegisterFloat64(rs1_reg()); } |
| 650 |
inline int32_t rs2_reg() const { return instr_.Rs2Value(); } |
| 651 |
inline sreg_t rs2() const { return getRegister(rs2_reg()); } |
| 652 |
inline float frs2() const { return getFpuRegisterFloat(rs2_reg()); } |
| 653 |
inline double drs2() const { return getFpuRegisterDouble(rs2_reg()); } |
| 654 |
inline Float32 frs2_boxed() const { return getFpuRegisterFloat32(rs2_reg()); } |
| 655 |
inline Float64 drs2_boxed() const { return getFpuRegisterFloat64(rs2_reg()); } |
| 656 |
inline int32_t rs3_reg() const { return instr_.Rs3Value(); } |
| 657 |
inline sreg_t rs3() const { return getRegister(rs3_reg()); } |
| 658 |
inline float frs3() const { return getFpuRegisterFloat(rs3_reg()); } |
| 659 |
inline double drs3() const { return getFpuRegisterDouble(rs3_reg()); } |
| 660 |
inline Float32 frs3_boxed() const { return getFpuRegisterFloat32(rs3_reg()); } |
| 661 |
inline Float64 drs3_boxed() const { return getFpuRegisterFloat64(rs3_reg()); } |
| 662 |
inline int32_t rd_reg() const { return instr_.RdValue(); } |
| 663 |
inline int32_t frd_reg() const { return instr_.RdValue(); } |
| 664 |
inline int32_t rvc_rs1_reg() const { return instr_.RvcRs1Value(); } |
| 665 |
inline sreg_t rvc_rs1() const { return getRegister(rvc_rs1_reg()); } |
| 666 |
inline int32_t rvc_rs2_reg() const { return instr_.RvcRs2Value(); } |
| 667 |
inline sreg_t rvc_rs2() const { return getRegister(rvc_rs2_reg()); } |
| 668 |
inline double rvc_drs2() const { return getFpuRegisterDouble(rvc_rs2_reg()); } |
| 669 |
inline int32_t rvc_rs1s_reg() const { return instr_.RvcRs1sValue(); } |
| 670 |
inline sreg_t rvc_rs1s() const { return getRegister(rvc_rs1s_reg()); } |
| 671 |
inline int32_t rvc_rs2s_reg() const { return instr_.RvcRs2sValue(); } |
| 672 |
inline sreg_t rvc_rs2s() const { return getRegister(rvc_rs2s_reg()); } |
| 673 |
inline double rvc_drs2s() const { |
| 674 |
return getFpuRegisterDouble(rvc_rs2s_reg()); |
| 675 |
} |
| 676 |
inline int32_t rvc_rd_reg() const { return instr_.RvcRdValue(); } |
| 677 |
inline int32_t rvc_frd_reg() const { return instr_.RvcRdValue(); } |
| 678 |
inline int16_t boffset() const { return instr_.BranchOffset(); } |
| 679 |
inline int16_t imm12() const { return instr_.Imm12Value(); } |
| 680 |
inline int32_t imm20J() const { return instr_.Imm20JValue(); } |
| 681 |
inline int32_t imm5CSR() const { return instr_.Rs1Value(); } |
| 682 |
inline int16_t csr_reg() const { return instr_.CsrValue(); } |
| 683 |
inline int16_t rvc_imm6() const { return instr_.RvcImm6Value(); } |
| 684 |
inline int16_t rvc_imm6_addi16sp() const { |
| 685 |
return instr_.RvcImm6Addi16spValue(); |
| 686 |
} |
| 687 |
inline int16_t rvc_imm8_addi4spn() const { |
| 688 |
return instr_.RvcImm8Addi4spnValue(); |
| 689 |
} |
| 690 |
inline int16_t rvc_imm6_lwsp() const { return instr_.RvcImm6LwspValue(); } |
| 691 |
inline int16_t rvc_imm6_ldsp() const { return instr_.RvcImm6LdspValue(); } |
| 692 |
inline int16_t rvc_imm6_swsp() const { return instr_.RvcImm6SwspValue(); } |
| 693 |
inline int16_t rvc_imm6_sdsp() const { return instr_.RvcImm6SdspValue(); } |
| 694 |
inline int16_t rvc_imm5_w() const { return instr_.RvcImm5WValue(); } |
| 695 |
inline int16_t rvc_imm5_d() const { return instr_.RvcImm5DValue(); } |
| 696 |
inline int16_t rvc_imm8_b() const { return instr_.RvcImm8BValue(); } |
| 697 |
|
| 698 |
// Helper for debugging memory access. |
| 699 |
inline void DieOrDebug(); |
| 700 |
|
| 701 |
# if JS_CODEGEN_RISCV32 |
| 702 |
template <typename T> |
| 703 |
void TraceRegWr(T value, TraceType t = WORD); |
| 704 |
# elif JS_CODEGEN_RISCV64 |
| 705 |
void TraceRegWr(sreg_t value, TraceType t = DWORD); |
| 706 |
# endif |
| 707 |
void TraceMemWr(sreg_t addr, sreg_t value, TraceType t); |
| 708 |
template <typename T> |
| 709 |
void TraceMemRd(sreg_t addr, T value, sreg_t reg_value); |
| 710 |
void TraceMemRdDouble(sreg_t addr, double value, int64_t reg_value); |
| 711 |
void TraceMemRdDouble(sreg_t addr, Float64 value, int64_t reg_value); |
| 712 |
void TraceMemRdFloat(sreg_t addr, Float32 value, int64_t reg_value); |
| 713 |
|
| 714 |
template <typename T> |
| 715 |
void TraceLr(sreg_t addr, T value, sreg_t reg_value); |
| 716 |
|
| 717 |
template <typename T> |
| 718 |
void TraceSc(sreg_t addr, T value); |
| 719 |
|
| 720 |
template <typename T> |
| 721 |
void TraceMemWr(sreg_t addr, T value); |
| 722 |
void TraceMemWrDouble(sreg_t addr, double value); |
| 723 |
|
| 724 |
inline void set_rd(sreg_t value, bool trace = true) { |
| 725 |
setRegister(rd_reg(), value); |
| 726 |
# if JS_CODEGEN_RISCV64 |
| 727 |
if (trace) TraceRegWr(getRegister(rd_reg()), DWORD); |
| 728 |
# elif JS_CODEGEN_RISCV32 |
| 729 |
if (trace) TraceRegWr(getRegister(rd_reg()), WORD); |
| 730 |
# endif |
| 731 |
} |
| 732 |
inline void set_frd(float value, bool trace = true) { |
| 733 |
setFpuRegisterFloat(rd_reg(), value); |
| 734 |
if (trace) TraceRegWr(getFpuRegister(rd_reg()), FLOAT); |
| 735 |
} |
| 736 |
inline void set_frd(Float32 value, bool trace = true) { |
| 737 |
setFpuRegisterFloat(rd_reg(), value); |
| 738 |
if (trace) TraceRegWr(getFpuRegister(rd_reg()), FLOAT); |
| 739 |
} |
| 740 |
inline void set_drd(double value, bool trace = true) { |
| 741 |
setFpuRegisterDouble(rd_reg(), value); |
| 742 |
if (trace) TraceRegWr(getFpuRegister(rd_reg()), DOUBLE); |
| 743 |
} |
| 744 |
inline void set_drd(Float64 value, bool trace = true) { |
| 745 |
setFpuRegisterDouble(rd_reg(), value); |
| 746 |
if (trace) TraceRegWr(getFpuRegister(rd_reg()), DOUBLE); |
| 747 |
} |
| 748 |
inline void set_rvc_rd(sreg_t value, bool trace = true) { |
| 749 |
setRegister(rvc_rd_reg(), value); |
| 750 |
# if JS_CODEGEN_RISCV64 |
| 751 |
if (trace) TraceRegWr(getRegister(rvc_rd_reg()), DWORD); |
| 752 |
# elif JS_CODEGEN_RISCV32 |
| 753 |
if (trace) TraceRegWr(getRegister(rvc_rd_reg()), WORD); |
| 754 |
# endif |
| 755 |
} |
| 756 |
inline void set_rvc_rs1s(sreg_t value, bool trace = true) { |
| 757 |
setRegister(rvc_rs1s_reg(), value); |
| 758 |
# if JS_CODEGEN_RISCV64 |
| 759 |
if (trace) TraceRegWr(getRegister(rvc_rs1s_reg()), DWORD); |
| 760 |
# elif JS_CODEGEN_RISCV32 |
| 761 |
if (trace) TraceRegWr(getRegister(rvc_rs1s_reg()), WORD); |
| 762 |
# endif |
| 763 |
} |
| 764 |
inline void set_rvc_rs2(sreg_t value, bool trace = true) { |
| 765 |
setRegister(rvc_rs2_reg(), value); |
| 766 |
# if JS_CODEGEN_RISCV64 |
| 767 |
if (trace) TraceRegWr(getRegister(rvc_rs2_reg()), DWORD); |
| 768 |
# elif JS_CODEGEN_RISCV32 |
| 769 |
if (trace) TraceRegWr(getRegister(rvc_rs2_reg()), WORD); |
| 770 |
# endif |
| 771 |
} |
| 772 |
inline void set_rvc_drd(double value, bool trace = true) { |
| 773 |
setFpuRegisterDouble(rvc_rd_reg(), value); |
| 774 |
if (trace) TraceRegWr(getFpuRegister(rvc_rd_reg()), DOUBLE); |
| 775 |
} |
| 776 |
inline void set_rvc_drd(Float64 value, bool trace = true) { |
| 777 |
setFpuRegisterDouble(rvc_rd_reg(), value); |
| 778 |
if (trace) TraceRegWr(getFpuRegister(rvc_rd_reg()), DOUBLE); |
| 779 |
} |
| 780 |
inline void set_rvc_frd(Float32 value, bool trace = true) { |
| 781 |
setFpuRegisterFloat(rvc_rd_reg(), value); |
| 782 |
if (trace) TraceRegWr(getFpuRegister(rvc_rd_reg()), DOUBLE); |
| 783 |
} |
| 784 |
inline void set_rvc_rs2s(sreg_t value, bool trace = true) { |
| 785 |
setRegister(rvc_rs2s_reg(), value); |
| 786 |
# if JS_CODEGEN_RISCV64 |
| 787 |
if (trace) TraceRegWr(getRegister(rvc_rs2s_reg()), DWORD); |
| 788 |
# elif JS_CODEGEN_RISCV32 |
| 789 |
if (trace) TraceRegWr(getRegister(rvc_rs2s_reg()), WORD); |
| 790 |
# endif |
| 791 |
} |
| 792 |
inline void set_rvc_drs2s(double value, bool trace = true) { |
| 793 |
setFpuRegisterDouble(rvc_rs2s_reg(), value); |
| 794 |
if (trace) TraceRegWr(getFpuRegister(rvc_rs2s_reg()), DOUBLE); |
| 795 |
} |
| 796 |
inline void set_rvc_drs2s(Float64 value, bool trace = true) { |
| 797 |
setFpuRegisterDouble(rvc_rs2s_reg(), value); |
| 798 |
if (trace) TraceRegWr(getFpuRegister(rvc_rs2s_reg()), DOUBLE); |
| 799 |
} |
| 800 |
|
| 801 |
inline void set_rvc_frs2s(Float32 value, bool trace = true) { |
| 802 |
setFpuRegisterFloat(rvc_rs2s_reg(), value); |
| 803 |
if (trace) TraceRegWr(getFpuRegister(rvc_rs2s_reg()), FLOAT); |
| 804 |
} |
| 805 |
|
| 806 |
uint32_t get_dynamic_rounding_mode() { return read_csr_value(csr_frm); } |
| 807 |
|
| 808 |
// helper functions to read/write/set/clear CRC values/bits |
| 809 |
uint32_t read_csr_value(uint32_t csr) { |
| 810 |
switch (csr) { |
| 811 |
case csr_fflags: // Floating-Point Accrued Exceptions (RW) |
| 812 |
return (FCSR_ & kFcsrFlagsMask); |
| 813 |
case csr_frm: // Floating-Point Dynamic Rounding Mode (RW) |
| 814 |
return (FCSR_ & kFcsrFrmMask) >> kFcsrFrmShift; |
| 815 |
case csr_fcsr: // Floating-Point Control and Status Register (RW) |
| 816 |
return (FCSR_ & kFcsrMask); |
| 817 |
default: |
| 818 |
MOZ_CRASH("UNIMPLEMENTED"); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
void write_csr_value(uint32_t csr, reg_t val) { |
| 823 |
uint32_t value = (uint32_t)val; |
| 824 |
switch (csr) { |
| 825 |
case csr_fflags: // Floating-Point Accrued Exceptions (RW) |
| 826 |
MOZ_ASSERT(value <= ((1 << kFcsrFlagsBits) - 1)); |
| 827 |
FCSR_ = (FCSR_ & (~kFcsrFlagsMask)) | value; |
| 828 |
break; |
| 829 |
case csr_frm: // Floating-Point Dynamic Rounding Mode (RW) |
| 830 |
MOZ_ASSERT(value <= ((1 << kFcsrFrmBits) - 1)); |
| 831 |
FCSR_ = (FCSR_ & (~kFcsrFrmMask)) | (value << kFcsrFrmShift); |
| 832 |
break; |
| 833 |
case csr_fcsr: // Floating-Point Control and Status Register (RW) |
| 834 |
MOZ_ASSERT(value <= ((1 << kFcsrBits) - 1)); |
| 835 |
FCSR_ = (FCSR_ & (~kFcsrMask)) | value; |
| 836 |
break; |
| 837 |
default: |
| 838 |
MOZ_CRASH("UNIMPLEMENTED"); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
void set_csr_bits(uint32_t csr, reg_t val) { |
| 843 |
uint32_t value = (uint32_t)val; |
| 844 |
switch (csr) { |
| 845 |
case csr_fflags: // Floating-Point Accrued Exceptions (RW) |
| 846 |
MOZ_ASSERT(value <= ((1 << kFcsrFlagsBits) - 1)); |
| 847 |
FCSR_ = FCSR_ | value; |
| 848 |
break; |
| 849 |
case csr_frm: // Floating-Point Dynamic Rounding Mode (RW) |
| 850 |
MOZ_ASSERT(value <= ((1 << kFcsrFrmBits) - 1)); |
| 851 |
FCSR_ = FCSR_ | (value << kFcsrFrmShift); |
| 852 |
break; |
| 853 |
case csr_fcsr: // Floating-Point Control and Status Register (RW) |
| 854 |
MOZ_ASSERT(value <= ((1 << kFcsrBits) - 1)); |
| 855 |
FCSR_ = FCSR_ | value; |
| 856 |
break; |
| 857 |
default: |
| 858 |
MOZ_CRASH("UNIMPLEMENTED"); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
void clear_csr_bits(uint32_t csr, reg_t val) { |
| 863 |
uint32_t value = (uint32_t)val; |
| 864 |
switch (csr) { |
| 865 |
case csr_fflags: // Floating-Point Accrued Exceptions (RW) |
| 866 |
MOZ_ASSERT(value <= ((1 << kFcsrFlagsBits) - 1)); |
| 867 |
FCSR_ = FCSR_ & (~value); |
| 868 |
break; |
| 869 |
case csr_frm: // Floating-Point Dynamic Rounding Mode (RW) |
| 870 |
MOZ_ASSERT(value <= ((1 << kFcsrFrmBits) - 1)); |
| 871 |
FCSR_ = FCSR_ & (~(value << kFcsrFrmShift)); |
| 872 |
break; |
| 873 |
case csr_fcsr: // Floating-Point Control and Status Register (RW) |
| 874 |
MOZ_ASSERT(value <= ((1 << kFcsrBits) - 1)); |
| 875 |
FCSR_ = FCSR_ & (~value); |
| 876 |
break; |
| 877 |
default: |
| 878 |
MOZ_CRASH("UNIMPLEMENTED"); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
bool test_fflags_bits(uint32_t mask) { |
| 883 |
return (FCSR_ & kFcsrFlagsMask & mask) != 0; |
| 884 |
} |
| 885 |
|
| 886 |
void set_fflags(uint32_t flags) { set_csr_bits(csr_fflags, flags); } |
| 887 |
void clear_fflags(int32_t flags) { clear_csr_bits(csr_fflags, flags); } |
| 888 |
|
| 889 |
float RoundF2FHelper(float input_val, int rmode); |
| 890 |
double RoundF2FHelper(double input_val, int rmode); |
| 891 |
template <typename I_TYPE, typename F_TYPE> |
| 892 |
I_TYPE RoundF2IHelper(F_TYPE original, int rmode); |
| 893 |
|
| 894 |
template <typename T> |
| 895 |
T FMaxMinHelper(T a, T b, MaxMinKind kind); |
| 896 |
|
| 897 |
template <typename T> |
| 898 |
bool CompareFHelper(T input1, T input2, FPUCondition cc); |
| 899 |
|
| 900 |
template <typename T> |
| 901 |
T get_pc_as() const { |
| 902 |
return reinterpret_cast<T>(get_pc()); |
| 903 |
} |
| 904 |
|
| 905 |
void enable_single_stepping(SingleStepCallback cb, void* arg); |
| 906 |
void disable_single_stepping(); |
| 907 |
|
| 908 |
// Accessor to the internal simulator stack area. |
| 909 |
uintptr_t stackLimit() const; |
| 910 |
bool overRecursed(uintptr_t newsp = 0) const; |
| 911 |
bool overRecursedWithExtra(uint32_t extra) const; |
| 912 |
|
| 913 |
// Executes MIPS instructions until the PC reaches end_sim_pc. |
| 914 |
template <bool enableStopSimAt> |
| 915 |
void execute(); |
| 916 |
|
| 917 |
// Sets up the simulator state and grabs the result on return. |
| 918 |
int64_t call(uint8_t* entry, int argument_count, ...); |
| 919 |
|
| 920 |
// Push an address onto the JS stack. |
| 921 |
uintptr_t pushAddress(uintptr_t address); |
| 922 |
|
| 923 |
// Pop an address from the JS stack. |
| 924 |
uintptr_t popAddress(); |
| 925 |
|
| 926 |
// Debugger input. |
| 927 |
void setLastDebuggerInput(char* input); |
| 928 |
char* lastDebuggerInput() { return lastDebuggerInput_; } |
| 929 |
|
| 930 |
// Returns true if pc register contains one of the 'SpecialValues' defined |
| 931 |
// below (bad_ra, end_sim_pc). |
| 932 |
bool has_bad_pc() const; |
| 933 |
|
| 934 |
private: |
| 935 |
enum SpecialValues { |
| 936 |
// Known bad pc value to ensure that the simulator does not execute |
| 937 |
// without being properly setup. |
| 938 |
bad_ra = -1, |
| 939 |
// A pc value used to signal the simulator to stop execution. Generally |
| 940 |
// the ra is set to this value on transition from native C code to |
| 941 |
// simulated execution, so that the simulator can "return" to the native |
| 942 |
// C code. |
| 943 |
end_sim_pc = -2, |
| 944 |
// Unpredictable value. |
| 945 |
Unpredictable = 0xbadbeaf |
| 946 |
}; |
| 947 |
|
| 948 |
bool init(); |
| 949 |
|
| 950 |
// Unsupported instructions use Format to print an error and stop execution. |
| 951 |
void format(SimInstruction* instr, const char* format); |
| 952 |
|
| 953 |
// Read and write memory. |
| 954 |
// RISCV Memory read/write methods |
| 955 |
template <typename T> |
| 956 |
T ReadMem(sreg_t addr, Instruction* instr); |
| 957 |
template <typename T> |
| 958 |
void WriteMem(sreg_t addr, T value, Instruction* instr); |
| 959 |
template <typename T, typename OP> |
| 960 |
T amo(sreg_t addr, OP f, Instruction* instr, TraceType t) { |
| 961 |
auto lhs = ReadMem<T>(addr, instr); |
| 962 |
// TODO(RISCV): trace memory read for AMO |
| 963 |
WriteMem<T>(addr, (T)f(lhs), instr); |
| 964 |
return lhs; |
| 965 |
} |
| 966 |
|
| 967 |
inline int32_t loadLinkedW(uint64_t addr, SimInstruction* instr); |
| 968 |
inline int storeConditionalW(uint64_t addr, int32_t value, |
| 969 |
SimInstruction* instr); |
| 970 |
|
| 971 |
inline int64_t loadLinkedD(uint64_t addr, SimInstruction* instr); |
| 972 |
inline int storeConditionalD(uint64_t addr, int64_t value, |
| 973 |
SimInstruction* instr); |
| 974 |
|
| 975 |
// Used for breakpoints and traps. |
| 976 |
void SoftwareInterrupt(); |
| 977 |
|
| 978 |
// Stop helper functions. |
| 979 |
bool isWatchpoint(uint32_t code); |
| 980 |
bool IsTracepoint(uint32_t code); |
| 981 |
void printWatchpoint(uint32_t code); |
| 982 |
void handleStop(uint32_t code); |
| 983 |
bool isStopInstruction(SimInstruction* instr); |
| 984 |
bool isEnabledStop(uint32_t code); |
| 985 |
void enableStop(uint32_t code); |
| 986 |
void disableStop(uint32_t code); |
| 987 |
void increaseStopCounter(uint32_t code); |
| 988 |
void printStopInfo(uint32_t code); |
| 989 |
|
| 990 |
// Simulator breakpoints. |
| 991 |
struct Breakpoint { |
| 992 |
SimInstruction* location; |
| 993 |
bool enabled; |
| 994 |
bool is_tbreak; |
| 995 |
}; |
| 996 |
std::vector<Breakpoint> breakpoints_; |
| 997 |
void SetBreakpoint(SimInstruction* breakpoint, bool is_tbreak); |
| 998 |
void ListBreakpoints(); |
| 999 |
void CheckBreakpoints(); |
| 1000 |
|
| 1001 |
JS::ProfilingFrameIterator::RegisterState registerState(); |
| 1002 |
void HandleWasmTrap(); |
| 1003 |
|
| 1004 |
// Handle any wasm faults, returning true if the fault was handled. |
| 1005 |
// This method is rather hot so inline the normal (no-wasm) case. |
| 1006 |
bool MOZ_ALWAYS_INLINE handleWasmSegFault(uint64_t addr, unsigned numBytes) { |
| 1007 |
if (MOZ_LIKELY(!js::wasm::CodeExists)) { |
| 1008 |
return false; |
| 1009 |
} |
| 1010 |
|
| 1011 |
uint8_t* newPC; |
| 1012 |
if (!js::wasm::MemoryAccessTraps(registerState(), (uint8_t*)addr, numBytes, |
| 1013 |
&newPC)) { |
| 1014 |
return false; |
| 1015 |
} |
| 1016 |
|
| 1017 |
LLBit_ = false; |
| 1018 |
set_pc(int64_t(newPC)); |
| 1019 |
return true; |
| 1020 |
} |
| 1021 |
|
| 1022 |
// Executes one instruction. |
| 1023 |
void InstructionDecode(Instruction* instr); |
| 1024 |
|
| 1025 |
// ICache. |
| 1026 |
// static void CheckICache(base::CustomMatcherHashMap* i_cache, |
| 1027 |
// Instruction* instr); |
| 1028 |
// static void FlushOnePage(base::CustomMatcherHashMap* i_cache, intptr_t |
| 1029 |
// start, |
| 1030 |
// size_t size); |
| 1031 |
// static CachePage* GetCachePage(base::CustomMatcherHashMap* i_cache, |
| 1032 |
// void* page); |
| 1033 |
template <typename T, typename Func> |
| 1034 |
inline T CanonicalizeFPUOpFMA(Func fn, T dst, T src1, T src2) { |
| 1035 |
static_assert(std::is_floating_point<T>::value); |
| 1036 |
auto alu_out = fn(dst, src1, src2); |
| 1037 |
// if any input or result is NaN, the result is quiet_NaN |
| 1038 |
if (std::isnan(alu_out) || std::isnan(src1) || std::isnan(src2) || |
| 1039 |
std::isnan(dst)) { |
| 1040 |
// signaling_nan sets kInvalidOperation bit |
| 1041 |
if (isSnan(alu_out) || isSnan(src1) || isSnan(src2) || isSnan(dst)) |
| 1042 |
set_fflags(kInvalidOperation); |
| 1043 |
alu_out = std::numeric_limits<T>::quiet_NaN(); |
| 1044 |
} |
| 1045 |
return alu_out; |
| 1046 |
} |
| 1047 |
|
| 1048 |
template <typename T, typename Func> |
| 1049 |
inline T CanonicalizeFPUOp3(Func fn) { |
| 1050 |
static_assert(std::is_floating_point<T>::value); |
| 1051 |
T src1 = std::is_same<float, T>::value ? frs1() : drs1(); |
| 1052 |
T src2 = std::is_same<float, T>::value ? frs2() : drs2(); |
| 1053 |
T src3 = std::is_same<float, T>::value ? frs3() : drs3(); |
| 1054 |
auto alu_out = fn(src1, src2, src3); |
| 1055 |
// if any input or result is NaN, the result is quiet_NaN |
| 1056 |
if (std::isnan(alu_out) || std::isnan(src1) || std::isnan(src2) || |
| 1057 |
std::isnan(src3)) { |
| 1058 |
// signaling_nan sets kInvalidOperation bit |
| 1059 |
if (isSnan(alu_out) || isSnan(src1) || isSnan(src2) || isSnan(src3)) |
| 1060 |
set_fflags(kInvalidOperation); |
| 1061 |
alu_out = std::numeric_limits<T>::quiet_NaN(); |
| 1062 |
} |
| 1063 |
return alu_out; |
| 1064 |
} |
| 1065 |
|
| 1066 |
template <typename T, typename Func> |
| 1067 |
inline T CanonicalizeFPUOp2(Func fn) { |
| 1068 |
static_assert(std::is_floating_point<T>::value); |
| 1069 |
T src1 = std::is_same<float, T>::value ? frs1() : drs1(); |
| 1070 |
T src2 = std::is_same<float, T>::value ? frs2() : drs2(); |
| 1071 |
auto alu_out = fn(src1, src2); |
| 1072 |
// if any input or result is NaN, the result is quiet_NaN |
| 1073 |
if (std::isnan(alu_out) || std::isnan(src1) || std::isnan(src2)) { |
| 1074 |
// signaling_nan sets kInvalidOperation bit |
| 1075 |
if (isSnan(alu_out) || isSnan(src1) || isSnan(src2)) |
| 1076 |
set_fflags(kInvalidOperation); |
| 1077 |
alu_out = std::numeric_limits<T>::quiet_NaN(); |
| 1078 |
} |
| 1079 |
return alu_out; |
| 1080 |
} |
| 1081 |
|
| 1082 |
template <typename T, typename Func> |
| 1083 |
inline T CanonicalizeFPUOp1(Func fn) { |
| 1084 |
static_assert(std::is_floating_point<T>::value); |
| 1085 |
T src1 = std::is_same<float, T>::value ? frs1() : drs1(); |
| 1086 |
auto alu_out = fn(src1); |
| 1087 |
// if any input or result is NaN, the result is quiet_NaN |
| 1088 |
if (std::isnan(alu_out) || std::isnan(src1)) { |
| 1089 |
// signaling_nan sets kInvalidOperation bit |
| 1090 |
if (isSnan(alu_out) || isSnan(src1)) set_fflags(kInvalidOperation); |
| 1091 |
alu_out = std::numeric_limits<T>::quiet_NaN(); |
| 1092 |
} |
| 1093 |
return alu_out; |
| 1094 |
} |
| 1095 |
|
| 1096 |
template <typename Func> |
| 1097 |
inline float CanonicalizeDoubleToFloatOperation(Func fn) { |
| 1098 |
float alu_out = fn(drs1()); |
| 1099 |
if (std::isnan(alu_out) || std::isnan(drs1())) |
| 1100 |
alu_out = std::numeric_limits<float>::quiet_NaN(); |
| 1101 |
return alu_out; |
| 1102 |
} |
| 1103 |
|
| 1104 |
template <typename Func> |
| 1105 |
inline float CanonicalizeDoubleToFloatOperation(Func fn, double frs) { |
| 1106 |
float alu_out = fn(frs); |
| 1107 |
if (std::isnan(alu_out) || std::isnan(drs1())) |
| 1108 |
alu_out = std::numeric_limits<float>::quiet_NaN(); |
| 1109 |
return alu_out; |
| 1110 |
} |
| 1111 |
|
| 1112 |
template <typename Func> |
| 1113 |
inline float CanonicalizeFloatToDoubleOperation(Func fn, float frs) { |
| 1114 |
double alu_out = fn(frs); |
| 1115 |
if (std::isnan(alu_out) || std::isnan(frs1())) |
| 1116 |
alu_out = std::numeric_limits<double>::quiet_NaN(); |
| 1117 |
return alu_out; |
| 1118 |
} |
| 1119 |
|
| 1120 |
template <typename Func> |
| 1121 |
inline float CanonicalizeFloatToDoubleOperation(Func fn) { |
| 1122 |
double alu_out = fn(frs1()); |
| 1123 |
if (std::isnan(alu_out) || std::isnan(frs1())) |
| 1124 |
alu_out = std::numeric_limits<double>::quiet_NaN(); |
| 1125 |
return alu_out; |
| 1126 |
} |
| 1127 |
|
| 1128 |
public: |
| 1129 |
static int64_t StopSimAt; |
| 1130 |
|
| 1131 |
// Runtime call support. |
| 1132 |
static void* RedirectNativeFunction(void* nativeFunction, |
| 1133 |
ABIFunctionType type); |
| 1134 |
|
| 1135 |
private: |
| 1136 |
enum Exception { |
| 1137 |
none, |
| 1138 |
kIntegerOverflow, |
| 1139 |
kIntegerUnderflow, |
| 1140 |
kDivideByZero, |
| 1141 |
kNumExceptions, |
| 1142 |
// RISCV illegual instruction exception |
| 1143 |
kIllegalInstruction, |
| 1144 |
}; |
| 1145 |
int16_t exceptions[kNumExceptions]; |
| 1146 |
|
| 1147 |
// Exceptions. |
| 1148 |
void SignalException(Exception e); |
| 1149 |
|
| 1150 |
// Handle return value for runtime FP functions. |
| 1151 |
void setCallResultDouble(double result); |
| 1152 |
void setCallResultFloat(float result); |
| 1153 |
void setCallResult(int64_t res); |
| 1154 |
void setCallResult(__int128 res); |
| 1155 |
|
| 1156 |
void callInternal(uint8_t* entry); |
| 1157 |
|
| 1158 |
// Architecture state. |
| 1159 |
// Registers. |
| 1160 |
int64_t registers_[kNumSimuRegisters]; |
| 1161 |
// Coprocessor Registers. |
| 1162 |
int64_t FPUregisters_[kNumFPURegisters]; |
| 1163 |
// FPU control register. |
| 1164 |
uint32_t FCSR_; |
| 1165 |
|
| 1166 |
bool LLBit_; |
| 1167 |
uintptr_t LLAddr_; |
| 1168 |
int64_t lastLLValue_; |
| 1169 |
|
| 1170 |
// Simulator support. |
| 1171 |
char* stack_; |
| 1172 |
uintptr_t stackLimit_; |
| 1173 |
bool pc_modified_; |
| 1174 |
int64_t icount_; |
| 1175 |
int64_t break_count_; |
| 1176 |
|
| 1177 |
// Debugger input. |
| 1178 |
char* lastDebuggerInput_; |
| 1179 |
|
| 1180 |
intptr_t* watch_address_ = nullptr; |
| 1181 |
intptr_t watch_value_ = 0; |
| 1182 |
|
| 1183 |
// Registered breakpoints. |
| 1184 |
SimInstruction* break_pc_; |
| 1185 |
Instr break_instr_; |
| 1186 |
EmbeddedVector<char, 256> trace_buf_; |
| 1187 |
|
| 1188 |
// Single-stepping support |
| 1189 |
bool single_stepping_; |
| 1190 |
SingleStepCallback single_step_callback_; |
| 1191 |
void* single_step_callback_arg_; |
| 1192 |
|
| 1193 |
// A stop is watched if its code is less than kNumOfWatchedStops. |
| 1194 |
// Only watched stops support enabling/disabling and the counter feature. |
| 1195 |
static const uint32_t kNumOfWatchedStops = 256; |
| 1196 |
|
| 1197 |
// Stop is disabled if bit 31 is set. |
| 1198 |
static const uint32_t kStopDisabledBit = 1U << 31; |
| 1199 |
|
| 1200 |
// A stop is enabled, meaning the simulator will stop when meeting the |
| 1201 |
// instruction, if bit 31 of watchedStops_[code].count is unset. |
| 1202 |
// The value watchedStops_[code].count & ~(1 << 31) indicates how many times |
| 1203 |
// the breakpoint was hit or gone through. |
| 1204 |
struct StopCountAndDesc { |
| 1205 |
uint32_t count_; |
| 1206 |
char* desc_; |
| 1207 |
}; |
| 1208 |
StopCountAndDesc watchedStops_[kNumOfWatchedStops]; |
| 1209 |
}; |
| 1210 |
|
| 1211 |
// Process wide simulator state. |
| 1212 |
class SimulatorProcess { |
| 1213 |
friend class Redirection; |
| 1214 |
friend class AutoLockSimulatorCache; |
| 1215 |
|
| 1216 |
private: |
| 1217 |
// ICache checking. |
| 1218 |
struct ICacheHasher { |
| 1219 |
typedef void* Key; |
| 1220 |
typedef void* Lookup; |
| 1221 |
static HashNumber hash(const Lookup& l); |
| 1222 |
static bool match(const Key& k, const Lookup& l); |
| 1223 |
}; |
| 1224 |
|
| 1225 |
public: |
| 1226 |
typedef HashMap<void*, CachePage*, ICacheHasher, SystemAllocPolicy> ICacheMap; |
| 1227 |
|
| 1228 |
static mozilla::Atomic<size_t, mozilla::ReleaseAcquire> |
| 1229 |
ICacheCheckingDisableCount; |
| 1230 |
static void FlushICache(void* start, size_t size); |
| 1231 |
|
| 1232 |
static void checkICacheLocked(SimInstruction* instr); |
| 1233 |
|
| 1234 |
static bool initialize() { |
| 1235 |
singleton_ = js_new<SimulatorProcess>(); |
| 1236 |
return singleton_; |
| 1237 |
} |
| 1238 |
static void destroy() { |
| 1239 |
js_delete(singleton_); |
| 1240 |
singleton_ = nullptr; |
| 1241 |
} |
| 1242 |
|
| 1243 |
SimulatorProcess(); |
| 1244 |
~SimulatorProcess(); |
| 1245 |
|
| 1246 |
private: |
| 1247 |
static SimulatorProcess* singleton_; |
| 1248 |
|
| 1249 |
// This lock creates a critical section around 'redirection_' and |
| 1250 |
// 'icache_', which are referenced both by the execution engine |
| 1251 |
// and by the off-thread compiler (see Redirection::Get in the cpp file). |
| 1252 |
Mutex cacheLock_ MOZ_UNANNOTATED; |
| 1253 |
|
| 1254 |
Redirection* redirection_; |
| 1255 |
ICacheMap icache_; |
| 1256 |
|
| 1257 |
public: |
| 1258 |
static ICacheMap& icache() { |
| 1259 |
// Technically we need the lock to access the innards of the |
| 1260 |
// icache, not to take its address, but the latter condition |
| 1261 |
// serves as a useful complement to the former. |
| 1262 |
singleton_->cacheLock_.assertOwnedByCurrentThread(); |
| 1263 |
return singleton_->icache_; |
| 1264 |
} |
| 1265 |
|
| 1266 |
static Redirection* redirection() { |
| 1267 |
singleton_->cacheLock_.assertOwnedByCurrentThread(); |
| 1268 |
return singleton_->redirection_; |
| 1269 |
} |
| 1270 |
|
| 1271 |
static void setRedirection(js::jit::Redirection* redirection) { |
| 1272 |
singleton_->cacheLock_.assertOwnedByCurrentThread(); |
| 1273 |
singleton_->redirection_ = redirection; |
| 1274 |
} |
| 1275 |
}; |
| 1276 |
|
| 1277 |
} // namespace jit |
| 1278 |
} // namespace js |
| 1279 |
|
| 1280 |
#endif /* JS_SIMULATOR_MIPS64 */ |
| 1281 |
|
| 1282 |
#endif /* jit_riscv64_Simulator_riscv64_h */ |