arch_exception.h 源码
2026/7/20大约 2 分钟源码参考架构层异常处理头文件
arch_exception.h — 异常处理接口头文件
源文件路径: arch/arm/armv7m/include/arch_exception.h
引用章节: 4.6.5 异常处理
完整源码
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
*
* Licensed 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.
*
* @file arch_exception.h
*
* @brief Header file for exception functions.
*
* @revision
* Date Author Notes
* 2021-10-22 OneOS Team First version.
***********************************************************************************************************************
*/
#ifndef __OS_ARCH_EXCEPTION_H__
#define __OS_ARCH_EXCEPTION_H__
#include <oneos_config.h>
#include <os_types.h>
#include <os_task.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SCB_CFSR (*(volatile const unsigned *)0xE000ED28) /* Configurable Fault Status Register */
#define SCB_HFSR (*(volatile const unsigned *)0xE000ED2C) /* HardFault Status Register */
#define SCB_MMAR (*(volatile const unsigned *)0xE000ED34) /* MemManage Fault Address register */
#define SCB_BFAR (*(volatile const unsigned *)0xE000ED38) /* Bus Fault Address Register */
#define SCB_AIRCR (*(volatile unsigned long *)0xE000ED0C) /* Reset control Address Register */
#define SCB_RESET_VALUE 0x05FA0004 /* Reset value, write to SCB_AIRCR can reset cpu */
#define SCB_CFSR_MFSR (*(volatile const unsigned char *)0xE000ED28) /* Memory-management Fault Status Register */
#define SCB_CFSR_BFSR (*(volatile const unsigned char *)0xE000ED29) /* Bus Fault Status Register */
#define SCB_CFSR_UFSR (*(volatile const unsigned short *)0xE000ED2A) /* Usage Fault Status Register */
/* 1 (Stack frame contains floating point) or 0 (Stack frame does not contain floating point) */
#define EXCEPTION_STACK_FRAME_TYPE_MASK 0x00000010
/* 1 (Return to Thread) or 0 (Return to Handler) */
#define EXCEPTION_RETURN_MODE_MASK 0x00000008
#define EXCEPTION_RETURN_MODE_THREAD 0x00000008
#define EXCEPTION_RETURN_MODE_HANDLER 0x00000000
/* 1 (Return with Process Stack) or 0 (Return with Main Stack)*/
#define EXCEPTION_RETURN_STACK_MASK 0x00000004
#if defined(__CC_ARM) || defined(__CLANG_ARM)
#ifndef CSTACK_BLOCK_NAME
#define CSTACK_BLOCK_NAME STACK
#endif
#ifndef CODE_SECTION_NAME
#define CODE_SECTION_NAME ER_IROM1
#endif
#elif defined(__ICCARM__)
#ifndef CSTACK_BLOCK_NAME
#define CSTACK_BLOCK_NAME "CSTACK"
#endif
#ifndef CODE_SECTION_NAME
#define CODE_SECTION_NAME ".text"
#endif
#elif defined(__GNUC__)
#ifndef CSTACK_BLOCK_START
#define CSTACK_BLOCK_START _sstack
#endif
#ifndef CSTACK_BLOCK_END
#define CSTACK_BLOCK_END _estack
#endif
#ifndef CODE_SECTION_START
#define CODE_SECTION_START _stext
#endif
#ifndef CODE_SECTION_END
#define CODE_SECTION_END _etext
#endif
#else
#error "not supported compiler"
#endif
#if defined(__CC_ARM) || defined(__CLANG_ARM)
#define SECTION_START(_name_) _name_##$$Base
#define SECTION_END(_name_) _name_##$$Limit
#define IMAGE_SECTION_START(_name_) Image$$##_name_##$$Base
#define IMAGE_SECTION_END(_name_) Image$$##_name_##$$Limit
#define CSTACK_BLOCK_START(_name_) SECTION_START(_name_)
#define CSTACK_BLOCK_END(_name_) SECTION_END(_name_)
#define CODE_SECTION_START(_name_) IMAGE_SECTION_START(_name_)
#define CODE_SECTION_END(_name_) IMAGE_SECTION_END(_name_)
extern const int CSTACK_BLOCK_START(CSTACK_BLOCK_NAME);
extern const int CSTACK_BLOCK_END(CSTACK_BLOCK_NAME);
extern const int CODE_SECTION_START(CODE_SECTION_NAME);
extern const int CODE_SECTION_END(CODE_SECTION_NAME);
#elif defined(__ICCARM__)
#pragma section = CSTACK_BLOCK_NAME
#pragma section = CODE_SECTION_NAME
#elif defined(__GNUC__)
extern const int CSTACK_BLOCK_START;
extern const int CSTACK_BLOCK_END;
extern const int CODE_SECTION_START;
extern const int CODE_SECTION_END;
#else
#error "not supported compiler"
#endif
#ifdef STACK_TRACE_EN
#include <stack_trace.h>
extern os_err_t task_stack_show(os_task_id task, uint32_t context);
extern os_err_t interrupt_stack_show(void);
extern os_bool_t disassembly_ins_is_exc_return(os_size_t ins);
extern os_bool_t disassembly_ins_is_bl_blx(uint32_t addr);
extern void trace_stack(os_size_t *stack_top, os_size_t *stack_bottom, call_back_trace_t *trace);
extern os_err_t arch_call_back_trace_init(void);
extern void _arch_exception_stack_show(void *stack_frame, os_size_t *msp, os_size_t *psp);
#endif /* STACK_TRACE_EN */
#ifdef __cplusplus
}
#endif
#endif /* __OS_ARCH_EXCEPTION_H__ */关键说明
| 模块 | 说明 |
|---|---|
| SCB 寄存器 | SCB_CFSR(可配置故障状态)、SCB_HFSR(HardFault 状态)、SCB_MMAR(MemManage 地址)、SCB_BFAR(BusFault 地址)、SCB_AIRCR(复位控制) |
| EXC_RETURN 解析 | 通过 EXCEPTION_RETURN_MODE_MASK(bit3)判断返回线程/Handler,EXCEPTION_RETURN_STACK_MASK(bit2)判断 PSP/MSP |
| 栈帧类型 | EXCEPTION_STACK_FRAME_TYPE_MASK(bit4)判断是否包含 FPU 寄存器 |
| 编译器适配 | 支持 ARMCC、Clang、IAR、GCC 四种编译器,通过链接器符号获取栈和代码段边界 |
| 栈回溯 | STACK_TRACE_EN 宏启用时提供 task_stack_show、`interrupt_stack_ |