From a080d9847029a9aab0c9327b5a67e6c86c8bc750 Mon Sep 17 00:00:00 2001 From: Kiritoy Date: Fri, 26 Nov 2021 22:37:38 +0800 Subject: [PATCH] Restore the file directory structure --- Ubiquitous/XiUOS/arch/arm/Makefile | 10 +- Ubiquitous/XiUOS/arch/arm/armv6m/Makefile | 3 - Ubiquitous/XiUOS/arch/arm/armv7m/Makefile | 3 - Ubiquitous/XiUOS/arch/arm/cortex-m0/Makefile | 2 +- .../arm/{armv6m => cortex-m0}/arm32_switch.c | 0 .../arch/arm/{armv6m => cortex-m0}/pendsv.S | 0 .../arch/arm/cortex-m0/prepare_ahwstack.c | 411 ++++++++++++++++++ .../XiUOS/arch/arm/cortex-m0/svc_handle.h | 58 +++ Ubiquitous/XiUOS/arch/arm/shared/Makefile | 2 +- .../arm/{armv7m => shared}/arm32_switch.c | 0 .../arch/arm/{armv7m => shared}/pendsv.S | 0 11 files changed, 474 insertions(+), 15 deletions(-) delete mode 100644 Ubiquitous/XiUOS/arch/arm/armv6m/Makefile delete mode 100644 Ubiquitous/XiUOS/arch/arm/armv7m/Makefile rename Ubiquitous/XiUOS/arch/arm/{armv6m => cortex-m0}/arm32_switch.c (100%) rename Ubiquitous/XiUOS/arch/arm/{armv6m => cortex-m0}/pendsv.S (100%) create mode 100644 Ubiquitous/XiUOS/arch/arm/cortex-m0/prepare_ahwstack.c create mode 100644 Ubiquitous/XiUOS/arch/arm/cortex-m0/svc_handle.h rename Ubiquitous/XiUOS/arch/arm/{armv7m => shared}/arm32_switch.c (100%) rename Ubiquitous/XiUOS/arch/arm/{armv7m => shared}/pendsv.S (100%) diff --git a/Ubiquitous/XiUOS/arch/arm/Makefile b/Ubiquitous/XiUOS/arch/arm/Makefile index 7b81c4a62..5000fdbfb 100644 --- a/Ubiquitous/XiUOS/arch/arm/Makefile +++ b/Ubiquitous/XiUOS/arch/arm/Makefile @@ -1,10 +1,7 @@ -#公共部分 -SRC_DIR := shared - # The following three platforms support compatiable instructions. ifeq ($(CONFIG_BOARD_CORTEX_M3_EVB),y) -SRC_DIR += armv7m +SRC_DIR := shared SRC_DIR += cortex-m3 endif @@ -13,19 +10,18 @@ SRC_DIR +=cortex-m3 endif ifeq ($(CONFIG_BOARD_STM32F407_EVB),y) -SRC_DIR += armv7m +SRC_DIR := shared SRC_DIR += cortex-m4 endif ifeq ($(CONFIG_BOARD_CORTEX_M7_EVB),y) -SRC_DIR += armv7m +SRC_DIR := shared SRC_DIR += cortex-m7 endif # cortex-m0 is ARMv6-m ifeq ($(CONFIG_BOARD_CORTEX_M0_EVB),y) -SRC_DIR += armv6m SRC_DIR += cortex-m0 endif diff --git a/Ubiquitous/XiUOS/arch/arm/armv6m/Makefile b/Ubiquitous/XiUOS/arch/arm/armv6m/Makefile deleted file mode 100644 index 7524ef1ec..000000000 --- a/Ubiquitous/XiUOS/arch/arm/armv6m/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -SRC_FILES := pendsv.S arm32_switch.c - -include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiUOS/arch/arm/armv7m/Makefile b/Ubiquitous/XiUOS/arch/arm/armv7m/Makefile deleted file mode 100644 index 7524ef1ec..000000000 --- a/Ubiquitous/XiUOS/arch/arm/armv7m/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -SRC_FILES := pendsv.S arm32_switch.c - -include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiUOS/arch/arm/cortex-m0/Makefile b/Ubiquitous/XiUOS/arch/arm/cortex-m0/Makefile index 34ddb6c52..fb37619fa 100644 --- a/Ubiquitous/XiUOS/arch/arm/cortex-m0/Makefile +++ b/Ubiquitous/XiUOS/arch/arm/cortex-m0/Makefile @@ -1,3 +1,3 @@ -SRC_FILES := boot.c interrupt.c interrupt_vector.S +SRC_FILES := boot.c interrupt.c interrupt_vector.S pendsv.S prepare_ahwstack.c arm32_switch.c include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiUOS/arch/arm/armv6m/arm32_switch.c b/Ubiquitous/XiUOS/arch/arm/cortex-m0/arm32_switch.c similarity index 100% rename from Ubiquitous/XiUOS/arch/arm/armv6m/arm32_switch.c rename to Ubiquitous/XiUOS/arch/arm/cortex-m0/arm32_switch.c diff --git a/Ubiquitous/XiUOS/arch/arm/armv6m/pendsv.S b/Ubiquitous/XiUOS/arch/arm/cortex-m0/pendsv.S similarity index 100% rename from Ubiquitous/XiUOS/arch/arm/armv6m/pendsv.S rename to Ubiquitous/XiUOS/arch/arm/cortex-m0/pendsv.S diff --git a/Ubiquitous/XiUOS/arch/arm/cortex-m0/prepare_ahwstack.c b/Ubiquitous/XiUOS/arch/arm/cortex-m0/prepare_ahwstack.c new file mode 100644 index 000000000..6b597821d --- /dev/null +++ b/Ubiquitous/XiUOS/arch/arm/cortex-m0/prepare_ahwstack.c @@ -0,0 +1,411 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +#include +#include +#include +#include "svc_handle.h" +#include +#include + +#if (defined ( __GNUC__ ) && defined ( __VFP_FP__ ) && !defined(__SOFTFP__)) +#define USE_FPU 1 +#else +#define USE_FPU 0 +#endif + +uint32 InterruptFromKtask; +uint32 InterruptToKtask; +uint32 KtaskSwitchInterruptFlag; +uint32 InterruptToKtaskDescriptor; +#define RunningKTask Assign.os_running_task + +static x_err_t (*ExceptionHook)(void *context) = NONE; + +struct ExceptionStackRegister +{ + uint32 r0; + uint32 r1; + uint32 r2; + uint32 r3; + uint32 r12; + uint32 lr; + uint32 pc; + uint32 psr; +}; + +struct StackRegisterContent +{ +#if defined ( __VFP_FP__ ) && !defined(__SOFTFP__) + uint32 flag; +#endif + uint32 primask; + uint32 r4; + uint32 r5; + uint32 r6; + uint32 r7; + uint32 r8; + uint32 r9; + uint32 r10; + uint32 r11; + // uint32 exc_ret; + + struct ExceptionStackRegister ExErrorStackContex; +}; + +struct ExceptionStackFrameFpu +{ + uint32 r0; + uint32 r1; + uint32 r2; + uint32 r3; + uint32 r12; + uint32 lr; + uint32 pc; + uint32 psr; + +#if USE_FPU + uint32 S0; + uint32 S1; + uint32 S2; + uint32 S3; + uint32 S4; + uint32 S5; + uint32 S6; + uint32 S7; + uint32 S8; + uint32 S9; + uint32 S10; + uint32 S11; + uint32 S12; + uint32 S13; + uint32 S14; + uint32 S15; + uint32 FPSCR; + uint32 NO_NAME; +#endif +}; + +struct StackFrameFpu +{ + uint32 flag; + + uint32 r4; + uint32 r5; + uint32 r6; + uint32 r7; + uint32 r8; + uint32 r9; + uint32 r10; + uint32 r11; + +#if USE_FPU + uint32 s16; + uint32 s17; + uint32 s18; + uint32 s19; + uint32 s20; + uint32 s21; + uint32 s22; + uint32 s23; + uint32 s24; + uint32 s25; + uint32 s26; + uint32 s27; + uint32 s28; + uint32 s29; + uint32 s30; + uint32 s31; +#endif + + struct ExceptionStackFrameFpu ExErrorStackContex; +}; + +uint8 KTaskStackSetup(struct TaskDescriptor *task) +{ + struct StackRegisterContent* StackContex; + int i = 0; + + task->stack_point = (uint8 *)ALIGN_MEN_DOWN((x_ubase)(task->task_base_info.stack_start + task->task_base_info.stack_depth), 8); + task->stack_point -= sizeof(struct StackRegisterContent); + + StackContex = (struct StackRegisterContent*)task->stack_point; + + for (i = 0; i < sizeof(struct StackRegisterContent) / sizeof(uint32); i++) + ((uint32 *)StackContex)[i] = 0xfadeface; + + StackContex->ExErrorStackContex.r0 = (unsigned long)task->task_base_info.func_param; + + StackContex->ExErrorStackContex.pc = (unsigned long)task->task_base_info.func_entry ; + StackContex->ExErrorStackContex.psr = 0x01000000L; + StackContex->primask = 0x00000000L; +#ifdef SEPARATE_COMPILE + if(task->task_dync_sched_member.isolation_flag == 1 ) { + StackContex->ExErrorStackContex.lr = (unsigned long)USERSPACE->us_taskquit; + } else { + StackContex->ExErrorStackContex.lr = (unsigned long)KTaskQuit; + } +#else + StackContex->ExErrorStackContex.lr = (unsigned long)KTaskQuit; +#endif + +#if USE_FPU + StackContex->flag = 0; +#endif + + return EOK; +} + + +void HwExceptionInstall(x_err_t (*exception_handle)(void *context)) +{ + ExceptionHook = exception_handle; +} + +#define SCB_CFSR (*(volatile const unsigned *)0xE000ED28) +#define SCB_HFSR (*(volatile const unsigned *)0xE000ED2C) +#define SCB_MMAR (*(volatile const unsigned *)0xE000ED34) +#define SCB_BFAR (*(volatile const unsigned *)0xE000ED38) +#define SCB_AIRCR (*(volatile unsigned long *)0xE000ED0C) +#define SCB_RESET_VALUE 0x05FA0004 + +#define SCB_CFSR_MFSR (*(volatile const unsigned char*)0xE000ED28) +#define SCB_CFSR_BFSR (*(volatile const unsigned char*)0xE000ED29) +#define SCB_CFSR_UFSR (*(volatile const unsigned short*)0xE000ED2A) + +#ifdef TOOL_SHELL +static void UsageFaultTrack(void) +{ + KPrintf("usage fault:\n"); + KPrintf("SCB_CFSR_UFSR:0x%02X ", SCB_CFSR_UFSR); + + if(SCB_CFSR_UFSR & (1<<0)) + KPrintf("UNDEFINSTR "); + + if(SCB_CFSR_UFSR & (1<<1)) + KPrintf("INVSTATE "); + + if(SCB_CFSR_UFSR & (1<<2)) + KPrintf("INVPC "); + + if(SCB_CFSR_UFSR & (1<<3)) + KPrintf("NOCP "); + + if(SCB_CFSR_UFSR & (1<<8)) + KPrintf("UNALIGNED "); + + if(SCB_CFSR_UFSR & (1<<9)) + KPrintf("DIVBYZERO "); + + KPrintf("\n"); +} + +static void BusFaultTrack(void) +{ + KPrintf("bus fault:\n"); + KPrintf("SCB_CFSR_BFSR:0x%02X ", SCB_CFSR_BFSR); + + if(SCB_CFSR_BFSR & (1<<0)) + KPrintf("IBUSERR "); + + if(SCB_CFSR_BFSR & (1<<1)) + KPrintf("PRECISERR "); + + if(SCB_CFSR_BFSR & (1<<2)) + KPrintf("IMPRECISERR "); + + if(SCB_CFSR_BFSR & (1<<3)) + KPrintf("UNSTKERR "); + + if(SCB_CFSR_BFSR & (1<<4)) + KPrintf("STKERR "); + + if(SCB_CFSR_BFSR & (1<<7)) + KPrintf("SCB->BFAR:%08X\n", SCB_BFAR); + else + KPrintf("\n"); +} + +static void MemManageFaultTrack(void) +{ + KPrintf("mem manage fault:\n"); + KPrintf("SCB_CFSR_MFSR:0x%02X ", SCB_CFSR_MFSR); + + if(SCB_CFSR_MFSR & (1<<0)) + KPrintf("IACCVIOL "); + if(SCB_CFSR_MFSR & (1<<1)) + KPrintf("DACCVIOL "); + + if(SCB_CFSR_MFSR & (1<<3)) + KPrintf("MUNSTKERR "); + + if(SCB_CFSR_MFSR & (1<<4)) + KPrintf("MSTKERR "); + + if(SCB_CFSR_MFSR & (1<<7)) + KPrintf("SCB->MMAR:%08X\n", SCB_MMAR); + else + KPrintf("\n"); +} + +static void HardFaultTrack(void) +{ + if(SCB_HFSR & (1UL<<1)) + KPrintf("failed vector fetch\n"); + + if(SCB_HFSR & (1UL<<30)) { + if(SCB_CFSR_BFSR) + BusFaultTrack(); + + if(SCB_CFSR_MFSR) + MemManageFaultTrack(); + + if(SCB_CFSR_UFSR) + UsageFaultTrack(); + } + + if(SCB_HFSR & (1UL<<31)) + KPrintf("debug event\n"); +} +#endif + +struct ExceptionInfo +{ + uint32 ExcReturn; + struct StackRegisterContent stackframe; +}; + +void HwHardFaultException(struct ExceptionInfo *ExceptionInfo) +{ + extern long ShowTask(void); + struct ExErrorStackContex* ExceptionStack = (struct ExErrorStackContex*)&ExceptionInfo->stackframe.ExErrorStackContex; + struct StackRegisterContent* context = (struct StackRegisterContent*)&ExceptionInfo->stackframe; + + if (ExceptionHook != NONE) { + x_err_t result = ExceptionHook(ExceptionStack); + if (result == EOK) return; + } + + KPrintf("psr: 0x%08x\n", context->ExErrorStackContex.psr); + KPrintf("r00: 0x%08x\n", context->ExErrorStackContex.r0); + KPrintf("r01: 0x%08x\n", context->ExErrorStackContex.r1); + KPrintf("r02: 0x%08x\n", context->ExErrorStackContex.r2); + KPrintf("r03: 0x%08x\n", context->ExErrorStackContex.r3); + KPrintf("r04: 0x%08x\n", context->r4); + KPrintf("r05: 0x%08x\n", context->r5); + KPrintf("r06: 0x%08x\n", context->r6); + KPrintf("r07: 0x%08x\n", context->r7); + KPrintf("r08: 0x%08x\n", context->r8); + KPrintf("r09: 0x%08x\n", context->r9); + KPrintf("r10: 0x%08x\n", context->r10); + KPrintf("r11: 0x%08x\n", context->r11); + //KPrintf("exc_ret: 0x%08x\n", context->exc_ret); + KPrintf("r12: 0x%08x\n", context->ExErrorStackContex.r12); + KPrintf(" lr: 0x%08x\n", context->ExErrorStackContex.lr); + KPrintf(" pc: 0x%08x\n", context->ExErrorStackContex.pc); + + if (ExceptionInfo->ExcReturn & (1 << 2)) { + KPrintf("hard fault on task: %s\r\n\r\n", GetKTaskDescriptor()->task_base_info.name); +#ifdef TOOL_SHELL + ShowTask(); +#endif + } else { + KPrintf("hard fault on handler\r\n\r\n"); + } + + if ( (ExceptionInfo->ExcReturn & 0x10) == 0) + KPrintf("FPU active!\r\n"); + +#ifdef TOOL_SHELL + HardFaultTrack(); +#endif + + while (1); +} + +void UpdateRunningTask(void) +{ + RunningKTask = (struct TaskDescriptor *)InterruptToKtaskDescriptor; +} + + +void MemFaultExceptionPrint(struct ExceptionInfo *ExceptionInfo) +{ + extern long ShowTask(void); + struct ExErrorStackContex* ExceptionStack = (struct ExErrorStackContex*)&ExceptionInfo->stackframe.ExErrorStackContex; + struct StackRegisterContent* context = (struct StackRegisterContent*)&ExceptionInfo->stackframe; + + if (ExceptionHook != NONE) { + x_err_t result = ExceptionHook(ExceptionStack); + if (result == EOK) return; + } + + KPrintf("psr: 0x%08x\n", context->ExErrorStackContex.psr); + KPrintf("r00: 0x%08x\n", context->ExErrorStackContex.r0); + KPrintf("r01: 0x%08x\n", context->ExErrorStackContex.r1); + KPrintf("r02: 0x%08x\n", context->ExErrorStackContex.r2); + KPrintf("r03: 0x%08x\n", context->ExErrorStackContex.r3); + KPrintf("r04: 0x%08x\n", context->r4); + KPrintf("r05: 0x%08x\n", context->r5); + KPrintf("r06: 0x%08x\n", context->r6); + KPrintf("r07: 0x%08x\n", context->r7); + KPrintf("r08: 0x%08x\n", context->r8); + KPrintf("r09: 0x%08x\n", context->r9); + KPrintf("r10: 0x%08x\n", context->r10); + KPrintf("r11: 0x%08x\n", context->r11); + KPrintf("exc_ret: 0x%08x\n", ExceptionInfo->ExcReturn); + KPrintf("r12: 0x%08x\n", context->ExErrorStackContex.r12); + KPrintf(" lr: 0x%08x\n", context->ExErrorStackContex.lr); + KPrintf(" pc: 0x%08x\n", context->ExErrorStackContex.pc); + + if (ExceptionInfo->ExcReturn & (1 << 2)) { + KPrintf("hard fault on task: %s\r\n\r\n", GetKTaskDescriptor()->task_base_info.name); +#ifdef TOOL_SHELL + ShowTask(); +#endif + } else { + KPrintf("hard fault on handler\r\n\r\n"); + } + + if ((ExceptionInfo->ExcReturn & 0x10) == 0) + KPrintf("FPU active!\r\n"); + + +#ifdef TOOL_SHELL + HardFaultTrack(); +#endif + + while (1); +} + +void MemFaultHandle(uintptr_t *sp) +{ +#ifdef TASK_ISOLATION + struct TaskDescriptor *task; + task = GetKTaskDescriptor(); + if( task->task_dync_sched_member.isolation_flag == 1){ + KPrintf("\nSegmentation fault, task: %s\n", task->task_base_info.name); + KTaskQuit(); + } + else +#endif + { + MemFaultExceptionPrint((struct ExceptionInfo *)sp); + } + +} + +__attribute__((weak)) void HwCpuReset(void) +{ + SCB_AIRCR = SCB_RESET_VALUE; +} +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), Reboot, HwCpuReset, reset machine ); \ No newline at end of file diff --git a/Ubiquitous/XiUOS/arch/arm/cortex-m0/svc_handle.h b/Ubiquitous/XiUOS/arch/arm/cortex-m0/svc_handle.h new file mode 100644 index 000000000..182241707 --- /dev/null +++ b/Ubiquitous/XiUOS/arch/arm/cortex-m0/svc_handle.h @@ -0,0 +1,58 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +#ifndef __INC_SVC_HANDLE_H__ +#define __INC_SVC_HANDLE_H__ + +#if defined ( __VFP_FP__ ) && !defined(__SOFTFP__) +#define INT_FPU_REGS (1) +#else +#define INT_FPU_REGS (0) +#endif + +#define HW_INT_REGS (8) +#define SW_INT_REGS (9 + INT_FPU_REGS) + +#define REG_INT_R0 (SW_INT_REGS + 0) /* R0 */ +#define REG_INT_R1 (SW_INT_REGS + 1) /* R1 */ +#define REG_INT_R2 (SW_INT_REGS + 2) /* R2 */ +#define REG_INT_R3 (SW_INT_REGS + 3) /* R3 */ +#define REG_INT_R12 (SW_INT_REGS + 4) /* R12 */ +#define REG_INT_R14 (SW_INT_REGS + 5) /* R14 = LR */ +#define REG_INT_PC (SW_INT_REGS + 6) /* R15 = PC */ +#define REG_INT_XPSR (SW_INT_REGS + 7) /* xPSR */ + +#if defined ( __VFP_FP__ ) && !defined(__SOFTFP__) +#define REG_INT_FPU_FLAG (0) /* fpu flag */ +#define REG_INT_PRIMASK (1) /* PRIMASK */ +#define REG_INT_R4 (2) /* R4 */ +#define REG_INT_R5 (3) /* R5 */ +#define REG_INT_R6 (4) /* R6 */ +#define REG_INT_R7 (5) /* R7 */ +#define REG_INT_R8 (6) /* R8 */ +#define REG_INT_R9 (7) /* R9 */ +#define REG_INT_R10 (8) /* R10 */ +#define REG_INT_R11 (9) /* R11 */ +#else +#define REG_INT_PRIMASK (0) /* PRIMASK */ +#define REG_INT_R4 (1) /* R4 */ +#define REG_INT_R5 (2) /* R5 */ +#define REG_INT_R6 (3) /* R6 */ +#define REG_INT_R7 (4) /* R7 */ +#define REG_INT_R8 (5) /* R8 */ +#define REG_INT_R9 (6) /* R9 */ +#define REG_INT_R10 (7) /* R10 */ +#define REG_INT_R11 (8) /* R11 */ +#endif + + +#endif diff --git a/Ubiquitous/XiUOS/arch/arm/shared/Makefile b/Ubiquitous/XiUOS/arch/arm/shared/Makefile index 9d183f38a..9edf7d314 100644 --- a/Ubiquitous/XiUOS/arch/arm/shared/Makefile +++ b/Ubiquitous/XiUOS/arch/arm/shared/Makefile @@ -1,4 +1,4 @@ -SRC_FILES := prepare_ahwstack.c +SRC_FILES := pendsv.S prepare_ahwstack.c arm32_switch.c include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiUOS/arch/arm/armv7m/arm32_switch.c b/Ubiquitous/XiUOS/arch/arm/shared/arm32_switch.c similarity index 100% rename from Ubiquitous/XiUOS/arch/arm/armv7m/arm32_switch.c rename to Ubiquitous/XiUOS/arch/arm/shared/arm32_switch.c diff --git a/Ubiquitous/XiUOS/arch/arm/armv7m/pendsv.S b/Ubiquitous/XiUOS/arch/arm/shared/pendsv.S similarity index 100% rename from Ubiquitous/XiUOS/arch/arm/armv7m/pendsv.S rename to Ubiquitous/XiUOS/arch/arm/shared/pendsv.S