refactor: rewrite kernel's make script
[lunaix-os.git] / lunaix-os / kernel / asm / x86 / pfault.c
index 0d5a6fa661b908d901e625a5911f61dddabd6f3a..70d5e560da7a145ca1b154f9e52cafd749e38d26 100644 (file)
@@ -28,6 +28,7 @@ __print_panic_msg(const char* msg, const isr_param* param);
 void
 intr_routine_page_fault(const isr_param* param)
 {
+    uint32_t errcode = param->execp->err_code;
     ptr_t ptr = cpu_rcr2();
     if (!ptr) {
         goto segv_term;
@@ -50,6 +51,11 @@ intr_routine_page_fault(const isr_param* param)
         goto segv_term;
     }
 
+    if ((errcode & PG_ALLOW_USER)) {
+        // invalid access
+        goto segv_term;
+    }
+
     volatile x86_pte_t* pte = &PTE_MOUNTED(VMS_SELF, ptr >> 12);
     if (PG_IS_PRESENT(*pte)) {
         if ((hit_region->attr & COW_MASK) == COW_MASK) {
@@ -78,7 +84,7 @@ intr_routine_page_fault(const isr_param* param)
                 goto oom;
             }
 
-            *pte = *pte | pa | PG_PRESENT;
+            *pte = *pte | pa | PG_PRESENT | PG_ALLOW_USER;
             memset((void*)PG_ALIGN(ptr), 0, PG_SIZE);
             goto resolved;
         }
@@ -101,7 +107,7 @@ intr_routine_page_fault(const isr_param* param)
         }
 
         cpu_invplg((ptr_t)pte);
-        *pte = (*pte & 0xFFF) | pa | PG_PRESENT;
+        *pte = (*pte & 0xFFF) | pa | PG_PRESENT | PG_ALLOW_USER;
 
         memset((void*)ptr, 0, PG_SIZE);
 
@@ -128,13 +134,17 @@ intr_routine_page_fault(const isr_param* param)
 
 oom:
     kprintf(KERROR "out of memory\n");
+
 segv_term:
-    kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p)\n",
+    kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p,e=0x%x)\n",
             __current->pid,
             ptr,
             param->execp->cs,
-            param->execp->eip);
+            param->execp->eip,
+            param->execp->err_code);
+
     __SIGSET(__current->sig_pending, _SIGSEGV);
+
     schedule();
     // should not reach
     while (1)