Debian Codegraph @

tests/test_cpuid.py

raw
1
2
3 import unittest
4 from cpuinfo import *
5 import helpers
6
7
8 class MockASM(ASM):
9 is_first = False
10
11 def __init__(self, restype=None, argtypes=(), machine_code=[]):
12 super(MockASM, self).__init__(restype, argtypes, machine_code)
13
14 def compile(self):
15 self.func = self.run
16
17 def run(self):
18 machine_code = tuple(self.machine_code)
19
20 # get_max_extension_support
21 if machine_code == \
22 (b"\xB8\x00\x00\x00\x80" # mov ax,0x80000000
23 b"\x0f\xa2" # cpuid
24 b"\xC3",): # ret
25 return 0x8000001f
26
27 # get_cache
28 if machine_code == \
29 (b"\xB8\x06\x00\x00\x80" # mov ax,0x80000006
30 b"\x0f\xa2" # cpuid
31 b"\x89\xC8" # mov ax,cx
32 b"\xC3",): # ret))
33 return 0x2006140
34
35 # get_info
36 if machine_code == \
37 (b"\xB8\x01\x00\x00\x00", # mov eax,0x1"
38 b"\x0f\xa2" # cpuid
39 b"\xC3",): # ret
40 return 0x800f82
41
42 # get_processor_brand
43 if machine_code == \
44 (b"\xB8\x02\x00\x00\x80", # mov ax,0x80000002
45 b"\x0f\xa2" # cpuid
46 b"\x89\xC0" # mov ax,ax
47 b"\xC3",): # ret
48 return 0x20444d41
49 elif machine_code == \
50 (b"\xB8\x02\x00\x00\x80", # mov ax,0x80000002
51 b"\x0f\xa2" # cpuid
52 b"\x89\xD8" # mov ax,bx
53 b"\xC3",): # ret
54 return 0x657a7952
55 elif machine_code == \
56 (b"\xB8\x02\x00\x00\x80", # mov ax,0x80000002
57 b"\x0f\xa2" # cpuid
58 b"\x89\xC8" # mov ax,cx
59 b"\xC3",): # ret
60 return 0x2037206e
61 elif machine_code == \
62 (b"\xB8\x02\x00\x00\x80", # mov ax,0x80000002
63 b"\x0f\xa2" # cpuid
64 b"\x89\xD0" # mov ax,dx
65 b"\xC3",): # ret
66 return 0x30303732
67 elif machine_code == \
68 (b"\xB8\x03\x00\x00\x80", # mov ax,0x80000003
69 b"\x0f\xa2" # cpuid
70 b"\x89\xC0" # mov ax,ax
71 b"\xC3",): # ret
72 return 0x69452058
73 elif machine_code == \
74 (b"\xB8\x03\x00\x00\x80", # mov ax,0x80000003
75 b"\x0f\xa2" # cpuid
76 b"\x89\xD8" # mov ax,bx
77 b"\xC3",): # ret
78 return 0x2d746867
79 elif machine_code == \
80 (b"\xB8\x03\x00\x00\x80", # mov ax,0x80000003
81 b"\x0f\xa2" # cpuid
82 b"\x89\xC8" # mov ax,cx
83 b"\xC3",): # ret
84 return 0x65726f43
85 elif machine_code == \
86 (b"\xB8\x03\x00\x00\x80", # mov ax,0x80000003
87 b"\x0f\xa2" # cpuid
88 b"\x89\xD0" # mov ax,dx
89 b"\xC3",): # ret
90 return 0x6f725020
91 elif machine_code == \
92 (b"\xB8\x04\x00\x00\x80", # mov ax,0x80000004
93 b"\x0f\xa2" # cpuid
94 b"\x89\xC0" # mov ax,ax
95 b"\xC3",): # ret
96 return 0x73736563
97 elif machine_code == \
98 (b"\xB8\x04\x00\x00\x80", # mov ax,0x80000004
99 b"\x0f\xa2" # cpuid
100 b"\x89\xD8" # mov ax,bx
101 b"\xC3",): # ret
102 return 0x2020726f
103 elif machine_code == \
104 (b"\xB8\x04\x00\x00\x80", # mov ax,0x80000004
105 b"\x0f\xa2" # cpuid
106 b"\x89\xC8" # mov ax,cx
107 b"\xC3",): # ret
108 return 0x20202020
109 elif machine_code == \
110 (b"\xB8\x04\x00\x00\x80", # mov ax,0x80000004
111 b"\x0f\xa2" # cpuid
112 b"\x89\xD0" # mov ax,dx
113 b"\xC3",): # ret
114 return 0x202020
115
116 # get_vendor_id
117 if machine_code == \
118 (b"\x31\xC0", # xor eax,eax
119 b"\x0F\xA2" # cpuid
120 b"\x89\xD8" # mov ax,bx
121 b"\xC3",): # ret
122 return 0x68747541
123 elif machine_code == \
124 (b"\x31\xC0", # xor eax,eax
125 b"\x0f\xa2" # cpuid
126 b"\x89\xC8" # mov ax,cx
127 b"\xC3",): # ret
128 return 0x444d4163
129 elif machine_code == \
130 (b"\x31\xC0", # xor eax,eax
131 b"\x0f\xa2" # cpuid
132 b"\x89\xD0" # mov ax,dx
133 b"\xC3",): # ret
134 return 0x69746e65
135
136 # get_flags
137 if machine_code == \
138 (b"\xB8\x01\x00\x00\x00", # mov eax,0x1"
139 b"\x0f\xa2" # cpuid
140 b"\x89\xD0" # mov ax,dx
141 b"\xC3",): # ret
142 return 0x178bfbff
143 elif machine_code == \
144 (b"\xB8\x01\x00\x00\x00", # mov eax,0x1"
145 b"\x0f\xa2" # cpuid
146 b"\x89\xC8" # mov ax,cx
147 b"\xC3",): # ret
148 return 0x7ed8320b
149 elif machine_code == \
150 (b"\x31\xC9", # xor ecx,ecx
151 b"\xB8\x07\x00\x00\x00" # mov eax,7
152 b"\x0f\xa2" # cpuid
153 b"\x89\xD8" # mov ax,bx
154 b"\xC3",): # ret
155 return 0x209c01a9
156 elif machine_code == \
157 (b"\x31\xC9", # xor ecx,ecx
158 b"\xB8\x07\x00\x00\x00" # mov eax,7
159 b"\x0f\xa2" # cpuid
160 b"\x89\xC8" # mov ax,cx
161 b"\xC3",): # ret
162 return 0x0
163 elif machine_code == \
164 (b"\xB8\x01\x00\x00\x80" # mov ax,0x80000001
165 b"\x0f\xa2" # cpuid
166 b"\x89\xD8" # mov ax,bx
167 b"\xC3",): # ret
168 return 0x20000000
169 elif machine_code == \
170 (b"\xB8\x01\x00\x00\x80" # mov ax,0x80000001
171 b"\x0f\xa2" # cpuid
172 b"\x89\xC8" # mov ax,cx
173 b"\xC3",): # ret
174 return 0x35c233ff
175
176 # get_ticks
177 # 32 bit
178 if machine_code == \
179 (b"\x55", # push bp
180 b"\x89\xE5", # mov bp,sp
181 b"\x31\xC0", # xor ax,ax
182 b"\x0F\xA2", # cpuid
183 b"\x0F\x31", # rdtsc
184 b"\x8B\x5D\x08", # mov bx,[di+0x8]
185 b"\x8B\x4D\x0C", # mov cx,[di+0xc]
186 b"\x89\x13", # mov [bp+di],dx
187 b"\x89\x01", # mov [bx+di],ax
188 b"\x5D", # pop bp
189 b"\xC3",): # ret
190 raise Exception("FIXME: Add ticks for 32bit get_ticks")
191 # 64 bit
192 elif machine_code == \
193 (b"\x48", # dec ax
194 b"\x31\xC0", # xor ax,ax
195 b"\x0F\xA2", # cpuid
196 b"\x0F\x31", # rdtsc
197 b"\x48", # dec ax
198 b"\xC1\xE2\x20", # shl dx,byte 0x20
199 b"\x48", # dec ax
200 b"\x09\xD0", # or ax,dx
201 b"\xC3",): # ret
202 MockASM.is_first = not MockASM.is_first
203 if MockASM.is_first:
204 return 19233706151817
205 else:
206 return 19237434253761
207
208 raise Exception("Unexpected machine code")
209
210 def free(self):
211 self.func = None
212
213
214 class MockDataSource:
215 bits = '64bit'
216 cpu_count = 1
217 is_windows = platform.system().lower() == 'windows'
218 arch_string_raw = 'INVALID'
219 uname_string_raw = 'INVALID'
220 can_cpuid = True
221
222
223 class TestCPUID(unittest.TestCase):
224 def setUp(self):
225 helpers.backup_data_source(cpuinfo)
226 helpers.monkey_patch_data_source(cpuinfo, MockDataSource)
227
228 helpers.backup_asm(cpuinfo)
229 helpers.monkey_patch_asm(cpuinfo, MockASM)
230
231 def tearDown(self):
232 helpers.restore_data_source(cpuinfo)
233
234 helpers.restore_asm(cpuinfo)
235
236 # Make sure this returns {} on an invalid arch
237 def test_return_empty(self):
238 self.assertEqual({}, cpuinfo._get_cpu_info_from_cpuid())
239
240 def test_normal(self):
241 cpuid = CPUID()
242 self.assertIsNotNone(cpuid)
243
244 self.assertFalse(cpuid.is_selinux_enforcing)
245
246 max_extension_support = cpuid.get_max_extension_support()
247 self.assertEqual(0x8000001f, max_extension_support)
248
249 cache_info = cpuid.get_cache(max_extension_support)
250 self.assertEqual({'size_b': 64 * 1024, 'line_size_b': 512, 'associativity': 6}, cache_info)
251
252 info = cpuid.get_info()
253 self.assertEqual({'stepping': 2, 'model': 8, 'family': 23, 'processor_type': 0}, info)
254
255 processor_brand = cpuid.get_processor_brand(max_extension_support)
256 self.assertEqual("AMD Ryzen 7 2700X Eight-Core Processor", processor_brand)
257
258 hz_actual = cpuid.get_raw_hz()
259 self.assertEqual(3728101944, hz_actual)
260
261 vendor_id = cpuid.get_vendor_id()
262 self.assertEqual('AuthenticAMD', vendor_id)
263
264 flags = cpuid.get_flags(max_extension_support)
265 self.assertEqual(
266 ['3dnowprefetch', 'abm', 'adx', 'aes', 'apic', 'avx', 'avx2', 'bmi1',
267 'bmi2', 'clflush', 'clflushopt', 'cmov', 'cmp_legacy', 'cr8_legacy',
268 'cx16', 'cx8', 'dbx', 'de', 'extapic', 'f16c', 'fma', 'fpu', 'fxsr',
269 'ht', 'lahf_lm', 'lm', 'mca', 'mce', 'misalignsse', 'mmx', 'monitor',
270 'movbe', 'msr', 'mtrr', 'osvw', 'osxsave', 'pae', 'pat', 'pci_l2i',
271 'pclmulqdq', 'perfctr_core', 'perfctr_nb', 'pge', 'pni', 'popcnt',
272 'pse', 'pse36', 'rdrnd', 'rdseed', 'sep', 'sha', 'skinit', 'smap',
273 'smep', 'sse', 'sse2', 'sse4_1', 'sse4_2', 'sse4a', 'ssse3', 'svm',
274 'tce', 'topoext', 'tsc', 'vme', 'wdt', 'xsave'
275 ], flags)