2026-02-08 22:41:38 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <drivers/uart.h>
|
|
|
|
|
#include <syscon/syscon.h>
|
2026-02-09 15:06:49 +00:00
|
|
|
#include "memory.h"
|
2026-02-08 22:41:38 +00:00
|
|
|
|
|
|
|
|
void kpanic(const char *reason){
|
2026-02-09 00:37:28 +00:00
|
|
|
kputs("\n!!! PANIC !!!\n");
|
|
|
|
|
kputs(reason);
|
|
|
|
|
kputs("\n!!! PANIC !!!\n");
|
2026-02-08 22:41:38 +00:00
|
|
|
poweroff();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void kpanic_force() {
|
|
|
|
|
kprint("\n!!! FORCE PANIC !!!\n");
|
|
|
|
|
|
|
|
|
|
// 'ebreak' is the standard RISC-V way to trigger a debug trap.
|
|
|
|
|
__asm__ volatile("ebreak");
|
|
|
|
|
|
2026-02-09 00:37:28 +00:00
|
|
|
kprint("Force panic falled. Power off. \n");
|
2026-02-08 22:41:38 +00:00
|
|
|
poweroff();
|
|
|
|
|
|
2026-02-09 00:37:28 +00:00
|
|
|
kprint("Force panic falled. Power off failed. sleep until interupt. \n");
|
2026-02-08 22:41:38 +00:00
|
|
|
while(1) {
|
|
|
|
|
__asm__ volatile("wfi");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-09 02:57:04 +00:00
|
|
|
|
|
|
|
|
void handle_trap() {
|
|
|
|
|
kprint("\n!!! HARDWARE EXCEPTION DETECTED !!!\n");
|
|
|
|
|
|
|
|
|
|
// Read the 'mcause' register to see WHY we trapped
|
|
|
|
|
unsigned long cause;
|
|
|
|
|
__asm__ volatile("csrr %0, mcause" : "=r"(cause));
|
|
|
|
|
|
2026-02-09 03:28:54 +00:00
|
|
|
// fault address (if applicable)
|
|
|
|
|
uintptr_t mtval;
|
|
|
|
|
asm volatile("csrr %0, mtval" : "=r"(mtval));
|
|
|
|
|
|
2026-02-09 02:57:04 +00:00
|
|
|
switch(cause) {
|
|
|
|
|
case 0: kprint("Reason: Instruction Address Misaligned\n"); break;
|
|
|
|
|
case 1: kprint("Reason: Instruction Access Fault\n"); break;
|
|
|
|
|
case 2: kprint("Reason: Illegal Instruction\n"); break;
|
|
|
|
|
case 3: kprint("Reason: Breakpoint (ebreak)\n"); break;
|
|
|
|
|
case 4: kprint("Reason: Load Address Misaligned\n"); break;
|
|
|
|
|
case 5: kprint("Reason: Load Access Fault\n"); break;
|
|
|
|
|
case 6: kprint("Reason: Store/AMO Address Misaligned\n"); break;
|
|
|
|
|
case 7: kprint("Reason: Store/AMO Access Fault\n"); break;
|
|
|
|
|
default:
|
|
|
|
|
kprintf("Reason: Unknown Exception Code %d\n", cause);
|
|
|
|
|
}
|
2026-02-09 15:32:24 +00:00
|
|
|
kprintf("Faulting Address (if applicable): %x\n", mtval);
|
2026-02-09 02:57:04 +00:00
|
|
|
|
2026-02-09 15:06:49 +00:00
|
|
|
heap_stats();
|
|
|
|
|
|
2026-02-09 02:57:04 +00:00
|
|
|
poweroff();
|
|
|
|
|
}
|