• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 #include <string.h>
3 
4 #include <cpuinfo.h>
5 #include <x86/cpuid.h>
6 #include <x86/api.h>
7 #include <cpuinfo/utils.h>
8 #include <cpuinfo/log.h>
9 #include <cpuinfo/common.h>
10 
11 
12 struct cpuinfo_x86_isa cpuinfo_isa = { 0 };
13 CPUINFO_INTERNAL uint32_t cpuinfo_x86_clflush_size = 0;
14 
cpuinfo_x86_init_processor(struct cpuinfo_x86_processor * processor)15 void cpuinfo_x86_init_processor(struct cpuinfo_x86_processor* processor) {
16 	const struct cpuid_regs leaf0 = cpuid(0);
17 	const uint32_t max_base_index = leaf0.eax;
18 	const enum cpuinfo_vendor vendor = processor->vendor =
19 		cpuinfo_x86_decode_vendor(leaf0.ebx, leaf0.ecx, leaf0.edx);
20 
21 	const struct cpuid_regs leaf0x80000000 = cpuid(UINT32_C(0x80000000));
22 	const uint32_t max_extended_index =
23 		leaf0x80000000.eax >= UINT32_C(0x80000000) ? leaf0x80000000.eax : 0;
24 
25 	const struct cpuid_regs leaf0x80000001 = max_extended_index >= UINT32_C(0x80000001) ?
26 		cpuid(UINT32_C(0x80000001)) : (struct cpuid_regs) { 0, 0, 0, 0 };
27 
28 	if (max_base_index >= 1) {
29 		const struct cpuid_regs leaf1 = cpuid(1);
30 		processor->cpuid = leaf1.eax;
31 
32 		const struct cpuinfo_x86_model_info model_info = cpuinfo_x86_decode_model_info(leaf1.eax);
33 		const enum cpuinfo_uarch uarch = processor->uarch =
34 			cpuinfo_x86_decode_uarch(vendor, &model_info);
35 
36 		cpuinfo_x86_clflush_size = ((leaf1.ebx >> 8) & UINT32_C(0x000000FF)) * 8;
37 
38 		/*
39 		 * Topology extensions support:
40 		 * - AMD: ecx[bit 22] in extended info (reserved bit on Intel CPUs).
41 		 */
42 		const bool amd_topology_extensions = !!(leaf0x80000001.ecx & UINT32_C(0x00400000));
43 
44 		cpuinfo_x86_detect_cache(
45 			max_base_index, max_extended_index, amd_topology_extensions, vendor, &model_info,
46 			&processor->cache,
47 			&processor->tlb.itlb_4KB,
48 			&processor->tlb.itlb_2MB,
49 			&processor->tlb.itlb_4MB,
50 			&processor->tlb.dtlb0_4KB,
51 			&processor->tlb.dtlb0_2MB,
52 			&processor->tlb.dtlb0_4MB,
53 			&processor->tlb.dtlb_4KB,
54 			&processor->tlb.dtlb_2MB,
55 			&processor->tlb.dtlb_4MB,
56 			&processor->tlb.dtlb_1GB,
57 			&processor->tlb.stlb2_4KB,
58 			&processor->tlb.stlb2_2MB,
59 			&processor->tlb.stlb2_1GB,
60 			&processor->topology.core_bits_length);
61 
62 		cpuinfo_x86_detect_topology(max_base_index, max_extended_index, leaf1, &processor->topology);
63 
64 		cpuinfo_isa = cpuinfo_x86_detect_isa(leaf1, leaf0x80000001,
65 			max_base_index, max_extended_index, vendor, uarch);
66 	}
67 	if (max_extended_index >= UINT32_C(0x80000004)) {
68 		struct cpuid_regs brand_string[3];
69 		for (uint32_t i = 0; i < 3; i++) {
70 			brand_string[i] = cpuid(UINT32_C(0x80000002) + i);
71 		}
72 		memcpy(processor->brand_string, brand_string, sizeof(processor->brand_string));
73 		cpuinfo_log_debug("raw CPUID brand string: \"%48s\"", processor->brand_string);
74 	}
75 }
76