Code-base clean-up and refactoring (#47)
[lunaix-os.git] / lunaix-os / arch / x86 / mm / fault.c
1 #include <lunaix/mm/fault.h>
2 #include <lunaix/hart_state.h>
3
4 bool
5 __arch_prepare_fault_context(struct fault_context* fault)
6 {
7     struct hart_state* ictx = fault->hstate;
8
9     ptr_t ptr = cpu_ldeaddr();
10     if (!ptr) {
11         return false;
12     }
13
14     fault->fault_ptep  = mkptep_va(VMS_SELF, ptr);
15     fault->fault_data  = ictx->execp->err_code;
16     fault->fault_instn = hart_pc(ictx);
17     fault->fault_va    = ptr;
18
19     return true;
20 }
21
22
23 void
24 intr_routine_page_fault(const struct hart_state* hstate)
25 {
26     if (hstate->depth > 10) {
27         // Too many nested fault! we must messed up something
28         // XXX should we failed silently?
29         spin();
30     }
31
32     struct fault_context fault = { .hstate = hstate };
33
34     if (!__arch_prepare_fault_context(&fault)) {
35         goto failed;
36     }
37
38     if (!handle_page_fault(&fault)) {
39         goto failed;
40     }
41
42     return;
43
44 failed:
45     fault_resolving_failed(&fault);
46 }