trap.c 5.0 KB
Newer Older
L
luohui2320@gmail.com 已提交
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
L
luohui2320@gmail.com 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
L
luohui2320@gmail.com 已提交
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-01-13     weety      modified from mini2440
9
 * 2015-04-15     ArdaFu     Split from AT91SAM9260 BSP
L
luohui2320@gmail.com 已提交
10 11 12 13 14
 */

#include <rtthread.h>
#include <rthw.h>

15 16
#define INT_IRQ     0x00
#define INT_FIQ     0x01
L
luohui2320@gmail.com 已提交
17 18

extern struct rt_thread *rt_current_thread;
19
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
L
luohui2320@gmail.com 已提交
20 21 22
extern long list_thread(void);
#endif

23 24
struct rt_hw_register
{
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    rt_uint32_t r0;
    rt_uint32_t r1;
    rt_uint32_t r2;
    rt_uint32_t r3;
    rt_uint32_t r4;
    rt_uint32_t r5;
    rt_uint32_t r6;
    rt_uint32_t r7;
    rt_uint32_t r8;
    rt_uint32_t r9;
    rt_uint32_t r10;
    rt_uint32_t fp;
    rt_uint32_t ip;
    rt_uint32_t sp;
    rt_uint32_t lr;
    rt_uint32_t pc;
    rt_uint32_t cpsr;
    rt_uint32_t ORIG_r0;
43
};
S
shaojinchun 已提交
44 45 46 47 48
static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context))
{
    rt_exception_hook = exception_handle;
}
L
luohui2320@gmail.com 已提交
49 50 51 52 53 54
/**
 * this function will show registers of CPU
 *
 * @param regs the registers point
 */

S
shaojinchun 已提交
55
void rt_hw_show_register(struct rt_hw_register *regs)
L
luohui2320@gmail.com 已提交
56
{
57 58 59 60 61 62 63 64 65 66 67 68
    rt_kprintf("Execption:\n");
    rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n",
               regs->r0, regs->r1, regs->r2, regs->r3);
    rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n",
               regs->r4, regs->r5, regs->r6, regs->r7);
    rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n",
               regs->r8, regs->r9, regs->r10);
    rt_kprintf("fp :0x%08x ip :0x%08x\n",
               regs->fp, regs->ip);
    rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n",
               regs->sp, regs->lr, regs->pc);
    rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
L
luohui2320@gmail.com 已提交
69 70 71 72 73 74 75 76 77 78 79 80
}

/**
 * When ARM7TDMI comes across an instruction which it cannot handle,
 * it takes the undefined instruction trap.
 *
 * @param regs system registers
 *
 * @note never invoke this function in application
 */
void rt_hw_trap_udef(struct rt_hw_register *regs)
{
S
shaojinchun 已提交
81 82 83 84 85 86 87
    if (rt_exception_hook != RT_NULL)
    {
        rt_err_t result;

        result = rt_exception_hook(regs);
        if (result == RT_EOK) return;
    }
88
    rt_hw_show_register(regs);
L
luohui2320@gmail.com 已提交
89

90 91
    rt_kprintf("undefined instruction\n");
    rt_kprintf("thread - %s stack:\n", rt_current_thread->name);
L
luohui2320@gmail.com 已提交
92

93
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
94
    list_thread();
L
luohui2320@gmail.com 已提交
95
#endif
96
    rt_hw_cpu_shutdown();
L
luohui2320@gmail.com 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
}

/**
 * The software interrupt instruction (SWI) is used for entering
 * Supervisor mode, usually to request a particular supervisor
 * function.
 *
 * @param regs system registers
 *
 * @note never invoke this function in application
 */
void rt_hw_trap_swi(struct rt_hw_register *regs)
{
S
shaojinchun 已提交
110 111 112 113 114 115 116
    if (rt_exception_hook != RT_NULL)
    {
        rt_err_t result;

        result = rt_exception_hook(regs);
        if (result == RT_EOK) return;
    }
117
    rt_hw_show_register(regs);
L
luohui2320@gmail.com 已提交
118

119 120
    rt_kprintf("software interrupt\n");
    rt_hw_cpu_shutdown();
L
luohui2320@gmail.com 已提交
121 122 123 124 125 126 127 128 129 130 131 132
}

/**
 * An abort indicates that the current memory access cannot be completed,
 * which occurs during an instruction prefetch.
 *
 * @param regs system registers
 *
 * @note never invoke this function in application
 */
void rt_hw_trap_pabt(struct rt_hw_register *regs)
{
S
shaojinchun 已提交
133 134 135 136 137 138 139
    if (rt_exception_hook != RT_NULL)
    {
        rt_err_t result;

        result = rt_exception_hook(regs);
        if (result == RT_EOK) return;
    }
140
    rt_hw_show_register(regs);
L
luohui2320@gmail.com 已提交
141

142 143
    rt_kprintf("prefetch abort\n");
    rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
L
luohui2320@gmail.com 已提交
144

145
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
146
    list_thread();
L
luohui2320@gmail.com 已提交
147
#endif
148
    rt_hw_cpu_shutdown();
L
luohui2320@gmail.com 已提交
149 150 151 152 153 154 155 156 157 158 159 160
}

/**
 * An abort indicates that the current memory access cannot be completed,
 * which occurs during a data access.
 *
 * @param regs system registers
 *
 * @note never invoke this function in application
 */
void rt_hw_trap_dabt(struct rt_hw_register *regs)
{
S
shaojinchun 已提交
161 162 163 164 165 166 167
    if (rt_exception_hook != RT_NULL)
    {
        rt_err_t result;

        result = rt_exception_hook(regs);
        if (result == RT_EOK) return;
    }
168
    rt_hw_show_register(regs);
L
luohui2320@gmail.com 已提交
169

170 171
    rt_kprintf("data abort\n");
    rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
L
luohui2320@gmail.com 已提交
172

173
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
174
    list_thread();
L
luohui2320@gmail.com 已提交
175
#endif
176
    rt_hw_cpu_shutdown();
L
luohui2320@gmail.com 已提交
177 178 179 180 181 182 183 184 185 186 187
}

/**
 * Normally, system will never reach here
 *
 * @param regs system registers
 *
 * @note never invoke this function in application
 */
void rt_hw_trap_resv(struct rt_hw_register *regs)
{
S
shaojinchun 已提交
188 189 190 191 192 193 194
    if (rt_exception_hook != RT_NULL)
    {
        rt_err_t result;

        result = rt_exception_hook(regs);
        if (result == RT_EOK) return;
    }
195 196 197
    rt_kprintf("not used\n");
    rt_hw_show_register(regs);
    rt_hw_cpu_shutdown();
L
luohui2320@gmail.com 已提交
198 199
}

200
extern void rt_interrupt_dispatch(rt_uint32_t fiq_irq);
201

S
shaojinchun 已提交
202 203
void rt_hw_trap_irq(void)
{
204
    rt_interrupt_dispatch(INT_IRQ);
L
luohui2320@gmail.com 已提交
205 206
}

S
shaojinchun 已提交
207
void rt_hw_trap_fiq(void)
L
luohui2320@gmail.com 已提交
208
{
209
    rt_interrupt_dispatch(INT_FIQ);
L
luohui2320@gmail.com 已提交
210
}