ia64/xen-unstable

view tools/tests/test_x86_emulator.c @ 15841:c5f735271e22

[IA64] Foreign p2m: Fix vti domain builder.

It should set arch_domain::convmem_end.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Alex Williamson <alex.williamson@hp.com>
date Thu Sep 06 13:48:43 2007 -0600 (2007-09-06)
parents b010e556fe2c
children f1a107ec62b6
line source
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdint.h>
6 typedef uint8_t u8;
7 typedef uint16_t u16;
8 typedef uint32_t u32;
9 typedef uint64_t u64;
10 typedef int8_t s8;
11 typedef int16_t s16;
12 typedef int32_t s32;
13 typedef int64_t s64;
14 #include <public/xen.h>
15 #include <asm-x86/x86_emulate.h>
16 #include <sys/mman.h>
18 #include "blowfish.h"
20 #define MMAP_SZ 16384
22 /* EFLAGS bit definitions. */
23 #define EFLG_OF (1<<11)
24 #define EFLG_DF (1<<10)
25 #define EFLG_SF (1<<7)
26 #define EFLG_ZF (1<<6)
27 #define EFLG_AF (1<<4)
28 #define EFLG_PF (1<<2)
29 #define EFLG_CF (1<<0)
31 static int read(
32 unsigned int seg,
33 unsigned long offset,
34 unsigned long *val,
35 unsigned int bytes,
36 struct x86_emulate_ctxt *ctxt)
37 {
38 unsigned long addr = offset;
39 switch ( bytes )
40 {
41 case 1: *val = *(u8 *)addr; break;
42 case 2: *val = *(u16 *)addr; break;
43 case 4: *val = *(u32 *)addr; break;
44 case 8: *val = *(unsigned long *)addr; break;
45 }
46 return X86EMUL_OKAY;
47 }
49 static int write(
50 unsigned int seg,
51 unsigned long offset,
52 unsigned long val,
53 unsigned int bytes,
54 struct x86_emulate_ctxt *ctxt)
55 {
56 unsigned long addr = offset;
57 switch ( bytes )
58 {
59 case 1: *(u8 *)addr = (u8)val; break;
60 case 2: *(u16 *)addr = (u16)val; break;
61 case 4: *(u32 *)addr = (u32)val; break;
62 case 8: *(unsigned long *)addr = val; break;
63 }
64 return X86EMUL_OKAY;
65 }
67 static int cmpxchg(
68 unsigned int seg,
69 unsigned long offset,
70 unsigned long old,
71 unsigned long new,
72 unsigned int bytes,
73 struct x86_emulate_ctxt *ctxt)
74 {
75 unsigned long addr = offset;
76 switch ( bytes )
77 {
78 case 1: *(u8 *)addr = (u8)new; break;
79 case 2: *(u16 *)addr = (u16)new; break;
80 case 4: *(u32 *)addr = (u32)new; break;
81 case 8: *(unsigned long *)addr = new; break;
82 }
83 return X86EMUL_OKAY;
84 }
86 static int cmpxchg8b(
87 unsigned int seg,
88 unsigned long offset,
89 unsigned long old_lo,
90 unsigned long old_hi,
91 unsigned long new_lo,
92 unsigned long new_hi,
93 struct x86_emulate_ctxt *ctxt)
94 {
95 unsigned long addr = offset;
96 ((unsigned long *)addr)[0] = new_lo;
97 ((unsigned long *)addr)[1] = new_hi;
98 return X86EMUL_OKAY;
99 }
101 static struct x86_emulate_ops emulops = {
102 .read = read,
103 .insn_fetch = read,
104 .write = write,
105 .cmpxchg = cmpxchg,
106 .cmpxchg8b = cmpxchg8b
107 };
109 int main(int argc, char **argv)
110 {
111 struct x86_emulate_ctxt ctxt;
112 struct cpu_user_regs regs;
113 char *instr;
114 unsigned int *res, i;
115 int rc;
116 #ifndef __x86_64__
117 unsigned int bcdres_native, bcdres_emul;
118 #endif
120 ctxt.regs = &regs;
121 ctxt.addr_size = 32;
122 ctxt.sp_size = 32;
124 res = mmap((void *)0x100000, MMAP_SZ, PROT_READ|PROT_WRITE,
125 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
126 if ( res == MAP_FAILED )
127 {
128 fprintf(stderr, "mmap to low address failed\n");
129 exit(1);
130 }
131 instr = (char *)res + 0x100;
133 printf("%-40s", "Testing addl %%ecx,(%%eax)...");
134 instr[0] = 0x01; instr[1] = 0x08;
135 regs.eflags = 0x200;
136 regs.eip = (unsigned long)&instr[0];
137 regs.ecx = 0x12345678;
138 regs.eax = (unsigned long)res;
139 *res = 0x7FFFFFFF;
140 rc = x86_emulate(&ctxt, &emulops);
141 if ( (rc != X86EMUL_OKAY) ||
142 (*res != 0x92345677) ||
143 (regs.eflags != 0xa94) ||
144 (regs.eip != (unsigned long)&instr[2]) )
145 goto fail;
146 printf("okay\n");
148 printf("%-40s", "Testing addl %%ecx,%%eax...");
149 instr[0] = 0x01; instr[1] = 0xc8;
150 regs.eflags = 0x200;
151 regs.eip = (unsigned long)&instr[0];
152 regs.ecx = 0x12345678;
153 regs.eax = 0x7FFFFFFF;
154 rc = x86_emulate(&ctxt, &emulops);
155 if ( (rc != X86EMUL_OKAY) ||
156 (regs.ecx != 0x12345678) ||
157 (regs.eax != 0x92345677) ||
158 (regs.eflags != 0xa94) ||
159 (regs.eip != (unsigned long)&instr[2]) )
160 goto fail;
161 printf("okay\n");
163 printf("%-40s", "Testing xorl (%%eax),%%ecx...");
164 instr[0] = 0x33; instr[1] = 0x08;
165 regs.eflags = 0x200;
166 regs.eip = (unsigned long)&instr[0];
167 #ifdef __x86_64__
168 regs.ecx = 0xFFFFFFFF12345678UL;
169 #else
170 regs.ecx = 0x12345678UL;
171 #endif
172 regs.eax = (unsigned long)res;
173 rc = x86_emulate(&ctxt, &emulops);
174 if ( (rc != X86EMUL_OKAY) ||
175 (*res != 0x92345677) ||
176 (regs.ecx != 0x8000000FUL) ||
177 (regs.eip != (unsigned long)&instr[2]) )
178 goto fail;
179 printf("okay\n");
181 printf("%-40s", "Testing movl (%%eax),%%ecx...");
182 instr[0] = 0x8b; instr[1] = 0x08;
183 regs.eflags = 0x200;
184 regs.eip = (unsigned long)&instr[0];
185 regs.ecx = ~0UL;
186 regs.eax = (unsigned long)res;
187 rc = x86_emulate(&ctxt, &emulops);
188 if ( (rc != X86EMUL_OKAY) ||
189 (*res != 0x92345677) ||
190 (regs.ecx != 0x92345677UL) ||
191 (regs.eip != (unsigned long)&instr[2]) )
192 goto fail;
193 printf("okay\n");
195 printf("%-40s", "Testing lock cmpxchgb %%cl,(%%ebx)...");
196 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb0; instr[3] = 0x0b;
197 regs.eflags = 0x200;
198 regs.eip = (unsigned long)&instr[0];
199 regs.eax = 0x92345677UL;
200 regs.ecx = 0xAA;
201 regs.ebx = (unsigned long)res;
202 rc = x86_emulate(&ctxt, &emulops);
203 if ( (rc != X86EMUL_OKAY) ||
204 (*res != 0x923456AA) ||
205 (regs.eflags != 0x244) ||
206 (regs.eax != 0x92345677UL) ||
207 (regs.eip != (unsigned long)&instr[4]) )
208 goto fail;
209 printf("okay\n");
211 printf("%-40s", "Testing lock cmpxchgb %%cl,(%%ebx)...");
212 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb0; instr[3] = 0x0b;
213 regs.eflags = 0x200;
214 regs.eip = (unsigned long)&instr[0];
215 regs.eax = 0xAABBCC77UL;
216 regs.ecx = 0xFF;
217 regs.ebx = (unsigned long)res;
218 rc = x86_emulate(&ctxt, &emulops);
219 if ( (rc != X86EMUL_OKAY) ||
220 (*res != 0x923456AA) ||
221 ((regs.eflags&0x240) != 0x200) ||
222 (regs.eax != 0xAABBCCAA) ||
223 (regs.ecx != 0xFF) ||
224 (regs.eip != (unsigned long)&instr[4]) )
225 goto fail;
226 printf("okay\n");
228 printf("%-40s", "Testing xchgl %%ecx,(%%eax)...");
229 instr[0] = 0x87; instr[1] = 0x08;
230 regs.eflags = 0x200;
231 regs.eip = (unsigned long)&instr[0];
232 regs.ecx = 0x12345678;
233 regs.eax = (unsigned long)res;
234 rc = x86_emulate(&ctxt, &emulops);
235 if ( (rc != X86EMUL_OKAY) ||
236 (*res != 0x12345678) ||
237 (regs.eflags != 0x200) ||
238 (regs.ecx != 0x923456AA) ||
239 (regs.eip != (unsigned long)&instr[2]) )
240 goto fail;
241 printf("okay\n");
243 printf("%-40s", "Testing lock cmpxchgl %%ecx,(%%ebx)...");
244 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb1; instr[3] = 0x0b;
245 regs.eflags = 0x200;
246 *res = 0x923456AA;
247 regs.eip = (unsigned long)&instr[0];
248 regs.eax = 0x923456AAUL;
249 regs.ecx = 0xDDEEFF00L;
250 regs.ebx = (unsigned long)res;
251 rc = x86_emulate(&ctxt, &emulops);
252 if ( (rc != X86EMUL_OKAY) ||
253 (*res != 0xDDEEFF00) ||
254 (regs.eflags != 0x244) ||
255 (regs.eax != 0x923456AAUL) ||
256 (regs.eip != (unsigned long)&instr[4]) )
257 goto fail;
258 printf("okay\n");
260 printf("%-40s", "Testing rep movsw...");
261 instr[0] = 0xf3; instr[1] = 0x66; instr[2] = 0xa5;
262 *res = 0x22334455;
263 regs.eflags = 0x200;
264 regs.ecx = 23;
265 regs.eip = (unsigned long)&instr[0];
266 regs.esi = (unsigned long)res + 0;
267 regs.edi = (unsigned long)res + 2;
268 rc = x86_emulate(&ctxt, &emulops);
269 if ( (rc != X86EMUL_OKAY) ||
270 (*res != 0x44554455) ||
271 (regs.eflags != 0x200) ||
272 (regs.ecx != 22) ||
273 (regs.esi != ((unsigned long)res + 2)) ||
274 (regs.edi != ((unsigned long)res + 4)) ||
275 (regs.eip != (unsigned long)&instr[0]) )
276 goto fail;
277 printf("okay\n");
279 printf("%-40s", "Testing btrl $0x1,(%edi)...");
280 instr[0] = 0x0f; instr[1] = 0xba; instr[2] = 0x37; instr[3] = 0x01;
281 *res = 0x2233445F;
282 regs.eflags = 0x200;
283 regs.eip = (unsigned long)&instr[0];
284 regs.edi = (unsigned long)res;
285 rc = x86_emulate(&ctxt, &emulops);
286 if ( (rc != X86EMUL_OKAY) ||
287 (*res != 0x2233445D) ||
288 ((regs.eflags&0x201) != 0x201) ||
289 (regs.eip != (unsigned long)&instr[4]) )
290 goto fail;
291 printf("okay\n");
293 printf("%-40s", "Testing btrl %eax,(%edi)...");
294 instr[0] = 0x0f; instr[1] = 0xb3; instr[2] = 0x07;
295 *res = 0x2233445F;
296 regs.eflags = 0x200;
297 regs.eip = (unsigned long)&instr[0];
298 regs.eax = -32;
299 regs.edi = (unsigned long)(res+1);
300 rc = x86_emulate(&ctxt, &emulops);
301 if ( (rc != X86EMUL_OKAY) ||
302 (*res != 0x2233445E) ||
303 ((regs.eflags&0x201) != 0x201) ||
304 (regs.eip != (unsigned long)&instr[3]) )
305 goto fail;
306 printf("okay\n");
308 res[0] = 0x12345678;
309 res[1] = 0x87654321;
311 printf("%-40s", "Testing cmpxchg8b (%edi) [succeeding]...");
312 instr[0] = 0x0f; instr[1] = 0xc7; instr[2] = 0x0f;
313 regs.eflags = 0x200;
314 regs.eax = res[0];
315 regs.edx = res[1];
316 regs.ebx = 0x9999AAAA;
317 regs.ecx = 0xCCCCFFFF;
318 regs.eip = (unsigned long)&instr[0];
319 regs.edi = (unsigned long)res;
320 rc = x86_emulate(&ctxt, &emulops);
321 if ( (rc != X86EMUL_OKAY) ||
322 (res[0] != 0x9999AAAA) ||
323 (res[1] != 0xCCCCFFFF) ||
324 ((regs.eflags&0x240) != 0x240) ||
325 (regs.eip != (unsigned long)&instr[3]) )
326 goto fail;
327 printf("okay\n");
329 printf("%-40s", "Testing cmpxchg8b (%edi) [failing]...");
330 instr[0] = 0x0f; instr[1] = 0xc7; instr[2] = 0x0f;
331 regs.eflags = 0x200;
332 regs.eip = (unsigned long)&instr[0];
333 regs.edi = (unsigned long)res;
334 rc = x86_emulate(&ctxt, &emulops);
335 if ( (rc != X86EMUL_OKAY) ||
336 (res[0] != 0x9999AAAA) ||
337 (res[1] != 0xCCCCFFFF) ||
338 (regs.eax != 0x9999AAAA) ||
339 (regs.edx != 0xCCCCFFFF) ||
340 ((regs.eflags&0x240) != 0x200) ||
341 (regs.eip != (unsigned long)&instr[3]) )
342 goto fail;
343 printf("okay\n");
345 printf("%-40s", "Testing movsxbd (%%eax),%%ecx...");
346 instr[0] = 0x0f; instr[1] = 0xbe; instr[2] = 0x08;
347 regs.eflags = 0x200;
348 regs.eip = (unsigned long)&instr[0];
349 regs.ecx = 0x12345678;
350 regs.eax = (unsigned long)res;
351 *res = 0x82;
352 rc = x86_emulate(&ctxt, &emulops);
353 if ( (rc != X86EMUL_OKAY) ||
354 (*res != 0x82) ||
355 (regs.ecx != 0xFFFFFF82) ||
356 ((regs.eflags&0x240) != 0x200) ||
357 (regs.eip != (unsigned long)&instr[3]) )
358 goto fail;
359 printf("okay\n");
361 printf("%-40s", "Testing movzxwd (%%eax),%%ecx...");
362 instr[0] = 0x0f; instr[1] = 0xb7; instr[2] = 0x08;
363 regs.eflags = 0x200;
364 regs.eip = (unsigned long)&instr[0];
365 regs.ecx = 0x12345678;
366 regs.eax = (unsigned long)res;
367 *res = 0x1234aa82;
368 rc = x86_emulate(&ctxt, &emulops);
369 if ( (rc != X86EMUL_OKAY) ||
370 (*res != 0x1234aa82) ||
371 (regs.ecx != 0xaa82) ||
372 ((regs.eflags&0x240) != 0x200) ||
373 (regs.eip != (unsigned long)&instr[3]) )
374 goto fail;
375 printf("okay\n");
377 printf("%-40s", "Testing xadd %%ax,(%%ecx)...");
378 instr[0] = 0x66; instr[1] = 0x0f; instr[2] = 0xc1; instr[3] = 0x01;
379 regs.eflags = 0x200;
380 regs.eip = (unsigned long)&instr[0];
381 regs.ecx = (unsigned long)res;
382 regs.eax = 0x12345678;
383 *res = 0x11111111;
384 rc = x86_emulate(&ctxt, &emulops);
385 if ( (rc != X86EMUL_OKAY) ||
386 (*res != 0x11116789) ||
387 (regs.eax != 0x12341111) ||
388 ((regs.eflags&0x240) != 0x200) ||
389 (regs.eip != (unsigned long)&instr[4]) )
390 goto fail;
391 printf("okay\n");
393 printf("%-40s", "Testing dec %%ax...");
394 instr[0] = 0x66; instr[1] = 0x48;
395 regs.eflags = 0x200;
396 regs.eip = (unsigned long)&instr[0];
397 regs.eax = 0x00000000;
398 rc = x86_emulate(&ctxt, &emulops);
399 if ( (rc != X86EMUL_OKAY) ||
400 (regs.eax != 0x0000ffff) ||
401 ((regs.eflags&0x240) != 0x200) ||
402 (regs.eip != (unsigned long)&instr[2]) )
403 goto fail;
404 printf("okay\n");
406 printf("%-40s", "Testing lea 8(%%ebp),%%eax...");
407 instr[0] = 0x8d; instr[1] = 0x45; instr[2] = 0x08;
408 regs.eflags = 0x200;
409 regs.eip = (unsigned long)&instr[0];
410 regs.eax = 0x12345678;
411 regs.ebp = 0xaaaaaaaa;
412 rc = x86_emulate(&ctxt, &emulops);
413 if ( (rc != X86EMUL_OKAY) ||
414 (regs.eax != 0xaaaaaab2) ||
415 ((regs.eflags&0x240) != 0x200) ||
416 (regs.eip != (unsigned long)&instr[3]) )
417 goto fail;
418 printf("okay\n");
420 printf("%-40s", "Testing daa/das (all inputs)...");
421 #ifndef __x86_64__
422 /* Bits 0-7: AL; Bit 8: EFLG_AF; Bit 9: EFLG_CF; Bit 10: DAA vs. DAS. */
423 for ( i = 0; i < 0x800; i++ )
424 {
425 regs.eflags = (i & 0x200) ? EFLG_CF : 0;
426 regs.eflags |= (i & 0x100) ? EFLG_AF : 0;
427 if ( i & 0x400 )
428 __asm__ (
429 "pushf; and $0xffffffee,(%%esp); or %1,(%%esp); popf; das; "
430 "pushf; popl %1"
431 : "=a" (bcdres_native), "=r" (regs.eflags)
432 : "0" (i & 0xff), "1" (regs.eflags) );
433 else
434 __asm__ (
435 "pushf; and $0xffffffee,(%%esp); or %1,(%%esp); popf; daa; "
436 "pushf; popl %1"
437 : "=a" (bcdres_native), "=r" (regs.eflags)
438 : "0" (i & 0xff), "1" (regs.eflags) );
439 bcdres_native |= (regs.eflags & EFLG_PF) ? 0x1000 : 0;
440 bcdres_native |= (regs.eflags & EFLG_ZF) ? 0x800 : 0;
441 bcdres_native |= (regs.eflags & EFLG_SF) ? 0x400 : 0;
442 bcdres_native |= (regs.eflags & EFLG_CF) ? 0x200 : 0;
443 bcdres_native |= (regs.eflags & EFLG_AF) ? 0x100 : 0;
445 instr[0] = (i & 0x400) ? 0x2f: 0x27; /* daa/das */
446 regs.eflags = (i & 0x200) ? EFLG_CF : 0;
447 regs.eflags |= (i & 0x100) ? EFLG_AF : 0;
448 regs.eip = (unsigned long)&instr[0];
449 regs.eax = (unsigned char)i;
450 rc = x86_emulate(&ctxt, &emulops);
451 bcdres_emul = regs.eax;
452 bcdres_emul |= (regs.eflags & EFLG_PF) ? 0x1000 : 0;
453 bcdres_emul |= (regs.eflags & EFLG_ZF) ? 0x800 : 0;
454 bcdres_emul |= (regs.eflags & EFLG_SF) ? 0x400 : 0;
455 bcdres_emul |= (regs.eflags & EFLG_CF) ? 0x200 : 0;
456 bcdres_emul |= (regs.eflags & EFLG_AF) ? 0x100 : 0;
457 if ( (rc != X86EMUL_OKAY) || (regs.eax > 255) ||
458 (regs.eip != (unsigned long)&instr[1]) )
459 goto fail;
461 if ( bcdres_emul != bcdres_native )
462 {
463 printf("%s: AL=%02x %s %s\n"
464 "Output: AL=%02x %s %s %s %s %s\n"
465 "Emul.: AL=%02x %s %s %s %s %s\n",
466 (i & 0x400) ? "DAS" : "DAA",
467 (unsigned char)i,
468 (i & 0x200) ? "CF" : " ",
469 (i & 0x100) ? "AF" : " ",
470 (unsigned char)bcdres_native,
471 (bcdres_native & 0x200) ? "CF" : " ",
472 (bcdres_native & 0x100) ? "AF" : " ",
473 (bcdres_native & 0x1000) ? "PF" : " ",
474 (bcdres_native & 0x800) ? "ZF" : " ",
475 (bcdres_native & 0x400) ? "SF" : " ",
476 (unsigned char)bcdres_emul,
477 (bcdres_emul & 0x200) ? "CF" : " ",
478 (bcdres_emul & 0x100) ? "AF" : " ",
479 (bcdres_emul & 0x1000) ? "PF" : " ",
480 (bcdres_emul & 0x800) ? "ZF" : " ",
481 (bcdres_emul & 0x400) ? "SF" : " ");
482 goto fail;
483 }
484 }
485 printf("okay\n");
486 #else
487 printf("skipped\n");
488 #endif
490 printf("Testing blowfish code sequence");
491 memcpy(res, blowfish_code, sizeof(blowfish_code));
492 regs.eax = 2;
493 regs.edx = 1;
494 regs.eip = (unsigned long)res;
495 regs.esp = (unsigned long)res + MMAP_SZ - 4;
496 *(uint32_t *)(unsigned long)regs.esp = 0x12345678;
497 regs.eflags = 2;
498 i = 0;
499 while ( (uint32_t)regs.eip != 0x12345678 )
500 {
501 if ( (i++ & 8191) == 0 )
502 printf(".");
503 rc = x86_emulate(&ctxt, &emulops);
504 if ( rc != X86EMUL_OKAY )
505 {
506 printf("failed at %%eip == %08x\n", (unsigned int)regs.eip);
507 return 1;
508 }
509 }
510 if ( (regs.esp != ((unsigned long)res + MMAP_SZ)) ||
511 (regs.eax != 2) || (regs.edx != 1) )
512 goto fail;
513 printf("okay\n");
515 #ifndef __x86_64__
516 printf("%-40s", "Testing blowfish native execution...");
517 asm volatile (
518 "movl $0x100000,%%ecx; call *%%ecx"
519 : "=a" (regs.eax), "=d" (regs.edx)
520 : "0" (2), "1" (1) : "ecx" );
521 if ( (regs.eax != 2) || (regs.edx != 1) )
522 goto fail;
523 printf("okay\n");
524 #endif
526 return 0;
528 fail:
529 printf("failed!\n");
530 return 1;
531 }