Merge branch 'master' into isa/arm64
[lunaix-os.git] / lunaix-os / arch / generic / includes / asm-generic / init_pagetable.h
1 #ifndef __LUNAIX_ARCH_GENERIC_INIT_PAGETABLE_H
2 #define __LUNAIX_ARCH_GENERIC_INIT_PAGETABLE_H
3
4 #include <lunaix/types.h>
5 #include <lunaix/mm/pagetable.h>
6
7 struct pt_alloc
8 {
9     ptr_t base;
10     int index;
11     int total;
12 };
13
14 struct ptw_state
15 {
16     struct pt_alloc* alloc;
17     pte_t* root;
18     pte_t* lntp;
19     int tab_index;
20     int level;
21 };
22
23 /**
24  * Allocate a page table from the page-table pool
25  */
26 ptr_t 
27 kpt_alloc_table(struct pt_alloc* alloc);
28
29 /**
30  * Set contiguous number of ptes starting from `addr`
31  * Using flattened apporach (i.e., unfold recursive)
32  */
33 unsigned int 
34 kpt_set_ptes_flatten(struct ptw_state* state, ptr_t addr, 
35                 pte_t pte, unsigned long lsize, unsigned int nr);
36
37 /**
38  * Remap the kernel to high-memory
39  */
40 void 
41 kpt_migrate_highmem(struct ptw_state* state);
42
43 static inline void must_inline
44 init_pt_alloc(struct pt_alloc* alloc, ptr_t pool, unsigned int size)
45 {
46     *alloc = (struct pt_alloc) {
47         .base = pool,
48         .index = 0,
49         .total = size / PAGE_SIZE
50     };
51 }
52
53 static inline void must_inline
54 init_ptw_state(struct ptw_state* state, struct pt_alloc* alloc, ptr_t root)
55 {
56     *state = (struct ptw_state) {
57         .alloc = alloc,
58         .root  = (pte_t*) root,
59         .level = 0
60     };
61 }
62
63 /**
64  * set_ptes that is ensured to success
65  */
66 static inline void must_inline
67 kpt_set_ptes(struct ptw_state* state, ptr_t addr, 
68                 pte_t pte, unsigned long lsize, unsigned int nr)
69 {
70     if (kpt_set_ptes_flatten(state, addr, pte, lsize, nr) != nr) {
71         spin();
72     }
73 }
74
75 /**
76  *  prepare a editable page table covering va range [addr, addr + lsize)
77  */
78 static inline void must_inline
79 kpt_mktable_at(struct ptw_state* state, ptr_t addr, unsigned long lsize)
80 {
81     kpt_set_ptes(state, addr, null_pte, lsize >> _PAGE_BASE_SHIFT, 1);
82 }
83
84 #endif /* __LUNAIX_ARCH_GENERIC_INIT_PAGETABLE_H */