1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-12-08 RT-Thread first version
* 2025-04-20 GuEe-GUI Port to the DM
*/
#include <rthw.h>
#include <rtthread.h>
#include <csr.h>
#include <stack.h>
#include <riscv_timer.h>
#ifdef RT_USING_DM
#include <drivers/pic.h>
#else
#include <board.h>
#include <interrupt.h>
#endif
#ifdef RT_USING_SMART
#include <lwp_arch.h>
#endif
#define DBG_TAG "libcpu.trap"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef RT_USING_FINSH
extern long list_thread(void);
#endif
#ifdef RT_USING_LWP
#include <mm_aspace.h>
#include <lwp.h>
#include <lwp_arch.h>
#ifdef LWP_USING_CORE_DUMP
#include <lwp_core_dump.h>
#endif
static void _check_fault(struct rt_hw_stack_frame *regs, uint32_t pc_adj, char *info)
{
rt_thread_t th;
rt_bool_t is_user_fault = !(regs->xstatus & SR_PP);
if (is_user_fault)
{
rt_kprintf("%s! pc = %p\n", info, regs->epc - pc_adj);
}
/* User stack backtrace */
th = rt_thread_self();
if (th && th->lwp)
{
arch_backtrace_uthread(th);
}
if (is_user_fault)
{
#ifdef LWP_USING_CORE_DUMP
lwp_core_dump(regs, pc_adj);
#endif
sys_exit_group(-1);
}
}
rt_inline rt_bool_t user_fault_fixable(struct rt_hw_stack_frame *regs, rt_ubase_t xcause, rt_ubase_t tval)
{
struct rt_lwp *lwp;
enum rt_mm_fault_op fault_op;
enum rt_mm_fault_type fault_type;
switch (xcause)
{
case EXC_LOAD_PAGE_FAULT:
fault_op = MM_FAULT_OP_READ;
fault_type = MM_FAULT_TYPE_GENERIC_MMU;
break;
case EXC_LOAD_ACCESS:
fault_op = MM_FAULT_OP_READ;
fault_type = MM_FAULT_TYPE_BUS_ERROR;
break;
case EXC_LOAD_MISALIGNED:
fault_op = MM_FAULT_OP_READ;
fault_type = MM_FAULT_TYPE_BUS_ERROR;
break;
case EXC_STORE_PAGE_FAULT:
fault_op = MM_FAULT_OP_WRITE;
fault_type = MM_FAULT_TYPE_GENERIC_MMU;
break;
case EXC_STORE_ACCESS:
fault_op = MM_FAULT_OP_WRITE;
fault_type = MM_FAULT_TYPE_BUS_ERROR;
break;
case EXC_STORE_MISALIGNED:
fault_op = MM_FAULT_OP_WRITE;
fault_type = MM_FAULT_TYPE_BUS_ERROR;
break;
case EXC_INST_PAGE_FAULT:
fault_op = MM_FAULT_OP_EXECUTE;
fault_type = MM_FAULT_TYPE_GENERIC_MMU;
break;
case EXC_INST_ACCESS:
fault_op = MM_FAULT_OP_EXECUTE;
fault_type = MM_FAULT_TYPE_BUS_ERROR;
break;
case EXC_INST_MISALIGNED:
fault_op = MM_FAULT_OP_EXECUTE;
fault_type = MM_FAULT_TYPE_BUS_ERROR;
break;
default:
fault_op = 0;
break;
}
if (fault_op)
{
rt_base_t saved_stat;
struct rt_aspace_fault_msg msg =
{
.fault_op = fault_op,
.fault_type = fault_type,
.fault_vaddr = (void *)tval,
};
lwp = lwp_self();
saved_stat = csr_read_set(CSR_STATUS, SR_IE);
if (lwp && rt_aspace_fault_try_fix(lwp->aspace, &msg))
{
csr_write(CSR_STATUS, saved_stat);
return RT_TRUE;
}
csr_write(CSR_STATUS, saved_stat);
}
return RT_FALSE;
}
#endif /* RT_USING_LWP */
#ifdef ARCH_RISCV_VECTOR
rt_inline void vector_enable(struct rt_hw_stack_frame *regs)
{
regs->xstatus |= SR_VS_INITIAL;
}
/**
* detect V/D support, and do not distinguish V/D instruction
*/
rt_inline rt_bool_t illegal_inst_recoverable(struct rt_hw_stack_frame *regs, rt_ubase_t tval)
{
/* first 7 bits is opcode */
int opcode = tval & 0x7f;
int csr = (tval & 0xfff00000) >> 20;
/* ref riscv-v-spec-1.0, [Vector Instruction Formats] */
int width = ((tval & 0x7000) >> 12) - 1;
switch (opcode)
{
case 0x57: /* V */
case 0x27: /* scalar FLOAT */
case 0x07:
case 0x73: /* CSR */
vector_enable(regs);
return RT_TRUE;
default:
break;
}
return RT_FALSE;
}
#endif /* ARCH_RISCV_VECTOR */
/**
* this function will show registers of CPU
*
* @param regs the registers point
*/
void rt_hw_show_register(struct rt_hw_stack_frame *regs)
{
#ifndef ARCH_RISCV_M_MODE
const char *sv_mode;
rt_ubase_t satp = csr_read(CSR_SATP);
switch (satp & SATP_MODE_MASK)
{
case SATP_MODE_BARE:
sv_mode = "Bare";
break;
#if __riscv_xlen == 32
case SATP_MODE_32:
sv_mode = "Sv32";
break;
#else
case SATP_MODE_39:
sv_mode = "Sv39";
break;
case SATP_MODE_48:
sv_mode = "Sv48";
break;
case SATP_MODE_57:
sv_mode = "Sv57";
break;
case SATP_MODE_64:
sv_mode = "Sv64";
break;
#endif /* __riscv_xlen == 32 */
default:
sv_mode = "Reserved";
break;
}
#endif /* !ARCH_RISCV_M_MODE */
#if __riscv_xlen == 64
#define F "0x%016lx"
#elif __riscv_xlen == 32
#define F "0x%08lx"
#else
#error "Unexpected __riscv_xlen"
#endif
rt_kprintf("Execption:\n");
rt_kprintf("x0/zero: " F " x1/ra : " F " x2/sp : " F " x3/gp : " F "\n", 0UL, regs->ra, (rt_ubase_t)(regs + 1), regs->gp);
rt_kprintf("x4/tp : " F " x5/t0 : " F " x6/t1 : " F " x7/t2 : " F "\n", regs->tp, regs->t0, regs->t1, regs->t2);
rt_kprintf("x8/s0 : " F " x9/s1 : " F " x10/a0 : " F " x11/a1 : " F "\n", regs->s0_fp, regs->s1, regs->a0, regs->a1);
rt_kprintf("x12/a2 : " F " x13/a3: " F " x14/a4 : " F " x15/a5 : " F "\n", regs->a2, regs->a3, regs->a4, regs->a5);
#ifndef ARCH_RISCV_32E
rt_kprintf("x16/a6 : " F " x17/a7: " F " x18/s2 : " F " x19/s3 : " F "\n", regs->a6, regs->a7, regs->s2, regs->s3);
rt_kprintf("x20/s4 : " F " x21/s5: " F " x22/s6 : " F " x23/s7 : " F "\n", regs->s4, regs->s5, regs->s6, regs->s7);
rt_kprintf("x24/s8 : " F " x25/s9: " F " x26/s10: " F " x27/s11: " F "\n", regs->s8, regs->s9, regs->s10, regs->s11);
rt_kprintf("x28/t3 : " F " x29/t4: " F " x30/t5 : " F " x31/t6 : " F "\n", regs->t3, regs->t4, regs->t5, regs->t6);
#endif /* !ARCH_RISCV_32E */
#ifdef ARCH_RISCV_M_MODE
rt_kprintf("status : " F " (%s %s %s)\n", regs->xstatus,
(regs->xstatus & SR_IE) ? "IE" : "--",
(regs->xstatus & SR_PIE) ? "PIE" : "---",
(regs->xstatus & SR_PP) ? "PP" : "--");
#else
rt_kprintf("status : " F " (%s %s %s %s %s)\n", regs->xstatus,
(regs->xstatus & SR_IE) ? "IE" : "--",
(regs->xstatus & SR_PIE) ? "PIE" : "---",
(regs->xstatus & SR_PP) ? "PP" : "--",
(regs->xstatus & SR_SUM) ? "SUM" : "---",
(regs->xstatus & SR_MXR) ? "MXR" : "---");
rt_kprintf("satp : " F " (ASID: " F ", %s)\n", satp, (satp >> SATP_ASID_SHIFT) & SATP_ASID_MASK, sv_mode);
#endif
rt_kprintf("cause : " F "\n", regs->xcause);
rt_kprintf("pc : " F "\n", regs->epc);
#undef F
}
static const char * const irq_name[] =
{
[IRQ_S_SOFT] = "Software Interrupt (User)",
[IRQ_VS_SOFT] = "Software Interrupt (Supervisor)",
[IRQ_M_SOFT] = "Software Interrupt (Machine)",
[IRQ_S_TIMER] = "Timer Interrupt (User)",
[IRQ_VS_TIMER] = "Timer Interrupt (Supervisor)",
[IRQ_M_TIMER] = "Timer Interrupt (Machine)",
[IRQ_S_EXT] = "External Interrupt (User)",
[IRQ_VS_EXT] = "External Interrupt (Supervisor)",
[IRQ_M_EXT] = "External Interrupt (Machine)",
[IRQ_S_GEXT] = "External Interrupt (Supervisor Guest)",
[IRQ_PMU_OVF] = "Performance Monitor Unit Overflow",
};
static const char * const exception_name[] =
{
[EXC_INST_MISALIGNED] = "INST Misaligned",
[EXC_INST_ACCESS] = "INST Access",
[EXC_INST_ILLEGAL] = "INST Illegal",
[EXC_BREAKPOINT] = "Breakpoint",
[EXC_LOAD_MISALIGNED] = "Load Misaligned",
[EXC_LOAD_ACCESS] = "Load Access",
[EXC_STORE_MISALIGNED] = "Store Misaligned",
[EXC_STORE_ACCESS] = "Store Access",
[EXC_SYSCALL] = "Syscall",
[EXC_HYPERVISOR_SYSCALL] = "Hypervisor Syscall",
[EXC_SUPERVISOR_SYSCALL] = "Supervisor Syscall",
[EXC_INST_PAGE_FAULT] = "INST Page Fault",
[EXC_LOAD_PAGE_FAULT] = "Load Page Fault",
[EXC_STORE_PAGE_FAULT] = "Store Page Fault",
[EXC_INST_GUEST_PAGE_FAULT] = "INST Guest Page Fault",
[EXC_LOAD_GUEST_PAGE_FAULT] = "Load Guest Page Fault",
[EXC_VIRTUAL_INST_FAULT] = "Virtual INST Fault",
[EXC_STORE_GUEST_PAGE_FAULT] = "Store Guest Page Fault",
};
void rt_hw_trap(struct rt_hw_stack_frame *regs)
{
rt_ubase_t xcause = regs->xcause;
rt_ubase_t tval = csr_read_clear(CSR_TVAL, 0);
/* IRQ */
if (xcause & CAUSE_IRQ_FLAG)
{
rt_ubase_t cause;
rt_interrupt_enter();
xcause &= ~CAUSE_IRQ_FLAG;
cause = xcause & 0xfff;
if (cause == RV_IRQ_TIMER)
{
riscv_timer_isr();
goto _irq_done;
}
else if (cause == RV_IRQ_SOFT
#if defined(RT_PIC_RISCV_CLIC) || INTC_TYPE == INTC_TYPE_CLIC
|| cause >= RV_IRQ_EXT)
#else
|| cause == RV_IRQ_EXT)
#endif
{
#ifdef RT_USING_DM
if (!rt_pic_do_traps())
{
goto _irq_done;
}
#else
rt_hw_interrupt_handle(cause, regs);
goto _irq_done;
#endif
}
LOG_E("Unhandle IRQ: (%d) %s", xcause,
xcause >= sizeof(irq_name) / sizeof(irq_name[0]) ? "Reserved" : irq_name[xcause]);
_irq_done:
rt_interrupt_leave();
}
/* Execption */
else
{
struct rt_hw_backtrace_frame frame;
switch (xcause)
{
case EXC_INST_ILLEGAL:
#ifdef ARCH_RISCV_VECTOR
if (!(regs->xstatus & SR_VS))
{
if (illegal_inst_recoverable(regs, tval))
{
return;
}
}
#endif /* ARCH_RISCV_VECTOR */
break;
case EXC_INST_MISALIGNED:
case EXC_INST_ACCESS:
case EXC_LOAD_MISALIGNED:
case EXC_LOAD_ACCESS:
case EXC_STORE_MISALIGNED:
case EXC_STORE_ACCESS:
case EXC_INST_PAGE_FAULT:
case EXC_LOAD_PAGE_FAULT:
case EXC_STORE_PAGE_FAULT:
#ifdef RT_USING_SMART
if (!(regs->xstatus & SR_PP) ||
(tval >= USER_VADDR_START && tval < USER_VADDR_TOP))
{
if (user_fault_fixable(regs, xcause, tval))
{
return;
}
}
#endif /* RT_USING_SMART */
default:
LOG_E("Unhandle Execption: (%d) %s, TVAL = %p", xcause,
xcause >= sizeof(exception_name) / sizeof(exception_name[0]) ? "Reserved" : exception_name[xcause],
tval);
}
rt_hw_show_register(regs);
if (rt_thread_self())
{
LOG_E("current thread: %s\n", rt_thread_self()->parent.name);
#ifdef RT_USING_FINSH
list_thread();
#endif
#ifdef RT_USING_LWP
_check_fault(regs, 0, "user fault");
#endif
}
frame.fp = regs->s0_fp;
frame.pc = regs->epc;
rt_backtrace_frame(rt_thread_self(), &frame);
rt_hw_cpu_shutdown();
}
}
|