1 #include <stdint.h>
2 #include <stddef.h>
3 
4 #if !CPUINFO_MOCK
5 	#error This file should be built only in mock mode
6 #endif
7 
8 #include <cpuinfo-mock.h>
9 
10 
11 static struct cpuinfo_mock_cpuid* cpuinfo_mock_cpuid_data = NULL;
12 static uint32_t cpuinfo_mock_cpuid_entries = 0;
13 static uint32_t cpuinfo_mock_cpuid_leaf4_iteration = 0;
14 
cpuinfo_mock_set_cpuid(struct cpuinfo_mock_cpuid * dump,size_t entries)15 void CPUINFO_ABI cpuinfo_mock_set_cpuid(struct cpuinfo_mock_cpuid* dump, size_t entries) {
16 	cpuinfo_mock_cpuid_data = dump;
17 	cpuinfo_mock_cpuid_entries = entries;
18 };
19 
cpuinfo_mock_get_cpuid(uint32_t eax,uint32_t regs[4])20 void CPUINFO_ABI cpuinfo_mock_get_cpuid(uint32_t eax, uint32_t regs[4]) {
21 	if (eax != 4) {
22 		cpuinfo_mock_cpuid_leaf4_iteration = 0;
23 	}
24 	if (cpuinfo_mock_cpuid_data != NULL && cpuinfo_mock_cpuid_entries != 0) {
25 		if (eax == 4) {
26 			uint32_t skip_entries = cpuinfo_mock_cpuid_leaf4_iteration;
27 			for (uint32_t i = 0; i < cpuinfo_mock_cpuid_entries; i++) {
28 				if (eax == cpuinfo_mock_cpuid_data[i].input_eax) {
29 					if (skip_entries-- == 0) {
30 						regs[0] = cpuinfo_mock_cpuid_data[i].eax;
31 						regs[1] = cpuinfo_mock_cpuid_data[i].ebx;
32 						regs[2] = cpuinfo_mock_cpuid_data[i].ecx;
33 						regs[3] = cpuinfo_mock_cpuid_data[i].edx;
34 						cpuinfo_mock_cpuid_leaf4_iteration++;
35 						return;
36 					}
37 				}
38 			}
39 		} else {
40 			for (uint32_t i = 0; i < cpuinfo_mock_cpuid_entries; i++) {
41 				if (eax == cpuinfo_mock_cpuid_data[i].input_eax) {
42 					regs[0] = cpuinfo_mock_cpuid_data[i].eax;
43 					regs[1] = cpuinfo_mock_cpuid_data[i].ebx;
44 					regs[2] = cpuinfo_mock_cpuid_data[i].ecx;
45 					regs[3] = cpuinfo_mock_cpuid_data[i].edx;
46 					return;
47 				}
48 			}
49 		}
50 	}
51 	regs[0] = regs[1] = regs[2] = regs[3] = 0;
52 }
53 
cpuinfo_mock_get_cpuidex(uint32_t eax,uint32_t ecx,uint32_t regs[4])54 void CPUINFO_ABI cpuinfo_mock_get_cpuidex(uint32_t eax, uint32_t ecx, uint32_t regs[4]) {
55 	cpuinfo_mock_cpuid_leaf4_iteration = 0;
56 	if (cpuinfo_mock_cpuid_data != NULL && cpuinfo_mock_cpuid_entries != 0) {
57 		for (uint32_t i = 0; i < cpuinfo_mock_cpuid_entries; i++) {
58 			if (eax == cpuinfo_mock_cpuid_data[i].input_eax &&
59 				ecx == cpuinfo_mock_cpuid_data[i].input_ecx)
60 			{
61 				regs[0] = cpuinfo_mock_cpuid_data[i].eax;
62 				regs[1] = cpuinfo_mock_cpuid_data[i].ebx;
63 				regs[2] = cpuinfo_mock_cpuid_data[i].ecx;
64 				regs[3] = cpuinfo_mock_cpuid_data[i].edx;
65 				return;
66 			}
67 		}
68 	}
69 	regs[0] = regs[1] = regs[2] = regs[3] = 0;
70 }
71