This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 569ad3d14bff53f115f1293201cc929793621972 Author: zhangyu117 <[email protected]> AuthorDate: Fri Nov 6 17:53:53 2026 +0800 arch/tricore: support protected mode Add the TriCore protected-mode infrastructure needed to run user applications in unprivileged mode, including syscall handling, task startup, signal delivery, and userspace initialization. Add the TC397 and TC4D9 board userspace descriptors and build support. CMake userspace sources are added by the board kernel CMakeLists files. [skip-cmake-parity] Signed-off-by: zhangyu117 <[email protected]> --- arch/tricore/include/irq.h | 27 +++ arch/tricore/src/common/CMakeLists.txt | 9 + arch/tricore/src/common/Make.defs | 10 + arch/tricore/src/common/tricore_allocateheap.c | 72 ++++++- arch/tricore/src/common/tricore_initialstate.c | 2 +- arch/tricore/src/common/tricore_internal.h | 4 + arch/tricore/src/common/tricore_main.c | 3 + ...core_allocateheap.c => tricore_pthread_start.c} | 63 +++--- .../tricore/src/common/tricore_schedulesigaction.c | 3 +- arch/tricore/src/common/tricore_signal_dispatch.c | 76 ++++++++ ...ore_allocateheap.c => tricore_signal_handler.c} | 44 ++--- arch/tricore/src/common/tricore_svcall.c | 211 ++++++++++++++++++++- ...tricore_allocateheap.c => tricore_task_start.c} | 60 +++--- arch/tricore/src/tc3xx/Make.defs | 5 + arch/tricore/src/tc4xx/Make.defs | 5 + .../tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt | 4 + .../a2g-tc397-5v-tft/{ => kernel}/CMakeLists.txt | 4 +- .../a2g-tc397-5v-tft/kernel/tc397_userspace.c | 81 ++++---- .../scripts/Lcf_Tasking_Tricore_Tc.lsl.user | 2 +- .../tc3xx/a2g-tc397-5v-tft/src/CMakeLists.txt | 1 + boards/tricore/tc3xx/a2g-tc397-5v-tft/src/Makefile | 4 + .../tc3xx/a2g-tc397-5v-tft/src/tc397_userspace.c | 109 ++++------- .../tc4xx/triboard_tc4x9_com/CMakeLists.txt | 4 + .../triboard_tc4x9_com/kernel}/CMakeLists.txt | 4 +- .../triboard_tc4x9_com/kernel/tc4d9_userspace.c | 81 ++++---- .../scripts/Lcf_Tasking_Tricore_Tc.lsl.user | 1 + .../tc4xx/triboard_tc4x9_com/src/CMakeLists.txt | 1 + .../tricore/tc4xx/triboard_tc4x9_com/src/Makefile | 4 + .../tc4xx/triboard_tc4x9_com/src/tc4d9_userspace.c | 109 ++++------- 29 files changed, 699 insertions(+), 304 deletions(-) diff --git a/arch/tricore/include/irq.h b/arch/tricore/include/irq.h index a119d9fdcf8..2d6b9d3d845 100644 --- a/arch/tricore/include/irq.h +++ b/arch/tricore/include/irq.h @@ -116,10 +116,23 @@ #define PSW_CDE (1 << 7) /* Bits 7: Call Depth Count Enable */ #define PSW_IS (1 << 9) /* Bits 9: Interrupt Stack Control */ #define PSW_IO (10) /* Bits 10-11: Access Privilege Level Control (I/O Privilege) */ +# define PSW_MODE_MASK (3 << PSW_IO) # define PSW_IO_USER0 (0 << PSW_IO) # define PSW_IO_USER1 (1 << PSW_IO) # define PSW_IO_SUPERVISOR (2 << PSW_IO) +#define PSW_PRS_KERNEL_SET (0) +#define PSW_PRS_USER_SET (1) + +#ifdef CONFIG_ARCH_USE_MPU +# define PSW_PRS_MASK ((3 << 12) | (1 << 15)) +# define PSW_PRS_USER (((PSW_PRS_USER_SET & 3) << 12) | \ + ((PSW_PRS_USER_SET & 4) << 13)) +#else +# define PSW_PRS_MASK (0) +# define PSW_PRS_USER (0) +#endif + /* PCXI: Previous Context Information and Pointer Register */ #define PCXI_UL (1 << 20) /* Bits 20: Upper or Lower Context Tag */ @@ -184,6 +197,20 @@ struct xcptcontext uintptr_t *saved_regs; #endif + +#ifdef CONFIG_BUILD_PROTECTED + /* This is the saved address to use when returning from a user-space + * signal handler. + */ + + uint32_t sigreturn; +#endif + +#ifdef CONFIG_LIB_SYSCALL + uint8_t nsyscalls; + uintptr_t *syscall_regs[CONFIG_SYS_NNEST]; +#endif + /* Register save area with XCPTCONTEXT_SIZE, only valid when: * 1.The task isn't running or * 2.The task is interrupted diff --git a/arch/tricore/src/common/CMakeLists.txt b/arch/tricore/src/common/CMakeLists.txt index 22f27f57095..8aaaeb9b2da 100644 --- a/arch/tricore/src/common/CMakeLists.txt +++ b/arch/tricore/src/common/CMakeLists.txt @@ -46,6 +46,15 @@ set(SRCS tricore_usestack.c tricore_fork.c) +if(CONFIG_BUILD_PROTECTED OR CONFIG_BUILD_KERNEL) + list(APPEND SRCS tricore_task_start.c tricore_pthread_start.c + tricore_signal_dispatch.c) + + if(CONFIG_BUILD_PROTECTED) + target_sources(arch_interface PRIVATE tricore_signal_handler.c) + endif() +endif() + if(CONFIG_SCHED_BACKTRACE) list(APPEND SRCS tricore_backtrace.c) endif() diff --git a/arch/tricore/src/common/Make.defs b/arch/tricore/src/common/Make.defs index f8ca26799c5..d7998444f10 100644 --- a/arch/tricore/src/common/Make.defs +++ b/arch/tricore/src/common/Make.defs @@ -64,6 +64,16 @@ ifeq ($(CONFIG_ENABLE_ALL_SIGNALS),y) CMN_CSRCS += tricore_schedulesigaction.c tricore_sigdeliver.c endif +ifeq ($(CONFIG_BUILD_PROTECTED)$(CONFIG_BUILD_KERNEL),y) + CMN_CSRCS += tricore_task_start.c + CMN_CSRCS += tricore_pthread_start.c + CMN_CSRCS += tricore_signal_dispatch.c + + ifeq ($(CONFIG_BUILD_PROTECTED),y) + CMN_UASRCS += tricore_signal_handler.c + endif +endif + ifeq ($(CONFIG_ARCH_USE_MPU),y) CMN_CSRCS += tricore_mpu.c endif diff --git a/arch/tricore/src/common/tricore_allocateheap.c b/arch/tricore/src/common/tricore_allocateheap.c index bdc31d7270e..7431ed3e578 100644 --- a/arch/tricore/src/common/tricore_allocateheap.c +++ b/arch/tricore/src/common/tricore_allocateheap.c @@ -25,6 +25,7 @@ ****************************************************************************/ #include <nuttx/config.h> +#include <nuttx/userspace.h> #include <sys/types.h> #include <nuttx/debug.h> @@ -57,17 +58,74 @@ * Description: * This function will be called to dynamically set aside the heap region. * - * For the kernel build (CONFIG_BUILD_KERNEL=y) with both kernel- and - * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function provides the - * size of the unprotected, user-space heap. + * - For the normal "flat" build, this function returns the size of the + * single heap. + * - For the protected build (CONFIG_BUILD_PROTECTED=y) with both kernel- + * and user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function + * provides the size of the user-space heap. * - * If a protected kernel-space heap is provided, the kernel heap must be - * allocated (and protected) by an analogous up_allocate_kheap(). + ****************************************************************************/ + +void weak_function up_allocate_heap(void **heap_start, size_t *heap_size) +{ +#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP) + + /* Get the unaligned size and position of the user-space heap. + * This heap begins after the user-space .bss section. + */ + + uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend; + uintptr_t utop = (uintptr_t)USERSPACE->us_heapend; + + DEBUGASSERT(ubase <= utop); + + /* Return the user-space heap settings */ + + *heap_start = (void *)ubase; + *heap_size = utop - ubase; +#else + + /* Return the heap settings */ + + *heap_start = _sheap; + *heap_size = (uintptr_t)_eheap - (uintptr_t)_sheap; + +#if defined(CONFIG_MM_KERNEL_HEAP) + *heap_start += CONFIG_MM_KERNEL_HEAPSIZE; + *heap_size -= CONFIG_MM_KERNEL_HEAPSIZE; +#endif + +#endif +} + +/**************************************************************************** + * Name: up_allocate_kheap + * + * Description: + * For the kernel build (CONFIG_BUILD_PROTECTED/KERNEL=y) with both kernel- + * and user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function allocates + * the kernel-space heap. * ****************************************************************************/ -void up_allocate_heap(void **heap_start, size_t *heap_size) +#if defined(CONFIG_MM_KERNEL_HEAP) +void weak_function up_allocate_kheap(void **heap_start, size_t *heap_size) { + /* Get the unaligned size and position of the kernel-space heap. + * This heap begins after the kernel-space idle stack. + */ + +#if defined(CONFIG_BUILD_PROTECTED) + DEBUGASSERT((uintptr_t)_sheap + CONFIG_MM_KERNEL_HEAPSIZE + <= (uintptr_t)_eheap); + + /* Return the kernel heap settings */ + + *heap_start = _sheap; + *heap_size = (uintptr_t)_eheap - (uintptr_t)_sheap; +#else *heap_start = _sheap; - *heap_size = (size_t)((uintptr_t)_eheap - (uintptr_t)_sheap); + *heap_size = CONFIG_MM_KERNEL_HEAPSIZE; +#endif } +#endif diff --git a/arch/tricore/src/common/tricore_initialstate.c b/arch/tricore/src/common/tricore_initialstate.c index 3c0baf997f2..05ac76ca960 100644 --- a/arch/tricore/src/common/tricore_initialstate.c +++ b/arch/tricore/src/common/tricore_initialstate.c @@ -94,5 +94,5 @@ void up_initial_state(struct tcb_s *tcb) xcp->regs = tricore_alloc_csa((uintptr_t)tcb->start, (uintptr_t)tcb->stack_base_ptr + tcb->adj_stack_size, - PSW_IO_SUPERVISOR | PSW_CDE, false); + PSW_IO_SUPERVISOR | PSW_IS | PSW_CDE, false); } diff --git a/arch/tricore/src/common/tricore_internal.h b/arch/tricore/src/common/tricore_internal.h index 09527eb34b7..91017e3e935 100644 --- a/arch/tricore/src/common/tricore_internal.h +++ b/arch/tricore/src/common/tricore_internal.h @@ -242,6 +242,10 @@ void tricore_fpuinit(void); int tricore_init_dbgmonitor(void); #endif +#ifdef CONFIG_BUILD_PROTECTED +void tricore_userspace(void); +#endif + /* System Timer *************************************************************/ struct oneshot_lowerhalf_s * diff --git a/arch/tricore/src/common/tricore_main.c b/arch/tricore/src/common/tricore_main.c index 151f91f28c9..b9ec352dde3 100644 --- a/arch/tricore/src/common/tricore_main.c +++ b/arch/tricore/src/common/tricore_main.c @@ -59,6 +59,9 @@ static void core_main(void) if (IfxCpu_getCoreIndex() == 0) { tricore_earlyserialinit(); +#ifdef CONFIG_BUILD_PROTECTED + tricore_userspace(); +#endif nx_start(); } diff --git a/arch/tricore/src/common/tricore_allocateheap.c b/arch/tricore/src/common/tricore_pthread_start.c similarity index 52% copy from arch/tricore/src/common/tricore_allocateheap.c copy to arch/tricore/src/common/tricore_pthread_start.c index bdc31d7270e..6d1f09b4db0 100644 --- a/arch/tricore/src/common/tricore_allocateheap.c +++ b/arch/tricore/src/common/tricore_pthread_start.c @@ -1,7 +1,5 @@ /**************************************************************************** - * arch/tricore/src/common/tricore_allocateheap.c - * - * SPDX-License-Identifier: Apache-2.0 + * arch/tricore/src/common/tricore_pthread_start.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -25,49 +23,56 @@ ****************************************************************************/ #include <nuttx/config.h> - -#include <sys/types.h> -#include <nuttx/debug.h> - +#include <pthread.h> #include <nuttx/arch.h> -#include <nuttx/board.h> -#include <arch/board/board.h> -#include "tricore_internal.h" +#include <assert.h> -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ +#include <arch/syscall.h> -/**************************************************************************** - * Private Data - ****************************************************************************/ +#include "tricore_internal.h" -/**************************************************************************** - * Private Functions - ****************************************************************************/ +#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__) && \ + !defined(CONFIG_DISABLE_PTHREAD) /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: up_allocate_heap + * Name: up_pthread_start * * Description: - * This function will be called to dynamically set aside the heap region. + * In this kernel mode build, this function will be called to execute a + * pthread in user-space. When the pthread is first started, a kernel-mode + * stub will first run to perform some housekeeping functions. This + * kernel-mode stub will then be called transfer control to the user-mode + * pthread. + * + * Normally the a user-mode start-up stub will also execute before the + * pthread actually starts. See libc/pthread/pthread_create.c * - * For the kernel build (CONFIG_BUILD_KERNEL=y) with both kernel- and - * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function provides the - * size of the unprotected, user-space heap. + * Input Parameters: + * startup - The user-space pthread startup function + * entrypt - The user-space address of the pthread entry point + * arg - Standard argument for the pthread entry point * - * If a protected kernel-space heap is provided, the kernel heap must be - * allocated (and protected) by an analogous up_allocate_kheap(). + * Returned Value: + * This function should not return. It should call the user-mode start-up + * stub and that stub should call pthread_exit if/when the user pthread + * terminates. * ****************************************************************************/ -void up_allocate_heap(void **heap_start, size_t *heap_size) +void up_pthread_start(pthread_trampoline_t startup, + pthread_startroutine_t entrypt, pthread_addr_t arg) { - *heap_start = _sheap; - *heap_size = (size_t)((uintptr_t)_eheap - (uintptr_t)_sheap); + /* Let sys_call3() do all of the work */ + + sys_call3(SYS_pthread_start, (uintptr_t)startup, (uintptr_t)entrypt, + (uintptr_t)arg); + + PANIC(); } + +#endif /* !CONFIG_BUILD_FLAT && __KERNEL__ && !CONFIG_DISABLE_PTHREAD */ diff --git a/arch/tricore/src/common/tricore_schedulesigaction.c b/arch/tricore/src/common/tricore_schedulesigaction.c index 19123713187..ad1e46cbd53 100644 --- a/arch/tricore/src/common/tricore_schedulesigaction.c +++ b/arch/tricore/src/common/tricore_schedulesigaction.c @@ -94,5 +94,6 @@ void up_schedule_sigaction(struct tcb_s *tcb) tcb->xcp.regs = tricore_alloc_csa((uintptr_t)tricore_sigdeliver, STACKFRAME_ALIGN_DOWN (up_getusrsp(tcb->xcp.regs)), - PSW_IO_SUPERVISOR | PSW_CDE, true); + PSW_IO_SUPERVISOR | PSW_IS | PSW_CDE, + true); } diff --git a/arch/tricore/src/common/tricore_signal_dispatch.c b/arch/tricore/src/common/tricore_signal_dispatch.c new file mode 100644 index 00000000000..94d99a7b860 --- /dev/null +++ b/arch/tricore/src/common/tricore_signal_dispatch.c @@ -0,0 +1,76 @@ +/**************************************************************************** + * arch/tricore/src/common/tricore_signal_dispatch.c + * + * 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 <arch/syscall.h> + +#include "tricore_internal.h" + +#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__) + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_signal_dispatch + * + * Description: + * In the kernel mode build, this function will be called to execute a + * a signal handler in user-space. When the signal is delivered, a + * kernel-mode stub will first run to perform some housekeeping functions. + * This kernel-mode stub will then be called transfer control to the user + * mode signal handler by calling this function. + * + * Normally the user-mode signaling handling stub will also execute + * before the ultimate signal handler is called. See + * arch/tricore/src/common/tricore_signal_handler.c. This function is the + * user-space, signal handler trampoline function. It is called from + * up_signal_dispatch() in user-mode. + * + * Input Parameters: + * sighand - The address user-space signal handling function + * signo, info, and ucontext - Standard arguments to be passed to the + * signal handling function. + * + * Returned Value: + * None. This function does not return in the normal sense. It returns + * via an architecture specific system call made by up_signal_handler(). + * However, this will look like a normal return by the caller of + * up_signal_dispatch. + * + ****************************************************************************/ + +void up_signal_dispatch(_sa_sigaction_t sighand, int signo, + siginfo_t *info, void *ucontext) +{ + /* Let sys_call4() do all of the work */ + + sys_call4(SYS_signal_handler, (uintptr_t)sighand, (uintptr_t)signo, + (uintptr_t)info, (uintptr_t)ucontext); +} + +#endif /* !CONFIG_BUILD_FLAT && __KERNEL__ */ diff --git a/arch/tricore/src/common/tricore_allocateheap.c b/arch/tricore/src/common/tricore_signal_handler.c similarity index 60% copy from arch/tricore/src/common/tricore_allocateheap.c copy to arch/tricore/src/common/tricore_signal_handler.c index bdc31d7270e..2bc323593f5 100644 --- a/arch/tricore/src/common/tricore_allocateheap.c +++ b/arch/tricore/src/common/tricore_signal_handler.c @@ -1,7 +1,5 @@ /**************************************************************************** - * arch/tricore/src/common/tricore_allocateheap.c - * - * SPDX-License-Identifier: Apache-2.0 + * arch/tricore/src/common/tricore_signal_handler.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -26,22 +24,12 @@ #include <nuttx/config.h> -#include <sys/types.h> -#include <nuttx/debug.h> - +#include <arch/syscall.h> #include <nuttx/arch.h> -#include <nuttx/board.h> -#include <arch/board/board.h> #include "tricore_internal.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ +#if defined(CONFIG_BUILD_PROTECTED) /**************************************************************************** * Private Functions @@ -52,22 +40,28 @@ ****************************************************************************/ /**************************************************************************** - * Name: up_allocate_heap + * Name: up_signal_handler * * Description: - * This function will be called to dynamically set aside the heap region. + * This function is the user-space, signal handler trampoline function. It + * is called from up_signal_dispatch() in user-mode. * - * For the kernel build (CONFIG_BUILD_KERNEL=y) with both kernel- and - * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function provides the - * size of the unprotected, user-space heap. + * Input Parameters: + * sighand - The address user-space signal handling function + * signo, info, and ucontext - Standard arguments to be passed to the + * signal handling function. * - * If a protected kernel-space heap is provided, the kernel heap must be - * allocated (and protected) by an analogous up_allocate_kheap(). + * Returned Value: + * None. This function does not return in the normal sense. It returns + * via the SYS_signal_handler_return (see syscall.h) * ****************************************************************************/ -void up_allocate_heap(void **heap_start, size_t *heap_size) +void up_signal_handler(_sa_sigaction_t sighand, int signo, + siginfo_t *info, void *ucontext) { - *heap_start = _sheap; - *heap_size = (size_t)((uintptr_t)_eheap - (uintptr_t)_sheap); + sighand(signo, info, ucontext); + sys_call0(SYS_signal_handler_return); + PANIC(); } +#endif diff --git a/arch/tricore/src/common/tricore_svcall.c b/arch/tricore/src/common/tricore_svcall.c index 6f062f0dd29..7bf21d20e85 100644 --- a/arch/tricore/src/common/tricore_svcall.c +++ b/arch/tricore/src/common/tricore_svcall.c @@ -29,7 +29,9 @@ #include <stdint.h> #include <string.h> #include <assert.h> +#include <errno.h> #include <nuttx/debug.h> +#include <nuttx/userspace.h> #include <syscall.h> #include <arch/barriers.h> @@ -39,6 +41,17 @@ #include "tricore_internal.h" +/**************************************************************************** + * Private Types + ****************************************************************************/ + +#ifdef CONFIG_LIB_SYSCALL +typedef uintptr_t (*syscall_stub_t)(int nbr, + uintptr_t parm1, uintptr_t parm2, + uintptr_t parm3, uintptr_t parm4, + uintptr_t parm5, uintptr_t parm6); +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -94,8 +107,204 @@ void tricore_svcall(volatile void *trap) UP_ISB(); break; + /* REG_D8 = SYS_task_start: This a user task start + * + * void up_task_start(main_t taskentry, int argc, char *argv[]) + * noreturn_function; + * + * At this point, the following values are saved in context: + * + * REG_D8 = SYS_task_start + * REG_D9 = taskentry + * REG_D10 = argc + * REG_D11 = argv + */ + +#ifdef CONFIG_BUILD_PROTECTED + case SYS_task_start: + { + /* Set up to return to the user-space _start function in + * unprivileged mode. We need: + * + * PC = task_startup + * PSW = user mode + * A4 = taskentry + * D4 = argc + * A4 = argv + */ + + /* Updata PC */ + + plregs[REG_LPC] = (uintptr_t)USERSPACE->task_startup; + + /* Updata function args */ + + plregs[REG_A4] = puregs[REG_D9]; + plregs[REG_D4] = puregs[REG_D10]; + plregs[REG_A5] = puregs[REG_D11]; + + /* Return unprivileged mode */ + + puregs[REG_PSW] = + (puregs[REG_PSW] & ~PSW_MODE_MASK) | PSW_IO_USER0; + } + break; +#endif + + /* REG_D8 = SYS_pthread_start: This a user pthread start + * + * void up_pthread_start(pthread_startroutine_t entrypt, + * pthread_addr_t arg) noreturn_function; + * + * At this point, the following values are saved in context: + * + * REG_D8 = SYS_pthread_start + * REG_D9 = startup (trampoline) + * REG_D10 = entrypt + * REG_D11 = arg + */ + +#if !defined(CONFIG_BUILD_FLAT) && !defined(CONFIG_DISABLE_PTHREAD) + case SYS_pthread_start: + { + /* Set up to return to the user-space pthread start-up function in + * unprivileged mode. + * + * PC = startup + * PSW = user mode + * A4 = entrypt + * A5 = arg + */ + + /* Updata PC */ + + plregs[REG_LPC] = puregs[REG_D9]; + + /* Updata function args */ + + plregs[REG_A4] = puregs[REG_D10]; + plregs[REG_A5] = puregs[REG_D11]; + + /* Return unprivileged mode */ + + puregs[REG_PSW] = + (puregs[REG_PSW] & ~PSW_MODE_MASK) | PSW_IO_USER0; + } + break; +#endif + + /* REG_D8 = SYS_signal_handler: This a user signal handler callback + * + * void signal_handler(_sa_sigaction_t sighand, int signo, + * siginfo_t *info, void *ucontext); + * + * At this point, the following values are saved in context: + * + * D8 = SYS_signal_handler + * D9 = sighand + * D10 = signo + * D11 = info + * D12 = ucontext + */ + +#ifdef CONFIG_BUILD_PROTECTED + case SYS_signal_handler: + { + struct tcb_s *rtcb = this_task(); + + /* Remember the caller's return address */ + + DEBUGASSERT(rtcb->xcp.sigreturn == 0); + rtcb->xcp.sigreturn = plregs[REG_LPC]; + + /* Set up to return to the user-space trampoline function in + * unprivileged mode. + */ + + plregs[REG_LPC] = (uintptr_t)USERSPACE->signal_handler; + + /* Change the parameter ordering to match the expectation of struct + * userpace_s signal_handler. + */ + + plregs[REG_A4] = puregs[REG_D9]; /* sighand */ + plregs[REG_D4] = puregs[REG_D10]; /* signal */ + plregs[REG_A5] = puregs[REG_D11]; /* info */ + plregs[REG_A6] = puregs[REG_D12]; /* ucontext */ + + /* Return unprivileged mode */ + + puregs[REG_PSW] = + (puregs[REG_PSW] & ~PSW_MODE_MASK) | PSW_IO_USER0; + } + break; +#endif + + /* REG_D8 = SYS_signal_handler_return: This a user signal handler + * callback + */ + +#ifdef CONFIG_BUILD_PROTECTED + case SYS_signal_handler_return: + { + struct tcb_s *rtcb = this_task(); + + /* Set up to return to the kernel-mode signal dispatching logic. */ + + DEBUGASSERT(rtcb->xcp.sigreturn != 0); + + plregs[REG_LPC] = rtcb->xcp.sigreturn; + + rtcb->xcp.sigreturn = 0; + + /* Return privileged mode */ + + puregs[REG_PSW] = + (puregs[REG_PSW] & ~PSW_MODE_MASK) | PSW_IO_SUPERVISOR; + } + break; +#endif + default: - svcerr("ERROR: Bad SYS call: %d\n", (int)puregs[REG_D8]); + { +#ifdef CONFIG_LIB_SYSCALL + uintptr_t ret; + int nbr = cmd - CONFIG_SYS_RESERVED; + struct tcb_s *rtcb = nxsched_self(); + syscall_stub_t stub; + + if (cmd < CONFIG_SYS_RESERVED || cmd >= SYS_maxsyscall || + rtcb->xcp.nsyscalls >= CONFIG_SYS_NNEST) + { + puregs[REG_D8] = -ENOSYS; + break; + } + + stub = (syscall_stub_t)g_stublookup[nbr]; + + /* Setup nested syscall */ + + rtcb->xcp.nsyscalls += 1; + + up_set_interrupt_context(false); + + /* Call syscall function */ + + ret = stub(nbr, puregs[REG_D9], puregs[REG_D10], + puregs[REG_D11], puregs[REG_D12], + puregs[REG_D13], puregs[REG_D14]); + + /* Setup return from nested syscall */ + + rtcb->xcp.nsyscalls -= 1; + + /* Assign the return value to the D8 register. */ + + puregs[REG_D8] = ret; +#else + svcerr("ERROR: Bad SYS call: %d\n", (int)puregs[REG_D8]); +#endif + } break; } diff --git a/arch/tricore/src/common/tricore_allocateheap.c b/arch/tricore/src/common/tricore_task_start.c similarity index 54% copy from arch/tricore/src/common/tricore_allocateheap.c copy to arch/tricore/src/common/tricore_task_start.c index bdc31d7270e..fc4a930be78 100644 --- a/arch/tricore/src/common/tricore_allocateheap.c +++ b/arch/tricore/src/common/tricore_task_start.c @@ -1,7 +1,5 @@ /**************************************************************************** - * arch/tricore/src/common/tricore_allocateheap.c - * - * SPDX-License-Identifier: Apache-2.0 + * arch/tricore/src/common/tricore_task_start.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -25,49 +23,53 @@ ****************************************************************************/ #include <nuttx/config.h> - -#include <sys/types.h> -#include <nuttx/debug.h> - #include <nuttx/arch.h> -#include <nuttx/board.h> -#include <arch/board/board.h> -#include "tricore_internal.h" +#include <assert.h> -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ +#include <arch/syscall.h> -/**************************************************************************** - * Private Data - ****************************************************************************/ +#include "tricore_internal.h" -/**************************************************************************** - * Private Functions - ****************************************************************************/ +#ifndef CONFIG_BUILD_FLAT /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: up_allocate_heap + * Name: up_task_start * * Description: - * This function will be called to dynamically set aside the heap region. + * In this kernel mode build, this function will be called to execute a + * task in user-space. When the task is first started, a kernel-mode + * stub will first run to perform some housekeeping functions. This + * kernel-mode stub will then be called transfer control to the user-mode + * task. + * + * Normally the a user-mode start-up stub will also execute before the + * task actually starts. See libc/sched/task_startup.c * - * For the kernel build (CONFIG_BUILD_KERNEL=y) with both kernel- and - * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function provides the - * size of the unprotected, user-space heap. + * Input Parameters: + * taskentry - The user-space entry point of the task. + * argc - The number of parameters being passed. + * argv - The parameters being passed. These lie in kernel-space memory + * and will have to be reallocated in user-space memory. * - * If a protected kernel-space heap is provided, the kernel heap must be - * allocated (and protected) by an analogous up_allocate_kheap(). + * Returned Value: + * This function should not return. It should call the user-mode start-up + * stub and that stub should call exit if/when the user task terminates. * ****************************************************************************/ -void up_allocate_heap(void **heap_start, size_t *heap_size) +void up_task_start(main_t taskentry, int argc, char *argv[]) { - *heap_start = _sheap; - *heap_size = (size_t)((uintptr_t)_eheap - (uintptr_t)_sheap); + /* Let sys_call3() do all of the work */ + + sys_call3(SYS_task_start, (uintptr_t)taskentry, (uintptr_t)argc, + (uintptr_t)argv); + + PANIC(); } + +#endif /* !CONFIG_BUILD_FLAT */ diff --git a/arch/tricore/src/tc3xx/Make.defs b/arch/tricore/src/tc3xx/Make.defs index 7de7bc00fd2..b87153a52de 100644 --- a/arch/tricore/src/tc3xx/Make.defs +++ b/arch/tricore/src/tc3xx/Make.defs @@ -25,6 +25,11 @@ CHIP_CSRCS += tc3xx_serial.c CHIP_CSRCS += tc3xx_wdt.c CHIP_CSRCS += tc3xx_clockconfig.c +ifeq ($(CONFIG_BUILD_PROTECTED),y) + CHIP_UCSRCS += tc397_userspace.c + VPATH += $(BOARD_DIR)/kernel +endif + VPATH += tc3xx ILLD_UNPACK = illd/tc3xx diff --git a/arch/tricore/src/tc4xx/Make.defs b/arch/tricore/src/tc4xx/Make.defs index ae07146faf4..9bb6e47d2e4 100644 --- a/arch/tricore/src/tc4xx/Make.defs +++ b/arch/tricore/src/tc4xx/Make.defs @@ -25,6 +25,11 @@ CHIP_CSRCS += tc4xx_serial.c CHIP_CSRCS += tc4xx_wdt.c CHIP_CSRCS += tc4xx_clockconfig.c +ifeq ($(CONFIG_BUILD_PROTECTED),y) + CHIP_UCSRCS += tc4d9_userspace.c + VPATH += $(BOARD_DIR)/kernel +endif + VPATH += tc4xx ILLD_UNPACK = illd/tc4xx diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt b/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt index 2a4959d2538..5864dba4ba2 100644 --- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt +++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt @@ -21,3 +21,7 @@ # ############################################################################## add_subdirectory(src) + +if(CONFIG_BUILD_PROTECTED) + add_subdirectory(kernel) +endif() diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt b/boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/CMakeLists.txt similarity index 89% copy from boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt copy to boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/CMakeLists.txt index 2a4959d2538..3a8826cddd8 100644 --- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt +++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/CMakeLists.txt @@ -1,5 +1,5 @@ # ############################################################################## -# boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt +# boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/CMakeLists.txt # # SPDX-License-Identifier: Apache-2.0 # @@ -20,4 +20,4 @@ # # ############################################################################## -add_subdirectory(src) +target_sources(nuttx_user PRIVATE tc397_userspace.c) diff --git a/arch/tricore/src/common/tricore_allocateheap.c b/boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/tc397_userspace.c similarity index 56% copy from arch/tricore/src/common/tricore_allocateheap.c copy to boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/tc397_userspace.c index bdc31d7270e..f34646b03d2 100644 --- a/arch/tricore/src/common/tricore_allocateheap.c +++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/tc397_userspace.c @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/tricore/src/common/tricore_allocateheap.c + * boards/tricore/tc3xx/a2g-tc397-5v-tft/kernel/tc397_userspace.c * * SPDX-License-Identifier: Apache-2.0 * @@ -26,48 +26,65 @@ #include <nuttx/config.h> -#include <sys/types.h> -#include <nuttx/debug.h> - #include <nuttx/arch.h> -#include <nuttx/board.h> -#include <arch/board/board.h> +#include <nuttx/mm/mm.h> +#include <nuttx/userspace.h> +#include <nuttx/wqueue.h> -#include "tricore_internal.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ +#if defined(CONFIG_BUILD_PROTECTED) && !defined(__KERNEL__) /**************************************************************************** * Private Data ****************************************************************************/ -/**************************************************************************** - * Private Functions - ****************************************************************************/ +static struct userspace_data_s g_userspace_data = +{ + .us_heap = &g_mmheap, +}; /**************************************************************************** - * Public Functions + * Public Data ****************************************************************************/ +extern uint8_t _stext[]; +extern uint8_t _etext[]; +extern const uint8_t _eronly[]; +extern uint8_t _sdata[]; +extern uint8_t _edata[]; +extern uint8_t _sbss[]; +extern uint8_t _ebss[]; +extern uint8_t _eheap[]; + +const struct userspace_s userspace locate_data(".userspace") = +{ + .us_entrypoint = CONFIG_INIT_ENTRYPOINT, + .us_textstart = (uintptr_t)_stext, + .us_textend = (uintptr_t)_etext, + .us_datasource = (uintptr_t)_eronly, + .us_datastart = (uintptr_t)_sdata, + .us_dataend = (uintptr_t)_edata, + .us_bssstart = (uintptr_t)_sbss, + .us_bssend = (uintptr_t)_ebss, + .us_heapend = (uintptr_t)_eheap, + .us_data = &g_userspace_data, + .task_startup = nxtask_startup, + +#ifdef CONFIG_ENABLE_ALL_SIGNALS + .signal_handler = up_signal_handler, +#endif + +#ifdef CONFIG_LIBC_USRWORK + .work_usrstart = work_usrstart, +#endif + +#ifdef CONFIG_BUILTIN + .builtin_count = &g_builtin_count, + .builtins = &g_builtins[0], +#endif +}; + /**************************************************************************** - * Name: up_allocate_heap - * - * Description: - * This function will be called to dynamically set aside the heap region. - * - * For the kernel build (CONFIG_BUILD_KERNEL=y) with both kernel- and - * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function provides the - * size of the unprotected, user-space heap. - * - * If a protected kernel-space heap is provided, the kernel heap must be - * allocated (and protected) by an analogous up_allocate_kheap(). - * + * Public Functions ****************************************************************************/ -void up_allocate_heap(void **heap_start, size_t *heap_size) -{ - *heap_start = _sheap; - *heap_size = (size_t)((uintptr_t)_eheap - (uintptr_t)_sheap); -} +#endif /* CONFIG_BUILD_PROTECTED && !__KERNEL__ */ diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/Lcf_Tasking_Tricore_Tc.lsl.user b/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/Lcf_Tasking_Tricore_Tc.lsl.user index 2af6a37341f..1a31cc368b7 100644 --- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/Lcf_Tasking_Tricore_Tc.lsl.user +++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/Lcf_Tasking_Tricore_Tc.lsl.user @@ -184,6 +184,6 @@ derivative tc39 select "(.zdata|.zdata.*)"; } "_sdata_pflash" = "_lc_gb_udata_rom"; - #include "symbol_share.ld" + "_eronly" = "_lc_gb_udata_rom"; } } diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/CMakeLists.txt b/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/CMakeLists.txt index 358b5f5d6c2..21979341ac9 100644 --- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/CMakeLists.txt +++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/CMakeLists.txt @@ -35,6 +35,7 @@ set_property( PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/Lcf_${toolchain}_Tricore_Tc.lsl.kernel") if(CONFIG_BUILD_PROTECTED) + target_sources(board PRIVATE tc397_userspace.c) set_property( GLOBAL PROPERTY LD_SCRIPT_USER diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/Makefile b/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/Makefile index d642cb5e845..e9d6f8cae86 100644 --- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/Makefile +++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/Makefile @@ -24,4 +24,8 @@ include $(TOPDIR)/Make.defs CSRCS = tc397_boardinit.c tc397_bringup.c +ifeq ($(CONFIG_BUILD_PROTECTED),y) + CSRCS += tc397_userspace.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/arch/tricore/src/common/tricore_main.c b/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/tc397_userspace.c similarity index 55% copy from arch/tricore/src/common/tricore_main.c copy to boards/tricore/tc3xx/a2g-tc397-5v-tft/src/tc397_userspace.c index 151f91f28c9..2297fae4883 100644 --- a/arch/tricore/src/common/tricore_main.c +++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/src/tc397_userspace.c @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/tricore/src/common/tricore_main.c + * boards/tricore/tc3xx/a2g-tc397-5v-tft/src/tc397_userspace.c * * SPDX-License-Identifier: Apache-2.0 * @@ -24,89 +24,56 @@ * Included Files ****************************************************************************/ -#include <tricore_internal.h> -#include <nuttx/init.h> +#include <nuttx/config.h> -#include "Ifx_Types.h" -#include "IfxCpu.h" +#include <stdint.h> +#include <assert.h> +#include <nuttx/userspace.h> + +#include "tricore_internal.h" /**************************************************************************** - * Private Functions + * Public Functions ****************************************************************************/ -static void core_main(void) -{ - static IfxCpu_syncEvent g_sync_event = 0; - -#ifdef CONFIG_ARCH_HAVE_FPU - tricore_fpuinit(); -#endif - - /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!! - * Enable the watchdogs and service them periodically if it is required - */ - - /* Wait for CPU sync event */ - - IfxCpu_emitEvent(&g_sync_event); - -#if defined(CONFIG_ARCH_CHIP_TC3XX) - IfxCpu_waitEvent(&g_sync_event, 1); -#else - IfxCpu_waitEvent(&g_sync_event, UINT32_MAX, 1); -#endif - - if (IfxCpu_getCoreIndex() == 0) - { - tricore_earlyserialinit(); - nx_start(); - } - - while (1); -} - /**************************************************************************** - * Public Functions + * Name: tricore_userspace + * + * Description: + * Initialize the user-space .data and .bss sections for the protected + * build. + * ****************************************************************************/ -void core0_main(void) +void tricore_userspace(void) { - /* All WDTs except WDTCPU0 and system watchdog WDTSYS are in - * disabled mode after Boot-FW execution. Disable the watchdog - * to ensure the normal startup of the system. - */ + uint8_t *src; + uint8_t *dest; + uint8_t *end; - tricore_wdt_disable(); + DEBUGASSERT(USERSPACE->us_bssstart != 0 && + USERSPACE->us_bssend != 0 && + USERSPACE->us_bssstart <= USERSPACE->us_bssend); - core_main(); -} + dest = (uint8_t *)USERSPACE->us_bssstart; + end = (uint8_t *)USERSPACE->us_bssend; -void core1_main(void) -{ - core_main(); -} - -void core2_main(void) -{ - core_main(); -} - -void core3_main(void) -{ - core_main(); -} + while (dest != end) + { + *dest++ = 0; + } -void core4_main(void) -{ - core_main(); -} + DEBUGASSERT(USERSPACE->us_datasource != 0 && + USERSPACE->us_datastart != 0 && + USERSPACE->us_dataend != 0 && + USERSPACE->us_datastart <= USERSPACE->us_dataend); -void core5_main(void) -{ - core_main(); -} + src = (uint8_t *)USERSPACE->us_datasource; + dest = (uint8_t *)USERSPACE->us_datastart; + end = (uint8_t *)USERSPACE->us_dataend; -void core6_main(void) -{ - core_main(); + while (dest != end) + { + *dest++ = *src++; + } } diff --git a/boards/tricore/tc4xx/triboard_tc4x9_com/CMakeLists.txt b/boards/tricore/tc4xx/triboard_tc4x9_com/CMakeLists.txt index 3c85e105954..219520b382c 100644 --- a/boards/tricore/tc4xx/triboard_tc4x9_com/CMakeLists.txt +++ b/boards/tricore/tc4xx/triboard_tc4x9_com/CMakeLists.txt @@ -21,3 +21,7 @@ # ############################################################################## add_subdirectory(src) + +if(CONFIG_BUILD_PROTECTED) + add_subdirectory(kernel) +endif() diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt b/boards/tricore/tc4xx/triboard_tc4x9_com/kernel/CMakeLists.txt similarity index 89% copy from boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt copy to boards/tricore/tc4xx/triboard_tc4x9_com/kernel/CMakeLists.txt index 2a4959d2538..f23c48dfeab 100644 --- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt +++ b/boards/tricore/tc4xx/triboard_tc4x9_com/kernel/CMakeLists.txt @@ -1,5 +1,5 @@ # ############################################################################## -# boards/tricore/tc3xx/a2g-tc397-5v-tft/CMakeLists.txt +# boards/tricore/tc4xx/triboard_tc4x9_com/kernel/CMakeLists.txt # # SPDX-License-Identifier: Apache-2.0 # @@ -20,4 +20,4 @@ # # ############################################################################## -add_subdirectory(src) +target_sources(nuttx_user PRIVATE tc4d9_userspace.c) diff --git a/arch/tricore/src/common/tricore_allocateheap.c b/boards/tricore/tc4xx/triboard_tc4x9_com/kernel/tc4d9_userspace.c similarity index 56% copy from arch/tricore/src/common/tricore_allocateheap.c copy to boards/tricore/tc4xx/triboard_tc4x9_com/kernel/tc4d9_userspace.c index bdc31d7270e..36b8bf450eb 100644 --- a/arch/tricore/src/common/tricore_allocateheap.c +++ b/boards/tricore/tc4xx/triboard_tc4x9_com/kernel/tc4d9_userspace.c @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/tricore/src/common/tricore_allocateheap.c + * boards/tricore/tc4xx/triboard_tc4x9_com/kernel/tc4d9_userspace.c * * SPDX-License-Identifier: Apache-2.0 * @@ -26,48 +26,65 @@ #include <nuttx/config.h> -#include <sys/types.h> -#include <nuttx/debug.h> - #include <nuttx/arch.h> -#include <nuttx/board.h> -#include <arch/board/board.h> +#include <nuttx/mm/mm.h> +#include <nuttx/userspace.h> +#include <nuttx/wqueue.h> -#include "tricore_internal.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ +#if defined(CONFIG_BUILD_PROTECTED) && !defined(__KERNEL__) /**************************************************************************** * Private Data ****************************************************************************/ -/**************************************************************************** - * Private Functions - ****************************************************************************/ +static struct userspace_data_s g_userspace_data = +{ + .us_heap = &g_mmheap, +}; /**************************************************************************** - * Public Functions + * Public Data ****************************************************************************/ +extern uint8_t _stext[]; +extern uint8_t _etext[]; +extern const uint8_t _eronly[]; +extern uint8_t _sdata[]; +extern uint8_t _edata[]; +extern uint8_t _sbss[]; +extern uint8_t _ebss[]; +extern uint8_t _eheap[]; + +const struct userspace_s userspace locate_data(".userspace") = +{ + .us_entrypoint = CONFIG_INIT_ENTRYPOINT, + .us_textstart = (uintptr_t)_stext, + .us_textend = (uintptr_t)_etext, + .us_datasource = (uintptr_t)_eronly, + .us_datastart = (uintptr_t)_sdata, + .us_dataend = (uintptr_t)_edata, + .us_bssstart = (uintptr_t)_sbss, + .us_bssend = (uintptr_t)_ebss, + .us_heapend = (uintptr_t)_eheap, + .us_data = &g_userspace_data, + .task_startup = nxtask_startup, + +#ifdef CONFIG_ENABLE_ALL_SIGNALS + .signal_handler = up_signal_handler, +#endif + +#ifdef CONFIG_LIBC_USRWORK + .work_usrstart = work_usrstart, +#endif + +#ifdef CONFIG_BUILTIN + .builtin_count = &g_builtin_count, + .builtins = &g_builtins[0], +#endif +}; + /**************************************************************************** - * Name: up_allocate_heap - * - * Description: - * This function will be called to dynamically set aside the heap region. - * - * For the kernel build (CONFIG_BUILD_KERNEL=y) with both kernel- and - * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function provides the - * size of the unprotected, user-space heap. - * - * If a protected kernel-space heap is provided, the kernel heap must be - * allocated (and protected) by an analogous up_allocate_kheap(). - * + * Public Functions ****************************************************************************/ -void up_allocate_heap(void **heap_start, size_t *heap_size) -{ - *heap_start = _sheap; - *heap_size = (size_t)((uintptr_t)_eheap - (uintptr_t)_sheap); -} +#endif /* CONFIG_BUILD_PROTECTED && !__KERNEL__ */ diff --git a/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/Lcf_Tasking_Tricore_Tc.lsl.user b/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/Lcf_Tasking_Tricore_Tc.lsl.user index 178dc8cc558..7ae23ffce8c 100644 --- a/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/Lcf_Tasking_Tricore_Tc.lsl.user +++ b/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/Lcf_Tasking_Tricore_Tc.lsl.user @@ -185,6 +185,7 @@ derivative tc4D select "(.zdata|.zdata.*)"; } "_sdata_pflash" = "_lc_gb_udata_rom"; + "_eronly" = "_lc_gb_udata_rom"; } #if CONFIG_CPU_COREID == 5 /* This sections is added to reserve last 4 bytes of flash in core5's user diff --git a/boards/tricore/tc4xx/triboard_tc4x9_com/src/CMakeLists.txt b/boards/tricore/tc4xx/triboard_tc4x9_com/src/CMakeLists.txt index cea6bde4273..20e3c10b1e7 100644 --- a/boards/tricore/tc4xx/triboard_tc4x9_com/src/CMakeLists.txt +++ b/boards/tricore/tc4xx/triboard_tc4x9_com/src/CMakeLists.txt @@ -35,6 +35,7 @@ set_property( PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/Lcf_${toolchain}_Tricore_Tc.lsl.kernel") if(CONFIG_BUILD_PROTECTED) + target_sources(board PRIVATE tc4d9_userspace.c) set_property( GLOBAL PROPERTY LD_SCRIPT_USER diff --git a/boards/tricore/tc4xx/triboard_tc4x9_com/src/Makefile b/boards/tricore/tc4xx/triboard_tc4x9_com/src/Makefile index 039360cb944..655bb3cb111 100644 --- a/boards/tricore/tc4xx/triboard_tc4x9_com/src/Makefile +++ b/boards/tricore/tc4xx/triboard_tc4x9_com/src/Makefile @@ -24,4 +24,8 @@ include $(TOPDIR)/Make.defs CSRCS = tc4da_boardinit.c tc4da_bringup.c +ifeq ($(CONFIG_BUILD_PROTECTED),y) + CSRCS += tc4d9_userspace.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/arch/tricore/src/common/tricore_main.c b/boards/tricore/tc4xx/triboard_tc4x9_com/src/tc4d9_userspace.c similarity index 55% copy from arch/tricore/src/common/tricore_main.c copy to boards/tricore/tc4xx/triboard_tc4x9_com/src/tc4d9_userspace.c index 151f91f28c9..0ac90f19fd4 100644 --- a/arch/tricore/src/common/tricore_main.c +++ b/boards/tricore/tc4xx/triboard_tc4x9_com/src/tc4d9_userspace.c @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/tricore/src/common/tricore_main.c + * boards/tricore/tc4xx/triboard_tc4x9_com/src/tc4d9_userspace.c * * SPDX-License-Identifier: Apache-2.0 * @@ -24,89 +24,56 @@ * Included Files ****************************************************************************/ -#include <tricore_internal.h> -#include <nuttx/init.h> +#include <nuttx/config.h> -#include "Ifx_Types.h" -#include "IfxCpu.h" +#include <stdint.h> +#include <assert.h> +#include <nuttx/userspace.h> + +#include "tricore_internal.h" /**************************************************************************** - * Private Functions + * Public Functions ****************************************************************************/ -static void core_main(void) -{ - static IfxCpu_syncEvent g_sync_event = 0; - -#ifdef CONFIG_ARCH_HAVE_FPU - tricore_fpuinit(); -#endif - - /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!! - * Enable the watchdogs and service them periodically if it is required - */ - - /* Wait for CPU sync event */ - - IfxCpu_emitEvent(&g_sync_event); - -#if defined(CONFIG_ARCH_CHIP_TC3XX) - IfxCpu_waitEvent(&g_sync_event, 1); -#else - IfxCpu_waitEvent(&g_sync_event, UINT32_MAX, 1); -#endif - - if (IfxCpu_getCoreIndex() == 0) - { - tricore_earlyserialinit(); - nx_start(); - } - - while (1); -} - /**************************************************************************** - * Public Functions + * Name: tricore_userspace + * + * Description: + * Initialize the user-space .data and .bss sections for the protected + * build. + * ****************************************************************************/ -void core0_main(void) +void tricore_userspace(void) { - /* All WDTs except WDTCPU0 and system watchdog WDTSYS are in - * disabled mode after Boot-FW execution. Disable the watchdog - * to ensure the normal startup of the system. - */ + uint8_t *src; + uint8_t *dest; + uint8_t *end; - tricore_wdt_disable(); + DEBUGASSERT(USERSPACE->us_bssstart != 0 && + USERSPACE->us_bssend != 0 && + USERSPACE->us_bssstart <= USERSPACE->us_bssend); - core_main(); -} + dest = (uint8_t *)USERSPACE->us_bssstart; + end = (uint8_t *)USERSPACE->us_bssend; -void core1_main(void) -{ - core_main(); -} - -void core2_main(void) -{ - core_main(); -} - -void core3_main(void) -{ - core_main(); -} + while (dest != end) + { + *dest++ = 0; + } -void core4_main(void) -{ - core_main(); -} + DEBUGASSERT(USERSPACE->us_datasource != 0 && + USERSPACE->us_datastart != 0 && + USERSPACE->us_dataend != 0 && + USERSPACE->us_datastart <= USERSPACE->us_dataend); -void core5_main(void) -{ - core_main(); -} + src = (uint8_t *)USERSPACE->us_datasource; + dest = (uint8_t *)USERSPACE->us_datastart; + end = (uint8_t *)USERSPACE->us_dataend; -void core6_main(void) -{ - core_main(); + while (dest != end) + { + *dest++ = *src++; + } }
