dynamic memory manager (malloc & free)
[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     reg32 eax;
11     reg32 ebx;
12     reg32 ecx;
13     reg32 edx;
14     reg32 edi;
15     reg32 ebp;
16     reg32 esi;
17     reg32 esp;
18     reg32 cs;
19     reg32 eip;
20 } __attribute__((packed)) registers;
21
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wreturn-type"
24 static inline reg32 cpu_rcr0() {
25     asm volatile ("mov %cr0, %eax");
26 }
27
28 static inline reg32 cpu_rcr2() {
29     asm volatile ("mov %cr2, %eax");
30 }
31
32 static inline reg32 cpu_rcr3() {
33     asm volatile ("mov %cr3, %eax");
34 }
35 #pragma GCC diagnostic pop
36
37 static inline void cpu_lcr0(reg32 v) {
38     asm volatile (
39         "mov %0, %%cr0"
40         :: "r"(v)
41     );
42 }
43
44 static inline void cpu_lcr2(reg32 v) {
45     asm volatile (
46         "mov %0, %%cr2"
47         :: "r"(v)
48     );
49 }
50
51 static inline void cpu_lcr3(reg32 v) {
52     asm volatile (
53         "mov %0, %%cr3"
54         :: "r"(v)
55     );
56 }
57
58 static inline void cpu_invplg(void* va) {
59     __asm__("invlpg (%0)" ::"r"((uintptr_t)va) : "memory");
60 }
61
62 static inline void cpu_invtlb() {
63     reg32 interm;
64     __asm__(
65         "movl %%cr3, %0\n"
66         "movl %0, %%cr3"
67         :"=r"(interm)
68         :"r"(interm)
69     );
70 }
71
72 #endif