Index: build/premake/premake5.lua =================================================================== --- build/premake/premake5.lua +++ build/premake/premake5.lua @@ -93,6 +93,8 @@ arch = "aarch64" elseif string.find(machine, "e2k") == 1 then arch = "e2k" + elseif string.find(machine, "riscv64") == 1 then + arch = "riscv64" elseif string.find(machine, "ppc64") == 1 or string.find(machine, "powerpc64") == 1 then arch = "ppc64" else @@ -885,6 +887,8 @@ table.insert(source_dirs, "lib/sysdep/arch/aarch64"); elseif arch == "e2k" then table.insert(source_dirs, "lib/sysdep/arch/e2k"); + elseif arch == "riscv64" then + table.insert(source_dirs, "lib/sysdep/arch/riscv64"); elseif arch == "ppc64" then table.insert(source_dirs, "lib/sysdep/arch/ppc64"); end Index: libraries/source/spidermonkey/build.sh =================================================================== --- libraries/source/spidermonkey/build.sh +++ libraries/source/spidermonkey/build.sh @@ -40,6 +40,13 @@ --enable-shared-js --disable-jitspew" +# unfortunately, spidermonkey doesn't have jit support on riscv yet +if [ $(gcc -dumpmachine | sed "s/\([^-]*\).*$/\1/") = "riscv64" ]; +then +CONF_OPTS="${CONF_OPTS} + --disable-jit" +fi + if [ "${OS}" = "Windows_NT" ] then CONF_OPTS="${CONF_OPTS} --with-visual-studio-version=2017 --target=i686" Index: libraries/source/spidermonkey/patch.sh =================================================================== --- libraries/source/spidermonkey/patch.sh +++ libraries/source/spidermonkey/patch.sh @@ -71,3 +71,12 @@ # https://svnweb.freebsd.org/ports/head/lang/spidermonkey78/files/patch-third__party_rust_cc_src_lib.rs?view=log patch -p1 < ../FixFreeBSDRustThirdPartyOSDetection.diff fi + +if [ $(gcc -dumpmachine | sed "s/\([^-]*\).*$/\1/") = "riscv64" ]; +then + # essential for building on riscv (no jit support unfortunately) + # https://bugzilla.mozilla.org/show_bug.cgi?id=1318905 + patch -p1 < ../riscv64-generic-support.diff + # https://salsa.debian.org/gnome-team/mozjs/-/blob/debian/78/master/debian/patches/Add-riscv64-support.patch + patch -p1 < ../riscv64-rust-fix.diff +fi Index: libraries/source/spidermonkey/riscv64-generic-support.diff =================================================================== --- /dev/null +++ libraries/source/spidermonkey/riscv64-generic-support.diff @@ -0,0 +1,99 @@ +Bug: https://bugs.gentoo.org/781137 +Upstream: https://bugzilla.mozilla.org/show_bug.cgi?id=1318905 + +Patches: +https://hg.mozilla.org/mozilla-central/rev/06d7e1b6b7e7 +https://hg.mozilla.org/mozilla-central/rev/ec48f15d085c +https://hg.mozilla.org/mozilla-central/rev/6803dda74d33 + +diff --git a/build/moz.configure/init.configure b/build/moz.configure/init.configure +index b887153321..5c27507606 100644 +--- a/build/moz.configure/init.configure ++++ b/build/moz.configure/init.configure +@@ -755,6 +755,9 @@ def split_triplet(triplet, allow_msvc=False): + elif cpu.startswith('aarch64'): + canonical_cpu = 'aarch64' + endianness = 'little' ++ elif cpu in ('riscv64', 'riscv64gc'): ++ canonical_cpu = 'riscv64' ++ endianness = 'little' + elif cpu == 'sh4': + canonical_cpu = 'sh4' + endianness = 'little' +diff --git a/python/mozbuild/mozbuild/configure/constants.py b/python/mozbuild/mozbuild/configure/constants.py +index 7542dcdc63..49ef3b857d 100644 +--- a/python/mozbuild/mozbuild/configure/constants.py ++++ b/python/mozbuild/mozbuild/configure/constants.py +@@ -49,6 +49,7 @@ CPU_bitness = { + 'mips64': 64, + 'ppc': 32, + 'ppc64': 64, ++ 'riscv64': 64, + 's390': 32, + 's390x': 64, + 'sh4': 32, +@@ -87,6 +88,7 @@ CPU_preprocessor_checks = OrderedDict(( + ('sparc', '__sparc__'), + ('mips64', '__mips64'), + ('mips32', '__mips__'), ++ ('riscv64', '__riscv && __riscv_xlen == 64'), + ('sh4', '__sh__'), + )) + +diff --git a/python/mozbuild/mozbuild/test/configure/test_toolchain_configure.py b/python/mozbuild/mozbuild/test/configure/test_toolchain_configure.py +index 37c4e26f0f..5d3d5891dc 100755 +--- a/python/mozbuild/mozbuild/test/configure/test_toolchain_configure.py ++++ b/python/mozbuild/mozbuild/test/configure/test_toolchain_configure.py +@@ -1208,6 +1208,10 @@ class LinuxCrossCompileToolchainTest(BaseToolchainTest): + 'mips-unknown-linux-gnu': big_endian + { + '__mips__': 1, + }, ++ 'riscv64-unknown-linux-gnu': little_endian + { ++ '__riscv': 1, ++ '__riscv_xlen': 64, ++ }, + 'sh4-unknown-linux-gnu': little_endian + { + '__sh__': 1, + }, +diff --git a/js/src/jit/AtomicOperations.h b/js/src/jit/AtomicOperations.h +index 0f45ac8b58..f48d2adce9 100644 +--- a/js/src/jit/AtomicOperations.h ++++ b/js/src/jit/AtomicOperations.h +@@ -392,6 +392,6 @@ inline bool AtomicOperations::isLockfreeJS(int32_t size) { + defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || \ + defined(__PPC64LE__) || defined(__alpha__) || defined(__hppa__) || \ +- defined(__sh__) || defined(__s390__) || defined(__s390x__) ++ defined(__sh__) || defined(__s390__) || defined(__s390x__) || defined(__riscv) + # include "jit/shared/AtomicOperations-feeling-lucky.h" + #else + # error "No AtomicOperations support provided for this platform" +diff --git a/js/src/jit/shared/AtomicOperations-feeling-lucky-gcc.h b/js/src/jit/shared/AtomicOperations-feeling-lucky-gcc.h +index f002cd46c9..1ce40efacc 100644 +--- a/js/src/jit/shared/AtomicOperations-feeling-lucky-gcc.h ++++ b/js/src/jit/shared/AtomicOperations-feeling-lucky-gcc.h +@@ -63,6 +63,11 @@ + # define HAS_64BIT_LOCKFREE + #endif + ++#if defined(__riscv) && __riscv_xlen == 64 ++# define HAS_64BIT_ATOMICS ++# define HAS_64BIT_LOCKFREE ++#endif ++ + #ifdef __sparc__ + # ifdef __LP64__ + # define HAS_64BIT_ATOMICS +diff --git a/mfbt/tests/TestPoisonArea.cpp b/mfbt/tests/TestPoisonArea.cpp +index fbd3364715..9d2ffa498b 100644 +--- a/mfbt/tests/TestPoisonArea.cpp ++++ b/mfbt/tests/TestPoisonArea.cpp +@@ -132,6 +132,9 @@ + #elif defined _ARCH_PPC || defined _ARCH_PWR || defined _ARCH_PWR2 + # define RETURN_INSTR 0x4E800020 /* blr */ + ++#elif defined __riscv ++# define RETURN_INSTR 0x80828082 /* ret; ret */ ++ + #elif defined __sparc || defined __sparcv9 + # define RETURN_INSTR 0x81c3e008 /* retl */ + Index: libraries/source/spidermonkey/riscv64-rust-fix.diff =================================================================== --- /dev/null +++ libraries/source/spidermonkey/riscv64-rust-fix.diff @@ -0,0 +1,66 @@ +From: William Grant +Date: Tue, 14 Apr 2020 02:18:28 +0200 +Subject: Add riscv64 support + +Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=932893 +Bug-Mozilla: https://bugzilla.mozilla.org/show_bug.cgi?id=1661027 +Original Patch: https://salsa.debian.org/gnome-team/mozjs/-/blob/debian/78/master/debian/patches/Add-riscv64-support.patch https://salsa.debian.org/gnome-team/mozjs/-/blob/debian/78/master/debian/patches/Add-riscv64-support.patch https://salsa.debian.org/gnome-team/mozjs/-/blob/debian/78/master/debian/patches/Add-riscv64-support.patch + +--- + third_party/rust/cc/.cargo-checksum.json | 2 +- + third_party/rust/cc/src/lib.rs | 14 +++++++++----- + 2 files changed, 25 insertions(+), 7 deletions(-) + +diff --git a/third_party/rust/cc/.cargo-checksum.json b/third_party/rust/cc/.cargo-checksum.json +index 417fde7..9836fc6 100644 +--- a/third_party/rust/cc/.cargo-checksum.json ++++ b/third_party/rust/cc/.cargo-checksum.json +@@ -1 +1 @@ +-{"files":{"Cargo.lock":"3aff5f8b0a7f4d72852b11b0526f0002e6bf55f19f1ebd6470d7f97fbd540e60","Cargo.toml":"6ab10d9b6a9c6f0909074e6698c90c6b6a7223661ec2e83174d2593117cbe7f2","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"378f5840b258e2779c39418f3f2d7b2ba96f1c7917dd6be0713f88305dbda397","README.md":"7184fbdf375a057e673257348f6d7584c0dd11b66318d98f3647f69eb610b097","src/bin/gcc-shim.rs":"b77907875029494b6288841c3aed2e4939ed40708c7f597fca5c9e2570490ca6","src/com.rs":"bcdaf1c28b71e6ef889c6b08d1ce9d7c0761344a677f523bc4c3cd297957f804","src/lib.rs":"4753929dbb7b676c19d7cfa06d0a47e37003554b80c536cbf2b892d591ef61c2","src/registry.rs":"3cc1b5a50879fa751572878ae1d0afbfc960c11665258492754b2c8bccb0ff5d","src/setup_config.rs":"7014103587d3382eac599cb76f016e2609b8140970861b2237982d1db24af265","src/winapi.rs":"ea8b7edbb9ff87957254f465c2334e714c5d6b3b19a8d757c48ea7ca0881c50c","src/windows_registry.rs":"388e79dcf3e84078ae0b086c6cdee9cf9eb7e3ffafdcbf3e2df26163661f5856","tests/cc_env.rs":"e02b3b0824ad039b47e4462c5ef6dbe6c824c28e7953af94a0f28f7b5158042e","tests/cflags.rs":"57f06eb5ce1557e5b4a032d0c4673e18fbe6f8d26c1deb153126e368b96b41b3","tests/cxxflags.rs":"c2c6c6d8a0d7146616fa1caed26876ee7bc9fcfffd525eb4743593cade5f3371","tests/support/mod.rs":"71620b178583b6e6e5e0d4cac14e2cef6afc62fb6841e0c72ed1784543abf8ac","tests/test.rs":"1605640c9b94a77f48fc92e1dc0485bdf1960da5626e2e00279e4703691656bc"},"package":"aa87058dce70a3ff5621797f1506cb837edd02ac4c0ae642b4542dce802908b8"} +\ No newline at end of file ++{"files":{"Cargo.lock":"3aff5f8b0a7f4d72852b11b0526f0002e6bf55f19f1ebd6470d7f97fbd540e60","Cargo.toml":"6ab10d9b6a9c6f0909074e6698c90c6b6a7223661ec2e83174d2593117cbe7f2","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"378f5840b258e2779c39418f3f2d7b2ba96f1c7917dd6be0713f88305dbda397","README.md":"7184fbdf375a057e673257348f6d7584c0dd11b66318d98f3647f69eb610b097","src/bin/gcc-shim.rs":"b77907875029494b6288841c3aed2e4939ed40708c7f597fca5c9e2570490ca6","src/com.rs":"bcdaf1c28b71e6ef889c6b08d1ce9d7c0761344a677f523bc4c3cd297957f804","src/lib.rs":"bbdfbab457b93c1f3fa58b97c99974a8cd61de38a9ea0a618ca73b0fb55c4d74","src/registry.rs":"3cc1b5a50879fa751572878ae1d0afbfc960c11665258492754b2c8bccb0ff5d","src/setup_config.rs":"7014103587d3382eac599cb76f016e2609b8140970861b2237982d1db24af265","src/winapi.rs":"ea8b7edbb9ff87957254f465c2334e714c5d6b3b19a8d757c48ea7ca0881c50c","src/windows_registry.rs":"388e79dcf3e84078ae0b086c6cdee9cf9eb7e3ffafdcbf3e2df26163661f5856","tests/cc_env.rs":"e02b3b0824ad039b47e4462c5ef6dbe6c824c28e7953af94a0f28f7b5158042e","tests/cflags.rs":"57f06eb5ce1557e5b4a032d0c4673e18fbe6f8d26c1deb153126e368b96b41b3","tests/cxxflags.rs":"c2c6c6d8a0d7146616fa1caed26876ee7bc9fcfffd525eb4743593cade5f3371","tests/support/mod.rs":"71620b178583b6e6e5e0d4cac14e2cef6afc62fb6841e0c72ed1784543abf8ac","tests/test.rs":"1605640c9b94a77f48fc92e1dc0485bdf1960da5626e2e00279e4703691656bc"},"package":"aa87058dce70a3ff5621797f1506cb837edd02ac4c0ae642b4542dce802908b8"} +diff --git a/third_party/rust/cc/src/lib.rs b/third_party/rust/cc/src/lib.rs +index 621d31d..968d03a 100644 +--- a/third_party/rust/cc/src/lib.rs ++++ b/third_party/rust/cc/src/lib.rs +@@ -1365,10 +1365,10 @@ impl Build { + cmd.push_cc_arg("-ffunction-sections".into()); + cmd.push_cc_arg("-fdata-sections".into()); + } +- // Disable generation of PIC on RISC-V for now: rust-lld doesn't support this yet ++ // Disable generation of PIC on bare-metal for now: rust-lld doesn't support this yet + if self + .pic +- .unwrap_or(!target.contains("windows-gnu") && !target.contains("riscv")) ++ .unwrap_or(!target.contains("windows") && !target.contains("-none-")) + { + cmd.push_cc_arg("-fPIC".into()); + // PLT only applies if code is compiled with PIC support, +@@ -1588,13 +1588,16 @@ impl Build { + if let Some(arch) = parts.next() { + let arch = &arch[5..]; + cmd.args.push(("-march=rv".to_owned() + arch).into()); +- // ABI is always soft-float right now, update this when this is no longer the +- // case: +- if arch.starts_with("64") { ++ if target.contains("linux") && arch.starts_with("64") { ++ cmd.args.push("-mabi=lp64d".into()); ++ } else if target.contains("linux") && arch.starts_with("32") { ++ cmd.args.push("-mabi=ilp32d".into()); ++ } else if arch.starts_with("64") { + cmd.args.push("-mabi=lp64".into()); + } else { + cmd.args.push("-mabi=ilp32".into()); + } ++ cmd.args.push("-mcmodel=medany".into()); + } + } + } +@@ -2024,6 +2027,7 @@ impl Build { + "riscv32imac-unknown-none-elf" => Some("riscv32-unknown-elf"), + "riscv32imc-unknown-none-elf" => Some("riscv32-unknown-elf"), + "riscv64gc-unknown-none-elf" => Some("riscv64-unknown-elf"), ++ "riscv64gc-unknown-linux-gnu" => Some("riscv64-linux-gnu"), + "riscv64imac-unknown-none-elf" => Some("riscv64-unknown-elf"), + "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu"), + "sparc-unknown-linux-gnu" => Some("sparc-linux-gnu"), Index: source/lib/byte_order.h =================================================================== --- source/lib/byte_order.h +++ source/lib/byte_order.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -33,7 +33,7 @@ #ifndef BYTE_ORDER # define LITTLE_ENDIAN 0x4321 # define BIG_ENDIAN 0x1234 -# if ARCH_IA32 || ARCH_IA64 || ARCH_AMD64 || ARCH_ALPHA || ARCH_ARM || ARCH_AARCH64 || ARCH_MIPS || ARCH_E2K || ARCH_PPC64 || defined(__LITTLE_ENDIAN__) +# if ARCH_IA32 || ARCH_IA64 || ARCH_AMD64 || ARCH_ALPHA || ARCH_ARM || ARCH_AARCH64 || ARCH_MIPS || ARCH_RISCV64 || ARCH_E2K || ARCH_PPC64 || defined(__LITTLE_ENDIAN__) # define BYTE_ORDER LITTLE_ENDIAN # else # define BYTE_ORDER BIG_ENDIAN Index: source/lib/sysdep/arch.h =================================================================== --- source/lib/sysdep/arch.h +++ source/lib/sysdep/arch.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -64,6 +64,12 @@ #else # define ARCH_AARCH64 0 #endif +// .. RISCV64 +#if defined(__riscv) && __riscv_xlen == 64 +# define ARCH_RISCV64 1 +#else +# define ARCH_RISCV64 0 +#endif // .. PowerPC64 (PPC64) #if defined(__powerpc64__) # define ARCH_PPC64 1 @@ -84,7 +90,7 @@ #endif // ensure exactly one architecture has been detected -#if (ARCH_IA32+ARCH_IA64+ARCH_AMD64+ARCH_ALPHA+ARCH_ARM+ARCH_AARCH64+ARCH_MIPS+ARCH_E2K+ARCH_PPC64) != 1 +#if (ARCH_IA32+ARCH_IA64+ARCH_AMD64+ARCH_ALPHA+ARCH_ARM+ARCH_AARCH64+ARCH_MIPS+ARCH_E2K+ARCH_RISCV64+ARCH_PPC64) != 1 # error "architecture not correctly detected (either none or multiple ARCH_* defined)" #endif Index: source/lib/sysdep/arch/riscv64/riscv64.cpp =================================================================== --- /dev/null +++ source/lib/sysdep/arch/riscv64/riscv64.cpp @@ -0,0 +1,49 @@ +/* Copyright (C) 2022 Wildfire Games. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * routines specific to RISCV64 + */ + +#include "precompiled.h" + +#include "lib/sysdep/cpu.h" + +intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment) +{ + return __sync_fetch_and_add(location, increment); +} + +bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue) +{ + return __sync_bool_compare_and_swap(location, expected, newValue); +} + +bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue) +{ + return __sync_bool_compare_and_swap(location, expected, newValue); +} + +const char* cpu_IdentifierString() +{ + return "unknown riscv64"; // TODO +} Index: source/lib/sysdep/cpu.h =================================================================== --- source/lib/sysdep/cpu.h +++ source/lib/sysdep/cpu.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -94,6 +94,12 @@ _mm_pause(); #elif GCC_VERSION && ARCH_X86_X64 __asm__ __volatile__( "rep; nop" : : : "memory" ); +#elif GCC_VERSION && ARCH_RISCV64 +/** + * A little hack for pause hint before zihintpause extension becomes more available + * https://github.com/riscv/riscv-isa-manual/pull/398 + **/ + __asm__ __volatile__( "div a5, a5, x0" : : : "memory" ); #endif } Index: source/ps/GameSetup/HWDetect.cpp =================================================================== --- source/ps/GameSetup/HWDetect.cpp +++ source/ps/GameSetup/HWDetect.cpp @@ -112,6 +112,7 @@ Script::SetProperty(rq, settings, "arch_arm", ARCH_ARM); Script::SetProperty(rq, settings, "arch_aarch64", ARCH_AARCH64); Script::SetProperty(rq, settings, "arch_e2k", ARCH_E2K); + Script::SetProperty(rq, settings, "arch_riscv64", ARCH_RISCV64); Script::SetProperty(rq, settings, "arch_ppc64", ARCH_PPC64); #ifdef NDEBUG