Virtual memory & paging
[lunaix-os.git] / lunaix-os / hal / cpu.c
1 #include <hal/cpu.h>
2 #include <stdint.h>
3 #include <cpuid.h>
4
5 void cpu_get_model(char* model_out) {
6     uint32_t* out = (uint32_t*)model_out;
7     reg32 eax, ebx, edx, ecx;
8     
9     __get_cpuid(0, &eax, &ebx, &ecx, &edx);
10
11     out[0] = ebx;
12     out[1] = edx;
13     out[2] = ecx;
14     model_out[12] = '\0';
15 }
16
17 #define BRAND_LEAF 0x80000000UL
18
19 int cpu_brand_string_supported() {
20     reg32 supported = __get_cpuid_max(BRAND_LEAF, 0);
21     return (supported >= 0x80000004UL);
22 }
23
24 void cpu_get_brand(char* brand_out) {
25     if(!cpu_brand_string_supported()) {
26         brand_out[0] = '?';
27         brand_out[1] = '\0';
28     }
29     uint32_t* out = (uint32_t*) brand_out;
30     reg32 eax, ebx, edx, ecx;
31     for (uint32_t i = 2, j = 0; i < 5; i++)
32     {
33         __get_cpuid(BRAND_LEAF + i, &eax, &ebx, &ecx, &edx);
34         out[j] = eax;
35         out[j + 1] = ebx;
36         out[j + 2] = ecx;
37         out[j + 3] = edx;
38         j+=4;
39     }
40     brand_out[48] = '\0';
41 }
42
43 reg32 cpu_r_cr0() {
44     asm volatile ("mov %cr0, %eax");
45 }
46
47 reg32 cpu_r_cr2() {
48     asm volatile ("mov %cr2, %eax");
49 }
50
51 reg32 cpu_r_cr3() {
52     asm volatile ("mov %cr3, %eax");
53 }
54
55 void cpu_w_cr0(reg32 v) {
56     asm volatile (
57         "mov %0, %%cr0"
58         :: "r"(v)
59     );
60 }
61
62 void cpu_w_cr2(reg32 v) {
63     asm volatile (
64         "mov %0, %%cr2"
65         :: "r"(v)
66     );
67 }
68
69 void cpu_w_cr3(reg32 v) {
70     asm volatile (
71         "mov %0, %%cr3"
72         :: "r"(v)
73     );
74 }
75
76