add asm headers and linker scripts for aarch64
[lunaix-os.git] / lunaix-os / arch / x86 / includes / asm / tlb.h
1 #ifndef __LUNAIX_ARCH_TLB_H
2 #define __LUNAIX_ARCH_TLB_H
3
4 #include <lunaix/compiler.h>
5 #include <asm/mm_defs.h>
6
7 /**
8  * @brief Invalidate an entry of all address space
9  * 
10  * @param va 
11  */
12 static inline void must_inline
13 __tlb_invalidate(ptr_t va) 
14 {
15     asm volatile("invlpg (%0)" ::"r"(va) : "memory");
16 }
17
18 /**
19  * @brief Invalidate an entry of an address space indetified
20  *        by ASID
21  * 
22  * @param va 
23  */
24 static inline void must_inline
25 __tlb_flush_asid(unsigned int asid, ptr_t va) 
26 {
27     // not supported on x86_32
28     asm volatile("invlpg (%0)" ::"r"(va) : "memory");
29 }
30
31 /**
32  * @brief Invalidate an entry of global address space
33  * 
34  * @param va 
35  */
36 static inline void must_inline
37 __tlb_flush_global(ptr_t va) 
38 {
39     // not supported on x86_32
40     asm volatile("invlpg (%0)" ::"r"(va) : "memory");
41 }
42
43 /**
44  * @brief Invalidate an entire TLB
45  * 
46  * @param va 
47  */
48 static inline void must_inline
49 __tlb_flush_all() 
50 {
51     asm volatile(
52         "movl %%cr3, %%eax\n"
53         "movl %%eax, %%cr3"
54         :::"eax"
55     );
56 }
57
58 /**
59  * @brief Invalidate an entire address space
60  * 
61  * @param va 
62  */
63 static inline void must_inline
64 __tlb_flush_asid_all(unsigned int asid) 
65 {
66     // not supported on x86_32
67     __tlb_flush_all();
68 }
69
70
71 /**
72  * @brief Invalidate entries of all address spaces
73  * 
74  * @param asid 
75  * @param addr 
76  * @param npages 
77  */
78 static inline void 
79 tlb_flush_range(ptr_t addr, unsigned int npages)
80 {
81     for (unsigned int i = 0; i < npages; i++)
82     {
83         __tlb_invalidate(addr + i * PAGE_SIZE);
84     }
85 }
86
87 /**
88  * @brief Invalidate entries of an address space identified
89  *        by ASID
90  * 
91  * @param asid 
92  * @param addr 
93  * @param npages 
94  */
95 static inline void 
96 tlb_flush_asid_range(unsigned int asid, ptr_t addr, unsigned int npages)
97 {
98     for (unsigned int i = 0; i < npages; i++)
99     {
100         __tlb_flush_asid(asid, addr + i * PAGE_SIZE);
101     }
102 }
103
104 /**
105  * @brief Invalidate an entry of kernel address spaces
106  * 
107  * @param asid 
108  * @param addr 
109  * @param npages 
110  */
111 static inline void 
112 tlb_flush_kernel(ptr_t addr)
113 {
114     __tlb_flush_global(addr);
115 }
116
117 /**
118  * @brief Invalidate entries of kernel address spaces
119  * 
120  * @param asid 
121  * @param addr 
122  * @param npages 
123  */
124 static inline void 
125 tlb_flush_kernel_ranged(ptr_t addr, unsigned int npages)
126 {
127     for (unsigned int i = 0; i < npages; i++)
128     {
129         tlb_flush_kernel(addr + i * PAGE_SIZE);
130     }
131 }
132
133 #include <asm-generic/tlb-shared.h>
134
135 #endif /* __LUNAIX_VMTLB_H */