From 7c976a83e0679d06ec7eb9be47158b74e6a5fed5 Mon Sep 17 00:00:00 2001 From: Sebastian Sumpf Date: Wed, 1 Dec 2021 10:03:26 +0100 Subject: [PATCH] libc: riscv support libc-gen, libc-setjmp, task startup issue #4312 --- repos/libports/lib/import/import-libc.mk | 4 +++ repos/libports/lib/mk/spec/riscv/libc-gen.mk | 23 ++++++++++++ .../libports/lib/mk/spec/riscv/libc-setjmp.mk | 9 +++++ repos/libports/lib/mk/spec/riscv/libc.mk | 8 +++++ repos/libports/ports/libc.hash | 2 +- repos/libports/ports/libc.port | 17 +++++++++ repos/libports/recipes/src/libc/content.mk | 2 +- repos/libports/src/lib/libc/fenv-softfloat.cc | 18 ++++++++++ .../src/lib/libc/patches/ieeefp_riscv.patch | 11 ++++++ .../lib/libc/spec/riscv/internal/call_func.h | 35 +++++++++++++++++++ 10 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 repos/libports/lib/mk/spec/riscv/libc-gen.mk create mode 100644 repos/libports/lib/mk/spec/riscv/libc-setjmp.mk create mode 100644 repos/libports/lib/mk/spec/riscv/libc.mk create mode 100644 repos/libports/src/lib/libc/fenv-softfloat.cc create mode 100644 repos/libports/src/lib/libc/patches/ieeefp_riscv.patch create mode 100644 repos/libports/src/lib/libc/spec/riscv/internal/call_func.h diff --git a/repos/libports/lib/import/import-libc.mk b/repos/libports/lib/import/import-libc.mk index c53a4d0781..98113f5745 100644 --- a/repos/libports/lib/import/import-libc.mk +++ b/repos/libports/lib/import/import-libc.mk @@ -18,6 +18,10 @@ ifeq ($(filter-out $(SPECS),arm_64),) LIBC_ARCH_INC_DIR := include/spec/arm_64/libc endif # ARM64 +ifeq ($(filter-out $(SPECS),riscv),) + LIBC_ARCH_INC_DIR := include/spec/riscv/libc +endif # RISC-V + # # If we found no valid include path for the configured target platform, # we have to prevent the build system from building the target. This is diff --git a/repos/libports/lib/mk/spec/riscv/libc-gen.mk b/repos/libports/lib/mk/spec/riscv/libc-gen.mk new file mode 100644 index 0000000000..36b37cfc01 --- /dev/null +++ b/repos/libports/lib/mk/spec/riscv/libc-gen.mk @@ -0,0 +1,23 @@ +include $(REP_DIR)/lib/mk/libc-gen.inc + +LIBC_GEN_RISCV_DIR = $(LIBC_DIR)/lib/libc/riscv/gen + +SRC_S += _ctx_start.S sigsetjmp.S +SRC_C += flt_rounds.c fpgetmask.c fpsetmask.c infinity.c makecontext.c + +# +# Fix missing include prefix for 'ucontext.h', should be 'sys/ucontext.h' +# +# The first path is in effect when using the regular build system. The second +# path is in effect when building the libc from a source archive (where the +# ucontext.h header is taken from the libc API archive). +# +CC_OPT_makecontext = -I$(call select_from_ports,libc)/include/libc/sys \ + $(addprefix -I,$(call select_from_repositories,/include/libc/sys)) + +CC_OPT += -DSOFTFLOAT_FOR_GCC + +vpath fpgetmask.c $(LIBC_DIR)/lib/libc/softfloat +vpath fpsetmask.c $(LIBC_DIR)/lib/libc/softfloat +vpath %.c $(LIBC_GEN_RISCV_DIR) +vpath %.S $(LIBC_GEN_RISCV_DIR) diff --git a/repos/libports/lib/mk/spec/riscv/libc-setjmp.mk b/repos/libports/lib/mk/spec/riscv/libc-setjmp.mk new file mode 100644 index 0000000000..86e6d79ba1 --- /dev/null +++ b/repos/libports/lib/mk/spec/riscv/libc-setjmp.mk @@ -0,0 +1,9 @@ +LIBC_GEN_RISCV_DIR = $(LIBC_DIR)/lib/libc/riscv/gen + +SRC_S = _setjmp.S setjmp.S + +include $(REP_DIR)/lib/mk/libc-common.inc + +vpath %.S $(LIBC_GEN_RISCV_DIR) + +CC_CXX_WARN_STRICT = diff --git a/repos/libports/lib/mk/spec/riscv/libc.mk b/repos/libports/lib/mk/spec/riscv/libc.mk new file mode 100644 index 0000000000..e3426cd27f --- /dev/null +++ b/repos/libports/lib/mk/spec/riscv/libc.mk @@ -0,0 +1,8 @@ +include $(REP_DIR)/lib/mk/libc.mk + +SRC_CC += fenv-softfloat.cc + +INC_DIR += $(REP_DIR)/src/lib/libc/spec/riscv +INC_DIR += $(LIBC_DIR)/include/spec/riscv + +CC_CXX_WARN_STRICT = diff --git a/repos/libports/ports/libc.hash b/repos/libports/ports/libc.hash index ef2d1d901e..bceb2ae93e 100644 --- a/repos/libports/ports/libc.hash +++ b/repos/libports/ports/libc.hash @@ -1 +1 @@ -1153300b5549eec9d7d3b171b7263d59b587b1ec +130633868713e5f3e0dac32775bfcb8a1731f6ad diff --git a/repos/libports/ports/libc.port b/repos/libports/ports/libc.port index ba0cd64e30..cc20759725 100644 --- a/repos/libports/ports/libc.port +++ b/repos/libports/ports/libc.port @@ -221,6 +221,23 @@ DIR_CONTENT(include/spec/arm_64/libc/machine) := \ $(addprefix $(D)/sys/arm64/include/, armreg.h) +# +# RISC-V-specific headers +# +DIRS += include/spec/riscv/libc +DIR_CONTENT(include/spec/riscv/libc) := \ + $(call common_include_libc_arch_content,riscv,riscv) \ + $(D)/lib/msun/riscv/fenv.h \ + $(addprefix $(D)/lib/libc/softfloat/, softfloat-for-gcc.h) \ + $(addprefix $(D)/lib/libc/riscv/softfloat/, milieu.h riscv-gcc.h \ + softfloat.h) + +DIRS += include/spec/riscv/libc/machine +DIR_CONTENT(include/spec/riscv/libc/machine) := \ + $(call common_include_libc_arch_machine_content,riscv) \ + $(addprefix $(D)/sys/riscv/include/, riscvreg.h) + + # # Rules for generating files # diff --git a/repos/libports/recipes/src/libc/content.mk b/repos/libports/recipes/src/libc/content.mk index 5dca045c46..af9a9a3413 100644 --- a/repos/libports/recipes/src/libc/content.mk +++ b/repos/libports/recipes/src/libc/content.mk @@ -18,7 +18,7 @@ include/libc-plugin include/libc/sys/ucontext.h: lib/mk: mkdir -p $@ cp $(addprefix $(REP_DIR)/$@/,libc.mk libc-* libm.inc) $@ - for spec in x86_32 x86_64 arm arm_64; do \ + for spec in x86_32 x86_64 arm arm_64 riscv; do \ mkdir -p $@/spec/$$spec; \ cp $(addprefix $(REP_DIR)/$@/spec/$$spec/,libc-* libc.mk libm.mk) $@/spec/$$spec/; done diff --git a/repos/libports/src/lib/libc/fenv-softfloat.cc b/repos/libports/src/lib/libc/fenv-softfloat.cc new file mode 100644 index 0000000000..e825c44d67 --- /dev/null +++ b/repos/libports/src/lib/libc/fenv-softfloat.cc @@ -0,0 +1,18 @@ +/* + * \brief Soft float support + * \author Sebastian Sumpf + * \date 2021-06-29 + */ + +/* + * Copyright (C) 2021 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +extern "C" { + int __softfloat_float_rounding_mode; + int __softfloat_float_exception_mask; + int __softfloat_float_exception_flags; +} diff --git a/repos/libports/src/lib/libc/patches/ieeefp_riscv.patch b/repos/libports/src/lib/libc/patches/ieeefp_riscv.patch new file mode 100644 index 0000000000..40eff9d68a --- /dev/null +++ b/repos/libports/src/lib/libc/patches/ieeefp_riscv.patch @@ -0,0 +1,11 @@ +diff --git a/src/lib/libc/sys/riscv/include/ieeefp.h b/src/lib/libc/sys/riscv/include/ieeefp.h +index ed7381d..44a1c00 100644 +--- src/lib/libc/sys/riscv/include/ieeefp.h ++++ src/lib/libc/sys/riscv/include/ieeefp.h +@@ -4,5 +4,6 @@ + #define _MACHINE_IEEEFP_H_ + + /* TODO */ ++typedef int fp_except; + + #endif /* _MACHINE_IEEEFP_H_ */ diff --git a/repos/libports/src/lib/libc/spec/riscv/internal/call_func.h b/repos/libports/src/lib/libc/spec/riscv/internal/call_func.h new file mode 100644 index 0000000000..7a99f575dd --- /dev/null +++ b/repos/libports/src/lib/libc/spec/riscv/internal/call_func.h @@ -0,0 +1,35 @@ +/* + * \brief User-level task helpers (riscv) + * \author Sebastian Sumpf + * \date 2021-06-29 + */ + +/* + * Copyright (C) 2021 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#ifndef _INCLUDE__SPEC__RISCV__INTERNAL__CALL_FUNC_H_ +#define _INCLUDE__SPEC__RISCV__INTERNAL__CALL_FUNC_H_ + +/* Libc includes */ +#include /* _setjmp() as we don't care about signal state */ + + +/** + * Call function with a new stack + */ +[[noreturn]] inline void call_func(void *sp, void *func, void *arg) +{ + asm volatile ("mv a0, %2\n" /* set arg */ + "mv sp, %0\n" /* set stack */ + "mv fp, x0\n" /* clear frame pointer */ + "jalr %1\n" /* call func */ + "" + : : "r"(sp), "r"(func), "r"(arg) : "a0"); + __builtin_unreachable(); +} + +#endif /* _INCLUDE__SPEC__RISCV__INTERNAL__CALL_FUNC_H_ */