refactor: rewrite kernel's make script
[lunaix-os.git] / lunaix-os / kernel / asm / x86 / pfault.c
1 #include <arch/x86/interrupts.h>
2 #include <lunaix/common.h>
3 #include <lunaix/mm/mm.h>
4 #include <lunaix/mm/pmm.h>
5 #include <lunaix/mm/region.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/sched.h>
8 #include <lunaix/signal.h>
9 #include <lunaix/status.h>
10 #include <lunaix/syslog.h>
11
12 #include <klibc/string.h>
13
14 static void
15 kprintf(const char* fmt, ...)
16 {
17     va_list args;
18     va_start(args, fmt);
19     __kprintf("PFAULT", fmt, args);
20     va_end(args);
21 }
22
23 #define COW_MASK (REGION_RSHARED | REGION_READ | REGION_WRITE)
24
25 extern void
26 __print_panic_msg(const char* msg, const isr_param* param);
27
28 void
29 intr_routine_page_fault(const isr_param* param)
30 {
31     uint32_t errcode = param->execp->err_code;
32     ptr_t ptr = cpu_rcr2();
33     if (!ptr) {
34         goto segv_term;
35     }
36
37     v_mapping mapping;
38     if (!vmm_lookup(ptr, &mapping)) {
39         goto segv_term;
40     }
41
42     if (!SEL_RPL(param->execp->cs)) {
43         // TODO if kernel pfault
44     }
45
46     vm_regions_t* vmr = (vm_regions_t*)&__current->mm.regions;
47     struct mm_region* hit_region = region_get(vmr, ptr);
48
49     if (!hit_region) {
50         // 当你凝视深渊时……
51         goto segv_term;
52     }
53
54     if ((errcode & PG_ALLOW_USER)) {
55         // invalid access
56         goto segv_term;
57     }
58
59     volatile x86_pte_t* pte = &PTE_MOUNTED(VMS_SELF, ptr >> 12);
60     if (PG_IS_PRESENT(*pte)) {
61         if ((hit_region->attr & COW_MASK) == COW_MASK) {
62             // normal page fault, do COW
63             cpu_invplg((ptr_t)pte);
64
65             ptr_t pa = (ptr_t)vmm_dup_page(__current->pid, PG_ENTRY_ADDR(*pte));
66
67             pmm_free_page(__current->pid, *pte & ~0xFFF);
68             *pte = (*pte & 0xFFF & ~PG_DIRTY) | pa | PG_WRITE;
69
70             goto resolved;
71         }
72         // impossible cases or accessing privileged page
73         goto segv_term;
74     }
75
76     // an anonymous page and not present
77     //   -> a new page need to be alloc
78     if ((hit_region->attr & REGION_ANON)) {
79         if (!PG_IS_PRESENT(*pte)) {
80             cpu_invplg((ptr_t)pte);
81
82             ptr_t pa = pmm_alloc_page(__current->pid, 0);
83             if (!pa) {
84                 goto oom;
85             }
86
87             *pte = *pte | pa | PG_PRESENT | PG_ALLOW_USER;
88             memset((void*)PG_ALIGN(ptr), 0, PG_SIZE);
89             goto resolved;
90         }
91         // permission denied on anon page (e.g., write on readonly page)
92         goto segv_term;
93     }
94
95     // if mfile is set (Non-anonymous), then it is a mem map
96     if (hit_region->mfile && !PG_IS_PRESENT(*pte)) {
97         struct v_file* file = hit_region->mfile;
98
99         ptr = PG_ALIGN(ptr);
100
101         u32_t mseg_off = (ptr - hit_region->start);
102         u32_t mfile_off = mseg_off + hit_region->foff;
103         ptr_t pa = pmm_alloc_page(__current->pid, 0);
104
105         if (!pa) {
106             goto oom;
107         }
108
109         cpu_invplg((ptr_t)pte);
110         *pte = (*pte & 0xFFF) | pa | PG_PRESENT | PG_ALLOW_USER;
111
112         memset((void*)ptr, 0, PG_SIZE);
113
114         int errno = 0;
115         if (mseg_off < hit_region->flen) {
116             errno =
117               file->ops->read_page(file->inode, (void*)ptr, PG_SIZE, mfile_off);
118         }
119
120         if (errno < 0) {
121             kprintf(KERROR "fail to populate page (%d)\n", errno);
122             goto segv_term;
123         }
124
125         *pte &= ~PG_DIRTY;
126
127         goto resolved;
128     }
129
130     // page not present, might be a chance to introduce swap file?
131     __print_panic_msg("WIP page fault route", param);
132     while (1)
133         ;
134
135 oom:
136     kprintf(KERROR "out of memory\n");
137
138 segv_term:
139     kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p,e=0x%x)\n",
140             __current->pid,
141             ptr,
142             param->execp->cs,
143             param->execp->eip,
144             param->execp->err_code);
145
146     __SIGSET(__current->sig_pending, _SIGSEGV);
147
148     schedule();
149     // should not reach
150     while (1)
151         ;
152
153 resolved:
154     cpu_invplg(ptr);
155     return;
156 }