Unifying the Lunaix's Physical Memory Model (#28)
[lunaix-os.git] / lunaix-os / kernel / debug / trace.c
index 1cf9ae34d37295a7251058d80813c978c7e84e12..173b1cd09d0d14b57d60d748f4297b011ff87d12 100644 (file)
@@ -1,12 +1,12 @@
 #include <lunaix/mm/page.h>
 #include <lunaix/mm/page.h>
-#include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 #include <lunaix/trace.h>
 
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 #include <lunaix/trace.h>
 
-#include <sys/cpu.h>
-#include <sys/mm/mempart.h>
+#include <sys/abi.h>
+#include <sys/mm/mm_defs.h>
+#include <sys/trace.h>
 
 #include <klibc/string.h>
 
 
 #include <klibc/string.h>
 
@@ -23,11 +23,12 @@ trace_modksyms_init(struct boot_handoff* bhctx)
     for (size_t i = 0; i < bhctx->mods.mods_num; i++) {
         struct boot_modent* mod = &bhctx->mods.entries[i];
         if (streq(mod->str, "modksyms")) {
     for (size_t i = 0; i < bhctx->mods.mods_num; i++) {
         struct boot_modent* mod = &bhctx->mods.entries[i];
         if (streq(mod->str, "modksyms")) {
-            assert(PG_ALIGNED(mod->start));
+            assert(!va_offset(mod->start));
 
 
-            ptr_t end = ROUNDUP(mod->end, PG_SIZE);
-            ptr_t ksym_va =
-              (ptr_t)vmap(mod->start, (end - mod->start), PG_PREM_R, 0);
+            pte_t pte = mkpte(mod->start, KERNEL_DATA);
+            size_t n = pfn(mod->end) - pfn(mod->start);
+            
+            ptr_t ksym_va = vmap_leaf_ptes(pte, n);
 
             assert(ksym_va);
             trace_ctx.ksym_table = (struct ksyms*)ksym_va;
 
             assert(ksym_va);
             trace_ctx.ksym_table = (struct ksyms*)ksym_va;
@@ -43,7 +44,7 @@ trace_sym_lookup(ptr_t addr)
 
     int i = c - 1, j = 0, m = 0;
 
 
     int i = c - 1, j = 0, m = 0;
 
-    if (addr > ksent[i].pc || addr < ksent[j].pc || addr < KERNEL_EXEC) {
+    if (addr > ksent[i].pc || addr < ksent[j].pc || !kernel_addr(addr)) {
         return NULL;
     }
 
         return NULL;
     }
 
@@ -77,6 +78,13 @@ ksym_getstr(struct ksym_entry* sym)
                    trace_ctx.ksym_table->ksym_label_off + sym->label_off);
 }
 
                    trace_ctx.ksym_table->ksym_label_off + sym->label_off);
 }
 
