diff --git a/node.gyp b/node.gyp index 009c3f7b36f..3c8ec1e9afe 100644 --- a/node.gyp +++ b/node.gyp @@ -520,6 +520,7 @@ 'src/ffi/data.h', 'src/ffi/fast.cc', 'src/ffi/fast.h', + 'src/ffi/platforms/scalar_trampoline.h', 'src/ffi/jit_memory.cc', 'src/ffi/jit_memory.h', 'src/ffi/types.cc', diff --git a/src/ffi/platforms/loong64.cc b/src/ffi/platforms/loong64.cc index dda579b6c63..bac66187266 100644 --- a/src/ffi/platforms/loong64.cc +++ b/src/ffi/platforms/loong64.cc @@ -4,30 +4,20 @@ #if defined(__loongarch64) -#include -#include +#include "ffi/platforms/scalar_trampoline.h" #include namespace { using node::ffi::FastFFIType; - -bool IsFloatType(FastFFIType type) { - return type == FastFFIType::kFloat32 || type == FastFFIType::kFloat64; -} - -bool IsNarrowType(FastFFIType type) { - switch (type) { - case FastFFIType::kInt8: - case FastFFIType::kUint8: - case FastFFIType::kInt16: - case FastFFIType::kUint16: - return true; - default: - return false; - } -} +using node::ffi::scalar_trampoline::AllocateCode; +using node::ffi::scalar_trampoline::CanCreate; +using node::ffi::scalar_trampoline::Emit32; +using node::ffi::scalar_trampoline::Emit64; +using node::ffi::scalar_trampoline::FinalizeCode; +using node::ffi::scalar_trampoline::FreeTrampoline; +using node::ffi::scalar_trampoline::IsFloatType; uint32_t Or(unsigned rd, unsigned rj, unsigned rk) { return (0x2au << 15) | (rk << 10) | (rj << 5) | rd; @@ -47,30 +37,6 @@ uint32_t Jirl(unsigned rd, unsigned rj, int imm16) { (rj << 5) | rd; } -void Emit32(uint32_t** cursor, uint32_t value) { - *(*cursor)++ = value; -} - -void Emit64(uint32_t** cursor, uint64_t value) { - uint64_t* slot = reinterpret_cast(*cursor); - *slot = value; - *cursor += 2; -} - -void* AllocateCode(size_t code_size) { - void* code = mmap(nullptr, - code_size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, - -1, - 0); - return code == MAP_FAILED ? nullptr : code; -} - -void FreeCode(void* code, size_t code_size) { - munmap(code, code_size); -} - } // namespace extern "C" bool node_ffi_create_fast_trampoline( @@ -79,32 +45,15 @@ extern "C" bool node_ffi_create_fast_trampoline( size_t argc, node::ffi::FastFFIType result, node::ffi::FastFFITrampoline* out) { - if (config.target == nullptr || out == nullptr || IsNarrowType(result)) { - return false; - } - - size_t gp_count = 0; - size_t fp_count = 0; - for (size_t i = 0; i < argc; i++) { - if (args[i] == FastFFIType::kBuffer) { - return false; - } - if (IsFloatType(args[i])) { - fp_count++; - } else { - gp_count++; - } - } - // LoongArch64 passes integer arguments in a0..a7. V8's receiver occupies a0, // so user GP arguments arrive in a1..a7 and are shifted down before the tail // branch. FP arguments are already in fa0..fa7. - if (gp_count > 7 || fp_count > 8) { + size_t gp_count; + if (!CanCreate(config, args, argc, result, out, 7, 8, &gp_count)) { return false; } - constexpr size_t kCodeSize = 256; - void* code = AllocateCode(kCodeSize); + void* code = AllocateCode(); if (code == nullptr) { return false; } @@ -130,27 +79,12 @@ extern "C" bool node_ffi_create_fast_trampoline( const size_t written = reinterpret_cast(cursor) - static_cast(code); - __builtin___clear_cache(static_cast(code), - static_cast(code) + written); - - if (mprotect(code, kCodeSize, PROT_READ | PROT_EXEC) != 0) { - FreeCode(code, kCodeSize); - return false; - } - - out->code = code; - out->size = kCodeSize; - return true; + return FinalizeCode(code, written, out); } extern "C" void node_ffi_free_fast_trampoline( node::ffi::FastFFITrampoline* trampoline) { - if (trampoline == nullptr || trampoline->code == nullptr) { - return; - } - FreeCode(trampoline->code, trampoline->size); - trampoline->code = nullptr; - trampoline->size = 0; + FreeTrampoline(trampoline); } #endif // defined(__loongarch64) diff --git a/src/ffi/platforms/ppc64.cc b/src/ffi/platforms/ppc64.cc index 52808abac89..156de1c0f01 100644 --- a/src/ffi/platforms/ppc64.cc +++ b/src/ffi/platforms/ppc64.cc @@ -8,30 +8,20 @@ __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) && \ !defined(_AIX) -#include -#include +#include "ffi/platforms/scalar_trampoline.h" #include namespace { using node::ffi::FastFFIType; - -bool IsFloatType(FastFFIType type) { - return type == FastFFIType::kFloat32 || type == FastFFIType::kFloat64; -} - -bool IsNarrowType(FastFFIType type) { - switch (type) { - case FastFFIType::kInt8: - case FastFFIType::kUint8: - case FastFFIType::kInt16: - case FastFFIType::kUint16: - return true; - default: - return false; - } -} +using node::ffi::scalar_trampoline::AllocateCode; +using node::ffi::scalar_trampoline::CanCreate; +using node::ffi::scalar_trampoline::Emit32; +using node::ffi::scalar_trampoline::Emit64; +using node::ffi::scalar_trampoline::FinalizeCode; +using node::ffi::scalar_trampoline::FreeTrampoline; +using node::ffi::scalar_trampoline::IsFloatType; uint32_t Or(unsigned ra, unsigned rs, unsigned rb) { return (31u << 26) | (rs << 21) | (ra << 16) | (rb << 11) | (444u << 1); @@ -59,30 +49,6 @@ uint32_t Ld(unsigned rt, unsigned ra, unsigned offset) { return (58u << 26) | (rt << 21) | (ra << 16) | (offset & 0xfffcu); } -void Emit32(uint32_t** cursor, uint32_t value) { - *(*cursor)++ = value; -} - -void Emit64(uint32_t** cursor, uint64_t value) { - uint64_t* slot = reinterpret_cast(*cursor); - *slot = value; - *cursor += 2; -} - -void* AllocateCode(size_t code_size) { - void* code = mmap(nullptr, - code_size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, - -1, - 0); - return code == MAP_FAILED ? nullptr : code; -} - -void FreeCode(void* code, size_t code_size) { - munmap(code, code_size); -} - } // namespace extern "C" bool node_ffi_create_fast_trampoline( @@ -91,32 +57,15 @@ extern "C" bool node_ffi_create_fast_trampoline( size_t argc, node::ffi::FastFFIType result, node::ffi::FastFFITrampoline* out) { - if (config.target == nullptr || out == nullptr || IsNarrowType(result)) { - return false; - } - - size_t gp_count = 0; - size_t fp_count = 0; - for (size_t i = 0; i < argc; i++) { - if (args[i] == FastFFIType::kBuffer) { - return false; - } - if (IsFloatType(args[i])) { - fp_count++; - } else { - gp_count++; - } - } - // ELFv2 PPC64LE passes integer arguments in r3..r10. V8's receiver occupies // r3, so the scalar-only fast path keeps user GP arguments in r4..r10 and // shifts them down before tail-branching to the native target. - if (gp_count > 7 || fp_count > 8) { + size_t gp_count; + if (!CanCreate(config, args, argc, result, out, 7, 8, &gp_count)) { return false; } - constexpr size_t kCodeSize = 256; - void* code = AllocateCode(kCodeSize); + void* code = AllocateCode(); if (code == nullptr) { return false; } @@ -153,27 +102,12 @@ extern "C" bool node_ffi_create_fast_trampoline( const size_t written = reinterpret_cast(cursor) - static_cast(code); - __builtin___clear_cache(static_cast(code), - static_cast(code) + written); - - if (mprotect(code, kCodeSize, PROT_READ | PROT_EXEC) != 0) { - FreeCode(code, kCodeSize); - return false; - } - - out->code = code; - out->size = kCodeSize; - return true; + return FinalizeCode(code, written, out); } extern "C" void node_ffi_free_fast_trampoline( node::ffi::FastFFITrampoline* trampoline) { - if (trampoline == nullptr || trampoline->code == nullptr) { - return; - } - FreeCode(trampoline->code, trampoline->size); - trampoline->code = nullptr; - trampoline->size = 0; + FreeTrampoline(trampoline); } #else diff --git a/src/ffi/platforms/riscv64.cc b/src/ffi/platforms/riscv64.cc index 841b0db85f6..4fa138a1df1 100644 --- a/src/ffi/platforms/riscv64.cc +++ b/src/ffi/platforms/riscv64.cc @@ -4,30 +4,20 @@ #if defined(__riscv) && __riscv_xlen == 64 -#include -#include +#include "ffi/platforms/scalar_trampoline.h" #include namespace { using node::ffi::FastFFIType; - -bool IsFloatType(FastFFIType type) { - return type == FastFFIType::kFloat32 || type == FastFFIType::kFloat64; -} - -bool IsNarrowType(FastFFIType type) { - switch (type) { - case FastFFIType::kInt8: - case FastFFIType::kUint8: - case FastFFIType::kInt16: - case FastFFIType::kUint16: - return true; - default: - return false; - } -} +using node::ffi::scalar_trampoline::AllocateCode; +using node::ffi::scalar_trampoline::CanCreate; +using node::ffi::scalar_trampoline::Emit32; +using node::ffi::scalar_trampoline::Emit64; +using node::ffi::scalar_trampoline::FinalizeCode; +using node::ffi::scalar_trampoline::FreeTrampoline; +using node::ffi::scalar_trampoline::IsFloatType; uint32_t Addi(unsigned rd, unsigned rs1, int imm) { return ((static_cast(imm) & 0xfff) << 20) | (rs1 << 15) | @@ -48,30 +38,6 @@ uint32_t Jalr(unsigned rd, unsigned rs1, int imm) { (rd << 7) | 0x67; } -void Emit32(uint32_t** cursor, uint32_t value) { - *(*cursor)++ = value; -} - -void Emit64(uint32_t** cursor, uint64_t value) { - uint64_t* slot = reinterpret_cast(*cursor); - *slot = value; - *cursor += 2; -} - -void* AllocateCode(size_t code_size) { - void* code = mmap(nullptr, - code_size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, - -1, - 0); - return code == MAP_FAILED ? nullptr : code; -} - -void FreeCode(void* code, size_t code_size) { - munmap(code, code_size); -} - } // namespace extern "C" bool node_ffi_create_fast_trampoline( @@ -80,32 +46,15 @@ extern "C" bool node_ffi_create_fast_trampoline( size_t argc, node::ffi::FastFFIType result, node::ffi::FastFFITrampoline* out) { - if (config.target == nullptr || out == nullptr || IsNarrowType(result)) { - return false; - } - - size_t gp_count = 0; - size_t fp_count = 0; - for (size_t i = 0; i < argc; i++) { - if (args[i] == FastFFIType::kBuffer) { - return false; - } - if (IsFloatType(args[i])) { - fp_count++; - } else { - gp_count++; - } - } - // RISC-V LP64D passes integer arguments in a0..a7. V8's receiver occupies // a0, so user GP arguments arrive in a1..a7 and are shifted down before the // tail branch. FP arguments are already in fa0..fa7. - if (gp_count > 7 || fp_count > 8) { + size_t gp_count; + if (!CanCreate(config, args, argc, result, out, 7, 8, &gp_count)) { return false; } - constexpr size_t kCodeSize = 256; - void* code = AllocateCode(kCodeSize); + void* code = AllocateCode(); if (code == nullptr) { return false; } @@ -131,27 +80,12 @@ extern "C" bool node_ffi_create_fast_trampoline( const size_t written = reinterpret_cast(cursor) - static_cast(code); - __builtin___clear_cache(static_cast(code), - static_cast(code) + written); - - if (mprotect(code, kCodeSize, PROT_READ | PROT_EXEC) != 0) { - FreeCode(code, kCodeSize); - return false; - } - - out->code = code; - out->size = kCodeSize; - return true; + return FinalizeCode(code, written, out); } extern "C" void node_ffi_free_fast_trampoline( node::ffi::FastFFITrampoline* trampoline) { - if (trampoline == nullptr || trampoline->code == nullptr) { - return; - } - FreeCode(trampoline->code, trampoline->size); - trampoline->code = nullptr; - trampoline->size = 0; + FreeTrampoline(trampoline); } #endif // defined(__riscv) && __riscv_xlen == 64 diff --git a/src/ffi/platforms/s390x.cc b/src/ffi/platforms/s390x.cc index 5ec44d9e486..b4cc94edf48 100644 --- a/src/ffi/platforms/s390x.cc +++ b/src/ffi/platforms/s390x.cc @@ -4,30 +4,18 @@ #if defined(__s390x__) -#include -#include +#include "ffi/platforms/scalar_trampoline.h" #include namespace { using node::ffi::FastFFIType; - -bool IsFloatType(FastFFIType type) { - return type == FastFFIType::kFloat32 || type == FastFFIType::kFloat64; -} - -bool IsNarrowType(FastFFIType type) { - switch (type) { - case FastFFIType::kInt8: - case FastFFIType::kUint8: - case FastFFIType::kInt16: - case FastFFIType::kUint16: - return true; - default: - return false; - } -} +using node::ffi::scalar_trampoline::AllocateCode; +using node::ffi::scalar_trampoline::CanCreate; +using node::ffi::scalar_trampoline::FinalizeCode; +using node::ffi::scalar_trampoline::FreeTrampoline; +using node::ffi::scalar_trampoline::IsFloatType; uint32_t Lgr(unsigned r1, unsigned r2) { return 0xb9040000u | (r1 << 4) | r2; @@ -69,20 +57,6 @@ void Emit64(uint8_t** cursor, uint64_t value) { } } -void* AllocateCode(size_t code_size) { - void* code = mmap(nullptr, - code_size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, - -1, - 0); - return code == MAP_FAILED ? nullptr : code; -} - -void FreeCode(void* code, size_t code_size) { - munmap(code, code_size); -} - } // namespace extern "C" bool node_ffi_create_fast_trampoline( @@ -91,32 +65,15 @@ extern "C" bool node_ffi_create_fast_trampoline( size_t argc, node::ffi::FastFFIType result, node::ffi::FastFFITrampoline* out) { - if (config.target == nullptr || out == nullptr || IsNarrowType(result)) { - return false; - } - - size_t gp_count = 0; - size_t fp_count = 0; - for (size_t i = 0; i < argc; i++) { - if (args[i] == FastFFIType::kBuffer) { - return false; - } - if (IsFloatType(args[i])) { - fp_count++; - } else { - gp_count++; - } - } - // Linux s390x passes integer arguments in r2..r6. V8's receiver occupies r2, // so user GP arguments arrive in r3..r6 and are shifted down before the tail // branch. FP arguments are already in f0, f2, f4, and f6. - if (gp_count > 4 || fp_count > 4) { + size_t gp_count; + if (!CanCreate(config, args, argc, result, out, 4, 4, &gp_count)) { return false; } - constexpr size_t kCodeSize = 256; - void* code = AllocateCode(kCodeSize); + void* code = AllocateCode(); if (code == nullptr) { return false; } @@ -139,27 +96,12 @@ extern "C" bool node_ffi_create_fast_trampoline( Emit64(&cursor, reinterpret_cast(config.target)); const size_t written = cursor - static_cast(code); - __builtin___clear_cache(static_cast(code), - static_cast(code) + written); - - if (mprotect(code, kCodeSize, PROT_READ | PROT_EXEC) != 0) { - FreeCode(code, kCodeSize); - return false; - } - - out->code = code; - out->size = kCodeSize; - return true; + return FinalizeCode(code, written, out); } extern "C" void node_ffi_free_fast_trampoline( node::ffi::FastFFITrampoline* trampoline) { - if (trampoline == nullptr || trampoline->code == nullptr) { - return; - } - FreeCode(trampoline->code, trampoline->size); - trampoline->code = nullptr; - trampoline->size = 0; + FreeTrampoline(trampoline); } #endif // defined(__s390x__) diff --git a/src/ffi/platforms/scalar_trampoline.h b/src/ffi/platforms/scalar_trampoline.h new file mode 100644 index 00000000000..e10087c1701 --- /dev/null +++ b/src/ffi/platforms/scalar_trampoline.h @@ -0,0 +1,123 @@ +#pragma once + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +// Shared helpers for the POSIX platforms whose fast FFI trampoline only shifts +// scalar GP arguments down by one register (dropping V8's receiver) and then +// tail-branches to the native target: loong64, ppc64, riscv64 and s390x. + +#include "ffi/fast.h" + +#include + +#include +#include + +namespace node::ffi::scalar_trampoline { + +// Size of the executable mapping allocated for each trampoline. +constexpr size_t kCodeSize = 256; + +inline bool IsFloatType(FastFFIType type) { + return type == FastFFIType::kFloat32 || type == FastFFIType::kFloat64; +} + +inline bool IsNarrowType(FastFFIType type) { + switch (type) { + case FastFFIType::kInt8: + case FastFFIType::kUint8: + case FastFFIType::kInt16: + case FastFFIType::kUint16: + return true; + default: + return false; + } +} + +// Validates the signature for the scalar-only fast path. Returns false when +// the trampoline cannot be generated, either because the inputs are invalid, +// the result needs narrowing, an argument is a buffer, or there are more GP or +// FP arguments than the platform passes in registers. +inline bool CanCreate(const FastFFITrampolineConfig& config, + const FastFFIType* args, + size_t argc, + FastFFIType result, + const FastFFITrampoline* out, + size_t max_gp_args, + size_t max_fp_args, + size_t* gp_count_out) { + if (config.target == nullptr || out == nullptr || IsNarrowType(result)) { + return false; + } + + size_t gp_count = 0; + size_t fp_count = 0; + for (size_t i = 0; i < argc; i++) { + if (args[i] == FastFFIType::kBuffer) { + return false; + } + if (IsFloatType(args[i])) { + fp_count++; + } else { + gp_count++; + } + } + + if (gp_count > max_gp_args || fp_count > max_fp_args) { + return false; + } + *gp_count_out = gp_count; + return true; +} + +inline void* AllocateCode() { + void* code = mmap(nullptr, + kCodeSize, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, + -1, + 0); + return code == MAP_FAILED ? nullptr : code; +} + +// Flushes the instruction cache for the `written` bytes emitted at `code`, +// makes the mapping executable and stores it in `out`. On failure the mapping +// is released and false is returned. +inline bool FinalizeCode(void* code, size_t written, FastFFITrampoline* out) { + __builtin___clear_cache(static_cast(code), + static_cast(code) + written); + + if (mprotect(code, kCodeSize, PROT_READ | PROT_EXEC) != 0) { + munmap(code, kCodeSize); + return false; + } + + out->code = code; + out->size = kCodeSize; + return true; +} + +inline void FreeTrampoline(FastFFITrampoline* trampoline) { + if (trampoline == nullptr || trampoline->code == nullptr) { + return; + } + munmap(trampoline->code, trampoline->size); + trampoline->code = nullptr; + trampoline->size = 0; +} + +// Word emitters for platforms with fixed-width 32-bit, native-endian +// instructions. The 64-bit literal must be 8-byte aligned by the caller. +inline void Emit32(uint32_t** cursor, uint32_t value) { + *(*cursor)++ = value; +} + +inline void Emit64(uint32_t** cursor, uint64_t value) { + uint64_t* slot = reinterpret_cast(*cursor); + *slot = value; + *cursor += 2; +} + +} // namespace node::ffi::scalar_trampoline + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS