Complete (almost!) printf fmt support
[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 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wreturn-type"
36 static inline reg32
37 cpu_rcr0()
38 {
39     asm("mov %cr0, %eax");
40 }
41
42 static inline reg32
43 cpu_rcr2()
44 {
45     asm("mov %cr2, %eax");
46 }
47
48 static inline reg32
49 cpu_rcr3()
50 {
51     asm("mov %cr3, %eax");
52 }
53 #pragma GCC diagnostic pop
54
55 static inline void
56 cpu_lcr0(reg32 v)
57 {
58     asm("mov %0, %%cr0" ::"r"(v));
59 }
60
61 static inline void
62 cpu_lcr2(reg32 v)
63 {
64     asm("mov %0, %%cr2" ::"r"(v));
65 }
66
67 static inline void
68 cpu_lcr3(reg32 v)
69 {
70     asm("mov %0, %%cr3" ::"r"(v));
71 }
72
73 static inline void
74 cpu_invplg(void* va)
75 {
76     asm("invlpg (%0)" ::"r"((uintptr_t)va) : "memory");
77 }
78
79 static inline void
80 cpu_enable_interrupt()
81 {
82     asm("sti");
83 }
84
85 static inline void
86 cpu_disable_interrupt()
87 {
88     asm("cli");
89 }
90
91 static inline void
92 cpu_invtlb()
93 {
94     reg32 interm;
95     asm("movl %%cr3, %0\n"
96         "movl %0, %%cr3"
97         : "=r"(interm)
98         : "r"(interm));
99 }
100
101 #endif