This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 75d87c6547574246c53fdc7ee456d5843fcf2ebd Author: Parthiban Nallathambi <[email protected]> AuthorDate: Sat Oct 31 18:32:15 2026 +0530 arch/rh850: add the Renesas RH850 architecture. Add the architecture code for the Renesas RH850 family with the G4KH core, built with v850-elf GCC through Make or CMake. Interrupts nest by priority, and a context switch happens only at the outermost exit. Co-authored-by: Saravanan Sekar <[email protected]> Signed-off-by: Parthiban Nallathambi <[email protected]> --- arch/Kconfig | 15 ++ arch/rh850/CMakeLists.txt | 23 +++ arch/rh850/Kconfig | 18 ++ arch/rh850/include/.gitignore | 2 + arch/rh850/include/arch.h | 36 ++++ arch/rh850/include/inttypes.h | 122 ++++++++++++ arch/rh850/include/irq.h | 193 ++++++++++++++++++ arch/rh850/include/limits.h | 84 ++++++++ arch/rh850/include/syscall.h | 171 ++++++++++++++++ arch/rh850/include/types.h | 97 +++++++++ arch/rh850/src/.gitignore | 2 + arch/rh850/src/CMakeLists.txt | 26 +++ arch/rh850/src/Makefile | 253 ++++++++++++++++++++++++ arch/rh850/src/cmake/Toolchain.cmake | 128 ++++++++++++ arch/rh850/src/cmake/platform.cmake | 60 ++++++ arch/rh850/src/common/CMakeLists.txt | 52 +++++ arch/rh850/src/common/Make.defs | 56 ++++++ arch/rh850/src/common/Toolchain.defs | 159 +++++++++++++++ arch/rh850/src/common/rh850_allocateheap.c | 52 +++++ arch/rh850/src/common/rh850_checkstack.c | 100 ++++++++++ arch/rh850/src/common/rh850_createstack.c | 144 ++++++++++++++ arch/rh850/src/common/rh850_doirq.c | 94 +++++++++ arch/rh850/src/common/rh850_exception.c | 155 +++++++++++++++ arch/rh850/src/common/rh850_exception_common.S | 119 +++++++++++ arch/rh850/src/common/rh850_exit.c | 55 ++++++ arch/rh850/src/common/rh850_fpucmp.c | 56 ++++++ arch/rh850/src/common/rh850_getintstack.c | 46 +++++ arch/rh850/src/common/rh850_head.S | 178 +++++++++++++++++ arch/rh850/src/common/rh850_idle.c | 47 +++++ arch/rh850/src/common/rh850_initialize.c | 46 +++++ arch/rh850/src/common/rh850_initialstate.c | 93 +++++++++ arch/rh850/src/common/rh850_internal.h | 118 +++++++++++ arch/rh850/src/common/rh850_irq.c | 51 +++++ arch/rh850/src/common/rh850_macros.S | 122 ++++++++++++ arch/rh850/src/common/rh850_nputs.c | 47 +++++ arch/rh850/src/common/rh850_registerdump.c | 78 ++++++++ arch/rh850/src/common/rh850_releasestack.c | 62 ++++++ arch/rh850/src/common/rh850_saveusercontext.S | 64 ++++++ arch/rh850/src/common/rh850_schedulesigaction.c | 65 ++++++ arch/rh850/src/common/rh850_sigdeliver.c | 88 +++++++++ arch/rh850/src/common/rh850_stackframe.c | 69 +++++++ arch/rh850/src/common/rh850_start.c | 73 +++++++ arch/rh850/src/common/rh850_swint.c | 97 +++++++++ arch/rh850/src/common/rh850_switchcontext.c | 53 +++++ arch/rh850/src/common/rh850_sysreg.h | 109 ++++++++++ arch/rh850/src/common/rh850_usestack.c | 79 ++++++++ libs/libc/machine/rh850/CMakeLists.txt | 21 ++ 47 files changed, 3878 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index f993b438ba5..b23706fcaa4 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -105,6 +105,17 @@ config ARCH_RENESAS ---help--- Renesas architectures (SH and M16C). +config ARCH_RH850 + bool "Renesas RH850" + select ARCH_HAVE_INTERRUPTSTACK + select ARCH_HAVE_CUSTOMOPT + select ARCH_HAVE_IRQPRIO + select ARCH_HAVE_STACKCHECK + select ARCH_TOOLCHAIN_GNU + select LIBC_ATOMIC_IRQ + ---help--- + Renesas RH850 architecture (G4KH core). + config ARCH_RISCV bool "RISC-V" select ARCH_HAVE_FORK if BUILD_KERNEL && ARCH_ADDRENV && !ARCH_STACK_DYNAMIC @@ -247,6 +258,7 @@ config ARCH default "mips" if ARCH_MIPS default "misoc" if ARCH_MISOC default "renesas" if ARCH_RENESAS + default "rh850" if ARCH_RH850 default "risc-v" if ARCH_RISCV default "sim" if ARCH_SIM default "x86" if ARCH_X86 @@ -279,6 +291,9 @@ endif if ARCH_RENESAS source "arch/renesas/Kconfig" endif +if ARCH_RH850 +source "arch/rh850/Kconfig" +endif if ARCH_RISCV source "arch/risc-v/Kconfig" endif diff --git a/arch/rh850/CMakeLists.txt b/arch/rh850/CMakeLists.txt new file mode 100644 index 00000000000..7af2a036705 --- /dev/null +++ b/arch/rh850/CMakeLists.txt @@ -0,0 +1,23 @@ +# ############################################################################## +# arch/rh850/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +nuttx_add_subdirectory() diff --git a/arch/rh850/Kconfig b/arch/rh850/Kconfig new file mode 100644 index 00000000000..bdb201f78d1 --- /dev/null +++ b/arch/rh850/Kconfig @@ -0,0 +1,18 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +if ARCH_RH850 +comment "RH850 Options" + +config ARCH_RH850_G4KH + bool + select ARCH_HAVE_FPU + ---help--- + RH850 G4KH CPU core with the single and double precision FPU. + +config ARCH_CHIP + string + +endif # ARCH_RH850 diff --git a/arch/rh850/include/.gitignore b/arch/rh850/include/.gitignore new file mode 100644 index 00000000000..af8911db061 --- /dev/null +++ b/arch/rh850/include/.gitignore @@ -0,0 +1,2 @@ +/board +/chip diff --git a/arch/rh850/include/arch.h b/arch/rh850/include/arch.h new file mode 100644 index 00000000000..9bc57bf6194 --- /dev/null +++ b/arch/rh850/include/arch.h @@ -0,0 +1,36 @@ +/**************************************************************************** + * arch/rh850/include/arch.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/* This file should never be included directly but, rather, + * only indirectly through nuttx/arch.h + */ + +#ifndef __ARCH_RH850_INCLUDE_ARCH_H +#define __ARCH_RH850_INCLUDE_ARCH_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#endif /* __ARCH_RH850_INCLUDE_ARCH_H */ diff --git a/arch/rh850/include/inttypes.h b/arch/rh850/include/inttypes.h new file mode 100644 index 00000000000..4b0b2c4261b --- /dev/null +++ b/arch/rh850/include/inttypes.h @@ -0,0 +1,122 @@ +/**************************************************************************** + * arch/rh850/include/inttypes.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RH850_INCLUDE_INTTYPES_H +#define __ARCH_RH850_INCLUDE_INTTYPES_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define INT32_C(x) x ## l +#define INT64_C(x) x ## ll +#define UINT32_C(x) x ## ul +#define UINT64_C(x) x ## ull + +#define PRId8 "d" +#define PRId16 "d" +#define PRId32 "ld" +#define PRId64 "lld" + +#define PRIdPTR "d" + +#define PRIi8 "i" +#define PRIi16 "i" +#define PRIi32 "li" +#define PRIi64 "lli" + +#define PRIiPTR "i" + +#define PRIo8 "o" +#define PRIo16 "o" +#define PRIo32 "lo" +#define PRIo64 "llo" + +#define PRIoPTR "o" + +#define PRIu8 "u" +#define PRIu16 "u" +#define PRIu32 "lu" +#define PRIu64 "llu" + +#define PRIuPTR "u" + +#define PRIx8 "x" +#define PRIx16 "x" +#define PRIx32 "lx" +#define PRIx64 "llx" + +#define PRIxPTR "x" + +#define PRIX8 "X" +#define PRIX16 "X" +#define PRIX32 "lX" +#define PRIX64 "llX" + +#define PRIXPTR "X" + +#define SCNd8 "hhd" +#define SCNd16 "hd" +#define SCNd32 "ld" +#define SCNd64 "lld" + +#define SCNdPTR "d" + +#define SCNi8 "hhi" +#define SCNi16 "hi" +#define SCNi32 "li" +#define SCNi64 "lli" + +#define SCNiPTR "i" + +#define SCNo8 "hho" +#define SCNo16 "ho" +#define SCNo32 "lo" +#define SCNo64 "llo" + +#define SCNoPTR "o" + +#define SCNu8 "hhu" +#define SCNu16 "hu" +#define SCNu32 "lu" +#define SCNu64 "llu" + +#define SCNuPTR "u" + +#define SCNx8 "hhx" +#define SCNx16 "hx" +#define SCNx32 "lx" +#define SCNx64 "llx" + +#define SCNxPTR "x" + +#define INT8_C(x) x +#define INT16_C(x) x + +#define UINT8_C(x) x +#define UINT16_C(x) x + +#endif /* __ARCH_RH850_INCLUDE_INTTYPES_H */ diff --git a/arch/rh850/include/irq.h b/arch/rh850/include/irq.h new file mode 100644 index 00000000000..791e98eef05 --- /dev/null +++ b/arch/rh850/include/irq.h @@ -0,0 +1,193 @@ +/**************************************************************************** + * arch/rh850/include/irq.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/* This file should never be included directly but, rather, only indirectly + * through nuttx/irq.h + */ + +#ifndef __ARCH_RH850_INCLUDE_IRQ_H +#define __ARCH_RH850_INCLUDE_IRQ_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sys/types.h> +#ifndef __ASSEMBLY__ +# include <stdbool.h> +#endif + +#include <nuttx/irq.h> +#include <arch/chip/irq.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Register save area: the frame stored by interrupt and exception entry. + * REG_SP is the stack pointer of the saved context (before the frame push). + */ + +#define REG_R1 0 +#define REG_R2 1 +#define REG_R4 2 +#define REG_R5 3 +#define REG_R6 4 +#define REG_R7 5 +#define REG_R8 6 +#define REG_R9 7 +#define REG_R10 8 +#define REG_R11 9 +#define REG_R12 10 +#define REG_R13 11 +#define REG_R14 12 +#define REG_R15 13 +#define REG_R16 14 +#define REG_R17 15 +#define REG_R18 16 +#define REG_R19 17 +#define REG_R20 18 +#define REG_R21 19 +#define REG_R22 20 +#define REG_R23 21 +#define REG_R24 22 +#define REG_R25 23 +#define REG_R26 24 +#define REG_R27 25 +#define REG_R28 26 +#define REG_R29 27 +#define REG_R30 28 +#define REG_R31 29 +#define REG_EIPC 30 +#define REG_EIPSW 31 +#define REG_FPSR 32 +#define REG_FPEPC 33 +#define REG_SP 34 + +#define XCPTCONTEXT_REGS 35 +#define XCPTCONTEXT_SIZE (4 * XCPTCONTEXT_REGS) + +#define REG_LP REG_R31 +#define REG_EP REG_R30 + +/* The stack pointer is kept 4-byte aligned (GCC __BIGGEST_ALIGNMENT__ 4) */ + +#define STACKFRAME_ALIGN 4 + +#define PSW_ID (1 << 5) /* EI level interrupts disabled */ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +struct xcptcontext +{ +#ifdef CONFIG_ENABLE_ALL_SIGNALS + /* Saved copy of the context used during signal processing */ + + uintptr_t *saved_regs; +#endif + + /* Register save area of XCPTCONTEXT_SIZE, valid while the task is not + * running or is interrupted. + */ + + uintptr_t *regs; +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern volatile bool g_interrupt_context; + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +static inline_function uintptr_t up_getsp(void) +{ + uintptr_t sp; + + __asm__ __volatile__("mov sp, %0" : "=r"(sp)); + return sp; +} + +/* Save the current interrupt state (PSW) and disable EI level interrupts */ + +noinstrument_function static inline_function irqstate_t up_irq_save(void) +{ + irqstate_t flags; + + __asm__ __volatile__("stsr 5, %0, 0\n\tdi" : "=r"(flags) : : "memory"); + return flags; +} + +noinstrument_function static inline_function +void up_irq_restore(irqstate_t flags) +{ + if ((flags & PSW_ID) == 0) + { + __asm__ __volatile__("ei" : : : "memory"); + } +} + +noinstrument_function +static inline_function void up_set_interrupt_context(bool flag) +{ + g_interrupt_context = flag; +} + +noinstrument_function static inline_function bool up_interrupt_context(void) +{ + return g_interrupt_context; +} + +static inline_function uintptr_t up_getusrsp(void *regs) +{ + return ((uintptr_t *)regs)[REG_SP]; +} + +static inline_function uintptr_t up_getusrpc(void *regs) +{ + return ((uintptr_t *)regs)[REG_EIPC]; +} + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +void up_irq_enable(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RH850_INCLUDE_IRQ_H */ diff --git a/arch/rh850/include/limits.h b/arch/rh850/include/limits.h new file mode 100644 index 00000000000..9cf153279f9 --- /dev/null +++ b/arch/rh850/include/limits.h @@ -0,0 +1,84 @@ +/**************************************************************************** + * arch/rh850/include/limits.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RH850_INCLUDE_LIMITS_H +#define __ARCH_RH850_INCLUDE_LIMITS_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define CHAR_BIT 8 +#define SCHAR_MIN (-SCHAR_MAX - 1) +#define SCHAR_MAX 127 +#define UCHAR_MAX 255 + +/* These could be different on machines where char is unsigned */ + +#ifdef __CHAR_UNSIGNED__ +# define CHAR_MIN 0 +# define CHAR_MAX UCHAR_MAX +#else +# define CHAR_MIN SCHAR_MIN +# define CHAR_MAX SCHAR_MAX +#endif + +#define SHRT_MIN (-SHRT_MAX - 1) +#define SHRT_MAX 32767 +#define USHRT_MAX 65535U + +#define INT_MIN (-INT_MAX - 1) +#define INT_MAX 2147483647 +#define UINT_MAX 4294967295U + +/* These change on 32-bit and 64-bit platforms */ + +# define LONG_MIN (-LONG_MAX - 1) +# define LONG_MAX 2147483647L +# define ULONG_MAX 4294967295UL + +# define LLONG_MIN (-LLONG_MAX - 1) +# define LLONG_MAX 9223372036854775807LL +# define ULLONG_MAX 18446744073709551615ULL + +/* A pointer is 4 bytes */ + +# define PTR_MIN (-PTR_MAX - 1) +# define PTR_MAX 2147483647 +# define UPTR_MAX 4294967295U + +#if !defined(__WCHAR_TYPE__) +# define WCHAR_MIN INT_MIN +# define WCHAR_MAX INT_MAX +#elif defined(__WCHAR_UNSIGNED__) +# define WCHAR_MIN 0 +# define WCHAR_MAX __WCHAR_MAX__ +#else +# define WCHAR_MIN (-__WCHAR_MAX__ - 1) +# define WCHAR_MAX __WCHAR_MAX__ +#endif + +#endif /* __ARCH_RH850_INCLUDE_LIMITS_H */ diff --git a/arch/rh850/include/syscall.h b/arch/rh850/include/syscall.h new file mode 100644 index 00000000000..fe9eefbfdd0 --- /dev/null +++ b/arch/rh850/include/syscall.h @@ -0,0 +1,171 @@ +/**************************************************************************** + * arch/rh850/include/syscall.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/* This file should never be included directly but, rather, only indirectly + * through include/syscall.h or include/sys/sycall.h + */ + +#ifndef __ARCH_RH850_INCLUDE_SYSCALL_H +#define __ARCH_RH850_INCLUDE_SYSCALL_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#ifndef __ASSEMBLY__ +# include <stdint.h> +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Reserved system call numbers, passed in r6 to TRAP 0 */ + +#define SYS_restore_context (1) +#define SYS_switch_context (2) + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* TRAP 0x0: r6 = call number, r7-r9 then r10-r12 = arguments, r10 = + * return value. + */ + +static inline uintptr_t sys_call0(unsigned int nbr) +{ + register uintptr_t reg0 __asm__("r6") = (uintptr_t)nbr; + register uintptr_t ret __asm__("r10"); + + __asm__ __volatile__("trap 0x0" : "=r"(ret) : "r"(reg0) : "memory"); + return ret; +} + +static inline uintptr_t sys_call1(unsigned int nbr, uintptr_t parm1) +{ + register uintptr_t reg0 __asm__("r6") = (uintptr_t)nbr; + register uintptr_t reg1 __asm__("r7") = parm1; + register uintptr_t ret __asm__("r10"); + + __asm__ __volatile__("trap 0x0" : "=r"(ret) : "r"(reg0), "r"(reg1) + : "memory"); + return ret; +} + +static inline uintptr_t sys_call2(unsigned int nbr, uintptr_t parm1, + uintptr_t parm2) +{ + register uintptr_t reg0 __asm__("r6") = (uintptr_t)nbr; + register uintptr_t reg1 __asm__("r7") = parm1; + register uintptr_t reg2 __asm__("r8") = parm2; + register uintptr_t ret __asm__("r10"); + + __asm__ __volatile__("trap 0x0" : "=r"(ret) + : "r"(reg0), "r"(reg1), "r"(reg2) : "memory"); + return ret; +} + +static inline uintptr_t sys_call3(unsigned int nbr, uintptr_t parm1, + uintptr_t parm2, uintptr_t parm3) +{ + register uintptr_t reg0 __asm__("r6") = (uintptr_t)nbr; + register uintptr_t reg1 __asm__("r7") = parm1; + register uintptr_t reg2 __asm__("r8") = parm2; + register uintptr_t reg3 __asm__("r9") = parm3; + register uintptr_t ret __asm__("r10"); + + __asm__ __volatile__("trap 0x0" : "=r"(ret) + : "r"(reg0), "r"(reg1), "r"(reg2), "r"(reg3) + : "memory"); + return ret; +} + +static inline uintptr_t sys_call4(unsigned int nbr, uintptr_t parm1, + uintptr_t parm2, uintptr_t parm3, + uintptr_t parm4) +{ + register uintptr_t reg0 __asm__("r6") = (uintptr_t)nbr; + register uintptr_t reg1 __asm__("r7") = parm1; + register uintptr_t reg2 __asm__("r8") = parm2; + register uintptr_t reg3 __asm__("r9") = parm3; + register uintptr_t reg4 __asm__("r10") = parm4; + + __asm__ __volatile__("trap 0x0" : "+r"(reg4) + : "r"(reg0), "r"(reg1), "r"(reg2), "r"(reg3) + : "memory"); + return reg4; +} + +static inline uintptr_t sys_call5(unsigned int nbr, uintptr_t parm1, + uintptr_t parm2, uintptr_t parm3, + uintptr_t parm4, uintptr_t parm5) +{ + register uintptr_t reg0 __asm__("r6") = (uintptr_t)nbr; + register uintptr_t reg1 __asm__("r7") = parm1; + register uintptr_t reg2 __asm__("r8") = parm2; + register uintptr_t reg3 __asm__("r9") = parm3; + register uintptr_t reg4 __asm__("r10") = parm4; + register uintptr_t reg5 __asm__("r11") = parm5; + + __asm__ __volatile__("trap 0x0" : "+r"(reg4) + : "r"(reg0), "r"(reg1), "r"(reg2), "r"(reg3), + "r"(reg5) + : "memory"); + return reg4; +} + +static inline uintptr_t sys_call6(unsigned int nbr, uintptr_t parm1, + uintptr_t parm2, uintptr_t parm3, + uintptr_t parm4, uintptr_t parm5, + uintptr_t parm6) +{ + register uintptr_t reg0 __asm__("r6") = (uintptr_t)nbr; + register uintptr_t reg1 __asm__("r7") = parm1; + register uintptr_t reg2 __asm__("r8") = parm2; + register uintptr_t reg3 __asm__("r9") = parm3; + register uintptr_t reg4 __asm__("r10") = parm4; + register uintptr_t reg5 __asm__("r11") = parm5; + register uintptr_t reg6 __asm__("r12") = parm6; + + __asm__ __volatile__("trap 0x0" : "+r"(reg4) + : "r"(reg0), "r"(reg1), "r"(reg2), "r"(reg3), + "r"(reg5), "r"(reg6) + : "memory"); + return reg4; +} + +#ifdef __cplusplus +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RH850_INCLUDE_SYSCALL_H */ diff --git a/arch/rh850/include/types.h b/arch/rh850/include/types.h new file mode 100644 index 00000000000..c438f21d7f3 --- /dev/null +++ b/arch/rh850/include/types.h @@ -0,0 +1,97 @@ +/**************************************************************************** + * arch/rh850/include/types.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/* This file should never be included directly but, rather, only indirectly + * through sys/types.h + */ + +#ifndef __ARCH_RH850_INCLUDE_TYPES_H +#define __ARCH_RH850_INCLUDE_TYPES_H + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +/* These are the sizes of the standard integer types. NOTE that these type + * names have a leading underscore character. This file will be included + * (indirectly) by include/stdint.h and typedef'ed to the final name without + * the underscore character. This roundabout way of doings things allows + * the stdint.h to be removed from the include/ directory in the event that + * the user prefers to use the definitions provided by their toolchain header + * files. + */ + +typedef signed char _int8_t; +typedef unsigned char _uint8_t; + +typedef signed short _int16_t; +typedef unsigned short _uint16_t; + +typedef signed long _int32_t; +typedef unsigned long _uint32_t; + +typedef signed long long _int64_t; +typedef unsigned long long _uint64_t; +#define __INT64_DEFINED + +typedef _int64_t _intmax_t; +typedef _uint64_t _uintmax_t; + +#if defined(__WCHAR_TYPE__) +typedef __WCHAR_TYPE__ _wchar_t; +#else +typedef long _wchar_t; +#endif + +#if defined(__WINT_TYPE__) +typedef __WINT_TYPE__ _wint_t; +#else +typedef unsigned int _wint_t; +#endif + +typedef int _wctype_t; + +/* A size is 4 bytes */ + +#if defined(__SIZE_TYPE__) +/* If __SIZE_TYPE__ is defined we define ssize_t based on size_t. + * We simply change "unsigned" to "signed" for this single definition + * to make sure ssize_t and size_t only differ by their signedness. + */ + +#define unsigned signed +typedef __SIZE_TYPE__ _ssize_t; +#undef unsigned +typedef __SIZE_TYPE__ _size_t; +#else +typedef signed int _ssize_t; +typedef unsigned int _size_t; +#endif + +/* This is the size of the interrupt state save returned by up_irq_save() */ + +typedef unsigned int irqstate_t; + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RH850_INCLUDE_TYPES_H */ diff --git a/arch/rh850/src/.gitignore b/arch/rh850/src/.gitignore new file mode 100644 index 00000000000..af8911db061 --- /dev/null +++ b/arch/rh850/src/.gitignore @@ -0,0 +1,2 @@ +/board +/chip diff --git a/arch/rh850/src/CMakeLists.txt b/arch/rh850/src/CMakeLists.txt new file mode 100644 index 00000000000..fc28a7bd7b6 --- /dev/null +++ b/arch/rh850/src/CMakeLists.txt @@ -0,0 +1,26 @@ +# ############################################################################## +# arch/rh850/src/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +add_subdirectory(${NUTTX_CHIP_ABS_DIR} exclude_chip EXCLUDE_FROM_ALL) +add_subdirectory(common) + +target_include_directories(arch BEFORE PUBLIC ${NUTTX_CHIP_ABS_DIR} common) diff --git a/arch/rh850/src/Makefile b/arch/rh850/src/Makefile new file mode 100644 index 00000000000..6563adcfd39 --- /dev/null +++ b/arch/rh850/src/Makefile @@ -0,0 +1,253 @@ +############################################################################ +# arch/rh850/src/Makefile +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(TOPDIR)/tools/Make.defs +include chip/Make.defs + +ARCH_SRCDIR = $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src + +INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)chip +INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)common +INCLUDES += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)sched + +CPPFLAGS += $(INCLUDES) +CFLAGS += $(INCLUDES) +CXXFLAGS += $(INCLUDES) +AFLAGS += $(INCLUDES) + +NUTTX = $(call CONVERT_PATH,$(TOPDIR)$(DELIM)nuttx$(EXEEXT)) + +# Additional rules for system call wrapper + +ifeq ($(CONFIG_SCHED_INSTRUMENTATION_SYSCALL),y) + EXTRALINKCMDS += @$(TOPDIR)/syscall/syscall_wraps.ldcmd +endif + +# The "head" object + +HEAD_OBJ = $(HEAD_ASRC:.S=$(OBJEXT)) +STARTUP_OBJS ?= $(HEAD_OBJ) + +# Flat build or kernel-mode objects + +ASRCS = $(CHIP_ASRCS) $(CMN_ASRCS) +AOBJS = $(ASRCS:.S=$(OBJEXT)) + +CSRCS = $(CHIP_CSRCS) $(CMN_CSRCS) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +# User-mode objects + +UASRCS = $(CHIP_UASRCS) $(CMN_UASRCS) +UAOBJS = $(UASRCS:.S=$(OBJEXT)) + +UCSRCS = $(CHIP_UCSRCS) $(CMN_UCSRCS) +UCOBJS = $(UCSRCS:.c=$(OBJEXT)) + +USRCS = $(UASRCS) $(UCSRCS) +UOBJS = $(UAOBJS) $(UCOBJS) + +KBIN = libkarch$(LIBEXT) +BIN = libarch$(LIBEXT) + +$(foreach lib,$(notdir $(wildcard $(APPDIR)$(DELIM)staging$(DELIM)*$(LIBEXT))), \ + $(foreach elib,$(EXTRA_LIBS), \ + $(if $(filter $(notdir $(elib)),$(lib)), \ + $(eval NAMEFULL_LIBS+=$(elib)), \ + $(if $(filter $(notdir $(elib)),$(patsubst lib%$(LIBEXT),-l%,$(lib))), \ + $(eval NAMESPEC_LIBS+=$(elib)) \ + ) \ + ) \ + ) \ + ) + +EXTRA_LIBS := $(filter-out $(NAMEFULL_LIBS) $(NAMESPEC_LIBS),$(EXTRA_LIBS)) +EXTRA_LIBS += $(wildcard $(APPDIR)$(DELIM)staging$(DELIM)*$(LIBEXT)) + +ARCHSCRIPT := $(call CONVERT_PATH,$(ARCHSCRIPT)) +LDFLAGS += $(addprefix -T,$(addsuffix .tmp,$(ARCHSCRIPT))) $(EXTRALINKCMDS) + +ifeq ($(LD),$(CC)) + ifeq ($(CONFIG_STACK_CANARIES),y) + # filter out ssp(Stack Smashing Protector) related flags: + # -fstack-protector + # -fstack-protector-all + # -fstack-protector-strong + # -fstack-protector-explicit + STRIPCFLAGS = $(filter -fstack-protector%,$(CFLAGS)) + endif + LDENTRY ?= -Wl,--entry=__start + LDSTARTGROUP ?= -Wl,--start-group + LDENDGROUP ?= -Wl,--end-group + LDFLAGS := $(addprefix -Xlinker ,$(LDFLAGS)) + LDFLAGS += $(filter-out $(STRIPCFLAGS),$(CFLAGS)) +else + LDENTRY ?= --entry=__start + LDSTARTGROUP ?= --start-group + LDENDGROUP ?= --end-group +endif + +BOARDMAKE = $(if $(wildcard board$(DELIM)Makefile),y,) + +LIBPATHS += -L $(call CONVERT_PATH,$(TOPDIR)$(DELIM)staging) +ifeq ($(BOARDMAKE),y) + LIBPATHS += -L $(call CONVERT_PATH,$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board) +endif + +LDLIBS = $(patsubst %.a,%,$(patsubst lib%,-l%,$(LINKLIBS))) +ifeq ($(BOARDMAKE),y) + LDLIBS += -lboard +endif + +VPATH += chip +VPATH += common +VPATH += $(ARCH_SUBDIR) + +vpath nuttx$(EXEEXT) $(TOPDIR) + +all: $(HEAD_OBJ) $(BIN) + +$(AOBJS) $(UAOBJS) $(HEAD_OBJ): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS) $(UCOBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +ifeq ($(CONFIG_BUILD_FLAT),y) +$(BIN): $(STARTUP_OBJS) $(OBJS) + $(call ARCHIVE_REBUILD, $@, $(OBJS)) +else +$(BIN): $(STARTUP_OBJS) $(UOBJS) + $(call ARCHIVE_REBUILD, $@, $(UOBJS)) +endif + +$(KBIN): $(OBJS) + $(call ARCHIVE_REBUILD, $@, $(OBJS)) + +board/libboard$(LIBEXT): FORCE + $(Q) $(MAKE) -C board libboard$(LIBEXT) EXTRAFLAGS="$(EXTRAFLAGS)" + +.PHONY: FORCE + +FORCE: + +# When multiple linking, these two additional linking objects will be included + +ifeq ($(CONFIG_MM_KASAN_GLOBAL),y) +EXTRA_LIBS += kasan_globals$(OBJEXT) +endif +ifeq ($(CONFIG_ALLSYMS),y) +EXTRA_LIBS += allsyms$(OBJEXT) +endif + +define LINK_ALLSYMS_KASAN + $(if $(CONFIG_ALLSYMS), + $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp --orderbyname $(CONFIG_SYMTAB_ORDEREDBYNAME) + $(Q) $(call COMPILE, allsyms.tmp, allsyms$(OBJEXT), -x c) + $(Q) $(call DELFILE, allsyms.tmp)) + $(if $(CONFIG_MM_KASAN_GLOBAL), + $(Q) $(TOPDIR)/tools/kasan_global.py -e $(NUTTX) -o kasan_globals.tmp -a $(CONFIG_MM_KASAN_GLOBAL_ALIGN) + $(Q) $(call COMPILE, kasan_globals.tmp, kasan_globals$(OBJEXT) -fno-sanitize=kernel-address, -x c) + $(Q) $(call DELFILE, kasan_globals.tmp)) + $(Q) $(LD) $(LDENTRY) $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \ + -o $(NUTTX) $(HEAD_OBJ) $(EXTRA_OBJS) \ + $(LDSTARTGROUP) $(LDLIBS) $(EXTRA_LIBS) $(LDENDGROUP) +endef + +$(addsuffix .tmp,$(ARCHSCRIPT)): $(ARCHSCRIPT) $(TOPDIR)$(DELIM).config + $(call PREPROCESS, $(patsubst %.tmp,%,$@), $@) + +nuttx$(EXEEXT): $(HEAD_OBJ) board/libboard$(LIBEXT) $(addsuffix .tmp,$(ARCHSCRIPT)) $(addprefix $(TOPDIR)$(DELIM)staging$(DELIM),$(LINKLIBS)) + $(Q) echo "LD: nuttx" +ifeq ($(CONFIG_ALLSYMS)$(CONFIG_MM_KASAN_GLOBAL),) + $(Q) $(LD) $(LDENTRY) $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \ + -o $(NUTTX) $(HEAD_OBJ) $(EXTRA_OBJS) \ + $(LDSTARTGROUP) $(LDLIBS) $(EXTRA_LIBS) $(LDENDGROUP) +else + $(Q) $(call LINK_ALLSYMS_KASAN) + $(Q) $(call LINK_ALLSYMS_KASAN) + $(Q) $(call LINK_ALLSYMS_KASAN) + $(Q) $(call LINK_ALLSYMS_KASAN) +endif +ifeq ($(CONFIG_MM_KASAN_GLOBAL),y) + $(Q) $(OBJCOPY) -R .kasan.global $(NUTTX) + $(Q) $(OBJCOPY) -R .kasan.unused $(NUTTX) +endif +ifneq ($(CONFIG_WINDOWS_NATIVE),y) + $(Q) $(NM) $(NUTTX) | \ + grep -v '\(compiled\)\|\(\$(OBJEXT)$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | \ + sort > $(TOPDIR)/System.map +endif + +# This is part of the top-level export target +# Note that there may not be a head object if layout is handled +# by the linker configuration. + +export_startup: $(STARTUP_OBJS) +ifneq ($(STARTUP_OBJS),) + $(Q) if [ -d "$(EXPORT_DIR)/startup" ]; then \ + cp -f $(STARTUP_OBJS) "$(EXPORT_DIR)/startup/."; \ + else \ + echo "$(EXPORT_DIR)/startup does not exist"; \ + exit 1; \ + fi +endif + +# Dependencies + +makedepfile: $(CSRCS:.c=.ddc) $(ASRCS:.S=.dds) $(HEAD_ASRC:.S=.dds) + $(call CATFILE, Make.dep, $^) + $(call DELFILE, $^) + +.depend: Makefile $(SRCS) $(TOPDIR)$(DELIM).config +ifeq ($(BOARDMAKE),y) + $(Q) $(MAKE) -C board depend +endif + $(Q) $(MAKE) makedepfile DEPPATH="$(patsubst %,--dep-path %,$(subst :, ,$(VPATH)))" + $(Q) touch $@ + +depend: .depend + +context:: + +clean: +ifeq ($(BOARDMAKE),y) + $(Q) $(MAKE) -C board clean +endif + $(call DELFILE, $(addsuffix .tmp,$(ARCHSCRIPT))) + $(call DELFILE, $(KBIN)) + $(call DELFILE, $(BIN)) + $(call DELFILE, $(HEAD_OBJ)) + $(call CLEAN) + +distclean:: clean +ifeq ($(BOARDMAKE),y) + $(Q) $(MAKE) -C board distclean +endif + $(call DELFILE, $(CSRCS:.c=.ddc) $(ASRCS:.S=.dds) $(HEAD_ASRC:.S=.dds)) + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep diff --git a/arch/rh850/src/cmake/Toolchain.cmake b/arch/rh850/src/cmake/Toolchain.cmake new file mode 100644 index 00000000000..eba26365624 --- /dev/null +++ b/arch/rh850/src/cmake/Toolchain.cmake @@ -0,0 +1,128 @@ +# ############################################################################## +# arch/rh850/src/cmake/Toolchain.cmake +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +# Toolchain + +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_VERSION 1) + +set(TOOLCHAIN_PREFIX v850-elf) +set(CMAKE_LIBRARY_ARCHITECTURE ${TOOLCHAIN_PREFIX}) +set(CMAKE_C_COMPILER_TARGET ${TOOLCHAIN_PREFIX}) +set(CMAKE_CXX_COMPILER_TARGET ${TOOLCHAIN_PREFIX}) + +set(CMAKE_ASM_COMPILER ${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_C_COMPILER ${CMAKE_ASM_COMPILER}) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) +set(CMAKE_STRIP ${TOOLCHAIN_PREFIX}-strip --strip-unneeded) +set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}-objcopy) +set(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}-objdump) +set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_LD ${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_AR ${TOOLCHAIN_PREFIX}-gcc-ar) +set(CMAKE_NM ${TOOLCHAIN_PREFIX}-gcc-nm) +set(CMAKE_RANLIB ${TOOLCHAIN_PREFIX}-gcc-ranlib) + +# override the ARCHIVE command + +set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> rcs <TARGET> <LINK_FLAGS> <OBJECTS>") +set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> rcs <TARGET> <LINK_FLAGS> <OBJECTS>") +set(CMAKE_ASM_ARCHIVE_CREATE "<CMAKE_AR> rcs <TARGET> <LINK_FLAGS> <OBJECTS>") + +# Core flags, the same at compile and link time so that the matching multilib +# (libgcc) is selected + +set(PLATFORM_FLAGS -mv850e3v5 -mrh850-abi) + +if(NOT CONFIG_ARCH_FPU) + list(APPEND PLATFORM_FLAGS -msoft-float) +endif() + +add_compile_options(${PLATFORM_FLAGS}) +add_link_options(${PLATFORM_FLAGS}) + +# Architecture flags + +add_compile_options(-fno-common) +add_compile_options(-Wall -Wshadow -Wundef) +add_compile_options(-nostdlib) +add_link_options(-nostdlib) + +if(CONFIG_DEBUG_CUSTOMOPT) + add_compile_options(${CONFIG_DEBUG_OPTLEVEL}) +elseif(CONFIG_DEBUG_FULLOPT) + add_compile_options(-Os) +endif() + +if(NOT CONFIG_DEBUG_NOOPT) + add_compile_options(-fno-strict-aliasing) +endif() + +if(CONFIG_FRAME_POINTER) + add_compile_options(-fno-omit-frame-pointer -fno-optimize-sibling-calls) +else() + add_compile_options(-fomit-frame-pointer) +endif() + +if(CONFIG_STACK_CANARIES) + add_compile_options(${CONFIG_STACK_CANARIES_LEVEL}) +endif() + +if(CONFIG_DEBUG_OPT_UNUSED_SECTIONS) + add_link_options(-Wl,--gc-sections) + add_compile_options(-ffunction-sections -fdata-sections) +endif() + +if(CONFIG_DEBUG_LINK_WHOLE_ARCHIVE) + add_link_options(-Wl,--whole-archive) +endif() + +if(CONFIG_DEBUG_LINK_MAP) + add_link_options(-Wl,--cref -Wl,-Map=nuttx.map) +endif() + +if(CONFIG_DEBUG_SYMBOLS) + add_compile_options(${CONFIG_DEBUG_SYMBOLS_LEVEL}) +endif() + +add_link_options(-Wl,--no-warn-rwx-segments) +add_link_options(-Wl,--entry=__start) + +add_compile_options(-Wno-attributes -Wno-unknown-pragmas + $<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>) + +if(CONFIG_CXX_STANDARD) + add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=${CONFIG_CXX_STANDARD}>) +endif() + +if(NOT CONFIG_LIBCXXTOOLCHAIN) + add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>) +endif() + +if(NOT CONFIG_CXX_EXCEPTION) + add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions> + $<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>) +endif() + +if(NOT CONFIG_CXX_RTTI) + add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>) +endif() diff --git a/arch/rh850/src/cmake/platform.cmake b/arch/rh850/src/cmake/platform.cmake new file mode 100644 index 00000000000..aabdd04ed48 --- /dev/null +++ b/arch/rh850/src/cmake/platform.cmake @@ -0,0 +1,60 @@ +# ############################################################################## +# arch/rh850/src/cmake/platform.cmake +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} + COMPILE_OPTIONS) + +set(NUTTX_EXTRA_FLAGS "") +foreach(FLAG ${TOOLCHAIN_DIR_FLAGS}) + if(NOT FLAG MATCHES "^\\$<.*>$") + if(NOT FLAG MATCHES "-nostdlib") + list(APPEND NUTTX_EXTRA_FLAGS ${FLAG}) + endif() + else() + string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG}) + if(matched) + list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1}) + endif() + endif() +endforeach() + +separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS}) + +nuttx_find_toolchain_lib() + +if(CONFIG_LIBM_TOOLCHAIN) + nuttx_find_toolchain_lib(libm.a) +endif() + +if(CONFIG_LIBSUPCXX) + nuttx_find_toolchain_lib(libsupc++.a) +endif() + +if(CONFIG_LIBCXXTOOLCHAIN) + nuttx_find_toolchain_lib(libstdc++.a) +endif() + +if(CONFIG_COVERAGE_TOOLCHAIN) + nuttx_find_toolchain_lib(libgcov.a) +endif() + +set(PREPROCESS ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} -E -P -x c) diff --git a/arch/rh850/src/common/CMakeLists.txt b/arch/rh850/src/common/CMakeLists.txt new file mode 100644 index 00000000000..32d9d8410f1 --- /dev/null +++ b/arch/rh850/src/common/CMakeLists.txt @@ -0,0 +1,52 @@ +# ############################################################################## +# arch/rh850/src/common/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +set(SRCS + rh850_head.S + rh850_exception_common.S + rh850_saveusercontext.S + rh850_allocateheap.c + rh850_checkstack.c + rh850_createstack.c + rh850_doirq.c + rh850_exception.c + rh850_exit.c + rh850_fpucmp.c + rh850_getintstack.c + rh850_idle.c + rh850_initialize.c + rh850_initialstate.c + rh850_irq.c + rh850_nputs.c + rh850_registerdump.c + rh850_releasestack.c + rh850_stackframe.c + rh850_start.c + rh850_swint.c + rh850_switchcontext.c + rh850_usestack.c) + +if(CONFIG_ENABLE_ALL_SIGNALS) + list(APPEND SRCS rh850_schedulesigaction.c rh850_sigdeliver.c) +endif() + +target_sources(arch PRIVATE ${SRCS}) diff --git a/arch/rh850/src/common/Make.defs b/arch/rh850/src/common/Make.defs new file mode 100644 index 00000000000..161965c6ab0 --- /dev/null +++ b/arch/rh850/src/common/Make.defs @@ -0,0 +1,56 @@ +############################################################################ +# arch/rh850/src/common/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +# The start-up, "head", file + +HEAD_ASRC += rh850_head.S + +# Common RH850 files + +CMN_ASRCS += rh850_exception_common.S +CMN_ASRCS += rh850_saveusercontext.S + +CMN_CSRCS += rh850_allocateheap.c +CMN_CSRCS += rh850_checkstack.c +CMN_CSRCS += rh850_createstack.c +CMN_CSRCS += rh850_doirq.c +CMN_CSRCS += rh850_exception.c +CMN_CSRCS += rh850_exit.c +CMN_CSRCS += rh850_fpucmp.c +CMN_CSRCS += rh850_getintstack.c +CMN_CSRCS += rh850_idle.c +CMN_CSRCS += rh850_initialize.c +CMN_CSRCS += rh850_initialstate.c +CMN_CSRCS += rh850_irq.c +CMN_CSRCS += rh850_nputs.c +CMN_CSRCS += rh850_registerdump.c +CMN_CSRCS += rh850_releasestack.c +CMN_CSRCS += rh850_stackframe.c +CMN_CSRCS += rh850_start.c +CMN_CSRCS += rh850_swint.c +CMN_CSRCS += rh850_switchcontext.c +CMN_CSRCS += rh850_usestack.c + +ifeq ($(CONFIG_ENABLE_ALL_SIGNALS),y) +CMN_CSRCS += rh850_schedulesigaction.c +CMN_CSRCS += rh850_sigdeliver.c +endif diff --git a/arch/rh850/src/common/Toolchain.defs b/arch/rh850/src/common/Toolchain.defs new file mode 100644 index 00000000000..90bfec2deee --- /dev/null +++ b/arch/rh850/src/common/Toolchain.defs @@ -0,0 +1,159 @@ +############################################################################ +# arch/rh850/src/common/Toolchain.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +# Supported toolchains +# +# Each toolchain definition should set: +# +# CROSSDEV The GNU toolchain triple (command prefix) +# ARCHCPUFLAGS CPU-specific flags selecting the instruction set +# FPU options, etc. +# ARCHOPTIMIZATION The optimization level that results in +# reliable code generation. +# + +CROSSDEV ?= v850-elf- + +# Core flags, also passed to the link (LD is the compiler driver) so that +# the matching multilib (libgcc) is selected + +ARCHCPUFLAGS += -mv850e3v5 -mrh850-abi + +ifneq ($(CONFIG_ARCH_FPU),y) + ARCHCPUFLAGS += -msoft-float +endif + +ifeq ($(CONFIG_DEBUG_CUSTOMOPT),y) + ARCHOPTIMIZATION += $(CONFIG_DEBUG_OPTLEVEL) +else ifeq ($(CONFIG_DEBUG_FULLOPT),y) + ARCHOPTIMIZATION += -Os +endif + +ifneq ($(CONFIG_DEBUG_NOOPT),y) + ARCHOPTIMIZATION += -fno-strict-aliasing +endif + +ifeq ($(CONFIG_FRAME_POINTER),y) + ARCHOPTIMIZATION += -fno-omit-frame-pointer -fno-optimize-sibling-calls +else + ARCHOPTIMIZATION += -fomit-frame-pointer +endif + +ifeq ($(CONFIG_STACK_CANARIES),y) + ARCHOPTIMIZATION += $(patsubst "%",%,$(CONFIG_STACK_CANARIES_LEVEL)) +endif + +ifeq ($(CONFIG_STACK_USAGE),y) + ARCHOPTIMIZATION += -fstack-usage +endif + +ifneq ($(CONFIG_STACK_USAGE_WARNING),0) + ARCHOPTIMIZATION += -Wstack-usage=$(CONFIG_STACK_USAGE_WARNING) +endif + +# Default toolchain + +CC = $(CROSSDEV)gcc +CXX = $(CROSSDEV)g++ +CPP = $(CROSSDEV)gcc -E -P -x c +LD = $(CROSSDEV)gcc +STRIP = $(CROSSDEV)strip --strip-unneeded +AR = $(CROSSDEV)gcc-ar rcs +UNAR = $(CROSSDEV)gcc-ar x +NM = $(CROSSDEV)gcc-nm +OBJCOPY = $(CROSSDEV)objcopy +OBJDUMP = $(CROSSDEV)objdump + +ARCHOPTIMIZATION += -fno-builtin + +# Architecture flags + +ARCHCFLAGS += -Wstrict-prototypes -Wno-attributes -Wno-unknown-pragmas +ARCHCXXFLAGS += -Wno-attributes -Wno-unknown-pragmas + +ifneq ($(CONFIG_LIBCXXTOOLCHAIN),y) + ARCHCXXFLAGS += -nostdinc++ +endif + +ifneq ($(CONFIG_CXX_STANDARD),) + ARCHCXXFLAGS += -std=$(CONFIG_CXX_STANDARD) +endif + +ifneq ($(CONFIG_CXX_EXCEPTION),y) + ARCHCXXFLAGS += -fno-exceptions -fcheck-new +endif + +ifneq ($(CONFIG_CXX_RTTI),y) + ARCHCXXFLAGS += -fno-rtti +endif + +ARCHOPTIMIZATION += -fno-common -Wall -Wshadow -Wundef + +LDFLAGS += -nostdlib +ARCHOPTIMIZATION += -nostdlib + +# Optimization of unused sections + +ifeq ($(CONFIG_DEBUG_OPT_UNUSED_SECTIONS),y) + LDFLAGS += --gc-sections + ARCHOPTIMIZATION += -ffunction-sections -fdata-sections +endif + +# Debug --whole-archive + +ifeq ($(CONFIG_DEBUG_LINK_WHOLE_ARCHIVE),y) + LDFLAGS += --whole-archive +endif + +# Debug link map + +ifeq ($(CONFIG_DEBUG_LINK_MAP),y) + LDFLAGS += --cref -Map=$(call CONVERT_PATH,$(TOPDIR)$(DELIM)nuttx.map) +endif + +ifeq ($(CONFIG_DEBUG_SYMBOLS),y) + ARCHOPTIMIZATION += $(CONFIG_DEBUG_SYMBOLS_LEVEL) +endif + +LDFLAGS += --no-warn-rwx-segments + +# Add the builtin library + +COMPILER_RT_LIB = $(shell $(CC) $(ARCHCPUFLAGS) --print-libgcc-file-name) + +EXTRA_LIBS += $(COMPILER_RT_LIB) + +ifeq ($(CONFIG_LIBM_TOOLCHAIN),y) + EXTRA_LIBS += $(wildcard $(shell $(CC) $(ARCHCPUFLAGS) --print-file-name=libm.a)) +endif + +ifeq ($(CONFIG_LIBSUPCXX),y) + EXTRA_LIBS += $(wildcard $(shell $(CC) $(ARCHCPUFLAGS) --print-file-name=libsupc++.a)) +endif + +ifeq ($(CONFIG_COVERAGE_TOOLCHAIN),y) + EXTRA_LIBS += $(wildcard $(shell $(CC) $(ARCHCPUFLAGS) --print-file-name=libgcov.a)) +endif + +ifeq ($(CONFIG_LIBCXXTOOLCHAIN),y) + EXTRA_LIBS += $(wildcard $(shell $(CC) $(ARCHCPUFLAGS) --print-file-name=libstdc++.a)) +endif diff --git a/arch/rh850/src/common/rh850_allocateheap.c b/arch/rh850/src/common/rh850_allocateheap.c new file mode 100644 index 00000000000..8ee36650ecb --- /dev/null +++ b/arch/rh850/src/common/rh850_allocateheap.c @@ -0,0 +1,52 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_allocateheap.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stddef.h> +#include <stdint.h> + +#include <nuttx/arch.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_allocate_heap + * + * Description: + * The heap is the RAM above the idle thread stack. + * + ****************************************************************************/ + +void up_allocate_heap(FAR void **heap_start, size_t *heap_size) +{ + *heap_start = (FAR void *)g_idle_topstack; + *heap_size = CONFIG_RAM_END - (uintptr_t)g_idle_topstack; +} diff --git a/arch/rh850/src/common/rh850_checkstack.c b/arch/rh850/src/common/rh850_checkstack.c new file mode 100644 index 00000000000..7da06c6c0c6 --- /dev/null +++ b/arch/rh850/src/common/rh850_checkstack.c @@ -0,0 +1,100 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_checkstack.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> + +#include <nuttx/arch.h> +#include <nuttx/board.h> +#include <nuttx/debug.h> +#include <nuttx/kmalloc.h> +#include <nuttx/sched.h> +#include <nuttx/tls.h> + +#include "rh850_internal.h" + +#ifdef CONFIG_STACK_COLORATION + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rh850_stack_check + * + * Description: + * Bytes used from the top of a colored stack: the stack grows down, the + * first word above the base without STACK_COLOR is the high mark. + * + ****************************************************************************/ + +static size_t rh850_stack_check(uintptr_t alloc, size_t size) +{ + uintptr_t start; + uintptr_t end; + uint32_t *ptr; + size_t mark; + + if (size == 0) + { + return 0; + } + + start = (alloc + 3) & ~3; + end = (alloc + size) & ~3; + size = end - start; + + for (ptr = (uint32_t *)start, mark = (size >> 2); + mark > 0 && *ptr == STACK_COLOR; + ptr++, mark--); + + return mark << 2; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +size_t up_check_tcbstack(FAR struct tcb_s *tcb, size_t check_size) +{ + return rh850_stack_check((uintptr_t)tcb->stack_base_ptr, check_size); +} + +size_t up_check_intstack(int cpu, size_t check_size) +{ + if (check_size == 0) + { + check_size = CONFIG_ARCH_INTERRUPTSTACK & ~3; + } + + return rh850_stack_check((uintptr_t)g_intstackalloc, check_size); +} + +#endif /* CONFIG_STACK_COLORATION */ diff --git a/arch/rh850/src/common/rh850_createstack.c b/arch/rh850/src/common/rh850_createstack.c new file mode 100644 index 00000000000..008bcd61876 --- /dev/null +++ b/arch/rh850/src/common/rh850_createstack.c @@ -0,0 +1,144 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_createstack.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> + +#include <nuttx/arch.h> +#include <nuttx/board.h> +#include <nuttx/debug.h> +#include <nuttx/kmalloc.h> +#include <nuttx/sched.h> +#include <nuttx/tls.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_create_stack + ****************************************************************************/ + +int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) +{ +#ifdef CONFIG_TLS_ALIGNED + DEBUGASSERT(stack_size <= TLS_MAXSTACK); + if (stack_size >= TLS_MAXSTACK) + { + stack_size = TLS_MAXSTACK; + } +#endif + + if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) + { + up_release_stack(tcb, ttype); + } + + if (!tcb->stack_alloc_ptr) + { +#ifdef CONFIG_TLS_ALIGNED + tcb->stack_alloc_ptr = kumm_memalign(TLS_STACK_ALIGN, stack_size); +#else + tcb->stack_alloc_ptr = kumm_malloc(stack_size); +#endif + +#ifdef CONFIG_DEBUG_FEATURES + if (!tcb->stack_alloc_ptr) + { + serr("ERROR: Failed to allocate stack, size %zu\n", stack_size); + } +#endif + } + + if (tcb->stack_alloc_ptr) + { + uintptr_t top_of_stack; + + top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size; + top_of_stack = STACKFRAME_ALIGN_DOWN(top_of_stack); + + tcb->stack_base_ptr = tcb->stack_alloc_ptr; + tcb->adj_stack_size = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr; + +#ifdef CONFIG_STACK_COLORATION + rh850_stack_color(tcb->stack_base_ptr, tcb->adj_stack_size); +#endif + + tcb->flags |= TCB_FLAG_FREE_STACK; + board_autoled_on(LED_STACKCREATED); + return OK; + } + + return -ENOMEM; +} + +#ifdef CONFIG_STACK_COLORATION +/**************************************************************************** + * Name: rh850_stack_color + * + * Description: + * Write STACK_COLOR to a stack; nbytes = 0 colors the running stack up + * to the current stack pointer. + * + ****************************************************************************/ + +void rh850_stack_color(FAR void *stackbase, size_t nbytes) +{ + uint32_t *stkptr; + uintptr_t stkend; + size_t nwords; + uintptr_t sp; + + stkptr = (uint32_t *)STACKFRAME_ALIGN_UP((uintptr_t)stackbase); + + if (nbytes == 0) + { + stkend = up_getsp(); + if (stkend > (uintptr_t)&sp) + { + stkend = (uintptr_t)&sp; + } + } + else + { + stkend = (uintptr_t)stackbase + nbytes; + } + + stkend = STACKFRAME_ALIGN_DOWN(stkend); + nwords = (stkend - (uintptr_t)stkptr) >> 2; + + while (nwords-- > 0) + { + *stkptr++ = STACK_COLOR; + } +} +#endif diff --git a/arch/rh850/src/common/rh850_doirq.c b/arch/rh850/src/common/rh850_doirq.c new file mode 100644 index 00000000000..a87f0a3a953 --- /dev/null +++ b/arch/rh850/src/common/rh850_doirq.c @@ -0,0 +1,94 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_doirq.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <assert.h> +#include <stdint.h> + +#include <nuttx/arch.h> +#include <nuttx/irq.h> + +#include "irq/irq.h" +#include "sched/sched.h" +#include "rh850_internal.h" + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* EIINT nesting depth, changed only with PSW.ID = 1 */ + +volatile uint8_t g_rh850_irqnest; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rh850_doirq + * + * Description: + * Interrupts are enabled around the dispatch. Only the outermost level + * switches tasks; inner levels return their own frame. + * + ****************************************************************************/ + +uintptr_t *rh850_doirq(int irq, uintptr_t *regs) +{ + struct tcb_s **running_task = &g_running_tasks[this_cpu()]; + struct tcb_s *tcb; + + if (g_rh850_irqnest++ == 0) + { + (*running_task)->xcp.regs = regs; + up_set_interrupt_context(true); + } + + /* Higher priorities nest from here; ISPR holds equal and lower ones */ + + up_irq_enable(); + irq_dispatch(irq, regs); + up_irq_save(); + + if (--g_rh850_irqnest > 0) + { + return regs; + } + + tcb = this_task(); + if (*running_task != tcb) + { + nxsched_switch_context(*running_task, tcb); + *running_task = tcb; + } + + up_set_interrupt_context(false); + + regs = tcb->xcp.regs; + (*running_task)->xcp.regs = NULL; + return regs; +} diff --git a/arch/rh850/src/common/rh850_exception.c b/arch/rh850/src/common/rh850_exception.c new file mode 100644 index 00000000000..f22b693aba7 --- /dev/null +++ b/arch/rh850/src/common/rh850_exception.c @@ -0,0 +1,155 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_exception.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <assert.h> +#include <inttypes.h> +#include <stdbool.h> +#include <stdint.h> + +#include <nuttx/arch.h> +#include <nuttx/debug.h> +#include <nuttx/irq.h> + +#include "rh850_internal.h" +#include "rh850_sysreg.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The high 16 bits of EIIC/FEIC hold a detail code */ + +#define RH850_CAUSE(code) ((code) & 0xffff) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static const char *rh850_cause_str(uint32_t cause) +{ + uint32_t c = RH850_CAUSE(cause); + + if (c >= 0x10 && c <= 0x1f) + { + return "SYSERR"; + } + + if (c >= 0x30 && c <= 0x3f) + { + return "FETRAP"; + } + + if (c >= 0x40 && c <= 0x4f) + { + return "TRAP0"; + } + + if (c >= 0x50 && c <= 0x5f) + { + return "TRAP1"; + } + + if (c >= 0x80 && c <= 0x82) + { + return "UCPOP"; + } + + if (c >= 0xf0 && c <= 0xff) + { + return "FEINT"; + } + + switch (c) + { + case 0x60: + return "RIE"; + + case 0x71: + return "FPE"; + + case 0x72: + return "FPI"; + + case 0x90: + return "MIP"; + + case 0x91: + case 0x95: + return "MDP"; + + case 0xa0: + return "PIE"; + + case 0xc0: + return "MAE"; + + case 0xe0: + return "FENMI"; + + default: + return "unknown"; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rh850_exception + * + * Description: + * EI and FE level exceptions and TRAP 0 codes other than the system call: + * report the cause and panic. + * + ****************************************************************************/ + +void rh850_exception(uint32_t cause, uint32_t level, uintptr_t *regs) +{ + bool fe = level == RH850_EXC_LEVEL_FE; + uint32_t c = RH850_CAUSE(cause); + + _alert("EXCEPTION: %s level %s, %s %08" PRIx32 ", PC %08" PRIxPTR + ", SP %08" PRIxPTR "\n", + fe ? "FE" : "EI", rh850_cause_str(cause), fe ? "FEIC" : "EIIC", + cause, regs[REG_EIPC], regs[REG_SP]); + + _alert("Interrupt context %d, EIINT nesting %d\n", + up_interrupt_context(), g_rh850_irqnest); + + if (c == 0xc0 || c == 0x91 || c == 0x95) + { + _alert("MEA %08" PRIx32 " MEI %08" PRIx32 "\n", + rh850_getsr(RH850_SR_MEA), rh850_getsr(RH850_SR_MEI)); + } + + _alert("PANIC!!! Exception = %" PRIx32 "\n", c); + up_irq_save(); + up_set_interrupt_context(true); + PANIC_WITH_REGS("panic", regs); +} diff --git a/arch/rh850/src/common/rh850_exception_common.S b/arch/rh850/src/common/rh850_exception_common.S new file mode 100644 index 00000000000..b125f264c98 --- /dev/null +++ b/arch/rh850/src/common/rh850_exception_common.S @@ -0,0 +1,119 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_exception_common.S + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include "rh850_internal.h" +#include "rh850_macros.S" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/* TRAP0 (system call): EIPC = next instruction, PSW.ID = EP = 1. EIRET + * with EP = 1 leaves ISPR unchanged. + */ + + .text + .globl _rh850_trap0 + .type _rh850_trap0, @function +_rh850_trap0: + rh850_frame_save + mov sp, r6 + jarl _rh850_doswi, lp + mov r10, sp + rh850_frame_restore + .size _rh850_trap0, . - _rh850_trap0 + +/* EIINT, all priority levels: irq = EIIC - 1000H. EIRET with EP = 0 + * clears the ISPR bit of the request. + */ + + .globl _rh850_eiint + .type _rh850_eiint, @function +_rh850_eiint: + rh850_frame_save + mov sp, r20 + mov hilo(_g_interrupt_context), r10 + ld.bu 0[r10], r10 + cmp 0, r10 + bne 1f + mov hilo(_g_intstacktop), sp +1: + rh850_stsr RH850_SR_EIIC, r6 + andi 0xffff, r6, r6 + addi -0x1000, r6, r6 + mov r20, r7 + jarl _rh850_doirq, lp + mov r10, sp + rh850_frame_restore + .size _rh850_eiint, . - _rh850_eiint + +/* CPU exceptions: rh850_exception(cause, level, regs) on the interrupt + * stack. It does not return. + */ + + .globl _rh850_exc_fe + .type _rh850_exc_fe, @function +_rh850_exc_fe: + rh850_frame_save fe=1, exc=1 + rh850_stsr RH850_SR_FEIC, r6 + mov RH850_EXC_LEVEL_FE, r7 + br 1f + .size _rh850_exc_fe, . - _rh850_exc_fe + + .globl _rh850_exc_ei + .type _rh850_exc_ei, @function +_rh850_exc_ei: + rh850_frame_save exc=1 + rh850_stsr RH850_SR_EIIC, r6 + mov RH850_EXC_LEVEL_EI, r7 +1: + mov sp, r8 + mov hilo(_g_interrupt_context), r10 + ld.bu 0[r10], r10 + cmp 0, r10 + bne 2f + mov hilo(_g_intstacktop), sp +2: + jarl _rh850_exception, lp + di +3: + halt + br 3b + .size _rh850_exc_ei, . - _rh850_exc_ei + +/**************************************************************************** + * Interrupt Stack + ****************************************************************************/ + + .section .bss + .balign 4 + .globl _g_intstackalloc + .globl _g_intstacktop +_g_intstackalloc: + .skip (CONFIG_ARCH_INTERRUPTSTACK & ~3) +_g_intstacktop: diff --git a/arch/rh850/src/common/rh850_exit.c b/arch/rh850/src/common/rh850_exit.c new file mode 100644 index 00000000000..abb601b2158 --- /dev/null +++ b/arch/rh850/src/common/rh850_exit.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_exit.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <assert.h> +#include <sched.h> + +#include <nuttx/arch.h> +#include <nuttx/irq.h> + +#include "task/task.h" +#include "sched/sched.h" +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_exit + ****************************************************************************/ + +void up_exit(int status) +{ + nxtask_exit(); + + g_running_tasks[this_cpu()] = this_task(); + rh850_fullcontextrestore(); + + PANIC(); +} diff --git a/arch/rh850/src/common/rh850_fpucmp.c b/arch/rh850/src/common/rh850_fpucmp.c new file mode 100644 index 00000000000..80ff17644cc --- /dev/null +++ b/arch/rh850/src/common/rh850_fpucmp.c @@ -0,0 +1,56 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_fpucmp.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> + +#include <nuttx/arch.h> +#include <nuttx/irq.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_fpucmp + * + * Description: + * The FPU works on the general registers; its own state is FPSR and FPEPC. + * + ****************************************************************************/ + +#ifdef CONFIG_ARCH_FPU +bool up_fpucmp(FAR const void *saveregs1, FAR const void *saveregs2) +{ + FAR const uintptr_t *regs1 = saveregs1; + FAR const uintptr_t *regs2 = saveregs2; + + return regs1[REG_FPSR] == regs2[REG_FPSR] && + regs1[REG_FPEPC] == regs2[REG_FPEPC]; +} +#endif diff --git a/arch/rh850/src/common/rh850_getintstack.c b/arch/rh850/src/common/rh850_getintstack.c new file mode 100644 index 00000000000..d3e1db1cfa0 --- /dev/null +++ b/arch/rh850/src/common/rh850_getintstack.c @@ -0,0 +1,46 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_getintstack.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +#include <nuttx/arch.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_get_intstackbase + ****************************************************************************/ + +uintptr_t up_get_intstackbase(int cpu) +{ + return (uintptr_t)g_intstackalloc; +} diff --git a/arch/rh850/src/common/rh850_head.S b/arch/rh850/src/common/rh850_head.S new file mode 100644 index 00000000000..b2f0f906a4d --- /dev/null +++ b/arch/rh850/src/common/rh850_head.S @@ -0,0 +1,178 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_head.S + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include "rh850_internal.h" +#include "rh850_sysreg.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_ARCH_FPU +# define RH850_PSW_INIT (RH850_PSW_ID | RH850_PSW_EBV | RH850_PSW_CU0) +#else +# define RH850_PSW_INIT (RH850_PSW_ID | RH850_PSW_EBV) +#endif + +/* One vector entry: 16 bytes */ + + .macro vector target + .balign 16 + jr \target + .endm + +/**************************************************************************** + * Vector Table + ****************************************************************************/ + +/* RBASE = EBASE = 0: reset and the exception vectors at 16-byte steps, then + * the 16 EIINT priority vectors (direct vector method). + */ + + .section .vectors, "ax" + .globl _rh850_vectors +_rh850_vectors: + vector __start /* 000H RESET */ + vector _rh850_exc_fe /* 010H SYSERR */ + vector _rh850_exc_fe /* 020H reserved */ + vector _rh850_exc_fe /* 030H FETRAP */ + vector _rh850_trap0 /* 040H TRAP0 */ + vector _rh850_exc_ei /* 050H TRAP1 */ + vector _rh850_exc_fe /* 060H RIE */ + vector _rh850_exc_ei /* 070H FPE, FPI */ + vector _rh850_exc_fe /* 080H UCPOP */ + vector _rh850_exc_fe /* 090H MIP, MDP */ + vector _rh850_exc_fe /* 0A0H PIE */ + vector _rh850_exc_fe /* 0B0H reserved */ + vector _rh850_exc_fe /* 0C0H MAE */ + vector _rh850_exc_fe /* 0D0H reserved */ + vector _rh850_exc_fe /* 0E0H FENMI */ + vector _rh850_exc_fe /* 0F0H FEINT */ + + .rept 16 + vector _rh850_eiint /* 100H-1F0H EIINT */ + .endr + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/* Reset entry: registers that are undefined after reset are written before + * use, then .data and .bss, then C. + */ + + .text + .globl __start + .type __start, @function +__start: + .irp n, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \ + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + mov r0, r\n + .endr + + ldsr r0, RH850_SR_EIPC + ldsr r0, RH850_SR_FEPC + ldsr r0, RH850_SR_CTPC + ldsr r0, RH850_SR_EIWR + ldsr r0, RH850_SR_FEWR + ldsr r0, RH850_SR_INTBP + ldsr r0, RH850_SR_MEA + ldsr r0, RH850_SR_MEI + ldsr r0, RH850_SR_RBIP + + mov hilo(_rh850_vectors), r10 + ldsr r10, RH850_SR_EBASE + + /* FPST and FPCC are views of FPSR and need PSW.CU0 */ + + mov hilo(RH850_PSW_ID | RH850_PSW_EBV | RH850_PSW_CU0), r10 + ldsr r10, RH850_SR_PSW + ldsr r0, RH850_SR_FPST + ldsr r0, RH850_SR_FPCC + mov hilo(RH850_FPSR_FS), r10 + ldsr r10, RH850_SR_FPSR + ldsr r0, RH850_SR_FPEPC + mov hilo(RH850_PSW_INIT), r10 + ldsr r10, RH850_SR_PSW + + ldsr r0, RH850_SR_MCA + ldsr r0, RH850_SR_MCS + ldsr r0, RH850_SR_MCR + ldsr r0, RH850_SR_MCI + mov r0, r10 + mov RH850_MPU_ENTRIES, r11 +1: + ldsr r10, RH850_SR_MPIDX + ldsr r0, RH850_SR_MPLA + ldsr r0, RH850_SR_MPUA + ldsr r0, RH850_SR_MPAT + add 1, r10 + cmp r11, r10 + bl 1b + ldsr r0, RH850_SR_MPIDX + .irp n, 24, 25, 26, 27, 28, 29, 30, 31 + ldsr r0, \n, 5 + .endr + + ldsr r0, RH850_SR_ICTAGL + ldsr r0, RH850_SR_ICTAGH + ldsr r0, RH850_SR_ICDATL + ldsr r0, RH850_SR_ICDATH + ldsr r0, RH850_SR_ICERR + + mov hilo(_g_idle_topstack), sp + + mov hilo(_eronly), r10 + mov hilo(_sdata), r11 + mov hilo(_edata), r12 +2: + cmp r12, r11 + bnl 3f + ld.w 0[r10], r13 + st.w r13, 0[r11] + add 4, r10 + add 4, r11 + br 2b +3: + + mov hilo(_sbss), r11 + mov hilo(_ebss), r12 +4: + cmp r12, r11 + bnl 5f + st.w r0, 0[r11] + add 4, r11 + br 4b +5: + + jarl _rh850_start, lp + di +6: + halt + br 6b + .size __start, . - __start diff --git a/arch/rh850/src/common/rh850_idle.c b/arch/rh850/src/common/rh850_idle.c new file mode 100644 index 00000000000..f9b0c938437 --- /dev/null +++ b/arch/rh850/src/common/rh850_idle.c @@ -0,0 +1,47 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_idle.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/arch.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_idle + * + * Description: + * A pending request releases HALT also while PSW.ID = 1, so DI, HALT, EI + * does not lose a wakeup. + * + ****************************************************************************/ + +void up_idle(void) +{ + __asm__ __volatile__("di\n\thalt\n\tei" : : : "memory"); +} diff --git a/arch/rh850/src/common/rh850_initialize.c b/arch/rh850/src/common/rh850_initialize.c new file mode 100644 index 00000000000..f77b2330802 --- /dev/null +++ b/arch/rh850/src/common/rh850_initialize.c @@ -0,0 +1,46 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_initialize.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/arch.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_initialize + ****************************************************************************/ + +void up_initialize(void) +{ +#ifdef USE_SERIALDRIVER + rh850_serialinit(); +#endif +} diff --git a/arch/rh850/src/common/rh850_initialstate.c b/arch/rh850/src/common/rh850_initialstate.c new file mode 100644 index 00000000000..44e52b63676 --- /dev/null +++ b/arch/rh850/src/common/rh850_initialstate.c @@ -0,0 +1,93 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_initialstate.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <string.h> + +#include <nuttx/arch.h> +#include <nuttx/sched.h> + +#include "rh850_internal.h" +#include "rh850_sysreg.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* PSW of a new task: EI level interrupts enabled */ + +#ifdef CONFIG_ARCH_FPU +# define RH850_PSW_NEWTASK (RH850_PSW_EBV | RH850_PSW_CU0) +# define RH850_FPSR_NEWTASK RH850_FPSR_FS +#else +# define RH850_PSW_NEWTASK RH850_PSW_EBV +# define RH850_FPSR_NEWTASK 0 +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_initial_state + * + * Description: + * The first frame of a new task sits at the top of its stack; EIRET + * enters tcb->start. The idle task already runs on the idle stack. + * + ****************************************************************************/ + +void up_initial_state(FAR struct tcb_s *tcb) +{ + struct xcptcontext *xcp = &tcb->xcp; + uintptr_t topstack; + + memset(xcp, 0, sizeof(struct xcptcontext)); + + if (tcb->pid == IDLE_PROCESS_ID) + { + tcb->stack_alloc_ptr = (FAR void *)((uintptr_t)g_idle_topstack - + CONFIG_IDLETHREAD_STACKSIZE); + tcb->stack_base_ptr = tcb->stack_alloc_ptr; + tcb->adj_stack_size = CONFIG_IDLETHREAD_STACKSIZE; + +#ifdef CONFIG_STACK_COLORATION + rh850_stack_color(tcb->stack_alloc_ptr, 0); +#endif + return; + } + + topstack = (uintptr_t)tcb->stack_base_ptr + tcb->adj_stack_size; + xcp->regs = (uintptr_t *)(topstack - XCPTCONTEXT_SIZE); + memset(xcp->regs, 0, XCPTCONTEXT_SIZE); + + xcp->regs[REG_SP] = topstack; + xcp->regs[REG_EIPC] = (uintptr_t)tcb->start; + xcp->regs[REG_EIPSW] = RH850_PSW_NEWTASK; + xcp->regs[REG_FPSR] = RH850_FPSR_NEWTASK; +} diff --git a/arch/rh850/src/common/rh850_internal.h b/arch/rh850/src/common/rh850_internal.h new file mode 100644 index 00000000000..cec185153b4 --- /dev/null +++ b/arch/rh850/src/common/rh850_internal.h @@ -0,0 +1,118 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_internal.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RH850_SRC_COMMON_RH850_INTERNAL_H +#define __ARCH_RH850_SRC_COMMON_RH850_INTERNAL_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#ifndef __ASSEMBLY__ +# include <stdbool.h> +# include <stddef.h> +# include <stdint.h> +# include <syscall.h> +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Console driver: the serial driver unless the console is the syslog */ + +#ifndef CONFIG_DEV_CONSOLE +# undef USE_SERIALDRIVER +#else +# if defined(CONFIG_CONSOLE_SYSLOG) +# undef USE_SERIALDRIVER +# else +# define USE_SERIALDRIVER 1 +# endif +#endif + +#define STACK_COLOR 0xdeadbeef + +/* Context switch and restore through TRAP 0 */ + +#define rh850_switchcontext() sys_call0(SYS_switch_context) +#define rh850_fullcontextrestore() sys_call0(SYS_restore_context) + +#define getreg8(a) (*(volatile uint8_t *)(a)) +#define putreg8(v,a) (*(volatile uint8_t *)(a) = (v)) +#define getreg16(a) (*(volatile uint16_t *)(a)) +#define putreg16(v,a) (*(volatile uint16_t *)(a) = (v)) +#define getreg32(a) (*(volatile uint32_t *)(a)) +#define putreg32(v,a) (*(volatile uint32_t *)(a) = (v)) + +/* Exception level passed to rh850_exception() */ + +#define RH850_EXC_LEVEL_EI 0 +#define RH850_EXC_LEVEL_FE 1 + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +/* Top of the idle thread stack (linker script), the heap starts there */ + +extern uint8_t g_idle_topstack[]; + +extern uint8_t g_intstackalloc[]; +extern uint8_t g_intstacktop[]; + +/* EIINT nesting depth */ + +extern volatile uint8_t g_rh850_irqnest; + +void rh850_start(void); + +/* Software system reset, does not return */ + +void up_systemreset(void) noreturn_function; + +/* Chip: clock and standby setup, console */ + +void rh850_clockconfig(void); +void rh850_lowsetup(void); +void rh850_lowputc(int ch); +void rh850_serialinit(void); +void u2c_rlin3_setup(uintptr_t base, uint32_t baud); +void u2c_rlin3_putc(uintptr_t base, int ch); +bool u2c_irq_pending(int irq); +void u2c_irq_raise(int irq); +int u2c_irq_setpriority(int irq, int priority); + +/* Exception entry, signals, stacks */ + +uintptr_t *rh850_doirq(int irq, uintptr_t *regs); +uintptr_t *rh850_doswi(uintptr_t *regs); +void rh850_exception(uint32_t cause, uint32_t level, uintptr_t *regs); +void rh850_sigdeliver(void); +void rh850_stack_color(void *stackbase, size_t nbytes); + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RH850_SRC_COMMON_RH850_INTERNAL_H */ diff --git a/arch/rh850/src/common/rh850_irq.c b/arch/rh850/src/common/rh850_irq.c new file mode 100644 index 00000000000..ce1e1d2ae47 --- /dev/null +++ b/arch/rh850/src/common/rh850_irq.c @@ -0,0 +1,51 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_irq.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdbool.h> + +#include <nuttx/arch.h> +#include <nuttx/irq.h> + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +volatile bool g_interrupt_context; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_irq_enable + ****************************************************************************/ + +void up_irq_enable(void) +{ + __asm__ __volatile__("ei" : : : "memory"); +} diff --git a/arch/rh850/src/common/rh850_macros.S b/arch/rh850/src/common/rh850_macros.S new file mode 100644 index 00000000000..2b7b1e90cbf --- /dev/null +++ b/arch/rh850/src/common/rh850_macros.S @@ -0,0 +1,122 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_macros.S + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RH850_SRC_COMMON_RH850_MACROS_S +#define __ARCH_RH850_SRC_COMMON_RH850_MACROS_S + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <arch/irq.h> + +#include "rh850_sysreg.h" + +/**************************************************************************** + * Assembly Macros + ****************************************************************************/ + + .macro rh850_stsr regid, selid, reg + stsr \regid, \reg, \selid + .endm + +/* fe = 1 saves FEPC/FEPSW in the EIPC/EIPSW slots. exc = 1 sets CU0 so + * that FPSR can be read after UCPOP, and EP so that EIRET keeps ISPR. + */ + + .macro rh850_frame_save fe=0, exc=0 + addi -XCPTCONTEXT_SIZE, sp, sp + st.w r1, (4 * REG_R1)[sp] + st.w r2, (4 * REG_R2)[sp] + .irp n, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \ + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + st.w r\n, (4 * (REG_R4 + \n - 4))[sp] + .endr + .if \fe + rh850_stsr RH850_SR_FEPC, r10 + rh850_stsr RH850_SR_FEPSW, r11 + .else + rh850_stsr RH850_SR_EIPC, r10 + rh850_stsr RH850_SR_EIPSW, r11 + .endif + st.w r10, (4 * REG_EIPC)[sp] + st.w r11, (4 * REG_EIPSW)[sp] + addi XCPTCONTEXT_SIZE, sp, r10 + st.w r10, (4 * REG_SP)[sp] + .if \exc + rh850_stsr RH850_SR_PSW, r10 + mov hilo(RH850_PSW_CU0 | RH850_PSW_EP), r11 + or r11, r10 + ldsr r10, RH850_SR_PSW + .endif +#ifdef CONFIG_ARCH_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, (4 * REG_FPSR)[sp] + rh850_stsr RH850_SR_FPEPC, r10 + st.w r10, (4 * REG_FPEPC)[sp] +#else + st.w r0, (4 * REG_FPSR)[sp] + st.w r0, (4 * REG_FPEPC)[sp] +#endif + .endm + +/* Restore the frame at sp and return with EIRET. sp is loaded last from + * REG_SP, the frame base is no longer needed after that. A pending FPI + * (FPEC.FPIVD = 1) keeps its FPEPC and FPSR cause bits. + */ + + .macro rh850_frame_restore +#ifdef CONFIG_ARCH_FPU + ld.w (4 * REG_FPSR)[sp], r10 + rh850_stsr RH850_SR_FPEC, r11 + andi RH850_FPEC_FPIVD, r11, r11 + bz .Lfpu_frame\@ + rh850_stsr RH850_SR_FPSR, r11 + andi RH850_FPSR_XC_MASK, r11, r11 + andi RH850_FPSR_XC_MASK, r10, r12 + xor r12, r10 + or r11, r10 + ldsr r10, RH850_SR_FPSR + br .Lfpu_done\@ +.Lfpu_frame\@: + ldsr r10, RH850_SR_FPSR + ld.w (4 * REG_FPEPC)[sp], r10 + ldsr r10, RH850_SR_FPEPC +.Lfpu_done\@: +#endif + ld.w (4 * REG_EIPC)[sp], r10 + ldsr r10, RH850_SR_EIPC + ld.w (4 * REG_EIPSW)[sp], r10 + ldsr r10, RH850_SR_EIPSW + ld.w (4 * REG_R1)[sp], r1 + ld.w (4 * REG_R2)[sp], r2 + .irp n, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \ + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + ld.w (4 * (REG_R4 + \n - 4))[sp], r\n + .endr + ld.w (4 * REG_SP)[sp], sp + eiret + .endm + +#endif /* __ARCH_RH850_SRC_COMMON_RH850_MACROS_S */ diff --git a/arch/rh850/src/common/rh850_nputs.c b/arch/rh850/src/common/rh850_nputs.c new file mode 100644 index 00000000000..023631990df --- /dev/null +++ b/arch/rh850/src/common/rh850_nputs.c @@ -0,0 +1,47 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_nputs.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stddef.h> + +#include <nuttx/arch.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_nputs + ****************************************************************************/ + +void up_nputs(FAR const char *str, size_t len) +{ + while (len-- > 0 && *str) + { + up_putc(*str++); + } +} diff --git a/arch/rh850/src/common/rh850_registerdump.c b/arch/rh850/src/common/rh850_registerdump.c new file mode 100644 index 00000000000..a7968a5ee2a --- /dev/null +++ b/arch/rh850/src/common/rh850_registerdump.c @@ -0,0 +1,78 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_registerdump.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <inttypes.h> +#include <stdint.h> + +#include <nuttx/arch.h> +#include <nuttx/debug.h> +#include <nuttx/irq.h> +#include <nuttx/sched.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_dump_register + ****************************************************************************/ + +void up_dump_register(FAR void *dumpregs) +{ + volatile uintptr_t *regs = dumpregs ? dumpregs : running_regs(); + + _alert("R1: %08" PRIxPTR " R2: %08" PRIxPTR " SP: %08" PRIxPTR + " R4: %08" PRIxPTR " R5: %08" PRIxPTR "\n", + regs[REG_R1], regs[REG_R2], regs[REG_SP], regs[REG_R4], + regs[REG_R5]); + _alert("R6: %08" PRIxPTR " R7: %08" PRIxPTR " R8: %08" PRIxPTR + " R9: %08" PRIxPTR "\n", + regs[REG_R6], regs[REG_R7], regs[REG_R8], regs[REG_R9]); + _alert("R10: %08" PRIxPTR " R11: %08" PRIxPTR " R12: %08" PRIxPTR + " R13: %08" PRIxPTR "\n", + regs[REG_R10], regs[REG_R11], regs[REG_R12], regs[REG_R13]); + _alert("R14: %08" PRIxPTR " R15: %08" PRIxPTR " R16: %08" PRIxPTR + " R17: %08" PRIxPTR "\n", + regs[REG_R14], regs[REG_R15], regs[REG_R16], regs[REG_R17]); + _alert("R18: %08" PRIxPTR " R19: %08" PRIxPTR " R20: %08" PRIxPTR + " R21: %08" PRIxPTR "\n", + regs[REG_R18], regs[REG_R19], regs[REG_R20], regs[REG_R21]); + _alert("R22: %08" PRIxPTR " R23: %08" PRIxPTR " R24: %08" PRIxPTR + " R25: %08" PRIxPTR "\n", + regs[REG_R22], regs[REG_R23], regs[REG_R24], regs[REG_R25]); + _alert("R26: %08" PRIxPTR " R27: %08" PRIxPTR " R28: %08" PRIxPTR + " R29: %08" PRIxPTR "\n", + regs[REG_R26], regs[REG_R27], regs[REG_R28], regs[REG_R29]); + _alert("EP: %08" PRIxPTR " LP: %08" PRIxPTR "\n", + regs[REG_EP], regs[REG_LP]); + _alert("PC: %08" PRIxPTR " PSW: %08" PRIxPTR " FPSR: %08" PRIxPTR + " FPEPC: %08" PRIxPTR "\n", + regs[REG_EIPC], regs[REG_EIPSW], regs[REG_FPSR], regs[REG_FPEPC]); +} diff --git a/arch/rh850/src/common/rh850_releasestack.c b/arch/rh850/src/common/rh850_releasestack.c new file mode 100644 index 00000000000..d206e0aa213 --- /dev/null +++ b/arch/rh850/src/common/rh850_releasestack.c @@ -0,0 +1,62 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_releasestack.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> + +#include <nuttx/arch.h> +#include <nuttx/board.h> +#include <nuttx/debug.h> +#include <nuttx/kmalloc.h> +#include <nuttx/sched.h> +#include <nuttx/tls.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_release_stack + ****************************************************************************/ + +void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) +{ + if (dtcb->stack_alloc_ptr && (dtcb->flags & TCB_FLAG_FREE_STACK)) + { + kumm_free(dtcb->stack_alloc_ptr); + } + + dtcb->flags &= ~TCB_FLAG_FREE_STACK; + dtcb->stack_alloc_ptr = NULL; + dtcb->stack_base_ptr = NULL; + dtcb->adj_stack_size = 0; +} diff --git a/arch/rh850/src/common/rh850_saveusercontext.S b/arch/rh850/src/common/rh850_saveusercontext.S new file mode 100644 index 00000000000..5892b52899d --- /dev/null +++ b/arch/rh850/src/common/rh850_saveusercontext.S @@ -0,0 +1,64 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_saveusercontext.S + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include "rh850_macros.S" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/* int up_saveusercontext(void *saveregs): frame of the caller, EIPC = the + * return address, returns 0. + */ + + .text + .globl _up_saveusercontext + .type _up_saveusercontext, @function +_up_saveusercontext: + st.w r1, (4 * REG_R1)[r6] + st.w r2, (4 * REG_R2)[r6] + .irp n, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \ + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + st.w r\n, (4 * (REG_R4 + \n - 4))[r6] + .endr + st.w lp, (4 * REG_EIPC)[r6] + st.w sp, (4 * REG_SP)[r6] + rh850_stsr RH850_SR_PSW, r10 + st.w r10, (4 * REG_EIPSW)[r6] +#ifdef CONFIG_ARCH_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, (4 * REG_FPSR)[r6] + rh850_stsr RH850_SR_FPEPC, r10 + st.w r10, (4 * REG_FPEPC)[r6] +#else + st.w r0, (4 * REG_FPSR)[r6] + st.w r0, (4 * REG_FPEPC)[r6] +#endif + mov 0, r10 + jmp [lp] + .size _up_saveusercontext, . - _up_saveusercontext diff --git a/arch/rh850/src/common/rh850_schedulesigaction.c b/arch/rh850/src/common/rh850_schedulesigaction.c new file mode 100644 index 00000000000..d65faa3dfe6 --- /dev/null +++ b/arch/rh850/src/common/rh850_schedulesigaction.c @@ -0,0 +1,65 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_schedulesigaction.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <string.h> + +#include <nuttx/arch.h> +#include <nuttx/debug.h> +#include <nuttx/sched.h> + +#include "sched/sched.h" +#include "rh850_internal.h" +#include "rh850_sysreg.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_schedule_sigaction + * + * Description: + * Let the task resume in rh850_sigdeliver() with EI level interrupts + * disabled; its frame is kept in saved_regs. + * + ****************************************************************************/ + +void up_schedule_sigaction(FAR struct tcb_s *tcb) +{ + sinfo("tcb=%p, rtcb=%p regs=%p\n", tcb, this_task(), tcb->xcp.regs); + + tcb->xcp.saved_regs = tcb->xcp.regs; + tcb->xcp.regs = (uintptr_t *)((uintptr_t)tcb->xcp.regs - + XCPTCONTEXT_SIZE); + memcpy(tcb->xcp.regs, tcb->xcp.saved_regs, XCPTCONTEXT_SIZE); + + tcb->xcp.regs[REG_SP] = (uintptr_t)tcb->xcp.saved_regs; + tcb->xcp.regs[REG_EIPC] = (uintptr_t)rh850_sigdeliver; + tcb->xcp.regs[REG_EIPSW] |= RH850_PSW_ID; +} diff --git a/arch/rh850/src/common/rh850_sigdeliver.c b/arch/rh850/src/common/rh850_sigdeliver.c new file mode 100644 index 00000000000..b070a8871e2 --- /dev/null +++ b/arch/rh850/src/common/rh850_sigdeliver.c @@ -0,0 +1,88 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_sigdeliver.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <assert.h> +#include <stdint.h> + +#include <nuttx/arch.h> +#include <nuttx/board.h> +#include <nuttx/debug.h> +#include <nuttx/irq.h> +#include <nuttx/queue.h> +#include <nuttx/sched.h> + +#include "sched/sched.h" +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rh850_sigdeliver + * + * Description: + * Run the pending signal actions of the current task, then restore the + * frame saved by up_schedule_sigaction(). + * + ****************************************************************************/ + +void rh850_sigdeliver(void) +{ + struct tcb_s *rtcb = this_task(); + uintptr_t *regs = rtcb->xcp.saved_regs; + + board_autoled_on(LED_SIGNAL); + + sinfo("rtcb=%p sigdeliver=%p sigpendactionq.head=%p\n", + rtcb, rtcb->sigdeliver, rtcb->sigpendactionq.head); + DEBUGASSERT(rtcb->sigdeliver != NULL); + +retry: +#ifndef CONFIG_SUPPRESS_INTERRUPTS + up_irq_enable(); +#endif + + (rtcb->sigdeliver)(rtcb); + +#ifndef CONFIG_SUPPRESS_INTERRUPTS + up_irq_save(); +#endif + + if (!sq_empty(&rtcb->sigpendactionq) && + (rtcb->flags & TCB_FLAG_SIGNAL_ACTION) == 0) + { + goto retry; + } + + rtcb->sigdeliver = NULL; + board_autoled_off(LED_SIGNAL); + + rtcb->xcp.regs = regs; + rh850_fullcontextrestore(); +} diff --git a/arch/rh850/src/common/rh850_stackframe.c b/arch/rh850/src/common/rh850_stackframe.c new file mode 100644 index 00000000000..36e8ce28d69 --- /dev/null +++ b/arch/rh850/src/common/rh850_stackframe.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_stackframe.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> + +#include <nuttx/arch.h> +#include <nuttx/board.h> +#include <nuttx/debug.h> +#include <nuttx/kmalloc.h> +#include <nuttx/sched.h> +#include <nuttx/tls.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_stack_frame + ****************************************************************************/ + +FAR void *up_stack_frame(FAR struct tcb_s *tcb, size_t frame_size) +{ + FAR void *ret; + + frame_size = STACKFRAME_ALIGN_UP(frame_size); + + if (!tcb->stack_alloc_ptr || tcb->adj_stack_size <= frame_size) + { + return NULL; + } + + ret = tcb->stack_base_ptr; + memset(ret, 0, frame_size); + + tcb->stack_base_ptr = (FAR uint8_t *)tcb->stack_base_ptr + frame_size; + tcb->adj_stack_size -= frame_size; + + return ret; +} diff --git a/arch/rh850/src/common/rh850_start.c b/arch/rh850/src/common/rh850_start.c new file mode 100644 index 00000000000..9a7fb066480 --- /dev/null +++ b/arch/rh850/src/common/rh850_start.c @@ -0,0 +1,73 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_start.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/arch.h> +#include <nuttx/init.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_DEBUG_FEATURES +# define showprogress(c) rh850_lowputc(c) +#else +# define showprogress(c) +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rh850_start + * + * Description: + * C entry from the reset code, with system registers, .data and .bss + * set up. + * + ****************************************************************************/ + +void rh850_start(void) +{ + rh850_clockconfig(); + rh850_lowsetup(); + + /* A: reset code done, console ready */ + + showprogress('A'); + + /* B: entering the OS */ + + showprogress('B'); + showprogress('\r'); + showprogress('\n'); + + nx_start(); +} diff --git a/arch/rh850/src/common/rh850_swint.c b/arch/rh850/src/common/rh850_swint.c new file mode 100644 index 00000000000..116404f0ee6 --- /dev/null +++ b/arch/rh850/src/common/rh850_swint.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_swint.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <assert.h> +#include <stdint.h> +#include <syscall.h> + +#include <nuttx/arch.h> +#include <nuttx/irq.h> + +#include "sched/sched.h" +#include "rh850_internal.h" +#include "rh850_sysreg.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rh850_doswi + * + * Description: + * TRAP 0 system call: r6 holds the call number. Returns the frame to + * restore. + * + ****************************************************************************/ + +uintptr_t *rh850_doswi(uintptr_t *regs) +{ + struct tcb_s **running_task = &g_running_tasks[this_cpu()]; + struct tcb_s *tcb; + uintptr_t cmd = regs[REG_R6]; + uint32_t eiic; + + eiic = rh850_getsr(RH850_SR_EIIC); + if (eiic != RH850_EIIC_TRAP0) + { + rh850_exception(eiic, RH850_EXC_LEVEL_EI, regs); + } + + if (cmd != SYS_restore_context) + { + (*running_task)->xcp.regs = regs; + } + + up_set_interrupt_context(true); + + switch (cmd) + { + case SYS_restore_context: + case SYS_switch_context: + restore_critical_section(this_task(), this_cpu()); + break; + + default: + PANIC(); + break; + } + + tcb = this_task(); + if (*running_task != tcb || cmd == SYS_restore_context) + { + nxsched_switch_context(*running_task, tcb); + *running_task = tcb; + } + + up_set_interrupt_context(false); + + regs = tcb->xcp.regs; + (*running_task)->xcp.regs = NULL; + return regs; +} diff --git a/arch/rh850/src/common/rh850_switchcontext.c b/arch/rh850/src/common/rh850_switchcontext.c new file mode 100644 index 00000000000..22a58f2f963 --- /dev/null +++ b/arch/rh850/src/common/rh850_switchcontext.c @@ -0,0 +1,53 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_switchcontext.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/arch.h> +#include <nuttx/sched.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_switch_context + * + * Description: + * In interrupt context the exit path restores the frame of this_task(). + * FPSR and FPEPC are part of every frame. + * + ****************************************************************************/ + +void up_switch_context(FAR struct tcb_s *tcb, FAR struct tcb_s *rtcb) +{ + if (!up_interrupt_context()) + { + rh850_switchcontext(); + } +} diff --git a/arch/rh850/src/common/rh850_sysreg.h b/arch/rh850/src/common/rh850_sysreg.h new file mode 100644 index 00000000000..42fbaa33efd --- /dev/null +++ b/arch/rh850/src/common/rh850_sysreg.h @@ -0,0 +1,109 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_sysreg.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RH850_SRC_COMMON_RH850_SYSREG_H +#define __ARCH_RH850_SRC_COMMON_RH850_SYSREG_H + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* System registers as "regID, selID" for LDSR/STSR */ + +#define RH850_SR_EIPC 0, 0 +#define RH850_SR_EIPSW 1, 0 +#define RH850_SR_FEPC 2, 0 +#define RH850_SR_FEPSW 3, 0 +#define RH850_SR_PSW 5, 0 +#define RH850_SR_FPSR 6, 0 +#define RH850_SR_FPEPC 7, 0 +#define RH850_SR_FPST 8, 0 +#define RH850_SR_FPCC 9, 0 +#define RH850_SR_FPEC 11, 0 +#define RH850_SR_EIIC 13, 0 +#define RH850_SR_FEIC 14, 0 +#define RH850_SR_CTPC 16, 0 +#define RH850_SR_EIWR 28, 0 +#define RH850_SR_FEWR 29, 0 +#define RH850_SR_EBASE 3, 1 +#define RH850_SR_INTBP 4, 1 +#define RH850_SR_MEA 6, 2 +#define RH850_SR_MEI 8, 2 +#define RH850_SR_ISPR 10, 2 +#define RH850_SR_RBCR0 15, 2 +#define RH850_SR_RBIP 18, 2 +#define RH850_SR_ICTAGL 16, 4 +#define RH850_SR_ICTAGH 17, 4 +#define RH850_SR_ICDATL 18, 4 +#define RH850_SR_ICDATH 19, 4 +#define RH850_SR_ICERR 28, 4 +#define RH850_SR_MCA 8, 5 +#define RH850_SR_MCS 9, 5 +#define RH850_SR_MCR 11, 5 +#define RH850_SR_MCI 12, 5 +#define RH850_SR_MPIDX 16, 5 +#define RH850_SR_MPLA 20, 5 +#define RH850_SR_MPUA 21, 5 +#define RH850_SR_MPAT 22, 5 + +/* PSW bits */ + +#define RH850_PSW_ID 0x00000020 /* EI level interrupts disabled */ +#define RH850_PSW_EP 0x00000040 /* Exception being processed */ +#define RH850_PSW_NP 0x00000080 /* FE level interrupts disabled */ +#define RH850_PSW_EBV 0x00008000 /* Vectors at EBASE */ +#define RH850_PSW_CU0 0x00010000 /* FPU usable */ + +/* FPSR.FS: subnormal operands and results are flushed, not trapped */ + +#define RH850_FPSR_FS 0x00020000 + +/* FPSR.XC: the cause bits E, V, Z, O, U, I */ + +#define RH850_FPSR_XC_MASK 0x0000fc00 + +/* FPEC.FPIVD: an imprecise FPU exception (FPI) is reported and pending */ + +#define RH850_FPEC_FPIVD 0x00000001 + +/* EIIC of TRAP 0x0 */ + +#define RH850_EIIC_TRAP0 0x40 + +/* MPU protection areas on U2C (MPCFG.NMPUE + 1) */ + +#define RH850_MPU_ENTRIES 24 + +/* C read of a system register named by its regID, selID pair */ + +#ifndef __ASSEMBLY__ +# define rh850_stsr_pair(regid, selid) \ + ({ \ + uint32_t __v; \ + __asm__ __volatile__("stsr " #regid ", %0, " #selid \ + : "=r"(__v)); \ + __v; \ + }) +# define rh850_getsr(sr) rh850_stsr_pair(sr) +#endif + +#endif /* __ARCH_RH850_SRC_COMMON_RH850_SYSREG_H */ diff --git a/arch/rh850/src/common/rh850_usestack.c b/arch/rh850/src/common/rh850_usestack.c new file mode 100644 index 00000000000..0cb8458696a --- /dev/null +++ b/arch/rh850/src/common/rh850_usestack.c @@ -0,0 +1,79 @@ +/**************************************************************************** + * arch/rh850/src/common/rh850_usestack.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> + +#include <nuttx/arch.h> +#include <nuttx/board.h> +#include <nuttx/debug.h> +#include <nuttx/kmalloc.h> +#include <nuttx/sched.h> +#include <nuttx/tls.h> + +#include "rh850_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_use_stack + ****************************************************************************/ + +int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size) +{ + uintptr_t top_of_stack; + +#ifdef CONFIG_TLS_ALIGNED + DEBUGASSERT(stack_size <= TLS_MAXSTACK); + if (stack_size >= TLS_MAXSTACK) + { + stack_size = TLS_MAXSTACK; + } +#endif + + if (tcb->stack_alloc_ptr) + { + up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK); + } + + tcb->stack_alloc_ptr = stack; + tcb->stack_base_ptr = (FAR void *)STACKFRAME_ALIGN_UP((uintptr_t)stack); + + top_of_stack = STACKFRAME_ALIGN_DOWN((uintptr_t)stack + stack_size); + tcb->adj_stack_size = top_of_stack - (uintptr_t)tcb->stack_base_ptr; + +#ifdef CONFIG_STACK_COLORATION + rh850_stack_color(tcb->stack_base_ptr, tcb->adj_stack_size); +#endif + + return OK; +} diff --git a/libs/libc/machine/rh850/CMakeLists.txt b/libs/libc/machine/rh850/CMakeLists.txt new file mode 100644 index 00000000000..e1f670a869e --- /dev/null +++ b/libs/libc/machine/rh850/CMakeLists.txt @@ -0,0 +1,21 @@ +# ############################################################################## +# libs/libc/machine/rh850/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ##############################################################################
