1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2013-07-20 Bernard first version
9 */
10
11 #include <rtthread.h>
12 #include <rthw.h>
13 #include <board.h>
14
15 #include "armv7.h"
16
17 #include "gic.h"
18
19 extern struct rt_thread *rt_current_thread;
20 #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
21 extern long list_thread(void);
22 #endif
23
24 /**
25 * this function will show registers of CPU
26 *
27 * @param regs the registers point
28 */
rt_hw_show_register(struct rt_hw_exp_stack * regs)29 void rt_hw_show_register(struct rt_hw_exp_stack *regs)
30 {
31 rt_kprintf("Execption:\n");
32 rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n", regs->r0, regs->r1, regs->r2, regs->r3);
33 rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n", regs->r4, regs->r5, regs->r6, regs->r7);
34 rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n", regs->r8, regs->r9, regs->r10);
35 rt_kprintf("fp :0x%08x ip :0x%08x\n", regs->fp, regs->ip);
36 rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n", regs->sp, regs->lr, regs->pc);
37 rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
38 }
39
40 /**
41 * When comes across an instruction which it cannot handle,
42 * it takes the undefined instruction trap.
43 *
44 * @param regs system registers
45 *
46 * @note never invoke this function in application
47 */
rt_hw_trap_undef(struct rt_hw_exp_stack * regs)48 void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
49 {
50 rt_kprintf("undefined instruction:\n");
51 rt_hw_show_register(regs);
52 #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
53 list_thread();
54 #endif
55 rt_hw_cpu_shutdown();
56 }
57
58 /**
59 * The software interrupt instruction (SWI) is used for entering
60 * Supervisor mode, usually to request a particular supervisor
61 * function.
62 *
63 * @param regs system registers
64 *
65 * @note never invoke this function in application
66 */
rt_hw_trap_swi(struct rt_hw_exp_stack * regs)67 void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
68 {
69 rt_kprintf("software interrupt:\n");
70 rt_hw_show_register(regs);
71 #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
72 list_thread();
73 #endif
74 rt_hw_cpu_shutdown();
75 }
76
77 /**
78 * An abort indicates that the current memory access cannot be completed,
79 * which occurs during an instruction prefetch.
80 *
81 * @param regs system registers
82 *
83 * @note never invoke this function in application
84 */
rt_hw_trap_pabt(struct rt_hw_exp_stack * regs)85 void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
86 {
87 rt_kprintf("prefetch abort:\n");
88 rt_hw_show_register(regs);
89 #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
90 list_thread();
91 #endif
92 rt_hw_cpu_shutdown();
93 }
94
95 /**
96 * An abort indicates that the current memory access cannot be completed,
97 * which occurs during a data access.
98 *
99 * @param regs system registers
100 *
101 * @note never invoke this function in application
102 */
rt_hw_trap_dabt(struct rt_hw_exp_stack * regs)103 void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
104 {
105 rt_kprintf("data abort:");
106 rt_hw_show_register(regs);
107 #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
108 list_thread();
109 #endif
110 rt_hw_cpu_shutdown();
111 }
112
113 /**
114 * Normally, system will never reach here
115 *
116 * @param regs system registers
117 *
118 * @note never invoke this function in application
119 */
rt_hw_trap_resv(struct rt_hw_exp_stack * regs)120 void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
121 {
122 rt_kprintf("reserved trap:\n");
123 rt_hw_show_register(regs);
124 #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
125 list_thread();
126 #endif
127 rt_hw_cpu_shutdown();
128 }
129
130 #define GIC_ACK_INTID_MASK 0x000003ff
131
rt_hw_trap_irq(void)132 void rt_hw_trap_irq(void)
133 {
134 void *param;
135 unsigned long ir;
136 unsigned long fullir;
137 rt_isr_handler_t isr_func;
138 extern struct rt_irq_desc isr_table[];
139
140 fullir = arm_gic_get_active_irq(0);
141 ir = fullir & GIC_ACK_INTID_MASK;
142
143 if (ir == 1023)
144 {
145 /* Spurious interrupt */
146 return;
147 }
148
149 /* get interrupt service routine */
150 isr_func = isr_table[ir].handler;
151 #ifdef RT_USING_INTERRUPT_INFO
152 isr_table[ir].counter++;
153 #endif
154 if (isr_func)
155 {
156 /* Interrupt for myself. */
157 param = isr_table[ir].param;
158 /* turn to interrupt service routine */
159 isr_func(ir, param);
160 }
161
162 /* end of interrupt */
163 arm_gic_ack(0, fullir);
164 }
165
rt_hw_trap_fiq(void)166 void rt_hw_trap_fiq(void)
167 {
168 void *param;
169 unsigned long ir;
170 unsigned long fullir;
171 rt_isr_handler_t isr_func;
172 extern struct rt_irq_desc isr_table[];
173
174 fullir = arm_gic_get_active_irq(0);
175 ir = fullir & GIC_ACK_INTID_MASK;
176
177 /* get interrupt service routine */
178 isr_func = isr_table[ir].handler;
179 param = isr_table[ir].param;
180
181 /* turn to interrupt service routine */
182 isr_func(ir, param);
183
184 /* end of interrupt */
185 arm_gic_ack(0, fullir);
186 }
187
188