Added .gitignore
[comos.git] / kernel / cpuid.c
blob4efa6665c8b3764e5b3b1383ec78623c0bf6ad6a
1 #include "cpuid.h"
2 #include "stddef.h"
3 #include "console.h"
5 struct cpuid_s cpuid;
7 /* CPUID(eax == 1) */
8 void cpuid_get_features(void)
10 uint32_t eax, ebx, ecx, edx;
12 eax = 0x01;
13 asm volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(eax));
15 cpuid.features_edx = edx;
16 cpuid.features_ecx = ecx;
18 cpuid.stepping = (eax & 0x0F);
19 cpuid.model = ((eax >> 4) & 0x0F);
20 cpuid.family = ((eax >> 8) & 0x0F);
21 cpuid.type = ((eax >> 12) & 0x03);
23 cpuid.cache_line_size = ((ebx >> 8) & 0xFF) * 8; /* cache_line_size * 8 = size in bytes */
24 cpuid.logical_processors = ((ebx >> 16) & 0xFF); /* # logical cpu's per physical cpu */
25 cpuid.lapic_id = ((ebx >> 24) & 0xFF); /* Local APIC ID */
27 console_printf(get_vterm(0), " Family: 0x%X | Model: 0x%X | Stepping: 0x%X | Type: 0x%X \n",
28 cpuid.family,
29 cpuid.model,
30 cpuid.stepping,
31 cpuid.type);
32 console_printf(get_vterm(0), " Cache Line Size: %u bytes | Local APIC ID: 0x%X \n",
33 cpuid.cache_line_size,
34 cpuid.lapic_id);
37 void cpuid_get_cpu_brand(void)
39 uint32_t eax, ebx, ecx, edx;
41 eax = 0x80000002;
42 asm volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(eax));
44 cpuid.cpu_brand[48] = 0; /* init cpu_brand to null-terminate the string */
45 *(uint32_t*)(cpuid.cpu_brand + 0 ) = eax;
46 *(uint32_t*)(cpuid.cpu_brand + 4 ) = ebx;
47 *(uint32_t*)(cpuid.cpu_brand + 8 ) = ecx;
48 *(uint32_t*)(cpuid.cpu_brand + 12) = edx;
50 eax = 0x80000003;
51 asm volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(eax));
53 *(uint32_t*)(cpuid.cpu_brand + 16) = eax;
54 *(uint32_t*)(cpuid.cpu_brand + 20) = ebx;
55 *(uint32_t*)(cpuid.cpu_brand + 24) = ecx;
56 *(uint32_t*)(cpuid.cpu_brand + 28) = edx;
58 eax = 0x80000004;
59 asm volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(eax));
61 *(uint32_t*)(cpuid.cpu_brand + 32) = eax;
62 *(uint32_t*)(cpuid.cpu_brand + 36) = ebx;
63 *(uint32_t*)(cpuid.cpu_brand + 40) = ecx;
64 *(uint32_t*)(cpuid.cpu_brand + 44) = edx;
66 console_printf(get_vterm(0), " CPU Brand: %s \n", cpuid.cpu_brand);
69 void parse_cpuid(void)
71 uint32_t eax, ebx, ecx, edx;
73 eax = 0x00;
74 asm volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(eax));
76 cpuid.max_basic_input_val = eax;
77 memset(cpuid.manufacturer_string, 0, 13);
78 *(uint32_t*)(cpuid.manufacturer_string + 0) = ebx;
79 *(uint32_t*)(cpuid.manufacturer_string + 4) = edx;
80 *(uint32_t*)(cpuid.manufacturer_string + 8) = ecx;
82 console_printf(get_vterm(0), "CPUID: \n");
83 console_printf(get_vterm(0), " Manufacturer String: %s \n", cpuid.manufacturer_string);
85 if(cpuid.max_basic_input_val >= 1){
86 cpuid_get_features();
89 eax = 0x80000000;
90 asm volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(eax));
92 cpuid.max_ext_input_val = eax;
94 if(cpuid.max_ext_input_val >= 0x80000004){
95 cpuid_get_cpu_brand();