1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 06:34:57 +00:00

Userland+Tests: Add initial riscv64 support

This commit is contained in:
Sönke Holz 2023-09-18 12:56:39 +02:00 committed by Andrew Kaster
parent c6b2a07326
commit 6824d2a788
23 changed files with 262 additions and 34 deletions

View file

@ -1 +1,20 @@
# Intentionally empty.
.global _setjmp
.global setjmp
_setjmp:
setjmp:
# FIXME: Implement setjmp.
unimp
.global _longjmp
.global longjmp
_longjmp:
longjmp:
# FIXME: Implement longjmp.
unimp
.global _sigsetjmp
.global sigsetjmp
_sigsetjmp:
sigsetjmp:
# FIXME: Implement sigsetjmp.
unimp

View file

@ -27,10 +27,17 @@ NAKED void _start(int, char**, char**)
"mov x29, 0\n"
"mov x30, 0\n"
"bl _entry\n");
# else
# elif ARCH(RISCV64)
asm(
"li fp, 0\n"
"li ra, 0\n"
"tail _entry@plt\n");
# elif ARCH(X86_64)
asm(
"push $0\n"
"jmp _entry@plt\n");
# else
# error "Unknown architecture"
# endif
}

View file

@ -10,7 +10,7 @@
// This is the size of the floating point environment image in protected mode
static_assert(sizeof(__x87_floating_point_environment) == 28);
#if !ARCH(AARCH64)
#if ARCH(X86_64)
static u16 read_status_register()
{
u16 status_register;
@ -58,11 +58,16 @@ int fegetenv(fenv_t* env)
#if ARCH(AARCH64)
(void)env;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)env;
TODO_RISCV64();
#elif ARCH(X86_64)
asm volatile("fnstenv %0"
: "=m"(env->__x87_fpu_env)::"memory");
env->__mxcsr = read_mxcsr();
#else
# error Unknown architecture
#endif
return 0;
@ -76,7 +81,10 @@ int fesetenv(fenv_t const* env)
#if ARCH(AARCH64)
(void)env;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)env;
TODO_RISCV64();
#elif ARCH(X86_64)
if (env == FE_DFL_ENV) {
asm volatile("finit");
set_mxcsr(default_mxcsr_value);
@ -87,6 +95,8 @@ int fesetenv(fenv_t const* env)
: "memory");
set_mxcsr(env->__mxcsr);
#else
# error Unknown architecture
#endif
return 0;
@ -102,10 +112,15 @@ int feholdexcept(fenv_t* env)
#if ARCH(AARCH64)
(void)env;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)env;
TODO_RISCV64();
#elif ARCH(X86_64)
current_env.__x87_fpu_env.__status_word &= ~FE_ALL_EXCEPT;
current_env.__x87_fpu_env.__status_word &= ~(1 << 7); // Clear the "Exception Status Summary" bit
current_env.__x87_fpu_env.__control_word &= FE_ALL_EXCEPT; // Masking these bits stops the corresponding exceptions from being generated according to the Intel Programmer's Manual
#else
# error Unknown architecture
#endif
fesetenv(&current_env);
@ -143,9 +158,15 @@ int fesetexceptflag(fexcept_t const* except, int exceptions)
(void)exceptions;
(void)except;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)exceptions;
(void)except;
TODO_RISCV64();
#elif ARCH(X86_64)
current_env.__x87_fpu_env.__status_word &= exceptions;
current_env.__x87_fpu_env.__status_word &= ~(1 << 7); // Make sure exceptions don't get raised
#else
# error Unknown architecture
#endif
fesetenv(&current_env);
@ -156,9 +177,13 @@ int fegetround()
{
#if ARCH(AARCH64)
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
TODO_RISCV64();
#elif ARCH(X86_64)
// There's no way to signal whether the SSE rounding mode and x87 ones are different, so we assume they're the same
return (read_status_register() >> 10) & 3;
#else
# error Unknown architecture
#endif
}
@ -169,7 +194,9 @@ int fesetround(int rounding_mode)
#if ARCH(AARCH64)
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
TODO_RISCV64();
#elif ARCH(X86_64)
auto control_word = read_control_word();
control_word &= ~(3 << 10);
@ -183,7 +210,8 @@ int fesetround(int rounding_mode)
mxcsr |= rounding_mode << 13;
set_mxcsr(mxcsr);
#else
# error Unknown architecture
#endif
return 0;
@ -199,9 +227,14 @@ int feclearexcept(int exceptions)
#if ARCH(AARCH64)
(void)exceptions;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)exceptions;
TODO_RISCV64();
#elif ARCH(X86_64)
current_env.__x87_fpu_env.__status_word &= ~exceptions;
current_env.__x87_fpu_env.__status_word &= ~(1 << 7); // Clear the "Exception Status Summary" bit
#else
# error Unknown architecture
#endif
fesetenv(&current_env);
@ -213,11 +246,16 @@ int fetestexcept(int exceptions)
#if ARCH(AARCH64)
(void)exceptions;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)exceptions;
TODO_RISCV64();
#elif ARCH(X86_64)
u16 status_register = read_status_register() & FE_ALL_EXCEPT;
exceptions &= FE_ALL_EXCEPT;
return status_register & exceptions;
#else
# error Unknown architecture
#endif
}
@ -231,7 +269,10 @@ int feraiseexcept(int exceptions)
#if ARCH(AARCH64)
(void)exceptions;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)exceptions;
TODO_RISCV64();
#elif ARCH(X86_64)
// While the order in which the exceptions is raised is unspecified, FE_OVERFLOW and FE_UNDERFLOW must be raised before FE_INEXACT, so handle that case in this branch
if (exceptions & FE_INEXACT) {
env.__x87_fpu_env.__status_word &= ((u16)exceptions & ~FE_INEXACT);
@ -249,6 +290,8 @@ int feraiseexcept(int exceptions)
env.__x87_fpu_env.__status_word &= exceptions;
fesetenv(&env);
asm volatile("fwait");
#else
# error Unknown architecture
#endif
return 0;

View file

@ -9,7 +9,7 @@
#include <AK/BuiltinWrappers.h>
#include <AK/FloatingPoint.h>
#if !ARCH(AARCH64)
#if ARCH(X86_64)
# include <AK/FPControl.h>
#endif
#include <AK/Math.h>
@ -363,7 +363,7 @@ MAKE_AK_BACKED2(remainder);
long double truncl(long double x) NOEXCEPT
{
#if !ARCH(AARCH64)
#if ARCH(X86_64)
if (fabsl(x) < LONG_LONG_MAX) {
// This is 1.6 times faster than the implementation using the "internal_to_integer"
// helper (on x86_64)
@ -383,7 +383,7 @@ long double truncl(long double x) NOEXCEPT
double trunc(double x) NOEXCEPT
{
#if !ARCH(AARCH64)
#if ARCH(X86_64)
if (fabs(x) < LONG_LONG_MAX) {
u64 temp;
asm(
@ -400,7 +400,7 @@ double trunc(double x) NOEXCEPT
float truncf(float x) NOEXCEPT
{
#if !ARCH(AARCH64)
#if ARCH(X86_64)
if (fabsf(x) < LONG_LONG_MAX) {
u64 temp;
asm(
@ -420,13 +420,18 @@ long double rintl(long double value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
long double res;
asm(
"frndint\n"
: "=t"(res)
: "0"(value));
return res;
#else
# error "Unknown architecture"
#endif
}
double rint(double value)
@ -434,13 +439,18 @@ double rint(double value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
double res;
asm(
"frndint\n"
: "=t"(res)
: "0"(value));
return res;
#else
# error "Unknown architecture"
#endif
}
float rintf(float value)
@ -448,13 +458,18 @@ float rintf(float value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
float res;
asm(
"frndint\n"
: "=t"(res)
: "0"(value));
return res;
#else
# error "Unknown architecture"
#endif
}
@ -463,7 +478,10 @@ long lrintl(long double value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
long res;
asm(
"fistpl %0\n"
@ -471,6 +489,8 @@ long lrintl(long double value)
: "t"(value)
: "st");
return res;
#else
# error "Unknown architecture"
#endif
}
long lrint(double value)
@ -478,7 +498,10 @@ long lrint(double value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
long res;
asm(
"fistpl %0\n"
@ -486,6 +509,8 @@ long lrint(double value)
: "t"(value)
: "st");
return res;
#else
# error "Unknown architecture"
#endif
}
long lrintf(float value)
@ -493,7 +518,10 @@ long lrintf(float value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
long res;
asm(
"fistpl %0\n"
@ -501,6 +529,8 @@ long lrintf(float value)
: "t"(value)
: "st");
return res;
#else
# error "Unknown architecture"
#endif
}
@ -509,7 +539,10 @@ long long llrintl(long double value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
long long res;
asm(
"fistpq %0\n"
@ -517,6 +550,8 @@ long long llrintl(long double value)
: "t"(value)
: "st");
return res;
#else
# error "Unknown architecture"
#endif
}
long long llrint(double value)
@ -524,7 +559,10 @@ long long llrint(double value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
long long res;
asm(
"fistpq %0\n"
@ -532,6 +570,8 @@ long long llrint(double value)
: "t"(value)
: "st");
return res;
#else
# error "Unknown architecture"
#endif
}
long long llrintf(float value)
@ -539,7 +579,10 @@ long long llrintf(float value)
#if ARCH(AARCH64)
(void)value;
TODO_AARCH64();
#else
#elif ARCH(RISCV64)
(void)value;
TODO_RISCV64();
#elif ARCH(X86_64)
long long res;
asm(
"fistpq %0\n"
@ -547,6 +590,8 @@ long long llrintf(float value)
: "t"(value)
: "st");
return res;
#else
# error "Unknown architecture"
#endif
}

View file

@ -31,8 +31,13 @@ struct __jmp_buf {
#elif defined(__aarch64__)
// FIXME: This is likely incorrect.
uint64_t regs[22];
#elif defined(__riscv) && __riscv_xlen == 64
// FIXME: This is likely incorrect.
uint64_t s[12];
uint64_t sp;
uint64_t pc;
#else
# error
# error "Unknown architecture"
#endif
int did_save_signal_mask;
sigset_t saved_signal_mask;
@ -49,8 +54,10 @@ typedef struct __jmp_buf sigjmp_buf[1];
static_assert(sizeof(struct __jmp_buf) == 72, "struct __jmp_buf unsynchronized with x86_64/setjmp.S");
# elif defined(__aarch64__)
static_assert(sizeof(struct __jmp_buf) == 184, "struct __jmp_buf unsynchronized with aarch64/setjmp.S");
# elif defined(__riscv) && __riscv_xlen == 64
static_assert(sizeof(struct __jmp_buf) == 120, "struct __jmp_buf unsynchronized with riscv64/setjmp.S");
# else
# error
# error "Unknown architecture"
# endif
#endif

View file

@ -14,4 +14,6 @@
# include "aarch64/regs.h"
#elif ARCH(RISCV64)
# include "riscv64/regs.h"
#else
# error Unknown architecture
#endif