7-ps2_keyboard.md and 8-multitasking.md (#29)
[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
21 /**
22  * @brief Load current processor state
23  *
24  * @return u32_t
25  */
26 static inline u32_t
27 cpu_ldstate()
28 {
29     ptr_t val;
30     asm volatile("pushf\n"
31                  "popl %0\n"
32                  : "=r"(val)::);
33     return val;
34 }
35
36 /**
37  * @brief Load current processor config
38  *
39  * @return u32_t
40  */
41 static inline u32_t
42 cpu_ldconfig()
43 {
44     ptr_t val;
45     asm volatile("movl %%cr0,%0" : "=r"(val));
46     return val;
47 }
48
49 /**
50  * @brief Change current processor state
51  *
52  * @return u32_t
53  */
54 static inline void
55 cpu_chconfig(u32_t val)
56 {
57     asm("mov %0, %%cr0" ::"r"(val));
58 }
59
60 /**
61  * @brief Load current virtual memory space
62  *
63  * @return u32_t
64  */
65 static inline u32_t
66 cpu_ldvmspace()
67 {
68     ptr_t val;
69     asm volatile("movl %%cr3,%0" : "=r"(val));
70     return val;
71 }
72
73 /**
74  * @brief Change current virtual memory space
75  *
76  * @return u32_t
77  */
78 static inline void
79 cpu_chvmspace(u32_t val)
80 {
81     asm("mov %0, %%cr3" ::"r"(val));
82 }
83
84 /**
85  * @brief Flush a certain TLB record
86  *
87  * @return u32_t
88  */
89 static inline void
90 cpu_flush_page(ptr_t va)
91 {
92     asm volatile("invlpg (%0)" ::"r"(va) : "memory");
93 }
94
95 /**
96  * @brief Flush entire TLB
97  *
98  */
99 static inline void
100 cpu_flush_vmspace()
101 {
102     asm("movl %%cr3, %%eax\n"
103         "movl %%eax, %%cr3" ::
104           : "eax");
105 }
106
107 static inline void
108 cpu_enable_interrupt()
109 {
110     asm volatile("sti");
111 }
112
113 static inline void
114 cpu_disable_interrupt()
115 {
116     asm volatile("cli");
117 }
118
119 static inline void
120 cpu_wait()
121 {
122     asm("hlt");
123 }
124
125 /**
126  * @brief Read exeception address
127  *
128  * @return ptr_t
129  */
130 static inline ptr_t
131 cpu_ldeaddr()
132 {
133     ptr_t val;
134     asm volatile("movl %%cr2,%0" : "=r"(val));
135     return val;
136 }
137
138 #endif /* __LUNAIX_CPU_H */