SYSCALL naked assembly working
[cortex-from-scratch] / ivt.c
diff --git a/ivt.c b/ivt.c
index 9f8e643..0e2d1f2 100644 (file)
--- a/ivt.c
+++ b/ivt.c
@@ -30,6 +30,7 @@
 #include <lib/stdio.h>
 #include <lib/string.h>
 #include <lib/regfunc.h>
+#include <lib/tinyprintf.h>
 
 /* 
  * These values are pushed on the stack just before
@@ -75,6 +76,9 @@ char * messages[] = {
     "BUS FAULT",
     "USAGE FAULT",
     "RESERVED",
+    "RESERVED",
+    "RESERVED",
+    "RESERVED",
     "SVC",
     "DEBUG MONITOR",
     "RESERVED",
@@ -92,13 +96,15 @@ char * messages[] = {
 
 if (intnr < 20) // TODO: strlen
        return messages[intnr];
-       return NULL;
+
+return "UNKNOWN";
 }
 
 void ivt_set_gate(unsigned char num, void * isr(), short pri) {
 
        ivt[num] = (uint32_t) isr;
-       *NVIC_ISER0 = (1 << ((uint32_t)(num) & 0x1F));
+//     if (num <= 32)
+//             *NVIC_ISER0 = (1 << ((uint32_t)(num) & 0x1F));
        /* TODO: Priorities */
 }
 
@@ -106,16 +112,58 @@ void ivt_set_gate(unsigned char num, void * isr(), short pri) {
 /* Dummy interrupt: comment out the comment to use a naked
  * function */
 
-// __attribute__ ((interrupt)) 
-void * dummy_isr(/* struct interrupt_frame * frame */) {
+struct interrupt_frame * frame;
 
-       uint8_t nr = *SCB_VTOR_ST & 0xFF;
+//__attribute__ ((interrupt)) 
+void * dummy_isr( struct interrupt_frame * f ) {
+
+       //uint32_t link_register = 0;
+       //asm ("mov %0, #33" : "=r" (lp));
        
-       cputs("EXCEPTION: ");
-       cputs(exception_message(nr));
-       cputs("\nSYSTEM HALTED\n");
+       /* Check Link Register:
+        * If you ever need to debug these: here are the most common
+        * values
+        * 0xFFFFFFF1: return to handler mode (nested interrupts
+        * 0xFFFFFFF9: return to thread mode using main stack
+        * 0xFFFFFFFD: return to thread mode using process stack */
+       // asm ("mov %0, lr" : "=r" (link_register));
+       // printf("%x\n", link_register);
+       // for(;;);
+
+//     asm volatile ("push {r0-r12}"); 
+//     int * stack;
+//     asm volatile ("tst lr, #4" "\n\t" 
+//     "ite eq" "\n\t"
+//     "mrseq %0, msp" "\n\t" 
+//     "mrsne r0, psp" : "=r" (stack));
+//     
+//     printf("STACK:  %x, %x,  %x, %x, %x", stack[32], stack[28], stack[24], stack[20], stack[16]);
+
+//     asm volatile ("CPSID f");
+
+//     uint32_t tmp = args[0];
+//     uint32_t tmp2 = args[1];
+//     printf("%x, %x\n", tmp, tmp2); 
+//     uint32_t tmp3 = args[2];
+//     uint32_t tmp4 = args[3];
+//     printf("%x, %x\n", tmp3, tmp4); 
+
+       struct interrupt_frame * frame = (struct interrupt_frame * )kalloc(get_kheap());
+       memcpy(frame, f, sizeof(struct interrupt_frame));
+
+       uint8_t nr = *SCB_VTOR_ST & 0xFF;
+       printf("EXCEPTION: %s\n", exception_message(nr));
+       printf("STACK TRACE:\n");
+       printf("R0:%p\n",frame->r0);
+       printf("R1:%p\n",frame->r1);
+       printf("R2:%p\n",frame->r2);
+       printf("R3:%p\n",frame->r3);
+       printf("R12:%p\n",frame->r12);
+       printf("LR:%p\n",frame->lr);
+       printf("PC:%p\n",frame->pc);
+       printf("PSR:%p\n",frame->psr);
        
-       for(;;);
+       for(;;); 
 }
 
 /* Initialize interrupt vector  */
@@ -139,6 +187,6 @@ void ivt_init() {
         * relocated to other memory locations. We can do this by setting 
         * a register in the NVIC called the vector table offset register */
 
-       regw_u32(SCB_VTOR, (uint32_t) &ivt, 0, OWRITE);
+       rwrite(SCB_VTOR, (uint32_t) &ivt);
 
 }