basic process support and some syscalls
[lunaix-os.git] / lunaix-os / includes / hal / cpu.h
1 #ifndef __LUNAIX_CPU_H
2 #define __LUNAIX_CPU_H
3
4 #include <stdint.h>
5
6 typedef unsigned int reg32;
7 typedef unsigned short reg16;
8
9 typedef struct
10 {
11     reg32 eax;
12     reg32 ebx;
13     reg32 ecx;
14     reg32 edx;
15     reg32 edi;
16     reg32 ebp;
17     reg32 esi;
18     reg32 esp;
19 } __attribute__((packed)) gp_regs;
20
21 typedef struct
22 {
23     reg16 ss;
24     reg16 es;
25     reg16 ds;
26     reg16 fs;
27     reg16 gs;
28     reg16 cs;
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(
68         "pushf\n"
69         "popl %0\n"
70         :"=r"(val)::);
71     return val;
72 }
73 #pragma GCC diagnostic pop
74
75 static inline void
76 cpu_lcr0(reg32 v)
77 {
78     asm("mov %0, %%cr0" ::"r"(v));
79 }
80
81 static inline void
82 cpu_lcr2(reg32 v)
83 {
84     asm("mov %0, %%cr2" ::"r"(v));
85 }
86
87 static inline void
88 cpu_lcr3(reg32 v)
89 {
90     asm("mov %0, %%cr3" ::"r"(v));
91 }
92
93 static inline void
94 cpu_invplg(void* va)
95 {
96     asm volatile("invlpg (%0)" ::"r"((uintptr_t)va) : "memory");
97 }
98
99 static inline void
100 cpu_enable_interrupt()
101 {
102     asm volatile("sti");
103 }
104
105 static inline void
106 cpu_disable_interrupt()
107 {
108     asm volatile("cli");
109 }
110
111 static inline void
112 cpu_invtlb()
113 {
114     reg32 interm;
115     asm("movl %%cr3, %0\n"
116         "movl %0, %%cr3"
117         : "=r"(interm)
118         : "r"(interm));
119 }
120
121 void
122 cpu_rdmsr(uint32_t msr_idx, uint32_t* reg_high, uint32_t* reg_low);
123
124 void
125 cpu_wrmsr(uint32_t msr_idx, uint32_t reg_high, uint32_t reg_low);
126
127 #endif