feat: heap support and re-worked
[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     uintptr_t ptr = cpu_rcr2();
32     if (!ptr) {
33         goto segv_term;
34     }
35
36     v_mapping mapping;
37     if (!vmm_lookup(ptr, &mapping)) {
38         goto segv_term;
39     }
40
41     if (!SEL_RPL(param->cs)) {
42         // 如果是内核页错误……
43         if (do_kernel(&mapping)) {
44             return;
45         }
46         // 如果不是,那么看看内核是不是需要用户页。
47     }
48
49     struct mm_region* hit_region = region_get(&__current->mm.regions, ptr);
50
51     if (!hit_region) {
52         // 当你凝视深渊时……
53         goto segv_term;
54     }
55
56     volatile x86_pte_t* pte = &PTE_MOUNTED(VMS_SELF, ptr >> 12);
57     if (PG_IS_PRESENT(*pte)) {
58         if ((hit_region->attr & COW_MASK) == COW_MASK) {
59             // normal page fault, do COW
60             cpu_invplg(pte);
61             uintptr_t pa =
62               (uintptr_t)vmm_dup_page(__current->pid, PG_ENTRY_ADDR(*pte));
63             pmm_free_page(__current->pid, *pte & ~0xFFF);
64             *pte = (*pte & 0xFFF & ~PG_DIRTY) | pa | PG_WRITE;
65             goto resolved;
66         }
67         // impossible cases or accessing privileged page
68         goto segv_term;
69     }
70
71     // an anonymous page and not present
72     //   -> a new page need to be alloc
73     if ((hit_region->attr & REGION_ANON)) {
74         if (!PG_IS_PRESENT(*pte)) {
75             cpu_invplg(pte);
76             uintptr_t pa = pmm_alloc_page(__current->pid, 0);
77             if (!pa) {
78                 goto oom;
79             }
80
81             *pte = *pte | pa | PG_PRESENT;
82             goto resolved;
83         }
84         // permission denied on anon page (e.g., write on readonly page)
85         goto segv_term;
86     }
87
88     // if mfile is set (Non-anonymous), then it is a mem map
89     if (hit_region->mfile && !PG_IS_PRESENT(*pte)) {
90         struct v_file* file = hit_region->mfile;
91         u32_t offset =
92           (ptr - hit_region->start) & (PG_SIZE - 1) + hit_region->foff;
93         uintptr_t pa = pmm_alloc_page(__current->pid, 0);
94
95         if (!pa) {
96             goto oom;
97         }
98
99         cpu_invplg(pte);
100         *pte = (*pte & 0xFFF) | pa | PG_PRESENT;
101
102         ptr = PG_ALIGN(ptr);
103         memset(ptr, 0, PG_SIZE);
104
105         int errno = 0;
106         if (hit_region->init_page) {
107             errno = hit_region->init_page(hit_region, ptr, offset);
108         } else {
109             errno = file->ops->read_page(file->inode, ptr, PG_SIZE, offset);
110         }
111
112         if (errno < 0) {
113             kprintf(KERROR "fail to populate page (%d)\n", errno);
114             goto segv_term;
115         }
116
117         *pte &= ~PG_DIRTY;
118
119         goto resolved;
120     }
121
122     // page not present, might be a chance to introduce swap file?
123     __print_panic_msg("WIP page fault route", param);
124     while (1)
125         ;
126
127 oom:
128     kprintf(KERROR "out of memory\n");
129 segv_term:
130     kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p)\n",
131             __current->pid,
132             ptr,
133             param->cs,
134             param->eip);
135     __SIGSET(__current->sig_pending, _SIGSEGV);
136     schedule();
137     // should not reach
138     while (1)
139         ;
140
141 resolved:
142     cpu_invplg(ptr);
143     return;
144 }
145
146 int
147 do_kernel(v_mapping* mapping)
148 {
149     uintptr_t addr = mapping->va;
150
151     // TODO
152
153     return 0;
154 done:
155     return 1;
156 }