+static inline bool valid_fp(ptr_t ptr) {
+    ptr_t start = ROUNDUP(current_thread->kstack - KSTACK_SIZE, MEM_PAGE);
+
+    return (start < ptr && ptr < current_thread->kstack) 
+           || arch_valid_fp(ptr);
+}
+
 int
 trace_walkback(struct trace_record* tb_buffer,
                ptr_t fp,
 int
 trace_walkback(struct trace_record* tb_buffer,
                ptr_t fp,
@@ -87,8 +95,8 @@ trace_walkback(struct trace_record* tb_buffer,
     struct ksym_entry* current = NULL;
     int i = 0;
 
     struct ksym_entry* current = NULL;
     int i = 0;
 
-    while (frame && i < limit) {
-        ptr_t pc = *(frame + 1);
+    while (valid_fp((ptr_t)frame) && i < limit) {
+        ptr_t pc = abi_get_retaddrat((ptr_t)frame);
 
         current = trace_sym_lookup(pc);
         tb_buffer[i] =
 
         current = trace_sym_lookup(pc);
         tb_buffer[i] =
@@ -100,6 +108,10 @@ trace_walkback(struct trace_record* tb_buffer,
         i++;
     }
 
         i++;
     }
 
+    if (!valid_fp((ptr_t)frame)) {
+        frame = NULL;
+    }
+
     if (last_fp) {
         *last_fp = (ptr_t)frame;
     }
     if (last_fp) {
         *last_fp = (ptr_t)frame;
     }
@@ -110,7 +122,11 @@ trace_walkback(struct trace_record* tb_buffer,
 static inline void
 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
 {
 static inline void
 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
 {
-    DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
+    if (sym_pc) {
+        DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
+    } else {
+        DEBUG("%p+%p: %s", inst_pc, sym_pc, sym);
+    }
 }
 
 void
 }
 
 void
@@ -134,19 +150,25 @@ trace_printstack_of(ptr_t fp)
 void
 trace_printstack()
 {
 void
 trace_printstack()
 {
-    trace_printstack_of(cpu_get_fp());
+    if (current_thread) {
+        trace_printstack_isr(current_thread->intr_ctx);
+    }
+    else {
+        trace_printstack_of(abi_get_callframe());
+    }
 }
 
 static void
 }
 
 static void
-trace_printswctx(const isr_param* p, char* direction)
+trace_printswctx(const isr_param* p, bool from_usr, bool to_usr)
 {
 
     struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
 
 {
 
     struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
 
-    DEBUG(">> (sw:%s) iv:%d, errno:%p <<",
-          direction,
+    DEBUG("^^^^^ --- %s", to_usr ? "user" : "kernel");
+    DEBUG("  interrupted on #%d, ecode=%p",
           p->execp->vector,
           p->execp->err_code);
           p->execp->vector,
           p->execp->err_code);
+    DEBUG("vvvvv --- %s", from_usr ? "user" : "kernel");
 
     ptr_t sym_pc = sym ? sym->pc : p->execp->eip;
     trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
 
     ptr_t sym_pc = sym ? sym->pc : p->execp->eip;
     trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
@@ -156,29 +178,36 @@ void
 trace_printstack_isr(const isr_param* isrm)
 {
     isr_param* p = isrm;
 trace_printstack_isr(const isr_param* isrm)
 {
     isr_param* p = isrm;
-    ptr_t fp = cpu_get_fp();
-    int prev_fromusr = 0;
+    ptr_t fp = abi_get_callframe();
+    int prev_usrctx = 0;
 
     DEBUG("stack trace (pid=%d)\n", __current->pid);
 
     trace_printstack_of(fp);
 
     while (p) {
 
     DEBUG("stack trace (pid=%d)\n", __current->pid);
 
     trace_printstack_of(fp);
 
     while (p) {
-        if (!prev_fromusr) {
-            if (uspace_context(p)) {
-                trace_printswctx(p, "s/u");
+        if (!prev_usrctx) {
+            if (!kernel_context(p)) {
+                trace_printswctx(p, true, false);
             } else {
             } else {
-                trace_printswctx(p, "s/s");
+                trace_printswctx(p, false, false);
             }
         } else {
             }
         } else {
-            trace_printswctx(p, "u/s");
+            trace_printswctx(p, false, true);
         }
 
         fp = saved_fp(p);
         }
 
         fp = saved_fp(p);
+        if (!valid_fp(fp)) {
+            DEBUG("??? invalid frame: %p", fp);
+            break;
+        }
+
         trace_printstack_of(fp);
 
         trace_printstack_of(fp);
 
-        prev_fromusr = uspace_context(p);
+        prev_usrctx = !kernel_context(p);
 
         p = p->execp->saved_prev_ctx;
     }
 
         p = p->execp->saved_prev_ctx;
     }
+
+    DEBUG("----- [trace end] -----\n");
 }
\ No newline at end of file
 }
\ No newline at end of file