feat: pgid 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 ds;
24     reg16 es;
25     reg16 fs;
26     reg16 gs;
27 } __attribute__((packed)) sg_reg;
28
29 void
30 cpu_get_brand(char* brand_out);
31
32 int
33 cpu_has_apic();
34
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wreturn-type"
37 static inline reg32
38 cpu_rcr0()
39 {
40     uintptr_t val;
41     asm volatile("movl %%cr0,%0" : "=r"(val));
42     return val;
43 }
44
45 static inline reg32
46 cpu_rcr2()
47 {
48     uintptr_t val;
49     asm volatile("movl %%cr2,%0" : "=r"(val));
50     return val;
51 }
52
53 static inline reg32
54 cpu_rcr3()
55 {
56     uintptr_t val;
57     asm volatile("movl %%cr3,%0" : "=r"(val));
58     return val;
59 }
60
61 static inline reg32
62 cpu_reflags()
63 {
64     uintptr_t val;
65     asm volatile("pushf\n"
66                  "popl %0\n"
67                  : "=r"(val)::);
68     return val;
69 }
70 #pragma GCC diagnostic pop
71
72 static inline void
73 cpu_lcr0(reg32 v)
74 {
75     asm("mov %0, %%cr0" ::"r"(v));
76 }
77
78 static inline void
79 cpu_lcr2(reg32 v)
80 {
81     asm("mov %0, %%cr2" ::"r"(v));
82 }
83
84 static inline void
85 cpu_lcr3(reg32 v)
86 {
87     asm("mov %0, %%cr3" ::"r"(v));
88 }
89
90 static inline void
91 cpu_invplg(void* va)
92 {
93     asm volatile("invlpg (%0)" ::"r"((uintptr_t)va) : "memory");
94 }
95
96 static inline void
97 cpu_enable_interrupt()
98 {
99     asm volatile("sti");
100 }
101
102 static inline void
103 cpu_disable_interrupt()
104 {
105     asm volatile("cli");
106 }
107
108 static inline void
109 cpu_invtlb()
110 {
111     reg32 interm;
112     asm("movl %%cr3, %0\n"
113         "movl %0, %%cr3"
114         : "=r"(interm)
115         : "r"(interm));
116 }
117
118 void
119 cpu_rdmsr(uint32_t msr_idx, uint32_t* reg_high, uint32_t* reg_low);
120
121 void
122 cpu_wrmsr(uint32_t msr_idx, uint32_t reg_high, uint32_t reg_low);
123
124 #endif