Debian Codegraph @

ports/qemu/mcu/rv32/interrupts.c

raw
1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2023 Alessandro Gatti
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30
31 // Vector table
32
33 void mtvec_table(void) __attribute__((naked, section(".text.mtvec"), aligned(256)));
34
35 // Default interrupts
36
37 #define ASSIGN_EMPTY_MACHINE_INTERRUPT(interrupt_name) \
38 void interrupt_name(void) __attribute__((alias("mtvec_nop")))
39
40 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_ssi);
41 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_msi);
42 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_sti);
43 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_mti);
44 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_sei);
45 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_mei);
46 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq0);
47 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq1);
48 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq2);
49 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq3);
50 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq4);
51 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq5);
52 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq6);
53 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq7);
54 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq8);
55 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq9);
56 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq10);
57 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq11);
58 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq12);
59 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq13);
60 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq14);
61 ASSIGN_EMPTY_MACHINE_INTERRUPT(mtvec_plat_irq15);
62
63 void mtvec_table(void) {
64 __asm volatile (
65 ".org mtvec_table + 0 \n"
66 "jal zero, mtvec_exception \n" // Exception Handler
67 ".org mtvec_table + 4 \n"
68 "jal zero, mtvec_ssi \n" // Supervisor Software Interrupt
69 ".org mtvec_table + 12 \n"
70 "jal zero, mtvec_msi \n" // Machine Software Interrupt
71 ".org mtvec_table + 20 \n"
72 "jal zero, mtvec_sti \n" // Supervisor Timer Interrupt
73 ".org mtvec_table + 28 \n"
74 "jal zero, mtvec_mti \n" // Machine Timer Interrupt
75 ".org mtvec_table + 36 \n"
76 "jal zero, mtvec_sei \n" // Supervisor External Interrupt
77 ".org mtvec_table + 44 \n"
78 "jal zero, mtvec_mei \n" // Machine External Interrupt
79 // Not sure how many platform interrupts QEMU handles...
80 ".org mtvec_table + 48 \n"
81 "jal mtvec_plat_irq0 \n" // Platform Interrupt #0
82 "jal mtvec_plat_irq1 \n" // Platform Interrupt #1
83 "jal mtvec_plat_irq2 \n" // Platform Interrupt #2
84 "jal mtvec_plat_irq3 \n" // Platform Interrupt #3
85 "jal mtvec_plat_irq4 \n" // Platform Interrupt #4
86 "jal mtvec_plat_irq5 \n" // Platform Interrupt #5
87 "jal mtvec_plat_irq6 \n" // Platform Interrupt #6
88 "jal mtvec_plat_irq7 \n" // Platform Interrupt #7
89 "jal mtvec_plat_irq8 \n" // Platform Interrupt #8
90 "jal mtvec_plat_irq9 \n" // Platform Interrupt #9
91 "jal mtvec_plat_irq10 \n" // Platform Interrupt #10
92 "jal mtvec_plat_irq11 \n" // Platform Interrupt #11
93 "jal mtvec_plat_irq12 \n" // Platform Interrupt #12
94 "jal mtvec_plat_irq13 \n" // Platform Interrupt #13
95 "jal mtvec_plat_irq14 \n" // Platform Interrupt #14
96 "jal mtvec_plat_irq15 \n" // Platform Interrupt #15
97 :
98 :
99 : "memory"
100 );
101 }
102
103 volatile uint32_t registers_copy[35] = { 0 };
104
105 const char *exception_causes[] = {
106 "Reserved", // 0
107 "Supervisor software interrupt", // 1
108 "Machine software interrupt", // 2
109 "Supervisor timer interrupt", // 3
110 "Machine timer interrupt", // 4
111 "Supervisor external interrupt", // 5
112 "Machine external interrupt", // 6
113 "Designated for platform use", // 7
114 "Instruction address misaligned", // 8
115 "Instruction address fault", // 9
116 "Illegal instruction", // 10
117 "Breakpoint", // 11
118 "Load address misaligned", // 12
119 "Load address fault", // 13
120 "Store/AMO address misaligned", // 14
121 "Store/AMO access fault", // 15
122 "Environment call from U-mode", // 16
123 "Environment call from S-mode", // 17
124 "Environment call from M-mode", // 18
125 "Instruction page fault", // 19
126 "Load page fault", // 20
127 "Store/AMO page fault", // 21
128 "Designated for custom use" // 22
129 };
130
131 const char *lookup_cause(uint32_t mcause) {
132 if (mcause & 0x80000000) {
133 switch (mcause & 0x7FFFFFFF) {
134 case 1:
135 return exception_causes[1];
136 case 3:
137 return exception_causes[2];
138 case 5:
139 return exception_causes[3];
140 case 7:
141 return exception_causes[4];
142 case 9:
143 return exception_causes[5];
144 case 11:
145 return exception_causes[6];
146 default:
147 return ((mcause & 0x7FFFFFFF) >= 16) ?
148 exception_causes[7] :
149 exception_causes[0];
150 }
151 }
152
153 switch (mcause) {
154 case 0:
155 return exception_causes[8];
156 case 1:
157 return exception_causes[9];
158 case 2:
159 return exception_causes[10];
160 case 3:
161 return exception_causes[11];
162 case 4:
163 return exception_causes[12];
164 case 5:
165 return exception_causes[13];
166 case 6:
167 return exception_causes[14];
168 case 7:
169 return exception_causes[15];
170 case 8:
171 return exception_causes[16];
172 case 9:
173 return exception_causes[17];
174 case 11:
175 return exception_causes[18];
176 case 12:
177 return exception_causes[19];
178 case 13:
179 return exception_causes[20];
180 case 15:
181 return exception_causes[21];
182 default: {
183 if ((mcause >= 24 && mcause <= 31) ||
184 (mcause >= 48 && mcause <= 63)) {
185 return exception_causes[22];
186 }
187
188 return exception_causes[0];
189 }
190 }
191 }
192
193 #pragma GCC push_options
194 #pragma GCC optimize ("align-functions=4")
195
196 __attribute__((interrupt("machine"), weak)) void mtvec_nop(void) {
197 }
198
199 __attribute__((interrupt("machine"), weak)) void mtvec_exception(void) {
200 __asm volatile (
201 "csrrw x31, mscratch, x31 \n" // Save X31
202 "la x31, registers_copy \n" // Load target address
203 "sw x1, 0(x31) \n" // Save X1
204 "sw x2, 4(x31) \n" // Save X2
205 "sw x3, 8(x31) \n" // Save X3
206 "sw x4, 12(x31) \n" // Save X4
207 "sw x5, 16(x31) \n" // Save X5
208 "sw x6, 20(x31) \n" // Save X6
209 "sw x7, 24(x31) \n" // Save X7
210 "sw x8, 28(x31) \n" // Save X8
211 "sw x9, 32(x31) \n" // Save X9
212 "sw x10, 36(x31) \n" // Save X10
213 "sw x11, 40(x31) \n" // Save X11
214 "sw x12, 44(x31) \n" // Save X12
215 "sw x13, 48(x31) \n" // Save X13
216 "sw x14, 52(x31) \n" // Save X14
217 "sw x15, 56(x31) \n" // Save X15
218 "sw x16, 60(x31) \n" // Save X16
219 "sw x17, 64(x31) \n" // Save X17
220 "sw x18, 68(x31) \n" // Save X18
221 "sw x19, 72(x31) \n" // Save X19
222 "sw x20, 76(x31) \n" // Save X20
223 "sw x21, 80(x31) \n" // Save X21
224 "sw x22, 84(x31) \n" // Save X22
225 "sw x23, 88(x31) \n" // Save X23
226 "sw x24, 92(x31) \n" // Save X24
227 "sw x25, 98(x31) \n" // Save X25
228 "sw x26, 100(x31) \n" // Save X26
229 "sw x27, 104(x31) \n" // Save X27
230 "sw x28, 108(x31) \n" // Save X28
231 "sw x29, 112(x31) \n" // Save X29
232 "sw x30, 116(x31) \n" // Save X30
233 "csrr x30, mscratch \n" // Restore X31
234 "sw x30, 120(x31) \n" // Save X31
235 "csrr x30, mepc \n" // Load MEPC
236 "sw x30, 124(x31) \n" // Save MEPC
237 "csrr x30, mcause \n" // Load MCAUSE
238 "sw x30, 128(x31) \n" // Save MCAUSE
239 "csrr x30, mtval \n" // Load MTVAL
240 "sw x30, 132(x31) \n" // Save MTVAL
241 "csrr x30, mstatus \n" // Load MSTATUS
242 "sw x30, 138(x31) \n" // Save MSTATUS
243 "lw x30, 116(x31) \n" // Restore X30
244 "lw x31, 120(x31) \n" // Restore X31
245 :
246 :
247 : "memory"
248 );
249
250 printf("\nMACHINE EXCEPTION CAUGHT:\n\n");
251
252 printf(" RA=%08lX SP=%08lX GP=%08lX TP=%08lX T0=%08lX T1=%08lX\n",
253 registers_copy[0], registers_copy[1], registers_copy[2],
254 registers_copy[3], registers_copy[4], registers_copy[5]);
255 printf(" T2=%08lX S0=%08lX S1=%08lX A0=%08lX A1=%08lX A2=%08lX\n",
256 registers_copy[6], registers_copy[7], registers_copy[8],
257 registers_copy[9], registers_copy[10], registers_copy[11]);
258 printf(" A3=%08lX A4=%08lX A5=%08lX A6=%08lX A7=%08lX S2=%08lX\n",
259 registers_copy[12], registers_copy[13], registers_copy[14],
260 registers_copy[15], registers_copy[16], registers_copy[17]);
261 printf(" S3=%08lX S4=%08lX S5=%08lX S6=%08lX S7=%08lX S8=%08lX\n",
262 registers_copy[18], registers_copy[19], registers_copy[20],
263 registers_copy[21], registers_copy[22], registers_copy[23]);
264 printf(" S9=%08lX S10=%08lX S11=%08lX T3=%08lX T4=%08lX T5=%08lX\n",
265 registers_copy[24], registers_copy[25], registers_copy[26],
266 registers_copy[27], registers_copy[28], registers_copy[29]);
267 printf(" T6=%08lX\n\n", registers_copy[30]);
268
269 printf(" MEPC=%08lX MTVAL=%08lX MSTATUS=%08lx MCAUSE=%08lx (%s)\n",
270 registers_copy[31], registers_copy[33], registers_copy[34],
271 registers_copy[32], lookup_cause(registers_copy[32]));
272
273 exit(-1);
274 }
275
276 #pragma GCC pop_options
277
278 void set_interrupt_table(void) {
279 __asm volatile (
280 "csrrci s0, mstatus, 8 \n" // S0 = MSTATUS & ~MIE
281 "csrw mstatus, s0 \n" // Global machine interrupts are disabled
282 "csrw mie, zero \n" // Disable machine interrupts
283 "csrw mip, zero \n" // Clear pending machine interrupts
284 "addi s0, %0, 1 \n" // Vectored machine interrupts enabled
285 "csrw mtvec, s0 \n" // Set new machine vector table
286 "csrrsi s0, mstatus, 8 \n" // S0 = MSTATUS | MIE
287 "csrw mstatus, s0 \n" // Global machine interrupts are enabled
288 :
289 : "r" (mtvec_table)
290 : "memory"
291 );
292 }