feat: kernel stack tracing
[lunaix-os.git] / lunaix-os / arch / i386 / includes / sys / cpu.h
1 #ifndef __LUNAIX_CPU_H
2 #define __LUNAIX_CPU_H
3
4 #include <lunaix/types.h>
5
6 /**
7  * @brief Get processor ID string
8  *
9  * @param id_out
10  */
11 void
12 cpu_get_id(char* id_out);
13
14 void
15 cpu_trap_sched();
16
17 void
18 cpu_trap_panic(char* message);
19
20 static inline ptr_t
21 cpu_get_fp()
22 {
23     ptr_t val;
24     asm("movl %%ebp, %0" : "=r"(val)::);
25     return val;
26 }
27
28 /**
29  * @brief Load current processor state
30  *
31  * @return u32_t
32  */
33 static inline u32_t
34 cpu_ldstate()
35 {
36     ptr_t val;
37     asm volatile("pushf\n"
38                  "popl %0\n"
39                  : "=r"(val)::);
40     return val;
41 }
42
43 /**
44  * @brief Load current processor config
45  *
46  * @return u32_t
47  */
48 static inline u32_t
49 cpu_ldconfig()
50 {
51     ptr_t val;
52     asm volatile("movl %%cr0,%0" : "=r"(val));
53     return val;
54 }
55
56 /**
57  * @brief Change current processor state
58  *
59  * @return u32_t
60  */
61 static inline void
62 cpu_chconfig(u32_t val)
63 {
64     asm("mov %0, %%cr0" ::"r"(val));
65 }
66
67 /**
68  * @brief Load current virtual memory space
69  *
70  * @return u32_t
71  */
72 static inline u32_t
73 cpu_ldvmspace()
74 {
75     ptr_t val;
76     asm volatile("movl %%cr3,%0" : "=r"(val));
77     return val;
78 }
79
80 /**
81  * @brief Change current virtual memory space
82  *
83  * @return u32_t
84  */
85 static inline void
86 cpu_chvmspace(u32_t val)
87 {
88     asm("mov %0, %%cr3" ::"r"(val));
89 }
90
91 /**
92  * @brief Flush TLB
93  *
94  * @return u32_t
95  */
96 static inline void
97 cpu_flush_page(ptr_t va)
98 {
99     asm volatile("invlpg (%0)" ::"r"(va) : "memory");
100 }
101
102 static inline void
103 cpu_flush_vmspace()
104 {
105     asm("movl %%cr3, %%eax\n"
106         "movl %%eax, %%cr3" ::
107           : "eax");
108 }
109
110 static inline void
111 cpu_enable_interrupt()
112 {
113     asm volatile("sti");
114 }
115
116 static inline void
117 cpu_disable_interrupt()
118 {
119     asm volatile("cli");
120 }
121
122 static inline void
123 cpu_wait()
124 {
125     asm("hlt");
126 }
127
128 static inline ptr_t
129 cpu_ldeaddr()
130 {
131     ptr_t val;
132     asm volatile("movl %%cr2,%0" : "=r"(val));
133     return val;
134 }
135
136 #endif /* __LUNAIX_CPU_H */