SayoriOS 0.3.3

cpuid.c
1 //
2 // Created by ndraey on 03.10.23.
3 //
4
5 #include "common.h"
6 #include "../../include/sys/cpuid.h"
7 #include "../../include/sys/cpu_intel.h"
8 #include "io/ports.h"
9 #include "io/tty.h"
10
11 size_t cpu_get_id() {
12 uint32_t ebx = 0, unused;
13
14 cpuid(0, unused, ebx, unused, unused);
15
16 return ebx;
17 }
18
19 void cpu_get_id_string(char out[12]) {
20 uint32_t* out32 = (uint32_t*)out;
21
22 uint32_t ebx, ecx, edx, unused;
23
24 cpuid(0, unused, ebx, ecx, edx);
25
26 out32[0] = ebx;
27 out32[1] = edx;
28 out32[2] = ecx;
29 }
30
31 // Scythe-eeee-er!
32 struct cpu_info cpu_get_basic_info() {
33 struct cpu_info info = {};
34
35 uint32_t eax, ebx, extended_max, unused;
36
37 info.manufacturer_id = cpu_get_id();
38
39 cpuid(1, eax, ebx, info.feature_flags_ecx, info.feature_flags_edx);
40
41 info.model_id = (eax >> 4) & 0xf;
42 info.family_id = (eax >> 8) & 0xf;
43
44 // Available only on Intel Processors.
45 if(info.manufacturer_id == INTEL_MAGIC) {
46 qemu_log("INTEL!");
47
48 info.type_id = (eax >> 12) & 0x3;
49 info.type_string = info.type_id < 4 ? intel_cpu_types[info.type_id] : 0;
50
51 info.brand_id = ebx & 0xff;
52
53 if (info.family_id == 15) {
54 info.extended_family_id = (eax >> 20) & 0xff;
55 }
56
57 const char** family_models = intel_model_names[info.family_id];
58
59 info.model_string = family_models ? family_models[info.model_id] : 0;
60
61 cpuid(0x80000000, extended_max, unused, unused, unused);
62
63 if(info.brand_id > 0) {
64 if(eax == 0x000006B1 || eax == 0x00000F13) {
65 info.brand_string = intel_additional_brand_names[info.brand_id];
66 } else {
67 info.brand_string = intel_brand_names[info.brand_id];
68 }
69 }
70
71 uint32_t cache_info[4] = {0};
72
73 if (extended_max >= 0x80000005) {
74 cpuid(0x80000005,
75 unused,
76 unused,
77 cache_info[0],
78 cache_info[1]
79 );
80 }
81
82 if (extended_max >= 0x80000006) {
83 cpuid(0x80000006,
84 unused,
85 unused,
86 cache_info[2],
87 cache_info[3]
88 );
89 }
90
91 info.l1_i_size = cache_info[0] & 0xff;
92 info.l1_d_size = cache_info[1] & 0xff;
93 info.l2_size = cache_info[2] & 0xffff;
94 info.l3_size = cache_info[3] & 0x3fff;
95
96 info.stepping_id = eax & 0xf;
97 } else if(info.manufacturer_id == AMD_MAGIC) {
98 qemu_log("AMD!");
99 } else {
100 qemu_log("Other!");
101 }
102
103 return info;
104 }
105
106 bool is_long_mode_supported() {
107 uint32_t eax = 0, unused = 0;
108
109 cpuid(0x80000000, eax, unused, unused, unused);
110
111 if(eax < 0x80000001) {
112 return false;
113 }
114
115 return true;
116 }
common.h
Основные определения ядра
AMD_MAGIC
#define AMD_MAGIC
Ключ процессора AMD.
Definition: cpuinfo.c:22
INTEL_MAGIC
#define INTEL_MAGIC
Ключ процессора Intel.
Definition: cpuinfo.c:21
cpu_info
Definition: cpuid.h:65