• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stddef.h>
2 #include <stdint.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 static struct cpuinfo_mock_cpuid* cpuinfo_mock_cpuid_data = NULL;
11 static uint32_t cpuinfo_mock_cpuid_entries = 0;
12 static uint32_t cpuinfo_mock_cpuid_leaf4_iteration = 0;
13 
cpuinfo_mock_set_cpuid(struct cpuinfo_mock_cpuid * dump,size_t entries)14 void CPUINFO_ABI cpuinfo_mock_set_cpuid(struct cpuinfo_mock_cpuid* dump, size_t entries) {
15 	cpuinfo_mock_cpuid_data = dump;
16 	cpuinfo_mock_cpuid_entries = entries;
17 };
18 
cpuinfo_mock_get_cpuid(uint32_t eax,uint32_t regs[4])19 void CPUINFO_ABI cpuinfo_mock_get_cpuid(uint32_t eax, uint32_t regs[4]) {
20 	if (eax != 4) {
21 		cpuinfo_mock_cpuid_leaf4_iteration = 0;
22 	}
23 	if (cpuinfo_mock_cpuid_data != NULL && cpuinfo_mock_cpuid_entries != 0) {
24 		if (eax == 4) {
25 			uint32_t skip_entries = cpuinfo_mock_cpuid_leaf4_iteration;
26 			for (uint32_t i = 0; i < cpuinfo_mock_cpuid_entries; i++) {
27 				if (eax == cpuinfo_mock_cpuid_data[i].input_eax) {
28 					if (skip_entries-- == 0) {
29 						regs[0] = cpuinfo_mock_cpuid_data[i].eax;
30 						regs[1] = cpuinfo_mock_cpuid_data[i].ebx;
31 						regs[2] = cpuinfo_mock_cpuid_data[i].ecx;
32 						regs[3] = cpuinfo_mock_cpuid_data[i].edx;
33 						cpuinfo_mock_cpuid_leaf4_iteration++;
34 						return;
35 					}
36 				}
37 			}
38 		} else {
39 			for (uint32_t i = 0; i < cpuinfo_mock_cpuid_entries; i++) {
40 				if (eax == cpuinfo_mock_cpuid_data[i].input_eax) {
41 					regs[0] = cpuinfo_mock_cpuid_data[i].eax;
42 					regs[1] = cpuinfo_mock_cpuid_data[i].ebx;
43 					regs[2] = cpuinfo_mock_cpuid_data[i].ecx;
44 					regs[3] = cpuinfo_mock_cpuid_data[i].edx;
45 					return;
46 				}
47 			}
48 		}
49 	}
50 	regs[0] = regs[1] = regs[2] = regs[3] = 0;
51 }
52 
cpuinfo_mock_get_cpuidex(uint32_t eax,uint32_t ecx,uint32_t regs[4])53 void CPUINFO_ABI cpuinfo_mock_get_cpuidex(uint32_t eax, uint32_t ecx, uint32_t regs[4]) {
54 	cpuinfo_mock_cpuid_leaf4_iteration = 0;
55 	if (cpuinfo_mock_cpuid_data != NULL && cpuinfo_mock_cpuid_entries != 0) {
56 		for (uint32_t i = 0; i < cpuinfo_mock_cpuid_entries; i++) {
57 			if (eax == cpuinfo_mock_cpuid_data[i].input_eax &&
58 			    ecx == cpuinfo_mock_cpuid_data[i].input_ecx) {
59 				regs[0] = cpuinfo_mock_cpuid_data[i].eax;
60 				regs[1] = cpuinfo_mock_cpuid_data[i].ebx;
61 				regs[2] = cpuinfo_mock_cpuid_data[i].ecx;
62 				regs[3] = cpuinfo_mock_cpuid_data[i].edx;
63 				return;
64 			}
65 		}
66 	}
67 	regs[0] = regs[1] = regs[2] = regs[3] = 0;
68 }
69