feat: (vm) memory mapping support: mmap/munmap
[lunaix-os.git] / lunaix-os / kernel / asm / x86 / pfault.c
1 #include <arch/x86/interrupts.h>
2 #include <lunaix/common.h>
3 #include <lunaix/lxsignal.h>
4 #include <lunaix/mm/mm.h>
5 #include <lunaix/mm/pmm.h>
6 #include <lunaix/mm/region.h>
7 #include <lunaix/mm/vmm.h>
8 #include <lunaix/sched.h>
9 #include <lunaix/status.h>
10 #include <lunaix/syslog.h>
11
12 static void
13 kprintf(const char* fmt, ...)
14 {
15     va_list args;
16     va_start(args, fmt);
17     __kprintf("PFAULT", fmt, args);
18     va_end(args);
19 }
20
21 #define COW_MASK (REGION_RSHARED | REGION_READ | REGION_WRITE)
22
23 extern void
24 __print_panic_msg(const char* msg, const isr_param* param);
25
26 void
27 intr_routine_page_fault(const isr_param* param)
28 {
29     uintptr_t ptr = cpu_rcr2();
30     if (!ptr) {
31         goto segv_term;
32     }
33
34     v_mapping mapping;
35     if (!vmm_lookup(ptr, &mapping)) {
36         goto segv_term;
37     }
38
39     if (!SEL_RPL(param->cs)) {
40         // 如果是内核页错误……
41         if (do_kernel(&mapping)) {
42             return;
43         }
44         // 如果不是,那么看看内核是不是需要用户页。
45     }
46
47     struct mm_region* hit_region = region_get(&__current->mm.regions, ptr);
48
49     if (!hit_region) {
50         // 当你凝视深渊时……
51         goto segv_term;
52     }
53
54     volatile x86_pte_t* pte = &PTE_MOUNTED(PD_REFERENCED, ptr >> 12);
55     if ((*pte & PG_PRESENT)) {
56         if ((hit_region->attr & COW_MASK) == COW_MASK) {
57             // normal page fault, do COW
58             cpu_invplg(pte);
59             uintptr_t pa =
60               (uintptr_t)vmm_dup_page(__current->pid, PG_ENTRY_ADDR(*pte));
61             pmm_free_page(__current->pid, *pte & ~0xFFF);
62             *pte = (*pte & 0xFFF) | pa | PG_WRITE;
63             goto resolved;
64         }
65         // impossible cases or accessing privileged page
66         goto segv_term;
67     }
68
69     // if (!(*pte)) {
70     //     // Invalid location
71     //     goto segv_term;
72     // }
73
74     uintptr_t loc = *pte & ~0xfff;
75
76     // a writable page, not present, not cached, pte attr is not null
77     //   -> a new page need to be alloc
78     if ((hit_region->attr & REGION_WRITE) && (*pte & 0xfff) && !loc) {
79         cpu_invplg(pte);
80         uintptr_t pa = pmm_alloc_page(__current->pid, 0);
81         if (!pa) {
82             goto oom;
83         }
84
85         *pte = *pte | pa | PG_PRESENT;
86         goto resolved;
87     }
88
89     // if mfile is set, then it is a mem map
90     if (hit_region->mfile) {
91         struct v_file* file = hit_region->mfile;
92         u32_t offset =
93           (ptr - hit_region->start) & (PG_SIZE - 1) + hit_region->offset;
94         uintptr_t pa = pmm_alloc_page(__current->pid, 0);
95
96         if (!pa) {
97             goto oom;
98         }
99
100         cpu_invplg(pte);
101         *pte = *pte | pa | PG_PRESENT;
102         int errno = file->ops->read_page(
103           file->inode, ptr & (PG_SIZE - 1), PG_SIZE, offset);
104         if (errno < 0) {
105             kprintf(KERROR "fail to read page (%d)\n", errno);
106             goto segv_term;
107         }
108         goto resolved;
109     }
110
111     // page not present, bring it from disk or somewhere else
112     __print_panic_msg("WIP page fault route", param);
113     while (1)
114         ;
115
116 oom:
117     kprintf(KERROR "out of memory\n");
118 segv_term:
119     kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p)\n",
120             __current->pid,
121             ptr,
122             param->cs,
123             param->eip);
124     __SIGSET(__current->sig_pending, _SIGSEGV);
125     schedule();
126     // should not reach
127     while (1)
128         ;
129
130 resolved:
131     cpu_invplg(ptr);
132     return;
133 }
134
135 int
136 do_kernel(v_mapping* mapping)
137 {
138     uintptr_t addr = mapping->va;
139
140     // TODO
141
142     return 0;
143 done:
144     return 1;
145 }