X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/cd09c4b222e4ddf45a42522563ae2471a829d1c8..2a49908006b177c4d6354309333d06b1b96e4887:/lunaix-os/hal/cpu.c diff --git a/lunaix-os/hal/cpu.c b/lunaix-os/hal/cpu.c index b2ef657..77059b6 100644 --- a/lunaix-os/hal/cpu.c +++ b/lunaix-os/hal/cpu.c @@ -1,11 +1,14 @@ +#include #include +#include #include -#include -void cpu_get_model(char* model_out) { - uint32_t* out = (uint32_t*)model_out; - reg32 eax, ebx, edx, ecx; - +void +cpu_get_model(char* model_out) +{ + u32_t* out = (u32_t*)model_out; + reg32 eax = 0, ebx = 0, edx = 0, ecx = 0; + __get_cpuid(0, &eax, &ebx, &ecx, &edx); out[0] = ebx; @@ -16,61 +19,63 @@ void cpu_get_model(char* model_out) { #define BRAND_LEAF 0x80000000UL -int cpu_brand_string_supported() { +int +cpu_brand_string_supported() +{ reg32 supported = __get_cpuid_max(BRAND_LEAF, 0); return (supported >= 0x80000004UL); } -void cpu_get_brand(char* brand_out) { - if(!cpu_brand_string_supported()) { +void +cpu_get_brand(char* brand_out) +{ + if (!cpu_brand_string_supported()) { brand_out[0] = '?'; brand_out[1] = '\0'; } - uint32_t* out = (uint32_t*) brand_out; - reg32 eax, ebx, edx, ecx; - for (uint32_t i = 2, j = 0; i < 5; i++) - { + u32_t* out = (u32_t*)brand_out; + reg32 eax = 0, ebx = 0, edx = 0, ecx = 0; + for (u32_t i = 2, j = 0; i < 5; i++) { __get_cpuid(BRAND_LEAF + i, &eax, &ebx, &ecx, &edx); out[j] = eax; out[j + 1] = ebx; out[j + 2] = ecx; out[j + 3] = edx; - j+=4; + j += 4; } brand_out[48] = '\0'; } -reg32 cpu_r_cr0() { - asm volatile ("mov %cr0, %eax"); -} - -reg32 cpu_r_cr2() { - asm volatile ("mov %cr2, %eax"); -} +int +cpu_has_apic() +{ + // reference: Intel manual, section 10.4.2 + reg32 eax = 0, ebx = 0, edx = 0, ecx = 0; + __get_cpuid(1, &eax, &ebx, &ecx, &edx); -reg32 cpu_r_cr3() { - asm volatile ("mov %cr3, %eax"); + return (edx & 0x100); } -void cpu_w_cr0(reg32 v) { - asm volatile ( - "mov %0, %%cr0" - :: "r"(v) - ); -} +void +cpu_rdmsr(u32_t msr_idx, u32_t* reg_high, u32_t* reg_low) +{ + u32_t h = 0, l = 0; + asm volatile("rdmsr" : "=d"(h), "=a"(l) : "c"(msr_idx)); -void cpu_w_cr2(reg32 v) { - asm volatile ( - "mov %0, %%cr2" - :: "r"(v) - ); + *reg_high = h; + *reg_low = l; } -void cpu_w_cr3(reg32 v) { - asm volatile ( - "mov %0, %%cr3" - :: "r"(v) - ); +void +cpu_wrmsr(u32_t msr_idx, u32_t reg_high, u32_t reg_low) +{ + asm volatile("wrmsr" : : "d"(reg_high), "a"(reg_low), "c"(msr_idx)); } - +int +rnd_is_supported() +{ + reg32 eax = 0, ebx = 0, ecx = 0, edx = 0; + __get_cpuid(0x01, &eax, &ebx, &ecx, &edx); + return (ecx & (1 << 30)); +} \ No newline at end of file