Test debugging code with interrupts.
[riscv-tests.git] / debug / programs / init.c
index 074bc2178de4ca96a0152186099d2d86339aa8de..8b047de591405c7a4ce180bbcc358012d5948545 100644 (file)
@@ -1,14 +1,47 @@
+#include "init.h"
+#include "encoding.h"
+
 int main(void);
 
-void handle_trap(unsigned int mcause, unsigned int mepc, unsigned int sp)
+trap_handler_t trap_handler[NHARTS] = {0};
+
+void set_trap_handler(trap_handler_t handler)
+{
+    unsigned hartid = csr_read(mhartid);
+    trap_handler[hartid] = handler;
+}
+
+void enable_timer_interrupts()
+{
+    set_csr(mie, MIP_MTIP);
+    set_csr(mstatus, MSTATUS_MIE);
+}
+
+void handle_trap(unsigned int mcause, void *mepc, void *sp)
 {
+    unsigned hartid = csr_read(mhartid);
+    if (trap_handler[hartid]) {
+        trap_handler[hartid](hartid, mcause, mepc, sp);
+        return;
+    }
+
     while (1)
         ;
 }
 
-void _init()
+void _exit(int status)
 {
-    main();
+    // Make sure gcc doesn't inline _exit, so we can actually set a breakpoint
+    // on it.
+    volatile int i = 42;
+    while (i)
+        ;
+    // _exit isn't supposed to return.
     while (1)
         ;
 }
+
+void _init()
+{
+    _exit(main());
+}