Unifying External Interrupt System (#51)
[lunaix-os.git] / lunaix-os / arch / x86 / includes / asm / x86_cpu.h
1 #ifndef __LUNAIX_CPU_X86_H
2 #define __LUNAIX_CPU_X86_H
3
4 #include "cpu.h"
5 #include "x86_crx.h"
6
7 #ifdef CONFIG_ARCH_X86_64
8 #   define _POP "popq "
9 #   define _MOV "movq "
10 #else
11 #   define _POP "popl "
12 #   define _MOV "movl "
13 #endif
14
15 /**
16  * @brief Load current processor state
17  *
18  * @return reg_t
19  */
20 static inline reg_t
21 cpu_ldstate()
22 {
23     ptr_t val;
24     asm volatile("pushf\n"
25                  _POP "%0\n"
26                  : "=r"(val)::);
27     return val;
28 }
29
30 /**
31  * @brief Load current processor config
32  *
33  * @return reg_t
34  */
35 static inline reg_t
36 cpu_ldconfig()
37 {
38     reg_t val;
39     asm volatile(_MOV "%%cr0,%0" : "=r"(val));
40     return val;
41 }
42
43 /**
44  * @brief Change current processor state
45  *
46  * @return reg_t
47  */
48 static inline void
49 cpu_chconfig(reg_t val)
50 {
51     asm(_MOV "%0, %%cr0" ::"r"(val));
52 }
53
54 /**
55  * @brief Change current virtual memory space
56  *
57  * @return reg_t
58  */
59 static inline void
60 cpu_chvmspace(reg_t val)
61 {
62     asm(_MOV "%0, %%cr3" ::"r"(val));
63 }
64
65 /**
66  * @brief Read exeception address
67  *
68  * @return ptr_t
69  */
70 static inline ptr_t
71 cpu_ldeaddr()
72 {
73     ptr_t val;
74     asm volatile(_MOV "%%cr2,%0" : "=r"(val));
75     return val;
76 }
77
78 #endif /* __LUNAIX_CPU_X86_H */