Boot framework rework (#45)
[lunaix-os.git] / lunaix-os / arch / x86 / includes / sys / cpu.h
1 #ifndef __LUNAIX_CPU_H
2 #define __LUNAIX_CPU_H
3
4 #include <lunaix/types.h>
5
6 #ifdef CONFIG_ARCH_X86_64
7 #   define _POP "popq "
8 #   define _MOV "movq "
9 #else
10 #   define _POP "popl "
11 #   define _MOV "movl "
12 #endif
13
14 /**
15  * @brief Get processor ID string
16  *
17  * @param id_out
18  */
19 void
20 cpu_get_id(char* id_out);
21
22 void
23 cpu_trap_sched();
24
25 void
26 cpu_trap_panic(char* message);
27
28 /**
29  * @brief Load current processor state
30  *
31  * @return reg_t
32  */
33 static inline reg_t
34 cpu_ldstate()
35 {
36     ptr_t val;
37     asm volatile("pushf\n"
38                  _POP "%0\n"
39                  : "=r"(val)::);
40     return val;
41 }
42
43 /**
44  * @brief Load current processor config
45  *
46  * @return reg_t
47  */
48 static inline reg_t
49 cpu_ldconfig()
50 {
51     reg_t val;
52     asm volatile(_MOV "%%cr0,%0" : "=r"(val));
53     return val;
54 }
55
56 /**
57  * @brief Change current processor state
58  *
59  * @return reg_t
60  */
61 static inline void
62 cpu_chconfig(reg_t val)
63 {
64     asm(_MOV "%0, %%cr0" ::"r"(val));
65 }
66
67 /**
68  * @brief Change current virtual memory space
69  *
70  * @return reg_t
71  */
72 static inline void
73 cpu_chvmspace(reg_t val)
74 {
75     asm(_MOV "%0, %%cr3" ::"r"(val));
76 }
77
78 /**
79  * @brief Read exeception address
80  *
81  * @return ptr_t
82  */
83 static inline ptr_t
84 cpu_ldeaddr()
85 {
86     ptr_t val;
87     asm volatile(_MOV "%%cr2,%0" : "=r"(val));
88     return val;
89 }
90
91
92 static inline void
93 cpu_enable_interrupt()
94 {
95     asm volatile("sti");
96 }
97
98 static inline void
99 cpu_disable_interrupt()
100 {
101     asm volatile("cli");
102 }
103
104 static inline void
105 cpu_wait()
106 {
107     asm("hlt");
108 }
109
110 #endif /* __LUNAIX_CPU_H */