Debian Codegraph @

tools/tests/x86_emulator/test_x86_emulator.c

raw
1 #include "x86-emulate.h"
2
3 #include <errno.h>
4 #include <limits.h>
5 #include <stdio.h>
6 #include <sys/mman.h>
7
8 asm ( ".pushsection .test, \"ax\", @progbits; .popsection" );
9
10 #include "blowfish.h"
11 #include "3dnow.h"
12 #include "sse.h"
13 #include "sse2.h"
14 #include "sse2-gf.h"
15 #include "ssse3-aes.h"
16 #include "ssse3-pclmul.h"
17 #include "sse4.h"
18 #include "sse4-sha.h"
19 #include "avx.h"
20 #include "avx-aes.h"
21 #include "avx-pclmul.h"
22 #include "avx-sha.h"
23 #include "fma4.h"
24 #include "fma.h"
25 #include "avx2.h"
26 #include "avx2-sg.h"
27 #include "avx2-vaes.h"
28 #include "avx2-vpclmulqdq.h"
29 #include "avx2-gf.h"
30 #include "xop.h"
31 #include "avx512f-opmask.h"
32 #include "avx512dq-opmask.h"
33 #include "avx512bw-opmask.h"
34 #include "avx512f.h"
35 #include "avx512f-sg.h"
36 #include "avx512f-sha.h"
37 #include "avx512bw.h"
38 #include "avx512bw-vaes.h"
39 #include "avx512bw-vpclmulqdq.h"
40 #include "avx512bw-gf.h"
41 #include "avx512dq.h"
42 #include "avx512vbmi.h"
43 #include "avx512vbmi2-vpclmulqdq.h"
44 #include "avx512fp16.h"
45
46 #define verbose false /* Switch to true for far more logging. */
47
48 static void blowfish_set_regs(struct cpu_user_regs *regs)
49 {
50 regs->eax = 2;
51 regs->edx = 1;
52 }
53
54 static bool blowfish_check_regs(const struct cpu_user_regs *regs)
55 {
56 return regs->eax == 2 && regs->edx == 1;
57 }
58
59 static bool simd_check__3dnow(void)
60 {
61 return cpu_has_3dnow_ext && cpu_has_sse;
62 }
63
64 static bool simd_check_sse(void)
65 {
66 return cpu_has_sse;
67 }
68
69 static bool simd_check_sse2(void)
70 {
71 return cpu_has_sse2;
72 }
73
74 static bool simd_check_sse4(void)
75 {
76 return cpu_has_sse4_2;
77 }
78
79 static bool simd_check_avx(void)
80 {
81 return cpu_has_avx;
82 }
83
84 static bool simd_check_fma4(void)
85 {
86 return cpu_has_fma4;
87 }
88
89 static bool simd_check_fma(void)
90 {
91 return cpu_has_fma;
92 }
93
94 static bool simd_check_avx2(void)
95 {
96 return cpu_has_avx2;
97 }
98 #define simd_check_avx2_sg simd_check_avx2
99
100 static bool simd_check_xop(void)
101 {
102 return cpu_has_xop;
103 }
104
105 static bool simd_check_ssse3_aes(void)
106 {
107 return cpu_has_aesni && cpu_has_ssse3;
108 }
109
110 static bool simd_check_avx_aes(void)
111 {
112 return cpu_has_aesni && cpu_has_avx;
113 }
114
115 static bool simd_check_ssse3_pclmul(void)
116 {
117 return cpu_has_pclmulqdq && cpu_has_ssse3;
118 }
119
120 static bool simd_check_avx_pclmul(void)
121 {
122 return cpu_has_pclmulqdq && cpu_has_avx;
123 }
124
125 static bool simd_check_avx512f(void)
126 {
127 return cpu_has_avx512f;
128 }
129 #define simd_check_avx512f_opmask simd_check_avx512f
130 #define simd_check_avx512f_sg simd_check_avx512f
131
132 static bool simd_check_avx512f_vl(void)
133 {
134 return cpu_has_avx512f && cpu_has_avx512vl;
135 }
136 #define simd_check_avx512vl_sg simd_check_avx512f_vl
137
138 static bool simd_check_avx512dq(void)
139 {
140 return cpu_has_avx512dq;
141 }
142 #define simd_check_avx512dq_opmask simd_check_avx512dq
143
144 static bool simd_check_avx512dq_vl(void)
145 {
146 return cpu_has_avx512dq && cpu_has_avx512vl;
147 }
148
149 static bool simd_check_avx512bw(void)
150 {
151 return cpu_has_avx512bw;
152 }
153 #define simd_check_avx512bw_opmask simd_check_avx512bw
154
155 static bool simd_check_avx512bw_vl(void)
156 {
157 return cpu_has_avx512bw && cpu_has_avx512vl;
158 }
159
160 static bool simd_check_avx512vbmi(void)
161 {
162 return cpu_has_avx512_vbmi;
163 }
164
165 static bool simd_check_avx512vbmi_vl(void)
166 {
167 return cpu_has_avx512_vbmi && cpu_has_avx512vl;
168 }
169
170 static bool simd_check_sse4_sha(void)
171 {
172 return cpu_has_sha && cpu_has_sse4_2;
173 }
174
175 static bool simd_check_avx_sha(void)
176 {
177 return cpu_has_sha && cpu_has_avx;
178 }
179
180 static bool simd_check_avx512f_sha_vl(void)
181 {
182 return cpu_has_sha && cpu_has_avx512vl;
183 }
184
185 static bool simd_check_avx2_vaes(void)
186 {
187 return cpu_has_aesni && cpu_has_vaes && cpu_has_avx2;
188 }
189
190 static bool simd_check_avx512bw_vaes(void)
191 {
192 return cpu_has_aesni && cpu_has_vaes && cpu_has_avx512bw;
193 }
194
195 static bool simd_check_avx512bw_vaes_vl(void)
196 {
197 return cpu_has_aesni && cpu_has_vaes &&
198 cpu_has_avx512bw && cpu_has_avx512vl;
199 }
200
201 static bool simd_check_avx2_vpclmulqdq(void)
202 {
203 return cpu_has_vpclmulqdq && cpu_has_avx2;
204 }
205
206 static bool simd_check_avx512bw_vpclmulqdq(void)
207 {
208 return cpu_has_vpclmulqdq && cpu_has_avx512bw;
209 }
210
211 static bool simd_check_avx512bw_vpclmulqdq_vl(void)
212 {
213 return cpu_has_vpclmulqdq && cpu_has_avx512bw && cpu_has_avx512vl;
214 }
215
216 static bool simd_check_avx512vbmi2_vpclmulqdq(void)
217 {
218 return cpu_has_avx512_vbmi2 && simd_check_avx512bw_vpclmulqdq();
219 }
220
221 static bool simd_check_avx512vbmi2_vpclmulqdq_vl(void)
222 {
223 return cpu_has_avx512_vbmi2 && simd_check_avx512bw_vpclmulqdq_vl();
224 }
225
226 static bool simd_check_sse2_gf(void)
227 {
228 return cpu_has_gfni && cpu_has_sse2;
229 }
230
231 static bool simd_check_avx2_gf(void)
232 {
233 return cpu_has_gfni && cpu_has_avx2;
234 }
235
236 static bool simd_check_avx512bw_gf(void)
237 {
238 return cpu_has_gfni && cpu_has_avx512bw;
239 }
240
241 static bool simd_check_avx512bw_gf_vl(void)
242 {
243 return cpu_has_gfni && cpu_has_avx512vl;
244 }
245
246 static bool simd_check_avx512fp16(void)
247 {
248 return cpu_has_avx512_fp16;
249 }
250
251 static bool simd_check_avx512fp16_vl(void)
252 {
253 return cpu_has_avx512_fp16 && cpu_has_avx512vl;
254 }
255
256 static void simd_set_regs(struct cpu_user_regs *regs)
257 {
258 if ( cpu_has_mmx )
259 asm volatile ( "emms" );
260 }
261
262 static bool simd_check_regs(const struct cpu_user_regs *regs)
263 {
264 if ( !regs->eax )
265 return true;
266 if ( (int)regs->eax > 0 )
267 printf("[line %u] ", (unsigned int)regs->eax);
268 else
269 printf("[FMA line %u] ", (unsigned int)-regs->eax);
270 return false;
271 }
272
273 static const struct {
274 const void *code;
275 size_t size;
276 unsigned int bitness;
277 const char*name;
278 bool (*check_cpu)(void);
279 void (*set_regs)(struct cpu_user_regs *);
280 bool (*check_regs)(const struct cpu_user_regs *);
281 } blobs[] = {
282 #define BLOWFISH(bits, desc, tag) \
283 { .code = blowfish_x86_ ## bits ## tag, \
284 .size = sizeof(blowfish_x86_ ## bits ## tag), \
285 .bitness = bits, .name = #desc, \
286 .set_regs = blowfish_set_regs, \
287 .check_regs = blowfish_check_regs }
288 #ifdef __x86_64__
289 BLOWFISH(64, blowfish, ),
290 #endif
291 BLOWFISH(32, blowfish, ),
292 BLOWFISH(32, blowfish (push), _mno_accumulate_outgoing_args),
293 #undef BLOWFISH
294 #define SIMD_(bits, desc, feat, form) \
295 { .code = feat ## _x86_ ## bits ## _D ## _ ## form, \
296 .size = sizeof(feat ## _x86_ ## bits ## _D ## _ ## form), \
297 .bitness = bits, .name = #desc, \
298 .check_cpu = simd_check_ ## feat, \
299 .set_regs = simd_set_regs, \
300 .check_regs = simd_check_regs }
301 #define AVX512VL_(bits, desc, feat, form) \
302 { .code = feat ## _x86_ ## bits ## _D ## _ ## form, \
303 .size = sizeof(feat ## _x86_ ## bits ## _D ## _ ## form), \
304 .bitness = bits, .name = "AVX512" #desc, \
305 .check_cpu = simd_check_ ## feat ## _vl, \
306 .set_regs = simd_set_regs, \
307 .check_regs = simd_check_regs }
308 #ifdef __x86_64__
309 # define SIMD(desc, feat, form) SIMD_(64, desc, feat, form), \
310 SIMD_(32, desc, feat, form)
311 # define AVX512VL(desc, feat, form) AVX512VL_(64, desc, feat, form), \
312 AVX512VL_(32, desc, feat, form)
313 #else
314 # define SIMD(desc, feat, form) SIMD_(32, desc, feat, form)
315 # define AVX512VL(desc, feat, form) AVX512VL_(32, desc, feat, form)
316 #endif
317 SIMD(3DNow! single, _3dnow, 8f4),
318 SIMD(SSE scalar single, sse, f4),
319 SIMD(SSE packed single, sse, 16f4),
320 SIMD(SSE2 scalar single, sse2, f4),
321 SIMD(SSE2 packed single, sse2, 16f4),
322 SIMD(SSE2 scalar double, sse2, f8),
323 SIMD(SSE2 packed double, sse2, 16f8),
324 SIMD(SSE2 packed s8, sse2, 16i1),
325 SIMD(SSE2 packed u8, sse2, 16u1),
326 SIMD(SSE2 packed s16, sse2, 16i2),
327 SIMD(SSE2 packed u16, sse2, 16u2),
328 SIMD(SSE2 packed s32, sse2, 16i4),
329 SIMD(SSE2 packed u32, sse2, 16u4),
330 SIMD(SSE2 packed s64, sse2, 16i8),
331 SIMD(SSE2 packed u64, sse2, 16u8),
332 SIMD(SSE4 scalar single, sse4, f4),
333 SIMD(SSE4 packed single, sse4, 16f4),
334 SIMD(SSE4 scalar double, sse4, f8),
335 SIMD(SSE4 packed double, sse4, 16f8),
336 SIMD(SSE4 packed s8, sse4, 16i1),
337 SIMD(SSE4 packed u8, sse4, 16u1),
338 SIMD(SSE4 packed s16, sse4, 16i2),
339 SIMD(SSE4 packed u16, sse4, 16u2),
340 SIMD(SSE4 packed s32, sse4, 16i4),
341 SIMD(SSE4 packed u32, sse4, 16u4),
342 SIMD(SSE4 packed s64, sse4, 16i8),
343 SIMD(SSE4 packed u64, sse4, 16u8),
344 SIMD(AVX scalar single, avx, f4),
345 SIMD(AVX 128bit single, avx, 16f4),
346 SIMD(AVX 256bit single, avx, 32f4),
347 SIMD(AVX scalar double, avx, f8),
348 SIMD(AVX 128bit double, avx, 16f8),
349 SIMD(AVX 256bit double, avx, 32f8),
350 SIMD(FMA4 scalar single, fma4, f4),
351 SIMD(FMA4 128bit single, fma4, 16f4),
352 SIMD(FMA4 256bit single, fma4, 32f4),
353 SIMD(FMA4 scalar double, fma4, f8),
354 SIMD(FMA4 128bit double, fma4, 16f8),
355 SIMD(FMA4 256bit double, fma4, 32f8),
356 SIMD(FMA scalar single, fma, f4),
357 SIMD(FMA 128bit single, fma, 16f4),
358 SIMD(FMA 256bit single, fma, 32f4),
359 SIMD(FMA scalar double, fma, f8),
360 SIMD(FMA 128bit double, fma, 16f8),
361 SIMD(FMA 256bit double, fma, 32f8),
362 SIMD(AVX2 128bit single, avx2, 16f4),
363 SIMD(AVX2 256bit single, avx2, 32f4),
364 SIMD(AVX2 128bit double, avx2, 16f8),
365 SIMD(AVX2 256bit double, avx2, 32f8),
366 SIMD(AVX2 s8x16, avx2, 16i1),
367 SIMD(AVX2 u8x16, avx2, 16u1),
368 SIMD(AVX2 s16x8, avx2, 16i2),
369 SIMD(AVX2 u16x8, avx2, 16u2),
370 SIMD(AVX2 s32x4, avx2, 16i4),
371 SIMD(AVX2 u32x4, avx2, 16u4),
372 SIMD(AVX2 s64x2, avx2, 16i8),
373 SIMD(AVX2 u64x2, avx2, 16u8),
374 SIMD(AVX2 s8x32, avx2, 32i1),
375 SIMD(AVX2 u8x32, avx2, 32u1),
376 SIMD(AVX2 s16x16, avx2, 32i2),
377 SIMD(AVX2 u16x16, avx2, 32u2),
378 SIMD(AVX2 s32x8, avx2, 32i4),
379 SIMD(AVX2 u32x8, avx2, 32u4),
380 SIMD(AVX2 s64x4, avx2, 32i8),
381 SIMD(AVX2 u64x4, avx2, 32u8),
382 SIMD(AVX2 S/G f32[4x32], avx2_sg, 16x4f4),
383 SIMD(AVX2 S/G f64[2x32], avx2_sg, 16x4f8),
384 SIMD(AVX2 S/G f32[2x64], avx2_sg, 16x8f4),
385 SIMD(AVX2 S/G f64[2x64], avx2_sg, 16x8f8),
386 SIMD(AVX2 S/G f32[8x32], avx2_sg, 32x4f4),
387 SIMD(AVX2 S/G f64[4x32], avx2_sg, 32x4f8),
388 SIMD(AVX2 S/G f32[4x64], avx2_sg, 32x8f4),
389 SIMD(AVX2 S/G f64[4x64], avx2_sg, 32x8f8),
390 SIMD(AVX2 S/G i32[4x32], avx2_sg, 16x4i4),
391 SIMD(AVX2 S/G i64[2x32], avx2_sg, 16x4i8),
392 SIMD(AVX2 S/G i32[2x64], avx2_sg, 16x8i4),
393 SIMD(AVX2 S/G i64[2x64], avx2_sg, 16x8i8),
394 SIMD(AVX2 S/G i32[8x32], avx2_sg, 32x4i4),
395 SIMD(AVX2 S/G i64[4x32], avx2_sg, 32x4i8),
396 SIMD(AVX2 S/G i32[4x64], avx2_sg, 32x8i4),
397 SIMD(AVX2 S/G i64[4x64], avx2_sg, 32x8i8),
398 #ifdef __x86_64__
399 SIMD_(64, AVX2 S/G %ymm8+, avx2_sg, high),
400 #endif
401 SIMD(XOP 128bit single, xop, 16f4),
402 SIMD(XOP 256bit single, xop, 32f4),
403 SIMD(XOP 128bit double, xop, 16f8),
404 SIMD(XOP 256bit double, xop, 32f8),
405 SIMD(XOP s8x16, xop, 16i1),
406 SIMD(XOP u8x16, xop, 16u1),
407 SIMD(XOP s16x8, xop, 16i2),
408 SIMD(XOP u16x8, xop, 16u2),
409 SIMD(XOP s32x4, xop, 16i4),
410 SIMD(XOP u32x4, xop, 16u4),
411 SIMD(XOP s64x2, xop, 16i8),
412 SIMD(XOP u64x2, xop, 16u8),
413 SIMD(XOP i8x32, xop, 32i1),
414 SIMD(XOP i16x16, xop, 32i2),
415 SIMD(XOP i32x8, xop, 32i4),
416 SIMD(XOP i64x4, xop, 32i8),
417 SIMD(AES (legacy), ssse3_aes, 16),
418 SIMD(AES (VEX/x16), avx_aes, 16),
419 SIMD(PCLMUL (legacy), ssse3_pclmul, 16),
420 SIMD(PCLMUL (VEX/x2), avx_pclmul, 16),
421 SIMD(OPMASK/w, avx512f_opmask, 2),
422 SIMD(OPMASK+DQ/b, avx512dq_opmask, 1),
423 SIMD(OPMASK+DQ/w, avx512dq_opmask, 2),
424 SIMD(OPMASK+BW/d, avx512bw_opmask, 4),
425 SIMD(OPMASK+BW/q, avx512bw_opmask, 8),
426 SIMD(AVX512F f32 scalar, avx512f, f4),
427 SIMD(AVX512F f32x16, avx512f, 64f4),
428 SIMD(AVX512F f64 scalar, avx512f, f8),
429 SIMD(AVX512F f64x8, avx512f, 64f8),
430 SIMD(AVX512F s32x16, avx512f, 64i4),
431 SIMD(AVX512F u32x16, avx512f, 64u4),
432 SIMD(AVX512F s64x8, avx512f, 64i8),
433 SIMD(AVX512F u64x8, avx512f, 64u8),
434 SIMD(AVX512F S/G f32[16x32], avx512f_sg, 64x4f4),
435 SIMD(AVX512F S/G f64[ 8x32], avx512f_sg, 64x4f8),
436 SIMD(AVX512F S/G f32[ 8x64], avx512f_sg, 64x8f4),
437 SIMD(AVX512F S/G f64[ 8x64], avx512f_sg, 64x8f8),
438 SIMD(AVX512F S/G i32[16x32], avx512f_sg, 64x4i4),
439 SIMD(AVX512F S/G i64[ 8x32], avx512f_sg, 64x4i8),
440 SIMD(AVX512F S/G i32[ 8x64], avx512f_sg, 64x8i4),
441 SIMD(AVX512F S/G i64[ 8x64], avx512f_sg, 64x8i8),
442 #ifdef __x86_64__
443 SIMD_(64, AVX512F S/G %zmm8+, avx512f_sg, higher),
444 SIMD_(64, AVX512F S/G %zmm16+, avx512f_sg, highest),
445 #endif
446 AVX512VL(VL f32x4, avx512f, 16f4),
447 AVX512VL(VL f64x2, avx512f, 16f8),
448 AVX512VL(VL f32x8, avx512f, 32f4),
449 AVX512VL(VL f64x4, avx512f, 32f8),
450 AVX512VL(VL s32x4, avx512f, 16i4),
451 AVX512VL(VL u32x4, avx512f, 16u4),
452 AVX512VL(VL s32x8, avx512f, 32i4),
453 AVX512VL(VL u32x8, avx512f, 32u4),
454 AVX512VL(VL s64x2, avx512f, 16i8),
455 AVX512VL(VL u64x2, avx512f, 16u8),
456 AVX512VL(VL s64x4, avx512f, 32i8),
457 AVX512VL(VL u64x4, avx512f, 32u8),
458 SIMD(AVX512VL S/G f32[4x32], avx512f_sg, 16x4f4),
459 SIMD(AVX512VL S/G f64[2x32], avx512f_sg, 16x4f8),
460 SIMD(AVX512VL S/G f32[2x64], avx512f_sg, 16x8f4),
461 SIMD(AVX512VL S/G f64[2x64], avx512f_sg, 16x8f8),
462 SIMD(AVX512VL S/G f32[8x32], avx512f_sg, 32x4f4),
463 SIMD(AVX512VL S/G f64[4x32], avx512f_sg, 32x4f8),
464 SIMD(AVX512VL S/G f32[4x64], avx512f_sg, 32x8f4),
465 SIMD(AVX512VL S/G f64[4x64], avx512f_sg, 32x8f8),
466 SIMD(AVX512VL S/G i32[4x32], avx512f_sg, 16x4i4),
467 SIMD(AVX512VL S/G i64[2x32], avx512f_sg, 16x4i8),
468 SIMD(AVX512VL S/G i32[2x64], avx512f_sg, 16x8i4),
469 SIMD(AVX512VL S/G i64[2x64], avx512f_sg, 16x8i8),
470 SIMD(AVX512VL S/G i32[8x32], avx512f_sg, 32x4i4),
471 SIMD(AVX512VL S/G i64[4x32], avx512f_sg, 32x4i8),
472 SIMD(AVX512VL S/G i32[4x64], avx512f_sg, 32x8i4),
473 SIMD(AVX512VL S/G i64[4x64], avx512f_sg, 32x8i8),
474 SIMD(AVX512BW s8x64, avx512bw, 64i1),
475 SIMD(AVX512BW u8x64, avx512bw, 64u1),
476 SIMD(AVX512BW s16x32, avx512bw, 64i2),
477 SIMD(AVX512BW u16x32, avx512bw, 64u2),
478 AVX512VL(BW+VL s8x16, avx512bw, 16i1),
479 AVX512VL(BW+VL u8x16, avx512bw, 16u1),
480 AVX512VL(BW+VL s8x32, avx512bw, 32i1),
481 AVX512VL(BW+VL u8x32, avx512bw, 32u1),
482 AVX512VL(BW+VL s16x8, avx512bw, 16i2),
483 AVX512VL(BW+VL u16x8, avx512bw, 16u2),
484 AVX512VL(BW+VL s16x16, avx512bw, 32i2),
485 AVX512VL(BW+VL u16x16, avx512bw, 32u2),
486 SIMD(AVX512DQ f32x16, avx512dq, 64f4),
487 SIMD(AVX512DQ f64x8, avx512dq, 64f8),
488 SIMD(AVX512DQ s32x16, avx512dq, 64i4),
489 SIMD(AVX512DQ u32x16, avx512dq, 64u4),
490 SIMD(AVX512DQ s64x8, avx512dq, 64i8),
491 SIMD(AVX512DQ u64x8, avx512dq, 64u8),
492 AVX512VL(DQ+VL f32x4, avx512dq, 16f4),
493 AVX512VL(DQ+VL f64x2, avx512dq, 16f8),
494 AVX512VL(DQ+VL f32x8, avx512dq, 32f4),
495 AVX512VL(DQ+VL f64x4, avx512dq, 32f8),
496 AVX512VL(DQ+VL s32x4, avx512dq, 16i4),
497 AVX512VL(DQ+VL u32x4, avx512dq, 16u4),
498 AVX512VL(DQ+VL s32x8, avx512dq, 32i4),
499 AVX512VL(DQ+VL u32x8, avx512dq, 32u4),
500 AVX512VL(DQ+VL s64x2, avx512dq, 16i8),
501 AVX512VL(DQ+VL u64x2, avx512dq, 16u8),
502 AVX512VL(DQ+VL s64x4, avx512dq, 32i8),
503 AVX512VL(DQ+VL u64x4, avx512dq, 32u8),
504 SIMD(AVX512_VBMI s8x64, avx512vbmi, 64i1),
505 SIMD(AVX512_VBMI u8x64, avx512vbmi, 64u1),
506 SIMD(AVX512_VBMI s16x32, avx512vbmi, 64i2),
507 SIMD(AVX512_VBMI u16x32, avx512vbmi, 64u2),
508 AVX512VL(_VBMI+VL s8x16, avx512vbmi, 16i1),
509 AVX512VL(_VBMI+VL u8x16, avx512vbmi, 16u1),
510 AVX512VL(_VBMI+VL s8x32, avx512vbmi, 32i1),
511 AVX512VL(_VBMI+VL u8x32, avx512vbmi, 32u1),
512 AVX512VL(_VBMI+VL s16x8, avx512vbmi, 16i2),
513 AVX512VL(_VBMI+VL u16x8, avx512vbmi, 16u2),
514 AVX512VL(_VBMI+VL s16x16, avx512vbmi, 32i2),
515 AVX512VL(_VBMI+VL u16x16, avx512vbmi, 32u2),
516 SIMD(AVX512_FP16 f16 scal,avx512fp16, f2),
517 SIMD(AVX512_FP16 f16x32, avx512fp16, 64f2),
518 AVX512VL(_FP16+VL f16x8, avx512fp16, 16f2),
519 AVX512VL(_FP16+VL f16x16,avx512fp16, 32f2),
520 SIMD(SHA, sse4_sha, 16),
521 SIMD(AVX+SHA, avx_sha, 16),
522 AVX512VL(VL+SHA, avx512f_sha, 16),
523 SIMD(VAES (VEX/x32), avx2_vaes, 32),
524 SIMD(VAES (EVEX/x64), avx512bw_vaes, 64),
525 AVX512VL(VL+VAES (x16), avx512bw_vaes, 16),
526 AVX512VL(VL+VAES (x32), avx512bw_vaes, 32),
527 SIMD(VPCLMUL (VEX/x4), avx2_vpclmulqdq, 32),
528 SIMD(VPCLMUL (EVEX/x8), avx512bw_vpclmulqdq, 64),
529 AVX512VL(VL+VPCLMUL (x2), avx512bw_vpclmulqdq, 16),
530 AVX512VL(VL+VPCLMUL (x4), avx512bw_vpclmulqdq, 32),
531 SIMD(AVX512_VBMI2+VPCLMUL (x8), avx512vbmi2_vpclmulqdq, 64),
532 AVX512VL(_VBMI2+VL+VPCLMUL (x2), avx512vbmi2_vpclmulqdq, 16),
533 AVX512VL(_VBMI2+VL+VPCLMUL (x4), avx512vbmi2_vpclmulqdq, 32),
534 SIMD(GFNI (legacy), sse2_gf, 16),
535 SIMD(GFNI (VEX/x16), avx2_gf, 16),
536 SIMD(GFNI (VEX/x32), avx2_gf, 32),
537 SIMD(GFNI (EVEX/x64), avx512bw_gf, 64),
538 AVX512VL(VL+GFNI (x16), avx512bw_gf, 16),
539 AVX512VL(VL+GFNI (x32), avx512bw_gf, 32),
540 #undef AVX512VL_
541 #undef AVX512VL
542 #undef SIMD_
543 #undef SIMD
544 };
545
546 static unsigned int bytes_read;
547
548 static int read(
549 enum x86_segment seg,
550 unsigned long offset,
551 void *p_data,
552 unsigned int bytes,
553 struct x86_emulate_ctxt *ctxt)
554 {
555 if ( verbose )
556 printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
557
558 switch ( seg )
559 {
560 uint64_t value;
561
562 case x86_seg_gdtr:
563 /* Fake system segment type matching table index. */
564 if ( (offset & 7) || (bytes > 8) )
565 return X86EMUL_UNHANDLEABLE;
566 #ifdef __x86_64__
567 if ( !(offset & 8) )
568 {
569 memset(p_data, 0, bytes);
570 return X86EMUL_OKAY;
571 }
572 value = (offset - 8) >> 4;
573 #else
574 value = (offset - 8) >> 3;
575 #endif
576 if ( value >= 0x10 )
577 return X86EMUL_UNHANDLEABLE;
578 value |= value << 40;
579 memcpy(p_data, &value, bytes);
580 return X86EMUL_OKAY;
581
582 case x86_seg_ldtr:
583 /* Fake user segment type matching table index. */
584 if ( (offset & 7) || (bytes > 8) )
585 return X86EMUL_UNHANDLEABLE;
586 value = offset >> 3;
587 if ( value >= 0x10 )
588 return X86EMUL_UNHANDLEABLE;
589 value |= (value | 0x10) << 40;
590 memcpy(p_data, &value, bytes);
591 return X86EMUL_OKAY;
592
593 default:
594 if ( !is_x86_user_segment(seg) )
595 return X86EMUL_UNHANDLEABLE;
596 case x86_seg_sys:
597 case x86_seg_none:
598 bytes_read += bytes;
599 break;
600 }
601 memcpy(p_data, (void *)offset, bytes);
602 return X86EMUL_OKAY;
603 }
604
605 static int fetch(
606 unsigned long offset,
607 void *p_data,
608 unsigned int bytes,
609 struct x86_emulate_ctxt *ctxt)
610 {
611 if ( verbose )
612 printf("** %s(CS:%p,, %u,)\n", __func__, (void *)offset, bytes);
613
614 memcpy(p_data, (void *)offset, bytes);
615 return X86EMUL_OKAY;
616 }
617
618 static int write(
619 enum x86_segment seg,
620 unsigned long offset,
621 void *p_data,
622 unsigned int bytes,
623 struct x86_emulate_ctxt *ctxt)
624 {
625 if ( verbose )
626 printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
627
628 if ( !is_x86_user_segment(seg) )
629 return X86EMUL_UNHANDLEABLE;
630 memcpy((void *)offset, p_data, bytes);
631 return X86EMUL_OKAY;
632 }
633
634 static int rmw(
635 enum x86_segment seg,
636 unsigned long offset,
637 unsigned int bytes,
638 uint32_t *eflags,
639 struct x86_emulate_state *state,
640 struct x86_emulate_ctxt *ctxt)
641 {
642 return x86_emul_rmw((void *)offset, bytes, eflags, state, ctxt);
643 }
644
645 static int cmpxchg(
646 enum x86_segment seg,
647 unsigned long offset,
648 void *old,
649 void *new,
650 unsigned int bytes,
651 bool lock,
652 struct x86_emulate_ctxt *ctxt)
653 {
654 if ( verbose )
655 printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
656
657 if ( !is_x86_user_segment(seg) )
658 return X86EMUL_UNHANDLEABLE;
659 memcpy((void *)offset, new, bytes);
660 return X86EMUL_OKAY;
661 }
662
663 static int blk(
664 enum x86_segment seg,
665 unsigned long offset,
666 void *p_data,
667 unsigned int bytes,
668 uint32_t *eflags,
669 struct x86_emulate_state *state,
670 struct x86_emulate_ctxt *ctxt)
671 {
672 return x86_emul_blk((void *)offset, p_data, bytes, eflags, state, ctxt);
673 }
674
675 static int read_segment(
676 enum x86_segment seg,
677 struct segment_register *reg,
678 struct x86_emulate_ctxt *ctxt)
679 {
680 if ( !is_x86_user_segment(seg) )
681 return X86EMUL_UNHANDLEABLE;
682 memset(reg, 0, sizeof(*reg));
683 reg->p = 1;
684 return X86EMUL_OKAY;
685 }
686
687 static int read_msr(
688 unsigned int reg,
689 uint64_t *val,
690 struct x86_emulate_ctxt *ctxt)
691 {
692 switch ( reg )
693 {
694 case MSR_EFER:
695 *val = ctxt->addr_size > 32 ? EFER_LME | EFER_LMA : 0;
696 return X86EMUL_OKAY;
697
698 case MSR_TSC_AUX:
699 #define TSC_AUX_VALUE 0xCACACACA
700 *val = TSC_AUX_VALUE;
701 return X86EMUL_OKAY;
702 }
703
704 return X86EMUL_UNHANDLEABLE;
705 }
706
707 #define INVPCID_ADDR 0x12345678
708 #define INVPCID_PCID 0x123
709
710 static int read_cr_invpcid(
711 unsigned int reg,
712 unsigned long *val,
713 struct x86_emulate_ctxt *ctxt)
714 {
715 int rc = emul_test_read_cr(reg, val, ctxt);
716
717 if ( rc == X86EMUL_OKAY && reg == 4 )
718 *val |= X86_CR4_PCIDE;
719
720 return rc;
721 }
722
723 static int tlb_op_invpcid(
724 enum x86emul_tlb_op op,
725 unsigned long addr,
726 unsigned long aux,
727 struct x86_emulate_ctxt *ctxt)
728 {
729 static unsigned int seq;
730
731 if ( op != x86emul_invpcid || addr != INVPCID_ADDR ||
732 x86emul_invpcid_pcid(aux) != (seq < 4 ? 0 : INVPCID_PCID) ||
733 x86emul_invpcid_type(aux) != (seq++ & 3) )
734 return X86EMUL_UNHANDLEABLE;
735
736 return X86EMUL_OKAY;
737 }
738
739 static struct x86_emulate_ops emulops = {
740 .read = read,
741 .insn_fetch = fetch,
742 .write = write,
743 .cmpxchg = cmpxchg,
744 .blk = blk,
745 .read_segment = read_segment,
746 .cpuid = emul_test_cpuid,
747 .read_cr = emul_test_read_cr,
748 .read_xcr = emul_test_read_xcr,
749 .read_msr = read_msr,
750 .get_fpu = emul_test_get_fpu,
751 .put_fpu = emul_test_put_fpu,
752 };
753
754 #define EFLAGS_ALWAYS_SET (X86_EFLAGS_IF | X86_EFLAGS_MBS)
755 #define EFLAGS_MASK (X86_EFLAGS_ARITH_MASK | EFLAGS_ALWAYS_SET)
756
757 #define MMAP_ADDR 0x100000
758
759 /*
760 * 64-bit OSes may not (be able to) properly restore the two selectors in
761 * the FPU environment. Zap them so that memcmp() on two saved images will
762 * work regardless of whether a context switch occurred in the middle.
763 *
764 * Additionally on AMD-like CPUs FDP/FIP/FOP may get lost across context
765 * switches, when there's no unmasked pending FP exception: With
766 * CPUID.80000008.EBX[2] clear, the fields don't get written/read by
767 * {F,}XSAVE / {F,}XRSTOR (which OSes often compensate for by invoking an
768 * insn forcing the fields to gain a deterministic value), whereas with said
769 * bit set, zeroes will get written (and hence later restored).
770 */
771 static void zap_fpsel(unsigned int *env, bool is_32bit)
772 {
773 if ( is_32bit )
774 {
775 env[4] &= ~0xffff;
776 env[6] &= ~0xffff;
777 }
778 else
779 {
780 env[2] &= ~0xffff;
781 env[3] &= ~0xffff;
782 }
783
784 if ( cpu_policy.x86_vendor != X86_VENDOR_AMD &&
785 cpu_policy.x86_vendor != X86_VENDOR_HYGON )
786 return;
787
788 if ( is_32bit )
789 {
790 env[3] = 0;
791 env[4] = 0;
792 env[5] = 0;
793 }
794 else
795 {
796 env[1] &= 0xffff;
797 env[2] = 0;
798 }
799 }
800
801 static void zap_xfpsel(unsigned int *env)
802 {
803 env[3] &= ~0xffff;
804 env[5] &= ~0xffff;
805 }
806
807 #ifdef __x86_64__
808 # define STKVAL_DISP 64
809 static const struct {
810 const char *descr;
811 uint8_t opcode[8];
812 /* Index 0: AMD, index 1: Intel. */
813 uint8_t opc_len[2];
814 int8_t stkoff[2];
815 int32_t disp[2];
816 } vendor_tests[] = {
817 {
818 .descr = "retw",
819 .opcode = { 0x66, 0xc3 },
820 .opc_len = { 2, 2 },
821 .stkoff = { 2, 8 },
822 .disp = { STKVAL_DISP - MMAP_ADDR, STKVAL_DISP },
823 }, {
824 .descr = "retw $16",
825 .opcode = { 0x66, 0xc2, 0x10, 0x00 },
826 .opc_len = { 4, 4 },
827 .stkoff = { 2 + 16, 8 + 16 },
828 .disp = { STKVAL_DISP - MMAP_ADDR, STKVAL_DISP },
829 }, {
830 .descr = "jmpw .+16",
831 .opcode = { 0x66, 0xeb, 0x10 },
832 .opc_len = { 3, 3 },
833 .disp = { 3 + 16 - MMAP_ADDR, 3 + 16 },
834 }, {
835 .descr = "jmpw .+128",
836 .opcode = { 0x66, 0xe9, 0x80, 0x00, 0x00, 0x00 },
837 .opc_len = { 4, 6 },
838 .disp = { 4 + 128 - MMAP_ADDR, 6 + 128 },
839 }, {
840 .descr = "callw .+16",
841 .opcode = { 0x66, 0xe8, 0x10, 0x00, 0x00, 0x00 },
842 .opc_len = { 4, 6 },
843 .stkoff = { -2, -8 },
844 .disp = { 4 + 16 - MMAP_ADDR, 6 + 16 },
845 }, {
846 .descr = "jzw .+16",
847 .opcode = { 0x66, 0x74, 0x10 },
848 .opc_len = { 3, 3 },
849 .disp = { 3, 3 },
850 }, {
851 .descr = "jzw .+128",
852 .opcode = { 0x66, 0x0f, 0x84, 0x80, 0x00, 0x00, 0x00 },
853 .opc_len = { 5, 7 },
854 .disp = { 5, 7 },
855 }, {
856 .descr = "jnzw .+16",
857 .opcode = { 0x66, 0x75, 0x10 },
858 .opc_len = { 3, 3 },
859 .disp = { 3 + 16 - MMAP_ADDR, 3 + 16 },
860 }, {
861 .descr = "jnzw .+128",
862 .opcode = { 0x66, 0x0f, 0x85, 0x80, 0x00, 0x00, 0x00 },
863 .opc_len = { 5, 7 },
864 .disp = { 5 + 128 - MMAP_ADDR, 7 + 128 },
865 }, {
866 .descr = "loopqw .+16 (RCX>1)",
867 .opcode = { 0x66, 0xe0, 0x10 },
868 .opc_len = { 3, 3 },
869 .disp = { 3 + 16 - MMAP_ADDR, 3 + 16 },
870 }, {
871 .descr = "looplw .+16 (ECX=1)",
872 .opcode = { 0x66, 0x67, 0xe0, 0x10 },
873 .opc_len = { 4, 4 },
874 .disp = { 4, 4 },
875 }, {
876 .descr = "jrcxzw .+16 (RCX>0)",
877 .opcode = { 0x66, 0xe3, 0x10 },
878 .opc_len = { 3, 3 },
879 .disp = { 3, 3 },
880 }, {
881 .descr = "jecxzw .+16 (ECX=0)",
882 .opcode = { 0x66, 0x67, 0xe3, 0x10 },
883 .opc_len = { 4, 4 },
884 .disp = { 4 + 16 - MMAP_ADDR, 4 + 16 },
885 }, {
886 .descr = "jmpw *(%rsp)",
887 .opcode = { 0x66, 0xff, 0x24, 0x24 },
888 .opc_len = { 4, 4 },
889 .disp = { STKVAL_DISP - MMAP_ADDR, STKVAL_DISP },
890 }, {
891 .descr = "callw *(%rsp)",
892 .opcode = { 0x66, 0xff, 0x14, 0x24 },
893 .opc_len = { 4, 4 },
894 .stkoff = { -2, -8 },
895 .disp = { STKVAL_DISP - MMAP_ADDR, STKVAL_DISP },
896 },
897 };
898 #endif
899
900 int main(int argc, char **argv)
901 {
902 struct x86_emulate_ctxt ctxt;
903 struct cpu_user_regs regs;
904 char *instr;
905 unsigned int *res, i, j;
906 bool stack_exec;
907 int rc;
908 #ifdef __x86_64__
909 unsigned int vendor_native;
910 #else
911 unsigned int bcdres_native, bcdres_emul;
912 #endif
913
914 /* Disable output buffering. */
915 setbuf(stdout, NULL);
916
917 ctxt.regs = &regs;
918 ctxt.force_writeback = 0;
919 ctxt.cpu_policy = &cpu_policy;
920 ctxt.lma = sizeof(void *) == 8;
921 ctxt.addr_size = 8 * sizeof(void *);
922 ctxt.sp_size = 8 * sizeof(void *);
923
924 res = mmap((void *)MMAP_ADDR, MMAP_SZ, PROT_READ|PROT_WRITE|PROT_EXEC,
925 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
926 if ( res == MAP_FAILED )
927 {
928 fprintf(stderr, "mmap to low address failed\n");
929 exit(1);
930 }
931 instr = (char *)res + 0x100;
932
933 stack_exec = emul_test_init();
934
935 if ( !stack_exec )
936 printf("Warning: Stack could not be made executable (%d).\n", errno);
937
938 rmw_restart:
939 printf("%-40s", "Testing addl %ecx,(%eax)...");
940 instr[0] = 0x01; instr[1] = 0x08;
941 regs.eflags = 0x200;
942 regs.eip = (unsigned long)&instr[0];
943 regs.ecx = 0x12345678;
944 regs.eax = (unsigned long)res;
945 *res = 0x7FFFFFFF;
946 rc = x86_emulate(&ctxt, &emulops);
947 if ( (rc != X86EMUL_OKAY) ||
948 (*res != 0x92345677) ||
949 (regs.eflags != 0xa94) ||
950 (regs.eip != (unsigned long)&instr[2]) )
951 goto fail;
952 printf("okay\n");
953
954 printf("%-40s", "Testing addl %ecx,%eax...");
955 instr[0] = 0x01; instr[1] = 0xc8;
956 regs.eflags = 0x200;
957 regs.eip = (unsigned long)&instr[0];
958 regs.ecx = 0x12345678;
959 regs.eax = 0x7FFFFFFF;
960 rc = x86_emulate(&ctxt, &emulops);
961 if ( (rc != X86EMUL_OKAY) ||
962 (regs.ecx != 0x12345678) ||
963 (regs.eax != 0x92345677) ||
964 (regs.eflags != 0xa94) ||
965 (regs.eip != (unsigned long)&instr[2]) )
966 goto fail;
967 printf("okay\n");
968
969 printf("%-40s", "Testing xorl (%eax),%ecx...");
970 instr[0] = 0x33; instr[1] = 0x08;
971 regs.eflags = 0x200;
972 regs.eip = (unsigned long)&instr[0];
973 #ifdef __x86_64__
974 regs.ecx = 0xFFFFFFFF12345678UL;
975 #else
976 regs.ecx = 0x12345678UL;
977 #endif
978 regs.eax = (unsigned long)res;
979 rc = x86_emulate(&ctxt, &emulops);
980 if ( (rc != X86EMUL_OKAY) ||
981 (*res != 0x92345677) ||
982 (regs.ecx != 0x8000000FUL) ||
983 (regs.eip != (unsigned long)&instr[2]) )
984 goto fail;
985 printf("okay\n");
986
987 printf("%-40s", "Testing movl (%eax),%ecx...");
988 instr[0] = 0x8b; instr[1] = 0x08;
989 regs.eflags = 0x200;
990 regs.eip = (unsigned long)&instr[0];
991 regs.ecx = ~0UL;
992 regs.eax = (unsigned long)res;
993 rc = x86_emulate(&ctxt, &emulops);
994 if ( (rc != X86EMUL_OKAY) ||
995 (*res != 0x92345677) ||
996 (regs.ecx != 0x92345677UL) ||
997 (regs.eip != (unsigned long)&instr[2]) )
998 goto fail;
999 printf("okay\n");
1000
1001 printf("%-40s", "Testing lock cmpxchgb %cl,(%ebx)...");
1002 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb0; instr[3] = 0x0b;
1003 regs.eflags = 0x200;
1004 regs.eip = (unsigned long)&instr[0];
1005 regs.eax = 0x92345677UL;
1006 regs.ecx = 0xAA;
1007 regs.ebx = (unsigned long)res;
1008 rc = x86_emulate(&ctxt, &emulops);
1009 if ( (rc != X86EMUL_OKAY) ||
1010 (*res != 0x923456AA) ||
1011 (regs.eflags != 0x244) ||
1012 (regs.eax != 0x92345677UL) ||
1013 (regs.eip != (unsigned long)&instr[4]) )
1014 goto fail;
1015 printf("okay\n");
1016
1017 printf("%-40s", "Testing lock cmpxchgb %cl,(%ebx)...");
1018 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb0; instr[3] = 0x0b;
1019 regs.eflags = 0x200;
1020 regs.eip = (unsigned long)&instr[0];
1021 regs.eax = 0xAABBCC77UL;
1022 regs.ecx = 0xFF;
1023 regs.ebx = (unsigned long)res;
1024 rc = x86_emulate(&ctxt, &emulops);
1025 if ( (rc != X86EMUL_OKAY) ||
1026 (*res != 0x923456AA) ||
1027 ((regs.eflags & 0xad5) != 0xa91) ||
1028 (regs.eax != 0xAABBCCAA) ||
1029 (regs.ecx != 0xFF) ||
1030 (regs.eip != (unsigned long)&instr[4]) )
1031 goto fail;
1032 printf("okay\n");
1033
1034 printf("%-40s", "Testing xchgl %ecx,(%eax)...");
1035 instr[0] = 0x87; instr[1] = 0x08;
1036 regs.eflags = 0x200;
1037 regs.eip = (unsigned long)&instr[0];
1038 regs.ecx = 0x12345678;
1039 regs.eax = (unsigned long)res;
1040 rc = x86_emulate(&ctxt, &emulops);
1041 if ( (rc != X86EMUL_OKAY) ||
1042 (*res != 0x12345678) ||
1043 (regs.eflags != 0x200) ||
1044 (regs.ecx != 0x923456AA) ||
1045 (regs.eip != (unsigned long)&instr[2]) )
1046 goto fail;
1047 printf("okay\n");
1048
1049 printf("%-40s", "Testing xchg %bl,%ah...");
1050 instr[0] = 0x86; instr[1] = 0xdc;
1051 regs.eflags = X86_EFLAGS_IF;
1052 regs.eip = (unsigned long)&instr[0];
1053 regs.eax = 0xaaaabbcc;
1054 regs.ebx = 0xddddeeff;
1055 rc = x86_emulate(&ctxt, &emulops);
1056 if ( (rc != X86EMUL_OKAY) ||
1057 (regs.eax != 0xaaaaffcc) ||
1058 (regs.ebx != 0xddddeebb) ||
1059 (regs.eflags != X86_EFLAGS_IF) ||
1060 (regs.eip != (unsigned long)&instr[2]) )
1061 goto fail;
1062 printf("okay\n");
1063
1064 printf("%-40s", "Testing lock cmpxchgl %ecx,(%ebx)...");
1065 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb1; instr[3] = 0x0b;
1066 regs.eflags = 0x200;
1067 *res = 0x923456AA;
1068 regs.eip = (unsigned long)&instr[0];
1069 regs.eax = 0x923456AAUL;
1070 regs.ecx = 0xDDEEFF00L;
1071 regs.ebx = (unsigned long)res;
1072 rc = x86_emulate(&ctxt, &emulops);
1073 if ( (rc != X86EMUL_OKAY) ||
1074 (*res != 0xDDEEFF00) ||
1075 (regs.eflags != 0x244) ||
1076 (regs.eax != 0x923456AAUL) ||
1077 (regs.eip != (unsigned long)&instr[4]) )
1078 goto fail;
1079 printf("okay\n");
1080
1081 printf("%-40s", "Testing notb (%edi)...");
1082 instr[0] = 0xf6; instr[1] = 0x17;
1083 *res = 0x22334455;
1084 regs.eflags = EFLAGS_MASK;
1085 regs.eip = (unsigned long)&instr[0];
1086 regs.edi = (unsigned long)res;
1087 rc = x86_emulate(&ctxt, &emulops);
1088 if ( (rc != X86EMUL_OKAY) ||
1089 (*res != 0x223344aa) ||
1090 ((regs.eflags & EFLAGS_MASK) != EFLAGS_MASK) ||
1091 (regs.eip != (unsigned long)&instr[2]) )
1092 goto fail;
1093 printf("okay\n");
1094
1095 printf("%-40s", "Testing rcll $2,(%edi)...");
1096 instr[0] = 0xc1; instr[1] = 0x17; instr[2] = 0x02;
1097 *res = 0x2233445F;
1098 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_CF;
1099 regs.eip = (unsigned long)&instr[0];
1100 regs.edi = (unsigned long)res;
1101 rc = x86_emulate(&ctxt, &emulops);
1102 if ( (rc != X86EMUL_OKAY) ||
1103 (*res != ((0x2233445F << 2) | 2)) ||
1104 ((regs.eflags & (EFLAGS_MASK & ~X86_EFLAGS_OF))
1105 != EFLAGS_ALWAYS_SET) ||
1106 (regs.eip != (unsigned long)&instr[3]) )
1107 goto fail;
1108 printf("okay\n");
1109
1110 printf("%-40s", "Testing shrdl $8,%ecx,(%edi)...");
1111 instr[0] = 0x0f; instr[1] = 0xac; instr[2] = 0x0f; instr[3] = 0x08;
1112 *res = 0x22334455;
1113 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_CF;
1114 regs.eip = (unsigned long)&instr[0];
1115 regs.ecx = 0x44332211;
1116 regs.edi = (unsigned long)res;
1117 rc = x86_emulate(&ctxt, &emulops);
1118 if ( (rc != X86EMUL_OKAY) ||
1119 (*res != 0x11223344) ||
1120 ((regs.eflags & (EFLAGS_MASK & ~(X86_EFLAGS_OF|X86_EFLAGS_AF)))
1121 != (EFLAGS_ALWAYS_SET | X86_EFLAGS_PF)) ||
1122 (regs.eip != (unsigned long)&instr[4]) )
1123 goto fail;
1124 printf("okay\n");
1125
1126 printf("%-40s", "Testing btrl $0x1,(%edi)...");
1127 instr[0] = 0x0f; instr[1] = 0xba; instr[2] = 0x37; instr[3] = 0x01;
1128 *res = 0x2233445F;
1129 regs.eflags = EFLAGS_ALWAYS_SET;
1130 regs.eip = (unsigned long)&instr[0];
1131 regs.edi = (unsigned long)res;
1132 rc = x86_emulate(&ctxt, &emulops);
1133 if ( (rc != X86EMUL_OKAY) ||
1134 (*res != 0x2233445D) ||
1135 ((regs.eflags & (EFLAGS_ALWAYS_SET | X86_EFLAGS_ZF |
1136 X86_EFLAGS_CF)) !=
1137 (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF)) ||
1138 (regs.eip != (unsigned long)&instr[4]) )
1139 goto fail;
1140 printf("okay\n");
1141
1142 printf("%-40s", "Testing btrl %eax,(%edi)...");
1143 instr[0] = 0x0f; instr[1] = 0xb3; instr[2] = 0x07;
1144 *res = 0x2233445F;
1145 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_ZF;
1146 regs.eip = (unsigned long)&instr[0];
1147 regs.eax = -32;
1148 regs.edi = (unsigned long)(res+1);
1149 rc = x86_emulate(&ctxt, &emulops);
1150 if ( (rc != X86EMUL_OKAY) ||
1151 (*res != 0x2233445E) ||
1152 ((regs.eflags & (EFLAGS_ALWAYS_SET | X86_EFLAGS_ZF |
1153 X86_EFLAGS_CF)) !=
1154 (EFLAGS_ALWAYS_SET | X86_EFLAGS_ZF | X86_EFLAGS_CF)) ||
1155 (regs.eip != (unsigned long)&instr[3]) )
1156 goto fail;
1157 printf("okay\n");
1158
1159 #ifdef __x86_64__
1160 printf("%-40s", "Testing btcq %r8,(%r11)...");
1161 instr[0] = 0x4d; instr[1] = 0x0f; instr[2] = 0xbb; instr[3] = 0x03;
1162 regs.eflags = EFLAGS_ALWAYS_SET;
1163 regs.rip = (unsigned long)&instr[0];
1164 regs.r8 = (-1L << 40) + 1;
1165 regs.r11 = (unsigned long)(res + (1L << 35));
1166 rc = x86_emulate(&ctxt, &emulops);
1167 if ( (rc != X86EMUL_OKAY) ||
1168 (*res != 0x2233445C) ||
1169 ((regs.eflags & (EFLAGS_ALWAYS_SET | X86_EFLAGS_ZF |
1170 X86_EFLAGS_CF)) !=
1171 (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF)) ||
1172 (regs.rip != (unsigned long)&instr[4]) )
1173 goto fail;
1174 printf("okay\n");
1175 #endif
1176
1177 printf("%-40s", "Testing xadd %ax,(%ecx)...");
1178 instr[0] = 0x66; instr[1] = 0x0f; instr[2] = 0xc1; instr[3] = 0x01;
1179 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_ARITH_MASK;
1180 regs.eip = (unsigned long)&instr[0];
1181 regs.ecx = (unsigned long)res;
1182 regs.eax = 0x12345678;
1183 *res = 0x11111111;
1184 rc = x86_emulate(&ctxt, &emulops);
1185 if ( (rc != X86EMUL_OKAY) ||
1186 (*res != 0x11116789) ||
1187 (regs.eax != 0x12341111) ||
1188 ((regs.eflags & EFLAGS_MASK) != EFLAGS_ALWAYS_SET) ||
1189 (regs.eip != (unsigned long)&instr[4]) )
1190 goto fail;
1191 printf("okay\n");
1192
1193 if ( !emulops.rmw )
1194 {
1195 printf("[Switching to read-modify-write mode]\n");
1196 emulops.rmw = rmw;
1197 goto rmw_restart;
1198 }
1199
1200 printf("%-40s", "Testing rep movsw...");
1201 instr[0] = 0xf3; instr[1] = 0x66; instr[2] = 0xa5;
1202 *res = 0x22334455;
1203 regs.eflags = 0x200;
1204 regs.ecx = 23;
1205 regs.eip = (unsigned long)&instr[0];
1206 regs.esi = (unsigned long)res + 0;
1207 regs.edi = (unsigned long)res + 2;
1208 rc = x86_emulate(&ctxt, &emulops);
1209 if ( (rc != X86EMUL_OKAY) ||
1210 (*res != 0x44554455) ||
1211 (regs.eflags != 0x200) ||
1212 (regs.ecx != 22) ||
1213 (regs.esi != ((unsigned long)res + 2)) ||
1214 (regs.edi != ((unsigned long)res + 4)) ||
1215 (regs.eip != (unsigned long)&instr[0]) )
1216 goto fail;
1217 printf("okay\n");
1218
1219 res[0] = 0x12345678;
1220 res[1] = 0x87654321;
1221
1222 printf("%-40s", "Testing cmpxchg8b (%edi) [succeeding]...");
1223 instr[0] = 0x0f; instr[1] = 0xc7; instr[2] = 0x0f;
1224 regs.eflags = 0x200;
1225 regs.eax = res[0];
1226 regs.edx = res[1];
1227 regs.ebx = 0x9999AAAA;
1228 regs.ecx = 0xCCCCFFFF;
1229 regs.eip = (unsigned long)&instr[0];
1230 regs.edi = (unsigned long)res;
1231 rc = x86_emulate(&ctxt, &emulops);
1232 if ( (rc != X86EMUL_OKAY) ||
1233 (res[0] != 0x9999AAAA) ||
1234 (res[1] != 0xCCCCFFFF) ||
1235 ((regs.eflags&0x240) != 0x240) ||
1236 (regs.eip != (unsigned long)&instr[3]) )
1237 goto fail;
1238 printf("okay\n");
1239
1240 printf("%-40s", "Testing cmpxchg8b (%edi) [failing]...");
1241 instr[0] = 0x0f; instr[1] = 0xc7; instr[2] = 0x0f;
1242 regs.eflags = 0x200;
1243 regs.eip = (unsigned long)&instr[0];
1244 regs.edi = (unsigned long)res;
1245 rc = x86_emulate(&ctxt, &emulops);
1246 if ( (rc != X86EMUL_OKAY) ||
1247 (res[0] != 0x9999AAAA) ||
1248 (res[1] != 0xCCCCFFFF) ||
1249 (regs.eax != 0x9999AAAA) ||
1250 (regs.edx != 0xCCCCFFFF) ||
1251 ((regs.eflags&0x240) != 0x200) ||
1252 (regs.eip != (unsigned long)&instr[3]) )
1253 goto fail;
1254 printf("okay\n");
1255
1256 printf("%-40s", "Testing cmpxchg8b (%edi) [opsize]...");
1257 instr[0] = 0x66; instr[1] = 0x0f; instr[2] = 0xc7; instr[3] = 0x0f;
1258 res[0] = 0x12345678;
1259 res[1] = 0x87654321;
1260 regs.eflags = 0x200;
1261 regs.eip = (unsigned long)&instr[0];
1262 regs.edi = (unsigned long)res;
1263 rc = x86_emulate(&ctxt, &emulops);
1264 if ( (rc != X86EMUL_OKAY) ||
1265 (res[0] != 0x12345678) ||
1266 (res[1] != 0x87654321) ||
1267 (regs.eax != 0x12345678) ||
1268 (regs.edx != 0x87654321) ||
1269 ((regs.eflags&0x240) != 0x200) ||
1270 (regs.eip != (unsigned long)&instr[4]) )
1271 goto fail;
1272 printf("okay\n");
1273
1274 printf("%-40s", "Testing movsxbd (%eax),%ecx...");
1275 instr[0] = 0x0f; instr[1] = 0xbe; instr[2] = 0x08;
1276 regs.eflags = 0x200;
1277 regs.eip = (unsigned long)&instr[0];
1278 regs.ecx = 0x12345678;
1279 regs.eax = (unsigned long)res;
1280 *res = 0x82;
1281 rc = x86_emulate(&ctxt, &emulops);
1282 if ( (rc != X86EMUL_OKAY) ||
1283 (*res != 0x82) ||
1284 (regs.ecx != 0xFFFFFF82) ||
1285 ((regs.eflags&0x240) != 0x200) ||
1286 (regs.eip != (unsigned long)&instr[3]) )
1287 goto fail;
1288 printf("okay\n");
1289
1290 printf("%-40s", "Testing movzxwd (%eax),%ecx...");
1291 instr[0] = 0x0f; instr[1] = 0xb7; instr[2] = 0x08;
1292 regs.eflags = 0x200;
1293 regs.eip = (unsigned long)&instr[0];
1294 regs.ecx = 0x12345678;
1295 regs.eax = (unsigned long)res;
1296 *res = 0x1234aa82;
1297 rc = x86_emulate(&ctxt, &emulops);
1298 if ( (rc != X86EMUL_OKAY) ||
1299 (*res != 0x1234aa82) ||
1300 (regs.ecx != 0xaa82) ||
1301 ((regs.eflags&0x240) != 0x200) ||
1302 (regs.eip != (unsigned long)&instr[3]) )
1303 goto fail;
1304 printf("okay\n");
1305
1306 #ifndef __x86_64__
1307 printf("%-40s", "Testing arpl %cx,(%eax)...");
1308 instr[0] = 0x63; instr[1] = 0x08;
1309 regs.eflags = 0x200;
1310 regs.eip = (unsigned long)&instr[0];
1311 regs.ecx = 0x22222222;
1312 regs.eax = (unsigned long)res;
1313 *res = 0x33331111;
1314 bytes_read = 0;
1315 rc = x86_emulate(&ctxt, &emulops);
1316 if ( (rc != X86EMUL_OKAY) ||
1317 (*res != 0x33331112) ||
1318 (regs.ecx != 0x22222222) ||
1319 !(regs.eflags & X86_EFLAGS_ZF) ||
1320 (regs.eip != (unsigned long)&instr[2]) )
1321 goto fail;
1322 #else
1323 printf("%-40s", "Testing movsxd (%rax),%rcx...");
1324 instr[0] = 0x48; instr[1] = 0x63; instr[2] = 0x08;
1325 regs.eip = (unsigned long)&instr[0];
1326 regs.ecx = 0x123456789abcdef;
1327 regs.eax = (unsigned long)res;
1328 *res = 0xfedcba98;
1329 bytes_read = 0;
1330 rc = x86_emulate(&ctxt, &emulops);
1331 if ( (rc != X86EMUL_OKAY) ||
1332 (*res != 0xfedcba98) ||
1333 (regs.ecx != 0xfffffffffedcba98) ||
1334 (regs.eip != (unsigned long)&instr[3]) )
1335 goto fail;
1336 if ( bytes_read != 4 )
1337 {
1338 printf("%u bytes read - ", bytes_read);
1339 goto fail;
1340 }
1341 #endif
1342 printf("okay\n");
1343
1344 printf("%-40s", "Testing dec %ax...");
1345 #ifndef __x86_64__
1346 instr[0] = 0x66; instr[1] = 0x48;
1347 #else
1348 instr[0] = 0x66; instr[1] = 0xff; instr[2] = 0xc8;
1349 #endif
1350 regs.eflags = 0x200;
1351 regs.eip = (unsigned long)&instr[0];
1352 regs.eax = 0x00000000;
1353 rc = x86_emulate(&ctxt, &emulops);
1354 if ( (rc != X86EMUL_OKAY) ||
1355 (regs.eax != 0x0000ffff) ||
1356 ((regs.eflags&0x240) != 0x200) ||
1357 (regs.eip != (unsigned long)&instr[2 + (ctxt.addr_size > 32)]) )
1358 goto fail;
1359 printf("okay\n");
1360
1361 printf("%-40s", "Testing lea 8(%ebp),%eax...");
1362 instr[0] = 0x8d; instr[1] = 0x45; instr[2] = 0x08;
1363 regs.eflags = 0x200;
1364 regs.eip = (unsigned long)&instr[0];
1365 regs.eax = 0x12345678;
1366 regs.ebp = 0xaaaaaaaa;
1367 rc = x86_emulate(&ctxt, &emulops);
1368 if ( (rc != X86EMUL_OKAY) ||
1369 (regs.eax != 0xaaaaaab2) ||
1370 ((regs.eflags&0x240) != 0x200) ||
1371 (regs.eip != (unsigned long)&instr[3]) )
1372 goto fail;
1373 printf("okay\n");
1374
1375 printf("%-40s", "Testing imull -4(%ecx)...");
1376 instr[0] = 0xf7; instr[1] = 0x69; instr[2] = 0xfc;
1377 regs.eflags = EFLAGS_ALWAYS_SET;
1378 regs.eip = (unsigned long)&instr[0];
1379 regs.eax = 0x89abcdef;
1380 res[0] = 0x12345678;
1381 regs.ecx = (unsigned long)(res + 1);
1382 rc = x86_emulate(&ctxt, &emulops);
1383 if ( (rc != X86EMUL_OKAY) ||
1384 (regs.eax != 0x89abcdef * 0x12345678) ||
1385 (regs.edx != (uint64_t)((int64_t)(int32_t)0x89abcdef *
1386 0x12345678) >> 32) ||
1387 ((regs.eflags & (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF |
1388 X86_EFLAGS_OF)) !=
1389 (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF | X86_EFLAGS_OF)) ||
1390 (regs.eip != (unsigned long)&instr[3]) )
1391 goto fail;
1392 printf("okay\n");
1393
1394 printf("%-40s", "Testing imul $3,-4(%edx),%ecx...");
1395 instr[0] = 0x6b; instr[1] = 0x4a; instr[2] = 0xfc; instr[3] = 0x03;
1396 regs.eflags = EFLAGS_ALWAYS_SET;
1397 regs.eip = (unsigned long)&instr[0];
1398 regs.ecx = 0x12345678;
1399 res[0] = 0x89abcdef;
1400 regs.edx = (unsigned long)(res + 1);
1401 rc = x86_emulate(&ctxt, &emulops);
1402 if ( (rc != X86EMUL_OKAY) ||
1403 (regs.ecx != 0x89abcdef * 3) ||
1404 ((regs.eflags & (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF |
1405 X86_EFLAGS_OF)) !=
1406 (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF | X86_EFLAGS_OF)) ||
1407 (regs.eip != (unsigned long)&instr[4]) )
1408 goto fail;
1409 printf("okay\n");
1410
1411 #ifndef __x86_64__
1412 printf("%-40s", "Testing daa/das (all inputs)...");
1413 /* Bits 0-7: AL; Bit 8: EFLAGS.AF; Bit 9: EFLAGS.CF; Bit 10: DAA vs. DAS. */
1414 for ( i = 0; i < 0x800; i++ )
1415 {
1416 regs.eflags = (i & 0x200) ? X86_EFLAGS_CF : 0;
1417 regs.eflags |= (i & 0x100) ? X86_EFLAGS_AF : 0;
1418 if ( i & 0x400 )
1419 __asm__ (
1420 "pushf; andl $~0x11,(%%esp); or %1,(%%esp); popf; das; "
1421 "pushf; popl %1"
1422 : "=a" (bcdres_native), "=r" (regs.eflags)
1423 : "0" (i & 0xff), "1" (regs.eflags) );
1424 else
1425 __asm__ (
1426 "pushf; andl $~0x11,(%%esp); or %1,(%%esp); popf; daa; "
1427 "pushf; popl %1"
1428 : "=a" (bcdres_native), "=r" (regs.eflags)
1429 : "0" (i & 0xff), "1" (regs.eflags) );
1430 bcdres_native |= (regs.eflags & X86_EFLAGS_PF) ? 0x1000 : 0;
1431 bcdres_native |= (regs.eflags & X86_EFLAGS_ZF) ? 0x800 : 0;
1432 bcdres_native |= (regs.eflags & X86_EFLAGS_SF) ? 0x400 : 0;
1433 bcdres_native |= (regs.eflags & X86_EFLAGS_CF) ? 0x200 : 0;
1434 bcdres_native |= (regs.eflags & X86_EFLAGS_AF) ? 0x100 : 0;
1435
1436 instr[0] = (i & 0x400) ? 0x2f: 0x27; /* daa/das */
1437 regs.eflags = (i & 0x200) ? X86_EFLAGS_CF : 0;
1438 regs.eflags |= (i & 0x100) ? X86_EFLAGS_AF : 0;
1439 regs.eip = (unsigned long)&instr[0];
1440 regs.eax = (unsigned char)i;
1441 rc = x86_emulate(&ctxt, &emulops);
1442 bcdres_emul = regs.eax;
1443 bcdres_emul |= (regs.eflags & X86_EFLAGS_PF) ? 0x1000 : 0;
1444 bcdres_emul |= (regs.eflags & X86_EFLAGS_ZF) ? 0x800 : 0;
1445 bcdres_emul |= (regs.eflags & X86_EFLAGS_SF) ? 0x400 : 0;
1446 bcdres_emul |= (regs.eflags & X86_EFLAGS_CF) ? 0x200 : 0;
1447 bcdres_emul |= (regs.eflags & X86_EFLAGS_AF) ? 0x100 : 0;
1448 if ( (rc != X86EMUL_OKAY) || (regs.eax > 255) ||
1449 (regs.eip != (unsigned long)&instr[1]) )
1450 goto fail;
1451
1452 if ( bcdres_emul != bcdres_native )
1453 {
1454 printf("%s: AL=%02x %s %s\n"
1455 "Output: AL=%02x %s %s %s %s %s\n"
1456 "Emul.: AL=%02x %s %s %s %s %s\n",
1457 (i & 0x400) ? "DAS" : "DAA",
1458 (unsigned char)i,
1459 (i & 0x200) ? "CF" : " ",
1460 (i & 0x100) ? "AF" : " ",
1461 (unsigned char)bcdres_native,
1462 (bcdres_native & 0x200) ? "CF" : " ",
1463 (bcdres_native & 0x100) ? "AF" : " ",
1464 (bcdres_native & 0x1000) ? "PF" : " ",
1465 (bcdres_native & 0x800) ? "ZF" : " ",
1466 (bcdres_native & 0x400) ? "SF" : " ",
1467 (unsigned char)bcdres_emul,
1468 (bcdres_emul & 0x200) ? "CF" : " ",
1469 (bcdres_emul & 0x100) ? "AF" : " ",
1470 (bcdres_emul & 0x1000) ? "PF" : " ",
1471 (bcdres_emul & 0x800) ? "ZF" : " ",
1472 (bcdres_emul & 0x400) ? "SF" : " ");
1473 goto fail;
1474 }
1475 }
1476 printf("okay\n");
1477 #else /* x86-64 */
1478 printf("%-40s", "Testing cmovz %ecx,%eax...");
1479 instr[0] = 0x0f; instr[1] = 0x44; instr[2] = 0xc1;
1480 regs.eflags = 0x200;
1481 regs.eip = (unsigned long)&instr[0];
1482 regs.rax = 0x1111111122222222;
1483 regs.rcx = 0x3333333344444444;
1484 rc = x86_emulate(&ctxt, &emulops);
1485 if ( (rc != X86EMUL_OKAY) ||
1486 (regs.rax != 0x0000000022222222) ||
1487 (regs.rcx != 0x3333333344444444) ||
1488 (regs.eflags != 0x200) ||
1489 (regs.eip != (unsigned long)&instr[3]) )
1490 goto fail;
1491 printf("okay\n");
1492
1493 vendor_native = cpu_policy.x86_vendor;
1494 for ( cpu_policy.x86_vendor = X86_VENDOR_AMD; ; )
1495 {
1496 unsigned int v = cpu_policy.x86_vendor == X86_VENDOR_INTEL;
1497 const char *vendor = cpu_policy.x86_vendor == X86_VENDOR_INTEL ? "Intel" : "AMD";
1498 uint64_t *stk = (void *)res + MMAP_SZ - 16;
1499
1500 regs.rcx = 2;
1501 for ( i = 0; i < ARRAY_SIZE(vendor_tests); ++i )
1502 {
1503 printf("%-*s",
1504 40 - printf("Testing %s [%s]", vendor_tests[i].descr, vendor),
1505 "...");
1506 memcpy(instr, vendor_tests[i].opcode, vendor_tests[i].opc_len[v]);
1507 regs.eflags = EFLAGS_ALWAYS_SET;
1508 regs.rip = (unsigned long)instr;
1509 regs.rsp = (unsigned long)stk;
1510 regs.rcx |= 0x8765432100000000UL;
1511 stk[0] = regs.rip + STKVAL_DISP;
1512 rc = x86_emulate(&ctxt, &emulops);
1513 if ( (rc != X86EMUL_OKAY) ||
1514 (regs.eflags != EFLAGS_ALWAYS_SET) ||
1515 (regs.rip != (unsigned long)instr +
1516 (vendor_tests[i].disp[v]
1517 ?: vendor_tests[i].opc_len[v])) ||
1518 (regs.rsp != (unsigned long)stk + vendor_tests[i].stkoff[v]) )
1519 goto fail;
1520 /* For now only call insns push something onto the stack. */
1521 if ( regs.rsp < (unsigned long)stk )
1522 {
1523 unsigned long opc_end = (unsigned long)instr +
1524 vendor_tests[i].opc_len[v];
1525
1526 if ( memcmp(&opc_end, (void *)regs.rsp,
1527 min((unsigned long)stk - regs.rsp, 8UL)) )
1528 goto fail;
1529 }
1530 printf("okay\n");
1531 }
1532
1533 if ( cpu_policy.x86_vendor == X86_VENDOR_INTEL )
1534 break;
1535 cpu_policy.x86_vendor = X86_VENDOR_INTEL;
1536 }
1537 cpu_policy.x86_vendor = vendor_native;
1538
1539 printf("%-40s", "Testing cmpbxadd %rbx,%r9,(%rdx)...");
1540 if ( stack_exec && cpu_has_cmpccxadd )
1541 {
1542 instr[0] = 0xc4; instr[1] = 0x62; instr[2] = 0xe1; instr[3] = 0xe2; instr[4] = 0x0a;
1543 regs.rip = (unsigned long)&instr[0];
1544 regs.eflags = EFLAGS_ALWAYS_SET;
1545 res[0] = 0x11223344;
1546 res[1] = 0x01020304;
1547 regs.rdx = (unsigned long)res;
1548 regs.r9 = 0x0001020300112233UL;
1549 regs.rbx = 0x0101010101010101UL;
1550 rc = x86_emulate(&ctxt, &emulops);
1551 if ( (rc != X86EMUL_OKAY) ||
1552 (regs.eip != (unsigned long)&instr[5]) ||
1553 (regs.r9 != 0x0102030411223344UL) ||
1554 (regs.rbx != 0x0101010101010101UL) ||
1555 ((regs.eflags & EFLAGS_MASK) !=
1556 (X86_EFLAGS_PF | EFLAGS_ALWAYS_SET)) ||
1557 (res[0] != 0x11223344) ||
1558 (res[1] != 0x01020304) )
1559 goto fail;
1560
1561 regs.rip = (unsigned long)&instr[0];
1562 regs.r9 <<= 8;
1563 rc = x86_emulate(&ctxt, &emulops);
1564 if ( (rc != X86EMUL_OKAY) ||
1565 (regs.eip != (unsigned long)&instr[5]) ||
1566 (regs.r9 != 0x0102030411223344UL) ||
1567 (regs.rbx != 0x0101010101010101UL) ||
1568 ((regs.eflags & EFLAGS_MASK) !=
1569 (X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_SF |
1570 EFLAGS_ALWAYS_SET)) ||
1571 (res[0] != 0x12233445) ||
1572 (res[1] != 0x02030405) )
1573 goto fail;
1574 printf("okay\n");
1575
1576 printf("%-40s", "Testing cmpsxadd %r9d,%ebx,4(%r10)...");
1577 instr[1] = 0xc2; instr[2] = 0x31; instr[3] = 0xe8; instr[4] = 0x5a; instr[5] = 0x04;
1578 regs.rip = (unsigned long)&instr[0];
1579 res[2] = res[0] = ~0;
1580 regs.r10 = (unsigned long)res;
1581 rc = x86_emulate(&ctxt, &emulops);
1582 if ( (rc != X86EMUL_OKAY) ||
1583 (regs.eip != (unsigned long)&instr[6]) ||
1584 (regs.r9 != 0x0102030411223344UL) ||
1585 (regs.rbx != 0x02030405) ||
1586 ((regs.eflags & EFLAGS_MASK) != EFLAGS_ALWAYS_SET) ||
1587 (res[0] + 1) ||
1588 (res[1] != 0x02030405) ||
1589 (res[2] + 1) )
1590 goto fail;
1591
1592 regs.rip = (unsigned long)&instr[0];
1593 regs.rbx <<= 8;
1594 rc = x86_emulate(&ctxt, &emulops);
1595 if ( (rc != X86EMUL_OKAY) ||
1596 (regs.eip != (unsigned long)&instr[6]) ||
1597 (regs.r9 != 0x0102030411223344UL) ||
1598 (regs.rbx != 0x02030405) ||
1599 ((regs.eflags & EFLAGS_MASK) !=
1600 (X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_SF |
1601 EFLAGS_ALWAYS_SET)) ||
1602 (res[0] + 1) ||
1603 (res[1] != 0x13253749) ||
1604 (res[2] + 1) )
1605 goto fail;
1606 printf("okay\n");
1607 }
1608 else
1609 printf("skipped\n");
1610 #endif /* x86-64 */
1611
1612 printf("%-40s", "Testing shld $1,%ecx,(%edx)...");
1613 res[0] = 0x12345678;
1614 regs.edx = (unsigned long)res;
1615 regs.ecx = 0x9abcdef0;
1616 instr[0] = 0x0f; instr[1] = 0xa4; instr[2] = 0x0a; instr[3] = 0x01;
1617 for ( i = 0; i < 0x20; ++i )
1618 {
1619 uint32_t r = res[0];
1620 const uint32_t m = X86_EFLAGS_ARITH_MASK & ~X86_EFLAGS_AF;
1621 unsigned long f;
1622
1623 asm ( "shld $1,%2,%0; pushf; pop %1"
1624 : "+rm" (r), "=rm" (f) : "r" ((uint32_t)regs.ecx) );
1625 regs.eflags = f ^ m;
1626 regs.eip = (unsigned long)&instr[0];
1627 rc = x86_emulate(&ctxt, &emulops);
1628 if ( (rc != X86EMUL_OKAY) ||
1629 (regs.eip != (unsigned long)&instr[4]) ||
1630 (res[0] != r) ||
1631 ((regs.eflags ^ f) & m) )
1632 goto fail;
1633 regs.ecx <<= 1;
1634 }
1635 printf("okay\n");
1636
1637 printf("%-40s", "Testing movbe (%ecx),%eax...");
1638 instr[0] = 0x0f; instr[1] = 0x38; instr[2] = 0xf0; instr[3] = 0x01;
1639 regs.eflags = 0x200;
1640 regs.eip = (unsigned long)&instr[0];
1641 regs.ecx = (unsigned long)res;
1642 regs.eax = 0x11111111;
1643 *res = 0x12345678;
1644 rc = x86_emulate(&ctxt, &emulops);
1645 if ( (rc != X86EMUL_OKAY) ||
1646 (*res != 0x12345678) ||
1647 (regs.eax != 0x78563412) ||
1648 (regs.eflags != 0x200) ||
1649 (regs.eip != (unsigned long)&instr[4]) )
1650 goto fail;
1651 printf("okay\n");
1652
1653 printf("%-40s", "Testing movbe %ax,(%ecx)...");
1654 instr[0] = 0x66; instr[1] = 0x0f; instr[2] = 0x38; instr[3] = 0xf1; instr[4] = 0x01;
1655 regs.eip = (unsigned long)&instr[0];
1656 rc = x86_emulate(&ctxt, &emulops);
1657 if ( (rc != X86EMUL_OKAY) ||
1658 (*res != 0x12341234) ||
1659 (regs.eax != 0x78563412) ||
1660 (regs.eflags != 0x200) ||
1661 (regs.eip != (unsigned long)&instr[5]) )
1662 goto fail;
1663 printf("okay\n");
1664
1665 printf("%-40s", "Testing popcnt (%edx),%cx...");
1666 if ( cpu_has_popcnt )
1667 {
1668 instr[0] = 0x66; instr[1] = 0xf3;
1669 instr[2] = 0x0f; instr[3] = 0xb8; instr[4] = 0x0a;
1670
1671 *res = 0xfedcba98;
1672 regs.edx = (unsigned long)res;
1673 regs.eflags = 0xac3;
1674 regs.eip = (unsigned long)&instr[0];
1675 rc = x86_emulate(&ctxt, &emulops);
1676 if ( (rc != X86EMUL_OKAY) || (uint16_t)regs.ecx != 8 || *res != 0xfedcba98 ||
1677 (regs.eflags & 0xfeb) != 0x202 ||
1678 (regs.eip != (unsigned long)&instr[5]) )
1679 goto fail;
1680 printf("okay\n");
1681
1682 printf("%-40s", "Testing popcnt (%edx),%ecx...");
1683 regs.eflags = 0xac3;
1684 regs.eip = (unsigned long)&instr[1];
1685 rc = x86_emulate(&ctxt, &emulops);
1686 if ( (rc != X86EMUL_OKAY) || regs.ecx != 20 || *res != 0xfedcba98 ||
1687 (regs.eflags & 0xfeb) != 0x202 ||
1688 (regs.eip != (unsigned long)&instr[5]) )
1689 goto fail;
1690 printf("okay\n");
1691
1692 #ifdef __x86_64__
1693 printf("%-40s", "Testing popcnt (%rdx),%rcx...");
1694 instr[0] = 0xf3;
1695 instr[1] = 0x48;
1696 res[1] = 0x12345678;
1697 regs.eflags = 0xac3;
1698 regs.eip = (unsigned long)&instr[0];
1699 rc = x86_emulate(&ctxt, &emulops);
1700 if ( (rc != X86EMUL_OKAY) || regs.ecx != 33 ||
1701 res[0] != 0xfedcba98 || res[1] != 0x12345678 ||
1702 (regs.eflags & 0xfeb) != 0x202 ||
1703 (regs.eip != (unsigned long)&instr[5]) )
1704 goto fail;
1705 printf("okay\n");
1706 #endif
1707 }
1708 else
1709 printf("skipped\n");
1710
1711 printf("%-40s", "Testing lar (null selector)...");
1712 instr[0] = 0x0f; instr[1] = 0x02; instr[2] = 0xc1;
1713 regs.eflags = 0x240;
1714 regs.eip = (unsigned long)&instr[0];
1715 regs.ecx = 0;
1716 regs.eax = 0x11111111;
1717 rc = x86_emulate(&ctxt, &emulops);
1718 if ( (rc != X86EMUL_OKAY) ||
1719 (regs.eax != 0x11111111) ||
1720 (regs.eflags != 0x200) ||
1721 (regs.eip != (unsigned long)&instr[3]) )
1722 goto fail;
1723 printf("okay\n");
1724
1725 printf("%-40s", "Testing lsl (null selector)...");
1726 instr[0] = 0x0f; instr[1] = 0x03; instr[2] = 0xca;
1727 regs.eflags = 0x240;
1728 regs.eip = (unsigned long)&instr[0];
1729 regs.edx = 0;
1730 regs.ecx = 0x11111111;
1731 rc = x86_emulate(&ctxt, &emulops);
1732 if ( (rc != X86EMUL_OKAY) ||
1733 (regs.ecx != 0x11111111) ||
1734 (regs.eflags != 0x200) ||
1735 (regs.eip != (unsigned long)&instr[3]) )
1736 goto fail;
1737 printf("okay\n");
1738
1739 printf("%-40s", "Testing verr (null selector)...");
1740 instr[0] = 0x0f; instr[1] = 0x00; instr[2] = 0x21;
1741 regs.eflags = 0x240;
1742 regs.eip = (unsigned long)&instr[0];
1743 regs.ecx = (unsigned long)res;
1744 *res = 0;
1745 rc = x86_emulate(&ctxt, &emulops);
1746 if ( (rc != X86EMUL_OKAY) ||
1747 (regs.eflags != 0x200) ||
1748 (regs.eip != (unsigned long)&instr[3]) )
1749 goto fail;
1750 printf("okay\n");
1751
1752 printf("%-40s", "Testing verw (null selector)...");
1753 instr[0] = 0x0f; instr[1] = 0x00; instr[2] = 0x2a;
1754 regs.eflags = 0x240;
1755 regs.eip = (unsigned long)&instr[0];
1756 regs.ecx = 0;
1757 regs.edx = (unsigned long)res;
1758 rc = x86_emulate(&ctxt, &emulops);
1759 if ( (rc != X86EMUL_OKAY) ||
1760 (regs.eflags != 0x200) ||
1761 (regs.eip != (unsigned long)&instr[3]) )
1762 goto fail;
1763 printf("okay\n");
1764
1765 printf("%-40s", "Testing lar/lsl/verr/verw (all types)...");
1766 for ( i = 0; i < 0x20; ++i )
1767 {
1768 unsigned int sel = i < 0x10 ?
1769 #ifndef __x86_64__
1770 (i << 3) + 8
1771 #else
1772 (i << 4) + 8
1773 #endif
1774 : ((i - 0x10) << 3) | 4;
1775 bool failed;
1776
1777 #ifndef __x86_64__
1778 # define LAR_VALID 0xffff1a3eU
1779 # define LSL_VALID 0xffff0a0eU
1780 #else
1781 # define LAR_VALID 0xffff1a04U
1782 # define LSL_VALID 0xffff0a04U
1783 #endif
1784 #define VERR_VALID 0xccff0000U
1785 #define VERW_VALID 0x00cc0000U
1786
1787 instr[0] = 0x0f; instr[1] = 0x02; instr[2] = 0xc2;
1788 regs.eflags = (LAR_VALID >> i) & 1 ? 0x200 : 0x240;
1789 regs.eip = (unsigned long)&instr[0];
1790 regs.edx = sel;
1791 regs.eax = 0x11111111;
1792 rc = x86_emulate(&ctxt, &emulops);
1793 if ( (rc != X86EMUL_OKAY) ||
1794 (regs.eip != (unsigned long)&instr[3]) )
1795 goto fail;
1796 if ( (LAR_VALID >> i) & 1 )
1797 failed = (regs.eflags != 0x240) ||
1798 ((regs.eax & 0xf0ff00) != (i << 8));
1799 else
1800 failed = (regs.eflags != 0x200) ||
1801 (regs.eax != 0x11111111);
1802 if ( failed )
1803 {
1804 printf("LAR %04x (type %02x) ", sel, i);
1805 goto fail;
1806 }
1807
1808 instr[0] = 0x0f; instr[1] = 0x03; instr[2] = 0xd1;
1809 regs.eflags = (LSL_VALID >> i) & 1 ? 0x200 : 0x240;
1810 regs.eip = (unsigned long)&instr[0];
1811 regs.ecx = sel;
1812 regs.edx = 0x11111111;
1813 rc = x86_emulate(&ctxt, &emulops);
1814 if ( (rc != X86EMUL_OKAY) ||
1815 (regs.eip != (unsigned long)&instr[3]) )
1816 goto fail;
1817 if ( (LSL_VALID >> i) & 1 )
1818 failed = (regs.eflags != 0x240) ||
1819 (regs.edx != (i & 0xf));
1820 else
1821 failed = (regs.eflags != 0x200) ||
1822 (regs.edx != 0x11111111);
1823 if ( failed )
1824 {
1825 printf("LSL %04x (type %02x) ", sel, i);
1826 goto fail;
1827 }
1828
1829 instr[0] = 0x0f; instr[1] = 0x00; instr[2] = 0xe2;
1830 regs.eflags = (VERR_VALID >> i) & 1 ? 0x200 : 0x240;
1831 regs.eip = (unsigned long)&instr[0];
1832 regs.ecx = 0;
1833 regs.edx = sel;
1834 rc = x86_emulate(&ctxt, &emulops);
1835 if ( (rc != X86EMUL_OKAY) ||
1836 (regs.eip != (unsigned long)&instr[3]) )
1837 goto fail;
1838 if ( regs.eflags != ((VERR_VALID >> i) & 1 ? 0x240 : 0x200) )
1839 {
1840 printf("VERR %04x (type %02x) ", sel, i);
1841 goto fail;
1842 }
1843
1844 instr[0] = 0x0f; instr[1] = 0x00; instr[2] = 0xe9;
1845 regs.eflags = (VERW_VALID >> i) & 1 ? 0x200 : 0x240;
1846 regs.eip = (unsigned long)&instr[0];
1847 regs.ecx = sel;
1848 regs.edx = 0;
1849 rc = x86_emulate(&ctxt, &emulops);
1850 if ( (rc != X86EMUL_OKAY) ||
1851 (regs.eip != (unsigned long)&instr[3]) )
1852 goto fail;
1853 if ( regs.eflags != ((VERW_VALID >> i) & 1 ? 0x240 : 0x200) )
1854 {
1855 printf("VERW %04x (type %02x) ", sel, i);
1856 goto fail;
1857 }
1858 }
1859 printf("okay\n");
1860
1861 printf("%-40s", "Testing mov %%cr4,%%esi (bad ModRM)...");
1862 /*
1863 * Mod = 1, Reg = 4, R/M = 6 would normally encode a memory reference of
1864 * disp8(%esi), but mov to/from cr/dr are special and behave as if they
1865 * were encoded with Mod == 3.
1866 */
1867 instr[0] = 0x0f; instr[1] = 0x20, instr[2] = 0x66;
1868 instr[3] = 0; /* Supposed disp8. */
1869 regs.esi = 0;
1870 regs.eip = (unsigned long)&instr[0];
1871 rc = x86_emulate(&ctxt, &emulops);
1872 /*
1873 * We don't care precicely what gets read from %cr4 into %esi, just so
1874 * long as ModRM is treated as a register operand and 0(%esi) isn't
1875 * followed as a memory reference.
1876 */
1877 if ( (rc != X86EMUL_OKAY) ||
1878 (regs.eip != (unsigned long)&instr[3]) )
1879 goto fail;
1880 printf("okay\n");
1881
1882 #define decl_insn(which) extern const unsigned char which[], \
1883 which##_end[] asm ( ".L" #which "_end" )
1884 #define put_insn(which, insn) ".pushsection .test\n" \
1885 #which ": " insn "\n" \
1886 ".L" #which "_end:\n" \
1887 ".popsection"
1888 #define set_insn(which) (regs.eip = (unsigned long)(which))
1889 #define valid_eip(which) (regs.eip >= (unsigned long)(which) && \
1890 regs.eip < (unsigned long)which##_end)
1891 #define check_eip(which) (regs.eip == (unsigned long)which##_end)
1892
1893 printf("%-40s", "Testing andn (%edx),%ecx,%ebx...");
1894 if ( stack_exec && cpu_has_bmi1 )
1895 {
1896 decl_insn(andn);
1897
1898 asm volatile ( put_insn(andn, "andn (%0), %%ecx, %%ebx")
1899 :: "d" (NULL) );
1900 set_insn(andn);
1901
1902 *res = 0xfedcba98;
1903 regs.ecx = 0xcccc3333;
1904 regs.edx = (unsigned long)res;
1905 regs.eflags = 0xac3;
1906 rc = x86_emulate(&ctxt, &emulops);
1907 if ( (rc != X86EMUL_OKAY) || regs.ebx != 0x32108888 ||
1908 regs.ecx != 0xcccc3333 || *res != 0xfedcba98 ||
1909 (regs.eflags & 0xfeb) != 0x202 || !check_eip(andn) )
1910 goto fail;
1911 printf("okay\n");
1912 }
1913 else
1914 printf("skipped\n");
1915
1916 printf("%-40s", "Testing bextr %edx,(%ecx),%ebx...");
1917 if ( stack_exec && cpu_has_bmi1 )
1918 {
1919 decl_insn(bextr);
1920 #ifdef __x86_64__
1921 decl_insn(bextr64);
1922 #endif
1923
1924 asm volatile ( put_insn(bextr, "bextr %%edx, (%0), %%ebx")
1925 :: "c" (NULL) );
1926 set_insn(bextr);
1927
1928 regs.ecx = (unsigned long)res;
1929 regs.edx = 0x0a03;
1930 regs.eflags = 0xa43;
1931 rc = x86_emulate(&ctxt, &emulops);
1932 if ( (rc != X86EMUL_OKAY) || regs.ebx != ((*res >> 3) & 0x3ff) ||
1933 regs.edx != 0x0a03 || *res != 0xfedcba98 ||
1934 (regs.eflags & 0xf6b) != 0x202 || !check_eip(bextr) )
1935 goto fail;
1936 printf("okay\n");
1937 #ifdef __x86_64__
1938 printf("%-40s", "Testing bextr %r9,(%r10),%r11...");
1939
1940 asm volatile ( put_insn(bextr64, "bextr %r9, (%r10), %r11") );
1941 set_insn(bextr64);
1942
1943 res[0] = 0x76543210;
1944 res[1] = 0xfedcba98;
1945 regs.r10 = (unsigned long)res;
1946 regs.r9 = 0x211e;
1947 regs.eflags = 0xa43;
1948 rc = x86_emulate(&ctxt, &emulops);
1949 if ( (rc != X86EMUL_OKAY) || regs.r9 != 0x211e ||
1950 regs.r11 != (((unsigned long)(res[1] << 1) << 1) |
1951 (res[0] >> 30)) ||
1952 res[0] != 0x76543210 || res[1] != 0xfedcba98 ||
1953 (regs.eflags & 0xf6b) != 0x202 || !check_eip(bextr64) )
1954 goto fail;
1955 printf("okay\n");
1956 #endif
1957 }
1958 else
1959 printf("skipped\n");
1960
1961 printf("%-40s", "Testing blsi (%edx),%ecx...");
1962 if ( stack_exec && cpu_has_bmi1 )
1963 {
1964 decl_insn(blsi);
1965
1966 asm volatile ( put_insn(blsi, "blsi (%0), %%ecx")
1967 :: "d" (NULL) );
1968 set_insn(blsi);
1969
1970 *res = 0xfedcba98;
1971 regs.edx = (unsigned long)res;
1972 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_OF | X86_EFLAGS_SF | \
1973 X86_EFLAGS_ZF;
1974 rc = x86_emulate(&ctxt, &emulops);
1975 if ( (rc != X86EMUL_OKAY) || regs.ecx != 8 || *res != 0xfedcba98 ||
1976 (regs.eflags & (EFLAGS_MASK & ~(X86_EFLAGS_AF | X86_EFLAGS_PF))) !=
1977 (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF) ||
1978 !check_eip(blsi) )
1979 goto fail;
1980 printf("okay\n");
1981 }
1982 else
1983 printf("skipped\n");
1984
1985 printf("%-40s", "Testing blsmsk (%edx),%ecx...");
1986 if ( stack_exec && cpu_has_bmi1 )
1987 {
1988 decl_insn(blsmsk);
1989
1990 asm volatile ( put_insn(blsmsk, "blsmsk (%0), %%ecx")
1991 :: "d" (NULL) );
1992 set_insn(blsmsk);
1993
1994 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_OF | X86_EFLAGS_SF | \
1995 X86_EFLAGS_ZF | X86_EFLAGS_CF;
1996 rc = x86_emulate(&ctxt, &emulops);
1997 if ( (rc != X86EMUL_OKAY) || regs.ecx != 0xf || *res != 0xfedcba98 ||
1998 (regs.eflags & (EFLAGS_MASK & ~(X86_EFLAGS_AF | X86_EFLAGS_PF))) !=
1999 EFLAGS_ALWAYS_SET ||
2000 !check_eip(blsmsk) )
2001 goto fail;
2002 printf("okay\n");
2003 }
2004 else
2005 printf("skipped\n");
2006
2007 printf("%-40s", "Testing blsr (%edx),%ecx...");
2008 if ( stack_exec && cpu_has_bmi1 )
2009 {
2010 decl_insn(blsr);
2011
2012 asm volatile ( put_insn(blsr, "blsr (%0), %%ecx")
2013 :: "d" (NULL) );
2014 set_insn(blsr);
2015
2016 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_OF | X86_EFLAGS_ZF | \
2017 X86_EFLAGS_CF;
2018 rc = x86_emulate(&ctxt, &emulops);
2019 if ( (rc != X86EMUL_OKAY) || regs.ecx != 0xfedcba90 ||
2020 (regs.eflags & (EFLAGS_MASK & ~(X86_EFLAGS_AF | X86_EFLAGS_PF))) !=
2021 (EFLAGS_ALWAYS_SET | X86_EFLAGS_SF) ||
2022 !check_eip(blsr) )
2023 goto fail;
2024
2025 #ifdef __x86_64__
2026 /* Re-test with VEX.W set while emulating 32-bit mode. */
2027 ctxt.lma = 0;
2028 ctxt.addr_size = 32;
2029 ctxt.sp_size = 32;
2030
2031 memcpy(instr, blsr, blsr_end - blsr);
2032 instr[2] |= 0x80;
2033 regs.rip = (unsigned long)&instr[0];
2034 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_OF | X86_EFLAGS_ZF | \
2035 X86_EFLAGS_CF;
2036 rc = x86_emulate(&ctxt, &emulops);
2037 if ( (rc != X86EMUL_OKAY) || regs.ecx != 0xfedcba90 ||
2038 (regs.eflags & (EFLAGS_MASK & ~(X86_EFLAGS_AF | X86_EFLAGS_PF))) !=
2039 (EFLAGS_ALWAYS_SET | X86_EFLAGS_SF) ||
2040 (regs.rip != (unsigned long)&instr[blsr_end - blsr]) )
2041 goto fail;
2042
2043 ctxt.lma = 1;
2044 ctxt.addr_size = 64;
2045 ctxt.sp_size = 64;
2046 #endif
2047
2048 printf("okay\n");
2049 }
2050 else
2051 printf("skipped\n");
2052
2053 printf("%-40s", "Testing bzhi %edx,(%ecx),%ebx...");
2054 if ( stack_exec && cpu_has_bmi2 )
2055 {
2056 decl_insn(bzhi);
2057
2058 asm volatile ( put_insn(bzhi, "bzhi %%edx, (%0), %%ebx")
2059 :: "c" (NULL) );
2060 set_insn(bzhi);
2061
2062 regs.ecx = (unsigned long)res;
2063 regs.edx = 0xff13;
2064 regs.eflags = EFLAGS_ALWAYS_SET | X86_EFLAGS_OF | X86_EFLAGS_SF | \
2065 X86_EFLAGS_ZF | X86_EFLAGS_CF;
2066 rc = x86_emulate(&ctxt, &emulops);
2067 if ( (rc != X86EMUL_OKAY) || regs.ebx != (*res & 0x7ffff) ||
2068 regs.edx != 0xff13 || *res != 0xfedcba98 ||
2069 (regs.eflags & (EFLAGS_MASK & ~(X86_EFLAGS_AF | X86_EFLAGS_PF))) !=
2070 EFLAGS_ALWAYS_SET ||
2071 !check_eip(bzhi) )
2072 goto fail;
2073 printf("okay\n");
2074 }
2075 else
2076 printf("skipped\n");
2077
2078 printf("%-40s", "Testing mulx (%eax),%ecx,%ebx...");
2079 if ( cpu_has_bmi2 )
2080 {
2081 decl_insn(mulx);
2082
2083 asm volatile ( put_insn(mulx, "mulx (%0), %%ecx, %%ebx")
2084 :: "a" (NULL) );
2085 set_insn(mulx);
2086
2087 regs.eax = (unsigned long)res;
2088 regs.edx = 0x12345678;
2089 regs.eflags = 0xac3;
2090 rc = x86_emulate(&ctxt, &emulops);
2091 if ( (rc != X86EMUL_OKAY) || regs.ebx != 0x121fa00a ||
2092 regs.ecx != 0x35068740 || *res != 0xfedcba98 ||
2093 regs.eflags != 0xac3 || !check_eip(mulx) )
2094 goto fail;
2095 printf("okay\n");
2096 }
2097 else
2098 printf("skipped\n");
2099
2100 printf("%-40s", "Testing pdep (%edx),%ecx,%ebx...");
2101 if ( stack_exec && cpu_has_bmi2 )
2102 {
2103 decl_insn(pdep);
2104
2105 asm volatile ( put_insn(pdep, "pdep (%0), %%ecx, %%ebx")
2106 :: "d" (NULL) );
2107 set_insn(pdep);
2108
2109 regs.ecx = 0x8cef;
2110 regs.edx = (unsigned long)res;
2111 regs.eflags = 0xa43;
2112 rc = x86_emulate(&ctxt, &emulops);
2113 if ( (rc != X86EMUL_OKAY) || regs.ebx != 0x850b298 ||
2114 regs.ecx != 0x8cef || *res != 0xfedcba98 ||
2115 regs.eflags != 0xa43 || !check_eip(pdep) )
2116 goto fail;
2117 printf("okay\n");
2118 }
2119 else
2120 printf("skipped\n");
2121
2122 printf("%-40s", "Testing pext (%edx),%ecx,%ebx...");
2123 if ( stack_exec && cpu_has_bmi2 )
2124 {
2125 decl_insn(pext);
2126
2127 asm volatile ( put_insn(pext, "pext (%0), %%ecx, %%ebx")
2128 :: "d" (NULL) );
2129 set_insn(pext);
2130
2131 regs.ecx = 0x137f8cef;
2132 regs.edx = (unsigned long)res;
2133 regs.eflags = 0xa43;
2134 rc = x86_emulate(&ctxt, &emulops);
2135 if ( (rc != X86EMUL_OKAY) || regs.ebx != 0x12f95 ||
2136 regs.ecx != 0x137f8cef || *res != 0xfedcba98 ||
2137 regs.eflags != 0xa43 || !check_eip(pext) )
2138 goto fail;
2139 printf("okay\n");
2140 }
2141 else
2142 printf("skipped\n");
2143
2144 printf("%-40s", "Testing rorx $16,(%ecx),%ebx...");
2145 if ( cpu_has_bmi2 )
2146 {
2147 decl_insn(rorx);
2148
2149 asm volatile ( put_insn(rorx, "rorx $16, (%0), %%ebx")
2150 :: "c" (NULL) );
2151 set_insn(rorx);
2152
2153 regs.ecx = (unsigned long)res;
2154 regs.eflags = 0xa43;
2155 rc = x86_emulate(&ctxt, &emulops);
2156 if ( (rc != X86EMUL_OKAY) || regs.ebx != 0xba98fedc ||
2157 *res != 0xfedcba98 ||
2158 regs.eflags != 0xa43 || !check_eip(rorx) )
2159 goto fail;
2160 printf("okay\n");
2161 }
2162 else
2163 printf("skipped\n");
2164
2165 printf("%-40s", "Testing sarx %edx,(%ecx),%ebx...");
2166 if ( stack_exec && cpu_has_bmi2 )
2167 {
2168 decl_insn(sarx);
2169
2170 asm volatile ( put_insn(sarx, "sarx %%edx, (%0), %%ebx")
2171 :: "c" (NULL) );
2172 set_insn(sarx);
2173
2174 regs.ecx = (unsigned long)res;
2175 regs.edx = 0xff13;
2176 regs.eflags = 0xa43;
2177 rc = x86_emulate(&ctxt, &emulops);
2178 if ( (rc != X86EMUL_OKAY) ||
2179 regs.ebx != (unsigned)(((signed)*res >> (regs.edx & 0x1f))) ||
2180 regs.edx != 0xff13 || *res != 0xfedcba98 ||
2181 regs.eflags != 0xa43 || !check_eip(sarx) )
2182 goto fail;
2183 printf("okay\n");
2184 }
2185 else
2186 printf("skipped\n");
2187
2188 printf("%-40s", "Testing shlx %edx,(%ecx),%ebx...");
2189 if ( stack_exec && cpu_has_bmi2 )
2190 {
2191 decl_insn(shlx);
2192
2193 asm volatile ( put_insn(shlx, "shlx %%edx, (%0), %%ebx")
2194 :: "c" (NULL) );
2195 set_insn(shlx);
2196
2197 rc = x86_emulate(&ctxt, &emulops);
2198 if ( (rc != X86EMUL_OKAY) ||
2199 regs.ebx != (*res << (regs.edx & 0x1f)) ||
2200 regs.edx != 0xff13 || *res != 0xfedcba98 ||
2201 regs.eflags != 0xa43 || !check_eip(shlx) )
2202 goto fail;
2203 printf("okay\n");
2204 }
2205 else
2206 printf("skipped\n");
2207
2208 printf("%-40s", "Testing shrx %edx,(%ecx),%ebx...");
2209 if ( stack_exec && cpu_has_bmi2 )
2210 {
2211 decl_insn(shrx);
2212
2213 asm volatile ( put_insn(shrx, "shrx %%edx, (%0), %%ebx")
2214 :: "c" (NULL) );
2215 set_insn(shrx);
2216
2217 rc = x86_emulate(&ctxt, &emulops);
2218 if ( (rc != X86EMUL_OKAY) ||
2219 regs.ebx != (*res >> (regs.edx & 0x1f)) ||
2220 regs.edx != 0xff13 || *res != 0xfedcba98 ||
2221 regs.eflags != 0xa43 || !check_eip(shrx) )
2222 goto fail;
2223 printf("okay\n");
2224 }
2225 else
2226 printf("skipped\n");
2227
2228 printf("%-40s", "Testing adcx/adox ...");
2229 {
2230 static const unsigned int data[] = {
2231 0x01234567, 0x12345678, 0x23456789, 0x3456789a,
2232 0x456789ab, 0x56789abc, 0x6789abcd, 0x789abcde,
2233 0x89abcdef, 0x9abcdef0, 0xabcdef01, 0xbcdef012,
2234 0xcdef0123, 0xdef01234, 0xef012345, 0xf0123456
2235 };
2236 decl_insn(adx);
2237 unsigned int cf, of;
2238
2239 asm volatile ( put_insn(adx, ".Lloop%=:\n\t"
2240 "adcx (%[addr]), %k[dst1]\n\t"
2241 "adox -%c[full]-%c[elem](%[addr],%[cnt],2*%c[elem]), %k[dst2]\n\t"
2242 "lea %c[elem](%[addr]),%[addr]\n\t"
2243 "loop .Lloop%=\n\t"
2244 "adcx %k[cnt], %k[dst1]\n\t"
2245 "adox %k[cnt], %k[dst2]\n\t" )
2246 : [addr] "=S" (regs.esi), [cnt] "=c" (regs.ecx),
2247 [dst1] "=a" (regs.eax), [dst2] "=d" (regs.edx)
2248 : [full] "i" (sizeof(data)), [elem] "i" (sizeof(*data)),
2249 "[addr]" (data), "[cnt]" (ARRAY_SIZE(data)),
2250 "[dst1]" (0), "[dst2]" (0) );
2251
2252 set_insn(adx);
2253 regs.eflags = 0x2d6;
2254 of = cf = i = 0;
2255 while ( (rc = x86_emulate(&ctxt, &emulops)) == X86EMUL_OKAY )
2256 {
2257 ++i;
2258 /*
2259 * Count CF/OF being set after each loop iteration during the
2260 * first half (to observe different counts), in order to catch
2261 * the wrong flag being fiddled with.
2262 */
2263 if ( i < ARRAY_SIZE(data) * 2 && !(i % 4) )
2264 {
2265 if ( regs.eflags & 0x001 )
2266 ++cf;
2267 if ( regs.eflags & 0x800 )
2268 ++of;
2269 }
2270 if ( !valid_eip(adx) )
2271 break;
2272 }
2273 if ( (rc != X86EMUL_OKAY) ||
2274 i != ARRAY_SIZE(data) * 4 + 2 || cf != 1 || of != 5 ||
2275 regs.eax != 0xffffffff || regs.ecx || regs.edx != 0xffffffff ||
2276 !check_eip(adx) || regs.eflags != 0x2d6 )
2277 goto fail;
2278 printf("okay\n");
2279 }
2280
2281 printf("%-40s", "Testing bextr $0x0a03,(%ecx),%ebx...");
2282 if ( stack_exec && cpu_has_tbm )
2283 {
2284 decl_insn(bextr_imm);
2285 #ifdef __x86_64__
2286 decl_insn(bextr64_imm);
2287 #endif
2288
2289 asm volatile ( put_insn(bextr_imm, "bextr $0x0a03, (%0), %%ebx")
2290 :: "c" (NULL) );
2291 set_insn(bextr_imm);
2292
2293 *res = 0xfedcba98;
2294 regs.ecx = (unsigned long)res;
2295 regs.eflags = 0xa43;
2296 rc = x86_emulate(&ctxt, &emulops);
2297 if ( (rc != X86EMUL_OKAY) || regs.ebx != ((*res >> 3) & 0x3ff) ||
2298 *res != 0xfedcba98 ||
2299 (regs.eflags & 0xf6b) != 0x202 || !check_eip(bextr_imm) )
2300 goto fail;
2301 printf("okay\n");
2302 #ifdef __x86_64__
2303 printf("%-40s", "Testing bextr $0x211e,(%r10),%r11...");
2304
2305 asm volatile ( put_insn(bextr64_imm, "bextr $0x211e, (%r10), %r11") );
2306 set_insn(bextr64_imm);
2307
2308 res[0] = 0x76543210;
2309 res[1] = 0xfedcba98;
2310 regs.r10 = (unsigned long)res;
2311 regs.eflags = 0xa43;
2312 rc = x86_emulate(&ctxt, &emulops);
2313 if ( (rc != X86EMUL_OKAY) ||
2314 regs.r11 != (((unsigned long)(res[1] << 1) << 1) |
2315 (res[0] >> 30)) ||
2316 res[0] != 0x76543210 || res[1] != 0xfedcba98 ||
2317 (regs.eflags & 0xf6b) != 0x202 || !check_eip(bextr64_imm) )
2318 goto fail;
2319 printf("okay\n");
2320 #endif
2321 }
2322 else
2323 printf("skipped\n");
2324
2325 res[0] = 0xfedcba98;
2326 res[1] = 0x01234567;
2327 regs.edx = (unsigned long)res;
2328
2329 printf("%-40s", "Testing blcfill 4(%edx),%ecx...");
2330 if ( stack_exec && cpu_has_tbm )
2331 {
2332 decl_insn(blcfill);
2333
2334 asm volatile ( put_insn(blcfill, "blcfill 4(%0), %%ecx")
2335 :: "d" (NULL) );
2336 set_insn(blcfill);
2337
2338 regs.eflags = 0xac3;
2339 rc = x86_emulate(&ctxt, &emulops);
2340 if ( (rc != X86EMUL_OKAY) || res[1] != 0x01234567 ||
2341 regs.ecx != ((res[1] + 1) & res[1]) ||
2342 (regs.eflags & 0xfeb) != 0x202 || !check_eip(blcfill) )
2343 goto fail;
2344 printf("okay\n");
2345 }
2346 else
2347 printf("skipped\n");
2348
2349 printf("%-40s", "Testing blci 4(%edx),%ecx...");
2350 if ( stack_exec && cpu_has_tbm )
2351 {
2352 decl_insn(blci);
2353
2354 asm volatile ( put_insn(blci, "blci 4(%0), %%ecx")
2355 :: "d" (NULL) );
2356 set_insn(blci);
2357
2358 regs.eflags = 0xac3;
2359 rc = x86_emulate(&ctxt, &emulops);
2360 if ( (rc != X86EMUL_OKAY) || res[1] != 0x01234567 ||
2361 regs.ecx != (~(res[1] + 1) | res[1]) ||
2362 (regs.eflags & 0xfeb) != 0x282 || !check_eip(blci) )
2363 goto fail;
2364 printf("okay\n");
2365 }
2366 else
2367 printf("skipped\n");
2368
2369 printf("%-40s", "Testing blcic 4(%edx),%ecx...");
2370 if ( stack_exec && cpu_has_tbm )
2371 {
2372 decl_insn(blcic);
2373
2374 asm volatile ( put_insn(blcic, "blcic 4(%0), %%ecx")
2375 :: "d" (NULL) );
2376 set_insn(blcic);
2377
2378 regs.eflags = 0xac3;
2379 rc = x86_emulate(&ctxt, &emulops);
2380 if ( (rc != X86EMUL_OKAY) || res[1] != 0x01234567 ||
2381 regs.ecx != ((res[1] + 1) & ~res[1]) ||
2382 (regs.eflags & 0xfeb) != 0x202 || !check_eip(blcic) )
2383 goto fail;
2384 printf("okay\n");
2385 }
2386 else
2387 printf("skipped\n");
2388
2389 printf("%-40s", "Testing blcmsk 4(%edx),%ecx...");
2390 if ( stack_exec && cpu_has_tbm )
2391 {
2392 decl_insn(blcmsk);
2393
2394 asm volatile ( put_insn(blcmsk, "blcmsk 4(%0), %%ecx")
2395 :: "d" (NULL) );
2396 set_insn(blcmsk);
2397
2398 regs.eflags = 0xac3;
2399 rc = x86_emulate(&ctxt, &emulops);
2400 if ( (rc != X86EMUL_OKAY) || res[1] != 0x01234567 ||
2401 regs.ecx != ((res[1] + 1) ^ res[1]) ||
2402 (regs.eflags & 0xfeb) != 0x202 || !check_eip(blcmsk) )
2403 goto fail;
2404 printf("okay\n");
2405 }
2406 else
2407 printf("skipped\n");
2408
2409 printf("%-40s", "Testing blcs 4(%edx),%ecx...");
2410 if ( stack_exec && cpu_has_tbm )
2411 {
2412 decl_insn(blcs);
2413
2414 asm volatile ( put_insn(blcs, "blcs 4(%0), %%ecx")
2415 :: "d" (NULL) );
2416 set_insn(blcs);
2417
2418 regs.eflags = 0xac3;
2419 rc = x86_emulate(&ctxt, &emulops);
2420 if ( (rc != X86EMUL_OKAY) || res[1] != 0x01234567 ||
2421 regs.ecx != ((res[1] + 1) | res[1]) ||
2422 (regs.eflags & 0xfeb) != 0x202 || !check_eip(blcs) )
2423 goto fail;
2424 printf("okay\n");
2425 }
2426 else
2427 printf("skipped\n");
2428
2429 printf("%-40s", "Testing blsfill (%edx),%ecx...");
2430 if ( stack_exec && cpu_has_tbm )
2431 {
2432 decl_insn(blsfill);
2433
2434 asm volatile ( put_insn(blsfill, "blsfill (%0), %%ecx")
2435 :: "d" (NULL) );
2436 set_insn(blsfill);
2437
2438 regs.eflags = 0xac3;
2439 rc = x86_emulate(&ctxt, &emulops);
2440 if ( (rc != X86EMUL_OKAY) || res[0] != 0xfedcba98 ||
2441 regs.ecx != ((res[0] - 1) | res[0]) ||
2442 (regs.eflags & 0xfeb) != 0x282 || !check_eip(blsfill) )
2443 goto fail;
2444 printf("okay\n");
2445 }
2446 else
2447 printf("skipped\n");
2448
2449 printf("%-40s", "Testing blsic (%edx),%ecx...");
2450 if ( stack_exec && cpu_has_tbm )
2451 {
2452 decl_insn(blsic);
2453
2454 asm volatile ( put_insn(blsic, "blsic (%0), %%ecx")
2455 :: "d" (NULL) );
2456 set_insn(blsic);
2457
2458 regs.eflags = 0xac3;
2459 rc = x86_emulate(&ctxt, &emulops);
2460 if ( (rc != X86EMUL_OKAY) || res[0] != 0xfedcba98 ||
2461 regs.ecx != ((res[0] - 1) | ~res[0]) ||
2462 (regs.eflags & 0xfeb) != 0x282 || !check_eip(blsic) )
2463 goto fail;
2464 printf("okay\n");
2465 }
2466 else
2467 printf("skipped\n");
2468
2469 printf("%-40s", "Testing t1mskc 4(%edx),%ecx...");
2470 if ( stack_exec && cpu_has_tbm )
2471 {
2472 decl_insn(t1mskc);
2473
2474 asm volatile ( put_insn(t1mskc, "t1mskc 4(%0), %%ecx")
2475 :: "d" (NULL) );
2476 set_insn(t1mskc);
2477
2478 regs.eflags = 0xac3;
2479 rc = x86_emulate(&ctxt, &emulops);
2480 if ( (rc != X86EMUL_OKAY) || res[1] != 0x01234567 ||
2481 regs.ecx != ((res[1] + 1) | ~res[1]) ||
2482 (regs.eflags & 0xfeb) != 0x282 || !check_eip(t1mskc) )
2483 goto fail;
2484 printf("okay\n");
2485 }
2486 else
2487 printf("skipped\n");
2488
2489 printf("%-40s", "Testing tzmsk (%edx),%ecx...");
2490 if ( stack_exec && cpu_has_tbm )
2491 {
2492 decl_insn(tzmsk);
2493
2494 asm volatile ( put_insn(tzmsk, "tzmsk (%0), %%ecx")
2495 :: "d" (NULL) );
2496 set_insn(tzmsk);
2497
2498 regs.eflags = 0xac3;
2499 rc = x86_emulate(&ctxt, &emulops);
2500 if ( (rc != X86EMUL_OKAY) || res[0] != 0xfedcba98 ||
2501 regs.ecx != ((res[0] - 1) & ~res[0]) ||
2502 (regs.eflags & 0xfeb) != 0x202 || !check_eip(tzmsk) )
2503 goto fail;
2504 printf("okay\n");
2505 }
2506 else
2507 printf("skipped\n");
2508
2509 printf("%-40s", "Testing rdpid %ecx...");
2510 instr[0] = 0xF3; instr[1] = 0x0f; instr[2] = 0xC7; instr[3] = 0xf9;
2511 regs.eip = (unsigned long)&instr[0];
2512 rc = x86_emulate(&ctxt, &emulops);
2513 if ( (rc != X86EMUL_OKAY) ||
2514 (regs.ecx != TSC_AUX_VALUE) ||
2515 (regs.eip != (unsigned long)&instr[4]) )
2516 goto fail;
2517 printf("okay\n");
2518
2519 printf("%-40s", "Testing rdpkru / wrpkru...");
2520 instr[0] = 0x0f; instr[1] = 0x01;
2521 regs.ecx = 0;
2522 for ( i = 0, j = (uint32_t)-__LINE__; i < 3; ++i )
2523 {
2524 instr[2] = 0xee | (i & 1);
2525 regs.eax = i < 2 ? j : 0;
2526 regs.edx = i & 1 ? 0 : j;
2527 regs.eip = (unsigned long)&instr[0];
2528 rc = x86_emulate(&ctxt, &emulops);
2529 if ( (rc != X86EMUL_OKAY) ||
2530 (!(i & 1) && (regs.eax != (i ? j : 0) || regs.edx)) ||
2531 (regs.eip != (unsigned long)&instr[3]) )
2532 goto fail;
2533 }
2534 printf("okay\n");
2535
2536 printf("%-40s", "Testing movdiri %edx,(%ecx)...");
2537 if ( stack_exec && cpu_has_movdiri )
2538 {
2539 instr[0] = 0x0f; instr[1] = 0x38; instr[2] = 0xf9; instr[3] = 0x11;
2540
2541 regs.eip = (unsigned long)&instr[0];
2542 regs.ecx = (unsigned long)memset(res, -1, 16);
2543 regs.edx = 0x44332211;
2544
2545 rc = x86_emulate(&ctxt, &emulops);
2546 if ( (rc != X86EMUL_OKAY) ||
2547 (regs.eip != (unsigned long)&instr[4]) ||
2548 res[0] != 0x44332211 || ~res[1] )
2549 goto fail;
2550 printf("okay\n");
2551 }
2552 else
2553 printf("skipped\n");
2554
2555 printf("%-40s", "Testing movdir64b 144(%edx),%ecx...");
2556 if ( stack_exec && cpu_has_movdir64b )
2557 {
2558 instr[0] = 0x66; instr[1] = 0x0f; instr[2] = 0x38; instr[3] = 0xf8;
2559 instr[4] = 0x8a; instr[5] = 0x90; instr[8] = instr[7] = instr[6] = 0;
2560
2561 regs.eip = (unsigned long)&instr[0];
2562 for ( i = 0; i < 64; ++i )
2563 res[i] = i - 20;
2564 regs.edx = (unsigned long)res;
2565 regs.ecx = (unsigned long)(res + 16);
2566
2567 rc = x86_emulate(&ctxt, &emulops);
2568 if ( (rc != X86EMUL_OKAY) ||
2569 (regs.eip != (unsigned long)&instr[9]) ||
2570 res[15] != -5 || res[32] != 12 )
2571 goto fail;
2572 for ( i = 16; i < 32; ++i )
2573 if ( res[i] != i )
2574 goto fail;
2575 printf("okay\n");
2576 }
2577 else
2578 printf("skipped\n");
2579
2580 printf("%-40s", "Testing fnstenv 4(%ecx)...");
2581 if ( stack_exec && cpu_has_fpu )
2582 {
2583 const uint16_t three = 3;
2584
2585 asm volatile ( "fninit\n\t"
2586 "fld1\n\t"
2587 "fidivs %1\n\t"
2588 "fstenv %0"
2589 : "=m" (res[9]) : "m" (three) : "memory" );
2590 zap_fpsel(&res[9], true);
2591 instr[0] = 0xd9; instr[1] = 0x71; instr[2] = 0x04;
2592 regs.eip = (unsigned long)&instr[0];
2593 regs.ecx = (unsigned long)res;
2594 res[8] = 0xaa55aa55;
2595 rc = x86_emulate(&ctxt, &emulops);
2596 zap_fpsel(&res[1], true);
2597 if ( (rc != X86EMUL_OKAY) ||
2598 memcmp(res + 1, res + 9, 28) ||
2599 res[8] != 0xaa55aa55 ||
2600 (regs.eip != (unsigned long)&instr[3]) )
2601 goto fail;
2602 printf("okay\n");
2603 }
2604 else
2605 printf("skipped\n");
2606
2607 printf("%-40s", "Testing fldenv 8(%edx)...");
2608 if ( stack_exec && cpu_has_fpu )
2609 {
2610 asm volatile ( "fnstenv %0\n\t"
2611 "fninit"
2612 : "=m" (res[2]) :: "memory" );
2613 zap_fpsel(&res[2], true);
2614 instr[0] = 0xd9; instr[1] = 0x62; instr[2] = 0x08;
2615 regs.eip = (unsigned long)&instr[0];
2616 regs.edx = (unsigned long)res;
2617 rc = x86_emulate(&ctxt, &emulops);
2618 asm volatile ( "fnstenv %0" : "=m" (res[9]) :: "memory" );
2619 zap_fpsel(&res[9], true);
2620 if ( (rc != X86EMUL_OKAY) ||
2621 memcmp(res + 2, res + 9, 28) ||
2622 (regs.eip != (unsigned long)&instr[3]) )
2623 goto fail;
2624 printf("okay\n");
2625 }
2626 else
2627 printf("skipped\n");
2628
2629 printf("%-40s", "Testing 16-bit fnsave (%ecx)...");
2630 if ( stack_exec && cpu_has_fpu )
2631 {
2632 const uint16_t five = 5;
2633
2634 asm volatile ( "fninit\n\t"
2635 "fld1\n\t"
2636 "fidivs %1\n\t"
2637 "fsaves %0"
2638 : "=m" (res[25]) : "m" (five) : "memory" );
2639 zap_fpsel(&res[25], false);
2640 asm volatile ( "frstors %0" :: "m" (res[25]) : "memory" );
2641 instr[0] = 0x66; instr[1] = 0xdd; instr[2] = 0x31;
2642 regs.eip = (unsigned long)&instr[0];
2643 regs.ecx = (unsigned long)res;
2644 res[23] = 0xaa55aa55;
2645 res[24] = 0xaa55aa55;
2646 rc = x86_emulate(&ctxt, &emulops);
2647 zap_fpsel(&res[0], false);
2648 if ( (rc != X86EMUL_OKAY) ||
2649 memcmp(res, res + 25, 94) ||
2650 (res[23] >> 16) != 0xaa55 ||
2651 res[24] != 0xaa55aa55 ||
2652 (regs.eip != (unsigned long)&instr[3]) )
2653 goto fail;
2654 printf("okay\n");
2655 }
2656 else
2657 printf("skipped\n");
2658
2659 printf("%-40s", "Testing frstor (%edx)...");
2660 if ( stack_exec && cpu_has_fpu )
2661 {
2662 const uint16_t seven = 7;
2663
2664 asm volatile ( "fninit\n\t"
2665 "fld1\n\t"
2666 "fidivs %1\n\t"
2667 "fnsave %0\n\t"
2668 : "=&m" (res[0]) : "m" (seven) : "memory" );
2669 zap_fpsel(&res[0], true);
2670 instr[0] = 0xdd; instr[1] = 0x22;
2671 regs.eip = (unsigned long)&instr[0];
2672 regs.edx = (unsigned long)res;
2673 rc = x86_emulate(&ctxt, &emulops);
2674 asm volatile ( "fnsave %0" : "=m" (res[27]) :: "memory" );
2675 zap_fpsel(&res[27], true);
2676 if ( (rc != X86EMUL_OKAY) ||
2677 memcmp(res, res + 27, 108) ||
2678 (regs.eip != (unsigned long)&instr[2]) )
2679 goto fail;
2680 printf("okay\n");
2681 }
2682 else
2683 printf("skipped\n");
2684
2685 printf("%-40s", "Testing fxsave 4(%ecx)...");
2686 if ( stack_exec && cpu_has_fxsr )
2687 {
2688 const uint16_t nine = 9;
2689
2690 memset(res + 0x80, 0xcc, 0x400);
2691 if ( cpu_has_sse2 )
2692 asm volatile ( "pcmpeqd %xmm7, %xmm7\n\t"
2693 "pxor %xmm6, %xmm6\n\t"
2694 "psubw %xmm7, %xmm6" );
2695 asm volatile ( "fninit\n\t"
2696 "fld1\n\t"
2697 "fidivs %1\n\t"
2698 "fxsave %0"
2699 : "=m" (res[0x100]) : "m" (nine) : "memory" );
2700 zap_xfpsel(&res[0x100]);
2701 instr[0] = 0x0f; instr[1] = 0xae; instr[2] = 0x41; instr[3] = 0x04;
2702 regs.eip = (unsigned long)&instr[0];
2703 regs.ecx = (unsigned long)(res + 0x7f);
2704 memset(res + 0x100 + 0x74, 0x33, 0x30);
2705 memset(res + 0x80 + 0x74, 0x33, 0x30);
2706 rc = x86_emulate(&ctxt, &emulops);
2707 zap_xfpsel(&res[0x80]);
2708 if ( (rc != X86EMUL_OKAY) ||
2709 memcmp(res + 0x80, res + 0x100, 0x200) ||
2710 (regs.eip != (unsigned long)&instr[4]) )
2711 goto fail;
2712 printf("okay\n");
2713 }
2714 else
2715 printf("skipped\n");
2716
2717 printf("%-40s", "Testing fxrstor -4(%ecx)...");
2718 if ( stack_exec && cpu_has_fxsr )
2719 {
2720 const uint16_t eleven = 11;
2721
2722 memset(res + 0x80, 0xcc, 0x400);
2723 asm volatile ( "fxsave %0" : "=m" (res[0x80]) :: "memory" );
2724 zap_xfpsel(&res[0x80]);
2725 if ( cpu_has_sse2 )
2726 asm volatile ( "pxor %xmm7, %xmm6\n\t"
2727 "pxor %xmm7, %xmm3\n\t"
2728 "pxor %xmm7, %xmm0\n\t"
2729 "pxor %xmm7, %xmm7" );
2730 asm volatile ( "fninit\n\t"
2731 "fld1\n\t"
2732 "fidivs %0\n\t"
2733 :: "m" (eleven) );
2734 instr[0] = 0x0f; instr[1] = 0xae; instr[2] = 0x49; instr[3] = 0xfc;
2735 regs.eip = (unsigned long)&instr[0];
2736 regs.ecx = (unsigned long)(res + 0x81);
2737 rc = x86_emulate(&ctxt, &emulops);
2738 asm volatile ( "fxsave %0" : "=m" (res[0x100]) :: "memory" );
2739 zap_xfpsel(&res[0x100]);
2740 if ( (rc != X86EMUL_OKAY) ||
2741 memcmp(res + 0x100, res + 0x80, 0x200) ||
2742 (regs.eip != (unsigned long)&instr[4]) )
2743 goto fail;
2744 printf("okay\n");
2745 }
2746 else
2747 printf("skipped\n");
2748
2749 #ifdef __x86_64__
2750 printf("%-40s", "Testing fxsaveq 8(%edx)...");
2751 if ( stack_exec && cpu_has_fxsr )
2752 {
2753 memset(res + 0x80, 0xcc, 0x400);
2754 asm volatile ( "fxsaveq %0" : "=m" (res[0x100]) :: "memory" );
2755 instr[0] = 0x48; instr[1] = 0x0f; instr[2] = 0xae; instr[3] = 0x42; instr[4] = 0x08;
2756 regs.eip = (unsigned long)&instr[0];
2757 regs.edx = (unsigned long)(res + 0x7e);
2758 memset(res + 0x100 + 0x74, 0x33, 0x30);
2759 memset(res + 0x80 + 0x74, 0x33, 0x30);
2760 rc = x86_emulate(&ctxt, &emulops);
2761 if ( (rc != X86EMUL_OKAY) ||
2762 memcmp(res + 0x80, res + 0x100, 0x200) ||
2763 (regs.eip != (unsigned long)&instr[5]) )
2764 goto fail;
2765 printf("okay\n");
2766 }
2767 else
2768 printf("skipped\n");
2769 #endif
2770
2771 printf("%-40s", "Testing movq %mm3,(%ecx)...");
2772 if ( stack_exec && cpu_has_mmx )
2773 {
2774 decl_insn(movq_to_mem);
2775
2776 asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
2777 put_insn(movq_to_mem, "movq %%mm3, (%0)")
2778 :: "c" (NULL) );
2779
2780 set_insn(movq_to_mem);
2781 memset(res, 0x33, 64);
2782 memset(res + 8, 0xff, 8);
2783 regs.ecx = (unsigned long)res;
2784 rc = x86_emulate(&ctxt, &emulops);
2785 if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
2786 !check_eip(movq_to_mem) )
2787 goto fail;
2788 printf("okay\n");
2789 }
2790 else
2791 printf("skipped\n");
2792
2793 printf("%-40s", "Testing movq (%edx),%mm5...");
2794 if ( stack_exec && cpu_has_mmx )
2795 {
2796 decl_insn(movq_from_mem);
2797
2798 asm volatile ( "pcmpgtb %%mm5, %%mm5\n"
2799 put_insn(movq_from_mem, "movq (%0), %%mm5")
2800 :: "d" (NULL) );
2801
2802 set_insn(movq_from_mem);
2803 regs.ecx = 0;
2804 regs.edx = (unsigned long)res;
2805 rc = x86_emulate(&ctxt, &emulops);
2806 if ( rc != X86EMUL_OKAY || !check_eip(movq_from_mem) )
2807 goto fail;
2808 asm ( "pcmpeqb %%mm3, %%mm3\n\t"
2809 "pcmpeqb %%mm5, %%mm3\n\t"
2810 "pmovmskb %%mm3, %0" : "=r" (rc) );
2811 if ( rc != 0xff )
2812 goto fail;
2813 printf("okay\n");
2814 }
2815 else
2816 printf("skipped\n");
2817
2818 printf("%-40s", "Testing movq %xmm0,32(%ecx)...");
2819 if ( stack_exec && cpu_has_sse2 )
2820 {
2821 decl_insn(movq_to_mem2);
2822
2823 asm volatile ( "pcmpgtb %%xmm0, %%xmm0\n"
2824 put_insn(movq_to_mem2, "movq %%xmm0, 32(%0)")
2825 :: "c" (NULL) );
2826
2827 memset(res, 0xbd, 64);
2828 set_insn(movq_to_mem2);
2829 regs.ecx = (unsigned long)res;
2830 regs.edx = 0;
2831 rc = x86_emulate(&ctxt, &emulops);
2832 if ( rc != X86EMUL_OKAY || !check_eip(movq_to_mem2) ||
2833 *((uint64_t *)res + 4) ||
2834 memcmp(res, res + 10, 24) ||
2835 memcmp(res, res + 6, 8) )
2836 goto fail;
2837 printf("okay\n");
2838 }
2839 else
2840 printf("skipped\n");
2841
2842 printf("%-40s", "Testing movq 32(%ecx),%xmm1...");
2843 if ( stack_exec && cpu_has_sse2 )
2844 {
2845 decl_insn(movq_from_mem2);
2846
2847 asm volatile ( "pcmpeqb %%xmm1, %%xmm1\n"
2848 put_insn(movq_from_mem2, "movq 32(%0), %%xmm1")
2849 :: "c" (NULL) );
2850
2851 set_insn(movq_from_mem2);
2852 rc = x86_emulate(&ctxt, &emulops);
2853 if ( rc != X86EMUL_OKAY || !check_eip(movq_from_mem2) )
2854 goto fail;
2855 asm ( "pcmpgtb %%xmm0, %%xmm0\n\t"
2856 "pcmpeqb %%xmm1, %%xmm0\n\t"
2857 "pmovmskb %%xmm0, %0" : "=r" (rc) );
2858 if ( rc != 0xffff )
2859 goto fail;
2860 printf("okay\n");
2861 }
2862 else
2863 printf("skipped\n");
2864
2865 printf("%-40s", "Testing vmovq %xmm1,32(%edx)...");
2866 if ( stack_exec && cpu_has_avx )
2867 {
2868 decl_insn(vmovq_to_mem);
2869
2870 asm volatile ( "pcmpgtb %%xmm1, %%xmm1\n"
2871 put_insn(vmovq_to_mem, "vmovq %%xmm1, 32(%0)")
2872 :: "d" (NULL) );
2873
2874 memset(res, 0xdb, 64);
2875 set_insn(vmovq_to_mem);
2876 regs.ecx = 0;
2877 regs.edx = (unsigned long)res;
2878 rc = x86_emulate(&ctxt, &emulops);
2879 if ( rc != X86EMUL_OKAY || !check_eip(vmovq_to_mem) ||
2880 *((uint64_t *)res + 4) ||
2881 memcmp(res, res + 10, 24) ||
2882 memcmp(res, res + 6, 8) )
2883 goto fail;
2884 printf("okay\n");
2885 }
2886 else
2887 printf("skipped\n");
2888
2889 printf("%-40s", "Testing vmovq 32(%edx),%xmm0...");
2890 if ( stack_exec && cpu_has_avx )
2891 {
2892 decl_insn(vmovq_from_mem);
2893
2894 asm volatile ( "pcmpeqb %%xmm0, %%xmm0\n"
2895 put_insn(vmovq_from_mem, "vmovq 32(%0), %%xmm0")
2896 :: "d" (NULL) );
2897
2898 set_insn(vmovq_from_mem);
2899 rc = x86_emulate(&ctxt, &emulops);
2900 if ( rc != X86EMUL_OKAY || !check_eip(vmovq_from_mem) )
2901 goto fail;
2902 asm ( "pcmpgtb %%xmm1, %%xmm1\n\t"
2903 "pcmpeqb %%xmm0, %%xmm1\n\t"
2904 "pmovmskb %%xmm1, %0" : "=r" (rc) );
2905 if ( rc != 0xffff )
2906 goto fail;
2907 printf("okay\n");
2908 }
2909 else
2910 printf("skipped\n");
2911
2912 printf("%-40s", "Testing {evex} vmovq %xmm1,32(%edx)...");
2913 if ( stack_exec && cpu_has_avx512f )
2914 {
2915 decl_insn(evex_vmovq_to_mem);
2916
2917 asm volatile ( "pcmpgtb %%xmm1, %%xmm1\n"
2918 put_insn(evex_vmovq_to_mem, "%{evex%} vmovq %%xmm1, 32(%0)")
2919 :: "d" (NULL) );
2920
2921 memset(res, 0xdb, 64);
2922 set_insn(evex_vmovq_to_mem);
2923 regs.ecx = 0;
2924 regs.edx = (unsigned long)res;
2925 rc = x86_emulate(&ctxt, &emulops);
2926 if ( rc != X86EMUL_OKAY || !check_eip(evex_vmovq_to_mem) ||
2927 *((uint64_t *)res + 4) ||
2928 memcmp(res, res + 10, 24) ||
2929 memcmp(res, res + 6, 8) )
2930 goto fail;
2931 printf("okay\n");
2932 }
2933 else
2934 printf("skipped\n");
2935
2936 printf("%-40s", "Testing {evex} vmovq 32(%edx),%xmm0...");
2937 if ( stack_exec && cpu_has_avx512f )
2938 {
2939 decl_insn(evex_vmovq_from_mem);
2940
2941 asm volatile ( "pcmpeqb %%xmm0, %%xmm0\n"
2942 put_insn(evex_vmovq_from_mem, "%{evex%} vmovq 32(%0), %%xmm0")
2943 :: "d" (NULL) );
2944
2945 set_insn(evex_vmovq_from_mem);
2946 rc = x86_emulate(&ctxt, &emulops);
2947 if ( rc != X86EMUL_OKAY || !check_eip(evex_vmovq_from_mem) )
2948 goto fail;
2949 asm ( "vmovq %1, %%xmm1\n\t"
2950 "vpcmpeqq %%zmm0, %%zmm1, %%k0\n"
2951 "kmovw %%k0, %0" : "=r" (rc) : "m" (res[8]) );
2952 if ( rc != 0xff )
2953 goto fail;
2954 printf("okay\n");
2955 }
2956 else
2957 printf("skipped\n");
2958
2959 printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
2960 if ( stack_exec && cpu_has_sse2 )
2961 {
2962 decl_insn(movdqu_to_mem);
2963
2964 asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
2965 put_insn(movdqu_to_mem, "movdqu %%xmm2, (%0)")
2966 :: "c" (NULL) );
2967
2968 set_insn(movdqu_to_mem);
2969 memset(res, 0x55, 64);
2970 memset(res + 8, 0xff, 16);
2971 regs.ecx = (unsigned long)res;
2972 rc = x86_emulate(&ctxt, &emulops);
2973 if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
2974 !check_eip(movdqu_to_mem) )
2975 goto fail;
2976 printf("okay\n");
2977 }
2978 else
2979 printf("skipped\n");
2980
2981 printf("%-40s", "Testing movdqu (%edx),%xmm4...");
2982 if ( stack_exec && cpu_has_sse2 )
2983 {
2984 decl_insn(movdqu_from_mem);
2985
2986 asm volatile ( "pcmpgtb %%xmm4, %%xmm4\n"
2987 put_insn(movdqu_from_mem, "movdqu (%0), %%xmm4")
2988 :: "d" (NULL) );
2989
2990 set_insn(movdqu_from_mem);
2991 regs.ecx = 0;
2992 regs.edx = (unsigned long)res;
2993 rc = x86_emulate(&ctxt, &emulops);
2994 if ( rc != X86EMUL_OKAY || !check_eip(movdqu_from_mem) )
2995 goto fail;
2996 asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
2997 "pcmpeqb %%xmm4, %%xmm2\n\t"
2998 "pmovmskb %%xmm2, %0" : "=r" (rc) );
2999 if ( rc != 0xffff )
3000 goto fail;
3001 printf("okay\n");
3002 }
3003 else
3004 printf("skipped\n");
3005
3006 printf("%-40s", "Testing vmovdqu %ymm2,(%ecx)...");
3007 if ( stack_exec && cpu_has_avx )
3008 {
3009 decl_insn(vmovdqu_to_mem);
3010
3011 asm volatile ( "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n"
3012 put_insn(vmovdqu_to_mem, "vmovdqu %%ymm2, (%0)")
3013 :: "c" (NULL) );
3014
3015 set_insn(vmovdqu_to_mem);
3016 memset(res, 0x55, 128);
3017 memset(res + 16, 0xff, 16);
3018 memset(res + 20, 0x00, 16);
3019 regs.ecx = (unsigned long)res;
3020 rc = x86_emulate(&ctxt, &emulops);
3021 if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
3022 !check_eip(vmovdqu_to_mem) )
3023 goto fail;
3024 printf("okay\n");
3025 }
3026 else
3027 printf("skipped\n");
3028
3029 printf("%-40s", "Testing vmovdqu (%edx),%ymm4...");
3030 if ( stack_exec && cpu_has_avx )
3031 {
3032 decl_insn(vmovdqu_from_mem);
3033
3034 asm volatile ( "vpxor %%xmm4, %%xmm4, %%xmm4\n"
3035 put_insn(vmovdqu_from_mem, "vmovdqu (%0), %%ymm4")
3036 :: "d" (NULL) );
3037
3038 set_insn(vmovdqu_from_mem);
3039 memset(res + 4, 0xff, 16);
3040 regs.ecx = 0;
3041 regs.edx = (unsigned long)res;
3042 rc = x86_emulate(&ctxt, &emulops);
3043 if ( rc != X86EMUL_OKAY || !check_eip(vmovdqu_from_mem) )
3044 goto fail;
3045 asm ( "vextractf128 $1, %%ymm4, %%xmm3\n\t"
3046 "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n\t"
3047 "vpcmpeqb %%xmm4, %%xmm2, %%xmm0\n\t"
3048 "vpcmpeqb %%xmm3, %%xmm2, %%xmm1\n\t"
3049 "vpmovmskb %%xmm0, %0\n\t"
3050 "vpmovmskb %%xmm1, %1" : "=r" (rc), "=r" (i) );
3051 rc |= i << 16;
3052 if ( rc != 0xffffffff )
3053 goto fail;
3054 printf("okay\n");
3055 }
3056 else
3057 printf("skipped\n");
3058
3059 printf("%-40s", "Testing vmovdqu32 %zmm2,(%ecx){%k1}...");
3060 if ( stack_exec && cpu_has_avx512f )
3061 {
3062 decl_insn(vmovdqu32_to_mem);
3063
3064 memset(res, 0x55, 128);
3065
3066 asm volatile ( "vpcmpeqd %%ymm2, %%ymm2, %%ymm2\n\t"
3067 "kmovw %1,%%k1\n"
3068 put_insn(vmovdqu32_to_mem,
3069 "vmovdqu32 %%zmm2, (%0)%{%%k1%}")
3070 :: "c" (NULL), "rm" (res[0]) );
3071 set_insn(vmovdqu32_to_mem);
3072
3073 regs.ecx = (unsigned long)res;
3074 rc = x86_emulate(&ctxt, &emulops);
3075 if ( (rc != X86EMUL_OKAY) || memcmp(res + 16, res + 24, 32) ||
3076 !check_eip(vmovdqu32_to_mem) )
3077 goto fail;
3078
3079 res[16] = ~0; res[18] = ~0; res[20] = ~0; res[22] = ~0;
3080 res[24] = 0; res[26] = 0; res[28] = 0; res[30] = 0;
3081 if ( memcmp(res, res + 16, 64) )
3082 goto fail;
3083
3084 printf("okay\n");
3085 }
3086 else
3087 printf("skipped\n");
3088
3089 printf("%-40s", "Testing vmovdqu32 64(%edx),%zmm2{%k2}...");
3090 if ( stack_exec && cpu_has_avx512f )
3091 {
3092 decl_insn(vmovdqu32_from_mem);
3093
3094 asm volatile ( "knotw %%k1, %%k2\n"
3095 put_insn(vmovdqu32_from_mem,
3096 "vmovdqu32 64(%0), %%zmm2%{%%k2%}")
3097 :: "d" (NULL) );
3098
3099 set_insn(vmovdqu32_from_mem);
3100 regs.ecx = 0;
3101 regs.edx = (unsigned long)res;
3102 rc = x86_emulate(&ctxt, &emulops);
3103 if ( rc != X86EMUL_OKAY || !check_eip(vmovdqu32_from_mem) )
3104 goto fail;
3105 asm ( "vpcmpeqd %1, %%zmm2, %%k0\n\t"
3106 "kmovw %%k0, %0" : "=r" (rc) : "m" (res[0]) );
3107 if ( rc != 0xffff )
3108 goto fail;
3109 printf("okay\n");
3110 }
3111 else
3112 printf("skipped\n");
3113
3114 printf("%-40s", "Testing vmovdqu16 %zmm3,(%ecx){%k1}...");
3115 if ( stack_exec && cpu_has_avx512bw )
3116 {
3117 decl_insn(vmovdqu16_to_mem);
3118
3119 memset(res, 0x55, 128);
3120
3121 asm volatile ( "vpcmpeqw %%ymm3, %%ymm3, %%ymm3\n\t"
3122 "kmovd %1,%%k1\n"
3123 put_insn(vmovdqu16_to_mem,
3124 "vmovdqu16 %%zmm3, (%0)%{%%k1%}")
3125 :: "c" (NULL), "rm" (res[0]) );
3126 set_insn(vmovdqu16_to_mem);
3127
3128 regs.ecx = (unsigned long)res;
3129 rc = x86_emulate(&ctxt, &emulops);
3130 if ( (rc != X86EMUL_OKAY) || memcmp(res + 16, res + 24, 32) ||
3131 !check_eip(vmovdqu16_to_mem) )
3132 goto fail;
3133
3134 for ( i = 16; i < 24; ++i )
3135 res[i] |= 0x0000ffff;
3136 for ( ; i < 32; ++i )
3137 res[i] &= 0xffff0000;
3138 if ( memcmp(res, res + 16, 64) )
3139 goto fail;
3140
3141 printf("okay\n");
3142 }
3143 else
3144 printf("skipped\n");
3145
3146 printf("%-40s", "Testing vmovdqu16 64(%edx),%zmm3{%k2}...");
3147 if ( stack_exec && cpu_has_avx512bw )
3148 {
3149 decl_insn(vmovdqu16_from_mem);
3150
3151 asm volatile ( "knotd %%k1, %%k2\n"
3152 put_insn(vmovdqu16_from_mem,
3153 "vmovdqu16 64(%0), %%zmm3%{%%k2%}")
3154 :: "d" (NULL) );
3155
3156 set_insn(vmovdqu16_from_mem);
3157 regs.ecx = 0;
3158 regs.edx = (unsigned long)res;
3159 rc = x86_emulate(&ctxt, &emulops);
3160 if ( rc != X86EMUL_OKAY || !check_eip(vmovdqu16_from_mem) )
3161 goto fail;
3162 asm ( "vpcmpeqw %1, %%zmm3, %%k0\n\t"
3163 "kmovd %%k0, %0" : "=r" (rc) : "m" (res[0]) );
3164 if ( rc != 0xffffffff )
3165 goto fail;
3166 printf("okay\n");
3167 }
3168 else
3169 printf("skipped\n");
3170
3171 printf("%-40s", "Testing movsd %xmm5,(%ecx)...");
3172 memset(res, 0x77, 64);
3173 memset(res + 10, 0x66, 8);
3174 if ( stack_exec && cpu_has_sse2 )
3175 {
3176 decl_insn(movsd_to_mem);
3177
3178 asm volatile ( "movlpd %0, %%xmm5\n\t"
3179 "movhpd %0, %%xmm5\n"
3180 put_insn(movsd_to_mem, "movsd %%xmm5, (%1)")
3181 :: "m" (res[10]), "c" (NULL) );
3182
3183 set_insn(movsd_to_mem);
3184 regs.ecx = (unsigned long)(res + 2);
3185 regs.edx = 0;
3186 rc = x86_emulate(&ctxt, &emulops);
3187 if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
3188 !check_eip(movsd_to_mem) )
3189 goto fail;
3190 printf("okay\n");
3191 }
3192 else
3193 {
3194 printf("skipped\n");
3195 memset(res + 2, 0x66, 8);
3196 }
3197
3198 printf("%-40s", "Testing movaps (%edx),%xmm7...");
3199 if ( stack_exec && cpu_has_sse )
3200 {
3201 decl_insn(movaps_from_mem);
3202
3203 asm volatile ( "xorps %%xmm7, %%xmm7\n"
3204 put_insn(movaps_from_mem, "movaps (%0), %%xmm7")
3205 :: "d" (NULL) );
3206
3207 set_insn(movaps_from_mem);
3208 regs.ecx = 0;
3209 regs.edx = (unsigned long)res;
3210 rc = x86_emulate(&ctxt, &emulops);
3211 if ( rc != X86EMUL_OKAY || !check_eip(movaps_from_mem) )
3212 goto fail;
3213 asm ( "cmpeqps %1, %%xmm7\n\t"
3214 "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
3215 if ( rc != 0xf )
3216 goto fail;
3217 printf("okay\n");
3218 }
3219 else
3220 printf("skipped\n");
3221
3222 printf("%-40s", "Testing vmovsd %xmm5,(%ecx)...");
3223 memset(res, 0x88, 64);
3224 memset(res + 10, 0x77, 8);
3225 if ( stack_exec && cpu_has_avx )
3226 {
3227 decl_insn(vmovsd_to_mem);
3228
3229 asm volatile ( "vbroadcastsd %0, %%ymm5\n"
3230 put_insn(vmovsd_to_mem, "vmovsd %%xmm5, (%1)")
3231 :: "m" (res[10]), "c" (NULL) );
3232
3233 set_insn(vmovsd_to_mem);
3234 regs.ecx = (unsigned long)(res + 2);
3235 regs.edx = 0;
3236 rc = x86_emulate(&ctxt, &emulops);
3237 if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
3238 !check_eip(vmovsd_to_mem) )
3239 goto fail;
3240 printf("okay\n");
3241 }
3242 else
3243 {
3244 printf("skipped\n");
3245 memset(res + 2, 0x77, 8);
3246 }
3247
3248 printf("%-40s", "Testing vmovaps (%edx),%ymm7...");
3249 if ( stack_exec && cpu_has_avx )
3250 {
3251 decl_insn(vmovaps_from_mem);
3252
3253 asm volatile ( "vxorps %%ymm7, %%ymm7, %%ymm7\n"
3254 put_insn(vmovaps_from_mem, "vmovaps (%0), %%ymm7")
3255 :: "d" (NULL) );
3256
3257 set_insn(vmovaps_from_mem);
3258 regs.ecx = 0;
3259 regs.edx = (unsigned long)res;
3260 rc = x86_emulate(&ctxt, &emulops);
3261 if ( rc != X86EMUL_OKAY || !check_eip(vmovaps_from_mem) )
3262 goto fail;
3263 asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
3264 "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
3265 if ( rc != 0xff )
3266 goto fail;
3267 printf("okay\n");
3268 }
3269 else
3270 printf("skipped\n");
3271
3272 printf("%-40s", "Testing vmovsd %xmm5,16(%ecx){%k3}...");
3273 memset(res, 0x88, 128);
3274 memset(res + 20, 0x77, 8);
3275 if ( stack_exec && cpu_has_avx512f )
3276 {
3277 decl_insn(vmovsd_masked_to_mem);
3278
3279 asm volatile ( "vbroadcastsd %0, %%ymm5\n\t"
3280 "kxorw %%k3, %%k3, %%k3\n"
3281 put_insn(vmovsd_masked_to_mem,
3282 "vmovsd %%xmm5, 16(%1)%{%%k3%}")
3283 :: "m" (res[20]), "c" (NULL) );
3284
3285 set_insn(vmovsd_masked_to_mem);
3286 regs.ecx = 0;
3287 regs.edx = 0;
3288 rc = x86_emulate(&ctxt, &emulops);
3289 if ( (rc != X86EMUL_OKAY) || !check_eip(vmovsd_masked_to_mem) )
3290 goto fail;
3291
3292 asm volatile ( "kmovw %0, %%k3\n" :: "m" (res[20]) );
3293
3294 set_insn(vmovsd_masked_to_mem);
3295 regs.ecx = (unsigned long)res;
3296 rc = x86_emulate(&ctxt, &emulops);
3297 if ( (rc != X86EMUL_OKAY) || !check_eip(vmovsd_masked_to_mem) ||
3298 memcmp(res, res + 16, 64) )
3299 goto fail;
3300
3301 printf("okay\n");
3302 }
3303 else
3304 {
3305 printf("skipped\n");
3306 memset(res + 4, 0x77, 8);
3307 }
3308
3309 printf("%-40s", "Testing vmovaps (%edx),%zmm7{%k3}{z}...");
3310 if ( stack_exec && cpu_has_avx512f )
3311 {
3312 decl_insn(vmovaps_masked_from_mem);
3313
3314 asm volatile ( "vpcmpeqd %%xmm7, %%xmm7, %%xmm7\n\t"
3315 "vbroadcastss %%xmm7, %%zmm7\n"
3316 put_insn(vmovaps_masked_from_mem,
3317 "vmovaps (%0), %%zmm7%{%%k3%}%{z%}")
3318 :: "d" (NULL) );
3319
3320 set_insn(vmovaps_masked_from_mem);
3321 regs.edx = (unsigned long)res;
3322 rc = x86_emulate(&ctxt, &emulops);
3323 if ( rc != X86EMUL_OKAY || !check_eip(vmovaps_masked_from_mem) )
3324 goto fail;
3325 asm ( "vcmpeqps %1, %%zmm7, %%k0\n\t"
3326 "vxorps %%xmm0, %%xmm0, %%xmm0\n\t"
3327 "vcmpeqps %%zmm0, %%zmm7, %%k1\n\t"
3328 "kxorw %%k1, %%k0, %%k0\n\t"
3329 "kmovw %%k0, %0" : "=r" (rc) : "m" (res[16]) );
3330 if ( rc != 0xffff )
3331 goto fail;
3332 printf("okay\n");
3333 }
3334 else
3335 printf("skipped\n");
3336
3337 printf("%-40s", "Testing movd %mm3,32(%ecx)...");
3338 if ( stack_exec && cpu_has_mmx )
3339 {
3340 decl_insn(movd_to_mem);
3341
3342 asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
3343 put_insn(movd_to_mem, "movd %%mm3, 32(%0)")
3344 :: "c" (NULL) );
3345
3346 memset(res, 0xbd, 64);
3347 set_insn(movd_to_mem);
3348 regs.ecx = (unsigned long)res;
3349 regs.edx = 0;
3350 rc = x86_emulate(&ctxt, &emulops);
3351 if ( rc != X86EMUL_OKAY || !check_eip(movd_to_mem) ||
3352 res[8] + 1 ||
3353 memcmp(res, res + 9, 28) ||
3354 memcmp(res, res + 6, 8) )
3355 goto fail;
3356 printf("okay\n");
3357 }
3358 else
3359 printf("skipped\n");
3360
3361 printf("%-40s", "Testing movd 32(%ecx),%mm4...");
3362 if ( stack_exec && cpu_has_mmx )
3363 {
3364 decl_insn(movd_from_mem);
3365
3366 asm volatile ( "pcmpgtb %%mm4, %%mm4\n"
3367 put_insn(movd_from_mem, "movd 32(%0), %%mm4")
3368 :: "c" (NULL) );
3369
3370 set_insn(movd_from_mem);
3371 rc = x86_emulate(&ctxt, &emulops);
3372 if ( rc != X86EMUL_OKAY || !check_eip(movd_from_mem) )
3373 goto fail;
3374 asm ( "pxor %%mm2,%%mm2\n\t"
3375 "pcmpeqb %%mm4, %%mm2\n\t"
3376 "pmovmskb %%mm2, %0" : "=r" (rc) );
3377 if ( rc != 0xf0 )
3378 goto fail;
3379 asm ( "pcmpeqb %%mm4, %%mm3\n\t"
3380 "pmovmskb %%mm3, %0" : "=r" (rc) );
3381 if ( rc != 0x0f )
3382 goto fail;
3383 printf("okay\n");
3384 }
3385 else
3386 printf("skipped\n");
3387
3388 printf("%-40s", "Testing movd %xmm2,32(%edx)...");
3389 if ( stack_exec && cpu_has_sse2 )
3390 {
3391 decl_insn(movd_to_mem2);
3392
3393 asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
3394 put_insn(movd_to_mem2, "movd %%xmm2, 32(%0)")
3395 :: "d" (NULL) );
3396
3397 memset(res, 0xdb, 64);
3398 set_insn(movd_to_mem2);
3399 regs.ecx = 0;
3400 regs.edx = (unsigned long)res;
3401 rc = x86_emulate(&ctxt, &emulops);
3402 if ( rc != X86EMUL_OKAY || !check_eip(movd_to_mem2) ||
3403 res[8] + 1 ||
3404 memcmp(res, res + 9, 28) ||
3405 memcmp(res, res + 6, 8) )
3406 goto fail;
3407 printf("okay\n");
3408 }
3409 else
3410 printf("skipped\n");
3411
3412 printf("%-40s", "Testing movd 32(%edx),%xmm3...");
3413 if ( stack_exec && cpu_has_sse2 )
3414 {
3415 decl_insn(movd_from_mem2);
3416
3417 asm volatile ( "pcmpeqb %%xmm3, %%xmm3\n"
3418 put_insn(movd_from_mem2, "movd 32(%0), %%xmm3")
3419 :: "d" (NULL) );
3420
3421 set_insn(movd_from_mem2);
3422 rc = x86_emulate(&ctxt, &emulops);
3423 if ( rc != X86EMUL_OKAY || !check_eip(movd_from_mem2) )
3424 goto fail;
3425 asm ( "pxor %%xmm1,%%xmm1\n\t"
3426 "pcmpeqb %%xmm3, %%xmm1\n\t"
3427 "pmovmskb %%xmm1, %0" : "=r" (rc) );
3428 if ( rc != 0xfff0 )
3429 goto fail;
3430 asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
3431 "pcmpeqb %%xmm3, %%xmm2\n\t"
3432 "pmovmskb %%xmm2, %0" : "=r" (rc) );
3433 if ( rc != 0x000f )
3434 goto fail;
3435 printf("okay\n");
3436 }
3437 else
3438 printf("skipped\n");
3439
3440 printf("%-40s", "Testing vmovd %xmm1,32(%ecx)...");
3441 if ( stack_exec && cpu_has_avx )
3442 {
3443 decl_insn(vmovd_to_mem);
3444
3445 asm volatile ( "pcmpeqb %%xmm1, %%xmm1\n"
3446 put_insn(vmovd_to_mem, "vmovd %%xmm1, 32(%0)")
3447 :: "c" (NULL) );
3448
3449 memset(res, 0xbd, 64);
3450 set_insn(vmovd_to_mem);
3451 regs.ecx = (unsigned long)res;
3452 regs.edx = 0;
3453 rc = x86_emulate(&ctxt, &emulops);
3454 if ( rc != X86EMUL_OKAY || !check_eip(vmovd_to_mem) ||
3455 res[8] + 1 ||
3456 memcmp(res, res + 9, 28) ||
3457 memcmp(res, res + 6, 8) )
3458 goto fail;
3459 printf("okay\n");
3460 }
3461 else
3462 printf("skipped\n");
3463
3464 printf("%-40s", "Testing vmovd 32(%ecx),%xmm2...");
3465 if ( stack_exec && cpu_has_avx )
3466 {
3467 decl_insn(vmovd_from_mem);
3468
3469 asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
3470 put_insn(vmovd_from_mem, "vmovd 32(%0), %%xmm2")
3471 :: "c" (NULL) );
3472
3473 set_insn(vmovd_from_mem);
3474 rc = x86_emulate(&ctxt, &emulops);
3475 if ( rc != X86EMUL_OKAY || !check_eip(vmovd_from_mem) )
3476 goto fail;
3477 asm ( "pxor %%xmm0,%%xmm0\n\t"
3478 "pcmpeqb %%xmm2, %%xmm0\n\t"
3479 "pmovmskb %%xmm0, %0" : "=r" (rc) );
3480 if ( rc != 0xfff0 )
3481 goto fail;
3482 asm ( "pcmpeqb %%xmm1, %%xmm1\n\t"
3483 "pcmpeqb %%xmm2, %%xmm1\n\t"
3484 "pmovmskb %%xmm1, %0" : "=r" (rc) );
3485 if ( rc != 0x000f )
3486 goto fail;
3487 printf("okay\n");
3488 }
3489 else
3490 printf("skipped\n");
3491
3492 printf("%-40s", "Testing {evex} vmovd %xmm3,32(%ecx)...");
3493 if ( stack_exec && cpu_has_avx512f )
3494 {
3495 decl_insn(evex_vmovd_to_mem);
3496
3497 asm volatile ( "pcmpeqb %%xmm3, %%xmm3\n"
3498 put_insn(evex_vmovd_to_mem,
3499 "%{evex%} vmovd %%xmm3, 32(%0)")
3500 :: "c" (NULL) );
3501
3502 memset(res, 0xbd, 64);
3503 set_insn(evex_vmovd_to_mem);
3504 regs.ecx = (unsigned long)res;
3505 regs.edx = 0;
3506 rc = x86_emulate(&ctxt, &emulops);
3507 if ( rc != X86EMUL_OKAY || !check_eip(evex_vmovd_to_mem) ||
3508 res[8] + 1 ||
3509 memcmp(res, res + 9, 28) ||
3510 memcmp(res, res + 6, 8) )
3511 goto fail;
3512 printf("okay\n");
3513 }
3514 else
3515 printf("skipped\n");
3516
3517 printf("%-40s", "Testing {evex} vmovd 32(%ecx),%xmm4...");
3518 if ( stack_exec && cpu_has_avx512f )
3519 {
3520 decl_insn(evex_vmovd_from_mem);
3521
3522 asm volatile ( "pcmpeqb %%xmm4, %%xmm4\n"
3523 put_insn(evex_vmovd_from_mem,
3524 "%{evex%} vmovd 32(%0), %%xmm4")
3525 :: "c" (NULL) );
3526
3527 set_insn(evex_vmovd_from_mem);
3528 rc = x86_emulate(&ctxt, &emulops);
3529 if ( rc != X86EMUL_OKAY || !check_eip(evex_vmovd_from_mem) )
3530 goto fail;
3531 asm ( "vmovd %1, %%xmm0\n\t"
3532 "vpcmpeqd %%zmm4, %%zmm0, %%k0\n\t"
3533 "kmovw %%k0, %0" : "=r" (rc) : "m" (res[8]) );
3534 if ( rc != 0xffff )
3535 goto fail;
3536 printf("okay\n");
3537 }
3538 else
3539 printf("skipped\n");
3540
3541 printf("%-40s", "Testing movd %mm3,%ebx...");
3542 if ( stack_exec && cpu_has_mmx )
3543 {
3544 decl_insn(movd_to_reg);
3545
3546 /*
3547 * Intentionally not specifying "b" as an input (or even output) here
3548 * to not keep the compiler from using the variable, which in turn
3549 * allows noticing whether the emulator touches the actual register
3550 * instead of the regs field.
3551 */
3552 asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
3553 put_insn(movd_to_reg, "movd %%mm3, %%ebx")
3554 :: );
3555
3556 set_insn(movd_to_reg);
3557 #ifdef __x86_64__
3558 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3559 #else
3560 regs.ebx = 0xbdbdbdbdUL;
3561 #endif
3562 rc = x86_emulate(&ctxt, &emulops);
3563 if ( (rc != X86EMUL_OKAY) || !check_eip(movd_to_reg) ||
3564 regs.ebx != 0xffffffff )
3565 goto fail;
3566 printf("okay\n");
3567 }
3568 else
3569 printf("skipped\n");
3570
3571 printf("%-40s", "Testing movd %ebx,%mm4...");
3572 if ( stack_exec && cpu_has_mmx )
3573 {
3574 decl_insn(movd_from_reg);
3575
3576 /* See comment next to movd above. */
3577 asm volatile ( "pcmpgtb %%mm4, %%mm4\n"
3578 put_insn(movd_from_reg, "movd %%ebx, %%mm4")
3579 :: );
3580
3581 set_insn(movd_from_reg);
3582 rc = x86_emulate(&ctxt, &emulops);
3583 if ( (rc != X86EMUL_OKAY) || !check_eip(movd_from_reg) )
3584 goto fail;
3585 asm ( "pxor %%mm2,%%mm2\n\t"
3586 "pcmpeqb %%mm4, %%mm2\n\t"
3587 "pmovmskb %%mm2, %0" : "=r" (rc) );
3588 if ( rc != 0xf0 )
3589 goto fail;
3590 asm ( "pcmpeqb %%mm4, %%mm3\n\t"
3591 "pmovmskb %%mm3, %0" : "=r" (rc) );
3592 if ( rc != 0x0f )
3593 goto fail;
3594 printf("okay\n");
3595 }
3596 else
3597 printf("skipped\n");
3598
3599 printf("%-40s", "Testing movd %xmm2,%ebx...");
3600 if ( stack_exec && cpu_has_sse2 )
3601 {
3602 decl_insn(movd_to_reg2);
3603
3604 /* See comment next to movd above. */
3605 asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
3606 put_insn(movd_to_reg2, "movd %%xmm2, %%ebx")
3607 :: );
3608
3609 set_insn(movd_to_reg2);
3610 #ifdef __x86_64__
3611 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3612 #else
3613 regs.ebx = 0xbdbdbdbdUL;
3614 #endif
3615 rc = x86_emulate(&ctxt, &emulops);
3616 if ( (rc != X86EMUL_OKAY) || !check_eip(movd_to_reg2) ||
3617 regs.ebx != 0xffffffff )
3618 goto fail;
3619 printf("okay\n");
3620 }
3621 else
3622 printf("skipped\n");
3623
3624 printf("%-40s", "Testing movd %ebx,%xmm3...");
3625 if ( stack_exec && cpu_has_sse2 )
3626 {
3627 decl_insn(movd_from_reg2);
3628
3629 /* See comment next to movd above. */
3630 asm volatile ( "pcmpgtb %%xmm3, %%xmm3\n"
3631 put_insn(movd_from_reg2, "movd %%ebx, %%xmm3")
3632 :: );
3633
3634 set_insn(movd_from_reg2);
3635 rc = x86_emulate(&ctxt, &emulops);
3636 if ( (rc != X86EMUL_OKAY) || !check_eip(movd_from_reg2) )
3637 goto fail;
3638 asm ( "pxor %%xmm1,%%xmm1\n\t"
3639 "pcmpeqb %%xmm3, %%xmm1\n\t"
3640 "pmovmskb %%xmm1, %0" : "=r" (rc) );
3641 if ( rc != 0xfff0 )
3642 goto fail;
3643 asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
3644 "pcmpeqb %%xmm3, %%xmm2\n\t"
3645 "pmovmskb %%xmm2, %0" : "=r" (rc) );
3646 if ( rc != 0x000f )
3647 goto fail;
3648 printf("okay\n");
3649 }
3650 else
3651 printf("skipped\n");
3652
3653 printf("%-40s", "Testing vmovd %xmm1,%ebx...");
3654 if ( stack_exec && cpu_has_avx )
3655 {
3656 decl_insn(vmovd_to_reg);
3657
3658 /* See comment next to movd above. */
3659 asm volatile ( "pcmpeqb %%xmm1, %%xmm1\n"
3660 put_insn(vmovd_to_reg, "vmovd %%xmm1, %%ebx")
3661 :: );
3662
3663 set_insn(vmovd_to_reg);
3664 #ifdef __x86_64__
3665 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3666 #else
3667 regs.ebx = 0xbdbdbdbdUL;
3668 #endif
3669 rc = x86_emulate(&ctxt, &emulops);
3670 if ( (rc != X86EMUL_OKAY) || !check_eip(vmovd_to_reg) ||
3671 regs.ebx != 0xffffffff )
3672 goto fail;
3673 printf("okay\n");
3674 }
3675 else
3676 printf("skipped\n");
3677
3678 printf("%-40s", "Testing vmovd %ebx,%xmm2...");
3679 if ( stack_exec && cpu_has_avx )
3680 {
3681 decl_insn(vmovd_from_reg);
3682
3683 /* See comment next to movd above. */
3684 asm volatile ( "pcmpgtb %%xmm2, %%xmm2\n"
3685 put_insn(vmovd_from_reg, "vmovd %%ebx, %%xmm2")
3686 :: );
3687
3688 set_insn(vmovd_from_reg);
3689 rc = x86_emulate(&ctxt, &emulops);
3690 if ( (rc != X86EMUL_OKAY) || !check_eip(vmovd_from_reg) )
3691 goto fail;
3692 asm ( "pxor %%xmm0,%%xmm0\n\t"
3693 "pcmpeqb %%xmm2, %%xmm0\n\t"
3694 "pmovmskb %%xmm0, %0" : "=r" (rc) );
3695 if ( rc != 0xfff0 )
3696 goto fail;
3697 asm ( "pcmpeqb %%xmm1, %%xmm1\n\t"
3698 "pcmpeqb %%xmm2, %%xmm1\n\t"
3699 "pmovmskb %%xmm1, %0" : "=r" (rc) );
3700 if ( rc != 0x000f )
3701 goto fail;
3702 printf("okay\n");
3703 }
3704 else
3705 printf("skipped\n");
3706
3707 printf("%-40s", "Testing {evex} vmovd %xmm2,%ebx...");
3708 if ( stack_exec && cpu_has_avx512f )
3709 {
3710 decl_insn(evex_vmovd_to_reg);
3711
3712 /* See comment next to movd above. */
3713 asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
3714 put_insn(evex_vmovd_to_reg,
3715 "%{evex%} vmovd %%xmm2, %%ebx")
3716 :: );
3717
3718 set_insn(evex_vmovd_to_reg);
3719 #ifdef __x86_64__
3720 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3721 #else
3722 regs.ebx = 0xbdbdbdbdUL;
3723 #endif
3724 rc = x86_emulate(&ctxt, &emulops);
3725 if ( (rc != X86EMUL_OKAY) || !check_eip(evex_vmovd_to_reg) ||
3726 regs.ebx != 0xffffffff )
3727 goto fail;
3728 printf("okay\n");
3729 }
3730 else
3731 printf("skipped\n");
3732
3733 printf("%-40s", "Testing {evex} vmovd %ebx,%xmm1...");
3734 if ( stack_exec && cpu_has_avx512f )
3735 {
3736 decl_insn(evex_vmovd_from_reg);
3737
3738 /* See comment next to movd above. */
3739 asm volatile ( "pcmpgtb %%xmm1, %%xmm1\n"
3740 put_insn(evex_vmovd_from_reg,
3741 "%{evex%} vmovd %%ebx, %%xmm1")
3742 :: );
3743
3744 set_insn(evex_vmovd_from_reg);
3745 rc = x86_emulate(&ctxt, &emulops);
3746 if ( (rc != X86EMUL_OKAY) || !check_eip(evex_vmovd_from_reg) )
3747 goto fail;
3748 asm ( "vmovd %1, %%xmm0\n\t"
3749 "vpcmpeqd %%zmm1, %%zmm0, %%k0\n\t"
3750 "kmovw %%k0, %0" : "=r" (rc) : "m" (res[8]) );
3751 if ( rc != 0xffff )
3752 goto fail;
3753 printf("okay\n");
3754 }
3755 else
3756 printf("skipped\n");
3757
3758 #ifdef __x86_64__
3759 printf("%-40s", "Testing movq %mm3,32(%ecx)...");
3760 if ( stack_exec && cpu_has_mmx )
3761 {
3762 decl_insn(movq_to_mem3);
3763
3764 asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
3765 put_insn(movq_to_mem3, "rex64 movd %%mm3, 32(%0)")
3766 :: "c" (NULL) );
3767
3768 memset(res, 0xbd, 64);
3769 set_insn(movq_to_mem3);
3770 regs.ecx = (unsigned long)res;
3771 regs.edx = 0;
3772 rc = x86_emulate(&ctxt, &emulops);
3773 if ( rc != X86EMUL_OKAY || !check_eip(movq_to_mem3) ||
3774 *((long *)res + 4) + 1 ||
3775 memcmp(res, res + 10, 24) ||
3776 memcmp(res, res + 6, 8) )
3777 goto fail;
3778 printf("okay\n");
3779 }
3780 else
3781 printf("skipped\n");
3782
3783 printf("%-40s", "Testing movq %xmm2,32(%edx)...");
3784 if ( stack_exec )
3785 {
3786 decl_insn(movq_to_mem4);
3787
3788 asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
3789 put_insn(movq_to_mem4, "rex64 movd %%xmm2, 32(%0)")
3790 :: "d" (NULL) );
3791
3792 memset(res, 0xdb, 64);
3793 set_insn(movq_to_mem4);
3794 regs.ecx = 0;
3795 regs.edx = (unsigned long)res;
3796 rc = x86_emulate(&ctxt, &emulops);
3797 if ( rc != X86EMUL_OKAY || !check_eip(movq_to_mem4) ||
3798 *((long *)res + 4) + 1 ||
3799 memcmp(res, res + 10, 24) ||
3800 memcmp(res, res + 6, 8) )
3801 goto fail;
3802 printf("okay\n");
3803 }
3804 else
3805 printf("skipped\n");
3806
3807 printf("%-40s", "Testing vmovq %xmm1,32(%ecx)...");
3808 if ( stack_exec && cpu_has_avx )
3809 {
3810 decl_insn(vmovq_to_mem2);
3811
3812 asm volatile ( "pcmpeqb %%xmm1, %%xmm1\n"
3813 #if 0 /* This doesn't work, as the assembler will pick opcode D6. */
3814 put_insn(vmovq_to_mem2, "vmovq %%xmm1, 32(%0)")
3815 #else
3816 put_insn(vmovq_to_mem2, ".byte 0xc4, 0xe1, 0xf9, 0x7e, 0x49, 0x20")
3817 #endif
3818 :: "c" (NULL) );
3819
3820 memset(res, 0xbd, 64);
3821 set_insn(vmovq_to_mem2);
3822 regs.ecx = (unsigned long)res;
3823 regs.edx = 0;
3824 rc = x86_emulate(&ctxt, &emulops);
3825 if ( rc != X86EMUL_OKAY || !check_eip(vmovq_to_mem2) ||
3826 *((long *)res + 4) + 1 ||
3827 memcmp(res, res + 10, 24) ||
3828 memcmp(res, res + 6, 8) )
3829 goto fail;
3830 printf("okay\n");
3831 }
3832 else
3833 printf("skipped\n");
3834
3835 printf("%-40s", "Testing {evex} vmovq %xmm11,32(%ecx)...");
3836 if ( stack_exec && cpu_has_avx512f )
3837 {
3838 decl_insn(evex_vmovq_to_mem2);
3839
3840 asm volatile ( "pcmpeqb %%xmm11, %%xmm11\n"
3841 #if 0 /* This may not work, as the assembler might pick opcode D6. */
3842 put_insn(evex_vmovq_to_mem2,
3843 "{evex} vmovq %%xmm11, 32(%0)")
3844 #else
3845 put_insn(evex_vmovq_to_mem2,
3846 ".byte 0x62, 0xf1, 0xfd, 0x08, 0x7e, 0x49, 0x04")
3847 #endif
3848 :: "c" (NULL) );
3849
3850 memset(res, 0xbd, 64);
3851 set_insn(evex_vmovq_to_mem2);
3852 regs.ecx = (unsigned long)res;
3853 regs.edx = 0;
3854 rc = x86_emulate(&ctxt, &emulops);
3855 if ( rc != X86EMUL_OKAY || !check_eip(evex_vmovq_to_mem2) ||
3856 *((long *)res + 4) + 1 ||
3857 memcmp(res, res + 10, 24) ||
3858 memcmp(res, res + 6, 8) )
3859 goto fail;
3860 printf("okay\n");
3861 }
3862 else
3863 printf("skipped\n");
3864
3865 printf("%-40s", "Testing movq %mm3,%rbx...");
3866 if ( stack_exec && cpu_has_mmx )
3867 {
3868 decl_insn(movq_to_reg);
3869
3870 /* See comment next to movd above. */
3871 asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
3872 put_insn(movq_to_reg, "movq %%mm3, %%rbx")
3873 :: );
3874
3875 set_insn(movq_to_reg);
3876 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3877 rc = x86_emulate(&ctxt, &emulops);
3878 if ( rc != X86EMUL_OKAY || regs.rbx + 1 || !check_eip(movq_to_reg) )
3879 goto fail;
3880 printf("okay\n");
3881 }
3882 else
3883 printf("skipped\n");
3884
3885 printf("%-40s", "Testing movq %xmm2,%rbx...");
3886 if ( stack_exec )
3887 {
3888 decl_insn(movq_to_reg2);
3889
3890 /* See comment next to movd above. */
3891 asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
3892 put_insn(movq_to_reg2, "movq %%xmm2, %%rbx")
3893 :: );
3894
3895 set_insn(movq_to_reg2);
3896 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3897 rc = x86_emulate(&ctxt, &emulops);
3898 if ( rc != X86EMUL_OKAY || regs.rbx + 1 || !check_eip(movq_to_reg2) )
3899 goto fail;
3900 printf("okay\n");
3901 }
3902 else
3903 printf("skipped\n");
3904
3905 printf("%-40s", "Testing vmovq %xmm1,%rbx...");
3906 if ( stack_exec && cpu_has_avx )
3907 {
3908 decl_insn(vmovq_to_reg);
3909
3910 /* See comment next to movd above. */
3911 asm volatile ( "pcmpeqb %%xmm1, %%xmm1\n"
3912 put_insn(vmovq_to_reg, "vmovq %%xmm1, %%rbx")
3913 :: );
3914
3915 set_insn(vmovq_to_reg);
3916 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3917 rc = x86_emulate(&ctxt, &emulops);
3918 if ( rc != X86EMUL_OKAY || regs.rbx + 1 || !check_eip(vmovq_to_reg) )
3919 goto fail;
3920 printf("okay\n");
3921 }
3922 else
3923 printf("skipped\n");
3924
3925 printf("%-40s", "Testing vmovq %xmm22,%rbx...");
3926 if ( stack_exec && cpu_has_avx512f )
3927 {
3928 decl_insn(evex_vmovq_to_reg);
3929
3930 /* See comment next to movd above. */
3931 asm volatile ( "pcmpeqq %%xmm2, %%xmm2\n\t"
3932 "vmovq %%xmm2, %%xmm22\n"
3933 put_insn(evex_vmovq_to_reg, "vmovq %%xmm22, %%rbx")
3934 :: );
3935
3936 set_insn(evex_vmovq_to_reg);
3937 regs.rbx = 0xbdbdbdbdbdbdbdbdUL;
3938 rc = x86_emulate(&ctxt, &emulops);
3939 if ( rc != X86EMUL_OKAY || !check_eip(evex_vmovq_to_reg) ||
3940 regs.rbx + 1 )
3941 goto fail;
3942 printf("okay\n");
3943 }
3944 else
3945 printf("skipped\n");
3946 #endif
3947
3948 printf("%-40s", "Testing maskmovq %mm4,%mm4...");
3949 if ( stack_exec && cpu_has_sse )
3950 {
3951 decl_insn(maskmovq);
3952
3953 asm volatile ( "pcmpgtb %mm4, %mm4\n"
3954 put_insn(maskmovq, "maskmovq %mm4, %mm4") );
3955
3956 set_insn(maskmovq);
3957 regs.edi = 0;
3958 rc = x86_emulate(&ctxt, &emulops);
3959 if ( rc != X86EMUL_OKAY || !check_eip(maskmovq) )
3960 goto fail;
3961
3962 asm volatile ( "pcmpeqb %mm3, %mm3\n\t"
3963 "punpcklbw %mm3, %mm4\n" );
3964 memset(res, 0x55, 24);
3965
3966 set_insn(maskmovq);
3967 regs.edi = (unsigned long)(res + 2);
3968 rc = x86_emulate(&ctxt, &emulops);
3969 if ( rc != X86EMUL_OKAY || !check_eip(maskmovq) ||
3970 memcmp(res, res + 4, 8) ||
3971 res[2] != 0xff55ff55 || res[3] != 0xff55ff55 )
3972 goto fail;
3973
3974 printf("okay\n");
3975 }
3976 else
3977 printf("skipped\n");
3978
3979 printf("%-40s", "Testing maskmovdqu %xmm3,%xmm3...");
3980 if ( stack_exec && cpu_has_sse2 )
3981 {
3982 decl_insn(maskmovdqu);
3983
3984 asm volatile ( "pcmpgtb %xmm3, %xmm3\n"
3985 put_insn(maskmovdqu, "maskmovdqu %xmm3, %xmm3") );
3986
3987 set_insn(maskmovdqu);
3988 regs.edi = 0;
3989 rc = x86_emulate(&ctxt, &emulops);
3990 if ( rc != X86EMUL_OKAY || !check_eip(maskmovdqu) )
3991 goto fail;
3992
3993 asm volatile ( "pcmpeqb %xmm4, %xmm4\n\t"
3994 "punpcklbw %xmm4, %xmm3\n" );
3995 memset(res, 0x55, 48);
3996
3997 set_insn(maskmovdqu);
3998 regs.edi = (unsigned long)(res + 4);
3999 rc = x86_emulate(&ctxt, &emulops);
4000 if ( rc != X86EMUL_OKAY || !check_eip(maskmovdqu) ||
4001 memcmp(res, res + 8, 16) ||
4002 res[4] != 0xff55ff55 || res[5] != 0xff55ff55 ||
4003 res[6] != 0xff55ff55 || res[7] != 0xff55ff55 )
4004 goto fail;
4005
4006 printf("okay\n");
4007 }
4008 else
4009 printf("skipped\n");
4010
4011 printf("%-40s", "Testing lddqu 4(%edx),%xmm4...");
4012 if ( stack_exec && cpu_has_sse3 )
4013 {
4014 decl_insn(lddqu);
4015
4016 asm volatile ( "pcmpgtb %%xmm4, %%xmm4\n"
4017 put_insn(lddqu, "lddqu 4(%0), %%xmm4")
4018 :: "d" (NULL) );
4019
4020 set_insn(lddqu);
4021 memset(res, 0x55, 64);
4022 memset(res + 1, 0xff, 16);
4023 regs.edx = (unsigned long)res;
4024 rc = x86_emulate(&ctxt, &emulops);
4025 if ( rc != X86EMUL_OKAY || !check_eip(lddqu) )
4026 goto fail;
4027 asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
4028 "pcmpeqb %%xmm4, %%xmm2\n\t"
4029 "pmovmskb %%xmm2, %0" : "=r" (rc) );
4030 if ( rc != 0xffff )
4031 goto fail;
4032 printf("okay\n");
4033 }
4034 else
4035 printf("skipped\n");
4036
4037 printf("%-40s", "Testing vlddqu (%ecx),%ymm4...");
4038 if ( stack_exec && cpu_has_avx )
4039 {
4040 decl_insn(vlddqu);
4041
4042 asm volatile ( "vpxor %%xmm4, %%xmm4, %%xmm4\n"
4043 put_insn(vlddqu, "vlddqu (%0), %%ymm4")
4044 :: "c" (NULL) );
4045
4046 set_insn(vlddqu);
4047 memset(res + 1, 0xff, 32);
4048 regs.ecx = (unsigned long)(res + 1);
4049 rc = x86_emulate(&ctxt, &emulops);
4050 if ( rc != X86EMUL_OKAY || !check_eip(vlddqu) )
4051 goto fail;
4052 asm ( "vextractf128 $1, %%ymm4, %%xmm3\n\t"
4053 "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n\t"
4054 "vpcmpeqb %%xmm4, %%xmm2, %%xmm0\n\t"
4055 "vpcmpeqb %%xmm3, %%xmm2, %%xmm1\n\t"
4056 "vpmovmskb %%xmm0, %0\n\t"
4057 "vpmovmskb %%xmm1, %1" : "=r" (rc), "=r" (i) );
4058 rc |= i << 16;
4059 if ( ~rc )
4060 goto fail;
4061 printf("okay\n");
4062 }
4063 else
4064 printf("skipped\n");
4065
4066 printf("%-40s", "Testing movntdqa 16(%edx),%xmm4...");
4067 if ( stack_exec && cpu_has_sse4_1 )
4068 {
4069 decl_insn(movntdqa);
4070
4071 asm volatile ( "pcmpgtb %%xmm4, %%xmm4\n"
4072 put_insn(movntdqa, "movntdqa 16(%0), %%xmm4")
4073 :: "d" (NULL) );
4074
4075 set_insn(movntdqa);
4076 memset(res, 0x55, 64);
4077 memset(res + 4, 0xff, 16);
4078 regs.edx = (unsigned long)res;
4079 rc = x86_emulate(&ctxt, &emulops);
4080 if ( rc != X86EMUL_OKAY || !check_eip(movntdqa) )
4081 goto fail;
4082 asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
4083 "pcmpeqb %%xmm4, %%xmm2\n\t"
4084 "pmovmskb %%xmm2, %0" : "=r" (rc) );
4085 if ( rc != 0xffff )
4086 goto fail;
4087 printf("okay\n");
4088 }
4089 else
4090 printf("skipped\n");
4091
4092 printf("%-40s", "Testing vmovntdqa (%ecx),%ymm4...");
4093 if ( stack_exec && cpu_has_avx2 )
4094 {
4095 decl_insn(vmovntdqa);
4096
4097 asm volatile ( "vpxor %%ymm4, %%ymm4, %%ymm4\n"
4098 put_insn(vmovntdqa, "vmovntdqa (%0), %%ymm4")
4099 :: "c" (NULL) );
4100
4101 set_insn(vmovntdqa);
4102 memset(res, 0x55, 96);
4103 memset(res + 8, 0xff, 32);
4104 regs.ecx = (unsigned long)(res + 8);
4105 rc = x86_emulate(&ctxt, &emulops);
4106 if ( rc != X86EMUL_OKAY || !check_eip(vmovntdqa) )
4107 goto fail;
4108 asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
4109 "vpcmpeqb %%ymm4, %%ymm2, %%ymm0\n\t"
4110 "vpmovmskb %%ymm0, %0" : "=r" (rc) );
4111 if ( ~rc )
4112 goto fail;
4113 printf("okay\n");
4114 }
4115 else
4116 printf("skipped\n");
4117
4118 printf("%-40s", "Testing vmovntdqa 64(%ecx),%zmm4...");
4119 if ( stack_exec && cpu_has_avx512f )
4120 {
4121 decl_insn(evex_vmovntdqa);
4122
4123 asm volatile ( "vpxor %%xmm4, %%xmm4, %%xmm4\n"
4124 put_insn(evex_vmovntdqa, "vmovntdqa 64(%0), %%zmm4")
4125 :: "c" (NULL) );
4126
4127 set_insn(evex_vmovntdqa);
4128 memset(res, 0x55, 192);
4129 memset(res + 16, 0xff, 64);
4130 regs.ecx = (unsigned long)res;
4131 rc = x86_emulate(&ctxt, &emulops);
4132 if ( rc != X86EMUL_OKAY || !check_eip(evex_vmovntdqa) )
4133 goto fail;
4134 asm ( "vpbroadcastd %1, %%zmm2\n\t"
4135 "vpcmpeqd %%zmm4, %%zmm2, %%k0\n\t"
4136 "kmovw %%k0, %0" : "=r" (rc) : "0" (~0) );
4137 if ( rc != 0xffff )
4138 goto fail;
4139 printf("okay\n");
4140 }
4141 else
4142 printf("skipped\n");
4143
4144 printf("%-40s", "Testing pcmpestri $0x1a,(%ecx),%xmm2...");
4145 if ( stack_exec && cpu_has_sse4_2 )
4146 {
4147 decl_insn(pcmpestri);
4148
4149 memcpy(res, "abcdefgh\0\1\2\3\4\5\6\7", 16);
4150 asm volatile ( "movq %0, %%xmm2\n"
4151 put_insn(pcmpestri, "pcmpestri $0b00011010, (%1), %%xmm2")
4152 :: "m" (res[0]), "c" (NULL) );
4153
4154 set_insn(pcmpestri);
4155 regs.eax = regs.edx = 12;
4156 regs.ecx = (unsigned long)res;
4157 regs.eflags = X86_EFLAGS_PF | X86_EFLAGS_AF |
4158 X86_EFLAGS_IF | X86_EFLAGS_OF;
4159 rc = x86_emulate(&ctxt, &emulops);
4160 if ( rc != X86EMUL_OKAY || !check_eip(pcmpestri) ||
4161 regs.ecx != 9 ||
4162 (regs.eflags & X86_EFLAGS_ARITH_MASK) !=
4163 (X86_EFLAGS_CF | X86_EFLAGS_ZF | X86_EFLAGS_SF) )
4164 goto fail;
4165 printf("okay\n");
4166 }
4167 else
4168 printf("skipped\n");
4169
4170 printf("%-40s", "Testing pcmpestrm $0x5a,(%ecx),%xmm2...");
4171 if ( stack_exec && cpu_has_sse4_2 )
4172 {
4173 decl_insn(pcmpestrm);
4174
4175 asm volatile ( "movq %0, %%xmm2\n"
4176 put_insn(pcmpestrm, "pcmpestrm $0b01011010, (%1), %%xmm2")
4177 :: "m" (res[0]), "c" (NULL) );
4178
4179 set_insn(pcmpestrm);
4180 regs.ecx = (unsigned long)res;
4181 regs.eflags = X86_EFLAGS_PF | X86_EFLAGS_AF |
4182 X86_EFLAGS_IF | X86_EFLAGS_OF;
4183 rc = x86_emulate(&ctxt, &emulops);
4184 if ( rc != X86EMUL_OKAY || !check_eip(pcmpestrm) )
4185 goto fail;
4186 asm ( "pmovmskb %%xmm0, %0" : "=r" (rc) );
4187 if ( rc != 0x0e00 ||
4188 (regs.eflags & X86_EFLAGS_ARITH_MASK) !=
4189 (X86_EFLAGS_CF | X86_EFLAGS_ZF | X86_EFLAGS_SF) )
4190 goto fail;
4191 printf("okay\n");
4192 }
4193 else
4194 printf("skipped\n");
4195
4196 printf("%-40s", "Testing pcmpistri $0x1a,(%ecx),%xmm2...");
4197 if ( stack_exec && cpu_has_sse4_2 )
4198 {
4199 decl_insn(pcmpistri);
4200
4201 asm volatile ( "movq %0, %%xmm2\n"
4202 put_insn(pcmpistri, "pcmpistri $0b00011010, (%1), %%xmm2")
4203 :: "m" (res[0]), "c" (NULL) );
4204
4205 set_insn(pcmpistri);
4206 regs.eflags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
4207 X86_EFLAGS_IF | X86_EFLAGS_OF;
4208 rc = x86_emulate(&ctxt, &emulops);
4209 if ( rc != X86EMUL_OKAY || !check_eip(pcmpistri) ||
4210 regs.ecx != 16 ||
4211 (regs.eflags & X86_EFLAGS_ARITH_MASK) !=
4212 (X86_EFLAGS_ZF | X86_EFLAGS_SF) )
4213 goto fail;
4214 printf("okay\n");
4215 }
4216 else
4217 printf("skipped\n");
4218
4219 printf("%-40s", "Testing pcmpistrm $0x4a,(%ecx),%xmm2...");
4220 if ( stack_exec && cpu_has_sse4_2 )
4221 {
4222 decl_insn(pcmpistrm);
4223
4224 asm volatile ( "movq %0, %%xmm2\n"
4225 put_insn(pcmpistrm, "pcmpistrm $0b01001010, (%1), %%xmm2")
4226 :: "m" (res[0]), "c" (NULL) );
4227
4228 set_insn(pcmpistrm);
4229 regs.ecx = (unsigned long)res;
4230 regs.eflags = X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_IF;
4231 rc = x86_emulate(&ctxt, &emulops);
4232 if ( rc != X86EMUL_OKAY || !check_eip(pcmpistrm) )
4233 goto fail;
4234 asm ( "pmovmskb %%xmm0, %0" : "=r" (rc) );
4235 if ( rc != 0xffff ||
4236 (regs.eflags & X86_EFLAGS_ARITH_MASK) !=
4237 (X86_EFLAGS_CF | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF) )
4238 goto fail;
4239 printf("okay\n");
4240 }
4241 else
4242 printf("skipped\n");
4243
4244 printf("%-40s", "Testing vpcmpestri $0x7a,(%esi),%xmm2...");
4245 if ( stack_exec && cpu_has_avx )
4246 {
4247 decl_insn(vpcmpestri);
4248
4249 asm volatile ( "movq %0, %%xmm2\n"
4250 #ifdef __x86_64__
4251 put_insn(vpcmpestri,
4252 "vpcmpestriq $0b01111010, (%1), %%xmm2")
4253 #else
4254 put_insn(vpcmpestri,
4255 "vpcmpestri $0b01111010, (%1), %%xmm2")
4256 #endif
4257 :: "m" (res[0]), "S" (NULL) );
4258
4259 set_insn(vpcmpestri);
4260 #ifdef __x86_64__
4261 regs.rax = ~0U + 1UL;
4262 regs.rcx = ~0UL;
4263 #else
4264 regs.eax = 0x7fffffff;
4265 #endif
4266 regs.esi = (unsigned long)res;
4267 regs.eflags = X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_SF |
4268 X86_EFLAGS_IF | X86_EFLAGS_OF;
4269 rc = x86_emulate(&ctxt, &emulops);
4270 if ( rc != X86EMUL_OKAY || !check_eip(vpcmpestri) ||
4271 regs.ecx != 11 ||
4272 (regs.eflags & X86_EFLAGS_ARITH_MASK) !=
4273 (X86_EFLAGS_ZF | X86_EFLAGS_CF) )
4274 goto fail;
4275 printf("okay\n");
4276 }
4277 else
4278 printf("skipped\n");
4279
4280 printf("%-40s", "Testing extrq $4,$56,%xmm2...");
4281 if ( stack_exec && cpu_has_sse4a )
4282 {
4283 decl_insn(extrq_imm);
4284
4285 res[0] = 0x44332211;
4286 res[1] = 0x88776655;
4287 asm volatile ( "movq %0, %%xmm2\n"
4288 put_insn(extrq_imm, "extrq $4, $56, %%xmm2")
4289 :: "m" (res[0]) : "memory" );
4290
4291 set_insn(extrq_imm);
4292 rc = x86_emulate(&ctxt, &emulops);
4293 asm ( "movq %%xmm2, %0" : "=m" (res[4]) :: "memory" );
4294 if ( rc != X86EMUL_OKAY || !check_eip(extrq_imm) ||
4295 res[4] != 0x54433221 || res[5] != 0x877665 )
4296 goto fail;
4297 printf("okay\n");
4298 }
4299 else
4300 printf("skipped\n");
4301
4302 printf("%-40s", "Testing extrq %xmm3,%xmm2...");
4303 if ( stack_exec && cpu_has_sse4a )
4304 {
4305 decl_insn(extrq_reg);
4306
4307 res[4] = 56 + (4 << 8);
4308 res[5] = 0;
4309 asm volatile ( "movq %0, %%xmm2\n"
4310 "movq %1, %%xmm3\n"
4311 put_insn(extrq_reg, "extrq %%xmm3, %%xmm2")
4312 :: "m" (res[0]), "m" (res[4]) : "memory" );
4313
4314 set_insn(extrq_reg);
4315 rc = x86_emulate(&ctxt, &emulops);
4316 asm ( "movq %%xmm2, %0" : "=m" (res[4]) :: "memory" );
4317 if ( rc != X86EMUL_OKAY || !check_eip(extrq_reg) ||
4318 res[4] != 0x54433221 || res[5] != 0x877665 )
4319 goto fail;
4320 printf("okay\n");
4321 }
4322 else
4323 printf("skipped\n");
4324
4325 printf("%-40s", "Testing insertq $12,$40,%xmm2,%xmm3...");
4326 if ( stack_exec && cpu_has_sse4a )
4327 {
4328 decl_insn(insertq_imm);
4329
4330 res[4] = 0xccbbaa99;
4331 res[5] = 0x00ffeedd;
4332 asm volatile ( "movq %1, %%xmm2\n"
4333 "movq %0, %%xmm3\n"
4334 put_insn(insertq_imm, "insertq $12, $40, %%xmm2, %%xmm3")
4335 :: "m" (res[0]), "m" (res[4]) : "memory" );
4336
4337 set_insn(insertq_imm);
4338 rc = x86_emulate(&ctxt, &emulops);
4339 asm ( "movq %%xmm3, %0" : "=m" (res[4]) :: "memory" );
4340 if ( rc != X86EMUL_OKAY || !check_eip(insertq_imm) ||
4341 res[4] != 0xbaa99211 || res[5] != 0x887ddccb )
4342 goto fail;
4343 printf("okay\n");
4344 }
4345 else
4346 printf("skipped\n");
4347
4348 printf("%-40s", "Testing insertq %xmm2,%xmm3...");
4349 if ( stack_exec && cpu_has_sse4a )
4350 {
4351 decl_insn(insertq_reg);
4352
4353 res[4] = 0xccbbaa99;
4354 res[5] = 0x00ffeedd;
4355 res[6] = 40 + (12 << 8);
4356 res[7] = 0;
4357 asm volatile ( "movdqu %1, %%xmm2\n"
4358 "movq %0, %%xmm3\n"
4359 put_insn(insertq_reg, "insertq %%xmm2, %%xmm3")
4360 :: "m" (res[0]), "m" (res[4]) : "memory" );
4361
4362 set_insn(insertq_reg);
4363 rc = x86_emulate(&ctxt, &emulops);
4364 asm ( "movq %%xmm3, %0" : "=m" (res[4]) :: "memory" );
4365 if ( rc != X86EMUL_OKAY || !check_eip(insertq_reg) ||
4366 res[4] != 0xbaa99211 || res[5] != 0x887ddccb )
4367 goto fail;
4368 printf("okay\n");
4369 }
4370 else
4371 printf("skipped\n");
4372
4373 /*
4374 * The following "maskmov" tests are not only making sure the written data
4375 * is correct, but verify (by placing operands on the mapping boundaries)
4376 * that elements controlled by clear mask bits aren't being accessed.
4377 */
4378 printf("%-40s", "Testing vmaskmovps %xmm1,%xmm2,(%edx)...");
4379 if ( stack_exec && cpu_has_avx )
4380 {
4381 decl_insn(vmaskmovps);
4382
4383 asm volatile ( "vxorps %%xmm1, %%xmm1, %%xmm1\n\t"
4384 "vcmpeqss %%xmm1, %%xmm1, %%xmm2\n\t"
4385 put_insn(vmaskmovps, "vmaskmovps %%xmm1, %%xmm2, (%0)")
4386 :: "d" (NULL) );
4387
4388 memset(res + MMAP_SZ / sizeof(*res) - 8, 0xdb, 32);
4389 set_insn(vmaskmovps);
4390 regs.edx = (unsigned long)res + MMAP_SZ - 4;
4391 rc = x86_emulate(&ctxt, &emulops);
4392 if ( rc != X86EMUL_OKAY || !check_eip(vmaskmovps) ||
4393 res[MMAP_SZ / sizeof(*res) - 1] ||
4394 memcmp(res + MMAP_SZ / sizeof(*res) - 8,
4395 res + MMAP_SZ / sizeof(*res) - 4, 12) )
4396 goto fail;
4397
4398 asm volatile ( "vinsertps $0b00110111, %xmm2, %xmm2, %xmm2" );
4399 memset(res, 0xdb, 32);
4400 set_insn(vmaskmovps);
4401 regs.edx = (unsigned long)(res - 3);
4402 rc = x86_emulate(&ctxt, &emulops);
4403 if ( rc != X86EMUL_OKAY || !check_eip(vmaskmovps) ||
4404 res[0] || memcmp(res + 1, res + 4, 12) )
4405 goto fail;
4406
4407 printf("okay\n");
4408 }
4409 else
4410 printf("skipped\n");
4411
4412 printf("%-40s", "Testing vmaskmovpd %xmm1,%xmm2,(%edx)...");
4413 if ( stack_exec && cpu_has_avx )
4414 {
4415 decl_insn(vmaskmovpd);
4416
4417 asm volatile ( "vxorpd %%xmm1, %%xmm1, %%xmm1\n\t"
4418 "vcmpeqsd %%xmm1, %%xmm1, %%xmm2\n\t"
4419 put_insn(vmaskmovpd, "vmaskmovpd %%xmm1, %%xmm2, (%0)")
4420 :: "d" (NULL) );
4421
4422 memset(res + MMAP_SZ / sizeof(*res) - 8, 0xdb, 32);
4423 set_insn(vmaskmovpd);
4424 regs.edx = (unsigned long)res + MMAP_SZ - 8;
4425 rc = x86_emulate(&ctxt, &emulops);
4426 if ( rc != X86EMUL_OKAY || !check_eip(vmaskmovpd) ||
4427 res[MMAP_SZ / sizeof(*res) - 1] ||
4428 res[MMAP_SZ / sizeof(*res) - 2] ||
4429 memcmp(res + MMAP_SZ / sizeof(*res) - 8,
4430 res + MMAP_SZ / sizeof(*res) - 4, 8) )
4431 goto fail;
4432
4433 asm volatile ( "vmovddup %xmm2, %xmm2\n\t"
4434 "vmovsd %xmm1, %xmm2, %xmm2" );
4435 memset(res, 0xdb, 32);
4436 set_insn(vmaskmovpd);
4437 regs.edx = (unsigned long)(res - 2);
4438 rc = x86_emulate(&ctxt, &emulops);
4439 if ( rc != X86EMUL_OKAY || !check_eip(vmaskmovpd) ||
4440 res[0] || res[1] || memcmp(res + 2, res + 4, 8) )
4441 goto fail;
4442
4443 printf("okay\n");
4444 }
4445 else
4446 printf("skipped\n");
4447
4448 printf("%-40s", "Testing vpmaskmovd %xmm1,%xmm2,(%edx)...");
4449 if ( stack_exec && cpu_has_avx2 )
4450 {
4451 decl_insn(vpmaskmovd);
4452
4453 asm volatile ( "vpxor %%xmm1, %%xmm1, %%xmm1\n\t"
4454 "vpinsrd $0b00, %1, %%xmm1, %%xmm2\n\t"
4455 put_insn(vpmaskmovd, "vpmaskmovd %%xmm1, %%xmm2, (%0)")
4456 :: "d" (NULL), "r" (~0) );
4457
4458 memset(res + MMAP_SZ / sizeof(*res) - 8, 0xdb, 32);
4459 set_insn(vpmaskmovd);
4460 regs.edx = (unsigned long)res + MMAP_SZ - 4;
4461 rc = x86_emulate(&ctxt, &emulops);
4462 if ( rc != X86EMUL_OKAY || !check_eip(vpmaskmovd) ||
4463 res[MMAP_SZ / sizeof(*res) - 1] ||
4464 memcmp(res + MMAP_SZ / sizeof(*res) - 8,
4465 res + MMAP_SZ / sizeof(*res) - 4, 12) )
4466 goto fail;
4467
4468 asm volatile ( "vpinsrd $0b11, %0, %%xmm1, %%xmm2" :: "r" (~0) );
4469 memset(res, 0xdb, 32);
4470 set_insn(vpmaskmovd);
4471 regs.edx = (unsigned long)(res - 3);
4472 rc = x86_emulate(&ctxt, &emulops);
4473 if ( rc != X86EMUL_OKAY || !check_eip(vpmaskmovd) ||
4474 res[0] || memcmp(res + 1, res + 4, 12) )
4475 goto fail;
4476
4477 printf("okay\n");
4478 }
4479 else
4480 printf("skipped\n");
4481
4482 printf("%-40s", "Testing vpmaskmovq %xmm1,%xmm2,(%edx)...");
4483 if ( stack_exec && cpu_has_avx2 )
4484 {
4485 decl_insn(vpmaskmovq);
4486
4487 asm volatile ( "vpxor %%xmm1, %%xmm1, %%xmm1\n\t"
4488 "vpcmpeqd %%xmm0, %%xmm0, %%xmm0\n\t"
4489 "vpblendd $0b0011, %%xmm0, %%xmm1, %%xmm2\n\t"
4490 put_insn(vpmaskmovq, "vpmaskmovq %%xmm1, %%xmm2, (%0)")
4491 :: "d" (NULL) );
4492
4493 memset(res + MMAP_SZ / sizeof(*res) - 8, 0xdb, 32);
4494 set_insn(vpmaskmovq);
4495 regs.edx = (unsigned long)res + MMAP_SZ - 8;
4496 rc = x86_emulate(&ctxt, &emulops);
4497 if ( rc != X86EMUL_OKAY || !check_eip(vpmaskmovq) ||
4498 res[MMAP_SZ / sizeof(*res) - 1] ||
4499 res[MMAP_SZ / sizeof(*res) - 2] ||
4500 memcmp(res + MMAP_SZ / sizeof(*res) - 8,
4501 res + MMAP_SZ / sizeof(*res) - 4, 8) )
4502 goto fail;
4503
4504 asm volatile ( "vpermq $0b00000001, %ymm2, %ymm2" );
4505 memset(res, 0xdb, 32);
4506 set_insn(vpmaskmovq);
4507 regs.edx = (unsigned long)(res - 2);
4508 rc = x86_emulate(&ctxt, &emulops);
4509 if ( rc != X86EMUL_OKAY || !check_eip(vpmaskmovq) ||
4510 res[0] || res[1] || memcmp(res + 2, res + 4, 8) )
4511 goto fail;
4512
4513 printf("okay\n");
4514 }
4515 else
4516 printf("skipped\n");
4517
4518 printf("%-40s", "Testing vbcstnebf162ps 2(%ecx),%ymm3...");
4519 if ( stack_exec && cpu_has_avx_ne_convert )
4520 {
4521 decl_insn(vbcstnebf162ps);
4522
4523 asm volatile ( /* vbcstnebf162ps 2(%0), %%ymm3 */
4524 put_insn(vbcstnebf162ps,
4525 ".byte 0xc4, 0xe2, 0x7e, 0xb1, 0x59, 0x02 ")
4526 :: "c" (NULL) );
4527
4528 res[0] = 0x43210000;
4529 regs.ecx = (unsigned long)res;
4530 set_insn(vbcstnebf162ps);
4531 bytes_read = 0;
4532 rc = x86_emulate(&ctxt, &emulops);
4533 if ( rc != X86EMUL_OKAY || !check_eip(vbcstnebf162ps) ||
4534 bytes_read != 2 )
4535 goto fail;
4536
4537 asm volatile ( "vbroadcastss %1, %%ymm2;"
4538 "vsubps %%ymm3, %%ymm2, %%ymm1;"
4539 "vptest %%ymm1, %%ymm1;"
4540 "setc %b0; setz %h0"
4541 : "=&Q" (rc)
4542 : "m" (res[0]) );
4543 if ( (rc & 0xffff) != 0x0101 )
4544 goto fail;
4545
4546 printf("okay\n");
4547 }
4548 else
4549 printf("skipped\n");
4550
4551 printf("%-40s", "Testing stmxcsr (%edx)...");
4552 if ( cpu_has_sse )
4553 {
4554 decl_insn(stmxcsr);
4555
4556 asm volatile ( put_insn(stmxcsr, "stmxcsr (%0)") :: "d" (NULL) );
4557
4558 res[0] = 0x12345678;
4559 res[1] = 0x87654321;
4560 asm ( "stmxcsr %0" : "=m" (res[2]) );
4561 set_insn(stmxcsr);
4562 regs.edx = (unsigned long)res;
4563 rc = x86_emulate(&ctxt, &emulops);
4564 if ( rc != X86EMUL_OKAY || !check_eip(stmxcsr) ||
4565 res[0] != res[2] || res[1] != 0x87654321 )
4566 goto fail;
4567 printf("okay\n");
4568 }
4569 else
4570 printf("skipped\n");
4571
4572 printf("%-40s", "Testing ldmxcsr 4(%ecx)...");
4573 if ( cpu_has_sse )
4574 {
4575 decl_insn(ldmxcsr);
4576
4577 asm volatile ( put_insn(ldmxcsr, "ldmxcsr 4(%0)") :: "c" (NULL) );
4578
4579 set_insn(ldmxcsr);
4580 res[1] = mxcsr_mask;
4581 regs.ecx = (unsigned long)res;
4582 rc = x86_emulate(&ctxt, &emulops);
4583 asm ( "stmxcsr %0; ldmxcsr %1" : "=m" (res[0]) : "m" (res[2]) );
4584 if ( rc != X86EMUL_OKAY || !check_eip(ldmxcsr) ||
4585 res[0] != mxcsr_mask )
4586 goto fail;
4587 printf("okay\n");
4588 }
4589 else
4590 printf("skipped\n");
4591
4592 printf("%-40s", "Testing vstmxcsr (%ecx)...");
4593 if ( cpu_has_avx )
4594 {
4595 decl_insn(vstmxcsr);
4596
4597 asm volatile ( put_insn(vstmxcsr, "vstmxcsr (%0)") :: "c" (NULL) );
4598
4599 res[0] = 0x12345678;
4600 res[1] = 0x87654321;
4601 set_insn(vstmxcsr);
4602 regs.ecx = (unsigned long)res;
4603 rc = x86_emulate(&ctxt, &emulops);
4604 if ( rc != X86EMUL_OKAY || !check_eip(vstmxcsr) ||
4605 res[0] != res[2] || res[1] != 0x87654321 )
4606 goto fail;
4607 printf("okay\n");
4608 }
4609 else
4610 printf("skipped\n");
4611
4612 printf("%-40s", "Testing vldmxcsr 4(%edx)...");
4613 if ( cpu_has_avx )
4614 {
4615 decl_insn(vldmxcsr);
4616
4617 asm volatile ( put_insn(vldmxcsr, "vldmxcsr 4(%0)") :: "d" (NULL) );
4618
4619 set_insn(vldmxcsr);
4620 res[1] = mxcsr_mask;
4621 regs.edx = (unsigned long)res;
4622 rc = x86_emulate(&ctxt, &emulops);
4623 asm ( "stmxcsr %0; ldmxcsr %1" : "=m" (res[0]) : "m" (res[2]) );
4624 if ( rc != X86EMUL_OKAY || !check_eip(vldmxcsr) ||
4625 res[0] != mxcsr_mask )
4626 goto fail;
4627 printf("okay\n");
4628 }
4629 else
4630 printf("skipped\n");
4631
4632 #ifdef __x86_64__
4633 printf("%-40s", "Testing vzeroupper (compat)...");
4634 if ( cpu_has_avx )
4635 {
4636 decl_insn(vzeroupper);
4637
4638 ctxt.lma = false;
4639 ctxt.sp_size = ctxt.addr_size = 32;
4640
4641 asm volatile ( "vxorps %xmm2, %xmm2, %xmm3\n"
4642 "vcmpeqps %ymm3, %ymm3, %ymm4\n"
4643 "vmovaps %ymm4, %ymm9\n"
4644 put_insn(vzeroupper, "vzeroupper") );
4645
4646 set_insn(vzeroupper);
4647 rc = x86_emulate(&ctxt, &emulops);
4648 if ( rc != X86EMUL_OKAY || !check_eip(vzeroupper) )
4649 goto fail;
4650
4651 /* XMM0...XMM7 should have their high parts cleared. */
4652 asm ( "vextractf128 $1, %%ymm4, %%xmm0\n\t"
4653 "vpmovmskb %%xmm4, %0\n\t"
4654 "vpmovmskb %%xmm0, %1" : "=r" (rc), "=r" (i) );
4655 if ( rc != 0xffff || i )
4656 goto fail;
4657
4658 /* XMM8...XMM15 should have their high parts preserved. */
4659 asm ( "vextractf128 $1, %%ymm9, %%xmm1\n\t"
4660 "vpmovmskb %%xmm9, %0\n\t"
4661 "vpmovmskb %%xmm1, %1" : "=r" (rc), "=r" (i) );
4662 if ( rc != 0xffff || i != 0xffff )
4663 goto fail;
4664 printf("okay\n");
4665
4666 ctxt.lma = true;
4667 ctxt.sp_size = ctxt.addr_size = 64;
4668 }
4669 else
4670 printf("skipped\n");
4671 #endif
4672
4673 printf("%-40s", "Testing vcvtph2ps (%ecx),%ymm1...");
4674 if ( stack_exec && cpu_has_f16c )
4675 {
4676 decl_insn(vcvtph2ps);
4677 decl_insn(vcvtps2ph);
4678
4679 asm volatile ( "vxorps %%xmm1, %%xmm1, %%xmm1\n"
4680 put_insn(vcvtph2ps, "vcvtph2ps (%0), %%ymm1")
4681 :: "c" (NULL) );
4682
4683 set_insn(vcvtph2ps);
4684 res[1] = 0x40003c00; /* (1.0, 2.0) */
4685 res[2] = 0x44004200; /* (3.0, 4.0) */
4686 res[3] = 0x3400b800; /* (-.5, .25) */
4687 res[4] = 0xbc000000; /* (0.0, -1.) */
4688 memset(res + 5, 0xff, 16);
4689 regs.ecx = (unsigned long)(res + 1);
4690 rc = x86_emulate(&ctxt, &emulops);
4691 asm volatile ( "vmovups %%ymm1, %0" : "=m" (res[16]) );
4692 if ( rc != X86EMUL_OKAY || !check_eip(vcvtph2ps) )
4693 goto fail;
4694 printf("okay\n");
4695
4696 printf("%-40s", "Testing vcvtps2ph $0,%ymm1,(%edx)...");
4697 asm volatile ( "vmovups %0, %%ymm1\n"
4698 put_insn(vcvtps2ph, "vcvtps2ph $0, %%ymm1, (%1)")
4699 :: "m" (res[16]), "d" (NULL) );
4700
4701 set_insn(vcvtps2ph);
4702 memset(res + 7, 0, 32);
4703 regs.edx = (unsigned long)(res + 7);
4704 rc = x86_emulate(&ctxt, &emulops);
4705 if ( rc != X86EMUL_OKAY || !check_eip(vcvtps2ph) ||
4706 memcmp(res + 1, res + 7, 16) ||
4707 res[11] || res[12] || res[13] || res[14] )
4708 goto fail;
4709 printf("okay\n");
4710 }
4711 else
4712 printf("skipped\n");
4713
4714 printf("%-40s", "Testing vcvtph2ps 32(%ecx),%zmm7{%k4}...");
4715 if ( stack_exec && cpu_has_avx512f )
4716 {
4717 decl_insn(evex_vcvtph2ps);
4718 decl_insn(evex_vcvtps2ph);
4719
4720 asm volatile ( "vpternlogd $0x81, %%zmm7, %%zmm7, %%zmm7\n\t"
4721 "kmovw %1,%%k4\n"
4722 put_insn(evex_vcvtph2ps, "vcvtph2ps 32(%0), %%zmm7%{%%k4%}")
4723 :: "c" (NULL), "r" (0x3333) );
4724
4725 set_insn(evex_vcvtph2ps);
4726 memset(res, 0xff, 128);
4727 res[8] = 0x40003c00; /* (1.0, 2.0) */
4728 res[10] = 0x44004200; /* (3.0, 4.0) */
4729 res[12] = 0x3400b800; /* (-.5, .25) */
4730 res[14] = 0xbc000000; /* (0.0, -1.) */
4731 regs.ecx = (unsigned long)res;
4732 rc = x86_emulate(&ctxt, &emulops);
4733 asm volatile ( "vmovups %%zmm7, %0" : "=m" (res[16]) );
4734 if ( rc != X86EMUL_OKAY || !check_eip(evex_vcvtph2ps) )
4735 goto fail;
4736 printf("okay\n");
4737
4738 printf("%-40s", "Testing vcvtps2ph $0,%zmm3,64(%edx){%k4}...");
4739 asm volatile ( "vmovups %0, %%zmm3\n"
4740 put_insn(evex_vcvtps2ph, "vcvtps2ph $0, %%zmm3, 128(%1)%{%%k4%}")
4741 :: "m" (res[16]), "d" (NULL) );
4742
4743 set_insn(evex_vcvtps2ph);
4744 regs.edx = (unsigned long)res;
4745 memset(res + 32, 0xcc, 32);
4746 rc = x86_emulate(&ctxt, &emulops);
4747 if ( rc != X86EMUL_OKAY || !check_eip(evex_vcvtps2ph) )
4748 goto fail;
4749 res[15] = res[13] = res[11] = res[9] = 0xcccccccc;
4750 if ( memcmp(res + 8, res + 32, 32) )
4751 goto fail;
4752 printf("okay\n");
4753 }
4754 else
4755 printf("skipped\n");
4756
4757 printf("%-40s", "Testing vfixupimmpd $0,8(%edx){1to8},%zmm3,%zmm4...");
4758 if ( stack_exec && cpu_has_avx512f )
4759 {
4760 decl_insn(vfixupimmpd);
4761 static const struct {
4762 double d[4];
4763 }
4764 src = { { -1, 0, 1, 2 } },
4765 dst = { { 3, 4, 5, 6 } },
4766 out = { { .5, -1, 90, 2 } };
4767
4768 asm volatile ( "vbroadcastf64x4 %1, %%zmm3\n\t"
4769 "vbroadcastf64x4 %2, %%zmm4\n"
4770 put_insn(vfixupimmpd,
4771 "vfixupimmpd $0, 8(%0)%{1to8%}, %%zmm3, %%zmm4")
4772 :: "d" (NULL), "m" (src), "m" (dst) );
4773
4774 set_insn(vfixupimmpd);
4775 /*
4776 * Nibble (token) mapping (unused ones simply set to zero):
4777 * 2 (ZERO) -> -1 (0x9)
4778 * 3 (POS_ONE) -> 90 (0xc)
4779 * 6 (NEG) -> 1/2 (0xb)
4780 * 7 (POS) -> src (0x1)
4781 */
4782 res[2] = 0x1b00c900;
4783 regs.edx = (unsigned long)res;
4784 rc = x86_emulate(&ctxt, &emulops);
4785 asm volatile ( "vmovupd %%zmm4, %0" : "=m" (res[0]) );
4786 if ( rc != X86EMUL_OKAY || !check_eip(vfixupimmpd) ||
4787 memcmp(res + 0, &out, sizeof(out)) ||
4788 memcmp(res + 8, &out, sizeof(out)) )
4789 goto fail;
4790 printf("okay\n");
4791 }
4792 else
4793 printf("skipped\n");
4794
4795
4796 printf("%-40s", "Testing vfpclasspsz $0x46,64(%edx),%k2...");
4797 if ( stack_exec && cpu_has_avx512dq )
4798 {
4799 decl_insn(vfpclassps);
4800
4801 asm volatile ( put_insn(vfpclassps,
4802 /* 0x46: check for +/- 0 and neg. */
4803 "vfpclasspsz $0x46, 64(%0), %%k2")
4804 :: "d" (NULL) );
4805
4806 set_insn(vfpclassps);
4807 for ( i = 0; i < 3; ++i )
4808 {
4809 res[16 + i * 5 + 0] = 0x00000000; /* +0 */
4810 res[16 + i * 5 + 1] = 0x80000000; /* -0 */
4811 res[16 + i * 5 + 2] = 0x80000001; /* -DEN */
4812 res[16 + i * 5 + 3] = 0xff000000; /* -FIN */
4813 res[16 + i * 5 + 4] = 0x7f000000; /* +FIN */
4814 }
4815 res[31] = 0;
4816 regs.edx = (unsigned long)res;
4817 rc = x86_emulate(&ctxt, &emulops);
4818 if ( rc != X86EMUL_OKAY || !check_eip(vfpclassps) )
4819 goto fail;
4820 asm volatile ( "kmovw %%k2, %0" : "=g" (rc) );
4821 if ( rc != 0xbdef )
4822 goto fail;
4823 printf("okay\n");
4824 }
4825 else
4826 printf("skipped\n");
4827
4828 printf("%-40s", "Testing vfpclassphz $0x46,128(%ecx),%k3...");
4829 if ( stack_exec && cpu_has_avx512_fp16 )
4830 {
4831 decl_insn(vfpclassph);
4832
4833 asm volatile ( put_insn(vfpclassph,
4834 /* 0x46: check for +/- 0 and neg. */
4835 /* vfpclassphz $0x46, 128(%0), %%k3 */
4836 ".byte 0x62, 0xf3, 0x7c, 0x48\n\t"
4837 ".byte 0x66, 0x59, 0x02, 0x46")
4838 :: "c" (NULL) );
4839
4840 set_insn(vfpclassph);
4841 for ( i = 0; i < 3; ++i )
4842 {
4843 res[16 + i * 5 + 0] = 0x7fff0000; /* +0 / +NaN */
4844 res[16 + i * 5 + 1] = 0xffff8000; /* -0 / -NaN */
4845 res[16 + i * 5 + 2] = 0x80010001; /* +DEN / -DEN */
4846 res[16 + i * 5 + 3] = 0xfc00f800; /* -FIN / -INF */
4847 res[16 + i * 5 + 4] = 0x7c007800; /* +FIN / +INF */
4848 }
4849 res[31] = 0;
4850 regs.ecx = (unsigned long)res - 64;
4851 rc = x86_emulate(&ctxt, &emulops);
4852 if ( rc != X86EMUL_OKAY || !check_eip(vfpclassph) )
4853 goto fail;
4854 asm volatile ( "kmovd %%k3, %0" : "=g" (rc) );
4855 /*
4856 * 0b11(0001100101)*3
4857 * 0b1100_0110_0101_0001_1001_0100_0110_0101
4858 */
4859 if ( rc != 0xc6519465 )
4860 goto fail;
4861 printf("okay\n");
4862 }
4863 else
4864 printf("skipped\n");
4865
4866 /*
4867 * The following compress/expand tests are not only making sure the
4868 * accessed data is correct, but they also verify (by placing operands
4869 * on the mapping boundaries) that elements controlled by clear mask
4870 * bits don't get accessed.
4871 */
4872 if ( stack_exec && cpu_has_avx512f )
4873 {
4874 decl_insn(vpcompressd);
4875 decl_insn(vpcompressq);
4876 decl_insn(vpexpandd);
4877 decl_insn(vpexpandq);
4878 static const struct {
4879 unsigned int d[16];
4880 } dsrc = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } };
4881 static const struct {
4882 unsigned long long q[8];
4883 } qsrc = { { 0, 1, 2, 3, 4, 5, 6, 7 } };
4884 unsigned int *ptr = res + MMAP_SZ / sizeof(*res) - 32;
4885
4886 printf("%-40s", "Testing vpcompressd %zmm1,24*4(%ecx){%k2}...");
4887 asm volatile ( "kmovw %1, %%k2\n\t"
4888 "vmovdqu32 %2, %%zmm1\n"
4889 put_insn(vpcompressd,
4890 "vpcompressd %%zmm1, 24*4(%0)%{%%k2%}")
4891 :: "c" (NULL), "r" (0x55aa), "m" (dsrc) );
4892
4893 memset(ptr, 0xdb, 32 * 4);
4894 set_insn(vpcompressd);
4895 regs.ecx = (unsigned long)ptr;
4896 rc = x86_emulate(&ctxt, &emulops);
4897 if ( rc != X86EMUL_OKAY || !check_eip(vpcompressd) ||
4898 memcmp(ptr, ptr + 8, 16 * 4) )
4899 goto fail;
4900 for ( i = 0; i < 4; ++i )
4901 if ( ptr[24 + i] != 2 * i + 1 )
4902 goto fail;
4903 for ( ; i < 8; ++i )
4904 if ( ptr[24 + i] != 2 * i )
4905 goto fail;
4906 printf("okay\n");
4907
4908 printf("%-40s", "Testing vpexpandd 8*4(%edx),%zmm3{%k2}{z}...");
4909 asm volatile ( "vpternlogd $0x81, %%zmm3, %%zmm3, %%zmm3\n"
4910 put_insn(vpexpandd,
4911 "vpexpandd 8*4(%0), %%zmm3%{%%k2%}%{z%}")
4912 :: "d" (NULL) );
4913 set_insn(vpexpandd);
4914 regs.edx = (unsigned long)(ptr + 16);
4915 rc = x86_emulate(&ctxt, &emulops);
4916 if ( rc != X86EMUL_OKAY || !check_eip(vpexpandd) )
4917 goto fail;
4918 asm ( "vmovdqa32 %%zmm1, %%zmm2%{%%k2%}%{z%}\n\t"
4919 "vpcmpeqd %%zmm2, %%zmm3, %%k0\n\t"
4920 "kmovw %%k0, %0"
4921 : "=r" (rc) );
4922 if ( rc != 0xffff )
4923 goto fail;
4924 printf("okay\n");
4925
4926 printf("%-40s", "Testing vpcompressq %zmm4,12*8(%edx){%k3}...");
4927 asm volatile ( "kmovw %1, %%k3\n\t"
4928 "vmovdqu64 %2, %%zmm4\n"
4929 put_insn(vpcompressq,
4930 "vpcompressq %%zmm4, 12*8(%0)%{%%k3%}")
4931 :: "d" (NULL), "r" (0x5a), "m" (qsrc) );
4932
4933 memset(ptr, 0xdb, 16 * 8);
4934 set_insn(vpcompressq);
4935 regs.edx = (unsigned long)ptr;
4936 rc = x86_emulate(&ctxt, &emulops);
4937 if ( rc != X86EMUL_OKAY || !check_eip(vpcompressq) ||
4938 memcmp(ptr, ptr + 8, 8 * 8) )
4939 goto fail;
4940 for ( i = 0; i < 2; ++i )
4941 {
4942 if ( ptr[(12 + i) * 2] != 2 * i + 1 ||
4943 ptr[(12 + i) * 2 + 1] )
4944 goto fail;
4945 }
4946 for ( ; i < 4; ++i )
4947 {
4948 if ( ptr[(12 + i) * 2] != 2 * i ||
4949 ptr[(12 + i) * 2 + 1] )
4950 goto fail;
4951 }
4952 printf("okay\n");
4953
4954 printf("%-40s", "Testing vpexpandq 4*8(%ecx),%zmm5{%k3}{z}...");
4955 asm volatile ( "vpternlogq $0x81, %%zmm5, %%zmm5, %%zmm5\n"
4956 put_insn(vpexpandq,
4957 "vpexpandq 4*8(%0), %%zmm5%{%%k3%}%{z%}")
4958 :: "c" (NULL) );
4959 set_insn(vpexpandq);
4960 regs.ecx = (unsigned long)(ptr + 16);
4961 rc = x86_emulate(&ctxt, &emulops);
4962 if ( rc != X86EMUL_OKAY || !check_eip(vpexpandq) )
4963 goto fail;
4964 asm ( "vmovdqa64 %%zmm4, %%zmm6%{%%k3%}%{z%}\n\t"
4965 "vpcmpeqq %%zmm5, %%zmm6, %%k0\n\t"
4966 "kmovw %%k0, %0"
4967 : "=r" (rc) );
4968 if ( rc != 0xff )
4969 goto fail;
4970 printf("okay\n");
4971 }
4972
4973 #if __GNUC__ > 7 /* can't check for __AVX512VBMI2__ here */
4974 if ( stack_exec && cpu_has_avx512_vbmi2 )
4975 {
4976 decl_insn(vpcompressb);
4977 decl_insn(vpcompressw);
4978 decl_insn(vpexpandb);
4979 decl_insn(vpexpandw);
4980 static const struct {
4981 unsigned char b[64];
4982 } bsrc = { { 0, 1, 2, 3, 4, 5, 6, 7,
4983 8, 9, 10, 11, 12, 13, 14, 15,
4984 16, 17, 18, 19, 20, 21, 22, 23,
4985 24, 25, 26, 27, 28, 29, 30, 31,
4986 32, 33, 34, 35, 36, 37, 38, 39,
4987 40, 41, 42, 43, 44, 45, 46, 47,
4988 48, 49, 50, 51, 52, 53, 54, 55,
4989 56, 57, 58, 59, 60, 61, 62, 63 } };
4990 static const struct {
4991 unsigned short w[32];
4992 } wsrc = { { 0, 1, 2, 3, 4, 5, 6, 7,
4993 8, 9, 10, 11, 12, 13, 14, 15,
4994 16, 17, 18, 19, 20, 21, 22, 23,
4995 24, 25, 26, 27, 28, 29, 30, 31 } };
4996 unsigned char *ptr = (void *)res + MMAP_SZ - 128;
4997 unsigned long long w = 0x55555555aaaaaaaaULL;
4998
4999 printf("%-40s", "Testing vpcompressb %zmm1,96*1(%ecx){%k2}...");
5000 asm volatile ( "kmovq %1, %%k2\n\t"
5001 "vmovdqu8 %2, %%zmm1\n"
5002 put_insn(vpcompressb,
5003 "vpcompressb %%zmm1, 96*1(%0)%{%%k2%}")
5004 :: "c" (NULL), "m" (w), "m" (bsrc) );
5005
5006 memset(ptr, 0xdb, 128 * 1);
5007 set_insn(vpcompressb);
5008 regs.ecx = (unsigned long)ptr;
5009 rc = x86_emulate(&ctxt, &emulops);
5010 if ( rc != X86EMUL_OKAY || !check_eip(vpcompressb) ||
5011 memcmp(ptr, ptr + 32, 64 * 1) )
5012 goto fail;
5013 for ( i = 0; i < 16; ++i )
5014 if ( ptr[96 + i] != 2 * i + 1 )
5015 goto fail;
5016 for ( ; i < 32; ++i )
5017 if ( ptr[96 + i] != 2 * i )
5018 goto fail;
5019 printf("okay\n");
5020
5021 printf("%-40s", "Testing vpexpandb 32*1(%edx),%zmm3{%k2}{z}...");
5022 asm volatile ( "vpternlogd $0x81, %%zmm3, %%zmm3, %%zmm3\n"
5023 put_insn(vpexpandb,
5024 "vpexpandb 32*1(%0), %%zmm3%{%%k2%}%{z%}")
5025 :: "d" (NULL) );
5026 set_insn(vpexpandb);
5027 regs.edx = (unsigned long)(ptr + 64);
5028 rc = x86_emulate(&ctxt, &emulops);
5029 if ( rc != X86EMUL_OKAY || !check_eip(vpexpandb) )
5030 goto fail;
5031 asm ( "vmovdqu8 %%zmm1, %%zmm2%{%%k2%}%{z%}\n\t"
5032 "vpcmpeqb %%zmm2, %%zmm3, %%k0\n\t"
5033 "kmovq %%k0, %0"
5034 : "=m" (w) );
5035 if ( w != 0xffffffffffffffffULL )
5036 goto fail;
5037 printf("okay\n");
5038
5039 printf("%-40s", "Testing vpcompressw %zmm4,48*2(%edx){%k3}...");
5040 asm volatile ( "kmovd %1, %%k3\n\t"
5041 "vmovdqu16 %2, %%zmm4\n"
5042 put_insn(vpcompressw,
5043 "vpcompressw %%zmm4, 48*2(%0)%{%%k3%}")
5044 :: "d" (NULL), "r" (0x5555aaaa), "m" (wsrc) );
5045
5046 memset(ptr, 0xdb, 64 * 2);
5047 set_insn(vpcompressw);
5048 regs.edx = (unsigned long)ptr;
5049 rc = x86_emulate(&ctxt, &emulops);
5050 if ( rc != X86EMUL_OKAY || !check_eip(vpcompressw) ||
5051 memcmp(ptr, ptr + 32, 32 * 2) )
5052 goto fail;
5053 for ( i = 0; i < 8; ++i )
5054 {
5055 if ( ptr[(48 + i) * 2] != 2 * i + 1 ||
5056 ptr[(48 + i) * 2 + 1] )
5057 goto fail;
5058 }
5059 for ( ; i < 16; ++i )
5060 {
5061 if ( ptr[(48 + i) * 2] != 2 * i ||
5062 ptr[(48 + i) * 2 + 1] )
5063 goto fail;
5064 }
5065 printf("okay\n");
5066
5067 printf("%-40s", "Testing vpexpandw 16*2(%ecx),%zmm5{%k3}{z}...");
5068 asm volatile ( "vpternlogd $0x81, %%zmm5, %%zmm5, %%zmm5\n"
5069 put_insn(vpexpandw,
5070 "vpexpandw 16*2(%0), %%zmm5%{%%k3%}%{z%}")
5071 :: "c" (NULL) );
5072 set_insn(vpexpandw);
5073 regs.ecx = (unsigned long)(ptr + 64);
5074 rc = x86_emulate(&ctxt, &emulops);
5075 if ( rc != X86EMUL_OKAY || !check_eip(vpexpandw) )
5076 goto fail;
5077 asm ( "vmovdqu16 %%zmm4, %%zmm6%{%%k3%}%{z%}\n\t"
5078 "vpcmpeqw %%zmm5, %%zmm6, %%k0\n\t"
5079 "kmovq %%k0, %0"
5080 : "=m" (w) );
5081 if ( w != 0xffffffff )
5082 goto fail;
5083 printf("okay\n");
5084 }
5085 #endif
5086
5087 if ( stack_exec && cpu_has_avx512_bf16 )
5088 {
5089 decl_insn(vcvtne2ps2bf16);
5090 decl_insn(vcvtneps2bf16);
5091 decl_insn(vdpbf16ps);
5092 static const struct {
5093 float f[16];
5094 } in1 = {{
5095 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
5096 }}, in2 = {{
5097 1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15, -16
5098 }}, out = {{
5099 1 * 1 + 2 * 2, 3 * 3 + 4 * 4,
5100 5 * 5 + 6 * 6, 7 * 7 + 8 * 8,
5101 9 * 9 + 10 * 10, 11 * 11 + 12 * 12,
5102 13 * 13 + 14 * 14, 15 * 15 + 16 * 16,
5103 1 * 1 - 2 * 2, 3 * 3 - 4 * 4,
5104 5 * 5 - 6 * 6, 7 * 7 - 8 * 8,
5105 9 * 9 - 10 * 10, 11 * 11 - 12 * 12,
5106 13 * 13 - 14 * 14, 15 * 15 - 16 * 16
5107 }};
5108
5109 printf("%-40s", "Testing vcvtne2ps2bf16 64(%ecx),%zmm1,%zmm2...");
5110 asm volatile ( "vmovups %1, %%zmm1\n"
5111 put_insn(vcvtne2ps2bf16,
5112 /* vcvtne2ps2bf16 64(%0), %%zmm1, %%zmm2 */
5113 ".byte 0x62, 0xf2, 0x77, 0x48, 0x72, 0x51, 0x01")
5114 :: "c" (NULL), "m" (in2) );
5115 set_insn(vcvtne2ps2bf16);
5116 regs.ecx = (unsigned long)&in1 - 64;
5117 rc = x86_emulate(&ctxt, &emulops);
5118 if ( rc != X86EMUL_OKAY || !check_eip(vcvtne2ps2bf16) )
5119 goto fail;
5120 printf("pending\n");
5121
5122 printf("%-40s", "Testing vcvtneps2bf16 64(%ecx),%ymm3...");
5123 asm volatile ( put_insn(vcvtneps2bf16,
5124 /* vcvtneps2bf16 64(%0), %%ymm3 */
5125 ".byte 0x62, 0xf2, 0x7e, 0x48, 0x72, 0x59, 0x01")
5126 :: "c" (NULL) );
5127 set_insn(vcvtneps2bf16);
5128 rc = x86_emulate(&ctxt, &emulops);
5129 if ( rc != X86EMUL_OKAY || !check_eip(vcvtneps2bf16) )
5130 goto fail;
5131 asm ( "vmovdqa %%ymm2, %%ymm5\n\t"
5132 "vpcmpeqd %%zmm3, %%zmm5, %%k0\n\t"
5133 "kmovw %%k0, %0"
5134 : "=g" (rc) : "m" (out) );
5135 if ( rc != 0xffff )
5136 goto fail;
5137 printf("pending\n");
5138
5139 printf("%-40s", "Testing vdpbf16ps 128(%ecx),%zmm2,%zmm4...");
5140 asm volatile ( "vmovdqa %%ymm3, %0\n\t"
5141 "vmovdqa %%ymm3, %1\n"
5142 put_insn(vdpbf16ps,
5143 /* vdpbf16ps 128(%2), %%zmm2, %%zmm4 */
5144 ".byte 0x62, 0xf2, 0x6e, 0x48, 0x52, 0x61, 0x02")
5145 : "=&m" (res[0]), "=&m" (res[8])
5146 : "c" (NULL)
5147 : "memory" );
5148 set_insn(vdpbf16ps);
5149 regs.ecx = (unsigned long)res - 128;
5150 rc = x86_emulate(&ctxt, &emulops);
5151 if ( rc != X86EMUL_OKAY || !check_eip(vdpbf16ps) )
5152 goto fail;
5153 asm ( "vcmpeqps %1, %%zmm4, %%k0\n\t"
5154 "kmovw %%k0, %0"
5155 : "=g" (rc) : "m" (out) );
5156 if ( rc != 0xffff )
5157 goto fail;
5158 printf("okay\n");
5159 }
5160
5161 printf("%-40s", "Testing vpdpwssd (%ecx),%{y,z}mmA,%{y,z}mmB...");
5162 if ( stack_exec && cpu_has_avx512_vnni && cpu_has_avx_vnni )
5163 {
5164 /* Do the same operation two ways and compare the results. */
5165 decl_insn(vpdpwssd_vex1);
5166 decl_insn(vpdpwssd_vex2);
5167 decl_insn(vpdpwssd_evex);
5168
5169 for ( i = 0; i < 24; ++i )
5170 res[i] = i | (~i << 16);
5171
5172 asm volatile ( "vmovdqu32 32(%0), %%zmm1\n\t"
5173 "vextracti64x4 $1, %%zmm1, %%ymm2\n\t"
5174 "vpxor %%xmm0, %%xmm0, %%xmm3\n\t"
5175 "vpxor %%xmm0, %%xmm0, %%xmm4\n\t"
5176 "vpxor %%xmm0, %%xmm0, %%xmm5\n"
5177 put_insn(vpdpwssd_vex1,
5178 /* %{vex%} vpdpwssd (%1), %%ymm1, %%ymm3" */
5179 ".byte 0xc4, 0xe2, 0x75, 0x52, 0x19") "\n"
5180 put_insn(vpdpwssd_vex2,
5181 /* "%{vex%} vpdpwssd 32(%1), %%ymm2, %%ymm4" */
5182 ".byte 0xc4, 0xe2, 0x6d, 0x52, 0x61, 0x20") "\n"
5183 put_insn(vpdpwssd_evex,
5184 /* "vpdpwssd (%1), %%zmm1, %%zmm5" */
5185 ".byte 0x62, 0xf2, 0x75, 0x48, 0x52, 0x29")
5186 :: "r" (res), "c" (NULL) );
5187
5188 set_insn(vpdpwssd_vex1);
5189 regs.ecx = (unsigned long)res;
5190 rc = x86_emulate(&ctxt, &emulops);
5191 if ( rc != X86EMUL_OKAY || !check_eip(vpdpwssd_vex1) )
5192 goto fail;
5193
5194 set_insn(vpdpwssd_vex2);
5195 regs.ecx = (unsigned long)res;
5196 rc = x86_emulate(&ctxt, &emulops);
5197 if ( rc != X86EMUL_OKAY || !check_eip(vpdpwssd_vex2) )
5198 goto fail;
5199
5200 set_insn(vpdpwssd_evex);
5201 regs.ecx = (unsigned long)res;
5202 rc = x86_emulate(&ctxt, &emulops);
5203 if ( rc != X86EMUL_OKAY || !check_eip(vpdpwssd_evex) )
5204 goto fail;
5205
5206 asm ( "vinserti64x4 $1, %%ymm4, %%zmm3, %%zmm0\n\t"
5207 "vpcmpeqd %%zmm0, %%zmm5, %%k0\n\t"
5208 "kmovw %%k0, %0" : "=g" (rc) );
5209 if ( rc != 0xffff )
5210 goto fail;
5211 printf("okay\n");
5212 }
5213 else
5214 printf("skipped\n");
5215
5216 printf("%-40s", "Testing vmovsh 8(%ecx),%xmm5...");
5217 if ( stack_exec && cpu_has_avx512_fp16 )
5218 {
5219 decl_insn(vmovsh_from_mem);
5220 decl_insn(vmovw_to_gpr);
5221
5222 asm volatile ( "vpcmpeqw %%ymm5, %%ymm5, %%ymm5\n\t"
5223 put_insn(vmovsh_from_mem,
5224 /* vmovsh 8(%0), %%xmm5 */
5225 ".byte 0x62, 0xf5, 0x7e, 0x08\n\t"
5226 ".byte 0x10, 0x69, 0x04")
5227 :: "c" (NULL) );
5228
5229 set_insn(vmovsh_from_mem);
5230 res[2] = 0x3c00bc00;
5231 regs.ecx = (unsigned long)res;
5232 rc = x86_emulate(&ctxt, &emulops);
5233 if ( (rc != X86EMUL_OKAY) || !check_eip(vmovsh_from_mem) )
5234 goto fail;
5235 asm volatile ( "kmovw %2, %%k1\n\t"
5236 "vmovdqu16 %1, %%zmm4%{%%k1%}%{z%}\n\t"
5237 "vpcmpeqw %%zmm4, %%zmm5, %%k0\n\t"
5238 "kmovw %%k0, %0"
5239 : "=g" (rc)
5240 : "m" (res[2]), "r" (1) );
5241 if ( rc != 0xffff )
5242 goto fail;
5243 printf("okay\n");
5244
5245 printf("%-40s", "Testing vmovsh %xmm4,2(%eax){%k3}...");
5246 memset(res, ~0, 8);
5247 res[2] = 0xbc00ffff;
5248 memset(res + 3, ~0, 8);
5249 regs.eax = (unsigned long)res;
5250 regs.ecx = ~0;
5251 for ( i = 0; i < 2; ++i )
5252 {
5253 decl_insn(vmovsh_to_mem);
5254
5255 asm volatile ( "kmovw %1, %%k3\n\t"
5256 put_insn(vmovsh_to_mem,
5257 /* vmovsh %%xmm4, 2(%0)%{%%k3%} */
5258 ".byte 0x62, 0xf5, 0x7e, 0x0b\n\t"
5259 ".byte 0x11, 0x60, 0x01")
5260 :: "a" (NULL), "r" (i) );
5261
5262 set_insn(vmovsh_to_mem);
5263 rc = x86_emulate(&ctxt, &emulops);
5264 if ( (rc != X86EMUL_OKAY) || !check_eip(vmovsh_to_mem) ||
5265 memcmp(res, res + 3 - i, 8) )
5266 goto fail;
5267 }
5268 printf("okay\n");
5269
5270 printf("%-40s", "Testing vmovw %xmm5,%ecx...");
5271 asm volatile ( put_insn(vmovw_to_gpr,
5272 /* vmovw %%xmm5, %0 */
5273 ".byte 0x62, 0xf5, 0x7d, 0x08\n\t"
5274 ".byte 0x7e, 0xe9")
5275 :: "c" (NULL) );
5276 set_insn(vmovw_to_gpr);
5277 rc = x86_emulate(&ctxt, &emulops);
5278 if ( (rc != X86EMUL_OKAY) || !check_eip(vmovw_to_gpr) ||
5279 regs.ecx != 0xbc00 )
5280 goto fail;
5281 printf("okay\n");
5282 }
5283 else
5284 printf("skipped\n");
5285
5286 printf("%-40s", "Testing invpcid 16(%ecx),%%edx...");
5287 if ( stack_exec )
5288 {
5289 decl_insn(invpcid);
5290
5291 asm volatile ( put_insn(invpcid, "invpcid 16(%0), %1")
5292 :: "c" (NULL), "d" (0L) );
5293
5294 res[4] = 0;
5295 res[5] = 0;
5296 res[6] = INVPCID_ADDR;
5297 res[7] = 0;
5298 regs.ecx = (unsigned long)res;
5299 emulops.tlb_op = tlb_op_invpcid;
5300
5301 for ( ; ; )
5302 {
5303 for ( regs.edx = 0; regs.edx < 4; ++regs.edx )
5304 {
5305 set_insn(invpcid);
5306 rc = x86_emulate(&ctxt, &emulops);
5307 if ( rc != X86EMUL_OKAY || !check_eip(invpcid) )
5308 goto fail;
5309 }
5310
5311 if ( ctxt.addr_size < 64 || res[4] == INVPCID_PCID )
5312 break;
5313
5314 emulops.read_cr = read_cr_invpcid;
5315 res[4] = INVPCID_PCID;
5316 }
5317
5318 emulops.read_cr = emul_test_read_cr;
5319 emulops.tlb_op = NULL;
5320
5321 printf("okay\n");
5322 }
5323 else
5324 printf("skipped\n");
5325
5326 #undef decl_insn
5327 #undef put_insn
5328 #undef set_insn
5329 #undef check_eip
5330
5331 j = cache_line_size();
5332 snprintf(instr, (char *)res + MMAP_SZ - instr,
5333 "Testing clzero (%u-byte line)...", j);
5334 printf("%-40s", instr);
5335 if ( j >= sizeof(*res) && j <= MMAP_SZ / 4 )
5336 {
5337 instr[0] = 0x0f; instr[1] = 0x01; instr[2] = 0xfc;
5338 regs.eflags = 0x200;
5339 regs.eip = (unsigned long)&instr[0];
5340 regs.eax = (unsigned long)res + MMAP_SZ / 2 + j - 1;
5341 memset((void *)res + MMAP_SZ / 4, ~0, 3 * MMAP_SZ / 4);
5342 rc = x86_emulate(&ctxt, &emulops);
5343 if ( (rc != X86EMUL_OKAY) ||
5344 (regs.eax != (unsigned long)res + MMAP_SZ / 2 + j - 1) ||
5345 (regs.eflags != 0x200) ||
5346 (regs.eip != (unsigned long)&instr[3]) ||
5347 (res[MMAP_SZ / 2 / sizeof(*res) - 1] != ~0U) ||
5348 (res[(MMAP_SZ / 2 + j) / sizeof(*res)] != ~0U) )
5349 goto fail;
5350 for ( i = 0; i < j; i += sizeof(*res) )
5351 if ( res[(MMAP_SZ / 2 + i) / sizeof(*res)] )
5352 break;
5353 if ( i < j )
5354 goto fail;
5355 printf("okay\n");
5356 }
5357 else
5358 printf("skipped\n");
5359
5360 if ( stack_exec )
5361 evex_disp8_test(instr, &ctxt, &emulops);
5362
5363 predicates_test(instr, &ctxt, fetch);
5364
5365 for ( j = 0; j < ARRAY_SIZE(blobs); j++ )
5366 {
5367 unsigned int nr;
5368
5369 if ( blobs[j].check_cpu && !blobs[j].check_cpu() )
5370 continue;
5371
5372 if ( !blobs[j].size )
5373 {
5374 printf("%-39s n/a (%u-bit)\n", blobs[j].name, blobs[j].bitness);
5375 continue;
5376 }
5377
5378 memcpy(res, blobs[j].code, blobs[j].size);
5379 ctxt.lma = blobs[j].bitness == 64;
5380 ctxt.addr_size = ctxt.sp_size = blobs[j].bitness;
5381
5382 if ( ctxt.addr_size == sizeof(void *) * CHAR_BIT )
5383 {
5384 nr = printf("Testing %s native execution...", blobs[j].name);
5385
5386 if ( blobs[j].set_regs )
5387 blobs[j].set_regs(&regs);
5388 asm volatile (
5389 #if defined(__i386__)
5390 "call *%%ecx"
5391 #else
5392 "call *%%rcx"
5393 #endif
5394 : "+a" (regs.eax), "+d" (regs.edx) : "c" (res)
5395 #ifdef __x86_64__
5396 : "rsi", "rdi", "r8", "r9", "r10", "r11"
5397 #endif
5398 );
5399 if ( !blobs[j].check_regs(&regs) )
5400 goto fail;
5401
5402 printf("%*sokay\n", nr < 40 ? 40 - nr : 0, "");
5403 }
5404
5405 nr = printf("Testing %s %u-bit code sequence",
5406 blobs[j].name, ctxt.addr_size);
5407
5408 if ( blobs[j].set_regs )
5409 blobs[j].set_regs(&regs);
5410 regs.eip = (unsigned long)res;
5411 regs.esp = (unsigned long)res + MMAP_SZ - 4;
5412 if ( ctxt.addr_size == 64 )
5413 {
5414 *(uint32_t *)(unsigned long)regs.esp = 0;
5415 regs.esp -= 4;
5416 }
5417 *(uint32_t *)(unsigned long)regs.esp = 0x12345678;
5418 regs.eflags = 2;
5419 i = 0;
5420 while ( regs.eip >= (unsigned long)res &&
5421 regs.eip < (unsigned long)res + blobs[j].size )
5422 {
5423 if ( (i++ & 8191) == 0 )
5424 {
5425 printf(".");
5426 ++nr;
5427 }
5428 rc = x86_emulate(&ctxt, &emulops);
5429 if ( rc != X86EMUL_OKAY )
5430 {
5431 printf("failed (%d) at %%eip == %08lx (opcode %08x)\n",
5432 rc, (unsigned long)regs.eip, ctxt.opcode);
5433 return 1;
5434 }
5435 }
5436 for ( ; i < 2 * 8192; i += 8192 ) {
5437 printf(".");
5438 ++nr;
5439 }
5440
5441 if ( (regs.eip != 0x12345678) ||
5442 (regs.esp != ((unsigned long)res + MMAP_SZ)) ||
5443 !blobs[j].check_regs(&regs) )
5444 goto fail;
5445
5446 printf("%*sokay\n", nr < 40 ? 40 - nr : 0, "");
5447 }
5448
5449 return 0;
5450
5451 fail:
5452 printf("failed!\n");
5453 return 1;
5454 }
5455
5456 /*
5457 * Local variables:
5458 * mode: C
5459 * c-file-style: "BSD"
5460 * c-basic-offset: 4
5461 * indent-tabs-mode: nil
5462 * End:
5463 */