Timer re-worked!
[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     asm("mov %cr0, %eax");
43 }
44
45 static inline reg32
46 cpu_rcr2()
47 {
48     asm("mov %cr2, %eax");
49 }
50
51 static inline reg32
52 cpu_rcr3()
53 {
54     asm("mov %cr3, %eax");
55 }
56 #pragma GCC diagnostic pop
57
58 static inline void
59 cpu_lcr0(reg32 v)
60 {
61     asm("mov %0, %%cr0" ::"r"(v));
62 }
63
64 static inline void
65 cpu_lcr2(reg32 v)
66 {
67     asm("mov %0, %%cr2" ::"r"(v));
68 }
69
70 static inline void
71 cpu_lcr3(reg32 v)
72 {
73     asm("mov %0, %%cr3" ::"r"(v));
74 }
75
76 static inline void
77 cpu_invplg(void* va)
78 {
79     asm volatile("invlpg (%0)" ::"r"((uintptr_t)va) : "memory");
80 }
81
82 static inline void
83 cpu_enable_interrupt()
84 {
85     asm volatile("sti");
86 }
87
88 static inline void
89 cpu_disable_interrupt()
90 {
91     asm volatile("cli");
92 }
93
94 static inline void
95 cpu_invtlb()
96 {
97     reg32 interm;
98     asm("movl %%cr3, %0\n"
99         "movl %0, %%cr3"
100         : "=r"(interm)
101         : "r"(interm));
102 }
103
104 void
105 cpu_rdmsr(uint32_t msr_idx, uint32_t* reg_high, uint32_t* reg_low);
106
107 void
108 cpu_wrmsr(uint32_t msr_idx, uint32_t reg_high, uint32_t reg_low);
109
110 #endif