refactor: cut off some bloats in intr_ctx
[lunaix-os.git] / lunaix-os / includes / hal / cpu.h
1 #ifndef __LUNAIX_CPU_H
2 #define __LUNAIX_CPU_H
3
4 #include <lunaix/types.h>
5
6 #define SEL_RPL(selector) ((selector)&0x3)
7
8 typedef unsigned int reg32;
9 typedef unsigned short reg16;
10
11 typedef struct
12 {
13     reg32 eax;
14     reg32 ebx;
15     reg32 ecx;
16     reg32 edx;
17     reg32 edi;
18     reg32 ebp;
19     reg32 esi;
20     reg32 esp;
21 } __attribute__((packed)) gp_regs;
22
23 typedef struct
24 {
25     reg16 ds;
26     reg16 es;
27     reg16 fs;
28     reg16 gs;
29 } __attribute__((packed)) sg_reg;
30
31 void
32 cpu_get_brand(char* brand_out);
33
34 int
35 cpu_has_apic();
36
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wreturn-type"
39 static inline reg32
40 cpu_rcr0()
41 {
42     uintptr_t val;
43     asm volatile("movl %%cr0,%0" : "=r"(val));
44     return val;
45 }
46
47 static inline reg32
48 cpu_rcr2()
49 {
50     uintptr_t val;
51     asm volatile("movl %%cr2,%0" : "=r"(val));
52     return val;
53 }
54
55 static inline reg32
56 cpu_rcr3()
57 {
58     uintptr_t val;
59     asm volatile("movl %%cr3,%0" : "=r"(val));
60     return val;
61 }
62
63 static inline reg32
64 cpu_reflags()
65 {
66     uintptr_t val;
67     asm volatile("pushf\n"
68                  "popl %0\n"
69                  : "=r"(val)::);
70     return val;
71 }
72 #pragma GCC diagnostic pop
73
74 static inline void
75 cpu_lcr0(reg32 v)
76 {
77     asm("mov %0, %%cr0" ::"r"(v));
78 }
79
80 static inline void
81 cpu_lcr2(reg32 v)
82 {
83     asm("mov %0, %%cr2" ::"r"(v));
84 }
85
86 static inline void
87 cpu_lcr3(reg32 v)
88 {
89     asm("mov %0, %%cr3" ::"r"(v));
90 }
91
92 static inline void
93 cpu_invplg(void* va)
94 {
95     asm volatile("invlpg (%0)" ::"r"((uintptr_t)va) : "memory");
96 }
97
98 static inline void
99 cpu_enable_interrupt()
100 {
101     asm volatile("sti");
102 }
103
104 static inline void
105 cpu_disable_interrupt()
106 {
107     asm volatile("cli");
108 }
109
110 static inline void
111 cpu_invtlb()
112 {
113     reg32 interm;
114     asm("movl %%cr3, %0\n"
115         "movl %0, %%cr3"
116         : "=r"(interm)
117         : "r"(interm));
118 }
119
120 static inline void
121 cpu_int(int vect)
122 {
123     asm("int %0" ::"i"(vect));
124 }
125
126 void
127 cpu_rdmsr(u32_t msr_idx, u32_t* reg_high, u32_t* reg_low);
128
129 void
130 cpu_wrmsr(u32_t msr_idx, u32_t reg_high, u32_t reg_low);
131
132 #endif